943 lines · c
1// SPDX-License-Identifier: GPL-2.0-only2/*3 * Common code for mac80211 Prism54 drivers4 *5 * Copyright (c) 2006, Michael Wu <flamingice@sourmilk.net>6 * Copyright (c) 2007-2009, Christian Lamparter <chunkeey@web.de>7 * Copyright 2008, Johannes Berg <johannes@sipsolutions.net>8 *9 * Based on:10 * - the islsm (softmac prism54) driver, which is:11 * Copyright 2004-2006 Jean-Baptiste Note <jbnote@gmail.com>, et al.12 * - stlc45xx driver13 * Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies).14 */15 16#include <linux/export.h>17#include <linux/firmware.h>18#include <linux/etherdevice.h>19#include <asm/div64.h>20 21#include <net/mac80211.h>22 23#include "p54.h"24#include "lmac.h"25 26#ifdef P54_MM_DEBUG27static void p54_dump_tx_queue(struct p54_common *priv)28{29 unsigned long flags;30 struct ieee80211_tx_info *info;31 struct p54_tx_info *range;32 struct sk_buff *skb;33 struct p54_hdr *hdr;34 unsigned int i = 0;35 u32 prev_addr;36 u32 largest_hole = 0, free;37 38 spin_lock_irqsave(&priv->tx_queue.lock, flags);39 wiphy_debug(priv->hw->wiphy, "/ --- tx queue dump (%d entries) ---\n",40 skb_queue_len(&priv->tx_queue));41 42 prev_addr = priv->rx_start;43 skb_queue_walk(&priv->tx_queue, skb) {44 info = IEEE80211_SKB_CB(skb);45 range = (void *) info->rate_driver_data;46 hdr = (void *) skb->data;47 48 free = range->start_addr - prev_addr;49 wiphy_debug(priv->hw->wiphy,50 "| [%02d] => [skb:%p skb_len:0x%04x "51 "hdr:{flags:%02x len:%04x req_id:%04x type:%02x} "52 "mem:{start:%04x end:%04x, free:%d}]\n",53 i++, skb, skb->len,54 le16_to_cpu(hdr->flags), le16_to_cpu(hdr->len),55 le32_to_cpu(hdr->req_id), le16_to_cpu(hdr->type),56 range->start_addr, range->end_addr, free);57 58 prev_addr = range->end_addr;59 largest_hole = max(largest_hole, free);60 }61 free = priv->rx_end - prev_addr;62 largest_hole = max(largest_hole, free);63 wiphy_debug(priv->hw->wiphy,64 "\\ --- [free: %d], largest free block: %d ---\n",65 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 its72 * memory incoming data should go to. By poking around in the firmware, we73 * can find some unused memory to upload our packets to. However, data that we74 * want the card to TX needs to stay intact until the card has told us that75 * it is done with it. This function finds empty places we can upload to and76 * marks allocated areas as reserved if necessary. p54_find_and_unlink_skb or77 * p54_free_skb frees allocated areas.78 */79static int p54_assign_address(struct p54_common *priv, struct sk_buff *skb)80{81 struct sk_buff *entry, *target_skb = NULL;82 struct ieee80211_tx_info *info;83 struct p54_tx_info *range;84 struct p54_hdr *data = (void *) skb->data;85 unsigned long flags;86 u32 last_addr = priv->rx_start;87 u32 target_addr = priv->rx_start;88 u16 len = priv->headroom + skb->len + priv->tailroom + 3;89 90 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 = skb_peek_tail(&priv->tx_queue);122 if (target_skb) {123 info = IEEE80211_SKB_CB(target_skb);124 range = (void *)info->rate_driver_data;125 target_addr = range->end_addr;126 }127 } else {128 spin_unlock_irqrestore(&priv->tx_queue.lock, flags);129 return -ENOSPC;130 }131 }132 133 info = IEEE80211_SKB_CB(skb);134 range = (void *) info->rate_driver_data;135 range->start_addr = target_addr;136 range->end_addr = target_addr + len;137 data->req_id = cpu_to_le32(target_addr + priv->headroom);138 if (IS_DATA_FRAME(skb) &&139 unlikely(GET_HW_QUEUE(skb) == P54_QUEUE_BEACON))140 priv->beacon_req_id = data->req_id;141 142 if (target_skb)143 __skb_queue_after(&priv->tx_queue, target_skb, skb);144 else145 __skb_queue_head(&priv->tx_queue, skb);146 spin_unlock_irqrestore(&priv->tx_queue.lock, flags);147 return 0;148}149 150static void p54_tx_pending(struct p54_common *priv)151{152 struct sk_buff *skb;153 int ret;154 155 skb = skb_dequeue(&priv->tx_pending);156 if (unlikely(!skb))157 return ;158 159 ret = p54_assign_address(priv, skb);160 if (unlikely(ret))161 skb_queue_head(&priv->tx_pending, skb);162 else163 priv->tx(priv->hw, skb);164}165 166static void p54_wake_queues(struct p54_common *priv)167{168 unsigned long flags;169 unsigned int i;170 171 if (unlikely(priv->mode == NL80211_IFTYPE_UNSPECIFIED))172 return ;173 174 p54_tx_pending(priv);175 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);181 }182 spin_unlock_irqrestore(&priv->tx_stats_lock, flags);183}184 185static int p54_tx_qos_accounting_alloc(struct p54_common *priv,186 struct sk_buff *skb,187 const u16 p54_queue)188{189 struct p54_tx_queue_stats *queue;190 unsigned long flags;191 192 if (WARN_ON(p54_queue >= P54_QUEUE_NUM))193 return -EINVAL;194 195 queue = &priv->tx_stats[p54_queue];196 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;201 }202 203 queue->len++;204 queue->count++;205 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);209 }210 211 spin_unlock_irqrestore(&priv->tx_stats_lock, flags);212 return 0;213}214 215static void p54_tx_qos_accounting_free(struct p54_common *priv,216 struct sk_buff *skb)217{218 if (IS_DATA_FRAME(skb)) {219 unsigned long flags;220 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);224 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;229 }230 complete(&priv->beacon_comp);231 }232 }233 p54_wake_queues(priv);234}235 236void p54_free_skb(struct ieee80211_hw *dev, struct sk_buff *skb)237{238 struct p54_common *priv = dev->priv;239 if (unlikely(!skb))240 return ;241 242 skb_unlink(skb, &priv->tx_queue);243 p54_tx_qos_accounting_free(priv, skb);244 ieee80211_free_txskb(dev, skb);245}246EXPORT_SYMBOL_GPL(p54_free_skb);247 248static struct sk_buff *p54_find_and_unlink_skb(struct p54_common *priv,249 const __le32 req_id)250{251 struct sk_buff *entry;252 unsigned long flags;253 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;257 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;263 }264 }265 spin_unlock_irqrestore(&priv->tx_queue.lock, flags);266 return NULL;267}268 269void p54_tx(struct p54_common *priv, struct sk_buff *skb)270{271 skb_queue_tail(&priv->tx_pending, skb);272 p54_tx_pending(priv);273}274 275static int p54_rssi_to_dbm(struct p54_common *priv, int rssi)276{277 if (priv->rxhw != 5) {278 return ((rssi * priv->cur_rssi->mul) / 64 +279 priv->cur_rssi->add) / 4;280 } else {281 /*282 * TODO: find the correct formula283 */284 return rssi / 2 - 110;285 }286}287 288/*289 * Even if the firmware is capable of dealing with incoming traffic,290 * while dozing, we have to prepared in case mac80211 uses PS-POLL291 * to retrieve outstanding frames from our AP.292 * (see comment in net/mac80211/mlme.c @ line 1993)293 */294static void p54_pspoll_workaround(struct p54_common *priv, struct sk_buff *skb)295{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;301 302 /* only beacons have a TIM IE */303 if (!ieee80211_is_beacon(hdr->frame_control))304 return;305 306 if (!priv->aid)307 return;308 309 /* only consider beacons from the associated BSSID */310 if (!ether_addr_equal_64bits(hdr->addr3, priv->bssid))311 return;312 313 tim = p54_find_ie(skb, WLAN_EID_TIM);314 if (!tim)315 return;316 317 tim_len = tim[1];318 tim_ie = (struct ieee80211_tim_ie *) &tim[2];319 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);324 }325}326 327static int p54_rx_data(struct p54_common *priv, struct sk_buff *skb)328{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 __le16 fc;335 u8 rate = hdr->rate & 0xf;336 337 /*338 * If the device is in a unspecified state we have to339 * ignore all data frames. Else we could end up with a340 * 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->enc_flags |= RX_ENC_FLAG_SHORTPRE;357 if (priv->hw->conf.chandef.chan->band == NL80211_BAND_5GHZ)358 rx_status->rate_idx = (rate < 4) ? 0 : rate - 4;359 else360 rx_status->rate_idx = rate;361 362 rx_status->freq = freq;363 rx_status->band = priv->hw->conf.chandef.chan->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 /* LMAC API Page 10/29 - s_lm_data_in - clock373 * "usec accurate timestamp of hardware clock374 * at end of frame (before OFDM SIFS EOF padding"375 */376 rx_status->flag |= RX_FLAG_MACTIME_END;377 378 if (hdr->flags & cpu_to_le16(P54_HDR_FLAG_DATA_ALIGN))379 header_len += hdr->align[0];380 381 skb_pull(skb, header_len);382 skb_trim(skb, le16_to_cpu(hdr->len));383 384 fc = ((struct ieee80211_hdr *)skb->data)->frame_control;385 if (ieee80211_is_probe_resp(fc) || ieee80211_is_beacon(fc))386 rx_status->boottime_ns = ktime_get_boottime_ns();387 388 if (unlikely(priv->hw->conf.flags & IEEE80211_CONF_PS))389 p54_pspoll_workaround(priv, skb);390 391 ieee80211_rx_irqsafe(priv->hw, skb);392 393 ieee80211_queue_delayed_work(priv->hw, &priv->work,394 msecs_to_jiffies(P54_STATISTICS_UPDATE));395 396 return -1;397}398 399static void p54_rx_frame_sent(struct p54_common *priv, struct sk_buff *skb)400{401 struct p54_hdr *hdr = (struct p54_hdr *) skb->data;402 struct p54_frame_sent *payload = (struct p54_frame_sent *) hdr->data;403 struct ieee80211_tx_info *info;404 struct p54_hdr *entry_hdr;405 struct p54_tx_data *entry_data;406 struct sk_buff *entry;407 unsigned int pad = 0, frame_len;408 int count, idx;409 410 entry = p54_find_and_unlink_skb(priv, hdr->req_id);411 if (unlikely(!entry))412 return ;413 414 frame_len = entry->len;415 info = IEEE80211_SKB_CB(entry);416 entry_hdr = (struct p54_hdr *) entry->data;417 entry_data = (struct p54_tx_data *) entry_hdr->data;418 priv->stats.dot11ACKFailureCount += payload->tries - 1;419 420 /*421 * Frames in P54_QUEUE_FWSCAN and P54_QUEUE_BEACON are422 * generated by the driver. Therefore tx_status is bogus423 * and we don't want to confuse the mac80211 stack.424 */425 if (unlikely(entry_data->hw_queue < P54_QUEUE_FWSCAN)) {426 dev_kfree_skb_any(entry);427 return ;428 }429 430 /*431 * Clear manually, ieee80211_tx_info_clear_status would432 * clear the counts too and we need them.433 */434 memset_after(&info->status, 0, rates);435 436 if (entry_hdr->flags & cpu_to_le16(P54_HDR_FLAG_DATA_ALIGN))437 pad = entry_data->align[0];438 439 /* walk through the rates array and adjust the counts */440 count = payload->tries;441 for (idx = 0; idx < 4; idx++) {442 if (count >= info->status.rates[idx].count) {443 count -= info->status.rates[idx].count;444 } else if (count > 0) {445 info->status.rates[idx].count = count;446 count = 0;447 } else {448 info->status.rates[idx].idx = -1;449 info->status.rates[idx].count = 0;450 }451 }452 453 if (!(info->flags & IEEE80211_TX_CTL_NO_ACK) &&454 !(payload->status & P54_TX_FAILED))455 info->flags |= IEEE80211_TX_STAT_ACK;456 if (payload->status & P54_TX_PSM_CANCELLED)457 info->flags |= IEEE80211_TX_STAT_TX_FILTERED;458 info->status.ack_signal = p54_rssi_to_dbm(priv,459 (int)payload->ack_rssi);460 461 /* Undo all changes to the frame. */462 switch (entry_data->key_type) {463 case P54_CRYPTO_TKIPMICHAEL: {464 u8 *iv = (u8 *)(entry_data->align + pad +465 entry_data->crypt_offset);466 467 /* Restore the original TKIP IV. */468 iv[2] = iv[0];469 iv[0] = iv[1];470 iv[1] = (iv[0] | 0x20) & 0x7f; /* WEPSeed - 8.3.2.2 */471 472 frame_len -= 12; /* remove TKIP_MMIC + TKIP_ICV */473 break;474 }475 case P54_CRYPTO_AESCCMP:476 frame_len -= 8; /* remove CCMP_MIC */477 break;478 case P54_CRYPTO_WEP:479 frame_len -= 4; /* remove WEP_ICV */480 break;481 }482 483 skb_trim(entry, frame_len);484 skb_pull(entry, sizeof(*hdr) + pad + sizeof(*entry_data));485 ieee80211_tx_status_irqsafe(priv->hw, entry);486}487 488static void p54_rx_eeprom_readback(struct p54_common *priv,489 struct sk_buff *skb)490{491 struct p54_hdr *hdr = (struct p54_hdr *) skb->data;492 struct p54_eeprom_lm86 *eeprom = (struct p54_eeprom_lm86 *) hdr->data;493 struct sk_buff *tmp;494 495 if (!priv->eeprom)496 return ;497 498 if (priv->fw_var >= 0x509) {499 memcpy(priv->eeprom, eeprom->v2.data,500 le16_to_cpu(eeprom->v2.len));501 } else {502 memcpy(priv->eeprom, eeprom->v1.data,503 le16_to_cpu(eeprom->v1.len));504 }505 506 priv->eeprom = NULL;507 tmp = p54_find_and_unlink_skb(priv, hdr->req_id);508 dev_kfree_skb_any(tmp);509 complete(&priv->eeprom_comp);510}511 512static void p54_rx_stats(struct p54_common *priv, struct sk_buff *skb)513{514 struct p54_hdr *hdr = (struct p54_hdr *) skb->data;515 struct p54_statistics *stats = (struct p54_statistics *) hdr->data;516 struct sk_buff *tmp;517 struct ieee80211_channel *chan;518 unsigned int i, rssi, tx, cca, dtime, dtotal, dcca, dtx, drssi, unit;519 u32 tsf32;520 521 if (unlikely(priv->mode == NL80211_IFTYPE_UNSPECIFIED))522 return ;523 524 tsf32 = le32_to_cpu(stats->tsf32);525 if (tsf32 < priv->tsf_low32)526 priv->tsf_high32++;527 priv->tsf_low32 = tsf32;528 529 priv->stats.dot11RTSFailureCount = le32_to_cpu(stats->rts_fail);530 priv->stats.dot11RTSSuccessCount = le32_to_cpu(stats->rts_success);531 priv->stats.dot11FCSErrorCount = le32_to_cpu(stats->rx_bad_fcs);532 533 priv->noise = p54_rssi_to_dbm(priv, le32_to_cpu(stats->noise));534 535 /*536 * STSW450X LMAC API page 26 - 3.8 Statistics537 * "The exact measurement period can be derived from the538 * timestamp member".539 */540 dtime = tsf32 - priv->survey_raw.timestamp;541 542 /*543 * STSW450X LMAC API page 26 - 3.8.1 Noise histogram544 * The LMAC samples RSSI, CCA and transmit state at regular545 * periods (typically 8 times per 1k [as in 1024] usec).546 */547 cca = le32_to_cpu(stats->sample_cca);548 tx = le32_to_cpu(stats->sample_tx);549 rssi = 0;550 for (i = 0; i < ARRAY_SIZE(stats->sample_noise); i++)551 rssi += le32_to_cpu(stats->sample_noise[i]);552 553 dcca = cca - priv->survey_raw.cached_cca;554 drssi = rssi - priv->survey_raw.cached_rssi;555 dtx = tx - priv->survey_raw.cached_tx;556 dtotal = dcca + drssi + dtx;557 558 /*559 * update statistics when more than a second is over since the560 * last call, or when a update is badly needed.561 */562 if (dtotal && (priv->update_stats || dtime >= USEC_PER_SEC) &&563 dtime >= dtotal) {564 priv->survey_raw.timestamp = tsf32;565 priv->update_stats = false;566 unit = dtime / dtotal;567 568 if (dcca) {569 priv->survey_raw.cca += dcca * unit;570 priv->survey_raw.cached_cca = cca;571 }572 if (dtx) {573 priv->survey_raw.tx += dtx * unit;574 priv->survey_raw.cached_tx = tx;575 }576 if (drssi) {577 priv->survey_raw.rssi += drssi * unit;578 priv->survey_raw.cached_rssi = rssi;579 }580 581 /* 1024 usec / 8 times = 128 usec / time */582 if (!(priv->phy_ps || priv->phy_idle))583 priv->survey_raw.active += dtotal * unit;584 else585 priv->survey_raw.active += (dcca + dtx) * unit;586 }587 588 chan = priv->curchan;589 if (chan) {590 struct survey_info *survey = &priv->survey[chan->hw_value];591 survey->noise = clamp(priv->noise, -128, 127);592 survey->time = priv->survey_raw.active;593 survey->time_tx = priv->survey_raw.tx;594 survey->time_busy = priv->survey_raw.tx +595 priv->survey_raw.cca;596 do_div(survey->time, 1024);597 do_div(survey->time_tx, 1024);598 do_div(survey->time_busy, 1024);599 }600 601 tmp = p54_find_and_unlink_skb(priv, hdr->req_id);602 dev_kfree_skb_any(tmp);603 complete(&priv->stat_comp);604}605 606static void p54_rx_trap(struct p54_common *priv, struct sk_buff *skb)607{608 struct p54_hdr *hdr = (struct p54_hdr *) skb->data;609 struct p54_trap *trap = (struct p54_trap *) hdr->data;610 u16 event = le16_to_cpu(trap->event);611 u16 freq = le16_to_cpu(trap->frequency);612 613 switch (event) {614 case P54_TRAP_BEACON_TX:615 break;616 case P54_TRAP_RADAR:617 wiphy_info(priv->hw->wiphy, "radar (freq:%d MHz)\n", freq);618 break;619 case P54_TRAP_NO_BEACON:620 if (priv->vif)621 ieee80211_beacon_loss(priv->vif);622 break;623 case P54_TRAP_SCAN:624 break;625 case P54_TRAP_TBTT:626 break;627 case P54_TRAP_TIMER:628 break;629 case P54_TRAP_FAA_RADIO_OFF:630 wiphy_rfkill_set_hw_state(priv->hw->wiphy, true);631 break;632 case P54_TRAP_FAA_RADIO_ON:633 wiphy_rfkill_set_hw_state(priv->hw->wiphy, false);634 break;635 default:636 wiphy_info(priv->hw->wiphy, "received event:%x freq:%d\n",637 event, freq);638 break;639 }640}641 642static int p54_rx_control(struct p54_common *priv, struct sk_buff *skb)643{644 struct p54_hdr *hdr = (struct p54_hdr *) skb->data;645 646 switch (le16_to_cpu(hdr->type)) {647 case P54_CONTROL_TYPE_TXDONE:648 p54_rx_frame_sent(priv, skb);649 break;650 case P54_CONTROL_TYPE_TRAP:651 p54_rx_trap(priv, skb);652 break;653 case P54_CONTROL_TYPE_BBP:654 break;655 case P54_CONTROL_TYPE_STAT_READBACK:656 p54_rx_stats(priv, skb);657 break;658 case P54_CONTROL_TYPE_EEPROM_READBACK:659 p54_rx_eeprom_readback(priv, skb);660 break;661 default:662 wiphy_debug(priv->hw->wiphy,663 "not handling 0x%02x type control frame\n",664 le16_to_cpu(hdr->type));665 break;666 }667 return 0;668}669 670/* returns zero if skb can be reused */671int p54_rx(struct ieee80211_hw *dev, struct sk_buff *skb)672{673 struct p54_common *priv = dev->priv;674 u16 type = le16_to_cpu(*((__le16 *)skb->data));675 676 if (type & P54_HDR_FLAG_CONTROL)677 return p54_rx_control(priv, skb);678 else679 return p54_rx_data(priv, skb);680}681EXPORT_SYMBOL_GPL(p54_rx);682 683static void p54_tx_80211_header(struct p54_common *priv, struct sk_buff *skb,684 struct ieee80211_tx_info *info,685 struct ieee80211_sta *sta,686 u8 *queue, u32 *extra_len, u16 *flags, u16 *aid,687 bool *burst_possible)688{689 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;690 691 if (ieee80211_is_data_qos(hdr->frame_control))692 *burst_possible = true;693 else694 *burst_possible = false;695 696 if (!(info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ))697 *flags |= P54_HDR_FLAG_DATA_OUT_SEQNR;698 699 if (info->flags & IEEE80211_TX_CTL_NO_PS_BUFFER)700 *flags |= P54_HDR_FLAG_DATA_OUT_NOCANCEL;701 702 if (info->flags & IEEE80211_TX_CTL_CLEAR_PS_FILT)703 *flags |= P54_HDR_FLAG_DATA_OUT_NOCANCEL;704 705 *queue = skb_get_queue_mapping(skb) + P54_QUEUE_DATA;706 707 switch (priv->mode) {708 case NL80211_IFTYPE_MONITOR:709 /*710 * We have to set P54_HDR_FLAG_DATA_OUT_PROMISC for711 * every frame in promiscuous/monitor mode.712 * see STSW45x0C LMAC API - page 12.713 */714 *aid = 0;715 *flags |= P54_HDR_FLAG_DATA_OUT_PROMISC;716 break;717 case NL80211_IFTYPE_STATION:718 *aid = 1;719 break;720 case NL80211_IFTYPE_AP:721 case NL80211_IFTYPE_ADHOC:722 case NL80211_IFTYPE_MESH_POINT:723 if (info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM) {724 *aid = 0;725 *queue = P54_QUEUE_CAB;726 return;727 }728 729 if (unlikely(ieee80211_is_mgmt(hdr->frame_control))) {730 if (ieee80211_is_probe_resp(hdr->frame_control)) {731 *aid = 0;732 *flags |= P54_HDR_FLAG_DATA_OUT_TIMESTAMP |733 P54_HDR_FLAG_DATA_OUT_NOCANCEL;734 return;735 } else if (ieee80211_is_beacon(hdr->frame_control)) {736 *aid = 0;737 738 if (info->flags & IEEE80211_TX_CTL_INJECTED) {739 /*740 * Injecting beacons on top of a AP is741 * not a good idea... nevertheless,742 * it should be doable.743 */744 745 return;746 }747 748 *flags |= P54_HDR_FLAG_DATA_OUT_TIMESTAMP;749 *queue = P54_QUEUE_BEACON;750 *extra_len = IEEE80211_MAX_TIM_LEN;751 return;752 }753 }754 755 if (sta)756 *aid = sta->aid;757 break;758 }759}760 761static u8 p54_convert_algo(u32 cipher)762{763 switch (cipher) {764 case WLAN_CIPHER_SUITE_WEP40:765 case WLAN_CIPHER_SUITE_WEP104:766 return P54_CRYPTO_WEP;767 case WLAN_CIPHER_SUITE_TKIP:768 return P54_CRYPTO_TKIPMICHAEL;769 case WLAN_CIPHER_SUITE_CCMP:770 return P54_CRYPTO_AESCCMP;771 default:772 return 0;773 }774}775 776void p54_tx_80211(struct ieee80211_hw *dev,777 struct ieee80211_tx_control *control,778 struct sk_buff *skb)779{780 struct p54_common *priv = dev->priv;781 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);782 struct p54_tx_info *p54info;783 struct p54_hdr *hdr;784 struct p54_tx_data *txhdr;785 unsigned int padding, len, extra_len = 0;786 int i, j, ridx;787 u16 hdr_flags = 0, aid = 0;788 u8 rate, queue = 0, crypt_offset = 0;789 u8 cts_rate = 0x20;790 u8 rc_flags;791 u8 calculated_tries[4];792 u8 nrates = 0, nremaining = 8;793 bool burst_allowed = false;794 795 p54_tx_80211_header(priv, skb, info, control->sta, &queue, &extra_len,796 &hdr_flags, &aid, &burst_allowed);797 798 if (p54_tx_qos_accounting_alloc(priv, skb, queue)) {799 ieee80211_free_txskb(dev, skb);800 return;801 }802 803 padding = (unsigned long)(skb->data - (sizeof(*hdr) + sizeof(*txhdr))) & 3;804 len = skb->len;805 806 if (info->control.hw_key) {807 crypt_offset = ieee80211_get_hdrlen_from_skb(skb);808 if (info->control.hw_key->cipher == WLAN_CIPHER_SUITE_TKIP) {809 u8 *iv = (u8 *)(skb->data + crypt_offset);810 /*811 * The firmware excepts that the IV has to have812 * this special format813 */814 iv[1] = iv[0];815 iv[0] = iv[2];816 iv[2] = 0;817 }818 }819 820 txhdr = skb_push(skb, sizeof(*txhdr) + padding);821 hdr = skb_push(skb, sizeof(*hdr));822 823 if (padding)824 hdr_flags |= P54_HDR_FLAG_DATA_ALIGN;825 hdr->type = cpu_to_le16(aid);826 hdr->rts_tries = info->control.rates[0].count;827 828 /*829 * we register the rates in perfect order, and830 * RTS/CTS won't happen on 5 GHz831 */832 cts_rate = info->control.rts_cts_rate_idx;833 834 memset(&txhdr->rateset, 0, sizeof(txhdr->rateset));835 836 /* see how many rates got used */837 for (i = 0; i < dev->max_rates; i++) {838 if (info->control.rates[i].idx < 0)839 break;840 nrates++;841 }842 843 /* limit tries to 8/nrates per rate */844 for (i = 0; i < nrates; i++) {845 /*846 * The magic expression here is equivalent to 8/nrates for847 * all values that matter, but avoids division and jumps.848 * Note that nrates can only take the values 1 through 4.849 */850 calculated_tries[i] = min_t(int, ((15 >> nrates) | 1) + 1,851 info->control.rates[i].count);852 nremaining -= calculated_tries[i];853 }854 855 /* if there are tries left, distribute from back to front */856 for (i = nrates - 1; nremaining > 0 && i >= 0; i--) {857 int tmp = info->control.rates[i].count - calculated_tries[i];858 859 if (tmp <= 0)860 continue;861 /* RC requested more tries at this rate */862 863 tmp = min_t(int, tmp, nremaining);864 calculated_tries[i] += tmp;865 nremaining -= tmp;866 }867 868 ridx = 0;869 for (i = 0; i < nrates && ridx < 8; i++) {870 /* we register the rates in perfect order */871 rate = info->control.rates[i].idx;872 if (info->band == NL80211_BAND_5GHZ)873 rate += 4;874 875 /* store the count we actually calculated for TX status */876 info->control.rates[i].count = calculated_tries[i];877 878 rc_flags = info->control.rates[i].flags;879 if (rc_flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE) {880 rate |= 0x10;881 cts_rate |= 0x10;882 }883 if (rc_flags & IEEE80211_TX_RC_USE_RTS_CTS) {884 burst_allowed = false;885 rate |= 0x40;886 } else if (rc_flags & IEEE80211_TX_RC_USE_CTS_PROTECT) {887 rate |= 0x20;888 burst_allowed = false;889 }890 for (j = 0; j < calculated_tries[i] && ridx < 8; j++) {891 txhdr->rateset[ridx] = rate;892 ridx++;893 }894 }895 896 if (burst_allowed)897 hdr_flags |= P54_HDR_FLAG_DATA_OUT_BURST;898 899 /* TODO: enable bursting */900 hdr->flags = cpu_to_le16(hdr_flags);901 hdr->tries = ridx;902 txhdr->rts_rate_idx = 0;903 if (info->control.hw_key) {904 txhdr->key_type = p54_convert_algo(info->control.hw_key->cipher);905 txhdr->key_len = min((u8)16, info->control.hw_key->keylen);906 memcpy(txhdr->key, info->control.hw_key->key, txhdr->key_len);907 if (info->control.hw_key->cipher == WLAN_CIPHER_SUITE_TKIP) {908 /* reserve space for the MIC key */909 len += 8;910 skb_put_data(skb,911 &(info->control.hw_key->key[NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY]),912 8);913 }914 /* reserve some space for ICV */915 len += info->control.hw_key->icv_len;916 skb_put_zero(skb, info->control.hw_key->icv_len);917 } else {918 txhdr->key_type = 0;919 txhdr->key_len = 0;920 }921 txhdr->crypt_offset = crypt_offset;922 txhdr->hw_queue = queue;923 txhdr->backlog = priv->tx_stats[queue].len - 1;924 memset(txhdr->durations, 0, sizeof(txhdr->durations));925 txhdr->tx_antenna = 2 & priv->tx_diversity_mask;926 if (priv->rxhw == 5) {927 txhdr->longbow.cts_rate = cts_rate;928 txhdr->longbow.output_power = cpu_to_le16(priv->output_power);929 } else {930 txhdr->normal.output_power = priv->output_power;931 txhdr->normal.cts_rate = cts_rate;932 }933 if (padding)934 txhdr->align[0] = padding;935 936 hdr->len = cpu_to_le16(len);937 /* modifies skb->cb and with it info, so must be last! */938 p54info = (void *) info->rate_driver_data;939 p54info->extra_len = extra_len;940 941 p54_tx(priv, skb);942}943