Add linux-next specific files for 20110831
[linux-2.6/next.git] / drivers / net / wireless / wl12xx / rx.c
blob78d8410da1f4586c947912c8ec9086b7a0fd81dc
1 /*
2 * This file is part of wl1271
4 * Copyright (C) 2009 Nokia Corporation
6 * Contact: Luciano Coelho <luciano.coelho@nokia.com>
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * version 2 as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA
24 #include <linux/gfp.h>
25 #include <linux/sched.h>
27 #include "wl12xx.h"
28 #include "acx.h"
29 #include "reg.h"
30 #include "rx.h"
31 #include "io.h"
33 static u8 wl12xx_rx_get_mem_block(struct wl12xx_fw_status *status,
34 u32 drv_rx_counter)
36 return le32_to_cpu(status->rx_pkt_descs[drv_rx_counter]) &
37 RX_MEM_BLOCK_MASK;
40 static u32 wl12xx_rx_get_buf_size(struct wl12xx_fw_status *status,
41 u32 drv_rx_counter)
43 return (le32_to_cpu(status->rx_pkt_descs[drv_rx_counter]) &
44 RX_BUF_SIZE_MASK) >> RX_BUF_SIZE_SHIFT_DIV;
47 static bool wl12xx_rx_get_unaligned(struct wl12xx_fw_status *status,
48 u32 drv_rx_counter)
50 /* Convert the value to bool */
51 return !!(le32_to_cpu(status->rx_pkt_descs[drv_rx_counter]) &
52 RX_BUF_UNALIGNED_PAYLOAD);
55 static void wl1271_rx_status(struct wl1271 *wl,
56 struct wl1271_rx_descriptor *desc,
57 struct ieee80211_rx_status *status,
58 u8 beacon)
60 memset(status, 0, sizeof(struct ieee80211_rx_status));
62 if ((desc->flags & WL1271_RX_DESC_BAND_MASK) == WL1271_RX_DESC_BAND_BG)
63 status->band = IEEE80211_BAND_2GHZ;
64 else
65 status->band = IEEE80211_BAND_5GHZ;
67 status->rate_idx = wl1271_rate_to_idx(desc->rate, status->band);
69 #ifdef CONFIG_WL12XX_HT
70 /* 11n support */
71 if (desc->rate <= CONF_HW_RXTX_RATE_MCS0)
72 status->flag |= RX_FLAG_HT;
73 #endif
75 status->signal = desc->rssi;
78 * FIXME: In wl1251, the SNR should be divided by two. In wl1271 we
79 * need to divide by two for now, but TI has been discussing about
80 * changing it. This needs to be rechecked.
82 wl->noise = desc->rssi - (desc->snr >> 1);
84 status->freq = ieee80211_channel_to_frequency(desc->channel,
85 status->band);
87 if (desc->flags & WL1271_RX_DESC_ENCRYPT_MASK) {
88 u8 desc_err_code = desc->status & WL1271_RX_DESC_STATUS_MASK;
90 status->flag |= RX_FLAG_IV_STRIPPED | RX_FLAG_MMIC_STRIPPED |
91 RX_FLAG_DECRYPTED;
93 if (unlikely(desc_err_code == WL1271_RX_DESC_MIC_FAIL)) {
94 status->flag |= RX_FLAG_MMIC_ERROR;
95 wl1271_warning("Michael MIC error");
100 static int wl1271_rx_handle_data(struct wl1271 *wl, u8 *data, u32 length,
101 bool unaligned)
103 struct wl1271_rx_descriptor *desc;
104 struct sk_buff *skb;
105 struct ieee80211_hdr *hdr;
106 u8 *buf;
107 u8 beacon = 0;
108 u8 is_data = 0;
109 u8 reserved = unaligned ? NET_IP_ALIGN : 0;
112 * In PLT mode we seem to get frames and mac80211 warns about them,
113 * workaround this by not retrieving them at all.
115 if (unlikely(wl->state == WL1271_STATE_PLT))
116 return -EINVAL;
118 /* the data read starts with the descriptor */
119 desc = (struct wl1271_rx_descriptor *) data;
121 if (desc->packet_class == WL12XX_RX_CLASS_LOGGER) {
122 size_t len = length - sizeof(*desc);
123 wl12xx_copy_fwlog(wl, data + sizeof(*desc), len);
124 wake_up_interruptible(&wl->fwlog_waitq);
125 return 0;
128 switch (desc->status & WL1271_RX_DESC_STATUS_MASK) {
129 /* discard corrupted packets */
130 case WL1271_RX_DESC_DRIVER_RX_Q_FAIL:
131 case WL1271_RX_DESC_DECRYPT_FAIL:
132 wl1271_warning("corrupted packet in RX with status: 0x%x",
133 desc->status & WL1271_RX_DESC_STATUS_MASK);
134 return -EINVAL;
135 case WL1271_RX_DESC_SUCCESS:
136 case WL1271_RX_DESC_MIC_FAIL:
137 break;
138 default:
139 wl1271_error("invalid RX descriptor status: 0x%x",
140 desc->status & WL1271_RX_DESC_STATUS_MASK);
141 return -EINVAL;
144 /* skb length not included rx descriptor */
145 skb = __dev_alloc_skb(length + reserved - sizeof(*desc), GFP_KERNEL);
146 if (!skb) {
147 wl1271_error("Couldn't allocate RX frame");
148 return -ENOMEM;
151 /* reserve the unaligned payload(if any) */
152 skb_reserve(skb, reserved);
154 buf = skb_put(skb, length - sizeof(*desc));
157 * Copy packets from aggregation buffer to the skbs without rx
158 * descriptor and with packet payload aligned care. In case of unaligned
159 * packets copy the packets in offset of 2 bytes guarantee IP header
160 * payload aligned to 4 bytes.
162 memcpy(buf, data + sizeof(*desc), length - sizeof(*desc));
164 hdr = (struct ieee80211_hdr *)skb->data;
165 if (ieee80211_is_beacon(hdr->frame_control))
166 beacon = 1;
167 if (ieee80211_is_data_present(hdr->frame_control))
168 is_data = 1;
170 wl1271_rx_status(wl, desc, IEEE80211_SKB_RXCB(skb), beacon);
172 wl1271_debug(DEBUG_RX, "rx skb 0x%p: %d B %s", skb,
173 skb->len - desc->pad_len,
174 beacon ? "beacon" : "");
176 skb_trim(skb, skb->len - desc->pad_len);
178 skb_queue_tail(&wl->deferred_rx_queue, skb);
179 queue_work(wl->freezable_wq, &wl->netstack_work);
181 return is_data;
184 void wl12xx_rx(struct wl1271 *wl, struct wl12xx_fw_status *status)
186 struct wl1271_acx_mem_map *wl_mem_map = wl->target_mem_map;
187 u32 buf_size;
188 u32 fw_rx_counter = status->fw_rx_counter & NUM_RX_PKT_DESC_MOD_MASK;
189 u32 drv_rx_counter = wl->rx_counter & NUM_RX_PKT_DESC_MOD_MASK;
190 u32 rx_counter;
191 u32 mem_block;
192 u32 pkt_length;
193 u32 pkt_offset;
194 bool is_ap = (wl->bss_type == BSS_TYPE_AP_BSS);
195 bool had_data = false;
196 bool unaligned = false;
198 while (drv_rx_counter != fw_rx_counter) {
199 buf_size = 0;
200 rx_counter = drv_rx_counter;
201 while (rx_counter != fw_rx_counter) {
202 pkt_length = wl12xx_rx_get_buf_size(status, rx_counter);
203 if (buf_size + pkt_length > WL1271_AGGR_BUFFER_SIZE)
204 break;
205 buf_size += pkt_length;
206 rx_counter++;
207 rx_counter &= NUM_RX_PKT_DESC_MOD_MASK;
210 if (buf_size == 0) {
211 wl1271_warning("received empty data");
212 break;
215 if (wl->chip.id != CHIP_ID_1283_PG20) {
217 * Choose the block we want to read
218 * For aggregated packets, only the first memory block
219 * should be retrieved. The FW takes care of the rest.
221 mem_block = wl12xx_rx_get_mem_block(status,
222 drv_rx_counter);
224 wl->rx_mem_pool_addr.addr = (mem_block << 8) +
225 le32_to_cpu(wl_mem_map->packet_memory_pool_start);
227 wl->rx_mem_pool_addr.addr_extra =
228 wl->rx_mem_pool_addr.addr + 4;
230 wl1271_write(wl, WL1271_SLV_REG_DATA,
231 &wl->rx_mem_pool_addr,
232 sizeof(wl->rx_mem_pool_addr), false);
235 /* Read all available packets at once */
236 wl1271_read(wl, WL1271_SLV_MEM_DATA, wl->aggr_buf,
237 buf_size, true);
239 /* Split data into separate packets */
240 pkt_offset = 0;
241 while (pkt_offset < buf_size) {
242 pkt_length = wl12xx_rx_get_buf_size(status,
243 drv_rx_counter);
245 unaligned = wl12xx_rx_get_unaligned(status,
246 drv_rx_counter);
249 * the handle data call can only fail in memory-outage
250 * conditions, in that case the received frame will just
251 * be dropped.
253 if (wl1271_rx_handle_data(wl,
254 wl->aggr_buf + pkt_offset,
255 pkt_length, unaligned) == 1)
256 had_data = true;
258 wl->rx_counter++;
259 drv_rx_counter++;
260 drv_rx_counter &= NUM_RX_PKT_DESC_MOD_MASK;
261 pkt_offset += pkt_length;
266 * Write the driver's packet counter to the FW. This is only required
267 * for older hardware revisions
269 if (wl->quirks & WL12XX_QUIRK_END_OF_TRANSACTION)
270 wl1271_write32(wl, RX_DRIVER_COUNTER_ADDRESS, wl->rx_counter);
272 if (!is_ap && wl->conf.rx_streaming.interval && had_data &&
273 (wl->conf.rx_streaming.always ||
274 test_bit(WL1271_FLAG_SOFT_GEMINI, &wl->flags))) {
275 u32 timeout = wl->conf.rx_streaming.duration;
277 /* restart rx streaming */
278 if (!test_bit(WL1271_FLAG_RX_STREAMING_STARTED, &wl->flags))
279 ieee80211_queue_work(wl->hw,
280 &wl->rx_streaming_enable_work);
282 mod_timer(&wl->rx_streaming_timer,
283 jiffies + msecs_to_jiffies(timeout));