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 <mb@bu3sch.de>
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_cck(struct b43legacy_plcp_hdr6 *plcp)
42 switch (plcp->raw[0]) {
44 return B43legacy_CCK_RATE_1MB;
46 return B43legacy_CCK_RATE_2MB;
48 return B43legacy_CCK_RATE_5MB;
50 return B43legacy_CCK_RATE_11MB;
56 /* Extract the bitrate out of an OFDM PLCP header. */
57 static u8 b43legacy_plcp_get_bitrate_ofdm(struct b43legacy_plcp_hdr6 *plcp)
59 switch (plcp->raw[0] & 0xF) {
61 return B43legacy_OFDM_RATE_6MB;
63 return B43legacy_OFDM_RATE_9MB;
65 return B43legacy_OFDM_RATE_12MB;
67 return B43legacy_OFDM_RATE_18MB;
69 return B43legacy_OFDM_RATE_24MB;
71 return B43legacy_OFDM_RATE_36MB;
73 return B43legacy_OFDM_RATE_48MB;
75 return B43legacy_OFDM_RATE_54MB;
81 u8 b43legacy_plcp_get_ratecode_cck(const u8 bitrate)
84 case B43legacy_CCK_RATE_1MB:
86 case B43legacy_CCK_RATE_2MB:
88 case B43legacy_CCK_RATE_5MB:
90 case B43legacy_CCK_RATE_11MB:
97 u8 b43legacy_plcp_get_ratecode_ofdm(const u8 bitrate)
100 case B43legacy_OFDM_RATE_6MB:
102 case B43legacy_OFDM_RATE_9MB:
104 case B43legacy_OFDM_RATE_12MB:
106 case B43legacy_OFDM_RATE_18MB:
108 case B43legacy_OFDM_RATE_24MB:
110 case B43legacy_OFDM_RATE_36MB:
112 case B43legacy_OFDM_RATE_48MB:
114 case B43legacy_OFDM_RATE_54MB:
121 void b43legacy_generate_plcp_hdr(struct b43legacy_plcp_hdr4 *plcp,
122 const u16 octets, const u8 bitrate)
124 __le32 *data = &(plcp->data);
125 __u8 *raw = plcp->raw;
127 if (b43legacy_is_ofdm_rate(bitrate)) {
130 d = b43legacy_plcp_get_ratecode_ofdm(bitrate);
131 B43legacy_WARN_ON(octets & 0xF000);
133 *data = cpu_to_le32(d);
137 plen = octets * 16 / bitrate;
138 if ((octets * 16 % bitrate) > 0) {
140 if ((bitrate == B43legacy_CCK_RATE_11MB)
141 && ((octets * 8 % 11) < 4))
147 *data |= cpu_to_le32(plen << 16);
148 raw[0] = b43legacy_plcp_get_ratecode_cck(bitrate);
152 static u8 b43legacy_calc_fallback_rate(u8 bitrate)
155 case B43legacy_CCK_RATE_1MB:
156 return B43legacy_CCK_RATE_1MB;
157 case B43legacy_CCK_RATE_2MB:
158 return B43legacy_CCK_RATE_1MB;
159 case B43legacy_CCK_RATE_5MB:
160 return B43legacy_CCK_RATE_2MB;
161 case B43legacy_CCK_RATE_11MB:
162 return B43legacy_CCK_RATE_5MB;
163 case B43legacy_OFDM_RATE_6MB:
164 return B43legacy_CCK_RATE_5MB;
165 case B43legacy_OFDM_RATE_9MB:
166 return B43legacy_OFDM_RATE_6MB;
167 case B43legacy_OFDM_RATE_12MB:
168 return B43legacy_OFDM_RATE_9MB;
169 case B43legacy_OFDM_RATE_18MB:
170 return B43legacy_OFDM_RATE_12MB;
171 case B43legacy_OFDM_RATE_24MB:
172 return B43legacy_OFDM_RATE_18MB;
173 case B43legacy_OFDM_RATE_36MB:
174 return B43legacy_OFDM_RATE_24MB;
175 case B43legacy_OFDM_RATE_48MB:
176 return B43legacy_OFDM_RATE_36MB;
177 case B43legacy_OFDM_RATE_54MB:
178 return B43legacy_OFDM_RATE_48MB;
184 static int generate_txhdr_fw3(struct b43legacy_wldev *dev,
185 struct b43legacy_txhdr_fw3 *txhdr,
186 const unsigned char *fragment_data,
187 unsigned int fragment_len,
188 const struct ieee80211_tx_control *txctl,
191 const struct ieee80211_hdr *wlhdr;
192 int use_encryption = (!(txctl->flags & IEEE80211_TXCTL_DO_NOT_ENCRYPT));
198 unsigned int plcp_fragment_len;
202 wlhdr = (const struct ieee80211_hdr *)fragment_data;
203 fctl = le16_to_cpu(wlhdr->frame_control);
205 memset(txhdr, 0, sizeof(*txhdr));
207 rate = txctl->tx_rate;
208 rate_ofdm = b43legacy_is_ofdm_rate(rate);
209 rate_fb = (txctl->alt_retry_rate == -1) ? rate : txctl->alt_retry_rate;
210 rate_fb_ofdm = b43legacy_is_ofdm_rate(rate_fb);
212 txhdr->mac_frame_ctl = wlhdr->frame_control;
213 memcpy(txhdr->tx_receiver, wlhdr->addr1, 6);
215 /* Calculate duration for fallback rate */
216 if ((rate_fb == rate) ||
217 (wlhdr->duration_id & cpu_to_le16(0x8000)) ||
218 (wlhdr->duration_id == cpu_to_le16(0))) {
219 /* If the fallback rate equals the normal rate or the
220 * dur_id field contains an AID, CFP magic or 0,
221 * use the original dur_id field. */
222 txhdr->dur_fb = wlhdr->duration_id;
224 int fbrate_base100kbps = B43legacy_RATE_TO_100KBPS(rate_fb);
225 txhdr->dur_fb = ieee80211_generic_frame_duration(dev->wl->hw,
231 plcp_fragment_len = fragment_len + FCS_LEN;
232 if (use_encryption) {
233 u8 key_idx = (u16)(txctl->key_idx);
234 struct b43legacy_key *key;
238 B43legacy_WARN_ON(key_idx >= dev->max_nr_keys);
239 key = &(dev->key[key_idx]);
242 /* Hardware appends ICV. */
243 plcp_fragment_len += txctl->icv_len;
245 key_idx = b43legacy_kidx_to_fw(dev, key_idx);
246 mac_ctl |= (key_idx << B43legacy_TX4_MAC_KEYIDX_SHIFT) &
247 B43legacy_TX4_MAC_KEYIDX;
248 mac_ctl |= (key->algorithm <<
249 B43legacy_TX4_MAC_KEYALG_SHIFT) &
250 B43legacy_TX4_MAC_KEYALG;
251 wlhdr_len = ieee80211_get_hdrlen(fctl);
252 iv_len = min((size_t)txctl->iv_len,
253 ARRAY_SIZE(txhdr->iv));
254 memcpy(txhdr->iv, ((u8 *)wlhdr) + wlhdr_len, iv_len);
256 /* This key is invalid. This might only happen
257 * in a short timeframe after machine resume before
258 * we were able to reconfigure keys.
259 * Drop this packet completely. Do not transmit it
260 * unencrypted to avoid leaking information. */
264 b43legacy_generate_plcp_hdr((struct b43legacy_plcp_hdr4 *)
265 (&txhdr->plcp), plcp_fragment_len,
267 b43legacy_generate_plcp_hdr((struct b43legacy_plcp_hdr4 *)
268 (&txhdr->plcp_fb), plcp_fragment_len,
271 /* PHY TX Control word */
273 phy_ctl |= B43legacy_TX4_PHY_OFDM;
274 if (dev->short_preamble)
275 phy_ctl |= B43legacy_TX4_PHY_SHORTPRMBL;
276 switch (txctl->antenna_sel_tx) {
278 phy_ctl |= B43legacy_TX4_PHY_ANTLAST;
281 phy_ctl |= B43legacy_TX4_PHY_ANT0;
284 phy_ctl |= B43legacy_TX4_PHY_ANT1;
291 if (!(txctl->flags & IEEE80211_TXCTL_NO_ACK))
292 mac_ctl |= B43legacy_TX4_MAC_ACK;
293 if (!(((fctl & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_CTL) &&
294 ((fctl & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_PSPOLL)))
295 mac_ctl |= B43legacy_TX4_MAC_HWSEQ;
296 if (txctl->flags & IEEE80211_TXCTL_FIRST_FRAGMENT)
297 mac_ctl |= B43legacy_TX4_MAC_STMSDU;
299 mac_ctl |= B43legacy_TX4_MAC_FALLBACKOFDM;
300 if (txctl->flags & IEEE80211_TXCTL_LONG_RETRY_LIMIT)
301 mac_ctl |= B43legacy_TX4_MAC_LONGFRAME;
303 /* Generate the RTS or CTS-to-self frame */
304 if ((txctl->flags & IEEE80211_TXCTL_USE_RTS_CTS) ||
305 (txctl->flags & IEEE80211_TXCTL_USE_CTS_PROTECT)) {
307 struct ieee80211_hdr *hdr;
311 int rts_rate_fb_ofdm;
313 rts_rate = txctl->rts_cts_rate;
314 rts_rate_ofdm = b43legacy_is_ofdm_rate(rts_rate);
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 (txctl->flags & IEEE80211_TXCTL_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, txctl,
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((struct b43legacy_plcp_hdr4 *)
343 (&txhdr->rts_plcp_fb),
345 hdr = (struct ieee80211_hdr *)(&txhdr->rts_frame);
346 txhdr->rts_dur_fb = hdr->duration_id;
350 txhdr->cookie = cpu_to_le16(cookie);
352 /* Apply the bitfields */
353 txhdr->mac_ctl = cpu_to_le32(mac_ctl);
354 txhdr->phy_ctl = cpu_to_le16(phy_ctl);
359 int b43legacy_generate_txhdr(struct b43legacy_wldev *dev,
361 const unsigned char *fragment_data,
362 unsigned int fragment_len,
363 const struct ieee80211_tx_control *txctl,
366 return generate_txhdr_fw3(dev, (struct b43legacy_txhdr_fw3 *)txhdr,
367 fragment_data, fragment_len,
371 static s8 b43legacy_rssi_postprocess(struct b43legacy_wldev *dev,
372 u8 in_rssi, int ofdm,
373 int adjust_2053, int adjust_2050)
375 struct b43legacy_phy *phy = &dev->phy;
378 switch (phy->radio_ver) {
391 if (dev->dev->bus->sprom.boardflags_lo
392 & B43legacy_BFL_RSSI) {
395 tmp = phy->nrssi_lt[in_rssi];
407 if (phy->type == B43legacy_PHYTYPE_G &&
432 void b43legacy_rx(struct b43legacy_wldev *dev,
436 struct ieee80211_rx_status status;
437 struct b43legacy_plcp_hdr6 *plcp;
438 struct ieee80211_hdr *wlhdr;
439 const struct b43legacy_rxhdr_fw3 *rxhdr = _rxhdr;
450 memset(&status, 0, sizeof(status));
452 /* Get metadata about the frame from the header. */
453 phystat0 = le16_to_cpu(rxhdr->phy_status0);
454 phystat3 = le16_to_cpu(rxhdr->phy_status3);
456 macstat = le16_to_cpu(rxhdr->mac_status);
457 mactime = le16_to_cpu(rxhdr->mac_time);
458 chanstat = le16_to_cpu(rxhdr->channel);
460 if (macstat & B43legacy_RX_MAC_FCSERR)
461 dev->wl->ieee_stats.dot11FCSErrorCount++;
463 /* Skip PLCP and padding */
464 padding = (macstat & B43legacy_RX_MAC_PADDING) ? 2 : 0;
465 if (unlikely(skb->len < (sizeof(struct b43legacy_plcp_hdr6) +
467 b43legacydbg(dev->wl, "RX: Packet size underrun (1)\n");
470 plcp = (struct b43legacy_plcp_hdr6 *)(skb->data + padding);
471 skb_pull(skb, sizeof(struct b43legacy_plcp_hdr6) + padding);
472 /* The skb contains the Wireless Header + payload data now */
473 if (unlikely(skb->len < (2+2+6/*minimum hdr*/ + FCS_LEN))) {
474 b43legacydbg(dev->wl, "RX: Packet size underrun (2)\n");
477 wlhdr = (struct ieee80211_hdr *)(skb->data);
478 fctl = le16_to_cpu(wlhdr->frame_control);
480 if ((macstat & B43legacy_RX_MAC_DEC) &&
481 !(macstat & B43legacy_RX_MAC_DECERR)) {
487 keyidx = ((macstat & B43legacy_RX_MAC_KEYIDX)
488 >> B43legacy_RX_MAC_KEYIDX_SHIFT);
489 /* We must adjust the key index here. We want the "physical"
490 * key index, but the ucode passed it slightly different.
492 keyidx = b43legacy_kidx_to_raw(dev, keyidx);
493 B43legacy_WARN_ON(keyidx >= dev->max_nr_keys);
495 if (dev->key[keyidx].algorithm != B43legacy_SEC_ALGO_NONE) {
496 /* Remove PROTECTED flag to mark it as decrypted. */
497 B43legacy_WARN_ON(!(fctl & IEEE80211_FCTL_PROTECTED));
498 fctl &= ~IEEE80211_FCTL_PROTECTED;
499 wlhdr->frame_control = cpu_to_le16(fctl);
501 wlhdr_len = ieee80211_get_hdrlen(fctl);
502 if (unlikely(skb->len < (wlhdr_len + 3))) {
503 b43legacydbg(dev->wl, "RX: Packet size"
507 if (skb->data[wlhdr_len + 3] & (1 << 5)) {
508 /* The Ext-IV Bit is set in the "KeyID"
517 if (unlikely(skb->len < (wlhdr_len + iv_len +
519 b43legacydbg(dev->wl, "RX: Packet size"
524 memmove(skb->data + iv_len, skb->data, wlhdr_len);
525 skb_pull(skb, iv_len);
527 skb_trim(skb, skb->len - icv_len);
529 status.flag |= RX_FLAG_DECRYPTED;
533 status.ssi = b43legacy_rssi_postprocess(dev, jssi,
534 (phystat0 & B43legacy_RX_PHYST0_OFDM),
535 (phystat0 & B43legacy_RX_PHYST0_GAINCTL),
536 (phystat3 & B43legacy_RX_PHYST3_TRSTATE));
537 status.noise = dev->stats.link_noise;
538 status.signal = (jssi * 100) / B43legacy_RX_MAX_SSI;
539 if (phystat0 & B43legacy_RX_PHYST0_OFDM)
540 status.rate = b43legacy_plcp_get_bitrate_ofdm(plcp);
542 status.rate = b43legacy_plcp_get_bitrate_cck(plcp);
543 status.antenna = !!(phystat0 & B43legacy_RX_PHYST0_ANT);
546 * If monitors are present get full 64-bit timestamp. This
547 * code assumes we get to process the packet within 16 bits
548 * of timestamp, i.e. about 65 milliseconds after the PHY
549 * received the first symbol.
551 if (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_TSFT;
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 status.phymode = MODE_IEEE80211B;
568 status.freq = chanid + 2400;
569 status.channel = b43legacy_freq_to_channel_bg(chanid + 2400);
571 case B43legacy_PHYTYPE_G:
572 status.phymode = MODE_IEEE80211G;
573 status.freq = chanid + 2400;
574 status.channel = b43legacy_freq_to_channel_bg(chanid + 2400);
577 b43legacywarn(dev->wl, "Unexpected value for chanstat (0x%X)\n",
581 dev->stats.last_rx = jiffies;
582 ieee80211_rx_irqsafe(dev->wl->hw, skb, &status);
586 b43legacydbg(dev->wl, "RX: Packet dropped\n");
587 dev_kfree_skb_any(skb);
590 void b43legacy_handle_txstatus(struct b43legacy_wldev *dev,
591 const struct b43legacy_txstatus *status)
593 b43legacy_debugfs_log_txstat(dev, status);
595 if (status->intermediate)
597 if (status->for_ampdu)
600 dev->wl->ieee_stats.dot11ACKFailureCount++;
601 if (status->rts_count) {
602 if (status->rts_count == 0xF) /* FIXME */
603 dev->wl->ieee_stats.dot11RTSFailureCount++;
605 dev->wl->ieee_stats.dot11RTSSuccessCount++;
608 if (b43legacy_using_pio(dev))
609 b43legacy_pio_handle_txstatus(dev, status);
611 b43legacy_dma_handle_txstatus(dev, status);
614 /* Handle TX status report as received through DMA/PIO queues */
615 void b43legacy_handle_hwtxstatus(struct b43legacy_wldev *dev,
616 const struct b43legacy_hwtxstatus *hw)
618 struct b43legacy_txstatus status;
621 status.cookie = le16_to_cpu(hw->cookie);
622 status.seq = le16_to_cpu(hw->seq);
623 status.phy_stat = hw->phy_stat;
625 status.frame_count = (tmp >> 4);
626 status.rts_count = (tmp & 0x0F);
628 status.supp_reason = ((tmp & 0x1C) >> 2);
629 status.pm_indicated = !!(tmp & 0x80);
630 status.intermediate = !!(tmp & 0x40);
631 status.for_ampdu = !!(tmp & 0x20);
632 status.acked = !!(tmp & 0x02);
634 b43legacy_handle_txstatus(dev, &status);
637 /* Stop any TX operation on the device (suspend the hardware queues) */
638 void b43legacy_tx_suspend(struct b43legacy_wldev *dev)
640 if (b43legacy_using_pio(dev))
641 b43legacy_pio_freeze_txqueues(dev);
643 b43legacy_dma_tx_suspend(dev);
646 /* Resume any TX operation on the device (resume the hardware queues) */
647 void b43legacy_tx_resume(struct b43legacy_wldev *dev)
649 if (b43legacy_using_pio(dev))
650 b43legacy_pio_thaw_txqueues(dev);
652 b43legacy_dma_tx_resume(dev);
655 /* Initialize the QoS parameters */
656 void b43legacy_qos_init(struct b43legacy_wldev *dev)
658 /* FIXME: This function must probably be called from the mac80211
659 * config callback. */
662 b43legacy_hf_write(dev, b43legacy_hf_read(dev) | B43legacy_HF_EDCF);
663 /* FIXME kill magic */
664 b43legacy_write16(dev, 0x688,
665 b43legacy_read16(dev, 0x688) | 0x4);
668 /*TODO: We might need some stack support here to get the values. */