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_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 const struct ieee80211_tx_control
*txctl
,
194 const struct ieee80211_hdr
*wlhdr
;
195 int use_encryption
= (!(txctl
->flags
& IEEE80211_TXCTL_DO_NOT_ENCRYPT
));
198 struct ieee80211_rate
*rate_fb
;
201 unsigned int plcp_fragment_len
;
205 wlhdr
= (const struct ieee80211_hdr
*)fragment_data
;
206 fctl
= le16_to_cpu(wlhdr
->frame_control
);
208 memset(txhdr
, 0, sizeof(*txhdr
));
210 rate
= txctl
->tx_rate
->hw_value
;
211 rate_ofdm
= b43legacy_is_ofdm_rate(rate
);
212 rate_fb
= txctl
->alt_retry_rate
? : txctl
->tx_rate
;
213 rate_fb_ofdm
= b43legacy_is_ofdm_rate(rate_fb
->hw_value
);
215 txhdr
->mac_frame_ctl
= wlhdr
->frame_control
;
216 memcpy(txhdr
->tx_receiver
, wlhdr
->addr1
, 6);
218 /* Calculate duration for fallback rate */
219 if ((rate_fb
->hw_value
== rate
) ||
220 (wlhdr
->duration_id
& cpu_to_le16(0x8000)) ||
221 (wlhdr
->duration_id
== cpu_to_le16(0))) {
222 /* If the fallback rate equals the normal rate or the
223 * dur_id field contains an AID, CFP magic or 0,
224 * use the original dur_id field. */
225 txhdr
->dur_fb
= wlhdr
->duration_id
;
227 txhdr
->dur_fb
= ieee80211_generic_frame_duration(dev
->wl
->hw
,
233 plcp_fragment_len
= fragment_len
+ FCS_LEN
;
234 if (use_encryption
) {
235 u8 key_idx
= (u16
)(txctl
->key_idx
);
236 struct b43legacy_key
*key
;
240 B43legacy_WARN_ON(key_idx
>= dev
->max_nr_keys
);
241 key
= &(dev
->key
[key_idx
]);
244 /* Hardware appends ICV. */
245 plcp_fragment_len
+= txctl
->icv_len
;
247 key_idx
= b43legacy_kidx_to_fw(dev
, key_idx
);
248 mac_ctl
|= (key_idx
<< B43legacy_TX4_MAC_KEYIDX_SHIFT
) &
249 B43legacy_TX4_MAC_KEYIDX
;
250 mac_ctl
|= (key
->algorithm
<<
251 B43legacy_TX4_MAC_KEYALG_SHIFT
) &
252 B43legacy_TX4_MAC_KEYALG
;
253 wlhdr_len
= ieee80211_get_hdrlen(fctl
);
254 iv_len
= min((size_t)txctl
->iv_len
,
255 ARRAY_SIZE(txhdr
->iv
));
256 memcpy(txhdr
->iv
, ((u8
*)wlhdr
) + wlhdr_len
, iv_len
);
258 /* This key is invalid. This might only happen
259 * in a short timeframe after machine resume before
260 * we were able to reconfigure keys.
261 * Drop this packet completely. Do not transmit it
262 * unencrypted to avoid leaking information. */
266 b43legacy_generate_plcp_hdr((struct b43legacy_plcp_hdr4
*)
267 (&txhdr
->plcp
), plcp_fragment_len
,
269 b43legacy_generate_plcp_hdr((struct b43legacy_plcp_hdr4
*)
270 (&txhdr
->plcp_fb
), plcp_fragment_len
,
273 /* PHY TX Control word */
275 phy_ctl
|= B43legacy_TX4_PHY_OFDM
;
276 if (dev
->short_preamble
)
277 phy_ctl
|= B43legacy_TX4_PHY_SHORTPRMBL
;
278 switch (txctl
->antenna_sel_tx
) {
280 phy_ctl
|= B43legacy_TX4_PHY_ANTLAST
;
283 phy_ctl
|= B43legacy_TX4_PHY_ANT0
;
286 phy_ctl
|= B43legacy_TX4_PHY_ANT1
;
293 if (!(txctl
->flags
& IEEE80211_TXCTL_NO_ACK
))
294 mac_ctl
|= B43legacy_TX4_MAC_ACK
;
295 if (!(((fctl
& IEEE80211_FCTL_FTYPE
) == IEEE80211_FTYPE_CTL
) &&
296 ((fctl
& IEEE80211_FCTL_STYPE
) == IEEE80211_STYPE_PSPOLL
)))
297 mac_ctl
|= B43legacy_TX4_MAC_HWSEQ
;
298 if (txctl
->flags
& IEEE80211_TXCTL_FIRST_FRAGMENT
)
299 mac_ctl
|= B43legacy_TX4_MAC_STMSDU
;
301 mac_ctl
|= B43legacy_TX4_MAC_FALLBACKOFDM
;
302 if (txctl
->flags
& IEEE80211_TXCTL_LONG_RETRY_LIMIT
)
303 mac_ctl
|= B43legacy_TX4_MAC_LONGFRAME
;
305 /* Generate the RTS or CTS-to-self frame */
306 if ((txctl
->flags
& IEEE80211_TXCTL_USE_RTS_CTS
) ||
307 (txctl
->flags
& IEEE80211_TXCTL_USE_CTS_PROTECT
)) {
309 struct ieee80211_hdr
*hdr
;
313 int rts_rate_fb_ofdm
;
315 rts_rate
= txctl
->rts_cts_rate
->hw_value
;
316 rts_rate_ofdm
= b43legacy_is_ofdm_rate(rts_rate
);
317 rts_rate_fb
= b43legacy_calc_fallback_rate(rts_rate
);
318 rts_rate_fb_ofdm
= b43legacy_is_ofdm_rate(rts_rate_fb
);
319 if (rts_rate_fb_ofdm
)
320 mac_ctl
|= B43legacy_TX4_MAC_CTSFALLBACKOFDM
;
322 if (txctl
->flags
& IEEE80211_TXCTL_USE_CTS_PROTECT
) {
323 ieee80211_ctstoself_get(dev
->wl
->hw
,
327 (struct ieee80211_cts
*)
329 mac_ctl
|= B43legacy_TX4_MAC_SENDCTS
;
330 len
= sizeof(struct ieee80211_cts
);
332 ieee80211_rts_get(dev
->wl
->hw
,
334 fragment_data
, fragment_len
, txctl
,
335 (struct ieee80211_rts
*)
337 mac_ctl
|= B43legacy_TX4_MAC_SENDRTS
;
338 len
= sizeof(struct ieee80211_rts
);
341 b43legacy_generate_plcp_hdr((struct b43legacy_plcp_hdr4
*)
344 b43legacy_generate_plcp_hdr((struct b43legacy_plcp_hdr4
*)
345 (&txhdr
->rts_plcp_fb
),
347 hdr
= (struct ieee80211_hdr
*)(&txhdr
->rts_frame
);
348 txhdr
->rts_dur_fb
= hdr
->duration_id
;
352 txhdr
->cookie
= cpu_to_le16(cookie
);
354 /* Apply the bitfields */
355 txhdr
->mac_ctl
= cpu_to_le32(mac_ctl
);
356 txhdr
->phy_ctl
= cpu_to_le16(phy_ctl
);
361 int b43legacy_generate_txhdr(struct b43legacy_wldev
*dev
,
363 const unsigned char *fragment_data
,
364 unsigned int fragment_len
,
365 const struct ieee80211_tx_control
*txctl
,
368 return generate_txhdr_fw3(dev
, (struct b43legacy_txhdr_fw3
*)txhdr
,
369 fragment_data
, fragment_len
,
373 static s8
b43legacy_rssi_postprocess(struct b43legacy_wldev
*dev
,
374 u8 in_rssi
, int ofdm
,
375 int adjust_2053
, int adjust_2050
)
377 struct b43legacy_phy
*phy
= &dev
->phy
;
380 switch (phy
->radio_ver
) {
393 if (dev
->dev
->bus
->sprom
.boardflags_lo
394 & B43legacy_BFL_RSSI
) {
397 tmp
= phy
->nrssi_lt
[in_rssi
];
409 if (phy
->type
== B43legacy_PHYTYPE_G
&&
434 void b43legacy_rx(struct b43legacy_wldev
*dev
,
438 struct ieee80211_rx_status status
;
439 struct b43legacy_plcp_hdr6
*plcp
;
440 struct ieee80211_hdr
*wlhdr
;
441 const struct b43legacy_rxhdr_fw3
*rxhdr
= _rxhdr
;
452 memset(&status
, 0, sizeof(status
));
454 /* Get metadata about the frame from the header. */
455 phystat0
= le16_to_cpu(rxhdr
->phy_status0
);
456 phystat3
= le16_to_cpu(rxhdr
->phy_status3
);
458 macstat
= le16_to_cpu(rxhdr
->mac_status
);
459 mactime
= le16_to_cpu(rxhdr
->mac_time
);
460 chanstat
= le16_to_cpu(rxhdr
->channel
);
462 if (macstat
& B43legacy_RX_MAC_FCSERR
)
463 dev
->wl
->ieee_stats
.dot11FCSErrorCount
++;
465 /* Skip PLCP and padding */
466 padding
= (macstat
& B43legacy_RX_MAC_PADDING
) ? 2 : 0;
467 if (unlikely(skb
->len
< (sizeof(struct b43legacy_plcp_hdr6
) +
469 b43legacydbg(dev
->wl
, "RX: Packet size underrun (1)\n");
472 plcp
= (struct b43legacy_plcp_hdr6
*)(skb
->data
+ padding
);
473 skb_pull(skb
, sizeof(struct b43legacy_plcp_hdr6
) + padding
);
474 /* The skb contains the Wireless Header + payload data now */
475 if (unlikely(skb
->len
< (2+2+6/*minimum hdr*/ + FCS_LEN
))) {
476 b43legacydbg(dev
->wl
, "RX: Packet size underrun (2)\n");
479 wlhdr
= (struct ieee80211_hdr
*)(skb
->data
);
480 fctl
= le16_to_cpu(wlhdr
->frame_control
);
482 if ((macstat
& B43legacy_RX_MAC_DEC
) &&
483 !(macstat
& B43legacy_RX_MAC_DECERR
)) {
489 keyidx
= ((macstat
& B43legacy_RX_MAC_KEYIDX
)
490 >> B43legacy_RX_MAC_KEYIDX_SHIFT
);
491 /* We must adjust the key index here. We want the "physical"
492 * key index, but the ucode passed it slightly different.
494 keyidx
= b43legacy_kidx_to_raw(dev
, keyidx
);
495 B43legacy_WARN_ON(keyidx
>= dev
->max_nr_keys
);
497 if (dev
->key
[keyidx
].algorithm
!= B43legacy_SEC_ALGO_NONE
) {
498 /* Remove PROTECTED flag to mark it as decrypted. */
499 B43legacy_WARN_ON(!(fctl
& IEEE80211_FCTL_PROTECTED
));
500 fctl
&= ~IEEE80211_FCTL_PROTECTED
;
501 wlhdr
->frame_control
= cpu_to_le16(fctl
);
503 wlhdr_len
= ieee80211_get_hdrlen(fctl
);
504 if (unlikely(skb
->len
< (wlhdr_len
+ 3))) {
505 b43legacydbg(dev
->wl
, "RX: Packet size"
509 if (skb
->data
[wlhdr_len
+ 3] & (1 << 5)) {
510 /* The Ext-IV Bit is set in the "KeyID"
519 if (unlikely(skb
->len
< (wlhdr_len
+ iv_len
+
521 b43legacydbg(dev
->wl
, "RX: Packet size"
526 memmove(skb
->data
+ iv_len
, skb
->data
, wlhdr_len
);
527 skb_pull(skb
, iv_len
);
529 skb_trim(skb
, skb
->len
- icv_len
);
531 status
.flag
|= RX_FLAG_DECRYPTED
;
535 status
.ssi
= b43legacy_rssi_postprocess(dev
, jssi
,
536 (phystat0
& B43legacy_RX_PHYST0_OFDM
),
537 (phystat0
& B43legacy_RX_PHYST0_GAINCTL
),
538 (phystat3
& B43legacy_RX_PHYST3_TRSTATE
));
539 status
.noise
= dev
->stats
.link_noise
;
540 status
.signal
= (jssi
* 100) / B43legacy_RX_MAX_SSI
;
541 /* change to support A PHY */
542 if (phystat0
& B43legacy_RX_PHYST0_OFDM
)
543 status
.rate_idx
= b43legacy_plcp_get_bitrate_idx_ofdm(plcp
, false);
545 status
.rate_idx
= b43legacy_plcp_get_bitrate_idx_cck(plcp
);
546 status
.antenna
= !!(phystat0
& B43legacy_RX_PHYST0_ANT
);
549 * All frames on monitor interfaces and beacons always need a full
550 * 64-bit timestamp. Monitor interfaces need it for diagnostic
551 * purposes and beacons for IBSS merging.
552 * This code assumes we get to process the packet within 16 bits
553 * of timestamp, i.e. about 65 milliseconds after the PHY received
556 if (((fctl
& (IEEE80211_FCTL_FTYPE
| IEEE80211_FCTL_STYPE
))
557 == (IEEE80211_FTYPE_MGMT
| IEEE80211_STYPE_BEACON
)) ||
558 dev
->wl
->radiotap_enabled
) {
561 b43legacy_tsf_read(dev
, &status
.mactime
);
562 low_mactime_now
= status
.mactime
;
563 status
.mactime
= status
.mactime
& ~0xFFFFULL
;
564 status
.mactime
+= mactime
;
565 if (low_mactime_now
<= mactime
)
566 status
.mactime
-= 0x10000;
567 status
.flag
|= RX_FLAG_TSFT
;
570 chanid
= (chanstat
& B43legacy_RX_CHAN_ID
) >>
571 B43legacy_RX_CHAN_ID_SHIFT
;
572 switch (chanstat
& B43legacy_RX_CHAN_PHYTYPE
) {
573 case B43legacy_PHYTYPE_B
:
574 case B43legacy_PHYTYPE_G
:
575 status
.band
= IEEE80211_BAND_2GHZ
;
576 status
.freq
= chanid
+ 2400;
579 b43legacywarn(dev
->wl
, "Unexpected value for chanstat (0x%X)\n",
583 dev
->stats
.last_rx
= jiffies
;
584 ieee80211_rx_irqsafe(dev
->wl
->hw
, skb
, &status
);
588 b43legacydbg(dev
->wl
, "RX: Packet dropped\n");
589 dev_kfree_skb_any(skb
);
592 void b43legacy_handle_txstatus(struct b43legacy_wldev
*dev
,
593 const struct b43legacy_txstatus
*status
)
595 b43legacy_debugfs_log_txstat(dev
, status
);
597 if (status
->intermediate
)
599 if (status
->for_ampdu
)
602 dev
->wl
->ieee_stats
.dot11ACKFailureCount
++;
603 if (status
->rts_count
) {
604 if (status
->rts_count
== 0xF) /* FIXME */
605 dev
->wl
->ieee_stats
.dot11RTSFailureCount
++;
607 dev
->wl
->ieee_stats
.dot11RTSSuccessCount
++;
610 if (b43legacy_using_pio(dev
))
611 b43legacy_pio_handle_txstatus(dev
, status
);
613 b43legacy_dma_handle_txstatus(dev
, status
);
616 /* Handle TX status report as received through DMA/PIO queues */
617 void b43legacy_handle_hwtxstatus(struct b43legacy_wldev
*dev
,
618 const struct b43legacy_hwtxstatus
*hw
)
620 struct b43legacy_txstatus status
;
623 status
.cookie
= le16_to_cpu(hw
->cookie
);
624 status
.seq
= le16_to_cpu(hw
->seq
);
625 status
.phy_stat
= hw
->phy_stat
;
627 status
.frame_count
= (tmp
>> 4);
628 status
.rts_count
= (tmp
& 0x0F);
630 status
.supp_reason
= ((tmp
& 0x1C) >> 2);
631 status
.pm_indicated
= !!(tmp
& 0x80);
632 status
.intermediate
= !!(tmp
& 0x40);
633 status
.for_ampdu
= !!(tmp
& 0x20);
634 status
.acked
= !!(tmp
& 0x02);
636 b43legacy_handle_txstatus(dev
, &status
);
639 /* Stop any TX operation on the device (suspend the hardware queues) */
640 void b43legacy_tx_suspend(struct b43legacy_wldev
*dev
)
642 if (b43legacy_using_pio(dev
))
643 b43legacy_pio_freeze_txqueues(dev
);
645 b43legacy_dma_tx_suspend(dev
);
648 /* Resume any TX operation on the device (resume the hardware queues) */
649 void b43legacy_tx_resume(struct b43legacy_wldev
*dev
)
651 if (b43legacy_using_pio(dev
))
652 b43legacy_pio_thaw_txqueues(dev
);
654 b43legacy_dma_tx_resume(dev
);
657 /* Initialize the QoS parameters */
658 void b43legacy_qos_init(struct b43legacy_wldev
*dev
)
660 /* FIXME: This function must probably be called from the mac80211
661 * config callback. */
664 b43legacy_hf_write(dev
, b43legacy_hf_read(dev
) | B43legacy_HF_EDCF
);
665 /* FIXME kill magic */
666 b43legacy_write16(dev
, 0x688,
667 b43legacy_read16(dev
, 0x688) | 0x4);
670 /*TODO: We might need some stack support here to get the values. */