blob: ca819d8720972249882ed30b1ac8bc3d3f210dcf [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 |
166 FH49_RCSR_CHNL0_RX_CONFIG_SINGLE_FRAME_MSK | rb_size | (rb_timeout
167 <<
168 FH49_RCSR_RX_CONFIG_REG_IRQ_RBTH_POS)
169 | (rfdnlog << FH49_RCSR_RX_CONFIG_RBDCB_SIZE_POS));
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200170
171 /* Set interrupt coalescing timer to default (2048 usecs) */
172 il_write8(il, CSR_INT_COALESCING, IL_HOST_INT_TIMEOUT_DEF);
173
174 return 0;
175}
176
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100177static void
178il4965_set_pwr_vmain(struct il_priv *il)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200179{
180/*
181 * (for documentation purposes)
182 * to set power to V_AUX, do:
183
184 if (pci_pme_capable(il->pci_dev, PCI_D3cold))
185 il_set_bits_mask_prph(il, APMG_PS_CTRL_REG,
186 APMG_PS_CTRL_VAL_PWR_SRC_VAUX,
187 ~APMG_PS_CTRL_MSK_PWR_SRC);
188 */
189
190 il_set_bits_mask_prph(il, APMG_PS_CTRL_REG,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100191 APMG_PS_CTRL_VAL_PWR_SRC_VMAIN,
192 ~APMG_PS_CTRL_MSK_PWR_SRC);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200193}
194
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100195int
196il4965_hw_nic_init(struct il_priv *il)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200197{
198 unsigned long flags;
199 struct il_rx_queue *rxq = &il->rxq;
200 int ret;
201
202 /* nic_init */
203 spin_lock_irqsave(&il->lock, flags);
204 il->cfg->ops->lib->apm_ops.init(il);
205
206 /* Set interrupt coalescing calibration timer to default (512 usecs) */
207 il_write8(il, CSR_INT_COALESCING, IL_HOST_INT_CALIB_TIMEOUT_DEF);
208
209 spin_unlock_irqrestore(&il->lock, flags);
210
211 il4965_set_pwr_vmain(il);
212
213 il->cfg->ops->lib->apm_ops.config(il);
214
215 /* Allocate the RX queue, or reset if it is already allocated */
216 if (!rxq->bd) {
217 ret = il_rx_queue_alloc(il);
218 if (ret) {
219 IL_ERR("Unable to initialize Rx queue\n");
220 return -ENOMEM;
221 }
222 } else
223 il4965_rx_queue_reset(il, rxq);
224
225 il4965_rx_replenish(il);
226
227 il4965_rx_init(il, rxq);
228
229 spin_lock_irqsave(&il->lock, flags);
230
231 rxq->need_update = 1;
232 il_rx_queue_update_write_ptr(il, rxq);
233
234 spin_unlock_irqrestore(&il->lock, flags);
235
236 /* Allocate or reset and init all Tx and Command queues */
237 if (!il->txq) {
238 ret = il4965_txq_ctx_alloc(il);
239 if (ret)
240 return ret;
241 } else
242 il4965_txq_ctx_reset(il);
243
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +0100244 set_bit(S_INIT, &il->status);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200245
246 return 0;
247}
248
249/**
250 * il4965_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer ptr
251 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100252static inline __le32
253il4965_dma_addr2rbd_ptr(struct il_priv *il, dma_addr_t dma_addr)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200254{
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100255 return cpu_to_le32((u32) (dma_addr >> 8));
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200256}
257
258/**
259 * il4965_rx_queue_restock - refill RX queue from pre-allocated pool
260 *
261 * If there are slots in the RX queue that need to be restocked,
262 * and we have free pre-allocated buffers, fill the ranks as much
263 * as we can, pulling from rx_free.
264 *
265 * This moves the 'write' idx forward to catch up with 'processed', and
266 * also updates the memory address in the firmware to reference the new
267 * target buffer.
268 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100269void
270il4965_rx_queue_restock(struct il_priv *il)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200271{
272 struct il_rx_queue *rxq = &il->rxq;
273 struct list_head *element;
274 struct il_rx_buf *rxb;
275 unsigned long flags;
276
277 spin_lock_irqsave(&rxq->lock, flags);
278 while (il_rx_queue_space(rxq) > 0 && rxq->free_count) {
279 /* The overwritten rxb must be a used one */
280 rxb = rxq->queue[rxq->write];
281 BUG_ON(rxb && rxb->page);
282
283 /* Get next free Rx buffer, remove from free list */
284 element = rxq->rx_free.next;
285 rxb = list_entry(element, struct il_rx_buf, list);
286 list_del(element);
287
288 /* Point to Rx buffer via next RBD in circular buffer */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100289 rxq->bd[rxq->write] =
290 il4965_dma_addr2rbd_ptr(il, rxb->page_dma);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200291 rxq->queue[rxq->write] = rxb;
292 rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
293 rxq->free_count--;
294 }
295 spin_unlock_irqrestore(&rxq->lock, flags);
296 /* If the pre-allocated buffer pool is dropping low, schedule to
297 * refill it */
298 if (rxq->free_count <= RX_LOW_WATERMARK)
299 queue_work(il->workqueue, &il->rx_replenish);
300
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200301 /* If we've added more space for the firmware to place data, tell it.
302 * Increment device's write pointer in multiples of 8. */
303 if (rxq->write_actual != (rxq->write & ~0x7)) {
304 spin_lock_irqsave(&rxq->lock, flags);
305 rxq->need_update = 1;
306 spin_unlock_irqrestore(&rxq->lock, flags);
307 il_rx_queue_update_write_ptr(il, rxq);
308 }
309}
310
311/**
312 * il4965_rx_replenish - Move all used packet from rx_used to rx_free
313 *
314 * When moving to rx_free an SKB is allocated for the slot.
315 *
316 * Also restock the Rx queue via il_rx_queue_restock.
317 * This is called as a scheduled work item (except for during initialization)
318 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100319static void
320il4965_rx_allocate(struct il_priv *il, gfp_t priority)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200321{
322 struct il_rx_queue *rxq = &il->rxq;
323 struct list_head *element;
324 struct il_rx_buf *rxb;
325 struct page *page;
326 unsigned long flags;
327 gfp_t gfp_mask = priority;
328
329 while (1) {
330 spin_lock_irqsave(&rxq->lock, flags);
331 if (list_empty(&rxq->rx_used)) {
332 spin_unlock_irqrestore(&rxq->lock, flags);
333 return;
334 }
335 spin_unlock_irqrestore(&rxq->lock, flags);
336
337 if (rxq->free_count > RX_LOW_WATERMARK)
338 gfp_mask |= __GFP_NOWARN;
339
340 if (il->hw_params.rx_page_order > 0)
341 gfp_mask |= __GFP_COMP;
342
343 /* Alloc a new receive buffer */
344 page = alloc_pages(gfp_mask, il->hw_params.rx_page_order);
345 if (!page) {
346 if (net_ratelimit())
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100347 D_INFO("alloc_pages failed, " "order: %d\n",
348 il->hw_params.rx_page_order);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200349
350 if (rxq->free_count <= RX_LOW_WATERMARK &&
351 net_ratelimit())
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100352 IL_ERR("Failed to alloc_pages with %s. "
353 "Only %u free buffers remaining.\n",
354 priority ==
355 GFP_ATOMIC ? "GFP_ATOMIC" : "GFP_KERNEL",
356 rxq->free_count);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200357 /* We don't reschedule replenish work here -- we will
358 * call the restock method and if it still needs
359 * more buffers it will schedule replenish */
360 return;
361 }
362
363 spin_lock_irqsave(&rxq->lock, flags);
364
365 if (list_empty(&rxq->rx_used)) {
366 spin_unlock_irqrestore(&rxq->lock, flags);
367 __free_pages(page, il->hw_params.rx_page_order);
368 return;
369 }
370 element = rxq->rx_used.next;
371 rxb = list_entry(element, struct il_rx_buf, list);
372 list_del(element);
373
374 spin_unlock_irqrestore(&rxq->lock, flags);
375
376 BUG_ON(rxb->page);
377 rxb->page = page;
378 /* Get physical address of the RB */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100379 rxb->page_dma =
380 pci_map_page(il->pci_dev, page, 0,
381 PAGE_SIZE << il->hw_params.rx_page_order,
382 PCI_DMA_FROMDEVICE);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200383 /* dma address must be no more than 36 bits */
384 BUG_ON(rxb->page_dma & ~DMA_BIT_MASK(36));
385 /* and also 256 byte aligned! */
386 BUG_ON(rxb->page_dma & DMA_BIT_MASK(8));
387
388 spin_lock_irqsave(&rxq->lock, flags);
389
390 list_add_tail(&rxb->list, &rxq->rx_free);
391 rxq->free_count++;
392 il->alloc_rxb_page++;
393
394 spin_unlock_irqrestore(&rxq->lock, flags);
395 }
396}
397
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100398void
399il4965_rx_replenish(struct il_priv *il)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200400{
401 unsigned long flags;
402
403 il4965_rx_allocate(il, GFP_KERNEL);
404
405 spin_lock_irqsave(&il->lock, flags);
406 il4965_rx_queue_restock(il);
407 spin_unlock_irqrestore(&il->lock, flags);
408}
409
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100410void
411il4965_rx_replenish_now(struct il_priv *il)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200412{
413 il4965_rx_allocate(il, GFP_ATOMIC);
414
415 il4965_rx_queue_restock(il);
416}
417
418/* Assumes that the skb field of the buffers in 'pool' is kept accurate.
419 * If an SKB has been detached, the POOL needs to have its SKB set to NULL
420 * This free routine walks the list of POOL entries and if SKB is set to
421 * non NULL it is unmapped and freed
422 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100423void
424il4965_rx_queue_free(struct il_priv *il, struct il_rx_queue *rxq)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200425{
426 int i;
427 for (i = 0; i < RX_QUEUE_SIZE + RX_FREE_BUFFERS; i++) {
428 if (rxq->pool[i].page != NULL) {
429 pci_unmap_page(il->pci_dev, rxq->pool[i].page_dma,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100430 PAGE_SIZE << il->hw_params.rx_page_order,
431 PCI_DMA_FROMDEVICE);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200432 __il_free_pages(il, rxq->pool[i].page);
433 rxq->pool[i].page = NULL;
434 }
435 }
436
437 dma_free_coherent(&il->pci_dev->dev, 4 * RX_QUEUE_SIZE, rxq->bd,
438 rxq->bd_dma);
439 dma_free_coherent(&il->pci_dev->dev, sizeof(struct il_rb_status),
440 rxq->rb_stts, rxq->rb_stts_dma);
441 rxq->bd = NULL;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100442 rxq->rb_stts = NULL;
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200443}
444
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100445int
446il4965_rxq_stop(struct il_priv *il)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200447{
448
449 /* stop Rx DMA */
Stanislaw Gruszka9a95b372011-08-31 14:20:23 +0200450 il_wr(il, FH49_MEM_RCSR_CHNL0_CONFIG_REG, 0);
451 il_poll_bit(il, FH49_MEM_RSSR_RX_STATUS_REG,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100452 FH49_RSSR_CHNL0_RX_STATUS_CHNL_IDLE, 1000);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200453
454 return 0;
455}
456
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100457int
458il4965_hwrate_to_mac80211_idx(u32 rate_n_flags, enum ieee80211_band band)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200459{
460 int idx = 0;
461 int band_offset = 0;
462
463 /* HT rate format: mac80211 wants an MCS number, which is just LSB */
464 if (rate_n_flags & RATE_MCS_HT_MSK) {
465 idx = (rate_n_flags & 0xff);
466 return idx;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100467 /* Legacy rate format, search for match in table */
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200468 } else {
469 if (band == IEEE80211_BAND_5GHZ)
470 band_offset = IL_FIRST_OFDM_RATE;
471 for (idx = band_offset; idx < RATE_COUNT_LEGACY; idx++)
472 if (il_rates[idx].plcp == (rate_n_flags & 0xFF))
473 return idx - band_offset;
474 }
475
476 return -1;
477}
478
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100479static int
480il4965_calc_rssi(struct il_priv *il, struct il_rx_phy_res *rx_resp)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200481{
482 /* data from PHY/DSP regarding signal strength, etc.,
483 * contents are always there, not configurable by host. */
484 struct il4965_rx_non_cfg_phy *ncphy =
485 (struct il4965_rx_non_cfg_phy *)rx_resp->non_cfg_phy_buf;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100486 u32 agc =
487 (le16_to_cpu(ncphy->agc_info) & IL49_AGC_DB_MASK) >>
488 IL49_AGC_DB_POS;
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200489
490 u32 valid_antennae =
491 (le16_to_cpu(rx_resp->phy_flags) & IL49_RX_PHY_FLAGS_ANTENNAE_MASK)
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100492 >> IL49_RX_PHY_FLAGS_ANTENNAE_OFFSET;
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200493 u8 max_rssi = 0;
494 u32 i;
495
496 /* Find max rssi among 3 possible receivers.
497 * These values are measured by the digital signal processor (DSP).
498 * They should stay fairly constant even as the signal strength varies,
499 * if the radio's automatic gain control (AGC) is working right.
500 * AGC value (see below) will provide the "interesting" info. */
501 for (i = 0; i < 3; i++)
502 if (valid_antennae & (1 << i))
503 max_rssi = max(ncphy->rssi_info[i << 1], max_rssi);
504
505 D_STATS("Rssi In A %d B %d C %d Max %d AGC dB %d\n",
506 ncphy->rssi_info[0], ncphy->rssi_info[2], ncphy->rssi_info[4],
507 max_rssi, agc);
508
509 /* dBm = max_rssi dB - agc dB - constant.
510 * Higher AGC (higher radio gain) means lower signal. */
511 return max_rssi - agc - IL4965_RSSI_OFFSET;
512}
513
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100514static u32
515il4965_translate_rx_status(struct il_priv *il, u32 decrypt_in)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200516{
517 u32 decrypt_out = 0;
518
519 if ((decrypt_in & RX_RES_STATUS_STATION_FOUND) ==
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100520 RX_RES_STATUS_STATION_FOUND)
521 decrypt_out |=
522 (RX_RES_STATUS_STATION_FOUND |
523 RX_RES_STATUS_NO_STATION_INFO_MISMATCH);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200524
525 decrypt_out |= (decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK);
526
527 /* packet was not encrypted */
528 if ((decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) ==
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100529 RX_RES_STATUS_SEC_TYPE_NONE)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200530 return decrypt_out;
531
532 /* packet was encrypted with unknown alg */
533 if ((decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) ==
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100534 RX_RES_STATUS_SEC_TYPE_ERR)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200535 return decrypt_out;
536
537 /* decryption was not done in HW */
538 if ((decrypt_in & RX_MPDU_RES_STATUS_DEC_DONE_MSK) !=
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100539 RX_MPDU_RES_STATUS_DEC_DONE_MSK)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200540 return decrypt_out;
541
542 switch (decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) {
543
544 case RX_RES_STATUS_SEC_TYPE_CCMP:
545 /* alg is CCM: check MIC only */
546 if (!(decrypt_in & RX_MPDU_RES_STATUS_MIC_OK))
547 /* Bad MIC */
548 decrypt_out |= RX_RES_STATUS_BAD_ICV_MIC;
549 else
550 decrypt_out |= RX_RES_STATUS_DECRYPT_OK;
551
552 break;
553
554 case RX_RES_STATUS_SEC_TYPE_TKIP:
555 if (!(decrypt_in & RX_MPDU_RES_STATUS_TTAK_OK)) {
556 /* Bad TTAK */
557 decrypt_out |= RX_RES_STATUS_BAD_KEY_TTAK;
558 break;
559 }
560 /* fall through if TTAK OK */
561 default:
562 if (!(decrypt_in & RX_MPDU_RES_STATUS_ICV_OK))
563 decrypt_out |= RX_RES_STATUS_BAD_ICV_MIC;
564 else
565 decrypt_out |= RX_RES_STATUS_DECRYPT_OK;
566 break;
567 }
568
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100569 D_RX("decrypt_in:0x%x decrypt_out = 0x%x\n", decrypt_in, decrypt_out);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200570
571 return decrypt_out;
572}
573
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100574static void
575il4965_pass_packet_to_mac80211(struct il_priv *il, struct ieee80211_hdr *hdr,
576 u16 len, u32 ampdu_status, struct il_rx_buf *rxb,
577 struct ieee80211_rx_status *stats)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200578{
579 struct sk_buff *skb;
580 __le16 fc = hdr->frame_control;
581
582 /* We only process data packets if the interface is open */
583 if (unlikely(!il->is_open)) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100584 D_DROP("Dropping packet while interface is not open.\n");
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200585 return;
586 }
587
588 /* In case of HW accelerated crypto and bad decryption, drop */
589 if (!il->cfg->mod_params->sw_crypto &&
590 il_set_decrypted_flag(il, hdr, ampdu_status, stats))
591 return;
592
593 skb = dev_alloc_skb(128);
594 if (!skb) {
595 IL_ERR("dev_alloc_skb failed\n");
596 return;
597 }
598
599 skb_add_rx_frag(skb, 0, rxb->page, (void *)hdr - rxb_addr(rxb), len);
600
601 il_update_stats(il, false, fc, len);
602 memcpy(IEEE80211_SKB_RXCB(skb), stats, sizeof(*stats));
603
604 ieee80211_rx(il->hw, skb);
605 il->alloc_rxb_page--;
606 rxb->page = NULL;
607}
608
Stanislaw Gruszka4d69c752011-08-30 15:26:35 +0200609/* Called for N_RX (legacy ABG frames), or
610 * N_RX_MPDU (HT high-throughput N frames). */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100611void
612il4965_hdl_rx(struct il_priv *il, struct il_rx_buf *rxb)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200613{
614 struct ieee80211_hdr *header;
615 struct ieee80211_rx_status rx_status;
616 struct il_rx_pkt *pkt = rxb_addr(rxb);
617 struct il_rx_phy_res *phy_res;
618 __le32 rx_pkt_status;
619 struct il_rx_mpdu_res_start *amsdu;
620 u32 len;
621 u32 ampdu_status;
622 u32 rate_n_flags;
623
624 /**
Stanislaw Gruszka4d69c752011-08-30 15:26:35 +0200625 * N_RX and N_RX_MPDU are handled differently.
626 * N_RX: physical layer info is in this buffer
627 * N_RX_MPDU: physical layer info was sent in separate
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200628 * command and cached in il->last_phy_res
629 *
630 * Here we set up local variables depending on which command is
631 * received.
632 */
Stanislaw Gruszka4d69c752011-08-30 15:26:35 +0200633 if (pkt->hdr.cmd == N_RX) {
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200634 phy_res = (struct il_rx_phy_res *)pkt->u.raw;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100635 header =
636 (struct ieee80211_hdr *)(pkt->u.raw + sizeof(*phy_res) +
637 phy_res->cfg_phy_cnt);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200638
639 len = le16_to_cpu(phy_res->byte_count);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100640 rx_pkt_status =
641 *(__le32 *) (pkt->u.raw + sizeof(*phy_res) +
642 phy_res->cfg_phy_cnt + len);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200643 ampdu_status = le32_to_cpu(rx_pkt_status);
644 } else {
645 if (!il->_4965.last_phy_res_valid) {
646 IL_ERR("MPDU frame without cached PHY data\n");
647 return;
648 }
649 phy_res = &il->_4965.last_phy_res;
650 amsdu = (struct il_rx_mpdu_res_start *)pkt->u.raw;
651 header = (struct ieee80211_hdr *)(pkt->u.raw + sizeof(*amsdu));
652 len = le16_to_cpu(amsdu->byte_count);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100653 rx_pkt_status = *(__le32 *) (pkt->u.raw + sizeof(*amsdu) + len);
654 ampdu_status =
655 il4965_translate_rx_status(il, le32_to_cpu(rx_pkt_status));
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200656 }
657
658 if ((unlikely(phy_res->cfg_phy_cnt > 20))) {
659 D_DROP("dsp size out of range [0,20]: %d/n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100660 phy_res->cfg_phy_cnt);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200661 return;
662 }
663
664 if (!(rx_pkt_status & RX_RES_STATUS_NO_CRC32_ERROR) ||
665 !(rx_pkt_status & RX_RES_STATUS_NO_RXE_OVERFLOW)) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100666 D_RX("Bad CRC or FIFO: 0x%08X.\n", le32_to_cpu(rx_pkt_status));
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200667 return;
668 }
669
670 /* This will be used in several places later */
671 rate_n_flags = le32_to_cpu(phy_res->rate_n_flags);
672
673 /* rx_status carries information about the packet to mac80211 */
674 rx_status.mactime = le64_to_cpu(phy_res->timestamp);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100675 rx_status.band =
676 (phy_res->
677 phy_flags & RX_RES_PHY_FLAGS_BAND_24_MSK) ? IEEE80211_BAND_2GHZ :
678 IEEE80211_BAND_5GHZ;
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200679 rx_status.freq =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100680 ieee80211_channel_to_frequency(le16_to_cpu(phy_res->channel),
681 rx_status.band);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200682 rx_status.rate_idx =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100683 il4965_hwrate_to_mac80211_idx(rate_n_flags, rx_status.band);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200684 rx_status.flag = 0;
685
686 /* TSF isn't reliable. In order to allow smooth user experience,
687 * this W/A doesn't propagate it to the mac80211 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100688 /*rx_status.flag |= RX_FLAG_MACTIME_MPDU; */
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200689
690 il->ucode_beacon_time = le32_to_cpu(phy_res->beacon_time_stamp);
691
692 /* Find max signal strength (dBm) among 3 antenna/receiver chains */
693 rx_status.signal = il4965_calc_rssi(il, phy_res);
694
695 il_dbg_log_rx_data_frame(il, len, header);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100696 D_STATS("Rssi %d, TSF %llu\n", rx_status.signal,
697 (unsigned long long)rx_status.mactime);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200698
699 /*
700 * "antenna number"
701 *
702 * It seems that the antenna field in the phy flags value
703 * is actually a bit field. This is undefined by radiotap,
704 * it wants an actual antenna number but I always get "7"
705 * for most legacy frames I receive indicating that the
706 * same frame was received on all three RX chains.
707 *
708 * I think this field should be removed in favor of a
709 * new 802.11n radiotap field "RX chains" that is defined
710 * as a bitmask.
711 */
712 rx_status.antenna =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100713 (le16_to_cpu(phy_res->phy_flags) & RX_RES_PHY_FLAGS_ANTENNA_MSK) >>
714 RX_RES_PHY_FLAGS_ANTENNA_POS;
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200715
716 /* set the preamble flag if appropriate */
717 if (phy_res->phy_flags & RX_RES_PHY_FLAGS_SHORT_PREAMBLE_MSK)
718 rx_status.flag |= RX_FLAG_SHORTPRE;
719
720 /* Set up the HT phy flags */
721 if (rate_n_flags & RATE_MCS_HT_MSK)
722 rx_status.flag |= RX_FLAG_HT;
723 if (rate_n_flags & RATE_MCS_HT40_MSK)
724 rx_status.flag |= RX_FLAG_40MHZ;
725 if (rate_n_flags & RATE_MCS_SGI_MSK)
726 rx_status.flag |= RX_FLAG_SHORT_GI;
727
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100728 il4965_pass_packet_to_mac80211(il, header, len, ampdu_status, rxb,
729 &rx_status);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200730}
731
Stanislaw Gruszka4d69c752011-08-30 15:26:35 +0200732/* Cache phy data (Rx signal strength, etc) for HT frame (N_RX_PHY).
Stanislaw Gruszka6e9848b42011-08-30 15:45:31 +0200733 * This will be used later in il_hdl_rx() for N_RX_MPDU. */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100734void
735il4965_hdl_rx_phy(struct il_priv *il, struct il_rx_buf *rxb)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200736{
737 struct il_rx_pkt *pkt = rxb_addr(rxb);
738 il->_4965.last_phy_res_valid = true;
739 memcpy(&il->_4965.last_phy_res, pkt->u.raw,
740 sizeof(struct il_rx_phy_res));
741}
742
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100743static int
744il4965_get_channels_for_scan(struct il_priv *il, struct ieee80211_vif *vif,
745 enum ieee80211_band band, u8 is_active,
746 u8 n_probes, struct il_scan_channel *scan_ch)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200747{
748 struct ieee80211_channel *chan;
749 const struct ieee80211_supported_band *sband;
750 const struct il_channel_info *ch_info;
751 u16 passive_dwell = 0;
752 u16 active_dwell = 0;
753 int added, i;
754 u16 channel;
755
756 sband = il_get_hw_mode(il, band);
757 if (!sband)
758 return 0;
759
760 active_dwell = il_get_active_dwell_time(il, band, n_probes);
761 passive_dwell = il_get_passive_dwell_time(il, band, vif);
762
763 if (passive_dwell <= active_dwell)
764 passive_dwell = active_dwell + 1;
765
766 for (i = 0, added = 0; i < il->scan_request->n_channels; i++) {
767 chan = il->scan_request->channels[i];
768
769 if (chan->band != band)
770 continue;
771
772 channel = chan->hw_value;
773 scan_ch->channel = cpu_to_le16(channel);
774
775 ch_info = il_get_channel_info(il, band, channel);
776 if (!il_is_channel_valid(ch_info)) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100777 D_SCAN("Channel %d is INVALID for this band.\n",
778 channel);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200779 continue;
780 }
781
782 if (!is_active || il_is_channel_passive(ch_info) ||
783 (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN))
784 scan_ch->type = SCAN_CHANNEL_TYPE_PASSIVE;
785 else
786 scan_ch->type = SCAN_CHANNEL_TYPE_ACTIVE;
787
788 if (n_probes)
789 scan_ch->type |= IL_SCAN_PROBE_MASK(n_probes);
790
791 scan_ch->active_dwell = cpu_to_le16(active_dwell);
792 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
793
794 /* Set txpower levels to defaults */
795 scan_ch->dsp_atten = 110;
796
797 /* NOTE: if we were doing 6Mb OFDM for scans we'd use
798 * power level:
799 * scan_ch->tx_gain = ((1 << 5) | (2 << 3)) | 3;
800 */
801 if (band == IEEE80211_BAND_5GHZ)
802 scan_ch->tx_gain = ((1 << 5) | (3 << 3)) | 3;
803 else
804 scan_ch->tx_gain = ((1 << 5) | (5 << 3));
805
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100806 D_SCAN("Scanning ch=%d prob=0x%X [%s %d]\n", channel,
807 le32_to_cpu(scan_ch->type),
808 (scan_ch->
809 type & SCAN_CHANNEL_TYPE_ACTIVE) ? "ACTIVE" : "PASSIVE",
810 (scan_ch->
811 type & SCAN_CHANNEL_TYPE_ACTIVE) ? active_dwell :
812 passive_dwell);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200813
814 scan_ch++;
815 added++;
816 }
817
818 D_SCAN("total channels to scan %d\n", added);
819 return added;
820}
821
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100822static inline u32
823il4965_ant_idx_to_flags(u8 ant_idx)
Stanislaw Gruszkaaf038f42011-08-30 13:58:27 +0200824{
825 return BIT(ant_idx) << RATE_MCS_ANT_POS;
826}
827
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100828int
829il4965_request_scan(struct il_priv *il, struct ieee80211_vif *vif)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200830{
831 struct il_host_cmd cmd = {
Stanislaw Gruszka4d69c752011-08-30 15:26:35 +0200832 .id = C_SCAN,
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200833 .len = sizeof(struct il_scan_cmd),
834 .flags = CMD_SIZE_HUGE,
835 };
836 struct il_scan_cmd *scan;
837 struct il_rxon_context *ctx = &il->ctx;
838 u32 rate_flags = 0;
839 u16 cmd_len;
840 u16 rx_chain = 0;
841 enum ieee80211_band band;
842 u8 n_probes = 0;
843 u8 rx_ant = il->hw_params.valid_rx_ant;
844 u8 rate;
845 bool is_active = false;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100846 int chan_mod;
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200847 u8 active_chains;
848 u8 scan_tx_antennas = il->hw_params.valid_tx_ant;
849 int ret;
850
851 lockdep_assert_held(&il->mutex);
852
853 if (vif)
854 ctx = il_rxon_ctx_from_vif(vif);
855
856 if (!il->scan_cmd) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100857 il->scan_cmd =
858 kmalloc(sizeof(struct il_scan_cmd) + IL_MAX_SCAN_SIZE,
859 GFP_KERNEL);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200860 if (!il->scan_cmd) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100861 D_SCAN("fail to allocate memory for scan\n");
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200862 return -ENOMEM;
863 }
864 }
865 scan = il->scan_cmd;
866 memset(scan, 0, sizeof(struct il_scan_cmd) + IL_MAX_SCAN_SIZE);
867
868 scan->quiet_plcp_th = IL_PLCP_QUIET_THRESH;
869 scan->quiet_time = IL_ACTIVE_QUIET_TIME;
870
871 if (il_is_any_associated(il)) {
872 u16 interval;
873 u32 extra;
874 u32 suspend_time = 100;
875 u32 scan_suspend_time = 100;
876
877 D_INFO("Scanning while associated...\n");
878 interval = vif->bss_conf.beacon_int;
879
880 scan->suspend_time = 0;
881 scan->max_out_time = cpu_to_le32(200 * 1024);
882 if (!interval)
883 interval = suspend_time;
884
885 extra = (suspend_time / interval) << 22;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100886 scan_suspend_time =
887 (extra | ((suspend_time % interval) * 1024));
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200888 scan->suspend_time = cpu_to_le32(scan_suspend_time);
889 D_SCAN("suspend_time 0x%X beacon interval %d\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100890 scan_suspend_time, interval);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200891 }
892
893 if (il->scan_request->n_ssids) {
894 int i, p = 0;
895 D_SCAN("Kicking off active scan\n");
896 for (i = 0; i < il->scan_request->n_ssids; i++) {
897 /* always does wildcard anyway */
898 if (!il->scan_request->ssids[i].ssid_len)
899 continue;
900 scan->direct_scan[p].id = WLAN_EID_SSID;
901 scan->direct_scan[p].len =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100902 il->scan_request->ssids[i].ssid_len;
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200903 memcpy(scan->direct_scan[p].ssid,
904 il->scan_request->ssids[i].ssid,
905 il->scan_request->ssids[i].ssid_len);
906 n_probes++;
907 p++;
908 }
909 is_active = true;
910 } else
911 D_SCAN("Start passive scan.\n");
912
913 scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
914 scan->tx_cmd.sta_id = ctx->bcast_sta_id;
915 scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
916
917 switch (il->scan_band) {
918 case IEEE80211_BAND_2GHZ:
919 scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100920 chan_mod =
921 le32_to_cpu(il->ctx.active.
922 flags & RXON_FLG_CHANNEL_MODE_MSK) >>
923 RXON_FLG_CHANNEL_MODE_POS;
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200924 if (chan_mod == CHANNEL_MODE_PURE_40) {
925 rate = RATE_6M_PLCP;
926 } else {
927 rate = RATE_1M_PLCP;
928 rate_flags = RATE_MCS_CCK_MSK;
929 }
930 break;
931 case IEEE80211_BAND_5GHZ:
932 rate = RATE_6M_PLCP;
933 break;
934 default:
935 IL_WARN("Invalid scan band\n");
936 return -EIO;
937 }
938
939 /*
940 * If active scanning is requested but a certain channel is
941 * marked passive, we can do active scanning if we detect
942 * transmissions.
943 *
944 * There is an issue with some firmware versions that triggers
945 * a sysassert on a "good CRC threshold" of zero (== disabled),
946 * on a radar channel even though this means that we should NOT
947 * send probes.
948 *
949 * The "good CRC threshold" is the number of frames that we
950 * need to receive during our dwell time on a channel before
951 * sending out probes -- setting this to a huge value will
952 * mean we never reach it, but at the same time work around
953 * the aforementioned issue. Thus use IL_GOOD_CRC_TH_NEVER
954 * here instead of IL_GOOD_CRC_TH_DISABLED.
955 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100956 scan->good_CRC_th =
957 is_active ? IL_GOOD_CRC_TH_DEFAULT : IL_GOOD_CRC_TH_NEVER;
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200958
959 band = il->scan_band;
960
961 if (il->cfg->scan_rx_antennas[band])
962 rx_ant = il->cfg->scan_rx_antennas[band];
963
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100964 il->scan_tx_ant[band] =
965 il4965_toggle_tx_ant(il, il->scan_tx_ant[band], scan_tx_antennas);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200966 rate_flags |= il4965_ant_idx_to_flags(il->scan_tx_ant[band]);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100967 scan->tx_cmd.rate_n_flags =
968 il4965_hw_set_rate_n_flags(rate, rate_flags);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200969
970 /* In power save mode use one chain, otherwise use all chains */
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +0100971 if (test_bit(S_POWER_PMI, &il->status)) {
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200972 /* rx_ant has been set to all valid chains previously */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100973 active_chains =
974 rx_ant & ((u8) (il->chain_noise_data.active_chains));
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200975 if (!active_chains)
976 active_chains = rx_ant;
977
978 D_SCAN("chain_noise_data.active_chains: %u\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100979 il->chain_noise_data.active_chains);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200980
981 rx_ant = il4965_first_antenna(active_chains);
982 }
983
984 /* MIMO is not used here, but value is required */
985 rx_chain |= il->hw_params.valid_rx_ant << RXON_RX_CHAIN_VALID_POS;
986 rx_chain |= rx_ant << RXON_RX_CHAIN_FORCE_MIMO_SEL_POS;
987 rx_chain |= rx_ant << RXON_RX_CHAIN_FORCE_SEL_POS;
988 rx_chain |= 0x1 << RXON_RX_CHAIN_DRIVER_FORCE_POS;
989 scan->rx_chain = cpu_to_le16(rx_chain);
990
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100991 cmd_len =
992 il_fill_probe_req(il, (struct ieee80211_mgmt *)scan->data,
993 vif->addr, il->scan_request->ie,
994 il->scan_request->ie_len,
995 IL_MAX_SCAN_SIZE - sizeof(*scan));
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +0200996 scan->tx_cmd.len = cpu_to_le16(cmd_len);
997
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100998 scan->filter_flags |=
999 (RXON_FILTER_ACCEPT_GRP_MSK | RXON_FILTER_BCON_AWARE_MSK);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001000
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001001 scan->channel_count =
1002 il4965_get_channels_for_scan(il, vif, band, is_active, n_probes,
1003 (void *)&scan->data[cmd_len]);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001004 if (scan->channel_count == 0) {
1005 D_SCAN("channel count %d\n", scan->channel_count);
1006 return -EIO;
1007 }
1008
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001009 cmd.len +=
1010 le16_to_cpu(scan->tx_cmd.len) +
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001011 scan->channel_count * sizeof(struct il_scan_channel);
1012 cmd.data = scan;
1013 scan->len = cpu_to_le16(cmd.len);
1014
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01001015 set_bit(S_SCAN_HW, &il->status);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001016
1017 ret = il_send_cmd_sync(il, &cmd);
1018 if (ret)
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01001019 clear_bit(S_SCAN_HW, &il->status);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001020
1021 return ret;
1022}
1023
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001024int
1025il4965_manage_ibss_station(struct il_priv *il, struct ieee80211_vif *vif,
1026 bool add)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001027{
1028 struct il_vif_priv *vif_priv = (void *)vif->drv_priv;
1029
1030 if (add)
1031 return il4965_add_bssid_station(il, vif_priv->ctx,
1032 vif->bss_conf.bssid,
1033 &vif_priv->ibss_bssid_sta_id);
1034 return il_remove_station(il, vif_priv->ibss_bssid_sta_id,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001035 vif->bss_conf.bssid);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001036}
1037
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001038void
1039il4965_free_tfds_in_queue(struct il_priv *il, int sta_id, int tid, int freed)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001040{
1041 lockdep_assert_held(&il->sta_lock);
1042
1043 if (il->stations[sta_id].tid[tid].tfds_in_queue >= freed)
1044 il->stations[sta_id].tid[tid].tfds_in_queue -= freed;
1045 else {
1046 D_TX("free more than tfds_in_queue (%u:%d)\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001047 il->stations[sta_id].tid[tid].tfds_in_queue, freed);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001048 il->stations[sta_id].tid[tid].tfds_in_queue = 0;
1049 }
1050}
1051
1052#define IL_TX_QUEUE_MSK 0xfffff
1053
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001054static bool
1055il4965_is_single_rx_stream(struct il_priv *il)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001056{
1057 return il->current_ht_config.smps == IEEE80211_SMPS_STATIC ||
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001058 il->current_ht_config.single_chain_sufficient;
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001059}
1060
1061#define IL_NUM_RX_CHAINS_MULTIPLE 3
1062#define IL_NUM_RX_CHAINS_SINGLE 2
1063#define IL_NUM_IDLE_CHAINS_DUAL 2
1064#define IL_NUM_IDLE_CHAINS_SINGLE 1
1065
1066/*
1067 * Determine how many receiver/antenna chains to use.
1068 *
1069 * More provides better reception via diversity. Fewer saves power
1070 * at the expense of throughput, but only when not in powersave to
1071 * start with.
1072 *
1073 * MIMO (dual stream) requires at least 2, but works better with 3.
1074 * This does not determine *which* chains to use, just how many.
1075 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001076static int
1077il4965_get_active_rx_chain_count(struct il_priv *il)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001078{
1079 /* # of Rx chains to use when expecting MIMO. */
1080 if (il4965_is_single_rx_stream(il))
1081 return IL_NUM_RX_CHAINS_SINGLE;
1082 else
1083 return IL_NUM_RX_CHAINS_MULTIPLE;
1084}
1085
1086/*
1087 * When we are in power saving mode, unless device support spatial
1088 * multiplexing power save, use the active count for rx chain count.
1089 */
1090static int
1091il4965_get_idle_rx_chain_count(struct il_priv *il, int active_cnt)
1092{
1093 /* # Rx chains when idling, depending on SMPS mode */
1094 switch (il->current_ht_config.smps) {
1095 case IEEE80211_SMPS_STATIC:
1096 case IEEE80211_SMPS_DYNAMIC:
1097 return IL_NUM_IDLE_CHAINS_SINGLE;
1098 case IEEE80211_SMPS_OFF:
1099 return active_cnt;
1100 default:
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001101 WARN(1, "invalid SMPS mode %d", il->current_ht_config.smps);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001102 return active_cnt;
1103 }
1104}
1105
1106/* up to 4 chains */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001107static u8
1108il4965_count_chain_bitmap(u32 chain_bitmap)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001109{
1110 u8 res;
1111 res = (chain_bitmap & BIT(0)) >> 0;
1112 res += (chain_bitmap & BIT(1)) >> 1;
1113 res += (chain_bitmap & BIT(2)) >> 2;
1114 res += (chain_bitmap & BIT(3)) >> 3;
1115 return res;
1116}
1117
1118/**
1119 * il4965_set_rxon_chain - Set up Rx chain usage in "staging" RXON image
1120 *
1121 * Selects how many and which Rx receivers/antennas/chains to use.
1122 * This should not be used for scan command ... it puts data in wrong place.
1123 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001124void
1125il4965_set_rxon_chain(struct il_priv *il, struct il_rxon_context *ctx)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001126{
1127 bool is_single = il4965_is_single_rx_stream(il);
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01001128 bool is_cam = !test_bit(S_POWER_PMI, &il->status);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001129 u8 idle_rx_cnt, active_rx_cnt, valid_rx_cnt;
1130 u32 active_chains;
1131 u16 rx_chain;
1132
1133 /* Tell uCode which antennas are actually connected.
1134 * Before first association, we assume all antennas are connected.
1135 * Just after first association, il4965_chain_noise_calibration()
1136 * checks which antennas actually *are* connected. */
1137 if (il->chain_noise_data.active_chains)
1138 active_chains = il->chain_noise_data.active_chains;
1139 else
1140 active_chains = il->hw_params.valid_rx_ant;
1141
1142 rx_chain = active_chains << RXON_RX_CHAIN_VALID_POS;
1143
1144 /* How many receivers should we use? */
1145 active_rx_cnt = il4965_get_active_rx_chain_count(il);
1146 idle_rx_cnt = il4965_get_idle_rx_chain_count(il, active_rx_cnt);
1147
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001148 /* correct rx chain count according hw settings
1149 * and chain noise calibration
1150 */
1151 valid_rx_cnt = il4965_count_chain_bitmap(active_chains);
1152 if (valid_rx_cnt < active_rx_cnt)
1153 active_rx_cnt = valid_rx_cnt;
1154
1155 if (valid_rx_cnt < idle_rx_cnt)
1156 idle_rx_cnt = valid_rx_cnt;
1157
1158 rx_chain |= active_rx_cnt << RXON_RX_CHAIN_MIMO_CNT_POS;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001159 rx_chain |= idle_rx_cnt << RXON_RX_CHAIN_CNT_POS;
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001160
1161 ctx->staging.rx_chain = cpu_to_le16(rx_chain);
1162
1163 if (!is_single && active_rx_cnt >= IL_NUM_RX_CHAINS_SINGLE && is_cam)
1164 ctx->staging.rx_chain |= RXON_RX_CHAIN_MIMO_FORCE_MSK;
1165 else
1166 ctx->staging.rx_chain &= ~RXON_RX_CHAIN_MIMO_FORCE_MSK;
1167
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001168 D_ASSOC("rx_chain=0x%X active=%d idle=%d\n", ctx->staging.rx_chain,
1169 active_rx_cnt, idle_rx_cnt);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001170
1171 WARN_ON(active_rx_cnt == 0 || idle_rx_cnt == 0 ||
1172 active_rx_cnt < idle_rx_cnt);
1173}
1174
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001175u8
1176il4965_toggle_tx_ant(struct il_priv *il, u8 ant, u8 valid)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001177{
1178 int i;
1179 u8 ind = ant;
1180
1181 for (i = 0; i < RATE_ANT_NUM - 1; i++) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001182 ind = (ind + 1) < RATE_ANT_NUM ? ind + 1 : 0;
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001183 if (valid & BIT(ind))
1184 return ind;
1185 }
1186 return ant;
1187}
1188
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001189static const char *
1190il4965_get_fh_string(int cmd)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001191{
1192 switch (cmd) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001193 IL_CMD(FH49_RSCSR_CHNL0_STTS_WPTR_REG);
1194 IL_CMD(FH49_RSCSR_CHNL0_RBDCB_BASE_REG);
1195 IL_CMD(FH49_RSCSR_CHNL0_WPTR);
1196 IL_CMD(FH49_MEM_RCSR_CHNL0_CONFIG_REG);
1197 IL_CMD(FH49_MEM_RSSR_SHARED_CTRL_REG);
1198 IL_CMD(FH49_MEM_RSSR_RX_STATUS_REG);
1199 IL_CMD(FH49_MEM_RSSR_RX_ENABLE_ERR_IRQ2DRV);
1200 IL_CMD(FH49_TSSR_TX_STATUS_REG);
1201 IL_CMD(FH49_TSSR_TX_ERROR_REG);
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001202 default:
1203 return "UNKNOWN";
1204 }
1205}
1206
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001207int
1208il4965_dump_fh(struct il_priv *il, char **buf, bool display)
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001209{
1210 int i;
1211#ifdef CONFIG_IWLEGACY_DEBUG
1212 int pos = 0;
1213 size_t bufsz = 0;
1214#endif
1215 static const u32 fh_tbl[] = {
Stanislaw Gruszka9a95b372011-08-31 14:20:23 +02001216 FH49_RSCSR_CHNL0_STTS_WPTR_REG,
1217 FH49_RSCSR_CHNL0_RBDCB_BASE_REG,
1218 FH49_RSCSR_CHNL0_WPTR,
1219 FH49_MEM_RCSR_CHNL0_CONFIG_REG,
1220 FH49_MEM_RSSR_SHARED_CTRL_REG,
1221 FH49_MEM_RSSR_RX_STATUS_REG,
1222 FH49_MEM_RSSR_RX_ENABLE_ERR_IRQ2DRV,
1223 FH49_TSSR_TX_STATUS_REG,
1224 FH49_TSSR_TX_ERROR_REG
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001225 };
1226#ifdef CONFIG_IWLEGACY_DEBUG
1227 if (display) {
1228 bufsz = ARRAY_SIZE(fh_tbl) * 48 + 40;
1229 *buf = kmalloc(bufsz, GFP_KERNEL);
1230 if (!*buf)
1231 return -ENOMEM;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001232 pos +=
1233 scnprintf(*buf + pos, bufsz - pos, "FH register values:\n");
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001234 for (i = 0; i < ARRAY_SIZE(fh_tbl); i++) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001235 pos +=
1236 scnprintf(*buf + pos, bufsz - pos,
1237 " %34s: 0X%08x\n",
1238 il4965_get_fh_string(fh_tbl[i]), il_rd(il,
1239 fh_tbl
1240 [i]));
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001241 }
1242 return pos;
1243 }
1244#endif
1245 IL_ERR("FH register values:\n");
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001246 for (i = 0; i < ARRAY_SIZE(fh_tbl); i++) {
1247 IL_ERR(" %34s: 0X%08x\n", il4965_get_fh_string(fh_tbl[i]),
1248 il_rd(il, fh_tbl[i]));
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +02001249 }
1250 return 0;
1251}
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001252
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001253void
1254il4965_hdl_missed_beacon(struct il_priv *il, struct il_rx_buf *rxb)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001255{
1256 struct il_rx_pkt *pkt = rxb_addr(rxb);
1257 struct il_missed_beacon_notif *missed_beacon;
1258
1259 missed_beacon = &pkt->u.missed_beacon;
1260 if (le32_to_cpu(missed_beacon->consecutive_missed_beacons) >
1261 il->missed_beacon_threshold) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001262 D_CALIB("missed bcn cnsq %d totl %d rcd %d expctd %d\n",
1263 le32_to_cpu(missed_beacon->consecutive_missed_beacons),
1264 le32_to_cpu(missed_beacon->total_missed_becons),
1265 le32_to_cpu(missed_beacon->num_recvd_beacons),
1266 le32_to_cpu(missed_beacon->num_expected_beacons));
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01001267 if (!test_bit(S_SCANNING, &il->status))
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001268 il4965_init_sensitivity(il);
1269 }
1270}
1271
1272/* Calculate noise level, based on measurements during network silence just
1273 * before arriving beacon. This measurement can be done only if we know
1274 * exactly when to expect beacons, therefore only when we're associated. */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001275static void
1276il4965_rx_calc_noise(struct il_priv *il)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001277{
1278 struct stats_rx_non_phy *rx_info;
1279 int num_active_rx = 0;
1280 int total_silence = 0;
1281 int bcn_silence_a, bcn_silence_b, bcn_silence_c;
1282 int last_rx_noise;
1283
1284 rx_info = &(il->_4965.stats.rx.general);
1285 bcn_silence_a =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001286 le32_to_cpu(rx_info->beacon_silence_rssi_a) & IN_BAND_FILTER;
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001287 bcn_silence_b =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001288 le32_to_cpu(rx_info->beacon_silence_rssi_b) & IN_BAND_FILTER;
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001289 bcn_silence_c =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001290 le32_to_cpu(rx_info->beacon_silence_rssi_c) & IN_BAND_FILTER;
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001291
1292 if (bcn_silence_a) {
1293 total_silence += bcn_silence_a;
1294 num_active_rx++;
1295 }
1296 if (bcn_silence_b) {
1297 total_silence += bcn_silence_b;
1298 num_active_rx++;
1299 }
1300 if (bcn_silence_c) {
1301 total_silence += bcn_silence_c;
1302 num_active_rx++;
1303 }
1304
1305 /* Average among active antennas */
1306 if (num_active_rx)
1307 last_rx_noise = (total_silence / num_active_rx) - 107;
1308 else
1309 last_rx_noise = IL_NOISE_MEAS_NOT_AVAILABLE;
1310
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001311 D_CALIB("inband silence a %u, b %u, c %u, dBm %d\n", bcn_silence_a,
1312 bcn_silence_b, bcn_silence_c, last_rx_noise);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001313}
1314
1315#ifdef CONFIG_IWLEGACY_DEBUGFS
1316/*
1317 * based on the assumption of all stats counter are in DWORD
1318 * FIXME: This function is for debugging, do not deal with
1319 * the case of counters roll-over.
1320 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001321static void
1322il4965_accumulative_stats(struct il_priv *il, __le32 * stats)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001323{
1324 int i, size;
1325 __le32 *prev_stats;
1326 u32 *accum_stats;
1327 u32 *delta, *max_delta;
1328 struct stats_general_common *general, *accum_general;
1329 struct stats_tx *tx, *accum_tx;
1330
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001331 prev_stats = (__le32 *) & il->_4965.stats;
1332 accum_stats = (u32 *) & il->_4965.accum_stats;
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001333 size = sizeof(struct il_notif_stats);
1334 general = &il->_4965.stats.general.common;
1335 accum_general = &il->_4965.accum_stats.general.common;
1336 tx = &il->_4965.stats.tx;
1337 accum_tx = &il->_4965.accum_stats.tx;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001338 delta = (u32 *) & il->_4965.delta_stats;
1339 max_delta = (u32 *) & il->_4965.max_delta;
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001340
1341 for (i = sizeof(__le32); i < size;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001342 i +=
1343 sizeof(__le32), stats++, prev_stats++, delta++, max_delta++,
1344 accum_stats++) {
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001345 if (le32_to_cpu(*stats) > le32_to_cpu(*prev_stats)) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001346 *delta =
1347 (le32_to_cpu(*stats) - le32_to_cpu(*prev_stats));
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001348 *accum_stats += *delta;
1349 if (*delta > *max_delta)
1350 *max_delta = *delta;
1351 }
1352 }
1353
1354 /* reset accumulative stats for "no-counter" type stats */
1355 accum_general->temperature = general->temperature;
1356 accum_general->ttl_timestamp = general->ttl_timestamp;
1357}
1358#endif
1359
1360#define REG_RECALIB_PERIOD (60)
1361
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001362void
1363il4965_hdl_stats(struct il_priv *il, struct il_rx_buf *rxb)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001364{
1365 int change;
1366 struct il_rx_pkt *pkt = rxb_addr(rxb);
1367
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001368 D_RX("Statistics notification received (%d vs %d).\n",
1369 (int)sizeof(struct il_notif_stats),
1370 le32_to_cpu(pkt->len_n_flags) & IL_RX_FRAME_SIZE_MSK);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001371
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001372 change =
1373 ((il->_4965.stats.general.common.temperature !=
1374 pkt->u.stats.general.common.temperature) ||
1375 ((il->_4965.stats.flag & STATS_REPLY_FLG_HT40_MODE_MSK) !=
1376 (pkt->u.stats.flag & STATS_REPLY_FLG_HT40_MODE_MSK)));
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001377#ifdef CONFIG_IWLEGACY_DEBUGFS
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001378 il4965_accumulative_stats(il, (__le32 *) & pkt->u.stats);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001379#endif
1380
1381 /* TODO: reading some of stats is unneeded */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001382 memcpy(&il->_4965.stats, &pkt->u.stats, sizeof(il->_4965.stats));
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001383
Stanislaw Gruszkadb7746f2011-11-15 13:11:50 +01001384 set_bit(S_STATS, &il->status);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001385
1386 /* Reschedule the stats timer to occur in
1387 * REG_RECALIB_PERIOD seconds to ensure we get a
1388 * thermal update even if the uCode doesn't give
1389 * us one */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001390 mod_timer(&il->stats_periodic,
1391 jiffies + msecs_to_jiffies(REG_RECALIB_PERIOD * 1000));
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001392
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01001393 if (unlikely(!test_bit(S_SCANNING, &il->status)) &&
Stanislaw Gruszka4d69c752011-08-30 15:26:35 +02001394 (pkt->hdr.cmd == N_STATS)) {
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001395 il4965_rx_calc_noise(il);
1396 queue_work(il->workqueue, &il->run_time_calib_work);
1397 }
1398 if (il->cfg->ops->lib->temp_ops.temperature && change)
1399 il->cfg->ops->lib->temp_ops.temperature(il);
1400}
1401
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001402void
1403il4965_hdl_c_stats(struct il_priv *il, struct il_rx_buf *rxb)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001404{
1405 struct il_rx_pkt *pkt = rxb_addr(rxb);
1406
Stanislaw Gruszkadb7746f2011-11-15 13:11:50 +01001407 if (le32_to_cpu(pkt->u.stats.flag) & UCODE_STATS_CLEAR_MSK) {
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001408#ifdef CONFIG_IWLEGACY_DEBUGFS
1409 memset(&il->_4965.accum_stats, 0,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001410 sizeof(struct il_notif_stats));
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001411 memset(&il->_4965.delta_stats, 0,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001412 sizeof(struct il_notif_stats));
1413 memset(&il->_4965.max_delta, 0, sizeof(struct il_notif_stats));
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001414#endif
1415 D_RX("Statistics have been cleared\n");
1416 }
Stanislaw Gruszkad2dfb332011-11-15 13:16:38 +01001417 il4965_hdl_stats(il, rxb);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001418}
1419
Stanislaw Gruszka8f29b452011-11-15 12:57:25 +01001420
1421/*
1422 * mac80211 queues, ACs, hardware queues, FIFOs.
1423 *
1424 * Cf. http://wireless.kernel.org/en/developers/Documentation/mac80211/queues
1425 *
1426 * Mac80211 uses the following numbers, which we get as from it
1427 * by way of skb_get_queue_mapping(skb):
1428 *
1429 * VO 0
1430 * VI 1
1431 * BE 2
1432 * BK 3
1433 *
1434 *
1435 * Regular (not A-MPDU) frames are put into hardware queues corresponding
1436 * to the FIFOs, see comments in iwl-prph.h. Aggregated frames get their
1437 * own queue per aggregation session (RA/TID combination), such queues are
1438 * set up to map into FIFOs too, for which we need an AC->FIFO mapping. In
1439 * order to map frames to the right queue, we also need an AC->hw queue
1440 * mapping. This is implemented here.
1441 *
1442 * Due to the way hw queues are set up (by the hw specific modules like
Stanislaw Gruszkaaf038f42011-08-30 13:58:27 +02001443 * 4965.c), the AC->hw queue mapping is the identity
Stanislaw Gruszka8f29b452011-11-15 12:57:25 +01001444 * mapping.
1445 */
1446
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001447static const u8 tid_to_ac[] = {
1448 IEEE80211_AC_BE,
1449 IEEE80211_AC_BK,
1450 IEEE80211_AC_BK,
1451 IEEE80211_AC_BE,
1452 IEEE80211_AC_VI,
1453 IEEE80211_AC_VI,
1454 IEEE80211_AC_VO,
1455 IEEE80211_AC_VO
1456};
1457
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001458static inline int
1459il4965_get_ac_from_tid(u16 tid)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001460{
1461 if (likely(tid < ARRAY_SIZE(tid_to_ac)))
1462 return tid_to_ac[tid];
1463
1464 /* no support for TIDs 8-15 yet */
1465 return -EINVAL;
1466}
1467
1468static inline int
1469il4965_get_fifo_from_tid(struct il_rxon_context *ctx, u16 tid)
1470{
1471 if (likely(tid < ARRAY_SIZE(tid_to_ac)))
1472 return ctx->ac_to_fifo[tid_to_ac[tid]];
1473
1474 /* no support for TIDs 8-15 yet */
1475 return -EINVAL;
1476}
1477
1478/*
Stanislaw Gruszka4d69c752011-08-30 15:26:35 +02001479 * handle build C_TX command notification.
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001480 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001481static void
1482il4965_tx_cmd_build_basic(struct il_priv *il, struct sk_buff *skb,
1483 struct il_tx_cmd *tx_cmd,
1484 struct ieee80211_tx_info *info,
1485 struct ieee80211_hdr *hdr, u8 std_id)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001486{
1487 __le16 fc = hdr->frame_control;
1488 __le32 tx_flags = tx_cmd->tx_flags;
1489
1490 tx_cmd->stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
1491 if (!(info->flags & IEEE80211_TX_CTL_NO_ACK)) {
1492 tx_flags |= TX_CMD_FLG_ACK_MSK;
1493 if (ieee80211_is_mgmt(fc))
1494 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
1495 if (ieee80211_is_probe_resp(fc) &&
1496 !(le16_to_cpu(hdr->seq_ctrl) & 0xf))
1497 tx_flags |= TX_CMD_FLG_TSF_MSK;
1498 } else {
1499 tx_flags &= (~TX_CMD_FLG_ACK_MSK);
1500 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
1501 }
1502
1503 if (ieee80211_is_back_req(fc))
1504 tx_flags |= TX_CMD_FLG_ACK_MSK | TX_CMD_FLG_IMM_BA_RSP_MASK;
1505
1506 tx_cmd->sta_id = std_id;
1507 if (ieee80211_has_morefrags(fc))
1508 tx_flags |= TX_CMD_FLG_MORE_FRAG_MSK;
1509
1510 if (ieee80211_is_data_qos(fc)) {
1511 u8 *qc = ieee80211_get_qos_ctl(hdr);
1512 tx_cmd->tid_tspec = qc[0] & 0xf;
1513 tx_flags &= ~TX_CMD_FLG_SEQ_CTL_MSK;
1514 } else {
1515 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
1516 }
1517
1518 il_tx_cmd_protection(il, info, fc, &tx_flags);
1519
1520 tx_flags &= ~(TX_CMD_FLG_ANT_SEL_MSK);
1521 if (ieee80211_is_mgmt(fc)) {
1522 if (ieee80211_is_assoc_req(fc) || ieee80211_is_reassoc_req(fc))
1523 tx_cmd->timeout.pm_frame_timeout = cpu_to_le16(3);
1524 else
1525 tx_cmd->timeout.pm_frame_timeout = cpu_to_le16(2);
1526 } else {
1527 tx_cmd->timeout.pm_frame_timeout = 0;
1528 }
1529
1530 tx_cmd->driver_txop = 0;
1531 tx_cmd->tx_flags = tx_flags;
1532 tx_cmd->next_frame_len = 0;
1533}
1534
1535#define RTS_DFAULT_RETRY_LIMIT 60
1536
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001537static void
1538il4965_tx_cmd_build_rate(struct il_priv *il, struct il_tx_cmd *tx_cmd,
1539 struct ieee80211_tx_info *info, __le16 fc)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001540{
1541 u32 rate_flags;
1542 int rate_idx;
1543 u8 rts_retry_limit;
1544 u8 data_retry_limit;
1545 u8 rate_plcp;
1546
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001547 /* Set retry limit on DATA packets and Probe Responses */
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001548 if (ieee80211_is_probe_resp(fc))
1549 data_retry_limit = 3;
1550 else
1551 data_retry_limit = IL4965_DEFAULT_TX_RETRY;
1552 tx_cmd->data_retry_limit = data_retry_limit;
1553
1554 /* Set retry limit on RTS packets */
1555 rts_retry_limit = RTS_DFAULT_RETRY_LIMIT;
1556 if (data_retry_limit < rts_retry_limit)
1557 rts_retry_limit = data_retry_limit;
1558 tx_cmd->rts_retry_limit = rts_retry_limit;
1559
1560 /* DATA packets will use the uCode station table for rate/antenna
1561 * selection */
1562 if (ieee80211_is_data(fc)) {
1563 tx_cmd->initial_rate_idx = 0;
1564 tx_cmd->tx_flags |= TX_CMD_FLG_STA_RATE_MSK;
1565 return;
1566 }
1567
1568 /**
1569 * If the current TX rate stored in mac80211 has the MCS bit set, it's
1570 * not really a TX rate. Thus, we use the lowest supported rate for
1571 * this band. Also use the lowest supported rate if the stored rate
1572 * idx is invalid.
1573 */
1574 rate_idx = info->control.rates[0].idx;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001575 if ((info->control.rates[0].flags & IEEE80211_TX_RC_MCS) || rate_idx < 0
1576 || rate_idx > RATE_COUNT_LEGACY)
1577 rate_idx =
1578 rate_lowest_index(&il->bands[info->band],
1579 info->control.sta);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001580 /* For 5 GHZ band, remap mac80211 rate indices into driver indices */
1581 if (info->band == IEEE80211_BAND_5GHZ)
1582 rate_idx += IL_FIRST_OFDM_RATE;
1583 /* Get PLCP rate for tx_cmd->rate_n_flags */
1584 rate_plcp = il_rates[rate_idx].plcp;
1585 /* Zero out flags for this packet */
1586 rate_flags = 0;
1587
1588 /* Set CCK flag as needed */
1589 if (rate_idx >= IL_FIRST_CCK_RATE && rate_idx <= IL_LAST_CCK_RATE)
1590 rate_flags |= RATE_MCS_CCK_MSK;
1591
1592 /* Set up antennas */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001593 il->mgmt_tx_ant =
1594 il4965_toggle_tx_ant(il, il->mgmt_tx_ant,
1595 il->hw_params.valid_tx_ant);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001596
1597 rate_flags |= il4965_ant_idx_to_flags(il->mgmt_tx_ant);
1598
1599 /* Set the rate in the TX cmd */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001600 tx_cmd->rate_n_flags =
1601 il4965_hw_set_rate_n_flags(rate_plcp, rate_flags);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001602}
1603
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001604static void
1605il4965_tx_cmd_build_hwcrypto(struct il_priv *il, struct ieee80211_tx_info *info,
1606 struct il_tx_cmd *tx_cmd, struct sk_buff *skb_frag,
1607 int sta_id)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001608{
1609 struct ieee80211_key_conf *keyconf = info->control.hw_key;
1610
1611 switch (keyconf->cipher) {
1612 case WLAN_CIPHER_SUITE_CCMP:
1613 tx_cmd->sec_ctl = TX_CMD_SEC_CCM;
1614 memcpy(tx_cmd->key, keyconf->key, keyconf->keylen);
1615 if (info->flags & IEEE80211_TX_CTL_AMPDU)
1616 tx_cmd->tx_flags |= TX_CMD_FLG_AGG_CCMP_MSK;
1617 D_TX("tx_cmd with AES hwcrypto\n");
1618 break;
1619
1620 case WLAN_CIPHER_SUITE_TKIP:
1621 tx_cmd->sec_ctl = TX_CMD_SEC_TKIP;
1622 ieee80211_get_tkip_p2k(keyconf, skb_frag, tx_cmd->key);
1623 D_TX("tx_cmd with tkip hwcrypto\n");
1624 break;
1625
1626 case WLAN_CIPHER_SUITE_WEP104:
1627 tx_cmd->sec_ctl |= TX_CMD_SEC_KEY128;
1628 /* fall through */
1629 case WLAN_CIPHER_SUITE_WEP40:
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001630 tx_cmd->sec_ctl |=
1631 (TX_CMD_SEC_WEP | (keyconf->keyidx & TX_CMD_SEC_MSK) <<
1632 TX_CMD_SEC_SHIFT);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001633
1634 memcpy(&tx_cmd->key[3], keyconf->key, keyconf->keylen);
1635
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001636 D_TX("Configuring packet for WEP encryption " "with key %d\n",
1637 keyconf->keyidx);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001638 break;
1639
1640 default:
1641 IL_ERR("Unknown encode cipher %x\n", keyconf->cipher);
1642 break;
1643 }
1644}
1645
1646/*
Stanislaw Gruszka4d69c752011-08-30 15:26:35 +02001647 * start C_TX command process
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001648 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001649int
1650il4965_tx_skb(struct il_priv *il, struct sk_buff *skb)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001651{
1652 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1653 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1654 struct ieee80211_sta *sta = info->control.sta;
1655 struct il_station_priv *sta_priv = NULL;
1656 struct il_tx_queue *txq;
1657 struct il_queue *q;
1658 struct il_device_cmd *out_cmd;
1659 struct il_cmd_meta *out_meta;
1660 struct il_tx_cmd *tx_cmd;
1661 struct il_rxon_context *ctx = &il->ctx;
1662 int txq_id;
1663 dma_addr_t phys_addr;
1664 dma_addr_t txcmd_phys;
1665 dma_addr_t scratch_phys;
1666 u16 len, firstlen, secondlen;
1667 u16 seq_number = 0;
1668 __le16 fc;
1669 u8 hdr_len;
1670 u8 sta_id;
1671 u8 wait_write_ptr = 0;
1672 u8 tid = 0;
1673 u8 *qc = NULL;
1674 unsigned long flags;
1675 bool is_agg = false;
1676
1677 if (info->control.vif)
1678 ctx = il_rxon_ctx_from_vif(info->control.vif);
1679
1680 spin_lock_irqsave(&il->lock, flags);
1681 if (il_is_rfkill(il)) {
1682 D_DROP("Dropping - RF KILL\n");
1683 goto drop_unlock;
1684 }
1685
1686 fc = hdr->frame_control;
1687
1688#ifdef CONFIG_IWLEGACY_DEBUG
1689 if (ieee80211_is_auth(fc))
1690 D_TX("Sending AUTH frame\n");
1691 else if (ieee80211_is_assoc_req(fc))
1692 D_TX("Sending ASSOC frame\n");
1693 else if (ieee80211_is_reassoc_req(fc))
1694 D_TX("Sending REASSOC frame\n");
1695#endif
1696
1697 hdr_len = ieee80211_hdrlen(fc);
1698
1699 /* For management frames use broadcast id to do not break aggregation */
1700 if (!ieee80211_is_data(fc))
1701 sta_id = ctx->bcast_sta_id;
1702 else {
1703 /* Find idx into station table for destination station */
1704 sta_id = il_sta_id_or_broadcast(il, ctx, info->control.sta);
1705
1706 if (sta_id == IL_INVALID_STATION) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001707 D_DROP("Dropping - INVALID STATION: %pM\n", hdr->addr1);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001708 goto drop_unlock;
1709 }
1710 }
1711
1712 D_TX("station Id %d\n", sta_id);
1713
1714 if (sta)
1715 sta_priv = (void *)sta->drv_priv;
1716
1717 if (sta_priv && sta_priv->asleep &&
1718 (info->flags & IEEE80211_TX_CTL_POLL_RESPONSE)) {
1719 /*
1720 * This sends an asynchronous command to the device,
1721 * but we can rely on it being processed before the
1722 * next frame is processed -- and the next frame to
1723 * this station is the one that will consume this
1724 * counter.
1725 * For now set the counter to just 1 since we do not
1726 * support uAPSD yet.
1727 */
1728 il4965_sta_modify_sleep_tx_count(il, sta_id, 1);
1729 }
1730
1731 /*
1732 * Send this frame after DTIM -- there's a special queue
1733 * reserved for this for contexts that support AP mode.
1734 */
1735 if (info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM) {
1736 txq_id = ctx->mcast_queue;
1737 /*
1738 * The microcode will clear the more data
1739 * bit in the last frame it transmits.
1740 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001741 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_MOREDATA);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001742 } else
1743 txq_id = ctx->ac_to_queue[skb_get_queue_mapping(skb)];
1744
1745 /* irqs already disabled/saved above when locking il->lock */
1746 spin_lock(&il->sta_lock);
1747
1748 if (ieee80211_is_data_qos(fc)) {
1749 qc = ieee80211_get_qos_ctl(hdr);
1750 tid = qc[0] & IEEE80211_QOS_CTL_TID_MASK;
1751 if (WARN_ON_ONCE(tid >= MAX_TID_COUNT)) {
1752 spin_unlock(&il->sta_lock);
1753 goto drop_unlock;
1754 }
1755 seq_number = il->stations[sta_id].tid[tid].seq_number;
1756 seq_number &= IEEE80211_SCTL_SEQ;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001757 hdr->seq_ctrl =
1758 hdr->seq_ctrl & cpu_to_le16(IEEE80211_SCTL_FRAG);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001759 hdr->seq_ctrl |= cpu_to_le16(seq_number);
1760 seq_number += 0x10;
1761 /* aggregation is on for this <sta,tid> */
1762 if (info->flags & IEEE80211_TX_CTL_AMPDU &&
1763 il->stations[sta_id].tid[tid].agg.state == IL_AGG_ON) {
1764 txq_id = il->stations[sta_id].tid[tid].agg.txq_id;
1765 is_agg = true;
1766 }
1767 }
1768
1769 txq = &il->txq[txq_id];
1770 q = &txq->q;
1771
1772 if (unlikely(il_queue_space(q) < q->high_mark)) {
1773 spin_unlock(&il->sta_lock);
1774 goto drop_unlock;
1775 }
1776
1777 if (ieee80211_is_data_qos(fc)) {
1778 il->stations[sta_id].tid[tid].tfds_in_queue++;
1779 if (!ieee80211_has_morefrags(fc))
1780 il->stations[sta_id].tid[tid].seq_number = seq_number;
1781 }
1782
1783 spin_unlock(&il->sta_lock);
1784
1785 /* Set up driver data for this TFD */
1786 memset(&(txq->txb[q->write_ptr]), 0, sizeof(struct il_tx_info));
1787 txq->txb[q->write_ptr].skb = skb;
1788 txq->txb[q->write_ptr].ctx = ctx;
1789
1790 /* Set up first empty entry in queue's array of Tx/cmd buffers */
1791 out_cmd = txq->cmd[q->write_ptr];
1792 out_meta = &txq->meta[q->write_ptr];
1793 tx_cmd = &out_cmd->cmd.tx;
1794 memset(&out_cmd->hdr, 0, sizeof(out_cmd->hdr));
1795 memset(tx_cmd, 0, sizeof(struct il_tx_cmd));
1796
1797 /*
1798 * Set up the Tx-command (not MAC!) header.
1799 * Store the chosen Tx queue and TFD idx within the sequence field;
1800 * after Tx, uCode's Tx response will return this value so driver can
1801 * locate the frame within the tx queue and do post-tx processing.
1802 */
Stanislaw Gruszka4d69c752011-08-30 15:26:35 +02001803 out_cmd->hdr.cmd = C_TX;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001804 out_cmd->hdr.sequence =
1805 cpu_to_le16((u16)
1806 (QUEUE_TO_SEQ(txq_id) | IDX_TO_SEQ(q->write_ptr)));
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001807
1808 /* Copy MAC header from skb into command buffer */
1809 memcpy(tx_cmd->hdr, hdr, hdr_len);
1810
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001811 /* Total # bytes to be transmitted */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001812 len = (u16) skb->len;
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001813 tx_cmd->len = cpu_to_le16(len);
1814
1815 if (info->control.hw_key)
1816 il4965_tx_cmd_build_hwcrypto(il, info, tx_cmd, skb, sta_id);
1817
1818 /* TODO need this for burst mode later on */
1819 il4965_tx_cmd_build_basic(il, skb, tx_cmd, info, hdr, sta_id);
1820 il_dbg_log_tx_data_frame(il, len, hdr);
1821
1822 il4965_tx_cmd_build_rate(il, tx_cmd, info, fc);
1823
1824 il_update_stats(il, true, fc, len);
1825 /*
1826 * Use the first empty entry in this queue's command buffer array
1827 * to contain the Tx command and MAC header concatenated together
1828 * (payload data will be in another buffer).
1829 * Size of this varies, due to varying MAC header length.
1830 * If end is not dword aligned, we'll have 2 extra bytes at the end
1831 * of the MAC header (device reads on dword boundaries).
1832 * We'll tell device about this padding later.
1833 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001834 len = sizeof(struct il_tx_cmd) + sizeof(struct il_cmd_header) + hdr_len;
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001835 firstlen = (len + 3) & ~3;
1836
1837 /* Tell NIC about any 2-byte padding after MAC header */
1838 if (firstlen != len)
1839 tx_cmd->tx_flags |= TX_CMD_FLG_MH_PAD_MSK;
1840
1841 /* Physical address of this Tx command's header (not MAC header!),
1842 * within command buffer array. */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001843 txcmd_phys =
1844 pci_map_single(il->pci_dev, &out_cmd->hdr, firstlen,
1845 PCI_DMA_BIDIRECTIONAL);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001846 dma_unmap_addr_set(out_meta, mapping, txcmd_phys);
1847 dma_unmap_len_set(out_meta, len, firstlen);
1848 /* Add buffer containing Tx command and MAC(!) header to TFD's
1849 * first entry */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001850 il->cfg->ops->lib->txq_attach_buf_to_tfd(il, txq, txcmd_phys, firstlen,
1851 1, 0);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001852
1853 if (!ieee80211_has_morefrags(hdr->frame_control)) {
1854 txq->need_update = 1;
1855 } else {
1856 wait_write_ptr = 1;
1857 txq->need_update = 0;
1858 }
1859
1860 /* Set up TFD's 2nd entry to point directly to remainder of skb,
1861 * if any (802.11 null frames have no payload). */
1862 secondlen = skb->len - hdr_len;
1863 if (secondlen > 0) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001864 phys_addr =
1865 pci_map_single(il->pci_dev, skb->data + hdr_len, secondlen,
1866 PCI_DMA_TODEVICE);
1867 il->cfg->ops->lib->txq_attach_buf_to_tfd(il, txq, phys_addr,
1868 secondlen, 0, 0);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001869 }
1870
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001871 scratch_phys =
1872 txcmd_phys + sizeof(struct il_cmd_header) +
1873 offsetof(struct il_tx_cmd, scratch);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001874
1875 /* take back ownership of DMA buffer to enable update */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001876 pci_dma_sync_single_for_cpu(il->pci_dev, txcmd_phys, firstlen,
1877 PCI_DMA_BIDIRECTIONAL);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001878 tx_cmd->dram_lsb_ptr = cpu_to_le32(scratch_phys);
1879 tx_cmd->dram_msb_ptr = il_get_dma_hi_addr(scratch_phys);
1880
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001881 D_TX("sequence nr = 0X%x\n", le16_to_cpu(out_cmd->hdr.sequence));
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001882 D_TX("tx_flags = 0X%x\n", le32_to_cpu(tx_cmd->tx_flags));
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001883 il_print_hex_dump(il, IL_DL_TX, (u8 *) tx_cmd, sizeof(*tx_cmd));
1884 il_print_hex_dump(il, IL_DL_TX, (u8 *) tx_cmd->hdr, hdr_len);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001885
1886 /* Set up entry for this TFD in Tx byte-count array */
1887 if (info->flags & IEEE80211_TX_CTL_AMPDU)
1888 il->cfg->ops->lib->txq_update_byte_cnt_tbl(il, txq,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001889 le16_to_cpu(tx_cmd->
1890 len));
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001891
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001892 pci_dma_sync_single_for_device(il->pci_dev, txcmd_phys, firstlen,
1893 PCI_DMA_BIDIRECTIONAL);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001894
1895 /* Tell device the write idx *just past* this latest filled TFD */
1896 q->write_ptr = il_queue_inc_wrap(q->write_ptr, q->n_bd);
1897 il_txq_update_write_ptr(il, txq);
1898 spin_unlock_irqrestore(&il->lock, flags);
1899
1900 /*
1901 * At this point the frame is "transmitted" successfully
1902 * and we will get a TX status notification eventually,
1903 * regardless of the value of ret. "ret" only indicates
1904 * whether or not we should update the write pointer.
1905 */
1906
1907 /*
1908 * Avoid atomic ops if it isn't an associated client.
1909 * Also, if this is a packet for aggregation, don't
1910 * increase the counter because the ucode will stop
1911 * aggregation queues when their respective station
1912 * goes to sleep.
1913 */
1914 if (sta_priv && sta_priv->client && !is_agg)
1915 atomic_inc(&sta_priv->pending_frames);
1916
1917 if (il_queue_space(q) < q->high_mark && il->mac80211_registered) {
1918 if (wait_write_ptr) {
1919 spin_lock_irqsave(&il->lock, flags);
1920 txq->need_update = 1;
1921 il_txq_update_write_ptr(il, txq);
1922 spin_unlock_irqrestore(&il->lock, flags);
1923 } else {
1924 il_stop_queue(il, txq);
1925 }
1926 }
1927
1928 return 0;
1929
1930drop_unlock:
1931 spin_unlock_irqrestore(&il->lock, flags);
1932 return -1;
1933}
1934
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001935static inline int
1936il4965_alloc_dma_ptr(struct il_priv *il, struct il_dma_ptr *ptr, size_t size)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001937{
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001938 ptr->addr =
1939 dma_alloc_coherent(&il->pci_dev->dev, size, &ptr->dma, GFP_KERNEL);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001940 if (!ptr->addr)
1941 return -ENOMEM;
1942 ptr->size = size;
1943 return 0;
1944}
1945
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001946static inline void
1947il4965_free_dma_ptr(struct il_priv *il, struct il_dma_ptr *ptr)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001948{
1949 if (unlikely(!ptr->addr))
1950 return;
1951
1952 dma_free_coherent(&il->pci_dev->dev, ptr->size, ptr->addr, ptr->dma);
1953 memset(ptr, 0, sizeof(*ptr));
1954}
1955
1956/**
1957 * il4965_hw_txq_ctx_free - Free TXQ Context
1958 *
1959 * Destroy all TX DMA queues and structures
1960 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001961void
1962il4965_hw_txq_ctx_free(struct il_priv *il)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001963{
1964 int txq_id;
1965
1966 /* Tx queues */
1967 if (il->txq) {
1968 for (txq_id = 0; txq_id < il->hw_params.max_txq_num; txq_id++)
1969 if (txq_id == il->cmd_queue)
1970 il_cmd_queue_free(il);
1971 else
1972 il_tx_queue_free(il, txq_id);
1973 }
1974 il4965_free_dma_ptr(il, &il->kw);
1975
1976 il4965_free_dma_ptr(il, &il->scd_bc_tbls);
1977
1978 /* free tx queue structure */
1979 il_txq_mem(il);
1980}
1981
1982/**
1983 * il4965_txq_ctx_alloc - allocate TX queue context
1984 * Allocate all Tx DMA structures and initialize them
1985 *
1986 * @param il
1987 * @return error code
1988 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001989int
1990il4965_txq_ctx_alloc(struct il_priv *il)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001991{
1992 int ret;
1993 int txq_id, slots_num;
1994 unsigned long flags;
1995
1996 /* Free all tx/cmd queues and keep-warm buffer */
1997 il4965_hw_txq_ctx_free(il);
1998
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01001999 ret =
2000 il4965_alloc_dma_ptr(il, &il->scd_bc_tbls,
2001 il->hw_params.scd_bc_tbls_size);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002002 if (ret) {
2003 IL_ERR("Scheduler BC Table allocation failed\n");
2004 goto error_bc_tbls;
2005 }
2006 /* Alloc keep-warm buffer */
2007 ret = il4965_alloc_dma_ptr(il, &il->kw, IL_KW_SIZE);
2008 if (ret) {
2009 IL_ERR("Keep Warm allocation failed\n");
2010 goto error_kw;
2011 }
2012
2013 /* allocate tx queue structure */
2014 ret = il_alloc_txq_mem(il);
2015 if (ret)
2016 goto error;
2017
2018 spin_lock_irqsave(&il->lock, flags);
2019
2020 /* Turn off all Tx DMA fifos */
2021 il4965_txq_set_sched(il, 0);
2022
2023 /* Tell NIC where to find the "keep warm" buffer */
Stanislaw Gruszka9a95b372011-08-31 14:20:23 +02002024 il_wr(il, FH49_KW_MEM_ADDR_REG, il->kw.dma >> 4);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002025
2026 spin_unlock_irqrestore(&il->lock, flags);
2027
2028 /* Alloc and init all Tx queues, including the command queue (#4/#9) */
2029 for (txq_id = 0; txq_id < il->hw_params.max_txq_num; txq_id++) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002030 slots_num =
2031 (txq_id ==
2032 il->cmd_queue) ? TFD_CMD_SLOTS : TFD_TX_CMD_SLOTS;
2033 ret = il_tx_queue_init(il, &il->txq[txq_id], slots_num, txq_id);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002034 if (ret) {
2035 IL_ERR("Tx %d queue init failed\n", txq_id);
2036 goto error;
2037 }
2038 }
2039
2040 return ret;
2041
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002042error:
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002043 il4965_hw_txq_ctx_free(il);
2044 il4965_free_dma_ptr(il, &il->kw);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002045error_kw:
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002046 il4965_free_dma_ptr(il, &il->scd_bc_tbls);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002047error_bc_tbls:
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002048 return ret;
2049}
2050
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002051void
2052il4965_txq_ctx_reset(struct il_priv *il)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002053{
2054 int txq_id, slots_num;
2055 unsigned long flags;
2056
2057 spin_lock_irqsave(&il->lock, flags);
2058
2059 /* Turn off all Tx DMA fifos */
2060 il4965_txq_set_sched(il, 0);
2061
2062 /* Tell NIC where to find the "keep warm" buffer */
Stanislaw Gruszka9a95b372011-08-31 14:20:23 +02002063 il_wr(il, FH49_KW_MEM_ADDR_REG, il->kw.dma >> 4);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002064
2065 spin_unlock_irqrestore(&il->lock, flags);
2066
2067 /* Alloc and init all Tx queues, including the command queue (#4) */
2068 for (txq_id = 0; txq_id < il->hw_params.max_txq_num; txq_id++) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002069 slots_num =
2070 txq_id == il->cmd_queue ? TFD_CMD_SLOTS : TFD_TX_CMD_SLOTS;
2071 il_tx_queue_reset(il, &il->txq[txq_id], slots_num, txq_id);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002072 }
2073}
2074
2075/**
2076 * il4965_txq_ctx_stop - Stop all Tx DMA channels
2077 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002078void
2079il4965_txq_ctx_stop(struct il_priv *il)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002080{
2081 int ch, txq_id;
2082 unsigned long flags;
2083
2084 /* Turn off all Tx DMA fifos */
2085 spin_lock_irqsave(&il->lock, flags);
2086
2087 il4965_txq_set_sched(il, 0);
2088
2089 /* Stop each Tx DMA channel, and wait for it to be idle */
2090 for (ch = 0; ch < il->hw_params.dma_chnl_num; ch++) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002091 il_wr(il, FH49_TCSR_CHNL_TX_CONFIG_REG(ch), 0x0);
2092 if (il_poll_bit
2093 (il, FH49_TSSR_TX_STATUS_REG,
2094 FH49_TSSR_TX_STATUS_REG_MSK_CHNL_IDLE(ch), 1000))
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002095 IL_ERR("Failing on timeout while stopping"
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002096 " DMA channel %d [0x%08x]", ch, il_rd(il,
2097 FH49_TSSR_TX_STATUS_REG));
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002098 }
2099 spin_unlock_irqrestore(&il->lock, flags);
2100
2101 if (!il->txq)
2102 return;
2103
2104 /* Unmap DMA from host system and free skb's */
2105 for (txq_id = 0; txq_id < il->hw_params.max_txq_num; txq_id++)
2106 if (txq_id == il->cmd_queue)
2107 il_cmd_queue_unmap(il);
2108 else
2109 il_tx_queue_unmap(il, txq_id);
2110}
2111
2112/*
2113 * Find first available (lowest unused) Tx Queue, mark it "active".
2114 * Called only when finding queue for aggregation.
2115 * Should never return anything < 7, because they should already
2116 * be in use as EDCA AC (0-3), Command (4), reserved (5, 6)
2117 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002118static int
2119il4965_txq_ctx_activate_free(struct il_priv *il)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002120{
2121 int txq_id;
2122
2123 for (txq_id = 0; txq_id < il->hw_params.max_txq_num; txq_id++)
2124 if (!test_and_set_bit(txq_id, &il->txq_ctx_active_msk))
2125 return txq_id;
2126 return -1;
2127}
2128
2129/**
2130 * il4965_tx_queue_stop_scheduler - Stop queue, but keep configuration
2131 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002132static void
2133il4965_tx_queue_stop_scheduler(struct il_priv *il, u16 txq_id)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002134{
2135 /* Simply stop the queue, but don't change any configuration;
2136 * the SCD_ACT_EN bit is the write-enable mask for the ACTIVE bit. */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002137 il_wr_prph(il, IL49_SCD_QUEUE_STATUS_BITS(txq_id),
2138 (0 << IL49_SCD_QUEUE_STTS_REG_POS_ACTIVE) | (1 <<
2139 IL49_SCD_QUEUE_STTS_REG_POS_SCD_ACT_EN));
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002140}
2141
2142/**
2143 * il4965_tx_queue_set_q2ratid - Map unique receiver/tid combination to a queue
2144 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002145static int
2146il4965_tx_queue_set_q2ratid(struct il_priv *il, u16 ra_tid, u16 txq_id)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002147{
2148 u32 tbl_dw_addr;
2149 u32 tbl_dw;
2150 u16 scd_q2ratid;
2151
2152 scd_q2ratid = ra_tid & IL_SCD_QUEUE_RA_TID_MAP_RATID_MSK;
2153
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002154 tbl_dw_addr =
2155 il->scd_base_addr + IL49_SCD_TRANSLATE_TBL_OFFSET_QUEUE(txq_id);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002156
2157 tbl_dw = il_read_targ_mem(il, tbl_dw_addr);
2158
2159 if (txq_id & 0x1)
2160 tbl_dw = (scd_q2ratid << 16) | (tbl_dw & 0x0000FFFF);
2161 else
2162 tbl_dw = scd_q2ratid | (tbl_dw & 0xFFFF0000);
2163
2164 il_write_targ_mem(il, tbl_dw_addr, tbl_dw);
2165
2166 return 0;
2167}
2168
2169/**
2170 * il4965_tx_queue_agg_enable - Set up & enable aggregation for selected queue
2171 *
2172 * NOTE: txq_id must be greater than IL49_FIRST_AMPDU_QUEUE,
2173 * i.e. it must be one of the higher queues used for aggregation
2174 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002175static int
2176il4965_txq_agg_enable(struct il_priv *il, int txq_id, int tx_fifo, int sta_id,
2177 int tid, u16 ssn_idx)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002178{
2179 unsigned long flags;
2180 u16 ra_tid;
2181 int ret;
2182
2183 if ((IL49_FIRST_AMPDU_QUEUE > txq_id) ||
2184 (IL49_FIRST_AMPDU_QUEUE +
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002185 il->cfg->base_params->num_of_ampdu_queues <= txq_id)) {
2186 IL_WARN("queue number out of range: %d, must be %d to %d\n",
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002187 txq_id, IL49_FIRST_AMPDU_QUEUE,
2188 IL49_FIRST_AMPDU_QUEUE +
2189 il->cfg->base_params->num_of_ampdu_queues - 1);
2190 return -EINVAL;
2191 }
2192
2193 ra_tid = BUILD_RAxTID(sta_id, tid);
2194
2195 /* Modify device's station table to Tx this TID */
2196 ret = il4965_sta_tx_modify_enable_tid(il, sta_id, tid);
2197 if (ret)
2198 return ret;
2199
2200 spin_lock_irqsave(&il->lock, flags);
2201
2202 /* Stop this Tx queue before configuring it */
2203 il4965_tx_queue_stop_scheduler(il, txq_id);
2204
2205 /* Map receiver-address / traffic-ID to this queue */
2206 il4965_tx_queue_set_q2ratid(il, ra_tid, txq_id);
2207
2208 /* Set this queue as a chain-building queue */
2209 il_set_bits_prph(il, IL49_SCD_QUEUECHAIN_SEL, (1 << txq_id));
2210
2211 /* Place first TFD at idx corresponding to start sequence number.
2212 * Assumes that ssn_idx is valid (!= 0xFFF) */
2213 il->txq[txq_id].q.read_ptr = (ssn_idx & 0xff);
2214 il->txq[txq_id].q.write_ptr = (ssn_idx & 0xff);
2215 il4965_set_wr_ptrs(il, txq_id, ssn_idx);
2216
2217 /* Set up Tx win size and frame limit for this queue */
2218 il_write_targ_mem(il,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002219 il->scd_base_addr +
2220 IL49_SCD_CONTEXT_QUEUE_OFFSET(txq_id),
2221 (SCD_WIN_SIZE << IL49_SCD_QUEUE_CTX_REG1_WIN_SIZE_POS)
2222 & IL49_SCD_QUEUE_CTX_REG1_WIN_SIZE_MSK);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002223
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002224 il_write_targ_mem(il,
2225 il->scd_base_addr +
2226 IL49_SCD_CONTEXT_QUEUE_OFFSET(txq_id) + sizeof(u32),
2227 (SCD_FRAME_LIMIT <<
2228 IL49_SCD_QUEUE_CTX_REG2_FRAME_LIMIT_POS) &
2229 IL49_SCD_QUEUE_CTX_REG2_FRAME_LIMIT_MSK);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002230
2231 il_set_bits_prph(il, IL49_SCD_INTERRUPT_MASK, (1 << txq_id));
2232
2233 /* Set up Status area in SRAM, map to Tx DMA/FIFO, activate the queue */
2234 il4965_tx_queue_set_status(il, &il->txq[txq_id], tx_fifo, 1);
2235
2236 spin_unlock_irqrestore(&il->lock, flags);
2237
2238 return 0;
2239}
2240
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002241int
2242il4965_tx_agg_start(struct il_priv *il, struct ieee80211_vif *vif,
2243 struct ieee80211_sta *sta, u16 tid, u16 * ssn)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002244{
2245 int sta_id;
2246 int tx_fifo;
2247 int txq_id;
2248 int ret;
2249 unsigned long flags;
2250 struct il_tid_data *tid_data;
2251
2252 tx_fifo = il4965_get_fifo_from_tid(il_rxon_ctx_from_vif(vif), tid);
2253 if (unlikely(tx_fifo < 0))
2254 return tx_fifo;
2255
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002256 IL_WARN("%s on ra = %pM tid = %d\n", __func__, sta->addr, tid);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002257
2258 sta_id = il_sta_id(sta);
2259 if (sta_id == IL_INVALID_STATION) {
2260 IL_ERR("Start AGG on invalid station\n");
2261 return -ENXIO;
2262 }
2263 if (unlikely(tid >= MAX_TID_COUNT))
2264 return -EINVAL;
2265
2266 if (il->stations[sta_id].tid[tid].agg.state != IL_AGG_OFF) {
2267 IL_ERR("Start AGG when state is not IL_AGG_OFF !\n");
2268 return -ENXIO;
2269 }
2270
2271 txq_id = il4965_txq_ctx_activate_free(il);
2272 if (txq_id == -1) {
2273 IL_ERR("No free aggregation queue available\n");
2274 return -ENXIO;
2275 }
2276
2277 spin_lock_irqsave(&il->sta_lock, flags);
2278 tid_data = &il->stations[sta_id].tid[tid];
2279 *ssn = SEQ_TO_SN(tid_data->seq_number);
2280 tid_data->agg.txq_id = txq_id;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002281 il_set_swq_id(&il->txq[txq_id], il4965_get_ac_from_tid(tid), txq_id);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002282 spin_unlock_irqrestore(&il->sta_lock, flags);
2283
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002284 ret = il4965_txq_agg_enable(il, txq_id, tx_fifo, sta_id, tid, *ssn);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002285 if (ret)
2286 return ret;
2287
2288 spin_lock_irqsave(&il->sta_lock, flags);
2289 tid_data = &il->stations[sta_id].tid[tid];
2290 if (tid_data->tfds_in_queue == 0) {
2291 D_HT("HW queue is empty\n");
2292 tid_data->agg.state = IL_AGG_ON;
2293 ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2294 } else {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002295 D_HT("HW queue is NOT empty: %d packets in HW queue\n",
2296 tid_data->tfds_in_queue);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002297 tid_data->agg.state = IL_EMPTYING_HW_QUEUE_ADDBA;
2298 }
2299 spin_unlock_irqrestore(&il->sta_lock, flags);
2300 return ret;
2301}
2302
2303/**
2304 * txq_id must be greater than IL49_FIRST_AMPDU_QUEUE
2305 * il->lock must be held by the caller
2306 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002307static int
2308il4965_txq_agg_disable(struct il_priv *il, u16 txq_id, u16 ssn_idx, u8 tx_fifo)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002309{
2310 if ((IL49_FIRST_AMPDU_QUEUE > txq_id) ||
2311 (IL49_FIRST_AMPDU_QUEUE +
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002312 il->cfg->base_params->num_of_ampdu_queues <= txq_id)) {
2313 IL_WARN("queue number out of range: %d, must be %d to %d\n",
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002314 txq_id, IL49_FIRST_AMPDU_QUEUE,
2315 IL49_FIRST_AMPDU_QUEUE +
2316 il->cfg->base_params->num_of_ampdu_queues - 1);
2317 return -EINVAL;
2318 }
2319
2320 il4965_tx_queue_stop_scheduler(il, txq_id);
2321
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002322 il_clear_bits_prph(il, IL49_SCD_QUEUECHAIN_SEL, (1 << txq_id));
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002323
2324 il->txq[txq_id].q.read_ptr = (ssn_idx & 0xff);
2325 il->txq[txq_id].q.write_ptr = (ssn_idx & 0xff);
2326 /* supposes that ssn_idx is valid (!= 0xFFF) */
2327 il4965_set_wr_ptrs(il, txq_id, ssn_idx);
2328
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002329 il_clear_bits_prph(il, IL49_SCD_INTERRUPT_MASK, (1 << txq_id));
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002330 il_txq_ctx_deactivate(il, txq_id);
2331 il4965_tx_queue_set_status(il, &il->txq[txq_id], tx_fifo, 0);
2332
2333 return 0;
2334}
2335
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002336int
2337il4965_tx_agg_stop(struct il_priv *il, struct ieee80211_vif *vif,
2338 struct ieee80211_sta *sta, u16 tid)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002339{
2340 int tx_fifo_id, txq_id, sta_id, ssn;
2341 struct il_tid_data *tid_data;
2342 int write_ptr, read_ptr;
2343 unsigned long flags;
2344
2345 tx_fifo_id = il4965_get_fifo_from_tid(il_rxon_ctx_from_vif(vif), tid);
2346 if (unlikely(tx_fifo_id < 0))
2347 return tx_fifo_id;
2348
2349 sta_id = il_sta_id(sta);
2350
2351 if (sta_id == IL_INVALID_STATION) {
2352 IL_ERR("Invalid station for AGG tid %d\n", tid);
2353 return -ENXIO;
2354 }
2355
2356 spin_lock_irqsave(&il->sta_lock, flags);
2357
2358 tid_data = &il->stations[sta_id].tid[tid];
2359 ssn = (tid_data->seq_number & IEEE80211_SCTL_SEQ) >> 4;
2360 txq_id = tid_data->agg.txq_id;
2361
2362 switch (il->stations[sta_id].tid[tid].agg.state) {
2363 case IL_EMPTYING_HW_QUEUE_ADDBA:
2364 /*
2365 * This can happen if the peer stops aggregation
2366 * again before we've had a chance to drain the
2367 * queue we selected previously, i.e. before the
2368 * session was really started completely.
2369 */
2370 D_HT("AGG stop before setup done\n");
2371 goto turn_off;
2372 case IL_AGG_ON:
2373 break;
2374 default:
2375 IL_WARN("Stopping AGG while state not ON or starting\n");
2376 }
2377
2378 write_ptr = il->txq[txq_id].q.write_ptr;
2379 read_ptr = il->txq[txq_id].q.read_ptr;
2380
2381 /* The queue is not empty */
2382 if (write_ptr != read_ptr) {
2383 D_HT("Stopping a non empty AGG HW QUEUE\n");
2384 il->stations[sta_id].tid[tid].agg.state =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002385 IL_EMPTYING_HW_QUEUE_DELBA;
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002386 spin_unlock_irqrestore(&il->sta_lock, flags);
2387 return 0;
2388 }
2389
2390 D_HT("HW queue is empty\n");
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002391turn_off:
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002392 il->stations[sta_id].tid[tid].agg.state = IL_AGG_OFF;
2393
2394 /* do not restore/save irqs */
2395 spin_unlock(&il->sta_lock);
2396 spin_lock(&il->lock);
2397
2398 /*
2399 * the only reason this call can fail is queue number out of range,
2400 * which can happen if uCode is reloaded and all the station
2401 * information are lost. if it is outside the range, there is no need
2402 * to deactivate the uCode queue, just return "success" to allow
2403 * mac80211 to clean up it own data.
2404 */
2405 il4965_txq_agg_disable(il, txq_id, ssn, tx_fifo_id);
2406 spin_unlock_irqrestore(&il->lock, flags);
2407
2408 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2409
2410 return 0;
2411}
2412
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002413int
2414il4965_txq_check_empty(struct il_priv *il, int sta_id, u8 tid, int txq_id)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002415{
2416 struct il_queue *q = &il->txq[txq_id].q;
2417 u8 *addr = il->stations[sta_id].sta.sta.addr;
2418 struct il_tid_data *tid_data = &il->stations[sta_id].tid[tid];
2419 struct il_rxon_context *ctx;
2420
2421 ctx = &il->ctx;
2422
2423 lockdep_assert_held(&il->sta_lock);
2424
2425 switch (il->stations[sta_id].tid[tid].agg.state) {
2426 case IL_EMPTYING_HW_QUEUE_DELBA:
2427 /* We are reclaiming the last packet of the */
2428 /* aggregated HW queue */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002429 if (txq_id == tid_data->agg.txq_id &&
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002430 q->read_ptr == q->write_ptr) {
2431 u16 ssn = SEQ_TO_SN(tid_data->seq_number);
2432 int tx_fifo = il4965_get_fifo_from_tid(ctx, tid);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002433 D_HT("HW queue empty: continue DELBA flow\n");
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002434 il4965_txq_agg_disable(il, txq_id, ssn, tx_fifo);
2435 tid_data->agg.state = IL_AGG_OFF;
2436 ieee80211_stop_tx_ba_cb_irqsafe(ctx->vif, addr, tid);
2437 }
2438 break;
2439 case IL_EMPTYING_HW_QUEUE_ADDBA:
2440 /* We are reclaiming the last packet of the queue */
2441 if (tid_data->tfds_in_queue == 0) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002442 D_HT("HW queue empty: continue ADDBA flow\n");
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002443 tid_data->agg.state = IL_AGG_ON;
2444 ieee80211_start_tx_ba_cb_irqsafe(ctx->vif, addr, tid);
2445 }
2446 break;
2447 }
2448
2449 return 0;
2450}
2451
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002452static void
2453il4965_non_agg_tx_status(struct il_priv *il, struct il_rxon_context *ctx,
2454 const u8 * addr1)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002455{
2456 struct ieee80211_sta *sta;
2457 struct il_station_priv *sta_priv;
2458
2459 rcu_read_lock();
2460 sta = ieee80211_find_sta(ctx->vif, addr1);
2461 if (sta) {
2462 sta_priv = (void *)sta->drv_priv;
2463 /* avoid atomic ops if this isn't a client */
2464 if (sta_priv->client &&
2465 atomic_dec_return(&sta_priv->pending_frames) == 0)
2466 ieee80211_sta_block_awake(il->hw, sta, false);
2467 }
2468 rcu_read_unlock();
2469}
2470
2471static void
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002472il4965_tx_status(struct il_priv *il, struct il_tx_info *tx_info, bool is_agg)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002473{
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002474 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx_info->skb->data;
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002475
2476 if (!is_agg)
2477 il4965_non_agg_tx_status(il, tx_info->ctx, hdr->addr1);
2478
2479 ieee80211_tx_status_irqsafe(il->hw, tx_info->skb);
2480}
2481
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002482int
2483il4965_tx_queue_reclaim(struct il_priv *il, int txq_id, int idx)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002484{
2485 struct il_tx_queue *txq = &il->txq[txq_id];
2486 struct il_queue *q = &txq->q;
2487 struct il_tx_info *tx_info;
2488 int nfreed = 0;
2489 struct ieee80211_hdr *hdr;
2490
2491 if (idx >= q->n_bd || il_queue_used(q, idx) == 0) {
2492 IL_ERR("Read idx for DMA queue txq id (%d), idx %d, "
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002493 "is out of range [0-%d] %d %d.\n", txq_id, idx, q->n_bd,
2494 q->write_ptr, q->read_ptr);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002495 return 0;
2496 }
2497
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002498 for (idx = il_queue_inc_wrap(idx, q->n_bd); q->read_ptr != idx;
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002499 q->read_ptr = il_queue_inc_wrap(q->read_ptr, q->n_bd)) {
2500
2501 tx_info = &txq->txb[txq->q.read_ptr];
2502
2503 if (WARN_ON_ONCE(tx_info->skb == NULL))
2504 continue;
2505
2506 hdr = (struct ieee80211_hdr *)tx_info->skb->data;
2507 if (ieee80211_is_data_qos(hdr->frame_control))
2508 nfreed++;
2509
2510 il4965_tx_status(il, tx_info,
2511 txq_id >= IL4965_FIRST_AMPDU_QUEUE);
2512 tx_info->skb = NULL;
2513
2514 il->cfg->ops->lib->txq_free_tfd(il, txq);
2515 }
2516 return nfreed;
2517}
2518
2519/**
2520 * il4965_tx_status_reply_compressed_ba - Update tx status from block-ack
2521 *
2522 * Go through block-ack's bitmap of ACK'd frames, update driver's record of
2523 * ACK vs. not. This gets sent to mac80211, then to rate scaling algo.
2524 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002525static int
2526il4965_tx_status_reply_compressed_ba(struct il_priv *il, struct il_ht_agg *agg,
2527 struct il_compressed_ba_resp *ba_resp)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002528{
2529 int i, sh, ack;
2530 u16 seq_ctl = le16_to_cpu(ba_resp->seq_ctl);
2531 u16 scd_flow = le16_to_cpu(ba_resp->scd_flow);
2532 int successes = 0;
2533 struct ieee80211_tx_info *info;
2534 u64 bitmap, sent_bitmap;
2535
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002536 if (unlikely(!agg->wait_for_ba)) {
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002537 if (unlikely(ba_resp->bitmap))
2538 IL_ERR("Received BA when not expected\n");
2539 return -EINVAL;
2540 }
2541
2542 /* Mark that the expected block-ack response arrived */
2543 agg->wait_for_ba = 0;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002544 D_TX_REPLY("BA %d %d\n", agg->start_idx, ba_resp->seq_ctl);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002545
2546 /* Calculate shift to align block-ack bits with our Tx win bits */
2547 sh = agg->start_idx - SEQ_TO_IDX(seq_ctl >> 4);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002548 if (sh < 0) /* tbw something is wrong with indices */
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002549 sh += 0x100;
2550
2551 if (agg->frame_count > (64 - sh)) {
2552 D_TX_REPLY("more frames than bitmap size");
2553 return -1;
2554 }
2555
2556 /* don't use 64-bit values for now */
2557 bitmap = le64_to_cpu(ba_resp->bitmap) >> sh;
2558
2559 /* check for success or failure according to the
2560 * transmitted bitmap and block-ack bitmap */
2561 sent_bitmap = bitmap & agg->bitmap;
2562
2563 /* For each frame attempted in aggregation,
2564 * update driver's record of tx frame's status. */
2565 i = 0;
2566 while (sent_bitmap) {
2567 ack = sent_bitmap & 1ULL;
2568 successes += ack;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002569 D_TX_REPLY("%s ON i=%d idx=%d raw=%d\n", ack ? "ACK" : "NACK",
2570 i, (agg->start_idx + i) & 0xff, agg->start_idx + i);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002571 sent_bitmap >>= 1;
2572 ++i;
2573 }
2574
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002575 D_TX_REPLY("Bitmap %llx\n", (unsigned long long)bitmap);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002576
2577 info = IEEE80211_SKB_CB(il->txq[scd_flow].txb[agg->start_idx].skb);
2578 memset(&info->status, 0, sizeof(info->status));
2579 info->flags |= IEEE80211_TX_STAT_ACK;
2580 info->flags |= IEEE80211_TX_STAT_AMPDU;
2581 info->status.ampdu_ack_len = successes;
2582 info->status.ampdu_len = agg->frame_count;
2583 il4965_hwrate_to_tx_control(il, agg->rate_n_flags, info);
2584
2585 return 0;
2586}
2587
2588/**
2589 * translate ucode response to mac80211 tx status control values
2590 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002591void
2592il4965_hwrate_to_tx_control(struct il_priv *il, u32 rate_n_flags,
2593 struct ieee80211_tx_info *info)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002594{
2595 struct ieee80211_tx_rate *r = &info->control.rates[0];
2596
2597 info->antenna_sel_tx =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002598 ((rate_n_flags & RATE_MCS_ANT_ABC_MSK) >> RATE_MCS_ANT_POS);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002599 if (rate_n_flags & RATE_MCS_HT_MSK)
2600 r->flags |= IEEE80211_TX_RC_MCS;
2601 if (rate_n_flags & RATE_MCS_GF_MSK)
2602 r->flags |= IEEE80211_TX_RC_GREEN_FIELD;
2603 if (rate_n_flags & RATE_MCS_HT40_MSK)
2604 r->flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
2605 if (rate_n_flags & RATE_MCS_DUP_MSK)
2606 r->flags |= IEEE80211_TX_RC_DUP_DATA;
2607 if (rate_n_flags & RATE_MCS_SGI_MSK)
2608 r->flags |= IEEE80211_TX_RC_SHORT_GI;
2609 r->idx = il4965_hwrate_to_mac80211_idx(rate_n_flags, info->band);
2610}
2611
2612/**
Stanislaw Gruszka6e9848b42011-08-30 15:45:31 +02002613 * il4965_hdl_compressed_ba - Handler for N_COMPRESSED_BA
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002614 *
2615 * Handles block-acknowledge notification from device, which reports success
2616 * of frames sent via aggregation.
2617 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002618void
2619il4965_hdl_compressed_ba(struct il_priv *il, struct il_rx_buf *rxb)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002620{
2621 struct il_rx_pkt *pkt = rxb_addr(rxb);
2622 struct il_compressed_ba_resp *ba_resp = &pkt->u.compressed_ba;
2623 struct il_tx_queue *txq = NULL;
2624 struct il_ht_agg *agg;
2625 int idx;
2626 int sta_id;
2627 int tid;
2628 unsigned long flags;
2629
2630 /* "flow" corresponds to Tx queue */
2631 u16 scd_flow = le16_to_cpu(ba_resp->scd_flow);
2632
2633 /* "ssn" is start of block-ack Tx win, corresponds to idx
2634 * (in Tx queue's circular buffer) of first TFD/frame in win */
2635 u16 ba_resp_scd_ssn = le16_to_cpu(ba_resp->scd_ssn);
2636
2637 if (scd_flow >= il->hw_params.max_txq_num) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002638 IL_ERR("BUG_ON scd_flow is bigger than number of queues\n");
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002639 return;
2640 }
2641
2642 txq = &il->txq[scd_flow];
2643 sta_id = ba_resp->sta_id;
2644 tid = ba_resp->tid;
2645 agg = &il->stations[sta_id].tid[tid].agg;
2646 if (unlikely(agg->txq_id != scd_flow)) {
2647 /*
2648 * FIXME: this is a uCode bug which need to be addressed,
2649 * log the information and return for now!
2650 * since it is possible happen very often and in order
2651 * not to fill the syslog, don't enable the logging by default
2652 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002653 D_TX_REPLY("BA scd_flow %d does not match txq_id %d\n",
2654 scd_flow, agg->txq_id);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002655 return;
2656 }
2657
2658 /* Find idx just before block-ack win */
2659 idx = il_queue_dec_wrap(ba_resp_scd_ssn & 0xff, txq->q.n_bd);
2660
2661 spin_lock_irqsave(&il->sta_lock, flags);
2662
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002663 D_TX_REPLY("N_COMPRESSED_BA [%d] Received from %pM, " "sta_id = %d\n",
2664 agg->wait_for_ba, (u8 *) & ba_resp->sta_addr_lo32,
2665 ba_resp->sta_id);
2666 D_TX_REPLY("TID = %d, SeqCtl = %d, bitmap = 0x%llx," "scd_flow = "
2667 "%d, scd_ssn = %d\n", ba_resp->tid, ba_resp->seq_ctl,
2668 (unsigned long long)le64_to_cpu(ba_resp->bitmap),
2669 ba_resp->scd_flow, ba_resp->scd_ssn);
2670 D_TX_REPLY("DAT start_idx = %d, bitmap = 0x%llx\n", agg->start_idx,
2671 (unsigned long long)agg->bitmap);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002672
2673 /* Update driver's record of ACK vs. not for each frame in win */
2674 il4965_tx_status_reply_compressed_ba(il, agg, ba_resp);
2675
2676 /* Release all TFDs before the SSN, i.e. all TFDs in front of
2677 * block-ack win (we assume that they've been successfully
2678 * transmitted ... if not, it's too late anyway). */
2679 if (txq->q.read_ptr != (ba_resp_scd_ssn & 0xff)) {
2680 /* calculate mac80211 ampdu sw queue to wake */
2681 int freed = il4965_tx_queue_reclaim(il, scd_flow, idx);
2682 il4965_free_tfds_in_queue(il, sta_id, tid, freed);
2683
2684 if (il_queue_space(&txq->q) > txq->q.low_mark &&
2685 il->mac80211_registered &&
2686 agg->state != IL_EMPTYING_HW_QUEUE_DELBA)
2687 il_wake_queue(il, txq);
2688
2689 il4965_txq_check_empty(il, sta_id, tid, scd_flow);
2690 }
2691
2692 spin_unlock_irqrestore(&il->sta_lock, flags);
2693}
2694
2695#ifdef CONFIG_IWLEGACY_DEBUG
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002696const char *
2697il4965_get_tx_fail_reason(u32 status)
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002698{
2699#define TX_STATUS_FAIL(x) case TX_STATUS_FAIL_ ## x: return #x
2700#define TX_STATUS_POSTPONE(x) case TX_STATUS_POSTPONE_ ## x: return #x
2701
2702 switch (status & TX_STATUS_MSK) {
2703 case TX_STATUS_SUCCESS:
2704 return "SUCCESS";
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002705 TX_STATUS_POSTPONE(DELAY);
2706 TX_STATUS_POSTPONE(FEW_BYTES);
2707 TX_STATUS_POSTPONE(QUIET_PERIOD);
2708 TX_STATUS_POSTPONE(CALC_TTAK);
2709 TX_STATUS_FAIL(INTERNAL_CROSSED_RETRY);
2710 TX_STATUS_FAIL(SHORT_LIMIT);
2711 TX_STATUS_FAIL(LONG_LIMIT);
2712 TX_STATUS_FAIL(FIFO_UNDERRUN);
2713 TX_STATUS_FAIL(DRAIN_FLOW);
2714 TX_STATUS_FAIL(RFKILL_FLUSH);
2715 TX_STATUS_FAIL(LIFE_EXPIRE);
2716 TX_STATUS_FAIL(DEST_PS);
2717 TX_STATUS_FAIL(HOST_ABORTED);
2718 TX_STATUS_FAIL(BT_RETRY);
2719 TX_STATUS_FAIL(STA_INVALID);
2720 TX_STATUS_FAIL(FRAG_DROPPED);
2721 TX_STATUS_FAIL(TID_DISABLE);
2722 TX_STATUS_FAIL(FIFO_FLUSHED);
2723 TX_STATUS_FAIL(INSUFFICIENT_CF_POLL);
2724 TX_STATUS_FAIL(PASSIVE_NO_RX);
2725 TX_STATUS_FAIL(NO_BEACON_ON_RADAR);
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01002726 }
2727
2728 return "UNKNOWN";
2729
2730#undef TX_STATUS_FAIL
2731#undef TX_STATUS_POSTPONE
2732}
2733#endif /* CONFIG_IWLEGACY_DEBUG */
2734
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002735static struct il_link_quality_cmd *
2736il4965_sta_alloc_lq(struct il_priv *il, u8 sta_id)
2737{
2738 int i, r;
2739 struct il_link_quality_cmd *link_cmd;
2740 u32 rate_flags = 0;
2741 __le32 rate_n_flags;
2742
2743 link_cmd = kzalloc(sizeof(struct il_link_quality_cmd), GFP_KERNEL);
2744 if (!link_cmd) {
2745 IL_ERR("Unable to allocate memory for LQ cmd.\n");
2746 return NULL;
2747 }
2748 /* Set up the rate scaling to start at selected rate, fall back
2749 * all the way down to 1M in IEEE order, and then spin on 1M */
2750 if (il->band == IEEE80211_BAND_5GHZ)
2751 r = RATE_6M_IDX;
2752 else
2753 r = RATE_1M_IDX;
2754
2755 if (r >= IL_FIRST_CCK_RATE && r <= IL_LAST_CCK_RATE)
2756 rate_flags |= RATE_MCS_CCK_MSK;
2757
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002758 rate_flags |=
2759 il4965_first_antenna(il->hw_params.
2760 valid_tx_ant) << RATE_MCS_ANT_POS;
2761 rate_n_flags = il4965_hw_set_rate_n_flags(il_rates[r].plcp, rate_flags);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002762 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
2763 link_cmd->rs_table[i].rate_n_flags = rate_n_flags;
2764
2765 link_cmd->general_params.single_stream_ant_msk =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002766 il4965_first_antenna(il->hw_params.valid_tx_ant);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002767
2768 link_cmd->general_params.dual_stream_ant_msk =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002769 il->hw_params.valid_tx_ant & ~il4965_first_antenna(il->hw_params.
2770 valid_tx_ant);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002771 if (!link_cmd->general_params.dual_stream_ant_msk) {
2772 link_cmd->general_params.dual_stream_ant_msk = ANT_AB;
2773 } else if (il4965_num_of_ant(il->hw_params.valid_tx_ant) == 2) {
2774 link_cmd->general_params.dual_stream_ant_msk =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002775 il->hw_params.valid_tx_ant;
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002776 }
2777
2778 link_cmd->agg_params.agg_dis_start_th = LINK_QUAL_AGG_DISABLE_START_DEF;
2779 link_cmd->agg_params.agg_time_limit =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002780 cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002781
2782 link_cmd->sta_id = sta_id;
2783
2784 return link_cmd;
2785}
2786
2787/*
2788 * il4965_add_bssid_station - Add the special IBSS BSSID station
2789 *
2790 * Function sleeps.
2791 */
2792int
2793il4965_add_bssid_station(struct il_priv *il, struct il_rxon_context *ctx,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002794 const u8 * addr, u8 * sta_id_r)
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002795{
2796 int ret;
2797 u8 sta_id;
2798 struct il_link_quality_cmd *link_cmd;
2799 unsigned long flags;
2800
2801 if (sta_id_r)
2802 *sta_id_r = IL_INVALID_STATION;
2803
2804 ret = il_add_station_common(il, ctx, addr, 0, NULL, &sta_id);
2805 if (ret) {
2806 IL_ERR("Unable to add station %pM\n", addr);
2807 return ret;
2808 }
2809
2810 if (sta_id_r)
2811 *sta_id_r = sta_id;
2812
2813 spin_lock_irqsave(&il->sta_lock, flags);
2814 il->stations[sta_id].used |= IL_STA_LOCAL;
2815 spin_unlock_irqrestore(&il->sta_lock, flags);
2816
2817 /* Set up default rate scaling table in device's station table */
2818 link_cmd = il4965_sta_alloc_lq(il, sta_id);
2819 if (!link_cmd) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002820 IL_ERR("Unable to initialize rate scaling for station %pM.\n",
2821 addr);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002822 return -ENOMEM;
2823 }
2824
2825 ret = il_send_lq_cmd(il, ctx, link_cmd, CMD_SYNC, true);
2826 if (ret)
2827 IL_ERR("Link quality command failed (%d)\n", ret);
2828
2829 spin_lock_irqsave(&il->sta_lock, flags);
2830 il->stations[sta_id].lq = link_cmd;
2831 spin_unlock_irqrestore(&il->sta_lock, flags);
2832
2833 return 0;
2834}
2835
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002836static int
2837il4965_static_wepkey_cmd(struct il_priv *il, struct il_rxon_context *ctx,
2838 bool send_if_empty)
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002839{
2840 int i, not_empty = 0;
2841 u8 buff[sizeof(struct il_wep_cmd) +
2842 sizeof(struct il_wep_key) * WEP_KEYS_MAX];
2843 struct il_wep_cmd *wep_cmd = (struct il_wep_cmd *)buff;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002844 size_t cmd_size = sizeof(struct il_wep_cmd);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002845 struct il_host_cmd cmd = {
2846 .id = ctx->wep_key_cmd,
2847 .data = wep_cmd,
2848 .flags = CMD_SYNC,
2849 };
2850
2851 might_sleep();
2852
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002853 memset(wep_cmd, 0,
2854 cmd_size + (sizeof(struct il_wep_key) * WEP_KEYS_MAX));
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002855
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002856 for (i = 0; i < WEP_KEYS_MAX; i++) {
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002857 wep_cmd->key[i].key_idx = i;
2858 if (ctx->wep_keys[i].key_size) {
2859 wep_cmd->key[i].key_offset = i;
2860 not_empty = 1;
2861 } else {
2862 wep_cmd->key[i].key_offset = WEP_INVALID_OFFSET;
2863 }
2864
2865 wep_cmd->key[i].key_size = ctx->wep_keys[i].key_size;
2866 memcpy(&wep_cmd->key[i].key[3], ctx->wep_keys[i].key,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002867 ctx->wep_keys[i].key_size);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002868 }
2869
2870 wep_cmd->global_key_type = WEP_KEY_WEP_TYPE;
2871 wep_cmd->num_keys = WEP_KEYS_MAX;
2872
2873 cmd_size += sizeof(struct il_wep_key) * WEP_KEYS_MAX;
2874
2875 cmd.len = cmd_size;
2876
2877 if (not_empty || send_if_empty)
2878 return il_send_cmd(il, &cmd);
2879 else
2880 return 0;
2881}
2882
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002883int
2884il4965_restore_default_wep_keys(struct il_priv *il, struct il_rxon_context *ctx)
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002885{
2886 lockdep_assert_held(&il->mutex);
2887
2888 return il4965_static_wepkey_cmd(il, ctx, false);
2889}
2890
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002891int
2892il4965_remove_default_wep_key(struct il_priv *il, struct il_rxon_context *ctx,
2893 struct ieee80211_key_conf *keyconf)
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002894{
2895 int ret;
2896
2897 lockdep_assert_held(&il->mutex);
2898
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002899 D_WEP("Removing default WEP key: idx=%d\n", keyconf->keyidx);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002900
2901 memset(&ctx->wep_keys[keyconf->keyidx], 0, sizeof(ctx->wep_keys[0]));
2902 if (il_is_rfkill(il)) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002903 D_WEP("Not sending C_WEPKEY command due to RFKILL.\n");
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002904 /* but keys in device are clear anyway so return success */
2905 return 0;
2906 }
2907 ret = il4965_static_wepkey_cmd(il, ctx, 1);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002908 D_WEP("Remove default WEP key: idx=%d ret=%d\n", keyconf->keyidx, ret);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002909
2910 return ret;
2911}
2912
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002913int
2914il4965_set_default_wep_key(struct il_priv *il, struct il_rxon_context *ctx,
2915 struct ieee80211_key_conf *keyconf)
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002916{
2917 int ret;
2918
2919 lockdep_assert_held(&il->mutex);
2920
2921 if (keyconf->keylen != WEP_KEY_LEN_128 &&
2922 keyconf->keylen != WEP_KEY_LEN_64) {
2923 D_WEP("Bad WEP key length %d\n", keyconf->keylen);
2924 return -EINVAL;
2925 }
2926
2927 keyconf->flags &= ~IEEE80211_KEY_FLAG_GENERATE_IV;
2928 keyconf->hw_key_idx = HW_KEY_DEFAULT;
2929 il->stations[ctx->ap_sta_id].keyinfo.cipher = keyconf->cipher;
2930
2931 ctx->wep_keys[keyconf->keyidx].key_size = keyconf->keylen;
2932 memcpy(&ctx->wep_keys[keyconf->keyidx].key, &keyconf->key,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002933 keyconf->keylen);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002934
2935 ret = il4965_static_wepkey_cmd(il, ctx, false);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002936 D_WEP("Set default WEP key: len=%d idx=%d ret=%d\n", keyconf->keylen,
2937 keyconf->keyidx, ret);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002938
2939 return ret;
2940}
2941
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002942static int
2943il4965_set_wep_dynamic_key_info(struct il_priv *il, struct il_rxon_context *ctx,
2944 struct ieee80211_key_conf *keyconf, u8 sta_id)
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002945{
2946 unsigned long flags;
2947 __le16 key_flags = 0;
2948 struct il_addsta_cmd sta_cmd;
2949
2950 lockdep_assert_held(&il->mutex);
2951
2952 keyconf->flags &= ~IEEE80211_KEY_FLAG_GENERATE_IV;
2953
2954 key_flags |= (STA_KEY_FLG_WEP | STA_KEY_FLG_MAP_KEY_MSK);
2955 key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
2956 key_flags &= ~STA_KEY_FLG_INVALID;
2957
2958 if (keyconf->keylen == WEP_KEY_LEN_128)
2959 key_flags |= STA_KEY_FLG_KEY_SIZE_MSK;
2960
2961 if (sta_id == ctx->bcast_sta_id)
2962 key_flags |= STA_KEY_MULTICAST_MSK;
2963
2964 spin_lock_irqsave(&il->sta_lock, flags);
2965
2966 il->stations[sta_id].keyinfo.cipher = keyconf->cipher;
2967 il->stations[sta_id].keyinfo.keylen = keyconf->keylen;
2968 il->stations[sta_id].keyinfo.keyidx = keyconf->keyidx;
2969
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002970 memcpy(il->stations[sta_id].keyinfo.key, keyconf->key, keyconf->keylen);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002971
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002972 memcpy(&il->stations[sta_id].sta.key.key[3], keyconf->key,
2973 keyconf->keylen);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002974
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002975 if ((il->stations[sta_id].sta.key.
2976 key_flags & STA_KEY_FLG_ENCRYPT_MSK) == STA_KEY_FLG_NO_ENC)
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002977 il->stations[sta_id].sta.key.key_offset =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002978 il_get_free_ucode_key_idx(il);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002979 /* else, we are overriding an existing key => no need to allocated room
2980 * in uCode. */
2981
2982 WARN(il->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002983 "no space for a new key");
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002984
2985 il->stations[sta_id].sta.key.key_flags = key_flags;
2986 il->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
2987 il->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
2988
2989 memcpy(&sta_cmd, &il->stations[sta_id].sta,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002990 sizeof(struct il_addsta_cmd));
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002991 spin_unlock_irqrestore(&il->sta_lock, flags);
2992
2993 return il_send_add_sta(il, &sta_cmd, CMD_SYNC);
2994}
2995
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01002996static int
2997il4965_set_ccmp_dynamic_key_info(struct il_priv *il,
2998 struct il_rxon_context *ctx,
2999 struct ieee80211_key_conf *keyconf, u8 sta_id)
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003000{
3001 unsigned long flags;
3002 __le16 key_flags = 0;
3003 struct il_addsta_cmd sta_cmd;
3004
3005 lockdep_assert_held(&il->mutex);
3006
3007 key_flags |= (STA_KEY_FLG_CCMP | STA_KEY_FLG_MAP_KEY_MSK);
3008 key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
3009 key_flags &= ~STA_KEY_FLG_INVALID;
3010
3011 if (sta_id == ctx->bcast_sta_id)
3012 key_flags |= STA_KEY_MULTICAST_MSK;
3013
3014 keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
3015
3016 spin_lock_irqsave(&il->sta_lock, flags);
3017 il->stations[sta_id].keyinfo.cipher = keyconf->cipher;
3018 il->stations[sta_id].keyinfo.keylen = keyconf->keylen;
3019
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003020 memcpy(il->stations[sta_id].keyinfo.key, keyconf->key, keyconf->keylen);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003021
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003022 memcpy(il->stations[sta_id].sta.key.key, keyconf->key, keyconf->keylen);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003023
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003024 if ((il->stations[sta_id].sta.key.
3025 key_flags & STA_KEY_FLG_ENCRYPT_MSK) == STA_KEY_FLG_NO_ENC)
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003026 il->stations[sta_id].sta.key.key_offset =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003027 il_get_free_ucode_key_idx(il);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003028 /* else, we are overriding an existing key => no need to allocated room
3029 * in uCode. */
3030
3031 WARN(il->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003032 "no space for a new key");
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003033
3034 il->stations[sta_id].sta.key.key_flags = key_flags;
3035 il->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
3036 il->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
3037
3038 memcpy(&sta_cmd, &il->stations[sta_id].sta,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003039 sizeof(struct il_addsta_cmd));
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003040 spin_unlock_irqrestore(&il->sta_lock, flags);
3041
3042 return il_send_add_sta(il, &sta_cmd, CMD_SYNC);
3043}
3044
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003045static int
3046il4965_set_tkip_dynamic_key_info(struct il_priv *il,
3047 struct il_rxon_context *ctx,
3048 struct ieee80211_key_conf *keyconf, u8 sta_id)
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003049{
3050 unsigned long flags;
3051 int ret = 0;
3052 __le16 key_flags = 0;
3053
3054 key_flags |= (STA_KEY_FLG_TKIP | STA_KEY_FLG_MAP_KEY_MSK);
3055 key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
3056 key_flags &= ~STA_KEY_FLG_INVALID;
3057
3058 if (sta_id == ctx->bcast_sta_id)
3059 key_flags |= STA_KEY_MULTICAST_MSK;
3060
3061 keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
3062 keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
3063
3064 spin_lock_irqsave(&il->sta_lock, flags);
3065
3066 il->stations[sta_id].keyinfo.cipher = keyconf->cipher;
3067 il->stations[sta_id].keyinfo.keylen = 16;
3068
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003069 if ((il->stations[sta_id].sta.key.
3070 key_flags & STA_KEY_FLG_ENCRYPT_MSK) == STA_KEY_FLG_NO_ENC)
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003071 il->stations[sta_id].sta.key.key_offset =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003072 il_get_free_ucode_key_idx(il);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003073 /* else, we are overriding an existing key => no need to allocated room
3074 * in uCode. */
3075
3076 WARN(il->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003077 "no space for a new key");
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003078
3079 il->stations[sta_id].sta.key.key_flags = key_flags;
3080
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003081 /* This copy is acutally not needed: we get the key with each TX */
3082 memcpy(il->stations[sta_id].keyinfo.key, keyconf->key, 16);
3083
3084 memcpy(il->stations[sta_id].sta.key.key, keyconf->key, 16);
3085
3086 spin_unlock_irqrestore(&il->sta_lock, flags);
3087
3088 return ret;
3089}
3090
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003091void
3092il4965_update_tkip_key(struct il_priv *il, struct il_rxon_context *ctx,
3093 struct ieee80211_key_conf *keyconf,
3094 struct ieee80211_sta *sta, u32 iv32, u16 * phase1key)
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003095{
3096 u8 sta_id;
3097 unsigned long flags;
3098 int i;
3099
3100 if (il_scan_cancel(il)) {
3101 /* cancel scan failed, just live w/ bad key and rely
3102 briefly on SW decryption */
3103 return;
3104 }
3105
3106 sta_id = il_sta_id_or_broadcast(il, ctx, sta);
3107 if (sta_id == IL_INVALID_STATION)
3108 return;
3109
3110 spin_lock_irqsave(&il->sta_lock, flags);
3111
3112 il->stations[sta_id].sta.key.tkip_rx_tsc_byte2 = (u8) iv32;
3113
3114 for (i = 0; i < 5; i++)
3115 il->stations[sta_id].sta.key.tkip_rx_ttak[i] =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003116 cpu_to_le16(phase1key[i]);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003117
3118 il->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
3119 il->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
3120
3121 il_send_add_sta(il, &il->stations[sta_id].sta, CMD_ASYNC);
3122
3123 spin_unlock_irqrestore(&il->sta_lock, flags);
3124
3125}
3126
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003127int
3128il4965_remove_dynamic_key(struct il_priv *il, struct il_rxon_context *ctx,
3129 struct ieee80211_key_conf *keyconf, u8 sta_id)
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003130{
3131 unsigned long flags;
3132 u16 key_flags;
3133 u8 keyidx;
3134 struct il_addsta_cmd sta_cmd;
3135
3136 lockdep_assert_held(&il->mutex);
3137
3138 ctx->key_mapping_keys--;
3139
3140 spin_lock_irqsave(&il->sta_lock, flags);
3141 key_flags = le16_to_cpu(il->stations[sta_id].sta.key.key_flags);
3142 keyidx = (key_flags >> STA_KEY_FLG_KEYID_POS) & 0x3;
3143
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003144 D_WEP("Remove dynamic key: idx=%d sta=%d\n", keyconf->keyidx, sta_id);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003145
3146 if (keyconf->keyidx != keyidx) {
3147 /* We need to remove a key with idx different that the one
3148 * in the uCode. This means that the key we need to remove has
3149 * been replaced by another one with different idx.
3150 * Don't do anything and return ok
3151 */
3152 spin_unlock_irqrestore(&il->sta_lock, flags);
3153 return 0;
3154 }
3155
3156 if (il->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003157 IL_WARN("Removing wrong key %d 0x%x\n", keyconf->keyidx,
3158 key_flags);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003159 spin_unlock_irqrestore(&il->sta_lock, flags);
3160 return 0;
3161 }
3162
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003163 if (!test_and_clear_bit
3164 (il->stations[sta_id].sta.key.key_offset, &il->ucode_key_table))
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003165 IL_ERR("idx %d not used in uCode key table.\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003166 il->stations[sta_id].sta.key.key_offset);
3167 memset(&il->stations[sta_id].keyinfo, 0, sizeof(struct il_hw_key));
3168 memset(&il->stations[sta_id].sta.key, 0, sizeof(struct il4965_keyinfo));
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003169 il->stations[sta_id].sta.key.key_flags =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003170 STA_KEY_FLG_NO_ENC | STA_KEY_FLG_INVALID;
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003171 il->stations[sta_id].sta.key.key_offset = WEP_INVALID_OFFSET;
3172 il->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
3173 il->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
3174
3175 if (il_is_rfkill(il)) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003176 D_WEP
3177 ("Not sending C_ADD_STA command because RFKILL enabled.\n");
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003178 spin_unlock_irqrestore(&il->sta_lock, flags);
3179 return 0;
3180 }
3181 memcpy(&sta_cmd, &il->stations[sta_id].sta,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003182 sizeof(struct il_addsta_cmd));
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003183 spin_unlock_irqrestore(&il->sta_lock, flags);
3184
3185 return il_send_add_sta(il, &sta_cmd, CMD_SYNC);
3186}
3187
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003188int
3189il4965_set_dynamic_key(struct il_priv *il, struct il_rxon_context *ctx,
3190 struct ieee80211_key_conf *keyconf, u8 sta_id)
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003191{
3192 int ret;
3193
3194 lockdep_assert_held(&il->mutex);
3195
3196 ctx->key_mapping_keys++;
3197 keyconf->hw_key_idx = HW_KEY_DYNAMIC;
3198
3199 switch (keyconf->cipher) {
3200 case WLAN_CIPHER_SUITE_CCMP:
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003201 ret =
3202 il4965_set_ccmp_dynamic_key_info(il, ctx, keyconf, sta_id);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003203 break;
3204 case WLAN_CIPHER_SUITE_TKIP:
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003205 ret =
3206 il4965_set_tkip_dynamic_key_info(il, ctx, keyconf, sta_id);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003207 break;
3208 case WLAN_CIPHER_SUITE_WEP40:
3209 case WLAN_CIPHER_SUITE_WEP104:
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003210 ret = il4965_set_wep_dynamic_key_info(il, ctx, keyconf, sta_id);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003211 break;
3212 default:
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003213 IL_ERR("Unknown alg: %s cipher = %x\n", __func__,
3214 keyconf->cipher);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003215 ret = -EINVAL;
3216 }
3217
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003218 D_WEP("Set dynamic key: cipher=%x len=%d idx=%d sta=%d ret=%d\n",
3219 keyconf->cipher, keyconf->keylen, keyconf->keyidx, sta_id, ret);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003220
3221 return ret;
3222}
3223
3224/**
3225 * il4965_alloc_bcast_station - add broadcast station into driver's station table.
3226 *
3227 * This adds the broadcast station into the driver's station table
3228 * and marks it driver active, so that it will be restored to the
3229 * device at the next best time.
3230 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003231int
3232il4965_alloc_bcast_station(struct il_priv *il, struct il_rxon_context *ctx)
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003233{
3234 struct il_link_quality_cmd *link_cmd;
3235 unsigned long flags;
3236 u8 sta_id;
3237
3238 spin_lock_irqsave(&il->sta_lock, flags);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003239 sta_id = il_prep_station(il, ctx, il_bcast_addr, false, NULL);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003240 if (sta_id == IL_INVALID_STATION) {
3241 IL_ERR("Unable to prepare broadcast station\n");
3242 spin_unlock_irqrestore(&il->sta_lock, flags);
3243
3244 return -EINVAL;
3245 }
3246
3247 il->stations[sta_id].used |= IL_STA_DRIVER_ACTIVE;
3248 il->stations[sta_id].used |= IL_STA_BCAST;
3249 spin_unlock_irqrestore(&il->sta_lock, flags);
3250
3251 link_cmd = il4965_sta_alloc_lq(il, sta_id);
3252 if (!link_cmd) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003253 IL_ERR
3254 ("Unable to initialize rate scaling for bcast station.\n");
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003255 return -ENOMEM;
3256 }
3257
3258 spin_lock_irqsave(&il->sta_lock, flags);
3259 il->stations[sta_id].lq = link_cmd;
3260 spin_unlock_irqrestore(&il->sta_lock, flags);
3261
3262 return 0;
3263}
3264
3265/**
3266 * il4965_update_bcast_station - update broadcast station's LQ command
3267 *
3268 * Only used by iwl4965. Placed here to have all bcast station management
3269 * code together.
3270 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003271static int
3272il4965_update_bcast_station(struct il_priv *il, struct il_rxon_context *ctx)
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003273{
3274 unsigned long flags;
3275 struct il_link_quality_cmd *link_cmd;
3276 u8 sta_id = ctx->bcast_sta_id;
3277
3278 link_cmd = il4965_sta_alloc_lq(il, sta_id);
3279 if (!link_cmd) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003280 IL_ERR
3281 ("Unable to initialize rate scaling for bcast station.\n");
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003282 return -ENOMEM;
3283 }
3284
3285 spin_lock_irqsave(&il->sta_lock, flags);
3286 if (il->stations[sta_id].lq)
3287 kfree(il->stations[sta_id].lq);
3288 else
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003289 D_INFO
3290 ("Bcast station rate scaling has not been initialized yet.\n");
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003291 il->stations[sta_id].lq = link_cmd;
3292 spin_unlock_irqrestore(&il->sta_lock, flags);
3293
3294 return 0;
3295}
3296
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003297int
3298il4965_update_bcast_stations(struct il_priv *il)
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003299{
3300 return il4965_update_bcast_station(il, &il->ctx);
3301}
3302
3303/**
3304 * il4965_sta_tx_modify_enable_tid - Enable Tx for this TID in station table
3305 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003306int
3307il4965_sta_tx_modify_enable_tid(struct il_priv *il, int sta_id, int tid)
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003308{
3309 unsigned long flags;
3310 struct il_addsta_cmd sta_cmd;
3311
3312 lockdep_assert_held(&il->mutex);
3313
3314 /* Remove "disable" flag, to enable Tx for this TID */
3315 spin_lock_irqsave(&il->sta_lock, flags);
3316 il->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_TID_DISABLE_TX;
3317 il->stations[sta_id].sta.tid_disable_tx &= cpu_to_le16(~(1 << tid));
3318 il->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
3319 memcpy(&sta_cmd, &il->stations[sta_id].sta,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003320 sizeof(struct il_addsta_cmd));
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003321 spin_unlock_irqrestore(&il->sta_lock, flags);
3322
3323 return il_send_add_sta(il, &sta_cmd, CMD_SYNC);
3324}
3325
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003326int
3327il4965_sta_rx_agg_start(struct il_priv *il, struct ieee80211_sta *sta, int tid,
3328 u16 ssn)
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003329{
3330 unsigned long flags;
3331 int sta_id;
3332 struct il_addsta_cmd sta_cmd;
3333
3334 lockdep_assert_held(&il->mutex);
3335
3336 sta_id = il_sta_id(sta);
3337 if (sta_id == IL_INVALID_STATION)
3338 return -ENXIO;
3339
3340 spin_lock_irqsave(&il->sta_lock, flags);
3341 il->stations[sta_id].sta.station_flags_msk = 0;
3342 il->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_ADDBA_TID_MSK;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003343 il->stations[sta_id].sta.add_immediate_ba_tid = (u8) tid;
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003344 il->stations[sta_id].sta.add_immediate_ba_ssn = cpu_to_le16(ssn);
3345 il->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
3346 memcpy(&sta_cmd, &il->stations[sta_id].sta,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003347 sizeof(struct il_addsta_cmd));
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003348 spin_unlock_irqrestore(&il->sta_lock, flags);
3349
3350 return il_send_add_sta(il, &sta_cmd, CMD_SYNC);
3351}
3352
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003353int
3354il4965_sta_rx_agg_stop(struct il_priv *il, struct ieee80211_sta *sta, int tid)
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003355{
3356 unsigned long flags;
3357 int sta_id;
3358 struct il_addsta_cmd sta_cmd;
3359
3360 lockdep_assert_held(&il->mutex);
3361
3362 sta_id = il_sta_id(sta);
3363 if (sta_id == IL_INVALID_STATION) {
3364 IL_ERR("Invalid station for AGG tid %d\n", tid);
3365 return -ENXIO;
3366 }
3367
3368 spin_lock_irqsave(&il->sta_lock, flags);
3369 il->stations[sta_id].sta.station_flags_msk = 0;
3370 il->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_DELBA_TID_MSK;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003371 il->stations[sta_id].sta.remove_immediate_ba_tid = (u8) tid;
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003372 il->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
3373 memcpy(&sta_cmd, &il->stations[sta_id].sta,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003374 sizeof(struct il_addsta_cmd));
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003375 spin_unlock_irqrestore(&il->sta_lock, flags);
3376
3377 return il_send_add_sta(il, &sta_cmd, CMD_SYNC);
3378}
3379
3380void
3381il4965_sta_modify_sleep_tx_count(struct il_priv *il, int sta_id, int cnt)
3382{
3383 unsigned long flags;
3384
3385 spin_lock_irqsave(&il->sta_lock, flags);
3386 il->stations[sta_id].sta.station_flags |= STA_FLG_PWR_SAVE_MSK;
3387 il->stations[sta_id].sta.station_flags_msk = STA_FLG_PWR_SAVE_MSK;
3388 il->stations[sta_id].sta.sta.modify_mask =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003389 STA_MODIFY_SLEEP_TX_COUNT_MSK;
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003390 il->stations[sta_id].sta.sleep_tx_count = cpu_to_le16(cnt);
3391 il->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003392 il_send_add_sta(il, &il->stations[sta_id].sta, CMD_ASYNC);
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02003393 spin_unlock_irqrestore(&il->sta_lock, flags);
3394
3395}
3396
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003397void
3398il4965_update_chain_flags(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003399{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003400 if (il->cfg->ops->hcmd->set_rxon_chain) {
Stanislaw Gruszka17d6e552011-08-29 12:52:20 +02003401 il->cfg->ops->hcmd->set_rxon_chain(il, &il->ctx);
3402 if (il->ctx.active.rx_chain != il->ctx.staging.rx_chain)
3403 il_commit_rxon(il, &il->ctx);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003404 }
3405}
3406
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003407static void
3408il4965_clear_free_frames(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003409{
3410 struct list_head *element;
3411
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003412 D_INFO("%d frames on pre-allocated heap on clear.\n", il->frames_count);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003413
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003414 while (!list_empty(&il->free_frames)) {
3415 element = il->free_frames.next;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003416 list_del(element);
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003417 kfree(list_entry(element, struct il_frame, list));
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003418 il->frames_count--;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003419 }
3420
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003421 if (il->frames_count) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02003422 IL_WARN("%d frames still in use. Did we lose one?\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003423 il->frames_count);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003424 il->frames_count = 0;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003425 }
3426}
3427
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003428static struct il_frame *
3429il4965_get_free_frame(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003430{
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003431 struct il_frame *frame;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003432 struct list_head *element;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003433 if (list_empty(&il->free_frames)) {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003434 frame = kzalloc(sizeof(*frame), GFP_KERNEL);
3435 if (!frame) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02003436 IL_ERR("Could not allocate frame!\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003437 return NULL;
3438 }
3439
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003440 il->frames_count++;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003441 return frame;
3442 }
3443
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003444 element = il->free_frames.next;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003445 list_del(element);
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003446 return list_entry(element, struct il_frame, list);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003447}
3448
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003449static void
3450il4965_free_frame(struct il_priv *il, struct il_frame *frame)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003451{
3452 memset(frame, 0, sizeof(*frame));
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003453 list_add(&frame->list, &il->free_frames);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003454}
3455
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003456static u32
3457il4965_fill_beacon_frame(struct il_priv *il, struct ieee80211_hdr *hdr,
3458 int left)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003459{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003460 lockdep_assert_held(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003461
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003462 if (!il->beacon_skb)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003463 return 0;
3464
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003465 if (il->beacon_skb->len > left)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003466 return 0;
3467
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003468 memcpy(hdr, il->beacon_skb->data, il->beacon_skb->len);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003469
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003470 return il->beacon_skb->len;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003471}
3472
3473/* Parse the beacon frame to find the TIM element and set tim_idx & tim_size */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003474static void
3475il4965_set_beacon_tim(struct il_priv *il,
3476 struct il_tx_beacon_cmd *tx_beacon_cmd, u8 * beacon,
3477 u32 frame_size)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003478{
3479 u16 tim_idx;
3480 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)beacon;
3481
3482 /*
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +01003483 * The idx is relative to frame start but we start looking at the
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003484 * variable-length part of the beacon.
3485 */
3486 tim_idx = mgmt->u.beacon.variable - beacon;
3487
3488 /* Parse variable-length elements of beacon to find WLAN_EID_TIM */
3489 while ((tim_idx < (frame_size - 2)) &&
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003490 (beacon[tim_idx] != WLAN_EID_TIM))
3491 tim_idx += beacon[tim_idx + 1] + 2;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003492
3493 /* If TIM field was found, set variables */
3494 if ((tim_idx < (frame_size - 1)) && (beacon[tim_idx] == WLAN_EID_TIM)) {
3495 tx_beacon_cmd->tim_idx = cpu_to_le16(tim_idx);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003496 tx_beacon_cmd->tim_size = beacon[tim_idx + 1];
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003497 } else
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02003498 IL_WARN("Unable to find TIM Element in beacon\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003499}
3500
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003501static unsigned int
3502il4965_hw_get_beacon_cmd(struct il_priv *il, struct il_frame *frame)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003503{
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003504 struct il_tx_beacon_cmd *tx_beacon_cmd;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003505 u32 frame_size;
3506 u32 rate_flags;
3507 u32 rate;
3508 /*
3509 * We have to set up the TX command, the TX Beacon command, and the
3510 * beacon contents.
3511 */
3512
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003513 lockdep_assert_held(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003514
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003515 if (!il->beacon_ctx) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02003516 IL_ERR("trying to build beacon w/o beacon context!\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003517 return 0;
3518 }
3519
3520 /* Initialize memory */
3521 tx_beacon_cmd = &frame->u.beacon;
3522 memset(tx_beacon_cmd, 0, sizeof(*tx_beacon_cmd));
3523
3524 /* Set up TX beacon contents */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003525 frame_size =
3526 il4965_fill_beacon_frame(il, tx_beacon_cmd->frame,
3527 sizeof(frame->u) - sizeof(*tx_beacon_cmd));
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003528 if (WARN_ON_ONCE(frame_size > MAX_MPDU_SIZE))
3529 return 0;
3530 if (!frame_size)
3531 return 0;
3532
3533 /* Set up TX command fields */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003534 tx_beacon_cmd->tx.len = cpu_to_le16((u16) frame_size);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003535 tx_beacon_cmd->tx.sta_id = il->beacon_ctx->bcast_sta_id;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003536 tx_beacon_cmd->tx.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003537 tx_beacon_cmd->tx.tx_flags =
3538 TX_CMD_FLG_SEQ_CTL_MSK | TX_CMD_FLG_TSF_MSK |
3539 TX_CMD_FLG_STA_RATE_MSK;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003540
3541 /* Set up TX beacon command fields */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003542 il4965_set_beacon_tim(il, tx_beacon_cmd, (u8 *) tx_beacon_cmd->frame,
3543 frame_size);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003544
3545 /* Set up packet rate and flags */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003546 rate = il_get_lowest_plcp(il, il->beacon_ctx);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003547 il->mgmt_tx_ant =
3548 il4965_toggle_tx_ant(il, il->mgmt_tx_ant,
3549 il->hw_params.valid_tx_ant);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003550 rate_flags = il4965_ant_idx_to_flags(il->mgmt_tx_ant);
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003551 if ((rate >= IL_FIRST_CCK_RATE) && (rate <= IL_LAST_CCK_RATE))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003552 rate_flags |= RATE_MCS_CCK_MSK;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003553 tx_beacon_cmd->tx.rate_n_flags =
3554 il4965_hw_set_rate_n_flags(rate, rate_flags);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003555
3556 return sizeof(*tx_beacon_cmd) + frame_size;
3557}
3558
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003559int
3560il4965_send_beacon_cmd(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003561{
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003562 struct il_frame *frame;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003563 unsigned int frame_size;
3564 int rc;
3565
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003566 frame = il4965_get_free_frame(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003567 if (!frame) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02003568 IL_ERR("Could not obtain free frame buffer for beacon "
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003569 "command.\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003570 return -ENOMEM;
3571 }
3572
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003573 frame_size = il4965_hw_get_beacon_cmd(il, frame);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003574 if (!frame_size) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02003575 IL_ERR("Error configuring the beacon command\n");
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003576 il4965_free_frame(il, frame);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003577 return -EINVAL;
3578 }
3579
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003580 rc = il_send_cmd_pdu(il, C_TX_BEACON, frame_size, &frame->u.cmd[0]);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003581
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003582 il4965_free_frame(il, frame);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003583
3584 return rc;
3585}
3586
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003587static inline dma_addr_t
3588il4965_tfd_tb_get_addr(struct il_tfd *tfd, u8 idx)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003589{
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003590 struct il_tfd_tb *tb = &tfd->tbs[idx];
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003591
3592 dma_addr_t addr = get_unaligned_le32(&tb->lo);
3593 if (sizeof(dma_addr_t) > sizeof(u32))
3594 addr |=
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003595 ((dma_addr_t) (le16_to_cpu(tb->hi_n_len) & 0xF) << 16) <<
3596 16;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003597
3598 return addr;
3599}
3600
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003601static inline u16
3602il4965_tfd_tb_get_len(struct il_tfd *tfd, u8 idx)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003603{
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003604 struct il_tfd_tb *tb = &tfd->tbs[idx];
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003605
3606 return le16_to_cpu(tb->hi_n_len) >> 4;
3607}
3608
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003609static inline void
3610il4965_tfd_set_tb(struct il_tfd *tfd, u8 idx, dma_addr_t addr, u16 len)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003611{
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003612 struct il_tfd_tb *tb = &tfd->tbs[idx];
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003613 u16 hi_n_len = len << 4;
3614
3615 put_unaligned_le32(addr, &tb->lo);
3616 if (sizeof(dma_addr_t) > sizeof(u32))
3617 hi_n_len |= ((addr >> 16) >> 16) & 0xF;
3618
3619 tb->hi_n_len = cpu_to_le16(hi_n_len);
3620
3621 tfd->num_tbs = idx + 1;
3622}
3623
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003624static inline u8
3625il4965_tfd_get_num_tbs(struct il_tfd *tfd)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003626{
3627 return tfd->num_tbs & 0x1f;
3628}
3629
3630/**
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003631 * il4965_hw_txq_free_tfd - Free all chunks referenced by TFD [txq->q.read_ptr]
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003632 * @il - driver ilate data
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003633 * @txq - tx queue
3634 *
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +01003635 * Does NOT advance any TFD circular buffer read/write idxes
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003636 * Does NOT free the TFD itself (which is within circular buffer)
3637 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003638void
3639il4965_hw_txq_free_tfd(struct il_priv *il, struct il_tx_queue *txq)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003640{
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003641 struct il_tfd *tfd_tmp = (struct il_tfd *)txq->tfds;
3642 struct il_tfd *tfd;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003643 struct pci_dev *dev = il->pci_dev;
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +01003644 int idx = txq->q.read_ptr;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003645 int i;
3646 int num_tbs;
3647
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +01003648 tfd = &tfd_tmp[idx];
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003649
3650 /* Sanity check on number of chunks */
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003651 num_tbs = il4965_tfd_get_num_tbs(tfd);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003652
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003653 if (num_tbs >= IL_NUM_OF_TBS) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02003654 IL_ERR("Too many chunks: %i\n", num_tbs);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003655 /* @todo issue fatal error, it is quite serious situation */
3656 return;
3657 }
3658
3659 /* Unmap tx_cmd */
3660 if (num_tbs)
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003661 pci_unmap_single(dev, dma_unmap_addr(&txq->meta[idx], mapping),
3662 dma_unmap_len(&txq->meta[idx], len),
3663 PCI_DMA_BIDIRECTIONAL);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003664
3665 /* Unmap chunks, if any. */
3666 for (i = 1; i < num_tbs; i++)
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003667 pci_unmap_single(dev, il4965_tfd_tb_get_addr(tfd, i),
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003668 il4965_tfd_tb_get_len(tfd, i),
3669 PCI_DMA_TODEVICE);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003670
3671 /* free SKB */
3672 if (txq->txb) {
3673 struct sk_buff *skb;
3674
3675 skb = txq->txb[txq->q.read_ptr].skb;
3676
3677 /* can be called from irqs-disabled context */
3678 if (skb) {
3679 dev_kfree_skb_any(skb);
3680 txq->txb[txq->q.read_ptr].skb = NULL;
3681 }
3682 }
3683}
3684
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003685int
3686il4965_hw_txq_attach_buf_to_tfd(struct il_priv *il, struct il_tx_queue *txq,
3687 dma_addr_t addr, u16 len, u8 reset, u8 pad)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003688{
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003689 struct il_queue *q;
3690 struct il_tfd *tfd, *tfd_tmp;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003691 u32 num_tbs;
3692
3693 q = &txq->q;
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003694 tfd_tmp = (struct il_tfd *)txq->tfds;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003695 tfd = &tfd_tmp[q->write_ptr];
3696
3697 if (reset)
3698 memset(tfd, 0, sizeof(*tfd));
3699
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003700 num_tbs = il4965_tfd_get_num_tbs(tfd);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003701
3702 /* Each TFD can point to a maximum 20 Tx buffers */
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003703 if (num_tbs >= IL_NUM_OF_TBS) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02003704 IL_ERR("Error can not send more than %d chunks\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003705 IL_NUM_OF_TBS);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003706 return -EINVAL;
3707 }
3708
3709 BUG_ON(addr & ~DMA_BIT_MASK(36));
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003710 if (unlikely(addr & ~IL_TX_DMA_MASK))
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003711 IL_ERR("Unaligned address = %llx\n", (unsigned long long)addr);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003712
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003713 il4965_tfd_set_tb(tfd, num_tbs, addr, len);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003714
3715 return 0;
3716}
3717
3718/*
3719 * Tell nic where to find circular buffer of Tx Frame Descriptors for
3720 * given Tx queue, and enable the DMA channel used for that queue.
3721 *
3722 * 4965 supports up to 16 Tx queues in DRAM, mapped to up to 8 Tx DMA
3723 * channels supported in hardware.
3724 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003725int
3726il4965_hw_tx_queue_init(struct il_priv *il, struct il_tx_queue *txq)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003727{
3728 int txq_id = txq->q.id;
3729
3730 /* Circular buffer (TFD queue in DRAM) physical base address */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003731 il_wr(il, FH49_MEM_CBBC_QUEUE(txq_id), txq->q.dma_addr >> 8);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003732
3733 return 0;
3734}
3735
3736/******************************************************************************
3737 *
3738 * Generic RX handler implementations
3739 *
3740 ******************************************************************************/
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003741static void
3742il4965_hdl_alive(struct il_priv *il, struct il_rx_buf *rxb)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003743{
Stanislaw Gruszkadcae1c62011-08-26 14:36:21 +02003744 struct il_rx_pkt *pkt = rxb_addr(rxb);
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003745 struct il_alive_resp *palive;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003746 struct delayed_work *pwork;
3747
3748 palive = &pkt->u.alive_frame;
3749
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003750 D_INFO("Alive ucode status 0x%08X revision " "0x%01X 0x%01X\n",
3751 palive->is_valid, palive->ver_type, palive->ver_subtype);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003752
3753 if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01003754 D_INFO("Initialization Alive received.\n");
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003755 memcpy(&il->card_alive_init, &pkt->u.alive_frame,
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003756 sizeof(struct il_init_alive_resp));
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003757 pwork = &il->init_alive_start;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003758 } else {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01003759 D_INFO("Runtime Alive received.\n");
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003760 memcpy(&il->card_alive, &pkt->u.alive_frame,
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003761 sizeof(struct il_alive_resp));
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003762 pwork = &il->alive_start;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003763 }
3764
3765 /* We delay the ALIVE response by 5ms to
3766 * give the HW RF Kill time to activate... */
3767 if (palive->is_valid == UCODE_VALID_OK)
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003768 queue_delayed_work(il->workqueue, pwork, msecs_to_jiffies(5));
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003769 else
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02003770 IL_WARN("uCode did not respond OK.\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003771}
3772
3773/**
Stanislaw Gruszkaebf0d902011-08-26 15:43:47 +02003774 * il4965_bg_stats_periodic - Timer callback to queue stats
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003775 *
Stanislaw Gruszkaebf0d902011-08-26 15:43:47 +02003776 * This callback is provided in order to send a stats request.
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003777 *
3778 * This timer function is continually reset to execute within
Stanislaw Gruszka4d69c752011-08-30 15:26:35 +02003779 * REG_RECALIB_PERIOD seconds since the last N_STATS
Stanislaw Gruszkaebf0d902011-08-26 15:43:47 +02003780 * was received. We need to ensure we receive the stats in order
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003781 * to update the temperature used for calibrating the TXPOWER.
3782 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003783static void
3784il4965_bg_stats_periodic(unsigned long data)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003785{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003786 struct il_priv *il = (struct il_priv *)data;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003787
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01003788 if (test_bit(S_EXIT_PENDING, &il->status))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003789 return;
3790
3791 /* dont send host command if rf-kill is on */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003792 if (!il_is_ready_rf(il))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003793 return;
3794
Stanislaw Gruszkaebf0d902011-08-26 15:43:47 +02003795 il_send_stats_request(il, CMD_ASYNC, false);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003796}
3797
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003798static void
3799il4965_hdl_beacon(struct il_priv *il, struct il_rx_buf *rxb)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003800{
Stanislaw Gruszkadcae1c62011-08-26 14:36:21 +02003801 struct il_rx_pkt *pkt = rxb_addr(rxb);
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003802 struct il4965_beacon_notif *beacon =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003803 (struct il4965_beacon_notif *)pkt->u.raw;
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01003804#ifdef CONFIG_IWLEGACY_DEBUG
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003805 u8 rate = il4965_hw_get_rate(beacon->beacon_notify_hdr.rate_n_flags);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003806
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003807 D_RX("beacon status %x retries %d iss %d " "tsf %d %d rate %d\n",
3808 le32_to_cpu(beacon->beacon_notify_hdr.u.status) & TX_STATUS_MSK,
3809 beacon->beacon_notify_hdr.failure_frame,
3810 le32_to_cpu(beacon->ibss_mgr_status),
3811 le32_to_cpu(beacon->high_tsf), le32_to_cpu(beacon->low_tsf), rate);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003812#endif
3813
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003814 il->ibss_manager = le32_to_cpu(beacon->ibss_mgr_status);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003815}
3816
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003817static void
3818il4965_perform_ct_kill_task(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003819{
3820 unsigned long flags;
3821
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01003822 D_POWER("Stop all queues\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003823
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003824 if (il->mac80211_registered)
3825 ieee80211_stop_queues(il->hw);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003826
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02003827 _il_wr(il, CSR_UCODE_DRV_GP1_SET,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003828 CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02003829 _il_rd(il, CSR_UCODE_DRV_GP1);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003830
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003831 spin_lock_irqsave(&il->reg_lock, flags);
Stanislaw Gruszka13882262011-08-24 15:39:23 +02003832 if (!_il_grab_nic_access(il))
3833 _il_release_nic_access(il);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003834 spin_unlock_irqrestore(&il->reg_lock, flags);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003835}
3836
3837/* Handle notification from uCode that card's power state is changing
3838 * due to software, hardware, or critical temperature RFKILL */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003839static void
3840il4965_hdl_card_state(struct il_priv *il, struct il_rx_buf *rxb)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003841{
Stanislaw Gruszkadcae1c62011-08-26 14:36:21 +02003842 struct il_rx_pkt *pkt = rxb_addr(rxb);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003843 u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003844 unsigned long status = il->status;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003845
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01003846 D_RF_KILL("Card state received: HW:%s SW:%s CT:%s\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003847 (flags & HW_CARD_DISABLED) ? "Kill" : "On",
3848 (flags & SW_CARD_DISABLED) ? "Kill" : "On",
3849 (flags & CT_CARD_DISABLED) ? "Reached" : "Not reached");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003850
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003851 if (flags & (SW_CARD_DISABLED | HW_CARD_DISABLED | CT_CARD_DISABLED)) {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003852
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02003853 _il_wr(il, CSR_UCODE_DRV_GP1_SET,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003854 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003855
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003856 il_wr(il, HBUS_TARG_MBX_C, HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003857
3858 if (!(flags & RXON_CARD_DISABLED)) {
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02003859 _il_wr(il, CSR_UCODE_DRV_GP1_CLR,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003860 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
Stanislaw Gruszka0c1a94e2011-08-24 17:37:16 +02003861 il_wr(il, HBUS_TARG_MBX_C,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003862 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003863 }
3864 }
3865
3866 if (flags & CT_CARD_DISABLED)
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003867 il4965_perform_ct_kill_task(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003868
3869 if (flags & HW_CARD_DISABLED)
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01003870 set_bit(S_RF_KILL_HW, &il->status);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003871 else
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01003872 clear_bit(S_RF_KILL_HW, &il->status);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003873
3874 if (!(flags & RXON_CARD_DISABLED))
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003875 il_scan_cancel(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003876
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01003877 if ((test_bit(S_RF_KILL_HW, &status) !=
3878 test_bit(S_RF_KILL_HW, &il->status)))
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003879 wiphy_rfkill_set_hw_state(il->hw->wiphy,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003880 test_bit(S_RF_KILL_HW, &il->status));
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003881 else
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003882 wake_up(&il->wait_command_queue);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003883}
3884
3885/**
Stanislaw Gruszkad0c72342011-08-30 15:39:42 +02003886 * il4965_setup_handlers - Initialize Rx handler callbacks
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003887 *
3888 * Setup the RX handlers for each of the reply types sent from the uCode
3889 * to the host.
3890 *
3891 * This function chains into the hardware specific files for them to setup
3892 * any hardware specific handlers as well.
3893 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003894static void
3895il4965_setup_handlers(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003896{
Stanislaw Gruszka6e9848b42011-08-30 15:45:31 +02003897 il->handlers[N_ALIVE] = il4965_hdl_alive;
3898 il->handlers[N_ERROR] = il_hdl_error;
Stanislaw Gruszkad2dfb332011-11-15 13:16:38 +01003899 il->handlers[N_CHANNEL_SWITCH] = il_hdl_csa;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003900 il->handlers[N_SPECTRUM_MEASUREMENT] = il_hdl_spectrum_measurement;
Stanislaw Gruszkad2dfb332011-11-15 13:16:38 +01003901 il->handlers[N_PM_SLEEP] = il_hdl_pm_sleep;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003902 il->handlers[N_PM_DEBUG_STATS] = il_hdl_pm_debug_stats;
Stanislaw Gruszkad2dfb332011-11-15 13:16:38 +01003903 il->handlers[N_BEACON] = il4965_hdl_beacon;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003904
3905 /*
3906 * The same handler is used for both the REPLY to a discrete
Stanislaw Gruszkaebf0d902011-08-26 15:43:47 +02003907 * stats request from the host as well as for the periodic
3908 * stats notifications (after received beacons) from the uCode.
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003909 */
Stanislaw Gruszkad2dfb332011-11-15 13:16:38 +01003910 il->handlers[C_STATS] = il4965_hdl_c_stats;
3911 il->handlers[N_STATS] = il4965_hdl_stats;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003912
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003913 il_setup_rx_scan_handlers(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003914
3915 /* status change handler */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003916 il->handlers[N_CARD_STATE] = il4965_hdl_card_state;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003917
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003918 il->handlers[N_MISSED_BEACONS] = il4965_hdl_missed_beacon;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003919 /* Rx handlers */
Stanislaw Gruszka6e9848b42011-08-30 15:45:31 +02003920 il->handlers[N_RX_PHY] = il4965_hdl_rx_phy;
3921 il->handlers[N_RX_MPDU] = il4965_hdl_rx;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003922 /* block ack */
Stanislaw Gruszka6e9848b42011-08-30 15:45:31 +02003923 il->handlers[N_COMPRESSED_BA] = il4965_hdl_compressed_ba;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003924 /* Set up hardware specific Rx handlers */
Stanislaw Gruszkad0c72342011-08-30 15:39:42 +02003925 il->cfg->ops->lib->handler_setup(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003926}
3927
3928/**
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003929 * il4965_rx_handle - Main entry function for receiving responses from uCode
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003930 *
Stanislaw Gruszkad0c72342011-08-30 15:39:42 +02003931 * Uses the il->handlers callback function array to invoke
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003932 * the appropriate handlers, including command responses,
3933 * frame-received notifications, and other notifications.
3934 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003935void
3936il4965_rx_handle(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003937{
Stanislaw Gruszkab73bb5f2011-08-26 14:37:54 +02003938 struct il_rx_buf *rxb;
Stanislaw Gruszkadcae1c62011-08-26 14:36:21 +02003939 struct il_rx_pkt *pkt;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003940 struct il_rx_queue *rxq = &il->rxq;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003941 u32 r, i;
3942 int reclaim;
3943 unsigned long flags;
3944 u8 fill_rx = 0;
3945 u32 count = 8;
3946 int total_empty;
3947
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +01003948 /* uCode's read idx (stored in shared DRAM) indicates the last Rx
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003949 * buffer that the driver may process (last buffer filled by ucode). */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003950 r = le16_to_cpu(rxq->rb_stts->closed_rb_num) & 0x0FFF;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003951 i = rxq->read;
3952
3953 /* Rx interrupt, but nothing sent from uCode */
3954 if (i == r)
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01003955 D_RX("r = %d, i = %d\n", r, i);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003956
3957 /* calculate total frames need to be restock after handling RX */
3958 total_empty = r - rxq->write_actual;
3959 if (total_empty < 0)
3960 total_empty += RX_QUEUE_SIZE;
3961
3962 if (total_empty > (RX_QUEUE_SIZE / 2))
3963 fill_rx = 1;
3964
3965 while (i != r) {
3966 int len;
3967
3968 rxb = rxq->queue[i];
3969
3970 /* If an RXB doesn't have a Rx queue slot associated with it,
3971 * then a bug has been introduced in the queue refilling
3972 * routines -- catch it here */
3973 BUG_ON(rxb == NULL);
3974
3975 rxq->queue[i] = NULL;
3976
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003977 pci_unmap_page(il->pci_dev, rxb->page_dma,
3978 PAGE_SIZE << il->hw_params.rx_page_order,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003979 PCI_DMA_FROMDEVICE);
3980 pkt = rxb_addr(rxb);
3981
Stanislaw Gruszkae94a4092011-08-31 13:23:20 +02003982 len = le32_to_cpu(pkt->len_n_flags) & IL_RX_FRAME_SIZE_MSK;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003983 len += sizeof(u32); /* account for status word */
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003984
3985 /* Reclaim a command buffer only if this packet is a response
3986 * to a (driver-originated) command.
3987 * If the packet (e.g. Rx frame) originated from uCode,
3988 * there is no command buffer to reclaim.
3989 * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
3990 * but apparently a few don't get set; catch them here. */
3991 reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01003992 (pkt->hdr.cmd != N_RX_PHY) && (pkt->hdr.cmd != N_RX) &&
3993 (pkt->hdr.cmd != N_RX_MPDU) &&
3994 (pkt->hdr.cmd != N_COMPRESSED_BA) &&
3995 (pkt->hdr.cmd != N_STATS) && (pkt->hdr.cmd != C_TX);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003996
3997 /* Based on type of command response or notification,
3998 * handle those that need handling via function in
Stanislaw Gruszkad0c72342011-08-30 15:39:42 +02003999 * handlers table. See il4965_setup_handlers() */
4000 if (il->handlers[pkt->hdr.cmd]) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004001 D_RX("r = %d, i = %d, %s, 0x%02x\n", r, i,
4002 il_get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
Stanislaw Gruszkad0c72342011-08-30 15:39:42 +02004003 il->isr_stats.handlers[pkt->hdr.cmd]++;
4004 il->handlers[pkt->hdr.cmd] (il, rxb);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004005 } else {
4006 /* No handling needed */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004007 D_RX("r %d i %d No handler needed for %s, 0x%02x\n", r,
4008 i, il_get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004009 }
4010
4011 /*
4012 * XXX: After here, we should always check rxb->page
4013 * against NULL before touching it or its virtual
Stanislaw Gruszkad0c72342011-08-30 15:39:42 +02004014 * memory (pkt). Because some handler might have
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004015 * already taken or freed the pages.
4016 */
4017
4018 if (reclaim) {
4019 /* Invoke any callbacks, transfer the buffer to caller,
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004020 * and fire off the (possibly) blocking il_send_cmd()
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004021 * as we reclaim the driver command queue */
4022 if (rxb->page)
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004023 il_tx_cmd_complete(il, rxb);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004024 else
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004025 IL_WARN("Claim null rxb?\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004026 }
4027
4028 /* Reuse the page if possible. For notification packets and
4029 * SKBs that fail to Rx correctly, add them back into the
4030 * rx_free list for reuse later. */
4031 spin_lock_irqsave(&rxq->lock, flags);
4032 if (rxb->page != NULL) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004033 rxb->page_dma =
4034 pci_map_page(il->pci_dev, rxb->page, 0,
4035 PAGE_SIZE << il->hw_params.
4036 rx_page_order, PCI_DMA_FROMDEVICE);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004037 list_add_tail(&rxb->list, &rxq->rx_free);
4038 rxq->free_count++;
4039 } else
4040 list_add_tail(&rxb->list, &rxq->rx_used);
4041
4042 spin_unlock_irqrestore(&rxq->lock, flags);
4043
4044 i = (i + 1) & RX_QUEUE_MASK;
4045 /* If there are a lot of unused frames,
4046 * restock the Rx queue so ucode wont assert. */
4047 if (fill_rx) {
4048 count++;
4049 if (count >= 8) {
4050 rxq->read = i;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004051 il4965_rx_replenish_now(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004052 count = 0;
4053 }
4054 }
4055 }
4056
4057 /* Backtrack one entry */
4058 rxq->read = i;
4059 if (fill_rx)
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004060 il4965_rx_replenish_now(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004061 else
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004062 il4965_rx_queue_restock(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004063}
4064
4065/* call this function to flush any scheduled tasklet */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004066static inline void
4067il4965_synchronize_irq(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004068{
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004069 /* wait to make sure we flush pending tasklet */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004070 synchronize_irq(il->pci_dev->irq);
4071 tasklet_kill(&il->irq_tasklet);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004072}
4073
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004074static void
4075il4965_irq_tasklet(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004076{
4077 u32 inta, handled = 0;
4078 u32 inta_fh;
4079 unsigned long flags;
4080 u32 i;
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01004081#ifdef CONFIG_IWLEGACY_DEBUG
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004082 u32 inta_mask;
4083#endif
4084
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004085 spin_lock_irqsave(&il->lock, flags);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004086
4087 /* Ack/clear/reset pending uCode interrupts.
4088 * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
4089 * and will clear only when CSR_FH_INT_STATUS gets cleared. */
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02004090 inta = _il_rd(il, CSR_INT);
4091 _il_wr(il, CSR_INT, inta);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004092
4093 /* Ack/clear/reset pending flow-handler (DMA) interrupts.
4094 * Any new interrupts that happen after this, either while we're
4095 * in this tasklet, or later, will show up in next ISR/tasklet. */
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02004096 inta_fh = _il_rd(il, CSR_FH_INT_STATUS);
4097 _il_wr(il, CSR_FH_INT_STATUS, inta_fh);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004098
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01004099#ifdef CONFIG_IWLEGACY_DEBUG
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004100 if (il_get_debug_level(il) & IL_DL_ISR) {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004101 /* just for debug */
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02004102 inta_mask = _il_rd(il, CSR_INT_MASK);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004103 D_ISR("inta 0x%08x, enabled 0x%08x, fh 0x%08x\n", inta,
4104 inta_mask, inta_fh);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004105 }
4106#endif
4107
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004108 spin_unlock_irqrestore(&il->lock, flags);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004109
4110 /* Since CSR_INT and CSR_FH_INT_STATUS reads and clears are not
4111 * atomic, make sure that inta covers all the interrupts that
4112 * we've discovered, even if FH interrupt came in just after
4113 * reading CSR_INT. */
4114 if (inta_fh & CSR49_FH_INT_RX_MASK)
4115 inta |= CSR_INT_BIT_FH_RX;
4116 if (inta_fh & CSR49_FH_INT_TX_MASK)
4117 inta |= CSR_INT_BIT_FH_TX;
4118
4119 /* Now service all interrupt bits discovered above. */
4120 if (inta & CSR_INT_BIT_HW_ERR) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004121 IL_ERR("Hardware error detected. Restarting.\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004122
4123 /* Tell the device to stop sending interrupts */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004124 il_disable_interrupts(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004125
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004126 il->isr_stats.hw++;
4127 il_irq_handle_error(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004128
4129 handled |= CSR_INT_BIT_HW_ERR;
4130
4131 return;
4132 }
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01004133#ifdef CONFIG_IWLEGACY_DEBUG
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004134 if (il_get_debug_level(il) & (IL_DL_ISR)) {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004135 /* NIC fires this, but we don't use it, redundant with WAKEUP */
4136 if (inta & CSR_INT_BIT_SCD) {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01004137 D_ISR("Scheduler finished to transmit "
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004138 "the frame/frames.\n");
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004139 il->isr_stats.sch++;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004140 }
4141
4142 /* Alive notification via Rx interrupt will do the real work */
4143 if (inta & CSR_INT_BIT_ALIVE) {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01004144 D_ISR("Alive interrupt\n");
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004145 il->isr_stats.alive++;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004146 }
4147 }
4148#endif
4149 /* Safely ignore these bits for debug checks below */
4150 inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
4151
4152 /* HW RF KILL switch toggled */
4153 if (inta & CSR_INT_BIT_RF_KILL) {
4154 int hw_rf_kill = 0;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004155 if (!
4156 (_il_rd(il, CSR_GP_CNTRL) &
4157 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004158 hw_rf_kill = 1;
4159
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004160 IL_WARN("RF_KILL bit toggled to %s.\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004161 hw_rf_kill ? "disable radio" : "enable radio");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004162
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004163 il->isr_stats.rfkill++;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004164
4165 /* driver only loads ucode once setting the interface up.
4166 * the driver allows loading the ucode even if the radio
4167 * is killed. Hence update the killswitch state here. The
4168 * rfkill handler will care about restarting if needed.
4169 */
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01004170 if (!test_bit(S_ALIVE, &il->status)) {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004171 if (hw_rf_kill)
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01004172 set_bit(S_RF_KILL_HW, &il->status);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004173 else
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01004174 clear_bit(S_RF_KILL_HW, &il->status);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004175 wiphy_rfkill_set_hw_state(il->hw->wiphy, hw_rf_kill);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004176 }
4177
4178 handled |= CSR_INT_BIT_RF_KILL;
4179 }
4180
4181 /* Chip got too hot and stopped itself */
4182 if (inta & CSR_INT_BIT_CT_KILL) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004183 IL_ERR("Microcode CT kill error detected.\n");
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004184 il->isr_stats.ctkill++;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004185 handled |= CSR_INT_BIT_CT_KILL;
4186 }
4187
4188 /* Error detected by uCode */
4189 if (inta & CSR_INT_BIT_SW_ERR) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004190 IL_ERR("Microcode SW error detected. " " Restarting 0x%X.\n",
4191 inta);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004192 il->isr_stats.sw++;
4193 il_irq_handle_error(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004194 handled |= CSR_INT_BIT_SW_ERR;
4195 }
4196
4197 /*
4198 * uCode wakes up after power-down sleep.
4199 * Tell device about any new tx or host commands enqueued,
4200 * and about any Rx buffers made available while asleep.
4201 */
4202 if (inta & CSR_INT_BIT_WAKEUP) {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01004203 D_ISR("Wakeup interrupt\n");
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004204 il_rx_queue_update_write_ptr(il, &il->rxq);
4205 for (i = 0; i < il->hw_params.max_txq_num; i++)
4206 il_txq_update_write_ptr(il, &il->txq[i]);
4207 il->isr_stats.wakeup++;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004208 handled |= CSR_INT_BIT_WAKEUP;
4209 }
4210
4211 /* All uCode command responses, including Tx command responses,
4212 * Rx "responses" (frame-received notification), and other
4213 * notifications from uCode come through here*/
4214 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004215 il4965_rx_handle(il);
4216 il->isr_stats.rx++;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004217 handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
4218 }
4219
4220 /* This "Tx" DMA channel is used only for loading uCode */
4221 if (inta & CSR_INT_BIT_FH_TX) {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01004222 D_ISR("uCode load interrupt\n");
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004223 il->isr_stats.tx++;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004224 handled |= CSR_INT_BIT_FH_TX;
4225 /* Wake up uCode load routine, now that load is complete */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004226 il->ucode_write_complete = 1;
4227 wake_up(&il->wait_command_queue);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004228 }
4229
4230 if (inta & ~handled) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004231 IL_ERR("Unhandled INTA bits 0x%08x\n", inta & ~handled);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004232 il->isr_stats.unhandled++;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004233 }
4234
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004235 if (inta & ~(il->inta_mask)) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004236 IL_WARN("Disabled INTA bits 0x%08x were pending\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004237 inta & ~il->inta_mask);
Stanislaw Gruszka9a95b372011-08-31 14:20:23 +02004238 IL_WARN(" with FH49_INT = 0x%08x\n", inta_fh);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004239 }
4240
4241 /* Re-enable all interrupts */
Stanislaw Gruszka93fd74e2011-04-28 11:51:30 +02004242 /* only Re-enable if disabled by irq */
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01004243 if (test_bit(S_INT_ENABLED, &il->status))
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004244 il_enable_interrupts(il);
Stanislaw Gruszkaa078a1f2011-04-28 11:51:25 +02004245 /* Re-enable RF_KILL if it occurred */
4246 else if (handled & CSR_INT_BIT_RF_KILL)
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004247 il_enable_rfkill_int(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004248
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01004249#ifdef CONFIG_IWLEGACY_DEBUG
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004250 if (il_get_debug_level(il) & (IL_DL_ISR)) {
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02004251 inta = _il_rd(il, CSR_INT);
4252 inta_mask = _il_rd(il, CSR_INT_MASK);
4253 inta_fh = _il_rd(il, CSR_FH_INT_STATUS);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004254 D_ISR("End inta 0x%08x, enabled 0x%08x, fh 0x%08x, "
4255 "flags 0x%08lx\n", inta, inta_mask, inta_fh, flags);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004256 }
4257#endif
4258}
4259
4260/*****************************************************************************
4261 *
4262 * sysfs attributes
4263 *
4264 *****************************************************************************/
4265
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01004266#ifdef CONFIG_IWLEGACY_DEBUG
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004267
4268/*
4269 * The following adds a new attribute to the sysfs representation
4270 * of this device driver (i.e. a new file in /sys/class/net/wlan0/device/)
4271 * used for controlling the debug level.
4272 *
4273 * See the level definitions in iwl for details.
4274 *
4275 * The debug_level being managed using sysfs below is a per device debug
4276 * level that is used instead of the global debug level if it (the per
4277 * device debug level) is set.
4278 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004279static ssize_t
4280il4965_show_debug_level(struct device *d, struct device_attribute *attr,
4281 char *buf)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004282{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004283 struct il_priv *il = dev_get_drvdata(d);
4284 return sprintf(buf, "0x%08X\n", il_get_debug_level(il));
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004285}
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004286
4287static ssize_t
4288il4965_store_debug_level(struct device *d, struct device_attribute *attr,
4289 const char *buf, size_t count)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004290{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004291 struct il_priv *il = dev_get_drvdata(d);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004292 unsigned long val;
4293 int ret;
4294
4295 ret = strict_strtoul(buf, 0, &val);
4296 if (ret)
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004297 IL_ERR("%s is not in hex or decimal form.\n", buf);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004298 else {
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004299 il->debug_level = val;
4300 if (il_alloc_traffic_mem(il))
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004301 IL_ERR("Not enough memory to generate traffic log\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004302 }
4303 return strnlen(buf, count);
4304}
4305
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004306static DEVICE_ATTR(debug_level, S_IWUSR | S_IRUGO, il4965_show_debug_level,
4307 il4965_store_debug_level);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004308
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01004309#endif /* CONFIG_IWLEGACY_DEBUG */
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004310
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004311static ssize_t
4312il4965_show_temperature(struct device *d, struct device_attribute *attr,
4313 char *buf)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004314{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004315 struct il_priv *il = dev_get_drvdata(d);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004316
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004317 if (!il_is_alive(il))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004318 return -EAGAIN;
4319
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004320 return sprintf(buf, "%d\n", il->temperature);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004321}
4322
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004323static DEVICE_ATTR(temperature, S_IRUGO, il4965_show_temperature, NULL);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004324
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004325static ssize_t
4326il4965_show_tx_power(struct device *d, struct device_attribute *attr, char *buf)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004327{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004328 struct il_priv *il = dev_get_drvdata(d);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004329
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004330 if (!il_is_ready_rf(il))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004331 return sprintf(buf, "off\n");
4332 else
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004333 return sprintf(buf, "%d\n", il->tx_power_user_lmt);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004334}
4335
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004336static ssize_t
4337il4965_store_tx_power(struct device *d, struct device_attribute *attr,
4338 const char *buf, size_t count)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004339{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004340 struct il_priv *il = dev_get_drvdata(d);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004341 unsigned long val;
4342 int ret;
4343
4344 ret = strict_strtoul(buf, 10, &val);
4345 if (ret)
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004346 IL_INFO("%s is not in decimal form.\n", buf);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004347 else {
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004348 ret = il_set_tx_power(il, val, false);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004349 if (ret)
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004350 IL_ERR("failed setting tx power (0x%d).\n", ret);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004351 else
4352 ret = count;
4353 }
4354 return ret;
4355}
4356
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004357static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO, il4965_show_tx_power,
4358 il4965_store_tx_power);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004359
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004360static struct attribute *il_sysfs_entries[] = {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004361 &dev_attr_temperature.attr,
4362 &dev_attr_tx_power.attr,
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01004363#ifdef CONFIG_IWLEGACY_DEBUG
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004364 &dev_attr_debug_level.attr,
4365#endif
4366 NULL
4367};
4368
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004369static struct attribute_group il_attribute_group = {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004370 .name = NULL, /* put in device directory */
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004371 .attrs = il_sysfs_entries,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004372};
4373
4374/******************************************************************************
4375 *
4376 * uCode download functions
4377 *
4378 ******************************************************************************/
4379
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004380static void
4381il4965_dealloc_ucode_pci(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004382{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004383 il_free_fw_desc(il->pci_dev, &il->ucode_code);
4384 il_free_fw_desc(il->pci_dev, &il->ucode_data);
4385 il_free_fw_desc(il->pci_dev, &il->ucode_data_backup);
4386 il_free_fw_desc(il->pci_dev, &il->ucode_init);
4387 il_free_fw_desc(il->pci_dev, &il->ucode_init_data);
4388 il_free_fw_desc(il->pci_dev, &il->ucode_boot);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004389}
4390
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004391static void
4392il4965_nic_start(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004393{
4394 /* Remove all resets to allow NIC to operate */
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02004395 _il_wr(il, CSR_RESET, 0);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004396}
4397
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004398static void il4965_ucode_callback(const struct firmware *ucode_raw,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004399 void *context);
4400static int il4965_mac_setup_register(struct il_priv *il, u32 max_probe_length);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004401
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004402static int __must_check
4403il4965_request_firmware(struct il_priv *il, bool first)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004404{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004405 const char *name_pre = il->cfg->fw_name_pre;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004406 char tag[8];
4407
4408 if (first) {
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +01004409 il->fw_idx = il->cfg->ucode_api_max;
4410 sprintf(tag, "%d", il->fw_idx);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004411 } else {
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +01004412 il->fw_idx--;
4413 sprintf(tag, "%d", il->fw_idx);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004414 }
4415
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +01004416 if (il->fw_idx < il->cfg->ucode_api_min) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004417 IL_ERR("no suitable firmware found!\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004418 return -ENOENT;
4419 }
4420
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004421 sprintf(il->firmware_name, "%s%s%s", name_pre, tag, ".ucode");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004422
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004423 D_INFO("attempting to load firmware '%s'\n", il->firmware_name);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004424
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004425 return request_firmware_nowait(THIS_MODULE, 1, il->firmware_name,
4426 &il->pci_dev->dev, GFP_KERNEL, il,
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004427 il4965_ucode_callback);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004428}
4429
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004430struct il4965_firmware_pieces {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004431 const void *inst, *data, *init, *init_data, *boot;
4432 size_t inst_size, data_size, init_size, init_data_size, boot_size;
4433};
4434
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004435static int
4436il4965_load_firmware(struct il_priv *il, const struct firmware *ucode_raw,
4437 struct il4965_firmware_pieces *pieces)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004438{
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004439 struct il_ucode_header *ucode = (void *)ucode_raw->data;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004440 u32 api_ver, hdr_size;
4441 const u8 *src;
4442
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004443 il->ucode_ver = le32_to_cpu(ucode->ver);
4444 api_ver = IL_UCODE_API(il->ucode_ver);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004445
4446 switch (api_ver) {
4447 default:
4448 case 0:
4449 case 1:
4450 case 2:
4451 hdr_size = 24;
4452 if (ucode_raw->size < hdr_size) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004453 IL_ERR("File size too small!\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004454 return -EINVAL;
4455 }
4456 pieces->inst_size = le32_to_cpu(ucode->v1.inst_size);
4457 pieces->data_size = le32_to_cpu(ucode->v1.data_size);
4458 pieces->init_size = le32_to_cpu(ucode->v1.init_size);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004459 pieces->init_data_size = le32_to_cpu(ucode->v1.init_data_size);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004460 pieces->boot_size = le32_to_cpu(ucode->v1.boot_size);
4461 src = ucode->v1.data;
4462 break;
4463 }
4464
4465 /* Verify size of file vs. image size info in file's header */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004466 if (ucode_raw->size !=
4467 hdr_size + pieces->inst_size + pieces->data_size +
4468 pieces->init_size + pieces->init_data_size + pieces->boot_size) {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004469
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004470 IL_ERR("uCode file size %d does not match expected size\n",
4471 (int)ucode_raw->size);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004472 return -EINVAL;
4473 }
4474
4475 pieces->inst = src;
4476 src += pieces->inst_size;
4477 pieces->data = src;
4478 src += pieces->data_size;
4479 pieces->init = src;
4480 src += pieces->init_size;
4481 pieces->init_data = src;
4482 src += pieces->init_data_size;
4483 pieces->boot = src;
4484 src += pieces->boot_size;
4485
4486 return 0;
4487}
4488
4489/**
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004490 * il4965_ucode_callback - callback when firmware was loaded
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004491 *
4492 * If loaded successfully, copies the firmware into buffers
4493 * for the card to fetch (via DMA).
4494 */
4495static void
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004496il4965_ucode_callback(const struct firmware *ucode_raw, void *context)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004497{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004498 struct il_priv *il = context;
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004499 struct il_ucode_header *ucode;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004500 int err;
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004501 struct il4965_firmware_pieces pieces;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004502 const unsigned int api_max = il->cfg->ucode_api_max;
4503 const unsigned int api_min = il->cfg->ucode_api_min;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004504 u32 api_ver;
4505
4506 u32 max_probe_length = 200;
4507 u32 standard_phy_calibration_size =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004508 IL_DEFAULT_STANDARD_PHY_CALIBRATE_TBL_SIZE;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004509
4510 memset(&pieces, 0, sizeof(pieces));
4511
4512 if (!ucode_raw) {
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +01004513 if (il->fw_idx <= il->cfg->ucode_api_max)
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004514 IL_ERR("request for firmware file '%s' failed.\n",
4515 il->firmware_name);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004516 goto try_again;
4517 }
4518
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004519 D_INFO("Loaded firmware file '%s' (%zd bytes).\n", il->firmware_name,
4520 ucode_raw->size);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004521
4522 /* Make sure that we got at least the API version number */
4523 if (ucode_raw->size < 4) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004524 IL_ERR("File size way too small!\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004525 goto try_again;
4526 }
4527
4528 /* Data from ucode file: header followed by uCode images */
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004529 ucode = (struct il_ucode_header *)ucode_raw->data;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004530
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004531 err = il4965_load_firmware(il, ucode_raw, &pieces);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004532
4533 if (err)
4534 goto try_again;
4535
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004536 api_ver = IL_UCODE_API(il->ucode_ver);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004537
4538 /*
4539 * api_ver should match the api version forming part of the
4540 * firmware filename ... but we don't check for that and only rely
4541 * on the API version read from firmware header from here on forward
4542 */
4543 if (api_ver < api_min || api_ver > api_max) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004544 IL_ERR("Driver unable to support your firmware API. "
4545 "Driver supports v%u, firmware is v%u.\n", api_max,
4546 api_ver);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004547 goto try_again;
4548 }
4549
4550 if (api_ver != api_max)
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004551 IL_ERR("Firmware has old API version. Expected v%u, "
4552 "got v%u. New firmware can be obtained "
4553 "from http://www.intellinuxwireless.org.\n", api_max,
4554 api_ver);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004555
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004556 IL_INFO("loaded firmware version %u.%u.%u.%u\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004557 IL_UCODE_MAJOR(il->ucode_ver), IL_UCODE_MINOR(il->ucode_ver),
4558 IL_UCODE_API(il->ucode_ver), IL_UCODE_SERIAL(il->ucode_ver));
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004559
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004560 snprintf(il->hw->wiphy->fw_version, sizeof(il->hw->wiphy->fw_version),
4561 "%u.%u.%u.%u", IL_UCODE_MAJOR(il->ucode_ver),
4562 IL_UCODE_MINOR(il->ucode_ver), IL_UCODE_API(il->ucode_ver),
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004563 IL_UCODE_SERIAL(il->ucode_ver));
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004564
4565 /*
4566 * For any of the failures below (before allocating pci memory)
4567 * we will try to load a version with a smaller API -- maybe the
4568 * user just got a corrupted version of the latest API.
4569 */
4570
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004571 D_INFO("f/w package hdr ucode version raw = 0x%x\n", il->ucode_ver);
4572 D_INFO("f/w package hdr runtime inst size = %Zd\n", pieces.inst_size);
4573 D_INFO("f/w package hdr runtime data size = %Zd\n", pieces.data_size);
4574 D_INFO("f/w package hdr init inst size = %Zd\n", pieces.init_size);
4575 D_INFO("f/w package hdr init data size = %Zd\n", pieces.init_data_size);
4576 D_INFO("f/w package hdr boot inst size = %Zd\n", pieces.boot_size);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004577
4578 /* Verify that uCode images will fit in card's SRAM */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004579 if (pieces.inst_size > il->hw_params.max_inst_size) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004580 IL_ERR("uCode instr len %Zd too large to fit in\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004581 pieces.inst_size);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004582 goto try_again;
4583 }
4584
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004585 if (pieces.data_size > il->hw_params.max_data_size) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004586 IL_ERR("uCode data len %Zd too large to fit in\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004587 pieces.data_size);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004588 goto try_again;
4589 }
4590
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004591 if (pieces.init_size > il->hw_params.max_inst_size) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004592 IL_ERR("uCode init instr len %Zd too large to fit in\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004593 pieces.init_size);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004594 goto try_again;
4595 }
4596
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004597 if (pieces.init_data_size > il->hw_params.max_data_size) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004598 IL_ERR("uCode init data len %Zd too large to fit in\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004599 pieces.init_data_size);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004600 goto try_again;
4601 }
4602
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004603 if (pieces.boot_size > il->hw_params.max_bsm_size) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004604 IL_ERR("uCode boot instr len %Zd too large to fit in\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004605 pieces.boot_size);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004606 goto try_again;
4607 }
4608
4609 /* Allocate ucode buffers for card's bus-master loading ... */
4610
4611 /* Runtime instructions and 2 copies of data:
4612 * 1) unmodified from disk
4613 * 2) backup cache for save/restore during power-downs */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004614 il->ucode_code.len = pieces.inst_size;
4615 il_alloc_fw_desc(il->pci_dev, &il->ucode_code);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004616
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004617 il->ucode_data.len = pieces.data_size;
4618 il_alloc_fw_desc(il->pci_dev, &il->ucode_data);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004619
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004620 il->ucode_data_backup.len = pieces.data_size;
4621 il_alloc_fw_desc(il->pci_dev, &il->ucode_data_backup);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004622
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004623 if (!il->ucode_code.v_addr || !il->ucode_data.v_addr ||
4624 !il->ucode_data_backup.v_addr)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004625 goto err_pci_alloc;
4626
4627 /* Initialization instructions and data */
4628 if (pieces.init_size && pieces.init_data_size) {
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004629 il->ucode_init.len = pieces.init_size;
4630 il_alloc_fw_desc(il->pci_dev, &il->ucode_init);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004631
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004632 il->ucode_init_data.len = pieces.init_data_size;
4633 il_alloc_fw_desc(il->pci_dev, &il->ucode_init_data);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004634
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004635 if (!il->ucode_init.v_addr || !il->ucode_init_data.v_addr)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004636 goto err_pci_alloc;
4637 }
4638
4639 /* Bootstrap (instructions only, no data) */
4640 if (pieces.boot_size) {
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004641 il->ucode_boot.len = pieces.boot_size;
4642 il_alloc_fw_desc(il->pci_dev, &il->ucode_boot);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004643
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004644 if (!il->ucode_boot.v_addr)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004645 goto err_pci_alloc;
4646 }
4647
4648 /* Now that we can no longer fail, copy information */
4649
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004650 il->sta_key_max_num = STA_KEY_MAX_NUM;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004651
4652 /* Copy images into buffers for card's bus-master reads ... */
4653
4654 /* Runtime instructions (first block of data in file) */
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01004655 D_INFO("Copying (but not loading) uCode instr len %Zd\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004656 pieces.inst_size);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004657 memcpy(il->ucode_code.v_addr, pieces.inst, pieces.inst_size);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004658
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01004659 D_INFO("uCode instr buf vaddr = 0x%p, paddr = 0x%08x\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004660 il->ucode_code.v_addr, (u32) il->ucode_code.p_addr);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004661
4662 /*
4663 * Runtime data
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004664 * NOTE: Copy into backup buffer will be done in il_up()
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004665 */
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01004666 D_INFO("Copying (but not loading) uCode data len %Zd\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004667 pieces.data_size);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004668 memcpy(il->ucode_data.v_addr, pieces.data, pieces.data_size);
4669 memcpy(il->ucode_data_backup.v_addr, pieces.data, pieces.data_size);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004670
4671 /* Initialization instructions */
4672 if (pieces.init_size) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004673 D_INFO("Copying (but not loading) init instr len %Zd\n",
4674 pieces.init_size);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004675 memcpy(il->ucode_init.v_addr, pieces.init, pieces.init_size);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004676 }
4677
4678 /* Initialization data */
4679 if (pieces.init_data_size) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004680 D_INFO("Copying (but not loading) init data len %Zd\n",
4681 pieces.init_data_size);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004682 memcpy(il->ucode_init_data.v_addr, pieces.init_data,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004683 pieces.init_data_size);
4684 }
4685
4686 /* Bootstrap instructions */
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01004687 D_INFO("Copying (but not loading) boot instr len %Zd\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004688 pieces.boot_size);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004689 memcpy(il->ucode_boot.v_addr, pieces.boot, pieces.boot_size);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004690
4691 /*
4692 * figure out the offset of chain noise reset and gain commands
4693 * base on the size of standard phy calibration commands table size
4694 */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004695 il->_4965.phy_calib_chain_noise_reset_cmd =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004696 standard_phy_calibration_size;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004697 il->_4965.phy_calib_chain_noise_gain_cmd =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004698 standard_phy_calibration_size + 1;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004699
4700 /**************************************************
4701 * This is still part of probe() in a sense...
4702 *
4703 * 9. Setup and register with mac80211 and debugfs
4704 **************************************************/
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004705 err = il4965_mac_setup_register(il, max_probe_length);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004706 if (err)
4707 goto out_unbind;
4708
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004709 err = il_dbgfs_register(il, DRV_NAME);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004710 if (err)
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004711 IL_ERR("failed to create debugfs files. Ignoring error: %d\n",
4712 err);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004713
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004714 err = sysfs_create_group(&il->pci_dev->dev.kobj, &il_attribute_group);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004715 if (err) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004716 IL_ERR("failed to create sysfs device attributes\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004717 goto out_unbind;
4718 }
4719
4720 /* We have our copies now, allow OS release its copies */
4721 release_firmware(ucode_raw);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004722 complete(&il->_4965.firmware_loading_complete);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004723 return;
4724
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004725try_again:
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004726 /* try next, if any */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004727 if (il4965_request_firmware(il, false))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004728 goto out_unbind;
4729 release_firmware(ucode_raw);
4730 return;
4731
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004732err_pci_alloc:
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004733 IL_ERR("failed to allocate pci memory\n");
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004734 il4965_dealloc_ucode_pci(il);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004735out_unbind:
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004736 complete(&il->_4965.firmware_loading_complete);
4737 device_release_driver(&il->pci_dev->dev);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004738 release_firmware(ucode_raw);
4739}
4740
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004741static const char *const desc_lookup_text[] = {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004742 "OK",
4743 "FAIL",
4744 "BAD_PARAM",
4745 "BAD_CHECKSUM",
4746 "NMI_INTERRUPT_WDG",
4747 "SYSASSERT",
4748 "FATAL_ERROR",
4749 "BAD_COMMAND",
4750 "HW_ERROR_TUNE_LOCK",
4751 "HW_ERROR_TEMPERATURE",
4752 "ILLEGAL_CHAN_FREQ",
Stanislaw Gruszka3b98c7f2011-08-26 16:29:35 +02004753 "VCC_NOT_STBL",
Stanislaw Gruszka9a95b372011-08-31 14:20:23 +02004754 "FH49_ERROR",
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004755 "NMI_INTERRUPT_HOST",
4756 "NMI_INTERRUPT_ACTION_PT",
4757 "NMI_INTERRUPT_UNKNOWN",
4758 "UCODE_VERSION_MISMATCH",
4759 "HW_ERROR_ABS_LOCK",
4760 "HW_ERROR_CAL_LOCK_FAIL",
4761 "NMI_INTERRUPT_INST_ACTION_PT",
4762 "NMI_INTERRUPT_DATA_ACTION_PT",
4763 "NMI_TRM_HW_ER",
4764 "NMI_INTERRUPT_TRM",
Joe Perches861d9c32011-07-08 23:20:24 -07004765 "NMI_INTERRUPT_BREAK_POINT",
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004766 "DEBUG_0",
4767 "DEBUG_1",
4768 "DEBUG_2",
4769 "DEBUG_3",
4770};
4771
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004772static struct {
4773 char *name;
4774 u8 num;
4775} advanced_lookup[] = {
4776 {
4777 "NMI_INTERRUPT_WDG", 0x34}, {
4778 "SYSASSERT", 0x35}, {
4779 "UCODE_VERSION_MISMATCH", 0x37}, {
4780 "BAD_COMMAND", 0x38}, {
4781 "NMI_INTERRUPT_DATA_ACTION_PT", 0x3C}, {
4782 "FATAL_ERROR", 0x3D}, {
4783 "NMI_TRM_HW_ERR", 0x46}, {
4784 "NMI_INTERRUPT_TRM", 0x4C}, {
4785 "NMI_INTERRUPT_BREAK_POINT", 0x54}, {
4786 "NMI_INTERRUPT_WDG_RXF_FULL", 0x5C}, {
4787 "NMI_INTERRUPT_WDG_NO_RBD_RXF_FULL", 0x64}, {
4788 "NMI_INTERRUPT_HOST", 0x66}, {
4789 "NMI_INTERRUPT_ACTION_PT", 0x7C}, {
4790 "NMI_INTERRUPT_UNKNOWN", 0x84}, {
4791 "NMI_INTERRUPT_INST_ACTION_PT", 0x86}, {
4792"ADVANCED_SYSASSERT", 0},};
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004793
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004794static const char *
4795il4965_desc_lookup(u32 num)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004796{
4797 int i;
4798 int max = ARRAY_SIZE(desc_lookup_text);
4799
4800 if (num < max)
4801 return desc_lookup_text[num];
4802
4803 max = ARRAY_SIZE(advanced_lookup) - 1;
4804 for (i = 0; i < max; i++) {
4805 if (advanced_lookup[i].num == num)
4806 break;
4807 }
4808 return advanced_lookup[i].name;
4809}
4810
4811#define ERROR_START_OFFSET (1 * sizeof(u32))
4812#define ERROR_ELEM_SIZE (7 * sizeof(u32))
4813
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004814void
4815il4965_dump_nic_error_log(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004816{
4817 u32 data2, line;
4818 u32 desc, time, count, base, data1;
4819 u32 blink1, blink2, ilink1, ilink2;
4820 u32 pc, hcmd;
4821
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004822 if (il->ucode_type == UCODE_INIT) {
4823 base = le32_to_cpu(il->card_alive_init.error_event_table_ptr);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004824 } else {
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004825 base = le32_to_cpu(il->card_alive.error_event_table_ptr);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004826 }
4827
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004828 if (!il->cfg->ops->lib->is_valid_rtc_data_addr(base)) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004829 IL_ERR("Not valid error log pointer 0x%08X for %s uCode\n",
4830 base, (il->ucode_type == UCODE_INIT) ? "Init" : "RT");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004831 return;
4832 }
4833
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004834 count = il_read_targ_mem(il, base);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004835
4836 if (ERROR_START_OFFSET <= count * ERROR_ELEM_SIZE) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004837 IL_ERR("Start IWL Error Log Dump:\n");
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004838 IL_ERR("Status: 0x%08lX, count: %d\n", il->status, count);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004839 }
4840
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004841 desc = il_read_targ_mem(il, base + 1 * sizeof(u32));
4842 il->isr_stats.err_code = desc;
4843 pc = il_read_targ_mem(il, base + 2 * sizeof(u32));
4844 blink1 = il_read_targ_mem(il, base + 3 * sizeof(u32));
4845 blink2 = il_read_targ_mem(il, base + 4 * sizeof(u32));
4846 ilink1 = il_read_targ_mem(il, base + 5 * sizeof(u32));
4847 ilink2 = il_read_targ_mem(il, base + 6 * sizeof(u32));
4848 data1 = il_read_targ_mem(il, base + 7 * sizeof(u32));
4849 data2 = il_read_targ_mem(il, base + 8 * sizeof(u32));
4850 line = il_read_targ_mem(il, base + 9 * sizeof(u32));
4851 time = il_read_targ_mem(il, base + 11 * sizeof(u32));
4852 hcmd = il_read_targ_mem(il, base + 22 * sizeof(u32));
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004853
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004854 IL_ERR("Desc Time "
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004855 "data1 data2 line\n");
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004856 IL_ERR("%-28s (0x%04X) %010u 0x%08X 0x%08X %u\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004857 il4965_desc_lookup(desc), desc, time, data1, data2, line);
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004858 IL_ERR("pc blink1 blink2 ilink1 ilink2 hcmd\n");
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004859 IL_ERR("0x%05X 0x%05X 0x%05X 0x%05X 0x%05X 0x%05X\n", pc, blink1,
4860 blink2, ilink1, ilink2, hcmd);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004861}
4862
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004863static void
4864il4965_rf_kill_ct_config(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004865{
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004866 struct il_ct_kill_config cmd;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004867 unsigned long flags;
4868 int ret = 0;
4869
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004870 spin_lock_irqsave(&il->lock, flags);
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02004871 _il_wr(il, CSR_UCODE_DRV_GP1_CLR,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004872 CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004873 spin_unlock_irqrestore(&il->lock, flags);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004874
4875 cmd.critical_temperature_R =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004876 cpu_to_le32(il->hw_params.ct_kill_threshold);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004877
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004878 ret = il_send_cmd_pdu(il, C_CT_KILL_CONFIG, sizeof(cmd), &cmd);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004879 if (ret)
Stanislaw Gruszka4d69c752011-08-30 15:26:35 +02004880 IL_ERR("C_CT_KILL_CONFIG failed\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004881 else
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004882 D_INFO("C_CT_KILL_CONFIG " "succeeded, "
4883 "critical temperature is %d\n",
4884 il->hw_params.ct_kill_threshold);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004885}
4886
4887static const s8 default_queue_to_tx_fifo[] = {
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004888 IL_TX_FIFO_VO,
4889 IL_TX_FIFO_VI,
4890 IL_TX_FIFO_BE,
4891 IL_TX_FIFO_BK,
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01004892 IL49_CMD_FIFO_NUM,
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004893 IL_TX_FIFO_UNUSED,
4894 IL_TX_FIFO_UNUSED,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004895};
4896
Stanislaw Gruszkae53aac42011-08-31 11:18:16 +02004897#define IL_MASK(lo, hi) ((1 << (hi)) | ((1 << (hi)) - (1 << (lo))))
4898
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004899static int
4900il4965_alive_notify(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004901{
4902 u32 a;
4903 unsigned long flags;
4904 int i, chan;
4905 u32 reg_val;
4906
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004907 spin_lock_irqsave(&il->lock, flags);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004908
4909 /* Clear 4965's internal Tx Scheduler data base */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004910 il->scd_base_addr = il_rd_prph(il, IL49_SCD_SRAM_BASE_ADDR);
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01004911 a = il->scd_base_addr + IL49_SCD_CONTEXT_DATA_OFFSET;
4912 for (; a < il->scd_base_addr + IL49_SCD_TX_STTS_BITMAP_OFFSET; a += 4)
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004913 il_write_targ_mem(il, a, 0);
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01004914 for (; a < il->scd_base_addr + IL49_SCD_TRANSLATE_TBL_OFFSET; a += 4)
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004915 il_write_targ_mem(il, a, 0);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004916 for (;
4917 a <
4918 il->scd_base_addr +
4919 IL49_SCD_TRANSLATE_TBL_OFFSET_QUEUE(il->hw_params.max_txq_num);
4920 a += 4)
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004921 il_write_targ_mem(il, a, 0);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004922
4923 /* Tel 4965 where to find Tx byte count tables */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004924 il_wr_prph(il, IL49_SCD_DRAM_BASE_ADDR, il->scd_bc_tbls.dma >> 10);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004925
4926 /* Enable DMA channel */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004927 for (chan = 0; chan < FH49_TCSR_CHNL_NUM; chan++)
4928 il_wr(il, FH49_TCSR_CHNL_TX_CONFIG_REG(chan),
4929 FH49_TCSR_TX_CONFIG_REG_VAL_DMA_CHNL_ENABLE |
4930 FH49_TCSR_TX_CONFIG_REG_VAL_DMA_CREDIT_ENABLE);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004931
4932 /* Update FH chicken bits */
Stanislaw Gruszka9a95b372011-08-31 14:20:23 +02004933 reg_val = il_rd(il, FH49_TX_CHICKEN_BITS_REG);
4934 il_wr(il, FH49_TX_CHICKEN_BITS_REG,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004935 reg_val | FH49_TX_CHICKEN_BITS_SCD_AUTO_RETRY_EN);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004936
4937 /* Disable chain mode for all queues */
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01004938 il_wr_prph(il, IL49_SCD_QUEUECHAIN_SEL, 0);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004939
4940 /* Initialize each Tx queue (including the command queue) */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004941 for (i = 0; i < il->hw_params.max_txq_num; i++) {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004942
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +01004943 /* TFD circular buffer read/write idxes */
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01004944 il_wr_prph(il, IL49_SCD_QUEUE_RDPTR(i), 0);
Stanislaw Gruszka0c1a94e2011-08-24 17:37:16 +02004945 il_wr(il, HBUS_TARG_WRPTR, 0 | (i << 8));
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004946
4947 /* Max Tx Window size for Scheduler-ACK mode */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004948 il_write_targ_mem(il,
4949 il->scd_base_addr +
4950 IL49_SCD_CONTEXT_QUEUE_OFFSET(i),
4951 (SCD_WIN_SIZE <<
4952 IL49_SCD_QUEUE_CTX_REG1_WIN_SIZE_POS) &
4953 IL49_SCD_QUEUE_CTX_REG1_WIN_SIZE_MSK);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004954
4955 /* Frame limit */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004956 il_write_targ_mem(il,
4957 il->scd_base_addr +
4958 IL49_SCD_CONTEXT_QUEUE_OFFSET(i) +
4959 sizeof(u32),
4960 (SCD_FRAME_LIMIT <<
4961 IL49_SCD_QUEUE_CTX_REG2_FRAME_LIMIT_POS) &
4962 IL49_SCD_QUEUE_CTX_REG2_FRAME_LIMIT_MSK);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004963
4964 }
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01004965 il_wr_prph(il, IL49_SCD_INTERRUPT_MASK,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01004966 (1 << il->hw_params.max_txq_num) - 1);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004967
4968 /* Activate all Tx DMA/FIFO channels */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004969 il4965_txq_set_sched(il, IL_MASK(0, 6));
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004970
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004971 il4965_set_wr_ptrs(il, IL_DEFAULT_CMD_QUEUE_NUM, 0);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004972
4973 /* make sure all queue are not stopped */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004974 memset(&il->queue_stopped[0], 0, sizeof(il->queue_stopped));
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004975 for (i = 0; i < 4; i++)
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004976 atomic_set(&il->queue_stop_count[i], 0);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004977
4978 /* reset to 0 to enable all the queue first */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004979 il->txq_ctx_active_msk = 0;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004980 /* Map each Tx/cmd queue to its corresponding fifo */
4981 BUILD_BUG_ON(ARRAY_SIZE(default_queue_to_tx_fifo) != 7);
4982
4983 for (i = 0; i < ARRAY_SIZE(default_queue_to_tx_fifo); i++) {
4984 int ac = default_queue_to_tx_fifo[i];
4985
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004986 il_txq_ctx_activate(il, i);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004987
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004988 if (ac == IL_TX_FIFO_UNUSED)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004989 continue;
4990
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004991 il4965_tx_queue_set_status(il, &il->txq[i], ac, 0);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004992 }
4993
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004994 spin_unlock_irqrestore(&il->lock, flags);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004995
4996 return 0;
4997}
4998
4999/**
Stanislaw Gruszka4d69c752011-08-30 15:26:35 +02005000 * il4965_alive_start - called after N_ALIVE notification received
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005001 * from protocol/runtime uCode (initialization uCode's
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005002 * Alive gets handled by il_init_alive_start()).
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005003 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005004static void
5005il4965_alive_start(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005006{
5007 int ret = 0;
Stanislaw Gruszka7c2cde22011-11-15 11:29:04 +01005008 struct il_rxon_context *ctx = &il->ctx;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005009
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005010 D_INFO("Runtime Alive received.\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005011
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005012 if (il->card_alive.is_valid != UCODE_VALID_OK) {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005013 /* We had an error bringing up the hardware, so take it
5014 * all the way back down so we can try again */
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005015 D_INFO("Alive failed.\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005016 goto restart;
5017 }
5018
5019 /* Initialize uCode has loaded Runtime uCode ... verify inst image.
5020 * This is a paranoid check, because we would not have gotten the
5021 * "runtime" alive if code weren't properly loaded. */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005022 if (il4965_verify_ucode(il)) {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005023 /* Runtime instruction load was bad;
5024 * take it all the way back down so we can try again */
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005025 D_INFO("Bad runtime uCode load.\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005026 goto restart;
5027 }
5028
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005029 ret = il4965_alive_notify(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005030 if (ret) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005031 IL_WARN("Could not complete ALIVE transition [ntf]: %d\n", ret);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005032 goto restart;
5033 }
5034
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005035 /* After the ALIVE response, we can send host commands to the uCode */
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005036 set_bit(S_ALIVE, &il->status);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005037
5038 /* Enable watchdog to monitor the driver tx queues */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005039 il_setup_watchdog(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005040
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005041 if (il_is_rfkill(il))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005042 return;
5043
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005044 ieee80211_wake_queues(il->hw);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005045
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +02005046 il->active_rate = RATES_MASK;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005047
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005048 if (il_is_associated_ctx(ctx)) {
5049 struct il_rxon_cmd *active_rxon =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005050 (struct il_rxon_cmd *)&ctx->active;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005051 /* apply any changes in staging */
5052 ctx->staging.filter_flags |= RXON_FILTER_ASSOC_MSK;
5053 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
5054 } else {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005055 /* Initialize our rx_config data */
Stanislaw Gruszka17d6e552011-08-29 12:52:20 +02005056 il_connection_init_rx_config(il, &il->ctx);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005057
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005058 if (il->cfg->ops->hcmd->set_rxon_chain)
5059 il->cfg->ops->hcmd->set_rxon_chain(il, ctx);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005060 }
5061
5062 /* Configure bluetooth coexistence if enabled */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005063 il_send_bt_config(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005064
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005065 il4965_reset_run_time_calib(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005066
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005067 set_bit(S_READY, &il->status);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005068
5069 /* Configure the adapter for unassociated operation */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005070 il_commit_rxon(il, ctx);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005071
5072 /* At this point, the NIC is initialized and operational */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005073 il4965_rf_kill_ct_config(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005074
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005075 D_INFO("ALIVE processing complete.\n");
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005076 wake_up(&il->wait_command_queue);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005077
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005078 il_power_update_mode(il, true);
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005079 D_INFO("Updated power mode\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005080
5081 return;
5082
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005083restart:
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005084 queue_work(il->workqueue, &il->restart);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005085}
5086
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005087static void il4965_cancel_deferred_work(struct il_priv *il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005088
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005089static void
5090__il4965_down(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005091{
5092 unsigned long flags;
Stanislaw Gruszkaab42b402011-04-28 11:51:24 +02005093 int exit_pending;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005094
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005095 D_INFO(DRV_NAME " is going down\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005096
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005097 il_scan_cancel_timeout(il, 200);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005098
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005099 exit_pending = test_and_set_bit(S_EXIT_PENDING, &il->status);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005100
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005101 /* Stop TX queues watchdog. We need to have S_EXIT_PENDING bit set
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005102 * to prevent rearm timer */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005103 del_timer_sync(&il->watchdog);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005104
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005105 il_clear_ucode_stations(il, NULL);
5106 il_dealloc_bcast_stations(il);
5107 il_clear_driver_stations(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005108
5109 /* Unblock any waiting calls */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005110 wake_up_all(&il->wait_command_queue);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005111
5112 /* Wipe out the EXIT_PENDING status bit if we are not actually
5113 * exiting the module */
5114 if (!exit_pending)
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005115 clear_bit(S_EXIT_PENDING, &il->status);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005116
5117 /* stop and reset the on-board processor */
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02005118 _il_wr(il, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005119
5120 /* tell the device to stop sending interrupts */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005121 spin_lock_irqsave(&il->lock, flags);
5122 il_disable_interrupts(il);
5123 spin_unlock_irqrestore(&il->lock, flags);
5124 il4965_synchronize_irq(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005125
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005126 if (il->mac80211_registered)
5127 ieee80211_stop_queues(il->hw);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005128
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005129 /* If we have not previously called il_init() then
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005130 * clear all bits but the RF Kill bit and return */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005131 if (!il_is_init(il)) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005132 il->status =
5133 test_bit(S_RF_KILL_HW,
5134 &il->
5135 status) << S_RF_KILL_HW |
5136 test_bit(S_GEO_CONFIGURED,
5137 &il->
5138 status) << S_GEO_CONFIGURED |
5139 test_bit(S_EXIT_PENDING, &il->status) << S_EXIT_PENDING;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005140 goto exit;
5141 }
5142
5143 /* ...otherwise clear out all the status bits but the RF Kill
5144 * bit and continue taking the NIC down. */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005145 il->status &=
5146 test_bit(S_RF_KILL_HW,
5147 &il->status) << S_RF_KILL_HW | test_bit(S_GEO_CONFIGURED,
5148 &il->
5149 status) <<
5150 S_GEO_CONFIGURED | test_bit(S_FW_ERROR,
5151 &il->
5152 status) << S_FW_ERROR |
5153 test_bit(S_EXIT_PENDING, &il->status) << S_EXIT_PENDING;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005154
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005155 il4965_txq_ctx_stop(il);
5156 il4965_rxq_stop(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005157
5158 /* Power-down device's busmaster DMA clocks */
Stanislaw Gruszkadb54eb52011-08-24 21:06:33 +02005159 il_wr_prph(il, APMG_CLK_DIS_REG, APMG_CLK_VAL_DMA_CLK_RQT);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005160 udelay(5);
5161
5162 /* Make sure (redundant) we've released our request to stay awake */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005163 il_clear_bit(il, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005164
5165 /* Stop the device, and put it in low power state */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005166 il_apm_stop(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005167
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005168exit:
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005169 memset(&il->card_alive, 0, sizeof(struct il_alive_resp));
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005170
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005171 dev_kfree_skb(il->beacon_skb);
5172 il->beacon_skb = NULL;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005173
5174 /* clear out any free frames */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005175 il4965_clear_free_frames(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005176}
5177
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005178static void
5179il4965_down(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005180{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005181 mutex_lock(&il->mutex);
5182 __il4965_down(il);
5183 mutex_unlock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005184
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005185 il4965_cancel_deferred_work(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005186}
5187
5188#define HW_READY_TIMEOUT (50)
5189
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005190static int
5191il4965_set_hw_ready(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005192{
5193 int ret = 0;
5194
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005195 il_set_bit(il, CSR_HW_IF_CONFIG_REG,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005196 CSR_HW_IF_CONFIG_REG_BIT_NIC_READY);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005197
5198 /* See if we got it */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005199 ret =
5200 _il_poll_bit(il, CSR_HW_IF_CONFIG_REG,
5201 CSR_HW_IF_CONFIG_REG_BIT_NIC_READY,
5202 CSR_HW_IF_CONFIG_REG_BIT_NIC_READY, HW_READY_TIMEOUT);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005203 if (ret != -ETIMEDOUT)
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005204 il->hw_ready = true;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005205 else
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005206 il->hw_ready = false;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005207
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005208 D_INFO("hardware %s\n", (il->hw_ready == 1) ? "ready" : "not ready");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005209 return ret;
5210}
5211
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005212static int
5213il4965_prepare_card_hw(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005214{
5215 int ret = 0;
5216
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005217 D_INFO("il4965_prepare_card_hw enter\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005218
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005219 ret = il4965_set_hw_ready(il);
5220 if (il->hw_ready)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005221 return ret;
5222
5223 /* If HW is not ready, prepare the conditions to check again */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005224 il_set_bit(il, CSR_HW_IF_CONFIG_REG, CSR_HW_IF_CONFIG_REG_PREPARE);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005225
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005226 ret =
5227 _il_poll_bit(il, CSR_HW_IF_CONFIG_REG,
5228 ~CSR_HW_IF_CONFIG_REG_BIT_NIC_PREPARE_DONE,
5229 CSR_HW_IF_CONFIG_REG_BIT_NIC_PREPARE_DONE, 150000);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005230
5231 /* HW should be ready by now, check again. */
5232 if (ret != -ETIMEDOUT)
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005233 il4965_set_hw_ready(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005234
5235 return ret;
5236}
5237
5238#define MAX_HW_RESTARTS 5
5239
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005240static int
5241__il4965_up(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005242{
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005243 int i;
5244 int ret;
5245
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005246 if (test_bit(S_EXIT_PENDING, &il->status)) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02005247 IL_WARN("Exit pending; will not bring the NIC up\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005248 return -EIO;
5249 }
5250
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005251 if (!il->ucode_data_backup.v_addr || !il->ucode_data.v_addr) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02005252 IL_ERR("ucode not available for device bringup\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005253 return -EIO;
5254 }
5255
Stanislaw Gruszka17d6e552011-08-29 12:52:20 +02005256 ret = il4965_alloc_bcast_station(il, &il->ctx);
5257 if (ret) {
5258 il_dealloc_bcast_stations(il);
5259 return ret;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005260 }
5261
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005262 il4965_prepare_card_hw(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005263
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005264 if (!il->hw_ready) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02005265 IL_WARN("Exit HW not ready\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005266 return -EIO;
5267 }
5268
5269 /* If platform's RF_KILL switch is NOT set to KILL */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005270 if (_il_rd(il, CSR_GP_CNTRL) & CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005271 clear_bit(S_RF_KILL_HW, &il->status);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005272 else
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005273 set_bit(S_RF_KILL_HW, &il->status);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005274
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005275 if (il_is_rfkill(il)) {
5276 wiphy_rfkill_set_hw_state(il->hw->wiphy, true);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005277
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005278 il_enable_interrupts(il);
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02005279 IL_WARN("Radio disabled by HW RF Kill switch\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005280 return 0;
5281 }
5282
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02005283 _il_wr(il, CSR_INT, 0xFFFFFFFF);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005284
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005285 /* must be initialised before il_hw_nic_init */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005286 il->cmd_queue = IL_DEFAULT_CMD_QUEUE_NUM;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005287
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005288 ret = il4965_hw_nic_init(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005289 if (ret) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02005290 IL_ERR("Unable to init nic\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005291 return ret;
5292 }
5293
5294 /* make sure rfkill handshake bits are cleared */
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02005295 _il_wr(il, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005296 _il_wr(il, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005297
5298 /* clear (again), then enable host interrupts */
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02005299 _il_wr(il, CSR_INT, 0xFFFFFFFF);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005300 il_enable_interrupts(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005301
5302 /* really make sure rfkill handshake bits are cleared */
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02005303 _il_wr(il, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
5304 _il_wr(il, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005305
5306 /* Copy original ucode data image from disk into backup cache.
5307 * This will be used to initialize the on-board processor's
5308 * data SRAM for a clean start when the runtime program first loads. */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005309 memcpy(il->ucode_data_backup.v_addr, il->ucode_data.v_addr,
5310 il->ucode_data.len);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005311
5312 for (i = 0; i < MAX_HW_RESTARTS; i++) {
5313
5314 /* load bootstrap state machine,
5315 * load bootstrap program into processor's memory,
5316 * prepare to load the "initialize" uCode */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005317 ret = il->cfg->ops->lib->load_ucode(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005318
5319 if (ret) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005320 IL_ERR("Unable to set up bootstrap uCode: %d\n", ret);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005321 continue;
5322 }
5323
5324 /* start card; "initialize" will load runtime ucode */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005325 il4965_nic_start(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005326
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005327 D_INFO(DRV_NAME " is coming up\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005328
5329 return 0;
5330 }
5331
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005332 set_bit(S_EXIT_PENDING, &il->status);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005333 __il4965_down(il);
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005334 clear_bit(S_EXIT_PENDING, &il->status);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005335
5336 /* tried to restart and config the device for as long as our
5337 * patience could withstand */
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02005338 IL_ERR("Unable to initialize device after %d attempts.\n", i);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005339 return -EIO;
5340}
5341
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005342/*****************************************************************************
5343 *
5344 * Workqueue callbacks
5345 *
5346 *****************************************************************************/
5347
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005348static void
5349il4965_bg_init_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, init_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 il->cfg->ops->lib->init_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_alive_start(struct work_struct *data)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005365{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005366 struct il_priv *il =
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005367 container_of(data, struct il_priv, alive_start.work);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005368
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005369 mutex_lock(&il->mutex);
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005370 if (test_bit(S_EXIT_PENDING, &il->status))
Stanislaw Gruszka28a6e572011-04-28 11:51:32 +02005371 goto out;
5372
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005373 il4965_alive_start(il);
Stanislaw Gruszka28a6e572011-04-28 11:51:32 +02005374out:
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005375 mutex_unlock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005376}
5377
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005378static void
5379il4965_bg_run_time_calib_work(struct work_struct *work)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005380{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005381 struct il_priv *il = container_of(work, struct il_priv,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005382 run_time_calib_work);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005383
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005384 mutex_lock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005385
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005386 if (test_bit(S_EXIT_PENDING, &il->status) ||
5387 test_bit(S_SCANNING, &il->status)) {
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005388 mutex_unlock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005389 return;
5390 }
5391
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005392 if (il->start_calib) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005393 il4965_chain_noise_calibration(il, (void *)&il->_4965.stats);
5394 il4965_sensitivity_calibration(il, (void *)&il->_4965.stats);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005395 }
5396
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005397 mutex_unlock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005398}
5399
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005400static void
5401il4965_bg_restart(struct work_struct *data)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005402{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005403 struct il_priv *il = container_of(data, struct il_priv, restart);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005404
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005405 if (test_bit(S_EXIT_PENDING, &il->status))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005406 return;
5407
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005408 if (test_and_clear_bit(S_FW_ERROR, &il->status)) {
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005409 mutex_lock(&il->mutex);
Stanislaw Gruszka17d6e552011-08-29 12:52:20 +02005410 il->ctx.vif = NULL;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005411 il->is_open = 0;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005412
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005413 __il4965_down(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005414
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005415 mutex_unlock(&il->mutex);
5416 il4965_cancel_deferred_work(il);
5417 ieee80211_restart_hw(il->hw);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005418 } else {
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005419 il4965_down(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005420
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005421 mutex_lock(&il->mutex);
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005422 if (test_bit(S_EXIT_PENDING, &il->status)) {
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005423 mutex_unlock(&il->mutex);
Stanislaw Gruszka28a6e572011-04-28 11:51:32 +02005424 return;
5425 }
5426
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005427 __il4965_up(il);
5428 mutex_unlock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005429 }
5430}
5431
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005432static void
5433il4965_bg_rx_replenish(struct work_struct *data)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005434{
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005435 struct il_priv *il = container_of(data, struct il_priv, rx_replenish);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005436
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005437 if (test_bit(S_EXIT_PENDING, &il->status))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005438 return;
5439
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005440 mutex_lock(&il->mutex);
5441 il4965_rx_replenish(il);
5442 mutex_unlock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005443}
5444
5445/*****************************************************************************
5446 *
5447 * mac80211 entry point functions
5448 *
5449 *****************************************************************************/
5450
5451#define UCODE_READY_TIMEOUT (4 * HZ)
5452
5453/*
5454 * Not a mac80211 entry point function, but it fits in with all the
5455 * other mac80211 functions grouped here.
5456 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005457static int
5458il4965_mac_setup_register(struct il_priv *il, u32 max_probe_length)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005459{
5460 int ret;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005461 struct ieee80211_hw *hw = il->hw;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005462
5463 hw->rate_control_algorithm = "iwl-4965-rs";
5464
5465 /* Tell mac80211 our characteristics */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005466 hw->flags =
5467 IEEE80211_HW_SIGNAL_DBM | IEEE80211_HW_AMPDU_AGGREGATION |
5468 IEEE80211_HW_NEED_DTIM_PERIOD | IEEE80211_HW_SPECTRUM_MGMT |
5469 IEEE80211_HW_REPORTS_TX_ACK_STATUS;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005470
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005471 if (il->cfg->sku & IL_SKU_N)
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005472 hw->flags |=
5473 IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS |
5474 IEEE80211_HW_SUPPORTS_STATIC_SMPS;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005475
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005476 hw->sta_data_size = sizeof(struct il_station_priv);
5477 hw->vif_data_size = sizeof(struct il_vif_priv);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005478
Stanislaw Gruszka17d6e552011-08-29 12:52:20 +02005479 hw->wiphy->interface_modes |= il->ctx.interface_modes;
5480 hw->wiphy->interface_modes |= il->ctx.exclusive_interface_modes;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005481
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005482 hw->wiphy->flags |=
5483 WIPHY_FLAG_CUSTOM_REGULATORY | WIPHY_FLAG_DISABLE_BEACON_HINTS;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005484
5485 /*
5486 * For now, disable PS by default because it affects
5487 * RX performance significantly.
5488 */
5489 hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
5490
5491 hw->wiphy->max_scan_ssids = PROBE_OPTION_MAX;
5492 /* we create the 802.11 header and a zero-length SSID element */
5493 hw->wiphy->max_scan_ie_len = max_probe_length - 24 - 2;
5494
5495 /* Default value; 4 EDCA QOS priorities */
5496 hw->queues = 4;
5497
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005498 hw->max_listen_interval = IL_CONN_MAX_LISTEN_INTERVAL;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005499
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005500 if (il->bands[IEEE80211_BAND_2GHZ].n_channels)
5501 il->hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005502 &il->bands[IEEE80211_BAND_2GHZ];
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005503 if (il->bands[IEEE80211_BAND_5GHZ].n_channels)
5504 il->hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005505 &il->bands[IEEE80211_BAND_5GHZ];
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005506
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005507 il_leds_init(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005508
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005509 ret = ieee80211_register_hw(il->hw);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005510 if (ret) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02005511 IL_ERR("Failed to register hw (error %d)\n", ret);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005512 return ret;
5513 }
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005514 il->mac80211_registered = 1;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005515
5516 return 0;
5517}
5518
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005519int
5520il4965_mac_start(struct ieee80211_hw *hw)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005521{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005522 struct il_priv *il = hw->priv;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005523 int ret;
5524
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005525 D_MAC80211("enter\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005526
5527 /* we should be verifying the device is ready to be opened */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005528 mutex_lock(&il->mutex);
5529 ret = __il4965_up(il);
5530 mutex_unlock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005531
5532 if (ret)
5533 return ret;
5534
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005535 if (il_is_rfkill(il))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005536 goto out;
5537
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005538 D_INFO("Start UP work done.\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005539
5540 /* Wait for START_ALIVE from Run Time ucode. Otherwise callbacks from
5541 * mac80211 will not be run successfully. */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005542 ret = wait_event_timeout(il->wait_command_queue,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005543 test_bit(S_READY, &il->status),
5544 UCODE_READY_TIMEOUT);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005545 if (!ret) {
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005546 if (!test_bit(S_READY, &il->status)) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02005547 IL_ERR("START_ALIVE timeout after %dms.\n",
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005548 jiffies_to_msecs(UCODE_READY_TIMEOUT));
5549 return -ETIMEDOUT;
5550 }
5551 }
5552
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005553 il4965_led_enable(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005554
5555out:
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005556 il->is_open = 1;
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005557 D_MAC80211("leave\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005558 return 0;
5559}
5560
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005561void
5562il4965_mac_stop(struct ieee80211_hw *hw)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005563{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005564 struct il_priv *il = hw->priv;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005565
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005566 D_MAC80211("enter\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005567
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005568 if (!il->is_open)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005569 return;
5570
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005571 il->is_open = 0;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005572
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005573 il4965_down(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005574
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005575 flush_workqueue(il->workqueue);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005576
Stanislaw Gruszkaa078a1f2011-04-28 11:51:25 +02005577 /* User space software may expect getting rfkill changes
5578 * even if interface is down */
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02005579 _il_wr(il, CSR_INT, 0xFFFFFFFF);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005580 il_enable_rfkill_int(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005581
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005582 D_MAC80211("leave\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005583}
5584
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005585void
5586il4965_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005587{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005588 struct il_priv *il = hw->priv;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005589
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005590 D_MACDUMP("enter\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005591
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005592 D_TX("dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005593 ieee80211_get_tx_rate(hw, IEEE80211_SKB_CB(skb))->bitrate);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005594
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005595 if (il4965_tx_skb(il, skb))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005596 dev_kfree_skb_any(skb);
5597
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005598 D_MACDUMP("leave\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005599}
5600
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005601void
5602il4965_mac_update_tkip_key(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
5603 struct ieee80211_key_conf *keyconf,
5604 struct ieee80211_sta *sta, u32 iv32, u16 * phase1key)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005605{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005606 struct il_priv *il = hw->priv;
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005607 struct il_vif_priv *vif_priv = (void *)vif->drv_priv;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005608
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005609 D_MAC80211("enter\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005610
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005611 il4965_update_tkip_key(il, vif_priv->ctx, keyconf, sta, iv32,
5612 phase1key);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005613
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005614 D_MAC80211("leave\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005615}
5616
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005617int
5618il4965_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
5619 struct ieee80211_vif *vif, struct ieee80211_sta *sta,
5620 struct ieee80211_key_conf *key)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005621{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005622 struct il_priv *il = hw->priv;
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005623 struct il_vif_priv *vif_priv = (void *)vif->drv_priv;
5624 struct il_rxon_context *ctx = vif_priv->ctx;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005625 int ret;
5626 u8 sta_id;
5627 bool is_default_wep_key = false;
5628
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005629 D_MAC80211("enter\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005630
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005631 if (il->cfg->mod_params->sw_crypto) {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005632 D_MAC80211("leave - hwcrypto disabled\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005633 return -EOPNOTSUPP;
5634 }
5635
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005636 sta_id = il_sta_id_or_broadcast(il, vif_priv->ctx, sta);
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005637 if (sta_id == IL_INVALID_STATION)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005638 return -EINVAL;
5639
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005640 mutex_lock(&il->mutex);
5641 il_scan_cancel_timeout(il, 100);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005642
5643 /*
5644 * If we are getting WEP group key and we didn't receive any key mapping
5645 * so far, we are in legacy wep mode (group key only), otherwise we are
5646 * in 1X mode.
5647 * In legacy wep mode, we use another host command to the uCode.
5648 */
5649 if ((key->cipher == WLAN_CIPHER_SUITE_WEP40 ||
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005650 key->cipher == WLAN_CIPHER_SUITE_WEP104) && !sta) {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005651 if (cmd == SET_KEY)
5652 is_default_wep_key = !ctx->key_mapping_keys;
5653 else
5654 is_default_wep_key =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005655 (key->hw_key_idx == HW_KEY_DEFAULT);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005656 }
5657
5658 switch (cmd) {
5659 case SET_KEY:
5660 if (is_default_wep_key)
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005661 ret =
5662 il4965_set_default_wep_key(il, vif_priv->ctx, key);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005663 else
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005664 ret =
5665 il4965_set_dynamic_key(il, vif_priv->ctx, key,
5666 sta_id);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005667
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005668 D_MAC80211("enable hwcrypto key\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005669 break;
5670 case DISABLE_KEY:
5671 if (is_default_wep_key)
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005672 ret = il4965_remove_default_wep_key(il, ctx, key);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005673 else
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005674 ret = il4965_remove_dynamic_key(il, ctx, key, sta_id);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005675
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005676 D_MAC80211("disable hwcrypto key\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005677 break;
5678 default:
5679 ret = -EINVAL;
5680 }
5681
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005682 mutex_unlock(&il->mutex);
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005683 D_MAC80211("leave\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005684
5685 return ret;
5686}
5687
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005688int
5689il4965_mac_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
5690 enum ieee80211_ampdu_mlme_action action,
5691 struct ieee80211_sta *sta, u16 tid, u16 * ssn,
5692 u8 buf_size)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005693{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005694 struct il_priv *il = hw->priv;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005695 int ret = -EINVAL;
5696
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005697 D_HT("A-MPDU action on addr %pM tid %d\n", sta->addr, tid);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005698
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005699 if (!(il->cfg->sku & IL_SKU_N))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005700 return -EACCES;
5701
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005702 mutex_lock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005703
5704 switch (action) {
5705 case IEEE80211_AMPDU_RX_START:
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005706 D_HT("start Rx\n");
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005707 ret = il4965_sta_rx_agg_start(il, sta, tid, *ssn);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005708 break;
5709 case IEEE80211_AMPDU_RX_STOP:
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005710 D_HT("stop Rx\n");
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005711 ret = il4965_sta_rx_agg_stop(il, sta, tid);
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005712 if (test_bit(S_EXIT_PENDING, &il->status))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005713 ret = 0;
5714 break;
5715 case IEEE80211_AMPDU_TX_START:
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005716 D_HT("start Tx\n");
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005717 ret = il4965_tx_agg_start(il, vif, sta, tid, ssn);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005718 break;
5719 case IEEE80211_AMPDU_TX_STOP:
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005720 D_HT("stop Tx\n");
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005721 ret = il4965_tx_agg_stop(il, vif, sta, tid);
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005722 if (test_bit(S_EXIT_PENDING, &il->status))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005723 ret = 0;
5724 break;
5725 case IEEE80211_AMPDU_TX_OPERATIONAL:
5726 ret = 0;
5727 break;
5728 }
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005729 mutex_unlock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005730
5731 return ret;
5732}
5733
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005734int
5735il4965_mac_sta_add(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
5736 struct ieee80211_sta *sta)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005737{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005738 struct il_priv *il = hw->priv;
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005739 struct il_station_priv *sta_priv = (void *)sta->drv_priv;
5740 struct il_vif_priv *vif_priv = (void *)vif->drv_priv;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005741 bool is_ap = vif->type == NL80211_IFTYPE_STATION;
5742 int ret;
5743 u8 sta_id;
5744
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005745 D_INFO("received request to add station %pM\n", sta->addr);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005746 mutex_lock(&il->mutex);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005747 D_INFO("proceeding to add station %pM\n", sta->addr);
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005748 sta_priv->common.sta_id = IL_INVALID_STATION;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005749
5750 atomic_set(&sta_priv->pending_frames, 0);
5751
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005752 ret =
5753 il_add_station_common(il, vif_priv->ctx, sta->addr, is_ap, sta,
5754 &sta_id);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005755 if (ret) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005756 IL_ERR("Unable to add station %pM (%d)\n", sta->addr, ret);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005757 /* Should we return success if return code is EEXIST ? */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005758 mutex_unlock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005759 return ret;
5760 }
5761
5762 sta_priv->common.sta_id = sta_id;
5763
5764 /* Initialize rate scaling */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005765 D_INFO("Initializing rate scaling for station %pM\n", sta->addr);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005766 il4965_rs_rate_init(il, sta, sta_id);
5767 mutex_unlock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005768
5769 return 0;
5770}
5771
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005772void
5773il4965_mac_channel_switch(struct ieee80211_hw *hw,
5774 struct ieee80211_channel_switch *ch_switch)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005775{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005776 struct il_priv *il = hw->priv;
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005777 const struct il_channel_info *ch_info;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005778 struct ieee80211_conf *conf = &hw->conf;
5779 struct ieee80211_channel *channel = ch_switch->channel;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005780 struct il_ht_config *ht_conf = &il->current_ht_config;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005781
Stanislaw Gruszka7c2cde22011-11-15 11:29:04 +01005782 struct il_rxon_context *ctx = &il->ctx;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005783 u16 ch;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005784
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005785 D_MAC80211("enter\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005786
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005787 mutex_lock(&il->mutex);
Stanislaw Gruszka28a6e572011-04-28 11:51:32 +02005788
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005789 if (il_is_rfkill(il))
Stanislaw Gruszka28a6e572011-04-28 11:51:32 +02005790 goto out;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005791
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005792 if (test_bit(S_EXIT_PENDING, &il->status) ||
5793 test_bit(S_SCANNING, &il->status) ||
5794 test_bit(S_CHANNEL_SWITCH_PENDING, &il->status))
Stanislaw Gruszka28a6e572011-04-28 11:51:32 +02005795 goto out;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005796
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005797 if (!il_is_associated_ctx(ctx))
Stanislaw Gruszka28a6e572011-04-28 11:51:32 +02005798 goto out;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005799
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005800 if (!il->cfg->ops->lib->set_channel_switch)
Stanislaw Gruszka7f1f9742011-06-08 15:28:29 +02005801 goto out;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005802
Stanislaw Gruszka7f1f9742011-06-08 15:28:29 +02005803 ch = channel->hw_value;
5804 if (le16_to_cpu(ctx->active.channel) == ch)
5805 goto out;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005806
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005807 ch_info = il_get_channel_info(il, channel->band, ch);
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005808 if (!il_is_channel_valid(ch_info)) {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005809 D_MAC80211("invalid channel\n");
Stanislaw Gruszka7f1f9742011-06-08 15:28:29 +02005810 goto out;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005811 }
Stanislaw Gruszka7f1f9742011-06-08 15:28:29 +02005812
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005813 spin_lock_irq(&il->lock);
Stanislaw Gruszka7f1f9742011-06-08 15:28:29 +02005814
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005815 il->current_ht_config.smps = conf->smps_mode;
Stanislaw Gruszka7f1f9742011-06-08 15:28:29 +02005816
5817 /* Configure HT40 channels */
5818 ctx->ht.enabled = conf_is_ht(conf);
5819 if (ctx->ht.enabled) {
5820 if (conf_is_ht40_minus(conf)) {
5821 ctx->ht.extension_chan_offset =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005822 IEEE80211_HT_PARAM_CHA_SEC_BELOW;
Stanislaw Gruszka7f1f9742011-06-08 15:28:29 +02005823 ctx->ht.is_40mhz = true;
5824 } else if (conf_is_ht40_plus(conf)) {
5825 ctx->ht.extension_chan_offset =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005826 IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
Stanislaw Gruszka7f1f9742011-06-08 15:28:29 +02005827 ctx->ht.is_40mhz = true;
5828 } else {
5829 ctx->ht.extension_chan_offset =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005830 IEEE80211_HT_PARAM_CHA_SEC_NONE;
Stanislaw Gruszka7f1f9742011-06-08 15:28:29 +02005831 ctx->ht.is_40mhz = false;
5832 }
5833 } else
5834 ctx->ht.is_40mhz = false;
5835
5836 if ((le16_to_cpu(ctx->staging.channel) != ch))
5837 ctx->staging.flags = 0;
5838
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005839 il_set_rxon_channel(il, channel, ctx);
5840 il_set_rxon_ht(il, ht_conf);
5841 il_set_flags_for_band(il, ctx, channel->band, ctx->vif);
Stanislaw Gruszka7f1f9742011-06-08 15:28:29 +02005842
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005843 spin_unlock_irq(&il->lock);
Stanislaw Gruszka7f1f9742011-06-08 15:28:29 +02005844
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005845 il_set_rate(il);
Stanislaw Gruszka7f1f9742011-06-08 15:28:29 +02005846 /*
5847 * at this point, staging_rxon has the
5848 * configuration for channel switch
5849 */
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005850 set_bit(S_CHANNEL_SWITCH_PENDING, &il->status);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005851 il->switch_channel = cpu_to_le16(ch);
5852 if (il->cfg->ops->lib->set_channel_switch(il, ch_switch)) {
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005853 clear_bit(S_CHANNEL_SWITCH_PENDING, &il->status);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005854 il->switch_channel = 0;
Stanislaw Gruszka7f1f9742011-06-08 15:28:29 +02005855 ieee80211_chswitch_done(ctx->vif, false);
5856 }
5857
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005858out:
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005859 mutex_unlock(&il->mutex);
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005860 D_MAC80211("leave\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005861}
5862
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005863void
5864il4965_configure_filter(struct ieee80211_hw *hw, unsigned int changed_flags,
5865 unsigned int *total_flags, u64 multicast)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005866{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005867 struct il_priv *il = hw->priv;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005868 __le32 filter_or = 0, filter_nand = 0;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005869
5870#define CHK(test, flag) do { \
5871 if (*total_flags & (test)) \
5872 filter_or |= (flag); \
5873 else \
5874 filter_nand |= (flag); \
5875 } while (0)
5876
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005877 D_MAC80211("Enter: changed: 0x%x, total: 0x%x\n", changed_flags,
5878 *total_flags);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005879
5880 CHK(FIF_OTHER_BSS | FIF_PROMISC_IN_BSS, RXON_FILTER_PROMISC_MSK);
5881 /* Setting _just_ RXON_FILTER_CTL2HOST_MSK causes FH errors */
5882 CHK(FIF_CONTROL, RXON_FILTER_CTL2HOST_MSK | RXON_FILTER_PROMISC_MSK);
5883 CHK(FIF_BCN_PRBRESP_PROMISC, RXON_FILTER_BCON_AWARE_MSK);
5884
5885#undef CHK
5886
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005887 mutex_lock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005888
Stanislaw Gruszka17d6e552011-08-29 12:52:20 +02005889 il->ctx.staging.filter_flags &= ~filter_nand;
5890 il->ctx.staging.filter_flags |= filter_or;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005891
Stanislaw Gruszka17d6e552011-08-29 12:52:20 +02005892 /*
5893 * Not committing directly because hardware can perform a scan,
5894 * but we'll eventually commit the filter flags change anyway.
5895 */
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005896
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005897 mutex_unlock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005898
5899 /*
5900 * Receiving all multicast frames is always enabled by the
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005901 * default flags setup in il_connection_init_rx_config()
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005902 * since we currently do not support programming multicast
5903 * filters into the device.
5904 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005905 *total_flags &=
5906 FIF_OTHER_BSS | FIF_ALLMULTI | FIF_PROMISC_IN_BSS |
5907 FIF_BCN_PRBRESP_PROMISC | FIF_CONTROL;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005908}
5909
5910/*****************************************************************************
5911 *
5912 * driver setup and teardown
5913 *
5914 *****************************************************************************/
5915
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005916static void
5917il4965_bg_txpower_work(struct work_struct *work)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005918{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005919 struct il_priv *il = container_of(work, struct il_priv,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005920 txpower_work);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005921
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005922 mutex_lock(&il->mutex);
Stanislaw Gruszkaf3257572011-04-28 11:36:54 +02005923
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005924 /* If a scan happened to start before we got here
Stanislaw Gruszkaebf0d902011-08-26 15:43:47 +02005925 * then just return; the stats notification will
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005926 * kick off another scheduled work to compensate for
5927 * any temperature delta we missed here. */
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005928 if (test_bit(S_EXIT_PENDING, &il->status) ||
5929 test_bit(S_SCANNING, &il->status))
Stanislaw Gruszkaf3257572011-04-28 11:36:54 +02005930 goto out;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005931
5932 /* Regardless of if we are associated, we must reconfigure the
5933 * TX power since frames can be sent on non-radar channels while
5934 * not associated */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005935 il->cfg->ops->lib->send_tx_power(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005936
5937 /* Update last_temperature to keep is_calib_needed from running
5938 * when it isn't needed... */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005939 il->last_temperature = il->temperature;
Stanislaw Gruszkaf3257572011-04-28 11:36:54 +02005940out:
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005941 mutex_unlock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005942}
5943
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005944static void
5945il4965_setup_deferred_work(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005946{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005947 il->workqueue = create_singlethread_workqueue(DRV_NAME);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005948
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005949 init_waitqueue_head(&il->wait_command_queue);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005950
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005951 INIT_WORK(&il->restart, il4965_bg_restart);
5952 INIT_WORK(&il->rx_replenish, il4965_bg_rx_replenish);
5953 INIT_WORK(&il->run_time_calib_work, il4965_bg_run_time_calib_work);
5954 INIT_DELAYED_WORK(&il->init_alive_start, il4965_bg_init_alive_start);
5955 INIT_DELAYED_WORK(&il->alive_start, il4965_bg_alive_start);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005956
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005957 il_setup_scan_deferred_work(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005958
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005959 INIT_WORK(&il->txpower_work, il4965_bg_txpower_work);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005960
Stanislaw Gruszkaebf0d902011-08-26 15:43:47 +02005961 init_timer(&il->stats_periodic);
5962 il->stats_periodic.data = (unsigned long)il;
5963 il->stats_periodic.function = il4965_bg_stats_periodic;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005964
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005965 init_timer(&il->watchdog);
5966 il->watchdog.data = (unsigned long)il;
5967 il->watchdog.function = il_bg_watchdog;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005968
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005969 tasklet_init(&il->irq_tasklet,
5970 (void (*)(unsigned long))il4965_irq_tasklet,
5971 (unsigned long)il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005972}
5973
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005974static void
5975il4965_cancel_deferred_work(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005976{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005977 cancel_work_sync(&il->txpower_work);
5978 cancel_delayed_work_sync(&il->init_alive_start);
5979 cancel_delayed_work(&il->alive_start);
5980 cancel_work_sync(&il->run_time_calib_work);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005981
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005982 il_cancel_scan_deferred_work(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005983
Stanislaw Gruszkaebf0d902011-08-26 15:43:47 +02005984 del_timer_sync(&il->stats_periodic);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005985}
5986
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005987static void
5988il4965_init_hw_rates(struct il_priv *il, struct ieee80211_rate *rates)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005989{
5990 int i;
5991
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +02005992 for (i = 0; i < RATE_COUNT_LEGACY; i++) {
Stanislaw Gruszkad2ddf6212011-08-16 14:17:04 +02005993 rates[i].bitrate = il_rates[i].ieee * 5;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01005994 rates[i].hw_value = i; /* Rate scaling will work on idxes */
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005995 rates[i].hw_value_short = i;
5996 rates[i].flags = 0;
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005997 if ((i >= IL_FIRST_CCK_RATE) && (i <= IL_LAST_CCK_RATE)) {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005998 /*
5999 * If CCK != 1M then set short preamble rate flag.
6000 */
6001 rates[i].flags |=
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006002 (il_rates[i].plcp ==
6003 RATE_1M_PLCP) ? 0 : IEEE80211_RATE_SHORT_PREAMBLE;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006004 }
6005 }
6006}
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006007
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006008/*
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006009 * Acquire il->lock before calling this function !
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006010 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006011void
6012il4965_set_wr_ptrs(struct il_priv *il, int txq_id, u32 idx)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006013{
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006014 il_wr(il, HBUS_TARG_WRPTR, (idx & 0xff) | (txq_id << 8));
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +01006015 il_wr_prph(il, IL49_SCD_QUEUE_RDPTR(txq_id), idx);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006016}
6017
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006018void
6019il4965_tx_queue_set_status(struct il_priv *il, struct il_tx_queue *txq,
6020 int tx_fifo_id, int scd_retry)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006021{
6022 int txq_id = txq->q.id;
6023
6024 /* Find out whether to activate Tx queue */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006025 int active = test_bit(txq_id, &il->txq_ctx_active_msk) ? 1 : 0;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006026
6027 /* Set up and activate */
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01006028 il_wr_prph(il, IL49_SCD_QUEUE_STATUS_BITS(txq_id),
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006029 (active << IL49_SCD_QUEUE_STTS_REG_POS_ACTIVE) | (tx_fifo_id
6030 <<
6031 IL49_SCD_QUEUE_STTS_REG_POS_TXF)
6032 | (scd_retry << IL49_SCD_QUEUE_STTS_REG_POS_WSL) | (scd_retry
6033 <<
6034 IL49_SCD_QUEUE_STTS_REG_POS_SCD_ACK)
6035 | IL49_SCD_QUEUE_STTS_REG_MSK);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006036
6037 txq->sched_retry = scd_retry;
6038
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006039 D_INFO("%s %s Queue %d on AC %d\n", active ? "Activate" : "Deactivate",
6040 scd_retry ? "BA" : "AC", txq_id, tx_fifo_id);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006041}
6042
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006043static int
6044il4965_init_drv(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006045{
6046 int ret;
6047
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006048 spin_lock_init(&il->sta_lock);
6049 spin_lock_init(&il->hcmd_lock);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006050
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006051 INIT_LIST_HEAD(&il->free_frames);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006052
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006053 mutex_init(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006054
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006055 il->ieee_channels = NULL;
6056 il->ieee_rates = NULL;
6057 il->band = IEEE80211_BAND_2GHZ;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006058
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006059 il->iw_mode = NL80211_IFTYPE_STATION;
6060 il->current_ht_config.smps = IEEE80211_SMPS_STATIC;
6061 il->missed_beacon_threshold = IL_MISSED_BEACON_THRESHOLD_DEF;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006062
6063 /* initialize force reset */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006064 il->force_reset.reset_duration = IL_DELAY_NEXT_FORCE_FW_RELOAD;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006065
6066 /* Choose which receivers/antennas to use */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006067 if (il->cfg->ops->hcmd->set_rxon_chain)
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006068 il->cfg->ops->hcmd->set_rxon_chain(il, &il->ctx);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006069
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006070 il_init_scan_params(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006071
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006072 ret = il_init_channel_map(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006073 if (ret) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02006074 IL_ERR("initializing regulatory failed: %d\n", ret);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006075 goto err;
6076 }
6077
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006078 ret = il_init_geos(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006079 if (ret) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02006080 IL_ERR("initializing geos failed: %d\n", ret);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006081 goto err_free_channel_map;
6082 }
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006083 il4965_init_hw_rates(il, il->ieee_rates);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006084
6085 return 0;
6086
6087err_free_channel_map:
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006088 il_free_channel_map(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006089err:
6090 return ret;
6091}
6092
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006093static void
6094il4965_uninit_drv(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006095{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006096 il4965_calib_free_results(il);
6097 il_free_geos(il);
6098 il_free_channel_map(il);
6099 kfree(il->scan_cmd);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006100}
6101
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006102static void
6103il4965_hw_detect(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006104{
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02006105 il->hw_rev = _il_rd(il, CSR_HW_REV);
6106 il->hw_wa_rev = _il_rd(il, CSR_HW_REV_WA_REG);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006107 il->rev_id = il->pci_dev->revision;
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01006108 D_INFO("HW Revision ID = 0x%X\n", il->rev_id);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006109}
6110
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006111static int
6112il4965_set_hw_params(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006113{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006114 il->hw_params.max_rxq_size = RX_QUEUE_SIZE;
6115 il->hw_params.max_rxq_log = RX_QUEUE_SIZE_LOG;
6116 if (il->cfg->mod_params->amsdu_size_8K)
6117 il->hw_params.rx_page_order = get_order(IL_RX_BUF_SIZE_8K);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006118 else
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006119 il->hw_params.rx_page_order = get_order(IL_RX_BUF_SIZE_4K);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006120
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006121 il->hw_params.max_beacon_itrvl = IL_MAX_UCODE_BEACON_INTERVAL;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006122
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006123 if (il->cfg->mod_params->disable_11n)
6124 il->cfg->sku &= ~IL_SKU_N;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006125
6126 /* Device-specific setup */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006127 return il->cfg->ops->lib->set_hw_params(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006128}
6129
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006130static const u8 il4965_bss_ac_to_fifo[] = {
6131 IL_TX_FIFO_VO,
6132 IL_TX_FIFO_VI,
6133 IL_TX_FIFO_BE,
6134 IL_TX_FIFO_BK,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006135};
6136
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006137static const u8 il4965_bss_ac_to_queue[] = {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006138 0, 1, 2, 3,
6139};
6140
6141static int
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006142il4965_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006143{
Stanislaw Gruszka7c2cde22011-11-15 11:29:04 +01006144 int err = 0;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006145 struct il_priv *il;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006146 struct ieee80211_hw *hw;
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006147 struct il_cfg *cfg = (struct il_cfg *)(ent->driver_data);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006148 unsigned long flags;
6149 u16 pci_cmd;
6150
6151 /************************
6152 * 1. Allocating HW data
6153 ************************/
6154
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006155 hw = il_alloc_all(cfg);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006156 if (!hw) {
6157 err = -ENOMEM;
6158 goto out;
6159 }
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006160 il = hw->priv;
6161 /* At this point both hw and il are allocated. */
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006162
Stanislaw Gruszka7c2cde22011-11-15 11:29:04 +01006163 il->ctx.ctxid = 0;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006164
Stanislaw Gruszka7c2cde22011-11-15 11:29:04 +01006165 il->ctx.always_active = true;
6166 il->ctx.is_active = true;
Stanislaw Gruszka4d69c752011-08-30 15:26:35 +02006167 il->ctx.rxon_cmd = C_RXON;
6168 il->ctx.rxon_timing_cmd = C_RXON_TIMING;
6169 il->ctx.rxon_assoc_cmd = C_RXON_ASSOC;
6170 il->ctx.qos_cmd = C_QOS_PARAM;
Stanislaw Gruszka7c2cde22011-11-15 11:29:04 +01006171 il->ctx.ap_sta_id = IL_AP_ID;
Stanislaw Gruszka4d69c752011-08-30 15:26:35 +02006172 il->ctx.wep_key_cmd = C_WEPKEY;
Stanislaw Gruszka7c2cde22011-11-15 11:29:04 +01006173 il->ctx.ac_to_fifo = il4965_bss_ac_to_fifo;
6174 il->ctx.ac_to_queue = il4965_bss_ac_to_queue;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006175 il->ctx.exclusive_interface_modes = BIT(NL80211_IFTYPE_ADHOC);
6176 il->ctx.interface_modes = BIT(NL80211_IFTYPE_STATION);
Stanislaw Gruszka7c2cde22011-11-15 11:29:04 +01006177 il->ctx.ap_devtype = RXON_DEV_TYPE_AP;
6178 il->ctx.ibss_devtype = RXON_DEV_TYPE_IBSS;
6179 il->ctx.station_devtype = RXON_DEV_TYPE_ESS;
6180 il->ctx.unused_devtype = RXON_DEV_TYPE_ESS;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006181
6182 SET_IEEE80211_DEV(hw, &pdev->dev);
6183
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01006184 D_INFO("*** LOAD DRIVER ***\n");
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006185 il->cfg = cfg;
6186 il->pci_dev = pdev;
6187 il->inta_mask = CSR_INI_SET_MASK;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006188
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006189 if (il_alloc_traffic_mem(il))
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02006190 IL_ERR("Not enough memory to generate traffic log\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006191
6192 /**************************
6193 * 2. Initializing PCI bus
6194 **************************/
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006195 pci_disable_link_state(pdev,
6196 PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
6197 PCIE_LINK_STATE_CLKPM);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006198
6199 if (pci_enable_device(pdev)) {
6200 err = -ENODEV;
6201 goto out_ieee80211_free_hw;
6202 }
6203
6204 pci_set_master(pdev);
6205
6206 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(36));
6207 if (!err)
6208 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(36));
6209 if (err) {
6210 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
6211 if (!err)
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006212 err =
6213 pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006214 /* both attempts failed: */
6215 if (err) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02006216 IL_WARN("No suitable DMA available.\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006217 goto out_pci_disable_device;
6218 }
6219 }
6220
6221 err = pci_request_regions(pdev, DRV_NAME);
6222 if (err)
6223 goto out_pci_disable_device;
6224
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006225 pci_set_drvdata(pdev, il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006226
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006227 /***********************
6228 * 3. Read REV register
6229 ***********************/
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006230 il->hw_base = pci_iomap(pdev, 0, 0);
6231 if (!il->hw_base) {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006232 err = -ENODEV;
6233 goto out_pci_release_regions;
6234 }
6235
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01006236 D_INFO("pci_resource_len = 0x%08llx\n",
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006237 (unsigned long long)pci_resource_len(pdev, 0));
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01006238 D_INFO("pci_resource_base = %p\n", il->hw_base);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006239
6240 /* these spin locks will be used in apm_ops.init and EEPROM access
6241 * we should init now
6242 */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006243 spin_lock_init(&il->reg_lock);
6244 spin_lock_init(&il->lock);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006245
6246 /*
6247 * stop and reset the on-board processor just in case it is in a
6248 * strange state ... like being left stranded by a primary kernel
6249 * and this is now the kdump kernel trying to start up
6250 */
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02006251 _il_wr(il, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006252
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006253 il4965_hw_detect(il);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006254 IL_INFO("Detected %s, REV=0x%X\n", il->cfg->name, il->hw_rev);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006255
6256 /* We disable the RETRY_TIMEOUT register (0x41) to keep
6257 * PCI Tx retries from interfering with C3 CPU state */
6258 pci_write_config_byte(pdev, PCI_CFG_RETRY_TIMEOUT, 0x00);
6259
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006260 il4965_prepare_card_hw(il);
6261 if (!il->hw_ready) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02006262 IL_WARN("Failed, HW not ready\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006263 goto out_iounmap;
6264 }
6265
6266 /*****************
6267 * 4. Read EEPROM
6268 *****************/
6269 /* Read the EEPROM */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006270 err = il_eeprom_init(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006271 if (err) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02006272 IL_ERR("Unable to init EEPROM\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006273 goto out_iounmap;
6274 }
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006275 err = il4965_eeprom_check_version(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006276 if (err)
6277 goto out_free_eeprom;
6278
6279 if (err)
6280 goto out_free_eeprom;
6281
6282 /* extract MAC Address */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006283 il4965_eeprom_get_mac(il, il->addresses[0].addr);
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01006284 D_INFO("MAC address: %pM\n", il->addresses[0].addr);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006285 il->hw->wiphy->addresses = il->addresses;
6286 il->hw->wiphy->n_addresses = 1;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006287
6288 /************************
6289 * 5. Setup HW constants
6290 ************************/
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006291 if (il4965_set_hw_params(il)) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02006292 IL_ERR("failed to set hw parameters\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006293 goto out_free_eeprom;
6294 }
6295
6296 /*******************
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006297 * 6. Setup il
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006298 *******************/
6299
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006300 err = il4965_init_drv(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006301 if (err)
6302 goto out_free_eeprom;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006303 /* At this point both hw and il are initialized. */
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006304
6305 /********************
6306 * 7. Setup services
6307 ********************/
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006308 spin_lock_irqsave(&il->lock, flags);
6309 il_disable_interrupts(il);
6310 spin_unlock_irqrestore(&il->lock, flags);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006311
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006312 pci_enable_msi(il->pci_dev);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006313
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006314 err = request_irq(il->pci_dev->irq, il_isr, IRQF_SHARED, DRV_NAME, il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006315 if (err) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02006316 IL_ERR("Error allocating IRQ %d\n", il->pci_dev->irq);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006317 goto out_disable_msi;
6318 }
6319
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006320 il4965_setup_deferred_work(il);
Stanislaw Gruszkad0c72342011-08-30 15:39:42 +02006321 il4965_setup_handlers(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006322
6323 /*********************************************
6324 * 8. Enable interrupts and read RFKILL state
6325 *********************************************/
6326
Stanislaw Gruszkaa078a1f2011-04-28 11:51:25 +02006327 /* enable rfkill interrupt: hw bug w/a */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006328 pci_read_config_word(il->pci_dev, PCI_COMMAND, &pci_cmd);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006329 if (pci_cmd & PCI_COMMAND_INTX_DISABLE) {
6330 pci_cmd &= ~PCI_COMMAND_INTX_DISABLE;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006331 pci_write_config_word(il->pci_dev, PCI_COMMAND, pci_cmd);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006332 }
6333
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006334 il_enable_rfkill_int(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006335
6336 /* If platform's RF_KILL switch is NOT set to KILL */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006337 if (_il_rd(il, CSR_GP_CNTRL) & CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01006338 clear_bit(S_RF_KILL_HW, &il->status);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006339 else
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01006340 set_bit(S_RF_KILL_HW, &il->status);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006341
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006342 wiphy_rfkill_set_hw_state(il->hw->wiphy,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006343 test_bit(S_RF_KILL_HW, &il->status));
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006344
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006345 il_power_initialize(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006346
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006347 init_completion(&il->_4965.firmware_loading_complete);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006348
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006349 err = il4965_request_firmware(il, true);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006350 if (err)
6351 goto out_destroy_workqueue;
6352
6353 return 0;
6354
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006355out_destroy_workqueue:
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006356 destroy_workqueue(il->workqueue);
6357 il->workqueue = NULL;
6358 free_irq(il->pci_dev->irq, il);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006359out_disable_msi:
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006360 pci_disable_msi(il->pci_dev);
6361 il4965_uninit_drv(il);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006362out_free_eeprom:
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006363 il_eeprom_free(il);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006364out_iounmap:
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006365 pci_iounmap(pdev, il->hw_base);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006366out_pci_release_regions:
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006367 pci_set_drvdata(pdev, NULL);
6368 pci_release_regions(pdev);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006369out_pci_disable_device:
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006370 pci_disable_device(pdev);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006371out_ieee80211_free_hw:
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006372 il_free_traffic_mem(il);
6373 ieee80211_free_hw(il->hw);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006374out:
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006375 return err;
6376}
6377
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006378static void __devexit
6379il4965_pci_remove(struct pci_dev *pdev)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006380{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006381 struct il_priv *il = pci_get_drvdata(pdev);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006382 unsigned long flags;
6383
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006384 if (!il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006385 return;
6386
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006387 wait_for_completion(&il->_4965.firmware_loading_complete);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006388
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01006389 D_INFO("*** UNLOAD DRIVER ***\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006390
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006391 il_dbgfs_unregister(il);
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006392 sysfs_remove_group(&pdev->dev.kobj, &il_attribute_group);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006393
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006394 /* ieee80211_unregister_hw call wil cause il_mac_stop to
6395 * to be called and il4965_down since we are removing the device
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01006396 * we need to set S_EXIT_PENDING bit.
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006397 */
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01006398 set_bit(S_EXIT_PENDING, &il->status);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006399
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006400 il_leds_exit(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006401
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006402 if (il->mac80211_registered) {
6403 ieee80211_unregister_hw(il->hw);
6404 il->mac80211_registered = 0;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006405 } else {
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006406 il4965_down(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006407 }
6408
6409 /*
6410 * Make sure device is reset to low power before unloading driver.
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006411 * This may be redundant with il4965_down(), but there are paths to
6412 * run il4965_down() without calling apm_ops.stop(), and there are
6413 * paths to avoid running il4965_down() at all before leaving driver.
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006414 * This (inexpensive) call *makes sure* device is reset.
6415 */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006416 il_apm_stop(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006417
6418 /* make sure we flush any pending irq or
6419 * tasklet for the driver
6420 */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006421 spin_lock_irqsave(&il->lock, flags);
6422 il_disable_interrupts(il);
6423 spin_unlock_irqrestore(&il->lock, flags);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006424
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006425 il4965_synchronize_irq(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006426
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006427 il4965_dealloc_ucode_pci(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006428
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006429 if (il->rxq.bd)
6430 il4965_rx_queue_free(il, &il->rxq);
6431 il4965_hw_txq_ctx_free(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006432
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006433 il_eeprom_free(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006434
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006435 /*netif_stop_queue(dev); */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006436 flush_workqueue(il->workqueue);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006437
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006438 /* ieee80211_unregister_hw calls il_mac_stop, which flushes
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006439 * il->workqueue... so we can't take down the workqueue
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006440 * until now... */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006441 destroy_workqueue(il->workqueue);
6442 il->workqueue = NULL;
6443 il_free_traffic_mem(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006444
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006445 free_irq(il->pci_dev->irq, il);
6446 pci_disable_msi(il->pci_dev);
6447 pci_iounmap(pdev, il->hw_base);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006448 pci_release_regions(pdev);
6449 pci_disable_device(pdev);
6450 pci_set_drvdata(pdev, NULL);
6451
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006452 il4965_uninit_drv(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006453
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006454 dev_kfree_skb(il->beacon_skb);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006455
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006456 ieee80211_free_hw(il->hw);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006457}
6458
6459/*
6460 * Activate/Deactivate Tx DMA/FIFO channels according tx fifos mask
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006461 * must be called under il->lock and mac access
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006462 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006463void
6464il4965_txq_set_sched(struct il_priv *il, u32 mask)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006465{
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01006466 il_wr_prph(il, IL49_SCD_TXFACT, mask);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006467}
6468
6469/*****************************************************************************
6470 *
6471 * driver and module entry point
6472 *
6473 *****************************************************************************/
6474
6475/* Hardware specific file defines the PCI IDs table for that hardware module */
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006476static DEFINE_PCI_DEVICE_TABLE(il4965_hw_card_ids) = {
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006477 {IL_PCI_DEVICE(0x4229, PCI_ANY_ID, il4965_cfg)},
6478 {IL_PCI_DEVICE(0x4230, PCI_ANY_ID, il4965_cfg)},
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006479 {0}
6480};
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006481MODULE_DEVICE_TABLE(pci, il4965_hw_card_ids);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006482
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006483static struct pci_driver il4965_driver = {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006484 .name = DRV_NAME,
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006485 .id_table = il4965_hw_card_ids,
6486 .probe = il4965_pci_probe,
6487 .remove = __devexit_p(il4965_pci_remove),
6488 .driver.pm = IL_LEGACY_PM_OPS,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006489};
6490
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006491static int __init
6492il4965_init(void)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006493{
6494
6495 int ret;
6496 pr_info(DRV_DESCRIPTION ", " DRV_VERSION "\n");
6497 pr_info(DRV_COPYRIGHT "\n");
6498
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006499 ret = il4965_rate_control_register();
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006500 if (ret) {
6501 pr_err("Unable to register rate control algorithm: %d\n", ret);
6502 return ret;
6503 }
6504
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006505 ret = pci_register_driver(&il4965_driver);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006506 if (ret) {
6507 pr_err("Unable to initialize PCI module\n");
6508 goto error_register;
6509 }
6510
6511 return ret;
6512
6513error_register:
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006514 il4965_rate_control_unregister();
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006515 return ret;
6516}
6517
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006518static void __exit
6519il4965_exit(void)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006520{
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006521 pci_unregister_driver(&il4965_driver);
6522 il4965_rate_control_unregister();
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006523}
6524
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006525module_exit(il4965_exit);
6526module_init(il4965_init);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006527
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01006528#ifdef CONFIG_IWLEGACY_DEBUG
Stanislaw Gruszkad2ddf6212011-08-16 14:17:04 +02006529module_param_named(debug, il_debug_level, uint, S_IRUGO | S_IWUSR);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006530MODULE_PARM_DESC(debug, "debug output mask");
6531#endif
6532
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006533module_param_named(swcrypto, il4965_mod_params.sw_crypto, int, S_IRUGO);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006534MODULE_PARM_DESC(swcrypto, "using crypto in software (default 0 [hardware])");
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006535module_param_named(queues_num, il4965_mod_params.num_of_queues, int, S_IRUGO);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006536MODULE_PARM_DESC(queues_num, "number of hw queues.");
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006537module_param_named(11n_disable, il4965_mod_params.disable_11n, int, S_IRUGO);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006538MODULE_PARM_DESC(11n_disable, "disable 11n functionality");
Stanislaw Gruszkae7392362011-11-15 14:45:59 +01006539module_param_named(amsdu_size_8K, il4965_mod_params.amsdu_size_8K, int,
6540 S_IRUGO);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006541MODULE_PARM_DESC(amsdu_size_8K, "enable 8K amsdu size");
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006542module_param_named(fw_restart, il4965_mod_params.restart_fw, int, S_IRUGO);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006543MODULE_PARM_DESC(fw_restart, "restart firmware in case of error");