1 // SPDX-License-Identifier: GPL-2.0-or-later
4 Broadcom B43legacy wireless driver
6 Transmission (TX/RX) related functions.
8 Copyright (C) 2005 Martin Langer <martin-langer@gmx.de>
9 Copyright (C) 2005 Stefano Brivio <stefano.brivio@polimi.it>
10 Copyright (C) 2005, 2006 Michael Buesch <m@bues.ch>
11 Copyright (C) 2005 Danny van Dyk <kugelfang@gentoo.org>
12 Copyright (C) 2005 Andreas Jaggi <andreas.jaggi@waterwave.ch>
13 Copyright (C) 2007 Larry Finger <Larry.Finger@lwfinger.net>
26 /* Extract the bitrate out of a CCK PLCP header. */
27 static u8
b43legacy_plcp_get_bitrate_idx_cck(struct b43legacy_plcp_hdr6
*plcp
)
29 switch (plcp
->raw
[0]) {
43 /* Extract the bitrate out of an OFDM PLCP header. */
44 static u8
b43legacy_plcp_get_bitrate_idx_ofdm(struct b43legacy_plcp_hdr6
*plcp
,
47 int base
= aphy
? 0 : 4;
49 switch (plcp
->raw
[0] & 0xF) {
71 u8
b43legacy_plcp_get_ratecode_cck(const u8 bitrate
)
74 case B43legacy_CCK_RATE_1MB
:
76 case B43legacy_CCK_RATE_2MB
:
78 case B43legacy_CCK_RATE_5MB
:
80 case B43legacy_CCK_RATE_11MB
:
87 u8
b43legacy_plcp_get_ratecode_ofdm(const u8 bitrate
)
90 case B43legacy_OFDM_RATE_6MB
:
92 case B43legacy_OFDM_RATE_9MB
:
94 case B43legacy_OFDM_RATE_12MB
:
96 case B43legacy_OFDM_RATE_18MB
:
98 case B43legacy_OFDM_RATE_24MB
:
100 case B43legacy_OFDM_RATE_36MB
:
102 case B43legacy_OFDM_RATE_48MB
:
104 case B43legacy_OFDM_RATE_54MB
:
111 void b43legacy_generate_plcp_hdr(struct b43legacy_plcp_hdr4
*plcp
,
112 const u16 octets
, const u8 bitrate
)
114 __le32
*data
= &(plcp
->data
);
115 __u8
*raw
= plcp
->raw
;
117 if (b43legacy_is_ofdm_rate(bitrate
)) {
120 d
= b43legacy_plcp_get_ratecode_ofdm(bitrate
);
121 B43legacy_WARN_ON(octets
& 0xF000);
123 *data
= cpu_to_le32(d
);
127 plen
= octets
* 16 / bitrate
;
128 if ((octets
* 16 % bitrate
) > 0) {
130 if ((bitrate
== B43legacy_CCK_RATE_11MB
)
131 && ((octets
* 8 % 11) < 4))
137 *data
|= cpu_to_le32(plen
<< 16);
138 raw
[0] = b43legacy_plcp_get_ratecode_cck(bitrate
);
142 static u8
b43legacy_calc_fallback_rate(u8 bitrate
)
145 case B43legacy_CCK_RATE_1MB
:
146 return B43legacy_CCK_RATE_1MB
;
147 case B43legacy_CCK_RATE_2MB
:
148 return B43legacy_CCK_RATE_1MB
;
149 case B43legacy_CCK_RATE_5MB
:
150 return B43legacy_CCK_RATE_2MB
;
151 case B43legacy_CCK_RATE_11MB
:
152 return B43legacy_CCK_RATE_5MB
;
153 case B43legacy_OFDM_RATE_6MB
:
154 return B43legacy_CCK_RATE_5MB
;
155 case B43legacy_OFDM_RATE_9MB
:
156 return B43legacy_OFDM_RATE_6MB
;
157 case B43legacy_OFDM_RATE_12MB
:
158 return B43legacy_OFDM_RATE_9MB
;
159 case B43legacy_OFDM_RATE_18MB
:
160 return B43legacy_OFDM_RATE_12MB
;
161 case B43legacy_OFDM_RATE_24MB
:
162 return B43legacy_OFDM_RATE_18MB
;
163 case B43legacy_OFDM_RATE_36MB
:
164 return B43legacy_OFDM_RATE_24MB
;
165 case B43legacy_OFDM_RATE_48MB
:
166 return B43legacy_OFDM_RATE_36MB
;
167 case B43legacy_OFDM_RATE_54MB
:
168 return B43legacy_OFDM_RATE_48MB
;
174 static int generate_txhdr_fw3(struct b43legacy_wldev
*dev
,
175 struct b43legacy_txhdr_fw3
*txhdr
,
176 const unsigned char *fragment_data
,
177 unsigned int fragment_len
,
178 struct ieee80211_tx_info
*info
,
181 const struct ieee80211_hdr
*wlhdr
;
182 int use_encryption
= !!info
->control
.hw_key
;
184 struct ieee80211_rate
*rate_fb
;
187 unsigned int plcp_fragment_len
;
190 struct ieee80211_rate
*tx_rate
;
191 struct ieee80211_tx_rate
*rates
;
193 wlhdr
= (const struct ieee80211_hdr
*)fragment_data
;
195 memset(txhdr
, 0, sizeof(*txhdr
));
197 tx_rate
= ieee80211_get_tx_rate(dev
->wl
->hw
, info
);
199 rate
= tx_rate
->hw_value
;
200 rate_ofdm
= b43legacy_is_ofdm_rate(rate
);
201 rate_fb
= ieee80211_get_alt_retry_rate(dev
->wl
->hw
, info
, 0) ? : tx_rate
;
202 rate_fb_ofdm
= b43legacy_is_ofdm_rate(rate_fb
->hw_value
);
204 txhdr
->mac_frame_ctl
= wlhdr
->frame_control
;
205 memcpy(txhdr
->tx_receiver
, wlhdr
->addr1
, ETH_ALEN
);
207 /* Calculate duration for fallback rate */
208 if ((rate_fb
->hw_value
== rate
) ||
209 (wlhdr
->duration_id
& cpu_to_le16(0x8000)) ||
210 (wlhdr
->duration_id
== cpu_to_le16(0))) {
211 /* If the fallback rate equals the normal rate or the
212 * dur_id field contains an AID, CFP magic or 0,
213 * use the original dur_id field. */
214 txhdr
->dur_fb
= wlhdr
->duration_id
;
216 txhdr
->dur_fb
= ieee80211_generic_frame_duration(dev
->wl
->hw
,
223 plcp_fragment_len
= fragment_len
+ FCS_LEN
;
224 if (use_encryption
) {
225 u8 key_idx
= info
->control
.hw_key
->hw_key_idx
;
226 struct b43legacy_key
*key
;
230 B43legacy_WARN_ON(key_idx
>= dev
->max_nr_keys
);
231 key
= &(dev
->key
[key_idx
]);
234 /* Hardware appends ICV. */
235 plcp_fragment_len
+= info
->control
.hw_key
->icv_len
;
237 key_idx
= b43legacy_kidx_to_fw(dev
, key_idx
);
238 mac_ctl
|= (key_idx
<< B43legacy_TX4_MAC_KEYIDX_SHIFT
) &
239 B43legacy_TX4_MAC_KEYIDX
;
240 mac_ctl
|= (key
->algorithm
<<
241 B43legacy_TX4_MAC_KEYALG_SHIFT
) &
242 B43legacy_TX4_MAC_KEYALG
;
243 wlhdr_len
= ieee80211_hdrlen(wlhdr
->frame_control
);
244 iv_len
= min_t(size_t, info
->control
.hw_key
->iv_len
,
245 ARRAY_SIZE(txhdr
->iv
));
246 memcpy(txhdr
->iv
, ((u8
*)wlhdr
) + wlhdr_len
, iv_len
);
248 /* This key is invalid. This might only happen
249 * in a short timeframe after machine resume before
250 * we were able to reconfigure keys.
251 * Drop this packet completely. Do not transmit it
252 * unencrypted to avoid leaking information. */
256 b43legacy_generate_plcp_hdr((struct b43legacy_plcp_hdr4
*)
257 (&txhdr
->plcp
), plcp_fragment_len
,
259 b43legacy_generate_plcp_hdr(&txhdr
->plcp_fb
, plcp_fragment_len
,
262 /* PHY TX Control word */
264 phy_ctl
|= B43legacy_TX4_PHY_ENC_OFDM
;
265 if (info
->control
.rates
[0].flags
& IEEE80211_TX_RC_USE_SHORT_PREAMBLE
)
266 phy_ctl
|= B43legacy_TX4_PHY_SHORTPRMBL
;
267 phy_ctl
|= B43legacy_TX4_PHY_ANTLAST
;
270 rates
= info
->control
.rates
;
271 if (!(info
->flags
& IEEE80211_TX_CTL_NO_ACK
))
272 mac_ctl
|= B43legacy_TX4_MAC_ACK
;
273 if (info
->flags
& IEEE80211_TX_CTL_ASSIGN_SEQ
)
274 mac_ctl
|= B43legacy_TX4_MAC_HWSEQ
;
275 if (info
->flags
& IEEE80211_TX_CTL_FIRST_FRAGMENT
)
276 mac_ctl
|= B43legacy_TX4_MAC_STMSDU
;
278 mac_ctl
|= B43legacy_TX4_MAC_FALLBACKOFDM
;
280 /* Overwrite rates[0].count to make the retry calculation
281 * in the tx status easier. need the actual retry limit to
282 * detect whether the fallback rate was used.
284 if ((rates
[0].flags
& IEEE80211_TX_RC_USE_RTS_CTS
) ||
285 (rates
[0].count
<= dev
->wl
->hw
->conf
.long_frame_max_tx_count
)) {
286 rates
[0].count
= dev
->wl
->hw
->conf
.long_frame_max_tx_count
;
287 mac_ctl
|= B43legacy_TX4_MAC_LONGFRAME
;
289 rates
[0].count
= dev
->wl
->hw
->conf
.short_frame_max_tx_count
;
292 /* Generate the RTS or CTS-to-self frame */
293 if ((rates
[0].flags
& IEEE80211_TX_RC_USE_RTS_CTS
) ||
294 (rates
[0].flags
& IEEE80211_TX_RC_USE_CTS_PROTECT
)) {
296 struct ieee80211_hdr
*hdr
;
299 int rts_rate_fb_ofdm
;
301 rts_rate
= ieee80211_get_rts_cts_rate(dev
->wl
->hw
, info
)->hw_value
;
302 rts_rate_fb
= b43legacy_calc_fallback_rate(rts_rate
);
303 rts_rate_fb_ofdm
= b43legacy_is_ofdm_rate(rts_rate_fb
);
304 if (rts_rate_fb_ofdm
)
305 mac_ctl
|= B43legacy_TX4_MAC_CTSFALLBACKOFDM
;
307 if (rates
[0].flags
& IEEE80211_TX_RC_USE_CTS_PROTECT
) {
308 ieee80211_ctstoself_get(dev
->wl
->hw
,
312 (struct ieee80211_cts
*)
314 mac_ctl
|= B43legacy_TX4_MAC_SENDCTS
;
315 len
= sizeof(struct ieee80211_cts
);
317 ieee80211_rts_get(dev
->wl
->hw
,
319 fragment_data
, fragment_len
, info
,
320 (struct ieee80211_rts
*)
322 mac_ctl
|= B43legacy_TX4_MAC_SENDRTS
;
323 len
= sizeof(struct ieee80211_rts
);
326 b43legacy_generate_plcp_hdr((struct b43legacy_plcp_hdr4
*)
329 b43legacy_generate_plcp_hdr(&txhdr
->rts_plcp_fb
,
331 hdr
= (struct ieee80211_hdr
*)(&txhdr
->rts_frame
);
332 txhdr
->rts_dur_fb
= hdr
->duration_id
;
336 txhdr
->cookie
= cpu_to_le16(cookie
);
338 /* Apply the bitfields */
339 txhdr
->mac_ctl
= cpu_to_le32(mac_ctl
);
340 txhdr
->phy_ctl
= cpu_to_le16(phy_ctl
);
345 int b43legacy_generate_txhdr(struct b43legacy_wldev
*dev
,
347 const unsigned char *fragment_data
,
348 unsigned int fragment_len
,
349 struct ieee80211_tx_info
*info
,
352 return generate_txhdr_fw3(dev
, (struct b43legacy_txhdr_fw3
*)txhdr
,
353 fragment_data
, fragment_len
,
357 static s8
b43legacy_rssi_postprocess(struct b43legacy_wldev
*dev
,
358 u8 in_rssi
, int ofdm
,
359 int adjust_2053
, int adjust_2050
)
361 struct b43legacy_phy
*phy
= &dev
->phy
;
364 switch (phy
->radio_ver
) {
377 if (dev
->dev
->bus
->sprom
.boardflags_lo
378 & B43legacy_BFL_RSSI
) {
381 tmp
= phy
->nrssi_lt
[in_rssi
];
393 if (phy
->type
== B43legacy_PHYTYPE_G
&&
418 void b43legacy_rx(struct b43legacy_wldev
*dev
,
422 struct ieee80211_rx_status status
;
423 struct b43legacy_plcp_hdr6
*plcp
;
424 struct ieee80211_hdr
*wlhdr
;
425 const struct b43legacy_rxhdr_fw3
*rxhdr
= _rxhdr
;
436 memset(&status
, 0, sizeof(status
));
438 /* Get metadata about the frame from the header. */
439 phystat0
= le16_to_cpu(rxhdr
->phy_status0
);
440 phystat3
= le16_to_cpu(rxhdr
->phy_status3
);
442 macstat
= le16_to_cpu(rxhdr
->mac_status
);
443 mactime
= le16_to_cpu(rxhdr
->mac_time
);
444 chanstat
= le16_to_cpu(rxhdr
->channel
);
446 if (macstat
& B43legacy_RX_MAC_FCSERR
)
447 dev
->wl
->ieee_stats
.dot11FCSErrorCount
++;
449 /* Skip PLCP and padding */
450 padding
= (macstat
& B43legacy_RX_MAC_PADDING
) ? 2 : 0;
451 if (unlikely(skb
->len
< (sizeof(struct b43legacy_plcp_hdr6
) +
453 b43legacydbg(dev
->wl
, "RX: Packet size underrun (1)\n");
456 plcp
= (struct b43legacy_plcp_hdr6
*)(skb
->data
+ padding
);
457 skb_pull(skb
, sizeof(struct b43legacy_plcp_hdr6
) + padding
);
458 /* The skb contains the Wireless Header + payload data now */
459 if (unlikely(skb
->len
< (2+2+6/*minimum hdr*/ + FCS_LEN
))) {
460 b43legacydbg(dev
->wl
, "RX: Packet size underrun (2)\n");
463 wlhdr
= (struct ieee80211_hdr
*)(skb
->data
);
464 fctl
= wlhdr
->frame_control
;
466 if ((macstat
& B43legacy_RX_MAC_DEC
) &&
467 !(macstat
& B43legacy_RX_MAC_DECERR
)) {
473 keyidx
= ((macstat
& B43legacy_RX_MAC_KEYIDX
)
474 >> B43legacy_RX_MAC_KEYIDX_SHIFT
);
475 /* We must adjust the key index here. We want the "physical"
476 * key index, but the ucode passed it slightly different.
478 keyidx
= b43legacy_kidx_to_raw(dev
, keyidx
);
479 B43legacy_WARN_ON(keyidx
>= dev
->max_nr_keys
);
481 if (dev
->key
[keyidx
].algorithm
!= B43legacy_SEC_ALGO_NONE
) {
482 /* Remove PROTECTED flag to mark it as decrypted. */
483 B43legacy_WARN_ON(!ieee80211_has_protected(fctl
));
484 fctl
&= ~cpu_to_le16(IEEE80211_FCTL_PROTECTED
);
485 wlhdr
->frame_control
= fctl
;
487 wlhdr_len
= ieee80211_hdrlen(fctl
);
488 if (unlikely(skb
->len
< (wlhdr_len
+ 3))) {
489 b43legacydbg(dev
->wl
, "RX: Packet size"
493 if (skb
->data
[wlhdr_len
+ 3] & (1 << 5)) {
494 /* The Ext-IV Bit is set in the "KeyID"
503 if (unlikely(skb
->len
< (wlhdr_len
+ iv_len
+
505 b43legacydbg(dev
->wl
, "RX: Packet size"
510 memmove(skb
->data
+ iv_len
, skb
->data
, wlhdr_len
);
511 skb_pull(skb
, iv_len
);
513 skb_trim(skb
, skb
->len
- icv_len
);
515 status
.flag
|= RX_FLAG_DECRYPTED
;
519 status
.signal
= b43legacy_rssi_postprocess(dev
, jssi
,
520 (phystat0
& B43legacy_RX_PHYST0_OFDM
),
521 (phystat0
& B43legacy_RX_PHYST0_GAINCTL
),
522 (phystat3
& B43legacy_RX_PHYST3_TRSTATE
));
523 /* change to support A PHY */
524 if (phystat0
& B43legacy_RX_PHYST0_OFDM
)
525 status
.rate_idx
= b43legacy_plcp_get_bitrate_idx_ofdm(plcp
, false);
527 status
.rate_idx
= b43legacy_plcp_get_bitrate_idx_cck(plcp
);
528 status
.antenna
= !!(phystat0
& B43legacy_RX_PHYST0_ANT
);
531 * All frames on monitor interfaces and beacons always need a full
532 * 64-bit timestamp. Monitor interfaces need it for diagnostic
533 * purposes and beacons for IBSS merging.
534 * This code assumes we get to process the packet within 16 bits
535 * of timestamp, i.e. about 65 milliseconds after the PHY received
538 if (ieee80211_is_beacon(fctl
) || dev
->wl
->radiotap_enabled
) {
541 b43legacy_tsf_read(dev
, &status
.mactime
);
542 low_mactime_now
= status
.mactime
;
543 status
.mactime
= status
.mactime
& ~0xFFFFULL
;
544 status
.mactime
+= mactime
;
545 if (low_mactime_now
<= mactime
)
546 status
.mactime
-= 0x10000;
547 status
.flag
|= RX_FLAG_MACTIME_START
;
550 chanid
= (chanstat
& B43legacy_RX_CHAN_ID
) >>
551 B43legacy_RX_CHAN_ID_SHIFT
;
552 switch (chanstat
& B43legacy_RX_CHAN_PHYTYPE
) {
553 case B43legacy_PHYTYPE_B
:
554 case B43legacy_PHYTYPE_G
:
555 status
.band
= NL80211_BAND_2GHZ
;
556 status
.freq
= chanid
+ 2400;
559 b43legacywarn(dev
->wl
, "Unexpected value for chanstat (0x%X)\n",
564 memcpy(IEEE80211_SKB_RXCB(skb
), &status
, sizeof(status
));
565 ieee80211_rx_irqsafe(dev
->wl
->hw
, skb
);
569 b43legacydbg(dev
->wl
, "RX: Packet dropped\n");
570 dev_kfree_skb_any(skb
);
573 void b43legacy_handle_txstatus(struct b43legacy_wldev
*dev
,
574 const struct b43legacy_txstatus
*status
)
576 b43legacy_debugfs_log_txstat(dev
, status
);
578 if (status
->intermediate
)
580 if (status
->for_ampdu
)
583 dev
->wl
->ieee_stats
.dot11ACKFailureCount
++;
584 if (status
->rts_count
) {
585 if (status
->rts_count
== 0xF) /* FIXME */
586 dev
->wl
->ieee_stats
.dot11RTSFailureCount
++;
588 dev
->wl
->ieee_stats
.dot11RTSSuccessCount
++;
591 if (b43legacy_using_pio(dev
))
592 b43legacy_pio_handle_txstatus(dev
, status
);
594 b43legacy_dma_handle_txstatus(dev
, status
);
597 /* Handle TX status report as received through DMA/PIO queues */
598 void b43legacy_handle_hwtxstatus(struct b43legacy_wldev
*dev
,
599 const struct b43legacy_hwtxstatus
*hw
)
601 struct b43legacy_txstatus status
;
604 status
.cookie
= le16_to_cpu(hw
->cookie
);
605 status
.seq
= le16_to_cpu(hw
->seq
);
606 status
.phy_stat
= hw
->phy_stat
;
608 status
.frame_count
= (tmp
>> 4);
609 status
.rts_count
= (tmp
& 0x0F);
610 tmp
= hw
->flags
<< 1;
611 status
.supp_reason
= ((tmp
& 0x1C) >> 2);
612 status
.pm_indicated
= !!(tmp
& 0x80);
613 status
.intermediate
= !!(tmp
& 0x40);
614 status
.for_ampdu
= !!(tmp
& 0x20);
615 status
.acked
= !!(tmp
& 0x02);
617 b43legacy_handle_txstatus(dev
, &status
);
620 /* Stop any TX operation on the device (suspend the hardware queues) */
621 void b43legacy_tx_suspend(struct b43legacy_wldev
*dev
)
623 if (b43legacy_using_pio(dev
))
624 b43legacy_pio_freeze_txqueues(dev
);
626 b43legacy_dma_tx_suspend(dev
);
629 /* Resume any TX operation on the device (resume the hardware queues) */
630 void b43legacy_tx_resume(struct b43legacy_wldev
*dev
)
632 if (b43legacy_using_pio(dev
))
633 b43legacy_pio_thaw_txqueues(dev
);
635 b43legacy_dma_tx_resume(dev
);
638 /* Initialize the QoS parameters */
639 void b43legacy_qos_init(struct b43legacy_wldev
*dev
)
641 /* FIXME: This function must probably be called from the mac80211
642 * config callback. */
645 b43legacy_hf_write(dev
, b43legacy_hf_read(dev
) | B43legacy_HF_EDCF
);
646 /* FIXME kill magic */
647 b43legacy_write16(dev
, 0x688,
648 b43legacy_read16(dev
, 0x688) | 0x4);
651 /*TODO: We might need some stack support here to get the values. */