3 Broadcom B43legacy 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 <m@bues.ch>
10 Copyright (C) 2005 Danny van Dyk <kugelfang@gentoo.org>
11 Copyright (C) 2005 Andreas Jaggi <andreas.jaggi@waterwave.ch>
12 Copyright (C) 2007 Larry Finger <Larry.Finger@lwfinger.net>
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 2 of the License, or
17 (at your option) any later version.
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with this program; see the file COPYING. If not, write to
26 the Free Software Foundation, Inc., 51 Franklin Steet, Fifth Floor,
27 Boston, MA 02110-1301, USA.
39 /* Extract the bitrate out of a CCK PLCP header. */
40 static u8 b43legacy_plcp_get_bitrate_idx_cck(struct b43legacy_plcp_hdr6 *plcp)
42 switch (plcp->raw[0]) {
56 /* Extract the bitrate out of an OFDM PLCP header. */
57 static u8 b43legacy_plcp_get_bitrate_idx_ofdm(struct b43legacy_plcp_hdr6 *plcp,
60 int base = aphy ? 0 : 4;
62 switch (plcp->raw[0] & 0xF) {
84 u8 b43legacy_plcp_get_ratecode_cck(const u8 bitrate)
87 case B43legacy_CCK_RATE_1MB:
89 case B43legacy_CCK_RATE_2MB:
91 case B43legacy_CCK_RATE_5MB:
93 case B43legacy_CCK_RATE_11MB:
100 u8 b43legacy_plcp_get_ratecode_ofdm(const u8 bitrate)
103 case B43legacy_OFDM_RATE_6MB:
105 case B43legacy_OFDM_RATE_9MB:
107 case B43legacy_OFDM_RATE_12MB:
109 case B43legacy_OFDM_RATE_18MB:
111 case B43legacy_OFDM_RATE_24MB:
113 case B43legacy_OFDM_RATE_36MB:
115 case B43legacy_OFDM_RATE_48MB:
117 case B43legacy_OFDM_RATE_54MB:
124 void b43legacy_generate_plcp_hdr(struct b43legacy_plcp_hdr4 *plcp,
125 const u16 octets, const u8 bitrate)
127 __le32 *data = &(plcp->data);
128 __u8 *raw = plcp->raw;
130 if (b43legacy_is_ofdm_rate(bitrate)) {
133 d = b43legacy_plcp_get_ratecode_ofdm(bitrate);
134 B43legacy_WARN_ON(octets & 0xF000);
136 *data = cpu_to_le32(d);
140 plen = octets * 16 / bitrate;
141 if ((octets * 16 % bitrate) > 0) {
143 if ((bitrate == B43legacy_CCK_RATE_11MB)
144 && ((octets * 8 % 11) < 4))
150 *data |= cpu_to_le32(plen << 16);
151 raw[0] = b43legacy_plcp_get_ratecode_cck(bitrate);
155 static u8 b43legacy_calc_fallback_rate(u8 bitrate)
158 case B43legacy_CCK_RATE_1MB:
159 return B43legacy_CCK_RATE_1MB;
160 case B43legacy_CCK_RATE_2MB:
161 return B43legacy_CCK_RATE_1MB;
162 case B43legacy_CCK_RATE_5MB:
163 return B43legacy_CCK_RATE_2MB;
164 case B43legacy_CCK_RATE_11MB:
165 return B43legacy_CCK_RATE_5MB;
166 case B43legacy_OFDM_RATE_6MB:
167 return B43legacy_CCK_RATE_5MB;
168 case B43legacy_OFDM_RATE_9MB:
169 return B43legacy_OFDM_RATE_6MB;
170 case B43legacy_OFDM_RATE_12MB:
171 return B43legacy_OFDM_RATE_9MB;
172 case B43legacy_OFDM_RATE_18MB:
173 return B43legacy_OFDM_RATE_12MB;
174 case B43legacy_OFDM_RATE_24MB:
175 return B43legacy_OFDM_RATE_18MB;
176 case B43legacy_OFDM_RATE_36MB:
177 return B43legacy_OFDM_RATE_24MB;
178 case B43legacy_OFDM_RATE_48MB:
179 return B43legacy_OFDM_RATE_36MB;
180 case B43legacy_OFDM_RATE_54MB:
181 return B43legacy_OFDM_RATE_48MB;
187 static int generate_txhdr_fw3(struct b43legacy_wldev *dev,
188 struct b43legacy_txhdr_fw3 *txhdr,
189 const unsigned char *fragment_data,
190 unsigned int fragment_len,
191 struct ieee80211_tx_info *info,
194 const struct ieee80211_hdr *wlhdr;
195 int use_encryption = !!info->control.hw_key;
197 struct ieee80211_rate *rate_fb;
200 unsigned int plcp_fragment_len;
203 struct ieee80211_rate *tx_rate;
204 struct ieee80211_tx_rate *rates;
206 wlhdr = (const struct ieee80211_hdr *)fragment_data;
208 memset(txhdr, 0, sizeof(*txhdr));
210 tx_rate = ieee80211_get_tx_rate(dev->wl->hw, info);
212 rate = tx_rate->hw_value;
213 rate_ofdm = b43legacy_is_ofdm_rate(rate);
214 rate_fb = ieee80211_get_alt_retry_rate(dev->wl->hw, info, 0) ? : tx_rate;
215 rate_fb_ofdm = b43legacy_is_ofdm_rate(rate_fb->hw_value);
217 txhdr->mac_frame_ctl = wlhdr->frame_control;
218 memcpy(txhdr->tx_receiver, wlhdr->addr1, ETH_ALEN);
220 /* Calculate duration for fallback rate */
221 if ((rate_fb->hw_value == rate) ||
222 (wlhdr->duration_id & cpu_to_le16(0x8000)) ||
223 (wlhdr->duration_id == cpu_to_le16(0))) {
224 /* If the fallback rate equals the normal rate or the
225 * dur_id field contains an AID, CFP magic or 0,
226 * use the original dur_id field. */
227 txhdr->dur_fb = wlhdr->duration_id;
229 txhdr->dur_fb = ieee80211_generic_frame_duration(dev->wl->hw,
236 plcp_fragment_len = fragment_len + FCS_LEN;
237 if (use_encryption) {
238 u8 key_idx = info->control.hw_key->hw_key_idx;
239 struct b43legacy_key *key;
243 B43legacy_WARN_ON(key_idx >= dev->max_nr_keys);
244 key = &(dev->key[key_idx]);
247 /* Hardware appends ICV. */
248 plcp_fragment_len += info->control.hw_key->icv_len;
250 key_idx = b43legacy_kidx_to_fw(dev, key_idx);
251 mac_ctl |= (key_idx << B43legacy_TX4_MAC_KEYIDX_SHIFT) &
252 B43legacy_TX4_MAC_KEYIDX;
253 mac_ctl |= (key->algorithm <<
254 B43legacy_TX4_MAC_KEYALG_SHIFT) &
255 B43legacy_TX4_MAC_KEYALG;
256 wlhdr_len = ieee80211_hdrlen(wlhdr->frame_control);
257 iv_len = min_t(size_t, info->control.hw_key->iv_len,
258 ARRAY_SIZE(txhdr->iv));
259 memcpy(txhdr->iv, ((u8 *)wlhdr) + wlhdr_len, iv_len);
261 /* This key is invalid. This might only happen
262 * in a short timeframe after machine resume before
263 * we were able to reconfigure keys.
264 * Drop this packet completely. Do not transmit it
265 * unencrypted to avoid leaking information. */
269 b43legacy_generate_plcp_hdr((struct b43legacy_plcp_hdr4 *)
270 (&txhdr->plcp), plcp_fragment_len,
272 b43legacy_generate_plcp_hdr(&txhdr->plcp_fb, plcp_fragment_len,
275 /* PHY TX Control word */
277 phy_ctl |= B43legacy_TX4_PHY_ENC_OFDM;
278 if (info->control.rates[0].flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
279 phy_ctl |= B43legacy_TX4_PHY_SHORTPRMBL;
280 phy_ctl |= B43legacy_TX4_PHY_ANTLAST;
283 rates = info->control.rates;
284 if (!(info->flags & IEEE80211_TX_CTL_NO_ACK))
285 mac_ctl |= B43legacy_TX4_MAC_ACK;
286 if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ)
287 mac_ctl |= B43legacy_TX4_MAC_HWSEQ;
288 if (info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT)
289 mac_ctl |= B43legacy_TX4_MAC_STMSDU;
291 mac_ctl |= B43legacy_TX4_MAC_FALLBACKOFDM;
293 /* Overwrite rates[0].count to make the retry calculation
294 * in the tx status easier. need the actual retry limit to
295 * detect whether the fallback rate was used.
297 if ((rates[0].flags & IEEE80211_TX_RC_USE_RTS_CTS) ||
298 (rates[0].count <= dev->wl->hw->conf.long_frame_max_tx_count)) {
299 rates[0].count = dev->wl->hw->conf.long_frame_max_tx_count;
300 mac_ctl |= B43legacy_TX4_MAC_LONGFRAME;
302 rates[0].count = dev->wl->hw->conf.short_frame_max_tx_count;
305 /* Generate the RTS or CTS-to-self frame */
306 if ((rates[0].flags & IEEE80211_TX_RC_USE_RTS_CTS) ||
307 (rates[0].flags & IEEE80211_TX_RC_USE_CTS_PROTECT)) {
309 struct ieee80211_hdr *hdr;
312 int rts_rate_fb_ofdm;
314 rts_rate = ieee80211_get_rts_cts_rate(dev->wl->hw, info)->hw_value;
315 rts_rate_fb = b43legacy_calc_fallback_rate(rts_rate);
316 rts_rate_fb_ofdm = b43legacy_is_ofdm_rate(rts_rate_fb);
317 if (rts_rate_fb_ofdm)
318 mac_ctl |= B43legacy_TX4_MAC_CTSFALLBACKOFDM;
320 if (rates[0].flags & IEEE80211_TX_RC_USE_CTS_PROTECT) {
321 ieee80211_ctstoself_get(dev->wl->hw,
325 (struct ieee80211_cts *)
327 mac_ctl |= B43legacy_TX4_MAC_SENDCTS;
328 len = sizeof(struct ieee80211_cts);
330 ieee80211_rts_get(dev->wl->hw,
332 fragment_data, fragment_len, info,
333 (struct ieee80211_rts *)
335 mac_ctl |= B43legacy_TX4_MAC_SENDRTS;
336 len = sizeof(struct ieee80211_rts);
339 b43legacy_generate_plcp_hdr((struct b43legacy_plcp_hdr4 *)
342 b43legacy_generate_plcp_hdr(&txhdr->rts_plcp_fb,
344 hdr = (struct ieee80211_hdr *)(&txhdr->rts_frame);
345 txhdr->rts_dur_fb = hdr->duration_id;
349 txhdr->cookie = cpu_to_le16(cookie);
351 /* Apply the bitfields */
352 txhdr->mac_ctl = cpu_to_le32(mac_ctl);
353 txhdr->phy_ctl = cpu_to_le16(phy_ctl);
358 int b43legacy_generate_txhdr(struct b43legacy_wldev *dev,
360 const unsigned char *fragment_data,
361 unsigned int fragment_len,
362 struct ieee80211_tx_info *info,
365 return generate_txhdr_fw3(dev, (struct b43legacy_txhdr_fw3 *)txhdr,
366 fragment_data, fragment_len,
370 static s8 b43legacy_rssi_postprocess(struct b43legacy_wldev *dev,
371 u8 in_rssi, int ofdm,
372 int adjust_2053, int adjust_2050)
374 struct b43legacy_phy *phy = &dev->phy;
377 switch (phy->radio_ver) {
390 if (dev->dev->bus->sprom.boardflags_lo
391 & B43legacy_BFL_RSSI) {
394 tmp = phy->nrssi_lt[in_rssi];
406 if (phy->type == B43legacy_PHYTYPE_G &&
431 void b43legacy_rx(struct b43legacy_wldev *dev,
435 struct ieee80211_rx_status status;
436 struct b43legacy_plcp_hdr6 *plcp;
437 struct ieee80211_hdr *wlhdr;
438 const struct b43legacy_rxhdr_fw3 *rxhdr = _rxhdr;
449 memset(&status, 0, sizeof(status));
451 /* Get metadata about the frame from the header. */
452 phystat0 = le16_to_cpu(rxhdr->phy_status0);
453 phystat3 = le16_to_cpu(rxhdr->phy_status3);
455 macstat = le16_to_cpu(rxhdr->mac_status);
456 mactime = le16_to_cpu(rxhdr->mac_time);
457 chanstat = le16_to_cpu(rxhdr->channel);
459 if (macstat & B43legacy_RX_MAC_FCSERR)
460 dev->wl->ieee_stats.dot11FCSErrorCount++;
462 /* Skip PLCP and padding */
463 padding = (macstat & B43legacy_RX_MAC_PADDING) ? 2 : 0;
464 if (unlikely(skb->len < (sizeof(struct b43legacy_plcp_hdr6) +
466 b43legacydbg(dev->wl, "RX: Packet size underrun (1)\n");
469 plcp = (struct b43legacy_plcp_hdr6 *)(skb->data + padding);
470 skb_pull(skb, sizeof(struct b43legacy_plcp_hdr6) + padding);
471 /* The skb contains the Wireless Header + payload data now */
472 if (unlikely(skb->len < (2+2+6/*minimum hdr*/ + FCS_LEN))) {
473 b43legacydbg(dev->wl, "RX: Packet size underrun (2)\n");
476 wlhdr = (struct ieee80211_hdr *)(skb->data);
477 fctl = wlhdr->frame_control;
479 if ((macstat & B43legacy_RX_MAC_DEC) &&
480 !(macstat & B43legacy_RX_MAC_DECERR)) {
486 keyidx = ((macstat & B43legacy_RX_MAC_KEYIDX)
487 >> B43legacy_RX_MAC_KEYIDX_SHIFT);
488 /* We must adjust the key index here. We want the "physical"
489 * key index, but the ucode passed it slightly different.
491 keyidx = b43legacy_kidx_to_raw(dev, keyidx);
492 B43legacy_WARN_ON(keyidx >= dev->max_nr_keys);
494 if (dev->key[keyidx].algorithm != B43legacy_SEC_ALGO_NONE) {
495 /* Remove PROTECTED flag to mark it as decrypted. */
496 B43legacy_WARN_ON(!ieee80211_has_protected(fctl));
497 fctl &= ~cpu_to_le16(IEEE80211_FCTL_PROTECTED);
498 wlhdr->frame_control = fctl;
500 wlhdr_len = ieee80211_hdrlen(fctl);
501 if (unlikely(skb->len < (wlhdr_len + 3))) {
502 b43legacydbg(dev->wl, "RX: Packet size"
506 if (skb->data[wlhdr_len + 3] & (1 << 5)) {
507 /* The Ext-IV Bit is set in the "KeyID"
516 if (unlikely(skb->len < (wlhdr_len + iv_len +
518 b43legacydbg(dev->wl, "RX: Packet size"
523 memmove(skb->data + iv_len, skb->data, wlhdr_len);
524 skb_pull(skb, iv_len);
526 skb_trim(skb, skb->len - icv_len);
528 status.flag |= RX_FLAG_DECRYPTED;
532 status.signal = b43legacy_rssi_postprocess(dev, jssi,
533 (phystat0 & B43legacy_RX_PHYST0_OFDM),
534 (phystat0 & B43legacy_RX_PHYST0_GAINCTL),
535 (phystat3 & B43legacy_RX_PHYST3_TRSTATE));
536 /* change to support A PHY */
537 if (phystat0 & B43legacy_RX_PHYST0_OFDM)
538 status.rate_idx = b43legacy_plcp_get_bitrate_idx_ofdm(plcp, false);
540 status.rate_idx = b43legacy_plcp_get_bitrate_idx_cck(plcp);
541 status.antenna = !!(phystat0 & B43legacy_RX_PHYST0_ANT);
544 * All frames on monitor interfaces and beacons always need a full
545 * 64-bit timestamp. Monitor interfaces need it for diagnostic
546 * purposes and beacons for IBSS merging.
547 * This code assumes we get to process the packet within 16 bits
548 * of timestamp, i.e. about 65 milliseconds after the PHY received
551 if (ieee80211_is_beacon(fctl) || dev->wl->radiotap_enabled) {
554 b43legacy_tsf_read(dev, &status.mactime);
555 low_mactime_now = status.mactime;
556 status.mactime = status.mactime & ~0xFFFFULL;
557 status.mactime += mactime;
558 if (low_mactime_now <= mactime)
559 status.mactime -= 0x10000;
560 status.flag |= RX_FLAG_MACTIME_START;
563 chanid = (chanstat & B43legacy_RX_CHAN_ID) >>
564 B43legacy_RX_CHAN_ID_SHIFT;
565 switch (chanstat & B43legacy_RX_CHAN_PHYTYPE) {
566 case B43legacy_PHYTYPE_B:
567 case B43legacy_PHYTYPE_G:
568 status.band = NL80211_BAND_2GHZ;
569 status.freq = chanid + 2400;
572 b43legacywarn(dev->wl, "Unexpected value for chanstat (0x%X)\n",
577 memcpy(IEEE80211_SKB_RXCB(skb), &status, sizeof(status));
578 ieee80211_rx_irqsafe(dev->wl->hw, skb);
582 b43legacydbg(dev->wl, "RX: Packet dropped\n");
583 dev_kfree_skb_any(skb);
586 void b43legacy_handle_txstatus(struct b43legacy_wldev *dev,
587 const struct b43legacy_txstatus *status)
589 b43legacy_debugfs_log_txstat(dev, status);
591 if (status->intermediate)
593 if (status->for_ampdu)
596 dev->wl->ieee_stats.dot11ACKFailureCount++;
597 if (status->rts_count) {
598 if (status->rts_count == 0xF) /* FIXME */
599 dev->wl->ieee_stats.dot11RTSFailureCount++;
601 dev->wl->ieee_stats.dot11RTSSuccessCount++;
604 if (b43legacy_using_pio(dev))
605 b43legacy_pio_handle_txstatus(dev, status);
607 b43legacy_dma_handle_txstatus(dev, status);
610 /* Handle TX status report as received through DMA/PIO queues */
611 void b43legacy_handle_hwtxstatus(struct b43legacy_wldev *dev,
612 const struct b43legacy_hwtxstatus *hw)
614 struct b43legacy_txstatus status;
617 status.cookie = le16_to_cpu(hw->cookie);
618 status.seq = le16_to_cpu(hw->seq);
619 status.phy_stat = hw->phy_stat;
621 status.frame_count = (tmp >> 4);
622 status.rts_count = (tmp & 0x0F);
623 tmp = hw->flags << 1;
624 status.supp_reason = ((tmp & 0x1C) >> 2);
625 status.pm_indicated = !!(tmp & 0x80);
626 status.intermediate = !!(tmp & 0x40);
627 status.for_ampdu = !!(tmp & 0x20);
628 status.acked = !!(tmp & 0x02);
630 b43legacy_handle_txstatus(dev, &status);
633 /* Stop any TX operation on the device (suspend the hardware queues) */
634 void b43legacy_tx_suspend(struct b43legacy_wldev *dev)
636 if (b43legacy_using_pio(dev))
637 b43legacy_pio_freeze_txqueues(dev);
639 b43legacy_dma_tx_suspend(dev);
642 /* Resume any TX operation on the device (resume the hardware queues) */
643 void b43legacy_tx_resume(struct b43legacy_wldev *dev)
645 if (b43legacy_using_pio(dev))
646 b43legacy_pio_thaw_txqueues(dev);
648 b43legacy_dma_tx_resume(dev);
651 /* Initialize the QoS parameters */
652 void b43legacy_qos_init(struct b43legacy_wldev *dev)
654 /* FIXME: This function must probably be called from the mac80211
655 * config callback. */
658 b43legacy_hf_write(dev, b43legacy_hf_read(dev) | B43legacy_HF_EDCF);
659 /* FIXME kill magic */
660 b43legacy_write16(dev, 0x688,
661 b43legacy_read16(dev, 0x688) | 0x4);
664 /*TODO: We might need some stack support here to get the values. */