wl12xx: Support routing FW logs to the host
[pandora-kernel.git] / drivers / net / wireless / wl12xx / 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 <linux/gfp.h>
25 #include <linux/sched.h>
26
27 #include "wl12xx.h"
28 #include "acx.h"
29 #include "reg.h"
30 #include "rx.h"
31 #include "io.h"
32
33 static u8 wl1271_rx_get_mem_block(struct wl1271_fw_common_status *status,
34                                   u32 drv_rx_counter)
35 {
36         return le32_to_cpu(status->rx_pkt_descs[drv_rx_counter]) &
37                 RX_MEM_BLOCK_MASK;
38 }
39
40 static u32 wl1271_rx_get_buf_size(struct wl1271_fw_common_status *status,
41                                  u32 drv_rx_counter)
42 {
43         return (le32_to_cpu(status->rx_pkt_descs[drv_rx_counter]) &
44                 RX_BUF_SIZE_MASK) >> RX_BUF_SIZE_SHIFT_DIV;
45 }
46
47 static void wl1271_rx_status(struct wl1271 *wl,
48                              struct wl1271_rx_descriptor *desc,
49                              struct ieee80211_rx_status *status,
50                              u8 beacon)
51 {
52         memset(status, 0, sizeof(struct ieee80211_rx_status));
53
54         if ((desc->flags & WL1271_RX_DESC_BAND_MASK) == WL1271_RX_DESC_BAND_BG)
55                 status->band = IEEE80211_BAND_2GHZ;
56         else
57                 status->band = IEEE80211_BAND_5GHZ;
58
59         status->rate_idx = wl1271_rate_to_idx(desc->rate, status->band);
60
61 #ifdef CONFIG_WL12XX_HT
62         /* 11n support */
63         if (desc->rate <= CONF_HW_RXTX_RATE_MCS0)
64                 status->flag |= RX_FLAG_HT;
65 #endif
66
67         status->signal = desc->rssi;
68
69         /*
70          * FIXME: In wl1251, the SNR should be divided by two.  In wl1271 we
71          * need to divide by two for now, but TI has been discussing about
72          * changing it.  This needs to be rechecked.
73          */
74         wl->noise = desc->rssi - (desc->snr >> 1);
75
76         status->freq = ieee80211_channel_to_frequency(desc->channel,
77                                                       status->band);
78
79         if (desc->flags & WL1271_RX_DESC_ENCRYPT_MASK) {
80                 u8 desc_err_code = desc->status & WL1271_RX_DESC_STATUS_MASK;
81
82                 status->flag |= RX_FLAG_IV_STRIPPED | RX_FLAG_MMIC_STRIPPED |
83                                 RX_FLAG_DECRYPTED;
84
85                 if (unlikely(desc_err_code == WL1271_RX_DESC_MIC_FAIL)) {
86                         status->flag |= RX_FLAG_MMIC_ERROR;
87                         wl1271_warning("Michael MIC error");
88                 }
89         }
90 }
91
92 static int wl1271_rx_handle_data(struct wl1271 *wl, u8 *data, u32 length)
93 {
94         struct wl1271_rx_descriptor *desc;
95         struct sk_buff *skb;
96         struct ieee80211_hdr *hdr;
97         u8 *buf;
98         u8 beacon = 0;
99         u8 is_data = 0;
100
101         /*
102          * In PLT mode we seem to get frames and mac80211 warns about them,
103          * workaround this by not retrieving them at all.
104          */
105         if (unlikely(wl->state == WL1271_STATE_PLT))
106                 return -EINVAL;
107
108         /* the data read starts with the descriptor */
109         desc = (struct wl1271_rx_descriptor *) data;
110
111         if (desc->packet_class == WL12XX_RX_CLASS_LOGGER) {
112                 size_t len = length - sizeof(*desc);
113                 wl12xx_copy_fwlog(wl, data + sizeof(*desc), len);
114                 wake_up_interruptible(&wl->fwlog_waitq);
115                 return 0;
116         }
117
118         switch (desc->status & WL1271_RX_DESC_STATUS_MASK) {
119         /* discard corrupted packets */
120         case WL1271_RX_DESC_DRIVER_RX_Q_FAIL:
121         case WL1271_RX_DESC_DECRYPT_FAIL:
122                 wl1271_warning("corrupted packet in RX with status: 0x%x",
123                                desc->status & WL1271_RX_DESC_STATUS_MASK);
124                 return -EINVAL;
125         case WL1271_RX_DESC_SUCCESS:
126         case WL1271_RX_DESC_MIC_FAIL:
127                 break;
128         default:
129                 wl1271_error("invalid RX descriptor status: 0x%x",
130                              desc->status & WL1271_RX_DESC_STATUS_MASK);
131                 return -EINVAL;
132         }
133
134         skb = __dev_alloc_skb(length, GFP_KERNEL);
135         if (!skb) {
136                 wl1271_error("Couldn't allocate RX frame");
137                 return -ENOMEM;
138         }
139
140         buf = skb_put(skb, length);
141         memcpy(buf, data, length);
142
143         /* now we pull the descriptor out of the buffer */
144         skb_pull(skb, sizeof(*desc));
145
146         hdr = (struct ieee80211_hdr *)skb->data;
147         if (ieee80211_is_beacon(hdr->frame_control))
148                 beacon = 1;
149         if (ieee80211_is_data_present(hdr->frame_control))
150                 is_data = 1;
151
152         wl1271_rx_status(wl, desc, IEEE80211_SKB_RXCB(skb), beacon);
153
154         wl1271_debug(DEBUG_RX, "rx skb 0x%p: %d B %s", skb,
155                      skb->len - desc->pad_len,
156                      beacon ? "beacon" : "");
157
158         skb_trim(skb, skb->len - desc->pad_len);
159
160         skb_queue_tail(&wl->deferred_rx_queue, skb);
161         queue_work(wl->freezable_wq, &wl->netstack_work);
162
163         return is_data;
164 }
165
166 void wl1271_rx(struct wl1271 *wl, struct wl1271_fw_common_status *status)
167 {
168         struct wl1271_acx_mem_map *wl_mem_map = wl->target_mem_map;
169         u32 buf_size;
170         u32 fw_rx_counter  = status->fw_rx_counter & NUM_RX_PKT_DESC_MOD_MASK;
171         u32 drv_rx_counter = wl->rx_counter & NUM_RX_PKT_DESC_MOD_MASK;
172         u32 rx_counter;
173         u32 mem_block;
174         u32 pkt_length;
175         u32 pkt_offset;
176         bool is_ap = (wl->bss_type == BSS_TYPE_AP_BSS);
177         bool had_data = false;
178
179         while (drv_rx_counter != fw_rx_counter) {
180                 buf_size = 0;
181                 rx_counter = drv_rx_counter;
182                 while (rx_counter != fw_rx_counter) {
183                         pkt_length = wl1271_rx_get_buf_size(status, rx_counter);
184                         if (buf_size + pkt_length > WL1271_AGGR_BUFFER_SIZE)
185                                 break;
186                         buf_size += pkt_length;
187                         rx_counter++;
188                         rx_counter &= NUM_RX_PKT_DESC_MOD_MASK;
189                 }
190
191                 if (buf_size == 0) {
192                         wl1271_warning("received empty data");
193                         break;
194                 }
195
196                 if (wl->chip.id != CHIP_ID_1283_PG20) {
197                         /*
198                          * Choose the block we want to read
199                          * For aggregated packets, only the first memory block
200                          * should be retrieved. The FW takes care of the rest.
201                          */
202                         mem_block = wl1271_rx_get_mem_block(status,
203                                                             drv_rx_counter);
204
205                         wl->rx_mem_pool_addr.addr = (mem_block << 8) +
206                            le32_to_cpu(wl_mem_map->packet_memory_pool_start);
207
208                         wl->rx_mem_pool_addr.addr_extra =
209                                 wl->rx_mem_pool_addr.addr + 4;
210
211                         wl1271_write(wl, WL1271_SLV_REG_DATA,
212                                      &wl->rx_mem_pool_addr,
213                                      sizeof(wl->rx_mem_pool_addr), false);
214                 }
215
216                 /* Read all available packets at once */
217                 wl1271_read(wl, WL1271_SLV_MEM_DATA, wl->aggr_buf,
218                                 buf_size, true);
219
220                 /* Split data into separate packets */
221                 pkt_offset = 0;
222                 while (pkt_offset < buf_size) {
223                         pkt_length = wl1271_rx_get_buf_size(status,
224                                         drv_rx_counter);
225                         /*
226                          * the handle data call can only fail in memory-outage
227                          * conditions, in that case the received frame will just
228                          * be dropped.
229                          */
230                         if (wl1271_rx_handle_data(wl,
231                                                   wl->aggr_buf + pkt_offset,
232                                                   pkt_length) == 1)
233                                 had_data = true;
234
235                         wl->rx_counter++;
236                         drv_rx_counter++;
237                         drv_rx_counter &= NUM_RX_PKT_DESC_MOD_MASK;
238                         pkt_offset += pkt_length;
239                 }
240         }
241
242         /*
243          * Write the driver's packet counter to the FW. This is only required
244          * for older hardware revisions
245          */
246         if (wl->quirks & WL12XX_QUIRK_END_OF_TRANSACTION)
247                 wl1271_write32(wl, RX_DRIVER_COUNTER_ADDRESS, wl->rx_counter);
248
249         if (!is_ap && wl->conf.rx_streaming.interval && had_data &&
250             (wl->conf.rx_streaming.always ||
251              test_bit(WL1271_FLAG_SOFT_GEMINI, &wl->flags))) {
252                 u32 timeout = wl->conf.rx_streaming.duration;
253
254                 /* restart rx streaming */
255                 if (!test_bit(WL1271_FLAG_RX_STREAMING_STARTED, &wl->flags))
256                         ieee80211_queue_work(wl->hw,
257                                              &wl->rx_streaming_enable_work);
258
259                 mod_timer(&wl->rx_streaming_timer,
260                           jiffies + msecs_to_jiffies(timeout));
261         }
262 }
263
264 void wl1271_set_default_filters(struct wl1271 *wl)
265 {
266         if (wl->bss_type == BSS_TYPE_AP_BSS) {
267                 wl->rx_config = WL1271_DEFAULT_AP_RX_CONFIG;
268                 wl->rx_filter = WL1271_DEFAULT_AP_RX_FILTER;
269         } else {
270                 wl->rx_config = WL1271_DEFAULT_STA_RX_CONFIG;
271                 wl->rx_filter = WL1271_DEFAULT_STA_RX_FILTER;
272         }
273 }