blob: 1dd84582ef32734736b2fa5c1219484c585a5b9a [file] [log] [blame]
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001/*
2 * This file is part of wl1271
3 *
4 * Copyright (C) 2009 Nokia Corporation
5 *
6 * Contact: Luciano Coelho <luciano.coelho@nokia.com>
7 *
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.
11 *
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.
16 *
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
21 *
22 */
23
24#include "wl1271.h"
25#include "wl1271_acx.h"
26#include "wl1271_reg.h"
27#include "wl1271_rx.h"
28#include "wl1271_spi.h"
29
30static u8 wl1271_rx_get_mem_block(struct wl1271_fw_status *status,
31 u32 drv_rx_counter)
32{
33 return status->rx_pkt_descs[drv_rx_counter] & RX_MEM_BLOCK_MASK;
34}
35
36static u32 wl1271_rx_get_buf_size(struct wl1271_fw_status *status,
37 u32 drv_rx_counter)
38{
39 return (status->rx_pkt_descs[drv_rx_counter] & RX_BUF_SIZE_MASK) >>
40 RX_BUF_SIZE_SHIFT_DIV;
41}
42
43/* The values of this table must match the wl1271_rates[] array */
44static u8 wl1271_rx_rate_to_idx[] = {
45 /* MCS rates are used only with 11n */
46 WL1271_RX_RATE_UNSUPPORTED, /* WL1271_RATE_MCS7 */
47 WL1271_RX_RATE_UNSUPPORTED, /* WL1271_RATE_MCS6 */
48 WL1271_RX_RATE_UNSUPPORTED, /* WL1271_RATE_MCS5 */
49 WL1271_RX_RATE_UNSUPPORTED, /* WL1271_RATE_MCS4 */
50 WL1271_RX_RATE_UNSUPPORTED, /* WL1271_RATE_MCS3 */
51 WL1271_RX_RATE_UNSUPPORTED, /* WL1271_RATE_MCS2 */
52 WL1271_RX_RATE_UNSUPPORTED, /* WL1271_RATE_MCS1 */
53 WL1271_RX_RATE_UNSUPPORTED, /* WL1271_RATE_MCS0 */
54
55 11, /* WL1271_RATE_54 */
56 10, /* WL1271_RATE_48 */
57 9, /* WL1271_RATE_36 */
58 8, /* WL1271_RATE_24 */
59
60 /* TI-specific rate */
61 WL1271_RX_RATE_UNSUPPORTED, /* WL1271_RATE_22 */
62
63 7, /* WL1271_RATE_18 */
64 6, /* WL1271_RATE_12 */
65 3, /* WL1271_RATE_11 */
66 5, /* WL1271_RATE_9 */
67 4, /* WL1271_RATE_6 */
68 2, /* WL1271_RATE_5_5 */
69 1, /* WL1271_RATE_2 */
70 0 /* WL1271_RATE_1 */
71};
72
Teemu Paasikivia4102642009-10-13 12:47:51 +030073/* The values of this table must match the wl1271_rates[] array */
74static u8 wl1271_5_ghz_rx_rate_to_idx[] = {
75 /* MCS rates are used only with 11n */
76 WL1271_RX_RATE_UNSUPPORTED, /* WL1271_RATE_MCS7 */
77 WL1271_RX_RATE_UNSUPPORTED, /* WL1271_RATE_MCS6 */
78 WL1271_RX_RATE_UNSUPPORTED, /* WL1271_RATE_MCS5 */
79 WL1271_RX_RATE_UNSUPPORTED, /* WL1271_RATE_MCS4 */
80 WL1271_RX_RATE_UNSUPPORTED, /* WL1271_RATE_MCS3 */
81 WL1271_RX_RATE_UNSUPPORTED, /* WL1271_RATE_MCS2 */
82 WL1271_RX_RATE_UNSUPPORTED, /* WL1271_RATE_MCS1 */
83 WL1271_RX_RATE_UNSUPPORTED, /* WL1271_RATE_MCS0 */
84
85 7, /* WL1271_RATE_54 */
86 6, /* WL1271_RATE_48 */
87 5, /* WL1271_RATE_36 */
88 4, /* WL1271_RATE_24 */
89
90 /* TI-specific rate */
91 WL1271_RX_RATE_UNSUPPORTED, /* WL1271_RATE_22 */
92
93 3, /* WL1271_RATE_18 */
94 2, /* WL1271_RATE_12 */
95 WL1271_RX_RATE_UNSUPPORTED, /* WL1271_RATE_11 */
96 1, /* WL1271_RATE_9 */
97 0, /* WL1271_RATE_6 */
98 WL1271_RX_RATE_UNSUPPORTED, /* WL1271_RATE_5_5 */
99 WL1271_RX_RATE_UNSUPPORTED, /* WL1271_RATE_2 */
100 WL1271_RX_RATE_UNSUPPORTED /* WL1271_RATE_1 */
101};
102
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300103static void wl1271_rx_status(struct wl1271 *wl,
104 struct wl1271_rx_descriptor *desc,
105 struct ieee80211_rx_status *status,
106 u8 beacon)
107{
108 memset(status, 0, sizeof(struct ieee80211_rx_status));
109
Teemu Paasikivia4102642009-10-13 12:47:51 +0300110 if ((desc->flags & WL1271_RX_DESC_BAND_MASK) ==
111 WL1271_RX_DESC_BAND_BG) {
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300112 status->band = IEEE80211_BAND_2GHZ;
Teemu Paasikivia4102642009-10-13 12:47:51 +0300113 status->rate_idx = wl1271_rx_rate_to_idx[desc->rate];
114 } else if ((desc->flags & WL1271_RX_DESC_BAND_MASK) ==
115 WL1271_RX_DESC_BAND_A) {
Teemu Paasikiviabb0b3b2009-10-13 12:47:50 +0300116 status->band = IEEE80211_BAND_5GHZ;
Teemu Paasikivia4102642009-10-13 12:47:51 +0300117 status->rate_idx = wl1271_5_ghz_rx_rate_to_idx[desc->rate];
118 } else
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300119 wl1271_warning("unsupported band 0x%x",
120 desc->flags & WL1271_RX_DESC_BAND_MASK);
121
Teemu Paasikivia4102642009-10-13 12:47:51 +0300122 if (unlikely(status->rate_idx == WL1271_RX_RATE_UNSUPPORTED))
123 wl1271_warning("unsupported rate");
124
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300125 /*
126 * FIXME: Add mactime handling. For IBSS (ad-hoc) we need to get the
127 * timestamp from the beacon (acx_tsf_info). In BSS mode (infra) we
128 * only need the mactime for monitor mode. For now the mactime is
129 * not valid, so RX_FLAG_TSFT should not be set
130 */
131 status->signal = desc->rssi;
132
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300133 /*
134 * FIXME: In wl1251, the SNR should be divided by two. In wl1271 we
135 * need to divide by two for now, but TI has been discussing about
136 * changing it. This needs to be rechecked.
137 */
138 status->noise = desc->rssi - (desc->snr >> 1);
139
140 status->freq = ieee80211_channel_to_frequency(desc->channel);
141
142 if (desc->flags & WL1271_RX_DESC_ENCRYPT_MASK) {
143 status->flag |= RX_FLAG_IV_STRIPPED | RX_FLAG_MMIC_STRIPPED;
144
145 if (likely(!(desc->flags & WL1271_RX_DESC_DECRYPT_FAIL)))
146 status->flag |= RX_FLAG_DECRYPTED;
Teemu Paasikivia4102642009-10-13 12:47:51 +0300147 /* FIXME: Flag should be also set when using 5 GHz band.
148 * At the moment chip reports MIC failed on all packets,
149 * so flag is silently discarded.
150 */
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300151 if (unlikely(desc->flags & WL1271_RX_DESC_MIC_FAIL))
Teemu Paasikivia4102642009-10-13 12:47:51 +0300152 if (status->band != IEEE80211_BAND_5GHZ)
153 status->flag |= RX_FLAG_MMIC_ERROR;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300154 }
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300155}
156
157static void wl1271_rx_handle_data(struct wl1271 *wl, u32 length)
158{
159 struct ieee80211_rx_status rx_status;
160 struct wl1271_rx_descriptor *desc;
161 struct sk_buff *skb;
162 u16 *fc;
163 u8 *buf;
164 u8 beacon = 0;
165
166 skb = dev_alloc_skb(length);
167 if (!skb) {
168 wl1271_error("Couldn't allocate RX frame");
169 return;
170 }
171
172 buf = skb_put(skb, length);
Juuso Oikarinen74621412009-10-12 15:08:54 +0300173 wl1271_spi_read(wl, WL1271_SLV_MEM_DATA, buf, length, true);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300174
175 /* the data read starts with the descriptor */
176 desc = (struct wl1271_rx_descriptor *) buf;
177
178 /* now we pull the descriptor out of the buffer */
179 skb_pull(skb, sizeof(*desc));
180
181 fc = (u16 *)skb->data;
182 if ((*fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_BEACON)
183 beacon = 1;
184
185 wl1271_rx_status(wl, desc, &rx_status, beacon);
186
187 wl1271_debug(DEBUG_RX, "rx skb 0x%p: %d B %s", skb, skb->len,
188 beacon ? "beacon" : "");
189
190 memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
191 ieee80211_rx(wl->hw, skb);
192}
193
194void wl1271_rx(struct wl1271 *wl, struct wl1271_fw_status *status)
195{
196 struct wl1271_acx_mem_map *wl_mem_map = wl->target_mem_map;
197 u32 buf_size;
198 u32 fw_rx_counter = status->fw_rx_counter & NUM_RX_PKT_DESC_MOD_MASK;
199 u32 drv_rx_counter = wl->rx_counter & NUM_RX_PKT_DESC_MOD_MASK;
200 u32 mem_block;
201
202 while (drv_rx_counter != fw_rx_counter) {
203 mem_block = wl1271_rx_get_mem_block(status, drv_rx_counter);
204 buf_size = wl1271_rx_get_buf_size(status, drv_rx_counter);
205
206 if (buf_size == 0) {
207 wl1271_warning("received empty data");
208 break;
209 }
210
211 wl->rx_mem_pool_addr.addr =
212 (mem_block << 8) + wl_mem_map->packet_memory_pool_start;
213 wl->rx_mem_pool_addr.addr_extra =
214 wl->rx_mem_pool_addr.addr + 4;
215
216 /* Choose the block we want to read */
Juuso Oikarinen74621412009-10-12 15:08:54 +0300217 wl1271_spi_write(wl, WL1271_SLV_REG_DATA,
218 &wl->rx_mem_pool_addr,
219 sizeof(wl->rx_mem_pool_addr), false);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300220
221 wl1271_rx_handle_data(wl, buf_size);
222
223 wl->rx_counter++;
224 drv_rx_counter = wl->rx_counter & NUM_RX_PKT_DESC_MOD_MASK;
225 }
226
Juuso Oikarinen74621412009-10-12 15:08:54 +0300227 wl1271_spi_write32(wl, RX_DRIVER_COUNTER_ADDRESS, wl->rx_counter);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300228}