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.
35 /* Extract the bitrate out of a CCK PLCP header. */
36 static u8
b43_plcp_get_bitrate_cck(struct b43_plcp_hdr6
*plcp
)
38 switch (plcp
->raw
[0]) {
40 return B43_CCK_RATE_1MB
;
42 return B43_CCK_RATE_2MB
;
44 return B43_CCK_RATE_5MB
;
46 return B43_CCK_RATE_11MB
;
52 /* Extract the bitrate out of an OFDM PLCP header. */
53 static u8
b43_plcp_get_bitrate_ofdm(struct b43_plcp_hdr6
*plcp
)
55 switch (plcp
->raw
[0] & 0xF) {
57 return B43_OFDM_RATE_6MB
;
59 return B43_OFDM_RATE_9MB
;
61 return B43_OFDM_RATE_12MB
;
63 return B43_OFDM_RATE_18MB
;
65 return B43_OFDM_RATE_24MB
;
67 return B43_OFDM_RATE_36MB
;
69 return B43_OFDM_RATE_48MB
;
71 return B43_OFDM_RATE_54MB
;
77 u8
b43_plcp_get_ratecode_cck(const u8 bitrate
)
80 case B43_CCK_RATE_1MB
:
82 case B43_CCK_RATE_2MB
:
84 case B43_CCK_RATE_5MB
:
86 case B43_CCK_RATE_11MB
:
93 u8
b43_plcp_get_ratecode_ofdm(const u8 bitrate
)
96 case B43_OFDM_RATE_6MB
:
98 case B43_OFDM_RATE_9MB
:
100 case B43_OFDM_RATE_12MB
:
102 case B43_OFDM_RATE_18MB
:
104 case B43_OFDM_RATE_24MB
:
106 case B43_OFDM_RATE_36MB
:
108 case B43_OFDM_RATE_48MB
:
110 case B43_OFDM_RATE_54MB
:
117 void b43_generate_plcp_hdr(struct b43_plcp_hdr4
*plcp
,
118 const u16 octets
, const u8 bitrate
)
120 __le32
*data
= &(plcp
->data
);
121 __u8
*raw
= plcp
->raw
;
123 if (b43_is_ofdm_rate(bitrate
)) {
126 d
= b43_plcp_get_ratecode_ofdm(bitrate
);
127 B43_WARN_ON(octets
& 0xF000);
129 *data
= cpu_to_le32(d
);
133 plen
= octets
* 16 / bitrate
;
134 if ((octets
* 16 % bitrate
) > 0) {
136 if ((bitrate
== B43_CCK_RATE_11MB
)
137 && ((octets
* 8 % 11) < 4)) {
143 *data
|= cpu_to_le32(plen
<< 16);
144 raw
[0] = b43_plcp_get_ratecode_cck(bitrate
);
148 static u8
b43_calc_fallback_rate(u8 bitrate
)
151 case B43_CCK_RATE_1MB
:
152 return B43_CCK_RATE_1MB
;
153 case B43_CCK_RATE_2MB
:
154 return B43_CCK_RATE_1MB
;
155 case B43_CCK_RATE_5MB
:
156 return B43_CCK_RATE_2MB
;
157 case B43_CCK_RATE_11MB
:
158 return B43_CCK_RATE_5MB
;
159 case B43_OFDM_RATE_6MB
:
160 return B43_CCK_RATE_5MB
;
161 case B43_OFDM_RATE_9MB
:
162 return B43_OFDM_RATE_6MB
;
163 case B43_OFDM_RATE_12MB
:
164 return B43_OFDM_RATE_9MB
;
165 case B43_OFDM_RATE_18MB
:
166 return B43_OFDM_RATE_12MB
;
167 case B43_OFDM_RATE_24MB
:
168 return B43_OFDM_RATE_18MB
;
169 case B43_OFDM_RATE_36MB
:
170 return B43_OFDM_RATE_24MB
;
171 case B43_OFDM_RATE_48MB
:
172 return B43_OFDM_RATE_36MB
;
173 case B43_OFDM_RATE_54MB
:
174 return B43_OFDM_RATE_48MB
;
180 static void generate_txhdr_fw4(struct b43_wldev
*dev
,
181 struct b43_txhdr_fw4
*txhdr
,
182 const unsigned char *fragment_data
,
183 unsigned int fragment_len
,
184 const struct ieee80211_tx_control
*txctl
,
187 const struct b43_phy
*phy
= &dev
->phy
;
188 const struct ieee80211_hdr
*wlhdr
=
189 (const struct ieee80211_hdr
*)fragment_data
;
190 int use_encryption
= (!(txctl
->flags
& IEEE80211_TXCTL_DO_NOT_ENCRYPT
));
191 u16 fctl
= le16_to_cpu(wlhdr
->frame_control
);
193 int rate_ofdm
, rate_fb_ofdm
;
194 unsigned int plcp_fragment_len
;
199 memset(txhdr
, 0, sizeof(*txhdr
));
201 rate
= txctl
->tx_rate
;
202 rate_ofdm
= b43_is_ofdm_rate(rate
);
203 rate_fb
= (txctl
->alt_retry_rate
== -1) ? rate
: txctl
->alt_retry_rate
;
204 rate_fb_ofdm
= b43_is_ofdm_rate(rate_fb
);
207 txhdr
->phy_rate
= b43_plcp_get_ratecode_ofdm(rate
);
209 txhdr
->phy_rate
= b43_plcp_get_ratecode_cck(rate
);
210 txhdr
->mac_frame_ctl
= wlhdr
->frame_control
;
211 memcpy(txhdr
->tx_receiver
, wlhdr
->addr1
, 6);
213 /* Calculate duration for fallback rate */
214 if ((rate_fb
== rate
) ||
215 (wlhdr
->duration_id
& cpu_to_le16(0x8000)) ||
216 (wlhdr
->duration_id
== cpu_to_le16(0))) {
217 /* If the fallback rate equals the normal rate or the
218 * dur_id field contains an AID, CFP magic or 0,
219 * use the original dur_id field. */
220 txhdr
->dur_fb
= wlhdr
->duration_id
;
222 int fbrate_base100kbps
= B43_RATE_TO_BASE100KBPS(rate_fb
);
223 txhdr
->dur_fb
= ieee80211_generic_frame_duration(dev
->wl
->hw
,
229 plcp_fragment_len
= fragment_len
+ FCS_LEN
;
230 if (use_encryption
) {
231 u8 key_idx
= (u16
) (txctl
->key_idx
);
236 B43_WARN_ON(key_idx
>= dev
->max_nr_keys
);
237 key
= &(dev
->key
[key_idx
]);
238 B43_WARN_ON(!key
->keyconf
);
240 /* Hardware appends ICV. */
241 plcp_fragment_len
+= txctl
->icv_len
;
243 key_idx
= b43_kidx_to_fw(dev
, key_idx
);
244 mac_ctl
|= (key_idx
<< B43_TX4_MAC_KEYIDX_SHIFT
) &
246 mac_ctl
|= (key
->algorithm
<< B43_TX4_MAC_KEYALG_SHIFT
) &
248 wlhdr_len
= ieee80211_get_hdrlen(fctl
);
249 iv_len
= min((size_t) txctl
->iv_len
,
250 ARRAY_SIZE(txhdr
->iv
));
251 memcpy(txhdr
->iv
, ((u8
*) wlhdr
) + wlhdr_len
, iv_len
);
253 b43_generate_plcp_hdr((struct b43_plcp_hdr4
*)(&txhdr
->plcp
),
254 plcp_fragment_len
, rate
);
255 b43_generate_plcp_hdr((struct b43_plcp_hdr4
*)(&txhdr
->plcp_fb
),
256 plcp_fragment_len
, rate_fb
);
258 /* Extra Frame Types */
260 extra_ft
|= B43_TX4_EFT_FBOFDM
;
262 /* Set channel radio code. Note that the micrcode ORs 0x100 to
263 * this value before comparing it to the value in SHM, if this
266 txhdr
->chan_radio_code
= phy
->channel
;
268 /* PHY TX Control word */
270 phy_ctl
|= B43_TX4_PHY_OFDM
;
271 if (dev
->short_preamble
)
272 phy_ctl
|= B43_TX4_PHY_SHORTPRMBL
;
273 switch (txctl
->antenna_sel_tx
) {
275 phy_ctl
|= B43_TX4_PHY_ANTLAST
;
278 phy_ctl
|= B43_TX4_PHY_ANT0
;
281 phy_ctl
|= B43_TX4_PHY_ANT1
;
288 if (!(txctl
->flags
& IEEE80211_TXCTL_NO_ACK
))
289 mac_ctl
|= B43_TX4_MAC_ACK
;
290 if (!(((fctl
& IEEE80211_FCTL_FTYPE
) == IEEE80211_FTYPE_CTL
) &&
291 ((fctl
& IEEE80211_FCTL_STYPE
) == IEEE80211_STYPE_PSPOLL
)))
292 mac_ctl
|= B43_TX4_MAC_HWSEQ
;
293 if (txctl
->flags
& IEEE80211_TXCTL_FIRST_FRAGMENT
)
294 mac_ctl
|= B43_TX4_MAC_STMSDU
;
295 if (phy
->type
== B43_PHYTYPE_A
)
296 mac_ctl
|= B43_TX4_MAC_5GHZ
;
297 if (txctl
->flags
& IEEE80211_TXCTL_LONG_RETRY_LIMIT
)
298 mac_ctl
|= B43_TX4_MAC_LONGFRAME
;
300 /* Generate the RTS or CTS-to-self frame */
301 if ((txctl
->flags
& IEEE80211_TXCTL_USE_RTS_CTS
) ||
302 (txctl
->flags
& IEEE80211_TXCTL_USE_CTS_PROTECT
)) {
304 struct ieee80211_hdr
*hdr
;
305 int rts_rate
, rts_rate_fb
;
306 int rts_rate_ofdm
, rts_rate_fb_ofdm
;
308 rts_rate
= txctl
->rts_cts_rate
;
309 rts_rate_ofdm
= b43_is_ofdm_rate(rts_rate
);
310 rts_rate_fb
= b43_calc_fallback_rate(rts_rate
);
311 rts_rate_fb_ofdm
= b43_is_ofdm_rate(rts_rate_fb
);
313 if (txctl
->flags
& IEEE80211_TXCTL_USE_CTS_PROTECT
) {
314 ieee80211_ctstoself_get(dev
->wl
->hw
, dev
->wl
->if_id
,
315 fragment_data
, fragment_len
,
317 (struct ieee80211_cts
*)(txhdr
->
319 mac_ctl
|= B43_TX4_MAC_SENDCTS
;
320 len
= sizeof(struct ieee80211_cts
);
322 ieee80211_rts_get(dev
->wl
->hw
, dev
->wl
->if_id
,
323 fragment_data
, fragment_len
, txctl
,
324 (struct ieee80211_rts
*)(txhdr
->
326 mac_ctl
|= B43_TX4_MAC_SENDRTS
;
327 len
= sizeof(struct ieee80211_rts
);
330 b43_generate_plcp_hdr((struct b43_plcp_hdr4
*)(&txhdr
->
333 b43_generate_plcp_hdr((struct b43_plcp_hdr4
*)(&txhdr
->
336 hdr
= (struct ieee80211_hdr
*)(&txhdr
->rts_frame
);
337 txhdr
->rts_dur_fb
= hdr
->duration_id
;
339 extra_ft
|= B43_TX4_EFT_RTSOFDM
;
340 txhdr
->phy_rate_rts
=
341 b43_plcp_get_ratecode_ofdm(rts_rate
);
343 txhdr
->phy_rate_rts
=
344 b43_plcp_get_ratecode_cck(rts_rate
);
345 if (rts_rate_fb_ofdm
)
346 extra_ft
|= B43_TX4_EFT_RTSFBOFDM
;
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
);
355 txhdr
->extra_ft
= extra_ft
;
358 void b43_generate_txhdr(struct b43_wldev
*dev
,
360 const unsigned char *fragment_data
,
361 unsigned int fragment_len
,
362 const struct ieee80211_tx_control
*txctl
, u16 cookie
)
364 generate_txhdr_fw4(dev
, (struct b43_txhdr_fw4
*)txhdr
,
365 fragment_data
, fragment_len
, txctl
, cookie
);
368 static s8
b43_rssi_postprocess(struct b43_wldev
*dev
,
369 u8 in_rssi
, int ofdm
,
370 int adjust_2053
, int adjust_2050
)
372 struct b43_phy
*phy
= &dev
->phy
;
375 switch (phy
->radio_ver
) {
388 if (dev
->dev
->bus
->sprom
.r1
.
389 boardflags_lo
& B43_BFL_RSSI
) {
392 tmp
= phy
->nrssi_lt
[in_rssi
];
404 if (phy
->type
== B43_PHYTYPE_G
&& adjust_2050
)
430 static s8
b43_rssinoise_postprocess(struct b43_wldev
*dev
, u8 in_rssi
)
432 struct b43_phy
*phy
= &dev
->phy
;
435 if (phy
->type
== B43_PHYTYPE_A
) {
436 //TODO: Incomplete specs.
439 ret
= b43_rssi_postprocess(dev
, in_rssi
, 0, 1, 1);
445 void b43_rx(struct b43_wldev
*dev
, struct sk_buff
*skb
, const void *_rxhdr
)
447 struct ieee80211_rx_status status
;
448 struct b43_plcp_hdr6
*plcp
;
449 struct ieee80211_hdr
*wlhdr
;
450 const struct b43_rxhdr_fw4
*rxhdr
= _rxhdr
;
452 u16 phystat0
, phystat3
, chanstat
, mactime
;
458 memset(&status
, 0, sizeof(status
));
460 /* Get metadata about the frame from the header. */
461 phystat0
= le16_to_cpu(rxhdr
->phy_status0
);
462 phystat3
= le16_to_cpu(rxhdr
->phy_status3
);
464 macstat
= le32_to_cpu(rxhdr
->mac_status
);
465 mactime
= le16_to_cpu(rxhdr
->mac_time
);
466 chanstat
= le16_to_cpu(rxhdr
->channel
);
468 if (macstat
& B43_RX_MAC_FCSERR
)
469 dev
->wl
->ieee_stats
.dot11FCSErrorCount
++;
470 if (macstat
& B43_RX_MAC_DECERR
) {
471 /* Decryption with the given key failed.
472 * Drop the packet. We also won't be able to decrypt it with
473 * the key in software. */
477 /* Skip PLCP and padding */
478 padding
= (macstat
& B43_RX_MAC_PADDING
) ? 2 : 0;
479 if (unlikely(skb
->len
< (sizeof(struct b43_plcp_hdr6
) + padding
))) {
480 b43dbg(dev
->wl
, "RX: Packet size underrun (1)\n");
483 plcp
= (struct b43_plcp_hdr6
*)(skb
->data
+ padding
);
484 skb_pull(skb
, sizeof(struct b43_plcp_hdr6
) + padding
);
485 /* The skb contains the Wireless Header + payload data now */
486 if (unlikely(skb
->len
< (2 + 2 + 6 /*minimum hdr */ + FCS_LEN
))) {
487 b43dbg(dev
->wl
, "RX: Packet size underrun (2)\n");
490 wlhdr
= (struct ieee80211_hdr
*)(skb
->data
);
491 fctl
= le16_to_cpu(wlhdr
->frame_control
);
492 skb_trim(skb
, skb
->len
- FCS_LEN
);
494 if (macstat
& B43_RX_MAC_DEC
) {
498 keyidx
= ((macstat
& B43_RX_MAC_KEYIDX
)
499 >> B43_RX_MAC_KEYIDX_SHIFT
);
500 /* We must adjust the key index here. We want the "physical"
501 * key index, but the ucode passed it slightly different.
503 keyidx
= b43_kidx_to_raw(dev
, keyidx
);
504 B43_WARN_ON(keyidx
>= dev
->max_nr_keys
);
506 if (dev
->key
[keyidx
].algorithm
!= B43_SEC_ALGO_NONE
) {
507 wlhdr_len
= ieee80211_get_hdrlen(fctl
);
508 if (unlikely(skb
->len
< (wlhdr_len
+ 3))) {
510 "RX: Packet size underrun (3)\n");
513 status
.flag
|= RX_FLAG_DECRYPTED
;
517 status
.ssi
= b43_rssi_postprocess(dev
, jssi
,
518 (phystat0
& B43_RX_PHYST0_OFDM
),
519 (phystat0
& B43_RX_PHYST0_GAINCTL
),
520 (phystat3
& B43_RX_PHYST3_TRSTATE
));
521 status
.noise
= dev
->stats
.link_noise
;
522 /* the next line looks wrong, but is what mac80211 wants */
523 status
.signal
= (jssi
* 100) / B43_RX_MAX_SSI
;
524 if (phystat0
& B43_RX_PHYST0_OFDM
)
525 status
.rate
= b43_plcp_get_bitrate_ofdm(plcp
);
527 status
.rate
= b43_plcp_get_bitrate_cck(plcp
);
528 status
.antenna
= !!(phystat0
& B43_RX_PHYST0_ANT
);
529 status
.mactime
= mactime
;
530 status
.flag
|= RX_FLAG_TSFT
;
532 chanid
= (chanstat
& B43_RX_CHAN_ID
) >> B43_RX_CHAN_ID_SHIFT
;
533 switch (chanstat
& B43_RX_CHAN_PHYTYPE
) {
535 status
.phymode
= MODE_IEEE80211A
;
537 /* FIXME: We don't really know which value the "chanid" contains.
538 * So the following assignment might be wrong. */
539 status
.channel
= chanid
;
540 status
.freq
= b43_channel_to_freq_5ghz(status
.channel
);
543 status
.phymode
= MODE_IEEE80211G
;
544 /* chanid is the radio channel cookie value as used
545 * to tune the radio. */
546 status
.freq
= chanid
+ 2400;
547 status
.channel
= b43_freq_to_channel_2ghz(status
.freq
);
550 status
.phymode
= 0xDEAD /*FIXME MODE_IEEE80211N*/;
551 /* chanid is the SHM channel cookie. Which is the plain
552 * channel number in b43. */
553 status
.channel
= chanid
;
554 if (chanstat
& B43_RX_CHAN_5GHZ
)
555 status
.freq
= b43_freq_to_channel_5ghz(status
.freq
);
557 status
.freq
= b43_freq_to_channel_2ghz(status
.freq
);
564 dev
->stats
.last_rx
= jiffies
;
565 ieee80211_rx_irqsafe(dev
->wl
->hw
, skb
, &status
);
569 b43dbg(dev
->wl
, "RX: Packet dropped\n");
570 dev_kfree_skb_any(skb
);
573 void b43_handle_txstatus(struct b43_wldev
*dev
,
574 const struct b43_txstatus
*status
)
576 b43_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 (b43_using_pio(dev
))
592 b43_pio_handle_txstatus(dev
, status
);
594 b43_dma_handle_txstatus(dev
, status
);
597 /* Handle TX status report as received through DMA/PIO queues */
598 void b43_handle_hwtxstatus(struct b43_wldev
*dev
,
599 const struct b43_hwtxstatus
*hw
)
601 struct b43_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);
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 b43_handle_txstatus(dev
, &status
);
620 /* Stop any TX operation on the device (suspend the hardware queues) */
621 void b43_tx_suspend(struct b43_wldev
*dev
)
623 if (b43_using_pio(dev
))
624 b43_pio_freeze_txqueues(dev
);
626 b43_dma_tx_suspend(dev
);
629 /* Resume any TX operation on the device (resume the hardware queues) */
630 void b43_tx_resume(struct b43_wldev
*dev
)
632 if (b43_using_pio(dev
))
633 b43_pio_thaw_txqueues(dev
);
635 b43_dma_tx_resume(dev
);
639 static void upload_qos_parms(struct b43_wldev
*dev
,
640 const u16
* parms
, u16 offset
)
644 for (i
= 0; i
< B43_NR_QOSPARMS
; i
++) {
645 b43_shm_write16(dev
, B43_SHM_SHARED
,
646 offset
+ (i
* 2), parms
[i
]);
651 /* Initialize the QoS parameters */
652 void b43_qos_init(struct b43_wldev
*dev
)
654 /* FIXME: This function must probably be called from the mac80211
655 * config callback. */
658 b43_hf_write(dev
, b43_hf_read(dev
) | B43_HF_EDCF
);
660 b43_write16(dev
, 0x688, b43_read16(dev
, 0x688) | 0x4);
662 /*TODO: We might need some stack support here to get the values. */