wl1251: Prepare for idle mode support
[pandora-wifi.git] / drivers / net / wireless / wl12xx / wl1251_tx.c
1 /*
2  * This file is part of wl1251
3  *
4  * Copyright (c) 1998-2007 Texas Instruments Incorporated
5  * Copyright (C) 2008 Nokia Corporation
6  *
7  * Contact: Kalle Valo <kalle.valo@nokia.com>
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * version 2 as published by the Free Software Foundation.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21  * 02110-1301 USA
22  *
23  */
24
25 #include <linux/kernel.h>
26 #include <linux/module.h>
27
28 #include "wl1251.h"
29 #include "wl1251_reg.h"
30 #include "wl1251_tx.h"
31 #include "wl1251_ps.h"
32 #include "wl1251_io.h"
33
34 static bool wl1251_tx_double_buffer_busy(struct wl1251 *wl, u32 data_out_count)
35 {
36         int used, data_in_count;
37
38         data_in_count = wl->data_in_count;
39
40         if (data_in_count < data_out_count)
41                 /* data_in_count has wrapped */
42                 data_in_count += TX_STATUS_DATA_OUT_COUNT_MASK + 1;
43
44         used = data_in_count - data_out_count;
45
46         WARN_ON(used < 0);
47         WARN_ON(used > DP_TX_PACKET_RING_CHUNK_NUM);
48
49         if (used >= DP_TX_PACKET_RING_CHUNK_NUM)
50                 return true;
51         else
52                 return false;
53 }
54
55 static int wl1251_tx_path_status(struct wl1251 *wl)
56 {
57         u32 status, addr, data_out_count;
58         bool busy;
59
60         addr = wl->data_path->tx_control_addr;
61         status = wl1251_mem_read32(wl, addr);
62         data_out_count = status & TX_STATUS_DATA_OUT_COUNT_MASK;
63         busy = wl1251_tx_double_buffer_busy(wl, data_out_count);
64
65         if (busy)
66                 return -EBUSY;
67
68         return 0;
69 }
70
71 static int wl1251_tx_id(struct wl1251 *wl, struct sk_buff *skb)
72 {
73         int i;
74
75         for (i = 0; i < FW_TX_CMPLT_BLOCK_SIZE; i++)
76                 if (wl->tx_frames[i] == NULL) {
77                         wl->tx_frames[i] = skb;
78                         return i;
79                 }
80
81         return -EBUSY;
82 }
83
84 static void wl1251_tx_control(struct tx_double_buffer_desc *tx_hdr,
85                               struct ieee80211_tx_info *control, u16 fc)
86 {
87         *(u16 *)&tx_hdr->control = 0;
88
89         tx_hdr->control.rate_policy = 0;
90
91         /* 802.11 packets */
92         tx_hdr->control.packet_type = 0;
93
94         if (control->flags & IEEE80211_TX_CTL_NO_ACK)
95                 tx_hdr->control.ack_policy = 1;
96
97         tx_hdr->control.tx_complete = 1;
98
99         if ((fc & IEEE80211_FTYPE_DATA) &&
100             ((fc & IEEE80211_STYPE_QOS_DATA) ||
101              (fc & IEEE80211_STYPE_QOS_NULLFUNC)))
102                 tx_hdr->control.qos = 1;
103 }
104
105 /* RSN + MIC = 8 + 8 = 16 bytes (worst case - AES). */
106 #define MAX_MSDU_SECURITY_LENGTH      16
107 #define MAX_MPDU_SECURITY_LENGTH      16
108 #define WLAN_QOS_HDR_LEN              26
109 #define MAX_MPDU_HEADER_AND_SECURITY  (MAX_MPDU_SECURITY_LENGTH + \
110                                        WLAN_QOS_HDR_LEN)
111 #define HW_BLOCK_SIZE                 252
112 static void wl1251_tx_frag_block_num(struct tx_double_buffer_desc *tx_hdr)
113 {
114         u16 payload_len, frag_threshold, mem_blocks;
115         u16 num_mpdus, mem_blocks_per_frag;
116
117         frag_threshold = IEEE80211_MAX_FRAG_THRESHOLD;
118         tx_hdr->frag_threshold = cpu_to_le16(frag_threshold);
119
120         payload_len = le16_to_cpu(tx_hdr->length) + MAX_MSDU_SECURITY_LENGTH;
121
122         if (payload_len > frag_threshold) {
123                 mem_blocks_per_frag =
124                         ((frag_threshold + MAX_MPDU_HEADER_AND_SECURITY) /
125                          HW_BLOCK_SIZE) + 1;
126                 num_mpdus = payload_len / frag_threshold;
127                 mem_blocks = num_mpdus * mem_blocks_per_frag;
128                 payload_len -= num_mpdus * frag_threshold;
129                 num_mpdus++;
130
131         } else {
132                 mem_blocks_per_frag = 0;
133                 mem_blocks = 0;
134                 num_mpdus = 1;
135         }
136
137         mem_blocks += (payload_len / HW_BLOCK_SIZE) + 1;
138
139         if (num_mpdus > 1)
140                 mem_blocks += min(num_mpdus, mem_blocks_per_frag);
141
142         tx_hdr->num_mem_blocks = mem_blocks;
143 }
144
145 static int wl1251_tx_fill_hdr(struct wl1251 *wl, struct sk_buff *skb,
146                               struct ieee80211_tx_info *control)
147 {
148         struct tx_double_buffer_desc *tx_hdr;
149         struct ieee80211_rate *rate;
150         int id;
151         u16 fc;
152
153         if (!skb)
154                 return -EINVAL;
155
156         id = wl1251_tx_id(wl, skb);
157         if (id < 0)
158                 return id;
159
160         fc = *(u16 *)skb->data;
161         tx_hdr = (struct tx_double_buffer_desc *) skb_push(skb,
162                                                            sizeof(*tx_hdr));
163
164         tx_hdr->length = cpu_to_le16(skb->len - sizeof(*tx_hdr));
165         rate = ieee80211_get_tx_rate(wl->hw, control);
166         tx_hdr->rate = cpu_to_le16(rate->hw_value);
167         tx_hdr->expiry_time = cpu_to_le32(1 << 16);
168         tx_hdr->id = id;
169
170         tx_hdr->xmit_queue = wl1251_tx_get_queue(skb_get_queue_mapping(skb));
171
172         wl1251_tx_control(tx_hdr, control, fc);
173         wl1251_tx_frag_block_num(tx_hdr);
174
175         return 0;
176 }
177
178 /* We copy the packet to the target */
179 static int wl1251_tx_send_packet(struct wl1251 *wl, struct sk_buff *skb,
180                                  struct ieee80211_tx_info *control)
181 {
182         struct tx_double_buffer_desc *tx_hdr;
183         int len;
184         u32 addr;
185
186         if (!skb)
187                 return -EINVAL;
188
189         tx_hdr = (struct tx_double_buffer_desc *) skb->data;
190
191         if (control->control.hw_key &&
192             control->control.hw_key->alg == ALG_TKIP) {
193                 int hdrlen;
194                 __le16 fc;
195                 u16 length;
196                 u8 *pos;
197
198                 fc = *(__le16 *)(skb->data + sizeof(*tx_hdr));
199                 length = le16_to_cpu(tx_hdr->length) + WL1251_TKIP_IV_SPACE;
200                 tx_hdr->length = cpu_to_le16(length);
201
202                 hdrlen = ieee80211_hdrlen(fc);
203
204                 pos = skb_push(skb, WL1251_TKIP_IV_SPACE);
205                 memmove(pos, pos + WL1251_TKIP_IV_SPACE,
206                         sizeof(*tx_hdr) + hdrlen);
207         }
208
209         /* Revisit. This is a workaround for getting non-aligned packets.
210            This happens at least with EAPOL packets from the user space.
211            Our DMA requires packets to be aligned on a 4-byte boundary.
212         */
213         if (unlikely((long)skb->data & 0x03)) {
214                 int offset = (4 - (long)skb->data) & 0x03;
215                 wl1251_debug(DEBUG_TX, "skb offset %d", offset);
216
217                 /* check whether the current skb can be used */
218                 if (skb_cloned(skb) || (skb_tailroom(skb) < offset)) {
219                         struct sk_buff *newskb = skb_copy_expand(skb, 0, 3,
220                                                                  GFP_KERNEL);
221
222                         if (unlikely(newskb == NULL)) {
223                                 wl1251_error("Can't allocate skb!");
224                                 return -EINVAL;
225                         }
226
227                         tx_hdr = (struct tx_double_buffer_desc *) newskb->data;
228
229                         dev_kfree_skb_any(skb);
230                         wl->tx_frames[tx_hdr->id] = skb = newskb;
231
232                         offset = (4 - (long)skb->data) & 0x03;
233                         wl1251_debug(DEBUG_TX, "new skb offset %d", offset);
234                 }
235
236                 /* align the buffer on a 4-byte boundary */
237                 if (offset) {
238                         unsigned char *src = skb->data;
239                         skb_reserve(skb, offset);
240                         memmove(skb->data, src, skb->len);
241                         tx_hdr = (struct tx_double_buffer_desc *) skb->data;
242                 }
243         }
244
245         /* Our skb->data at this point includes the HW header */
246         len = WL1251_TX_ALIGN(skb->len);
247
248         if (wl->data_in_count & 0x1)
249                 addr = wl->data_path->tx_packet_ring_addr +
250                         wl->data_path->tx_packet_ring_chunk_size;
251         else
252                 addr = wl->data_path->tx_packet_ring_addr;
253
254         wl1251_mem_write(wl, addr, skb->data, len);
255
256         wl1251_debug(DEBUG_TX, "tx id %u skb 0x%p payload %u rate 0x%x "
257                      "queue %d", tx_hdr->id, skb, tx_hdr->length,
258                      tx_hdr->rate, tx_hdr->xmit_queue);
259
260         return 0;
261 }
262
263 static void wl1251_tx_trigger(struct wl1251 *wl)
264 {
265         u32 data, addr;
266
267         if (wl->data_in_count & 0x1) {
268                 addr = ACX_REG_INTERRUPT_TRIG_H;
269                 data = INTR_TRIG_TX_PROC1;
270         } else {
271                 addr = ACX_REG_INTERRUPT_TRIG;
272                 data = INTR_TRIG_TX_PROC0;
273         }
274
275         wl1251_reg_write32(wl, addr, data);
276
277         /* Bumping data in */
278         wl->data_in_count = (wl->data_in_count + 1) &
279                 TX_STATUS_DATA_OUT_COUNT_MASK;
280 }
281
282 /* caller must hold wl->mutex */
283 static int wl1251_tx_frame(struct wl1251 *wl, struct sk_buff *skb)
284 {
285         struct ieee80211_tx_info *info;
286         int ret = 0;
287         u8 idx;
288
289         info = IEEE80211_SKB_CB(skb);
290
291         if (info->control.hw_key) {
292                 idx = info->control.hw_key->hw_key_idx;
293                 if (unlikely(wl->default_key != idx)) {
294                         ret = wl1251_acx_default_key(wl, idx);
295                         if (ret < 0)
296                                 return ret;
297                 }
298         }
299
300         ret = wl1251_tx_path_status(wl);
301         if (ret < 0)
302                 return ret;
303
304         ret = wl1251_tx_fill_hdr(wl, skb, info);
305         if (ret < 0)
306                 return ret;
307
308         ret = wl1251_tx_send_packet(wl, skb, info);
309         if (ret < 0)
310                 return ret;
311
312         wl1251_tx_trigger(wl);
313
314         return ret;
315 }
316
317 void wl1251_tx_work(struct work_struct *work)
318 {
319         struct wl1251 *wl = container_of(work, struct wl1251, tx_work);
320         struct sk_buff *skb;
321         bool woken_up = false;
322         int ret;
323
324         mutex_lock(&wl->mutex);
325
326         if (unlikely(wl->state == WL1251_STATE_OFF))
327                 goto out;
328
329         while ((skb = skb_dequeue(&wl->tx_queue))) {
330                 if (!woken_up) {
331                         ret = wl1251_ps_elp_wakeup(wl);
332                         if (ret < 0)
333                                 goto out;
334                         woken_up = true;
335                 }
336
337                 ret = wl1251_tx_frame(wl, skb);
338                 if (ret == -EBUSY) {
339                         skb_queue_head(&wl->tx_queue, skb);
340                         goto out;
341                 } else if (ret < 0) {
342                         dev_kfree_skb(skb);
343                         goto out;
344                 }
345         }
346
347 out:
348         if (woken_up)
349                 wl1251_ps_elp_sleep(wl);
350
351         mutex_unlock(&wl->mutex);
352 }
353
354 static const char *wl1251_tx_parse_status(u8 status)
355 {
356         /* 8 bit status field, one character per bit plus null */
357         static char buf[9];
358         int i = 0;
359
360         memset(buf, 0, sizeof(buf));
361
362         if (status & TX_DMA_ERROR)
363                 buf[i++] = 'm';
364         if (status & TX_DISABLED)
365                 buf[i++] = 'd';
366         if (status & TX_RETRY_EXCEEDED)
367                 buf[i++] = 'r';
368         if (status & TX_TIMEOUT)
369                 buf[i++] = 't';
370         if (status & TX_KEY_NOT_FOUND)
371                 buf[i++] = 'k';
372         if (status & TX_ENCRYPT_FAIL)
373                 buf[i++] = 'e';
374         if (status & TX_UNAVAILABLE_PRIORITY)
375                 buf[i++] = 'p';
376
377         /* bit 0 is unused apparently */
378
379         return buf;
380 }
381
382 static void wl1251_tx_packet_cb(struct wl1251 *wl,
383                                 struct tx_result *result)
384 {
385         struct ieee80211_tx_info *info;
386         struct sk_buff *skb;
387         int hdrlen;
388         u8 *frame;
389
390         skb = wl->tx_frames[result->id];
391         if (skb == NULL) {
392                 wl1251_error("SKB for packet %d is NULL", result->id);
393                 return;
394         }
395
396         info = IEEE80211_SKB_CB(skb);
397
398         if (!(info->flags & IEEE80211_TX_CTL_NO_ACK) &&
399             (result->status == TX_SUCCESS))
400                 info->flags |= IEEE80211_TX_STAT_ACK;
401
402         info->status.rates[0].count = result->ack_failures + 1;
403         wl->stats.retry_count += result->ack_failures;
404
405         /*
406          * We have to remove our private TX header before pushing
407          * the skb back to mac80211.
408          */
409         frame = skb_pull(skb, sizeof(struct tx_double_buffer_desc));
410         if (info->control.hw_key &&
411             info->control.hw_key->alg == ALG_TKIP) {
412                 hdrlen = ieee80211_get_hdrlen_from_skb(skb);
413                 memmove(frame + WL1251_TKIP_IV_SPACE, frame, hdrlen);
414                 skb_pull(skb, WL1251_TKIP_IV_SPACE);
415         }
416
417         wl1251_debug(DEBUG_TX, "tx status id %u skb 0x%p failures %u rate 0x%x"
418                      " status 0x%x (%s)",
419                      result->id, skb, result->ack_failures, result->rate,
420                      result->status, wl1251_tx_parse_status(result->status));
421
422
423         ieee80211_tx_status(wl->hw, skb);
424
425         wl->tx_frames[result->id] = NULL;
426 }
427
428 /* Called upon reception of a TX complete interrupt */
429 void wl1251_tx_complete(struct wl1251 *wl)
430 {
431         int i, result_index, num_complete = 0, queue_len;
432         struct tx_result result[FW_TX_CMPLT_BLOCK_SIZE], *result_ptr;
433         unsigned long flags;
434
435         if (unlikely(wl->state != WL1251_STATE_ON))
436                 return;
437
438         /* First we read the result */
439         wl1251_mem_read(wl, wl->data_path->tx_complete_addr,
440                             result, sizeof(result));
441
442         result_index = wl->next_tx_complete;
443
444         for (i = 0; i < ARRAY_SIZE(result); i++) {
445                 result_ptr = &result[result_index];
446
447                 if (result_ptr->done_1 == 1 &&
448                     result_ptr->done_2 == 1) {
449                         wl1251_tx_packet_cb(wl, result_ptr);
450
451                         result_ptr->done_1 = 0;
452                         result_ptr->done_2 = 0;
453
454                         result_index = (result_index + 1) &
455                                 (FW_TX_CMPLT_BLOCK_SIZE - 1);
456                         num_complete++;
457                 } else {
458                         break;
459                 }
460         }
461
462         queue_len = skb_queue_len(&wl->tx_queue);
463
464         if ((num_complete > 0) && (queue_len > 0)) {
465                 /* firmware buffer has space, reschedule tx_work */
466                 wl1251_debug(DEBUG_TX, "tx_complete: reschedule tx_work");
467                 ieee80211_queue_work(wl->hw, &wl->tx_work);
468         }
469
470         if (wl->tx_queue_stopped &&
471             queue_len <= WL1251_TX_QUEUE_LOW_WATERMARK) {
472                 /* tx_queue has space, restart queues */
473                 wl1251_debug(DEBUG_TX, "tx_complete: waking queues");
474                 spin_lock_irqsave(&wl->wl_lock, flags);
475                 ieee80211_wake_queues(wl->hw);
476                 wl->tx_queue_stopped = false;
477                 spin_unlock_irqrestore(&wl->wl_lock, flags);
478         }
479
480         /* Every completed frame needs to be acknowledged */
481         if (num_complete) {
482                 /*
483                  * If we've wrapped, we have to clear
484                  * the results in 2 steps.
485                  */
486                 if (result_index > wl->next_tx_complete) {
487                         /* Only 1 write is needed */
488                         wl1251_mem_write(wl,
489                                          wl->data_path->tx_complete_addr +
490                                          (wl->next_tx_complete *
491                                           sizeof(struct tx_result)),
492                                          &result[wl->next_tx_complete],
493                                          num_complete *
494                                          sizeof(struct tx_result));
495
496
497                 } else if (result_index < wl->next_tx_complete) {
498                         /* 2 writes are needed */
499                         wl1251_mem_write(wl,
500                                          wl->data_path->tx_complete_addr +
501                                          (wl->next_tx_complete *
502                                           sizeof(struct tx_result)),
503                                          &result[wl->next_tx_complete],
504                                          (FW_TX_CMPLT_BLOCK_SIZE -
505                                           wl->next_tx_complete) *
506                                          sizeof(struct tx_result));
507
508                         wl1251_mem_write(wl,
509                                          wl->data_path->tx_complete_addr,
510                                          result,
511                                          (num_complete -
512                                           FW_TX_CMPLT_BLOCK_SIZE +
513                                           wl->next_tx_complete) *
514                                          sizeof(struct tx_result));
515
516                 } else {
517                         /* We have to write the whole array */
518                         wl1251_mem_write(wl,
519                                          wl->data_path->tx_complete_addr,
520                                          result,
521                                          FW_TX_CMPLT_BLOCK_SIZE *
522                                          sizeof(struct tx_result));
523                 }
524
525         }
526
527         wl->next_tx_complete = result_index;
528 }
529
530 /* caller must hold wl->mutex */
531 void wl1251_tx_flush(struct wl1251 *wl)
532 {
533         int i;
534         struct sk_buff *skb;
535         struct ieee80211_tx_info *info;
536
537         /* TX failure */
538 /*      control->flags = 0; FIXME */
539
540         while ((skb = skb_dequeue(&wl->tx_queue))) {
541                 info = IEEE80211_SKB_CB(skb);
542
543                 wl1251_debug(DEBUG_TX, "flushing skb 0x%p", skb);
544
545                 if (!(info->flags & IEEE80211_TX_CTL_REQ_TX_STATUS))
546                                 continue;
547
548                 ieee80211_tx_status(wl->hw, skb);
549         }
550
551         for (i = 0; i < FW_TX_CMPLT_BLOCK_SIZE; i++)
552                 if (wl->tx_frames[i] != NULL) {
553                         skb = wl->tx_frames[i];
554                         info = IEEE80211_SKB_CB(skb);
555
556                         if (!(info->flags & IEEE80211_TX_CTL_REQ_TX_STATUS))
557                                 continue;
558
559                         ieee80211_tx_status(wl->hw, skb);
560                         wl->tx_frames[i] = NULL;
561                 }
562 }