blob: 3507c81c750053f22af724ed8565217619a1437d [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 <linux/kernel.h>
25#include <linux/module.h>
Arik Nemtsovc6c8a652010-10-16 20:27:53 +020026#include <linux/etherdevice.h>
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030027
Shahar Levi00d20102010-11-08 11:20:10 +000028#include "wl12xx.h"
29#include "io.h"
30#include "reg.h"
31#include "ps.h"
32#include "tx.h"
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030033
Arik Nemtsov7f179b42010-10-16 21:39:06 +020034static int wl1271_set_default_wep_key(struct wl1271 *wl, u8 id)
35{
36 int ret;
37 bool is_ap = (wl->bss_type == BSS_TYPE_AP_BSS);
38
39 if (is_ap)
40 ret = wl1271_cmd_set_ap_default_wep_key(wl, id);
41 else
42 ret = wl1271_cmd_set_sta_default_wep_key(wl, id);
43
44 if (ret < 0)
45 return ret;
46
47 wl1271_debug(DEBUG_CRYPT, "default wep key idx: %d", (int)id);
48 return 0;
49}
50
Ido Yariv25eeb9e2010-10-12 16:20:06 +020051static int wl1271_alloc_tx_id(struct wl1271 *wl, struct sk_buff *skb)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030052{
Ido Yariv25eeb9e2010-10-12 16:20:06 +020053 int id;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030054
Ido Yariv25eeb9e2010-10-12 16:20:06 +020055 id = find_first_zero_bit(wl->tx_frames_map, ACX_TX_DESCRIPTORS);
56 if (id >= ACX_TX_DESCRIPTORS)
57 return -EBUSY;
58
59 __set_bit(id, wl->tx_frames_map);
60 wl->tx_frames[id] = skb;
61 wl->tx_frames_cnt++;
62 return id;
63}
64
65static void wl1271_free_tx_id(struct wl1271 *wl, int id)
66{
67 if (__test_and_clear_bit(id, wl->tx_frames_map)) {
68 wl->tx_frames[id] = NULL;
69 wl->tx_frames_cnt--;
70 }
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030071}
72
Ido Yariva19606b2010-09-30 13:28:28 +020073static int wl1271_tx_allocate(struct wl1271 *wl, struct sk_buff *skb, u32 extra,
74 u32 buf_offset)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030075{
76 struct wl1271_tx_hw_descr *desc;
77 u32 total_len = skb->len + sizeof(struct wl1271_tx_hw_descr) + extra;
Juuso Oikarinen5c9417f2010-02-22 08:38:39 +020078 u32 total_blocks;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030079 int id, ret = -EBUSY;
80
Ido Yariva19606b2010-09-30 13:28:28 +020081 if (buf_offset + total_len > WL1271_AGGR_BUFFER_SIZE)
Ido Yariv6c6e6692010-10-12 14:49:09 +020082 return -EAGAIN;
Ido Yariva19606b2010-09-30 13:28:28 +020083
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030084 /* allocate free identifier for the packet */
Ido Yariv25eeb9e2010-10-12 16:20:06 +020085 id = wl1271_alloc_tx_id(wl, skb);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030086 if (id < 0)
87 return id;
88
89 /* approximate the number of blocks required for this packet
90 in the firmware */
Juuso Oikarinen5c9417f2010-02-22 08:38:39 +020091 total_blocks = total_len + TX_HW_BLOCK_SIZE - 1;
92 total_blocks = total_blocks / TX_HW_BLOCK_SIZE + TX_HW_BLOCK_SPARE;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030093 if (total_blocks <= wl->tx_blocks_available) {
94 desc = (struct wl1271_tx_hw_descr *)skb_push(
95 skb, total_len - skb->len);
96
97 desc->extra_mem_blocks = TX_HW_BLOCK_SPARE;
98 desc->total_mem_blocks = total_blocks;
99 desc->id = id;
100
101 wl->tx_blocks_available -= total_blocks;
102
103 ret = 0;
104
105 wl1271_debug(DEBUG_TX,
106 "tx_allocate: size: %d, blocks: %d, id: %d",
107 total_len, total_blocks, id);
Juuso Oikarinen781608c2010-05-24 11:18:17 +0300108 } else {
Ido Yariv25eeb9e2010-10-12 16:20:06 +0200109 wl1271_free_tx_id(wl, id);
Juuso Oikarinen781608c2010-05-24 11:18:17 +0300110 }
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300111
112 return ret;
113}
114
Ido Yariva19606b2010-09-30 13:28:28 +0200115static void wl1271_tx_fill_hdr(struct wl1271 *wl, struct sk_buff *skb,
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300116 u32 extra, struct ieee80211_tx_info *control)
117{
Juuso Oikarinenac5e1e32010-02-22 08:38:38 +0200118 struct timespec ts;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300119 struct wl1271_tx_hw_descr *desc;
Arik Nemtsovc6c8a652010-10-16 20:27:53 +0200120 int pad, ac, rate_idx;
Juuso Oikarinenac5e1e32010-02-22 08:38:38 +0200121 s64 hosttime;
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300122 u16 tx_attr;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300123
124 desc = (struct wl1271_tx_hw_descr *) skb->data;
125
Juuso Oikarinen1e2b7972009-10-08 21:56:20 +0300126 /* relocate space for security header */
127 if (extra) {
128 void *framestart = skb->data + sizeof(*desc);
129 u16 fc = *(u16 *)(framestart + extra);
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300130 int hdrlen = ieee80211_hdrlen(cpu_to_le16(fc));
Juuso Oikarinen1e2b7972009-10-08 21:56:20 +0300131 memmove(framestart, framestart + extra, hdrlen);
132 }
133
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300134 /* configure packet life time */
Juuso Oikarinenac5e1e32010-02-22 08:38:38 +0200135 getnstimeofday(&ts);
136 hosttime = (timespec_to_ns(&ts) >> 10);
137 desc->start_time = cpu_to_le32(hosttime - wl->time_offset);
Arik Nemtsovc6c8a652010-10-16 20:27:53 +0200138
139 if (wl->bss_type != BSS_TYPE_AP_BSS)
140 desc->life_time = cpu_to_le16(TX_HW_MGMT_PKT_LIFETIME_TU);
141 else
142 desc->life_time = cpu_to_le16(TX_HW_AP_MODE_PKT_LIFETIME_TU);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300143
144 /* configure the tx attributes */
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300145 tx_attr = wl->session_counter << TX_HW_ATTR_OFST_SESSION_COUNTER;
Kalle Valoc6999d82010-02-18 13:25:41 +0200146
Juuso Oikarinened484a12010-09-01 11:31:11 +0200147 /* queue (we use same identifiers for tid's and ac's */
Kalle Valoc6999d82010-02-18 13:25:41 +0200148 ac = wl1271_tx_get_queue(skb_get_queue_mapping(skb));
Juuso Oikarinened484a12010-09-01 11:31:11 +0200149 desc->tid = ac;
Arik Nemtsovc6c8a652010-10-16 20:27:53 +0200150
151 if (wl->bss_type != BSS_TYPE_AP_BSS) {
152 desc->aid = TX_HW_DEFAULT_AID;
153
154 /* if the packets are destined for AP (have a STA entry)
155 send them with AP rate policies, otherwise use default
156 basic rates */
157 if (control->control.sta)
158 rate_idx = ACX_TX_AP_FULL_RATE;
159 else
160 rate_idx = ACX_TX_BASIC_RATE;
161 } else {
162 if (control->control.sta) {
163 struct wl1271_station *wl_sta;
164
165 wl_sta = (struct wl1271_station *)
166 control->control.sta->drv_priv;
167 desc->hlid = wl_sta->hlid;
168 rate_idx = ac;
169 } else {
170 struct ieee80211_hdr *hdr;
171
172 hdr = (struct ieee80211_hdr *)
173 (skb->data + sizeof(*desc));
174 if (ieee80211_is_mgmt(hdr->frame_control)) {
175 desc->hlid = WL1271_AP_GLOBAL_HLID;
176 rate_idx = ACX_TX_AP_MODE_MGMT_RATE;
177 } else {
178 desc->hlid = WL1271_AP_BROADCAST_HLID;
179 rate_idx = ACX_TX_AP_MODE_BCST_RATE;
180 }
181 }
182 }
183
184 tx_attr |= rate_idx << TX_HW_ATTR_OFST_RATE_POLICY;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300185 desc->reserved = 0;
186
187 /* align the length (and store in terms of words) */
188 pad = WL1271_TX_ALIGN(skb->len);
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300189 desc->length = cpu_to_le16(pad >> 2);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300190
191 /* calculate number of padding bytes */
192 pad = pad - skb->len;
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300193 tx_attr |= pad << TX_HW_ATTR_OFST_LAST_WORD_PAD;
194
195 desc->tx_attr = cpu_to_le16(tx_attr);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300196
Arik Nemtsovc6c8a652010-10-16 20:27:53 +0200197 wl1271_debug(DEBUG_TX, "tx_fill_hdr: pad: %d hlid: %d "
Eliad Peller1d4801f2011-01-16 10:07:10 +0100198 "tx_attr: 0x%x len: %d life: %d mem: %d", pad, desc->hlid,
199 le16_to_cpu(desc->tx_attr), le16_to_cpu(desc->length),
200 le16_to_cpu(desc->life_time), desc->total_mem_blocks);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300201}
202
203/* caller must hold wl->mutex */
Ido Yariva19606b2010-09-30 13:28:28 +0200204static int wl1271_prepare_tx_frame(struct wl1271 *wl, struct sk_buff *skb,
205 u32 buf_offset)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300206{
207 struct ieee80211_tx_info *info;
208 u32 extra = 0;
209 int ret = 0;
Ido Yariva19606b2010-09-30 13:28:28 +0200210 u32 total_len;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300211
212 if (!skb)
213 return -EINVAL;
214
215 info = IEEE80211_SKB_CB(skb);
216
217 if (info->control.hw_key &&
Johannes Berg97359d12010-08-10 09:46:38 +0200218 info->control.hw_key->cipher == WLAN_CIPHER_SUITE_TKIP)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300219 extra = WL1271_TKIP_IV_SPACE;
220
221 if (info->control.hw_key) {
Arik Nemtsov7f179b42010-10-16 21:39:06 +0200222 bool is_wep;
223 u8 idx = info->control.hw_key->hw_key_idx;
224 u32 cipher = info->control.hw_key->cipher;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300225
Arik Nemtsov7f179b42010-10-16 21:39:06 +0200226 is_wep = (cipher == WLAN_CIPHER_SUITE_WEP40) ||
227 (cipher == WLAN_CIPHER_SUITE_WEP104);
228
229 if (unlikely(is_wep && wl->default_key != idx)) {
230 ret = wl1271_set_default_wep_key(wl, idx);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300231 if (ret < 0)
232 return ret;
Juuso Oikarinenee444cf2010-02-18 13:25:50 +0200233 wl->default_key = idx;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300234 }
235 }
236
Ido Yariva19606b2010-09-30 13:28:28 +0200237 ret = wl1271_tx_allocate(wl, skb, extra, buf_offset);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300238 if (ret < 0)
239 return ret;
240
Ido Yariva19606b2010-09-30 13:28:28 +0200241 wl1271_tx_fill_hdr(wl, skb, extra, info);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300242
Ido Yariva19606b2010-09-30 13:28:28 +0200243 /*
244 * The length of each packet is stored in terms of words. Thus, we must
245 * pad the skb data to make sure its length is aligned.
246 * The number of padding bytes is computed and set in wl1271_tx_fill_hdr
247 */
248 total_len = WL1271_TX_ALIGN(skb->len);
249 memcpy(wl->aggr_buf + buf_offset, skb->data, skb->len);
250 memset(wl->aggr_buf + buf_offset + skb->len, 0, total_len - skb->len);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300251
Ido Yariva19606b2010-09-30 13:28:28 +0200252 return total_len;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300253}
254
Juuso Oikarinenebba60c2010-04-01 11:38:20 +0300255u32 wl1271_tx_enabled_rates_get(struct wl1271 *wl, u32 rate_set)
Juuso Oikarinen830fb672009-12-11 15:41:06 +0200256{
257 struct ieee80211_supported_band *band;
258 u32 enabled_rates = 0;
259 int bit;
260
261 band = wl->hw->wiphy->bands[wl->band];
262 for (bit = 0; bit < band->n_bitrates; bit++) {
263 if (rate_set & 0x1)
264 enabled_rates |= band->bitrates[bit].hw_value;
265 rate_set >>= 1;
266 }
267
Shahar Levi00d20102010-11-08 11:20:10 +0000268#ifdef CONFIG_WL12XX_HT
Shahar Levi18357852010-10-13 16:09:41 +0200269 /* MCS rates indication are on bits 16 - 23 */
270 rate_set >>= HW_HT_RATES_OFFSET - band->n_bitrates;
271
272 for (bit = 0; bit < 8; bit++) {
273 if (rate_set & 0x1)
274 enabled_rates |= (CONF_HW_BIT_RATE_MCS_0 << bit);
275 rate_set >>= 1;
276 }
277#endif
278
Juuso Oikarinen830fb672009-12-11 15:41:06 +0200279 return enabled_rates;
280}
281
Ido Yariv2fe33e82010-10-12 14:49:12 +0200282static void handle_tx_low_watermark(struct wl1271 *wl)
283{
284 unsigned long flags;
285
286 if (test_bit(WL1271_FLAG_TX_QUEUE_STOPPED, &wl->flags) &&
Juuso Oikarinen6742f552010-12-13 09:52:37 +0200287 wl->tx_queue_count <= WL1271_TX_QUEUE_LOW_WATERMARK) {
Ido Yariv2fe33e82010-10-12 14:49:12 +0200288 /* firmware buffer has space, restart queues */
289 spin_lock_irqsave(&wl->wl_lock, flags);
290 ieee80211_wake_queues(wl->hw);
291 clear_bit(WL1271_FLAG_TX_QUEUE_STOPPED, &wl->flags);
292 spin_unlock_irqrestore(&wl->wl_lock, flags);
293 }
294}
295
Juuso Oikarinen6742f552010-12-13 09:52:37 +0200296static struct sk_buff *wl1271_skb_dequeue(struct wl1271 *wl)
297{
298 struct sk_buff *skb = NULL;
299 unsigned long flags;
300
301 skb = skb_dequeue(&wl->tx_queue[CONF_TX_AC_VO]);
302 if (skb)
303 goto out;
304 skb = skb_dequeue(&wl->tx_queue[CONF_TX_AC_VI]);
305 if (skb)
306 goto out;
307 skb = skb_dequeue(&wl->tx_queue[CONF_TX_AC_BE]);
308 if (skb)
309 goto out;
310 skb = skb_dequeue(&wl->tx_queue[CONF_TX_AC_BK]);
311
312out:
313 if (skb) {
314 spin_lock_irqsave(&wl->wl_lock, flags);
315 wl->tx_queue_count--;
316 spin_unlock_irqrestore(&wl->wl_lock, flags);
317 }
318
319 return skb;
320}
321
322static void wl1271_skb_queue_head(struct wl1271 *wl, struct sk_buff *skb)
323{
324 unsigned long flags;
325 int q = wl1271_tx_get_queue(skb_get_queue_mapping(skb));
326
327 skb_queue_head(&wl->tx_queue[q], skb);
328 spin_lock_irqsave(&wl->wl_lock, flags);
329 wl->tx_queue_count++;
330 spin_unlock_irqrestore(&wl->wl_lock, flags);
331}
332
Ido Yariva5225502010-10-12 14:49:10 +0200333void wl1271_tx_work_locked(struct wl1271 *wl)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300334{
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300335 struct sk_buff *skb;
336 bool woken_up = false;
Juuso Oikarinen830fb672009-12-11 15:41:06 +0200337 u32 sta_rates = 0;
Ido Yariv6c6e6692010-10-12 14:49:09 +0200338 u32 buf_offset = 0;
339 bool sent_packets = false;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300340 int ret;
341
Juuso Oikarinen830fb672009-12-11 15:41:06 +0200342 /* check if the rates supported by the AP have changed */
343 if (unlikely(test_and_clear_bit(WL1271_FLAG_STA_RATES_CHANGED,
344 &wl->flags))) {
345 unsigned long flags;
Ido Yariv2fe33e82010-10-12 14:49:12 +0200346
Juuso Oikarinen830fb672009-12-11 15:41:06 +0200347 spin_lock_irqsave(&wl->wl_lock, flags);
348 sta_rates = wl->sta_rate_set;
349 spin_unlock_irqrestore(&wl->wl_lock, flags);
350 }
351
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300352 if (unlikely(wl->state == WL1271_STATE_OFF))
353 goto out;
354
Juuso Oikarinen830fb672009-12-11 15:41:06 +0200355 /* if rates have changed, re-configure the rate policy */
356 if (unlikely(sta_rates)) {
Luciano Coelho54046432010-10-20 15:15:52 +0300357 ret = wl1271_ps_elp_wakeup(wl, false);
358 if (ret < 0)
359 goto out;
360 woken_up = true;
361
Juuso Oikarinen830fb672009-12-11 15:41:06 +0200362 wl->rate_set = wl1271_tx_enabled_rates_get(wl, sta_rates);
Arik Nemtsov79b223f2010-10-16 17:52:59 +0200363 wl1271_acx_sta_rate_policies(wl);
Juuso Oikarinen830fb672009-12-11 15:41:06 +0200364 }
365
Juuso Oikarinen6742f552010-12-13 09:52:37 +0200366 while ((skb = wl1271_skb_dequeue(wl))) {
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300367 if (!woken_up) {
368 ret = wl1271_ps_elp_wakeup(wl, false);
369 if (ret < 0)
Juuso Oikarinenffb591c2010-02-22 08:38:31 +0200370 goto out_ack;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300371 woken_up = true;
372 }
373
Ido Yariva19606b2010-09-30 13:28:28 +0200374 ret = wl1271_prepare_tx_frame(wl, skb, buf_offset);
Ido Yariv6c6e6692010-10-12 14:49:09 +0200375 if (ret == -EAGAIN) {
Ido Yariva19606b2010-09-30 13:28:28 +0200376 /*
Ido Yariv6c6e6692010-10-12 14:49:09 +0200377 * Aggregation buffer is full.
378 * Flush buffer and try again.
379 */
Juuso Oikarinen6742f552010-12-13 09:52:37 +0200380 wl1271_skb_queue_head(wl, skb);
Ido Yariv6c6e6692010-10-12 14:49:09 +0200381 wl1271_write(wl, WL1271_SLV_MEM_DATA, wl->aggr_buf,
Juuso Oikarinen6742f552010-12-13 09:52:37 +0200382 buf_offset, true);
Ido Yariv6c6e6692010-10-12 14:49:09 +0200383 sent_packets = true;
384 buf_offset = 0;
385 continue;
386 } else if (ret == -EBUSY) {
387 /*
388 * Firmware buffer is full.
Ido Yariva19606b2010-09-30 13:28:28 +0200389 * Queue back last skb, and stop aggregating.
390 */
Juuso Oikarinen6742f552010-12-13 09:52:37 +0200391 wl1271_skb_queue_head(wl, skb);
Ido Yariva5225502010-10-12 14:49:10 +0200392 /* No work left, avoid scheduling redundant tx work */
393 set_bit(WL1271_FLAG_FW_TX_BUSY, &wl->flags);
Juuso Oikarinenffb591c2010-02-22 08:38:31 +0200394 goto out_ack;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300395 } else if (ret < 0) {
396 dev_kfree_skb(skb);
Juuso Oikarinenffb591c2010-02-22 08:38:31 +0200397 goto out_ack;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300398 }
Ido Yariva19606b2010-09-30 13:28:28 +0200399 buf_offset += ret;
400 wl->tx_packets_count++;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300401 }
402
Juuso Oikarinenffb591c2010-02-22 08:38:31 +0200403out_ack:
Ido Yariva19606b2010-09-30 13:28:28 +0200404 if (buf_offset) {
405 wl1271_write(wl, WL1271_SLV_MEM_DATA, wl->aggr_buf,
406 buf_offset, true);
Ido Yariv6c6e6692010-10-12 14:49:09 +0200407 sent_packets = true;
408 }
409 if (sent_packets) {
Ido Yariva19606b2010-09-30 13:28:28 +0200410 /* interrupt the firmware with the new packets */
Juuso Oikarinenffb591c2010-02-22 08:38:31 +0200411 wl1271_write32(wl, WL1271_HOST_WR_ACCESS, wl->tx_packets_count);
Ido Yariv2fe33e82010-10-12 14:49:12 +0200412 handle_tx_low_watermark(wl);
Ido Yariva19606b2010-09-30 13:28:28 +0200413 }
Juuso Oikarinenffb591c2010-02-22 08:38:31 +0200414
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300415out:
416 if (woken_up)
417 wl1271_ps_elp_sleep(wl);
Ido Yariva5225502010-10-12 14:49:10 +0200418}
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300419
Ido Yariva5225502010-10-12 14:49:10 +0200420void wl1271_tx_work(struct work_struct *work)
421{
422 struct wl1271 *wl = container_of(work, struct wl1271, tx_work);
423
424 mutex_lock(&wl->mutex);
425 wl1271_tx_work_locked(wl);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300426 mutex_unlock(&wl->mutex);
427}
428
429static void wl1271_tx_complete_packet(struct wl1271 *wl,
430 struct wl1271_tx_hw_res_descr *result)
431{
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300432 struct ieee80211_tx_info *info;
433 struct sk_buff *skb;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300434 int id = result->id;
Juuso Oikarinen31627dc2010-03-26 12:53:12 +0200435 int rate = -1;
436 u8 retries = 0;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300437
438 /* check for id legality */
Juuso Oikarinenffb591c2010-02-22 08:38:31 +0200439 if (unlikely(id >= ACX_TX_DESCRIPTORS || wl->tx_frames[id] == NULL)) {
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300440 wl1271_warning("TX result illegal id: %d", id);
441 return;
442 }
443
444 skb = wl->tx_frames[id];
445 info = IEEE80211_SKB_CB(skb);
446
Juuso Oikarinen31627dc2010-03-26 12:53:12 +0200447 /* update the TX status info */
448 if (result->status == TX_SUCCESS) {
449 if (!(info->flags & IEEE80211_TX_CTL_NO_ACK))
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300450 info->flags |= IEEE80211_TX_STAT_ACK;
Teemu Paasikivi6a2de932010-10-14 11:00:04 +0200451 rate = wl1271_rate_to_idx(result->rate_class_index, wl->band);
Juuso Oikarinen31627dc2010-03-26 12:53:12 +0200452 retries = result->ack_failures;
453 } else if (result->status == TX_RETRY_EXCEEDED) {
454 wl->stats.excessive_retries++;
455 retries = result->ack_failures;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300456 }
457
Juuso Oikarinen31627dc2010-03-26 12:53:12 +0200458 info->status.rates[0].idx = rate;
459 info->status.rates[0].count = retries;
460 info->status.rates[0].flags = 0;
461 info->status.ack_signal = -1;
462
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300463 wl->stats.retry_count += result->ack_failures;
464
Juuso Oikarinenac4e4ce2009-10-08 21:56:19 +0300465 /* update security sequence number */
Juuso Oikarinen04e36fc2010-02-22 08:38:40 +0200466 wl->tx_security_seq += (result->lsb_security_sequence_number -
467 wl->tx_security_last_seq);
Juuso Oikarinenac4e4ce2009-10-08 21:56:19 +0300468 wl->tx_security_last_seq = result->lsb_security_sequence_number;
469
Juuso Oikarinen1e2b7972009-10-08 21:56:20 +0300470 /* remove private header from packet */
471 skb_pull(skb, sizeof(struct wl1271_tx_hw_descr));
472
473 /* remove TKIP header space if present */
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300474 if (info->control.hw_key &&
Johannes Berg97359d12010-08-10 09:46:38 +0200475 info->control.hw_key->cipher == WLAN_CIPHER_SUITE_TKIP) {
Juuso Oikarinen1e2b7972009-10-08 21:56:20 +0300476 int hdrlen = ieee80211_get_hdrlen_from_skb(skb);
477 memmove(skb->data + WL1271_TKIP_IV_SPACE, skb->data, hdrlen);
478 skb_pull(skb, WL1271_TKIP_IV_SPACE);
479 }
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300480
481 wl1271_debug(DEBUG_TX, "tx status id %u skb 0x%p failures %u rate 0x%x"
482 " status 0x%x",
483 result->id, skb, result->ack_failures,
484 result->rate_class_index, result->status);
485
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300486 /* return the packet to the stack */
487 ieee80211_tx_status(wl->hw, skb);
Ido Yariv25eeb9e2010-10-12 16:20:06 +0200488 wl1271_free_tx_id(wl, result->id);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300489}
490
491/* Called upon reception of a TX complete interrupt */
Juuso Oikarinenffb591c2010-02-22 08:38:31 +0200492void wl1271_tx_complete(struct wl1271 *wl)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300493{
494 struct wl1271_acx_mem_map *memmap =
495 (struct wl1271_acx_mem_map *)wl->target_mem_map;
Juuso Oikarinenffb591c2010-02-22 08:38:31 +0200496 u32 count, fw_counter;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300497 u32 i;
498
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300499 /* read the tx results from the chipset */
Teemu Paasikivi7b048c52010-02-18 13:25:55 +0200500 wl1271_read(wl, le32_to_cpu(memmap->tx_result),
501 wl->tx_res_if, sizeof(*wl->tx_res_if), false);
Juuso Oikarinenffb591c2010-02-22 08:38:31 +0200502 fw_counter = le32_to_cpu(wl->tx_res_if->tx_result_fw_counter);
503
504 /* write host counter to chipset (to ack) */
505 wl1271_write32(wl, le32_to_cpu(memmap->tx_result) +
506 offsetof(struct wl1271_tx_hw_res_if,
507 tx_result_host_counter), fw_counter);
508
509 count = fw_counter - wl->tx_results_count;
Juuso Oikarinen06f7bc72010-02-22 08:38:33 +0200510 wl1271_debug(DEBUG_TX, "tx_complete received, packets: %d", count);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300511
512 /* verify that the result buffer is not getting overrun */
Juuso Oikarinenffb591c2010-02-22 08:38:31 +0200513 if (unlikely(count > TX_HW_RESULT_QUEUE_LEN))
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300514 wl1271_warning("TX result overflow from chipset: %d", count);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300515
516 /* process the results */
517 for (i = 0; i < count; i++) {
518 struct wl1271_tx_hw_res_descr *result;
519 u8 offset = wl->tx_results_count & TX_HW_RESULT_QUEUE_LEN_MASK;
520
521 /* process the packet */
522 result = &(wl->tx_res_if->tx_results_queue[offset]);
523 wl1271_tx_complete_packet(wl, result);
524
525 wl->tx_results_count++;
526 }
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300527}
528
529/* caller must hold wl->mutex */
Juuso Oikarinen781608c2010-05-24 11:18:17 +0300530void wl1271_tx_reset(struct wl1271 *wl)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300531{
532 int i;
533 struct sk_buff *skb;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300534
535 /* TX failure */
Juuso Oikarinen6742f552010-12-13 09:52:37 +0200536 for (i = 0; i < NUM_TX_QUEUES; i++) {
537 while ((skb = skb_dequeue(&wl->tx_queue[i]))) {
538 wl1271_debug(DEBUG_TX, "freeing skb 0x%p", skb);
539 ieee80211_tx_status(wl->hw, skb);
540 }
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300541 }
Juuso Oikarinen6742f552010-12-13 09:52:37 +0200542 wl->tx_queue_count = 0;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300543
Ido Yariv2fe33e82010-10-12 14:49:12 +0200544 /*
545 * Make sure the driver is at a consistent state, in case this
546 * function is called from a context other than interface removal.
547 */
548 handle_tx_low_watermark(wl);
549
Juuso Oikarinenbe7078c2009-10-08 21:56:26 +0300550 for (i = 0; i < ACX_TX_DESCRIPTORS; i++)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300551 if (wl->tx_frames[i] != NULL) {
552 skb = wl->tx_frames[i];
Ido Yariv25eeb9e2010-10-12 16:20:06 +0200553 wl1271_free_tx_id(wl, i);
Juuso Oikarinen781608c2010-05-24 11:18:17 +0300554 wl1271_debug(DEBUG_TX, "freeing skb 0x%p", skb);
Juuso Oikarinen6bbe89d2010-04-01 11:38:24 +0300555 ieee80211_tx_status(wl->hw, skb);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300556 }
Juuso Oikarinen781608c2010-05-24 11:18:17 +0300557}
558
559#define WL1271_TX_FLUSH_TIMEOUT 500000
560
561/* caller must *NOT* hold wl->mutex */
562void wl1271_tx_flush(struct wl1271 *wl)
563{
564 unsigned long timeout;
565 timeout = jiffies + usecs_to_jiffies(WL1271_TX_FLUSH_TIMEOUT);
566
567 while (!time_after(jiffies, timeout)) {
568 mutex_lock(&wl->mutex);
569 wl1271_debug(DEBUG_TX, "flushing tx buffer: %d",
570 wl->tx_frames_cnt);
Juuso Oikarinen6742f552010-12-13 09:52:37 +0200571 if ((wl->tx_frames_cnt == 0) && (wl->tx_queue_count == 0)) {
Juuso Oikarinen781608c2010-05-24 11:18:17 +0300572 mutex_unlock(&wl->mutex);
573 return;
574 }
575 mutex_unlock(&wl->mutex);
576 msleep(1);
577 }
578
579 wl1271_warning("Unable to flush all TX buffers, timed out.");
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300580}
Arik Nemtsove0fe3712010-10-16 18:19:53 +0200581
582u32 wl1271_tx_min_rate_get(struct wl1271 *wl)
583{
584 int i;
585 u32 rate = 0;
586
587 if (!wl->basic_rate_set) {
588 WARN_ON(1);
589 wl->basic_rate_set = wl->conf.tx.basic_rate;
590 }
591
592 for (i = 0; !rate; i++) {
593 if ((wl->basic_rate_set >> i) & 0x1)
594 rate = 1 << i;
595 }
596
597 return rate;
598}