blob: 6426d2cae6de0e7ccbc40425df01097b5016109e [file] [log] [blame]
Christian Lamparter0a5fb842009-06-23 10:39:12 -05001/*
2 * Common code for mac80211 Prism54 drivers
3 *
4 * Copyright (c) 2006, Michael Wu <flamingice@sourmilk.net>
5 * Copyright (c) 2007-2009, Christian Lamparter <chunkeey@web.de>
6 * Copyright 2008, Johannes Berg <johannes@sipsolutions.net>
7 *
8 * Based on:
9 * - the islsm (softmac prism54) driver, which is:
10 * Copyright 2004-2006 Jean-Baptiste Note <jbnote@gmail.com>, et al.
11 * - stlc45xx driver
12 * Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies).
13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License version 2 as
16 * published by the Free Software Foundation.
17 */
18
19#include <linux/init.h>
20#include <linux/firmware.h>
21#include <linux/etherdevice.h>
22
23#include <net/mac80211.h>
24
25#include "p54.h"
26#include "lmac.h"
27
28#ifdef P54_MM_DEBUG
29static void p54_dump_tx_queue(struct p54_common *priv)
30{
31 unsigned long flags;
32 struct ieee80211_tx_info *info;
33 struct p54_tx_info *range;
34 struct sk_buff *skb;
35 struct p54_hdr *hdr;
36 unsigned int i = 0;
37 u32 prev_addr;
38 u32 largest_hole = 0, free;
39
40 spin_lock_irqsave(&priv->tx_queue.lock, flags);
41 printk(KERN_DEBUG "%s: / --- tx queue dump (%d entries) --- \n",
42 wiphy_name(priv->hw->wiphy), skb_queue_len(&priv->tx_queue));
43
44 prev_addr = priv->rx_start;
45 skb_queue_walk(&priv->tx_queue, skb) {
46 info = IEEE80211_SKB_CB(skb);
47 range = (void *) info->rate_driver_data;
48 hdr = (void *) skb->data;
49
50 free = range->start_addr - prev_addr;
51 printk(KERN_DEBUG "%s: | [%02d] => [skb:%p skb_len:0x%04x "
52 "hdr:{flags:%02x len:%04x req_id:%04x type:%02x} "
53 "mem:{start:%04x end:%04x, free:%d}]\n",
54 wiphy_name(priv->hw->wiphy), i++, skb, skb->len,
55 le16_to_cpu(hdr->flags), le16_to_cpu(hdr->len),
56 le32_to_cpu(hdr->req_id), le16_to_cpu(hdr->type),
57 range->start_addr, range->end_addr, free);
58
59 prev_addr = range->end_addr;
60 largest_hole = max(largest_hole, free);
61 }
62 free = priv->rx_end - prev_addr;
63 largest_hole = max(largest_hole, free);
64 printk(KERN_DEBUG "%s: \\ --- [free: %d], largest free block: %d ---\n",
65 wiphy_name(priv->hw->wiphy), free, largest_hole);
66 spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
67}
68#endif /* P54_MM_DEBUG */
69
70/*
71 * So, the firmware is somewhat stupid and doesn't know what places in its
72 * memory incoming data should go to. By poking around in the firmware, we
73 * can find some unused memory to upload our packets to. However, data that we
74 * want the card to TX needs to stay intact until the card has told us that
75 * it is done with it. This function finds empty places we can upload to and
76 * marks allocated areas as reserved if necessary. p54_find_and_unlink_skb or
77 * p54_free_skb frees allocated areas.
78 */
79static int p54_assign_address(struct p54_common *priv, struct sk_buff *skb)
80{
81 struct sk_buff *entry, *target_skb = NULL;
82 struct ieee80211_tx_info *info;
83 struct p54_tx_info *range;
84 struct p54_hdr *data = (void *) skb->data;
85 unsigned long flags;
86 u32 last_addr = priv->rx_start;
87 u32 target_addr = priv->rx_start;
88 u16 len = priv->headroom + skb->len + priv->tailroom + 3;
89
90 if (unlikely(WARN_ON(!skb || !priv)))
91 return -EINVAL;
92
93 info = IEEE80211_SKB_CB(skb);
94 range = (void *) info->rate_driver_data;
95 len = (range->extra_len + len) & ~0x3;
96
97 spin_lock_irqsave(&priv->tx_queue.lock, flags);
98 if (unlikely(skb_queue_len(&priv->tx_queue) == 32)) {
99 /*
100 * The tx_queue is now really full.
101 *
102 * TODO: check if the device has crashed and reset it.
103 */
104 spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
105 return -EBUSY;
106 }
107
108 skb_queue_walk(&priv->tx_queue, entry) {
109 u32 hole_size;
110 info = IEEE80211_SKB_CB(entry);
111 range = (void *) info->rate_driver_data;
112 hole_size = range->start_addr - last_addr;
113
114 if (!entry->next) {
115 spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
116 return -ENOSPC;
117 }
118
119 if (!target_skb && hole_size >= len) {
120 target_skb = entry->prev;
121 hole_size -= len;
122 target_addr = last_addr;
123 break;
124 }
125 last_addr = range->end_addr;
126 }
127 if (unlikely(!target_skb)) {
128 if (priv->rx_end - last_addr >= len) {
129 target_skb = priv->tx_queue.prev;
130 if (!skb_queue_empty(&priv->tx_queue)) {
131 info = IEEE80211_SKB_CB(target_skb);
132 range = (void *)info->rate_driver_data;
133 target_addr = range->end_addr;
134 }
135 } else {
136 spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
137 return -ENOSPC;
138 }
139 }
140
141 info = IEEE80211_SKB_CB(skb);
142 range = (void *) info->rate_driver_data;
143 range->start_addr = target_addr;
144 range->end_addr = target_addr + len;
145 __skb_queue_after(&priv->tx_queue, target_skb, skb);
146 spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
147 data->req_id = cpu_to_le32(target_addr + priv->headroom);
148 return 0;
149}
150
151static void p54_tx_pending(struct p54_common *priv)
152{
153 struct sk_buff *skb;
154 int ret;
155
156 if (unlikely(WARN_ON(!priv)))
157 return ;
158
159 skb = skb_dequeue(&priv->tx_pending);
160 if (unlikely(!skb))
161 return ;
162
163 ret = p54_assign_address(priv, skb);
164 if (unlikely(ret))
165 skb_queue_head(&priv->tx_pending, skb);
166 else
167 priv->tx(priv->hw, skb);
168}
169
170static void p54_wake_queues(struct p54_common *priv)
171{
172 unsigned long flags;
173 unsigned int i;
174
175 if (unlikely(priv->mode == NL80211_IFTYPE_UNSPECIFIED))
176 return ;
177
178 p54_tx_pending(priv);
179
180 spin_lock_irqsave(&priv->tx_stats_lock, flags);
181 for (i = 0; i < priv->hw->queues; i++) {
182 if (priv->tx_stats[i + P54_QUEUE_DATA].len <
183 priv->tx_stats[i + P54_QUEUE_DATA].limit)
184 ieee80211_wake_queue(priv->hw, i);
185 }
186 spin_unlock_irqrestore(&priv->tx_stats_lock, flags);
187}
188
189static int p54_tx_qos_accounting_alloc(struct p54_common *priv,
190 struct sk_buff *skb,
191 const u16 p54_queue)
192{
193 struct ieee80211_tx_queue_stats *queue;
194 unsigned long flags;
195
196 if (WARN_ON(p54_queue > P54_QUEUE_NUM))
197 return -EINVAL;
198
199 queue = &priv->tx_stats[p54_queue];
200
201 spin_lock_irqsave(&priv->tx_stats_lock, flags);
Christian Lamparter2ffa5fe2009-07-06 15:18:13 +0200202 if (unlikely(queue->len >= queue->limit && IS_QOS_QUEUE(p54_queue))) {
Christian Lamparter0a5fb842009-06-23 10:39:12 -0500203 spin_unlock_irqrestore(&priv->tx_stats_lock, flags);
204 return -ENOSPC;
205 }
206
207 queue->len++;
208 queue->count++;
209
210 if (unlikely(queue->len == queue->limit && IS_QOS_QUEUE(p54_queue))) {
211 u16 ac_queue = p54_queue - P54_QUEUE_DATA;
212 ieee80211_stop_queue(priv->hw, ac_queue);
213 }
214
215 spin_unlock_irqrestore(&priv->tx_stats_lock, flags);
216 return 0;
217}
218
219static void p54_tx_qos_accounting_free(struct p54_common *priv,
220 struct sk_buff *skb)
221{
222 if (skb && IS_DATA_FRAME(skb)) {
223 struct p54_hdr *hdr = (void *) skb->data;
224 struct p54_tx_data *data = (void *) hdr->data;
Christian Lamparter2ffa5fe2009-07-06 15:18:13 +0200225 unsigned long flags;
Christian Lamparter0a5fb842009-06-23 10:39:12 -0500226
Christian Lamparter2ffa5fe2009-07-06 15:18:13 +0200227 spin_lock_irqsave(&priv->tx_stats_lock, flags);
Christian Lamparter0a5fb842009-06-23 10:39:12 -0500228 priv->tx_stats[data->hw_queue].len--;
Christian Lamparter2ffa5fe2009-07-06 15:18:13 +0200229 spin_unlock_irqrestore(&priv->tx_stats_lock, flags);
Christian Lamparter0a5fb842009-06-23 10:39:12 -0500230 }
231 p54_wake_queues(priv);
232}
233
234void p54_free_skb(struct ieee80211_hw *dev, struct sk_buff *skb)
235{
236 struct p54_common *priv = dev->priv;
237 if (unlikely(!skb))
238 return ;
239
240 skb_unlink(skb, &priv->tx_queue);
241 p54_tx_qos_accounting_free(priv, skb);
242 dev_kfree_skb_any(skb);
243}
244EXPORT_SYMBOL_GPL(p54_free_skb);
245
246static struct sk_buff *p54_find_and_unlink_skb(struct p54_common *priv,
247 const __le32 req_id)
248{
249 struct sk_buff *entry;
250 unsigned long flags;
251
252 spin_lock_irqsave(&priv->tx_queue.lock, flags);
253 skb_queue_walk(&priv->tx_queue, entry) {
254 struct p54_hdr *hdr = (struct p54_hdr *) entry->data;
255
256 if (hdr->req_id == req_id) {
257 __skb_unlink(entry, &priv->tx_queue);
258 spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
259 p54_tx_qos_accounting_free(priv, entry);
260 return entry;
261 }
262 }
263 spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
264 return NULL;
265}
266
267void p54_tx(struct p54_common *priv, struct sk_buff *skb)
268{
269 if (unlikely(WARN_ON(!priv)))
270 return ;
271
272 skb_queue_tail(&priv->tx_pending, skb);
273 p54_tx_pending(priv);
274}
275
276static int p54_rssi_to_dbm(struct p54_common *priv, int rssi)
277{
278 int band = priv->hw->conf.channel->band;
279
280 if (priv->rxhw != 5)
281 return ((rssi * priv->rssical_db[band].mul) / 64 +
282 priv->rssical_db[band].add) / 4;
283 else
284 /*
285 * TODO: find the correct formula
286 */
287 return ((rssi * priv->rssical_db[band].mul) / 64 +
288 priv->rssical_db[band].add) / 4;
289}
290
291static int p54_rx_data(struct p54_common *priv, struct sk_buff *skb)
292{
293 struct p54_rx_data *hdr = (struct p54_rx_data *) skb->data;
294 struct ieee80211_rx_status *rx_status = IEEE80211_SKB_RXCB(skb);
295 u16 freq = le16_to_cpu(hdr->freq);
296 size_t header_len = sizeof(*hdr);
297 u32 tsf32;
298 u8 rate = hdr->rate & 0xf;
299
300 /*
301 * If the device is in a unspecified state we have to
302 * ignore all data frames. Else we could end up with a
303 * nasty crash.
304 */
305 if (unlikely(priv->mode == NL80211_IFTYPE_UNSPECIFIED))
306 return 0;
307
308 if (!(hdr->flags & cpu_to_le16(P54_HDR_FLAG_DATA_IN_FCS_GOOD)))
309 return 0;
310
311 if (hdr->decrypt_status == P54_DECRYPT_OK)
312 rx_status->flag |= RX_FLAG_DECRYPTED;
313 if ((hdr->decrypt_status == P54_DECRYPT_FAIL_MICHAEL) ||
314 (hdr->decrypt_status == P54_DECRYPT_FAIL_TKIP))
315 rx_status->flag |= RX_FLAG_MMIC_ERROR;
316
317 rx_status->signal = p54_rssi_to_dbm(priv, hdr->rssi);
318 rx_status->noise = priv->noise;
319 if (hdr->rate & 0x10)
320 rx_status->flag |= RX_FLAG_SHORTPRE;
321 if (priv->hw->conf.channel->band == IEEE80211_BAND_5GHZ)
322 rx_status->rate_idx = (rate < 4) ? 0 : rate - 4;
323 else
324 rx_status->rate_idx = rate;
325
326 rx_status->freq = freq;
327 rx_status->band = priv->hw->conf.channel->band;
328 rx_status->antenna = hdr->antenna;
329
330 tsf32 = le32_to_cpu(hdr->tsf32);
331 if (tsf32 < priv->tsf_low32)
332 priv->tsf_high32++;
333 rx_status->mactime = ((u64)priv->tsf_high32) << 32 | tsf32;
334 priv->tsf_low32 = tsf32;
335
336 rx_status->flag |= RX_FLAG_TSFT;
337
338 if (hdr->flags & cpu_to_le16(P54_HDR_FLAG_DATA_ALIGN))
339 header_len += hdr->align[0];
340
341 skb_pull(skb, header_len);
342 skb_trim(skb, le16_to_cpu(hdr->len));
343 ieee80211_rx_irqsafe(priv->hw, skb);
344
345 queue_delayed_work(priv->hw->workqueue, &priv->work,
346 msecs_to_jiffies(P54_STATISTICS_UPDATE));
347
348 return -1;
349}
350
351static void p54_rx_frame_sent(struct p54_common *priv, struct sk_buff *skb)
352{
353 struct p54_hdr *hdr = (struct p54_hdr *) skb->data;
354 struct p54_frame_sent *payload = (struct p54_frame_sent *) hdr->data;
355 struct ieee80211_tx_info *info;
356 struct p54_hdr *entry_hdr;
357 struct p54_tx_data *entry_data;
358 struct sk_buff *entry;
359 unsigned int pad = 0, frame_len;
360 int count, idx;
361
362 entry = p54_find_and_unlink_skb(priv, hdr->req_id);
363 if (unlikely(!entry))
364 return ;
365
366 frame_len = entry->len;
367 info = IEEE80211_SKB_CB(entry);
368 entry_hdr = (struct p54_hdr *) entry->data;
369 entry_data = (struct p54_tx_data *) entry_hdr->data;
370 priv->stats.dot11ACKFailureCount += payload->tries - 1;
371
372 /*
373 * Frames in P54_QUEUE_FWSCAN and P54_QUEUE_BEACON are
374 * generated by the driver. Therefore tx_status is bogus
375 * and we don't want to confuse the mac80211 stack.
376 */
377 if (unlikely(entry_data->hw_queue < P54_QUEUE_FWSCAN)) {
378 if (entry_data->hw_queue == P54_QUEUE_BEACON &&
379 hdr->req_id == priv->beacon_req_id)
380 priv->beacon_req_id = cpu_to_le32(0);
381
382 dev_kfree_skb_any(entry);
383 return ;
384 }
385
386 /*
387 * Clear manually, ieee80211_tx_info_clear_status would
388 * clear the counts too and we need them.
389 */
390 memset(&info->status.ampdu_ack_len, 0,
391 sizeof(struct ieee80211_tx_info) -
392 offsetof(struct ieee80211_tx_info, status.ampdu_ack_len));
393 BUILD_BUG_ON(offsetof(struct ieee80211_tx_info,
394 status.ampdu_ack_len) != 23);
395
396 if (entry_hdr->flags & cpu_to_le16(P54_HDR_FLAG_DATA_ALIGN))
397 pad = entry_data->align[0];
398
399 /* walk through the rates array and adjust the counts */
400 count = payload->tries;
401 for (idx = 0; idx < 4; idx++) {
402 if (count >= info->status.rates[idx].count) {
403 count -= info->status.rates[idx].count;
404 } else if (count > 0) {
405 info->status.rates[idx].count = count;
406 count = 0;
407 } else {
408 info->status.rates[idx].idx = -1;
409 info->status.rates[idx].count = 0;
410 }
411 }
412
413 if (!(info->flags & IEEE80211_TX_CTL_NO_ACK) &&
414 (!payload->status))
415 info->flags |= IEEE80211_TX_STAT_ACK;
416 if (payload->status & P54_TX_PSM_CANCELLED)
417 info->flags |= IEEE80211_TX_STAT_TX_FILTERED;
418 info->status.ack_signal = p54_rssi_to_dbm(priv,
419 (int)payload->ack_rssi);
420
421 /* Undo all changes to the frame. */
422 switch (entry_data->key_type) {
423 case P54_CRYPTO_TKIPMICHAEL: {
424 u8 *iv = (u8 *)(entry_data->align + pad +
425 entry_data->crypt_offset);
426
427 /* Restore the original TKIP IV. */
428 iv[2] = iv[0];
429 iv[0] = iv[1];
430 iv[1] = (iv[0] | 0x20) & 0x7f; /* WEPSeed - 8.3.2.2 */
431
432 frame_len -= 12; /* remove TKIP_MMIC + TKIP_ICV */
433 break;
434 }
435 case P54_CRYPTO_AESCCMP:
436 frame_len -= 8; /* remove CCMP_MIC */
437 break;
438 case P54_CRYPTO_WEP:
439 frame_len -= 4; /* remove WEP_ICV */
440 break;
441 }
442
443 skb_trim(entry, frame_len);
444 skb_pull(entry, sizeof(*hdr) + pad + sizeof(*entry_data));
445 ieee80211_tx_status_irqsafe(priv->hw, entry);
446}
447
448static void p54_rx_eeprom_readback(struct p54_common *priv,
449 struct sk_buff *skb)
450{
451 struct p54_hdr *hdr = (struct p54_hdr *) skb->data;
452 struct p54_eeprom_lm86 *eeprom = (struct p54_eeprom_lm86 *) hdr->data;
453 struct sk_buff *tmp;
454
455 if (!priv->eeprom)
456 return ;
457
458 if (priv->fw_var >= 0x509) {
459 memcpy(priv->eeprom, eeprom->v2.data,
460 le16_to_cpu(eeprom->v2.len));
461 } else {
462 memcpy(priv->eeprom, eeprom->v1.data,
463 le16_to_cpu(eeprom->v1.len));
464 }
465
466 priv->eeprom = NULL;
467 tmp = p54_find_and_unlink_skb(priv, hdr->req_id);
Christian Lamparter0a5fb842009-06-23 10:39:12 -0500468 dev_kfree_skb_any(tmp);
469 complete(&priv->eeprom_comp);
470}
471
472static void p54_rx_stats(struct p54_common *priv, struct sk_buff *skb)
473{
474 struct p54_hdr *hdr = (struct p54_hdr *) skb->data;
475 struct p54_statistics *stats = (struct p54_statistics *) hdr->data;
476 struct sk_buff *tmp;
477 u32 tsf32;
478
479 if (unlikely(priv->mode == NL80211_IFTYPE_UNSPECIFIED))
480 return ;
481
482 tsf32 = le32_to_cpu(stats->tsf32);
483 if (tsf32 < priv->tsf_low32)
484 priv->tsf_high32++;
485 priv->tsf_low32 = tsf32;
486
487 priv->stats.dot11RTSFailureCount = le32_to_cpu(stats->rts_fail);
488 priv->stats.dot11RTSSuccessCount = le32_to_cpu(stats->rts_success);
489 priv->stats.dot11FCSErrorCount = le32_to_cpu(stats->rx_bad_fcs);
490
491 priv->noise = p54_rssi_to_dbm(priv, le32_to_cpu(stats->noise));
492
493 tmp = p54_find_and_unlink_skb(priv, hdr->req_id);
Christian Lamparter0a5fb842009-06-23 10:39:12 -0500494 dev_kfree_skb_any(tmp);
495}
496
497static void p54_rx_trap(struct p54_common *priv, struct sk_buff *skb)
498{
499 struct p54_hdr *hdr = (struct p54_hdr *) skb->data;
500 struct p54_trap *trap = (struct p54_trap *) hdr->data;
501 u16 event = le16_to_cpu(trap->event);
502 u16 freq = le16_to_cpu(trap->frequency);
503
504 switch (event) {
505 case P54_TRAP_BEACON_TX:
506 break;
507 case P54_TRAP_RADAR:
508 printk(KERN_INFO "%s: radar (freq:%d MHz)\n",
509 wiphy_name(priv->hw->wiphy), freq);
510 break;
511 case P54_TRAP_NO_BEACON:
512 if (priv->vif)
513 ieee80211_beacon_loss(priv->vif);
514 break;
515 case P54_TRAP_SCAN:
516 break;
517 case P54_TRAP_TBTT:
518 break;
519 case P54_TRAP_TIMER:
520 break;
521 default:
522 printk(KERN_INFO "%s: received event:%x freq:%d\n",
523 wiphy_name(priv->hw->wiphy), event, freq);
524 break;
525 }
526}
527
528static int p54_rx_control(struct p54_common *priv, struct sk_buff *skb)
529{
530 struct p54_hdr *hdr = (struct p54_hdr *) skb->data;
531
532 switch (le16_to_cpu(hdr->type)) {
533 case P54_CONTROL_TYPE_TXDONE:
534 p54_rx_frame_sent(priv, skb);
535 break;
536 case P54_CONTROL_TYPE_TRAP:
537 p54_rx_trap(priv, skb);
538 break;
539 case P54_CONTROL_TYPE_BBP:
540 break;
541 case P54_CONTROL_TYPE_STAT_READBACK:
542 p54_rx_stats(priv, skb);
543 break;
544 case P54_CONTROL_TYPE_EEPROM_READBACK:
545 p54_rx_eeprom_readback(priv, skb);
546 break;
547 default:
548 printk(KERN_DEBUG "%s: not handling 0x%02x type control frame\n",
549 wiphy_name(priv->hw->wiphy), le16_to_cpu(hdr->type));
550 break;
551 }
552 return 0;
553}
554
555/* returns zero if skb can be reused */
556int p54_rx(struct ieee80211_hw *dev, struct sk_buff *skb)
557{
558 struct p54_common *priv = dev->priv;
559 u16 type = le16_to_cpu(*((__le16 *)skb->data));
560
561 if (type & P54_HDR_FLAG_CONTROL)
562 return p54_rx_control(priv, skb);
563 else
564 return p54_rx_data(priv, skb);
565}
566EXPORT_SYMBOL_GPL(p54_rx);
567
568static void p54_tx_80211_header(struct p54_common *priv, struct sk_buff *skb,
569 struct ieee80211_tx_info *info, u8 *queue,
570 u32 *extra_len, u16 *flags, u16 *aid,
571 bool *burst_possible)
572{
573 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
574
575 if (ieee80211_is_data_qos(hdr->frame_control))
576 *burst_possible = true;
577 else
578 *burst_possible = false;
579
580 if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ)
581 *flags |= P54_HDR_FLAG_DATA_OUT_SEQNR;
582
583 if (info->flags & IEEE80211_TX_CTL_CLEAR_PS_FILT)
584 *flags |= P54_HDR_FLAG_DATA_OUT_NOCANCEL;
585
586 *queue = skb_get_queue_mapping(skb) + P54_QUEUE_DATA;
587
588 switch (priv->mode) {
589 case NL80211_IFTYPE_MONITOR:
590 /*
591 * We have to set P54_HDR_FLAG_DATA_OUT_PROMISC for
592 * every frame in promiscuous/monitor mode.
593 * see STSW45x0C LMAC API - page 12.
594 */
595 *aid = 0;
596 *flags |= P54_HDR_FLAG_DATA_OUT_PROMISC;
597 break;
598 case NL80211_IFTYPE_STATION:
599 *aid = 1;
600 break;
601 case NL80211_IFTYPE_AP:
602 case NL80211_IFTYPE_ADHOC:
603 case NL80211_IFTYPE_MESH_POINT:
604 if (info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM) {
605 *aid = 0;
606 *queue = P54_QUEUE_CAB;
607 return;
608 }
609
610 if (unlikely(ieee80211_is_mgmt(hdr->frame_control))) {
611 if (ieee80211_is_probe_resp(hdr->frame_control)) {
612 *aid = 0;
613 *flags |= P54_HDR_FLAG_DATA_OUT_TIMESTAMP |
614 P54_HDR_FLAG_DATA_OUT_NOCANCEL;
615 return;
616 } else if (ieee80211_is_beacon(hdr->frame_control)) {
617 *aid = 0;
618
619 if (info->flags & IEEE80211_TX_CTL_INJECTED) {
620 /*
621 * Injecting beacons on top of a AP is
622 * not a good idea... nevertheless,
623 * it should be doable.
624 */
625
626 return;
627 }
628
629 *flags |= P54_HDR_FLAG_DATA_OUT_TIMESTAMP;
630 *queue = P54_QUEUE_BEACON;
631 *extra_len = IEEE80211_MAX_TIM_LEN;
632 return;
633 }
634 }
635
636 if (info->control.sta)
637 *aid = info->control.sta->aid;
638 break;
639 }
640}
641
642static u8 p54_convert_algo(enum ieee80211_key_alg alg)
643{
644 switch (alg) {
645 case ALG_WEP:
646 return P54_CRYPTO_WEP;
647 case ALG_TKIP:
648 return P54_CRYPTO_TKIPMICHAEL;
649 case ALG_CCMP:
650 return P54_CRYPTO_AESCCMP;
651 default:
652 return 0;
653 }
654}
655
656int p54_tx_80211(struct ieee80211_hw *dev, struct sk_buff *skb)
657{
658 struct p54_common *priv = dev->priv;
659 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
660 struct p54_tx_info *p54info;
661 struct p54_hdr *hdr;
662 struct p54_tx_data *txhdr;
663 unsigned int padding, len, extra_len;
664 int i, j, ridx;
665 u16 hdr_flags = 0, aid = 0;
666 u8 rate, queue = 0, crypt_offset = 0;
667 u8 cts_rate = 0x20;
668 u8 rc_flags;
669 u8 calculated_tries[4];
670 u8 nrates = 0, nremaining = 8;
671 bool burst_allowed = false;
672
673 p54_tx_80211_header(priv, skb, info, &queue, &extra_len,
674 &hdr_flags, &aid, &burst_allowed);
675
676 if (p54_tx_qos_accounting_alloc(priv, skb, queue)) {
677 if (!IS_QOS_QUEUE(queue)) {
678 dev_kfree_skb_any(skb);
679 return NETDEV_TX_OK;
680 } else {
681 return NETDEV_TX_BUSY;
682 }
683 }
684
685 padding = (unsigned long)(skb->data - (sizeof(*hdr) + sizeof(*txhdr))) & 3;
686 len = skb->len;
687
688 if (info->control.hw_key) {
689 crypt_offset = ieee80211_get_hdrlen_from_skb(skb);
690 if (info->control.hw_key->alg == ALG_TKIP) {
691 u8 *iv = (u8 *)(skb->data + crypt_offset);
692 /*
693 * The firmware excepts that the IV has to have
694 * this special format
695 */
696 iv[1] = iv[0];
697 iv[0] = iv[2];
698 iv[2] = 0;
699 }
700 }
701
702 txhdr = (struct p54_tx_data *) skb_push(skb, sizeof(*txhdr) + padding);
703 hdr = (struct p54_hdr *) skb_push(skb, sizeof(*hdr));
704
705 if (padding)
706 hdr_flags |= P54_HDR_FLAG_DATA_ALIGN;
707 hdr->type = cpu_to_le16(aid);
708 hdr->rts_tries = info->control.rates[0].count;
709
710 /*
711 * we register the rates in perfect order, and
712 * RTS/CTS won't happen on 5 GHz
713 */
714 cts_rate = info->control.rts_cts_rate_idx;
715
716 memset(&txhdr->rateset, 0, sizeof(txhdr->rateset));
717
718 /* see how many rates got used */
719 for (i = 0; i < dev->max_rates; i++) {
720 if (info->control.rates[i].idx < 0)
721 break;
722 nrates++;
723 }
724
725 /* limit tries to 8/nrates per rate */
726 for (i = 0; i < nrates; i++) {
727 /*
728 * The magic expression here is equivalent to 8/nrates for
729 * all values that matter, but avoids division and jumps.
730 * Note that nrates can only take the values 1 through 4.
731 */
732 calculated_tries[i] = min_t(int, ((15 >> nrates) | 1) + 1,
733 info->control.rates[i].count);
734 nremaining -= calculated_tries[i];
735 }
736
737 /* if there are tries left, distribute from back to front */
738 for (i = nrates - 1; nremaining > 0 && i >= 0; i--) {
739 int tmp = info->control.rates[i].count - calculated_tries[i];
740
741 if (tmp <= 0)
742 continue;
743 /* RC requested more tries at this rate */
744
745 tmp = min_t(int, tmp, nremaining);
746 calculated_tries[i] += tmp;
747 nremaining -= tmp;
748 }
749
750 ridx = 0;
751 for (i = 0; i < nrates && ridx < 8; i++) {
752 /* we register the rates in perfect order */
753 rate = info->control.rates[i].idx;
754 if (info->band == IEEE80211_BAND_5GHZ)
755 rate += 4;
756
757 /* store the count we actually calculated for TX status */
758 info->control.rates[i].count = calculated_tries[i];
759
760 rc_flags = info->control.rates[i].flags;
761 if (rc_flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE) {
762 rate |= 0x10;
763 cts_rate |= 0x10;
764 }
765 if (rc_flags & IEEE80211_TX_RC_USE_RTS_CTS) {
766 burst_allowed = false;
767 rate |= 0x40;
768 } else if (rc_flags & IEEE80211_TX_RC_USE_CTS_PROTECT) {
769 rate |= 0x20;
770 burst_allowed = false;
771 }
772 for (j = 0; j < calculated_tries[i] && ridx < 8; j++) {
773 txhdr->rateset[ridx] = rate;
774 ridx++;
775 }
776 }
777
778 if (burst_allowed)
779 hdr_flags |= P54_HDR_FLAG_DATA_OUT_BURST;
780
781 /* TODO: enable bursting */
782 hdr->flags = cpu_to_le16(hdr_flags);
783 hdr->tries = ridx;
784 txhdr->rts_rate_idx = 0;
785 if (info->control.hw_key) {
786 txhdr->key_type = p54_convert_algo(info->control.hw_key->alg);
787 txhdr->key_len = min((u8)16, info->control.hw_key->keylen);
788 memcpy(txhdr->key, info->control.hw_key->key, txhdr->key_len);
789 if (info->control.hw_key->alg == ALG_TKIP) {
790 /* reserve space for the MIC key */
791 len += 8;
792 memcpy(skb_put(skb, 8), &(info->control.hw_key->key
793 [NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY]), 8);
794 }
795 /* reserve some space for ICV */
796 len += info->control.hw_key->icv_len;
797 memset(skb_put(skb, info->control.hw_key->icv_len), 0,
798 info->control.hw_key->icv_len);
799 } else {
800 txhdr->key_type = 0;
801 txhdr->key_len = 0;
802 }
803 txhdr->crypt_offset = crypt_offset;
804 txhdr->hw_queue = queue;
805 txhdr->backlog = priv->tx_stats[queue].len - 1;
806 memset(txhdr->durations, 0, sizeof(txhdr->durations));
807 txhdr->tx_antenna = ((info->antenna_sel_tx == 0) ?
808 2 : info->antenna_sel_tx - 1) & priv->tx_diversity_mask;
809 if (priv->rxhw == 5) {
810 txhdr->longbow.cts_rate = cts_rate;
811 txhdr->longbow.output_power = cpu_to_le16(priv->output_power);
812 } else {
813 txhdr->normal.output_power = priv->output_power;
814 txhdr->normal.cts_rate = cts_rate;
815 }
816 if (padding)
817 txhdr->align[0] = padding;
818
819 hdr->len = cpu_to_le16(len);
820 /* modifies skb->cb and with it info, so must be last! */
821 p54info = (void *) info->rate_driver_data;
822 p54info->extra_len = extra_len;
823
824 p54_tx(priv, skb);
825 return NETDEV_TX_OK;
826}