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