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