blob: a8a6461d103f5b494c46dcf3a082db6b07d15ac9 [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
53#include "iwl-eeprom.h"
54#include "iwl-dev.h"
55#include "iwl-core.h"
56#include "iwl-io.h"
57#include "iwl-helpers.h"
58#include "iwl-sta.h"
59#include "iwl-4965-calib.h"
60#include "iwl-4965.h"
Wey-Yi Guybe663ab2011-02-21 11:27:26 -080061
62
63/******************************************************************************
64 *
65 * module boiler plate
66 *
67 ******************************************************************************/
68
69/*
70 * module name, copyright, version, etc.
71 */
72#define DRV_DESCRIPTION "Intel(R) Wireless WiFi 4965 driver for Linux"
73
Stanislaw Gruszkad3175162011-11-15 11:25:42 +010074#ifdef CONFIG_IWLEGACY_DEBUG
Wey-Yi Guybe663ab2011-02-21 11:27:26 -080075#define VD "d"
76#else
77#define VD
78#endif
79
80#define DRV_VERSION IWLWIFI_VERSION VD
81
82
83MODULE_DESCRIPTION(DRV_DESCRIPTION);
84MODULE_VERSION(DRV_VERSION);
85MODULE_AUTHOR(DRV_COPYRIGHT " " DRV_AUTHOR);
86MODULE_LICENSE("GPL");
87MODULE_ALIAS("iwl4965");
88
Stanislaw Gruszkafcb74582011-08-30 13:06:03 +020089void il4965_check_abort_status(struct il_priv *il,
90 u8 frame_count, u32 status)
91{
92 if (frame_count == 1 && status == TX_STATUS_FAIL_RFKILL_FLUSH) {
93 IL_ERR("Tx flush command to flush out all frames\n");
94 if (!test_bit(STATUS_EXIT_PENDING, &il->status))
95 queue_work(il->workqueue, &il->tx_flush);
96 }
97}
98
99/*
100 * EEPROM
101 */
102struct il_mod_params il4965_mod_params = {
103 .amsdu_size_8K = 1,
104 .restart_fw = 1,
105 /* the rest are 0 by default */
106};
107
108void il4965_rx_queue_reset(struct il_priv *il, struct il_rx_queue *rxq)
109{
110 unsigned long flags;
111 int i;
112 spin_lock_irqsave(&rxq->lock, flags);
113 INIT_LIST_HEAD(&rxq->rx_free);
114 INIT_LIST_HEAD(&rxq->rx_used);
115 /* Fill the rx_used queue with _all_ of the Rx buffers */
116 for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++) {
117 /* In the reset function, these buffers may have been allocated
118 * to an SKB, so we need to unmap and free potential storage */
119 if (rxq->pool[i].page != NULL) {
120 pci_unmap_page(il->pci_dev, rxq->pool[i].page_dma,
121 PAGE_SIZE << il->hw_params.rx_page_order,
122 PCI_DMA_FROMDEVICE);
123 __il_free_pages(il, rxq->pool[i].page);
124 rxq->pool[i].page = NULL;
125 }
126 list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
127 }
128
129 for (i = 0; i < RX_QUEUE_SIZE; i++)
130 rxq->queue[i] = NULL;
131
132 /* Set us so that we have processed and used all buffers, but have
133 * not restocked the Rx queue with fresh buffers */
134 rxq->read = rxq->write = 0;
135 rxq->write_actual = 0;
136 rxq->free_count = 0;
137 spin_unlock_irqrestore(&rxq->lock, flags);
138}
139
140int il4965_rx_init(struct il_priv *il, struct il_rx_queue *rxq)
141{
142 u32 rb_size;
143 const u32 rfdnlog = RX_QUEUE_SIZE_LOG; /* 256 RBDs */
144 u32 rb_timeout = 0;
145
146 if (il->cfg->mod_params->amsdu_size_8K)
147 rb_size = FH_RCSR_RX_CONFIG_REG_VAL_RB_SIZE_8K;
148 else
149 rb_size = FH_RCSR_RX_CONFIG_REG_VAL_RB_SIZE_4K;
150
151 /* Stop Rx DMA */
152 il_wr(il, FH_MEM_RCSR_CHNL0_CONFIG_REG, 0);
153
154 /* Reset driver's Rx queue write idx */
155 il_wr(il, FH_RSCSR_CHNL0_RBDCB_WPTR_REG, 0);
156
157 /* Tell device where to find RBD circular buffer in DRAM */
158 il_wr(il, FH_RSCSR_CHNL0_RBDCB_BASE_REG,
159 (u32)(rxq->bd_dma >> 8));
160
161 /* Tell device where in DRAM to update its Rx status */
162 il_wr(il, FH_RSCSR_CHNL0_STTS_WPTR_REG,
163 rxq->rb_stts_dma >> 4);
164
165 /* Enable Rx DMA
166 * Direct rx interrupts to hosts
167 * Rx buffer size 4 or 8k
168 * RB timeout 0x10
169 * 256 RBDs
170 */
171 il_wr(il, FH_MEM_RCSR_CHNL0_CONFIG_REG,
172 FH_RCSR_RX_CONFIG_CHNL_EN_ENABLE_VAL |
173 FH_RCSR_CHNL0_RX_CONFIG_IRQ_DEST_INT_HOST_VAL |
174 FH_RCSR_CHNL0_RX_CONFIG_SINGLE_FRAME_MSK |
175 rb_size|
176 (rb_timeout << FH_RCSR_RX_CONFIG_REG_IRQ_RBTH_POS)|
177 (rfdnlog << FH_RCSR_RX_CONFIG_RBDCB_SIZE_POS));
178
179 /* Set interrupt coalescing timer to default (2048 usecs) */
180 il_write8(il, CSR_INT_COALESCING, IL_HOST_INT_TIMEOUT_DEF);
181
182 return 0;
183}
184
185static void il4965_set_pwr_vmain(struct il_priv *il)
186{
187/*
188 * (for documentation purposes)
189 * to set power to V_AUX, do:
190
191 if (pci_pme_capable(il->pci_dev, PCI_D3cold))
192 il_set_bits_mask_prph(il, APMG_PS_CTRL_REG,
193 APMG_PS_CTRL_VAL_PWR_SRC_VAUX,
194 ~APMG_PS_CTRL_MSK_PWR_SRC);
195 */
196
197 il_set_bits_mask_prph(il, APMG_PS_CTRL_REG,
198 APMG_PS_CTRL_VAL_PWR_SRC_VMAIN,
199 ~APMG_PS_CTRL_MSK_PWR_SRC);
200}
201
202int il4965_hw_nic_init(struct il_priv *il)
203{
204 unsigned long flags;
205 struct il_rx_queue *rxq = &il->rxq;
206 int ret;
207
208 /* nic_init */
209 spin_lock_irqsave(&il->lock, flags);
210 il->cfg->ops->lib->apm_ops.init(il);
211
212 /* Set interrupt coalescing calibration timer to default (512 usecs) */
213 il_write8(il, CSR_INT_COALESCING, IL_HOST_INT_CALIB_TIMEOUT_DEF);
214
215 spin_unlock_irqrestore(&il->lock, flags);
216
217 il4965_set_pwr_vmain(il);
218
219 il->cfg->ops->lib->apm_ops.config(il);
220
221 /* Allocate the RX queue, or reset if it is already allocated */
222 if (!rxq->bd) {
223 ret = il_rx_queue_alloc(il);
224 if (ret) {
225 IL_ERR("Unable to initialize Rx queue\n");
226 return -ENOMEM;
227 }
228 } else
229 il4965_rx_queue_reset(il, rxq);
230
231 il4965_rx_replenish(il);
232
233 il4965_rx_init(il, rxq);
234
235 spin_lock_irqsave(&il->lock, flags);
236
237 rxq->need_update = 1;
238 il_rx_queue_update_write_ptr(il, rxq);
239
240 spin_unlock_irqrestore(&il->lock, flags);
241
242 /* Allocate or reset and init all Tx and Command queues */
243 if (!il->txq) {
244 ret = il4965_txq_ctx_alloc(il);
245 if (ret)
246 return ret;
247 } else
248 il4965_txq_ctx_reset(il);
249
250 set_bit(STATUS_INIT, &il->status);
251
252 return 0;
253}
254
255/**
256 * il4965_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer ptr
257 */
258static inline __le32 il4965_dma_addr2rbd_ptr(struct il_priv *il,
259 dma_addr_t dma_addr)
260{
261 return cpu_to_le32((u32)(dma_addr >> 8));
262}
263
264/**
265 * il4965_rx_queue_restock - refill RX queue from pre-allocated pool
266 *
267 * If there are slots in the RX queue that need to be restocked,
268 * and we have free pre-allocated buffers, fill the ranks as much
269 * as we can, pulling from rx_free.
270 *
271 * This moves the 'write' idx forward to catch up with 'processed', and
272 * also updates the memory address in the firmware to reference the new
273 * target buffer.
274 */
275void il4965_rx_queue_restock(struct il_priv *il)
276{
277 struct il_rx_queue *rxq = &il->rxq;
278 struct list_head *element;
279 struct il_rx_buf *rxb;
280 unsigned long flags;
281
282 spin_lock_irqsave(&rxq->lock, flags);
283 while (il_rx_queue_space(rxq) > 0 && rxq->free_count) {
284 /* The overwritten rxb must be a used one */
285 rxb = rxq->queue[rxq->write];
286 BUG_ON(rxb && rxb->page);
287
288 /* Get next free Rx buffer, remove from free list */
289 element = rxq->rx_free.next;
290 rxb = list_entry(element, struct il_rx_buf, list);
291 list_del(element);
292
293 /* Point to Rx buffer via next RBD in circular buffer */
294 rxq->bd[rxq->write] = il4965_dma_addr2rbd_ptr(il,
295 rxb->page_dma);
296 rxq->queue[rxq->write] = rxb;
297 rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
298 rxq->free_count--;
299 }
300 spin_unlock_irqrestore(&rxq->lock, flags);
301 /* If the pre-allocated buffer pool is dropping low, schedule to
302 * refill it */
303 if (rxq->free_count <= RX_LOW_WATERMARK)
304 queue_work(il->workqueue, &il->rx_replenish);
305
306
307 /* If we've added more space for the firmware to place data, tell it.
308 * Increment device's write pointer in multiples of 8. */
309 if (rxq->write_actual != (rxq->write & ~0x7)) {
310 spin_lock_irqsave(&rxq->lock, flags);
311 rxq->need_update = 1;
312 spin_unlock_irqrestore(&rxq->lock, flags);
313 il_rx_queue_update_write_ptr(il, rxq);
314 }
315}
316
317/**
318 * il4965_rx_replenish - Move all used packet from rx_used to rx_free
319 *
320 * When moving to rx_free an SKB is allocated for the slot.
321 *
322 * Also restock the Rx queue via il_rx_queue_restock.
323 * This is called as a scheduled work item (except for during initialization)
324 */
325static void il4965_rx_allocate(struct il_priv *il, gfp_t priority)
326{
327 struct il_rx_queue *rxq = &il->rxq;
328 struct list_head *element;
329 struct il_rx_buf *rxb;
330 struct page *page;
331 unsigned long flags;
332 gfp_t gfp_mask = priority;
333
334 while (1) {
335 spin_lock_irqsave(&rxq->lock, flags);
336 if (list_empty(&rxq->rx_used)) {
337 spin_unlock_irqrestore(&rxq->lock, flags);
338 return;
339 }
340 spin_unlock_irqrestore(&rxq->lock, flags);
341
342 if (rxq->free_count > RX_LOW_WATERMARK)
343 gfp_mask |= __GFP_NOWARN;
344
345 if (il->hw_params.rx_page_order > 0)
346 gfp_mask |= __GFP_COMP;
347
348 /* Alloc a new receive buffer */
349 page = alloc_pages(gfp_mask, il->hw_params.rx_page_order);
350 if (!page) {
351 if (net_ratelimit())
352 D_INFO("alloc_pages failed, "
353 "order: %d\n",
354 il->hw_params.rx_page_order);
355
356 if (rxq->free_count <= RX_LOW_WATERMARK &&
357 net_ratelimit())
358 IL_ERR(
359 "Failed to alloc_pages with %s. "
360 "Only %u free buffers remaining.\n",
361 priority == GFP_ATOMIC ?
362 "GFP_ATOMIC" : "GFP_KERNEL",
363 rxq->free_count);
364 /* We don't reschedule replenish work here -- we will
365 * call the restock method and if it still needs
366 * more buffers it will schedule replenish */
367 return;
368 }
369
370 spin_lock_irqsave(&rxq->lock, flags);
371
372 if (list_empty(&rxq->rx_used)) {
373 spin_unlock_irqrestore(&rxq->lock, flags);
374 __free_pages(page, il->hw_params.rx_page_order);
375 return;
376 }
377 element = rxq->rx_used.next;
378 rxb = list_entry(element, struct il_rx_buf, list);
379 list_del(element);
380
381 spin_unlock_irqrestore(&rxq->lock, flags);
382
383 BUG_ON(rxb->page);
384 rxb->page = page;
385 /* Get physical address of the RB */
386 rxb->page_dma = pci_map_page(il->pci_dev, page, 0,
387 PAGE_SIZE << il->hw_params.rx_page_order,
388 PCI_DMA_FROMDEVICE);
389 /* dma address must be no more than 36 bits */
390 BUG_ON(rxb->page_dma & ~DMA_BIT_MASK(36));
391 /* and also 256 byte aligned! */
392 BUG_ON(rxb->page_dma & DMA_BIT_MASK(8));
393
394 spin_lock_irqsave(&rxq->lock, flags);
395
396 list_add_tail(&rxb->list, &rxq->rx_free);
397 rxq->free_count++;
398 il->alloc_rxb_page++;
399
400 spin_unlock_irqrestore(&rxq->lock, flags);
401 }
402}
403
404void il4965_rx_replenish(struct il_priv *il)
405{
406 unsigned long flags;
407
408 il4965_rx_allocate(il, GFP_KERNEL);
409
410 spin_lock_irqsave(&il->lock, flags);
411 il4965_rx_queue_restock(il);
412 spin_unlock_irqrestore(&il->lock, flags);
413}
414
415void il4965_rx_replenish_now(struct il_priv *il)
416{
417 il4965_rx_allocate(il, GFP_ATOMIC);
418
419 il4965_rx_queue_restock(il);
420}
421
422/* Assumes that the skb field of the buffers in 'pool' is kept accurate.
423 * If an SKB has been detached, the POOL needs to have its SKB set to NULL
424 * This free routine walks the list of POOL entries and if SKB is set to
425 * non NULL it is unmapped and freed
426 */
427void il4965_rx_queue_free(struct il_priv *il, struct il_rx_queue *rxq)
428{
429 int i;
430 for (i = 0; i < RX_QUEUE_SIZE + RX_FREE_BUFFERS; i++) {
431 if (rxq->pool[i].page != NULL) {
432 pci_unmap_page(il->pci_dev, rxq->pool[i].page_dma,
433 PAGE_SIZE << il->hw_params.rx_page_order,
434 PCI_DMA_FROMDEVICE);
435 __il_free_pages(il, rxq->pool[i].page);
436 rxq->pool[i].page = NULL;
437 }
438 }
439
440 dma_free_coherent(&il->pci_dev->dev, 4 * RX_QUEUE_SIZE, rxq->bd,
441 rxq->bd_dma);
442 dma_free_coherent(&il->pci_dev->dev, sizeof(struct il_rb_status),
443 rxq->rb_stts, rxq->rb_stts_dma);
444 rxq->bd = NULL;
445 rxq->rb_stts = NULL;
446}
447
448int il4965_rxq_stop(struct il_priv *il)
449{
450
451 /* stop Rx DMA */
452 il_wr(il, FH_MEM_RCSR_CHNL0_CONFIG_REG, 0);
453 il_poll_bit(il, FH_MEM_RSSR_RX_STATUS_REG,
454 FH_RSSR_CHNL0_RX_STATUS_CHNL_IDLE, 1000);
455
456 return 0;
457}
458
459int il4965_hwrate_to_mac80211_idx(u32 rate_n_flags, enum ieee80211_band band)
460{
461 int idx = 0;
462 int band_offset = 0;
463
464 /* HT rate format: mac80211 wants an MCS number, which is just LSB */
465 if (rate_n_flags & RATE_MCS_HT_MSK) {
466 idx = (rate_n_flags & 0xff);
467 return idx;
468 /* Legacy rate format, search for match in table */
469 } else {
470 if (band == IEEE80211_BAND_5GHZ)
471 band_offset = IL_FIRST_OFDM_RATE;
472 for (idx = band_offset; idx < RATE_COUNT_LEGACY; idx++)
473 if (il_rates[idx].plcp == (rate_n_flags & 0xFF))
474 return idx - band_offset;
475 }
476
477 return -1;
478}
479
480static int il4965_calc_rssi(struct il_priv *il,
481 struct il_rx_phy_res *rx_resp)
482{
483 /* data from PHY/DSP regarding signal strength, etc.,
484 * contents are always there, not configurable by host. */
485 struct il4965_rx_non_cfg_phy *ncphy =
486 (struct il4965_rx_non_cfg_phy *)rx_resp->non_cfg_phy_buf;
487 u32 agc = (le16_to_cpu(ncphy->agc_info) & IL49_AGC_DB_MASK)
488 >> IL49_AGC_DB_POS;
489
490 u32 valid_antennae =
491 (le16_to_cpu(rx_resp->phy_flags) & IL49_RX_PHY_FLAGS_ANTENNAE_MASK)
492 >> IL49_RX_PHY_FLAGS_ANTENNAE_OFFSET;
493 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
514
515static u32 il4965_translate_rx_status(struct il_priv *il, u32 decrypt_in)
516{
517 u32 decrypt_out = 0;
518
519 if ((decrypt_in & RX_RES_STATUS_STATION_FOUND) ==
520 RX_RES_STATUS_STATION_FOUND)
521 decrypt_out |= (RX_RES_STATUS_STATION_FOUND |
522 RX_RES_STATUS_NO_STATION_INFO_MISMATCH);
523
524 decrypt_out |= (decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK);
525
526 /* packet was not encrypted */
527 if ((decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) ==
528 RX_RES_STATUS_SEC_TYPE_NONE)
529 return decrypt_out;
530
531 /* packet was encrypted with unknown alg */
532 if ((decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) ==
533 RX_RES_STATUS_SEC_TYPE_ERR)
534 return decrypt_out;
535
536 /* decryption was not done in HW */
537 if ((decrypt_in & RX_MPDU_RES_STATUS_DEC_DONE_MSK) !=
538 RX_MPDU_RES_STATUS_DEC_DONE_MSK)
539 return decrypt_out;
540
541 switch (decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) {
542
543 case RX_RES_STATUS_SEC_TYPE_CCMP:
544 /* alg is CCM: check MIC only */
545 if (!(decrypt_in & RX_MPDU_RES_STATUS_MIC_OK))
546 /* Bad MIC */
547 decrypt_out |= RX_RES_STATUS_BAD_ICV_MIC;
548 else
549 decrypt_out |= RX_RES_STATUS_DECRYPT_OK;
550
551 break;
552
553 case RX_RES_STATUS_SEC_TYPE_TKIP:
554 if (!(decrypt_in & RX_MPDU_RES_STATUS_TTAK_OK)) {
555 /* Bad TTAK */
556 decrypt_out |= RX_RES_STATUS_BAD_KEY_TTAK;
557 break;
558 }
559 /* fall through if TTAK OK */
560 default:
561 if (!(decrypt_in & RX_MPDU_RES_STATUS_ICV_OK))
562 decrypt_out |= RX_RES_STATUS_BAD_ICV_MIC;
563 else
564 decrypt_out |= RX_RES_STATUS_DECRYPT_OK;
565 break;
566 }
567
568 D_RX("decrypt_in:0x%x decrypt_out = 0x%x\n",
569 decrypt_in, decrypt_out);
570
571 return decrypt_out;
572}
573
574static void il4965_pass_packet_to_mac80211(struct il_priv *il,
575 struct ieee80211_hdr *hdr,
576 u16 len,
577 u32 ampdu_status,
578 struct il_rx_buf *rxb,
579 struct ieee80211_rx_status *stats)
580{
581 struct sk_buff *skb;
582 __le16 fc = hdr->frame_control;
583
584 /* We only process data packets if the interface is open */
585 if (unlikely(!il->is_open)) {
586 D_DROP(
587 "Dropping packet while interface is not open.\n");
588 return;
589 }
590
591 /* In case of HW accelerated crypto and bad decryption, drop */
592 if (!il->cfg->mod_params->sw_crypto &&
593 il_set_decrypted_flag(il, hdr, ampdu_status, stats))
594 return;
595
596 skb = dev_alloc_skb(128);
597 if (!skb) {
598 IL_ERR("dev_alloc_skb failed\n");
599 return;
600 }
601
602 skb_add_rx_frag(skb, 0, rxb->page, (void *)hdr - rxb_addr(rxb), len);
603
604 il_update_stats(il, false, fc, len);
605 memcpy(IEEE80211_SKB_RXCB(skb), stats, sizeof(*stats));
606
607 ieee80211_rx(il->hw, skb);
608 il->alloc_rxb_page--;
609 rxb->page = NULL;
610}
611
612/* Called for REPLY_RX (legacy ABG frames), or
613 * REPLY_RX_MPDU_CMD (HT high-throughput N frames). */
614void il4965_rx_reply_rx(struct il_priv *il,
615 struct il_rx_buf *rxb)
616{
617 struct ieee80211_hdr *header;
618 struct ieee80211_rx_status rx_status;
619 struct il_rx_pkt *pkt = rxb_addr(rxb);
620 struct il_rx_phy_res *phy_res;
621 __le32 rx_pkt_status;
622 struct il_rx_mpdu_res_start *amsdu;
623 u32 len;
624 u32 ampdu_status;
625 u32 rate_n_flags;
626
627 /**
628 * REPLY_RX and REPLY_RX_MPDU_CMD are handled differently.
629 * REPLY_RX: physical layer info is in this buffer
630 * REPLY_RX_MPDU_CMD: physical layer info was sent in separate
631 * command and cached in il->last_phy_res
632 *
633 * Here we set up local variables depending on which command is
634 * received.
635 */
636 if (pkt->hdr.cmd == REPLY_RX) {
637 phy_res = (struct il_rx_phy_res *)pkt->u.raw;
638 header = (struct ieee80211_hdr *)(pkt->u.raw + sizeof(*phy_res)
639 + phy_res->cfg_phy_cnt);
640
641 len = le16_to_cpu(phy_res->byte_count);
642 rx_pkt_status = *(__le32 *)(pkt->u.raw + sizeof(*phy_res) +
643 phy_res->cfg_phy_cnt + len);
644 ampdu_status = le32_to_cpu(rx_pkt_status);
645 } else {
646 if (!il->_4965.last_phy_res_valid) {
647 IL_ERR("MPDU frame without cached PHY data\n");
648 return;
649 }
650 phy_res = &il->_4965.last_phy_res;
651 amsdu = (struct il_rx_mpdu_res_start *)pkt->u.raw;
652 header = (struct ieee80211_hdr *)(pkt->u.raw + sizeof(*amsdu));
653 len = le16_to_cpu(amsdu->byte_count);
654 rx_pkt_status = *(__le32 *)(pkt->u.raw + sizeof(*amsdu) + len);
655 ampdu_status = il4965_translate_rx_status(il,
656 le32_to_cpu(rx_pkt_status));
657 }
658
659 if ((unlikely(phy_res->cfg_phy_cnt > 20))) {
660 D_DROP("dsp size out of range [0,20]: %d/n",
661 phy_res->cfg_phy_cnt);
662 return;
663 }
664
665 if (!(rx_pkt_status & RX_RES_STATUS_NO_CRC32_ERROR) ||
666 !(rx_pkt_status & RX_RES_STATUS_NO_RXE_OVERFLOW)) {
667 D_RX("Bad CRC or FIFO: 0x%08X.\n",
668 le32_to_cpu(rx_pkt_status));
669 return;
670 }
671
672 /* This will be used in several places later */
673 rate_n_flags = le32_to_cpu(phy_res->rate_n_flags);
674
675 /* rx_status carries information about the packet to mac80211 */
676 rx_status.mactime = le64_to_cpu(phy_res->timestamp);
677 rx_status.band = (phy_res->phy_flags & RX_RES_PHY_FLAGS_BAND_24_MSK) ?
678 IEEE80211_BAND_2GHZ : IEEE80211_BAND_5GHZ;
679 rx_status.freq =
680 ieee80211_channel_to_frequency(le16_to_cpu(phy_res->channel),
681 rx_status.band);
682 rx_status.rate_idx =
683 il4965_hwrate_to_mac80211_idx(rate_n_flags, rx_status.band);
684 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 */
688 /*rx_status.flag |= RX_FLAG_MACTIME_MPDU;*/
689
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);
696 D_STATS("Rssi %d, TSF %llu\n",
697 rx_status.signal, (unsigned long long)rx_status.mactime);
698
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 =
713 (le16_to_cpu(phy_res->phy_flags) & RX_RES_PHY_FLAGS_ANTENNA_MSK)
714 >> RX_RES_PHY_FLAGS_ANTENNA_POS;
715
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
728 il4965_pass_packet_to_mac80211(il, header, len, ampdu_status,
729 rxb, &rx_status);
730}
731
732/* Cache phy data (Rx signal strength, etc) for HT frame (REPLY_RX_PHY_CMD).
733 * This will be used later in il_rx_reply_rx() for REPLY_RX_MPDU_CMD. */
734void il4965_rx_reply_rx_phy(struct il_priv *il,
735 struct il_rx_buf *rxb)
736{
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
743static int il4965_get_channels_for_scan(struct il_priv *il,
744 struct ieee80211_vif *vif,
745 enum ieee80211_band band,
746 u8 is_active, u8 n_probes,
747 struct il_scan_channel *scan_ch)
748{
749 struct ieee80211_channel *chan;
750 const struct ieee80211_supported_band *sband;
751 const struct il_channel_info *ch_info;
752 u16 passive_dwell = 0;
753 u16 active_dwell = 0;
754 int added, i;
755 u16 channel;
756
757 sband = il_get_hw_mode(il, band);
758 if (!sband)
759 return 0;
760
761 active_dwell = il_get_active_dwell_time(il, band, n_probes);
762 passive_dwell = il_get_passive_dwell_time(il, band, vif);
763
764 if (passive_dwell <= active_dwell)
765 passive_dwell = active_dwell + 1;
766
767 for (i = 0, added = 0; i < il->scan_request->n_channels; i++) {
768 chan = il->scan_request->channels[i];
769
770 if (chan->band != band)
771 continue;
772
773 channel = chan->hw_value;
774 scan_ch->channel = cpu_to_le16(channel);
775
776 ch_info = il_get_channel_info(il, band, channel);
777 if (!il_is_channel_valid(ch_info)) {
778 D_SCAN(
779 "Channel %d is INVALID for this band.\n",
780 channel);
781 continue;
782 }
783
784 if (!is_active || il_is_channel_passive(ch_info) ||
785 (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN))
786 scan_ch->type = SCAN_CHANNEL_TYPE_PASSIVE;
787 else
788 scan_ch->type = SCAN_CHANNEL_TYPE_ACTIVE;
789
790 if (n_probes)
791 scan_ch->type |= IL_SCAN_PROBE_MASK(n_probes);
792
793 scan_ch->active_dwell = cpu_to_le16(active_dwell);
794 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
795
796 /* Set txpower levels to defaults */
797 scan_ch->dsp_atten = 110;
798
799 /* NOTE: if we were doing 6Mb OFDM for scans we'd use
800 * power level:
801 * scan_ch->tx_gain = ((1 << 5) | (2 << 3)) | 3;
802 */
803 if (band == IEEE80211_BAND_5GHZ)
804 scan_ch->tx_gain = ((1 << 5) | (3 << 3)) | 3;
805 else
806 scan_ch->tx_gain = ((1 << 5) | (5 << 3));
807
808 D_SCAN("Scanning ch=%d prob=0x%X [%s %d]\n",
809 channel, le32_to_cpu(scan_ch->type),
810 (scan_ch->type & SCAN_CHANNEL_TYPE_ACTIVE) ?
811 "ACTIVE" : "PASSIVE",
812 (scan_ch->type & SCAN_CHANNEL_TYPE_ACTIVE) ?
813 active_dwell : passive_dwell);
814
815 scan_ch++;
816 added++;
817 }
818
819 D_SCAN("total channels to scan %d\n", added);
820 return added;
821}
822
823int il4965_request_scan(struct il_priv *il, struct ieee80211_vif *vif)
824{
825 struct il_host_cmd cmd = {
826 .id = REPLY_SCAN_CMD,
827 .len = sizeof(struct il_scan_cmd),
828 .flags = CMD_SIZE_HUGE,
829 };
830 struct il_scan_cmd *scan;
831 struct il_rxon_context *ctx = &il->ctx;
832 u32 rate_flags = 0;
833 u16 cmd_len;
834 u16 rx_chain = 0;
835 enum ieee80211_band band;
836 u8 n_probes = 0;
837 u8 rx_ant = il->hw_params.valid_rx_ant;
838 u8 rate;
839 bool is_active = false;
840 int chan_mod;
841 u8 active_chains;
842 u8 scan_tx_antennas = il->hw_params.valid_tx_ant;
843 int ret;
844
845 lockdep_assert_held(&il->mutex);
846
847 if (vif)
848 ctx = il_rxon_ctx_from_vif(vif);
849
850 if (!il->scan_cmd) {
851 il->scan_cmd = kmalloc(sizeof(struct il_scan_cmd) +
852 IL_MAX_SCAN_SIZE, GFP_KERNEL);
853 if (!il->scan_cmd) {
854 D_SCAN(
855 "fail to allocate memory for scan\n");
856 return -ENOMEM;
857 }
858 }
859 scan = il->scan_cmd;
860 memset(scan, 0, sizeof(struct il_scan_cmd) + IL_MAX_SCAN_SIZE);
861
862 scan->quiet_plcp_th = IL_PLCP_QUIET_THRESH;
863 scan->quiet_time = IL_ACTIVE_QUIET_TIME;
864
865 if (il_is_any_associated(il)) {
866 u16 interval;
867 u32 extra;
868 u32 suspend_time = 100;
869 u32 scan_suspend_time = 100;
870
871 D_INFO("Scanning while associated...\n");
872 interval = vif->bss_conf.beacon_int;
873
874 scan->suspend_time = 0;
875 scan->max_out_time = cpu_to_le32(200 * 1024);
876 if (!interval)
877 interval = suspend_time;
878
879 extra = (suspend_time / interval) << 22;
880 scan_suspend_time = (extra |
881 ((suspend_time % interval) * 1024));
882 scan->suspend_time = cpu_to_le32(scan_suspend_time);
883 D_SCAN("suspend_time 0x%X beacon interval %d\n",
884 scan_suspend_time, interval);
885 }
886
887 if (il->scan_request->n_ssids) {
888 int i, p = 0;
889 D_SCAN("Kicking off active scan\n");
890 for (i = 0; i < il->scan_request->n_ssids; i++) {
891 /* always does wildcard anyway */
892 if (!il->scan_request->ssids[i].ssid_len)
893 continue;
894 scan->direct_scan[p].id = WLAN_EID_SSID;
895 scan->direct_scan[p].len =
896 il->scan_request->ssids[i].ssid_len;
897 memcpy(scan->direct_scan[p].ssid,
898 il->scan_request->ssids[i].ssid,
899 il->scan_request->ssids[i].ssid_len);
900 n_probes++;
901 p++;
902 }
903 is_active = true;
904 } else
905 D_SCAN("Start passive scan.\n");
906
907 scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
908 scan->tx_cmd.sta_id = ctx->bcast_sta_id;
909 scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
910
911 switch (il->scan_band) {
912 case IEEE80211_BAND_2GHZ:
913 scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
914 chan_mod = le32_to_cpu(
915 il->ctx.active.flags &
916 RXON_FLG_CHANNEL_MODE_MSK)
917 >> RXON_FLG_CHANNEL_MODE_POS;
918 if (chan_mod == CHANNEL_MODE_PURE_40) {
919 rate = RATE_6M_PLCP;
920 } else {
921 rate = RATE_1M_PLCP;
922 rate_flags = RATE_MCS_CCK_MSK;
923 }
924 break;
925 case IEEE80211_BAND_5GHZ:
926 rate = RATE_6M_PLCP;
927 break;
928 default:
929 IL_WARN("Invalid scan band\n");
930 return -EIO;
931 }
932
933 /*
934 * If active scanning is requested but a certain channel is
935 * marked passive, we can do active scanning if we detect
936 * transmissions.
937 *
938 * There is an issue with some firmware versions that triggers
939 * a sysassert on a "good CRC threshold" of zero (== disabled),
940 * on a radar channel even though this means that we should NOT
941 * send probes.
942 *
943 * The "good CRC threshold" is the number of frames that we
944 * need to receive during our dwell time on a channel before
945 * sending out probes -- setting this to a huge value will
946 * mean we never reach it, but at the same time work around
947 * the aforementioned issue. Thus use IL_GOOD_CRC_TH_NEVER
948 * here instead of IL_GOOD_CRC_TH_DISABLED.
949 */
950 scan->good_CRC_th = is_active ? IL_GOOD_CRC_TH_DEFAULT :
951 IL_GOOD_CRC_TH_NEVER;
952
953 band = il->scan_band;
954
955 if (il->cfg->scan_rx_antennas[band])
956 rx_ant = il->cfg->scan_rx_antennas[band];
957
958 il->scan_tx_ant[band] = il4965_toggle_tx_ant(il,
959 il->scan_tx_ant[band],
960 scan_tx_antennas);
961 rate_flags |= il4965_ant_idx_to_flags(il->scan_tx_ant[band]);
962 scan->tx_cmd.rate_n_flags = il4965_hw_set_rate_n_flags(rate, rate_flags);
963
964 /* In power save mode use one chain, otherwise use all chains */
965 if (test_bit(STATUS_POWER_PMI, &il->status)) {
966 /* rx_ant has been set to all valid chains previously */
967 active_chains = rx_ant &
968 ((u8)(il->chain_noise_data.active_chains));
969 if (!active_chains)
970 active_chains = rx_ant;
971
972 D_SCAN("chain_noise_data.active_chains: %u\n",
973 il->chain_noise_data.active_chains);
974
975 rx_ant = il4965_first_antenna(active_chains);
976 }
977
978 /* MIMO is not used here, but value is required */
979 rx_chain |= il->hw_params.valid_rx_ant << RXON_RX_CHAIN_VALID_POS;
980 rx_chain |= rx_ant << RXON_RX_CHAIN_FORCE_MIMO_SEL_POS;
981 rx_chain |= rx_ant << RXON_RX_CHAIN_FORCE_SEL_POS;
982 rx_chain |= 0x1 << RXON_RX_CHAIN_DRIVER_FORCE_POS;
983 scan->rx_chain = cpu_to_le16(rx_chain);
984
985 cmd_len = il_fill_probe_req(il,
986 (struct ieee80211_mgmt *)scan->data,
987 vif->addr,
988 il->scan_request->ie,
989 il->scan_request->ie_len,
990 IL_MAX_SCAN_SIZE - sizeof(*scan));
991 scan->tx_cmd.len = cpu_to_le16(cmd_len);
992
993 scan->filter_flags |= (RXON_FILTER_ACCEPT_GRP_MSK |
994 RXON_FILTER_BCON_AWARE_MSK);
995
996 scan->channel_count = il4965_get_channels_for_scan(il, vif, band,
997 is_active, n_probes,
998 (void *)&scan->data[cmd_len]);
999 if (scan->channel_count == 0) {
1000 D_SCAN("channel count %d\n", scan->channel_count);
1001 return -EIO;
1002 }
1003
1004 cmd.len += le16_to_cpu(scan->tx_cmd.len) +
1005 scan->channel_count * sizeof(struct il_scan_channel);
1006 cmd.data = scan;
1007 scan->len = cpu_to_le16(cmd.len);
1008
1009 set_bit(STATUS_SCAN_HW, &il->status);
1010
1011 ret = il_send_cmd_sync(il, &cmd);
1012 if (ret)
1013 clear_bit(STATUS_SCAN_HW, &il->status);
1014
1015 return ret;
1016}
1017
1018int il4965_manage_ibss_station(struct il_priv *il,
1019 struct ieee80211_vif *vif, bool add)
1020{
1021 struct il_vif_priv *vif_priv = (void *)vif->drv_priv;
1022
1023 if (add)
1024 return il4965_add_bssid_station(il, vif_priv->ctx,
1025 vif->bss_conf.bssid,
1026 &vif_priv->ibss_bssid_sta_id);
1027 return il_remove_station(il, vif_priv->ibss_bssid_sta_id,
1028 vif->bss_conf.bssid);
1029}
1030
1031void il4965_free_tfds_in_queue(struct il_priv *il,
1032 int sta_id, int tid, int freed)
1033{
1034 lockdep_assert_held(&il->sta_lock);
1035
1036 if (il->stations[sta_id].tid[tid].tfds_in_queue >= freed)
1037 il->stations[sta_id].tid[tid].tfds_in_queue -= freed;
1038 else {
1039 D_TX("free more than tfds_in_queue (%u:%d)\n",
1040 il->stations[sta_id].tid[tid].tfds_in_queue,
1041 freed);
1042 il->stations[sta_id].tid[tid].tfds_in_queue = 0;
1043 }
1044}
1045
1046#define IL_TX_QUEUE_MSK 0xfffff
1047
1048static bool il4965_is_single_rx_stream(struct il_priv *il)
1049{
1050 return il->current_ht_config.smps == IEEE80211_SMPS_STATIC ||
1051 il->current_ht_config.single_chain_sufficient;
1052}
1053
1054#define IL_NUM_RX_CHAINS_MULTIPLE 3
1055#define IL_NUM_RX_CHAINS_SINGLE 2
1056#define IL_NUM_IDLE_CHAINS_DUAL 2
1057#define IL_NUM_IDLE_CHAINS_SINGLE 1
1058
1059/*
1060 * Determine how many receiver/antenna chains to use.
1061 *
1062 * More provides better reception via diversity. Fewer saves power
1063 * at the expense of throughput, but only when not in powersave to
1064 * start with.
1065 *
1066 * MIMO (dual stream) requires at least 2, but works better with 3.
1067 * This does not determine *which* chains to use, just how many.
1068 */
1069static int il4965_get_active_rx_chain_count(struct il_priv *il)
1070{
1071 /* # of Rx chains to use when expecting MIMO. */
1072 if (il4965_is_single_rx_stream(il))
1073 return IL_NUM_RX_CHAINS_SINGLE;
1074 else
1075 return IL_NUM_RX_CHAINS_MULTIPLE;
1076}
1077
1078/*
1079 * When we are in power saving mode, unless device support spatial
1080 * multiplexing power save, use the active count for rx chain count.
1081 */
1082static int
1083il4965_get_idle_rx_chain_count(struct il_priv *il, int active_cnt)
1084{
1085 /* # Rx chains when idling, depending on SMPS mode */
1086 switch (il->current_ht_config.smps) {
1087 case IEEE80211_SMPS_STATIC:
1088 case IEEE80211_SMPS_DYNAMIC:
1089 return IL_NUM_IDLE_CHAINS_SINGLE;
1090 case IEEE80211_SMPS_OFF:
1091 return active_cnt;
1092 default:
1093 WARN(1, "invalid SMPS mode %d",
1094 il->current_ht_config.smps);
1095 return active_cnt;
1096 }
1097}
1098
1099/* up to 4 chains */
1100static u8 il4965_count_chain_bitmap(u32 chain_bitmap)
1101{
1102 u8 res;
1103 res = (chain_bitmap & BIT(0)) >> 0;
1104 res += (chain_bitmap & BIT(1)) >> 1;
1105 res += (chain_bitmap & BIT(2)) >> 2;
1106 res += (chain_bitmap & BIT(3)) >> 3;
1107 return res;
1108}
1109
1110/**
1111 * il4965_set_rxon_chain - Set up Rx chain usage in "staging" RXON image
1112 *
1113 * Selects how many and which Rx receivers/antennas/chains to use.
1114 * This should not be used for scan command ... it puts data in wrong place.
1115 */
1116void il4965_set_rxon_chain(struct il_priv *il, struct il_rxon_context *ctx)
1117{
1118 bool is_single = il4965_is_single_rx_stream(il);
1119 bool is_cam = !test_bit(STATUS_POWER_PMI, &il->status);
1120 u8 idle_rx_cnt, active_rx_cnt, valid_rx_cnt;
1121 u32 active_chains;
1122 u16 rx_chain;
1123
1124 /* Tell uCode which antennas are actually connected.
1125 * Before first association, we assume all antennas are connected.
1126 * Just after first association, il4965_chain_noise_calibration()
1127 * checks which antennas actually *are* connected. */
1128 if (il->chain_noise_data.active_chains)
1129 active_chains = il->chain_noise_data.active_chains;
1130 else
1131 active_chains = il->hw_params.valid_rx_ant;
1132
1133 rx_chain = active_chains << RXON_RX_CHAIN_VALID_POS;
1134
1135 /* How many receivers should we use? */
1136 active_rx_cnt = il4965_get_active_rx_chain_count(il);
1137 idle_rx_cnt = il4965_get_idle_rx_chain_count(il, active_rx_cnt);
1138
1139
1140 /* correct rx chain count according hw settings
1141 * and chain noise calibration
1142 */
1143 valid_rx_cnt = il4965_count_chain_bitmap(active_chains);
1144 if (valid_rx_cnt < active_rx_cnt)
1145 active_rx_cnt = valid_rx_cnt;
1146
1147 if (valid_rx_cnt < idle_rx_cnt)
1148 idle_rx_cnt = valid_rx_cnt;
1149
1150 rx_chain |= active_rx_cnt << RXON_RX_CHAIN_MIMO_CNT_POS;
1151 rx_chain |= idle_rx_cnt << RXON_RX_CHAIN_CNT_POS;
1152
1153 ctx->staging.rx_chain = cpu_to_le16(rx_chain);
1154
1155 if (!is_single && active_rx_cnt >= IL_NUM_RX_CHAINS_SINGLE && is_cam)
1156 ctx->staging.rx_chain |= RXON_RX_CHAIN_MIMO_FORCE_MSK;
1157 else
1158 ctx->staging.rx_chain &= ~RXON_RX_CHAIN_MIMO_FORCE_MSK;
1159
1160 D_ASSOC("rx_chain=0x%X active=%d idle=%d\n",
1161 ctx->staging.rx_chain,
1162 active_rx_cnt, idle_rx_cnt);
1163
1164 WARN_ON(active_rx_cnt == 0 || idle_rx_cnt == 0 ||
1165 active_rx_cnt < idle_rx_cnt);
1166}
1167
1168u8 il4965_toggle_tx_ant(struct il_priv *il, u8 ant, u8 valid)
1169{
1170 int i;
1171 u8 ind = ant;
1172
1173 for (i = 0; i < RATE_ANT_NUM - 1; i++) {
1174 ind = (ind + 1) < RATE_ANT_NUM ? ind + 1 : 0;
1175 if (valid & BIT(ind))
1176 return ind;
1177 }
1178 return ant;
1179}
1180
1181static const char *il4965_get_fh_string(int cmd)
1182{
1183 switch (cmd) {
1184 IL_CMD(FH_RSCSR_CHNL0_STTS_WPTR_REG);
1185 IL_CMD(FH_RSCSR_CHNL0_RBDCB_BASE_REG);
1186 IL_CMD(FH_RSCSR_CHNL0_WPTR);
1187 IL_CMD(FH_MEM_RCSR_CHNL0_CONFIG_REG);
1188 IL_CMD(FH_MEM_RSSR_SHARED_CTRL_REG);
1189 IL_CMD(FH_MEM_RSSR_RX_STATUS_REG);
1190 IL_CMD(FH_MEM_RSSR_RX_ENABLE_ERR_IRQ2DRV);
1191 IL_CMD(FH_TSSR_TX_STATUS_REG);
1192 IL_CMD(FH_TSSR_TX_ERROR_REG);
1193 default:
1194 return "UNKNOWN";
1195 }
1196}
1197
1198int il4965_dump_fh(struct il_priv *il, char **buf, bool display)
1199{
1200 int i;
1201#ifdef CONFIG_IWLEGACY_DEBUG
1202 int pos = 0;
1203 size_t bufsz = 0;
1204#endif
1205 static const u32 fh_tbl[] = {
1206 FH_RSCSR_CHNL0_STTS_WPTR_REG,
1207 FH_RSCSR_CHNL0_RBDCB_BASE_REG,
1208 FH_RSCSR_CHNL0_WPTR,
1209 FH_MEM_RCSR_CHNL0_CONFIG_REG,
1210 FH_MEM_RSSR_SHARED_CTRL_REG,
1211 FH_MEM_RSSR_RX_STATUS_REG,
1212 FH_MEM_RSSR_RX_ENABLE_ERR_IRQ2DRV,
1213 FH_TSSR_TX_STATUS_REG,
1214 FH_TSSR_TX_ERROR_REG
1215 };
1216#ifdef CONFIG_IWLEGACY_DEBUG
1217 if (display) {
1218 bufsz = ARRAY_SIZE(fh_tbl) * 48 + 40;
1219 *buf = kmalloc(bufsz, GFP_KERNEL);
1220 if (!*buf)
1221 return -ENOMEM;
1222 pos += scnprintf(*buf + pos, bufsz - pos,
1223 "FH register values:\n");
1224 for (i = 0; i < ARRAY_SIZE(fh_tbl); i++) {
1225 pos += scnprintf(*buf + pos, bufsz - pos,
1226 " %34s: 0X%08x\n",
1227 il4965_get_fh_string(fh_tbl[i]),
1228 il_rd(il, fh_tbl[i]));
1229 }
1230 return pos;
1231 }
1232#endif
1233 IL_ERR("FH register values:\n");
1234 for (i = 0; i < ARRAY_SIZE(fh_tbl); i++) {
1235 IL_ERR(" %34s: 0X%08x\n",
1236 il4965_get_fh_string(fh_tbl[i]),
1237 il_rd(il, fh_tbl[i]));
1238 }
1239 return 0;
1240}
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001241void il4965_rx_missed_beacon_notif(struct il_priv *il,
1242 struct il_rx_buf *rxb)
1243
1244{
1245 struct il_rx_pkt *pkt = rxb_addr(rxb);
1246 struct il_missed_beacon_notif *missed_beacon;
1247
1248 missed_beacon = &pkt->u.missed_beacon;
1249 if (le32_to_cpu(missed_beacon->consecutive_missed_beacons) >
1250 il->missed_beacon_threshold) {
1251 D_CALIB(
1252 "missed bcn cnsq %d totl %d rcd %d expctd %d\n",
1253 le32_to_cpu(missed_beacon->consecutive_missed_beacons),
1254 le32_to_cpu(missed_beacon->total_missed_becons),
1255 le32_to_cpu(missed_beacon->num_recvd_beacons),
1256 le32_to_cpu(missed_beacon->num_expected_beacons));
1257 if (!test_bit(STATUS_SCANNING, &il->status))
1258 il4965_init_sensitivity(il);
1259 }
1260}
1261
1262/* Calculate noise level, based on measurements during network silence just
1263 * before arriving beacon. This measurement can be done only if we know
1264 * exactly when to expect beacons, therefore only when we're associated. */
1265static void il4965_rx_calc_noise(struct il_priv *il)
1266{
1267 struct stats_rx_non_phy *rx_info;
1268 int num_active_rx = 0;
1269 int total_silence = 0;
1270 int bcn_silence_a, bcn_silence_b, bcn_silence_c;
1271 int last_rx_noise;
1272
1273 rx_info = &(il->_4965.stats.rx.general);
1274 bcn_silence_a =
1275 le32_to_cpu(rx_info->beacon_silence_rssi_a) & IN_BAND_FILTER;
1276 bcn_silence_b =
1277 le32_to_cpu(rx_info->beacon_silence_rssi_b) & IN_BAND_FILTER;
1278 bcn_silence_c =
1279 le32_to_cpu(rx_info->beacon_silence_rssi_c) & IN_BAND_FILTER;
1280
1281 if (bcn_silence_a) {
1282 total_silence += bcn_silence_a;
1283 num_active_rx++;
1284 }
1285 if (bcn_silence_b) {
1286 total_silence += bcn_silence_b;
1287 num_active_rx++;
1288 }
1289 if (bcn_silence_c) {
1290 total_silence += bcn_silence_c;
1291 num_active_rx++;
1292 }
1293
1294 /* Average among active antennas */
1295 if (num_active_rx)
1296 last_rx_noise = (total_silence / num_active_rx) - 107;
1297 else
1298 last_rx_noise = IL_NOISE_MEAS_NOT_AVAILABLE;
1299
1300 D_CALIB("inband silence a %u, b %u, c %u, dBm %d\n",
1301 bcn_silence_a, bcn_silence_b, bcn_silence_c,
1302 last_rx_noise);
1303}
1304
1305#ifdef CONFIG_IWLEGACY_DEBUGFS
1306/*
1307 * based on the assumption of all stats counter are in DWORD
1308 * FIXME: This function is for debugging, do not deal with
1309 * the case of counters roll-over.
1310 */
1311static void il4965_accumulative_stats(struct il_priv *il,
1312 __le32 *stats)
1313{
1314 int i, size;
1315 __le32 *prev_stats;
1316 u32 *accum_stats;
1317 u32 *delta, *max_delta;
1318 struct stats_general_common *general, *accum_general;
1319 struct stats_tx *tx, *accum_tx;
1320
1321 prev_stats = (__le32 *)&il->_4965.stats;
1322 accum_stats = (u32 *)&il->_4965.accum_stats;
1323 size = sizeof(struct il_notif_stats);
1324 general = &il->_4965.stats.general.common;
1325 accum_general = &il->_4965.accum_stats.general.common;
1326 tx = &il->_4965.stats.tx;
1327 accum_tx = &il->_4965.accum_stats.tx;
1328 delta = (u32 *)&il->_4965.delta_stats;
1329 max_delta = (u32 *)&il->_4965.max_delta;
1330
1331 for (i = sizeof(__le32); i < size;
1332 i += sizeof(__le32), stats++, prev_stats++, delta++,
1333 max_delta++, accum_stats++) {
1334 if (le32_to_cpu(*stats) > le32_to_cpu(*prev_stats)) {
1335 *delta = (le32_to_cpu(*stats) -
1336 le32_to_cpu(*prev_stats));
1337 *accum_stats += *delta;
1338 if (*delta > *max_delta)
1339 *max_delta = *delta;
1340 }
1341 }
1342
1343 /* reset accumulative stats for "no-counter" type stats */
1344 accum_general->temperature = general->temperature;
1345 accum_general->ttl_timestamp = general->ttl_timestamp;
1346}
1347#endif
1348
1349#define REG_RECALIB_PERIOD (60)
1350
1351void il4965_rx_stats(struct il_priv *il,
1352 struct il_rx_buf *rxb)
1353{
1354 int change;
1355 struct il_rx_pkt *pkt = rxb_addr(rxb);
1356
1357 D_RX(
1358 "Statistics notification received (%d vs %d).\n",
1359 (int)sizeof(struct il_notif_stats),
1360 le32_to_cpu(pkt->len_n_flags) &
1361 FH_RSCSR_FRAME_SIZE_MSK);
1362
1363 change = ((il->_4965.stats.general.common.temperature !=
1364 pkt->u.stats.general.common.temperature) ||
1365 ((il->_4965.stats.flag &
1366 STATISTICS_REPLY_FLG_HT40_MODE_MSK) !=
1367 (pkt->u.stats.flag &
1368 STATISTICS_REPLY_FLG_HT40_MODE_MSK)));
1369#ifdef CONFIG_IWLEGACY_DEBUGFS
1370 il4965_accumulative_stats(il, (__le32 *)&pkt->u.stats);
1371#endif
1372
1373 /* TODO: reading some of stats is unneeded */
1374 memcpy(&il->_4965.stats, &pkt->u.stats,
1375 sizeof(il->_4965.stats));
1376
1377 set_bit(STATUS_STATISTICS, &il->status);
1378
1379 /* Reschedule the stats timer to occur in
1380 * REG_RECALIB_PERIOD seconds to ensure we get a
1381 * thermal update even if the uCode doesn't give
1382 * us one */
1383 mod_timer(&il->stats_periodic, jiffies +
1384 msecs_to_jiffies(REG_RECALIB_PERIOD * 1000));
1385
1386 if (unlikely(!test_bit(STATUS_SCANNING, &il->status)) &&
1387 (pkt->hdr.cmd == STATISTICS_NOTIFICATION)) {
1388 il4965_rx_calc_noise(il);
1389 queue_work(il->workqueue, &il->run_time_calib_work);
1390 }
1391 if (il->cfg->ops->lib->temp_ops.temperature && change)
1392 il->cfg->ops->lib->temp_ops.temperature(il);
1393}
1394
1395void il4965_reply_stats(struct il_priv *il,
1396 struct il_rx_buf *rxb)
1397{
1398 struct il_rx_pkt *pkt = rxb_addr(rxb);
1399
1400 if (le32_to_cpu(pkt->u.stats.flag) & UCODE_STATISTICS_CLEAR_MSK) {
1401#ifdef CONFIG_IWLEGACY_DEBUGFS
1402 memset(&il->_4965.accum_stats, 0,
1403 sizeof(struct il_notif_stats));
1404 memset(&il->_4965.delta_stats, 0,
1405 sizeof(struct il_notif_stats));
1406 memset(&il->_4965.max_delta, 0,
1407 sizeof(struct il_notif_stats));
1408#endif
1409 D_RX("Statistics have been cleared\n");
1410 }
1411 il4965_rx_stats(il, rxb);
1412}
1413
Stanislaw Gruszka8f29b452011-11-15 12:57:25 +01001414
1415/*
1416 * mac80211 queues, ACs, hardware queues, FIFOs.
1417 *
1418 * Cf. http://wireless.kernel.org/en/developers/Documentation/mac80211/queues
1419 *
1420 * Mac80211 uses the following numbers, which we get as from it
1421 * by way of skb_get_queue_mapping(skb):
1422 *
1423 * VO 0
1424 * VI 1
1425 * BE 2
1426 * BK 3
1427 *
1428 *
1429 * Regular (not A-MPDU) frames are put into hardware queues corresponding
1430 * to the FIFOs, see comments in iwl-prph.h. Aggregated frames get their
1431 * own queue per aggregation session (RA/TID combination), such queues are
1432 * set up to map into FIFOs too, for which we need an AC->FIFO mapping. In
1433 * order to map frames to the right queue, we also need an AC->hw queue
1434 * mapping. This is implemented here.
1435 *
1436 * Due to the way hw queues are set up (by the hw specific modules like
1437 * iwl-4965.c), the AC->hw queue mapping is the identity
1438 * mapping.
1439 */
1440
Stanislaw Gruszkaa1751b22011-11-15 12:50:37 +01001441static const u8 tid_to_ac[] = {
1442 IEEE80211_AC_BE,
1443 IEEE80211_AC_BK,
1444 IEEE80211_AC_BK,
1445 IEEE80211_AC_BE,
1446 IEEE80211_AC_VI,
1447 IEEE80211_AC_VI,
1448 IEEE80211_AC_VO,
1449 IEEE80211_AC_VO
1450};
1451
1452static inline int il4965_get_ac_from_tid(u16 tid)
1453{
1454 if (likely(tid < ARRAY_SIZE(tid_to_ac)))
1455 return tid_to_ac[tid];
1456
1457 /* no support for TIDs 8-15 yet */
1458 return -EINVAL;
1459}
1460
1461static inline int
1462il4965_get_fifo_from_tid(struct il_rxon_context *ctx, u16 tid)
1463{
1464 if (likely(tid < ARRAY_SIZE(tid_to_ac)))
1465 return ctx->ac_to_fifo[tid_to_ac[tid]];
1466
1467 /* no support for TIDs 8-15 yet */
1468 return -EINVAL;
1469}
1470
1471/*
1472 * handle build REPLY_TX command notification.
1473 */
1474static void il4965_tx_cmd_build_basic(struct il_priv *il,
1475 struct sk_buff *skb,
1476 struct il_tx_cmd *tx_cmd,
1477 struct ieee80211_tx_info *info,
1478 struct ieee80211_hdr *hdr,
1479 u8 std_id)
1480{
1481 __le16 fc = hdr->frame_control;
1482 __le32 tx_flags = tx_cmd->tx_flags;
1483
1484 tx_cmd->stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
1485 if (!(info->flags & IEEE80211_TX_CTL_NO_ACK)) {
1486 tx_flags |= TX_CMD_FLG_ACK_MSK;
1487 if (ieee80211_is_mgmt(fc))
1488 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
1489 if (ieee80211_is_probe_resp(fc) &&
1490 !(le16_to_cpu(hdr->seq_ctrl) & 0xf))
1491 tx_flags |= TX_CMD_FLG_TSF_MSK;
1492 } else {
1493 tx_flags &= (~TX_CMD_FLG_ACK_MSK);
1494 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
1495 }
1496
1497 if (ieee80211_is_back_req(fc))
1498 tx_flags |= TX_CMD_FLG_ACK_MSK | TX_CMD_FLG_IMM_BA_RSP_MASK;
1499
1500 tx_cmd->sta_id = std_id;
1501 if (ieee80211_has_morefrags(fc))
1502 tx_flags |= TX_CMD_FLG_MORE_FRAG_MSK;
1503
1504 if (ieee80211_is_data_qos(fc)) {
1505 u8 *qc = ieee80211_get_qos_ctl(hdr);
1506 tx_cmd->tid_tspec = qc[0] & 0xf;
1507 tx_flags &= ~TX_CMD_FLG_SEQ_CTL_MSK;
1508 } else {
1509 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
1510 }
1511
1512 il_tx_cmd_protection(il, info, fc, &tx_flags);
1513
1514 tx_flags &= ~(TX_CMD_FLG_ANT_SEL_MSK);
1515 if (ieee80211_is_mgmt(fc)) {
1516 if (ieee80211_is_assoc_req(fc) || ieee80211_is_reassoc_req(fc))
1517 tx_cmd->timeout.pm_frame_timeout = cpu_to_le16(3);
1518 else
1519 tx_cmd->timeout.pm_frame_timeout = cpu_to_le16(2);
1520 } else {
1521 tx_cmd->timeout.pm_frame_timeout = 0;
1522 }
1523
1524 tx_cmd->driver_txop = 0;
1525 tx_cmd->tx_flags = tx_flags;
1526 tx_cmd->next_frame_len = 0;
1527}
1528
1529#define RTS_DFAULT_RETRY_LIMIT 60
1530
1531static void il4965_tx_cmd_build_rate(struct il_priv *il,
1532 struct il_tx_cmd *tx_cmd,
1533 struct ieee80211_tx_info *info,
1534 __le16 fc)
1535{
1536 u32 rate_flags;
1537 int rate_idx;
1538 u8 rts_retry_limit;
1539 u8 data_retry_limit;
1540 u8 rate_plcp;
1541
1542 /* Set retry limit on DATA packets and Probe Responses*/
1543 if (ieee80211_is_probe_resp(fc))
1544 data_retry_limit = 3;
1545 else
1546 data_retry_limit = IL4965_DEFAULT_TX_RETRY;
1547 tx_cmd->data_retry_limit = data_retry_limit;
1548
1549 /* Set retry limit on RTS packets */
1550 rts_retry_limit = RTS_DFAULT_RETRY_LIMIT;
1551 if (data_retry_limit < rts_retry_limit)
1552 rts_retry_limit = data_retry_limit;
1553 tx_cmd->rts_retry_limit = rts_retry_limit;
1554
1555 /* DATA packets will use the uCode station table for rate/antenna
1556 * selection */
1557 if (ieee80211_is_data(fc)) {
1558 tx_cmd->initial_rate_idx = 0;
1559 tx_cmd->tx_flags |= TX_CMD_FLG_STA_RATE_MSK;
1560 return;
1561 }
1562
1563 /**
1564 * If the current TX rate stored in mac80211 has the MCS bit set, it's
1565 * not really a TX rate. Thus, we use the lowest supported rate for
1566 * this band. Also use the lowest supported rate if the stored rate
1567 * idx is invalid.
1568 */
1569 rate_idx = info->control.rates[0].idx;
1570 if ((info->control.rates[0].flags & IEEE80211_TX_RC_MCS) ||
1571 rate_idx < 0 || rate_idx > RATE_COUNT_LEGACY)
1572 rate_idx = rate_lowest_index(&il->bands[info->band],
1573 info->control.sta);
1574 /* For 5 GHZ band, remap mac80211 rate indices into driver indices */
1575 if (info->band == IEEE80211_BAND_5GHZ)
1576 rate_idx += IL_FIRST_OFDM_RATE;
1577 /* Get PLCP rate for tx_cmd->rate_n_flags */
1578 rate_plcp = il_rates[rate_idx].plcp;
1579 /* Zero out flags for this packet */
1580 rate_flags = 0;
1581
1582 /* Set CCK flag as needed */
1583 if (rate_idx >= IL_FIRST_CCK_RATE && rate_idx <= IL_LAST_CCK_RATE)
1584 rate_flags |= RATE_MCS_CCK_MSK;
1585
1586 /* Set up antennas */
1587 il->mgmt_tx_ant = il4965_toggle_tx_ant(il, il->mgmt_tx_ant,
1588 il->hw_params.valid_tx_ant);
1589
1590 rate_flags |= il4965_ant_idx_to_flags(il->mgmt_tx_ant);
1591
1592 /* Set the rate in the TX cmd */
1593 tx_cmd->rate_n_flags = il4965_hw_set_rate_n_flags(rate_plcp, rate_flags);
1594}
1595
1596static void il4965_tx_cmd_build_hwcrypto(struct il_priv *il,
1597 struct ieee80211_tx_info *info,
1598 struct il_tx_cmd *tx_cmd,
1599 struct sk_buff *skb_frag,
1600 int sta_id)
1601{
1602 struct ieee80211_key_conf *keyconf = info->control.hw_key;
1603
1604 switch (keyconf->cipher) {
1605 case WLAN_CIPHER_SUITE_CCMP:
1606 tx_cmd->sec_ctl = TX_CMD_SEC_CCM;
1607 memcpy(tx_cmd->key, keyconf->key, keyconf->keylen);
1608 if (info->flags & IEEE80211_TX_CTL_AMPDU)
1609 tx_cmd->tx_flags |= TX_CMD_FLG_AGG_CCMP_MSK;
1610 D_TX("tx_cmd with AES hwcrypto\n");
1611 break;
1612
1613 case WLAN_CIPHER_SUITE_TKIP:
1614 tx_cmd->sec_ctl = TX_CMD_SEC_TKIP;
1615 ieee80211_get_tkip_p2k(keyconf, skb_frag, tx_cmd->key);
1616 D_TX("tx_cmd with tkip hwcrypto\n");
1617 break;
1618
1619 case WLAN_CIPHER_SUITE_WEP104:
1620 tx_cmd->sec_ctl |= TX_CMD_SEC_KEY128;
1621 /* fall through */
1622 case WLAN_CIPHER_SUITE_WEP40:
1623 tx_cmd->sec_ctl |= (TX_CMD_SEC_WEP |
1624 (keyconf->keyidx & TX_CMD_SEC_MSK) << TX_CMD_SEC_SHIFT);
1625
1626 memcpy(&tx_cmd->key[3], keyconf->key, keyconf->keylen);
1627
1628 D_TX("Configuring packet for WEP encryption "
1629 "with key %d\n", keyconf->keyidx);
1630 break;
1631
1632 default:
1633 IL_ERR("Unknown encode cipher %x\n", keyconf->cipher);
1634 break;
1635 }
1636}
1637
1638/*
1639 * start REPLY_TX command process
1640 */
1641int il4965_tx_skb(struct il_priv *il, struct sk_buff *skb)
1642{
1643 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1644 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1645 struct ieee80211_sta *sta = info->control.sta;
1646 struct il_station_priv *sta_priv = NULL;
1647 struct il_tx_queue *txq;
1648 struct il_queue *q;
1649 struct il_device_cmd *out_cmd;
1650 struct il_cmd_meta *out_meta;
1651 struct il_tx_cmd *tx_cmd;
1652 struct il_rxon_context *ctx = &il->ctx;
1653 int txq_id;
1654 dma_addr_t phys_addr;
1655 dma_addr_t txcmd_phys;
1656 dma_addr_t scratch_phys;
1657 u16 len, firstlen, secondlen;
1658 u16 seq_number = 0;
1659 __le16 fc;
1660 u8 hdr_len;
1661 u8 sta_id;
1662 u8 wait_write_ptr = 0;
1663 u8 tid = 0;
1664 u8 *qc = NULL;
1665 unsigned long flags;
1666 bool is_agg = false;
1667
1668 if (info->control.vif)
1669 ctx = il_rxon_ctx_from_vif(info->control.vif);
1670
1671 spin_lock_irqsave(&il->lock, flags);
1672 if (il_is_rfkill(il)) {
1673 D_DROP("Dropping - RF KILL\n");
1674 goto drop_unlock;
1675 }
1676
1677 fc = hdr->frame_control;
1678
1679#ifdef CONFIG_IWLEGACY_DEBUG
1680 if (ieee80211_is_auth(fc))
1681 D_TX("Sending AUTH frame\n");
1682 else if (ieee80211_is_assoc_req(fc))
1683 D_TX("Sending ASSOC frame\n");
1684 else if (ieee80211_is_reassoc_req(fc))
1685 D_TX("Sending REASSOC frame\n");
1686#endif
1687
1688 hdr_len = ieee80211_hdrlen(fc);
1689
1690 /* For management frames use broadcast id to do not break aggregation */
1691 if (!ieee80211_is_data(fc))
1692 sta_id = ctx->bcast_sta_id;
1693 else {
1694 /* Find idx into station table for destination station */
1695 sta_id = il_sta_id_or_broadcast(il, ctx, info->control.sta);
1696
1697 if (sta_id == IL_INVALID_STATION) {
1698 D_DROP("Dropping - INVALID STATION: %pM\n",
1699 hdr->addr1);
1700 goto drop_unlock;
1701 }
1702 }
1703
1704 D_TX("station Id %d\n", sta_id);
1705
1706 if (sta)
1707 sta_priv = (void *)sta->drv_priv;
1708
1709 if (sta_priv && sta_priv->asleep &&
1710 (info->flags & IEEE80211_TX_CTL_POLL_RESPONSE)) {
1711 /*
1712 * This sends an asynchronous command to the device,
1713 * but we can rely on it being processed before the
1714 * next frame is processed -- and the next frame to
1715 * this station is the one that will consume this
1716 * counter.
1717 * For now set the counter to just 1 since we do not
1718 * support uAPSD yet.
1719 */
1720 il4965_sta_modify_sleep_tx_count(il, sta_id, 1);
1721 }
1722
1723 /*
1724 * Send this frame after DTIM -- there's a special queue
1725 * reserved for this for contexts that support AP mode.
1726 */
1727 if (info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM) {
1728 txq_id = ctx->mcast_queue;
1729 /*
1730 * The microcode will clear the more data
1731 * bit in the last frame it transmits.
1732 */
1733 hdr->frame_control |=
1734 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1735 } else
1736 txq_id = ctx->ac_to_queue[skb_get_queue_mapping(skb)];
1737
1738 /* irqs already disabled/saved above when locking il->lock */
1739 spin_lock(&il->sta_lock);
1740
1741 if (ieee80211_is_data_qos(fc)) {
1742 qc = ieee80211_get_qos_ctl(hdr);
1743 tid = qc[0] & IEEE80211_QOS_CTL_TID_MASK;
1744 if (WARN_ON_ONCE(tid >= MAX_TID_COUNT)) {
1745 spin_unlock(&il->sta_lock);
1746 goto drop_unlock;
1747 }
1748 seq_number = il->stations[sta_id].tid[tid].seq_number;
1749 seq_number &= IEEE80211_SCTL_SEQ;
1750 hdr->seq_ctrl = hdr->seq_ctrl &
1751 cpu_to_le16(IEEE80211_SCTL_FRAG);
1752 hdr->seq_ctrl |= cpu_to_le16(seq_number);
1753 seq_number += 0x10;
1754 /* aggregation is on for this <sta,tid> */
1755 if (info->flags & IEEE80211_TX_CTL_AMPDU &&
1756 il->stations[sta_id].tid[tid].agg.state == IL_AGG_ON) {
1757 txq_id = il->stations[sta_id].tid[tid].agg.txq_id;
1758 is_agg = true;
1759 }
1760 }
1761
1762 txq = &il->txq[txq_id];
1763 q = &txq->q;
1764
1765 if (unlikely(il_queue_space(q) < q->high_mark)) {
1766 spin_unlock(&il->sta_lock);
1767 goto drop_unlock;
1768 }
1769
1770 if (ieee80211_is_data_qos(fc)) {
1771 il->stations[sta_id].tid[tid].tfds_in_queue++;
1772 if (!ieee80211_has_morefrags(fc))
1773 il->stations[sta_id].tid[tid].seq_number = seq_number;
1774 }
1775
1776 spin_unlock(&il->sta_lock);
1777
1778 /* Set up driver data for this TFD */
1779 memset(&(txq->txb[q->write_ptr]), 0, sizeof(struct il_tx_info));
1780 txq->txb[q->write_ptr].skb = skb;
1781 txq->txb[q->write_ptr].ctx = ctx;
1782
1783 /* Set up first empty entry in queue's array of Tx/cmd buffers */
1784 out_cmd = txq->cmd[q->write_ptr];
1785 out_meta = &txq->meta[q->write_ptr];
1786 tx_cmd = &out_cmd->cmd.tx;
1787 memset(&out_cmd->hdr, 0, sizeof(out_cmd->hdr));
1788 memset(tx_cmd, 0, sizeof(struct il_tx_cmd));
1789
1790 /*
1791 * Set up the Tx-command (not MAC!) header.
1792 * Store the chosen Tx queue and TFD idx within the sequence field;
1793 * after Tx, uCode's Tx response will return this value so driver can
1794 * locate the frame within the tx queue and do post-tx processing.
1795 */
1796 out_cmd->hdr.cmd = REPLY_TX;
1797 out_cmd->hdr.sequence = cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
1798 IDX_TO_SEQ(q->write_ptr)));
1799
1800 /* Copy MAC header from skb into command buffer */
1801 memcpy(tx_cmd->hdr, hdr, hdr_len);
1802
1803
1804 /* Total # bytes to be transmitted */
1805 len = (u16)skb->len;
1806 tx_cmd->len = cpu_to_le16(len);
1807
1808 if (info->control.hw_key)
1809 il4965_tx_cmd_build_hwcrypto(il, info, tx_cmd, skb, sta_id);
1810
1811 /* TODO need this for burst mode later on */
1812 il4965_tx_cmd_build_basic(il, skb, tx_cmd, info, hdr, sta_id);
1813 il_dbg_log_tx_data_frame(il, len, hdr);
1814
1815 il4965_tx_cmd_build_rate(il, tx_cmd, info, fc);
1816
1817 il_update_stats(il, true, fc, len);
1818 /*
1819 * Use the first empty entry in this queue's command buffer array
1820 * to contain the Tx command and MAC header concatenated together
1821 * (payload data will be in another buffer).
1822 * Size of this varies, due to varying MAC header length.
1823 * If end is not dword aligned, we'll have 2 extra bytes at the end
1824 * of the MAC header (device reads on dword boundaries).
1825 * We'll tell device about this padding later.
1826 */
1827 len = sizeof(struct il_tx_cmd) +
1828 sizeof(struct il_cmd_header) + hdr_len;
1829 firstlen = (len + 3) & ~3;
1830
1831 /* Tell NIC about any 2-byte padding after MAC header */
1832 if (firstlen != len)
1833 tx_cmd->tx_flags |= TX_CMD_FLG_MH_PAD_MSK;
1834
1835 /* Physical address of this Tx command's header (not MAC header!),
1836 * within command buffer array. */
1837 txcmd_phys = pci_map_single(il->pci_dev,
1838 &out_cmd->hdr, firstlen,
1839 PCI_DMA_BIDIRECTIONAL);
1840 dma_unmap_addr_set(out_meta, mapping, txcmd_phys);
1841 dma_unmap_len_set(out_meta, len, firstlen);
1842 /* Add buffer containing Tx command and MAC(!) header to TFD's
1843 * first entry */
1844 il->cfg->ops->lib->txq_attach_buf_to_tfd(il, txq,
1845 txcmd_phys, firstlen, 1, 0);
1846
1847 if (!ieee80211_has_morefrags(hdr->frame_control)) {
1848 txq->need_update = 1;
1849 } else {
1850 wait_write_ptr = 1;
1851 txq->need_update = 0;
1852 }
1853
1854 /* Set up TFD's 2nd entry to point directly to remainder of skb,
1855 * if any (802.11 null frames have no payload). */
1856 secondlen = skb->len - hdr_len;
1857 if (secondlen > 0) {
1858 phys_addr = pci_map_single(il->pci_dev, skb->data + hdr_len,
1859 secondlen, PCI_DMA_TODEVICE);
1860 il->cfg->ops->lib->txq_attach_buf_to_tfd(il, txq,
1861 phys_addr, secondlen,
1862 0, 0);
1863 }
1864
1865 scratch_phys = txcmd_phys + sizeof(struct il_cmd_header) +
1866 offsetof(struct il_tx_cmd, scratch);
1867
1868 /* take back ownership of DMA buffer to enable update */
1869 pci_dma_sync_single_for_cpu(il->pci_dev, txcmd_phys,
1870 firstlen, PCI_DMA_BIDIRECTIONAL);
1871 tx_cmd->dram_lsb_ptr = cpu_to_le32(scratch_phys);
1872 tx_cmd->dram_msb_ptr = il_get_dma_hi_addr(scratch_phys);
1873
1874 D_TX("sequence nr = 0X%x\n",
1875 le16_to_cpu(out_cmd->hdr.sequence));
1876 D_TX("tx_flags = 0X%x\n", le32_to_cpu(tx_cmd->tx_flags));
1877 il_print_hex_dump(il, IL_DL_TX, (u8 *)tx_cmd, sizeof(*tx_cmd));
1878 il_print_hex_dump(il, IL_DL_TX, (u8 *)tx_cmd->hdr, hdr_len);
1879
1880 /* Set up entry for this TFD in Tx byte-count array */
1881 if (info->flags & IEEE80211_TX_CTL_AMPDU)
1882 il->cfg->ops->lib->txq_update_byte_cnt_tbl(il, txq,
1883 le16_to_cpu(tx_cmd->len));
1884
1885 pci_dma_sync_single_for_device(il->pci_dev, txcmd_phys,
1886 firstlen, PCI_DMA_BIDIRECTIONAL);
1887
1888 /* Tell device the write idx *just past* this latest filled TFD */
1889 q->write_ptr = il_queue_inc_wrap(q->write_ptr, q->n_bd);
1890 il_txq_update_write_ptr(il, txq);
1891 spin_unlock_irqrestore(&il->lock, flags);
1892
1893 /*
1894 * At this point the frame is "transmitted" successfully
1895 * and we will get a TX status notification eventually,
1896 * regardless of the value of ret. "ret" only indicates
1897 * whether or not we should update the write pointer.
1898 */
1899
1900 /*
1901 * Avoid atomic ops if it isn't an associated client.
1902 * Also, if this is a packet for aggregation, don't
1903 * increase the counter because the ucode will stop
1904 * aggregation queues when their respective station
1905 * goes to sleep.
1906 */
1907 if (sta_priv && sta_priv->client && !is_agg)
1908 atomic_inc(&sta_priv->pending_frames);
1909
1910 if (il_queue_space(q) < q->high_mark && il->mac80211_registered) {
1911 if (wait_write_ptr) {
1912 spin_lock_irqsave(&il->lock, flags);
1913 txq->need_update = 1;
1914 il_txq_update_write_ptr(il, txq);
1915 spin_unlock_irqrestore(&il->lock, flags);
1916 } else {
1917 il_stop_queue(il, txq);
1918 }
1919 }
1920
1921 return 0;
1922
1923drop_unlock:
1924 spin_unlock_irqrestore(&il->lock, flags);
1925 return -1;
1926}
1927
1928static inline int il4965_alloc_dma_ptr(struct il_priv *il,
1929 struct il_dma_ptr *ptr, size_t size)
1930{
1931 ptr->addr = dma_alloc_coherent(&il->pci_dev->dev, size, &ptr->dma,
1932 GFP_KERNEL);
1933 if (!ptr->addr)
1934 return -ENOMEM;
1935 ptr->size = size;
1936 return 0;
1937}
1938
1939static inline void il4965_free_dma_ptr(struct il_priv *il,
1940 struct il_dma_ptr *ptr)
1941{
1942 if (unlikely(!ptr->addr))
1943 return;
1944
1945 dma_free_coherent(&il->pci_dev->dev, ptr->size, ptr->addr, ptr->dma);
1946 memset(ptr, 0, sizeof(*ptr));
1947}
1948
1949/**
1950 * il4965_hw_txq_ctx_free - Free TXQ Context
1951 *
1952 * Destroy all TX DMA queues and structures
1953 */
1954void il4965_hw_txq_ctx_free(struct il_priv *il)
1955{
1956 int txq_id;
1957
1958 /* Tx queues */
1959 if (il->txq) {
1960 for (txq_id = 0; txq_id < il->hw_params.max_txq_num; txq_id++)
1961 if (txq_id == il->cmd_queue)
1962 il_cmd_queue_free(il);
1963 else
1964 il_tx_queue_free(il, txq_id);
1965 }
1966 il4965_free_dma_ptr(il, &il->kw);
1967
1968 il4965_free_dma_ptr(il, &il->scd_bc_tbls);
1969
1970 /* free tx queue structure */
1971 il_txq_mem(il);
1972}
1973
1974/**
1975 * il4965_txq_ctx_alloc - allocate TX queue context
1976 * Allocate all Tx DMA structures and initialize them
1977 *
1978 * @param il
1979 * @return error code
1980 */
1981int il4965_txq_ctx_alloc(struct il_priv *il)
1982{
1983 int ret;
1984 int txq_id, slots_num;
1985 unsigned long flags;
1986
1987 /* Free all tx/cmd queues and keep-warm buffer */
1988 il4965_hw_txq_ctx_free(il);
1989
1990 ret = il4965_alloc_dma_ptr(il, &il->scd_bc_tbls,
1991 il->hw_params.scd_bc_tbls_size);
1992 if (ret) {
1993 IL_ERR("Scheduler BC Table allocation failed\n");
1994 goto error_bc_tbls;
1995 }
1996 /* Alloc keep-warm buffer */
1997 ret = il4965_alloc_dma_ptr(il, &il->kw, IL_KW_SIZE);
1998 if (ret) {
1999 IL_ERR("Keep Warm allocation failed\n");
2000 goto error_kw;
2001 }
2002
2003 /* allocate tx queue structure */
2004 ret = il_alloc_txq_mem(il);
2005 if (ret)
2006 goto error;
2007
2008 spin_lock_irqsave(&il->lock, flags);
2009
2010 /* Turn off all Tx DMA fifos */
2011 il4965_txq_set_sched(il, 0);
2012
2013 /* Tell NIC where to find the "keep warm" buffer */
2014 il_wr(il, FH_KW_MEM_ADDR_REG, il->kw.dma >> 4);
2015
2016 spin_unlock_irqrestore(&il->lock, flags);
2017
2018 /* Alloc and init all Tx queues, including the command queue (#4/#9) */
2019 for (txq_id = 0; txq_id < il->hw_params.max_txq_num; txq_id++) {
2020 slots_num = (txq_id == il->cmd_queue) ?
2021 TFD_CMD_SLOTS : TFD_TX_CMD_SLOTS;
2022 ret = il_tx_queue_init(il,
2023 &il->txq[txq_id], slots_num,
2024 txq_id);
2025 if (ret) {
2026 IL_ERR("Tx %d queue init failed\n", txq_id);
2027 goto error;
2028 }
2029 }
2030
2031 return ret;
2032
2033 error:
2034 il4965_hw_txq_ctx_free(il);
2035 il4965_free_dma_ptr(il, &il->kw);
2036 error_kw:
2037 il4965_free_dma_ptr(il, &il->scd_bc_tbls);
2038 error_bc_tbls:
2039 return ret;
2040}
2041
2042void il4965_txq_ctx_reset(struct il_priv *il)
2043{
2044 int txq_id, slots_num;
2045 unsigned long flags;
2046
2047 spin_lock_irqsave(&il->lock, flags);
2048
2049 /* Turn off all Tx DMA fifos */
2050 il4965_txq_set_sched(il, 0);
2051
2052 /* Tell NIC where to find the "keep warm" buffer */
2053 il_wr(il, FH_KW_MEM_ADDR_REG, il->kw.dma >> 4);
2054
2055 spin_unlock_irqrestore(&il->lock, flags);
2056
2057 /* Alloc and init all Tx queues, including the command queue (#4) */
2058 for (txq_id = 0; txq_id < il->hw_params.max_txq_num; txq_id++) {
2059 slots_num = txq_id == il->cmd_queue ?
2060 TFD_CMD_SLOTS : TFD_TX_CMD_SLOTS;
2061 il_tx_queue_reset(il, &il->txq[txq_id],
2062 slots_num, txq_id);
2063 }
2064}
2065
2066/**
2067 * il4965_txq_ctx_stop - Stop all Tx DMA channels
2068 */
2069void il4965_txq_ctx_stop(struct il_priv *il)
2070{
2071 int ch, txq_id;
2072 unsigned long flags;
2073
2074 /* Turn off all Tx DMA fifos */
2075 spin_lock_irqsave(&il->lock, flags);
2076
2077 il4965_txq_set_sched(il, 0);
2078
2079 /* Stop each Tx DMA channel, and wait for it to be idle */
2080 for (ch = 0; ch < il->hw_params.dma_chnl_num; ch++) {
2081 il_wr(il,
2082 FH_TCSR_CHNL_TX_CONFIG_REG(ch), 0x0);
2083 if (il_poll_bit(il, FH_TSSR_TX_STATUS_REG,
2084 FH_TSSR_TX_STATUS_REG_MSK_CHNL_IDLE(ch),
2085 1000))
2086 IL_ERR("Failing on timeout while stopping"
2087 " DMA channel %d [0x%08x]", ch,
2088 il_rd(il,
2089 FH_TSSR_TX_STATUS_REG));
2090 }
2091 spin_unlock_irqrestore(&il->lock, flags);
2092
2093 if (!il->txq)
2094 return;
2095
2096 /* Unmap DMA from host system and free skb's */
2097 for (txq_id = 0; txq_id < il->hw_params.max_txq_num; txq_id++)
2098 if (txq_id == il->cmd_queue)
2099 il_cmd_queue_unmap(il);
2100 else
2101 il_tx_queue_unmap(il, txq_id);
2102}
2103
2104/*
2105 * Find first available (lowest unused) Tx Queue, mark it "active".
2106 * Called only when finding queue for aggregation.
2107 * Should never return anything < 7, because they should already
2108 * be in use as EDCA AC (0-3), Command (4), reserved (5, 6)
2109 */
2110static int il4965_txq_ctx_activate_free(struct il_priv *il)
2111{
2112 int txq_id;
2113
2114 for (txq_id = 0; txq_id < il->hw_params.max_txq_num; txq_id++)
2115 if (!test_and_set_bit(txq_id, &il->txq_ctx_active_msk))
2116 return txq_id;
2117 return -1;
2118}
2119
2120/**
2121 * il4965_tx_queue_stop_scheduler - Stop queue, but keep configuration
2122 */
2123static void il4965_tx_queue_stop_scheduler(struct il_priv *il,
2124 u16 txq_id)
2125{
2126 /* Simply stop the queue, but don't change any configuration;
2127 * the SCD_ACT_EN bit is the write-enable mask for the ACTIVE bit. */
2128 il_wr_prph(il,
2129 IL49_SCD_QUEUE_STATUS_BITS(txq_id),
2130 (0 << IL49_SCD_QUEUE_STTS_REG_POS_ACTIVE)|
2131 (1 << IL49_SCD_QUEUE_STTS_REG_POS_SCD_ACT_EN));
2132}
2133
2134/**
2135 * il4965_tx_queue_set_q2ratid - Map unique receiver/tid combination to a queue
2136 */
2137static int il4965_tx_queue_set_q2ratid(struct il_priv *il, u16 ra_tid,
2138 u16 txq_id)
2139{
2140 u32 tbl_dw_addr;
2141 u32 tbl_dw;
2142 u16 scd_q2ratid;
2143
2144 scd_q2ratid = ra_tid & IL_SCD_QUEUE_RA_TID_MAP_RATID_MSK;
2145
2146 tbl_dw_addr = il->scd_base_addr +
2147 IL49_SCD_TRANSLATE_TBL_OFFSET_QUEUE(txq_id);
2148
2149 tbl_dw = il_read_targ_mem(il, tbl_dw_addr);
2150
2151 if (txq_id & 0x1)
2152 tbl_dw = (scd_q2ratid << 16) | (tbl_dw & 0x0000FFFF);
2153 else
2154 tbl_dw = scd_q2ratid | (tbl_dw & 0xFFFF0000);
2155
2156 il_write_targ_mem(il, tbl_dw_addr, tbl_dw);
2157
2158 return 0;
2159}
2160
2161/**
2162 * il4965_tx_queue_agg_enable - Set up & enable aggregation for selected queue
2163 *
2164 * NOTE: txq_id must be greater than IL49_FIRST_AMPDU_QUEUE,
2165 * i.e. it must be one of the higher queues used for aggregation
2166 */
2167static int il4965_txq_agg_enable(struct il_priv *il, int txq_id,
2168 int tx_fifo, int sta_id, int tid, u16 ssn_idx)
2169{
2170 unsigned long flags;
2171 u16 ra_tid;
2172 int ret;
2173
2174 if ((IL49_FIRST_AMPDU_QUEUE > txq_id) ||
2175 (IL49_FIRST_AMPDU_QUEUE +
2176 il->cfg->base_params->num_of_ampdu_queues <= txq_id)) {
2177 IL_WARN(
2178 "queue number out of range: %d, must be %d to %d\n",
2179 txq_id, IL49_FIRST_AMPDU_QUEUE,
2180 IL49_FIRST_AMPDU_QUEUE +
2181 il->cfg->base_params->num_of_ampdu_queues - 1);
2182 return -EINVAL;
2183 }
2184
2185 ra_tid = BUILD_RAxTID(sta_id, tid);
2186
2187 /* Modify device's station table to Tx this TID */
2188 ret = il4965_sta_tx_modify_enable_tid(il, sta_id, tid);
2189 if (ret)
2190 return ret;
2191
2192 spin_lock_irqsave(&il->lock, flags);
2193
2194 /* Stop this Tx queue before configuring it */
2195 il4965_tx_queue_stop_scheduler(il, txq_id);
2196
2197 /* Map receiver-address / traffic-ID to this queue */
2198 il4965_tx_queue_set_q2ratid(il, ra_tid, txq_id);
2199
2200 /* Set this queue as a chain-building queue */
2201 il_set_bits_prph(il, IL49_SCD_QUEUECHAIN_SEL, (1 << txq_id));
2202
2203 /* Place first TFD at idx corresponding to start sequence number.
2204 * Assumes that ssn_idx is valid (!= 0xFFF) */
2205 il->txq[txq_id].q.read_ptr = (ssn_idx & 0xff);
2206 il->txq[txq_id].q.write_ptr = (ssn_idx & 0xff);
2207 il4965_set_wr_ptrs(il, txq_id, ssn_idx);
2208
2209 /* Set up Tx win size and frame limit for this queue */
2210 il_write_targ_mem(il,
2211 il->scd_base_addr + IL49_SCD_CONTEXT_QUEUE_OFFSET(txq_id),
2212 (SCD_WIN_SIZE << IL49_SCD_QUEUE_CTX_REG1_WIN_SIZE_POS) &
2213 IL49_SCD_QUEUE_CTX_REG1_WIN_SIZE_MSK);
2214
2215 il_write_targ_mem(il, il->scd_base_addr +
2216 IL49_SCD_CONTEXT_QUEUE_OFFSET(txq_id) + sizeof(u32),
2217 (SCD_FRAME_LIMIT << IL49_SCD_QUEUE_CTX_REG2_FRAME_LIMIT_POS)
2218 & IL49_SCD_QUEUE_CTX_REG2_FRAME_LIMIT_MSK);
2219
2220 il_set_bits_prph(il, IL49_SCD_INTERRUPT_MASK, (1 << txq_id));
2221
2222 /* Set up Status area in SRAM, map to Tx DMA/FIFO, activate the queue */
2223 il4965_tx_queue_set_status(il, &il->txq[txq_id], tx_fifo, 1);
2224
2225 spin_unlock_irqrestore(&il->lock, flags);
2226
2227 return 0;
2228}
2229
2230
2231int il4965_tx_agg_start(struct il_priv *il, struct ieee80211_vif *vif,
2232 struct ieee80211_sta *sta, u16 tid, u16 *ssn)
2233{
2234 int sta_id;
2235 int tx_fifo;
2236 int txq_id;
2237 int ret;
2238 unsigned long flags;
2239 struct il_tid_data *tid_data;
2240
2241 tx_fifo = il4965_get_fifo_from_tid(il_rxon_ctx_from_vif(vif), tid);
2242 if (unlikely(tx_fifo < 0))
2243 return tx_fifo;
2244
2245 IL_WARN("%s on ra = %pM tid = %d\n",
2246 __func__, sta->addr, tid);
2247
2248 sta_id = il_sta_id(sta);
2249 if (sta_id == IL_INVALID_STATION) {
2250 IL_ERR("Start AGG on invalid station\n");
2251 return -ENXIO;
2252 }
2253 if (unlikely(tid >= MAX_TID_COUNT))
2254 return -EINVAL;
2255
2256 if (il->stations[sta_id].tid[tid].agg.state != IL_AGG_OFF) {
2257 IL_ERR("Start AGG when state is not IL_AGG_OFF !\n");
2258 return -ENXIO;
2259 }
2260
2261 txq_id = il4965_txq_ctx_activate_free(il);
2262 if (txq_id == -1) {
2263 IL_ERR("No free aggregation queue available\n");
2264 return -ENXIO;
2265 }
2266
2267 spin_lock_irqsave(&il->sta_lock, flags);
2268 tid_data = &il->stations[sta_id].tid[tid];
2269 *ssn = SEQ_TO_SN(tid_data->seq_number);
2270 tid_data->agg.txq_id = txq_id;
2271 il_set_swq_id(&il->txq[txq_id],
2272 il4965_get_ac_from_tid(tid), txq_id);
2273 spin_unlock_irqrestore(&il->sta_lock, flags);
2274
2275 ret = il4965_txq_agg_enable(il, txq_id, tx_fifo,
2276 sta_id, tid, *ssn);
2277 if (ret)
2278 return ret;
2279
2280 spin_lock_irqsave(&il->sta_lock, flags);
2281 tid_data = &il->stations[sta_id].tid[tid];
2282 if (tid_data->tfds_in_queue == 0) {
2283 D_HT("HW queue is empty\n");
2284 tid_data->agg.state = IL_AGG_ON;
2285 ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2286 } else {
2287 D_HT(
2288 "HW queue is NOT empty: %d packets in HW queue\n",
2289 tid_data->tfds_in_queue);
2290 tid_data->agg.state = IL_EMPTYING_HW_QUEUE_ADDBA;
2291 }
2292 spin_unlock_irqrestore(&il->sta_lock, flags);
2293 return ret;
2294}
2295
2296/**
2297 * txq_id must be greater than IL49_FIRST_AMPDU_QUEUE
2298 * il->lock must be held by the caller
2299 */
2300static int il4965_txq_agg_disable(struct il_priv *il, u16 txq_id,
2301 u16 ssn_idx, u8 tx_fifo)
2302{
2303 if ((IL49_FIRST_AMPDU_QUEUE > txq_id) ||
2304 (IL49_FIRST_AMPDU_QUEUE +
2305 il->cfg->base_params->num_of_ampdu_queues <= txq_id)) {
2306 IL_WARN(
2307 "queue number out of range: %d, must be %d to %d\n",
2308 txq_id, IL49_FIRST_AMPDU_QUEUE,
2309 IL49_FIRST_AMPDU_QUEUE +
2310 il->cfg->base_params->num_of_ampdu_queues - 1);
2311 return -EINVAL;
2312 }
2313
2314 il4965_tx_queue_stop_scheduler(il, txq_id);
2315
2316 il_clear_bits_prph(il,
2317 IL49_SCD_QUEUECHAIN_SEL, (1 << txq_id));
2318
2319 il->txq[txq_id].q.read_ptr = (ssn_idx & 0xff);
2320 il->txq[txq_id].q.write_ptr = (ssn_idx & 0xff);
2321 /* supposes that ssn_idx is valid (!= 0xFFF) */
2322 il4965_set_wr_ptrs(il, txq_id, ssn_idx);
2323
2324 il_clear_bits_prph(il,
2325 IL49_SCD_INTERRUPT_MASK, (1 << txq_id));
2326 il_txq_ctx_deactivate(il, txq_id);
2327 il4965_tx_queue_set_status(il, &il->txq[txq_id], tx_fifo, 0);
2328
2329 return 0;
2330}
2331
2332int il4965_tx_agg_stop(struct il_priv *il, struct ieee80211_vif *vif,
2333 struct ieee80211_sta *sta, u16 tid)
2334{
2335 int tx_fifo_id, txq_id, sta_id, ssn;
2336 struct il_tid_data *tid_data;
2337 int write_ptr, read_ptr;
2338 unsigned long flags;
2339
2340 tx_fifo_id = il4965_get_fifo_from_tid(il_rxon_ctx_from_vif(vif), tid);
2341 if (unlikely(tx_fifo_id < 0))
2342 return tx_fifo_id;
2343
2344 sta_id = il_sta_id(sta);
2345
2346 if (sta_id == IL_INVALID_STATION) {
2347 IL_ERR("Invalid station for AGG tid %d\n", tid);
2348 return -ENXIO;
2349 }
2350
2351 spin_lock_irqsave(&il->sta_lock, flags);
2352
2353 tid_data = &il->stations[sta_id].tid[tid];
2354 ssn = (tid_data->seq_number & IEEE80211_SCTL_SEQ) >> 4;
2355 txq_id = tid_data->agg.txq_id;
2356
2357 switch (il->stations[sta_id].tid[tid].agg.state) {
2358 case IL_EMPTYING_HW_QUEUE_ADDBA:
2359 /*
2360 * This can happen if the peer stops aggregation
2361 * again before we've had a chance to drain the
2362 * queue we selected previously, i.e. before the
2363 * session was really started completely.
2364 */
2365 D_HT("AGG stop before setup done\n");
2366 goto turn_off;
2367 case IL_AGG_ON:
2368 break;
2369 default:
2370 IL_WARN("Stopping AGG while state not ON or starting\n");
2371 }
2372
2373 write_ptr = il->txq[txq_id].q.write_ptr;
2374 read_ptr = il->txq[txq_id].q.read_ptr;
2375
2376 /* The queue is not empty */
2377 if (write_ptr != read_ptr) {
2378 D_HT("Stopping a non empty AGG HW QUEUE\n");
2379 il->stations[sta_id].tid[tid].agg.state =
2380 IL_EMPTYING_HW_QUEUE_DELBA;
2381 spin_unlock_irqrestore(&il->sta_lock, flags);
2382 return 0;
2383 }
2384
2385 D_HT("HW queue is empty\n");
2386 turn_off:
2387 il->stations[sta_id].tid[tid].agg.state = IL_AGG_OFF;
2388
2389 /* do not restore/save irqs */
2390 spin_unlock(&il->sta_lock);
2391 spin_lock(&il->lock);
2392
2393 /*
2394 * the only reason this call can fail is queue number out of range,
2395 * which can happen if uCode is reloaded and all the station
2396 * information are lost. if it is outside the range, there is no need
2397 * to deactivate the uCode queue, just return "success" to allow
2398 * mac80211 to clean up it own data.
2399 */
2400 il4965_txq_agg_disable(il, txq_id, ssn, tx_fifo_id);
2401 spin_unlock_irqrestore(&il->lock, flags);
2402
2403 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2404
2405 return 0;
2406}
2407
2408int il4965_txq_check_empty(struct il_priv *il,
2409 int sta_id, u8 tid, int txq_id)
2410{
2411 struct il_queue *q = &il->txq[txq_id].q;
2412 u8 *addr = il->stations[sta_id].sta.sta.addr;
2413 struct il_tid_data *tid_data = &il->stations[sta_id].tid[tid];
2414 struct il_rxon_context *ctx;
2415
2416 ctx = &il->ctx;
2417
2418 lockdep_assert_held(&il->sta_lock);
2419
2420 switch (il->stations[sta_id].tid[tid].agg.state) {
2421 case IL_EMPTYING_HW_QUEUE_DELBA:
2422 /* We are reclaiming the last packet of the */
2423 /* aggregated HW queue */
2424 if (txq_id == tid_data->agg.txq_id &&
2425 q->read_ptr == q->write_ptr) {
2426 u16 ssn = SEQ_TO_SN(tid_data->seq_number);
2427 int tx_fifo = il4965_get_fifo_from_tid(ctx, tid);
2428 D_HT(
2429 "HW queue empty: continue DELBA flow\n");
2430 il4965_txq_agg_disable(il, txq_id, ssn, tx_fifo);
2431 tid_data->agg.state = IL_AGG_OFF;
2432 ieee80211_stop_tx_ba_cb_irqsafe(ctx->vif, addr, tid);
2433 }
2434 break;
2435 case IL_EMPTYING_HW_QUEUE_ADDBA:
2436 /* We are reclaiming the last packet of the queue */
2437 if (tid_data->tfds_in_queue == 0) {
2438 D_HT(
2439 "HW queue empty: continue ADDBA flow\n");
2440 tid_data->agg.state = IL_AGG_ON;
2441 ieee80211_start_tx_ba_cb_irqsafe(ctx->vif, addr, tid);
2442 }
2443 break;
2444 }
2445
2446 return 0;
2447}
2448
2449static void il4965_non_agg_tx_status(struct il_priv *il,
2450 struct il_rxon_context *ctx,
2451 const u8 *addr1)
2452{
2453 struct ieee80211_sta *sta;
2454 struct il_station_priv *sta_priv;
2455
2456 rcu_read_lock();
2457 sta = ieee80211_find_sta(ctx->vif, addr1);
2458 if (sta) {
2459 sta_priv = (void *)sta->drv_priv;
2460 /* avoid atomic ops if this isn't a client */
2461 if (sta_priv->client &&
2462 atomic_dec_return(&sta_priv->pending_frames) == 0)
2463 ieee80211_sta_block_awake(il->hw, sta, false);
2464 }
2465 rcu_read_unlock();
2466}
2467
2468static void
2469il4965_tx_status(struct il_priv *il, struct il_tx_info *tx_info,
2470 bool is_agg)
2471{
2472 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) tx_info->skb->data;
2473
2474 if (!is_agg)
2475 il4965_non_agg_tx_status(il, tx_info->ctx, hdr->addr1);
2476
2477 ieee80211_tx_status_irqsafe(il->hw, tx_info->skb);
2478}
2479
2480int il4965_tx_queue_reclaim(struct il_priv *il, int txq_id, int idx)
2481{
2482 struct il_tx_queue *txq = &il->txq[txq_id];
2483 struct il_queue *q = &txq->q;
2484 struct il_tx_info *tx_info;
2485 int nfreed = 0;
2486 struct ieee80211_hdr *hdr;
2487
2488 if (idx >= q->n_bd || il_queue_used(q, idx) == 0) {
2489 IL_ERR("Read idx for DMA queue txq id (%d), idx %d, "
2490 "is out of range [0-%d] %d %d.\n", txq_id,
2491 idx, q->n_bd, q->write_ptr, q->read_ptr);
2492 return 0;
2493 }
2494
2495 for (idx = il_queue_inc_wrap(idx, q->n_bd);
2496 q->read_ptr != idx;
2497 q->read_ptr = il_queue_inc_wrap(q->read_ptr, q->n_bd)) {
2498
2499 tx_info = &txq->txb[txq->q.read_ptr];
2500
2501 if (WARN_ON_ONCE(tx_info->skb == NULL))
2502 continue;
2503
2504 hdr = (struct ieee80211_hdr *)tx_info->skb->data;
2505 if (ieee80211_is_data_qos(hdr->frame_control))
2506 nfreed++;
2507
2508 il4965_tx_status(il, tx_info,
2509 txq_id >= IL4965_FIRST_AMPDU_QUEUE);
2510 tx_info->skb = NULL;
2511
2512 il->cfg->ops->lib->txq_free_tfd(il, txq);
2513 }
2514 return nfreed;
2515}
2516
2517/**
2518 * il4965_tx_status_reply_compressed_ba - Update tx status from block-ack
2519 *
2520 * Go through block-ack's bitmap of ACK'd frames, update driver's record of
2521 * ACK vs. not. This gets sent to mac80211, then to rate scaling algo.
2522 */
2523static int il4965_tx_status_reply_compressed_ba(struct il_priv *il,
2524 struct il_ht_agg *agg,
2525 struct il_compressed_ba_resp *ba_resp)
2526
2527{
2528 int i, sh, ack;
2529 u16 seq_ctl = le16_to_cpu(ba_resp->seq_ctl);
2530 u16 scd_flow = le16_to_cpu(ba_resp->scd_flow);
2531 int successes = 0;
2532 struct ieee80211_tx_info *info;
2533 u64 bitmap, sent_bitmap;
2534
2535 if (unlikely(!agg->wait_for_ba)) {
2536 if (unlikely(ba_resp->bitmap))
2537 IL_ERR("Received BA when not expected\n");
2538 return -EINVAL;
2539 }
2540
2541 /* Mark that the expected block-ack response arrived */
2542 agg->wait_for_ba = 0;
2543 D_TX_REPLY("BA %d %d\n", agg->start_idx,
2544 ba_resp->seq_ctl);
2545
2546 /* Calculate shift to align block-ack bits with our Tx win bits */
2547 sh = agg->start_idx - SEQ_TO_IDX(seq_ctl >> 4);
2548 if (sh < 0) /* tbw something is wrong with indices */
2549 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;
2569 D_TX_REPLY("%s ON i=%d idx=%d raw=%d\n",
2570 ack ? "ACK" : "NACK", i,
2571 (agg->start_idx + i) & 0xff,
2572 agg->start_idx + i);
2573 sent_bitmap >>= 1;
2574 ++i;
2575 }
2576
2577 D_TX_REPLY("Bitmap %llx\n",
2578 (unsigned long long)bitmap);
2579
2580 info = IEEE80211_SKB_CB(il->txq[scd_flow].txb[agg->start_idx].skb);
2581 memset(&info->status, 0, sizeof(info->status));
2582 info->flags |= IEEE80211_TX_STAT_ACK;
2583 info->flags |= IEEE80211_TX_STAT_AMPDU;
2584 info->status.ampdu_ack_len = successes;
2585 info->status.ampdu_len = agg->frame_count;
2586 il4965_hwrate_to_tx_control(il, agg->rate_n_flags, info);
2587
2588 return 0;
2589}
2590
2591/**
2592 * translate ucode response to mac80211 tx status control values
2593 */
2594void il4965_hwrate_to_tx_control(struct il_priv *il, u32 rate_n_flags,
2595 struct ieee80211_tx_info *info)
2596{
2597 struct ieee80211_tx_rate *r = &info->control.rates[0];
2598
2599 info->antenna_sel_tx =
2600 ((rate_n_flags & RATE_MCS_ANT_ABC_MSK) >> RATE_MCS_ANT_POS);
2601 if (rate_n_flags & RATE_MCS_HT_MSK)
2602 r->flags |= IEEE80211_TX_RC_MCS;
2603 if (rate_n_flags & RATE_MCS_GF_MSK)
2604 r->flags |= IEEE80211_TX_RC_GREEN_FIELD;
2605 if (rate_n_flags & RATE_MCS_HT40_MSK)
2606 r->flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
2607 if (rate_n_flags & RATE_MCS_DUP_MSK)
2608 r->flags |= IEEE80211_TX_RC_DUP_DATA;
2609 if (rate_n_flags & RATE_MCS_SGI_MSK)
2610 r->flags |= IEEE80211_TX_RC_SHORT_GI;
2611 r->idx = il4965_hwrate_to_mac80211_idx(rate_n_flags, info->band);
2612}
2613
2614/**
2615 * il4965_rx_reply_compressed_ba - Handler for REPLY_COMPRESSED_BA
2616 *
2617 * Handles block-acknowledge notification from device, which reports success
2618 * of frames sent via aggregation.
2619 */
2620void il4965_rx_reply_compressed_ba(struct il_priv *il,
2621 struct il_rx_buf *rxb)
2622{
2623 struct il_rx_pkt *pkt = rxb_addr(rxb);
2624 struct il_compressed_ba_resp *ba_resp = &pkt->u.compressed_ba;
2625 struct il_tx_queue *txq = NULL;
2626 struct il_ht_agg *agg;
2627 int idx;
2628 int sta_id;
2629 int tid;
2630 unsigned long flags;
2631
2632 /* "flow" corresponds to Tx queue */
2633 u16 scd_flow = le16_to_cpu(ba_resp->scd_flow);
2634
2635 /* "ssn" is start of block-ack Tx win, corresponds to idx
2636 * (in Tx queue's circular buffer) of first TFD/frame in win */
2637 u16 ba_resp_scd_ssn = le16_to_cpu(ba_resp->scd_ssn);
2638
2639 if (scd_flow >= il->hw_params.max_txq_num) {
2640 IL_ERR(
2641 "BUG_ON scd_flow is bigger than number of queues\n");
2642 return;
2643 }
2644
2645 txq = &il->txq[scd_flow];
2646 sta_id = ba_resp->sta_id;
2647 tid = ba_resp->tid;
2648 agg = &il->stations[sta_id].tid[tid].agg;
2649 if (unlikely(agg->txq_id != scd_flow)) {
2650 /*
2651 * FIXME: this is a uCode bug which need to be addressed,
2652 * log the information and return for now!
2653 * since it is possible happen very often and in order
2654 * not to fill the syslog, don't enable the logging by default
2655 */
2656 D_TX_REPLY(
2657 "BA scd_flow %d does not match txq_id %d\n",
2658 scd_flow, agg->txq_id);
2659 return;
2660 }
2661
2662 /* Find idx just before block-ack win */
2663 idx = il_queue_dec_wrap(ba_resp_scd_ssn & 0xff, txq->q.n_bd);
2664
2665 spin_lock_irqsave(&il->sta_lock, flags);
2666
2667 D_TX_REPLY("REPLY_COMPRESSED_BA [%d] Received from %pM, "
2668 "sta_id = %d\n",
2669 agg->wait_for_ba,
2670 (u8 *) &ba_resp->sta_addr_lo32,
2671 ba_resp->sta_id);
2672 D_TX_REPLY("TID = %d, SeqCtl = %d, bitmap = 0x%llx,"
2673 "scd_flow = "
2674 "%d, scd_ssn = %d\n",
2675 ba_resp->tid,
2676 ba_resp->seq_ctl,
2677 (unsigned long long)le64_to_cpu(ba_resp->bitmap),
2678 ba_resp->scd_flow,
2679 ba_resp->scd_ssn);
2680 D_TX_REPLY("DAT start_idx = %d, bitmap = 0x%llx\n",
2681 agg->start_idx,
2682 (unsigned long long)agg->bitmap);
2683
2684 /* Update driver's record of ACK vs. not for each frame in win */
2685 il4965_tx_status_reply_compressed_ba(il, agg, ba_resp);
2686
2687 /* Release all TFDs before the SSN, i.e. all TFDs in front of
2688 * block-ack win (we assume that they've been successfully
2689 * transmitted ... if not, it's too late anyway). */
2690 if (txq->q.read_ptr != (ba_resp_scd_ssn & 0xff)) {
2691 /* calculate mac80211 ampdu sw queue to wake */
2692 int freed = il4965_tx_queue_reclaim(il, scd_flow, idx);
2693 il4965_free_tfds_in_queue(il, sta_id, tid, freed);
2694
2695 if (il_queue_space(&txq->q) > txq->q.low_mark &&
2696 il->mac80211_registered &&
2697 agg->state != IL_EMPTYING_HW_QUEUE_DELBA)
2698 il_wake_queue(il, txq);
2699
2700 il4965_txq_check_empty(il, sta_id, tid, scd_flow);
2701 }
2702
2703 spin_unlock_irqrestore(&il->sta_lock, flags);
2704}
2705
2706#ifdef CONFIG_IWLEGACY_DEBUG
2707const char *il4965_get_tx_fail_reason(u32 status)
2708{
2709#define TX_STATUS_FAIL(x) case TX_STATUS_FAIL_ ## x: return #x
2710#define TX_STATUS_POSTPONE(x) case TX_STATUS_POSTPONE_ ## x: return #x
2711
2712 switch (status & TX_STATUS_MSK) {
2713 case TX_STATUS_SUCCESS:
2714 return "SUCCESS";
2715 TX_STATUS_POSTPONE(DELAY);
2716 TX_STATUS_POSTPONE(FEW_BYTES);
2717 TX_STATUS_POSTPONE(QUIET_PERIOD);
2718 TX_STATUS_POSTPONE(CALC_TTAK);
2719 TX_STATUS_FAIL(INTERNAL_CROSSED_RETRY);
2720 TX_STATUS_FAIL(SHORT_LIMIT);
2721 TX_STATUS_FAIL(LONG_LIMIT);
2722 TX_STATUS_FAIL(FIFO_UNDERRUN);
2723 TX_STATUS_FAIL(DRAIN_FLOW);
2724 TX_STATUS_FAIL(RFKILL_FLUSH);
2725 TX_STATUS_FAIL(LIFE_EXPIRE);
2726 TX_STATUS_FAIL(DEST_PS);
2727 TX_STATUS_FAIL(HOST_ABORTED);
2728 TX_STATUS_FAIL(BT_RETRY);
2729 TX_STATUS_FAIL(STA_INVALID);
2730 TX_STATUS_FAIL(FRAG_DROPPED);
2731 TX_STATUS_FAIL(TID_DISABLE);
2732 TX_STATUS_FAIL(FIFO_FLUSHED);
2733 TX_STATUS_FAIL(INSUFFICIENT_CF_POLL);
2734 TX_STATUS_FAIL(PASSIVE_NO_RX);
2735 TX_STATUS_FAIL(NO_BEACON_ON_RADAR);
2736 }
2737
2738 return "UNKNOWN";
2739
2740#undef TX_STATUS_FAIL
2741#undef TX_STATUS_POSTPONE
2742}
2743#endif /* CONFIG_IWLEGACY_DEBUG */
2744
Stanislaw Gruszkaeb3cdfb2011-08-30 12:58:35 +02002745static struct il_link_quality_cmd *
2746il4965_sta_alloc_lq(struct il_priv *il, u8 sta_id)
2747{
2748 int i, r;
2749 struct il_link_quality_cmd *link_cmd;
2750 u32 rate_flags = 0;
2751 __le32 rate_n_flags;
2752
2753 link_cmd = kzalloc(sizeof(struct il_link_quality_cmd), GFP_KERNEL);
2754 if (!link_cmd) {
2755 IL_ERR("Unable to allocate memory for LQ cmd.\n");
2756 return NULL;
2757 }
2758 /* Set up the rate scaling to start at selected rate, fall back
2759 * all the way down to 1M in IEEE order, and then spin on 1M */
2760 if (il->band == IEEE80211_BAND_5GHZ)
2761 r = RATE_6M_IDX;
2762 else
2763 r = RATE_1M_IDX;
2764
2765 if (r >= IL_FIRST_CCK_RATE && r <= IL_LAST_CCK_RATE)
2766 rate_flags |= RATE_MCS_CCK_MSK;
2767
2768 rate_flags |= il4965_first_antenna(il->hw_params.valid_tx_ant) <<
2769 RATE_MCS_ANT_POS;
2770 rate_n_flags = il4965_hw_set_rate_n_flags(il_rates[r].plcp,
2771 rate_flags);
2772 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
2773 link_cmd->rs_table[i].rate_n_flags = rate_n_flags;
2774
2775 link_cmd->general_params.single_stream_ant_msk =
2776 il4965_first_antenna(il->hw_params.valid_tx_ant);
2777
2778 link_cmd->general_params.dual_stream_ant_msk =
2779 il->hw_params.valid_tx_ant &
2780 ~il4965_first_antenna(il->hw_params.valid_tx_ant);
2781 if (!link_cmd->general_params.dual_stream_ant_msk) {
2782 link_cmd->general_params.dual_stream_ant_msk = ANT_AB;
2783 } else if (il4965_num_of_ant(il->hw_params.valid_tx_ant) == 2) {
2784 link_cmd->general_params.dual_stream_ant_msk =
2785 il->hw_params.valid_tx_ant;
2786 }
2787
2788 link_cmd->agg_params.agg_dis_start_th = LINK_QUAL_AGG_DISABLE_START_DEF;
2789 link_cmd->agg_params.agg_time_limit =
2790 cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF);
2791
2792 link_cmd->sta_id = sta_id;
2793
2794 return link_cmd;
2795}
2796
2797/*
2798 * il4965_add_bssid_station - Add the special IBSS BSSID station
2799 *
2800 * Function sleeps.
2801 */
2802int
2803il4965_add_bssid_station(struct il_priv *il, struct il_rxon_context *ctx,
2804 const u8 *addr, u8 *sta_id_r)
2805{
2806 int ret;
2807 u8 sta_id;
2808 struct il_link_quality_cmd *link_cmd;
2809 unsigned long flags;
2810
2811 if (sta_id_r)
2812 *sta_id_r = IL_INVALID_STATION;
2813
2814 ret = il_add_station_common(il, ctx, addr, 0, NULL, &sta_id);
2815 if (ret) {
2816 IL_ERR("Unable to add station %pM\n", addr);
2817 return ret;
2818 }
2819
2820 if (sta_id_r)
2821 *sta_id_r = sta_id;
2822
2823 spin_lock_irqsave(&il->sta_lock, flags);
2824 il->stations[sta_id].used |= IL_STA_LOCAL;
2825 spin_unlock_irqrestore(&il->sta_lock, flags);
2826
2827 /* Set up default rate scaling table in device's station table */
2828 link_cmd = il4965_sta_alloc_lq(il, sta_id);
2829 if (!link_cmd) {
2830 IL_ERR(
2831 "Unable to initialize rate scaling for station %pM.\n",
2832 addr);
2833 return -ENOMEM;
2834 }
2835
2836 ret = il_send_lq_cmd(il, ctx, link_cmd, CMD_SYNC, true);
2837 if (ret)
2838 IL_ERR("Link quality command failed (%d)\n", ret);
2839
2840 spin_lock_irqsave(&il->sta_lock, flags);
2841 il->stations[sta_id].lq = link_cmd;
2842 spin_unlock_irqrestore(&il->sta_lock, flags);
2843
2844 return 0;
2845}
2846
2847static int il4965_static_wepkey_cmd(struct il_priv *il,
2848 struct il_rxon_context *ctx,
2849 bool send_if_empty)
2850{
2851 int i, not_empty = 0;
2852 u8 buff[sizeof(struct il_wep_cmd) +
2853 sizeof(struct il_wep_key) * WEP_KEYS_MAX];
2854 struct il_wep_cmd *wep_cmd = (struct il_wep_cmd *)buff;
2855 size_t cmd_size = sizeof(struct il_wep_cmd);
2856 struct il_host_cmd cmd = {
2857 .id = ctx->wep_key_cmd,
2858 .data = wep_cmd,
2859 .flags = CMD_SYNC,
2860 };
2861
2862 might_sleep();
2863
2864 memset(wep_cmd, 0, cmd_size +
2865 (sizeof(struct il_wep_key) * WEP_KEYS_MAX));
2866
2867 for (i = 0; i < WEP_KEYS_MAX ; i++) {
2868 wep_cmd->key[i].key_idx = i;
2869 if (ctx->wep_keys[i].key_size) {
2870 wep_cmd->key[i].key_offset = i;
2871 not_empty = 1;
2872 } else {
2873 wep_cmd->key[i].key_offset = WEP_INVALID_OFFSET;
2874 }
2875
2876 wep_cmd->key[i].key_size = ctx->wep_keys[i].key_size;
2877 memcpy(&wep_cmd->key[i].key[3], ctx->wep_keys[i].key,
2878 ctx->wep_keys[i].key_size);
2879 }
2880
2881 wep_cmd->global_key_type = WEP_KEY_WEP_TYPE;
2882 wep_cmd->num_keys = WEP_KEYS_MAX;
2883
2884 cmd_size += sizeof(struct il_wep_key) * WEP_KEYS_MAX;
2885
2886 cmd.len = cmd_size;
2887
2888 if (not_empty || send_if_empty)
2889 return il_send_cmd(il, &cmd);
2890 else
2891 return 0;
2892}
2893
2894int il4965_restore_default_wep_keys(struct il_priv *il,
2895 struct il_rxon_context *ctx)
2896{
2897 lockdep_assert_held(&il->mutex);
2898
2899 return il4965_static_wepkey_cmd(il, ctx, false);
2900}
2901
2902int il4965_remove_default_wep_key(struct il_priv *il,
2903 struct il_rxon_context *ctx,
2904 struct ieee80211_key_conf *keyconf)
2905{
2906 int ret;
2907
2908 lockdep_assert_held(&il->mutex);
2909
2910 D_WEP("Removing default WEP key: idx=%d\n",
2911 keyconf->keyidx);
2912
2913 memset(&ctx->wep_keys[keyconf->keyidx], 0, sizeof(ctx->wep_keys[0]));
2914 if (il_is_rfkill(il)) {
2915 D_WEP(
2916 "Not sending REPLY_WEPKEY command due to RFKILL.\n");
2917 /* but keys in device are clear anyway so return success */
2918 return 0;
2919 }
2920 ret = il4965_static_wepkey_cmd(il, ctx, 1);
2921 D_WEP("Remove default WEP key: idx=%d ret=%d\n",
2922 keyconf->keyidx, ret);
2923
2924 return ret;
2925}
2926
2927int il4965_set_default_wep_key(struct il_priv *il,
2928 struct il_rxon_context *ctx,
2929 struct ieee80211_key_conf *keyconf)
2930{
2931 int ret;
2932
2933 lockdep_assert_held(&il->mutex);
2934
2935 if (keyconf->keylen != WEP_KEY_LEN_128 &&
2936 keyconf->keylen != WEP_KEY_LEN_64) {
2937 D_WEP("Bad WEP key length %d\n", keyconf->keylen);
2938 return -EINVAL;
2939 }
2940
2941 keyconf->flags &= ~IEEE80211_KEY_FLAG_GENERATE_IV;
2942 keyconf->hw_key_idx = HW_KEY_DEFAULT;
2943 il->stations[ctx->ap_sta_id].keyinfo.cipher = keyconf->cipher;
2944
2945 ctx->wep_keys[keyconf->keyidx].key_size = keyconf->keylen;
2946 memcpy(&ctx->wep_keys[keyconf->keyidx].key, &keyconf->key,
2947 keyconf->keylen);
2948
2949 ret = il4965_static_wepkey_cmd(il, ctx, false);
2950 D_WEP("Set default WEP key: len=%d idx=%d ret=%d\n",
2951 keyconf->keylen, keyconf->keyidx, ret);
2952
2953 return ret;
2954}
2955
2956static int il4965_set_wep_dynamic_key_info(struct il_priv *il,
2957 struct il_rxon_context *ctx,
2958 struct ieee80211_key_conf *keyconf,
2959 u8 sta_id)
2960{
2961 unsigned long flags;
2962 __le16 key_flags = 0;
2963 struct il_addsta_cmd sta_cmd;
2964
2965 lockdep_assert_held(&il->mutex);
2966
2967 keyconf->flags &= ~IEEE80211_KEY_FLAG_GENERATE_IV;
2968
2969 key_flags |= (STA_KEY_FLG_WEP | STA_KEY_FLG_MAP_KEY_MSK);
2970 key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
2971 key_flags &= ~STA_KEY_FLG_INVALID;
2972
2973 if (keyconf->keylen == WEP_KEY_LEN_128)
2974 key_flags |= STA_KEY_FLG_KEY_SIZE_MSK;
2975
2976 if (sta_id == ctx->bcast_sta_id)
2977 key_flags |= STA_KEY_MULTICAST_MSK;
2978
2979 spin_lock_irqsave(&il->sta_lock, flags);
2980
2981 il->stations[sta_id].keyinfo.cipher = keyconf->cipher;
2982 il->stations[sta_id].keyinfo.keylen = keyconf->keylen;
2983 il->stations[sta_id].keyinfo.keyidx = keyconf->keyidx;
2984
2985 memcpy(il->stations[sta_id].keyinfo.key,
2986 keyconf->key, keyconf->keylen);
2987
2988 memcpy(&il->stations[sta_id].sta.key.key[3],
2989 keyconf->key, keyconf->keylen);
2990
2991 if ((il->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
2992 == STA_KEY_FLG_NO_ENC)
2993 il->stations[sta_id].sta.key.key_offset =
2994 il_get_free_ucode_key_idx(il);
2995 /* else, we are overriding an existing key => no need to allocated room
2996 * in uCode. */
2997
2998 WARN(il->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
2999 "no space for a new key");
3000
3001 il->stations[sta_id].sta.key.key_flags = key_flags;
3002 il->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
3003 il->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
3004
3005 memcpy(&sta_cmd, &il->stations[sta_id].sta,
3006 sizeof(struct il_addsta_cmd));
3007 spin_unlock_irqrestore(&il->sta_lock, flags);
3008
3009 return il_send_add_sta(il, &sta_cmd, CMD_SYNC);
3010}
3011
3012static int il4965_set_ccmp_dynamic_key_info(struct il_priv *il,
3013 struct il_rxon_context *ctx,
3014 struct ieee80211_key_conf *keyconf,
3015 u8 sta_id)
3016{
3017 unsigned long flags;
3018 __le16 key_flags = 0;
3019 struct il_addsta_cmd sta_cmd;
3020
3021 lockdep_assert_held(&il->mutex);
3022
3023 key_flags |= (STA_KEY_FLG_CCMP | STA_KEY_FLG_MAP_KEY_MSK);
3024 key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
3025 key_flags &= ~STA_KEY_FLG_INVALID;
3026
3027 if (sta_id == ctx->bcast_sta_id)
3028 key_flags |= STA_KEY_MULTICAST_MSK;
3029
3030 keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
3031
3032 spin_lock_irqsave(&il->sta_lock, flags);
3033 il->stations[sta_id].keyinfo.cipher = keyconf->cipher;
3034 il->stations[sta_id].keyinfo.keylen = keyconf->keylen;
3035
3036 memcpy(il->stations[sta_id].keyinfo.key, keyconf->key,
3037 keyconf->keylen);
3038
3039 memcpy(il->stations[sta_id].sta.key.key, keyconf->key,
3040 keyconf->keylen);
3041
3042 if ((il->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
3043 == STA_KEY_FLG_NO_ENC)
3044 il->stations[sta_id].sta.key.key_offset =
3045 il_get_free_ucode_key_idx(il);
3046 /* else, we are overriding an existing key => no need to allocated room
3047 * in uCode. */
3048
3049 WARN(il->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
3050 "no space for a new key");
3051
3052 il->stations[sta_id].sta.key.key_flags = key_flags;
3053 il->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
3054 il->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
3055
3056 memcpy(&sta_cmd, &il->stations[sta_id].sta,
3057 sizeof(struct il_addsta_cmd));
3058 spin_unlock_irqrestore(&il->sta_lock, flags);
3059
3060 return il_send_add_sta(il, &sta_cmd, CMD_SYNC);
3061}
3062
3063static int il4965_set_tkip_dynamic_key_info(struct il_priv *il,
3064 struct il_rxon_context *ctx,
3065 struct ieee80211_key_conf *keyconf,
3066 u8 sta_id)
3067{
3068 unsigned long flags;
3069 int ret = 0;
3070 __le16 key_flags = 0;
3071
3072 key_flags |= (STA_KEY_FLG_TKIP | STA_KEY_FLG_MAP_KEY_MSK);
3073 key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
3074 key_flags &= ~STA_KEY_FLG_INVALID;
3075
3076 if (sta_id == ctx->bcast_sta_id)
3077 key_flags |= STA_KEY_MULTICAST_MSK;
3078
3079 keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
3080 keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
3081
3082 spin_lock_irqsave(&il->sta_lock, flags);
3083
3084 il->stations[sta_id].keyinfo.cipher = keyconf->cipher;
3085 il->stations[sta_id].keyinfo.keylen = 16;
3086
3087 if ((il->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
3088 == STA_KEY_FLG_NO_ENC)
3089 il->stations[sta_id].sta.key.key_offset =
3090 il_get_free_ucode_key_idx(il);
3091 /* else, we are overriding an existing key => no need to allocated room
3092 * in uCode. */
3093
3094 WARN(il->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
3095 "no space for a new key");
3096
3097 il->stations[sta_id].sta.key.key_flags = key_flags;
3098
3099
3100 /* This copy is acutally not needed: we get the key with each TX */
3101 memcpy(il->stations[sta_id].keyinfo.key, keyconf->key, 16);
3102
3103 memcpy(il->stations[sta_id].sta.key.key, keyconf->key, 16);
3104
3105 spin_unlock_irqrestore(&il->sta_lock, flags);
3106
3107 return ret;
3108}
3109
3110void il4965_update_tkip_key(struct il_priv *il,
3111 struct il_rxon_context *ctx,
3112 struct ieee80211_key_conf *keyconf,
3113 struct ieee80211_sta *sta, u32 iv32, u16 *phase1key)
3114{
3115 u8 sta_id;
3116 unsigned long flags;
3117 int i;
3118
3119 if (il_scan_cancel(il)) {
3120 /* cancel scan failed, just live w/ bad key and rely
3121 briefly on SW decryption */
3122 return;
3123 }
3124
3125 sta_id = il_sta_id_or_broadcast(il, ctx, sta);
3126 if (sta_id == IL_INVALID_STATION)
3127 return;
3128
3129 spin_lock_irqsave(&il->sta_lock, flags);
3130
3131 il->stations[sta_id].sta.key.tkip_rx_tsc_byte2 = (u8) iv32;
3132
3133 for (i = 0; i < 5; i++)
3134 il->stations[sta_id].sta.key.tkip_rx_ttak[i] =
3135 cpu_to_le16(phase1key[i]);
3136
3137 il->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
3138 il->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
3139
3140 il_send_add_sta(il, &il->stations[sta_id].sta, CMD_ASYNC);
3141
3142 spin_unlock_irqrestore(&il->sta_lock, flags);
3143
3144}
3145
3146int il4965_remove_dynamic_key(struct il_priv *il,
3147 struct il_rxon_context *ctx,
3148 struct ieee80211_key_conf *keyconf,
3149 u8 sta_id)
3150{
3151 unsigned long flags;
3152 u16 key_flags;
3153 u8 keyidx;
3154 struct il_addsta_cmd sta_cmd;
3155
3156 lockdep_assert_held(&il->mutex);
3157
3158 ctx->key_mapping_keys--;
3159
3160 spin_lock_irqsave(&il->sta_lock, flags);
3161 key_flags = le16_to_cpu(il->stations[sta_id].sta.key.key_flags);
3162 keyidx = (key_flags >> STA_KEY_FLG_KEYID_POS) & 0x3;
3163
3164 D_WEP("Remove dynamic key: idx=%d sta=%d\n",
3165 keyconf->keyidx, sta_id);
3166
3167 if (keyconf->keyidx != keyidx) {
3168 /* We need to remove a key with idx different that the one
3169 * in the uCode. This means that the key we need to remove has
3170 * been replaced by another one with different idx.
3171 * Don't do anything and return ok
3172 */
3173 spin_unlock_irqrestore(&il->sta_lock, flags);
3174 return 0;
3175 }
3176
3177 if (il->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET) {
3178 IL_WARN("Removing wrong key %d 0x%x\n",
3179 keyconf->keyidx, key_flags);
3180 spin_unlock_irqrestore(&il->sta_lock, flags);
3181 return 0;
3182 }
3183
3184 if (!test_and_clear_bit(il->stations[sta_id].sta.key.key_offset,
3185 &il->ucode_key_table))
3186 IL_ERR("idx %d not used in uCode key table.\n",
3187 il->stations[sta_id].sta.key.key_offset);
3188 memset(&il->stations[sta_id].keyinfo, 0,
3189 sizeof(struct il_hw_key));
3190 memset(&il->stations[sta_id].sta.key, 0,
3191 sizeof(struct il4965_keyinfo));
3192 il->stations[sta_id].sta.key.key_flags =
3193 STA_KEY_FLG_NO_ENC | STA_KEY_FLG_INVALID;
3194 il->stations[sta_id].sta.key.key_offset = WEP_INVALID_OFFSET;
3195 il->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
3196 il->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
3197
3198 if (il_is_rfkill(il)) {
3199 D_WEP(
3200 "Not sending REPLY_ADD_STA command because RFKILL enabled.\n");
3201 spin_unlock_irqrestore(&il->sta_lock, flags);
3202 return 0;
3203 }
3204 memcpy(&sta_cmd, &il->stations[sta_id].sta,
3205 sizeof(struct il_addsta_cmd));
3206 spin_unlock_irqrestore(&il->sta_lock, flags);
3207
3208 return il_send_add_sta(il, &sta_cmd, CMD_SYNC);
3209}
3210
3211int il4965_set_dynamic_key(struct il_priv *il, struct il_rxon_context *ctx,
3212 struct ieee80211_key_conf *keyconf, u8 sta_id)
3213{
3214 int ret;
3215
3216 lockdep_assert_held(&il->mutex);
3217
3218 ctx->key_mapping_keys++;
3219 keyconf->hw_key_idx = HW_KEY_DYNAMIC;
3220
3221 switch (keyconf->cipher) {
3222 case WLAN_CIPHER_SUITE_CCMP:
3223 ret = il4965_set_ccmp_dynamic_key_info(il, ctx,
3224 keyconf, sta_id);
3225 break;
3226 case WLAN_CIPHER_SUITE_TKIP:
3227 ret = il4965_set_tkip_dynamic_key_info(il, ctx,
3228 keyconf, sta_id);
3229 break;
3230 case WLAN_CIPHER_SUITE_WEP40:
3231 case WLAN_CIPHER_SUITE_WEP104:
3232 ret = il4965_set_wep_dynamic_key_info(il, ctx,
3233 keyconf, sta_id);
3234 break;
3235 default:
3236 IL_ERR(
3237 "Unknown alg: %s cipher = %x\n", __func__,
3238 keyconf->cipher);
3239 ret = -EINVAL;
3240 }
3241
3242 D_WEP(
3243 "Set dynamic key: cipher=%x len=%d idx=%d sta=%d ret=%d\n",
3244 keyconf->cipher, keyconf->keylen, keyconf->keyidx,
3245 sta_id, ret);
3246
3247 return ret;
3248}
3249
3250/**
3251 * il4965_alloc_bcast_station - add broadcast station into driver's station table.
3252 *
3253 * This adds the broadcast station into the driver's station table
3254 * and marks it driver active, so that it will be restored to the
3255 * device at the next best time.
3256 */
3257int il4965_alloc_bcast_station(struct il_priv *il,
3258 struct il_rxon_context *ctx)
3259{
3260 struct il_link_quality_cmd *link_cmd;
3261 unsigned long flags;
3262 u8 sta_id;
3263
3264 spin_lock_irqsave(&il->sta_lock, flags);
3265 sta_id = il_prep_station(il, ctx, il_bcast_addr,
3266 false, NULL);
3267 if (sta_id == IL_INVALID_STATION) {
3268 IL_ERR("Unable to prepare broadcast station\n");
3269 spin_unlock_irqrestore(&il->sta_lock, flags);
3270
3271 return -EINVAL;
3272 }
3273
3274 il->stations[sta_id].used |= IL_STA_DRIVER_ACTIVE;
3275 il->stations[sta_id].used |= IL_STA_BCAST;
3276 spin_unlock_irqrestore(&il->sta_lock, flags);
3277
3278 link_cmd = il4965_sta_alloc_lq(il, sta_id);
3279 if (!link_cmd) {
3280 IL_ERR(
3281 "Unable to initialize rate scaling for bcast station.\n");
3282 return -ENOMEM;
3283 }
3284
3285 spin_lock_irqsave(&il->sta_lock, flags);
3286 il->stations[sta_id].lq = link_cmd;
3287 spin_unlock_irqrestore(&il->sta_lock, flags);
3288
3289 return 0;
3290}
3291
3292/**
3293 * il4965_update_bcast_station - update broadcast station's LQ command
3294 *
3295 * Only used by iwl4965. Placed here to have all bcast station management
3296 * code together.
3297 */
3298static int il4965_update_bcast_station(struct il_priv *il,
3299 struct il_rxon_context *ctx)
3300{
3301 unsigned long flags;
3302 struct il_link_quality_cmd *link_cmd;
3303 u8 sta_id = ctx->bcast_sta_id;
3304
3305 link_cmd = il4965_sta_alloc_lq(il, sta_id);
3306 if (!link_cmd) {
3307 IL_ERR(
3308 "Unable to initialize rate scaling for bcast station.\n");
3309 return -ENOMEM;
3310 }
3311
3312 spin_lock_irqsave(&il->sta_lock, flags);
3313 if (il->stations[sta_id].lq)
3314 kfree(il->stations[sta_id].lq);
3315 else
3316 D_INFO(
3317 "Bcast station rate scaling has not been initialized yet.\n");
3318 il->stations[sta_id].lq = link_cmd;
3319 spin_unlock_irqrestore(&il->sta_lock, flags);
3320
3321 return 0;
3322}
3323
3324int il4965_update_bcast_stations(struct il_priv *il)
3325{
3326 return il4965_update_bcast_station(il, &il->ctx);
3327}
3328
3329/**
3330 * il4965_sta_tx_modify_enable_tid - Enable Tx for this TID in station table
3331 */
3332int il4965_sta_tx_modify_enable_tid(struct il_priv *il, int sta_id, int tid)
3333{
3334 unsigned long flags;
3335 struct il_addsta_cmd sta_cmd;
3336
3337 lockdep_assert_held(&il->mutex);
3338
3339 /* Remove "disable" flag, to enable Tx for this TID */
3340 spin_lock_irqsave(&il->sta_lock, flags);
3341 il->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_TID_DISABLE_TX;
3342 il->stations[sta_id].sta.tid_disable_tx &= cpu_to_le16(~(1 << tid));
3343 il->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
3344 memcpy(&sta_cmd, &il->stations[sta_id].sta,
3345 sizeof(struct il_addsta_cmd));
3346 spin_unlock_irqrestore(&il->sta_lock, flags);
3347
3348 return il_send_add_sta(il, &sta_cmd, CMD_SYNC);
3349}
3350
3351int il4965_sta_rx_agg_start(struct il_priv *il, struct ieee80211_sta *sta,
3352 int tid, u16 ssn)
3353{
3354 unsigned long flags;
3355 int sta_id;
3356 struct il_addsta_cmd sta_cmd;
3357
3358 lockdep_assert_held(&il->mutex);
3359
3360 sta_id = il_sta_id(sta);
3361 if (sta_id == IL_INVALID_STATION)
3362 return -ENXIO;
3363
3364 spin_lock_irqsave(&il->sta_lock, flags);
3365 il->stations[sta_id].sta.station_flags_msk = 0;
3366 il->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_ADDBA_TID_MSK;
3367 il->stations[sta_id].sta.add_immediate_ba_tid = (u8)tid;
3368 il->stations[sta_id].sta.add_immediate_ba_ssn = cpu_to_le16(ssn);
3369 il->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
3370 memcpy(&sta_cmd, &il->stations[sta_id].sta,
3371 sizeof(struct il_addsta_cmd));
3372 spin_unlock_irqrestore(&il->sta_lock, flags);
3373
3374 return il_send_add_sta(il, &sta_cmd, CMD_SYNC);
3375}
3376
3377int il4965_sta_rx_agg_stop(struct il_priv *il, struct ieee80211_sta *sta,
3378 int tid)
3379{
3380 unsigned long flags;
3381 int sta_id;
3382 struct il_addsta_cmd sta_cmd;
3383
3384 lockdep_assert_held(&il->mutex);
3385
3386 sta_id = il_sta_id(sta);
3387 if (sta_id == IL_INVALID_STATION) {
3388 IL_ERR("Invalid station for AGG tid %d\n", tid);
3389 return -ENXIO;
3390 }
3391
3392 spin_lock_irqsave(&il->sta_lock, flags);
3393 il->stations[sta_id].sta.station_flags_msk = 0;
3394 il->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_DELBA_TID_MSK;
3395 il->stations[sta_id].sta.remove_immediate_ba_tid = (u8)tid;
3396 il->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
3397 memcpy(&sta_cmd, &il->stations[sta_id].sta,
3398 sizeof(struct il_addsta_cmd));
3399 spin_unlock_irqrestore(&il->sta_lock, flags);
3400
3401 return il_send_add_sta(il, &sta_cmd, CMD_SYNC);
3402}
3403
3404void
3405il4965_sta_modify_sleep_tx_count(struct il_priv *il, int sta_id, int cnt)
3406{
3407 unsigned long flags;
3408
3409 spin_lock_irqsave(&il->sta_lock, flags);
3410 il->stations[sta_id].sta.station_flags |= STA_FLG_PWR_SAVE_MSK;
3411 il->stations[sta_id].sta.station_flags_msk = STA_FLG_PWR_SAVE_MSK;
3412 il->stations[sta_id].sta.sta.modify_mask =
3413 STA_MODIFY_SLEEP_TX_COUNT_MSK;
3414 il->stations[sta_id].sta.sleep_tx_count = cpu_to_le16(cnt);
3415 il->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
3416 il_send_add_sta(il,
3417 &il->stations[sta_id].sta, CMD_ASYNC);
3418 spin_unlock_irqrestore(&il->sta_lock, flags);
3419
3420}
3421
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003422void il4965_update_chain_flags(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003423{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003424 if (il->cfg->ops->hcmd->set_rxon_chain) {
Stanislaw Gruszka17d6e552011-08-29 12:52:20 +02003425 il->cfg->ops->hcmd->set_rxon_chain(il, &il->ctx);
3426 if (il->ctx.active.rx_chain != il->ctx.staging.rx_chain)
3427 il_commit_rxon(il, &il->ctx);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003428 }
3429}
3430
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003431static void il4965_clear_free_frames(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003432{
3433 struct list_head *element;
3434
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01003435 D_INFO("%d frames on pre-allocated heap on clear.\n",
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003436 il->frames_count);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003437
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003438 while (!list_empty(&il->free_frames)) {
3439 element = il->free_frames.next;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003440 list_del(element);
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003441 kfree(list_entry(element, struct il_frame, list));
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003442 il->frames_count--;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003443 }
3444
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003445 if (il->frames_count) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02003446 IL_WARN("%d frames still in use. Did we lose one?\n",
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003447 il->frames_count);
3448 il->frames_count = 0;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003449 }
3450}
3451
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003452static struct il_frame *il4965_get_free_frame(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003453{
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003454 struct il_frame *frame;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003455 struct list_head *element;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003456 if (list_empty(&il->free_frames)) {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003457 frame = kzalloc(sizeof(*frame), GFP_KERNEL);
3458 if (!frame) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02003459 IL_ERR("Could not allocate frame!\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003460 return NULL;
3461 }
3462
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003463 il->frames_count++;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003464 return frame;
3465 }
3466
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003467 element = il->free_frames.next;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003468 list_del(element);
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003469 return list_entry(element, struct il_frame, list);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003470}
3471
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003472static void il4965_free_frame(struct il_priv *il, struct il_frame *frame)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003473{
3474 memset(frame, 0, sizeof(*frame));
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003475 list_add(&frame->list, &il->free_frames);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003476}
3477
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003478static u32 il4965_fill_beacon_frame(struct il_priv *il,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003479 struct ieee80211_hdr *hdr,
3480 int left)
3481{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003482 lockdep_assert_held(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003483
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003484 if (!il->beacon_skb)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003485 return 0;
3486
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003487 if (il->beacon_skb->len > left)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003488 return 0;
3489
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003490 memcpy(hdr, il->beacon_skb->data, il->beacon_skb->len);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003491
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003492 return il->beacon_skb->len;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003493}
3494
3495/* Parse the beacon frame to find the TIM element and set tim_idx & tim_size */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003496static void il4965_set_beacon_tim(struct il_priv *il,
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003497 struct il_tx_beacon_cmd *tx_beacon_cmd,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003498 u8 *beacon, u32 frame_size)
3499{
3500 u16 tim_idx;
3501 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)beacon;
3502
3503 /*
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +01003504 * The idx is relative to frame start but we start looking at the
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003505 * variable-length part of the beacon.
3506 */
3507 tim_idx = mgmt->u.beacon.variable - beacon;
3508
3509 /* Parse variable-length elements of beacon to find WLAN_EID_TIM */
3510 while ((tim_idx < (frame_size - 2)) &&
3511 (beacon[tim_idx] != WLAN_EID_TIM))
3512 tim_idx += beacon[tim_idx+1] + 2;
3513
3514 /* If TIM field was found, set variables */
3515 if ((tim_idx < (frame_size - 1)) && (beacon[tim_idx] == WLAN_EID_TIM)) {
3516 tx_beacon_cmd->tim_idx = cpu_to_le16(tim_idx);
3517 tx_beacon_cmd->tim_size = beacon[tim_idx+1];
3518 } else
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02003519 IL_WARN("Unable to find TIM Element in beacon\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003520}
3521
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003522static unsigned int il4965_hw_get_beacon_cmd(struct il_priv *il,
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003523 struct il_frame *frame)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003524{
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003525 struct il_tx_beacon_cmd *tx_beacon_cmd;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003526 u32 frame_size;
3527 u32 rate_flags;
3528 u32 rate;
3529 /*
3530 * We have to set up the TX command, the TX Beacon command, and the
3531 * beacon contents.
3532 */
3533
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003534 lockdep_assert_held(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003535
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003536 if (!il->beacon_ctx) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02003537 IL_ERR("trying to build beacon w/o beacon context!\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003538 return 0;
3539 }
3540
3541 /* Initialize memory */
3542 tx_beacon_cmd = &frame->u.beacon;
3543 memset(tx_beacon_cmd, 0, sizeof(*tx_beacon_cmd));
3544
3545 /* Set up TX beacon contents */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003546 frame_size = il4965_fill_beacon_frame(il, tx_beacon_cmd->frame,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003547 sizeof(frame->u) - sizeof(*tx_beacon_cmd));
3548 if (WARN_ON_ONCE(frame_size > MAX_MPDU_SIZE))
3549 return 0;
3550 if (!frame_size)
3551 return 0;
3552
3553 /* Set up TX command fields */
3554 tx_beacon_cmd->tx.len = cpu_to_le16((u16)frame_size);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003555 tx_beacon_cmd->tx.sta_id = il->beacon_ctx->bcast_sta_id;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003556 tx_beacon_cmd->tx.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
3557 tx_beacon_cmd->tx.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK |
3558 TX_CMD_FLG_TSF_MSK | TX_CMD_FLG_STA_RATE_MSK;
3559
3560 /* Set up TX beacon command fields */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003561 il4965_set_beacon_tim(il, tx_beacon_cmd, (u8 *)tx_beacon_cmd->frame,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003562 frame_size);
3563
3564 /* Set up packet rate and flags */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003565 rate = il_get_lowest_plcp(il, il->beacon_ctx);
3566 il->mgmt_tx_ant = il4965_toggle_tx_ant(il, il->mgmt_tx_ant,
3567 il->hw_params.valid_tx_ant);
3568 rate_flags = il4965_ant_idx_to_flags(il->mgmt_tx_ant);
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003569 if ((rate >= IL_FIRST_CCK_RATE) && (rate <= IL_LAST_CCK_RATE))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003570 rate_flags |= RATE_MCS_CCK_MSK;
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003571 tx_beacon_cmd->tx.rate_n_flags = il4965_hw_set_rate_n_flags(rate,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003572 rate_flags);
3573
3574 return sizeof(*tx_beacon_cmd) + frame_size;
3575}
3576
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003577int il4965_send_beacon_cmd(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003578{
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003579 struct il_frame *frame;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003580 unsigned int frame_size;
3581 int rc;
3582
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003583 frame = il4965_get_free_frame(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003584 if (!frame) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02003585 IL_ERR("Could not obtain free frame buffer for beacon "
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003586 "command.\n");
3587 return -ENOMEM;
3588 }
3589
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003590 frame_size = il4965_hw_get_beacon_cmd(il, frame);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003591 if (!frame_size) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02003592 IL_ERR("Error configuring the beacon command\n");
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003593 il4965_free_frame(il, frame);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003594 return -EINVAL;
3595 }
3596
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003597 rc = il_send_cmd_pdu(il, REPLY_TX_BEACON, frame_size,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003598 &frame->u.cmd[0]);
3599
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003600 il4965_free_frame(il, frame);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003601
3602 return rc;
3603}
3604
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003605static inline dma_addr_t il4965_tfd_tb_get_addr(struct il_tfd *tfd, u8 idx)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003606{
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003607 struct il_tfd_tb *tb = &tfd->tbs[idx];
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003608
3609 dma_addr_t addr = get_unaligned_le32(&tb->lo);
3610 if (sizeof(dma_addr_t) > sizeof(u32))
3611 addr |=
3612 ((dma_addr_t)(le16_to_cpu(tb->hi_n_len) & 0xF) << 16) << 16;
3613
3614 return addr;
3615}
3616
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003617static inline u16 il4965_tfd_tb_get_len(struct il_tfd *tfd, u8 idx)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003618{
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003619 struct il_tfd_tb *tb = &tfd->tbs[idx];
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003620
3621 return le16_to_cpu(tb->hi_n_len) >> 4;
3622}
3623
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003624static inline void il4965_tfd_set_tb(struct il_tfd *tfd, u8 idx,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003625 dma_addr_t addr, u16 len)
3626{
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003627 struct il_tfd_tb *tb = &tfd->tbs[idx];
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003628 u16 hi_n_len = len << 4;
3629
3630 put_unaligned_le32(addr, &tb->lo);
3631 if (sizeof(dma_addr_t) > sizeof(u32))
3632 hi_n_len |= ((addr >> 16) >> 16) & 0xF;
3633
3634 tb->hi_n_len = cpu_to_le16(hi_n_len);
3635
3636 tfd->num_tbs = idx + 1;
3637}
3638
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003639static inline u8 il4965_tfd_get_num_tbs(struct il_tfd *tfd)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003640{
3641 return tfd->num_tbs & 0x1f;
3642}
3643
3644/**
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003645 * il4965_hw_txq_free_tfd - Free all chunks referenced by TFD [txq->q.read_ptr]
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003646 * @il - driver ilate data
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003647 * @txq - tx queue
3648 *
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +01003649 * Does NOT advance any TFD circular buffer read/write idxes
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003650 * Does NOT free the TFD itself (which is within circular buffer)
3651 */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003652void il4965_hw_txq_free_tfd(struct il_priv *il, struct il_tx_queue *txq)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003653{
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003654 struct il_tfd *tfd_tmp = (struct il_tfd *)txq->tfds;
3655 struct il_tfd *tfd;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003656 struct pci_dev *dev = il->pci_dev;
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +01003657 int idx = txq->q.read_ptr;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003658 int i;
3659 int num_tbs;
3660
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +01003661 tfd = &tfd_tmp[idx];
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003662
3663 /* Sanity check on number of chunks */
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003664 num_tbs = il4965_tfd_get_num_tbs(tfd);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003665
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003666 if (num_tbs >= IL_NUM_OF_TBS) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02003667 IL_ERR("Too many chunks: %i\n", num_tbs);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003668 /* @todo issue fatal error, it is quite serious situation */
3669 return;
3670 }
3671
3672 /* Unmap tx_cmd */
3673 if (num_tbs)
3674 pci_unmap_single(dev,
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +01003675 dma_unmap_addr(&txq->meta[idx], mapping),
3676 dma_unmap_len(&txq->meta[idx], len),
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003677 PCI_DMA_BIDIRECTIONAL);
3678
3679 /* Unmap chunks, if any. */
3680 for (i = 1; i < num_tbs; i++)
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003681 pci_unmap_single(dev, il4965_tfd_tb_get_addr(tfd, i),
3682 il4965_tfd_tb_get_len(tfd, i),
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003683 PCI_DMA_TODEVICE);
3684
3685 /* free SKB */
3686 if (txq->txb) {
3687 struct sk_buff *skb;
3688
3689 skb = txq->txb[txq->q.read_ptr].skb;
3690
3691 /* can be called from irqs-disabled context */
3692 if (skb) {
3693 dev_kfree_skb_any(skb);
3694 txq->txb[txq->q.read_ptr].skb = NULL;
3695 }
3696 }
3697}
3698
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003699int il4965_hw_txq_attach_buf_to_tfd(struct il_priv *il,
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003700 struct il_tx_queue *txq,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003701 dma_addr_t addr, u16 len,
3702 u8 reset, u8 pad)
3703{
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003704 struct il_queue *q;
3705 struct il_tfd *tfd, *tfd_tmp;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003706 u32 num_tbs;
3707
3708 q = &txq->q;
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003709 tfd_tmp = (struct il_tfd *)txq->tfds;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003710 tfd = &tfd_tmp[q->write_ptr];
3711
3712 if (reset)
3713 memset(tfd, 0, sizeof(*tfd));
3714
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003715 num_tbs = il4965_tfd_get_num_tbs(tfd);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003716
3717 /* Each TFD can point to a maximum 20 Tx buffers */
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003718 if (num_tbs >= IL_NUM_OF_TBS) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02003719 IL_ERR("Error can not send more than %d chunks\n",
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003720 IL_NUM_OF_TBS);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003721 return -EINVAL;
3722 }
3723
3724 BUG_ON(addr & ~DMA_BIT_MASK(36));
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003725 if (unlikely(addr & ~IL_TX_DMA_MASK))
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02003726 IL_ERR("Unaligned address = %llx\n",
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003727 (unsigned long long)addr);
3728
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003729 il4965_tfd_set_tb(tfd, num_tbs, addr, len);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003730
3731 return 0;
3732}
3733
3734/*
3735 * Tell nic where to find circular buffer of Tx Frame Descriptors for
3736 * given Tx queue, and enable the DMA channel used for that queue.
3737 *
3738 * 4965 supports up to 16 Tx queues in DRAM, mapped to up to 8 Tx DMA
3739 * channels supported in hardware.
3740 */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003741int il4965_hw_tx_queue_init(struct il_priv *il,
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003742 struct il_tx_queue *txq)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003743{
3744 int txq_id = txq->q.id;
3745
3746 /* Circular buffer (TFD queue in DRAM) physical base address */
Stanislaw Gruszka0c1a94e2011-08-24 17:37:16 +02003747 il_wr(il, FH_MEM_CBBC_QUEUE(txq_id),
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003748 txq->q.dma_addr >> 8);
3749
3750 return 0;
3751}
3752
3753/******************************************************************************
3754 *
3755 * Generic RX handler implementations
3756 *
3757 ******************************************************************************/
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003758static void il4965_rx_reply_alive(struct il_priv *il,
Stanislaw Gruszkab73bb5f2011-08-26 14:37:54 +02003759 struct il_rx_buf *rxb)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003760{
Stanislaw Gruszkadcae1c62011-08-26 14:36:21 +02003761 struct il_rx_pkt *pkt = rxb_addr(rxb);
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003762 struct il_alive_resp *palive;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003763 struct delayed_work *pwork;
3764
3765 palive = &pkt->u.alive_frame;
3766
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01003767 D_INFO("Alive ucode status 0x%08X revision "
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003768 "0x%01X 0x%01X\n",
3769 palive->is_valid, palive->ver_type,
3770 palive->ver_subtype);
3771
3772 if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01003773 D_INFO("Initialization Alive received.\n");
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003774 memcpy(&il->card_alive_init,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003775 &pkt->u.alive_frame,
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003776 sizeof(struct il_init_alive_resp));
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003777 pwork = &il->init_alive_start;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003778 } else {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01003779 D_INFO("Runtime Alive received.\n");
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003780 memcpy(&il->card_alive, &pkt->u.alive_frame,
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003781 sizeof(struct il_alive_resp));
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003782 pwork = &il->alive_start;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003783 }
3784
3785 /* We delay the ALIVE response by 5ms to
3786 * give the HW RF Kill time to activate... */
3787 if (palive->is_valid == UCODE_VALID_OK)
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003788 queue_delayed_work(il->workqueue, pwork,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003789 msecs_to_jiffies(5));
3790 else
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02003791 IL_WARN("uCode did not respond OK.\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003792}
3793
3794/**
Stanislaw Gruszkaebf0d902011-08-26 15:43:47 +02003795 * il4965_bg_stats_periodic - Timer callback to queue stats
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003796 *
Stanislaw Gruszkaebf0d902011-08-26 15:43:47 +02003797 * This callback is provided in order to send a stats request.
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003798 *
3799 * This timer function is continually reset to execute within
3800 * REG_RECALIB_PERIOD seconds since the last STATISTICS_NOTIFICATION
Stanislaw Gruszkaebf0d902011-08-26 15:43:47 +02003801 * was received. We need to ensure we receive the stats in order
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003802 * to update the temperature used for calibrating the TXPOWER.
3803 */
Stanislaw Gruszkaebf0d902011-08-26 15:43:47 +02003804static void il4965_bg_stats_periodic(unsigned long data)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003805{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003806 struct il_priv *il = (struct il_priv *)data;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003807
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003808 if (test_bit(STATUS_EXIT_PENDING, &il->status))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003809 return;
3810
3811 /* dont send host command if rf-kill is on */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003812 if (!il_is_ready_rf(il))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003813 return;
3814
Stanislaw Gruszkaebf0d902011-08-26 15:43:47 +02003815 il_send_stats_request(il, CMD_ASYNC, false);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003816}
3817
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003818static void il4965_rx_beacon_notif(struct il_priv *il,
Stanislaw Gruszkab73bb5f2011-08-26 14:37:54 +02003819 struct il_rx_buf *rxb)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003820{
Stanislaw Gruszkadcae1c62011-08-26 14:36:21 +02003821 struct il_rx_pkt *pkt = rxb_addr(rxb);
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003822 struct il4965_beacon_notif *beacon =
3823 (struct il4965_beacon_notif *)pkt->u.raw;
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01003824#ifdef CONFIG_IWLEGACY_DEBUG
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003825 u8 rate = il4965_hw_get_rate(beacon->beacon_notify_hdr.rate_n_flags);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003826
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01003827 D_RX("beacon status %x retries %d iss %d "
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003828 "tsf %d %d rate %d\n",
3829 le32_to_cpu(beacon->beacon_notify_hdr.u.status) & TX_STATUS_MSK,
3830 beacon->beacon_notify_hdr.failure_frame,
3831 le32_to_cpu(beacon->ibss_mgr_status),
3832 le32_to_cpu(beacon->high_tsf),
3833 le32_to_cpu(beacon->low_tsf), rate);
3834#endif
3835
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003836 il->ibss_manager = le32_to_cpu(beacon->ibss_mgr_status);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003837}
3838
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003839static void il4965_perform_ct_kill_task(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003840{
3841 unsigned long flags;
3842
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01003843 D_POWER("Stop all queues\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003844
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003845 if (il->mac80211_registered)
3846 ieee80211_stop_queues(il->hw);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003847
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02003848 _il_wr(il, CSR_UCODE_DRV_GP1_SET,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003849 CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02003850 _il_rd(il, CSR_UCODE_DRV_GP1);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003851
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003852 spin_lock_irqsave(&il->reg_lock, flags);
Stanislaw Gruszka13882262011-08-24 15:39:23 +02003853 if (!_il_grab_nic_access(il))
3854 _il_release_nic_access(il);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003855 spin_unlock_irqrestore(&il->reg_lock, flags);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003856}
3857
3858/* Handle notification from uCode that card's power state is changing
3859 * due to software, hardware, or critical temperature RFKILL */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003860static void il4965_rx_card_state_notif(struct il_priv *il,
Stanislaw Gruszkab73bb5f2011-08-26 14:37:54 +02003861 struct il_rx_buf *rxb)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003862{
Stanislaw Gruszkadcae1c62011-08-26 14:36:21 +02003863 struct il_rx_pkt *pkt = rxb_addr(rxb);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003864 u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003865 unsigned long status = il->status;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003866
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01003867 D_RF_KILL("Card state received: HW:%s SW:%s CT:%s\n",
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003868 (flags & HW_CARD_DISABLED) ? "Kill" : "On",
3869 (flags & SW_CARD_DISABLED) ? "Kill" : "On",
3870 (flags & CT_CARD_DISABLED) ?
3871 "Reached" : "Not reached");
3872
3873 if (flags & (SW_CARD_DISABLED | HW_CARD_DISABLED |
3874 CT_CARD_DISABLED)) {
3875
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02003876 _il_wr(il, CSR_UCODE_DRV_GP1_SET,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003877 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
3878
Stanislaw Gruszka0c1a94e2011-08-24 17:37:16 +02003879 il_wr(il, HBUS_TARG_MBX_C,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003880 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
3881
3882 if (!(flags & RXON_CARD_DISABLED)) {
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02003883 _il_wr(il, CSR_UCODE_DRV_GP1_CLR,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003884 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
Stanislaw Gruszka0c1a94e2011-08-24 17:37:16 +02003885 il_wr(il, HBUS_TARG_MBX_C,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003886 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
3887 }
3888 }
3889
3890 if (flags & CT_CARD_DISABLED)
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003891 il4965_perform_ct_kill_task(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003892
3893 if (flags & HW_CARD_DISABLED)
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003894 set_bit(STATUS_RF_KILL_HW, &il->status);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003895 else
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003896 clear_bit(STATUS_RF_KILL_HW, &il->status);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003897
3898 if (!(flags & RXON_CARD_DISABLED))
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003899 il_scan_cancel(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003900
3901 if ((test_bit(STATUS_RF_KILL_HW, &status) !=
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003902 test_bit(STATUS_RF_KILL_HW, &il->status)))
3903 wiphy_rfkill_set_hw_state(il->hw->wiphy,
3904 test_bit(STATUS_RF_KILL_HW, &il->status));
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003905 else
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003906 wake_up(&il->wait_command_queue);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003907}
3908
3909/**
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003910 * il4965_setup_rx_handlers - Initialize Rx handler callbacks
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003911 *
3912 * Setup the RX handlers for each of the reply types sent from the uCode
3913 * to the host.
3914 *
3915 * This function chains into the hardware specific files for them to setup
3916 * any hardware specific handlers as well.
3917 */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003918static void il4965_setup_rx_handlers(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003919{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003920 il->rx_handlers[REPLY_ALIVE] = il4965_rx_reply_alive;
3921 il->rx_handlers[REPLY_ERROR] = il_rx_reply_error;
3922 il->rx_handlers[CHANNEL_SWITCH_NOTIFICATION] = il_rx_csa;
3923 il->rx_handlers[SPECTRUM_MEASURE_NOTIFICATION] =
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003924 il_rx_spectrum_measure_notif;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003925 il->rx_handlers[PM_SLEEP_NOTIFICATION] = il_rx_pm_sleep_notif;
3926 il->rx_handlers[PM_DEBUG_STATISTIC_NOTIFIC] =
Stanislaw Gruszkaebf0d902011-08-26 15:43:47 +02003927 il_rx_pm_debug_stats_notif;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003928 il->rx_handlers[BEACON_NOTIFICATION] = il4965_rx_beacon_notif;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003929
3930 /*
3931 * The same handler is used for both the REPLY to a discrete
Stanislaw Gruszkaebf0d902011-08-26 15:43:47 +02003932 * stats request from the host as well as for the periodic
3933 * stats notifications (after received beacons) from the uCode.
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003934 */
Stanislaw Gruszkaebf0d902011-08-26 15:43:47 +02003935 il->rx_handlers[REPLY_STATISTICS_CMD] = il4965_reply_stats;
3936 il->rx_handlers[STATISTICS_NOTIFICATION] = il4965_rx_stats;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003937
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003938 il_setup_rx_scan_handlers(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003939
3940 /* status change handler */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003941 il->rx_handlers[CARD_STATE_NOTIFICATION] =
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003942 il4965_rx_card_state_notif;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003943
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003944 il->rx_handlers[MISSED_BEACONS_NOTIFICATION] =
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003945 il4965_rx_missed_beacon_notif;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003946 /* Rx handlers */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003947 il->rx_handlers[REPLY_RX_PHY_CMD] = il4965_rx_reply_rx_phy;
3948 il->rx_handlers[REPLY_RX_MPDU_CMD] = il4965_rx_reply_rx;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003949 /* block ack */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003950 il->rx_handlers[REPLY_COMPRESSED_BA] = il4965_rx_reply_compressed_ba;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003951 /* Set up hardware specific Rx handlers */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003952 il->cfg->ops->lib->rx_handler_setup(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003953}
3954
3955/**
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003956 * il4965_rx_handle - Main entry function for receiving responses from uCode
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003957 *
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003958 * Uses the il->rx_handlers callback function array to invoke
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003959 * the appropriate handlers, including command responses,
3960 * frame-received notifications, and other notifications.
3961 */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003962void il4965_rx_handle(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003963{
Stanislaw Gruszkab73bb5f2011-08-26 14:37:54 +02003964 struct il_rx_buf *rxb;
Stanislaw Gruszkadcae1c62011-08-26 14:36:21 +02003965 struct il_rx_pkt *pkt;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003966 struct il_rx_queue *rxq = &il->rxq;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003967 u32 r, i;
3968 int reclaim;
3969 unsigned long flags;
3970 u8 fill_rx = 0;
3971 u32 count = 8;
3972 int total_empty;
3973
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +01003974 /* uCode's read idx (stored in shared DRAM) indicates the last Rx
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003975 * buffer that the driver may process (last buffer filled by ucode). */
3976 r = le16_to_cpu(rxq->rb_stts->closed_rb_num) & 0x0FFF;
3977 i = rxq->read;
3978
3979 /* Rx interrupt, but nothing sent from uCode */
3980 if (i == r)
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01003981 D_RX("r = %d, i = %d\n", r, i);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003982
3983 /* calculate total frames need to be restock after handling RX */
3984 total_empty = r - rxq->write_actual;
3985 if (total_empty < 0)
3986 total_empty += RX_QUEUE_SIZE;
3987
3988 if (total_empty > (RX_QUEUE_SIZE / 2))
3989 fill_rx = 1;
3990
3991 while (i != r) {
3992 int len;
3993
3994 rxb = rxq->queue[i];
3995
3996 /* If an RXB doesn't have a Rx queue slot associated with it,
3997 * then a bug has been introduced in the queue refilling
3998 * routines -- catch it here */
3999 BUG_ON(rxb == NULL);
4000
4001 rxq->queue[i] = NULL;
4002
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004003 pci_unmap_page(il->pci_dev, rxb->page_dma,
4004 PAGE_SIZE << il->hw_params.rx_page_order,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004005 PCI_DMA_FROMDEVICE);
4006 pkt = rxb_addr(rxb);
4007
4008 len = le32_to_cpu(pkt->len_n_flags) & FH_RSCSR_FRAME_SIZE_MSK;
4009 len += sizeof(u32); /* account for status word */
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004010
4011 /* Reclaim a command buffer only if this packet is a response
4012 * to a (driver-originated) command.
4013 * If the packet (e.g. Rx frame) originated from uCode,
4014 * there is no command buffer to reclaim.
4015 * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
4016 * but apparently a few don't get set; catch them here. */
4017 reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
4018 (pkt->hdr.cmd != REPLY_RX_PHY_CMD) &&
4019 (pkt->hdr.cmd != REPLY_RX) &&
4020 (pkt->hdr.cmd != REPLY_RX_MPDU_CMD) &&
4021 (pkt->hdr.cmd != REPLY_COMPRESSED_BA) &&
4022 (pkt->hdr.cmd != STATISTICS_NOTIFICATION) &&
4023 (pkt->hdr.cmd != REPLY_TX);
4024
4025 /* Based on type of command response or notification,
4026 * handle those that need handling via function in
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004027 * rx_handlers table. See il4965_setup_rx_handlers() */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004028 if (il->rx_handlers[pkt->hdr.cmd]) {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01004029 D_RX("r = %d, i = %d, %s, 0x%02x\n", r,
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004030 i, il_get_cmd_string(pkt->hdr.cmd),
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004031 pkt->hdr.cmd);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004032 il->isr_stats.rx_handlers[pkt->hdr.cmd]++;
4033 il->rx_handlers[pkt->hdr.cmd] (il, rxb);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004034 } else {
4035 /* No handling needed */
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01004036 D_RX(
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004037 "r %d i %d No handler needed for %s, 0x%02x\n",
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004038 r, i, il_get_cmd_string(pkt->hdr.cmd),
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004039 pkt->hdr.cmd);
4040 }
4041
4042 /*
4043 * XXX: After here, we should always check rxb->page
4044 * against NULL before touching it or its virtual
4045 * memory (pkt). Because some rx_handler might have
4046 * already taken or freed the pages.
4047 */
4048
4049 if (reclaim) {
4050 /* Invoke any callbacks, transfer the buffer to caller,
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004051 * and fire off the (possibly) blocking il_send_cmd()
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004052 * as we reclaim the driver command queue */
4053 if (rxb->page)
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004054 il_tx_cmd_complete(il, rxb);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004055 else
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004056 IL_WARN("Claim null rxb?\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004057 }
4058
4059 /* Reuse the page if possible. For notification packets and
4060 * SKBs that fail to Rx correctly, add them back into the
4061 * rx_free list for reuse later. */
4062 spin_lock_irqsave(&rxq->lock, flags);
4063 if (rxb->page != NULL) {
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004064 rxb->page_dma = pci_map_page(il->pci_dev, rxb->page,
4065 0, PAGE_SIZE << il->hw_params.rx_page_order,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004066 PCI_DMA_FROMDEVICE);
4067 list_add_tail(&rxb->list, &rxq->rx_free);
4068 rxq->free_count++;
4069 } else
4070 list_add_tail(&rxb->list, &rxq->rx_used);
4071
4072 spin_unlock_irqrestore(&rxq->lock, flags);
4073
4074 i = (i + 1) & RX_QUEUE_MASK;
4075 /* If there are a lot of unused frames,
4076 * restock the Rx queue so ucode wont assert. */
4077 if (fill_rx) {
4078 count++;
4079 if (count >= 8) {
4080 rxq->read = i;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004081 il4965_rx_replenish_now(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004082 count = 0;
4083 }
4084 }
4085 }
4086
4087 /* Backtrack one entry */
4088 rxq->read = i;
4089 if (fill_rx)
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004090 il4965_rx_replenish_now(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004091 else
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004092 il4965_rx_queue_restock(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004093}
4094
4095/* call this function to flush any scheduled tasklet */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004096static inline void il4965_synchronize_irq(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004097{
4098 /* wait to make sure we flush pending tasklet*/
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004099 synchronize_irq(il->pci_dev->irq);
4100 tasklet_kill(&il->irq_tasklet);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004101}
4102
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004103static void il4965_irq_tasklet(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004104{
4105 u32 inta, handled = 0;
4106 u32 inta_fh;
4107 unsigned long flags;
4108 u32 i;
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01004109#ifdef CONFIG_IWLEGACY_DEBUG
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004110 u32 inta_mask;
4111#endif
4112
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004113 spin_lock_irqsave(&il->lock, flags);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004114
4115 /* Ack/clear/reset pending uCode interrupts.
4116 * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
4117 * and will clear only when CSR_FH_INT_STATUS gets cleared. */
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02004118 inta = _il_rd(il, CSR_INT);
4119 _il_wr(il, CSR_INT, inta);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004120
4121 /* Ack/clear/reset pending flow-handler (DMA) interrupts.
4122 * Any new interrupts that happen after this, either while we're
4123 * in this tasklet, or later, will show up in next ISR/tasklet. */
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02004124 inta_fh = _il_rd(il, CSR_FH_INT_STATUS);
4125 _il_wr(il, CSR_FH_INT_STATUS, inta_fh);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004126
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01004127#ifdef CONFIG_IWLEGACY_DEBUG
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004128 if (il_get_debug_level(il) & IL_DL_ISR) {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004129 /* just for debug */
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02004130 inta_mask = _il_rd(il, CSR_INT_MASK);
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01004131 D_ISR("inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004132 inta, inta_mask, inta_fh);
4133 }
4134#endif
4135
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004136 spin_unlock_irqrestore(&il->lock, flags);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004137
4138 /* Since CSR_INT and CSR_FH_INT_STATUS reads and clears are not
4139 * atomic, make sure that inta covers all the interrupts that
4140 * we've discovered, even if FH interrupt came in just after
4141 * reading CSR_INT. */
4142 if (inta_fh & CSR49_FH_INT_RX_MASK)
4143 inta |= CSR_INT_BIT_FH_RX;
4144 if (inta_fh & CSR49_FH_INT_TX_MASK)
4145 inta |= CSR_INT_BIT_FH_TX;
4146
4147 /* Now service all interrupt bits discovered above. */
4148 if (inta & CSR_INT_BIT_HW_ERR) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004149 IL_ERR("Hardware error detected. Restarting.\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004150
4151 /* Tell the device to stop sending interrupts */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004152 il_disable_interrupts(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004153
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004154 il->isr_stats.hw++;
4155 il_irq_handle_error(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004156
4157 handled |= CSR_INT_BIT_HW_ERR;
4158
4159 return;
4160 }
4161
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01004162#ifdef CONFIG_IWLEGACY_DEBUG
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004163 if (il_get_debug_level(il) & (IL_DL_ISR)) {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004164 /* NIC fires this, but we don't use it, redundant with WAKEUP */
4165 if (inta & CSR_INT_BIT_SCD) {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01004166 D_ISR("Scheduler finished to transmit "
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004167 "the frame/frames.\n");
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004168 il->isr_stats.sch++;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004169 }
4170
4171 /* Alive notification via Rx interrupt will do the real work */
4172 if (inta & CSR_INT_BIT_ALIVE) {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01004173 D_ISR("Alive interrupt\n");
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004174 il->isr_stats.alive++;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004175 }
4176 }
4177#endif
4178 /* Safely ignore these bits for debug checks below */
4179 inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
4180
4181 /* HW RF KILL switch toggled */
4182 if (inta & CSR_INT_BIT_RF_KILL) {
4183 int hw_rf_kill = 0;
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02004184 if (!(_il_rd(il, CSR_GP_CNTRL) &
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004185 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
4186 hw_rf_kill = 1;
4187
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004188 IL_WARN("RF_KILL bit toggled to %s.\n",
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004189 hw_rf_kill ? "disable radio" : "enable radio");
4190
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004191 il->isr_stats.rfkill++;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004192
4193 /* driver only loads ucode once setting the interface up.
4194 * the driver allows loading the ucode even if the radio
4195 * is killed. Hence update the killswitch state here. The
4196 * rfkill handler will care about restarting if needed.
4197 */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004198 if (!test_bit(STATUS_ALIVE, &il->status)) {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004199 if (hw_rf_kill)
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004200 set_bit(STATUS_RF_KILL_HW, &il->status);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004201 else
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004202 clear_bit(STATUS_RF_KILL_HW, &il->status);
4203 wiphy_rfkill_set_hw_state(il->hw->wiphy, hw_rf_kill);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004204 }
4205
4206 handled |= CSR_INT_BIT_RF_KILL;
4207 }
4208
4209 /* Chip got too hot and stopped itself */
4210 if (inta & CSR_INT_BIT_CT_KILL) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004211 IL_ERR("Microcode CT kill error detected.\n");
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004212 il->isr_stats.ctkill++;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004213 handled |= CSR_INT_BIT_CT_KILL;
4214 }
4215
4216 /* Error detected by uCode */
4217 if (inta & CSR_INT_BIT_SW_ERR) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004218 IL_ERR("Microcode SW error detected. "
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004219 " Restarting 0x%X.\n", inta);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004220 il->isr_stats.sw++;
4221 il_irq_handle_error(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004222 handled |= CSR_INT_BIT_SW_ERR;
4223 }
4224
4225 /*
4226 * uCode wakes up after power-down sleep.
4227 * Tell device about any new tx or host commands enqueued,
4228 * and about any Rx buffers made available while asleep.
4229 */
4230 if (inta & CSR_INT_BIT_WAKEUP) {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01004231 D_ISR("Wakeup interrupt\n");
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004232 il_rx_queue_update_write_ptr(il, &il->rxq);
4233 for (i = 0; i < il->hw_params.max_txq_num; i++)
4234 il_txq_update_write_ptr(il, &il->txq[i]);
4235 il->isr_stats.wakeup++;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004236 handled |= CSR_INT_BIT_WAKEUP;
4237 }
4238
4239 /* All uCode command responses, including Tx command responses,
4240 * Rx "responses" (frame-received notification), and other
4241 * notifications from uCode come through here*/
4242 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004243 il4965_rx_handle(il);
4244 il->isr_stats.rx++;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004245 handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
4246 }
4247
4248 /* This "Tx" DMA channel is used only for loading uCode */
4249 if (inta & CSR_INT_BIT_FH_TX) {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01004250 D_ISR("uCode load interrupt\n");
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004251 il->isr_stats.tx++;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004252 handled |= CSR_INT_BIT_FH_TX;
4253 /* Wake up uCode load routine, now that load is complete */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004254 il->ucode_write_complete = 1;
4255 wake_up(&il->wait_command_queue);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004256 }
4257
4258 if (inta & ~handled) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004259 IL_ERR("Unhandled INTA bits 0x%08x\n", inta & ~handled);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004260 il->isr_stats.unhandled++;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004261 }
4262
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004263 if (inta & ~(il->inta_mask)) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004264 IL_WARN("Disabled INTA bits 0x%08x were pending\n",
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004265 inta & ~il->inta_mask);
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004266 IL_WARN(" with FH_INT = 0x%08x\n", inta_fh);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004267 }
4268
4269 /* Re-enable all interrupts */
Stanislaw Gruszka93fd74e2011-04-28 11:51:30 +02004270 /* only Re-enable if disabled by irq */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004271 if (test_bit(STATUS_INT_ENABLED, &il->status))
4272 il_enable_interrupts(il);
Stanislaw Gruszkaa078a1f2011-04-28 11:51:25 +02004273 /* Re-enable RF_KILL if it occurred */
4274 else if (handled & CSR_INT_BIT_RF_KILL)
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004275 il_enable_rfkill_int(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004276
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01004277#ifdef CONFIG_IWLEGACY_DEBUG
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004278 if (il_get_debug_level(il) & (IL_DL_ISR)) {
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02004279 inta = _il_rd(il, CSR_INT);
4280 inta_mask = _il_rd(il, CSR_INT_MASK);
4281 inta_fh = _il_rd(il, CSR_FH_INT_STATUS);
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01004282 D_ISR(
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004283 "End inta 0x%08x, enabled 0x%08x, fh 0x%08x, "
4284 "flags 0x%08lx\n", inta, inta_mask, inta_fh, flags);
4285 }
4286#endif
4287}
4288
4289/*****************************************************************************
4290 *
4291 * sysfs attributes
4292 *
4293 *****************************************************************************/
4294
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01004295#ifdef CONFIG_IWLEGACY_DEBUG
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004296
4297/*
4298 * The following adds a new attribute to the sysfs representation
4299 * of this device driver (i.e. a new file in /sys/class/net/wlan0/device/)
4300 * used for controlling the debug level.
4301 *
4302 * See the level definitions in iwl for details.
4303 *
4304 * The debug_level being managed using sysfs below is a per device debug
4305 * level that is used instead of the global debug level if it (the per
4306 * device debug level) is set.
4307 */
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004308static ssize_t il4965_show_debug_level(struct device *d,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004309 struct device_attribute *attr, char *buf)
4310{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004311 struct il_priv *il = dev_get_drvdata(d);
4312 return sprintf(buf, "0x%08X\n", il_get_debug_level(il));
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004313}
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004314static ssize_t il4965_store_debug_level(struct device *d,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004315 struct device_attribute *attr,
4316 const char *buf, size_t count)
4317{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004318 struct il_priv *il = dev_get_drvdata(d);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004319 unsigned long val;
4320 int ret;
4321
4322 ret = strict_strtoul(buf, 0, &val);
4323 if (ret)
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004324 IL_ERR("%s is not in hex or decimal form.\n", buf);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004325 else {
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004326 il->debug_level = val;
4327 if (il_alloc_traffic_mem(il))
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004328 IL_ERR(
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004329 "Not enough memory to generate traffic log\n");
4330 }
4331 return strnlen(buf, count);
4332}
4333
4334static DEVICE_ATTR(debug_level, S_IWUSR | S_IRUGO,
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004335 il4965_show_debug_level, il4965_store_debug_level);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004336
4337
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01004338#endif /* CONFIG_IWLEGACY_DEBUG */
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004339
4340
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004341static ssize_t il4965_show_temperature(struct device *d,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004342 struct device_attribute *attr, char *buf)
4343{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004344 struct il_priv *il = dev_get_drvdata(d);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004345
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004346 if (!il_is_alive(il))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004347 return -EAGAIN;
4348
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004349 return sprintf(buf, "%d\n", il->temperature);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004350}
4351
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004352static DEVICE_ATTR(temperature, S_IRUGO, il4965_show_temperature, NULL);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004353
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004354static ssize_t il4965_show_tx_power(struct device *d,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004355 struct device_attribute *attr, char *buf)
4356{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004357 struct il_priv *il = dev_get_drvdata(d);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004358
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004359 if (!il_is_ready_rf(il))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004360 return sprintf(buf, "off\n");
4361 else
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004362 return sprintf(buf, "%d\n", il->tx_power_user_lmt);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004363}
4364
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004365static ssize_t il4965_store_tx_power(struct device *d,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004366 struct device_attribute *attr,
4367 const char *buf, size_t count)
4368{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004369 struct il_priv *il = dev_get_drvdata(d);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004370 unsigned long val;
4371 int ret;
4372
4373 ret = strict_strtoul(buf, 10, &val);
4374 if (ret)
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004375 IL_INFO("%s is not in decimal form.\n", buf);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004376 else {
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004377 ret = il_set_tx_power(il, val, false);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004378 if (ret)
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004379 IL_ERR("failed setting tx power (0x%d).\n",
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004380 ret);
4381 else
4382 ret = count;
4383 }
4384 return ret;
4385}
4386
4387static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO,
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004388 il4965_show_tx_power, il4965_store_tx_power);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004389
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004390static struct attribute *il_sysfs_entries[] = {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004391 &dev_attr_temperature.attr,
4392 &dev_attr_tx_power.attr,
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01004393#ifdef CONFIG_IWLEGACY_DEBUG
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004394 &dev_attr_debug_level.attr,
4395#endif
4396 NULL
4397};
4398
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004399static struct attribute_group il_attribute_group = {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004400 .name = NULL, /* put in device directory */
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004401 .attrs = il_sysfs_entries,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004402};
4403
4404/******************************************************************************
4405 *
4406 * uCode download functions
4407 *
4408 ******************************************************************************/
4409
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004410static void il4965_dealloc_ucode_pci(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004411{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004412 il_free_fw_desc(il->pci_dev, &il->ucode_code);
4413 il_free_fw_desc(il->pci_dev, &il->ucode_data);
4414 il_free_fw_desc(il->pci_dev, &il->ucode_data_backup);
4415 il_free_fw_desc(il->pci_dev, &il->ucode_init);
4416 il_free_fw_desc(il->pci_dev, &il->ucode_init_data);
4417 il_free_fw_desc(il->pci_dev, &il->ucode_boot);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004418}
4419
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004420static void il4965_nic_start(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004421{
4422 /* Remove all resets to allow NIC to operate */
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02004423 _il_wr(il, CSR_RESET, 0);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004424}
4425
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004426static void il4965_ucode_callback(const struct firmware *ucode_raw,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004427 void *context);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004428static int il4965_mac_setup_register(struct il_priv *il,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004429 u32 max_probe_length);
4430
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004431static int __must_check il4965_request_firmware(struct il_priv *il, bool first)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004432{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004433 const char *name_pre = il->cfg->fw_name_pre;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004434 char tag[8];
4435
4436 if (first) {
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +01004437 il->fw_idx = il->cfg->ucode_api_max;
4438 sprintf(tag, "%d", il->fw_idx);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004439 } else {
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +01004440 il->fw_idx--;
4441 sprintf(tag, "%d", il->fw_idx);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004442 }
4443
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +01004444 if (il->fw_idx < il->cfg->ucode_api_min) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004445 IL_ERR("no suitable firmware found!\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004446 return -ENOENT;
4447 }
4448
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004449 sprintf(il->firmware_name, "%s%s%s", name_pre, tag, ".ucode");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004450
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01004451 D_INFO("attempting to load firmware '%s'\n",
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004452 il->firmware_name);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004453
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004454 return request_firmware_nowait(THIS_MODULE, 1, il->firmware_name,
4455 &il->pci_dev->dev, GFP_KERNEL, il,
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004456 il4965_ucode_callback);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004457}
4458
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004459struct il4965_firmware_pieces {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004460 const void *inst, *data, *init, *init_data, *boot;
4461 size_t inst_size, data_size, init_size, init_data_size, boot_size;
4462};
4463
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004464static int il4965_load_firmware(struct il_priv *il,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004465 const struct firmware *ucode_raw,
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004466 struct il4965_firmware_pieces *pieces)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004467{
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004468 struct il_ucode_header *ucode = (void *)ucode_raw->data;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004469 u32 api_ver, hdr_size;
4470 const u8 *src;
4471
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004472 il->ucode_ver = le32_to_cpu(ucode->ver);
4473 api_ver = IL_UCODE_API(il->ucode_ver);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004474
4475 switch (api_ver) {
4476 default:
4477 case 0:
4478 case 1:
4479 case 2:
4480 hdr_size = 24;
4481 if (ucode_raw->size < hdr_size) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004482 IL_ERR("File size too small!\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004483 return -EINVAL;
4484 }
4485 pieces->inst_size = le32_to_cpu(ucode->v1.inst_size);
4486 pieces->data_size = le32_to_cpu(ucode->v1.data_size);
4487 pieces->init_size = le32_to_cpu(ucode->v1.init_size);
4488 pieces->init_data_size =
4489 le32_to_cpu(ucode->v1.init_data_size);
4490 pieces->boot_size = le32_to_cpu(ucode->v1.boot_size);
4491 src = ucode->v1.data;
4492 break;
4493 }
4494
4495 /* Verify size of file vs. image size info in file's header */
4496 if (ucode_raw->size != hdr_size + pieces->inst_size +
4497 pieces->data_size + pieces->init_size +
4498 pieces->init_data_size + pieces->boot_size) {
4499
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004500 IL_ERR(
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004501 "uCode file size %d does not match expected size\n",
4502 (int)ucode_raw->size);
4503 return -EINVAL;
4504 }
4505
4506 pieces->inst = src;
4507 src += pieces->inst_size;
4508 pieces->data = src;
4509 src += pieces->data_size;
4510 pieces->init = src;
4511 src += pieces->init_size;
4512 pieces->init_data = src;
4513 src += pieces->init_data_size;
4514 pieces->boot = src;
4515 src += pieces->boot_size;
4516
4517 return 0;
4518}
4519
4520/**
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004521 * il4965_ucode_callback - callback when firmware was loaded
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004522 *
4523 * If loaded successfully, copies the firmware into buffers
4524 * for the card to fetch (via DMA).
4525 */
4526static void
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004527il4965_ucode_callback(const struct firmware *ucode_raw, void *context)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004528{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004529 struct il_priv *il = context;
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004530 struct il_ucode_header *ucode;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004531 int err;
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004532 struct il4965_firmware_pieces pieces;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004533 const unsigned int api_max = il->cfg->ucode_api_max;
4534 const unsigned int api_min = il->cfg->ucode_api_min;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004535 u32 api_ver;
4536
4537 u32 max_probe_length = 200;
4538 u32 standard_phy_calibration_size =
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004539 IL_DEFAULT_STANDARD_PHY_CALIBRATE_TBL_SIZE;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004540
4541 memset(&pieces, 0, sizeof(pieces));
4542
4543 if (!ucode_raw) {
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +01004544 if (il->fw_idx <= il->cfg->ucode_api_max)
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004545 IL_ERR(
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004546 "request for firmware file '%s' failed.\n",
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004547 il->firmware_name);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004548 goto try_again;
4549 }
4550
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01004551 D_INFO("Loaded firmware file '%s' (%zd bytes).\n",
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004552 il->firmware_name, ucode_raw->size);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004553
4554 /* Make sure that we got at least the API version number */
4555 if (ucode_raw->size < 4) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004556 IL_ERR("File size way too small!\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004557 goto try_again;
4558 }
4559
4560 /* Data from ucode file: header followed by uCode images */
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004561 ucode = (struct il_ucode_header *)ucode_raw->data;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004562
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004563 err = il4965_load_firmware(il, ucode_raw, &pieces);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004564
4565 if (err)
4566 goto try_again;
4567
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004568 api_ver = IL_UCODE_API(il->ucode_ver);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004569
4570 /*
4571 * api_ver should match the api version forming part of the
4572 * firmware filename ... but we don't check for that and only rely
4573 * on the API version read from firmware header from here on forward
4574 */
4575 if (api_ver < api_min || api_ver > api_max) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004576 IL_ERR(
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004577 "Driver unable to support your firmware API. "
4578 "Driver supports v%u, firmware is v%u.\n",
4579 api_max, api_ver);
4580 goto try_again;
4581 }
4582
4583 if (api_ver != api_max)
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004584 IL_ERR(
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004585 "Firmware has old API version. Expected v%u, "
4586 "got v%u. New firmware can be obtained "
4587 "from http://www.intellinuxwireless.org.\n",
4588 api_max, api_ver);
4589
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004590 IL_INFO("loaded firmware version %u.%u.%u.%u\n",
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004591 IL_UCODE_MAJOR(il->ucode_ver),
4592 IL_UCODE_MINOR(il->ucode_ver),
4593 IL_UCODE_API(il->ucode_ver),
4594 IL_UCODE_SERIAL(il->ucode_ver));
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004595
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004596 snprintf(il->hw->wiphy->fw_version,
4597 sizeof(il->hw->wiphy->fw_version),
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004598 "%u.%u.%u.%u",
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004599 IL_UCODE_MAJOR(il->ucode_ver),
4600 IL_UCODE_MINOR(il->ucode_ver),
4601 IL_UCODE_API(il->ucode_ver),
4602 IL_UCODE_SERIAL(il->ucode_ver));
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004603
4604 /*
4605 * For any of the failures below (before allocating pci memory)
4606 * we will try to load a version with a smaller API -- maybe the
4607 * user just got a corrupted version of the latest API.
4608 */
4609
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01004610 D_INFO("f/w package hdr ucode version raw = 0x%x\n",
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004611 il->ucode_ver);
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01004612 D_INFO("f/w package hdr runtime inst size = %Zd\n",
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004613 pieces.inst_size);
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01004614 D_INFO("f/w package hdr runtime data size = %Zd\n",
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004615 pieces.data_size);
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01004616 D_INFO("f/w package hdr init inst size = %Zd\n",
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004617 pieces.init_size);
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01004618 D_INFO("f/w package hdr init data size = %Zd\n",
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004619 pieces.init_data_size);
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01004620 D_INFO("f/w package hdr boot inst size = %Zd\n",
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004621 pieces.boot_size);
4622
4623 /* Verify that uCode images will fit in card's SRAM */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004624 if (pieces.inst_size > il->hw_params.max_inst_size) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004625 IL_ERR("uCode instr len %Zd too large to fit in\n",
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004626 pieces.inst_size);
4627 goto try_again;
4628 }
4629
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004630 if (pieces.data_size > il->hw_params.max_data_size) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004631 IL_ERR("uCode data len %Zd too large to fit in\n",
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004632 pieces.data_size);
4633 goto try_again;
4634 }
4635
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004636 if (pieces.init_size > il->hw_params.max_inst_size) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004637 IL_ERR("uCode init instr len %Zd too large to fit in\n",
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004638 pieces.init_size);
4639 goto try_again;
4640 }
4641
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004642 if (pieces.init_data_size > il->hw_params.max_data_size) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004643 IL_ERR("uCode init data len %Zd too large to fit in\n",
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004644 pieces.init_data_size);
4645 goto try_again;
4646 }
4647
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004648 if (pieces.boot_size > il->hw_params.max_bsm_size) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004649 IL_ERR("uCode boot instr len %Zd too large to fit in\n",
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004650 pieces.boot_size);
4651 goto try_again;
4652 }
4653
4654 /* Allocate ucode buffers for card's bus-master loading ... */
4655
4656 /* Runtime instructions and 2 copies of data:
4657 * 1) unmodified from disk
4658 * 2) backup cache for save/restore during power-downs */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004659 il->ucode_code.len = pieces.inst_size;
4660 il_alloc_fw_desc(il->pci_dev, &il->ucode_code);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004661
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004662 il->ucode_data.len = pieces.data_size;
4663 il_alloc_fw_desc(il->pci_dev, &il->ucode_data);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004664
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004665 il->ucode_data_backup.len = pieces.data_size;
4666 il_alloc_fw_desc(il->pci_dev, &il->ucode_data_backup);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004667
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004668 if (!il->ucode_code.v_addr || !il->ucode_data.v_addr ||
4669 !il->ucode_data_backup.v_addr)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004670 goto err_pci_alloc;
4671
4672 /* Initialization instructions and data */
4673 if (pieces.init_size && pieces.init_data_size) {
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004674 il->ucode_init.len = pieces.init_size;
4675 il_alloc_fw_desc(il->pci_dev, &il->ucode_init);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004676
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004677 il->ucode_init_data.len = pieces.init_data_size;
4678 il_alloc_fw_desc(il->pci_dev, &il->ucode_init_data);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004679
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004680 if (!il->ucode_init.v_addr || !il->ucode_init_data.v_addr)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004681 goto err_pci_alloc;
4682 }
4683
4684 /* Bootstrap (instructions only, no data) */
4685 if (pieces.boot_size) {
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004686 il->ucode_boot.len = pieces.boot_size;
4687 il_alloc_fw_desc(il->pci_dev, &il->ucode_boot);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004688
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004689 if (!il->ucode_boot.v_addr)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004690 goto err_pci_alloc;
4691 }
4692
4693 /* Now that we can no longer fail, copy information */
4694
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004695 il->sta_key_max_num = STA_KEY_MAX_NUM;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004696
4697 /* Copy images into buffers for card's bus-master reads ... */
4698
4699 /* Runtime instructions (first block of data in file) */
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01004700 D_INFO("Copying (but not loading) uCode instr len %Zd\n",
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004701 pieces.inst_size);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004702 memcpy(il->ucode_code.v_addr, pieces.inst, pieces.inst_size);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004703
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01004704 D_INFO("uCode instr buf vaddr = 0x%p, paddr = 0x%08x\n",
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004705 il->ucode_code.v_addr, (u32)il->ucode_code.p_addr);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004706
4707 /*
4708 * Runtime data
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004709 * NOTE: Copy into backup buffer will be done in il_up()
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004710 */
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01004711 D_INFO("Copying (but not loading) uCode data len %Zd\n",
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004712 pieces.data_size);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004713 memcpy(il->ucode_data.v_addr, pieces.data, pieces.data_size);
4714 memcpy(il->ucode_data_backup.v_addr, pieces.data, pieces.data_size);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004715
4716 /* Initialization instructions */
4717 if (pieces.init_size) {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01004718 D_INFO(
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004719 "Copying (but not loading) init instr len %Zd\n",
4720 pieces.init_size);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004721 memcpy(il->ucode_init.v_addr, pieces.init, pieces.init_size);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004722 }
4723
4724 /* Initialization data */
4725 if (pieces.init_data_size) {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01004726 D_INFO(
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004727 "Copying (but not loading) init data len %Zd\n",
4728 pieces.init_data_size);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004729 memcpy(il->ucode_init_data.v_addr, pieces.init_data,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004730 pieces.init_data_size);
4731 }
4732
4733 /* Bootstrap instructions */
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01004734 D_INFO("Copying (but not loading) boot instr len %Zd\n",
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004735 pieces.boot_size);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004736 memcpy(il->ucode_boot.v_addr, pieces.boot, pieces.boot_size);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004737
4738 /*
4739 * figure out the offset of chain noise reset and gain commands
4740 * base on the size of standard phy calibration commands table size
4741 */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004742 il->_4965.phy_calib_chain_noise_reset_cmd =
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004743 standard_phy_calibration_size;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004744 il->_4965.phy_calib_chain_noise_gain_cmd =
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004745 standard_phy_calibration_size + 1;
4746
4747 /**************************************************
4748 * This is still part of probe() in a sense...
4749 *
4750 * 9. Setup and register with mac80211 and debugfs
4751 **************************************************/
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004752 err = il4965_mac_setup_register(il, max_probe_length);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004753 if (err)
4754 goto out_unbind;
4755
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004756 err = il_dbgfs_register(il, DRV_NAME);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004757 if (err)
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004758 IL_ERR(
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004759 "failed to create debugfs files. Ignoring error: %d\n", err);
4760
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004761 err = sysfs_create_group(&il->pci_dev->dev.kobj,
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004762 &il_attribute_group);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004763 if (err) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004764 IL_ERR("failed to create sysfs device attributes\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004765 goto out_unbind;
4766 }
4767
4768 /* We have our copies now, allow OS release its copies */
4769 release_firmware(ucode_raw);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004770 complete(&il->_4965.firmware_loading_complete);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004771 return;
4772
4773 try_again:
4774 /* try next, if any */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004775 if (il4965_request_firmware(il, false))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004776 goto out_unbind;
4777 release_firmware(ucode_raw);
4778 return;
4779
4780 err_pci_alloc:
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004781 IL_ERR("failed to allocate pci memory\n");
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004782 il4965_dealloc_ucode_pci(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004783 out_unbind:
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004784 complete(&il->_4965.firmware_loading_complete);
4785 device_release_driver(&il->pci_dev->dev);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004786 release_firmware(ucode_raw);
4787}
4788
4789static const char * const desc_lookup_text[] = {
4790 "OK",
4791 "FAIL",
4792 "BAD_PARAM",
4793 "BAD_CHECKSUM",
4794 "NMI_INTERRUPT_WDG",
4795 "SYSASSERT",
4796 "FATAL_ERROR",
4797 "BAD_COMMAND",
4798 "HW_ERROR_TUNE_LOCK",
4799 "HW_ERROR_TEMPERATURE",
4800 "ILLEGAL_CHAN_FREQ",
Stanislaw Gruszka3b98c7f2011-08-26 16:29:35 +02004801 "VCC_NOT_STBL",
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004802 "FH_ERROR",
4803 "NMI_INTERRUPT_HOST",
4804 "NMI_INTERRUPT_ACTION_PT",
4805 "NMI_INTERRUPT_UNKNOWN",
4806 "UCODE_VERSION_MISMATCH",
4807 "HW_ERROR_ABS_LOCK",
4808 "HW_ERROR_CAL_LOCK_FAIL",
4809 "NMI_INTERRUPT_INST_ACTION_PT",
4810 "NMI_INTERRUPT_DATA_ACTION_PT",
4811 "NMI_TRM_HW_ER",
4812 "NMI_INTERRUPT_TRM",
Joe Perches861d9c32011-07-08 23:20:24 -07004813 "NMI_INTERRUPT_BREAK_POINT",
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004814 "DEBUG_0",
4815 "DEBUG_1",
4816 "DEBUG_2",
4817 "DEBUG_3",
4818};
4819
4820static struct { char *name; u8 num; } advanced_lookup[] = {
4821 { "NMI_INTERRUPT_WDG", 0x34 },
4822 { "SYSASSERT", 0x35 },
4823 { "UCODE_VERSION_MISMATCH", 0x37 },
4824 { "BAD_COMMAND", 0x38 },
4825 { "NMI_INTERRUPT_DATA_ACTION_PT", 0x3C },
4826 { "FATAL_ERROR", 0x3D },
4827 { "NMI_TRM_HW_ERR", 0x46 },
4828 { "NMI_INTERRUPT_TRM", 0x4C },
4829 { "NMI_INTERRUPT_BREAK_POINT", 0x54 },
4830 { "NMI_INTERRUPT_WDG_RXF_FULL", 0x5C },
4831 { "NMI_INTERRUPT_WDG_NO_RBD_RXF_FULL", 0x64 },
4832 { "NMI_INTERRUPT_HOST", 0x66 },
4833 { "NMI_INTERRUPT_ACTION_PT", 0x7C },
4834 { "NMI_INTERRUPT_UNKNOWN", 0x84 },
4835 { "NMI_INTERRUPT_INST_ACTION_PT", 0x86 },
4836 { "ADVANCED_SYSASSERT", 0 },
4837};
4838
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004839static const char *il4965_desc_lookup(u32 num)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004840{
4841 int i;
4842 int max = ARRAY_SIZE(desc_lookup_text);
4843
4844 if (num < max)
4845 return desc_lookup_text[num];
4846
4847 max = ARRAY_SIZE(advanced_lookup) - 1;
4848 for (i = 0; i < max; i++) {
4849 if (advanced_lookup[i].num == num)
4850 break;
4851 }
4852 return advanced_lookup[i].name;
4853}
4854
4855#define ERROR_START_OFFSET (1 * sizeof(u32))
4856#define ERROR_ELEM_SIZE (7 * sizeof(u32))
4857
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004858void il4965_dump_nic_error_log(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004859{
4860 u32 data2, line;
4861 u32 desc, time, count, base, data1;
4862 u32 blink1, blink2, ilink1, ilink2;
4863 u32 pc, hcmd;
4864
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004865 if (il->ucode_type == UCODE_INIT) {
4866 base = le32_to_cpu(il->card_alive_init.error_event_table_ptr);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004867 } else {
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004868 base = le32_to_cpu(il->card_alive.error_event_table_ptr);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004869 }
4870
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004871 if (!il->cfg->ops->lib->is_valid_rtc_data_addr(base)) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004872 IL_ERR(
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004873 "Not valid error log pointer 0x%08X for %s uCode\n",
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004874 base, (il->ucode_type == UCODE_INIT) ? "Init" : "RT");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004875 return;
4876 }
4877
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004878 count = il_read_targ_mem(il, base);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004879
4880 if (ERROR_START_OFFSET <= count * ERROR_ELEM_SIZE) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004881 IL_ERR("Start IWL Error Log Dump:\n");
4882 IL_ERR("Status: 0x%08lX, count: %d\n",
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004883 il->status, count);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004884 }
4885
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004886 desc = il_read_targ_mem(il, base + 1 * sizeof(u32));
4887 il->isr_stats.err_code = desc;
4888 pc = il_read_targ_mem(il, base + 2 * sizeof(u32));
4889 blink1 = il_read_targ_mem(il, base + 3 * sizeof(u32));
4890 blink2 = il_read_targ_mem(il, base + 4 * sizeof(u32));
4891 ilink1 = il_read_targ_mem(il, base + 5 * sizeof(u32));
4892 ilink2 = il_read_targ_mem(il, base + 6 * sizeof(u32));
4893 data1 = il_read_targ_mem(il, base + 7 * sizeof(u32));
4894 data2 = il_read_targ_mem(il, base + 8 * sizeof(u32));
4895 line = il_read_targ_mem(il, base + 9 * sizeof(u32));
4896 time = il_read_targ_mem(il, base + 11 * sizeof(u32));
4897 hcmd = il_read_targ_mem(il, base + 22 * sizeof(u32));
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004898
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004899 IL_ERR("Desc Time "
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004900 "data1 data2 line\n");
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004901 IL_ERR("%-28s (0x%04X) %010u 0x%08X 0x%08X %u\n",
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004902 il4965_desc_lookup(desc), desc, time, data1, data2, line);
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004903 IL_ERR("pc blink1 blink2 ilink1 ilink2 hcmd\n");
4904 IL_ERR("0x%05X 0x%05X 0x%05X 0x%05X 0x%05X 0x%05X\n",
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004905 pc, blink1, blink2, ilink1, ilink2, hcmd);
4906}
4907
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004908static void il4965_rf_kill_ct_config(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004909{
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004910 struct il_ct_kill_config cmd;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004911 unsigned long flags;
4912 int ret = 0;
4913
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004914 spin_lock_irqsave(&il->lock, flags);
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02004915 _il_wr(il, CSR_UCODE_DRV_GP1_CLR,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004916 CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004917 spin_unlock_irqrestore(&il->lock, flags);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004918
4919 cmd.critical_temperature_R =
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004920 cpu_to_le32(il->hw_params.ct_kill_threshold);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004921
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004922 ret = il_send_cmd_pdu(il, REPLY_CT_KILL_CONFIG_CMD,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004923 sizeof(cmd), &cmd);
4924 if (ret)
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004925 IL_ERR("REPLY_CT_KILL_CONFIG_CMD failed\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004926 else
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01004927 D_INFO("REPLY_CT_KILL_CONFIG_CMD "
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004928 "succeeded, "
4929 "critical temperature is %d\n",
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004930 il->hw_params.ct_kill_threshold);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004931}
4932
4933static const s8 default_queue_to_tx_fifo[] = {
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004934 IL_TX_FIFO_VO,
4935 IL_TX_FIFO_VI,
4936 IL_TX_FIFO_BE,
4937 IL_TX_FIFO_BK,
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01004938 IL49_CMD_FIFO_NUM,
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004939 IL_TX_FIFO_UNUSED,
4940 IL_TX_FIFO_UNUSED,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004941};
4942
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004943static int il4965_alive_notify(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004944{
4945 u32 a;
4946 unsigned long flags;
4947 int i, chan;
4948 u32 reg_val;
4949
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004950 spin_lock_irqsave(&il->lock, flags);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004951
4952 /* Clear 4965's internal Tx Scheduler data base */
Stanislaw Gruszkadb54eb52011-08-24 21:06:33 +02004953 il->scd_base_addr = il_rd_prph(il,
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01004954 IL49_SCD_SRAM_BASE_ADDR);
4955 a = il->scd_base_addr + IL49_SCD_CONTEXT_DATA_OFFSET;
4956 for (; a < il->scd_base_addr + IL49_SCD_TX_STTS_BITMAP_OFFSET; a += 4)
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004957 il_write_targ_mem(il, a, 0);
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01004958 for (; a < il->scd_base_addr + IL49_SCD_TRANSLATE_TBL_OFFSET; a += 4)
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004959 il_write_targ_mem(il, a, 0);
4960 for (; a < il->scd_base_addr +
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01004961 IL49_SCD_TRANSLATE_TBL_OFFSET_QUEUE(il->hw_params.max_txq_num); a += 4)
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004962 il_write_targ_mem(il, a, 0);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004963
4964 /* Tel 4965 where to find Tx byte count tables */
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01004965 il_wr_prph(il, IL49_SCD_DRAM_BASE_ADDR,
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004966 il->scd_bc_tbls.dma >> 10);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004967
4968 /* Enable DMA channel */
4969 for (chan = 0; chan < FH49_TCSR_CHNL_NUM ; chan++)
Stanislaw Gruszka0c1a94e2011-08-24 17:37:16 +02004970 il_wr(il,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004971 FH_TCSR_CHNL_TX_CONFIG_REG(chan),
4972 FH_TCSR_TX_CONFIG_REG_VAL_DMA_CHNL_ENABLE |
4973 FH_TCSR_TX_CONFIG_REG_VAL_DMA_CREDIT_ENABLE);
4974
4975 /* Update FH chicken bits */
Stanislaw Gruszka0c1a94e2011-08-24 17:37:16 +02004976 reg_val = il_rd(il, FH_TX_CHICKEN_BITS_REG);
4977 il_wr(il, FH_TX_CHICKEN_BITS_REG,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004978 reg_val | FH_TX_CHICKEN_BITS_SCD_AUTO_RETRY_EN);
4979
4980 /* Disable chain mode for all queues */
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01004981 il_wr_prph(il, IL49_SCD_QUEUECHAIN_SEL, 0);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004982
4983 /* Initialize each Tx queue (including the command queue) */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004984 for (i = 0; i < il->hw_params.max_txq_num; i++) {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004985
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +01004986 /* TFD circular buffer read/write idxes */
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01004987 il_wr_prph(il, IL49_SCD_QUEUE_RDPTR(i), 0);
Stanislaw Gruszka0c1a94e2011-08-24 17:37:16 +02004988 il_wr(il, HBUS_TARG_WRPTR, 0 | (i << 8));
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004989
4990 /* Max Tx Window size for Scheduler-ACK mode */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004991 il_write_targ_mem(il, il->scd_base_addr +
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01004992 IL49_SCD_CONTEXT_QUEUE_OFFSET(i),
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004993 (SCD_WIN_SIZE <<
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01004994 IL49_SCD_QUEUE_CTX_REG1_WIN_SIZE_POS) &
4995 IL49_SCD_QUEUE_CTX_REG1_WIN_SIZE_MSK);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004996
4997 /* Frame limit */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004998 il_write_targ_mem(il, il->scd_base_addr +
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01004999 IL49_SCD_CONTEXT_QUEUE_OFFSET(i) +
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005000 sizeof(u32),
5001 (SCD_FRAME_LIMIT <<
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01005002 IL49_SCD_QUEUE_CTX_REG2_FRAME_LIMIT_POS) &
5003 IL49_SCD_QUEUE_CTX_REG2_FRAME_LIMIT_MSK);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005004
5005 }
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01005006 il_wr_prph(il, IL49_SCD_INTERRUPT_MASK,
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005007 (1 << il->hw_params.max_txq_num) - 1);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005008
5009 /* Activate all Tx DMA/FIFO channels */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005010 il4965_txq_set_sched(il, IL_MASK(0, 6));
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005011
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005012 il4965_set_wr_ptrs(il, IL_DEFAULT_CMD_QUEUE_NUM, 0);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005013
5014 /* make sure all queue are not stopped */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005015 memset(&il->queue_stopped[0], 0, sizeof(il->queue_stopped));
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005016 for (i = 0; i < 4; i++)
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005017 atomic_set(&il->queue_stop_count[i], 0);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005018
5019 /* reset to 0 to enable all the queue first */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005020 il->txq_ctx_active_msk = 0;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005021 /* Map each Tx/cmd queue to its corresponding fifo */
5022 BUILD_BUG_ON(ARRAY_SIZE(default_queue_to_tx_fifo) != 7);
5023
5024 for (i = 0; i < ARRAY_SIZE(default_queue_to_tx_fifo); i++) {
5025 int ac = default_queue_to_tx_fifo[i];
5026
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005027 il_txq_ctx_activate(il, i);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005028
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005029 if (ac == IL_TX_FIFO_UNUSED)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005030 continue;
5031
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005032 il4965_tx_queue_set_status(il, &il->txq[i], ac, 0);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005033 }
5034
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005035 spin_unlock_irqrestore(&il->lock, flags);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005036
5037 return 0;
5038}
5039
5040/**
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005041 * il4965_alive_start - called after REPLY_ALIVE notification received
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005042 * from protocol/runtime uCode (initialization uCode's
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005043 * Alive gets handled by il_init_alive_start()).
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005044 */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005045static void il4965_alive_start(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005046{
5047 int ret = 0;
Stanislaw Gruszka7c2cde22011-11-15 11:29:04 +01005048 struct il_rxon_context *ctx = &il->ctx;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005049
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005050 D_INFO("Runtime Alive received.\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005051
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005052 if (il->card_alive.is_valid != UCODE_VALID_OK) {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005053 /* We had an error bringing up the hardware, so take it
5054 * all the way back down so we can try again */
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005055 D_INFO("Alive failed.\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005056 goto restart;
5057 }
5058
5059 /* Initialize uCode has loaded Runtime uCode ... verify inst image.
5060 * This is a paranoid check, because we would not have gotten the
5061 * "runtime" alive if code weren't properly loaded. */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005062 if (il4965_verify_ucode(il)) {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005063 /* Runtime instruction load was bad;
5064 * take it all the way back down so we can try again */
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005065 D_INFO("Bad runtime uCode load.\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005066 goto restart;
5067 }
5068
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005069 ret = il4965_alive_notify(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005070 if (ret) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02005071 IL_WARN(
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005072 "Could not complete ALIVE transition [ntf]: %d\n", ret);
5073 goto restart;
5074 }
5075
5076
5077 /* After the ALIVE response, we can send host commands to the uCode */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005078 set_bit(STATUS_ALIVE, &il->status);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005079
5080 /* Enable watchdog to monitor the driver tx queues */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005081 il_setup_watchdog(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005082
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005083 if (il_is_rfkill(il))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005084 return;
5085
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005086 ieee80211_wake_queues(il->hw);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005087
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +02005088 il->active_rate = RATES_MASK;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005089
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005090 if (il_is_associated_ctx(ctx)) {
5091 struct il_rxon_cmd *active_rxon =
5092 (struct il_rxon_cmd *)&ctx->active;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005093 /* apply any changes in staging */
5094 ctx->staging.filter_flags |= RXON_FILTER_ASSOC_MSK;
5095 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
5096 } else {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005097 /* Initialize our rx_config data */
Stanislaw Gruszka17d6e552011-08-29 12:52:20 +02005098 il_connection_init_rx_config(il, &il->ctx);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005099
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005100 if (il->cfg->ops->hcmd->set_rxon_chain)
5101 il->cfg->ops->hcmd->set_rxon_chain(il, ctx);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005102 }
5103
5104 /* Configure bluetooth coexistence if enabled */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005105 il_send_bt_config(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005106
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005107 il4965_reset_run_time_calib(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005108
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005109 set_bit(STATUS_READY, &il->status);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005110
5111 /* Configure the adapter for unassociated operation */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005112 il_commit_rxon(il, ctx);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005113
5114 /* At this point, the NIC is initialized and operational */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005115 il4965_rf_kill_ct_config(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005116
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005117 D_INFO("ALIVE processing complete.\n");
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005118 wake_up(&il->wait_command_queue);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005119
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005120 il_power_update_mode(il, true);
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005121 D_INFO("Updated power mode\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005122
5123 return;
5124
5125 restart:
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005126 queue_work(il->workqueue, &il->restart);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005127}
5128
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005129static void il4965_cancel_deferred_work(struct il_priv *il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005130
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005131static void __il4965_down(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005132{
5133 unsigned long flags;
Stanislaw Gruszkaab42b402011-04-28 11:51:24 +02005134 int exit_pending;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005135
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005136 D_INFO(DRV_NAME " is going down\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005137
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005138 il_scan_cancel_timeout(il, 200);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005139
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005140 exit_pending = test_and_set_bit(STATUS_EXIT_PENDING, &il->status);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005141
5142 /* Stop TX queues watchdog. We need to have STATUS_EXIT_PENDING bit set
5143 * to prevent rearm timer */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005144 del_timer_sync(&il->watchdog);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005145
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005146 il_clear_ucode_stations(il, NULL);
5147 il_dealloc_bcast_stations(il);
5148 il_clear_driver_stations(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005149
5150 /* Unblock any waiting calls */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005151 wake_up_all(&il->wait_command_queue);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005152
5153 /* Wipe out the EXIT_PENDING status bit if we are not actually
5154 * exiting the module */
5155 if (!exit_pending)
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005156 clear_bit(STATUS_EXIT_PENDING, &il->status);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005157
5158 /* stop and reset the on-board processor */
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02005159 _il_wr(il, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005160
5161 /* tell the device to stop sending interrupts */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005162 spin_lock_irqsave(&il->lock, flags);
5163 il_disable_interrupts(il);
5164 spin_unlock_irqrestore(&il->lock, flags);
5165 il4965_synchronize_irq(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005166
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005167 if (il->mac80211_registered)
5168 ieee80211_stop_queues(il->hw);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005169
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005170 /* If we have not previously called il_init() then
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005171 * clear all bits but the RF Kill bit and return */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005172 if (!il_is_init(il)) {
5173 il->status = test_bit(STATUS_RF_KILL_HW, &il->status) <<
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005174 STATUS_RF_KILL_HW |
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005175 test_bit(STATUS_GEO_CONFIGURED, &il->status) <<
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005176 STATUS_GEO_CONFIGURED |
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005177 test_bit(STATUS_EXIT_PENDING, &il->status) <<
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005178 STATUS_EXIT_PENDING;
5179 goto exit;
5180 }
5181
5182 /* ...otherwise clear out all the status bits but the RF Kill
5183 * bit and continue taking the NIC down. */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005184 il->status &= test_bit(STATUS_RF_KILL_HW, &il->status) <<
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005185 STATUS_RF_KILL_HW |
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005186 test_bit(STATUS_GEO_CONFIGURED, &il->status) <<
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005187 STATUS_GEO_CONFIGURED |
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005188 test_bit(STATUS_FW_ERROR, &il->status) <<
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005189 STATUS_FW_ERROR |
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005190 test_bit(STATUS_EXIT_PENDING, &il->status) <<
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005191 STATUS_EXIT_PENDING;
5192
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005193 il4965_txq_ctx_stop(il);
5194 il4965_rxq_stop(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005195
5196 /* Power-down device's busmaster DMA clocks */
Stanislaw Gruszkadb54eb52011-08-24 21:06:33 +02005197 il_wr_prph(il, APMG_CLK_DIS_REG, APMG_CLK_VAL_DMA_CLK_RQT);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005198 udelay(5);
5199
5200 /* Make sure (redundant) we've released our request to stay awake */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005201 il_clear_bit(il, CSR_GP_CNTRL,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005202 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
5203
5204 /* Stop the device, and put it in low power state */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005205 il_apm_stop(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005206
5207 exit:
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005208 memset(&il->card_alive, 0, sizeof(struct il_alive_resp));
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005209
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005210 dev_kfree_skb(il->beacon_skb);
5211 il->beacon_skb = NULL;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005212
5213 /* clear out any free frames */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005214 il4965_clear_free_frames(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005215}
5216
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005217static void il4965_down(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005218{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005219 mutex_lock(&il->mutex);
5220 __il4965_down(il);
5221 mutex_unlock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005222
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005223 il4965_cancel_deferred_work(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005224}
5225
5226#define HW_READY_TIMEOUT (50)
5227
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005228static int il4965_set_hw_ready(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005229{
5230 int ret = 0;
5231
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005232 il_set_bit(il, CSR_HW_IF_CONFIG_REG,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005233 CSR_HW_IF_CONFIG_REG_BIT_NIC_READY);
5234
5235 /* See if we got it */
Stanislaw Gruszka142b3432011-08-24 15:22:57 +02005236 ret = _il_poll_bit(il, CSR_HW_IF_CONFIG_REG,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005237 CSR_HW_IF_CONFIG_REG_BIT_NIC_READY,
5238 CSR_HW_IF_CONFIG_REG_BIT_NIC_READY,
5239 HW_READY_TIMEOUT);
5240 if (ret != -ETIMEDOUT)
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005241 il->hw_ready = true;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005242 else
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005243 il->hw_ready = false;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005244
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005245 D_INFO("hardware %s\n",
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005246 (il->hw_ready == 1) ? "ready" : "not ready");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005247 return ret;
5248}
5249
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005250static int il4965_prepare_card_hw(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005251{
5252 int ret = 0;
5253
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005254 D_INFO("il4965_prepare_card_hw enter\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005255
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005256 ret = il4965_set_hw_ready(il);
5257 if (il->hw_ready)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005258 return ret;
5259
5260 /* If HW is not ready, prepare the conditions to check again */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005261 il_set_bit(il, CSR_HW_IF_CONFIG_REG,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005262 CSR_HW_IF_CONFIG_REG_PREPARE);
5263
Stanislaw Gruszka142b3432011-08-24 15:22:57 +02005264 ret = _il_poll_bit(il, CSR_HW_IF_CONFIG_REG,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005265 ~CSR_HW_IF_CONFIG_REG_BIT_NIC_PREPARE_DONE,
5266 CSR_HW_IF_CONFIG_REG_BIT_NIC_PREPARE_DONE, 150000);
5267
5268 /* HW should be ready by now, check again. */
5269 if (ret != -ETIMEDOUT)
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005270 il4965_set_hw_ready(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005271
5272 return ret;
5273}
5274
5275#define MAX_HW_RESTARTS 5
5276
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005277static int __il4965_up(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005278{
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005279 int i;
5280 int ret;
5281
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005282 if (test_bit(STATUS_EXIT_PENDING, &il->status)) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02005283 IL_WARN("Exit pending; will not bring the NIC up\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005284 return -EIO;
5285 }
5286
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005287 if (!il->ucode_data_backup.v_addr || !il->ucode_data.v_addr) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02005288 IL_ERR("ucode not available for device bringup\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005289 return -EIO;
5290 }
5291
Stanislaw Gruszka17d6e552011-08-29 12:52:20 +02005292 ret = il4965_alloc_bcast_station(il, &il->ctx);
5293 if (ret) {
5294 il_dealloc_bcast_stations(il);
5295 return ret;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005296 }
5297
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005298 il4965_prepare_card_hw(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005299
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005300 if (!il->hw_ready) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02005301 IL_WARN("Exit HW not ready\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005302 return -EIO;
5303 }
5304
5305 /* If platform's RF_KILL switch is NOT set to KILL */
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02005306 if (_il_rd(il,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005307 CSR_GP_CNTRL) & CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005308 clear_bit(STATUS_RF_KILL_HW, &il->status);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005309 else
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005310 set_bit(STATUS_RF_KILL_HW, &il->status);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005311
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005312 if (il_is_rfkill(il)) {
5313 wiphy_rfkill_set_hw_state(il->hw->wiphy, true);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005314
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005315 il_enable_interrupts(il);
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02005316 IL_WARN("Radio disabled by HW RF Kill switch\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005317 return 0;
5318 }
5319
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02005320 _il_wr(il, CSR_INT, 0xFFFFFFFF);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005321
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005322 /* must be initialised before il_hw_nic_init */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005323 il->cmd_queue = IL_DEFAULT_CMD_QUEUE_NUM;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005324
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005325 ret = il4965_hw_nic_init(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005326 if (ret) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02005327 IL_ERR("Unable to init nic\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005328 return ret;
5329 }
5330
5331 /* make sure rfkill handshake bits are cleared */
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02005332 _il_wr(il, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
5333 _il_wr(il, CSR_UCODE_DRV_GP1_CLR,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005334 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
5335
5336 /* clear (again), then enable host interrupts */
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02005337 _il_wr(il, CSR_INT, 0xFFFFFFFF);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005338 il_enable_interrupts(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005339
5340 /* really make sure rfkill handshake bits are cleared */
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02005341 _il_wr(il, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
5342 _il_wr(il, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005343
5344 /* Copy original ucode data image from disk into backup cache.
5345 * This will be used to initialize the on-board processor's
5346 * data SRAM for a clean start when the runtime program first loads. */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005347 memcpy(il->ucode_data_backup.v_addr, il->ucode_data.v_addr,
5348 il->ucode_data.len);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005349
5350 for (i = 0; i < MAX_HW_RESTARTS; i++) {
5351
5352 /* load bootstrap state machine,
5353 * load bootstrap program into processor's memory,
5354 * prepare to load the "initialize" uCode */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005355 ret = il->cfg->ops->lib->load_ucode(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005356
5357 if (ret) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02005358 IL_ERR("Unable to set up bootstrap uCode: %d\n",
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005359 ret);
5360 continue;
5361 }
5362
5363 /* start card; "initialize" will load runtime ucode */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005364 il4965_nic_start(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005365
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005366 D_INFO(DRV_NAME " is coming up\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005367
5368 return 0;
5369 }
5370
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005371 set_bit(STATUS_EXIT_PENDING, &il->status);
5372 __il4965_down(il);
5373 clear_bit(STATUS_EXIT_PENDING, &il->status);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005374
5375 /* tried to restart and config the device for as long as our
5376 * patience could withstand */
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02005377 IL_ERR("Unable to initialize device after %d attempts.\n", i);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005378 return -EIO;
5379}
5380
5381
5382/*****************************************************************************
5383 *
5384 * Workqueue callbacks
5385 *
5386 *****************************************************************************/
5387
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005388static void il4965_bg_init_alive_start(struct work_struct *data)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005389{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005390 struct il_priv *il =
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005391 container_of(data, struct il_priv, init_alive_start.work);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005392
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005393 mutex_lock(&il->mutex);
5394 if (test_bit(STATUS_EXIT_PENDING, &il->status))
Stanislaw Gruszka28a6e572011-04-28 11:51:32 +02005395 goto out;
5396
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005397 il->cfg->ops->lib->init_alive_start(il);
Stanislaw Gruszka28a6e572011-04-28 11:51:32 +02005398out:
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005399 mutex_unlock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005400}
5401
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005402static void il4965_bg_alive_start(struct work_struct *data)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005403{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005404 struct il_priv *il =
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005405 container_of(data, struct il_priv, alive_start.work);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005406
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005407 mutex_lock(&il->mutex);
5408 if (test_bit(STATUS_EXIT_PENDING, &il->status))
Stanislaw Gruszka28a6e572011-04-28 11:51:32 +02005409 goto out;
5410
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005411 il4965_alive_start(il);
Stanislaw Gruszka28a6e572011-04-28 11:51:32 +02005412out:
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005413 mutex_unlock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005414}
5415
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005416static void il4965_bg_run_time_calib_work(struct work_struct *work)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005417{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005418 struct il_priv *il = container_of(work, struct il_priv,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005419 run_time_calib_work);
5420
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005421 mutex_lock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005422
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005423 if (test_bit(STATUS_EXIT_PENDING, &il->status) ||
5424 test_bit(STATUS_SCANNING, &il->status)) {
5425 mutex_unlock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005426 return;
5427 }
5428
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005429 if (il->start_calib) {
5430 il4965_chain_noise_calibration(il,
Stanislaw Gruszkaebf0d902011-08-26 15:43:47 +02005431 (void *)&il->_4965.stats);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005432 il4965_sensitivity_calibration(il,
Stanislaw Gruszkaebf0d902011-08-26 15:43:47 +02005433 (void *)&il->_4965.stats);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005434 }
5435
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005436 mutex_unlock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005437}
5438
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005439static void il4965_bg_restart(struct work_struct *data)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005440{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005441 struct il_priv *il = container_of(data, struct il_priv, restart);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005442
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005443 if (test_bit(STATUS_EXIT_PENDING, &il->status))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005444 return;
5445
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005446 if (test_and_clear_bit(STATUS_FW_ERROR, &il->status)) {
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005447 mutex_lock(&il->mutex);
Stanislaw Gruszka17d6e552011-08-29 12:52:20 +02005448 il->ctx.vif = NULL;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005449 il->is_open = 0;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005450
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005451 __il4965_down(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005452
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005453 mutex_unlock(&il->mutex);
5454 il4965_cancel_deferred_work(il);
5455 ieee80211_restart_hw(il->hw);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005456 } else {
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005457 il4965_down(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005458
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005459 mutex_lock(&il->mutex);
5460 if (test_bit(STATUS_EXIT_PENDING, &il->status)) {
5461 mutex_unlock(&il->mutex);
Stanislaw Gruszka28a6e572011-04-28 11:51:32 +02005462 return;
5463 }
5464
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005465 __il4965_up(il);
5466 mutex_unlock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005467 }
5468}
5469
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005470static void il4965_bg_rx_replenish(struct work_struct *data)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005471{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005472 struct il_priv *il =
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005473 container_of(data, struct il_priv, rx_replenish);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005474
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005475 if (test_bit(STATUS_EXIT_PENDING, &il->status))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005476 return;
5477
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005478 mutex_lock(&il->mutex);
5479 il4965_rx_replenish(il);
5480 mutex_unlock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005481}
5482
5483/*****************************************************************************
5484 *
5485 * mac80211 entry point functions
5486 *
5487 *****************************************************************************/
5488
5489#define UCODE_READY_TIMEOUT (4 * HZ)
5490
5491/*
5492 * Not a mac80211 entry point function, but it fits in with all the
5493 * other mac80211 functions grouped here.
5494 */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005495static int il4965_mac_setup_register(struct il_priv *il,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005496 u32 max_probe_length)
5497{
5498 int ret;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005499 struct ieee80211_hw *hw = il->hw;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005500
5501 hw->rate_control_algorithm = "iwl-4965-rs";
5502
5503 /* Tell mac80211 our characteristics */
5504 hw->flags = IEEE80211_HW_SIGNAL_DBM |
5505 IEEE80211_HW_AMPDU_AGGREGATION |
5506 IEEE80211_HW_NEED_DTIM_PERIOD |
5507 IEEE80211_HW_SPECTRUM_MGMT |
5508 IEEE80211_HW_REPORTS_TX_ACK_STATUS;
5509
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005510 if (il->cfg->sku & IL_SKU_N)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005511 hw->flags |= IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS |
5512 IEEE80211_HW_SUPPORTS_STATIC_SMPS;
5513
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005514 hw->sta_data_size = sizeof(struct il_station_priv);
5515 hw->vif_data_size = sizeof(struct il_vif_priv);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005516
Stanislaw Gruszka17d6e552011-08-29 12:52:20 +02005517 hw->wiphy->interface_modes |= il->ctx.interface_modes;
5518 hw->wiphy->interface_modes |= il->ctx.exclusive_interface_modes;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005519
5520 hw->wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY |
5521 WIPHY_FLAG_DISABLE_BEACON_HINTS;
5522
5523 /*
5524 * For now, disable PS by default because it affects
5525 * RX performance significantly.
5526 */
5527 hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
5528
5529 hw->wiphy->max_scan_ssids = PROBE_OPTION_MAX;
5530 /* we create the 802.11 header and a zero-length SSID element */
5531 hw->wiphy->max_scan_ie_len = max_probe_length - 24 - 2;
5532
5533 /* Default value; 4 EDCA QOS priorities */
5534 hw->queues = 4;
5535
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005536 hw->max_listen_interval = IL_CONN_MAX_LISTEN_INTERVAL;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005537
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005538 if (il->bands[IEEE80211_BAND_2GHZ].n_channels)
5539 il->hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
5540 &il->bands[IEEE80211_BAND_2GHZ];
5541 if (il->bands[IEEE80211_BAND_5GHZ].n_channels)
5542 il->hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
5543 &il->bands[IEEE80211_BAND_5GHZ];
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005544
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005545 il_leds_init(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005546
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005547 ret = ieee80211_register_hw(il->hw);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005548 if (ret) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02005549 IL_ERR("Failed to register hw (error %d)\n", ret);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005550 return ret;
5551 }
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005552 il->mac80211_registered = 1;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005553
5554 return 0;
5555}
5556
5557
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005558int il4965_mac_start(struct ieee80211_hw *hw)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005559{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005560 struct il_priv *il = hw->priv;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005561 int ret;
5562
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005563 D_MAC80211("enter\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005564
5565 /* we should be verifying the device is ready to be opened */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005566 mutex_lock(&il->mutex);
5567 ret = __il4965_up(il);
5568 mutex_unlock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005569
5570 if (ret)
5571 return ret;
5572
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005573 if (il_is_rfkill(il))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005574 goto out;
5575
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005576 D_INFO("Start UP work done.\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005577
5578 /* Wait for START_ALIVE from Run Time ucode. Otherwise callbacks from
5579 * mac80211 will not be run successfully. */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005580 ret = wait_event_timeout(il->wait_command_queue,
5581 test_bit(STATUS_READY, &il->status),
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005582 UCODE_READY_TIMEOUT);
5583 if (!ret) {
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005584 if (!test_bit(STATUS_READY, &il->status)) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02005585 IL_ERR("START_ALIVE timeout after %dms.\n",
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005586 jiffies_to_msecs(UCODE_READY_TIMEOUT));
5587 return -ETIMEDOUT;
5588 }
5589 }
5590
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005591 il4965_led_enable(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005592
5593out:
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005594 il->is_open = 1;
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005595 D_MAC80211("leave\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005596 return 0;
5597}
5598
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005599void il4965_mac_stop(struct ieee80211_hw *hw)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005600{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005601 struct il_priv *il = hw->priv;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005602
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005603 D_MAC80211("enter\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005604
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005605 if (!il->is_open)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005606 return;
5607
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005608 il->is_open = 0;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005609
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005610 il4965_down(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005611
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005612 flush_workqueue(il->workqueue);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005613
Stanislaw Gruszkaa078a1f2011-04-28 11:51:25 +02005614 /* User space software may expect getting rfkill changes
5615 * even if interface is down */
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02005616 _il_wr(il, CSR_INT, 0xFFFFFFFF);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005617 il_enable_rfkill_int(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005618
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005619 D_MAC80211("leave\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005620}
5621
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005622void il4965_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005623{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005624 struct il_priv *il = hw->priv;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005625
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005626 D_MACDUMP("enter\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005627
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005628 D_TX("dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005629 ieee80211_get_tx_rate(hw, IEEE80211_SKB_CB(skb))->bitrate);
5630
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005631 if (il4965_tx_skb(il, skb))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005632 dev_kfree_skb_any(skb);
5633
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005634 D_MACDUMP("leave\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005635}
5636
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005637void il4965_mac_update_tkip_key(struct ieee80211_hw *hw,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005638 struct ieee80211_vif *vif,
5639 struct ieee80211_key_conf *keyconf,
5640 struct ieee80211_sta *sta,
5641 u32 iv32, u16 *phase1key)
5642{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005643 struct il_priv *il = hw->priv;
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005644 struct il_vif_priv *vif_priv = (void *)vif->drv_priv;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005645
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005646 D_MAC80211("enter\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005647
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005648 il4965_update_tkip_key(il, vif_priv->ctx, keyconf, sta,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005649 iv32, phase1key);
5650
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005651 D_MAC80211("leave\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005652}
5653
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005654int il4965_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005655 struct ieee80211_vif *vif, struct ieee80211_sta *sta,
5656 struct ieee80211_key_conf *key)
5657{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005658 struct il_priv *il = hw->priv;
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005659 struct il_vif_priv *vif_priv = (void *)vif->drv_priv;
5660 struct il_rxon_context *ctx = vif_priv->ctx;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005661 int ret;
5662 u8 sta_id;
5663 bool is_default_wep_key = false;
5664
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005665 D_MAC80211("enter\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005666
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005667 if (il->cfg->mod_params->sw_crypto) {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005668 D_MAC80211("leave - hwcrypto disabled\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005669 return -EOPNOTSUPP;
5670 }
5671
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005672 sta_id = il_sta_id_or_broadcast(il, vif_priv->ctx, sta);
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005673 if (sta_id == IL_INVALID_STATION)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005674 return -EINVAL;
5675
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005676 mutex_lock(&il->mutex);
5677 il_scan_cancel_timeout(il, 100);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005678
5679 /*
5680 * If we are getting WEP group key and we didn't receive any key mapping
5681 * so far, we are in legacy wep mode (group key only), otherwise we are
5682 * in 1X mode.
5683 * In legacy wep mode, we use another host command to the uCode.
5684 */
5685 if ((key->cipher == WLAN_CIPHER_SUITE_WEP40 ||
5686 key->cipher == WLAN_CIPHER_SUITE_WEP104) &&
5687 !sta) {
5688 if (cmd == SET_KEY)
5689 is_default_wep_key = !ctx->key_mapping_keys;
5690 else
5691 is_default_wep_key =
5692 (key->hw_key_idx == HW_KEY_DEFAULT);
5693 }
5694
5695 switch (cmd) {
5696 case SET_KEY:
5697 if (is_default_wep_key)
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005698 ret = il4965_set_default_wep_key(il,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005699 vif_priv->ctx, key);
5700 else
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005701 ret = il4965_set_dynamic_key(il, vif_priv->ctx,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005702 key, sta_id);
5703
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005704 D_MAC80211("enable hwcrypto key\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005705 break;
5706 case DISABLE_KEY:
5707 if (is_default_wep_key)
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005708 ret = il4965_remove_default_wep_key(il, ctx, key);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005709 else
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005710 ret = il4965_remove_dynamic_key(il, ctx,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005711 key, sta_id);
5712
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005713 D_MAC80211("disable hwcrypto key\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005714 break;
5715 default:
5716 ret = -EINVAL;
5717 }
5718
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005719 mutex_unlock(&il->mutex);
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005720 D_MAC80211("leave\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005721
5722 return ret;
5723}
5724
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005725int il4965_mac_ampdu_action(struct ieee80211_hw *hw,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005726 struct ieee80211_vif *vif,
5727 enum ieee80211_ampdu_mlme_action action,
5728 struct ieee80211_sta *sta, u16 tid, u16 *ssn,
5729 u8 buf_size)
5730{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005731 struct il_priv *il = hw->priv;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005732 int ret = -EINVAL;
5733
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005734 D_HT("A-MPDU action on addr %pM tid %d\n",
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005735 sta->addr, tid);
5736
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005737 if (!(il->cfg->sku & IL_SKU_N))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005738 return -EACCES;
5739
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005740 mutex_lock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005741
5742 switch (action) {
5743 case IEEE80211_AMPDU_RX_START:
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005744 D_HT("start Rx\n");
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005745 ret = il4965_sta_rx_agg_start(il, sta, tid, *ssn);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005746 break;
5747 case IEEE80211_AMPDU_RX_STOP:
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005748 D_HT("stop Rx\n");
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005749 ret = il4965_sta_rx_agg_stop(il, sta, tid);
5750 if (test_bit(STATUS_EXIT_PENDING, &il->status))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005751 ret = 0;
5752 break;
5753 case IEEE80211_AMPDU_TX_START:
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005754 D_HT("start Tx\n");
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005755 ret = il4965_tx_agg_start(il, vif, sta, tid, ssn);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005756 break;
5757 case IEEE80211_AMPDU_TX_STOP:
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005758 D_HT("stop Tx\n");
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005759 ret = il4965_tx_agg_stop(il, vif, sta, tid);
5760 if (test_bit(STATUS_EXIT_PENDING, &il->status))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005761 ret = 0;
5762 break;
5763 case IEEE80211_AMPDU_TX_OPERATIONAL:
5764 ret = 0;
5765 break;
5766 }
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005767 mutex_unlock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005768
5769 return ret;
5770}
5771
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005772int il4965_mac_sta_add(struct ieee80211_hw *hw,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005773 struct ieee80211_vif *vif,
5774 struct ieee80211_sta *sta)
5775{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005776 struct il_priv *il = hw->priv;
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005777 struct il_station_priv *sta_priv = (void *)sta->drv_priv;
5778 struct il_vif_priv *vif_priv = (void *)vif->drv_priv;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005779 bool is_ap = vif->type == NL80211_IFTYPE_STATION;
5780 int ret;
5781 u8 sta_id;
5782
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005783 D_INFO("received request to add station %pM\n",
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005784 sta->addr);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005785 mutex_lock(&il->mutex);
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005786 D_INFO("proceeding to add station %pM\n",
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005787 sta->addr);
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005788 sta_priv->common.sta_id = IL_INVALID_STATION;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005789
5790 atomic_set(&sta_priv->pending_frames, 0);
5791
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005792 ret = il_add_station_common(il, vif_priv->ctx, sta->addr,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005793 is_ap, sta, &sta_id);
5794 if (ret) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02005795 IL_ERR("Unable to add station %pM (%d)\n",
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005796 sta->addr, ret);
5797 /* Should we return success if return code is EEXIST ? */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005798 mutex_unlock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005799 return ret;
5800 }
5801
5802 sta_priv->common.sta_id = sta_id;
5803
5804 /* Initialize rate scaling */
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005805 D_INFO("Initializing rate scaling for station %pM\n",
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005806 sta->addr);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005807 il4965_rs_rate_init(il, sta, sta_id);
5808 mutex_unlock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005809
5810 return 0;
5811}
5812
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005813void il4965_mac_channel_switch(struct ieee80211_hw *hw,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005814 struct ieee80211_channel_switch *ch_switch)
5815{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005816 struct il_priv *il = hw->priv;
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005817 const struct il_channel_info *ch_info;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005818 struct ieee80211_conf *conf = &hw->conf;
5819 struct ieee80211_channel *channel = ch_switch->channel;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005820 struct il_ht_config *ht_conf = &il->current_ht_config;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005821
Stanislaw Gruszka7c2cde22011-11-15 11:29:04 +01005822 struct il_rxon_context *ctx = &il->ctx;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005823 u16 ch;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005824
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005825 D_MAC80211("enter\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005826
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005827 mutex_lock(&il->mutex);
Stanislaw Gruszka28a6e572011-04-28 11:51:32 +02005828
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005829 if (il_is_rfkill(il))
Stanislaw Gruszka28a6e572011-04-28 11:51:32 +02005830 goto out;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005831
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005832 if (test_bit(STATUS_EXIT_PENDING, &il->status) ||
5833 test_bit(STATUS_SCANNING, &il->status) ||
5834 test_bit(STATUS_CHANNEL_SWITCH_PENDING, &il->status))
Stanislaw Gruszka28a6e572011-04-28 11:51:32 +02005835 goto out;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005836
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005837 if (!il_is_associated_ctx(ctx))
Stanislaw Gruszka28a6e572011-04-28 11:51:32 +02005838 goto out;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005839
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005840 if (!il->cfg->ops->lib->set_channel_switch)
Stanislaw Gruszka7f1f9742011-06-08 15:28:29 +02005841 goto out;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005842
Stanislaw Gruszka7f1f9742011-06-08 15:28:29 +02005843 ch = channel->hw_value;
5844 if (le16_to_cpu(ctx->active.channel) == ch)
5845 goto out;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005846
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005847 ch_info = il_get_channel_info(il, channel->band, ch);
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005848 if (!il_is_channel_valid(ch_info)) {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005849 D_MAC80211("invalid channel\n");
Stanislaw Gruszka7f1f9742011-06-08 15:28:29 +02005850 goto out;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005851 }
Stanislaw Gruszka7f1f9742011-06-08 15:28:29 +02005852
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005853 spin_lock_irq(&il->lock);
Stanislaw Gruszka7f1f9742011-06-08 15:28:29 +02005854
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005855 il->current_ht_config.smps = conf->smps_mode;
Stanislaw Gruszka7f1f9742011-06-08 15:28:29 +02005856
5857 /* Configure HT40 channels */
5858 ctx->ht.enabled = conf_is_ht(conf);
5859 if (ctx->ht.enabled) {
5860 if (conf_is_ht40_minus(conf)) {
5861 ctx->ht.extension_chan_offset =
5862 IEEE80211_HT_PARAM_CHA_SEC_BELOW;
5863 ctx->ht.is_40mhz = true;
5864 } else if (conf_is_ht40_plus(conf)) {
5865 ctx->ht.extension_chan_offset =
5866 IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
5867 ctx->ht.is_40mhz = true;
5868 } else {
5869 ctx->ht.extension_chan_offset =
5870 IEEE80211_HT_PARAM_CHA_SEC_NONE;
5871 ctx->ht.is_40mhz = false;
5872 }
5873 } else
5874 ctx->ht.is_40mhz = false;
5875
5876 if ((le16_to_cpu(ctx->staging.channel) != ch))
5877 ctx->staging.flags = 0;
5878
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005879 il_set_rxon_channel(il, channel, ctx);
5880 il_set_rxon_ht(il, ht_conf);
5881 il_set_flags_for_band(il, ctx, channel->band, ctx->vif);
Stanislaw Gruszka7f1f9742011-06-08 15:28:29 +02005882
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005883 spin_unlock_irq(&il->lock);
Stanislaw Gruszka7f1f9742011-06-08 15:28:29 +02005884
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005885 il_set_rate(il);
Stanislaw Gruszka7f1f9742011-06-08 15:28:29 +02005886 /*
5887 * at this point, staging_rxon has the
5888 * configuration for channel switch
5889 */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005890 set_bit(STATUS_CHANNEL_SWITCH_PENDING, &il->status);
5891 il->switch_channel = cpu_to_le16(ch);
5892 if (il->cfg->ops->lib->set_channel_switch(il, ch_switch)) {
5893 clear_bit(STATUS_CHANNEL_SWITCH_PENDING, &il->status);
5894 il->switch_channel = 0;
Stanislaw Gruszka7f1f9742011-06-08 15:28:29 +02005895 ieee80211_chswitch_done(ctx->vif, false);
5896 }
5897
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005898out:
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005899 mutex_unlock(&il->mutex);
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005900 D_MAC80211("leave\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005901}
5902
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005903void il4965_configure_filter(struct ieee80211_hw *hw,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005904 unsigned int changed_flags,
5905 unsigned int *total_flags,
5906 u64 multicast)
5907{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005908 struct il_priv *il = hw->priv;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005909 __le32 filter_or = 0, filter_nand = 0;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005910
5911#define CHK(test, flag) do { \
5912 if (*total_flags & (test)) \
5913 filter_or |= (flag); \
5914 else \
5915 filter_nand |= (flag); \
5916 } while (0)
5917
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005918 D_MAC80211("Enter: changed: 0x%x, total: 0x%x\n",
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005919 changed_flags, *total_flags);
5920
5921 CHK(FIF_OTHER_BSS | FIF_PROMISC_IN_BSS, RXON_FILTER_PROMISC_MSK);
5922 /* Setting _just_ RXON_FILTER_CTL2HOST_MSK causes FH errors */
5923 CHK(FIF_CONTROL, RXON_FILTER_CTL2HOST_MSK | RXON_FILTER_PROMISC_MSK);
5924 CHK(FIF_BCN_PRBRESP_PROMISC, RXON_FILTER_BCON_AWARE_MSK);
5925
5926#undef CHK
5927
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005928 mutex_lock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005929
Stanislaw Gruszka17d6e552011-08-29 12:52:20 +02005930 il->ctx.staging.filter_flags &= ~filter_nand;
5931 il->ctx.staging.filter_flags |= filter_or;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005932
Stanislaw Gruszka17d6e552011-08-29 12:52:20 +02005933 /*
5934 * Not committing directly because hardware can perform a scan,
5935 * but we'll eventually commit the filter flags change anyway.
5936 */
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005937
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005938 mutex_unlock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005939
5940 /*
5941 * Receiving all multicast frames is always enabled by the
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005942 * default flags setup in il_connection_init_rx_config()
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005943 * since we currently do not support programming multicast
5944 * filters into the device.
5945 */
5946 *total_flags &= FIF_OTHER_BSS | FIF_ALLMULTI | FIF_PROMISC_IN_BSS |
5947 FIF_BCN_PRBRESP_PROMISC | FIF_CONTROL;
5948}
5949
5950/*****************************************************************************
5951 *
5952 * driver setup and teardown
5953 *
5954 *****************************************************************************/
5955
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005956static void il4965_bg_txpower_work(struct work_struct *work)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005957{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005958 struct il_priv *il = container_of(work, struct il_priv,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005959 txpower_work);
5960
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005961 mutex_lock(&il->mutex);
Stanislaw Gruszkaf3257572011-04-28 11:36:54 +02005962
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005963 /* If a scan happened to start before we got here
Stanislaw Gruszkaebf0d902011-08-26 15:43:47 +02005964 * then just return; the stats notification will
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005965 * kick off another scheduled work to compensate for
5966 * any temperature delta we missed here. */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005967 if (test_bit(STATUS_EXIT_PENDING, &il->status) ||
5968 test_bit(STATUS_SCANNING, &il->status))
Stanislaw Gruszkaf3257572011-04-28 11:36:54 +02005969 goto out;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005970
5971 /* Regardless of if we are associated, we must reconfigure the
5972 * TX power since frames can be sent on non-radar channels while
5973 * not associated */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005974 il->cfg->ops->lib->send_tx_power(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005975
5976 /* Update last_temperature to keep is_calib_needed from running
5977 * when it isn't needed... */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005978 il->last_temperature = il->temperature;
Stanislaw Gruszkaf3257572011-04-28 11:36:54 +02005979out:
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005980 mutex_unlock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005981}
5982
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005983static void il4965_setup_deferred_work(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005984{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005985 il->workqueue = create_singlethread_workqueue(DRV_NAME);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005986
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005987 init_waitqueue_head(&il->wait_command_queue);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005988
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005989 INIT_WORK(&il->restart, il4965_bg_restart);
5990 INIT_WORK(&il->rx_replenish, il4965_bg_rx_replenish);
5991 INIT_WORK(&il->run_time_calib_work, il4965_bg_run_time_calib_work);
5992 INIT_DELAYED_WORK(&il->init_alive_start, il4965_bg_init_alive_start);
5993 INIT_DELAYED_WORK(&il->alive_start, il4965_bg_alive_start);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005994
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005995 il_setup_scan_deferred_work(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005996
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005997 INIT_WORK(&il->txpower_work, il4965_bg_txpower_work);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005998
Stanislaw Gruszkaebf0d902011-08-26 15:43:47 +02005999 init_timer(&il->stats_periodic);
6000 il->stats_periodic.data = (unsigned long)il;
6001 il->stats_periodic.function = il4965_bg_stats_periodic;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006002
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006003 init_timer(&il->watchdog);
6004 il->watchdog.data = (unsigned long)il;
6005 il->watchdog.function = il_bg_watchdog;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006006
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006007 tasklet_init(&il->irq_tasklet, (void (*)(unsigned long))
6008 il4965_irq_tasklet, (unsigned long)il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006009}
6010
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006011static void il4965_cancel_deferred_work(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006012{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006013 cancel_work_sync(&il->txpower_work);
6014 cancel_delayed_work_sync(&il->init_alive_start);
6015 cancel_delayed_work(&il->alive_start);
6016 cancel_work_sync(&il->run_time_calib_work);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006017
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006018 il_cancel_scan_deferred_work(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006019
Stanislaw Gruszkaebf0d902011-08-26 15:43:47 +02006020 del_timer_sync(&il->stats_periodic);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006021}
6022
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006023static void il4965_init_hw_rates(struct il_priv *il,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006024 struct ieee80211_rate *rates)
6025{
6026 int i;
6027
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +02006028 for (i = 0; i < RATE_COUNT_LEGACY; i++) {
Stanislaw Gruszkad2ddf6212011-08-16 14:17:04 +02006029 rates[i].bitrate = il_rates[i].ieee * 5;
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +01006030 rates[i].hw_value = i; /* Rate scaling will work on idxes */
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006031 rates[i].hw_value_short = i;
6032 rates[i].flags = 0;
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006033 if ((i >= IL_FIRST_CCK_RATE) && (i <= IL_LAST_CCK_RATE)) {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006034 /*
6035 * If CCK != 1M then set short preamble rate flag.
6036 */
6037 rates[i].flags |=
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +02006038 (il_rates[i].plcp == RATE_1M_PLCP) ?
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006039 0 : IEEE80211_RATE_SHORT_PREAMBLE;
6040 }
6041 }
6042}
6043/*
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006044 * Acquire il->lock before calling this function !
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006045 */
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +01006046void il4965_set_wr_ptrs(struct il_priv *il, int txq_id, u32 idx)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006047{
Stanislaw Gruszka0c1a94e2011-08-24 17:37:16 +02006048 il_wr(il, HBUS_TARG_WRPTR,
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +01006049 (idx & 0xff) | (txq_id << 8));
6050 il_wr_prph(il, IL49_SCD_QUEUE_RDPTR(txq_id), idx);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006051}
6052
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006053void il4965_tx_queue_set_status(struct il_priv *il,
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006054 struct il_tx_queue *txq,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006055 int tx_fifo_id, int scd_retry)
6056{
6057 int txq_id = txq->q.id;
6058
6059 /* Find out whether to activate Tx queue */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006060 int active = test_bit(txq_id, &il->txq_ctx_active_msk) ? 1 : 0;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006061
6062 /* Set up and activate */
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01006063 il_wr_prph(il, IL49_SCD_QUEUE_STATUS_BITS(txq_id),
6064 (active << IL49_SCD_QUEUE_STTS_REG_POS_ACTIVE) |
6065 (tx_fifo_id << IL49_SCD_QUEUE_STTS_REG_POS_TXF) |
6066 (scd_retry << IL49_SCD_QUEUE_STTS_REG_POS_WSL) |
6067 (scd_retry << IL49_SCD_QUEUE_STTS_REG_POS_SCD_ACK) |
6068 IL49_SCD_QUEUE_STTS_REG_MSK);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006069
6070 txq->sched_retry = scd_retry;
6071
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01006072 D_INFO("%s %s Queue %d on AC %d\n",
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006073 active ? "Activate" : "Deactivate",
6074 scd_retry ? "BA" : "AC", txq_id, tx_fifo_id);
6075}
6076
6077
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006078static int il4965_init_drv(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006079{
6080 int ret;
6081
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006082 spin_lock_init(&il->sta_lock);
6083 spin_lock_init(&il->hcmd_lock);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006084
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006085 INIT_LIST_HEAD(&il->free_frames);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006086
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006087 mutex_init(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006088
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006089 il->ieee_channels = NULL;
6090 il->ieee_rates = NULL;
6091 il->band = IEEE80211_BAND_2GHZ;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006092
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006093 il->iw_mode = NL80211_IFTYPE_STATION;
6094 il->current_ht_config.smps = IEEE80211_SMPS_STATIC;
6095 il->missed_beacon_threshold = IL_MISSED_BEACON_THRESHOLD_DEF;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006096
6097 /* initialize force reset */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006098 il->force_reset.reset_duration = IL_DELAY_NEXT_FORCE_FW_RELOAD;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006099
6100 /* Choose which receivers/antennas to use */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006101 if (il->cfg->ops->hcmd->set_rxon_chain)
6102 il->cfg->ops->hcmd->set_rxon_chain(il,
Stanislaw Gruszka7c2cde22011-11-15 11:29:04 +01006103 &il->ctx);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006104
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006105 il_init_scan_params(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006106
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006107 ret = il_init_channel_map(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006108 if (ret) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02006109 IL_ERR("initializing regulatory failed: %d\n", ret);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006110 goto err;
6111 }
6112
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006113 ret = il_init_geos(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006114 if (ret) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02006115 IL_ERR("initializing geos failed: %d\n", ret);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006116 goto err_free_channel_map;
6117 }
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006118 il4965_init_hw_rates(il, il->ieee_rates);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006119
6120 return 0;
6121
6122err_free_channel_map:
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006123 il_free_channel_map(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006124err:
6125 return ret;
6126}
6127
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006128static void il4965_uninit_drv(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006129{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006130 il4965_calib_free_results(il);
6131 il_free_geos(il);
6132 il_free_channel_map(il);
6133 kfree(il->scan_cmd);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006134}
6135
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006136static void il4965_hw_detect(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006137{
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02006138 il->hw_rev = _il_rd(il, CSR_HW_REV);
6139 il->hw_wa_rev = _il_rd(il, CSR_HW_REV_WA_REG);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006140 il->rev_id = il->pci_dev->revision;
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01006141 D_INFO("HW Revision ID = 0x%X\n", il->rev_id);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006142}
6143
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006144static int il4965_set_hw_params(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006145{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006146 il->hw_params.max_rxq_size = RX_QUEUE_SIZE;
6147 il->hw_params.max_rxq_log = RX_QUEUE_SIZE_LOG;
6148 if (il->cfg->mod_params->amsdu_size_8K)
6149 il->hw_params.rx_page_order = get_order(IL_RX_BUF_SIZE_8K);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006150 else
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006151 il->hw_params.rx_page_order = get_order(IL_RX_BUF_SIZE_4K);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006152
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006153 il->hw_params.max_beacon_itrvl = IL_MAX_UCODE_BEACON_INTERVAL;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006154
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006155 if (il->cfg->mod_params->disable_11n)
6156 il->cfg->sku &= ~IL_SKU_N;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006157
6158 /* Device-specific setup */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006159 return il->cfg->ops->lib->set_hw_params(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006160}
6161
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006162static const u8 il4965_bss_ac_to_fifo[] = {
6163 IL_TX_FIFO_VO,
6164 IL_TX_FIFO_VI,
6165 IL_TX_FIFO_BE,
6166 IL_TX_FIFO_BK,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006167};
6168
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006169static const u8 il4965_bss_ac_to_queue[] = {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006170 0, 1, 2, 3,
6171};
6172
6173static int
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006174il4965_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006175{
Stanislaw Gruszka7c2cde22011-11-15 11:29:04 +01006176 int err = 0;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006177 struct il_priv *il;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006178 struct ieee80211_hw *hw;
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006179 struct il_cfg *cfg = (struct il_cfg *)(ent->driver_data);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006180 unsigned long flags;
6181 u16 pci_cmd;
6182
6183 /************************
6184 * 1. Allocating HW data
6185 ************************/
6186
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006187 hw = il_alloc_all(cfg);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006188 if (!hw) {
6189 err = -ENOMEM;
6190 goto out;
6191 }
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006192 il = hw->priv;
6193 /* At this point both hw and il are allocated. */
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006194
Stanislaw Gruszka7c2cde22011-11-15 11:29:04 +01006195 il->ctx.ctxid = 0;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006196
Stanislaw Gruszka7c2cde22011-11-15 11:29:04 +01006197 il->ctx.always_active = true;
6198 il->ctx.is_active = true;
6199 il->ctx.rxon_cmd = REPLY_RXON;
6200 il->ctx.rxon_timing_cmd = REPLY_RXON_TIMING;
6201 il->ctx.rxon_assoc_cmd = REPLY_RXON_ASSOC;
6202 il->ctx.qos_cmd = REPLY_QOS_PARAM;
6203 il->ctx.ap_sta_id = IL_AP_ID;
6204 il->ctx.wep_key_cmd = REPLY_WEPKEY;
6205 il->ctx.ac_to_fifo = il4965_bss_ac_to_fifo;
6206 il->ctx.ac_to_queue = il4965_bss_ac_to_queue;
6207 il->ctx.exclusive_interface_modes =
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006208 BIT(NL80211_IFTYPE_ADHOC);
Stanislaw Gruszka7c2cde22011-11-15 11:29:04 +01006209 il->ctx.interface_modes =
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006210 BIT(NL80211_IFTYPE_STATION);
Stanislaw Gruszka7c2cde22011-11-15 11:29:04 +01006211 il->ctx.ap_devtype = RXON_DEV_TYPE_AP;
6212 il->ctx.ibss_devtype = RXON_DEV_TYPE_IBSS;
6213 il->ctx.station_devtype = RXON_DEV_TYPE_ESS;
6214 il->ctx.unused_devtype = RXON_DEV_TYPE_ESS;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006215
6216 SET_IEEE80211_DEV(hw, &pdev->dev);
6217
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01006218 D_INFO("*** LOAD DRIVER ***\n");
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006219 il->cfg = cfg;
6220 il->pci_dev = pdev;
6221 il->inta_mask = CSR_INI_SET_MASK;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006222
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006223 if (il_alloc_traffic_mem(il))
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02006224 IL_ERR("Not enough memory to generate traffic log\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006225
6226 /**************************
6227 * 2. Initializing PCI bus
6228 **************************/
6229 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
6230 PCIE_LINK_STATE_CLKPM);
6231
6232 if (pci_enable_device(pdev)) {
6233 err = -ENODEV;
6234 goto out_ieee80211_free_hw;
6235 }
6236
6237 pci_set_master(pdev);
6238
6239 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(36));
6240 if (!err)
6241 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(36));
6242 if (err) {
6243 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
6244 if (!err)
6245 err = pci_set_consistent_dma_mask(pdev,
6246 DMA_BIT_MASK(32));
6247 /* both attempts failed: */
6248 if (err) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02006249 IL_WARN("No suitable DMA available.\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006250 goto out_pci_disable_device;
6251 }
6252 }
6253
6254 err = pci_request_regions(pdev, DRV_NAME);
6255 if (err)
6256 goto out_pci_disable_device;
6257
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006258 pci_set_drvdata(pdev, il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006259
6260
6261 /***********************
6262 * 3. Read REV register
6263 ***********************/
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006264 il->hw_base = pci_iomap(pdev, 0, 0);
6265 if (!il->hw_base) {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006266 err = -ENODEV;
6267 goto out_pci_release_regions;
6268 }
6269
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01006270 D_INFO("pci_resource_len = 0x%08llx\n",
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006271 (unsigned long long) pci_resource_len(pdev, 0));
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01006272 D_INFO("pci_resource_base = %p\n", il->hw_base);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006273
6274 /* these spin locks will be used in apm_ops.init and EEPROM access
6275 * we should init now
6276 */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006277 spin_lock_init(&il->reg_lock);
6278 spin_lock_init(&il->lock);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006279
6280 /*
6281 * stop and reset the on-board processor just in case it is in a
6282 * strange state ... like being left stranded by a primary kernel
6283 * and this is now the kdump kernel trying to start up
6284 */
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02006285 _il_wr(il, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006286
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006287 il4965_hw_detect(il);
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02006288 IL_INFO("Detected %s, REV=0x%X\n",
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006289 il->cfg->name, il->hw_rev);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006290
6291 /* We disable the RETRY_TIMEOUT register (0x41) to keep
6292 * PCI Tx retries from interfering with C3 CPU state */
6293 pci_write_config_byte(pdev, PCI_CFG_RETRY_TIMEOUT, 0x00);
6294
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006295 il4965_prepare_card_hw(il);
6296 if (!il->hw_ready) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02006297 IL_WARN("Failed, HW not ready\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006298 goto out_iounmap;
6299 }
6300
6301 /*****************
6302 * 4. Read EEPROM
6303 *****************/
6304 /* Read the EEPROM */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006305 err = il_eeprom_init(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006306 if (err) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02006307 IL_ERR("Unable to init EEPROM\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006308 goto out_iounmap;
6309 }
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006310 err = il4965_eeprom_check_version(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006311 if (err)
6312 goto out_free_eeprom;
6313
6314 if (err)
6315 goto out_free_eeprom;
6316
6317 /* extract MAC Address */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006318 il4965_eeprom_get_mac(il, il->addresses[0].addr);
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01006319 D_INFO("MAC address: %pM\n", il->addresses[0].addr);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006320 il->hw->wiphy->addresses = il->addresses;
6321 il->hw->wiphy->n_addresses = 1;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006322
6323 /************************
6324 * 5. Setup HW constants
6325 ************************/
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006326 if (il4965_set_hw_params(il)) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02006327 IL_ERR("failed to set hw parameters\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006328 goto out_free_eeprom;
6329 }
6330
6331 /*******************
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006332 * 6. Setup il
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006333 *******************/
6334
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006335 err = il4965_init_drv(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006336 if (err)
6337 goto out_free_eeprom;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006338 /* At this point both hw and il are initialized. */
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006339
6340 /********************
6341 * 7. Setup services
6342 ********************/
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006343 spin_lock_irqsave(&il->lock, flags);
6344 il_disable_interrupts(il);
6345 spin_unlock_irqrestore(&il->lock, flags);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006346
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006347 pci_enable_msi(il->pci_dev);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006348
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006349 err = request_irq(il->pci_dev->irq, il_isr,
6350 IRQF_SHARED, DRV_NAME, il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006351 if (err) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02006352 IL_ERR("Error allocating IRQ %d\n", il->pci_dev->irq);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006353 goto out_disable_msi;
6354 }
6355
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006356 il4965_setup_deferred_work(il);
6357 il4965_setup_rx_handlers(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006358
6359 /*********************************************
6360 * 8. Enable interrupts and read RFKILL state
6361 *********************************************/
6362
Stanislaw Gruszkaa078a1f2011-04-28 11:51:25 +02006363 /* enable rfkill interrupt: hw bug w/a */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006364 pci_read_config_word(il->pci_dev, PCI_COMMAND, &pci_cmd);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006365 if (pci_cmd & PCI_COMMAND_INTX_DISABLE) {
6366 pci_cmd &= ~PCI_COMMAND_INTX_DISABLE;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006367 pci_write_config_word(il->pci_dev, PCI_COMMAND, pci_cmd);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006368 }
6369
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006370 il_enable_rfkill_int(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006371
6372 /* If platform's RF_KILL switch is NOT set to KILL */
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02006373 if (_il_rd(il, CSR_GP_CNTRL) &
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006374 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006375 clear_bit(STATUS_RF_KILL_HW, &il->status);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006376 else
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006377 set_bit(STATUS_RF_KILL_HW, &il->status);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006378
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006379 wiphy_rfkill_set_hw_state(il->hw->wiphy,
6380 test_bit(STATUS_RF_KILL_HW, &il->status));
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006381
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006382 il_power_initialize(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006383
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006384 init_completion(&il->_4965.firmware_loading_complete);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006385
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006386 err = il4965_request_firmware(il, true);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006387 if (err)
6388 goto out_destroy_workqueue;
6389
6390 return 0;
6391
6392 out_destroy_workqueue:
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006393 destroy_workqueue(il->workqueue);
6394 il->workqueue = NULL;
6395 free_irq(il->pci_dev->irq, il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006396 out_disable_msi:
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006397 pci_disable_msi(il->pci_dev);
6398 il4965_uninit_drv(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006399 out_free_eeprom:
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006400 il_eeprom_free(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006401 out_iounmap:
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006402 pci_iounmap(pdev, il->hw_base);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006403 out_pci_release_regions:
6404 pci_set_drvdata(pdev, NULL);
6405 pci_release_regions(pdev);
6406 out_pci_disable_device:
6407 pci_disable_device(pdev);
6408 out_ieee80211_free_hw:
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006409 il_free_traffic_mem(il);
6410 ieee80211_free_hw(il->hw);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006411 out:
6412 return err;
6413}
6414
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006415static void __devexit il4965_pci_remove(struct pci_dev *pdev)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006416{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006417 struct il_priv *il = pci_get_drvdata(pdev);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006418 unsigned long flags;
6419
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006420 if (!il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006421 return;
6422
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006423 wait_for_completion(&il->_4965.firmware_loading_complete);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006424
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01006425 D_INFO("*** UNLOAD DRIVER ***\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006426
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006427 il_dbgfs_unregister(il);
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006428 sysfs_remove_group(&pdev->dev.kobj, &il_attribute_group);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006429
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006430 /* ieee80211_unregister_hw call wil cause il_mac_stop to
6431 * to be called and il4965_down since we are removing the device
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006432 * we need to set STATUS_EXIT_PENDING bit.
6433 */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006434 set_bit(STATUS_EXIT_PENDING, &il->status);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006435
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006436 il_leds_exit(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006437
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006438 if (il->mac80211_registered) {
6439 ieee80211_unregister_hw(il->hw);
6440 il->mac80211_registered = 0;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006441 } else {
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006442 il4965_down(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006443 }
6444
6445 /*
6446 * Make sure device is reset to low power before unloading driver.
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006447 * This may be redundant with il4965_down(), but there are paths to
6448 * run il4965_down() without calling apm_ops.stop(), and there are
6449 * paths to avoid running il4965_down() at all before leaving driver.
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006450 * This (inexpensive) call *makes sure* device is reset.
6451 */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006452 il_apm_stop(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006453
6454 /* make sure we flush any pending irq or
6455 * tasklet for the driver
6456 */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006457 spin_lock_irqsave(&il->lock, flags);
6458 il_disable_interrupts(il);
6459 spin_unlock_irqrestore(&il->lock, flags);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006460
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006461 il4965_synchronize_irq(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006462
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006463 il4965_dealloc_ucode_pci(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006464
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006465 if (il->rxq.bd)
6466 il4965_rx_queue_free(il, &il->rxq);
6467 il4965_hw_txq_ctx_free(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006468
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006469 il_eeprom_free(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006470
6471
6472 /*netif_stop_queue(dev); */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006473 flush_workqueue(il->workqueue);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006474
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006475 /* ieee80211_unregister_hw calls il_mac_stop, which flushes
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006476 * il->workqueue... so we can't take down the workqueue
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006477 * until now... */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006478 destroy_workqueue(il->workqueue);
6479 il->workqueue = NULL;
6480 il_free_traffic_mem(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006481
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006482 free_irq(il->pci_dev->irq, il);
6483 pci_disable_msi(il->pci_dev);
6484 pci_iounmap(pdev, il->hw_base);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006485 pci_release_regions(pdev);
6486 pci_disable_device(pdev);
6487 pci_set_drvdata(pdev, NULL);
6488
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006489 il4965_uninit_drv(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006490
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006491 dev_kfree_skb(il->beacon_skb);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006492
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006493 ieee80211_free_hw(il->hw);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006494}
6495
6496/*
6497 * Activate/Deactivate Tx DMA/FIFO channels according tx fifos mask
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006498 * must be called under il->lock and mac access
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006499 */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02006500void il4965_txq_set_sched(struct il_priv *il, u32 mask)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006501{
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01006502 il_wr_prph(il, IL49_SCD_TXFACT, mask);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006503}
6504
6505/*****************************************************************************
6506 *
6507 * driver and module entry point
6508 *
6509 *****************************************************************************/
6510
6511/* Hardware specific file defines the PCI IDs table for that hardware module */
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006512static DEFINE_PCI_DEVICE_TABLE(il4965_hw_card_ids) = {
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006513 {IL_PCI_DEVICE(0x4229, PCI_ANY_ID, il4965_cfg)},
6514 {IL_PCI_DEVICE(0x4230, PCI_ANY_ID, il4965_cfg)},
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006515 {0}
6516};
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006517MODULE_DEVICE_TABLE(pci, il4965_hw_card_ids);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006518
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006519static struct pci_driver il4965_driver = {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006520 .name = DRV_NAME,
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006521 .id_table = il4965_hw_card_ids,
6522 .probe = il4965_pci_probe,
6523 .remove = __devexit_p(il4965_pci_remove),
6524 .driver.pm = IL_LEGACY_PM_OPS,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006525};
6526
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006527static int __init il4965_init(void)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006528{
6529
6530 int ret;
6531 pr_info(DRV_DESCRIPTION ", " DRV_VERSION "\n");
6532 pr_info(DRV_COPYRIGHT "\n");
6533
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006534 ret = il4965_rate_control_register();
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006535 if (ret) {
6536 pr_err("Unable to register rate control algorithm: %d\n", ret);
6537 return ret;
6538 }
6539
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006540 ret = pci_register_driver(&il4965_driver);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006541 if (ret) {
6542 pr_err("Unable to initialize PCI module\n");
6543 goto error_register;
6544 }
6545
6546 return ret;
6547
6548error_register:
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006549 il4965_rate_control_unregister();
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006550 return ret;
6551}
6552
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006553static void __exit il4965_exit(void)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006554{
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006555 pci_unregister_driver(&il4965_driver);
6556 il4965_rate_control_unregister();
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006557}
6558
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006559module_exit(il4965_exit);
6560module_init(il4965_init);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006561
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01006562#ifdef CONFIG_IWLEGACY_DEBUG
Stanislaw Gruszkad2ddf6212011-08-16 14:17:04 +02006563module_param_named(debug, il_debug_level, uint, S_IRUGO | S_IWUSR);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006564MODULE_PARM_DESC(debug, "debug output mask");
6565#endif
6566
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006567module_param_named(swcrypto, il4965_mod_params.sw_crypto, int, S_IRUGO);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006568MODULE_PARM_DESC(swcrypto, "using crypto in software (default 0 [hardware])");
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006569module_param_named(queues_num, il4965_mod_params.num_of_queues, int, S_IRUGO);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006570MODULE_PARM_DESC(queues_num, "number of hw queues.");
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006571module_param_named(11n_disable, il4965_mod_params.disable_11n, int, S_IRUGO);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006572MODULE_PARM_DESC(11n_disable, "disable 11n functionality");
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006573module_param_named(amsdu_size_8K, il4965_mod_params.amsdu_size_8K,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006574 int, S_IRUGO);
6575MODULE_PARM_DESC(amsdu_size_8K, "enable 8K amsdu size");
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02006576module_param_named(fw_restart, il4965_mod_params.restart_fw, int, S_IRUGO);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08006577MODULE_PARM_DESC(fw_restart, "restart firmware in case of error");