]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/net/wireless/wl12xx/wl1251_tx.c
mac80211: use cipher suite selectors
[mirror_ubuntu-artful-kernel.git] / drivers / net / wireless / wl12xx / wl1251_tx.c
CommitLineData
2f01a1f5 1/*
80301cdc 2 * This file is part of wl1251
2f01a1f5
KV
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
13674118 28#include "wl1251.h"
29d904c4 29#include "wl1251_reg.h"
9f2ad4fb 30#include "wl1251_tx.h"
ef2f8d45 31#include "wl1251_ps.h"
0764de64 32#include "wl1251_io.h"
2f01a1f5 33
80301cdc 34static bool wl1251_tx_double_buffer_busy(struct wl1251 *wl, u32 data_out_count)
2f01a1f5
KV
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
80301cdc 55static int wl1251_tx_path_status(struct wl1251 *wl)
2f01a1f5
KV
56{
57 u32 status, addr, data_out_count;
58 bool busy;
59
60 addr = wl->data_path->tx_control_addr;
80301cdc 61 status = wl1251_mem_read32(wl, addr);
2f01a1f5 62 data_out_count = status & TX_STATUS_DATA_OUT_COUNT_MASK;
9f2ad4fb 63 busy = wl1251_tx_double_buffer_busy(wl, data_out_count);
2f01a1f5
KV
64
65 if (busy)
66 return -EBUSY;
67
68 return 0;
69}
70
80301cdc 71static int wl1251_tx_id(struct wl1251 *wl, struct sk_buff *skb)
2f01a1f5
KV
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
9f2ad4fb 84static void wl1251_tx_control(struct tx_double_buffer_desc *tx_hdr,
2f01a1f5
KV
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
9f2ad4fb 112static void wl1251_tx_frag_block_num(struct tx_double_buffer_desc *tx_hdr)
2f01a1f5
KV
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
1ab36d68 120 payload_len = le16_to_cpu(tx_hdr->length) + MAX_MSDU_SECURITY_LENGTH;
2f01a1f5
KV
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
80301cdc 145static int wl1251_tx_fill_hdr(struct wl1251 *wl, struct sk_buff *skb,
2f01a1f5
KV
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
9f2ad4fb 156 id = wl1251_tx_id(wl, skb);
2f01a1f5
KV
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
49e1b9fa 170 tx_hdr->xmit_queue = wl1251_tx_get_queue(skb_get_queue_mapping(skb));
2f01a1f5 171
9f2ad4fb
JO
172 wl1251_tx_control(tx_hdr, control, fc);
173 wl1251_tx_frag_block_num(tx_hdr);
2f01a1f5
KV
174
175 return 0;
176}
177
178/* We copy the packet to the target */
80301cdc 179static int wl1251_tx_send_packet(struct wl1251 *wl, struct sk_buff *skb,
2f01a1f5
KV
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 &&
97359d12 192 control->control.hw_key->cipher == WLAN_CIPHER_SUITE_TKIP) {
2f01a1f5 193 int hdrlen;
1ab36d68
JL
194 __le16 fc;
195 u16 length;
2f01a1f5
KV
196 u8 *pos;
197
1ab36d68
JL
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);
2f01a1f5
KV
201
202 hdrlen = ieee80211_hdrlen(fc);
203
9f2ad4fb
JO
204 pos = skb_push(skb, WL1251_TKIP_IV_SPACE);
205 memmove(pos, pos + WL1251_TKIP_IV_SPACE,
2f01a1f5
KV
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;
80301cdc 215 wl1251_debug(DEBUG_TX, "skb offset %d", offset);
2f01a1f5
KV
216
217 /* check whether the current skb can be used */
218 if (!skb_cloned(skb) && (skb_tailroom(skb) >= offset)) {
219 unsigned char *src = skb->data;
220
221 /* align the buffer on a 4-byte boundary */
222 skb_reserve(skb, offset);
223 memmove(skb->data, src, skb->len);
46cb35f5 224 tx_hdr = (struct tx_double_buffer_desc *) skb->data;
2f01a1f5 225 } else {
80301cdc 226 wl1251_info("No handler, fixme!");
2f01a1f5
KV
227 return -EINVAL;
228 }
229 }
230
231 /* Our skb->data at this point includes the HW header */
9f2ad4fb 232 len = WL1251_TX_ALIGN(skb->len);
2f01a1f5
KV
233
234 if (wl->data_in_count & 0x1)
235 addr = wl->data_path->tx_packet_ring_addr +
236 wl->data_path->tx_packet_ring_chunk_size;
237 else
238 addr = wl->data_path->tx_packet_ring_addr;
239
0764de64 240 wl1251_mem_write(wl, addr, skb->data, len);
2f01a1f5 241
49e1b9fa
KV
242 wl1251_debug(DEBUG_TX, "tx id %u skb 0x%p payload %u rate 0x%x "
243 "queue %d", tx_hdr->id, skb, tx_hdr->length,
244 tx_hdr->rate, tx_hdr->xmit_queue);
2f01a1f5
KV
245
246 return 0;
247}
248
80301cdc 249static void wl1251_tx_trigger(struct wl1251 *wl)
2f01a1f5
KV
250{
251 u32 data, addr;
252
253 if (wl->data_in_count & 0x1) {
254 addr = ACX_REG_INTERRUPT_TRIG_H;
255 data = INTR_TRIG_TX_PROC1;
256 } else {
257 addr = ACX_REG_INTERRUPT_TRIG;
258 data = INTR_TRIG_TX_PROC0;
259 }
260
80301cdc 261 wl1251_reg_write32(wl, addr, data);
2f01a1f5
KV
262
263 /* Bumping data in */
264 wl->data_in_count = (wl->data_in_count + 1) &
265 TX_STATUS_DATA_OUT_COUNT_MASK;
266}
267
268/* caller must hold wl->mutex */
80301cdc 269static int wl1251_tx_frame(struct wl1251 *wl, struct sk_buff *skb)
2f01a1f5
KV
270{
271 struct ieee80211_tx_info *info;
272 int ret = 0;
273 u8 idx;
274
275 info = IEEE80211_SKB_CB(skb);
276
277 if (info->control.hw_key) {
278 idx = info->control.hw_key->hw_key_idx;
279 if (unlikely(wl->default_key != idx)) {
80301cdc 280 ret = wl1251_acx_default_key(wl, idx);
2f01a1f5
KV
281 if (ret < 0)
282 return ret;
283 }
284 }
285
9f2ad4fb 286 ret = wl1251_tx_path_status(wl);
2f01a1f5
KV
287 if (ret < 0)
288 return ret;
289
9f2ad4fb 290 ret = wl1251_tx_fill_hdr(wl, skb, info);
2f01a1f5
KV
291 if (ret < 0)
292 return ret;
293
9f2ad4fb 294 ret = wl1251_tx_send_packet(wl, skb, info);
2f01a1f5
KV
295 if (ret < 0)
296 return ret;
297
9f2ad4fb 298 wl1251_tx_trigger(wl);
2f01a1f5
KV
299
300 return ret;
301}
302
9f2ad4fb 303void wl1251_tx_work(struct work_struct *work)
2f01a1f5 304{
80301cdc 305 struct wl1251 *wl = container_of(work, struct wl1251, tx_work);
2f01a1f5
KV
306 struct sk_buff *skb;
307 bool woken_up = false;
308 int ret;
309
310 mutex_lock(&wl->mutex);
311
80301cdc 312 if (unlikely(wl->state == WL1251_STATE_OFF))
2f01a1f5
KV
313 goto out;
314
315 while ((skb = skb_dequeue(&wl->tx_queue))) {
316 if (!woken_up) {
80301cdc 317 ret = wl1251_ps_elp_wakeup(wl);
c5483b71
KV
318 if (ret < 0)
319 goto out;
2f01a1f5
KV
320 woken_up = true;
321 }
322
9f2ad4fb 323 ret = wl1251_tx_frame(wl, skb);
2f01a1f5
KV
324 if (ret == -EBUSY) {
325 /* firmware buffer is full, stop queues */
80301cdc 326 wl1251_debug(DEBUG_TX, "tx_work: fw buffer full, "
2f01a1f5
KV
327 "stop queues");
328 ieee80211_stop_queues(wl->hw);
329 wl->tx_queue_stopped = true;
330 skb_queue_head(&wl->tx_queue, skb);
331 goto out;
332 } else if (ret < 0) {
333 dev_kfree_skb(skb);
334 goto out;
335 }
336 }
337
338out:
339 if (woken_up)
80301cdc 340 wl1251_ps_elp_sleep(wl);
2f01a1f5
KV
341
342 mutex_unlock(&wl->mutex);
343}
344
9f2ad4fb 345static const char *wl1251_tx_parse_status(u8 status)
2f01a1f5
KV
346{
347 /* 8 bit status field, one character per bit plus null */
348 static char buf[9];
349 int i = 0;
350
351 memset(buf, 0, sizeof(buf));
352
353 if (status & TX_DMA_ERROR)
354 buf[i++] = 'm';
355 if (status & TX_DISABLED)
356 buf[i++] = 'd';
357 if (status & TX_RETRY_EXCEEDED)
358 buf[i++] = 'r';
359 if (status & TX_TIMEOUT)
360 buf[i++] = 't';
361 if (status & TX_KEY_NOT_FOUND)
362 buf[i++] = 'k';
363 if (status & TX_ENCRYPT_FAIL)
364 buf[i++] = 'e';
365 if (status & TX_UNAVAILABLE_PRIORITY)
366 buf[i++] = 'p';
367
368 /* bit 0 is unused apparently */
369
370 return buf;
371}
372
80301cdc 373static void wl1251_tx_packet_cb(struct wl1251 *wl,
2f01a1f5
KV
374 struct tx_result *result)
375{
376 struct ieee80211_tx_info *info;
377 struct sk_buff *skb;
378 int hdrlen, ret;
379 u8 *frame;
380
381 skb = wl->tx_frames[result->id];
382 if (skb == NULL) {
80301cdc 383 wl1251_error("SKB for packet %d is NULL", result->id);
2f01a1f5
KV
384 return;
385 }
386
387 info = IEEE80211_SKB_CB(skb);
388
389 if (!(info->flags & IEEE80211_TX_CTL_NO_ACK) &&
390 (result->status == TX_SUCCESS))
391 info->flags |= IEEE80211_TX_STAT_ACK;
392
393 info->status.rates[0].count = result->ack_failures + 1;
394 wl->stats.retry_count += result->ack_failures;
395
396 /*
397 * We have to remove our private TX header before pushing
398 * the skb back to mac80211.
399 */
400 frame = skb_pull(skb, sizeof(struct tx_double_buffer_desc));
401 if (info->control.hw_key &&
97359d12 402 info->control.hw_key->cipher == WLAN_CIPHER_SUITE_TKIP) {
2f01a1f5 403 hdrlen = ieee80211_get_hdrlen_from_skb(skb);
9f2ad4fb
JO
404 memmove(frame + WL1251_TKIP_IV_SPACE, frame, hdrlen);
405 skb_pull(skb, WL1251_TKIP_IV_SPACE);
2f01a1f5
KV
406 }
407
80301cdc 408 wl1251_debug(DEBUG_TX, "tx status id %u skb 0x%p failures %u rate 0x%x"
2f01a1f5
KV
409 " status 0x%x (%s)",
410 result->id, skb, result->ack_failures, result->rate,
9f2ad4fb 411 result->status, wl1251_tx_parse_status(result->status));
2f01a1f5
KV
412
413
414 ieee80211_tx_status(wl->hw, skb);
415
416 wl->tx_frames[result->id] = NULL;
417
418 if (wl->tx_queue_stopped) {
80301cdc 419 wl1251_debug(DEBUG_TX, "cb: queue was stopped");
2f01a1f5
KV
420
421 skb = skb_dequeue(&wl->tx_queue);
422
423 /* The skb can be NULL because tx_work might have been
424 scheduled before the queue was stopped making the
425 queue empty */
426
427 if (skb) {
9f2ad4fb 428 ret = wl1251_tx_frame(wl, skb);
2f01a1f5
KV
429 if (ret == -EBUSY) {
430 /* firmware buffer is still full */
80301cdc 431 wl1251_debug(DEBUG_TX, "cb: fw buffer "
2f01a1f5
KV
432 "still full");
433 skb_queue_head(&wl->tx_queue, skb);
434 return;
435 } else if (ret < 0) {
436 dev_kfree_skb(skb);
437 return;
438 }
439 }
440
80301cdc 441 wl1251_debug(DEBUG_TX, "cb: waking queues");
2f01a1f5
KV
442 ieee80211_wake_queues(wl->hw);
443 wl->tx_queue_stopped = false;
444 }
445}
446
447/* Called upon reception of a TX complete interrupt */
80301cdc 448void wl1251_tx_complete(struct wl1251 *wl)
2f01a1f5
KV
449{
450 int i, result_index, num_complete = 0;
451 struct tx_result result[FW_TX_CMPLT_BLOCK_SIZE], *result_ptr;
452
80301cdc 453 if (unlikely(wl->state != WL1251_STATE_ON))
2f01a1f5
KV
454 return;
455
456 /* First we read the result */
0764de64 457 wl1251_mem_read(wl, wl->data_path->tx_complete_addr,
2f01a1f5
KV
458 result, sizeof(result));
459
460 result_index = wl->next_tx_complete;
461
462 for (i = 0; i < ARRAY_SIZE(result); i++) {
463 result_ptr = &result[result_index];
464
465 if (result_ptr->done_1 == 1 &&
466 result_ptr->done_2 == 1) {
9f2ad4fb 467 wl1251_tx_packet_cb(wl, result_ptr);
2f01a1f5
KV
468
469 result_ptr->done_1 = 0;
470 result_ptr->done_2 = 0;
471
472 result_index = (result_index + 1) &
473 (FW_TX_CMPLT_BLOCK_SIZE - 1);
474 num_complete++;
475 } else {
476 break;
477 }
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 */
0764de64
BC
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));
2f01a1f5
KV
495
496
497 } else if (result_index < wl->next_tx_complete) {
498 /* 2 writes are needed */
0764de64
BC
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));
2f01a1f5
KV
515
516 } else {
517 /* We have to write the whole array */
0764de64
BC
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));
2f01a1f5
KV
523 }
524
525 }
526
527 wl->next_tx_complete = result_index;
528}
529
530/* caller must hold wl->mutex */
80301cdc 531void wl1251_tx_flush(struct wl1251 *wl)
2f01a1f5
KV
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
80301cdc 543 wl1251_debug(DEBUG_TX, "flushing skb 0x%p", skb);
2f01a1f5
KV
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}