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