blob: ea1382bd38f401d12b690e2000f4a81b455e8d6e [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
Arik Nemtsov99a27752011-02-23 00:22:25 +020073static void wl1271_tx_ap_update_inconnection_sta(struct wl1271 *wl,
74 struct sk_buff *skb)
75{
76 struct ieee80211_hdr *hdr;
77
78 /*
79 * add the station to the known list before transmitting the
80 * authentication response. this way it won't get de-authed by FW
81 * when transmitting too soon.
82 */
83 hdr = (struct ieee80211_hdr *)(skb->data +
84 sizeof(struct wl1271_tx_hw_descr));
85 if (ieee80211_is_auth(hdr->frame_control))
86 wl1271_acx_set_inconnection_sta(wl, hdr->addr1);
87}
88
Arik Nemtsova8c0ddb2011-02-23 00:22:26 +020089u8 wl1271_tx_get_hlid(struct sk_buff *skb)
90{
91 struct ieee80211_tx_info *control = IEEE80211_SKB_CB(skb);
92
93 if (control->control.sta) {
94 struct wl1271_station *wl_sta;
95
96 wl_sta = (struct wl1271_station *)
97 control->control.sta->drv_priv;
98 return wl_sta->hlid;
99 } else {
100 struct ieee80211_hdr *hdr;
101
102 hdr = (struct ieee80211_hdr *)skb->data;
103 if (ieee80211_is_mgmt(hdr->frame_control))
104 return WL1271_AP_GLOBAL_HLID;
105 else
106 return WL1271_AP_BROADCAST_HLID;
107 }
108}
109
Ido Yariva19606b2010-09-30 13:28:28 +0200110static int wl1271_tx_allocate(struct wl1271 *wl, struct sk_buff *skb, u32 extra,
Arik Nemtsov09039f42011-02-23 00:22:30 +0200111 u32 buf_offset, u8 hlid)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300112{
113 struct wl1271_tx_hw_descr *desc;
114 u32 total_len = skb->len + sizeof(struct wl1271_tx_hw_descr) + extra;
Juuso Oikarinen5c9417f2010-02-22 08:38:39 +0200115 u32 total_blocks;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300116 int id, ret = -EBUSY;
117
Ido Yariva19606b2010-09-30 13:28:28 +0200118 if (buf_offset + total_len > WL1271_AGGR_BUFFER_SIZE)
Ido Yariv6c6e6692010-10-12 14:49:09 +0200119 return -EAGAIN;
Ido Yariva19606b2010-09-30 13:28:28 +0200120
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300121 /* allocate free identifier for the packet */
Ido Yariv25eeb9e2010-10-12 16:20:06 +0200122 id = wl1271_alloc_tx_id(wl, skb);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300123 if (id < 0)
124 return id;
125
126 /* approximate the number of blocks required for this packet
127 in the firmware */
Juuso Oikarinen5c9417f2010-02-22 08:38:39 +0200128 total_blocks = total_len + TX_HW_BLOCK_SIZE - 1;
129 total_blocks = total_blocks / TX_HW_BLOCK_SIZE + TX_HW_BLOCK_SPARE;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300130 if (total_blocks <= wl->tx_blocks_available) {
131 desc = (struct wl1271_tx_hw_descr *)skb_push(
132 skb, total_len - skb->len);
133
134 desc->extra_mem_blocks = TX_HW_BLOCK_SPARE;
135 desc->total_mem_blocks = total_blocks;
136 desc->id = id;
137
138 wl->tx_blocks_available -= total_blocks;
139
Arik Nemtsov09039f42011-02-23 00:22:30 +0200140 if (wl->bss_type == BSS_TYPE_AP_BSS)
141 wl->links[hlid].allocated_blks += total_blocks;
142
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300143 ret = 0;
144
145 wl1271_debug(DEBUG_TX,
146 "tx_allocate: size: %d, blocks: %d, id: %d",
147 total_len, total_blocks, id);
Juuso Oikarinen781608c2010-05-24 11:18:17 +0300148 } else {
Ido Yariv25eeb9e2010-10-12 16:20:06 +0200149 wl1271_free_tx_id(wl, id);
Juuso Oikarinen781608c2010-05-24 11:18:17 +0300150 }
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300151
152 return ret;
153}
154
Ido Yariva19606b2010-09-30 13:28:28 +0200155static void wl1271_tx_fill_hdr(struct wl1271 *wl, struct sk_buff *skb,
Arik Nemtsov09039f42011-02-23 00:22:30 +0200156 u32 extra, struct ieee80211_tx_info *control,
157 u8 hlid)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300158{
Juuso Oikarinenac5e1e32010-02-22 08:38:38 +0200159 struct timespec ts;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300160 struct wl1271_tx_hw_descr *desc;
Arik Nemtsovc6c8a652010-10-16 20:27:53 +0200161 int pad, ac, rate_idx;
Juuso Oikarinenac5e1e32010-02-22 08:38:38 +0200162 s64 hosttime;
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300163 u16 tx_attr;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300164
165 desc = (struct wl1271_tx_hw_descr *) skb->data;
166
Juuso Oikarinen1e2b7972009-10-08 21:56:20 +0300167 /* relocate space for security header */
168 if (extra) {
169 void *framestart = skb->data + sizeof(*desc);
170 u16 fc = *(u16 *)(framestart + extra);
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300171 int hdrlen = ieee80211_hdrlen(cpu_to_le16(fc));
Juuso Oikarinen1e2b7972009-10-08 21:56:20 +0300172 memmove(framestart, framestart + extra, hdrlen);
173 }
174
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300175 /* configure packet life time */
Juuso Oikarinenac5e1e32010-02-22 08:38:38 +0200176 getnstimeofday(&ts);
177 hosttime = (timespec_to_ns(&ts) >> 10);
178 desc->start_time = cpu_to_le32(hosttime - wl->time_offset);
Arik Nemtsovc6c8a652010-10-16 20:27:53 +0200179
180 if (wl->bss_type != BSS_TYPE_AP_BSS)
181 desc->life_time = cpu_to_le16(TX_HW_MGMT_PKT_LIFETIME_TU);
182 else
183 desc->life_time = cpu_to_le16(TX_HW_AP_MODE_PKT_LIFETIME_TU);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300184
185 /* configure the tx attributes */
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300186 tx_attr = wl->session_counter << TX_HW_ATTR_OFST_SESSION_COUNTER;
Kalle Valoc6999d82010-02-18 13:25:41 +0200187
Juuso Oikarinened484a12010-09-01 11:31:11 +0200188 /* queue (we use same identifiers for tid's and ac's */
Kalle Valoc6999d82010-02-18 13:25:41 +0200189 ac = wl1271_tx_get_queue(skb_get_queue_mapping(skb));
Juuso Oikarinened484a12010-09-01 11:31:11 +0200190 desc->tid = ac;
Arik Nemtsovc6c8a652010-10-16 20:27:53 +0200191
192 if (wl->bss_type != BSS_TYPE_AP_BSS) {
Arik Nemtsov09039f42011-02-23 00:22:30 +0200193 desc->aid = hlid;
Arik Nemtsovc6c8a652010-10-16 20:27:53 +0200194
195 /* if the packets are destined for AP (have a STA entry)
196 send them with AP rate policies, otherwise use default
197 basic rates */
198 if (control->control.sta)
199 rate_idx = ACX_TX_AP_FULL_RATE;
200 else
201 rate_idx = ACX_TX_BASIC_RATE;
202 } else {
Arik Nemtsov09039f42011-02-23 00:22:30 +0200203 desc->hlid = hlid;
204 switch (hlid) {
205 case WL1271_AP_GLOBAL_HLID:
206 rate_idx = ACX_TX_AP_MODE_MGMT_RATE;
207 break;
208 case WL1271_AP_BROADCAST_HLID:
209 rate_idx = ACX_TX_AP_MODE_BCST_RATE;
210 break;
211 default:
Arik Nemtsovc6c8a652010-10-16 20:27:53 +0200212 rate_idx = ac;
Arik Nemtsov09039f42011-02-23 00:22:30 +0200213 break;
Arik Nemtsovc6c8a652010-10-16 20:27:53 +0200214 }
215 }
216
217 tx_attr |= rate_idx << TX_HW_ATTR_OFST_RATE_POLICY;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300218 desc->reserved = 0;
219
220 /* align the length (and store in terms of words) */
Eliad Peller02ad2d92011-02-23 00:27:06 +0200221 pad = ALIGN(skb->len, WL1271_TX_ALIGN_TO);
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300222 desc->length = cpu_to_le16(pad >> 2);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300223
224 /* calculate number of padding bytes */
225 pad = pad - skb->len;
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300226 tx_attr |= pad << TX_HW_ATTR_OFST_LAST_WORD_PAD;
227
228 desc->tx_attr = cpu_to_le16(tx_attr);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300229
Arik Nemtsovc6c8a652010-10-16 20:27:53 +0200230 wl1271_debug(DEBUG_TX, "tx_fill_hdr: pad: %d hlid: %d "
Eliad Peller1d4801f2011-01-16 10:07:10 +0100231 "tx_attr: 0x%x len: %d life: %d mem: %d", pad, desc->hlid,
232 le16_to_cpu(desc->tx_attr), le16_to_cpu(desc->length),
233 le16_to_cpu(desc->life_time), desc->total_mem_blocks);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300234}
235
236/* caller must hold wl->mutex */
Ido Yariva19606b2010-09-30 13:28:28 +0200237static int wl1271_prepare_tx_frame(struct wl1271 *wl, struct sk_buff *skb,
238 u32 buf_offset)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300239{
240 struct ieee80211_tx_info *info;
241 u32 extra = 0;
242 int ret = 0;
Ido Yariva19606b2010-09-30 13:28:28 +0200243 u32 total_len;
Arik Nemtsov09039f42011-02-23 00:22:30 +0200244 u8 hlid;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300245
246 if (!skb)
247 return -EINVAL;
248
249 info = IEEE80211_SKB_CB(skb);
250
251 if (info->control.hw_key &&
Johannes Berg97359d12010-08-10 09:46:38 +0200252 info->control.hw_key->cipher == WLAN_CIPHER_SUITE_TKIP)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300253 extra = WL1271_TKIP_IV_SPACE;
254
255 if (info->control.hw_key) {
Arik Nemtsov7f179b42010-10-16 21:39:06 +0200256 bool is_wep;
257 u8 idx = info->control.hw_key->hw_key_idx;
258 u32 cipher = info->control.hw_key->cipher;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300259
Arik Nemtsov7f179b42010-10-16 21:39:06 +0200260 is_wep = (cipher == WLAN_CIPHER_SUITE_WEP40) ||
261 (cipher == WLAN_CIPHER_SUITE_WEP104);
262
263 if (unlikely(is_wep && wl->default_key != idx)) {
264 ret = wl1271_set_default_wep_key(wl, idx);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300265 if (ret < 0)
266 return ret;
Juuso Oikarinenee444cf2010-02-18 13:25:50 +0200267 wl->default_key = idx;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300268 }
269 }
270
Arik Nemtsov09039f42011-02-23 00:22:30 +0200271 if (wl->bss_type == BSS_TYPE_AP_BSS)
272 hlid = wl1271_tx_get_hlid(skb);
273 else
274 hlid = TX_HW_DEFAULT_AID;
275
276 ret = wl1271_tx_allocate(wl, skb, extra, buf_offset, hlid);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300277 if (ret < 0)
278 return ret;
279
Arik Nemtsov99a27752011-02-23 00:22:25 +0200280 if (wl->bss_type == BSS_TYPE_AP_BSS)
281 wl1271_tx_ap_update_inconnection_sta(wl, skb);
282
Arik Nemtsov09039f42011-02-23 00:22:30 +0200283 wl1271_tx_fill_hdr(wl, skb, extra, info, hlid);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300284
Ido Yariva19606b2010-09-30 13:28:28 +0200285 /*
286 * The length of each packet is stored in terms of words. Thus, we must
287 * pad the skb data to make sure its length is aligned.
288 * The number of padding bytes is computed and set in wl1271_tx_fill_hdr
289 */
Eliad Peller02ad2d92011-02-23 00:27:06 +0200290 total_len = ALIGN(skb->len, WL1271_TX_ALIGN_TO);
Ido Yariva19606b2010-09-30 13:28:28 +0200291 memcpy(wl->aggr_buf + buf_offset, skb->data, skb->len);
292 memset(wl->aggr_buf + buf_offset + skb->len, 0, total_len - skb->len);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300293
Ido Yariva19606b2010-09-30 13:28:28 +0200294 return total_len;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300295}
296
Juuso Oikarinenebba60c2010-04-01 11:38:20 +0300297u32 wl1271_tx_enabled_rates_get(struct wl1271 *wl, u32 rate_set)
Juuso Oikarinen830fb672009-12-11 15:41:06 +0200298{
299 struct ieee80211_supported_band *band;
300 u32 enabled_rates = 0;
301 int bit;
302
303 band = wl->hw->wiphy->bands[wl->band];
304 for (bit = 0; bit < band->n_bitrates; bit++) {
305 if (rate_set & 0x1)
306 enabled_rates |= band->bitrates[bit].hw_value;
307 rate_set >>= 1;
308 }
309
Shahar Levi00d20102010-11-08 11:20:10 +0000310#ifdef CONFIG_WL12XX_HT
Shahar Levi18357852010-10-13 16:09:41 +0200311 /* MCS rates indication are on bits 16 - 23 */
312 rate_set >>= HW_HT_RATES_OFFSET - band->n_bitrates;
313
314 for (bit = 0; bit < 8; bit++) {
315 if (rate_set & 0x1)
316 enabled_rates |= (CONF_HW_BIT_RATE_MCS_0 << bit);
317 rate_set >>= 1;
318 }
319#endif
320
Juuso Oikarinen830fb672009-12-11 15:41:06 +0200321 return enabled_rates;
322}
323
Arik Nemtsova8c0ddb2011-02-23 00:22:26 +0200324void wl1271_handle_tx_low_watermark(struct wl1271 *wl)
Ido Yariv2fe33e82010-10-12 14:49:12 +0200325{
326 unsigned long flags;
327
328 if (test_bit(WL1271_FLAG_TX_QUEUE_STOPPED, &wl->flags) &&
Juuso Oikarinen6742f552010-12-13 09:52:37 +0200329 wl->tx_queue_count <= WL1271_TX_QUEUE_LOW_WATERMARK) {
Ido Yariv2fe33e82010-10-12 14:49:12 +0200330 /* firmware buffer has space, restart queues */
331 spin_lock_irqsave(&wl->wl_lock, flags);
332 ieee80211_wake_queues(wl->hw);
333 clear_bit(WL1271_FLAG_TX_QUEUE_STOPPED, &wl->flags);
334 spin_unlock_irqrestore(&wl->wl_lock, flags);
335 }
336}
337
Arik Nemtsova8c0ddb2011-02-23 00:22:26 +0200338static struct sk_buff *wl1271_sta_skb_dequeue(struct wl1271 *wl)
Juuso Oikarinen6742f552010-12-13 09:52:37 +0200339{
340 struct sk_buff *skb = NULL;
341 unsigned long flags;
342
343 skb = skb_dequeue(&wl->tx_queue[CONF_TX_AC_VO]);
344 if (skb)
345 goto out;
346 skb = skb_dequeue(&wl->tx_queue[CONF_TX_AC_VI]);
347 if (skb)
348 goto out;
349 skb = skb_dequeue(&wl->tx_queue[CONF_TX_AC_BE]);
350 if (skb)
351 goto out;
352 skb = skb_dequeue(&wl->tx_queue[CONF_TX_AC_BK]);
353
354out:
355 if (skb) {
356 spin_lock_irqsave(&wl->wl_lock, flags);
357 wl->tx_queue_count--;
358 spin_unlock_irqrestore(&wl->wl_lock, flags);
359 }
360
361 return skb;
362}
363
Arik Nemtsova8c0ddb2011-02-23 00:22:26 +0200364static struct sk_buff *wl1271_ap_skb_dequeue(struct wl1271 *wl)
365{
366 struct sk_buff *skb = NULL;
367 unsigned long flags;
368 int i, h, start_hlid;
369
370 /* start from the link after the last one */
371 start_hlid = (wl->last_tx_hlid + 1) % AP_MAX_LINKS;
372
373 /* dequeue according to AC, round robin on each link */
374 for (i = 0; i < AP_MAX_LINKS; i++) {
375 h = (start_hlid + i) % AP_MAX_LINKS;
376
377 skb = skb_dequeue(&wl->links[h].tx_queue[CONF_TX_AC_VO]);
378 if (skb)
379 goto out;
380 skb = skb_dequeue(&wl->links[h].tx_queue[CONF_TX_AC_VI]);
381 if (skb)
382 goto out;
383 skb = skb_dequeue(&wl->links[h].tx_queue[CONF_TX_AC_BE]);
384 if (skb)
385 goto out;
386 skb = skb_dequeue(&wl->links[h].tx_queue[CONF_TX_AC_BK]);
387 if (skb)
388 goto out;
389 }
390
391out:
392 if (skb) {
393 wl->last_tx_hlid = h;
394 spin_lock_irqsave(&wl->wl_lock, flags);
395 wl->tx_queue_count--;
396 spin_unlock_irqrestore(&wl->wl_lock, flags);
397 } else {
398 wl->last_tx_hlid = 0;
399 }
400
401 return skb;
402}
403
404static struct sk_buff *wl1271_skb_dequeue(struct wl1271 *wl)
405{
406 if (wl->bss_type == BSS_TYPE_AP_BSS)
407 return wl1271_ap_skb_dequeue(wl);
408
409 return wl1271_sta_skb_dequeue(wl);
410}
411
Juuso Oikarinen6742f552010-12-13 09:52:37 +0200412static void wl1271_skb_queue_head(struct wl1271 *wl, struct sk_buff *skb)
413{
414 unsigned long flags;
415 int q = wl1271_tx_get_queue(skb_get_queue_mapping(skb));
416
Arik Nemtsova8c0ddb2011-02-23 00:22:26 +0200417 if (wl->bss_type == BSS_TYPE_AP_BSS) {
418 u8 hlid = wl1271_tx_get_hlid(skb);
419 skb_queue_head(&wl->links[hlid].tx_queue[q], skb);
420
421 /* make sure we dequeue the same packet next time */
422 wl->last_tx_hlid = (hlid + AP_MAX_LINKS - 1) % AP_MAX_LINKS;
423 } else {
424 skb_queue_head(&wl->tx_queue[q], skb);
425 }
426
Juuso Oikarinen6742f552010-12-13 09:52:37 +0200427 spin_lock_irqsave(&wl->wl_lock, flags);
428 wl->tx_queue_count++;
429 spin_unlock_irqrestore(&wl->wl_lock, flags);
430}
431
Ido Yariva5225502010-10-12 14:49:10 +0200432void wl1271_tx_work_locked(struct wl1271 *wl)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300433{
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300434 struct sk_buff *skb;
435 bool woken_up = false;
Ido Yariv6c6e6692010-10-12 14:49:09 +0200436 u32 buf_offset = 0;
437 bool sent_packets = false;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300438 int ret;
439
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300440 if (unlikely(wl->state == WL1271_STATE_OFF))
441 goto out;
442
Juuso Oikarinen6742f552010-12-13 09:52:37 +0200443 while ((skb = wl1271_skb_dequeue(wl))) {
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300444 if (!woken_up) {
445 ret = wl1271_ps_elp_wakeup(wl, false);
446 if (ret < 0)
Juuso Oikarinenffb591c2010-02-22 08:38:31 +0200447 goto out_ack;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300448 woken_up = true;
449 }
450
Ido Yariva19606b2010-09-30 13:28:28 +0200451 ret = wl1271_prepare_tx_frame(wl, skb, buf_offset);
Ido Yariv6c6e6692010-10-12 14:49:09 +0200452 if (ret == -EAGAIN) {
Ido Yariva19606b2010-09-30 13:28:28 +0200453 /*
Ido Yariv6c6e6692010-10-12 14:49:09 +0200454 * Aggregation buffer is full.
455 * Flush buffer and try again.
456 */
Juuso Oikarinen6742f552010-12-13 09:52:37 +0200457 wl1271_skb_queue_head(wl, skb);
Ido Yariv6c6e6692010-10-12 14:49:09 +0200458 wl1271_write(wl, WL1271_SLV_MEM_DATA, wl->aggr_buf,
Juuso Oikarinen6742f552010-12-13 09:52:37 +0200459 buf_offset, true);
Ido Yariv6c6e6692010-10-12 14:49:09 +0200460 sent_packets = true;
461 buf_offset = 0;
462 continue;
463 } else if (ret == -EBUSY) {
464 /*
465 * Firmware buffer is full.
Ido Yariva19606b2010-09-30 13:28:28 +0200466 * Queue back last skb, and stop aggregating.
467 */
Juuso Oikarinen6742f552010-12-13 09:52:37 +0200468 wl1271_skb_queue_head(wl, skb);
Ido Yariva5225502010-10-12 14:49:10 +0200469 /* No work left, avoid scheduling redundant tx work */
470 set_bit(WL1271_FLAG_FW_TX_BUSY, &wl->flags);
Juuso Oikarinenffb591c2010-02-22 08:38:31 +0200471 goto out_ack;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300472 } else if (ret < 0) {
473 dev_kfree_skb(skb);
Juuso Oikarinenffb591c2010-02-22 08:38:31 +0200474 goto out_ack;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300475 }
Ido Yariva19606b2010-09-30 13:28:28 +0200476 buf_offset += ret;
477 wl->tx_packets_count++;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300478 }
479
Juuso Oikarinenffb591c2010-02-22 08:38:31 +0200480out_ack:
Ido Yariva19606b2010-09-30 13:28:28 +0200481 if (buf_offset) {
482 wl1271_write(wl, WL1271_SLV_MEM_DATA, wl->aggr_buf,
483 buf_offset, true);
Ido Yariv6c6e6692010-10-12 14:49:09 +0200484 sent_packets = true;
485 }
486 if (sent_packets) {
Ido Yariva19606b2010-09-30 13:28:28 +0200487 /* interrupt the firmware with the new packets */
Juuso Oikarinenffb591c2010-02-22 08:38:31 +0200488 wl1271_write32(wl, WL1271_HOST_WR_ACCESS, wl->tx_packets_count);
Arik Nemtsova8c0ddb2011-02-23 00:22:26 +0200489 wl1271_handle_tx_low_watermark(wl);
Ido Yariva19606b2010-09-30 13:28:28 +0200490 }
Juuso Oikarinenffb591c2010-02-22 08:38:31 +0200491
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300492out:
493 if (woken_up)
494 wl1271_ps_elp_sleep(wl);
Ido Yariva5225502010-10-12 14:49:10 +0200495}
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300496
Ido Yariva5225502010-10-12 14:49:10 +0200497void wl1271_tx_work(struct work_struct *work)
498{
499 struct wl1271 *wl = container_of(work, struct wl1271, tx_work);
500
501 mutex_lock(&wl->mutex);
502 wl1271_tx_work_locked(wl);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300503 mutex_unlock(&wl->mutex);
504}
505
506static void wl1271_tx_complete_packet(struct wl1271 *wl,
507 struct wl1271_tx_hw_res_descr *result)
508{
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300509 struct ieee80211_tx_info *info;
510 struct sk_buff *skb;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300511 int id = result->id;
Juuso Oikarinen31627dc2010-03-26 12:53:12 +0200512 int rate = -1;
513 u8 retries = 0;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300514
515 /* check for id legality */
Juuso Oikarinenffb591c2010-02-22 08:38:31 +0200516 if (unlikely(id >= ACX_TX_DESCRIPTORS || wl->tx_frames[id] == NULL)) {
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300517 wl1271_warning("TX result illegal id: %d", id);
518 return;
519 }
520
521 skb = wl->tx_frames[id];
522 info = IEEE80211_SKB_CB(skb);
523
Juuso Oikarinen31627dc2010-03-26 12:53:12 +0200524 /* update the TX status info */
525 if (result->status == TX_SUCCESS) {
526 if (!(info->flags & IEEE80211_TX_CTL_NO_ACK))
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300527 info->flags |= IEEE80211_TX_STAT_ACK;
Teemu Paasikivi6a2de932010-10-14 11:00:04 +0200528 rate = wl1271_rate_to_idx(result->rate_class_index, wl->band);
Juuso Oikarinen31627dc2010-03-26 12:53:12 +0200529 retries = result->ack_failures;
530 } else if (result->status == TX_RETRY_EXCEEDED) {
531 wl->stats.excessive_retries++;
532 retries = result->ack_failures;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300533 }
534
Juuso Oikarinen31627dc2010-03-26 12:53:12 +0200535 info->status.rates[0].idx = rate;
536 info->status.rates[0].count = retries;
537 info->status.rates[0].flags = 0;
538 info->status.ack_signal = -1;
539
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300540 wl->stats.retry_count += result->ack_failures;
541
Juuso Oikarinenac4e4ce2009-10-08 21:56:19 +0300542 /* update security sequence number */
Juuso Oikarinen04e36fc2010-02-22 08:38:40 +0200543 wl->tx_security_seq += (result->lsb_security_sequence_number -
544 wl->tx_security_last_seq);
Juuso Oikarinenac4e4ce2009-10-08 21:56:19 +0300545 wl->tx_security_last_seq = result->lsb_security_sequence_number;
546
Juuso Oikarinen1e2b7972009-10-08 21:56:20 +0300547 /* remove private header from packet */
548 skb_pull(skb, sizeof(struct wl1271_tx_hw_descr));
549
550 /* remove TKIP header space if present */
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300551 if (info->control.hw_key &&
Johannes Berg97359d12010-08-10 09:46:38 +0200552 info->control.hw_key->cipher == WLAN_CIPHER_SUITE_TKIP) {
Juuso Oikarinen1e2b7972009-10-08 21:56:20 +0300553 int hdrlen = ieee80211_get_hdrlen_from_skb(skb);
554 memmove(skb->data + WL1271_TKIP_IV_SPACE, skb->data, hdrlen);
555 skb_pull(skb, WL1271_TKIP_IV_SPACE);
556 }
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300557
558 wl1271_debug(DEBUG_TX, "tx status id %u skb 0x%p failures %u rate 0x%x"
559 " status 0x%x",
560 result->id, skb, result->ack_failures,
561 result->rate_class_index, result->status);
562
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300563 /* return the packet to the stack */
564 ieee80211_tx_status(wl->hw, skb);
Ido Yariv25eeb9e2010-10-12 16:20:06 +0200565 wl1271_free_tx_id(wl, result->id);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300566}
567
568/* Called upon reception of a TX complete interrupt */
Juuso Oikarinenffb591c2010-02-22 08:38:31 +0200569void wl1271_tx_complete(struct wl1271 *wl)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300570{
571 struct wl1271_acx_mem_map *memmap =
572 (struct wl1271_acx_mem_map *)wl->target_mem_map;
Juuso Oikarinenffb591c2010-02-22 08:38:31 +0200573 u32 count, fw_counter;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300574 u32 i;
575
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300576 /* read the tx results from the chipset */
Teemu Paasikivi7b048c52010-02-18 13:25:55 +0200577 wl1271_read(wl, le32_to_cpu(memmap->tx_result),
578 wl->tx_res_if, sizeof(*wl->tx_res_if), false);
Juuso Oikarinenffb591c2010-02-22 08:38:31 +0200579 fw_counter = le32_to_cpu(wl->tx_res_if->tx_result_fw_counter);
580
581 /* write host counter to chipset (to ack) */
582 wl1271_write32(wl, le32_to_cpu(memmap->tx_result) +
583 offsetof(struct wl1271_tx_hw_res_if,
584 tx_result_host_counter), fw_counter);
585
586 count = fw_counter - wl->tx_results_count;
Juuso Oikarinen06f7bc72010-02-22 08:38:33 +0200587 wl1271_debug(DEBUG_TX, "tx_complete received, packets: %d", count);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300588
589 /* verify that the result buffer is not getting overrun */
Juuso Oikarinenffb591c2010-02-22 08:38:31 +0200590 if (unlikely(count > TX_HW_RESULT_QUEUE_LEN))
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300591 wl1271_warning("TX result overflow from chipset: %d", count);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300592
593 /* process the results */
594 for (i = 0; i < count; i++) {
595 struct wl1271_tx_hw_res_descr *result;
596 u8 offset = wl->tx_results_count & TX_HW_RESULT_QUEUE_LEN_MASK;
597
598 /* process the packet */
599 result = &(wl->tx_res_if->tx_results_queue[offset]);
600 wl1271_tx_complete_packet(wl, result);
601
602 wl->tx_results_count++;
603 }
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300604}
605
Arik Nemtsova8c0ddb2011-02-23 00:22:26 +0200606void wl1271_tx_reset_link_queues(struct wl1271 *wl, u8 hlid)
607{
608 struct sk_buff *skb;
609 int i, total = 0;
610 unsigned long flags;
Arik Nemtsov1d36cd82011-02-23 00:22:27 +0200611 struct ieee80211_tx_info *info;
Arik Nemtsova8c0ddb2011-02-23 00:22:26 +0200612
613 for (i = 0; i < NUM_TX_QUEUES; i++) {
614 while ((skb = skb_dequeue(&wl->links[hlid].tx_queue[i]))) {
615 wl1271_debug(DEBUG_TX, "link freeing skb 0x%p", skb);
Arik Nemtsov1d36cd82011-02-23 00:22:27 +0200616 info = IEEE80211_SKB_CB(skb);
617 info->status.rates[0].idx = -1;
618 info->status.rates[0].count = 0;
Arik Nemtsova8c0ddb2011-02-23 00:22:26 +0200619 ieee80211_tx_status(wl->hw, skb);
620 total++;
621 }
622 }
623
624 spin_lock_irqsave(&wl->wl_lock, flags);
625 wl->tx_queue_count -= total;
626 spin_unlock_irqrestore(&wl->wl_lock, flags);
627
628 wl1271_handle_tx_low_watermark(wl);
629}
630
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300631/* caller must hold wl->mutex */
Juuso Oikarinen781608c2010-05-24 11:18:17 +0300632void wl1271_tx_reset(struct wl1271 *wl)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300633{
634 int i;
635 struct sk_buff *skb;
Arik Nemtsov1d36cd82011-02-23 00:22:27 +0200636 struct ieee80211_tx_info *info;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300637
638 /* TX failure */
Arik Nemtsova8c0ddb2011-02-23 00:22:26 +0200639 if (wl->bss_type == BSS_TYPE_AP_BSS) {
Arik Nemtsov09039f42011-02-23 00:22:30 +0200640 for (i = 0; i < AP_MAX_LINKS; i++) {
Arik Nemtsova8c0ddb2011-02-23 00:22:26 +0200641 wl1271_tx_reset_link_queues(wl, i);
Arik Nemtsov09039f42011-02-23 00:22:30 +0200642 wl->links[i].allocated_blks = 0;
643 wl->links[i].prev_freed_blks = 0;
644 }
Arik Nemtsova8c0ddb2011-02-23 00:22:26 +0200645
646 wl->last_tx_hlid = 0;
647 } else {
648 for (i = 0; i < NUM_TX_QUEUES; i++) {
649 while ((skb = skb_dequeue(&wl->tx_queue[i]))) {
650 wl1271_debug(DEBUG_TX, "freeing skb 0x%p",
651 skb);
Arik Nemtsov1d36cd82011-02-23 00:22:27 +0200652 info = IEEE80211_SKB_CB(skb);
653 info->status.rates[0].idx = -1;
654 info->status.rates[0].count = 0;
Arik Nemtsova8c0ddb2011-02-23 00:22:26 +0200655 ieee80211_tx_status(wl->hw, skb);
656 }
Juuso Oikarinen6742f552010-12-13 09:52:37 +0200657 }
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300658 }
Arik Nemtsova8c0ddb2011-02-23 00:22:26 +0200659
Juuso Oikarinen6742f552010-12-13 09:52:37 +0200660 wl->tx_queue_count = 0;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300661
Ido Yariv2fe33e82010-10-12 14:49:12 +0200662 /*
663 * Make sure the driver is at a consistent state, in case this
664 * function is called from a context other than interface removal.
665 */
Arik Nemtsova8c0ddb2011-02-23 00:22:26 +0200666 wl1271_handle_tx_low_watermark(wl);
Ido Yariv2fe33e82010-10-12 14:49:12 +0200667
Juuso Oikarinenbe7078c2009-10-08 21:56:26 +0300668 for (i = 0; i < ACX_TX_DESCRIPTORS; i++)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300669 if (wl->tx_frames[i] != NULL) {
670 skb = wl->tx_frames[i];
Ido Yariv25eeb9e2010-10-12 16:20:06 +0200671 wl1271_free_tx_id(wl, i);
Juuso Oikarinen781608c2010-05-24 11:18:17 +0300672 wl1271_debug(DEBUG_TX, "freeing skb 0x%p", skb);
Arik Nemtsov1d36cd82011-02-23 00:22:27 +0200673 info = IEEE80211_SKB_CB(skb);
674 info->status.rates[0].idx = -1;
675 info->status.rates[0].count = 0;
Juuso Oikarinen6bbe89d2010-04-01 11:38:24 +0300676 ieee80211_tx_status(wl->hw, skb);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300677 }
Juuso Oikarinen781608c2010-05-24 11:18:17 +0300678}
679
680#define WL1271_TX_FLUSH_TIMEOUT 500000
681
682/* caller must *NOT* hold wl->mutex */
683void wl1271_tx_flush(struct wl1271 *wl)
684{
685 unsigned long timeout;
686 timeout = jiffies + usecs_to_jiffies(WL1271_TX_FLUSH_TIMEOUT);
687
688 while (!time_after(jiffies, timeout)) {
689 mutex_lock(&wl->mutex);
Arik Nemtsova8c0ddb2011-02-23 00:22:26 +0200690 wl1271_debug(DEBUG_TX, "flushing tx buffer: %d %d",
691 wl->tx_frames_cnt, wl->tx_queue_count);
Juuso Oikarinen6742f552010-12-13 09:52:37 +0200692 if ((wl->tx_frames_cnt == 0) && (wl->tx_queue_count == 0)) {
Juuso Oikarinen781608c2010-05-24 11:18:17 +0300693 mutex_unlock(&wl->mutex);
694 return;
695 }
696 mutex_unlock(&wl->mutex);
697 msleep(1);
698 }
699
700 wl1271_warning("Unable to flush all TX buffers, timed out.");
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300701}
Arik Nemtsove0fe3712010-10-16 18:19:53 +0200702
703u32 wl1271_tx_min_rate_get(struct wl1271 *wl)
704{
705 int i;
706 u32 rate = 0;
707
708 if (!wl->basic_rate_set) {
709 WARN_ON(1);
710 wl->basic_rate_set = wl->conf.tx.basic_rate;
711 }
712
713 for (i = 0; !rate; i++) {
714 if ((wl->basic_rate_set >> i) & 0x1)
715 rate = 1 << i;
716 }
717
718 return rate;
719}