]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/net/wireless/wl12xx/wl1271_tx.c
wl1271: Clean up RX rate reporting
[net-next-2.6.git] / drivers / net / wireless / wl12xx / wl1271_tx.c
CommitLineData
f5fc0f86
LC
1/*
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>
26
27#include "wl1271.h"
7b048c52 28#include "wl1271_io.h"
f5fc0f86
LC
29#include "wl1271_reg.h"
30#include "wl1271_ps.h"
31#include "wl1271_tx.h"
32
33static int wl1271_tx_id(struct wl1271 *wl, struct sk_buff *skb)
34{
35 int i;
be7078c2 36 for (i = 0; i < ACX_TX_DESCRIPTORS; i++)
f5fc0f86
LC
37 if (wl->tx_frames[i] == NULL) {
38 wl->tx_frames[i] = skb;
39 return i;
40 }
41
42 return -EBUSY;
43}
44
45static int wl1271_tx_allocate(struct wl1271 *wl, struct sk_buff *skb, u32 extra)
46{
47 struct wl1271_tx_hw_descr *desc;
48 u32 total_len = skb->len + sizeof(struct wl1271_tx_hw_descr) + extra;
5c9417f1 49 u32 total_blocks;
f5fc0f86
LC
50 int id, ret = -EBUSY;
51
52 /* allocate free identifier for the packet */
53 id = wl1271_tx_id(wl, skb);
54 if (id < 0)
55 return id;
56
57 /* approximate the number of blocks required for this packet
58 in the firmware */
5c9417f1
JO
59 total_blocks = total_len + TX_HW_BLOCK_SIZE - 1;
60 total_blocks = total_blocks / TX_HW_BLOCK_SIZE + TX_HW_BLOCK_SPARE;
f5fc0f86
LC
61 if (total_blocks <= wl->tx_blocks_available) {
62 desc = (struct wl1271_tx_hw_descr *)skb_push(
63 skb, total_len - skb->len);
64
65 desc->extra_mem_blocks = TX_HW_BLOCK_SPARE;
66 desc->total_mem_blocks = total_blocks;
67 desc->id = id;
68
69 wl->tx_blocks_available -= total_blocks;
70
71 ret = 0;
72
73 wl1271_debug(DEBUG_TX,
74 "tx_allocate: size: %d, blocks: %d, id: %d",
75 total_len, total_blocks, id);
76 } else
77 wl->tx_frames[id] = NULL;
78
79 return ret;
80}
81
82static int wl1271_tx_fill_hdr(struct wl1271 *wl, struct sk_buff *skb,
83 u32 extra, struct ieee80211_tx_info *control)
84{
ac5e1e39 85 struct timespec ts;
f5fc0f86 86 struct wl1271_tx_hw_descr *desc;
c6999d83 87 int pad, ac;
ac5e1e39 88 s64 hosttime;
d0f63b20 89 u16 tx_attr;
f5fc0f86
LC
90
91 desc = (struct wl1271_tx_hw_descr *) skb->data;
92
1e2b7976
JO
93 /* relocate space for security header */
94 if (extra) {
95 void *framestart = skb->data + sizeof(*desc);
96 u16 fc = *(u16 *)(framestart + extra);
d0f63b20 97 int hdrlen = ieee80211_hdrlen(cpu_to_le16(fc));
1e2b7976
JO
98 memmove(framestart, framestart + extra, hdrlen);
99 }
100
f5fc0f86 101 /* configure packet life time */
ac5e1e39
JO
102 getnstimeofday(&ts);
103 hosttime = (timespec_to_ns(&ts) >> 10);
104 desc->start_time = cpu_to_le32(hosttime - wl->time_offset);
d0f63b20 105 desc->life_time = cpu_to_le16(TX_HW_MGMT_PKT_LIFETIME_TU);
f5fc0f86
LC
106
107 /* configure the tx attributes */
d0f63b20 108 tx_attr = wl->session_counter << TX_HW_ATTR_OFST_SESSION_COUNTER;
c6999d83
KV
109
110 /* queue */
111 ac = wl1271_tx_get_queue(skb_get_queue_mapping(skb));
112 desc->tid = wl1271_tx_ac_to_tid(ac);
113
f5fc0f86
LC
114 desc->aid = TX_HW_DEFAULT_AID;
115 desc->reserved = 0;
116
117 /* align the length (and store in terms of words) */
118 pad = WL1271_TX_ALIGN(skb->len);
d0f63b20 119 desc->length = cpu_to_le16(pad >> 2);
f5fc0f86
LC
120
121 /* calculate number of padding bytes */
122 pad = pad - skb->len;
d0f63b20
LC
123 tx_attr |= pad << TX_HW_ATTR_OFST_LAST_WORD_PAD;
124
830fb67b
JO
125 /* if the packets are destined for AP (have a STA entry) send them
126 with AP rate policies, otherwise use default basic rates */
127 if (control->control.sta)
128 tx_attr |= ACX_TX_AP_FULL_RATE << TX_HW_ATTR_OFST_RATE_POLICY;
129
d0f63b20 130 desc->tx_attr = cpu_to_le16(tx_attr);
f5fc0f86
LC
131
132 wl1271_debug(DEBUG_TX, "tx_fill_hdr: pad: %d", pad);
133 return 0;
134}
135
136static int wl1271_tx_send_packet(struct wl1271 *wl, struct sk_buff *skb,
137 struct ieee80211_tx_info *control)
138{
139
140 struct wl1271_tx_hw_descr *desc;
141 int len;
142
143 /* FIXME: This is a workaround for getting non-aligned packets.
144 This happens at least with EAPOL packets from the user space.
145 Our DMA requires packets to be aligned on a 4-byte boundary.
146 */
147 if (unlikely((long)skb->data & 0x03)) {
148 int offset = (4 - (long)skb->data) & 0x03;
149 wl1271_debug(DEBUG_TX, "skb offset %d", offset);
150
151 /* check whether the current skb can be used */
152 if (!skb_cloned(skb) && (skb_tailroom(skb) >= offset)) {
153 unsigned char *src = skb->data;
154
155 /* align the buffer on a 4-byte boundary */
156 skb_reserve(skb, offset);
157 memmove(skb->data, src, skb->len);
158 } else {
159 wl1271_info("No handler, fixme!");
160 return -EINVAL;
161 }
162 }
163
164 len = WL1271_TX_ALIGN(skb->len);
165
166 /* perform a fixed address block write with the packet */
7b048c52 167 wl1271_write(wl, WL1271_SLV_MEM_DATA, skb->data, len, true);
f5fc0f86
LC
168
169 /* write packet new counter into the write access register */
170 wl->tx_packets_count++;
f5fc0f86
LC
171
172 desc = (struct wl1271_tx_hw_descr *) skb->data;
173 wl1271_debug(DEBUG_TX, "tx id %u skb 0x%p payload %u (%u words)",
174 desc->id, skb, len, desc->length);
175
176 return 0;
177}
178
179/* caller must hold wl->mutex */
180static int wl1271_tx_frame(struct wl1271 *wl, struct sk_buff *skb)
181{
182 struct ieee80211_tx_info *info;
183 u32 extra = 0;
184 int ret = 0;
185 u8 idx;
186
187 if (!skb)
188 return -EINVAL;
189
190 info = IEEE80211_SKB_CB(skb);
191
192 if (info->control.hw_key &&
193 info->control.hw_key->alg == ALG_TKIP)
194 extra = WL1271_TKIP_IV_SPACE;
195
196 if (info->control.hw_key) {
197 idx = info->control.hw_key->hw_key_idx;
198
199 /* FIXME: do we have to do this if we're not using WEP? */
200 if (unlikely(wl->default_key != idx)) {
201 ret = wl1271_cmd_set_default_wep_key(wl, idx);
202 if (ret < 0)
203 return ret;
ee444cf0 204 wl->default_key = idx;
f5fc0f86
LC
205 }
206 }
207
208 ret = wl1271_tx_allocate(wl, skb, extra);
209 if (ret < 0)
210 return ret;
211
212 ret = wl1271_tx_fill_hdr(wl, skb, extra, info);
213 if (ret < 0)
214 return ret;
215
216 ret = wl1271_tx_send_packet(wl, skb, info);
217 if (ret < 0)
218 return ret;
219
220 return ret;
221}
222
830fb67b
JO
223static u32 wl1271_tx_enabled_rates_get(struct wl1271 *wl, u32 rate_set)
224{
225 struct ieee80211_supported_band *band;
226 u32 enabled_rates = 0;
227 int bit;
228
229 band = wl->hw->wiphy->bands[wl->band];
230 for (bit = 0; bit < band->n_bitrates; bit++) {
231 if (rate_set & 0x1)
232 enabled_rates |= band->bitrates[bit].hw_value;
233 rate_set >>= 1;
234 }
235
236 return enabled_rates;
237}
238
f5fc0f86
LC
239void wl1271_tx_work(struct work_struct *work)
240{
241 struct wl1271 *wl = container_of(work, struct wl1271, tx_work);
242 struct sk_buff *skb;
243 bool woken_up = false;
830fb67b 244 u32 sta_rates = 0;
ffb591cd 245 u32 prev_tx_packets_count;
f5fc0f86
LC
246 int ret;
247
830fb67b
JO
248 /* check if the rates supported by the AP have changed */
249 if (unlikely(test_and_clear_bit(WL1271_FLAG_STA_RATES_CHANGED,
250 &wl->flags))) {
251 unsigned long flags;
252 spin_lock_irqsave(&wl->wl_lock, flags);
253 sta_rates = wl->sta_rate_set;
254 spin_unlock_irqrestore(&wl->wl_lock, flags);
255 }
256
f5fc0f86
LC
257 mutex_lock(&wl->mutex);
258
259 if (unlikely(wl->state == WL1271_STATE_OFF))
260 goto out;
261
ffb591cd
JO
262 prev_tx_packets_count = wl->tx_packets_count;
263
830fb67b
JO
264 /* if rates have changed, re-configure the rate policy */
265 if (unlikely(sta_rates)) {
266 wl->rate_set = wl1271_tx_enabled_rates_get(wl, sta_rates);
267 wl1271_acx_rate_policies(wl);
268 }
269
f5fc0f86
LC
270 while ((skb = skb_dequeue(&wl->tx_queue))) {
271 if (!woken_up) {
272 ret = wl1271_ps_elp_wakeup(wl, false);
273 if (ret < 0)
ffb591cd 274 goto out_ack;
f5fc0f86
LC
275 woken_up = true;
276 }
277
278 ret = wl1271_tx_frame(wl, skb);
279 if (ret == -EBUSY) {
06f7bc7d 280 /* firmware buffer is full, lets stop transmitting. */
f5fc0f86 281 skb_queue_head(&wl->tx_queue, skb);
ffb591cd 282 goto out_ack;
f5fc0f86
LC
283 } else if (ret < 0) {
284 dev_kfree_skb(skb);
ffb591cd 285 goto out_ack;
f5fc0f86
LC
286 }
287 }
288
ffb591cd
JO
289out_ack:
290 /* interrupt the firmware with the new packets */
291 if (prev_tx_packets_count != wl->tx_packets_count)
292 wl1271_write32(wl, WL1271_HOST_WR_ACCESS, wl->tx_packets_count);
293
f5fc0f86
LC
294out:
295 if (woken_up)
296 wl1271_ps_elp_sleep(wl);
297
298 mutex_unlock(&wl->mutex);
299}
300
301static void wl1271_tx_complete_packet(struct wl1271 *wl,
302 struct wl1271_tx_hw_res_descr *result)
303{
f5fc0f86
LC
304 struct ieee80211_tx_info *info;
305 struct sk_buff *skb;
f5fc0f86
LC
306 int id = result->id;
307
308 /* check for id legality */
ffb591cd 309 if (unlikely(id >= ACX_TX_DESCRIPTORS || wl->tx_frames[id] == NULL)) {
f5fc0f86
LC
310 wl1271_warning("TX result illegal id: %d", id);
311 return;
312 }
313
314 skb = wl->tx_frames[id];
315 info = IEEE80211_SKB_CB(skb);
316
317 /* update packet status */
318 if (!(info->flags & IEEE80211_TX_CTL_NO_ACK)) {
319 if (result->status == TX_SUCCESS)
320 info->flags |= IEEE80211_TX_STAT_ACK;
321 if (result->status & TX_RETRY_EXCEEDED) {
322 /* FIXME */
323 /* info->status.excessive_retries = 1; */
324 wl->stats.excessive_retries++;
325 }
326 }
327
328 /* FIXME */
329 /* info->status.retry_count = result->ack_failures; */
330 wl->stats.retry_count += result->ack_failures;
331
ac4e4ce5 332 /* update security sequence number */
04e36fc5
JO
333 wl->tx_security_seq += (result->lsb_security_sequence_number -
334 wl->tx_security_last_seq);
ac4e4ce5
JO
335 wl->tx_security_last_seq = result->lsb_security_sequence_number;
336
1e2b7976
JO
337 /* remove private header from packet */
338 skb_pull(skb, sizeof(struct wl1271_tx_hw_descr));
339
340 /* remove TKIP header space if present */
f5fc0f86 341 if (info->control.hw_key &&
1e2b7976
JO
342 info->control.hw_key->alg == ALG_TKIP) {
343 int hdrlen = ieee80211_get_hdrlen_from_skb(skb);
344 memmove(skb->data + WL1271_TKIP_IV_SPACE, skb->data, hdrlen);
345 skb_pull(skb, WL1271_TKIP_IV_SPACE);
346 }
f5fc0f86
LC
347
348 wl1271_debug(DEBUG_TX, "tx status id %u skb 0x%p failures %u rate 0x%x"
349 " status 0x%x",
350 result->id, skb, result->ack_failures,
351 result->rate_class_index, result->status);
352
0a34332f
JO
353 /* FIXME: do we need to tell the stack about the used rate? */
354
f5fc0f86
LC
355 /* return the packet to the stack */
356 ieee80211_tx_status(wl->hw, skb);
357 wl->tx_frames[result->id] = NULL;
358}
359
360/* Called upon reception of a TX complete interrupt */
ffb591cd 361void wl1271_tx_complete(struct wl1271 *wl)
f5fc0f86
LC
362{
363 struct wl1271_acx_mem_map *memmap =
364 (struct wl1271_acx_mem_map *)wl->target_mem_map;
ffb591cd 365 u32 count, fw_counter;
f5fc0f86
LC
366 u32 i;
367
f5fc0f86 368 /* read the tx results from the chipset */
7b048c52
TP
369 wl1271_read(wl, le32_to_cpu(memmap->tx_result),
370 wl->tx_res_if, sizeof(*wl->tx_res_if), false);
ffb591cd
JO
371 fw_counter = le32_to_cpu(wl->tx_res_if->tx_result_fw_counter);
372
373 /* write host counter to chipset (to ack) */
374 wl1271_write32(wl, le32_to_cpu(memmap->tx_result) +
375 offsetof(struct wl1271_tx_hw_res_if,
376 tx_result_host_counter), fw_counter);
377
378 count = fw_counter - wl->tx_results_count;
06f7bc7d 379 wl1271_debug(DEBUG_TX, "tx_complete received, packets: %d", count);
f5fc0f86
LC
380
381 /* verify that the result buffer is not getting overrun */
ffb591cd 382 if (unlikely(count > TX_HW_RESULT_QUEUE_LEN))
f5fc0f86 383 wl1271_warning("TX result overflow from chipset: %d", count);
f5fc0f86
LC
384
385 /* process the results */
386 for (i = 0; i < count; i++) {
387 struct wl1271_tx_hw_res_descr *result;
388 u8 offset = wl->tx_results_count & TX_HW_RESULT_QUEUE_LEN_MASK;
389
390 /* process the packet */
391 result = &(wl->tx_res_if->tx_results_queue[offset]);
392 wl1271_tx_complete_packet(wl, result);
393
394 wl->tx_results_count++;
395 }
06f7bc7d
JO
396
397 if (test_bit(WL1271_FLAG_TX_QUEUE_STOPPED, &wl->flags) &&
398 skb_queue_len(&wl->tx_queue) <= WL1271_TX_QUEUE_LOW_WATERMARK) {
399 unsigned long flags;
400
401 /* firmware buffer has space, restart queues */
402 wl1271_debug(DEBUG_TX, "tx_complete: waking queues");
403 spin_lock_irqsave(&wl->wl_lock, flags);
404 ieee80211_wake_queues(wl->hw);
405 clear_bit(WL1271_FLAG_TX_QUEUE_STOPPED, &wl->flags);
406 spin_unlock_irqrestore(&wl->wl_lock, flags);
407 ieee80211_queue_work(wl->hw, &wl->tx_work);
408 }
f5fc0f86
LC
409}
410
411/* caller must hold wl->mutex */
412void wl1271_tx_flush(struct wl1271 *wl)
413{
414 int i;
415 struct sk_buff *skb;
416 struct ieee80211_tx_info *info;
417
418 /* TX failure */
419/* control->flags = 0; FIXME */
420
421 while ((skb = skb_dequeue(&wl->tx_queue))) {
422 info = IEEE80211_SKB_CB(skb);
423
424 wl1271_debug(DEBUG_TX, "flushing skb 0x%p", skb);
425
426 if (!(info->flags & IEEE80211_TX_CTL_REQ_TX_STATUS))
427 continue;
428
429 ieee80211_tx_status(wl->hw, skb);
430 }
431
be7078c2 432 for (i = 0; i < ACX_TX_DESCRIPTORS; i++)
f5fc0f86
LC
433 if (wl->tx_frames[i] != NULL) {
434 skb = wl->tx_frames[i];
435 info = IEEE80211_SKB_CB(skb);
436
437 if (!(info->flags & IEEE80211_TX_CTL_REQ_TX_STATUS))
438 continue;
439
440 ieee80211_tx_status(wl->hw, skb);
441 wl->tx_frames[i] = NULL;
442 }
443}