Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/linville/wirel...
[linux-2.6.git] / drivers / net / wireless / wl12xx / wl1271_rx.c
1 /*
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 #include "wl1271_io.h"
30
31 static u8 wl1271_rx_get_mem_block(struct wl1271_fw_status *status,
32                                   u32 drv_rx_counter)
33 {
34         return le32_to_cpu(status->rx_pkt_descs[drv_rx_counter]) &
35                 RX_MEM_BLOCK_MASK;
36 }
37
38 static u32 wl1271_rx_get_buf_size(struct wl1271_fw_status *status,
39                                  u32 drv_rx_counter)
40 {
41         return (le32_to_cpu(status->rx_pkt_descs[drv_rx_counter]) &
42                 RX_BUF_SIZE_MASK) >> RX_BUF_SIZE_SHIFT_DIV;
43 }
44
45 /* The values of this table must match the wl1271_rates[] array */
46 static u8 wl1271_rx_rate_to_idx[] = {
47         /* MCS rates are used only with 11n */
48         WL1271_RX_RATE_UNSUPPORTED, /* WL1271_RATE_MCS7 */
49         WL1271_RX_RATE_UNSUPPORTED, /* WL1271_RATE_MCS6 */
50         WL1271_RX_RATE_UNSUPPORTED, /* WL1271_RATE_MCS5 */
51         WL1271_RX_RATE_UNSUPPORTED, /* WL1271_RATE_MCS4 */
52         WL1271_RX_RATE_UNSUPPORTED, /* WL1271_RATE_MCS3 */
53         WL1271_RX_RATE_UNSUPPORTED, /* WL1271_RATE_MCS2 */
54         WL1271_RX_RATE_UNSUPPORTED, /* WL1271_RATE_MCS1 */
55         WL1271_RX_RATE_UNSUPPORTED, /* WL1271_RATE_MCS0 */
56
57         11,                         /* WL1271_RATE_54   */
58         10,                         /* WL1271_RATE_48   */
59         9,                          /* WL1271_RATE_36   */
60         8,                          /* WL1271_RATE_24   */
61
62         /* TI-specific rate */
63         WL1271_RX_RATE_UNSUPPORTED, /* WL1271_RATE_22   */
64
65         7,                          /* WL1271_RATE_18   */
66         6,                          /* WL1271_RATE_12   */
67         3,                          /* WL1271_RATE_11   */
68         5,                          /* WL1271_RATE_9    */
69         4,                          /* WL1271_RATE_6    */
70         2,                          /* WL1271_RATE_5_5  */
71         1,                          /* WL1271_RATE_2    */
72         0                           /* WL1271_RATE_1    */
73 };
74
75 /* The values of this table must match the wl1271_rates[] array */
76 static u8 wl1271_5_ghz_rx_rate_to_idx[] = {
77         /* MCS rates are used only with 11n */
78         WL1271_RX_RATE_UNSUPPORTED, /* WL1271_RATE_MCS7 */
79         WL1271_RX_RATE_UNSUPPORTED, /* WL1271_RATE_MCS6 */
80         WL1271_RX_RATE_UNSUPPORTED, /* WL1271_RATE_MCS5 */
81         WL1271_RX_RATE_UNSUPPORTED, /* WL1271_RATE_MCS4 */
82         WL1271_RX_RATE_UNSUPPORTED, /* WL1271_RATE_MCS3 */
83         WL1271_RX_RATE_UNSUPPORTED, /* WL1271_RATE_MCS2 */
84         WL1271_RX_RATE_UNSUPPORTED, /* WL1271_RATE_MCS1 */
85         WL1271_RX_RATE_UNSUPPORTED, /* WL1271_RATE_MCS0 */
86
87         7,                          /* WL1271_RATE_54   */
88         6,                          /* WL1271_RATE_48   */
89         5,                          /* WL1271_RATE_36   */
90         4,                          /* WL1271_RATE_24   */
91
92         /* TI-specific rate */
93         WL1271_RX_RATE_UNSUPPORTED, /* WL1271_RATE_22   */
94
95         3,                          /* WL1271_RATE_18   */
96         2,                          /* WL1271_RATE_12   */
97         WL1271_RX_RATE_UNSUPPORTED, /* WL1271_RATE_11   */
98         1,                          /* WL1271_RATE_9    */
99         0,                          /* WL1271_RATE_6    */
100         WL1271_RX_RATE_UNSUPPORTED, /* WL1271_RATE_5_5  */
101         WL1271_RX_RATE_UNSUPPORTED, /* WL1271_RATE_2    */
102         WL1271_RX_RATE_UNSUPPORTED  /* WL1271_RATE_1    */
103 };
104
105 static void wl1271_rx_status(struct wl1271 *wl,
106                              struct wl1271_rx_descriptor *desc,
107                              struct ieee80211_rx_status *status,
108                              u8 beacon)
109 {
110         memset(status, 0, sizeof(struct ieee80211_rx_status));
111
112         if ((desc->flags & WL1271_RX_DESC_BAND_MASK) ==
113             WL1271_RX_DESC_BAND_BG) {
114                 status->band = IEEE80211_BAND_2GHZ;
115                 status->rate_idx = wl1271_rx_rate_to_idx[desc->rate];
116         } else if ((desc->flags & WL1271_RX_DESC_BAND_MASK) ==
117                  WL1271_RX_DESC_BAND_A) {
118                 status->band = IEEE80211_BAND_5GHZ;
119                 status->rate_idx = wl1271_5_ghz_rx_rate_to_idx[desc->rate];
120         } else
121                 wl1271_warning("unsupported band 0x%x",
122                                desc->flags & WL1271_RX_DESC_BAND_MASK);
123
124         if (unlikely(status->rate_idx == WL1271_RX_RATE_UNSUPPORTED))
125                 wl1271_warning("unsupported rate");
126
127         /*
128          * FIXME: Add mactime handling.  For IBSS (ad-hoc) we need to get the
129          * timestamp from the beacon (acx_tsf_info).  In BSS mode (infra) we
130          * only need the mactime for monitor mode.  For now the mactime is
131          * not valid, so RX_FLAG_TSFT should not be set
132          */
133         status->signal = desc->rssi;
134
135         /*
136          * FIXME: In wl1251, the SNR should be divided by two.  In wl1271 we
137          * need to divide by two for now, but TI has been discussing about
138          * changing it.  This needs to be rechecked.
139          */
140         status->noise = desc->rssi - (desc->snr >> 1);
141
142         status->freq = ieee80211_channel_to_frequency(desc->channel);
143
144         if (desc->flags & WL1271_RX_DESC_ENCRYPT_MASK) {
145                 status->flag |= RX_FLAG_IV_STRIPPED | RX_FLAG_MMIC_STRIPPED;
146
147                 if (likely(!(desc->status & WL1271_RX_DESC_DECRYPT_FAIL)))
148                         status->flag |= RX_FLAG_DECRYPTED;
149                 if (unlikely(desc->status & WL1271_RX_DESC_MIC_FAIL))
150                         status->flag |= RX_FLAG_MMIC_ERROR;
151         }
152 }
153
154 static void wl1271_rx_handle_data(struct wl1271 *wl, u32 length)
155 {
156         struct ieee80211_rx_status rx_status;
157         struct wl1271_rx_descriptor *desc;
158         struct sk_buff *skb;
159         u16 *fc;
160         u8 *buf;
161         u8 beacon = 0;
162
163         skb = __dev_alloc_skb(length, GFP_KERNEL);
164         if (!skb) {
165                 wl1271_error("Couldn't allocate RX frame");
166                 return;
167         }
168
169         buf = skb_put(skb, length);
170         wl1271_read(wl, WL1271_SLV_MEM_DATA, buf, length, true);
171
172         /* the data read starts with the descriptor */
173         desc = (struct wl1271_rx_descriptor *) buf;
174
175         /* now we pull the descriptor out of the buffer */
176         skb_pull(skb, sizeof(*desc));
177
178         fc = (u16 *)skb->data;
179         if ((*fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_BEACON)
180                 beacon = 1;
181
182         wl1271_rx_status(wl, desc, &rx_status, beacon);
183
184         wl1271_debug(DEBUG_RX, "rx skb 0x%p: %d B %s", skb, skb->len,
185                      beacon ? "beacon" : "");
186
187         memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
188         ieee80211_rx_ni(wl->hw, skb);
189 }
190
191 void wl1271_rx(struct wl1271 *wl, struct wl1271_fw_status *status)
192 {
193         struct wl1271_acx_mem_map *wl_mem_map = wl->target_mem_map;
194         u32 buf_size;
195         u32 fw_rx_counter  = status->fw_rx_counter & NUM_RX_PKT_DESC_MOD_MASK;
196         u32 drv_rx_counter = wl->rx_counter & NUM_RX_PKT_DESC_MOD_MASK;
197         u32 mem_block;
198
199         while (drv_rx_counter != fw_rx_counter) {
200                 mem_block = wl1271_rx_get_mem_block(status, drv_rx_counter);
201                 buf_size = wl1271_rx_get_buf_size(status, drv_rx_counter);
202
203                 if (buf_size == 0) {
204                         wl1271_warning("received empty data");
205                         break;
206                 }
207
208                 wl->rx_mem_pool_addr.addr = (mem_block << 8) +
209                         le32_to_cpu(wl_mem_map->packet_memory_pool_start);
210                 wl->rx_mem_pool_addr.addr_extra =
211                         wl->rx_mem_pool_addr.addr + 4;
212
213                 /* Choose the block we want to read */
214                 wl1271_write(wl, WL1271_SLV_REG_DATA, &wl->rx_mem_pool_addr,
215                              sizeof(wl->rx_mem_pool_addr), false);
216
217                 wl1271_rx_handle_data(wl, buf_size);
218
219                 wl->rx_counter++;
220                 drv_rx_counter = wl->rx_counter & NUM_RX_PKT_DESC_MOD_MASK;
221                 wl1271_write32(wl, RX_DRIVER_COUNTER_ADDRESS, wl->rx_counter);
222         }
223 }