mac80211: implement uAPSD
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / net / wireless / p54 / txrx.c
blobf485784a60aeb0a903871464e6db494c71dc2bf6
1 /*
2 * Common code for mac80211 Prism54 drivers
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>
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).
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.
19 #include <linux/init.h>
20 #include <linux/firmware.h>
21 #include <linux/etherdevice.h>
22 #include <asm/div64.h>
24 #include <net/mac80211.h>
26 #include "p54.h"
27 #include "lmac.h"
29 #ifdef P54_MM_DEBUG
30 static void p54_dump_tx_queue(struct p54_common *priv)
32 unsigned long flags;
33 struct ieee80211_tx_info *info;
34 struct p54_tx_info *range;
35 struct sk_buff *skb;
36 struct p54_hdr *hdr;
37 unsigned int i = 0;
38 u32 prev_addr;
39 u32 largest_hole = 0, free;
41 spin_lock_irqsave(&priv->tx_queue.lock, flags);
42 wiphy_debug(priv->hw->wiphy, "/ --- tx queue dump (%d entries) ---\n",
43 skb_queue_len(&priv->tx_queue));
45 prev_addr = priv->rx_start;
46 skb_queue_walk(&priv->tx_queue, skb) {
47 info = IEEE80211_SKB_CB(skb);
48 range = (void *) info->rate_driver_data;
49 hdr = (void *) skb->data;
51 free = range->start_addr - prev_addr;
52 wiphy_debug(priv->hw->wiphy,
53 "| [%02d] => [skb:%p skb_len:0x%04x "
54 "hdr:{flags:%02x len:%04x req_id:%04x type:%02x} "
55 "mem:{start:%04x end:%04x, free:%d}]\n",
56 i++, skb, skb->len,
57 le16_to_cpu(hdr->flags), le16_to_cpu(hdr->len),
58 le32_to_cpu(hdr->req_id), le16_to_cpu(hdr->type),
59 range->start_addr, range->end_addr, free);
61 prev_addr = range->end_addr;
62 largest_hole = max(largest_hole, free);
64 free = priv->rx_end - prev_addr;
65 largest_hole = max(largest_hole, free);
66 wiphy_debug(priv->hw->wiphy,
67 "\\ --- [free: %d], largest free block: %d ---\n",
68 free, largest_hole);
69 spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
71 #endif /* P54_MM_DEBUG */
74 * So, the firmware is somewhat stupid and doesn't know what places in its
75 * memory incoming data should go to. By poking around in the firmware, we
76 * can find some unused memory to upload our packets to. However, data that we
77 * want the card to TX needs to stay intact until the card has told us that
78 * it is done with it. This function finds empty places we can upload to and
79 * marks allocated areas as reserved if necessary. p54_find_and_unlink_skb or
80 * p54_free_skb frees allocated areas.
82 static int p54_assign_address(struct p54_common *priv, struct sk_buff *skb)
84 struct sk_buff *entry, *target_skb = NULL;
85 struct ieee80211_tx_info *info;
86 struct p54_tx_info *range;
87 struct p54_hdr *data = (void *) skb->data;
88 unsigned long flags;
89 u32 last_addr = priv->rx_start;
90 u32 target_addr = priv->rx_start;
91 u16 len = priv->headroom + skb->len + priv->tailroom + 3;
93 info = IEEE80211_SKB_CB(skb);
94 range = (void *) info->rate_driver_data;
95 len = (range->extra_len + len) & ~0x3;
97 spin_lock_irqsave(&priv->tx_queue.lock, flags);
98 if (unlikely(skb_queue_len(&priv->tx_queue) == 32)) {
100 * The tx_queue is now really full.
102 * TODO: check if the device has crashed and reset it.
104 spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
105 return -EBUSY;
108 skb_queue_walk(&priv->tx_queue, entry) {
109 u32 hole_size;
110 info = IEEE80211_SKB_CB(entry);
111 range = (void *) info->rate_driver_data;
112 hole_size = range->start_addr - last_addr;
114 if (!target_skb && hole_size >= len) {
115 target_skb = entry->prev;
116 hole_size -= len;
117 target_addr = last_addr;
118 break;
120 last_addr = range->end_addr;
122 if (unlikely(!target_skb)) {
123 if (priv->rx_end - last_addr >= len) {
124 target_skb = priv->tx_queue.prev;
125 if (!skb_queue_empty(&priv->tx_queue)) {
126 info = IEEE80211_SKB_CB(target_skb);
127 range = (void *)info->rate_driver_data;
128 target_addr = range->end_addr;
130 } else {
131 spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
132 return -ENOSPC;
136 info = IEEE80211_SKB_CB(skb);
137 range = (void *) info->rate_driver_data;
138 range->start_addr = target_addr;
139 range->end_addr = target_addr + len;
140 data->req_id = cpu_to_le32(target_addr + priv->headroom);
141 if (IS_DATA_FRAME(skb) &&
142 unlikely(GET_HW_QUEUE(skb) == P54_QUEUE_BEACON))
143 priv->beacon_req_id = data->req_id;
145 __skb_queue_after(&priv->tx_queue, target_skb, skb);
146 spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
147 return 0;
150 static void p54_tx_pending(struct p54_common *priv)
152 struct sk_buff *skb;
153 int ret;
155 skb = skb_dequeue(&priv->tx_pending);
156 if (unlikely(!skb))
157 return ;
159 ret = p54_assign_address(priv, skb);
160 if (unlikely(ret))
161 skb_queue_head(&priv->tx_pending, skb);
162 else
163 priv->tx(priv->hw, skb);
166 static void p54_wake_queues(struct p54_common *priv)
168 unsigned long flags;
169 unsigned int i;
171 if (unlikely(priv->mode == NL80211_IFTYPE_UNSPECIFIED))
172 return ;
174 p54_tx_pending(priv);
176 spin_lock_irqsave(&priv->tx_stats_lock, flags);
177 for (i = 0; i < priv->hw->queues; i++) {
178 if (priv->tx_stats[i + P54_QUEUE_DATA].len <
179 priv->tx_stats[i + P54_QUEUE_DATA].limit)
180 ieee80211_wake_queue(priv->hw, i);
182 spin_unlock_irqrestore(&priv->tx_stats_lock, flags);
185 static int p54_tx_qos_accounting_alloc(struct p54_common *priv,
186 struct sk_buff *skb,
187 const u16 p54_queue)
189 struct p54_tx_queue_stats *queue;
190 unsigned long flags;
192 if (WARN_ON(p54_queue >= P54_QUEUE_NUM))
193 return -EINVAL;
195 queue = &priv->tx_stats[p54_queue];
197 spin_lock_irqsave(&priv->tx_stats_lock, flags);
198 if (unlikely(queue->len >= queue->limit && IS_QOS_QUEUE(p54_queue))) {
199 spin_unlock_irqrestore(&priv->tx_stats_lock, flags);
200 return -ENOSPC;
203 queue->len++;
204 queue->count++;
206 if (unlikely(queue->len == queue->limit && IS_QOS_QUEUE(p54_queue))) {
207 u16 ac_queue = p54_queue - P54_QUEUE_DATA;
208 ieee80211_stop_queue(priv->hw, ac_queue);
211 spin_unlock_irqrestore(&priv->tx_stats_lock, flags);
212 return 0;
215 static void p54_tx_qos_accounting_free(struct p54_common *priv,
216 struct sk_buff *skb)
218 if (IS_DATA_FRAME(skb)) {
219 unsigned long flags;
221 spin_lock_irqsave(&priv->tx_stats_lock, flags);
222 priv->tx_stats[GET_HW_QUEUE(skb)].len--;
223 spin_unlock_irqrestore(&priv->tx_stats_lock, flags);
225 if (unlikely(GET_HW_QUEUE(skb) == P54_QUEUE_BEACON)) {
226 if (priv->beacon_req_id == GET_REQ_ID(skb)) {
227 /* this is the active beacon set anymore */
228 priv->beacon_req_id = 0;
230 complete(&priv->beacon_comp);
233 p54_wake_queues(priv);
236 void p54_free_skb(struct ieee80211_hw *dev, struct sk_buff *skb)
238 struct p54_common *priv = dev->priv;
239 if (unlikely(!skb))
240 return ;
242 skb_unlink(skb, &priv->tx_queue);
243 p54_tx_qos_accounting_free(priv, skb);
244 dev_kfree_skb_any(skb);
246 EXPORT_SYMBOL_GPL(p54_free_skb);
248 static struct sk_buff *p54_find_and_unlink_skb(struct p54_common *priv,
249 const __le32 req_id)
251 struct sk_buff *entry;
252 unsigned long flags;
254 spin_lock_irqsave(&priv->tx_queue.lock, flags);
255 skb_queue_walk(&priv->tx_queue, entry) {
256 struct p54_hdr *hdr = (struct p54_hdr *) entry->data;
258 if (hdr->req_id == req_id) {
259 __skb_unlink(entry, &priv->tx_queue);
260 spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
261 p54_tx_qos_accounting_free(priv, entry);
262 return entry;
265 spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
266 return NULL;
269 void p54_tx(struct p54_common *priv, struct sk_buff *skb)
271 skb_queue_tail(&priv->tx_pending, skb);
272 p54_tx_pending(priv);
275 static int p54_rssi_to_dbm(struct p54_common *priv, int rssi)
277 if (priv->rxhw != 5) {
278 return ((rssi * priv->cur_rssi->mul) / 64 +
279 priv->cur_rssi->add) / 4;
280 } else {
282 * TODO: find the correct formula
284 return rssi / 2 - 110;
289 * Even if the firmware is capable of dealing with incoming traffic,
290 * while dozing, we have to prepared in case mac80211 uses PS-POLL
291 * to retrieve outstanding frames from our AP.
292 * (see comment in net/mac80211/mlme.c @ line 1993)
294 static void p54_pspoll_workaround(struct p54_common *priv, struct sk_buff *skb)
296 struct ieee80211_hdr *hdr = (void *) skb->data;
297 struct ieee80211_tim_ie *tim_ie;
298 u8 *tim;
299 u8 tim_len;
300 bool new_psm;
302 /* only beacons have a TIM IE */
303 if (!ieee80211_is_beacon(hdr->frame_control))
304 return;
306 if (!priv->aid)
307 return;
309 /* only consider beacons from the associated BSSID */
310 if (compare_ether_addr(hdr->addr3, priv->bssid))
311 return;
313 tim = p54_find_ie(skb, WLAN_EID_TIM);
314 if (!tim)
315 return;
317 tim_len = tim[1];
318 tim_ie = (struct ieee80211_tim_ie *) &tim[2];
320 new_psm = ieee80211_check_tim(tim_ie, tim_len, priv->aid);
321 if (new_psm != priv->powersave_override) {
322 priv->powersave_override = new_psm;
323 p54_set_ps(priv);
327 static int p54_rx_data(struct p54_common *priv, struct sk_buff *skb)
329 struct p54_rx_data *hdr = (struct p54_rx_data *) skb->data;
330 struct ieee80211_rx_status *rx_status = IEEE80211_SKB_RXCB(skb);
331 u16 freq = le16_to_cpu(hdr->freq);
332 size_t header_len = sizeof(*hdr);
333 u32 tsf32;
334 u8 rate = hdr->rate & 0xf;
337 * If the device is in a unspecified state we have to
338 * ignore all data frames. Else we could end up with a
339 * nasty crash.
341 if (unlikely(priv->mode == NL80211_IFTYPE_UNSPECIFIED))
342 return 0;
344 if (!(hdr->flags & cpu_to_le16(P54_HDR_FLAG_DATA_IN_FCS_GOOD)))
345 return 0;
347 if (hdr->decrypt_status == P54_DECRYPT_OK)
348 rx_status->flag |= RX_FLAG_DECRYPTED;
349 if ((hdr->decrypt_status == P54_DECRYPT_FAIL_MICHAEL) ||
350 (hdr->decrypt_status == P54_DECRYPT_FAIL_TKIP))
351 rx_status->flag |= RX_FLAG_MMIC_ERROR;
353 rx_status->signal = p54_rssi_to_dbm(priv, hdr->rssi);
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;
361 rx_status->freq = freq;
362 rx_status->band = priv->hw->conf.channel->band;
363 rx_status->antenna = hdr->antenna;
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;
371 rx_status->flag |= RX_FLAG_MACTIME_MPDU;
373 if (hdr->flags & cpu_to_le16(P54_HDR_FLAG_DATA_ALIGN))
374 header_len += hdr->align[0];
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);
381 ieee80211_rx_irqsafe(priv->hw, skb);
383 ieee80211_queue_delayed_work(priv->hw, &priv->work,
384 msecs_to_jiffies(P54_STATISTICS_UPDATE));
386 return -1;
389 static void p54_rx_frame_sent(struct p54_common *priv, struct sk_buff *skb)
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;
400 entry = p54_find_and_unlink_skb(priv, hdr->req_id);
401 if (unlikely(!entry))
402 return ;
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;
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.
415 if (unlikely(entry_data->hw_queue < P54_QUEUE_FWSCAN)) {
416 dev_kfree_skb_any(entry);
417 return ;
421 * Clear manually, ieee80211_tx_info_clear_status would
422 * clear the counts too and we need them.
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);
430 if (entry_hdr->flags & cpu_to_le16(P54_HDR_FLAG_DATA_ALIGN))
431 pad = entry_data->align[0];
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;
447 if (!(info->flags & IEEE80211_TX_CTL_NO_ACK) &&
448 !(payload->status & P54_TX_FAILED))
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);
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);
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 */
466 frame_len -= 12; /* remove TKIP_MMIC + TKIP_ICV */
467 break;
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;
477 skb_trim(entry, frame_len);
478 skb_pull(entry, sizeof(*hdr) + pad + sizeof(*entry_data));
479 ieee80211_tx_status_irqsafe(priv->hw, entry);
482 static void p54_rx_eeprom_readback(struct p54_common *priv,
483 struct sk_buff *skb)
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;
489 if (!priv->eeprom)
490 return ;
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));
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);
506 static void p54_rx_stats(struct p54_common *priv, struct sk_buff *skb)
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 struct ieee80211_channel *chan;
512 unsigned int i, rssi, tx, cca, dtime, dtotal, dcca, dtx, drssi, unit;
513 u32 tsf32;
515 if (unlikely(priv->mode == NL80211_IFTYPE_UNSPECIFIED))
516 return ;
518 tsf32 = le32_to_cpu(stats->tsf32);
519 if (tsf32 < priv->tsf_low32)
520 priv->tsf_high32++;
521 priv->tsf_low32 = tsf32;
523 priv->stats.dot11RTSFailureCount = le32_to_cpu(stats->rts_fail);
524 priv->stats.dot11RTSSuccessCount = le32_to_cpu(stats->rts_success);
525 priv->stats.dot11FCSErrorCount = le32_to_cpu(stats->rx_bad_fcs);
527 priv->noise = p54_rssi_to_dbm(priv, le32_to_cpu(stats->noise));
530 * STSW450X LMAC API page 26 - 3.8 Statistics
531 * "The exact measurement period can be derived from the
532 * timestamp member".
534 dtime = tsf32 - priv->survey_raw.timestamp;
537 * STSW450X LMAC API page 26 - 3.8.1 Noise histogram
538 * The LMAC samples RSSI, CCA and transmit state at regular
539 * periods (typically 8 times per 1k [as in 1024] usec).
541 cca = le32_to_cpu(stats->sample_cca);
542 tx = le32_to_cpu(stats->sample_tx);
543 rssi = 0;
544 for (i = 0; i < ARRAY_SIZE(stats->sample_noise); i++)
545 rssi += le32_to_cpu(stats->sample_noise[i]);
547 dcca = cca - priv->survey_raw.cached_cca;
548 drssi = rssi - priv->survey_raw.cached_rssi;
549 dtx = tx - priv->survey_raw.cached_tx;
550 dtotal = dcca + drssi + dtx;
553 * update statistics when more than a second is over since the
554 * last call, or when a update is badly needed.
556 if (dtotal && (priv->update_stats || dtime >= USEC_PER_SEC) &&
557 dtime >= dtotal) {
558 priv->survey_raw.timestamp = tsf32;
559 priv->update_stats = false;
560 unit = dtime / dtotal;
562 if (dcca) {
563 priv->survey_raw.cca += dcca * unit;
564 priv->survey_raw.cached_cca = cca;
566 if (dtx) {
567 priv->survey_raw.tx += dtx * unit;
568 priv->survey_raw.cached_tx = tx;
570 if (drssi) {
571 priv->survey_raw.rssi += drssi * unit;
572 priv->survey_raw.cached_rssi = rssi;
575 /* 1024 usec / 8 times = 128 usec / time */
576 if (!(priv->phy_ps || priv->phy_idle))
577 priv->survey_raw.active += dtotal * unit;
578 else
579 priv->survey_raw.active += (dcca + dtx) * unit;
582 chan = priv->curchan;
583 if (chan) {
584 struct survey_info *survey = &priv->survey[chan->hw_value];
585 survey->noise = clamp_t(s8, priv->noise, -128, 127);
586 survey->channel_time = priv->survey_raw.active;
587 survey->channel_time_tx = priv->survey_raw.tx;
588 survey->channel_time_busy = priv->survey_raw.tx +
589 priv->survey_raw.cca;
590 do_div(survey->channel_time, 1024);
591 do_div(survey->channel_time_tx, 1024);
592 do_div(survey->channel_time_busy, 1024);
595 tmp = p54_find_and_unlink_skb(priv, hdr->req_id);
596 dev_kfree_skb_any(tmp);
597 complete(&priv->stat_comp);
600 static void p54_rx_trap(struct p54_common *priv, struct sk_buff *skb)
602 struct p54_hdr *hdr = (struct p54_hdr *) skb->data;
603 struct p54_trap *trap = (struct p54_trap *) hdr->data;
604 u16 event = le16_to_cpu(trap->event);
605 u16 freq = le16_to_cpu(trap->frequency);
607 switch (event) {
608 case P54_TRAP_BEACON_TX:
609 break;
610 case P54_TRAP_RADAR:
611 wiphy_info(priv->hw->wiphy, "radar (freq:%d MHz)\n", freq);
612 break;
613 case P54_TRAP_NO_BEACON:
614 if (priv->vif)
615 ieee80211_beacon_loss(priv->vif);
616 break;
617 case P54_TRAP_SCAN:
618 break;
619 case P54_TRAP_TBTT:
620 break;
621 case P54_TRAP_TIMER:
622 break;
623 case P54_TRAP_FAA_RADIO_OFF:
624 wiphy_rfkill_set_hw_state(priv->hw->wiphy, true);
625 break;
626 case P54_TRAP_FAA_RADIO_ON:
627 wiphy_rfkill_set_hw_state(priv->hw->wiphy, false);
628 break;
629 default:
630 wiphy_info(priv->hw->wiphy, "received event:%x freq:%d\n",
631 event, freq);
632 break;
636 static int p54_rx_control(struct p54_common *priv, struct sk_buff *skb)
638 struct p54_hdr *hdr = (struct p54_hdr *) skb->data;
640 switch (le16_to_cpu(hdr->type)) {
641 case P54_CONTROL_TYPE_TXDONE:
642 p54_rx_frame_sent(priv, skb);
643 break;
644 case P54_CONTROL_TYPE_TRAP:
645 p54_rx_trap(priv, skb);
646 break;
647 case P54_CONTROL_TYPE_BBP:
648 break;
649 case P54_CONTROL_TYPE_STAT_READBACK:
650 p54_rx_stats(priv, skb);
651 break;
652 case P54_CONTROL_TYPE_EEPROM_READBACK:
653 p54_rx_eeprom_readback(priv, skb);
654 break;
655 default:
656 wiphy_debug(priv->hw->wiphy,
657 "not handling 0x%02x type control frame\n",
658 le16_to_cpu(hdr->type));
659 break;
661 return 0;
664 /* returns zero if skb can be reused */
665 int p54_rx(struct ieee80211_hw *dev, struct sk_buff *skb)
667 struct p54_common *priv = dev->priv;
668 u16 type = le16_to_cpu(*((__le16 *)skb->data));
670 if (type & P54_HDR_FLAG_CONTROL)
671 return p54_rx_control(priv, skb);
672 else
673 return p54_rx_data(priv, skb);
675 EXPORT_SYMBOL_GPL(p54_rx);
677 static void p54_tx_80211_header(struct p54_common *priv, struct sk_buff *skb,
678 struct ieee80211_tx_info *info, u8 *queue,
679 u32 *extra_len, u16 *flags, u16 *aid,
680 bool *burst_possible)
682 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
684 if (ieee80211_is_data_qos(hdr->frame_control))
685 *burst_possible = true;
686 else
687 *burst_possible = false;
689 if (!(info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ))
690 *flags |= P54_HDR_FLAG_DATA_OUT_SEQNR;
692 if (info->flags & IEEE80211_TX_CTL_POLL_RESPONSE)
693 *flags |= P54_HDR_FLAG_DATA_OUT_NOCANCEL;
695 if (info->flags & IEEE80211_TX_CTL_CLEAR_PS_FILT)
696 *flags |= P54_HDR_FLAG_DATA_OUT_NOCANCEL;
698 *queue = skb_get_queue_mapping(skb) + P54_QUEUE_DATA;
700 switch (priv->mode) {
701 case NL80211_IFTYPE_MONITOR:
703 * We have to set P54_HDR_FLAG_DATA_OUT_PROMISC for
704 * every frame in promiscuous/monitor mode.
705 * see STSW45x0C LMAC API - page 12.
707 *aid = 0;
708 *flags |= P54_HDR_FLAG_DATA_OUT_PROMISC;
709 break;
710 case NL80211_IFTYPE_STATION:
711 *aid = 1;
712 break;
713 case NL80211_IFTYPE_AP:
714 case NL80211_IFTYPE_ADHOC:
715 case NL80211_IFTYPE_MESH_POINT:
716 if (info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM) {
717 *aid = 0;
718 *queue = P54_QUEUE_CAB;
719 return;
722 if (unlikely(ieee80211_is_mgmt(hdr->frame_control))) {
723 if (ieee80211_is_probe_resp(hdr->frame_control)) {
724 *aid = 0;
725 *flags |= P54_HDR_FLAG_DATA_OUT_TIMESTAMP |
726 P54_HDR_FLAG_DATA_OUT_NOCANCEL;
727 return;
728 } else if (ieee80211_is_beacon(hdr->frame_control)) {
729 *aid = 0;
731 if (info->flags & IEEE80211_TX_CTL_INJECTED) {
733 * Injecting beacons on top of a AP is
734 * not a good idea... nevertheless,
735 * it should be doable.
738 return;
741 *flags |= P54_HDR_FLAG_DATA_OUT_TIMESTAMP;
742 *queue = P54_QUEUE_BEACON;
743 *extra_len = IEEE80211_MAX_TIM_LEN;
744 return;
748 if (info->control.sta)
749 *aid = info->control.sta->aid;
750 break;
754 static u8 p54_convert_algo(u32 cipher)
756 switch (cipher) {
757 case WLAN_CIPHER_SUITE_WEP40:
758 case WLAN_CIPHER_SUITE_WEP104:
759 return P54_CRYPTO_WEP;
760 case WLAN_CIPHER_SUITE_TKIP:
761 return P54_CRYPTO_TKIPMICHAEL;
762 case WLAN_CIPHER_SUITE_CCMP:
763 return P54_CRYPTO_AESCCMP;
764 default:
765 return 0;
769 void p54_tx_80211(struct ieee80211_hw *dev, struct sk_buff *skb)
771 struct p54_common *priv = dev->priv;
772 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
773 struct p54_tx_info *p54info;
774 struct p54_hdr *hdr;
775 struct p54_tx_data *txhdr;
776 unsigned int padding, len, extra_len = 0;
777 int i, j, ridx;
778 u16 hdr_flags = 0, aid = 0;
779 u8 rate, queue = 0, crypt_offset = 0;
780 u8 cts_rate = 0x20;
781 u8 rc_flags;
782 u8 calculated_tries[4];
783 u8 nrates = 0, nremaining = 8;
784 bool burst_allowed = false;
786 p54_tx_80211_header(priv, skb, info, &queue, &extra_len,
787 &hdr_flags, &aid, &burst_allowed);
789 if (p54_tx_qos_accounting_alloc(priv, skb, queue)) {
790 dev_kfree_skb_any(skb);
791 return;
794 padding = (unsigned long)(skb->data - (sizeof(*hdr) + sizeof(*txhdr))) & 3;
795 len = skb->len;
797 if (info->control.hw_key) {
798 crypt_offset = ieee80211_get_hdrlen_from_skb(skb);
799 if (info->control.hw_key->cipher == WLAN_CIPHER_SUITE_TKIP) {
800 u8 *iv = (u8 *)(skb->data + crypt_offset);
802 * The firmware excepts that the IV has to have
803 * this special format
805 iv[1] = iv[0];
806 iv[0] = iv[2];
807 iv[2] = 0;
811 txhdr = (struct p54_tx_data *) skb_push(skb, sizeof(*txhdr) + padding);
812 hdr = (struct p54_hdr *) skb_push(skb, sizeof(*hdr));
814 if (padding)
815 hdr_flags |= P54_HDR_FLAG_DATA_ALIGN;
816 hdr->type = cpu_to_le16(aid);
817 hdr->rts_tries = info->control.rates[0].count;
820 * we register the rates in perfect order, and
821 * RTS/CTS won't happen on 5 GHz
823 cts_rate = info->control.rts_cts_rate_idx;
825 memset(&txhdr->rateset, 0, sizeof(txhdr->rateset));
827 /* see how many rates got used */
828 for (i = 0; i < dev->max_rates; i++) {
829 if (info->control.rates[i].idx < 0)
830 break;
831 nrates++;
834 /* limit tries to 8/nrates per rate */
835 for (i = 0; i < nrates; i++) {
837 * The magic expression here is equivalent to 8/nrates for
838 * all values that matter, but avoids division and jumps.
839 * Note that nrates can only take the values 1 through 4.
841 calculated_tries[i] = min_t(int, ((15 >> nrates) | 1) + 1,
842 info->control.rates[i].count);
843 nremaining -= calculated_tries[i];
846 /* if there are tries left, distribute from back to front */
847 for (i = nrates - 1; nremaining > 0 && i >= 0; i--) {
848 int tmp = info->control.rates[i].count - calculated_tries[i];
850 if (tmp <= 0)
851 continue;
852 /* RC requested more tries at this rate */
854 tmp = min_t(int, tmp, nremaining);
855 calculated_tries[i] += tmp;
856 nremaining -= tmp;
859 ridx = 0;
860 for (i = 0; i < nrates && ridx < 8; i++) {
861 /* we register the rates in perfect order */
862 rate = info->control.rates[i].idx;
863 if (info->band == IEEE80211_BAND_5GHZ)
864 rate += 4;
866 /* store the count we actually calculated for TX status */
867 info->control.rates[i].count = calculated_tries[i];
869 rc_flags = info->control.rates[i].flags;
870 if (rc_flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE) {
871 rate |= 0x10;
872 cts_rate |= 0x10;
874 if (rc_flags & IEEE80211_TX_RC_USE_RTS_CTS) {
875 burst_allowed = false;
876 rate |= 0x40;
877 } else if (rc_flags & IEEE80211_TX_RC_USE_CTS_PROTECT) {
878 rate |= 0x20;
879 burst_allowed = false;
881 for (j = 0; j < calculated_tries[i] && ridx < 8; j++) {
882 txhdr->rateset[ridx] = rate;
883 ridx++;
887 if (burst_allowed)
888 hdr_flags |= P54_HDR_FLAG_DATA_OUT_BURST;
890 /* TODO: enable bursting */
891 hdr->flags = cpu_to_le16(hdr_flags);
892 hdr->tries = ridx;
893 txhdr->rts_rate_idx = 0;
894 if (info->control.hw_key) {
895 txhdr->key_type = p54_convert_algo(info->control.hw_key->cipher);
896 txhdr->key_len = min((u8)16, info->control.hw_key->keylen);
897 memcpy(txhdr->key, info->control.hw_key->key, txhdr->key_len);
898 if (info->control.hw_key->cipher == WLAN_CIPHER_SUITE_TKIP) {
899 /* reserve space for the MIC key */
900 len += 8;
901 memcpy(skb_put(skb, 8), &(info->control.hw_key->key
902 [NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY]), 8);
904 /* reserve some space for ICV */
905 len += info->control.hw_key->icv_len;
906 memset(skb_put(skb, info->control.hw_key->icv_len), 0,
907 info->control.hw_key->icv_len);
908 } else {
909 txhdr->key_type = 0;
910 txhdr->key_len = 0;
912 txhdr->crypt_offset = crypt_offset;
913 txhdr->hw_queue = queue;
914 txhdr->backlog = priv->tx_stats[queue].len - 1;
915 memset(txhdr->durations, 0, sizeof(txhdr->durations));
916 txhdr->tx_antenna = ((info->antenna_sel_tx == 0) ?
917 2 : info->antenna_sel_tx - 1) & priv->tx_diversity_mask;
918 if (priv->rxhw == 5) {
919 txhdr->longbow.cts_rate = cts_rate;
920 txhdr->longbow.output_power = cpu_to_le16(priv->output_power);
921 } else {
922 txhdr->normal.output_power = priv->output_power;
923 txhdr->normal.cts_rate = cts_rate;
925 if (padding)
926 txhdr->align[0] = padding;
928 hdr->len = cpu_to_le16(len);
929 /* modifies skb->cb and with it info, so must be last! */
930 p54info = (void *) info->rate_driver_data;
931 p54info->extra_len = extra_len;
933 p54_tx(priv, skb);