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