iwl-rx.c 8.9 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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
/******************************************************************************
 *
 * Copyright(c) 2003 - 2011 Intel Corporation. All rights reserved.
 *
 * Portions of this file are derived from the ipw3945 project, as well
 * as portions of the ieee80211 subsystem header files.
 *
 * 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.
 *
 * 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/slab.h>
#include <net/mac80211.h>
#include <asm/unaligned.h>
#include "iwl-eeprom.h"
#include "iwl-dev.h"
#include "iwl-core.h"
#include "iwl-sta.h"
#include "iwl-io.h"
#include "iwl-helpers.h"
/************************** RX-FUNCTIONS ****************************/
/*
 * Rx theory of operation
 *
 * Driver allocates a circular buffer of Receive Buffer Descriptors (RBDs),
 * each of which point to Receive Buffers to be filled by the NIC.  These get
 * used not only for Rx frames, but for any command response or notification
 * from the NIC.  The driver and NIC manage the Rx buffers by means
 * of indexes into the circular buffer.
 *
 * Rx Queue Indexes
 * The host/firmware share two index registers for managing the Rx buffers.
 *
 * The READ index maps to the first position that the firmware may be writing
 * to -- the driver can read up to (but not including) this position and get
 * good data.
 * The READ index is managed by the firmware once the card is enabled.
 *
 * The WRITE index maps to the last position the driver has read from -- the
 * position preceding WRITE is the last slot the firmware can place a packet.
 *
 * The queue is empty (no good data) if WRITE = READ - 1, and is full if
 * WRITE = READ.
 *
 * During initialization, the host sets up the READ queue position to the first
 * INDEX position, and WRITE to the last (READ - 1 wrapped)
 *
 * When the firmware places a packet in a buffer, it will advance the READ index
 * and fire the RX interrupt.  The driver can then query the READ index and
 * process as many packets as possible, moving the WRITE index forward as it
 * resets the Rx queue buffers with new memory.
 *
 * The management in the driver is as follows:
 * + A list of pre-allocated SKBs is stored in iwl->rxq->rx_free.  When
 *   iwl->rxq->free_count drops to or below RX_LOW_WATERMARK, work is scheduled
 *   to replenish the iwl->rxq->rx_free.
S
Stanislaw Gruszka 已提交
76
 * + In il_rx_replenish (scheduled) if 'processed' != 'read' then the
77 78 79 80 81 82 83 84 85 86 87 88
 *   iwl->rxq is replenished and the READ INDEX is updated (updating the
 *   'processed' and 'read' driver indexes as well)
 * + A received packet is processed and handed to the kernel network stack,
 *   detached from the iwl->rxq.  The driver 'processed' index is updated.
 * + The Host/Firmware iwl->rxq is replenished at tasklet time from the rx_free
 *   list. If there are no allocated buffers in iwl->rxq->rx_free, the READ
 *   INDEX is not incremented and iwl->status(RX_STALLED) is set.  If there
 *   were enough free buffers and RX_STALLED is set it is cleared.
 *
 *
 * Driver sequence:
 *
S
Stanislaw Gruszka 已提交
89 90 91 92
 * il_rx_queue_alloc()   Allocates rx_free
 * il_rx_replenish()     Replenishes rx_free list from rx_used, and calls
 *                            il_rx_queue_restock
 * il_rx_queue_restock() Moves available buffers from rx_free into Rx
93 94
 *                            queue, updates firmware pointers, and updates
 *                            the WRITE index.  If insufficient rx_free buffers
S
Stanislaw Gruszka 已提交
95
 *                            are available, schedules il_rx_replenish
96 97
 *
 * -- enable interrupts --
S
Stanislaw Gruszka 已提交
98
 * ISR - il_rx()         Detach il_rx_mem_buffers from pool up to the
99 100
 *                            READ INDEX, detaching the SKB from the pool.
 *                            Moves the packet buffer from queue to rx_used.
S
Stanislaw Gruszka 已提交
101
 *                            Calls il_rx_queue_restock to refill any empty
102 103 104 105 106 107
 *                            slots.
 * ...
 *
 */

/**
S
Stanislaw Gruszka 已提交
108
 * il_rx_queue_space - Return number of free slots available in queue.
109
 */
S
Stanislaw Gruszka 已提交
110
int il_rx_queue_space(const struct il_rx_queue *q)
111 112 113 114 115 116 117 118 119 120
{
	int s = q->read - q->write;
	if (s <= 0)
		s += RX_QUEUE_SIZE;
	/* keep some buffer to not confuse full and empty queue */
	s -= 2;
	if (s < 0)
		s = 0;
	return s;
}
S
Stanislaw Gruszka 已提交
121
EXPORT_SYMBOL(il_rx_queue_space);
122 123

/**
S
Stanislaw Gruszka 已提交
124
 * il_rx_queue_update_write_ptr - Update the write pointer for the RX queue
125 126
 */
void
S
Stanislaw Gruszka 已提交
127
il_rx_queue_update_write_ptr(struct il_priv *il,
S
Stanislaw Gruszka 已提交
128
					struct il_rx_queue *q)
129 130
{
	unsigned long flags;
S
Stanislaw Gruszka 已提交
131
	u32 rx_wrt_ptr_reg = il->hw_params.rx_wrt_ptr_reg;
132 133 134 135 136 137 138 139
	u32 reg;

	spin_lock_irqsave(&q->lock, flags);

	if (q->need_update == 0)
		goto exit_unlock;

	/* If power-saving is in use, make sure device is awake */
S
Stanislaw Gruszka 已提交
140
	if (test_bit(STATUS_POWER_PMI, &il->status)) {
141
		reg = _il_rd(il, CSR_UCODE_DRV_GP1);
142 143

		if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
144
			D_INFO(
145 146
				"Rx queue requesting wakeup,"
				" GP1 = 0x%x\n", reg);
S
Stanislaw Gruszka 已提交
147
			il_set_bit(il, CSR_GP_CNTRL,
148 149 150 151 152
				CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
			goto exit_unlock;
		}

		q->write_actual = (q->write & ~0x7);
153
		il_wr(il, rx_wrt_ptr_reg,
154 155 156 157 158 159
				q->write_actual);

	/* Else device is assumed to be awake */
	} else {
		/* Device expects a multiple of 8 */
		q->write_actual = (q->write & ~0x7);
160
		il_wr(il, rx_wrt_ptr_reg,
161 162 163 164 165 166 167 168
			q->write_actual);
	}

	q->need_update = 0;

 exit_unlock:
	spin_unlock_irqrestore(&q->lock, flags);
}
S
Stanislaw Gruszka 已提交
169
EXPORT_SYMBOL(il_rx_queue_update_write_ptr);
170

S
Stanislaw Gruszka 已提交
171
int il_rx_queue_alloc(struct il_priv *il)
172
{
S
Stanislaw Gruszka 已提交
173 174
	struct il_rx_queue *rxq = &il->rxq;
	struct device *dev = &il->pci_dev->dev;
175 176 177 178 179 180 181 182 183 184 185 186
	int i;

	spin_lock_init(&rxq->lock);
	INIT_LIST_HEAD(&rxq->rx_free);
	INIT_LIST_HEAD(&rxq->rx_used);

	/* Alloc the circular buffer of Read Buffer Descriptors (RBDs) */
	rxq->bd = dma_alloc_coherent(dev, 4 * RX_QUEUE_SIZE, &rxq->bd_dma,
				     GFP_KERNEL);
	if (!rxq->bd)
		goto err_bd;

S
Stanislaw Gruszka 已提交
187
	rxq->rb_stts = dma_alloc_coherent(dev, sizeof(struct il_rb_status),
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
					  &rxq->rb_stts_dma, GFP_KERNEL);
	if (!rxq->rb_stts)
		goto err_rb;

	/* Fill the rx_used queue with _all_ of the Rx buffers */
	for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++)
		list_add_tail(&rxq->pool[i].list, &rxq->rx_used);

	/* 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;
	rxq->need_update = 0;
	return 0;

err_rb:
S
Stanislaw Gruszka 已提交
205
	dma_free_coherent(&il->pci_dev->dev, 4 * RX_QUEUE_SIZE, rxq->bd,
206 207 208 209
			  rxq->bd_dma);
err_bd:
	return -ENOMEM;
}
S
Stanislaw Gruszka 已提交
210
EXPORT_SYMBOL(il_rx_queue_alloc);
211 212


S
Stanislaw Gruszka 已提交
213
void il_rx_spectrum_measure_notif(struct il_priv *il,
S
Stanislaw Gruszka 已提交
214
					  struct il_rx_mem_buffer *rxb)
215
{
216
	struct il_rx_pkt *pkt = rxb_addr(rxb);
S
Stanislaw Gruszka 已提交
217
	struct il_spectrum_notification *report = &(pkt->u.spectrum_notif);
218 219

	if (!report->state) {
220
		D_11H(
221 222 223 224
			"Spectrum Measure Notification: Start\n");
		return;
	}

S
Stanislaw Gruszka 已提交
225 226
	memcpy(&il->measure_report, report, sizeof(*report));
	il->measurement_status |= MEASUREMENT_READY;
227
}
S
Stanislaw Gruszka 已提交
228
EXPORT_SYMBOL(il_rx_spectrum_measure_notif);
229 230 231 232

/*
 * returns non-zero if packet should be dropped
 */
S
Stanislaw Gruszka 已提交
233
int il_set_decrypted_flag(struct il_priv *il,
234 235 236 237 238 239 240 241 242 243
			   struct ieee80211_hdr *hdr,
			   u32 decrypt_res,
			   struct ieee80211_rx_status *stats)
{
	u16 fc = le16_to_cpu(hdr->frame_control);

	/*
	 * All contexts have the same setting here due to it being
	 * a module parameter, so OK to check any context.
	 */
S
Stanislaw Gruszka 已提交
244
	if (il->contexts[IL_RXON_CTX_BSS].active.filter_flags &
245 246 247 248 249 250
						RXON_FILTER_DIS_DECRYPT_MSK)
		return 0;

	if (!(fc & IEEE80211_FCTL_PROTECTED))
		return 0;

251
	D_RX("decrypt_res:0x%x\n", decrypt_res);
252 253 254 255 256 257 258 259 260 261 262 263 264
	switch (decrypt_res & RX_RES_STATUS_SEC_TYPE_MSK) {
	case RX_RES_STATUS_SEC_TYPE_TKIP:
		/* The uCode has got a bad phase 1 Key, pushes the packet.
		 * Decryption will be done in SW. */
		if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
		    RX_RES_STATUS_BAD_KEY_TTAK)
			break;

	case RX_RES_STATUS_SEC_TYPE_WEP:
		if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
		    RX_RES_STATUS_BAD_ICV_MIC) {
			/* bad ICV, the packet is destroyed since the
			 * decryption is inplace, drop it */
265
			D_RX("Packet destroyed\n");
266 267 268 269 270
			return -1;
		}
	case RX_RES_STATUS_SEC_TYPE_CCMP:
		if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
		    RX_RES_STATUS_DECRYPT_OK) {
271
			D_RX("hw decrypt successfully!!!\n");
272 273 274 275 276 277 278 279 280
			stats->flag |= RX_FLAG_DECRYPTED;
		}
		break;

	default:
		break;
	}
	return 0;
}
S
Stanislaw Gruszka 已提交
281
EXPORT_SYMBOL(il_set_decrypted_flag);