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