3 Broadcom B43 wireless driver
5 Transmission (TX/RX) related functions.
7 Copyright (C) 2005 Martin Langer <martin-langer@gmx.de>
8 Copyright (C) 2005 Stefano Brivio <stefano.brivio@polimi.it>
9 Copyright (C) 2005, 2006 Michael Buesch <mb@bu3sch.de>
10 Copyright (C) 2005 Danny van Dyk <kugelfang@gentoo.org>
11 Copyright (C) 2005 Andreas Jaggi <andreas.jaggi@waterwave.ch>
13 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2 of the License, or
16 (at your option) any later version.
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program; see the file COPYING. If not, write to
25 the Free Software Foundation, Inc., 51 Franklin Steet, Fifth Floor,
26 Boston, MA 02110-1301, USA.
31 #include "phy_common.h"
36 /* Extract the bitrate index out of a CCK PLCP header. */
37 static int b43_plcp_get_bitrate_idx_cck(struct b43_plcp_hdr6 *plcp)
39 switch (plcp->raw[0]) {
52 /* Extract the bitrate index out of an OFDM PLCP header. */
53 static u8 b43_plcp_get_bitrate_idx_ofdm(struct b43_plcp_hdr6 *plcp, bool aphy)
55 int base = aphy ? 0 : 4;
57 switch (plcp->raw[0] & 0xF) {
78 u8 b43_plcp_get_ratecode_cck(const u8 bitrate)
81 case B43_CCK_RATE_1MB:
83 case B43_CCK_RATE_2MB:
85 case B43_CCK_RATE_5MB:
87 case B43_CCK_RATE_11MB:
94 u8 b43_plcp_get_ratecode_ofdm(const u8 bitrate)
97 case B43_OFDM_RATE_6MB:
99 case B43_OFDM_RATE_9MB:
101 case B43_OFDM_RATE_12MB:
103 case B43_OFDM_RATE_18MB:
105 case B43_OFDM_RATE_24MB:
107 case B43_OFDM_RATE_36MB:
109 case B43_OFDM_RATE_48MB:
111 case B43_OFDM_RATE_54MB:
118 void b43_generate_plcp_hdr(struct b43_plcp_hdr4 *plcp,
119 const u16 octets, const u8 bitrate)
121 __le32 *data = &(plcp->data);
122 __u8 *raw = plcp->raw;
124 if (b43_is_ofdm_rate(bitrate)) {
127 d = b43_plcp_get_ratecode_ofdm(bitrate);
128 B43_WARN_ON(octets & 0xF000);
130 *data = cpu_to_le32(d);
134 plen = octets * 16 / bitrate;
135 if ((octets * 16 % bitrate) > 0) {
137 if ((bitrate == B43_CCK_RATE_11MB)
138 && ((octets * 8 % 11) < 4)) {
144 *data |= cpu_to_le32(plen << 16);
145 raw[0] = b43_plcp_get_ratecode_cck(bitrate);
149 static u8 b43_calc_fallback_rate(u8 bitrate)
152 case B43_CCK_RATE_1MB:
153 return B43_CCK_RATE_1MB;
154 case B43_CCK_RATE_2MB:
155 return B43_CCK_RATE_1MB;
156 case B43_CCK_RATE_5MB:
157 return B43_CCK_RATE_2MB;
158 case B43_CCK_RATE_11MB:
159 return B43_CCK_RATE_5MB;
160 case B43_OFDM_RATE_6MB:
161 return B43_CCK_RATE_5MB;
162 case B43_OFDM_RATE_9MB:
163 return B43_OFDM_RATE_6MB;
164 case B43_OFDM_RATE_12MB:
165 return B43_OFDM_RATE_9MB;
166 case B43_OFDM_RATE_18MB:
167 return B43_OFDM_RATE_12MB;
168 case B43_OFDM_RATE_24MB:
169 return B43_OFDM_RATE_18MB;
170 case B43_OFDM_RATE_36MB:
171 return B43_OFDM_RATE_24MB;
172 case B43_OFDM_RATE_48MB:
173 return B43_OFDM_RATE_36MB;
174 case B43_OFDM_RATE_54MB:
175 return B43_OFDM_RATE_48MB;
181 /* Generate a TX data header. */
182 int b43_generate_txhdr(struct b43_wldev *dev,
184 const unsigned char *fragment_data,
185 unsigned int fragment_len,
186 struct ieee80211_tx_info *info,
189 struct b43_txhdr *txhdr = (struct b43_txhdr *)_txhdr;
190 const struct b43_phy *phy = &dev->phy;
191 const struct ieee80211_hdr *wlhdr =
192 (const struct ieee80211_hdr *)fragment_data;
193 int use_encryption = !!info->control.hw_key;
194 __le16 fctl = wlhdr->frame_control;
195 struct ieee80211_rate *fbrate;
197 int rate_ofdm, rate_fb_ofdm;
198 unsigned int plcp_fragment_len;
202 struct ieee80211_rate *txrate;
203 struct ieee80211_tx_rate *rates;
205 memset(txhdr, 0, sizeof(*txhdr));
207 txrate = ieee80211_get_tx_rate(dev->wl->hw, info);
208 rate = txrate ? txrate->hw_value : B43_CCK_RATE_1MB;
209 rate_ofdm = b43_is_ofdm_rate(rate);
210 fbrate = ieee80211_get_alt_retry_rate(dev->wl->hw, info, 0) ? : txrate;
211 rate_fb = fbrate->hw_value;
212 rate_fb_ofdm = b43_is_ofdm_rate(rate_fb);
215 txhdr->phy_rate = b43_plcp_get_ratecode_ofdm(rate);
217 txhdr->phy_rate = b43_plcp_get_ratecode_cck(rate);
218 txhdr->mac_frame_ctl = wlhdr->frame_control;
219 memcpy(txhdr->tx_receiver, wlhdr->addr1, 6);
221 /* Calculate duration for fallback rate */
222 if ((rate_fb == rate) ||
223 (wlhdr->duration_id & cpu_to_le16(0x8000)) ||
224 (wlhdr->duration_id == cpu_to_le16(0))) {
225 /* If the fallback rate equals the normal rate or the
226 * dur_id field contains an AID, CFP magic or 0,
227 * use the original dur_id field. */
228 txhdr->dur_fb = wlhdr->duration_id;
230 txhdr->dur_fb = ieee80211_generic_frame_duration(
231 dev->wl->hw, info->control.vif, fragment_len, fbrate);
234 plcp_fragment_len = fragment_len + FCS_LEN;
235 if (use_encryption) {
236 u8 key_idx = info->control.hw_key->hw_key_idx;
241 B43_WARN_ON(key_idx >= dev->max_nr_keys);
242 key = &(dev->key[key_idx]);
244 if (unlikely(!key->keyconf)) {
245 /* This key is invalid. This might only happen
246 * in a short timeframe after machine resume before
247 * we were able to reconfigure keys.
248 * Drop this packet completely. Do not transmit it
249 * unencrypted to avoid leaking information. */
253 /* Hardware appends ICV. */
254 plcp_fragment_len += info->control.hw_key->icv_len;
256 key_idx = b43_kidx_to_fw(dev, key_idx);
257 mac_ctl |= (key_idx << B43_TXH_MAC_KEYIDX_SHIFT) &
259 mac_ctl |= (key->algorithm << B43_TXH_MAC_KEYALG_SHIFT) &
261 wlhdr_len = ieee80211_hdrlen(fctl);
262 iv_len = min((size_t) info->control.hw_key->iv_len,
263 ARRAY_SIZE(txhdr->iv));
264 memcpy(txhdr->iv, ((u8 *) wlhdr) + wlhdr_len, iv_len);
266 if (b43_is_old_txhdr_format(dev)) {
267 b43_generate_plcp_hdr((struct b43_plcp_hdr4 *)(&txhdr->old_format.plcp),
268 plcp_fragment_len, rate);
270 b43_generate_plcp_hdr((struct b43_plcp_hdr4 *)(&txhdr->new_format.plcp),
271 plcp_fragment_len, rate);
273 b43_generate_plcp_hdr((struct b43_plcp_hdr4 *)(&txhdr->plcp_fb),
274 plcp_fragment_len, rate_fb);
276 /* Extra Frame Types */
278 extra_ft |= B43_TXH_EFT_FB_OFDM;
280 extra_ft |= B43_TXH_EFT_FB_CCK;
282 /* Set channel radio code. Note that the micrcode ORs 0x100 to
283 * this value before comparing it to the value in SHM, if this
286 txhdr->chan_radio_code = phy->channel;
288 /* PHY TX Control word */
290 phy_ctl |= B43_TXH_PHY_ENC_OFDM;
292 phy_ctl |= B43_TXH_PHY_ENC_CCK;
293 if (info->control.rates[0].flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
294 phy_ctl |= B43_TXH_PHY_SHORTPRMBL;
296 switch (b43_ieee80211_antenna_sanitize(dev, info->antenna_sel_tx)) {
297 case 0: /* Default */
298 phy_ctl |= B43_TXH_PHY_ANT01AUTO;
300 case 1: /* Antenna 0 */
301 phy_ctl |= B43_TXH_PHY_ANT0;
303 case 2: /* Antenna 1 */
304 phy_ctl |= B43_TXH_PHY_ANT1;
306 case 3: /* Antenna 2 */
307 phy_ctl |= B43_TXH_PHY_ANT2;
309 case 4: /* Antenna 3 */
310 phy_ctl |= B43_TXH_PHY_ANT3;
316 rates = info->control.rates;
318 if (!(info->flags & IEEE80211_TX_CTL_NO_ACK))
319 mac_ctl |= B43_TXH_MAC_ACK;
320 /* use hardware sequence counter as the non-TID counter */
321 if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ)
322 mac_ctl |= B43_TXH_MAC_HWSEQ;
323 if (info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT)
324 mac_ctl |= B43_TXH_MAC_STMSDU;
325 if (phy->type == B43_PHYTYPE_A)
326 mac_ctl |= B43_TXH_MAC_5GHZ;
328 /* Overwrite rates[0].count to make the retry calculation
329 * in the tx status easier. need the actual retry limit to
330 * detect whether the fallback rate was used.
332 if ((rates[0].flags & IEEE80211_TX_RC_USE_RTS_CTS) ||
333 (rates[0].count <= dev->wl->hw->conf.long_frame_max_tx_count)) {
334 rates[0].count = dev->wl->hw->conf.long_frame_max_tx_count;
335 mac_ctl |= B43_TXH_MAC_LONGFRAME;
337 rates[0].count = dev->wl->hw->conf.short_frame_max_tx_count;
340 /* Generate the RTS or CTS-to-self frame */
341 if ((rates[0].flags & IEEE80211_TX_RC_USE_RTS_CTS) ||
342 (rates[0].flags & IEEE80211_TX_RC_USE_CTS_PROTECT)) {
344 struct ieee80211_hdr *hdr;
345 int rts_rate, rts_rate_fb;
346 int rts_rate_ofdm, rts_rate_fb_ofdm;
347 struct b43_plcp_hdr6 *plcp;
348 struct ieee80211_rate *rts_cts_rate;
350 rts_cts_rate = ieee80211_get_rts_cts_rate(dev->wl->hw, info);
352 rts_rate = rts_cts_rate ? rts_cts_rate->hw_value : B43_CCK_RATE_1MB;
353 rts_rate_ofdm = b43_is_ofdm_rate(rts_rate);
354 rts_rate_fb = b43_calc_fallback_rate(rts_rate);
355 rts_rate_fb_ofdm = b43_is_ofdm_rate(rts_rate_fb);
357 if (rates[0].flags & IEEE80211_TX_RC_USE_CTS_PROTECT) {
358 struct ieee80211_cts *cts;
360 if (b43_is_old_txhdr_format(dev)) {
361 cts = (struct ieee80211_cts *)
362 (txhdr->old_format.rts_frame);
364 cts = (struct ieee80211_cts *)
365 (txhdr->new_format.rts_frame);
367 ieee80211_ctstoself_get(dev->wl->hw, info->control.vif,
368 fragment_data, fragment_len,
370 mac_ctl |= B43_TXH_MAC_SENDCTS;
371 len = sizeof(struct ieee80211_cts);
373 struct ieee80211_rts *rts;
375 if (b43_is_old_txhdr_format(dev)) {
376 rts = (struct ieee80211_rts *)
377 (txhdr->old_format.rts_frame);
379 rts = (struct ieee80211_rts *)
380 (txhdr->new_format.rts_frame);
382 ieee80211_rts_get(dev->wl->hw, info->control.vif,
383 fragment_data, fragment_len,
385 mac_ctl |= B43_TXH_MAC_SENDRTS;
386 len = sizeof(struct ieee80211_rts);
390 /* Generate the PLCP headers for the RTS/CTS frame */
391 if (b43_is_old_txhdr_format(dev))
392 plcp = &txhdr->old_format.rts_plcp;
394 plcp = &txhdr->new_format.rts_plcp;
395 b43_generate_plcp_hdr((struct b43_plcp_hdr4 *)plcp,
397 plcp = &txhdr->rts_plcp_fb;
398 b43_generate_plcp_hdr((struct b43_plcp_hdr4 *)plcp,
401 if (b43_is_old_txhdr_format(dev)) {
402 hdr = (struct ieee80211_hdr *)
403 (&txhdr->old_format.rts_frame);
405 hdr = (struct ieee80211_hdr *)
406 (&txhdr->new_format.rts_frame);
408 txhdr->rts_dur_fb = hdr->duration_id;
411 extra_ft |= B43_TXH_EFT_RTS_OFDM;
412 txhdr->phy_rate_rts =
413 b43_plcp_get_ratecode_ofdm(rts_rate);
415 extra_ft |= B43_TXH_EFT_RTS_CCK;
416 txhdr->phy_rate_rts =
417 b43_plcp_get_ratecode_cck(rts_rate);
419 if (rts_rate_fb_ofdm)
420 extra_ft |= B43_TXH_EFT_RTSFB_OFDM;
422 extra_ft |= B43_TXH_EFT_RTSFB_CCK;
426 if (b43_is_old_txhdr_format(dev))
427 txhdr->old_format.cookie = cpu_to_le16(cookie);
429 txhdr->new_format.cookie = cpu_to_le16(cookie);
431 /* Apply the bitfields */
432 txhdr->mac_ctl = cpu_to_le32(mac_ctl);
433 txhdr->phy_ctl = cpu_to_le16(phy_ctl);
434 txhdr->extra_ft = extra_ft;
439 static s8 b43_rssi_postprocess(struct b43_wldev *dev,
440 u8 in_rssi, int ofdm,
441 int adjust_2053, int adjust_2050)
443 struct b43_phy *phy = &dev->phy;
444 struct b43_phy_g *gphy = phy->g;
447 switch (phy->radio_ver) {
460 if (dev->dev->bus->sprom.
461 boardflags_lo & B43_BFL_RSSI) {
464 B43_WARN_ON(phy->type != B43_PHYTYPE_G);
465 tmp = gphy->nrssi_lt[in_rssi];
477 if (phy->type == B43_PHYTYPE_G && adjust_2050)
503 static s8 b43_rssinoise_postprocess(struct b43_wldev *dev, u8 in_rssi)
505 struct b43_phy *phy = &dev->phy;
508 if (phy->type == B43_PHYTYPE_A) {
509 //TODO: Incomplete specs.
512 ret = b43_rssi_postprocess(dev, in_rssi, 0, 1, 1);
518 void b43_rx(struct b43_wldev *dev, struct sk_buff *skb, const void *_rxhdr)
520 struct ieee80211_rx_status status;
521 struct b43_plcp_hdr6 *plcp;
522 struct ieee80211_hdr *wlhdr;
523 const struct b43_rxhdr_fw4 *rxhdr = _rxhdr;
525 u16 phystat0, phystat3, chanstat, mactime;
531 memset(&status, 0, sizeof(status));
533 /* Get metadata about the frame from the header. */
534 phystat0 = le16_to_cpu(rxhdr->phy_status0);
535 phystat3 = le16_to_cpu(rxhdr->phy_status3);
536 macstat = le32_to_cpu(rxhdr->mac_status);
537 mactime = le16_to_cpu(rxhdr->mac_time);
538 chanstat = le16_to_cpu(rxhdr->channel);
539 phytype = chanstat & B43_RX_CHAN_PHYTYPE;
541 if (macstat & B43_RX_MAC_FCSERR)
542 dev->wl->ieee_stats.dot11FCSErrorCount++;
543 if (macstat & B43_RX_MAC_DECERR) {
544 /* Decryption with the given key failed.
545 * Drop the packet. We also won't be able to decrypt it with
546 * the key in software. */
550 /* Skip PLCP and padding */
551 padding = (macstat & B43_RX_MAC_PADDING) ? 2 : 0;
552 if (unlikely(skb->len < (sizeof(struct b43_plcp_hdr6) + padding))) {
553 b43dbg(dev->wl, "RX: Packet size underrun (1)\n");
556 plcp = (struct b43_plcp_hdr6 *)(skb->data + padding);
557 skb_pull(skb, sizeof(struct b43_plcp_hdr6) + padding);
558 /* The skb contains the Wireless Header + payload data now */
559 if (unlikely(skb->len < (2 + 2 + 6 /*minimum hdr */ + FCS_LEN))) {
560 b43dbg(dev->wl, "RX: Packet size underrun (2)\n");
563 wlhdr = (struct ieee80211_hdr *)(skb->data);
564 fctl = wlhdr->frame_control;
566 if (macstat & B43_RX_MAC_DEC) {
570 keyidx = ((macstat & B43_RX_MAC_KEYIDX)
571 >> B43_RX_MAC_KEYIDX_SHIFT);
572 /* We must adjust the key index here. We want the "physical"
573 * key index, but the ucode passed it slightly different.
575 keyidx = b43_kidx_to_raw(dev, keyidx);
576 B43_WARN_ON(keyidx >= dev->max_nr_keys);
578 if (dev->key[keyidx].algorithm != B43_SEC_ALGO_NONE) {
579 wlhdr_len = ieee80211_hdrlen(fctl);
580 if (unlikely(skb->len < (wlhdr_len + 3))) {
582 "RX: Packet size underrun (3)\n");
585 status.flag |= RX_FLAG_DECRYPTED;
589 /* Link quality statistics */
590 status.noise = dev->stats.link_noise;
591 if ((chanstat & B43_RX_CHAN_PHYTYPE) == B43_PHYTYPE_N) {
592 // s8 rssi = max(rxhdr->power0, rxhdr->power1);
593 //TODO: Find out what the rssi value is (dBm or percentage?)
594 // and also find out what the maximum possible value is.
595 // Fill status.ssi and status.signal fields.
597 status.signal = b43_rssi_postprocess(dev, rxhdr->jssi,
598 (phystat0 & B43_RX_PHYST0_OFDM),
599 (phystat0 & B43_RX_PHYST0_GAINCTL),
600 (phystat3 & B43_RX_PHYST3_TRSTATE));
601 status.qual = (rxhdr->jssi * 100) / B43_RX_MAX_SSI;
604 if (phystat0 & B43_RX_PHYST0_OFDM)
605 status.rate_idx = b43_plcp_get_bitrate_idx_ofdm(plcp,
606 phytype == B43_PHYTYPE_A);
608 status.rate_idx = b43_plcp_get_bitrate_idx_cck(plcp);
609 if (unlikely(status.rate_idx == -1))
611 status.antenna = !!(phystat0 & B43_RX_PHYST0_ANT);
614 * All frames on monitor interfaces and beacons always need a full
615 * 64-bit timestamp. Monitor interfaces need it for diagnostic
616 * purposes and beacons for IBSS merging.
617 * This code assumes we get to process the packet within 16 bits
618 * of timestamp, i.e. about 65 milliseconds after the PHY received
621 if (ieee80211_is_beacon(fctl) || dev->wl->radiotap_enabled) {
624 b43_tsf_read(dev, &status.mactime);
625 low_mactime_now = status.mactime;
626 status.mactime = status.mactime & ~0xFFFFULL;
627 status.mactime += mactime;
628 if (low_mactime_now <= mactime)
629 status.mactime -= 0x10000;
630 status.flag |= RX_FLAG_TSFT;
633 chanid = (chanstat & B43_RX_CHAN_ID) >> B43_RX_CHAN_ID_SHIFT;
634 switch (chanstat & B43_RX_CHAN_PHYTYPE) {
636 status.band = IEEE80211_BAND_5GHZ;
638 /* FIXME: We don't really know which value the "chanid" contains.
639 * So the following assignment might be wrong. */
640 status.freq = b43_channel_to_freq_5ghz(chanid);
643 status.band = IEEE80211_BAND_2GHZ;
644 /* chanid is the radio channel cookie value as used
645 * to tune the radio. */
646 status.freq = chanid + 2400;
649 /* chanid is the SHM channel cookie. Which is the plain
650 * channel number in b43. */
651 if (chanstat & B43_RX_CHAN_5GHZ) {
652 status.band = IEEE80211_BAND_5GHZ;
653 status.freq = b43_freq_to_channel_5ghz(chanid);
655 status.band = IEEE80211_BAND_2GHZ;
656 status.freq = b43_freq_to_channel_2ghz(chanid);
664 dev->stats.last_rx = jiffies;
665 ieee80211_rx_irqsafe(dev->wl->hw, skb, &status);
669 b43dbg(dev->wl, "RX: Packet dropped\n");
670 dev_kfree_skb_any(skb);
673 void b43_handle_txstatus(struct b43_wldev *dev,
674 const struct b43_txstatus *status)
676 b43_debugfs_log_txstat(dev, status);
678 if (status->intermediate)
680 if (status->for_ampdu)
683 dev->wl->ieee_stats.dot11ACKFailureCount++;
684 if (status->rts_count) {
685 if (status->rts_count == 0xF) //FIXME
686 dev->wl->ieee_stats.dot11RTSFailureCount++;
688 dev->wl->ieee_stats.dot11RTSSuccessCount++;
691 if (b43_using_pio_transfers(dev))
692 b43_pio_handle_txstatus(dev, status);
694 b43_dma_handle_txstatus(dev, status);
696 b43_phy_txpower_check(dev, 0);
699 /* Fill out the mac80211 TXstatus report based on the b43-specific
700 * txstatus report data. This returns a boolean whether the frame was
701 * successfully transmitted. */
702 bool b43_fill_txstatus_report(struct b43_wldev *dev,
703 struct ieee80211_tx_info *report,
704 const struct b43_txstatus *status)
706 bool frame_success = 1;
709 /* preserve the confiured retry limit before clearing the status
710 * The xmit function has overwritten the rc's value with the actual
711 * retry limit done by the hardware */
712 retry_limit = report->status.rates[0].count;
713 ieee80211_tx_info_clear_status(report);
716 /* The frame was ACKed. */
717 report->flags |= IEEE80211_TX_STAT_ACK;
719 /* The frame was not ACKed... */
720 if (!(report->flags & IEEE80211_TX_CTL_NO_ACK)) {
721 /* ...but we expected an ACK. */
725 if (status->frame_count == 0) {
726 /* The frame was not transmitted at all. */
727 report->status.rates[0].count = 0;
728 } else if (status->rts_count > dev->wl->hw->conf.short_frame_max_tx_count) {
730 * If the short retries (RTS, not data frame) have exceeded
731 * the limit, the hw will not have tried the selected rate,
732 * but will have used the fallback rate instead.
733 * Don't let the rate control count attempts for the selected
734 * rate in this case, otherwise the statistics will be off.
736 report->status.rates[0].count = 0;
737 report->status.rates[1].count = status->frame_count;
739 if (status->frame_count > retry_limit) {
740 report->status.rates[0].count = retry_limit;
741 report->status.rates[1].count = status->frame_count -
745 report->status.rates[0].count = status->frame_count;
746 report->status.rates[1].idx = -1;
750 return frame_success;
753 /* Stop any TX operation on the device (suspend the hardware queues) */
754 void b43_tx_suspend(struct b43_wldev *dev)
756 if (b43_using_pio_transfers(dev))
757 b43_pio_tx_suspend(dev);
759 b43_dma_tx_suspend(dev);
762 /* Resume any TX operation on the device (resume the hardware queues) */
763 void b43_tx_resume(struct b43_wldev *dev)
765 if (b43_using_pio_transfers(dev))
766 b43_pio_tx_resume(dev);
768 b43_dma_tx_resume(dev);