blob: 3245c240cbdd3294d712cd55d1df24d226bc7eeb [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
Ido Yariv25eeb9e2010-10-12 16:20:06 +020034static int wl1271_alloc_tx_id(struct wl1271 *wl, struct sk_buff *skb)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030035{
Ido Yariv25eeb9e2010-10-12 16:20:06 +020036 int id;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030037
Ido Yariv25eeb9e2010-10-12 16:20:06 +020038 id = find_first_zero_bit(wl->tx_frames_map, ACX_TX_DESCRIPTORS);
39 if (id >= ACX_TX_DESCRIPTORS)
40 return -EBUSY;
41
42 __set_bit(id, wl->tx_frames_map);
43 wl->tx_frames[id] = skb;
44 wl->tx_frames_cnt++;
45 return id;
46}
47
48static void wl1271_free_tx_id(struct wl1271 *wl, int id)
49{
50 if (__test_and_clear_bit(id, wl->tx_frames_map)) {
51 wl->tx_frames[id] = NULL;
52 wl->tx_frames_cnt--;
53 }
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030054}
55
Ido Yariva19606b2010-09-30 13:28:28 +020056static int wl1271_tx_allocate(struct wl1271 *wl, struct sk_buff *skb, u32 extra,
57 u32 buf_offset)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030058{
59 struct wl1271_tx_hw_descr *desc;
60 u32 total_len = skb->len + sizeof(struct wl1271_tx_hw_descr) + extra;
Juuso Oikarinen5c9417f2010-02-22 08:38:39 +020061 u32 total_blocks;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030062 int id, ret = -EBUSY;
63
Ido Yariva19606b2010-09-30 13:28:28 +020064 if (buf_offset + total_len > WL1271_AGGR_BUFFER_SIZE)
Ido Yariv6c6e6692010-10-12 14:49:09 +020065 return -EAGAIN;
Ido Yariva19606b2010-09-30 13:28:28 +020066
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030067 /* allocate free identifier for the packet */
Ido Yariv25eeb9e2010-10-12 16:20:06 +020068 id = wl1271_alloc_tx_id(wl, skb);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030069 if (id < 0)
70 return id;
71
72 /* approximate the number of blocks required for this packet
73 in the firmware */
Juuso Oikarinen5c9417f2010-02-22 08:38:39 +020074 total_blocks = total_len + TX_HW_BLOCK_SIZE - 1;
75 total_blocks = total_blocks / TX_HW_BLOCK_SIZE + TX_HW_BLOCK_SPARE;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030076 if (total_blocks <= wl->tx_blocks_available) {
77 desc = (struct wl1271_tx_hw_descr *)skb_push(
78 skb, total_len - skb->len);
79
80 desc->extra_mem_blocks = TX_HW_BLOCK_SPARE;
81 desc->total_mem_blocks = total_blocks;
82 desc->id = id;
83
84 wl->tx_blocks_available -= total_blocks;
85
86 ret = 0;
87
88 wl1271_debug(DEBUG_TX,
89 "tx_allocate: size: %d, blocks: %d, id: %d",
90 total_len, total_blocks, id);
Juuso Oikarinen781608c2010-05-24 11:18:17 +030091 } else {
Ido Yariv25eeb9e2010-10-12 16:20:06 +020092 wl1271_free_tx_id(wl, id);
Juuso Oikarinen781608c2010-05-24 11:18:17 +030093 }
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030094
95 return ret;
96}
97
Ido Yariva19606b2010-09-30 13:28:28 +020098static void wl1271_tx_fill_hdr(struct wl1271 *wl, struct sk_buff *skb,
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030099 u32 extra, struct ieee80211_tx_info *control)
100{
Juuso Oikarinenac5e1e32010-02-22 08:38:38 +0200101 struct timespec ts;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300102 struct wl1271_tx_hw_descr *desc;
Arik Nemtsovc6c8a652010-10-16 20:27:53 +0200103 int pad, ac, rate_idx;
Juuso Oikarinenac5e1e32010-02-22 08:38:38 +0200104 s64 hosttime;
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300105 u16 tx_attr;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300106
107 desc = (struct wl1271_tx_hw_descr *) skb->data;
108
Juuso Oikarinen1e2b7972009-10-08 21:56:20 +0300109 /* relocate space for security header */
110 if (extra) {
111 void *framestart = skb->data + sizeof(*desc);
112 u16 fc = *(u16 *)(framestart + extra);
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300113 int hdrlen = ieee80211_hdrlen(cpu_to_le16(fc));
Juuso Oikarinen1e2b7972009-10-08 21:56:20 +0300114 memmove(framestart, framestart + extra, hdrlen);
115 }
116
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300117 /* configure packet life time */
Juuso Oikarinenac5e1e32010-02-22 08:38:38 +0200118 getnstimeofday(&ts);
119 hosttime = (timespec_to_ns(&ts) >> 10);
120 desc->start_time = cpu_to_le32(hosttime - wl->time_offset);
Arik Nemtsovc6c8a652010-10-16 20:27:53 +0200121
122 if (wl->bss_type != BSS_TYPE_AP_BSS)
123 desc->life_time = cpu_to_le16(TX_HW_MGMT_PKT_LIFETIME_TU);
124 else
125 desc->life_time = cpu_to_le16(TX_HW_AP_MODE_PKT_LIFETIME_TU);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300126
127 /* configure the tx attributes */
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300128 tx_attr = wl->session_counter << TX_HW_ATTR_OFST_SESSION_COUNTER;
Kalle Valoc6999d82010-02-18 13:25:41 +0200129
Juuso Oikarinened484a12010-09-01 11:31:11 +0200130 /* queue (we use same identifiers for tid's and ac's */
Kalle Valoc6999d82010-02-18 13:25:41 +0200131 ac = wl1271_tx_get_queue(skb_get_queue_mapping(skb));
Juuso Oikarinened484a12010-09-01 11:31:11 +0200132 desc->tid = ac;
Arik Nemtsovc6c8a652010-10-16 20:27:53 +0200133
134 if (wl->bss_type != BSS_TYPE_AP_BSS) {
135 desc->aid = TX_HW_DEFAULT_AID;
136
137 /* if the packets are destined for AP (have a STA entry)
138 send them with AP rate policies, otherwise use default
139 basic rates */
140 if (control->control.sta)
141 rate_idx = ACX_TX_AP_FULL_RATE;
142 else
143 rate_idx = ACX_TX_BASIC_RATE;
144 } else {
145 if (control->control.sta) {
146 struct wl1271_station *wl_sta;
147
148 wl_sta = (struct wl1271_station *)
149 control->control.sta->drv_priv;
150 desc->hlid = wl_sta->hlid;
151 rate_idx = ac;
152 } else {
153 struct ieee80211_hdr *hdr;
154
155 hdr = (struct ieee80211_hdr *)
156 (skb->data + sizeof(*desc));
157 if (ieee80211_is_mgmt(hdr->frame_control)) {
158 desc->hlid = WL1271_AP_GLOBAL_HLID;
159 rate_idx = ACX_TX_AP_MODE_MGMT_RATE;
160 } else {
161 desc->hlid = WL1271_AP_BROADCAST_HLID;
162 rate_idx = ACX_TX_AP_MODE_BCST_RATE;
163 }
164 }
165 }
166
167 tx_attr |= rate_idx << TX_HW_ATTR_OFST_RATE_POLICY;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300168 desc->reserved = 0;
169
170 /* align the length (and store in terms of words) */
171 pad = WL1271_TX_ALIGN(skb->len);
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300172 desc->length = cpu_to_le16(pad >> 2);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300173
174 /* calculate number of padding bytes */
175 pad = pad - skb->len;
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300176 tx_attr |= pad << TX_HW_ATTR_OFST_LAST_WORD_PAD;
177
178 desc->tx_attr = cpu_to_le16(tx_attr);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300179
Arik Nemtsovc6c8a652010-10-16 20:27:53 +0200180 wl1271_debug(DEBUG_TX, "tx_fill_hdr: pad: %d hlid: %d "
181 "tx_attr: 0x%x len: %d life: %d mem: %d", pad, (int)desc->hlid,
182 (int)desc->tx_attr, (int)desc->length, (int)desc->life_time,
183 (int)desc->total_mem_blocks);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300184}
185
186/* caller must hold wl->mutex */
Ido Yariva19606b2010-09-30 13:28:28 +0200187static int wl1271_prepare_tx_frame(struct wl1271 *wl, struct sk_buff *skb,
188 u32 buf_offset)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300189{
190 struct ieee80211_tx_info *info;
191 u32 extra = 0;
192 int ret = 0;
193 u8 idx;
Ido Yariva19606b2010-09-30 13:28:28 +0200194 u32 total_len;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300195
196 if (!skb)
197 return -EINVAL;
198
199 info = IEEE80211_SKB_CB(skb);
200
201 if (info->control.hw_key &&
Johannes Berg97359d12010-08-10 09:46:38 +0200202 info->control.hw_key->cipher == WLAN_CIPHER_SUITE_TKIP)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300203 extra = WL1271_TKIP_IV_SPACE;
204
205 if (info->control.hw_key) {
206 idx = info->control.hw_key->hw_key_idx;
207
208 /* FIXME: do we have to do this if we're not using WEP? */
209 if (unlikely(wl->default_key != idx)) {
Arik Nemtsov98bdaab2010-10-16 18:08:58 +0200210 ret = wl1271_cmd_set_sta_default_wep_key(wl, idx);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300211 if (ret < 0)
212 return ret;
Juuso Oikarinenee444cf2010-02-18 13:25:50 +0200213 wl->default_key = idx;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300214 }
215 }
216
Ido Yariva19606b2010-09-30 13:28:28 +0200217 ret = wl1271_tx_allocate(wl, skb, extra, buf_offset);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300218 if (ret < 0)
219 return ret;
220
Ido Yariva19606b2010-09-30 13:28:28 +0200221 wl1271_tx_fill_hdr(wl, skb, extra, info);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300222
Ido Yariva19606b2010-09-30 13:28:28 +0200223 /*
224 * The length of each packet is stored in terms of words. Thus, we must
225 * pad the skb data to make sure its length is aligned.
226 * The number of padding bytes is computed and set in wl1271_tx_fill_hdr
227 */
228 total_len = WL1271_TX_ALIGN(skb->len);
229 memcpy(wl->aggr_buf + buf_offset, skb->data, skb->len);
230 memset(wl->aggr_buf + buf_offset + skb->len, 0, total_len - skb->len);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300231
Ido Yariva19606b2010-09-30 13:28:28 +0200232 return total_len;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300233}
234
Juuso Oikarinenebba60c2010-04-01 11:38:20 +0300235u32 wl1271_tx_enabled_rates_get(struct wl1271 *wl, u32 rate_set)
Juuso Oikarinen830fb672009-12-11 15:41:06 +0200236{
237 struct ieee80211_supported_band *band;
238 u32 enabled_rates = 0;
239 int bit;
240
241 band = wl->hw->wiphy->bands[wl->band];
242 for (bit = 0; bit < band->n_bitrates; bit++) {
243 if (rate_set & 0x1)
244 enabled_rates |= band->bitrates[bit].hw_value;
245 rate_set >>= 1;
246 }
247
Shahar Levi00d20102010-11-08 11:20:10 +0000248#ifdef CONFIG_WL12XX_HT
Shahar Levi18357852010-10-13 16:09:41 +0200249 /* MCS rates indication are on bits 16 - 23 */
250 rate_set >>= HW_HT_RATES_OFFSET - band->n_bitrates;
251
252 for (bit = 0; bit < 8; bit++) {
253 if (rate_set & 0x1)
254 enabled_rates |= (CONF_HW_BIT_RATE_MCS_0 << bit);
255 rate_set >>= 1;
256 }
257#endif
258
Juuso Oikarinen830fb672009-12-11 15:41:06 +0200259 return enabled_rates;
260}
261
Ido Yariv2fe33e82010-10-12 14:49:12 +0200262static void handle_tx_low_watermark(struct wl1271 *wl)
263{
264 unsigned long flags;
265
266 if (test_bit(WL1271_FLAG_TX_QUEUE_STOPPED, &wl->flags) &&
Juuso Oikarinen6742f552010-12-13 09:52:37 +0200267 wl->tx_queue_count <= WL1271_TX_QUEUE_LOW_WATERMARK) {
Ido Yariv2fe33e82010-10-12 14:49:12 +0200268 /* firmware buffer has space, restart queues */
269 spin_lock_irqsave(&wl->wl_lock, flags);
270 ieee80211_wake_queues(wl->hw);
271 clear_bit(WL1271_FLAG_TX_QUEUE_STOPPED, &wl->flags);
272 spin_unlock_irqrestore(&wl->wl_lock, flags);
273 }
274}
275
Juuso Oikarinen6742f552010-12-13 09:52:37 +0200276static struct sk_buff *wl1271_skb_dequeue(struct wl1271 *wl)
277{
278 struct sk_buff *skb = NULL;
279 unsigned long flags;
280
281 skb = skb_dequeue(&wl->tx_queue[CONF_TX_AC_VO]);
282 if (skb)
283 goto out;
284 skb = skb_dequeue(&wl->tx_queue[CONF_TX_AC_VI]);
285 if (skb)
286 goto out;
287 skb = skb_dequeue(&wl->tx_queue[CONF_TX_AC_BE]);
288 if (skb)
289 goto out;
290 skb = skb_dequeue(&wl->tx_queue[CONF_TX_AC_BK]);
291
292out:
293 if (skb) {
294 spin_lock_irqsave(&wl->wl_lock, flags);
295 wl->tx_queue_count--;
296 spin_unlock_irqrestore(&wl->wl_lock, flags);
297 }
298
299 return skb;
300}
301
302static void wl1271_skb_queue_head(struct wl1271 *wl, struct sk_buff *skb)
303{
304 unsigned long flags;
305 int q = wl1271_tx_get_queue(skb_get_queue_mapping(skb));
306
307 skb_queue_head(&wl->tx_queue[q], skb);
308 spin_lock_irqsave(&wl->wl_lock, flags);
309 wl->tx_queue_count++;
310 spin_unlock_irqrestore(&wl->wl_lock, flags);
311}
312
Ido Yariva5225502010-10-12 14:49:10 +0200313void wl1271_tx_work_locked(struct wl1271 *wl)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300314{
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300315 struct sk_buff *skb;
316 bool woken_up = false;
Juuso Oikarinen830fb672009-12-11 15:41:06 +0200317 u32 sta_rates = 0;
Ido Yariv6c6e6692010-10-12 14:49:09 +0200318 u32 buf_offset = 0;
319 bool sent_packets = false;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300320 int ret;
321
Juuso Oikarinen830fb672009-12-11 15:41:06 +0200322 /* check if the rates supported by the AP have changed */
323 if (unlikely(test_and_clear_bit(WL1271_FLAG_STA_RATES_CHANGED,
324 &wl->flags))) {
325 unsigned long flags;
Ido Yariv2fe33e82010-10-12 14:49:12 +0200326
Juuso Oikarinen830fb672009-12-11 15:41:06 +0200327 spin_lock_irqsave(&wl->wl_lock, flags);
328 sta_rates = wl->sta_rate_set;
329 spin_unlock_irqrestore(&wl->wl_lock, flags);
330 }
331
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300332 if (unlikely(wl->state == WL1271_STATE_OFF))
333 goto out;
334
Juuso Oikarinen830fb672009-12-11 15:41:06 +0200335 /* if rates have changed, re-configure the rate policy */
336 if (unlikely(sta_rates)) {
Luciano Coelho54046432010-10-20 15:15:52 +0300337 ret = wl1271_ps_elp_wakeup(wl, false);
338 if (ret < 0)
339 goto out;
340 woken_up = true;
341
Juuso Oikarinen830fb672009-12-11 15:41:06 +0200342 wl->rate_set = wl1271_tx_enabled_rates_get(wl, sta_rates);
Arik Nemtsov79b223f2010-10-16 17:52:59 +0200343 wl1271_acx_sta_rate_policies(wl);
Juuso Oikarinen830fb672009-12-11 15:41:06 +0200344 }
345
Juuso Oikarinen6742f552010-12-13 09:52:37 +0200346 while ((skb = wl1271_skb_dequeue(wl))) {
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300347 if (!woken_up) {
348 ret = wl1271_ps_elp_wakeup(wl, false);
349 if (ret < 0)
Juuso Oikarinenffb591c2010-02-22 08:38:31 +0200350 goto out_ack;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300351 woken_up = true;
352 }
353
Ido Yariva19606b2010-09-30 13:28:28 +0200354 ret = wl1271_prepare_tx_frame(wl, skb, buf_offset);
Ido Yariv6c6e6692010-10-12 14:49:09 +0200355 if (ret == -EAGAIN) {
Ido Yariva19606b2010-09-30 13:28:28 +0200356 /*
Ido Yariv6c6e6692010-10-12 14:49:09 +0200357 * Aggregation buffer is full.
358 * Flush buffer and try again.
359 */
Juuso Oikarinen6742f552010-12-13 09:52:37 +0200360 wl1271_skb_queue_head(wl, skb);
Ido Yariv6c6e6692010-10-12 14:49:09 +0200361 wl1271_write(wl, WL1271_SLV_MEM_DATA, wl->aggr_buf,
Juuso Oikarinen6742f552010-12-13 09:52:37 +0200362 buf_offset, true);
Ido Yariv6c6e6692010-10-12 14:49:09 +0200363 sent_packets = true;
364 buf_offset = 0;
365 continue;
366 } else if (ret == -EBUSY) {
367 /*
368 * Firmware buffer is full.
Ido Yariva19606b2010-09-30 13:28:28 +0200369 * Queue back last skb, and stop aggregating.
370 */
Juuso Oikarinen6742f552010-12-13 09:52:37 +0200371 wl1271_skb_queue_head(wl, skb);
Ido Yariva5225502010-10-12 14:49:10 +0200372 /* No work left, avoid scheduling redundant tx work */
373 set_bit(WL1271_FLAG_FW_TX_BUSY, &wl->flags);
Juuso Oikarinenffb591c2010-02-22 08:38:31 +0200374 goto out_ack;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300375 } else if (ret < 0) {
376 dev_kfree_skb(skb);
Juuso Oikarinenffb591c2010-02-22 08:38:31 +0200377 goto out_ack;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300378 }
Ido Yariva19606b2010-09-30 13:28:28 +0200379 buf_offset += ret;
380 wl->tx_packets_count++;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300381 }
382
Juuso Oikarinenffb591c2010-02-22 08:38:31 +0200383out_ack:
Ido Yariva19606b2010-09-30 13:28:28 +0200384 if (buf_offset) {
385 wl1271_write(wl, WL1271_SLV_MEM_DATA, wl->aggr_buf,
386 buf_offset, true);
Ido Yariv6c6e6692010-10-12 14:49:09 +0200387 sent_packets = true;
388 }
389 if (sent_packets) {
Ido Yariva19606b2010-09-30 13:28:28 +0200390 /* interrupt the firmware with the new packets */
Juuso Oikarinenffb591c2010-02-22 08:38:31 +0200391 wl1271_write32(wl, WL1271_HOST_WR_ACCESS, wl->tx_packets_count);
Ido Yariv2fe33e82010-10-12 14:49:12 +0200392 handle_tx_low_watermark(wl);
Ido Yariva19606b2010-09-30 13:28:28 +0200393 }
Juuso Oikarinenffb591c2010-02-22 08:38:31 +0200394
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300395out:
396 if (woken_up)
397 wl1271_ps_elp_sleep(wl);
Ido Yariva5225502010-10-12 14:49:10 +0200398}
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300399
Ido Yariva5225502010-10-12 14:49:10 +0200400void wl1271_tx_work(struct work_struct *work)
401{
402 struct wl1271 *wl = container_of(work, struct wl1271, tx_work);
403
404 mutex_lock(&wl->mutex);
405 wl1271_tx_work_locked(wl);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300406 mutex_unlock(&wl->mutex);
407}
408
409static void wl1271_tx_complete_packet(struct wl1271 *wl,
410 struct wl1271_tx_hw_res_descr *result)
411{
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300412 struct ieee80211_tx_info *info;
413 struct sk_buff *skb;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300414 int id = result->id;
Juuso Oikarinen31627dc2010-03-26 12:53:12 +0200415 int rate = -1;
416 u8 retries = 0;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300417
418 /* check for id legality */
Juuso Oikarinenffb591c2010-02-22 08:38:31 +0200419 if (unlikely(id >= ACX_TX_DESCRIPTORS || wl->tx_frames[id] == NULL)) {
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300420 wl1271_warning("TX result illegal id: %d", id);
421 return;
422 }
423
424 skb = wl->tx_frames[id];
425 info = IEEE80211_SKB_CB(skb);
426
Juuso Oikarinen31627dc2010-03-26 12:53:12 +0200427 /* update the TX status info */
428 if (result->status == TX_SUCCESS) {
429 if (!(info->flags & IEEE80211_TX_CTL_NO_ACK))
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300430 info->flags |= IEEE80211_TX_STAT_ACK;
Teemu Paasikivi6a2de932010-10-14 11:00:04 +0200431 rate = wl1271_rate_to_idx(result->rate_class_index, wl->band);
Juuso Oikarinen31627dc2010-03-26 12:53:12 +0200432 retries = result->ack_failures;
433 } else if (result->status == TX_RETRY_EXCEEDED) {
434 wl->stats.excessive_retries++;
435 retries = result->ack_failures;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300436 }
437
Juuso Oikarinen31627dc2010-03-26 12:53:12 +0200438 info->status.rates[0].idx = rate;
439 info->status.rates[0].count = retries;
440 info->status.rates[0].flags = 0;
441 info->status.ack_signal = -1;
442
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300443 wl->stats.retry_count += result->ack_failures;
444
Juuso Oikarinenac4e4ce2009-10-08 21:56:19 +0300445 /* update security sequence number */
Juuso Oikarinen04e36fc2010-02-22 08:38:40 +0200446 wl->tx_security_seq += (result->lsb_security_sequence_number -
447 wl->tx_security_last_seq);
Juuso Oikarinenac4e4ce2009-10-08 21:56:19 +0300448 wl->tx_security_last_seq = result->lsb_security_sequence_number;
449
Juuso Oikarinen1e2b7972009-10-08 21:56:20 +0300450 /* remove private header from packet */
451 skb_pull(skb, sizeof(struct wl1271_tx_hw_descr));
452
453 /* remove TKIP header space if present */
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300454 if (info->control.hw_key &&
Johannes Berg97359d12010-08-10 09:46:38 +0200455 info->control.hw_key->cipher == WLAN_CIPHER_SUITE_TKIP) {
Juuso Oikarinen1e2b7972009-10-08 21:56:20 +0300456 int hdrlen = ieee80211_get_hdrlen_from_skb(skb);
457 memmove(skb->data + WL1271_TKIP_IV_SPACE, skb->data, hdrlen);
458 skb_pull(skb, WL1271_TKIP_IV_SPACE);
459 }
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300460
461 wl1271_debug(DEBUG_TX, "tx status id %u skb 0x%p failures %u rate 0x%x"
462 " status 0x%x",
463 result->id, skb, result->ack_failures,
464 result->rate_class_index, result->status);
465
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300466 /* return the packet to the stack */
467 ieee80211_tx_status(wl->hw, skb);
Ido Yariv25eeb9e2010-10-12 16:20:06 +0200468 wl1271_free_tx_id(wl, result->id);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300469}
470
471/* Called upon reception of a TX complete interrupt */
Juuso Oikarinenffb591c2010-02-22 08:38:31 +0200472void wl1271_tx_complete(struct wl1271 *wl)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300473{
474 struct wl1271_acx_mem_map *memmap =
475 (struct wl1271_acx_mem_map *)wl->target_mem_map;
Juuso Oikarinenffb591c2010-02-22 08:38:31 +0200476 u32 count, fw_counter;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300477 u32 i;
478
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300479 /* read the tx results from the chipset */
Teemu Paasikivi7b048c52010-02-18 13:25:55 +0200480 wl1271_read(wl, le32_to_cpu(memmap->tx_result),
481 wl->tx_res_if, sizeof(*wl->tx_res_if), false);
Juuso Oikarinenffb591c2010-02-22 08:38:31 +0200482 fw_counter = le32_to_cpu(wl->tx_res_if->tx_result_fw_counter);
483
484 /* write host counter to chipset (to ack) */
485 wl1271_write32(wl, le32_to_cpu(memmap->tx_result) +
486 offsetof(struct wl1271_tx_hw_res_if,
487 tx_result_host_counter), fw_counter);
488
489 count = fw_counter - wl->tx_results_count;
Juuso Oikarinen06f7bc72010-02-22 08:38:33 +0200490 wl1271_debug(DEBUG_TX, "tx_complete received, packets: %d", count);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300491
492 /* verify that the result buffer is not getting overrun */
Juuso Oikarinenffb591c2010-02-22 08:38:31 +0200493 if (unlikely(count > TX_HW_RESULT_QUEUE_LEN))
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300494 wl1271_warning("TX result overflow from chipset: %d", count);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300495
496 /* process the results */
497 for (i = 0; i < count; i++) {
498 struct wl1271_tx_hw_res_descr *result;
499 u8 offset = wl->tx_results_count & TX_HW_RESULT_QUEUE_LEN_MASK;
500
501 /* process the packet */
502 result = &(wl->tx_res_if->tx_results_queue[offset]);
503 wl1271_tx_complete_packet(wl, result);
504
505 wl->tx_results_count++;
506 }
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300507}
508
509/* caller must hold wl->mutex */
Juuso Oikarinen781608c2010-05-24 11:18:17 +0300510void wl1271_tx_reset(struct wl1271 *wl)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300511{
512 int i;
513 struct sk_buff *skb;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300514
515 /* TX failure */
Juuso Oikarinen6742f552010-12-13 09:52:37 +0200516 for (i = 0; i < NUM_TX_QUEUES; i++) {
517 while ((skb = skb_dequeue(&wl->tx_queue[i]))) {
518 wl1271_debug(DEBUG_TX, "freeing skb 0x%p", skb);
519 ieee80211_tx_status(wl->hw, skb);
520 }
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300521 }
Juuso Oikarinen6742f552010-12-13 09:52:37 +0200522 wl->tx_queue_count = 0;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300523
Ido Yariv2fe33e82010-10-12 14:49:12 +0200524 /*
525 * Make sure the driver is at a consistent state, in case this
526 * function is called from a context other than interface removal.
527 */
528 handle_tx_low_watermark(wl);
529
Juuso Oikarinenbe7078c2009-10-08 21:56:26 +0300530 for (i = 0; i < ACX_TX_DESCRIPTORS; i++)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300531 if (wl->tx_frames[i] != NULL) {
532 skb = wl->tx_frames[i];
Ido Yariv25eeb9e2010-10-12 16:20:06 +0200533 wl1271_free_tx_id(wl, i);
Juuso Oikarinen781608c2010-05-24 11:18:17 +0300534 wl1271_debug(DEBUG_TX, "freeing skb 0x%p", skb);
Juuso Oikarinen6bbe89d2010-04-01 11:38:24 +0300535 ieee80211_tx_status(wl->hw, skb);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300536 }
Juuso Oikarinen781608c2010-05-24 11:18:17 +0300537}
538
539#define WL1271_TX_FLUSH_TIMEOUT 500000
540
541/* caller must *NOT* hold wl->mutex */
542void wl1271_tx_flush(struct wl1271 *wl)
543{
544 unsigned long timeout;
545 timeout = jiffies + usecs_to_jiffies(WL1271_TX_FLUSH_TIMEOUT);
546
547 while (!time_after(jiffies, timeout)) {
548 mutex_lock(&wl->mutex);
549 wl1271_debug(DEBUG_TX, "flushing tx buffer: %d",
550 wl->tx_frames_cnt);
Juuso Oikarinen6742f552010-12-13 09:52:37 +0200551 if ((wl->tx_frames_cnt == 0) && (wl->tx_queue_count == 0)) {
Juuso Oikarinen781608c2010-05-24 11:18:17 +0300552 mutex_unlock(&wl->mutex);
553 return;
554 }
555 mutex_unlock(&wl->mutex);
556 msleep(1);
557 }
558
559 wl1271_warning("Unable to flush all TX buffers, timed out.");
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300560}
Arik Nemtsove0fe3712010-10-16 18:19:53 +0200561
562u32 wl1271_tx_min_rate_get(struct wl1271 *wl)
563{
564 int i;
565 u32 rate = 0;
566
567 if (!wl->basic_rate_set) {
568 WARN_ON(1);
569 wl->basic_rate_set = wl->conf.tx.basic_rate;
570 }
571
572 for (i = 0; !rate; i++) {
573 if ((wl->basic_rate_set >> i) & 0x1)
574 rate = 1 << i;
575 }
576
577 return rate;
578}