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.
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]) {
53 /* Extract the bitrate index out of an OFDM PLCP header. */
54 static u8 b43_plcp_get_bitrate_idx_ofdm(struct b43_plcp_hdr6 *plcp, bool aphy)
56 int base = aphy ? 0 : 4;
58 switch (plcp->raw[0] & 0xF) {
80 u8 b43_plcp_get_ratecode_cck(const u8 bitrate)
83 case B43_CCK_RATE_1MB:
85 case B43_CCK_RATE_2MB:
87 case B43_CCK_RATE_5MB:
89 case B43_CCK_RATE_11MB:
96 u8 b43_plcp_get_ratecode_ofdm(const u8 bitrate)
99 case B43_OFDM_RATE_6MB:
101 case B43_OFDM_RATE_9MB:
103 case B43_OFDM_RATE_12MB:
105 case B43_OFDM_RATE_18MB:
107 case B43_OFDM_RATE_24MB:
109 case B43_OFDM_RATE_36MB:
111 case B43_OFDM_RATE_48MB:
113 case B43_OFDM_RATE_54MB:
120 void b43_generate_plcp_hdr(struct b43_plcp_hdr4 *plcp,
121 const u16 octets, const u8 bitrate)
123 __le32 *data = &(plcp->data);
124 __u8 *raw = plcp->raw;
126 if (b43_is_ofdm_rate(bitrate)) {
129 d = b43_plcp_get_ratecode_ofdm(bitrate);
130 B43_WARN_ON(octets & 0xF000);
132 *data = cpu_to_le32(d);
136 plen = octets * 16 / bitrate;
137 if ((octets * 16 % bitrate) > 0) {
139 if ((bitrate == B43_CCK_RATE_11MB)
140 && ((octets * 8 % 11) < 4)) {
146 *data |= cpu_to_le32(plen << 16);
147 raw[0] = b43_plcp_get_ratecode_cck(bitrate);
151 static u8 b43_calc_fallback_rate(u8 bitrate)
154 case B43_CCK_RATE_1MB:
155 return B43_CCK_RATE_1MB;
156 case B43_CCK_RATE_2MB:
157 return B43_CCK_RATE_1MB;
158 case B43_CCK_RATE_5MB:
159 return B43_CCK_RATE_2MB;
160 case B43_CCK_RATE_11MB:
161 return B43_CCK_RATE_5MB;
162 case B43_OFDM_RATE_6MB:
163 return B43_CCK_RATE_5MB;
164 case B43_OFDM_RATE_9MB:
165 return B43_OFDM_RATE_6MB;
166 case B43_OFDM_RATE_12MB:
167 return B43_OFDM_RATE_9MB;
168 case B43_OFDM_RATE_18MB:
169 return B43_OFDM_RATE_12MB;
170 case B43_OFDM_RATE_24MB:
171 return B43_OFDM_RATE_18MB;
172 case B43_OFDM_RATE_36MB:
173 return B43_OFDM_RATE_24MB;
174 case B43_OFDM_RATE_48MB:
175 return B43_OFDM_RATE_36MB;
176 case B43_OFDM_RATE_54MB:
177 return B43_OFDM_RATE_48MB;
183 /* Generate a TX data header. */
184 int b43_generate_txhdr(struct b43_wldev *dev,
186 const unsigned char *fragment_data,
187 unsigned int fragment_len,
188 const struct ieee80211_tx_control *txctl,
191 struct b43_txhdr *txhdr = (struct b43_txhdr *)_txhdr;
192 const struct b43_phy *phy = &dev->phy;
193 const struct ieee80211_hdr *wlhdr =
194 (const struct ieee80211_hdr *)fragment_data;
195 int use_encryption = (!(txctl->flags & IEEE80211_TXCTL_DO_NOT_ENCRYPT));
196 u16 fctl = le16_to_cpu(wlhdr->frame_control);
197 struct ieee80211_rate *fbrate;
199 int rate_ofdm, rate_fb_ofdm;
200 unsigned int plcp_fragment_len;
205 memset(txhdr, 0, sizeof(*txhdr));
207 WARN_ON(!txctl->tx_rate);
208 rate = txctl->tx_rate ? txctl->tx_rate->hw_value : B43_CCK_RATE_1MB;
209 rate_ofdm = b43_is_ofdm_rate(rate);
210 fbrate = txctl->alt_retry_rate ? : txctl->tx_rate;
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(dev->wl->hw,
236 plcp_fragment_len = fragment_len + FCS_LEN;
237 if (use_encryption) {
238 u8 key_idx = (u16) (txctl->key_idx);
243 B43_WARN_ON(key_idx >= dev->max_nr_keys);
244 key = &(dev->key[key_idx]);
246 if (unlikely(!key->keyconf)) {
247 /* This key is invalid. This might only happen
248 * in a short timeframe after machine resume before
249 * we were able to reconfigure keys.
250 * Drop this packet completely. Do not transmit it
251 * unencrypted to avoid leaking information. */
255 /* Hardware appends ICV. */
256 plcp_fragment_len += txctl->icv_len;
258 key_idx = b43_kidx_to_fw(dev, key_idx);
259 mac_ctl |= (key_idx << B43_TXH_MAC_KEYIDX_SHIFT) &
261 mac_ctl |= (key->algorithm << B43_TXH_MAC_KEYALG_SHIFT) &
263 wlhdr_len = ieee80211_get_hdrlen(fctl);
264 iv_len = min((size_t) txctl->iv_len,
265 ARRAY_SIZE(txhdr->iv));
266 memcpy(txhdr->iv, ((u8 *) wlhdr) + wlhdr_len, iv_len);
268 if (b43_is_old_txhdr_format(dev)) {
269 b43_generate_plcp_hdr((struct b43_plcp_hdr4 *)(&txhdr->old_format.plcp),
270 plcp_fragment_len, rate);
272 b43_generate_plcp_hdr((struct b43_plcp_hdr4 *)(&txhdr->new_format.plcp),
273 plcp_fragment_len, rate);
275 b43_generate_plcp_hdr((struct b43_plcp_hdr4 *)(&txhdr->plcp_fb),
276 plcp_fragment_len, rate_fb);
278 /* Extra Frame Types */
280 extra_ft |= B43_TXH_EFT_FB_OFDM;
282 extra_ft |= B43_TXH_EFT_FB_CCK;
284 /* Set channel radio code. Note that the micrcode ORs 0x100 to
285 * this value before comparing it to the value in SHM, if this
288 txhdr->chan_radio_code = phy->channel;
290 /* PHY TX Control word */
292 phy_ctl |= B43_TXH_PHY_ENC_OFDM;
294 phy_ctl |= B43_TXH_PHY_ENC_CCK;
295 if (txctl->flags & IEEE80211_TXCTL_SHORT_PREAMBLE)
296 phy_ctl |= B43_TXH_PHY_SHORTPRMBL;
298 switch (b43_ieee80211_antenna_sanitize(dev, txctl->antenna_sel_tx)) {
299 case 0: /* Default */
300 phy_ctl |= B43_TXH_PHY_ANT01AUTO;
302 case 1: /* Antenna 0 */
303 phy_ctl |= B43_TXH_PHY_ANT0;
305 case 2: /* Antenna 1 */
306 phy_ctl |= B43_TXH_PHY_ANT1;
308 case 3: /* Antenna 2 */
309 phy_ctl |= B43_TXH_PHY_ANT2;
311 case 4: /* Antenna 3 */
312 phy_ctl |= B43_TXH_PHY_ANT3;
319 if (!(txctl->flags & IEEE80211_TXCTL_NO_ACK))
320 mac_ctl |= B43_TXH_MAC_ACK;
321 if (!(((fctl & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_CTL) &&
322 ((fctl & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_PSPOLL)))
323 mac_ctl |= B43_TXH_MAC_HWSEQ;
324 if (txctl->flags & IEEE80211_TXCTL_FIRST_FRAGMENT)
325 mac_ctl |= B43_TXH_MAC_STMSDU;
326 if (phy->type == B43_PHYTYPE_A)
327 mac_ctl |= B43_TXH_MAC_5GHZ;
328 if (txctl->flags & IEEE80211_TXCTL_LONG_RETRY_LIMIT)
329 mac_ctl |= B43_TXH_MAC_LONGFRAME;
331 /* Generate the RTS or CTS-to-self frame */
332 if ((txctl->flags & IEEE80211_TXCTL_USE_RTS_CTS) ||
333 (txctl->flags & IEEE80211_TXCTL_USE_CTS_PROTECT)) {
335 struct ieee80211_hdr *hdr;
336 int rts_rate, rts_rate_fb;
337 int rts_rate_ofdm, rts_rate_fb_ofdm;
338 struct b43_plcp_hdr6 *plcp;
340 WARN_ON(!txctl->rts_cts_rate);
341 rts_rate = txctl->rts_cts_rate ? txctl->rts_cts_rate->hw_value : B43_CCK_RATE_1MB;
342 rts_rate_ofdm = b43_is_ofdm_rate(rts_rate);
343 rts_rate_fb = b43_calc_fallback_rate(rts_rate);
344 rts_rate_fb_ofdm = b43_is_ofdm_rate(rts_rate_fb);
346 if (txctl->flags & IEEE80211_TXCTL_USE_CTS_PROTECT) {
347 struct ieee80211_cts *cts;
349 if (b43_is_old_txhdr_format(dev)) {
350 cts = (struct ieee80211_cts *)
351 (txhdr->old_format.rts_frame);
353 cts = (struct ieee80211_cts *)
354 (txhdr->new_format.rts_frame);
356 ieee80211_ctstoself_get(dev->wl->hw, txctl->vif,
357 fragment_data, fragment_len,
359 mac_ctl |= B43_TXH_MAC_SENDCTS;
360 len = sizeof(struct ieee80211_cts);
362 struct ieee80211_rts *rts;
364 if (b43_is_old_txhdr_format(dev)) {
365 rts = (struct ieee80211_rts *)
366 (txhdr->old_format.rts_frame);
368 rts = (struct ieee80211_rts *)
369 (txhdr->new_format.rts_frame);
371 ieee80211_rts_get(dev->wl->hw, txctl->vif,
372 fragment_data, fragment_len,
374 mac_ctl |= B43_TXH_MAC_SENDRTS;
375 len = sizeof(struct ieee80211_rts);
379 /* Generate the PLCP headers for the RTS/CTS frame */
380 if (b43_is_old_txhdr_format(dev))
381 plcp = &txhdr->old_format.rts_plcp;
383 plcp = &txhdr->new_format.rts_plcp;
384 b43_generate_plcp_hdr((struct b43_plcp_hdr4 *)plcp,
386 plcp = &txhdr->rts_plcp_fb;
387 b43_generate_plcp_hdr((struct b43_plcp_hdr4 *)plcp,
390 if (b43_is_old_txhdr_format(dev)) {
391 hdr = (struct ieee80211_hdr *)
392 (&txhdr->old_format.rts_frame);
394 hdr = (struct ieee80211_hdr *)
395 (&txhdr->new_format.rts_frame);
397 txhdr->rts_dur_fb = hdr->duration_id;
400 extra_ft |= B43_TXH_EFT_RTS_OFDM;
401 txhdr->phy_rate_rts =
402 b43_plcp_get_ratecode_ofdm(rts_rate);
404 extra_ft |= B43_TXH_EFT_RTS_CCK;
405 txhdr->phy_rate_rts =
406 b43_plcp_get_ratecode_cck(rts_rate);
408 if (rts_rate_fb_ofdm)
409 extra_ft |= B43_TXH_EFT_RTSFB_OFDM;
411 extra_ft |= B43_TXH_EFT_RTSFB_CCK;
415 if (b43_is_old_txhdr_format(dev))
416 txhdr->old_format.cookie = cpu_to_le16(cookie);
418 txhdr->new_format.cookie = cpu_to_le16(cookie);
420 /* Apply the bitfields */
421 txhdr->mac_ctl = cpu_to_le32(mac_ctl);
422 txhdr->phy_ctl = cpu_to_le16(phy_ctl);
423 txhdr->extra_ft = extra_ft;
428 static s8 b43_rssi_postprocess(struct b43_wldev *dev,
429 u8 in_rssi, int ofdm,
430 int adjust_2053, int adjust_2050)
432 struct b43_phy *phy = &dev->phy;
435 switch (phy->radio_ver) {
448 if (dev->dev->bus->sprom.
449 boardflags_lo & B43_BFL_RSSI) {
452 tmp = phy->nrssi_lt[in_rssi];
464 if (phy->type == B43_PHYTYPE_G && adjust_2050)
490 static s8 b43_rssinoise_postprocess(struct b43_wldev *dev, u8 in_rssi)
492 struct b43_phy *phy = &dev->phy;
495 if (phy->type == B43_PHYTYPE_A) {
496 //TODO: Incomplete specs.
499 ret = b43_rssi_postprocess(dev, in_rssi, 0, 1, 1);
505 void b43_rx(struct b43_wldev *dev, struct sk_buff *skb, const void *_rxhdr)
507 struct ieee80211_rx_status status;
508 struct b43_plcp_hdr6 *plcp;
509 struct ieee80211_hdr *wlhdr;
510 const struct b43_rxhdr_fw4 *rxhdr = _rxhdr;
512 u16 phystat0, phystat3, chanstat, mactime;
518 memset(&status, 0, sizeof(status));
520 /* Get metadata about the frame from the header. */
521 phystat0 = le16_to_cpu(rxhdr->phy_status0);
522 phystat3 = le16_to_cpu(rxhdr->phy_status3);
523 macstat = le32_to_cpu(rxhdr->mac_status);
524 mactime = le16_to_cpu(rxhdr->mac_time);
525 chanstat = le16_to_cpu(rxhdr->channel);
526 phytype = chanstat & B43_RX_CHAN_PHYTYPE;
528 if (macstat & B43_RX_MAC_FCSERR)
529 dev->wl->ieee_stats.dot11FCSErrorCount++;
530 if (macstat & B43_RX_MAC_DECERR) {
531 /* Decryption with the given key failed.
532 * Drop the packet. We also won't be able to decrypt it with
533 * the key in software. */
537 /* Skip PLCP and padding */
538 padding = (macstat & B43_RX_MAC_PADDING) ? 2 : 0;
539 if (unlikely(skb->len < (sizeof(struct b43_plcp_hdr6) + padding))) {
540 b43dbg(dev->wl, "RX: Packet size underrun (1)\n");
543 plcp = (struct b43_plcp_hdr6 *)(skb->data + padding);
544 skb_pull(skb, sizeof(struct b43_plcp_hdr6) + padding);
545 /* The skb contains the Wireless Header + payload data now */
546 if (unlikely(skb->len < (2 + 2 + 6 /*minimum hdr */ + FCS_LEN))) {
547 b43dbg(dev->wl, "RX: Packet size underrun (2)\n");
550 wlhdr = (struct ieee80211_hdr *)(skb->data);
551 fctl = le16_to_cpu(wlhdr->frame_control);
553 if (macstat & B43_RX_MAC_DEC) {
557 keyidx = ((macstat & B43_RX_MAC_KEYIDX)
558 >> B43_RX_MAC_KEYIDX_SHIFT);
559 /* We must adjust the key index here. We want the "physical"
560 * key index, but the ucode passed it slightly different.
562 keyidx = b43_kidx_to_raw(dev, keyidx);
563 B43_WARN_ON(keyidx >= dev->max_nr_keys);
565 if (dev->key[keyidx].algorithm != B43_SEC_ALGO_NONE) {
566 wlhdr_len = ieee80211_get_hdrlen(fctl);
567 if (unlikely(skb->len < (wlhdr_len + 3))) {
569 "RX: Packet size underrun (3)\n");
572 status.flag |= RX_FLAG_DECRYPTED;
576 /* Link quality statistics */
577 status.noise = dev->stats.link_noise;
578 if ((chanstat & B43_RX_CHAN_PHYTYPE) == B43_PHYTYPE_N) {
579 // s8 rssi = max(rxhdr->power0, rxhdr->power1);
580 //TODO: Find out what the rssi value is (dBm or percentage?)
581 // and also find out what the maximum possible value is.
582 // Fill status.ssi and status.signal fields.
584 status.ssi = b43_rssi_postprocess(dev, rxhdr->jssi,
585 (phystat0 & B43_RX_PHYST0_OFDM),
586 (phystat0 & B43_RX_PHYST0_GAINCTL),
587 (phystat3 & B43_RX_PHYST3_TRSTATE));
588 /* the next line looks wrong, but is what mac80211 wants */
589 status.signal = (rxhdr->jssi * 100) / B43_RX_MAX_SSI;
592 if (phystat0 & B43_RX_PHYST0_OFDM)
593 status.rate_idx = b43_plcp_get_bitrate_idx_ofdm(plcp,
594 phytype == B43_PHYTYPE_A);
596 status.rate_idx = b43_plcp_get_bitrate_idx_cck(plcp);
597 status.antenna = !!(phystat0 & B43_RX_PHYST0_ANT);
600 * All frames on monitor interfaces and beacons always need a full
601 * 64-bit timestamp. Monitor interfaces need it for diagnostic
602 * purposes and beacons for IBSS merging.
603 * This code assumes we get to process the packet within 16 bits
604 * of timestamp, i.e. about 65 milliseconds after the PHY received
607 if (((fctl & (IEEE80211_FCTL_FTYPE | IEEE80211_FCTL_STYPE))
608 == (IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_BEACON)) ||
609 dev->wl->radiotap_enabled) {
612 b43_tsf_read(dev, &status.mactime);
613 low_mactime_now = status.mactime;
614 status.mactime = status.mactime & ~0xFFFFULL;
615 status.mactime += mactime;
616 if (low_mactime_now <= mactime)
617 status.mactime -= 0x10000;
618 status.flag |= RX_FLAG_TSFT;
621 chanid = (chanstat & B43_RX_CHAN_ID) >> B43_RX_CHAN_ID_SHIFT;
622 switch (chanstat & B43_RX_CHAN_PHYTYPE) {
624 status.band = IEEE80211_BAND_5GHZ;
626 /* FIXME: We don't really know which value the "chanid" contains.
627 * So the following assignment might be wrong. */
628 status.freq = b43_channel_to_freq_5ghz(chanid);
631 status.band = IEEE80211_BAND_2GHZ;
632 /* chanid is the radio channel cookie value as used
633 * to tune the radio. */
634 status.freq = chanid + 2400;
637 /* chanid is the SHM channel cookie. Which is the plain
638 * channel number in b43. */
639 if (chanstat & B43_RX_CHAN_5GHZ) {
640 status.band = IEEE80211_BAND_5GHZ;
641 status.freq = b43_freq_to_channel_5ghz(chanid);
643 status.band = IEEE80211_BAND_2GHZ;
644 status.freq = b43_freq_to_channel_2ghz(chanid);
652 dev->stats.last_rx = jiffies;
653 ieee80211_rx_irqsafe(dev->wl->hw, skb, &status);
657 b43dbg(dev->wl, "RX: Packet dropped\n");
658 dev_kfree_skb_any(skb);
661 void b43_handle_txstatus(struct b43_wldev *dev,
662 const struct b43_txstatus *status)
664 b43_debugfs_log_txstat(dev, status);
666 if (status->intermediate)
668 if (status->for_ampdu)
671 dev->wl->ieee_stats.dot11ACKFailureCount++;
672 if (status->rts_count) {
673 if (status->rts_count == 0xF) //FIXME
674 dev->wl->ieee_stats.dot11RTSFailureCount++;
676 dev->wl->ieee_stats.dot11RTSSuccessCount++;
679 if (b43_using_pio_transfers(dev))
680 b43_pio_handle_txstatus(dev, status);
682 b43_dma_handle_txstatus(dev, status);
685 /* Fill out the mac80211 TXstatus report based on the b43-specific
686 * txstatus report data. This returns a boolean whether the frame was
687 * successfully transmitted. */
688 bool b43_fill_txstatus_report(struct ieee80211_tx_status *report,
689 const struct b43_txstatus *status)
691 bool frame_success = 1;
694 /* The frame was ACKed. */
695 report->flags |= IEEE80211_TX_STATUS_ACK;
697 /* The frame was not ACKed... */
698 if (!(report->control.flags & IEEE80211_TXCTL_NO_ACK)) {
699 /* ...but we expected an ACK. */
701 report->excessive_retries = 1;
704 if (status->frame_count == 0) {
705 /* The frame was not transmitted at all. */
706 report->retry_count = 0;
708 report->retry_count = status->frame_count - 1;
710 return frame_success;
713 /* Stop any TX operation on the device (suspend the hardware queues) */
714 void b43_tx_suspend(struct b43_wldev *dev)
716 if (b43_using_pio_transfers(dev))
717 b43_pio_tx_suspend(dev);
719 b43_dma_tx_suspend(dev);
722 /* Resume any TX operation on the device (resume the hardware queues) */
723 void b43_tx_resume(struct b43_wldev *dev)
725 if (b43_using_pio_transfers(dev))
726 b43_pio_tx_resume(dev);
728 b43_dma_tx_resume(dev);