blob: dd2f8b714a7fe892599cc61d134579acf6fc50b7 [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>
Ido Yariv95dac04f2011-06-06 14:57:06 +030025#include <linux/sched.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026
Shahar Levi00d20102010-11-08 11:20:10 +000027#include "wl12xx.h"
28#include "acx.h"
29#include "reg.h"
30#include "rx.h"
Eliad Peller9eb599e2011-10-10 10:12:59 +020031#include "tx.h"
Shahar Levi00d20102010-11-08 11:20:10 +000032#include "io.h"
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030033
Eliad Peller4d56ad92011-08-14 13:17:05 +030034static u8 wl12xx_rx_get_mem_block(struct wl12xx_fw_status *status,
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030035 u32 drv_rx_counter)
36{
Luciano Coelhod0f63b22009-10-15 10:33:29 +030037 return le32_to_cpu(status->rx_pkt_descs[drv_rx_counter]) &
38 RX_MEM_BLOCK_MASK;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030039}
40
Eliad Peller4d56ad92011-08-14 13:17:05 +030041static u32 wl12xx_rx_get_buf_size(struct wl12xx_fw_status *status,
42 u32 drv_rx_counter)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030043{
Luciano Coelhod0f63b22009-10-15 10:33:29 +030044 return (le32_to_cpu(status->rx_pkt_descs[drv_rx_counter]) &
45 RX_BUF_SIZE_MASK) >> RX_BUF_SIZE_SHIFT_DIV;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030046}
47
Eliad Peller4d56ad92011-08-14 13:17:05 +030048static bool wl12xx_rx_get_unaligned(struct wl12xx_fw_status *status,
Shahar Levi0a1d3ab2011-07-14 11:50:27 +030049 u32 drv_rx_counter)
50{
51 /* Convert the value to bool */
52 return !!(le32_to_cpu(status->rx_pkt_descs[drv_rx_counter]) &
53 RX_BUF_UNALIGNED_PAYLOAD);
54}
55
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030056static void wl1271_rx_status(struct wl1271 *wl,
57 struct wl1271_rx_descriptor *desc,
58 struct ieee80211_rx_status *status,
59 u8 beacon)
60{
61 memset(status, 0, sizeof(struct ieee80211_rx_status));
62
Teemu Paasikivi6a2de932010-10-14 11:00:04 +020063 if ((desc->flags & WL1271_RX_DESC_BAND_MASK) == WL1271_RX_DESC_BAND_BG)
Juuso Oikarinen0af04672011-03-10 10:01:43 +020064 status->band = IEEE80211_BAND_2GHZ;
Teemu Paasikivi6a2de932010-10-14 11:00:04 +020065 else
Juuso Oikarinen0af04672011-03-10 10:01:43 +020066 status->band = IEEE80211_BAND_5GHZ;
Teemu Paasikivi6a2de932010-10-14 11:00:04 +020067
Juuso Oikarinen0af04672011-03-10 10:01:43 +020068 status->rate_idx = wl1271_rate_to_idx(desc->rate, status->band);
Teemu Paasikivia4102642009-10-13 12:47:51 +030069
Shahar Levi18357852010-10-13 16:09:41 +020070 /* 11n support */
71 if (desc->rate <= CONF_HW_RXTX_RATE_MCS0)
72 status->flag |= RX_FLAG_HT;
Shahar Levi18357852010-10-13 16:09:41 +020073
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030074 status->signal = desc->rssi;
75
John W. Linvilleece550d2010-07-28 16:41:06 -040076 /*
77 * FIXME: In wl1251, the SNR should be divided by two. In wl1271 we
78 * need to divide by two for now, but TI has been discussing about
79 * changing it. This needs to be rechecked.
80 */
81 wl->noise = desc->rssi - (desc->snr >> 1);
82
Juuso Oikarinen0af04672011-03-10 10:01:43 +020083 status->freq = ieee80211_channel_to_frequency(desc->channel,
84 status->band);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030085
86 if (desc->flags & WL1271_RX_DESC_ENCRYPT_MASK) {
Arik Nemtsov34c8e3d2011-04-26 23:35:40 +030087 u8 desc_err_code = desc->status & WL1271_RX_DESC_STATUS_MASK;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030088
Arik Nemtsov34c8e3d2011-04-26 23:35:40 +030089 status->flag |= RX_FLAG_IV_STRIPPED | RX_FLAG_MMIC_STRIPPED |
90 RX_FLAG_DECRYPTED;
91
92 if (unlikely(desc_err_code == WL1271_RX_DESC_MIC_FAIL)) {
Teemu Paasikivi5d07b662009-10-13 12:47:52 +030093 status->flag |= RX_FLAG_MMIC_ERROR;
Arik Nemtsov34c8e3d2011-04-26 23:35:40 +030094 wl1271_warning("Michael MIC error");
95 }
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030096 }
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030097}
98
Shahar Levi0a1d3ab2011-07-14 11:50:27 +030099static int wl1271_rx_handle_data(struct wl1271 *wl, u8 *data, u32 length,
Eliad Peller9eb599e2011-10-10 10:12:59 +0200100 bool unaligned, u8 *hlid)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300101{
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300102 struct wl1271_rx_descriptor *desc;
103 struct sk_buff *skb;
Eliad Peller92fe9b52011-02-09 12:25:14 +0200104 struct ieee80211_hdr *hdr;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300105 u8 *buf;
106 u8 beacon = 0;
Eliad Peller77ddaa12011-05-15 11:10:29 +0300107 u8 is_data = 0;
Shahar Levi0a1d3ab2011-07-14 11:50:27 +0300108 u8 reserved = unaligned ? NET_IP_ALIGN : 0;
Eliad Peller5c472142011-08-25 18:10:58 +0300109 u16 seq_num;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300110
Kalle Valo93c5bb62010-02-22 08:38:30 +0200111 /*
112 * In PLT mode we seem to get frames and mac80211 warns about them,
113 * workaround this by not retrieving them at all.
114 */
115 if (unlikely(wl->state == WL1271_STATE_PLT))
Ido Yariv1f37cbc2010-09-30 13:28:27 +0200116 return -EINVAL;
Kalle Valo93c5bb62010-02-22 08:38:30 +0200117
Arik Nemtsov34c8e3d2011-04-26 23:35:40 +0300118 /* the data read starts with the descriptor */
119 desc = (struct wl1271_rx_descriptor *) data;
120
Ido Yariv95dac04f2011-06-06 14:57:06 +0300121 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;
126 }
127
Arik Nemtsov34c8e3d2011-04-26 23:35:40 +0300128 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;
142 }
143
Shahar Levi0a1d3ab2011-07-14 11:50:27 +0300144 /* skb length not included rx descriptor */
145 skb = __dev_alloc_skb(length + reserved - sizeof(*desc), GFP_KERNEL);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300146 if (!skb) {
147 wl1271_error("Couldn't allocate RX frame");
Ido Yariv1f37cbc2010-09-30 13:28:27 +0200148 return -ENOMEM;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300149 }
150
Shahar Levi0a1d3ab2011-07-14 11:50:27 +0300151 /* reserve the unaligned payload(if any) */
152 skb_reserve(skb, reserved);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300153
Shahar Levi0a1d3ab2011-07-14 11:50:27 +0300154 buf = skb_put(skb, length - sizeof(*desc));
155
156 /*
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.
161 */
162 memcpy(buf, data + sizeof(*desc), length - sizeof(*desc));
Eliad Peller9eb599e2011-10-10 10:12:59 +0200163 *hlid = desc->hlid;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300164
Eliad Peller92fe9b52011-02-09 12:25:14 +0200165 hdr = (struct ieee80211_hdr *)skb->data;
166 if (ieee80211_is_beacon(hdr->frame_control))
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300167 beacon = 1;
Eliad Peller77ddaa12011-05-15 11:10:29 +0300168 if (ieee80211_is_data_present(hdr->frame_control))
169 is_data = 1;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300170
Eliad Peller58be4602010-09-19 18:55:08 +0200171 wl1271_rx_status(wl, desc, IEEE80211_SKB_RXCB(skb), beacon);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300172
Eliad Peller5c472142011-08-25 18:10:58 +0300173 seq_num = (le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_SEQ) >> 4;
Eliad Peller9eb599e2011-10-10 10:12:59 +0200174 wl1271_debug(DEBUG_RX, "rx skb 0x%p: %d B %s seq %d hlid %d", skb,
Eliad Pellera20a5b72011-04-05 18:21:31 +0300175 skb->len - desc->pad_len,
Eliad Peller5c472142011-08-25 18:10:58 +0300176 beacon ? "beacon" : "",
Eliad Peller9eb599e2011-10-10 10:12:59 +0200177 seq_num, *hlid);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300178
Juuso Oikarinenb9f2e392010-05-14 10:46:24 +0300179 skb_trim(skb, skb->len - desc->pad_len);
180
Ido Yariva6208652011-03-01 15:14:41 +0200181 skb_queue_tail(&wl->deferred_rx_queue, skb);
Eliad Peller92ef8962011-06-07 12:50:46 +0300182 queue_work(wl->freezable_wq, &wl->netstack_work);
Ido Yariv1f37cbc2010-09-30 13:28:27 +0200183
Eliad Peller77ddaa12011-05-15 11:10:29 +0300184 return is_data;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300185}
186
Eliad Peller4d56ad92011-08-14 13:17:05 +0300187void wl12xx_rx(struct wl1271 *wl, struct wl12xx_fw_status *status)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300188{
189 struct wl1271_acx_mem_map *wl_mem_map = wl->target_mem_map;
Eliad Peller9eb599e2011-10-10 10:12:59 +0200190 unsigned long active_hlids[BITS_TO_LONGS(WL12XX_MAX_LINKS)] = {0};
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300191 u32 buf_size;
192 u32 fw_rx_counter = status->fw_rx_counter & NUM_RX_PKT_DESC_MOD_MASK;
193 u32 drv_rx_counter = wl->rx_counter & NUM_RX_PKT_DESC_MOD_MASK;
Ido Yariv1f37cbc2010-09-30 13:28:27 +0200194 u32 rx_counter;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300195 u32 mem_block;
Ido Yariv1f37cbc2010-09-30 13:28:27 +0200196 u32 pkt_length;
197 u32 pkt_offset;
Eliad Peller9eb599e2011-10-10 10:12:59 +0200198 u8 hlid;
Shahar Levi0a1d3ab2011-07-14 11:50:27 +0300199 bool unaligned = false;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300200
201 while (drv_rx_counter != fw_rx_counter) {
Ido Yariv1f37cbc2010-09-30 13:28:27 +0200202 buf_size = 0;
203 rx_counter = drv_rx_counter;
204 while (rx_counter != fw_rx_counter) {
Eliad Peller4d56ad92011-08-14 13:17:05 +0300205 pkt_length = wl12xx_rx_get_buf_size(status, rx_counter);
Ido Yariv1f37cbc2010-09-30 13:28:27 +0200206 if (buf_size + pkt_length > WL1271_AGGR_BUFFER_SIZE)
207 break;
208 buf_size += pkt_length;
209 rx_counter++;
210 rx_counter &= NUM_RX_PKT_DESC_MOD_MASK;
211 }
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300212
213 if (buf_size == 0) {
214 wl1271_warning("received empty data");
215 break;
216 }
217
Shahar Leviae77ecc2011-03-06 16:32:13 +0200218 if (wl->chip.id != CHIP_ID_1283_PG20) {
219 /*
220 * Choose the block we want to read
221 * For aggregated packets, only the first memory block
222 * should be retrieved. The FW takes care of the rest.
223 */
Eliad Peller4d56ad92011-08-14 13:17:05 +0300224 mem_block = wl12xx_rx_get_mem_block(status,
Shahar Leviae77ecc2011-03-06 16:32:13 +0200225 drv_rx_counter);
226
227 wl->rx_mem_pool_addr.addr = (mem_block << 8) +
228 le32_to_cpu(wl_mem_map->packet_memory_pool_start);
229
230 wl->rx_mem_pool_addr.addr_extra =
231 wl->rx_mem_pool_addr.addr + 4;
232
233 wl1271_write(wl, WL1271_SLV_REG_DATA,
234 &wl->rx_mem_pool_addr,
235 sizeof(wl->rx_mem_pool_addr), false);
236 }
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300237
Ido Yariv1f37cbc2010-09-30 13:28:27 +0200238 /* Read all available packets at once */
239 wl1271_read(wl, WL1271_SLV_MEM_DATA, wl->aggr_buf,
240 buf_size, true);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300241
Ido Yariv1f37cbc2010-09-30 13:28:27 +0200242 /* Split data into separate packets */
243 pkt_offset = 0;
244 while (pkt_offset < buf_size) {
Eliad Peller4d56ad92011-08-14 13:17:05 +0300245 pkt_length = wl12xx_rx_get_buf_size(status,
Ido Yariv1f37cbc2010-09-30 13:28:27 +0200246 drv_rx_counter);
Shahar Levi0a1d3ab2011-07-14 11:50:27 +0300247
Eliad Peller4d56ad92011-08-14 13:17:05 +0300248 unaligned = wl12xx_rx_get_unaligned(status,
Shahar Levi0a1d3ab2011-07-14 11:50:27 +0300249 drv_rx_counter);
250
Juuso Oikarinenfb2382c2010-10-25 11:24:29 +0200251 /*
252 * the handle data call can only fail in memory-outage
253 * conditions, in that case the received frame will just
254 * be dropped.
255 */
Eliad Peller77ddaa12011-05-15 11:10:29 +0300256 if (wl1271_rx_handle_data(wl,
257 wl->aggr_buf + pkt_offset,
Eliad Peller9eb599e2011-10-10 10:12:59 +0200258 pkt_length, unaligned,
259 &hlid) == 1) {
260 WARN_ON(hlid >= WL12XX_MAX_LINKS);
261 __set_bit(hlid, active_hlids);
262 }
Eliad Peller77ddaa12011-05-15 11:10:29 +0300263
Ido Yariv1f37cbc2010-09-30 13:28:27 +0200264 wl->rx_counter++;
265 drv_rx_counter++;
266 drv_rx_counter &= NUM_RX_PKT_DESC_MOD_MASK;
267 pkt_offset += pkt_length;
268 }
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300269 }
Ido Yariv606ea9f2011-03-01 15:14:39 +0200270
271 /*
272 * Write the driver's packet counter to the FW. This is only required
273 * for older hardware revisions
274 */
275 if (wl->quirks & WL12XX_QUIRK_END_OF_TRANSACTION)
276 wl1271_write32(wl, RX_DRIVER_COUNTER_ADDRESS, wl->rx_counter);
Eliad Peller77ddaa12011-05-15 11:10:29 +0300277
Eliad Peller9eb599e2011-10-10 10:12:59 +0200278 wl12xx_rearm_rx_streaming(wl, active_hlids);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300279}