mac80211: redefine usage of the mac80211 workqueue
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / net / wireless / p54 / txrx.c
1 /*
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
29 static 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 */
79 static 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 info = IEEE80211_SKB_CB(skb);
91 range = (void *) info->rate_driver_data;
92 len = (range->extra_len + len) & ~0x3;
93
94 spin_lock_irqsave(&priv->tx_queue.lock, flags);
95 if (unlikely(skb_queue_len(&priv->tx_queue) == 32)) {
96 /*
97 * The tx_queue is now really full.
98 *
99 * TODO: check if the device has crashed and reset it.
100 */
101 spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
102 return -EBUSY;
103 }
104
105 skb_queue_walk(&priv->tx_queue, entry) {
106 u32 hole_size;
107 info = IEEE80211_SKB_CB(entry);
108 range = (void *) info->rate_driver_data;
109 hole_size = range->start_addr - last_addr;
110
111 if (!target_skb && hole_size >= len) {
112 target_skb = entry->prev;
113 hole_size -= len;
114 target_addr = last_addr;
115 break;
116 }
117 last_addr = range->end_addr;
118 }
119 if (unlikely(!target_skb)) {
120 if (priv->rx_end - last_addr >= len) {
121 target_skb = priv->tx_queue.prev;
122 if (!skb_queue_empty(&priv->tx_queue)) {
123 info = IEEE80211_SKB_CB(target_skb);
124 range = (void *)info->rate_driver_data;
125 target_addr = range->end_addr;
126 }
127 } else {
128 spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
129 return -ENOSPC;
130 }
131 }
132
133 info = IEEE80211_SKB_CB(skb);
134 range = (void *) info->rate_driver_data;
135 range->start_addr = target_addr;
136 range->end_addr = target_addr + len;
137 data->req_id = cpu_to_le32(target_addr + priv->headroom);
138 if (IS_DATA_FRAME(skb) &&
139 unlikely(GET_HW_QUEUE(skb) == P54_QUEUE_BEACON))
140 priv->beacon_req_id = data->req_id;
141
142 __skb_queue_after(&priv->tx_queue, target_skb, skb);
143 spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
144 return 0;
145 }
146
147 static void p54_tx_pending(struct p54_common *priv)
148 {
149 struct sk_buff *skb;
150 int ret;
151
152 skb = skb_dequeue(&priv->tx_pending);
153 if (unlikely(!skb))
154 return ;
155
156 ret = p54_assign_address(priv, skb);
157 if (unlikely(ret))
158 skb_queue_head(&priv->tx_pending, skb);
159 else
160 priv->tx(priv->hw, skb);
161 }
162
163 static void p54_wake_queues(struct p54_common *priv)
164 {
165 unsigned long flags;
166 unsigned int i;
167
168 if (unlikely(priv->mode == NL80211_IFTYPE_UNSPECIFIED))
169 return ;
170
171 p54_tx_pending(priv);
172
173 spin_lock_irqsave(&priv->tx_stats_lock, flags);
174 for (i = 0; i < priv->hw->queues; i++) {
175 if (priv->tx_stats[i + P54_QUEUE_DATA].len <
176 priv->tx_stats[i + P54_QUEUE_DATA].limit)
177 ieee80211_wake_queue(priv->hw, i);
178 }
179 spin_unlock_irqrestore(&priv->tx_stats_lock, flags);
180 }
181
182 static int p54_tx_qos_accounting_alloc(struct p54_common *priv,
183 struct sk_buff *skb,
184 const u16 p54_queue)
185 {
186 struct ieee80211_tx_queue_stats *queue;
187 unsigned long flags;
188
189 if (WARN_ON(p54_queue > P54_QUEUE_NUM))
190 return -EINVAL;
191
192 queue = &priv->tx_stats[p54_queue];
193
194 spin_lock_irqsave(&priv->tx_stats_lock, flags);
195 if (unlikely(queue->len >= queue->limit && IS_QOS_QUEUE(p54_queue))) {
196 spin_unlock_irqrestore(&priv->tx_stats_lock, flags);
197 return -ENOSPC;
198 }
199
200 queue->len++;
201 queue->count++;
202
203 if (unlikely(queue->len == queue->limit && IS_QOS_QUEUE(p54_queue))) {
204 u16 ac_queue = p54_queue - P54_QUEUE_DATA;
205 ieee80211_stop_queue(priv->hw, ac_queue);
206 }
207
208 spin_unlock_irqrestore(&priv->tx_stats_lock, flags);
209 return 0;
210 }
211
212 static void p54_tx_qos_accounting_free(struct p54_common *priv,
213 struct sk_buff *skb)
214 {
215 if (IS_DATA_FRAME(skb)) {
216 unsigned long flags;
217
218 spin_lock_irqsave(&priv->tx_stats_lock, flags);
219 priv->tx_stats[GET_HW_QUEUE(skb)].len--;
220 spin_unlock_irqrestore(&priv->tx_stats_lock, flags);
221
222 if (unlikely(GET_HW_QUEUE(skb) == P54_QUEUE_BEACON)) {
223 if (priv->beacon_req_id == GET_REQ_ID(skb)) {
224 /* this is the active beacon set anymore */
225 priv->beacon_req_id = 0;
226 }
227 complete(&priv->beacon_comp);
228 }
229 }
230 p54_wake_queues(priv);
231 }
232
233 void p54_free_skb(struct ieee80211_hw *dev, struct sk_buff *skb)
234 {
235 struct p54_common *priv = dev->priv;
236 if (unlikely(!skb))
237 return ;
238
239 skb_unlink(skb, &priv->tx_queue);
240 p54_tx_qos_accounting_free(priv, skb);
241 dev_kfree_skb_any(skb);
242 }
243 EXPORT_SYMBOL_GPL(p54_free_skb);
244
245 static struct sk_buff *p54_find_and_unlink_skb(struct p54_common *priv,
246 const __le32 req_id)
247 {
248 struct sk_buff *entry;
249 unsigned long flags;
250
251 spin_lock_irqsave(&priv->tx_queue.lock, flags);
252 skb_queue_walk(&priv->tx_queue, entry) {
253 struct p54_hdr *hdr = (struct p54_hdr *) entry->data;
254
255 if (hdr->req_id == req_id) {
256 __skb_unlink(entry, &priv->tx_queue);
257 spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
258 p54_tx_qos_accounting_free(priv, entry);
259 return entry;
260 }
261 }
262 spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
263 return NULL;
264 }
265
266 void p54_tx(struct p54_common *priv, struct sk_buff *skb)
267 {
268 skb_queue_tail(&priv->tx_pending, skb);
269 p54_tx_pending(priv);
270 }
271
272 static int p54_rssi_to_dbm(struct p54_common *priv, int rssi)
273 {
274 int band = priv->hw->conf.channel->band;
275
276 if (priv->rxhw != 5)
277 return ((rssi * priv->rssical_db[band].mul) / 64 +
278 priv->rssical_db[band].add) / 4;
279 else
280 /*
281 * TODO: find the correct formula
282 */
283 return ((rssi * priv->rssical_db[band].mul) / 64 +
284 priv->rssical_db[band].add) / 4;
285 }
286
287 /*
288 * Even if the firmware is capable of dealing with incoming traffic,
289 * while dozing, we have to prepared in case mac80211 uses PS-POLL
290 * to retrieve outstanding frames from our AP.
291 * (see comment in net/mac80211/mlme.c @ line 1993)
292 */
293 static void p54_pspoll_workaround(struct p54_common *priv, struct sk_buff *skb)
294 {
295 struct ieee80211_hdr *hdr = (void *) skb->data;
296 struct ieee80211_tim_ie *tim_ie;
297 u8 *tim;
298 u8 tim_len;
299 bool new_psm;
300
301 /* only beacons have a TIM IE */
302 if (!ieee80211_is_beacon(hdr->frame_control))
303 return;
304
305 if (!priv->aid)
306 return;
307
308 /* only consider beacons from the associated BSSID */
309 if (compare_ether_addr(hdr->addr3, priv->bssid))
310 return;
311
312 tim = p54_find_ie(skb, WLAN_EID_TIM);
313 if (!tim)
314 return;
315
316 tim_len = tim[1];
317 tim_ie = (struct ieee80211_tim_ie *) &tim[2];
318
319 new_psm = ieee80211_check_tim(tim_ie, tim_len, priv->aid);
320 if (new_psm != priv->powersave_override) {
321 priv->powersave_override = new_psm;
322 p54_set_ps(priv);
323 }
324 }
325
326 static int p54_rx_data(struct p54_common *priv, struct sk_buff *skb)
327 {
328 struct p54_rx_data *hdr = (struct p54_rx_data *) skb->data;
329 struct ieee80211_rx_status *rx_status = IEEE80211_SKB_RXCB(skb);
330 u16 freq = le16_to_cpu(hdr->freq);
331 size_t header_len = sizeof(*hdr);
332 u32 tsf32;
333 u8 rate = hdr->rate & 0xf;
334
335 /*
336 * If the device is in a unspecified state we have to
337 * ignore all data frames. Else we could end up with a
338 * nasty crash.
339 */
340 if (unlikely(priv->mode == NL80211_IFTYPE_UNSPECIFIED))
341 return 0;
342
343 if (!(hdr->flags & cpu_to_le16(P54_HDR_FLAG_DATA_IN_FCS_GOOD)))
344 return 0;
345
346 if (hdr->decrypt_status == P54_DECRYPT_OK)
347 rx_status->flag |= RX_FLAG_DECRYPTED;
348 if ((hdr->decrypt_status == P54_DECRYPT_FAIL_MICHAEL) ||
349 (hdr->decrypt_status == P54_DECRYPT_FAIL_TKIP))
350 rx_status->flag |= RX_FLAG_MMIC_ERROR;
351
352 rx_status->signal = p54_rssi_to_dbm(priv, hdr->rssi);
353 rx_status->noise = priv->noise;
354 if (hdr->rate & 0x10)
355 rx_status->flag |= RX_FLAG_SHORTPRE;
356 if (priv->hw->conf.channel->band == IEEE80211_BAND_5GHZ)
357 rx_status->rate_idx = (rate < 4) ? 0 : rate - 4;
358 else
359 rx_status->rate_idx = rate;
360
361 rx_status->freq = freq;
362 rx_status->band = priv->hw->conf.channel->band;
363 rx_status->antenna = hdr->antenna;
364
365 tsf32 = le32_to_cpu(hdr->tsf32);
366 if (tsf32 < priv->tsf_low32)
367 priv->tsf_high32++;
368 rx_status->mactime = ((u64)priv->tsf_high32) << 32 | tsf32;
369 priv->tsf_low32 = tsf32;
370
371 rx_status->flag |= RX_FLAG_TSFT;
372
373 if (hdr->flags & cpu_to_le16(P54_HDR_FLAG_DATA_ALIGN))
374 header_len += hdr->align[0];
375
376 skb_pull(skb, header_len);
377 skb_trim(skb, le16_to_cpu(hdr->len));
378 if (unlikely(priv->hw->conf.flags & IEEE80211_CONF_PS))
379 p54_pspoll_workaround(priv, skb);
380
381 ieee80211_rx_irqsafe(priv->hw, skb);
382
383 ieee80211_queue_delayed_work(priv->hw, &priv->work,
384 msecs_to_jiffies(P54_STATISTICS_UPDATE));
385
386 return -1;
387 }
388
389 static void p54_rx_frame_sent(struct p54_common *priv, struct sk_buff *skb)
390 {
391 struct p54_hdr *hdr = (struct p54_hdr *) skb->data;
392 struct p54_frame_sent *payload = (struct p54_frame_sent *) hdr->data;
393 struct ieee80211_tx_info *info;
394 struct p54_hdr *entry_hdr;
395 struct p54_tx_data *entry_data;
396 struct sk_buff *entry;
397 unsigned int pad = 0, frame_len;
398 int count, idx;
399
400 entry = p54_find_and_unlink_skb(priv, hdr->req_id);
401 if (unlikely(!entry))
402 return ;
403
404 frame_len = entry->len;
405 info = IEEE80211_SKB_CB(entry);
406 entry_hdr = (struct p54_hdr *) entry->data;
407 entry_data = (struct p54_tx_data *) entry_hdr->data;
408 priv->stats.dot11ACKFailureCount += payload->tries - 1;
409
410 /*
411 * Frames in P54_QUEUE_FWSCAN and P54_QUEUE_BEACON are
412 * generated by the driver. Therefore tx_status is bogus
413 * and we don't want to confuse the mac80211 stack.
414 */
415 if (unlikely(entry_data->hw_queue < P54_QUEUE_FWSCAN)) {
416 dev_kfree_skb_any(entry);
417 return ;
418 }
419
420 /*
421 * Clear manually, ieee80211_tx_info_clear_status would
422 * clear the counts too and we need them.
423 */
424 memset(&info->status.ampdu_ack_len, 0,
425 sizeof(struct ieee80211_tx_info) -
426 offsetof(struct ieee80211_tx_info, status.ampdu_ack_len));
427 BUILD_BUG_ON(offsetof(struct ieee80211_tx_info,
428 status.ampdu_ack_len) != 23);
429
430 if (entry_hdr->flags & cpu_to_le16(P54_HDR_FLAG_DATA_ALIGN))
431 pad = entry_data->align[0];
432
433 /* walk through the rates array and adjust the counts */
434 count = payload->tries;
435 for (idx = 0; idx < 4; idx++) {
436 if (count >= info->status.rates[idx].count) {
437 count -= info->status.rates[idx].count;
438 } else if (count > 0) {
439 info->status.rates[idx].count = count;
440 count = 0;
441 } else {
442 info->status.rates[idx].idx = -1;
443 info->status.rates[idx].count = 0;
444 }
445 }
446
447 if (!(info->flags & IEEE80211_TX_CTL_NO_ACK) &&
448 (!payload->status))
449 info->flags |= IEEE80211_TX_STAT_ACK;
450 if (payload->status & P54_TX_PSM_CANCELLED)
451 info->flags |= IEEE80211_TX_STAT_TX_FILTERED;
452 info->status.ack_signal = p54_rssi_to_dbm(priv,
453 (int)payload->ack_rssi);
454
455 /* Undo all changes to the frame. */
456 switch (entry_data->key_type) {
457 case P54_CRYPTO_TKIPMICHAEL: {
458 u8 *iv = (u8 *)(entry_data->align + pad +
459 entry_data->crypt_offset);
460
461 /* Restore the original TKIP IV. */
462 iv[2] = iv[0];
463 iv[0] = iv[1];
464 iv[1] = (iv[0] | 0x20) & 0x7f; /* WEPSeed - 8.3.2.2 */
465
466 frame_len -= 12; /* remove TKIP_MMIC + TKIP_ICV */
467 break;
468 }
469 case P54_CRYPTO_AESCCMP:
470 frame_len -= 8; /* remove CCMP_MIC */
471 break;
472 case P54_CRYPTO_WEP:
473 frame_len -= 4; /* remove WEP_ICV */
474 break;
475 }
476
477 skb_trim(entry, frame_len);
478 skb_pull(entry, sizeof(*hdr) + pad + sizeof(*entry_data));
479 ieee80211_tx_status_irqsafe(priv->hw, entry);
480 }
481
482 static void p54_rx_eeprom_readback(struct p54_common *priv,
483 struct sk_buff *skb)
484 {
485 struct p54_hdr *hdr = (struct p54_hdr *) skb->data;
486 struct p54_eeprom_lm86 *eeprom = (struct p54_eeprom_lm86 *) hdr->data;
487 struct sk_buff *tmp;
488
489 if (!priv->eeprom)
490 return ;
491
492 if (priv->fw_var >= 0x509) {
493 memcpy(priv->eeprom, eeprom->v2.data,
494 le16_to_cpu(eeprom->v2.len));
495 } else {
496 memcpy(priv->eeprom, eeprom->v1.data,
497 le16_to_cpu(eeprom->v1.len));
498 }
499
500 priv->eeprom = NULL;
501 tmp = p54_find_and_unlink_skb(priv, hdr->req_id);
502 dev_kfree_skb_any(tmp);
503 complete(&priv->eeprom_comp);
504 }
505
506 static void p54_rx_stats(struct p54_common *priv, struct sk_buff *skb)
507 {
508 struct p54_hdr *hdr = (struct p54_hdr *) skb->data;
509 struct p54_statistics *stats = (struct p54_statistics *) hdr->data;
510 struct sk_buff *tmp;
511 u32 tsf32;
512
513 if (unlikely(priv->mode == NL80211_IFTYPE_UNSPECIFIED))
514 return ;
515
516 tsf32 = le32_to_cpu(stats->tsf32);
517 if (tsf32 < priv->tsf_low32)
518 priv->tsf_high32++;
519 priv->tsf_low32 = tsf32;
520
521 priv->stats.dot11RTSFailureCount = le32_to_cpu(stats->rts_fail);
522 priv->stats.dot11RTSSuccessCount = le32_to_cpu(stats->rts_success);
523 priv->stats.dot11FCSErrorCount = le32_to_cpu(stats->rx_bad_fcs);
524
525 priv->noise = p54_rssi_to_dbm(priv, le32_to_cpu(stats->noise));
526
527 tmp = p54_find_and_unlink_skb(priv, hdr->req_id);
528 dev_kfree_skb_any(tmp);
529 }
530
531 static void p54_rx_trap(struct p54_common *priv, struct sk_buff *skb)
532 {
533 struct p54_hdr *hdr = (struct p54_hdr *) skb->data;
534 struct p54_trap *trap = (struct p54_trap *) hdr->data;
535 u16 event = le16_to_cpu(trap->event);
536 u16 freq = le16_to_cpu(trap->frequency);
537
538 switch (event) {
539 case P54_TRAP_BEACON_TX:
540 break;
541 case P54_TRAP_RADAR:
542 printk(KERN_INFO "%s: radar (freq:%d MHz)\n",
543 wiphy_name(priv->hw->wiphy), freq);
544 break;
545 case P54_TRAP_NO_BEACON:
546 if (priv->vif)
547 ieee80211_beacon_loss(priv->vif);
548 break;
549 case P54_TRAP_SCAN:
550 break;
551 case P54_TRAP_TBTT:
552 break;
553 case P54_TRAP_TIMER:
554 break;
555 default:
556 printk(KERN_INFO "%s: received event:%x freq:%d\n",
557 wiphy_name(priv->hw->wiphy), event, freq);
558 break;
559 }
560 }
561
562 static int p54_rx_control(struct p54_common *priv, struct sk_buff *skb)
563 {
564 struct p54_hdr *hdr = (struct p54_hdr *) skb->data;
565
566 switch (le16_to_cpu(hdr->type)) {
567 case P54_CONTROL_TYPE_TXDONE:
568 p54_rx_frame_sent(priv, skb);
569 break;
570 case P54_CONTROL_TYPE_TRAP:
571 p54_rx_trap(priv, skb);
572 break;
573 case P54_CONTROL_TYPE_BBP:
574 break;
575 case P54_CONTROL_TYPE_STAT_READBACK:
576 p54_rx_stats(priv, skb);
577 break;
578 case P54_CONTROL_TYPE_EEPROM_READBACK:
579 p54_rx_eeprom_readback(priv, skb);
580 break;
581 default:
582 printk(KERN_DEBUG "%s: not handling 0x%02x type control frame\n",
583 wiphy_name(priv->hw->wiphy), le16_to_cpu(hdr->type));
584 break;
585 }
586 return 0;
587 }
588
589 /* returns zero if skb can be reused */
590 int p54_rx(struct ieee80211_hw *dev, struct sk_buff *skb)
591 {
592 struct p54_common *priv = dev->priv;
593 u16 type = le16_to_cpu(*((__le16 *)skb->data));
594
595 if (type & P54_HDR_FLAG_CONTROL)
596 return p54_rx_control(priv, skb);
597 else
598 return p54_rx_data(priv, skb);
599 }
600 EXPORT_SYMBOL_GPL(p54_rx);
601
602 static void p54_tx_80211_header(struct p54_common *priv, struct sk_buff *skb,
603 struct ieee80211_tx_info *info, u8 *queue,
604 u32 *extra_len, u16 *flags, u16 *aid,
605 bool *burst_possible)
606 {
607 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
608
609 if (ieee80211_is_data_qos(hdr->frame_control))
610 *burst_possible = true;
611 else
612 *burst_possible = false;
613
614 if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ)
615 *flags |= P54_HDR_FLAG_DATA_OUT_SEQNR;
616
617 if (info->flags & IEEE80211_TX_CTL_PSPOLL_RESPONSE)
618 *flags |= P54_HDR_FLAG_DATA_OUT_NOCANCEL;
619
620 *queue = skb_get_queue_mapping(skb) + P54_QUEUE_DATA;
621
622 switch (priv->mode) {
623 case NL80211_IFTYPE_MONITOR:
624 /*
625 * We have to set P54_HDR_FLAG_DATA_OUT_PROMISC for
626 * every frame in promiscuous/monitor mode.
627 * see STSW45x0C LMAC API - page 12.
628 */
629 *aid = 0;
630 *flags |= P54_HDR_FLAG_DATA_OUT_PROMISC;
631 break;
632 case NL80211_IFTYPE_STATION:
633 *aid = 1;
634 break;
635 case NL80211_IFTYPE_AP:
636 case NL80211_IFTYPE_ADHOC:
637 case NL80211_IFTYPE_MESH_POINT:
638 if (info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM) {
639 *aid = 0;
640 *queue = P54_QUEUE_CAB;
641 return;
642 }
643
644 if (unlikely(ieee80211_is_mgmt(hdr->frame_control))) {
645 if (ieee80211_is_probe_resp(hdr->frame_control)) {
646 *aid = 0;
647 *flags |= P54_HDR_FLAG_DATA_OUT_TIMESTAMP |
648 P54_HDR_FLAG_DATA_OUT_NOCANCEL;
649 return;
650 } else if (ieee80211_is_beacon(hdr->frame_control)) {
651 *aid = 0;
652
653 if (info->flags & IEEE80211_TX_CTL_INJECTED) {
654 /*
655 * Injecting beacons on top of a AP is
656 * not a good idea... nevertheless,
657 * it should be doable.
658 */
659
660 return;
661 }
662
663 *flags |= P54_HDR_FLAG_DATA_OUT_TIMESTAMP;
664 *queue = P54_QUEUE_BEACON;
665 *extra_len = IEEE80211_MAX_TIM_LEN;
666 return;
667 }
668 }
669
670 if (info->control.sta)
671 *aid = info->control.sta->aid;
672 break;
673 }
674 }
675
676 static u8 p54_convert_algo(enum ieee80211_key_alg alg)
677 {
678 switch (alg) {
679 case ALG_WEP:
680 return P54_CRYPTO_WEP;
681 case ALG_TKIP:
682 return P54_CRYPTO_TKIPMICHAEL;
683 case ALG_CCMP:
684 return P54_CRYPTO_AESCCMP;
685 default:
686 return 0;
687 }
688 }
689
690 int p54_tx_80211(struct ieee80211_hw *dev, struct sk_buff *skb)
691 {
692 struct p54_common *priv = dev->priv;
693 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
694 struct p54_tx_info *p54info;
695 struct p54_hdr *hdr;
696 struct p54_tx_data *txhdr;
697 unsigned int padding, len, extra_len;
698 int i, j, ridx;
699 u16 hdr_flags = 0, aid = 0;
700 u8 rate, queue = 0, crypt_offset = 0;
701 u8 cts_rate = 0x20;
702 u8 rc_flags;
703 u8 calculated_tries[4];
704 u8 nrates = 0, nremaining = 8;
705 bool burst_allowed = false;
706
707 p54_tx_80211_header(priv, skb, info, &queue, &extra_len,
708 &hdr_flags, &aid, &burst_allowed);
709
710 if (p54_tx_qos_accounting_alloc(priv, skb, queue)) {
711 if (!IS_QOS_QUEUE(queue)) {
712 dev_kfree_skb_any(skb);
713 return NETDEV_TX_OK;
714 } else {
715 return NETDEV_TX_BUSY;
716 }
717 }
718
719 padding = (unsigned long)(skb->data - (sizeof(*hdr) + sizeof(*txhdr))) & 3;
720 len = skb->len;
721
722 if (info->control.hw_key) {
723 crypt_offset = ieee80211_get_hdrlen_from_skb(skb);
724 if (info->control.hw_key->alg == ALG_TKIP) {
725 u8 *iv = (u8 *)(skb->data + crypt_offset);
726 /*
727 * The firmware excepts that the IV has to have
728 * this special format
729 */
730 iv[1] = iv[0];
731 iv[0] = iv[2];
732 iv[2] = 0;
733 }
734 }
735
736 txhdr = (struct p54_tx_data *) skb_push(skb, sizeof(*txhdr) + padding);
737 hdr = (struct p54_hdr *) skb_push(skb, sizeof(*hdr));
738
739 if (padding)
740 hdr_flags |= P54_HDR_FLAG_DATA_ALIGN;
741 hdr->type = cpu_to_le16(aid);
742 hdr->rts_tries = info->control.rates[0].count;
743
744 /*
745 * we register the rates in perfect order, and
746 * RTS/CTS won't happen on 5 GHz
747 */
748 cts_rate = info->control.rts_cts_rate_idx;
749
750 memset(&txhdr->rateset, 0, sizeof(txhdr->rateset));
751
752 /* see how many rates got used */
753 for (i = 0; i < dev->max_rates; i++) {
754 if (info->control.rates[i].idx < 0)
755 break;
756 nrates++;
757 }
758
759 /* limit tries to 8/nrates per rate */
760 for (i = 0; i < nrates; i++) {
761 /*
762 * The magic expression here is equivalent to 8/nrates for
763 * all values that matter, but avoids division and jumps.
764 * Note that nrates can only take the values 1 through 4.
765 */
766 calculated_tries[i] = min_t(int, ((15 >> nrates) | 1) + 1,
767 info->control.rates[i].count);
768 nremaining -= calculated_tries[i];
769 }
770
771 /* if there are tries left, distribute from back to front */
772 for (i = nrates - 1; nremaining > 0 && i >= 0; i--) {
773 int tmp = info->control.rates[i].count - calculated_tries[i];
774
775 if (tmp <= 0)
776 continue;
777 /* RC requested more tries at this rate */
778
779 tmp = min_t(int, tmp, nremaining);
780 calculated_tries[i] += tmp;
781 nremaining -= tmp;
782 }
783
784 ridx = 0;
785 for (i = 0; i < nrates && ridx < 8; i++) {
786 /* we register the rates in perfect order */
787 rate = info->control.rates[i].idx;
788 if (info->band == IEEE80211_BAND_5GHZ)
789 rate += 4;
790
791 /* store the count we actually calculated for TX status */
792 info->control.rates[i].count = calculated_tries[i];
793
794 rc_flags = info->control.rates[i].flags;
795 if (rc_flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE) {
796 rate |= 0x10;
797 cts_rate |= 0x10;
798 }
799 if (rc_flags & IEEE80211_TX_RC_USE_RTS_CTS) {
800 burst_allowed = false;
801 rate |= 0x40;
802 } else if (rc_flags & IEEE80211_TX_RC_USE_CTS_PROTECT) {
803 rate |= 0x20;
804 burst_allowed = false;
805 }
806 for (j = 0; j < calculated_tries[i] && ridx < 8; j++) {
807 txhdr->rateset[ridx] = rate;
808 ridx++;
809 }
810 }
811
812 if (burst_allowed)
813 hdr_flags |= P54_HDR_FLAG_DATA_OUT_BURST;
814
815 /* TODO: enable bursting */
816 hdr->flags = cpu_to_le16(hdr_flags);
817 hdr->tries = ridx;
818 txhdr->rts_rate_idx = 0;
819 if (info->control.hw_key) {
820 txhdr->key_type = p54_convert_algo(info->control.hw_key->alg);
821 txhdr->key_len = min((u8)16, info->control.hw_key->keylen);
822 memcpy(txhdr->key, info->control.hw_key->key, txhdr->key_len);
823 if (info->control.hw_key->alg == ALG_TKIP) {
824 /* reserve space for the MIC key */
825 len += 8;
826 memcpy(skb_put(skb, 8), &(info->control.hw_key->key
827 [NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY]), 8);
828 }
829 /* reserve some space for ICV */
830 len += info->control.hw_key->icv_len;
831 memset(skb_put(skb, info->control.hw_key->icv_len), 0,
832 info->control.hw_key->icv_len);
833 } else {
834 txhdr->key_type = 0;
835 txhdr->key_len = 0;
836 }
837 txhdr->crypt_offset = crypt_offset;
838 txhdr->hw_queue = queue;
839 txhdr->backlog = priv->tx_stats[queue].len - 1;
840 memset(txhdr->durations, 0, sizeof(txhdr->durations));
841 txhdr->tx_antenna = ((info->antenna_sel_tx == 0) ?
842 2 : info->antenna_sel_tx - 1) & priv->tx_diversity_mask;
843 if (priv->rxhw == 5) {
844 txhdr->longbow.cts_rate = cts_rate;
845 txhdr->longbow.output_power = cpu_to_le16(priv->output_power);
846 } else {
847 txhdr->normal.output_power = priv->output_power;
848 txhdr->normal.cts_rate = cts_rate;
849 }
850 if (padding)
851 txhdr->align[0] = padding;
852
853 hdr->len = cpu_to_le16(len);
854 /* modifies skb->cb and with it info, so must be last! */
855 p54info = (void *) info->rate_driver_data;
856 p54info->extra_len = extra_len;
857
858 p54_tx(priv, skb);
859 return NETDEV_TX_OK;
860 }