blob: af8187909c944a2192a237de3c86036c7ea75d85 [file] [log] [blame]
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001/*
Kalle Valo80301cd2009-06-12 14:17:39 +03002 * This file is part of wl1251
Kalle Valo2f01a1f2009-04-29 23:33:31 +03003 *
4 * Copyright (c) 1998-2007 Texas Instruments Incorporated
5 * Copyright (C) 2008 Nokia Corporation
6 *
7 * Contact: Kalle Valo <kalle.valo@nokia.com>
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * version 2 as published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21 * 02110-1301 USA
22 *
23 */
24
25#include <linux/skbuff.h>
26#include <net/mac80211.h>
27
Kalle Valo13674112009-06-12 14:17:25 +030028#include "wl1251.h"
Kalle Valo2f01a1f2009-04-29 23:33:31 +030029#include "reg.h"
Bob Copeland0764de62009-08-07 13:32:56 +030030#include "wl1251_io.h"
Kalle Valoef2f8d42009-06-12 14:17:19 +030031#include "wl1251_rx.h"
Bob Copeland0764de62009-08-07 13:32:56 +030032#include "wl1251_cmd.h"
Kalle Valoef2f8d42009-06-12 14:17:19 +030033#include "wl1251_acx.h"
Kalle Valo2f01a1f2009-04-29 23:33:31 +030034
Kalle Valo80301cd2009-06-12 14:17:39 +030035static void wl1251_rx_header(struct wl1251 *wl,
36 struct wl1251_rx_descriptor *desc)
Kalle Valo2f01a1f2009-04-29 23:33:31 +030037{
38 u32 rx_packet_ring_addr;
39
40 rx_packet_ring_addr = wl->data_path->rx_packet_ring_addr;
41 if (wl->rx_current_buffer)
42 rx_packet_ring_addr += wl->data_path->rx_packet_ring_chunk_size;
43
Bob Copeland0764de62009-08-07 13:32:56 +030044 wl1251_mem_read(wl, rx_packet_ring_addr, desc, sizeof(*desc));
Kalle Valo2f01a1f2009-04-29 23:33:31 +030045}
46
Kalle Valo80301cd2009-06-12 14:17:39 +030047static void wl1251_rx_status(struct wl1251 *wl,
48 struct wl1251_rx_descriptor *desc,
Kalle Valo2f01a1f2009-04-29 23:33:31 +030049 struct ieee80211_rx_status *status,
50 u8 beacon)
51{
Kalle Valoff258392009-06-12 14:14:19 +030052 u64 mactime;
53 int ret;
54
Kalle Valo2f01a1f2009-04-29 23:33:31 +030055 memset(status, 0, sizeof(struct ieee80211_rx_status));
56
57 status->band = IEEE80211_BAND_2GHZ;
58 status->mactime = desc->timestamp;
59
60 /*
61 * The rx status timestamp is a 32 bits value while the TSF is a
62 * 64 bits one.
63 * For IBSS merging, TSF is mandatory, so we have to get it
64 * somehow, so we ask for ACX_TSF_INFO.
65 * That could be moved to the get_tsf() hook, but unfortunately,
66 * this one must be atomic, while our SPI routines can sleep.
67 */
68 if ((wl->bss_type == BSS_TYPE_IBSS) && beacon) {
Kalle Valo80301cd2009-06-12 14:17:39 +030069 ret = wl1251_acx_tsf_info(wl, &mactime);
Kalle Valoff258392009-06-12 14:14:19 +030070 if (ret == 0)
71 status->mactime = mactime;
Kalle Valo2f01a1f2009-04-29 23:33:31 +030072 }
73
74 status->signal = desc->rssi;
Kalle Valo80301cd2009-06-12 14:17:39 +030075 status->qual = (desc->rssi - WL1251_RX_MIN_RSSI) * 100 /
76 (WL1251_RX_MAX_RSSI - WL1251_RX_MIN_RSSI);
Kalle Valo2f01a1f2009-04-29 23:33:31 +030077 status->qual = min(status->qual, 100);
78 status->qual = max(status->qual, 0);
79
80 /*
81 * FIXME: guessing that snr needs to be divided by two, otherwise
82 * the values don't make any sense
83 */
84 status->noise = desc->rssi - desc->snr / 2;
85
86 status->freq = ieee80211_channel_to_frequency(desc->channel);
87
88 status->flag |= RX_FLAG_TSFT;
89
90 if (desc->flags & RX_DESC_ENCRYPTION_MASK) {
91 status->flag |= RX_FLAG_IV_STRIPPED | RX_FLAG_MMIC_STRIPPED;
92
93 if (likely(!(desc->flags & RX_DESC_DECRYPT_FAIL)))
94 status->flag |= RX_FLAG_DECRYPTED;
95
96 if (unlikely(desc->flags & RX_DESC_MIC_FAIL))
97 status->flag |= RX_FLAG_MMIC_ERROR;
98 }
99
100 if (unlikely(!(desc->flags & RX_DESC_VALID_FCS)))
101 status->flag |= RX_FLAG_FAILED_FCS_CRC;
102
103
104 /* FIXME: set status->rate_idx */
105}
106
Kalle Valo80301cd2009-06-12 14:17:39 +0300107static void wl1251_rx_body(struct wl1251 *wl,
108 struct wl1251_rx_descriptor *desc)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300109{
110 struct sk_buff *skb;
111 struct ieee80211_rx_status status;
112 u8 *rx_buffer, beacon = 0;
113 u16 length, *fc;
114 u32 curr_id, last_id_inc, rx_packet_ring_addr;
115
Kalle Valo80301cd2009-06-12 14:17:39 +0300116 length = WL1251_RX_ALIGN(desc->length - PLCP_HEADER_LENGTH);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300117 curr_id = (desc->flags & RX_DESC_SEQNUM_MASK) >> RX_DESC_PACKETID_SHIFT;
118 last_id_inc = (wl->rx_last_id + 1) % (RX_MAX_PACKET_ID + 1);
119
120 if (last_id_inc != curr_id) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300121 wl1251_warning("curr ID:%d, last ID inc:%d",
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300122 curr_id, last_id_inc);
123 wl->rx_last_id = curr_id;
124 } else {
125 wl->rx_last_id = last_id_inc;
126 }
127
128 rx_packet_ring_addr = wl->data_path->rx_packet_ring_addr +
Kalle Valo80301cd2009-06-12 14:17:39 +0300129 sizeof(struct wl1251_rx_descriptor) + 20;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300130 if (wl->rx_current_buffer)
131 rx_packet_ring_addr += wl->data_path->rx_packet_ring_chunk_size;
132
133 skb = dev_alloc_skb(length);
134 if (!skb) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300135 wl1251_error("Couldn't allocate RX frame");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300136 return;
137 }
138
139 rx_buffer = skb_put(skb, length);
Bob Copeland0764de62009-08-07 13:32:56 +0300140 wl1251_mem_read(wl, rx_packet_ring_addr, rx_buffer, length);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300141
142 /* The actual lenght doesn't include the target's alignment */
143 skb->len = desc->length - PLCP_HEADER_LENGTH;
144
145 fc = (u16 *)skb->data;
146
147 if ((*fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_BEACON)
148 beacon = 1;
149
Kalle Valo80301cd2009-06-12 14:17:39 +0300150 wl1251_rx_status(wl, desc, &status, beacon);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300151
Kalle Valo80301cd2009-06-12 14:17:39 +0300152 wl1251_debug(DEBUG_RX, "rx skb 0x%p: %d B %s", skb, skb->len,
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300153 beacon ? "beacon" : "");
154
Johannes Bergf1d58c22009-06-17 13:13:00 +0200155 memcpy(IEEE80211_SKB_RXCB(skb), &status, sizeof(status));
156 ieee80211_rx(wl->hw, skb);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300157}
158
Kalle Valo80301cd2009-06-12 14:17:39 +0300159static void wl1251_rx_ack(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300160{
161 u32 data, addr;
162
163 if (wl->rx_current_buffer) {
164 addr = ACX_REG_INTERRUPT_TRIG_H;
165 data = INTR_TRIG_RX_PROC1;
166 } else {
167 addr = ACX_REG_INTERRUPT_TRIG;
168 data = INTR_TRIG_RX_PROC0;
169 }
170
Kalle Valo80301cd2009-06-12 14:17:39 +0300171 wl1251_reg_write32(wl, addr, data);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300172
173 /* Toggle buffer ring */
174 wl->rx_current_buffer = !wl->rx_current_buffer;
175}
176
177
Kalle Valo80301cd2009-06-12 14:17:39 +0300178void wl1251_rx(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300179{
Kalle Valo80301cd2009-06-12 14:17:39 +0300180 struct wl1251_rx_descriptor *rx_desc;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300181
Kalle Valo80301cd2009-06-12 14:17:39 +0300182 if (wl->state != WL1251_STATE_ON)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300183 return;
184
Kalle Valo47212132009-06-12 14:15:08 +0300185 rx_desc = wl->rx_descriptor;
186
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300187 /* We first read the frame's header */
Kalle Valo80301cd2009-06-12 14:17:39 +0300188 wl1251_rx_header(wl, rx_desc);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300189
190 /* Now we can read the body */
Kalle Valo80301cd2009-06-12 14:17:39 +0300191 wl1251_rx_body(wl, rx_desc);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300192
193 /* Finally, we need to ACK the RX */
Kalle Valo80301cd2009-06-12 14:17:39 +0300194 wl1251_rx_ack(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300195
196 return;
197}