blob: ac13f7d252198f2ee9542ac866260257ee83885a [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
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/gfp.h>
25
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030026#include "wl1271.h"
27#include "wl1271_acx.h"
28#include "wl1271_reg.h"
29#include "wl1271_rx.h"
Teemu Paasikivi7b048c52010-02-18 13:25:55 +020030#include "wl1271_io.h"
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030031
32static u8 wl1271_rx_get_mem_block(struct wl1271_fw_status *status,
33 u32 drv_rx_counter)
34{
Luciano Coelhod0f63b22009-10-15 10:33:29 +030035 return le32_to_cpu(status->rx_pkt_descs[drv_rx_counter]) &
36 RX_MEM_BLOCK_MASK;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030037}
38
39static u32 wl1271_rx_get_buf_size(struct wl1271_fw_status *status,
40 u32 drv_rx_counter)
41{
Luciano Coelhod0f63b22009-10-15 10:33:29 +030042 return (le32_to_cpu(status->rx_pkt_descs[drv_rx_counter]) &
43 RX_BUF_SIZE_MASK) >> RX_BUF_SIZE_SHIFT_DIV;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030044}
45
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030046static void wl1271_rx_status(struct wl1271 *wl,
47 struct wl1271_rx_descriptor *desc,
48 struct ieee80211_rx_status *status,
49 u8 beacon)
50{
51 memset(status, 0, sizeof(struct ieee80211_rx_status));
52
Juuso Oikarinenf876bb92010-03-26 12:53:11 +020053 status->band = wl->band;
54 status->rate_idx = wl1271_rate_to_idx(wl, desc->rate);
Teemu Paasikivia4102642009-10-13 12:47:51 +030055
Shahar Levi18357852010-10-13 16:09:41 +020056#ifdef CONFIG_WL1271_HT
57 /* 11n support */
58 if (desc->rate <= CONF_HW_RXTX_RATE_MCS0)
59 status->flag |= RX_FLAG_HT;
60#endif
61
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030062 status->signal = desc->rssi;
63
John W. Linvilleece550d2010-07-28 16:41:06 -040064 /*
65 * FIXME: In wl1251, the SNR should be divided by two. In wl1271 we
66 * need to divide by two for now, but TI has been discussing about
67 * changing it. This needs to be rechecked.
68 */
69 wl->noise = desc->rssi - (desc->snr >> 1);
70
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030071 status->freq = ieee80211_channel_to_frequency(desc->channel);
72
73 if (desc->flags & WL1271_RX_DESC_ENCRYPT_MASK) {
74 status->flag |= RX_FLAG_IV_STRIPPED | RX_FLAG_MMIC_STRIPPED;
75
Teemu Paasikivi5d07b662009-10-13 12:47:52 +030076 if (likely(!(desc->status & WL1271_RX_DESC_DECRYPT_FAIL)))
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030077 status->flag |= RX_FLAG_DECRYPTED;
Teemu Paasikivi5d07b662009-10-13 12:47:52 +030078 if (unlikely(desc->status & WL1271_RX_DESC_MIC_FAIL))
79 status->flag |= RX_FLAG_MMIC_ERROR;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030080 }
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030081}
82
Ido Yariv1f37cbc2010-09-30 13:28:27 +020083static int wl1271_rx_handle_data(struct wl1271 *wl, u8 *data, u32 length)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030084{
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030085 struct wl1271_rx_descriptor *desc;
86 struct sk_buff *skb;
87 u16 *fc;
88 u8 *buf;
89 u8 beacon = 0;
90
Kalle Valo93c5bb62010-02-22 08:38:30 +020091 /*
92 * In PLT mode we seem to get frames and mac80211 warns about them,
93 * workaround this by not retrieving them at all.
94 */
95 if (unlikely(wl->state == WL1271_STATE_PLT))
Ido Yariv1f37cbc2010-09-30 13:28:27 +020096 return -EINVAL;
Kalle Valo93c5bb62010-02-22 08:38:30 +020097
Luis R. Rodrigueze9a62692009-11-02 12:15:15 -050098 skb = __dev_alloc_skb(length, GFP_KERNEL);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030099 if (!skb) {
100 wl1271_error("Couldn't allocate RX frame");
Ido Yariv1f37cbc2010-09-30 13:28:27 +0200101 return -ENOMEM;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300102 }
103
104 buf = skb_put(skb, length);
Ido Yariv1f37cbc2010-09-30 13:28:27 +0200105 memcpy(buf, data, length);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300106
107 /* the data read starts with the descriptor */
108 desc = (struct wl1271_rx_descriptor *) buf;
109
110 /* now we pull the descriptor out of the buffer */
111 skb_pull(skb, sizeof(*desc));
112
113 fc = (u16 *)skb->data;
114 if ((*fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_BEACON)
115 beacon = 1;
116
Eliad Peller58be4602010-09-19 18:55:08 +0200117 wl1271_rx_status(wl, desc, IEEE80211_SKB_RXCB(skb), beacon);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300118
119 wl1271_debug(DEBUG_RX, "rx skb 0x%p: %d B %s", skb, skb->len,
120 beacon ? "beacon" : "");
121
Juuso Oikarinenb9f2e392010-05-14 10:46:24 +0300122 skb_trim(skb, skb->len - desc->pad_len);
123
Luciano Coelho287d3742009-10-21 14:03:46 +0300124 ieee80211_rx_ni(wl->hw, skb);
Ido Yariv1f37cbc2010-09-30 13:28:27 +0200125
126 return 0;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300127}
128
129void wl1271_rx(struct wl1271 *wl, struct wl1271_fw_status *status)
130{
131 struct wl1271_acx_mem_map *wl_mem_map = wl->target_mem_map;
132 u32 buf_size;
133 u32 fw_rx_counter = status->fw_rx_counter & NUM_RX_PKT_DESC_MOD_MASK;
134 u32 drv_rx_counter = wl->rx_counter & NUM_RX_PKT_DESC_MOD_MASK;
Ido Yariv1f37cbc2010-09-30 13:28:27 +0200135 u32 rx_counter;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300136 u32 mem_block;
Ido Yariv1f37cbc2010-09-30 13:28:27 +0200137 u32 pkt_length;
138 u32 pkt_offset;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300139
140 while (drv_rx_counter != fw_rx_counter) {
Ido Yariv1f37cbc2010-09-30 13:28:27 +0200141 buf_size = 0;
142 rx_counter = drv_rx_counter;
143 while (rx_counter != fw_rx_counter) {
144 pkt_length = wl1271_rx_get_buf_size(status, rx_counter);
145 if (buf_size + pkt_length > WL1271_AGGR_BUFFER_SIZE)
146 break;
147 buf_size += pkt_length;
148 rx_counter++;
149 rx_counter &= NUM_RX_PKT_DESC_MOD_MASK;
150 }
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300151
152 if (buf_size == 0) {
153 wl1271_warning("received empty data");
154 break;
155 }
156
Ido Yariv1f37cbc2010-09-30 13:28:27 +0200157 /*
158 * Choose the block we want to read
159 * For aggregated packets, only the first memory block should
160 * be retrieved. The FW takes care of the rest.
161 */
162 mem_block = wl1271_rx_get_mem_block(status, drv_rx_counter);
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300163 wl->rx_mem_pool_addr.addr = (mem_block << 8) +
164 le32_to_cpu(wl_mem_map->packet_memory_pool_start);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300165 wl->rx_mem_pool_addr.addr_extra =
166 wl->rx_mem_pool_addr.addr + 4;
Teemu Paasikivi7b048c52010-02-18 13:25:55 +0200167 wl1271_write(wl, WL1271_SLV_REG_DATA, &wl->rx_mem_pool_addr,
Ido Yariv1f37cbc2010-09-30 13:28:27 +0200168 sizeof(wl->rx_mem_pool_addr), false);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300169
Ido Yariv1f37cbc2010-09-30 13:28:27 +0200170 /* Read all available packets at once */
171 wl1271_read(wl, WL1271_SLV_MEM_DATA, wl->aggr_buf,
172 buf_size, true);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300173
Ido Yariv1f37cbc2010-09-30 13:28:27 +0200174 /* Split data into separate packets */
175 pkt_offset = 0;
176 while (pkt_offset < buf_size) {
177 pkt_length = wl1271_rx_get_buf_size(status,
178 drv_rx_counter);
179 if (wl1271_rx_handle_data(wl,
180 wl->aggr_buf + pkt_offset,
181 pkt_length) < 0)
182 break;
183 wl->rx_counter++;
184 drv_rx_counter++;
185 drv_rx_counter &= NUM_RX_PKT_DESC_MOD_MASK;
186 pkt_offset += pkt_length;
187 }
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300188 }
Ido Yariv1f37cbc2010-09-30 13:28:27 +0200189 wl1271_write32(wl, RX_DRIVER_COUNTER_ADDRESS,
190 cpu_to_le32(wl->rx_counter));
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300191}