wl1251_rx.c 5.0 KB
Newer Older
K
Kalle Valo 已提交
1
/*
2
 * This file is part of wl1251
K
Kalle Valo 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *
 * Copyright (c) 1998-2007 Texas Instruments Incorporated
 * Copyright (C) 2008 Nokia Corporation
 *
 * Contact: Kalle Valo <kalle.valo@nokia.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2 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 St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */

#include <linux/skbuff.h>
26
#include <linux/gfp.h>
K
Kalle Valo 已提交
27 28
#include <net/mac80211.h>

K
Kalle Valo 已提交
29
#include "wl1251.h"
30
#include "wl1251_reg.h"
31
#include "wl1251_io.h"
32
#include "wl1251_rx.h"
33
#include "wl1251_cmd.h"
34
#include "wl1251_acx.h"
K
Kalle Valo 已提交
35

36 37
static void wl1251_rx_header(struct wl1251 *wl,
			     struct wl1251_rx_descriptor *desc)
K
Kalle Valo 已提交
38 39 40 41 42 43 44
{
	u32 rx_packet_ring_addr;

	rx_packet_ring_addr = wl->data_path->rx_packet_ring_addr;
	if (wl->rx_current_buffer)
		rx_packet_ring_addr += wl->data_path->rx_packet_ring_chunk_size;

45
	wl1251_mem_read(wl, rx_packet_ring_addr, desc, sizeof(*desc));
K
Kalle Valo 已提交
46 47
}

48 49
static void wl1251_rx_status(struct wl1251 *wl,
			     struct wl1251_rx_descriptor *desc,
K
Kalle Valo 已提交
50 51 52
			     struct ieee80211_rx_status *status,
			     u8 beacon)
{
53 54 55
	u64 mactime;
	int ret;

K
Kalle Valo 已提交
56 57 58 59 60 61 62 63 64 65 66 67 68 69
	memset(status, 0, sizeof(struct ieee80211_rx_status));

	status->band = IEEE80211_BAND_2GHZ;
	status->mactime = desc->timestamp;

	/*
	 * The rx status timestamp is a 32 bits value while the TSF is a
	 * 64 bits one.
	 * For IBSS merging, TSF is mandatory, so we have to get it
	 * somehow, so we ask for ACX_TSF_INFO.
	 * That could be moved to the get_tsf() hook, but unfortunately,
	 * this one must be atomic, while our SPI routines can sleep.
	 */
	if ((wl->bss_type == BSS_TYPE_IBSS) && beacon) {
70
		ret = wl1251_acx_tsf_info(wl, &mactime);
71 72
		if (ret == 0)
			status->mactime = mactime;
K
Kalle Valo 已提交
73 74 75 76
	}

	status->signal = desc->rssi;

77 78 79 80 81 82
	/*
	 * FIXME: guessing that snr needs to be divided by two, otherwise
	 * the values don't make any sense
	 */
	wl->noise = desc->rssi - desc->snr / 2;

K
Kalle Valo 已提交
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
	status->freq = ieee80211_channel_to_frequency(desc->channel);

	status->flag |= RX_FLAG_TSFT;

	if (desc->flags & RX_DESC_ENCRYPTION_MASK) {
		status->flag |= RX_FLAG_IV_STRIPPED | RX_FLAG_MMIC_STRIPPED;

		if (likely(!(desc->flags & RX_DESC_DECRYPT_FAIL)))
			status->flag |= RX_FLAG_DECRYPTED;

		if (unlikely(desc->flags & RX_DESC_MIC_FAIL))
			status->flag |= RX_FLAG_MMIC_ERROR;
	}

	if (unlikely(!(desc->flags & RX_DESC_VALID_FCS)))
		status->flag |= RX_FLAG_FAILED_FCS_CRC;


	/* FIXME: set status->rate_idx */
}

104 105
static void wl1251_rx_body(struct wl1251 *wl,
			   struct wl1251_rx_descriptor *desc)
K
Kalle Valo 已提交
106 107 108 109 110 111 112
{
	struct sk_buff *skb;
	struct ieee80211_rx_status status;
	u8 *rx_buffer, beacon = 0;
	u16 length, *fc;
	u32 curr_id, last_id_inc, rx_packet_ring_addr;

113
	length = WL1251_RX_ALIGN(desc->length  - PLCP_HEADER_LENGTH);
K
Kalle Valo 已提交
114 115 116 117
	curr_id = (desc->flags & RX_DESC_SEQNUM_MASK) >> RX_DESC_PACKETID_SHIFT;
	last_id_inc = (wl->rx_last_id + 1) % (RX_MAX_PACKET_ID + 1);

	if (last_id_inc != curr_id) {
118
		wl1251_warning("curr ID:%d, last ID inc:%d",
K
Kalle Valo 已提交
119 120 121 122 123 124 125
			       curr_id, last_id_inc);
		wl->rx_last_id = curr_id;
	} else {
		wl->rx_last_id = last_id_inc;
	}

	rx_packet_ring_addr = wl->data_path->rx_packet_ring_addr +
126
		sizeof(struct wl1251_rx_descriptor) + 20;
K
Kalle Valo 已提交
127 128 129
	if (wl->rx_current_buffer)
		rx_packet_ring_addr += wl->data_path->rx_packet_ring_chunk_size;

K
Kalle Valo 已提交
130
	skb = __dev_alloc_skb(length, GFP_KERNEL);
K
Kalle Valo 已提交
131
	if (!skb) {
132
		wl1251_error("Couldn't allocate RX frame");
K
Kalle Valo 已提交
133 134 135 136
		return;
	}

	rx_buffer = skb_put(skb, length);
137
	wl1251_mem_read(wl, rx_packet_ring_addr, rx_buffer, length);
K
Kalle Valo 已提交
138 139 140 141 142 143 144 145 146

	/* The actual lenght doesn't include the target's alignment */
	skb->len = desc->length  - PLCP_HEADER_LENGTH;

	fc = (u16 *)skb->data;

	if ((*fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_BEACON)
		beacon = 1;

147
	wl1251_rx_status(wl, desc, &status, beacon);
K
Kalle Valo 已提交
148

149
	wl1251_debug(DEBUG_RX, "rx skb 0x%p: %d B %s", skb, skb->len,
K
Kalle Valo 已提交
150 151
		     beacon ? "beacon" : "");

152
	memcpy(IEEE80211_SKB_RXCB(skb), &status, sizeof(status));
K
Kalle Valo 已提交
153
	ieee80211_rx_ni(wl->hw, skb);
K
Kalle Valo 已提交
154 155
}

156
static void wl1251_rx_ack(struct wl1251 *wl)
K
Kalle Valo 已提交
157 158 159 160 161 162 163 164 165 166 167
{
	u32 data, addr;

	if (wl->rx_current_buffer) {
		addr = ACX_REG_INTERRUPT_TRIG_H;
		data = INTR_TRIG_RX_PROC1;
	} else {
		addr = ACX_REG_INTERRUPT_TRIG;
		data = INTR_TRIG_RX_PROC0;
	}

168
	wl1251_reg_write32(wl, addr, data);
K
Kalle Valo 已提交
169 170 171 172 173 174

	/* Toggle buffer ring */
	wl->rx_current_buffer = !wl->rx_current_buffer;
}


175
void wl1251_rx(struct wl1251 *wl)
K
Kalle Valo 已提交
176
{
177
	struct wl1251_rx_descriptor *rx_desc;
K
Kalle Valo 已提交
178

179
	if (wl->state != WL1251_STATE_ON)
K
Kalle Valo 已提交
180 181
		return;

K
Kalle Valo 已提交
182 183
	rx_desc = wl->rx_descriptor;

K
Kalle Valo 已提交
184
	/* We first read the frame's header */
185
	wl1251_rx_header(wl, rx_desc);
K
Kalle Valo 已提交
186 187

	/* Now we can read the body */
188
	wl1251_rx_body(wl, rx_desc);
K
Kalle Valo 已提交
189 190

	/* Finally, we need to ACK the RX */
191
	wl1251_rx_ack(wl);
K
Kalle Valo 已提交
192
}