iwl-4965-lib.c 33.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
/******************************************************************************
 *
 * GPL LICENSE SUMMARY
 *
 * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of version 2 of the GNU General Public License as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
 * USA
 *
 * The full GNU General Public License is included in this distribution
 * in the file called LICENSE.GPL.
 *
 * Contact Information:
 *  Intel Linux Wireless <ilw@linux.intel.com>
 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
 *
 *****************************************************************************/
#include <linux/etherdevice.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/sched.h>

#include "iwl-dev.h"
#include "iwl-core.h"
#include "iwl-io.h"
#include "iwl-helpers.h"
#include "iwl-4965-hw.h"
#include "iwl-4965.h"
#include "iwl-sta.h"

S
Stanislaw Gruszka 已提交
43
void il4965_check_abort_status(struct il_priv *il,
44 45 46
			    u8 frame_count, u32 status)
{
	if (frame_count == 1 && status == TX_STATUS_FAIL_RFKILL_FLUSH) {
47
		IL_ERR("Tx flush command to flush out all frames\n");
S
Stanislaw Gruszka 已提交
48 49
		if (!test_bit(STATUS_EXIT_PENDING, &il->status))
			queue_work(il->workqueue, &il->tx_flush);
50 51 52 53 54 55
	}
}

/*
 * EEPROM
 */
S
Stanislaw Gruszka 已提交
56
struct il_mod_params il4965_mod_params = {
57 58 59 60 61
	.amsdu_size_8K = 1,
	.restart_fw = 1,
	/* the rest are 0 by default */
};

S
Stanislaw Gruszka 已提交
62
void il4965_rx_queue_reset(struct il_priv *il, struct il_rx_queue *rxq)
63 64 65 66 67 68 69 70 71 72 73
{
	unsigned long flags;
	int i;
	spin_lock_irqsave(&rxq->lock, flags);
	INIT_LIST_HEAD(&rxq->rx_free);
	INIT_LIST_HEAD(&rxq->rx_used);
	/* Fill the rx_used queue with _all_ of the Rx buffers */
	for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++) {
		/* In the reset function, these buffers may have been allocated
		 * to an SKB, so we need to unmap and free potential storage */
		if (rxq->pool[i].page != NULL) {
S
Stanislaw Gruszka 已提交
74 75
			pci_unmap_page(il->pci_dev, rxq->pool[i].page_dma,
				PAGE_SIZE << il->hw_params.rx_page_order,
76
				PCI_DMA_FROMDEVICE);
S
Stanislaw Gruszka 已提交
77
			__il_free_pages(il, rxq->pool[i].page);
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
			rxq->pool[i].page = NULL;
		}
		list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
	}

	for (i = 0; i < RX_QUEUE_SIZE; i++)
		rxq->queue[i] = NULL;

	/* Set us so that we have processed and used all buffers, but have
	 * not restocked the Rx queue with fresh buffers */
	rxq->read = rxq->write = 0;
	rxq->write_actual = 0;
	rxq->free_count = 0;
	spin_unlock_irqrestore(&rxq->lock, flags);
}

S
Stanislaw Gruszka 已提交
94
int il4965_rx_init(struct il_priv *il, struct il_rx_queue *rxq)
95 96 97 98 99
{
	u32 rb_size;
	const u32 rfdnlog = RX_QUEUE_SIZE_LOG; /* 256 RBDs */
	u32 rb_timeout = 0;

S
Stanislaw Gruszka 已提交
100
	if (il->cfg->mod_params->amsdu_size_8K)
101 102 103 104 105
		rb_size = FH_RCSR_RX_CONFIG_REG_VAL_RB_SIZE_8K;
	else
		rb_size = FH_RCSR_RX_CONFIG_REG_VAL_RB_SIZE_4K;

	/* Stop Rx DMA */
106
	il_wr(il, FH_MEM_RCSR_CHNL0_CONFIG_REG, 0);
107 108

	/* Reset driver's Rx queue write index */
109
	il_wr(il, FH_RSCSR_CHNL0_RBDCB_WPTR_REG, 0);
110 111

	/* Tell device where to find RBD circular buffer in DRAM */
112
	il_wr(il, FH_RSCSR_CHNL0_RBDCB_BASE_REG,
113 114 115
			   (u32)(rxq->bd_dma >> 8));

	/* Tell device where in DRAM to update its Rx status */
116
	il_wr(il, FH_RSCSR_CHNL0_STTS_WPTR_REG,
117 118 119 120 121 122 123 124
			   rxq->rb_stts_dma >> 4);

	/* Enable Rx DMA
	 * Direct rx interrupts to hosts
	 * Rx buffer size 4 or 8k
	 * RB timeout 0x10
	 * 256 RBDs
	 */
125
	il_wr(il, FH_MEM_RCSR_CHNL0_CONFIG_REG,
126 127 128 129 130 131 132 133
			   FH_RCSR_RX_CONFIG_CHNL_EN_ENABLE_VAL |
			   FH_RCSR_CHNL0_RX_CONFIG_IRQ_DEST_INT_HOST_VAL |
			   FH_RCSR_CHNL0_RX_CONFIG_SINGLE_FRAME_MSK |
			   rb_size|
			   (rb_timeout << FH_RCSR_RX_CONFIG_REG_IRQ_RBTH_POS)|
			   (rfdnlog << FH_RCSR_RX_CONFIG_RBDCB_SIZE_POS));

	/* Set interrupt coalescing timer to default (2048 usecs) */
S
Stanislaw Gruszka 已提交
134
	il_write8(il, CSR_INT_COALESCING, IL_HOST_INT_TIMEOUT_DEF);
135 136 137 138

	return 0;
}

S
Stanislaw Gruszka 已提交
139
static void il4965_set_pwr_vmain(struct il_priv *il)
140 141 142 143 144
{
/*
 * (for documentation purposes)
 * to set power to V_AUX, do:

S
Stanislaw Gruszka 已提交
145 146
		if (pci_pme_capable(il->pci_dev, PCI_D3cold))
			il_set_bits_mask_prph(il, APMG_PS_CTRL_REG,
147 148 149 150
					       APMG_PS_CTRL_VAL_PWR_SRC_VAUX,
					       ~APMG_PS_CTRL_MSK_PWR_SRC);
 */

S
Stanislaw Gruszka 已提交
151
	il_set_bits_mask_prph(il, APMG_PS_CTRL_REG,
152 153 154 155
			       APMG_PS_CTRL_VAL_PWR_SRC_VMAIN,
			       ~APMG_PS_CTRL_MSK_PWR_SRC);
}

S
Stanislaw Gruszka 已提交
156
int il4965_hw_nic_init(struct il_priv *il)
157 158
{
	unsigned long flags;
S
Stanislaw Gruszka 已提交
159
	struct il_rx_queue *rxq = &il->rxq;
160 161 162
	int ret;

	/* nic_init */
S
Stanislaw Gruszka 已提交
163 164
	spin_lock_irqsave(&il->lock, flags);
	il->cfg->ops->lib->apm_ops.init(il);
165 166

	/* Set interrupt coalescing calibration timer to default (512 usecs) */
S
Stanislaw Gruszka 已提交
167
	il_write8(il, CSR_INT_COALESCING, IL_HOST_INT_CALIB_TIMEOUT_DEF);
168

S
Stanislaw Gruszka 已提交
169
	spin_unlock_irqrestore(&il->lock, flags);
170

S
Stanislaw Gruszka 已提交
171
	il4965_set_pwr_vmain(il);
172

S
Stanislaw Gruszka 已提交
173
	il->cfg->ops->lib->apm_ops.config(il);
174 175 176

	/* Allocate the RX queue, or reset if it is already allocated */
	if (!rxq->bd) {
S
Stanislaw Gruszka 已提交
177
		ret = il_rx_queue_alloc(il);
178
		if (ret) {
179
			IL_ERR("Unable to initialize Rx queue\n");
180 181 182
			return -ENOMEM;
		}
	} else
S
Stanislaw Gruszka 已提交
183
		il4965_rx_queue_reset(il, rxq);
184

S
Stanislaw Gruszka 已提交
185
	il4965_rx_replenish(il);
186

S
Stanislaw Gruszka 已提交
187
	il4965_rx_init(il, rxq);
188

S
Stanislaw Gruszka 已提交
189
	spin_lock_irqsave(&il->lock, flags);
190 191

	rxq->need_update = 1;
S
Stanislaw Gruszka 已提交
192
	il_rx_queue_update_write_ptr(il, rxq);
193

S
Stanislaw Gruszka 已提交
194
	spin_unlock_irqrestore(&il->lock, flags);
195 196

	/* Allocate or reset and init all Tx and Command queues */
S
Stanislaw Gruszka 已提交
197 198
	if (!il->txq) {
		ret = il4965_txq_ctx_alloc(il);
199 200 201
		if (ret)
			return ret;
	} else
S
Stanislaw Gruszka 已提交
202
		il4965_txq_ctx_reset(il);
203

S
Stanislaw Gruszka 已提交
204
	set_bit(STATUS_INIT, &il->status);
205 206 207 208 209

	return 0;
}

/**
S
Stanislaw Gruszka 已提交
210
 * il4965_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer ptr
211
 */
S
Stanislaw Gruszka 已提交
212
static inline __le32 il4965_dma_addr2rbd_ptr(struct il_priv *il,
213 214 215 216 217 218
					  dma_addr_t dma_addr)
{
	return cpu_to_le32((u32)(dma_addr >> 8));
}

/**
S
Stanislaw Gruszka 已提交
219
 * il4965_rx_queue_restock - refill RX queue from pre-allocated pool
220 221 222 223 224 225 226 227 228
 *
 * If there are slots in the RX queue that need to be restocked,
 * and we have free pre-allocated buffers, fill the ranks as much
 * as we can, pulling from rx_free.
 *
 * This moves the 'write' index forward to catch up with 'processed', and
 * also updates the memory address in the firmware to reference the new
 * target buffer.
 */
S
Stanislaw Gruszka 已提交
229
void il4965_rx_queue_restock(struct il_priv *il)
230
{
S
Stanislaw Gruszka 已提交
231
	struct il_rx_queue *rxq = &il->rxq;
232
	struct list_head *element;
S
Stanislaw Gruszka 已提交
233
	struct il_rx_mem_buffer *rxb;
234 235 236
	unsigned long flags;

	spin_lock_irqsave(&rxq->lock, flags);
237
	while (il_rx_queue_space(rxq) > 0 && rxq->free_count) {
238 239 240 241 242 243
		/* The overwritten rxb must be a used one */
		rxb = rxq->queue[rxq->write];
		BUG_ON(rxb && rxb->page);

		/* Get next free Rx buffer, remove from free list */
		element = rxq->rx_free.next;
S
Stanislaw Gruszka 已提交
244
		rxb = list_entry(element, struct il_rx_mem_buffer, list);
245 246 247
		list_del(element);

		/* Point to Rx buffer via next RBD in circular buffer */
S
Stanislaw Gruszka 已提交
248
		rxq->bd[rxq->write] = il4965_dma_addr2rbd_ptr(il,
249 250 251 252 253 254 255 256 257
							      rxb->page_dma);
		rxq->queue[rxq->write] = rxb;
		rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
		rxq->free_count--;
	}
	spin_unlock_irqrestore(&rxq->lock, flags);
	/* If the pre-allocated buffer pool is dropping low, schedule to
	 * refill it */
	if (rxq->free_count <= RX_LOW_WATERMARK)
S
Stanislaw Gruszka 已提交
258
		queue_work(il->workqueue, &il->rx_replenish);
259 260 261 262 263 264 265 266


	/* If we've added more space for the firmware to place data, tell it.
	 * Increment device's write pointer in multiples of 8. */
	if (rxq->write_actual != (rxq->write & ~0x7)) {
		spin_lock_irqsave(&rxq->lock, flags);
		rxq->need_update = 1;
		spin_unlock_irqrestore(&rxq->lock, flags);
S
Stanislaw Gruszka 已提交
267
		il_rx_queue_update_write_ptr(il, rxq);
268 269 270 271
	}
}

/**
S
Stanislaw Gruszka 已提交
272
 * il4965_rx_replenish - Move all used packet from rx_used to rx_free
273 274 275
 *
 * When moving to rx_free an SKB is allocated for the slot.
 *
S
Stanislaw Gruszka 已提交
276
 * Also restock the Rx queue via il_rx_queue_restock.
277 278
 * This is called as a scheduled work item (except for during initialization)
 */
S
Stanislaw Gruszka 已提交
279
static void il4965_rx_allocate(struct il_priv *il, gfp_t priority)
280
{
S
Stanislaw Gruszka 已提交
281
	struct il_rx_queue *rxq = &il->rxq;
282
	struct list_head *element;
S
Stanislaw Gruszka 已提交
283
	struct il_rx_mem_buffer *rxb;
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
	struct page *page;
	unsigned long flags;
	gfp_t gfp_mask = priority;

	while (1) {
		spin_lock_irqsave(&rxq->lock, flags);
		if (list_empty(&rxq->rx_used)) {
			spin_unlock_irqrestore(&rxq->lock, flags);
			return;
		}
		spin_unlock_irqrestore(&rxq->lock, flags);

		if (rxq->free_count > RX_LOW_WATERMARK)
			gfp_mask |= __GFP_NOWARN;

S
Stanislaw Gruszka 已提交
299
		if (il->hw_params.rx_page_order > 0)
300 301 302
			gfp_mask |= __GFP_COMP;

		/* Alloc a new receive buffer */
S
Stanislaw Gruszka 已提交
303
		page = alloc_pages(gfp_mask, il->hw_params.rx_page_order);
304 305
		if (!page) {
			if (net_ratelimit())
306
				D_INFO("alloc_pages failed, "
307
					       "order: %d\n",
S
Stanislaw Gruszka 已提交
308
					       il->hw_params.rx_page_order);
309

310
			if (rxq->free_count <= RX_LOW_WATERMARK &&
311
			    net_ratelimit())
S
Stanislaw Gruszka 已提交
312
				IL_ERR(
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
					"Failed to alloc_pages with %s. "
					"Only %u free buffers remaining.\n",
					 priority == GFP_ATOMIC ?
						 "GFP_ATOMIC" : "GFP_KERNEL",
					 rxq->free_count);
			/* We don't reschedule replenish work here -- we will
			 * call the restock method and if it still needs
			 * more buffers it will schedule replenish */
			return;
		}

		spin_lock_irqsave(&rxq->lock, flags);

		if (list_empty(&rxq->rx_used)) {
			spin_unlock_irqrestore(&rxq->lock, flags);
S
Stanislaw Gruszka 已提交
328
			__free_pages(page, il->hw_params.rx_page_order);
329 330 331
			return;
		}
		element = rxq->rx_used.next;
S
Stanislaw Gruszka 已提交
332
		rxb = list_entry(element, struct il_rx_mem_buffer, list);
333 334 335 336 337 338 339
		list_del(element);

		spin_unlock_irqrestore(&rxq->lock, flags);

		BUG_ON(rxb->page);
		rxb->page = page;
		/* Get physical address of the RB */
S
Stanislaw Gruszka 已提交
340 341
		rxb->page_dma = pci_map_page(il->pci_dev, page, 0,
				PAGE_SIZE << il->hw_params.rx_page_order,
342 343 344 345 346 347 348 349 350 351
				PCI_DMA_FROMDEVICE);
		/* dma address must be no more than 36 bits */
		BUG_ON(rxb->page_dma & ~DMA_BIT_MASK(36));
		/* and also 256 byte aligned! */
		BUG_ON(rxb->page_dma & DMA_BIT_MASK(8));

		spin_lock_irqsave(&rxq->lock, flags);

		list_add_tail(&rxb->list, &rxq->rx_free);
		rxq->free_count++;
S
Stanislaw Gruszka 已提交
352
		il->alloc_rxb_page++;
353 354 355 356 357

		spin_unlock_irqrestore(&rxq->lock, flags);
	}
}

S
Stanislaw Gruszka 已提交
358
void il4965_rx_replenish(struct il_priv *il)
359 360 361
{
	unsigned long flags;

S
Stanislaw Gruszka 已提交
362
	il4965_rx_allocate(il, GFP_KERNEL);
363

S
Stanislaw Gruszka 已提交
364 365 366
	spin_lock_irqsave(&il->lock, flags);
	il4965_rx_queue_restock(il);
	spin_unlock_irqrestore(&il->lock, flags);
367 368
}

S
Stanislaw Gruszka 已提交
369
void il4965_rx_replenish_now(struct il_priv *il)
370
{
S
Stanislaw Gruszka 已提交
371
	il4965_rx_allocate(il, GFP_ATOMIC);
372

S
Stanislaw Gruszka 已提交
373
	il4965_rx_queue_restock(il);
374 375 376 377 378 379 380
}

/* Assumes that the skb field of the buffers in 'pool' is kept accurate.
 * If an SKB has been detached, the POOL needs to have its SKB set to NULL
 * This free routine walks the list of POOL entries and if SKB is set to
 * non NULL it is unmapped and freed
 */
S
Stanislaw Gruszka 已提交
381
void il4965_rx_queue_free(struct il_priv *il, struct il_rx_queue *rxq)
382 383 384 385
{
	int i;
	for (i = 0; i < RX_QUEUE_SIZE + RX_FREE_BUFFERS; i++) {
		if (rxq->pool[i].page != NULL) {
S
Stanislaw Gruszka 已提交
386 387
			pci_unmap_page(il->pci_dev, rxq->pool[i].page_dma,
				PAGE_SIZE << il->hw_params.rx_page_order,
388
				PCI_DMA_FROMDEVICE);
S
Stanislaw Gruszka 已提交
389
			__il_free_pages(il, rxq->pool[i].page);
390 391 392 393
			rxq->pool[i].page = NULL;
		}
	}

S
Stanislaw Gruszka 已提交
394
	dma_free_coherent(&il->pci_dev->dev, 4 * RX_QUEUE_SIZE, rxq->bd,
395
			  rxq->bd_dma);
S
Stanislaw Gruszka 已提交
396
	dma_free_coherent(&il->pci_dev->dev, sizeof(struct il_rb_status),
397 398 399 400 401
			  rxq->rb_stts, rxq->rb_stts_dma);
	rxq->bd = NULL;
	rxq->rb_stts  = NULL;
}

S
Stanislaw Gruszka 已提交
402
int il4965_rxq_stop(struct il_priv *il)
403 404 405
{

	/* stop Rx DMA */
406 407
	il_wr(il, FH_MEM_RCSR_CHNL0_CONFIG_REG, 0);
	il_poll_bit(il, FH_MEM_RSSR_RX_STATUS_REG,
408 409 410 411 412
			    FH_RSSR_CHNL0_RX_STATUS_CHNL_IDLE, 1000);

	return 0;
}

S
Stanislaw Gruszka 已提交
413
int il4965_hwrate_to_mac80211_idx(u32 rate_n_flags, enum ieee80211_band band)
414 415 416 417 418 419 420 421 422 423 424
{
	int idx = 0;
	int band_offset = 0;

	/* HT rate format: mac80211 wants an MCS number, which is just LSB */
	if (rate_n_flags & RATE_MCS_HT_MSK) {
		idx = (rate_n_flags & 0xff);
		return idx;
	/* Legacy rate format, search for match in table */
	} else {
		if (band == IEEE80211_BAND_5GHZ)
S
Stanislaw Gruszka 已提交
425 426
			band_offset = IL_FIRST_OFDM_RATE;
		for (idx = band_offset; idx < IL_RATE_COUNT_LEGACY; idx++)
427
			if (il_rates[idx].plcp == (rate_n_flags & 0xFF))
428 429 430 431 432 433
				return idx - band_offset;
	}

	return -1;
}

S
Stanislaw Gruszka 已提交
434
static int il4965_calc_rssi(struct il_priv *il,
S
Stanislaw Gruszka 已提交
435
			     struct il_rx_phy_res *rx_resp)
436 437 438
{
	/* data from PHY/DSP regarding signal strength, etc.,
	 *   contents are always there, not configurable by host.  */
S
Stanislaw Gruszka 已提交
439 440
	struct il4965_rx_non_cfg_phy *ncphy =
	    (struct il4965_rx_non_cfg_phy *)rx_resp->non_cfg_phy_buf;
441 442
	u32 agc = (le16_to_cpu(ncphy->agc_info) & IL49_AGC_DB_MASK)
			>> IL49_AGC_DB_POS;
443 444

	u32 valid_antennae =
445 446
	    (le16_to_cpu(rx_resp->phy_flags) & IL49_RX_PHY_FLAGS_ANTENNAE_MASK)
			>> IL49_RX_PHY_FLAGS_ANTENNAE_OFFSET;
447 448 449 450 451 452 453 454 455 456 457 458
	u8 max_rssi = 0;
	u32 i;

	/* Find max rssi among 3 possible receivers.
	 * These values are measured by the digital signal processor (DSP).
	 * They should stay fairly constant even as the signal strength varies,
	 *   if the radio's automatic gain control (AGC) is working right.
	 * AGC value (see below) will provide the "interesting" info. */
	for (i = 0; i < 3; i++)
		if (valid_antennae & (1 << i))
			max_rssi = max(ncphy->rssi_info[i << 1], max_rssi);

459
	D_STATS("Rssi In A %d B %d C %d Max %d AGC dB %d\n",
460 461 462 463 464
		ncphy->rssi_info[0], ncphy->rssi_info[2], ncphy->rssi_info[4],
		max_rssi, agc);

	/* dBm = max_rssi dB - agc dB - constant.
	 * Higher AGC (higher radio gain) means lower signal. */
465
	return max_rssi - agc - IL4965_RSSI_OFFSET;
466 467 468
}


S
Stanislaw Gruszka 已提交
469
static u32 il4965_translate_rx_status(struct il_priv *il, u32 decrypt_in)
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521
{
	u32 decrypt_out = 0;

	if ((decrypt_in & RX_RES_STATUS_STATION_FOUND) ==
					RX_RES_STATUS_STATION_FOUND)
		decrypt_out |= (RX_RES_STATUS_STATION_FOUND |
				RX_RES_STATUS_NO_STATION_INFO_MISMATCH);

	decrypt_out |= (decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK);

	/* packet was not encrypted */
	if ((decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) ==
					RX_RES_STATUS_SEC_TYPE_NONE)
		return decrypt_out;

	/* packet was encrypted with unknown alg */
	if ((decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) ==
					RX_RES_STATUS_SEC_TYPE_ERR)
		return decrypt_out;

	/* decryption was not done in HW */
	if ((decrypt_in & RX_MPDU_RES_STATUS_DEC_DONE_MSK) !=
					RX_MPDU_RES_STATUS_DEC_DONE_MSK)
		return decrypt_out;

	switch (decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) {

	case RX_RES_STATUS_SEC_TYPE_CCMP:
		/* alg is CCM: check MIC only */
		if (!(decrypt_in & RX_MPDU_RES_STATUS_MIC_OK))
			/* Bad MIC */
			decrypt_out |= RX_RES_STATUS_BAD_ICV_MIC;
		else
			decrypt_out |= RX_RES_STATUS_DECRYPT_OK;

		break;

	case RX_RES_STATUS_SEC_TYPE_TKIP:
		if (!(decrypt_in & RX_MPDU_RES_STATUS_TTAK_OK)) {
			/* Bad TTAK */
			decrypt_out |= RX_RES_STATUS_BAD_KEY_TTAK;
			break;
		}
		/* fall through if TTAK OK */
	default:
		if (!(decrypt_in & RX_MPDU_RES_STATUS_ICV_OK))
			decrypt_out |= RX_RES_STATUS_BAD_ICV_MIC;
		else
			decrypt_out |= RX_RES_STATUS_DECRYPT_OK;
		break;
	}

522
	D_RX("decrypt_in:0x%x  decrypt_out = 0x%x\n",
523 524 525 526 527
					decrypt_in, decrypt_out);

	return decrypt_out;
}

S
Stanislaw Gruszka 已提交
528
static void il4965_pass_packet_to_mac80211(struct il_priv *il,
529 530 531
					struct ieee80211_hdr *hdr,
					u16 len,
					u32 ampdu_status,
S
Stanislaw Gruszka 已提交
532
					struct il_rx_mem_buffer *rxb,
533 534 535 536 537 538
					struct ieee80211_rx_status *stats)
{
	struct sk_buff *skb;
	__le16 fc = hdr->frame_control;

	/* We only process data packets if the interface is open */
S
Stanislaw Gruszka 已提交
539
	if (unlikely(!il->is_open)) {
540
		D_DROP(
541 542 543 544 545
		    "Dropping packet while interface is not open.\n");
		return;
	}

	/* In case of HW accelerated crypto and bad decryption, drop */
S
Stanislaw Gruszka 已提交
546 547
	if (!il->cfg->mod_params->sw_crypto &&
	    il_set_decrypted_flag(il, hdr, ampdu_status, stats))
548 549 550 551
		return;

	skb = dev_alloc_skb(128);
	if (!skb) {
552
		IL_ERR("dev_alloc_skb failed\n");
553 554 555 556 557
		return;
	}

	skb_add_rx_frag(skb, 0, rxb->page, (void *)hdr - rxb_addr(rxb), len);

S
Stanislaw Gruszka 已提交
558
	il_update_stats(il, false, fc, len);
559 560
	memcpy(IEEE80211_SKB_RXCB(skb), stats, sizeof(*stats));

S
Stanislaw Gruszka 已提交
561 562
	ieee80211_rx(il->hw, skb);
	il->alloc_rxb_page--;
563 564 565 566 567
	rxb->page = NULL;
}

/* Called for REPLY_RX (legacy ABG frames), or
 * REPLY_RX_MPDU_CMD (HT high-throughput N frames). */
S
Stanislaw Gruszka 已提交
568
void il4965_rx_reply_rx(struct il_priv *il,
S
Stanislaw Gruszka 已提交
569
				struct il_rx_mem_buffer *rxb)
570 571 572
{
	struct ieee80211_hdr *header;
	struct ieee80211_rx_status rx_status;
573
	struct il_rx_pkt *pkt = rxb_addr(rxb);
S
Stanislaw Gruszka 已提交
574
	struct il_rx_phy_res *phy_res;
575
	__le32 rx_pkt_status;
S
Stanislaw Gruszka 已提交
576
	struct il_rx_mpdu_res_start *amsdu;
577 578 579 580 581 582 583 584
	u32 len;
	u32 ampdu_status;
	u32 rate_n_flags;

	/**
	 * REPLY_RX and REPLY_RX_MPDU_CMD are handled differently.
	 *	REPLY_RX: physical layer info is in this buffer
	 *	REPLY_RX_MPDU_CMD: physical layer info was sent in separate
S
Stanislaw Gruszka 已提交
585
	 *		command and cached in il->last_phy_res
586 587 588 589 590
	 *
	 * Here we set up local variables depending on which command is
	 * received.
	 */
	if (pkt->hdr.cmd == REPLY_RX) {
S
Stanislaw Gruszka 已提交
591
		phy_res = (struct il_rx_phy_res *)pkt->u.raw;
592 593 594 595 596 597 598 599
		header = (struct ieee80211_hdr *)(pkt->u.raw + sizeof(*phy_res)
				+ phy_res->cfg_phy_cnt);

		len = le16_to_cpu(phy_res->byte_count);
		rx_pkt_status = *(__le32 *)(pkt->u.raw + sizeof(*phy_res) +
				phy_res->cfg_phy_cnt + len);
		ampdu_status = le32_to_cpu(rx_pkt_status);
	} else {
S
Stanislaw Gruszka 已提交
600
		if (!il->_4965.last_phy_res_valid) {
601
			IL_ERR("MPDU frame without cached PHY data\n");
602 603
			return;
		}
S
Stanislaw Gruszka 已提交
604
		phy_res = &il->_4965.last_phy_res;
S
Stanislaw Gruszka 已提交
605
		amsdu = (struct il_rx_mpdu_res_start *)pkt->u.raw;
606 607 608
		header = (struct ieee80211_hdr *)(pkt->u.raw + sizeof(*amsdu));
		len = le16_to_cpu(amsdu->byte_count);
		rx_pkt_status = *(__le32 *)(pkt->u.raw + sizeof(*amsdu) + len);
S
Stanislaw Gruszka 已提交
609
		ampdu_status = il4965_translate_rx_status(il,
610 611 612 613
				le32_to_cpu(rx_pkt_status));
	}

	if ((unlikely(phy_res->cfg_phy_cnt > 20))) {
614
		D_DROP("dsp size out of range [0,20]: %d/n",
615 616 617 618 619 620
				phy_res->cfg_phy_cnt);
		return;
	}

	if (!(rx_pkt_status & RX_RES_STATUS_NO_CRC32_ERROR) ||
	    !(rx_pkt_status & RX_RES_STATUS_NO_RXE_OVERFLOW)) {
621
		D_RX("Bad CRC or FIFO: 0x%08X.\n",
622 623 624 625 626 627 628 629 630
				le32_to_cpu(rx_pkt_status));
		return;
	}

	/* This will be used in several places later */
	rate_n_flags = le32_to_cpu(phy_res->rate_n_flags);

	/* rx_status carries information about the packet to mac80211 */
	rx_status.mactime = le64_to_cpu(phy_res->timestamp);
S
Stanislaw Gruszka 已提交
631 632
	rx_status.band = (phy_res->phy_flags & RX_RES_PHY_FLAGS_BAND_24_MSK) ?
				IEEE80211_BAND_2GHZ : IEEE80211_BAND_5GHZ;
633 634 635 636
	rx_status.freq =
		ieee80211_channel_to_frequency(le16_to_cpu(phy_res->channel),
							rx_status.band);
	rx_status.rate_idx =
S
Stanislaw Gruszka 已提交
637
		il4965_hwrate_to_mac80211_idx(rate_n_flags, rx_status.band);
638 639 640 641
	rx_status.flag = 0;

	/* TSF isn't reliable. In order to allow smooth user experience,
	 * this W/A doesn't propagate it to the mac80211 */
J
Johannes Berg 已提交
642
	/*rx_status.flag |= RX_FLAG_MACTIME_MPDU;*/
643

S
Stanislaw Gruszka 已提交
644
	il->ucode_beacon_time = le32_to_cpu(phy_res->beacon_time_stamp);
645 646

	/* Find max signal strength (dBm) among 3 antenna/receiver chains */
S
Stanislaw Gruszka 已提交
647
	rx_status.signal = il4965_calc_rssi(il, phy_res);
648

S
Stanislaw Gruszka 已提交
649
	il_dbg_log_rx_data_frame(il, len, header);
650
	D_STATS("Rssi %d, TSF %llu\n",
651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681
		rx_status.signal, (unsigned long long)rx_status.mactime);

	/*
	 * "antenna number"
	 *
	 * It seems that the antenna field in the phy flags value
	 * is actually a bit field. This is undefined by radiotap,
	 * it wants an actual antenna number but I always get "7"
	 * for most legacy frames I receive indicating that the
	 * same frame was received on all three RX chains.
	 *
	 * I think this field should be removed in favor of a
	 * new 802.11n radiotap field "RX chains" that is defined
	 * as a bitmask.
	 */
	rx_status.antenna =
		(le16_to_cpu(phy_res->phy_flags) & RX_RES_PHY_FLAGS_ANTENNA_MSK)
		>> RX_RES_PHY_FLAGS_ANTENNA_POS;

	/* set the preamble flag if appropriate */
	if (phy_res->phy_flags & RX_RES_PHY_FLAGS_SHORT_PREAMBLE_MSK)
		rx_status.flag |= RX_FLAG_SHORTPRE;

	/* Set up the HT phy flags */
	if (rate_n_flags & RATE_MCS_HT_MSK)
		rx_status.flag |= RX_FLAG_HT;
	if (rate_n_flags & RATE_MCS_HT40_MSK)
		rx_status.flag |= RX_FLAG_40MHZ;
	if (rate_n_flags & RATE_MCS_SGI_MSK)
		rx_status.flag |= RX_FLAG_SHORT_GI;

S
Stanislaw Gruszka 已提交
682
	il4965_pass_packet_to_mac80211(il, header, len, ampdu_status,
683 684 685 686
				    rxb, &rx_status);
}

/* Cache phy data (Rx signal strength, etc) for HT frame (REPLY_RX_PHY_CMD).
S
Stanislaw Gruszka 已提交
687
 * This will be used later in il_rx_reply_rx() for REPLY_RX_MPDU_CMD. */
S
Stanislaw Gruszka 已提交
688
void il4965_rx_reply_rx_phy(struct il_priv *il,
S
Stanislaw Gruszka 已提交
689
			    struct il_rx_mem_buffer *rxb)
690
{
691
	struct il_rx_pkt *pkt = rxb_addr(rxb);
S
Stanislaw Gruszka 已提交
692 693
	il->_4965.last_phy_res_valid = true;
	memcpy(&il->_4965.last_phy_res, pkt->u.raw,
S
Stanislaw Gruszka 已提交
694
	       sizeof(struct il_rx_phy_res));
695 696
}

S
Stanislaw Gruszka 已提交
697
static int il4965_get_channels_for_scan(struct il_priv *il,
698 699 700
				     struct ieee80211_vif *vif,
				     enum ieee80211_band band,
				     u8 is_active, u8 n_probes,
S
Stanislaw Gruszka 已提交
701
				     struct il_scan_channel *scan_ch)
702 703 704
{
	struct ieee80211_channel *chan;
	const struct ieee80211_supported_band *sband;
S
Stanislaw Gruszka 已提交
705
	const struct il_channel_info *ch_info;
706 707 708 709 710
	u16 passive_dwell = 0;
	u16 active_dwell = 0;
	int added, i;
	u16 channel;

S
Stanislaw Gruszka 已提交
711
	sband = il_get_hw_mode(il, band);
712 713 714
	if (!sband)
		return 0;

S
Stanislaw Gruszka 已提交
715 716
	active_dwell = il_get_active_dwell_time(il, band, n_probes);
	passive_dwell = il_get_passive_dwell_time(il, band, vif);
717 718 719 720

	if (passive_dwell <= active_dwell)
		passive_dwell = active_dwell + 1;

S
Stanislaw Gruszka 已提交
721 722
	for (i = 0, added = 0; i < il->scan_request->n_channels; i++) {
		chan = il->scan_request->channels[i];
723 724 725 726 727 728 729

		if (chan->band != band)
			continue;

		channel = chan->hw_value;
		scan_ch->channel = cpu_to_le16(channel);

S
Stanislaw Gruszka 已提交
730
		ch_info = il_get_channel_info(il, band, channel);
S
Stanislaw Gruszka 已提交
731
		if (!il_is_channel_valid(ch_info)) {
732
			D_SCAN(
733 734 735 736 737
				 "Channel %d is INVALID for this band.\n",
					channel);
			continue;
		}

S
Stanislaw Gruszka 已提交
738
		if (!is_active || il_is_channel_passive(ch_info) ||
739 740 741 742 743 744
		    (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN))
			scan_ch->type = SCAN_CHANNEL_TYPE_PASSIVE;
		else
			scan_ch->type = SCAN_CHANNEL_TYPE_ACTIVE;

		if (n_probes)
S
Stanislaw Gruszka 已提交
745
			scan_ch->type |= IL_SCAN_PROBE_MASK(n_probes);
746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761

		scan_ch->active_dwell = cpu_to_le16(active_dwell);
		scan_ch->passive_dwell = cpu_to_le16(passive_dwell);

		/* Set txpower levels to defaults */
		scan_ch->dsp_atten = 110;

		/* NOTE: if we were doing 6Mb OFDM for scans we'd use
		 * power level:
		 * scan_ch->tx_gain = ((1 << 5) | (2 << 3)) | 3;
		 */
		if (band == IEEE80211_BAND_5GHZ)
			scan_ch->tx_gain = ((1 << 5) | (3 << 3)) | 3;
		else
			scan_ch->tx_gain = ((1 << 5) | (5 << 3));

762
		D_SCAN("Scanning ch=%d prob=0x%X [%s %d]\n",
763 764 765 766 767 768 769 770 771 772
			       channel, le32_to_cpu(scan_ch->type),
			       (scan_ch->type & SCAN_CHANNEL_TYPE_ACTIVE) ?
				"ACTIVE" : "PASSIVE",
			       (scan_ch->type & SCAN_CHANNEL_TYPE_ACTIVE) ?
			       active_dwell : passive_dwell);

		scan_ch++;
		added++;
	}

773
	D_SCAN("total channels to scan %d\n", added);
774 775 776
	return added;
}

S
Stanislaw Gruszka 已提交
777
int il4965_request_scan(struct il_priv *il, struct ieee80211_vif *vif)
778
{
S
Stanislaw Gruszka 已提交
779
	struct il_host_cmd cmd = {
780
		.id = REPLY_SCAN_CMD,
S
Stanislaw Gruszka 已提交
781
		.len = sizeof(struct il_scan_cmd),
782 783
		.flags = CMD_SIZE_HUGE,
	};
S
Stanislaw Gruszka 已提交
784
	struct il_scan_cmd *scan;
S
Stanislaw Gruszka 已提交
785
	struct il_rxon_context *ctx = &il->contexts[IL_RXON_CTX_BSS];
786 787 788 789 790
	u32 rate_flags = 0;
	u16 cmd_len;
	u16 rx_chain = 0;
	enum ieee80211_band band;
	u8 n_probes = 0;
S
Stanislaw Gruszka 已提交
791
	u8 rx_ant = il->hw_params.valid_rx_ant;
792 793 794 795
	u8 rate;
	bool is_active = false;
	int  chan_mod;
	u8 active_chains;
S
Stanislaw Gruszka 已提交
796
	u8 scan_tx_antennas = il->hw_params.valid_tx_ant;
797 798
	int ret;

S
Stanislaw Gruszka 已提交
799
	lockdep_assert_held(&il->mutex);
800 801

	if (vif)
S
Stanislaw Gruszka 已提交
802
		ctx = il_rxon_ctx_from_vif(vif);
803

S
Stanislaw Gruszka 已提交
804 805
	if (!il->scan_cmd) {
		il->scan_cmd = kmalloc(sizeof(struct il_scan_cmd) +
S
Stanislaw Gruszka 已提交
806
					 IL_MAX_SCAN_SIZE, GFP_KERNEL);
S
Stanislaw Gruszka 已提交
807
		if (!il->scan_cmd) {
808
			D_SCAN(
809 810 811 812
				       "fail to allocate memory for scan\n");
			return -ENOMEM;
		}
	}
S
Stanislaw Gruszka 已提交
813
	scan = il->scan_cmd;
S
Stanislaw Gruszka 已提交
814
	memset(scan, 0, sizeof(struct il_scan_cmd) + IL_MAX_SCAN_SIZE);
815

S
Stanislaw Gruszka 已提交
816 817
	scan->quiet_plcp_th = IL_PLCP_QUIET_THRESH;
	scan->quiet_time = IL_ACTIVE_QUIET_TIME;
818

S
Stanislaw Gruszka 已提交
819
	if (il_is_any_associated(il)) {
820
		u16 interval;
821 822 823 824
		u32 extra;
		u32 suspend_time = 100;
		u32 scan_suspend_time = 100;

825
		D_INFO("Scanning while associated...\n");
826
		interval = vif->bss_conf.beacon_int;
827 828 829 830 831 832 833 834 835 836

		scan->suspend_time = 0;
		scan->max_out_time = cpu_to_le32(200 * 1024);
		if (!interval)
			interval = suspend_time;

		extra = (suspend_time / interval) << 22;
		scan_suspend_time = (extra |
		    ((suspend_time % interval) * 1024));
		scan->suspend_time = cpu_to_le32(scan_suspend_time);
837
		D_SCAN("suspend_time 0x%X beacon interval %d\n",
838 839 840
			       scan_suspend_time, interval);
	}

S
Stanislaw Gruszka 已提交
841
	if (il->scan_request->n_ssids) {
842
		int i, p = 0;
843
		D_SCAN("Kicking off active scan\n");
S
Stanislaw Gruszka 已提交
844
		for (i = 0; i < il->scan_request->n_ssids; i++) {
845
			/* always does wildcard anyway */
S
Stanislaw Gruszka 已提交
846
			if (!il->scan_request->ssids[i].ssid_len)
847 848 849
				continue;
			scan->direct_scan[p].id = WLAN_EID_SSID;
			scan->direct_scan[p].len =
S
Stanislaw Gruszka 已提交
850
				il->scan_request->ssids[i].ssid_len;
851
			memcpy(scan->direct_scan[p].ssid,
S
Stanislaw Gruszka 已提交
852 853
			       il->scan_request->ssids[i].ssid,
			       il->scan_request->ssids[i].ssid_len);
854 855 856 857 858
			n_probes++;
			p++;
		}
		is_active = true;
	} else
859
		D_SCAN("Start passive scan.\n");
860 861 862 863 864

	scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
	scan->tx_cmd.sta_id = ctx->bcast_sta_id;
	scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;

S
Stanislaw Gruszka 已提交
865
	switch (il->scan_band) {
866 867 868
	case IEEE80211_BAND_2GHZ:
		scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
		chan_mod = le32_to_cpu(
S
Stanislaw Gruszka 已提交
869
			il->contexts[IL_RXON_CTX_BSS].active.flags &
870 871 872
						RXON_FLG_CHANNEL_MODE_MSK)
				       >> RXON_FLG_CHANNEL_MODE_POS;
		if (chan_mod == CHANNEL_MODE_PURE_40) {
S
Stanislaw Gruszka 已提交
873
			rate = IL_RATE_6M_PLCP;
874
		} else {
S
Stanislaw Gruszka 已提交
875
			rate = IL_RATE_1M_PLCP;
876 877 878 879
			rate_flags = RATE_MCS_CCK_MSK;
		}
		break;
	case IEEE80211_BAND_5GHZ:
S
Stanislaw Gruszka 已提交
880
		rate = IL_RATE_6M_PLCP;
881 882
		break;
	default:
883
		IL_WARN("Invalid scan band\n");
884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900
		return -EIO;
	}

	/*
	 * If active scanning is requested but a certain channel is
	 * marked passive, we can do active scanning if we detect
	 * transmissions.
	 *
	 * There is an issue with some firmware versions that triggers
	 * a sysassert on a "good CRC threshold" of zero (== disabled),
	 * on a radar channel even though this means that we should NOT
	 * send probes.
	 *
	 * The "good CRC threshold" is the number of frames that we
	 * need to receive during our dwell time on a channel before
	 * sending out probes -- setting this to a huge value will
	 * mean we never reach it, but at the same time work around
S
Stanislaw Gruszka 已提交
901 902
	 * the aforementioned issue. Thus use IL_GOOD_CRC_TH_NEVER
	 * here instead of IL_GOOD_CRC_TH_DISABLED.
903
	 */
S
Stanislaw Gruszka 已提交
904 905
	scan->good_CRC_th = is_active ? IL_GOOD_CRC_TH_DEFAULT :
					IL_GOOD_CRC_TH_NEVER;
906

S
Stanislaw Gruszka 已提交
907
	band = il->scan_band;
908

S
Stanislaw Gruszka 已提交
909 910
	if (il->cfg->scan_rx_antennas[band])
		rx_ant = il->cfg->scan_rx_antennas[band];
911

S
Stanislaw Gruszka 已提交
912 913
	il->scan_tx_ant[band] = il4965_toggle_tx_ant(il,
						il->scan_tx_ant[band],
914
						    scan_tx_antennas);
S
Stanislaw Gruszka 已提交
915
	rate_flags |= il4965_ant_idx_to_flags(il->scan_tx_ant[band]);
S
Stanislaw Gruszka 已提交
916
	scan->tx_cmd.rate_n_flags = il4965_hw_set_rate_n_flags(rate, rate_flags);
917 918

	/* In power save mode use one chain, otherwise use all chains */
S
Stanislaw Gruszka 已提交
919
	if (test_bit(STATUS_POWER_PMI, &il->status)) {
920 921
		/* rx_ant has been set to all valid chains previously */
		active_chains = rx_ant &
S
Stanislaw Gruszka 已提交
922
				((u8)(il->chain_noise_data.active_chains));
923 924 925
		if (!active_chains)
			active_chains = rx_ant;

926
		D_SCAN("chain_noise_data.active_chains: %u\n",
S
Stanislaw Gruszka 已提交
927
				il->chain_noise_data.active_chains);
928

S
Stanislaw Gruszka 已提交
929
		rx_ant = il4965_first_antenna(active_chains);
930 931 932
	}

	/* MIMO is not used here, but value is required */
S
Stanislaw Gruszka 已提交
933
	rx_chain |= il->hw_params.valid_rx_ant << RXON_RX_CHAIN_VALID_POS;
934 935 936 937
	rx_chain |= rx_ant << RXON_RX_CHAIN_FORCE_MIMO_SEL_POS;
	rx_chain |= rx_ant << RXON_RX_CHAIN_FORCE_SEL_POS;
	rx_chain |= 0x1 << RXON_RX_CHAIN_DRIVER_FORCE_POS;
	scan->rx_chain = cpu_to_le16(rx_chain);
938

S
Stanislaw Gruszka 已提交
939
	cmd_len = il_fill_probe_req(il,
940 941
					(struct ieee80211_mgmt *)scan->data,
					vif->addr,
S
Stanislaw Gruszka 已提交
942 943
					il->scan_request->ie,
					il->scan_request->ie_len,
S
Stanislaw Gruszka 已提交
944
					IL_MAX_SCAN_SIZE - sizeof(*scan));
945 946 947 948 949
	scan->tx_cmd.len = cpu_to_le16(cmd_len);

	scan->filter_flags |= (RXON_FILTER_ACCEPT_GRP_MSK |
			       RXON_FILTER_BCON_AWARE_MSK);

S
Stanislaw Gruszka 已提交
950
	scan->channel_count = il4965_get_channels_for_scan(il, vif, band,
951 952
						is_active, n_probes,
						(void *)&scan->data[cmd_len]);
953
	if (scan->channel_count == 0) {
954
		D_SCAN("channel count %d\n", scan->channel_count);
955 956 957 958
		return -EIO;
	}

	cmd.len += le16_to_cpu(scan->tx_cmd.len) +
S
Stanislaw Gruszka 已提交
959
	    scan->channel_count * sizeof(struct il_scan_channel);
960 961 962
	cmd.data = scan;
	scan->len = cpu_to_le16(cmd.len);

S
Stanislaw Gruszka 已提交
963
	set_bit(STATUS_SCAN_HW, &il->status);
964

S
Stanislaw Gruszka 已提交
965
	ret = il_send_cmd_sync(il, &cmd);
966
	if (ret)
S
Stanislaw Gruszka 已提交
967
		clear_bit(STATUS_SCAN_HW, &il->status);
968 969 970 971

	return ret;
}

S
Stanislaw Gruszka 已提交
972
int il4965_manage_ibss_station(struct il_priv *il,
973 974
			       struct ieee80211_vif *vif, bool add)
{
S
Stanislaw Gruszka 已提交
975
	struct il_vif_priv *vif_priv = (void *)vif->drv_priv;
976 977

	if (add)
S
Stanislaw Gruszka 已提交
978
		return il4965_add_bssid_station(il, vif_priv->ctx,
979 980
						vif->bss_conf.bssid,
						&vif_priv->ibss_bssid_sta_id);
S
Stanislaw Gruszka 已提交
981
	return il_remove_station(il, vif_priv->ibss_bssid_sta_id,
982 983 984
				  vif->bss_conf.bssid);
}

S
Stanislaw Gruszka 已提交
985
void il4965_free_tfds_in_queue(struct il_priv *il,
986 987
			    int sta_id, int tid, int freed)
{
S
Stanislaw Gruszka 已提交
988
	lockdep_assert_held(&il->sta_lock);
989

S
Stanislaw Gruszka 已提交
990 991
	if (il->stations[sta_id].tid[tid].tfds_in_queue >= freed)
		il->stations[sta_id].tid[tid].tfds_in_queue -= freed;
992
	else {
993
		D_TX("free more than tfds_in_queue (%u:%d)\n",
S
Stanislaw Gruszka 已提交
994
			il->stations[sta_id].tid[tid].tfds_in_queue,
995
			freed);
S
Stanislaw Gruszka 已提交
996
		il->stations[sta_id].tid[tid].tfds_in_queue = 0;
997 998 999
	}
}

S
Stanislaw Gruszka 已提交
1000
#define IL_TX_QUEUE_MSK	0xfffff
1001

S
Stanislaw Gruszka 已提交
1002
static bool il4965_is_single_rx_stream(struct il_priv *il)
1003
{
S
Stanislaw Gruszka 已提交
1004 1005
	return il->current_ht_config.smps == IEEE80211_SMPS_STATIC ||
	       il->current_ht_config.single_chain_sufficient;
1006 1007
}

S
Stanislaw Gruszka 已提交
1008 1009 1010 1011
#define IL_NUM_RX_CHAINS_MULTIPLE	3
#define IL_NUM_RX_CHAINS_SINGLE	2
#define IL_NUM_IDLE_CHAINS_DUAL	2
#define IL_NUM_IDLE_CHAINS_SINGLE	1
1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022

/*
 * Determine how many receiver/antenna chains to use.
 *
 * More provides better reception via diversity.  Fewer saves power
 * at the expense of throughput, but only when not in powersave to
 * start with.
 *
 * MIMO (dual stream) requires at least 2, but works better with 3.
 * This does not determine *which* chains to use, just how many.
 */
S
Stanislaw Gruszka 已提交
1023
static int il4965_get_active_rx_chain_count(struct il_priv *il)
1024 1025
{
	/* # of Rx chains to use when expecting MIMO. */
S
Stanislaw Gruszka 已提交
1026
	if (il4965_is_single_rx_stream(il))
S
Stanislaw Gruszka 已提交
1027
		return IL_NUM_RX_CHAINS_SINGLE;
1028
	else
S
Stanislaw Gruszka 已提交
1029
		return IL_NUM_RX_CHAINS_MULTIPLE;
1030 1031 1032 1033 1034 1035 1036
}

/*
 * When we are in power saving mode, unless device support spatial
 * multiplexing power save, use the active count for rx chain count.
 */
static int
S
Stanislaw Gruszka 已提交
1037
il4965_get_idle_rx_chain_count(struct il_priv *il, int active_cnt)
1038 1039
{
	/* # Rx chains when idling, depending on SMPS mode */
S
Stanislaw Gruszka 已提交
1040
	switch (il->current_ht_config.smps) {
1041 1042
	case IEEE80211_SMPS_STATIC:
	case IEEE80211_SMPS_DYNAMIC:
S
Stanislaw Gruszka 已提交
1043
		return IL_NUM_IDLE_CHAINS_SINGLE;
1044 1045 1046 1047
	case IEEE80211_SMPS_OFF:
		return active_cnt;
	default:
		WARN(1, "invalid SMPS mode %d",
S
Stanislaw Gruszka 已提交
1048
		     il->current_ht_config.smps);
1049 1050 1051 1052 1053
		return active_cnt;
	}
}

/* up to 4 chains */
S
Stanislaw Gruszka 已提交
1054
static u8 il4965_count_chain_bitmap(u32 chain_bitmap)
1055 1056 1057 1058 1059 1060 1061 1062 1063 1064
{
	u8 res;
	res = (chain_bitmap & BIT(0)) >> 0;
	res += (chain_bitmap & BIT(1)) >> 1;
	res += (chain_bitmap & BIT(2)) >> 2;
	res += (chain_bitmap & BIT(3)) >> 3;
	return res;
}

/**
S
Stanislaw Gruszka 已提交
1065
 * il4965_set_rxon_chain - Set up Rx chain usage in "staging" RXON image
1066 1067 1068 1069
 *
 * Selects how many and which Rx receivers/antennas/chains to use.
 * This should not be used for scan command ... it puts data in wrong place.
 */
S
Stanislaw Gruszka 已提交
1070
void il4965_set_rxon_chain(struct il_priv *il, struct il_rxon_context *ctx)
1071
{
S
Stanislaw Gruszka 已提交
1072 1073
	bool is_single = il4965_is_single_rx_stream(il);
	bool is_cam = !test_bit(STATUS_POWER_PMI, &il->status);
1074 1075 1076 1077 1078 1079
	u8 idle_rx_cnt, active_rx_cnt, valid_rx_cnt;
	u32 active_chains;
	u16 rx_chain;

	/* Tell uCode which antennas are actually connected.
	 * Before first association, we assume all antennas are connected.
S
Stanislaw Gruszka 已提交
1080
	 * Just after first association, il4965_chain_noise_calibration()
1081
	 *    checks which antennas actually *are* connected. */
S
Stanislaw Gruszka 已提交
1082 1083
	if (il->chain_noise_data.active_chains)
		active_chains = il->chain_noise_data.active_chains;
1084
	else
S
Stanislaw Gruszka 已提交
1085
		active_chains = il->hw_params.valid_rx_ant;
1086 1087 1088 1089

	rx_chain = active_chains << RXON_RX_CHAIN_VALID_POS;

	/* How many receivers should we use? */
S
Stanislaw Gruszka 已提交
1090 1091
	active_rx_cnt = il4965_get_active_rx_chain_count(il);
	idle_rx_cnt = il4965_get_idle_rx_chain_count(il, active_rx_cnt);
1092 1093 1094 1095 1096


	/* correct rx chain count according hw settings
	 * and chain noise calibration
	 */
S
Stanislaw Gruszka 已提交
1097
	valid_rx_cnt = il4965_count_chain_bitmap(active_chains);
1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108
	if (valid_rx_cnt < active_rx_cnt)
		active_rx_cnt = valid_rx_cnt;

	if (valid_rx_cnt < idle_rx_cnt)
		idle_rx_cnt = valid_rx_cnt;

	rx_chain |= active_rx_cnt << RXON_RX_CHAIN_MIMO_CNT_POS;
	rx_chain |= idle_rx_cnt  << RXON_RX_CHAIN_CNT_POS;

	ctx->staging.rx_chain = cpu_to_le16(rx_chain);

1109
	if (!is_single && active_rx_cnt >= IL_NUM_RX_CHAINS_SINGLE && is_cam)
1110 1111 1112 1113
		ctx->staging.rx_chain |= RXON_RX_CHAIN_MIMO_FORCE_MSK;
	else
		ctx->staging.rx_chain &= ~RXON_RX_CHAIN_MIMO_FORCE_MSK;

1114
	D_ASSOC("rx_chain=0x%X active=%d idle=%d\n",
1115 1116 1117 1118 1119 1120 1121
			ctx->staging.rx_chain,
			active_rx_cnt, idle_rx_cnt);

	WARN_ON(active_rx_cnt == 0 || idle_rx_cnt == 0 ||
		active_rx_cnt < idle_rx_cnt);
}

S
Stanislaw Gruszka 已提交
1122
u8 il4965_toggle_tx_ant(struct il_priv *il, u8 ant, u8 valid)
1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134
{
	int i;
	u8 ind = ant;

	for (i = 0; i < RATE_ANT_NUM - 1; i++) {
		ind = (ind + 1) < RATE_ANT_NUM ?  ind + 1 : 0;
		if (valid & BIT(ind))
			return ind;
	}
	return ant;
}

S
Stanislaw Gruszka 已提交
1135
static const char *il4965_get_fh_string(int cmd)
1136 1137
{
	switch (cmd) {
S
Stanislaw Gruszka 已提交
1138 1139 1140 1141 1142 1143 1144 1145 1146
	IL_CMD(FH_RSCSR_CHNL0_STTS_WPTR_REG);
	IL_CMD(FH_RSCSR_CHNL0_RBDCB_BASE_REG);
	IL_CMD(FH_RSCSR_CHNL0_WPTR);
	IL_CMD(FH_MEM_RCSR_CHNL0_CONFIG_REG);
	IL_CMD(FH_MEM_RSSR_SHARED_CTRL_REG);
	IL_CMD(FH_MEM_RSSR_RX_STATUS_REG);
	IL_CMD(FH_MEM_RSSR_RX_ENABLE_ERR_IRQ2DRV);
	IL_CMD(FH_TSSR_TX_STATUS_REG);
	IL_CMD(FH_TSSR_TX_ERROR_REG);
1147 1148 1149 1150 1151
	default:
		return "UNKNOWN";
	}
}

S
Stanislaw Gruszka 已提交
1152
int il4965_dump_fh(struct il_priv *il, char **buf, bool display)
1153 1154
{
	int i;
1155
#ifdef CONFIG_IWLEGACY_DEBUG
1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169
	int pos = 0;
	size_t bufsz = 0;
#endif
	static const u32 fh_tbl[] = {
		FH_RSCSR_CHNL0_STTS_WPTR_REG,
		FH_RSCSR_CHNL0_RBDCB_BASE_REG,
		FH_RSCSR_CHNL0_WPTR,
		FH_MEM_RCSR_CHNL0_CONFIG_REG,
		FH_MEM_RSSR_SHARED_CTRL_REG,
		FH_MEM_RSSR_RX_STATUS_REG,
		FH_MEM_RSSR_RX_ENABLE_ERR_IRQ2DRV,
		FH_TSSR_TX_STATUS_REG,
		FH_TSSR_TX_ERROR_REG
	};
1170
#ifdef CONFIG_IWLEGACY_DEBUG
1171 1172 1173 1174 1175 1176 1177 1178 1179 1180
	if (display) {
		bufsz = ARRAY_SIZE(fh_tbl) * 48 + 40;
		*buf = kmalloc(bufsz, GFP_KERNEL);
		if (!*buf)
			return -ENOMEM;
		pos += scnprintf(*buf + pos, bufsz - pos,
				"FH register values:\n");
		for (i = 0; i < ARRAY_SIZE(fh_tbl); i++) {
			pos += scnprintf(*buf + pos, bufsz - pos,
				"  %34s: 0X%08x\n",
S
Stanislaw Gruszka 已提交
1181
				il4965_get_fh_string(fh_tbl[i]),
1182
				il_rd(il, fh_tbl[i]));
1183 1184 1185 1186
		}
		return pos;
	}
#endif
1187
	IL_ERR("FH register values:\n");
1188
	for (i = 0; i <  ARRAY_SIZE(fh_tbl); i++) {
1189
		IL_ERR("  %34s: 0X%08x\n",
S
Stanislaw Gruszka 已提交
1190
			il4965_get_fh_string(fh_tbl[i]),
1191
			il_rd(il, fh_tbl[i]));
1192 1193 1194
	}
	return 0;
}