blob: ba69ba7051fae6919b31a66fa5893c309e09dbe3 [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
Ohad Ben-Cohenc5745182011-03-30 16:35:59 +020073static int wl1271_tx_update_filters(struct wl1271 *wl,
74 struct sk_buff *skb)
75{
76 struct ieee80211_hdr *hdr;
77
78 hdr = (struct ieee80211_hdr *)(skb->data +
79 sizeof(struct wl1271_tx_hw_descr));
80
81 /*
82 * stop bssid-based filtering before transmitting authentication
83 * requests. this way the hw will never drop authentication
84 * responses coming from BSSIDs it isn't familiar with (e.g. on
85 * roaming)
86 */
87 if (!ieee80211_is_auth(hdr->frame_control))
88 return 0;
89
90 wl1271_configure_filters(wl, FIF_OTHER_BSS);
91
92 return wl1271_acx_rx_config(wl, wl->rx_config, wl->rx_filter);
93}
94
Arik Nemtsov99a27752011-02-23 00:22:25 +020095static void wl1271_tx_ap_update_inconnection_sta(struct wl1271 *wl,
96 struct sk_buff *skb)
97{
98 struct ieee80211_hdr *hdr;
99
100 /*
101 * add the station to the known list before transmitting the
102 * authentication response. this way it won't get de-authed by FW
103 * when transmitting too soon.
104 */
105 hdr = (struct ieee80211_hdr *)(skb->data +
106 sizeof(struct wl1271_tx_hw_descr));
107 if (ieee80211_is_auth(hdr->frame_control))
108 wl1271_acx_set_inconnection_sta(wl, hdr->addr1);
109}
110
Arik Nemtsovb622d992011-02-23 00:22:31 +0200111static void wl1271_tx_regulate_link(struct wl1271 *wl, u8 hlid)
112{
113 bool fw_ps;
114 u8 tx_blks;
115
116 /* only regulate station links */
117 if (hlid < WL1271_AP_STA_HLID_START)
118 return;
119
120 fw_ps = test_bit(hlid, (unsigned long *)&wl->ap_fw_ps_map);
121 tx_blks = wl->links[hlid].allocated_blks;
122
123 /*
124 * if in FW PS and there is enough data in FW we can put the link
125 * into high-level PS and clean out its TX queues.
126 */
127 if (fw_ps && tx_blks >= WL1271_PS_STA_MAX_BLOCKS)
128 wl1271_ps_link_start(wl, hlid, true);
129}
130
Arik Nemtsova8c0ddb2011-02-23 00:22:26 +0200131u8 wl1271_tx_get_hlid(struct sk_buff *skb)
132{
133 struct ieee80211_tx_info *control = IEEE80211_SKB_CB(skb);
134
135 if (control->control.sta) {
136 struct wl1271_station *wl_sta;
137
138 wl_sta = (struct wl1271_station *)
139 control->control.sta->drv_priv;
140 return wl_sta->hlid;
141 } else {
142 struct ieee80211_hdr *hdr;
143
144 hdr = (struct ieee80211_hdr *)skb->data;
145 if (ieee80211_is_mgmt(hdr->frame_control))
146 return WL1271_AP_GLOBAL_HLID;
147 else
148 return WL1271_AP_BROADCAST_HLID;
149 }
150}
151
Ido Yariv0da13da2011-03-31 10:06:58 +0200152static unsigned int wl12xx_calc_packet_alignment(struct wl1271 *wl,
153 unsigned int packet_length)
154{
155 if (wl->quirks & WL12XX_QUIRK_BLOCKSIZE_ALIGNMENT)
156 return ALIGN(packet_length, WL12XX_BUS_BLOCK_SIZE);
157 else
158 return ALIGN(packet_length, WL1271_TX_ALIGN_TO);
159}
160
Ido Yariva19606b2010-09-30 13:28:28 +0200161static int wl1271_tx_allocate(struct wl1271 *wl, struct sk_buff *skb, u32 extra,
Arik Nemtsov09039f42011-02-23 00:22:30 +0200162 u32 buf_offset, u8 hlid)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300163{
164 struct wl1271_tx_hw_descr *desc;
165 u32 total_len = skb->len + sizeof(struct wl1271_tx_hw_descr) + extra;
Shahar Levi48a61472011-03-06 16:32:08 +0200166 u32 len;
Juuso Oikarinen5c9417f2010-02-22 08:38:39 +0200167 u32 total_blocks;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300168 int id, ret = -EBUSY;
Luciano Coelhoe7ddf542011-03-10 15:24:57 +0200169 u32 spare_blocks;
170
171 if (unlikely(wl->quirks & WL12XX_QUIRK_USE_2_SPARE_BLOCKS))
172 spare_blocks = 2;
173 else
174 spare_blocks = 1;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300175
Ido Yariva19606b2010-09-30 13:28:28 +0200176 if (buf_offset + total_len > WL1271_AGGR_BUFFER_SIZE)
Ido Yariv6c6e6692010-10-12 14:49:09 +0200177 return -EAGAIN;
Ido Yariva19606b2010-09-30 13:28:28 +0200178
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300179 /* allocate free identifier for the packet */
Ido Yariv25eeb9e2010-10-12 16:20:06 +0200180 id = wl1271_alloc_tx_id(wl, skb);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300181 if (id < 0)
182 return id;
183
184 /* approximate the number of blocks required for this packet
185 in the firmware */
Ido Yariv0da13da2011-03-31 10:06:58 +0200186 len = wl12xx_calc_packet_alignment(wl, total_len);
Shahar Levi48a61472011-03-06 16:32:08 +0200187
188 total_blocks = (len + TX_HW_BLOCK_SIZE - 1) / TX_HW_BLOCK_SIZE +
Luciano Coelhoe7ddf542011-03-10 15:24:57 +0200189 spare_blocks;
Shahar Levi48a61472011-03-06 16:32:08 +0200190
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300191 if (total_blocks <= wl->tx_blocks_available) {
192 desc = (struct wl1271_tx_hw_descr *)skb_push(
193 skb, total_len - skb->len);
194
Shahar Leviae77ecc2011-03-06 16:32:13 +0200195 /* HW descriptor fields change between wl127x and wl128x */
196 if (wl->chip.id == CHIP_ID_1283_PG20) {
197 desc->wl128x_mem.total_mem_blocks = total_blocks;
198 } else {
Luciano Coelhoe7ddf542011-03-10 15:24:57 +0200199 desc->wl127x_mem.extra_blocks = spare_blocks;
Shahar Leviae77ecc2011-03-06 16:32:13 +0200200 desc->wl127x_mem.total_mem_blocks = total_blocks;
201 }
202
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300203 desc->id = id;
204
205 wl->tx_blocks_available -= total_blocks;
206
Arik Nemtsov09039f42011-02-23 00:22:30 +0200207 if (wl->bss_type == BSS_TYPE_AP_BSS)
208 wl->links[hlid].allocated_blks += total_blocks;
209
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300210 ret = 0;
211
212 wl1271_debug(DEBUG_TX,
213 "tx_allocate: size: %d, blocks: %d, id: %d",
214 total_len, total_blocks, id);
Juuso Oikarinen781608c2010-05-24 11:18:17 +0300215 } else {
Ido Yariv25eeb9e2010-10-12 16:20:06 +0200216 wl1271_free_tx_id(wl, id);
Juuso Oikarinen781608c2010-05-24 11:18:17 +0300217 }
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300218
219 return ret;
220}
221
Ido Yariva19606b2010-09-30 13:28:28 +0200222static void wl1271_tx_fill_hdr(struct wl1271 *wl, struct sk_buff *skb,
Arik Nemtsov09039f42011-02-23 00:22:30 +0200223 u32 extra, struct ieee80211_tx_info *control,
224 u8 hlid)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300225{
Juuso Oikarinenac5e1e32010-02-22 08:38:38 +0200226 struct timespec ts;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300227 struct wl1271_tx_hw_descr *desc;
Shahar Levi48a61472011-03-06 16:32:08 +0200228 int aligned_len, ac, rate_idx;
Juuso Oikarinenac5e1e32010-02-22 08:38:38 +0200229 s64 hosttime;
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300230 u16 tx_attr;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300231
232 desc = (struct wl1271_tx_hw_descr *) skb->data;
233
Juuso Oikarinen1e2b7972009-10-08 21:56:20 +0300234 /* relocate space for security header */
235 if (extra) {
236 void *framestart = skb->data + sizeof(*desc);
237 u16 fc = *(u16 *)(framestart + extra);
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300238 int hdrlen = ieee80211_hdrlen(cpu_to_le16(fc));
Juuso Oikarinen1e2b7972009-10-08 21:56:20 +0300239 memmove(framestart, framestart + extra, hdrlen);
240 }
241
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300242 /* configure packet life time */
Juuso Oikarinenac5e1e32010-02-22 08:38:38 +0200243 getnstimeofday(&ts);
244 hosttime = (timespec_to_ns(&ts) >> 10);
245 desc->start_time = cpu_to_le32(hosttime - wl->time_offset);
Arik Nemtsovc6c8a652010-10-16 20:27:53 +0200246
247 if (wl->bss_type != BSS_TYPE_AP_BSS)
248 desc->life_time = cpu_to_le16(TX_HW_MGMT_PKT_LIFETIME_TU);
249 else
250 desc->life_time = cpu_to_le16(TX_HW_AP_MODE_PKT_LIFETIME_TU);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300251
Eliad Pellerdb674d22011-03-16 23:03:54 +0200252 /* queue */
Kalle Valoc6999d82010-02-18 13:25:41 +0200253 ac = wl1271_tx_get_queue(skb_get_queue_mapping(skb));
Eliad Pellerdb674d22011-03-16 23:03:54 +0200254 desc->tid = skb->priority;
Arik Nemtsovc6c8a652010-10-16 20:27:53 +0200255
Shahar Leviae47c452011-03-06 16:32:14 +0200256 if (skb->pkt_type == TX_PKT_TYPE_DUMMY_REQ) {
257 /*
258 * FW expects the dummy packet to have an invalid session id -
259 * any session id that is different than the one set in the join
260 */
261 tx_attr = ((~wl->session_counter) <<
262 TX_HW_ATTR_OFST_SESSION_COUNTER) &
263 TX_HW_ATTR_SESSION_COUNTER;
264
265 tx_attr |= TX_HW_ATTR_TX_DUMMY_REQ;
Shahar Leviae47c452011-03-06 16:32:14 +0200266 } else {
267 /* configure the tx attributes */
268 tx_attr =
269 wl->session_counter << TX_HW_ATTR_OFST_SESSION_COUNTER;
270 }
271
Arik Nemtsovc6c8a652010-10-16 20:27:53 +0200272 if (wl->bss_type != BSS_TYPE_AP_BSS) {
Arik Nemtsov09039f42011-02-23 00:22:30 +0200273 desc->aid = hlid;
Arik Nemtsovc6c8a652010-10-16 20:27:53 +0200274
275 /* if the packets are destined for AP (have a STA entry)
276 send them with AP rate policies, otherwise use default
277 basic rates */
278 if (control->control.sta)
279 rate_idx = ACX_TX_AP_FULL_RATE;
280 else
281 rate_idx = ACX_TX_BASIC_RATE;
282 } else {
Arik Nemtsov09039f42011-02-23 00:22:30 +0200283 desc->hlid = hlid;
284 switch (hlid) {
285 case WL1271_AP_GLOBAL_HLID:
286 rate_idx = ACX_TX_AP_MODE_MGMT_RATE;
287 break;
288 case WL1271_AP_BROADCAST_HLID:
289 rate_idx = ACX_TX_AP_MODE_BCST_RATE;
290 break;
291 default:
Arik Nemtsovc6c8a652010-10-16 20:27:53 +0200292 rate_idx = ac;
Arik Nemtsov09039f42011-02-23 00:22:30 +0200293 break;
Arik Nemtsovc6c8a652010-10-16 20:27:53 +0200294 }
295 }
296
297 tx_attr |= rate_idx << TX_HW_ATTR_OFST_RATE_POLICY;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300298 desc->reserved = 0;
299
Ido Yariv0da13da2011-03-31 10:06:58 +0200300 aligned_len = wl12xx_calc_packet_alignment(wl, skb->len);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300301
Ido Yariv0da13da2011-03-31 10:06:58 +0200302 if (wl->chip.id == CHIP_ID_1283_PG20) {
Shahar Levi48a61472011-03-06 16:32:08 +0200303 desc->wl128x_mem.extra_bytes = aligned_len - skb->len;
304 desc->length = cpu_to_le16(aligned_len >> 2);
Shahar Leviae77ecc2011-03-06 16:32:13 +0200305
306 wl1271_debug(DEBUG_TX, "tx_fill_hdr: hlid: %d "
307 "tx_attr: 0x%x len: %d life: %d mem: %d",
308 desc->hlid, tx_attr,
309 le16_to_cpu(desc->length),
310 le16_to_cpu(desc->life_time),
311 desc->wl128x_mem.total_mem_blocks);
Shahar Levi48a61472011-03-06 16:32:08 +0200312 } else {
313 int pad;
314
Ido Yariv0da13da2011-03-31 10:06:58 +0200315 /* Store the aligned length in terms of words */
Shahar Levi48a61472011-03-06 16:32:08 +0200316 desc->length = cpu_to_le16(aligned_len >> 2);
317
318 /* calculate number of padding bytes */
319 pad = aligned_len - skb->len;
320 tx_attr |= pad << TX_HW_ATTR_OFST_LAST_WORD_PAD;
321
Shahar Leviae77ecc2011-03-06 16:32:13 +0200322 wl1271_debug(DEBUG_TX, "tx_fill_hdr: pad: %d hlid: %d "
323 "tx_attr: 0x%x len: %d life: %d mem: %d", pad,
324 desc->hlid, tx_attr,
325 le16_to_cpu(desc->length),
326 le16_to_cpu(desc->life_time),
327 desc->wl127x_mem.total_mem_blocks);
Shahar Levi48a61472011-03-06 16:32:08 +0200328 }
Luciano Coelhod0f63b22009-10-15 10:33:29 +0300329
330 desc->tx_attr = cpu_to_le16(tx_attr);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300331}
332
333/* caller must hold wl->mutex */
Ido Yariva19606b2010-09-30 13:28:28 +0200334static int wl1271_prepare_tx_frame(struct wl1271 *wl, struct sk_buff *skb,
335 u32 buf_offset)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300336{
337 struct ieee80211_tx_info *info;
338 u32 extra = 0;
339 int ret = 0;
Ido Yariva19606b2010-09-30 13:28:28 +0200340 u32 total_len;
Arik Nemtsov09039f42011-02-23 00:22:30 +0200341 u8 hlid;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300342
343 if (!skb)
344 return -EINVAL;
345
346 info = IEEE80211_SKB_CB(skb);
347
348 if (info->control.hw_key &&
Johannes Berg97359d12010-08-10 09:46:38 +0200349 info->control.hw_key->cipher == WLAN_CIPHER_SUITE_TKIP)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300350 extra = WL1271_TKIP_IV_SPACE;
351
352 if (info->control.hw_key) {
Arik Nemtsov7f179b42010-10-16 21:39:06 +0200353 bool is_wep;
354 u8 idx = info->control.hw_key->hw_key_idx;
355 u32 cipher = info->control.hw_key->cipher;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300356
Arik Nemtsov7f179b42010-10-16 21:39:06 +0200357 is_wep = (cipher == WLAN_CIPHER_SUITE_WEP40) ||
358 (cipher == WLAN_CIPHER_SUITE_WEP104);
359
360 if (unlikely(is_wep && wl->default_key != idx)) {
361 ret = wl1271_set_default_wep_key(wl, idx);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300362 if (ret < 0)
363 return ret;
Juuso Oikarinenee444cf2010-02-18 13:25:50 +0200364 wl->default_key = idx;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300365 }
366 }
367
Arik Nemtsov09039f42011-02-23 00:22:30 +0200368 if (wl->bss_type == BSS_TYPE_AP_BSS)
369 hlid = wl1271_tx_get_hlid(skb);
370 else
371 hlid = TX_HW_DEFAULT_AID;
372
373 ret = wl1271_tx_allocate(wl, skb, extra, buf_offset, hlid);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300374 if (ret < 0)
375 return ret;
376
Arik Nemtsovb622d992011-02-23 00:22:31 +0200377 if (wl->bss_type == BSS_TYPE_AP_BSS) {
Arik Nemtsov99a27752011-02-23 00:22:25 +0200378 wl1271_tx_ap_update_inconnection_sta(wl, skb);
Arik Nemtsovb622d992011-02-23 00:22:31 +0200379 wl1271_tx_regulate_link(wl, hlid);
Ohad Ben-Cohenc5745182011-03-30 16:35:59 +0200380 } else {
381 wl1271_tx_update_filters(wl, skb);
Arik Nemtsovb622d992011-02-23 00:22:31 +0200382 }
Arik Nemtsov99a27752011-02-23 00:22:25 +0200383
Arik Nemtsov09039f42011-02-23 00:22:30 +0200384 wl1271_tx_fill_hdr(wl, skb, extra, info, hlid);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300385
Ido Yariva19606b2010-09-30 13:28:28 +0200386 /*
Shahar Levi48a61472011-03-06 16:32:08 +0200387 * The length of each packet is stored in terms of
388 * words. Thus, we must pad the skb data to make sure its
389 * length is aligned. The number of padding bytes is computed
390 * and set in wl1271_tx_fill_hdr.
391 * In special cases, we want to align to a specific block size
392 * (eg. for wl128x with SDIO we align to 256).
Ido Yariva19606b2010-09-30 13:28:28 +0200393 */
Ido Yariv0da13da2011-03-31 10:06:58 +0200394 total_len = wl12xx_calc_packet_alignment(wl, skb->len);
Shahar Levi48a61472011-03-06 16:32:08 +0200395
Ido Yariva19606b2010-09-30 13:28:28 +0200396 memcpy(wl->aggr_buf + buf_offset, skb->data, skb->len);
397 memset(wl->aggr_buf + buf_offset + skb->len, 0, total_len - skb->len);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300398
Ido Yariva19606b2010-09-30 13:28:28 +0200399 return total_len;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300400}
401
Juuso Oikarinenebba60c2010-04-01 11:38:20 +0300402u32 wl1271_tx_enabled_rates_get(struct wl1271 *wl, u32 rate_set)
Juuso Oikarinen830fb672009-12-11 15:41:06 +0200403{
404 struct ieee80211_supported_band *band;
405 u32 enabled_rates = 0;
406 int bit;
407
408 band = wl->hw->wiphy->bands[wl->band];
409 for (bit = 0; bit < band->n_bitrates; bit++) {
410 if (rate_set & 0x1)
411 enabled_rates |= band->bitrates[bit].hw_value;
412 rate_set >>= 1;
413 }
414
Shahar Levi00d20102010-11-08 11:20:10 +0000415#ifdef CONFIG_WL12XX_HT
Shahar Levi18357852010-10-13 16:09:41 +0200416 /* MCS rates indication are on bits 16 - 23 */
417 rate_set >>= HW_HT_RATES_OFFSET - band->n_bitrates;
418
419 for (bit = 0; bit < 8; bit++) {
420 if (rate_set & 0x1)
421 enabled_rates |= (CONF_HW_BIT_RATE_MCS_0 << bit);
422 rate_set >>= 1;
423 }
424#endif
425
Juuso Oikarinen830fb672009-12-11 15:41:06 +0200426 return enabled_rates;
427}
428
Arik Nemtsova8c0ddb2011-02-23 00:22:26 +0200429void wl1271_handle_tx_low_watermark(struct wl1271 *wl)
Ido Yariv2fe33e82010-10-12 14:49:12 +0200430{
431 unsigned long flags;
432
433 if (test_bit(WL1271_FLAG_TX_QUEUE_STOPPED, &wl->flags) &&
Juuso Oikarinen6742f552010-12-13 09:52:37 +0200434 wl->tx_queue_count <= WL1271_TX_QUEUE_LOW_WATERMARK) {
Ido Yariv2fe33e82010-10-12 14:49:12 +0200435 /* firmware buffer has space, restart queues */
436 spin_lock_irqsave(&wl->wl_lock, flags);
437 ieee80211_wake_queues(wl->hw);
438 clear_bit(WL1271_FLAG_TX_QUEUE_STOPPED, &wl->flags);
439 spin_unlock_irqrestore(&wl->wl_lock, flags);
440 }
441}
442
Arik Nemtsova8c0ddb2011-02-23 00:22:26 +0200443static struct sk_buff *wl1271_sta_skb_dequeue(struct wl1271 *wl)
Juuso Oikarinen6742f552010-12-13 09:52:37 +0200444{
445 struct sk_buff *skb = NULL;
446 unsigned long flags;
447
448 skb = skb_dequeue(&wl->tx_queue[CONF_TX_AC_VO]);
449 if (skb)
450 goto out;
451 skb = skb_dequeue(&wl->tx_queue[CONF_TX_AC_VI]);
452 if (skb)
453 goto out;
454 skb = skb_dequeue(&wl->tx_queue[CONF_TX_AC_BE]);
455 if (skb)
456 goto out;
457 skb = skb_dequeue(&wl->tx_queue[CONF_TX_AC_BK]);
458
459out:
460 if (skb) {
461 spin_lock_irqsave(&wl->wl_lock, flags);
462 wl->tx_queue_count--;
463 spin_unlock_irqrestore(&wl->wl_lock, flags);
464 }
465
466 return skb;
467}
468
Arik Nemtsova8c0ddb2011-02-23 00:22:26 +0200469static struct sk_buff *wl1271_ap_skb_dequeue(struct wl1271 *wl)
470{
471 struct sk_buff *skb = NULL;
472 unsigned long flags;
473 int i, h, start_hlid;
474
475 /* start from the link after the last one */
476 start_hlid = (wl->last_tx_hlid + 1) % AP_MAX_LINKS;
477
478 /* dequeue according to AC, round robin on each link */
479 for (i = 0; i < AP_MAX_LINKS; i++) {
480 h = (start_hlid + i) % AP_MAX_LINKS;
481
482 skb = skb_dequeue(&wl->links[h].tx_queue[CONF_TX_AC_VO]);
483 if (skb)
484 goto out;
485 skb = skb_dequeue(&wl->links[h].tx_queue[CONF_TX_AC_VI]);
486 if (skb)
487 goto out;
488 skb = skb_dequeue(&wl->links[h].tx_queue[CONF_TX_AC_BE]);
489 if (skb)
490 goto out;
491 skb = skb_dequeue(&wl->links[h].tx_queue[CONF_TX_AC_BK]);
492 if (skb)
493 goto out;
494 }
495
496out:
497 if (skb) {
498 wl->last_tx_hlid = h;
499 spin_lock_irqsave(&wl->wl_lock, flags);
500 wl->tx_queue_count--;
501 spin_unlock_irqrestore(&wl->wl_lock, flags);
502 } else {
503 wl->last_tx_hlid = 0;
504 }
505
506 return skb;
507}
508
509static struct sk_buff *wl1271_skb_dequeue(struct wl1271 *wl)
510{
511 if (wl->bss_type == BSS_TYPE_AP_BSS)
512 return wl1271_ap_skb_dequeue(wl);
513
514 return wl1271_sta_skb_dequeue(wl);
515}
516
Juuso Oikarinen6742f552010-12-13 09:52:37 +0200517static void wl1271_skb_queue_head(struct wl1271 *wl, struct sk_buff *skb)
518{
519 unsigned long flags;
520 int q = wl1271_tx_get_queue(skb_get_queue_mapping(skb));
521
Arik Nemtsova8c0ddb2011-02-23 00:22:26 +0200522 if (wl->bss_type == BSS_TYPE_AP_BSS) {
523 u8 hlid = wl1271_tx_get_hlid(skb);
524 skb_queue_head(&wl->links[hlid].tx_queue[q], skb);
525
526 /* make sure we dequeue the same packet next time */
527 wl->last_tx_hlid = (hlid + AP_MAX_LINKS - 1) % AP_MAX_LINKS;
528 } else {
529 skb_queue_head(&wl->tx_queue[q], skb);
530 }
531
Juuso Oikarinen6742f552010-12-13 09:52:37 +0200532 spin_lock_irqsave(&wl->wl_lock, flags);
533 wl->tx_queue_count++;
534 spin_unlock_irqrestore(&wl->wl_lock, flags);
535}
536
Ido Yariva5225502010-10-12 14:49:10 +0200537void wl1271_tx_work_locked(struct wl1271 *wl)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300538{
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300539 struct sk_buff *skb;
Ido Yariv6c6e6692010-10-12 14:49:09 +0200540 u32 buf_offset = 0;
541 bool sent_packets = false;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300542 int ret;
543
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300544 if (unlikely(wl->state == WL1271_STATE_OFF))
Eliad Pellerc1b193e2011-03-23 22:22:15 +0200545 return;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300546
Juuso Oikarinen6742f552010-12-13 09:52:37 +0200547 while ((skb = wl1271_skb_dequeue(wl))) {
Ido Yariva19606b2010-09-30 13:28:28 +0200548 ret = wl1271_prepare_tx_frame(wl, skb, buf_offset);
Ido Yariv6c6e6692010-10-12 14:49:09 +0200549 if (ret == -EAGAIN) {
Ido Yariva19606b2010-09-30 13:28:28 +0200550 /*
Ido Yariv6c6e6692010-10-12 14:49:09 +0200551 * Aggregation buffer is full.
552 * Flush buffer and try again.
553 */
Juuso Oikarinen6742f552010-12-13 09:52:37 +0200554 wl1271_skb_queue_head(wl, skb);
Ido Yariv6c6e6692010-10-12 14:49:09 +0200555 wl1271_write(wl, WL1271_SLV_MEM_DATA, wl->aggr_buf,
Juuso Oikarinen6742f552010-12-13 09:52:37 +0200556 buf_offset, true);
Ido Yariv6c6e6692010-10-12 14:49:09 +0200557 sent_packets = true;
558 buf_offset = 0;
559 continue;
560 } else if (ret == -EBUSY) {
561 /*
562 * Firmware buffer is full.
Ido Yariva19606b2010-09-30 13:28:28 +0200563 * Queue back last skb, and stop aggregating.
564 */
Juuso Oikarinen6742f552010-12-13 09:52:37 +0200565 wl1271_skb_queue_head(wl, skb);
Ido Yariva5225502010-10-12 14:49:10 +0200566 /* No work left, avoid scheduling redundant tx work */
567 set_bit(WL1271_FLAG_FW_TX_BUSY, &wl->flags);
Juuso Oikarinenffb591c2010-02-22 08:38:31 +0200568 goto out_ack;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300569 } else if (ret < 0) {
570 dev_kfree_skb(skb);
Juuso Oikarinenffb591c2010-02-22 08:38:31 +0200571 goto out_ack;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300572 }
Ido Yariva19606b2010-09-30 13:28:28 +0200573 buf_offset += ret;
574 wl->tx_packets_count++;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300575 }
576
Juuso Oikarinenffb591c2010-02-22 08:38:31 +0200577out_ack:
Ido Yariva19606b2010-09-30 13:28:28 +0200578 if (buf_offset) {
579 wl1271_write(wl, WL1271_SLV_MEM_DATA, wl->aggr_buf,
580 buf_offset, true);
Ido Yariv6c6e6692010-10-12 14:49:09 +0200581 sent_packets = true;
582 }
583 if (sent_packets) {
Ido Yariv606ea9f2011-03-01 15:14:39 +0200584 /*
585 * Interrupt the firmware with the new packets. This is only
586 * required for older hardware revisions
587 */
588 if (wl->quirks & WL12XX_QUIRK_END_OF_TRANSACTION)
589 wl1271_write32(wl, WL1271_HOST_WR_ACCESS,
590 wl->tx_packets_count);
591
Arik Nemtsova8c0ddb2011-02-23 00:22:26 +0200592 wl1271_handle_tx_low_watermark(wl);
Ido Yariva19606b2010-09-30 13:28:28 +0200593 }
Ido Yariva5225502010-10-12 14:49:10 +0200594}
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300595
Ido Yariva5225502010-10-12 14:49:10 +0200596void wl1271_tx_work(struct work_struct *work)
597{
598 struct wl1271 *wl = container_of(work, struct wl1271, tx_work);
Eliad Pellerc1b193e2011-03-23 22:22:15 +0200599 int ret;
Ido Yariva5225502010-10-12 14:49:10 +0200600
601 mutex_lock(&wl->mutex);
Eliad Pellerc1b193e2011-03-23 22:22:15 +0200602 ret = wl1271_ps_elp_wakeup(wl);
603 if (ret < 0)
604 goto out;
605
Ido Yariva5225502010-10-12 14:49:10 +0200606 wl1271_tx_work_locked(wl);
Eliad Pellerc1b193e2011-03-23 22:22:15 +0200607
608 wl1271_ps_elp_wakeup(wl);
609out:
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300610 mutex_unlock(&wl->mutex);
611}
612
613static void wl1271_tx_complete_packet(struct wl1271 *wl,
614 struct wl1271_tx_hw_res_descr *result)
615{
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300616 struct ieee80211_tx_info *info;
617 struct sk_buff *skb;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300618 int id = result->id;
Juuso Oikarinen31627dc2010-03-26 12:53:12 +0200619 int rate = -1;
620 u8 retries = 0;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300621
622 /* check for id legality */
Juuso Oikarinenffb591c2010-02-22 08:38:31 +0200623 if (unlikely(id >= ACX_TX_DESCRIPTORS || wl->tx_frames[id] == NULL)) {
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300624 wl1271_warning("TX result illegal id: %d", id);
625 return;
626 }
627
628 skb = wl->tx_frames[id];
629 info = IEEE80211_SKB_CB(skb);
630
Shahar Leviae47c452011-03-06 16:32:14 +0200631 if (skb->pkt_type == TX_PKT_TYPE_DUMMY_REQ) {
632 dev_kfree_skb(skb);
633 wl1271_free_tx_id(wl, id);
634 return;
635 }
636
Juuso Oikarinen31627dc2010-03-26 12:53:12 +0200637 /* update the TX status info */
638 if (result->status == TX_SUCCESS) {
639 if (!(info->flags & IEEE80211_TX_CTL_NO_ACK))
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300640 info->flags |= IEEE80211_TX_STAT_ACK;
Teemu Paasikivi6a2de932010-10-14 11:00:04 +0200641 rate = wl1271_rate_to_idx(result->rate_class_index, wl->band);
Juuso Oikarinen31627dc2010-03-26 12:53:12 +0200642 retries = result->ack_failures;
643 } else if (result->status == TX_RETRY_EXCEEDED) {
644 wl->stats.excessive_retries++;
645 retries = result->ack_failures;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300646 }
647
Juuso Oikarinen31627dc2010-03-26 12:53:12 +0200648 info->status.rates[0].idx = rate;
649 info->status.rates[0].count = retries;
650 info->status.rates[0].flags = 0;
651 info->status.ack_signal = -1;
652
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300653 wl->stats.retry_count += result->ack_failures;
654
Juuso Oikarinenac4e4ce2009-10-08 21:56:19 +0300655 /* update security sequence number */
Juuso Oikarinen04e36fc2010-02-22 08:38:40 +0200656 wl->tx_security_seq += (result->lsb_security_sequence_number -
657 wl->tx_security_last_seq);
Juuso Oikarinenac4e4ce2009-10-08 21:56:19 +0300658 wl->tx_security_last_seq = result->lsb_security_sequence_number;
659
Juuso Oikarinen1e2b7972009-10-08 21:56:20 +0300660 /* remove private header from packet */
661 skb_pull(skb, sizeof(struct wl1271_tx_hw_descr));
662
663 /* remove TKIP header space if present */
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300664 if (info->control.hw_key &&
Johannes Berg97359d12010-08-10 09:46:38 +0200665 info->control.hw_key->cipher == WLAN_CIPHER_SUITE_TKIP) {
Juuso Oikarinen1e2b7972009-10-08 21:56:20 +0300666 int hdrlen = ieee80211_get_hdrlen_from_skb(skb);
667 memmove(skb->data + WL1271_TKIP_IV_SPACE, skb->data, hdrlen);
668 skb_pull(skb, WL1271_TKIP_IV_SPACE);
669 }
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300670
671 wl1271_debug(DEBUG_TX, "tx status id %u skb 0x%p failures %u rate 0x%x"
672 " status 0x%x",
673 result->id, skb, result->ack_failures,
674 result->rate_class_index, result->status);
675
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300676 /* return the packet to the stack */
Ido Yariva6208652011-03-01 15:14:41 +0200677 skb_queue_tail(&wl->deferred_tx_queue, skb);
678 ieee80211_queue_work(wl->hw, &wl->netstack_work);
Ido Yariv25eeb9e2010-10-12 16:20:06 +0200679 wl1271_free_tx_id(wl, result->id);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300680}
681
682/* Called upon reception of a TX complete interrupt */
Juuso Oikarinenffb591c2010-02-22 08:38:31 +0200683void wl1271_tx_complete(struct wl1271 *wl)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300684{
685 struct wl1271_acx_mem_map *memmap =
686 (struct wl1271_acx_mem_map *)wl->target_mem_map;
Juuso Oikarinenffb591c2010-02-22 08:38:31 +0200687 u32 count, fw_counter;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300688 u32 i;
689
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300690 /* read the tx results from the chipset */
Teemu Paasikivi7b048c52010-02-18 13:25:55 +0200691 wl1271_read(wl, le32_to_cpu(memmap->tx_result),
692 wl->tx_res_if, sizeof(*wl->tx_res_if), false);
Juuso Oikarinenffb591c2010-02-22 08:38:31 +0200693 fw_counter = le32_to_cpu(wl->tx_res_if->tx_result_fw_counter);
694
695 /* write host counter to chipset (to ack) */
696 wl1271_write32(wl, le32_to_cpu(memmap->tx_result) +
697 offsetof(struct wl1271_tx_hw_res_if,
698 tx_result_host_counter), fw_counter);
699
700 count = fw_counter - wl->tx_results_count;
Juuso Oikarinen06f7bc72010-02-22 08:38:33 +0200701 wl1271_debug(DEBUG_TX, "tx_complete received, packets: %d", count);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300702
703 /* verify that the result buffer is not getting overrun */
Juuso Oikarinenffb591c2010-02-22 08:38:31 +0200704 if (unlikely(count > TX_HW_RESULT_QUEUE_LEN))
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300705 wl1271_warning("TX result overflow from chipset: %d", count);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300706
707 /* process the results */
708 for (i = 0; i < count; i++) {
709 struct wl1271_tx_hw_res_descr *result;
710 u8 offset = wl->tx_results_count & TX_HW_RESULT_QUEUE_LEN_MASK;
711
712 /* process the packet */
713 result = &(wl->tx_res_if->tx_results_queue[offset]);
714 wl1271_tx_complete_packet(wl, result);
715
716 wl->tx_results_count++;
717 }
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300718}
719
Arik Nemtsova8c0ddb2011-02-23 00:22:26 +0200720void wl1271_tx_reset_link_queues(struct wl1271 *wl, u8 hlid)
721{
722 struct sk_buff *skb;
723 int i, total = 0;
724 unsigned long flags;
Arik Nemtsov1d36cd82011-02-23 00:22:27 +0200725 struct ieee80211_tx_info *info;
Arik Nemtsova8c0ddb2011-02-23 00:22:26 +0200726
727 for (i = 0; i < NUM_TX_QUEUES; i++) {
728 while ((skb = skb_dequeue(&wl->links[hlid].tx_queue[i]))) {
729 wl1271_debug(DEBUG_TX, "link freeing skb 0x%p", skb);
Arik Nemtsov1d36cd82011-02-23 00:22:27 +0200730 info = IEEE80211_SKB_CB(skb);
731 info->status.rates[0].idx = -1;
732 info->status.rates[0].count = 0;
Arik Nemtsova8c0ddb2011-02-23 00:22:26 +0200733 ieee80211_tx_status(wl->hw, skb);
734 total++;
735 }
736 }
737
738 spin_lock_irqsave(&wl->wl_lock, flags);
739 wl->tx_queue_count -= total;
740 spin_unlock_irqrestore(&wl->wl_lock, flags);
741
742 wl1271_handle_tx_low_watermark(wl);
743}
744
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300745/* caller must hold wl->mutex */
Juuso Oikarinen781608c2010-05-24 11:18:17 +0300746void wl1271_tx_reset(struct wl1271 *wl)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300747{
748 int i;
749 struct sk_buff *skb;
Arik Nemtsov1d36cd82011-02-23 00:22:27 +0200750 struct ieee80211_tx_info *info;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300751
752 /* TX failure */
Arik Nemtsova8c0ddb2011-02-23 00:22:26 +0200753 if (wl->bss_type == BSS_TYPE_AP_BSS) {
Arik Nemtsov09039f42011-02-23 00:22:30 +0200754 for (i = 0; i < AP_MAX_LINKS; i++) {
Arik Nemtsova8c0ddb2011-02-23 00:22:26 +0200755 wl1271_tx_reset_link_queues(wl, i);
Arik Nemtsov09039f42011-02-23 00:22:30 +0200756 wl->links[i].allocated_blks = 0;
757 wl->links[i].prev_freed_blks = 0;
758 }
Arik Nemtsova8c0ddb2011-02-23 00:22:26 +0200759
760 wl->last_tx_hlid = 0;
761 } else {
762 for (i = 0; i < NUM_TX_QUEUES; i++) {
763 while ((skb = skb_dequeue(&wl->tx_queue[i]))) {
764 wl1271_debug(DEBUG_TX, "freeing skb 0x%p",
765 skb);
Shahar Leviae47c452011-03-06 16:32:14 +0200766
767 if (skb->pkt_type == TX_PKT_TYPE_DUMMY_REQ) {
768 dev_kfree_skb(skb);
769 } else {
770 info = IEEE80211_SKB_CB(skb);
771 info->status.rates[0].idx = -1;
772 info->status.rates[0].count = 0;
773 ieee80211_tx_status(wl->hw, skb);
774 }
Arik Nemtsova8c0ddb2011-02-23 00:22:26 +0200775 }
Juuso Oikarinen6742f552010-12-13 09:52:37 +0200776 }
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300777 }
Arik Nemtsova8c0ddb2011-02-23 00:22:26 +0200778
Juuso Oikarinen6742f552010-12-13 09:52:37 +0200779 wl->tx_queue_count = 0;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300780
Ido Yariv2fe33e82010-10-12 14:49:12 +0200781 /*
782 * Make sure the driver is at a consistent state, in case this
783 * function is called from a context other than interface removal.
784 */
Arik Nemtsova8c0ddb2011-02-23 00:22:26 +0200785 wl1271_handle_tx_low_watermark(wl);
Ido Yariv2fe33e82010-10-12 14:49:12 +0200786
Ido Yariv50e9f742011-02-28 00:16:13 +0200787 for (i = 0; i < ACX_TX_DESCRIPTORS; i++) {
788 if (wl->tx_frames[i] == NULL)
789 continue;
790
791 skb = wl->tx_frames[i];
792 wl1271_free_tx_id(wl, i);
793 wl1271_debug(DEBUG_TX, "freeing skb 0x%p", skb);
794
Shahar Leviae47c452011-03-06 16:32:14 +0200795 if (skb->pkt_type == TX_PKT_TYPE_DUMMY_REQ) {
796 dev_kfree_skb(skb);
797 } else {
798 /*
799 * Remove private headers before passing the skb to
800 * mac80211
801 */
802 info = IEEE80211_SKB_CB(skb);
803 skb_pull(skb, sizeof(struct wl1271_tx_hw_descr));
804 if (info->control.hw_key &&
805 info->control.hw_key->cipher ==
806 WLAN_CIPHER_SUITE_TKIP) {
807 int hdrlen = ieee80211_get_hdrlen_from_skb(skb);
808 memmove(skb->data + WL1271_TKIP_IV_SPACE,
809 skb->data, hdrlen);
810 skb_pull(skb, WL1271_TKIP_IV_SPACE);
811 }
812
813 info->status.rates[0].idx = -1;
814 info->status.rates[0].count = 0;
815
816 ieee80211_tx_status(wl->hw, skb);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300817 }
Ido Yariv50e9f742011-02-28 00:16:13 +0200818 }
Juuso Oikarinen781608c2010-05-24 11:18:17 +0300819}
820
821#define WL1271_TX_FLUSH_TIMEOUT 500000
822
823/* caller must *NOT* hold wl->mutex */
824void wl1271_tx_flush(struct wl1271 *wl)
825{
826 unsigned long timeout;
827 timeout = jiffies + usecs_to_jiffies(WL1271_TX_FLUSH_TIMEOUT);
828
829 while (!time_after(jiffies, timeout)) {
830 mutex_lock(&wl->mutex);
Arik Nemtsova8c0ddb2011-02-23 00:22:26 +0200831 wl1271_debug(DEBUG_TX, "flushing tx buffer: %d %d",
832 wl->tx_frames_cnt, wl->tx_queue_count);
Juuso Oikarinen6742f552010-12-13 09:52:37 +0200833 if ((wl->tx_frames_cnt == 0) && (wl->tx_queue_count == 0)) {
Juuso Oikarinen781608c2010-05-24 11:18:17 +0300834 mutex_unlock(&wl->mutex);
835 return;
836 }
837 mutex_unlock(&wl->mutex);
838 msleep(1);
839 }
840
841 wl1271_warning("Unable to flush all TX buffers, timed out.");
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300842}
Arik Nemtsove0fe3712010-10-16 18:19:53 +0200843
844u32 wl1271_tx_min_rate_get(struct wl1271 *wl)
845{
846 int i;
847 u32 rate = 0;
848
849 if (!wl->basic_rate_set) {
850 WARN_ON(1);
851 wl->basic_rate_set = wl->conf.tx.basic_rate;
852 }
853
854 for (i = 0; !rate; i++) {
855 if ((wl->basic_rate_set >> i) & 0x1)
856 rate = 1 << i;
857 }
858
859 return rate;
860}