blob: 51d68971f6faa096a904ba928d147876405eeca6 [file] [log] [blame]
Michael Buesche4d6b792007-09-18 15:39:42 -04001/*
2
3 Broadcom B43 wireless driver
4
5 Transmission (TX/RX) related functions.
6
7 Copyright (C) 2005 Martin Langer <martin-langer@gmx.de>
Stefano Brivio1f21ad22007-11-06 22:49:20 +01008 Copyright (C) 2005 Stefano Brivio <stefano.brivio@polimi.it>
Michael Buesche4d6b792007-09-18 15:39:42 -04009 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
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.
17
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.
22
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.
27
28*/
29
Michael Buesch88499ab2009-10-09 20:33:32 +020030#include "xmit.h"
Michael Bueschef1a6282008-08-27 18:53:02 +020031#include "phy_common.h"
Michael Buesche4d6b792007-09-18 15:39:42 -040032#include "dma.h"
Michael Buesch5100d5a2008-03-29 21:01:16 +010033#include "pio.h"
Michael Buesch03b29772007-12-26 14:41:30 +010034
Michael Buesche4d6b792007-09-18 15:39:42 -040035
Johannes Berg8318d782008-01-24 19:38:38 +010036/* Extract the bitrate index out of a CCK PLCP header. */
37static int b43_plcp_get_bitrate_idx_cck(struct b43_plcp_hdr6 *plcp)
Michael Buesche4d6b792007-09-18 15:39:42 -040038{
39 switch (plcp->raw[0]) {
40 case 0x0A:
Johannes Berg8318d782008-01-24 19:38:38 +010041 return 0;
Michael Buesche4d6b792007-09-18 15:39:42 -040042 case 0x14:
Johannes Berg8318d782008-01-24 19:38:38 +010043 return 1;
Michael Buesche4d6b792007-09-18 15:39:42 -040044 case 0x37:
Johannes Berg8318d782008-01-24 19:38:38 +010045 return 2;
Michael Buesche4d6b792007-09-18 15:39:42 -040046 case 0x6E:
Johannes Berg8318d782008-01-24 19:38:38 +010047 return 3;
Michael Buesche4d6b792007-09-18 15:39:42 -040048 }
Johannes Berg8318d782008-01-24 19:38:38 +010049 return -1;
Michael Buesche4d6b792007-09-18 15:39:42 -040050}
51
Johannes Berg8318d782008-01-24 19:38:38 +010052/* Extract the bitrate index out of an OFDM PLCP header. */
Lorenzo Navaa3c0b872009-03-22 19:15:41 +010053static int b43_plcp_get_bitrate_idx_ofdm(struct b43_plcp_hdr6 *plcp, bool aphy)
Michael Buesche4d6b792007-09-18 15:39:42 -040054{
Johannes Berg8318d782008-01-24 19:38:38 +010055 int base = aphy ? 0 : 4;
56
Michael Buesche4d6b792007-09-18 15:39:42 -040057 switch (plcp->raw[0] & 0xF) {
58 case 0xB:
Johannes Berg8318d782008-01-24 19:38:38 +010059 return base + 0;
Michael Buesche4d6b792007-09-18 15:39:42 -040060 case 0xF:
Johannes Berg8318d782008-01-24 19:38:38 +010061 return base + 1;
Michael Buesche4d6b792007-09-18 15:39:42 -040062 case 0xA:
Johannes Berg8318d782008-01-24 19:38:38 +010063 return base + 2;
Michael Buesche4d6b792007-09-18 15:39:42 -040064 case 0xE:
Johannes Berg8318d782008-01-24 19:38:38 +010065 return base + 3;
Michael Buesche4d6b792007-09-18 15:39:42 -040066 case 0x9:
Johannes Berg8318d782008-01-24 19:38:38 +010067 return base + 4;
Michael Buesche4d6b792007-09-18 15:39:42 -040068 case 0xD:
Johannes Berg8318d782008-01-24 19:38:38 +010069 return base + 5;
Michael Buesche4d6b792007-09-18 15:39:42 -040070 case 0x8:
Johannes Berg8318d782008-01-24 19:38:38 +010071 return base + 6;
Michael Buesche4d6b792007-09-18 15:39:42 -040072 case 0xC:
Johannes Berg8318d782008-01-24 19:38:38 +010073 return base + 7;
Michael Buesche4d6b792007-09-18 15:39:42 -040074 }
Johannes Berg8318d782008-01-24 19:38:38 +010075 return -1;
Michael Buesche4d6b792007-09-18 15:39:42 -040076}
77
78u8 b43_plcp_get_ratecode_cck(const u8 bitrate)
79{
80 switch (bitrate) {
81 case B43_CCK_RATE_1MB:
82 return 0x0A;
83 case B43_CCK_RATE_2MB:
84 return 0x14;
85 case B43_CCK_RATE_5MB:
86 return 0x37;
87 case B43_CCK_RATE_11MB:
88 return 0x6E;
89 }
90 B43_WARN_ON(1);
91 return 0;
92}
93
94u8 b43_plcp_get_ratecode_ofdm(const u8 bitrate)
95{
96 switch (bitrate) {
97 case B43_OFDM_RATE_6MB:
98 return 0xB;
99 case B43_OFDM_RATE_9MB:
100 return 0xF;
101 case B43_OFDM_RATE_12MB:
102 return 0xA;
103 case B43_OFDM_RATE_18MB:
104 return 0xE;
105 case B43_OFDM_RATE_24MB:
106 return 0x9;
107 case B43_OFDM_RATE_36MB:
108 return 0xD;
109 case B43_OFDM_RATE_48MB:
110 return 0x8;
111 case B43_OFDM_RATE_54MB:
112 return 0xC;
113 }
114 B43_WARN_ON(1);
115 return 0;
116}
117
118void b43_generate_plcp_hdr(struct b43_plcp_hdr4 *plcp,
119 const u16 octets, const u8 bitrate)
120{
Michael Buesche4d6b792007-09-18 15:39:42 -0400121 __u8 *raw = plcp->raw;
122
123 if (b43_is_ofdm_rate(bitrate)) {
Michael Buesch1a094042007-09-20 11:13:40 -0700124 u32 d;
125
126 d = b43_plcp_get_ratecode_ofdm(bitrate);
Michael Buesche4d6b792007-09-18 15:39:42 -0400127 B43_WARN_ON(octets & 0xF000);
Michael Buesch1a094042007-09-20 11:13:40 -0700128 d |= (octets << 5);
Matthieu CASTETb52a0332009-06-04 23:18:33 +0200129 plcp->data = cpu_to_le32(d);
Michael Buesche4d6b792007-09-18 15:39:42 -0400130 } else {
131 u32 plen;
132
133 plen = octets * 16 / bitrate;
134 if ((octets * 16 % bitrate) > 0) {
135 plen++;
136 if ((bitrate == B43_CCK_RATE_11MB)
137 && ((octets * 8 % 11) < 4)) {
138 raw[1] = 0x84;
139 } else
140 raw[1] = 0x04;
141 } else
142 raw[1] = 0x04;
Matthieu CASTETb52a0332009-06-04 23:18:33 +0200143 plcp->data |= cpu_to_le32(plen << 16);
Michael Buesche4d6b792007-09-18 15:39:42 -0400144 raw[0] = b43_plcp_get_ratecode_cck(bitrate);
145 }
146}
147
148static u8 b43_calc_fallback_rate(u8 bitrate)
149{
150 switch (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;
175 }
176 B43_WARN_ON(1);
177 return 0;
178}
179
Michael Buescheb189d8b2008-01-28 14:47:41 -0800180/* Generate a TX data header. */
Michael Buesch09552cc2008-01-23 21:44:15 +0100181int b43_generate_txhdr(struct b43_wldev *dev,
182 u8 *_txhdr,
gregor kowski035d0242009-08-19 22:35:45 +0200183 struct sk_buff *skb_frag,
Johannes Berge6a98542008-10-21 12:40:02 +0200184 struct ieee80211_tx_info *info,
Michael Buesch09552cc2008-01-23 21:44:15 +0100185 u16 cookie)
Michael Buesche4d6b792007-09-18 15:39:42 -0400186{
gregor kowski035d0242009-08-19 22:35:45 +0200187 const unsigned char *fragment_data = skb_frag->data;
188 unsigned int fragment_len = skb_frag->len;
Michael Buescheb189d8b2008-01-28 14:47:41 -0800189 struct b43_txhdr *txhdr = (struct b43_txhdr *)_txhdr;
Michael Buesche4d6b792007-09-18 15:39:42 -0400190 const struct b43_phy *phy = &dev->phy;
191 const struct ieee80211_hdr *wlhdr =
192 (const struct ieee80211_hdr *)fragment_data;
Johannes Bergd0f09802008-07-29 11:32:07 +0200193 int use_encryption = !!info->control.hw_key;
Harvey Harrisonf37d9232008-06-14 23:33:39 -0700194 __le16 fctl = wlhdr->frame_control;
Johannes Berg8318d782008-01-24 19:38:38 +0100195 struct ieee80211_rate *fbrate;
Michael Buesche4d6b792007-09-18 15:39:42 -0400196 u8 rate, rate_fb;
197 int rate_ofdm, rate_fb_ofdm;
198 unsigned int plcp_fragment_len;
199 u32 mac_ctl = 0;
200 u16 phy_ctl = 0;
201 u8 extra_ft = 0;
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200202 struct ieee80211_rate *txrate;
Johannes Berge6a98542008-10-21 12:40:02 +0200203 struct ieee80211_tx_rate *rates;
Michael Buesche4d6b792007-09-18 15:39:42 -0400204
205 memset(txhdr, 0, sizeof(*txhdr));
206
Johannes Berge039fa42008-05-15 12:55:29 +0200207 txrate = ieee80211_get_tx_rate(dev->wl->hw, info);
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200208 rate = txrate ? txrate->hw_value : B43_CCK_RATE_1MB;
Michael Buesche4d6b792007-09-18 15:39:42 -0400209 rate_ofdm = b43_is_ofdm_rate(rate);
Felix Fietkau870abdf2008-10-05 18:04:24 +0200210 fbrate = ieee80211_get_alt_retry_rate(dev->wl->hw, info, 0) ? : txrate;
Johannes Berg8318d782008-01-24 19:38:38 +0100211 rate_fb = fbrate->hw_value;
Michael Buesche4d6b792007-09-18 15:39:42 -0400212 rate_fb_ofdm = b43_is_ofdm_rate(rate_fb);
213
214 if (rate_ofdm)
215 txhdr->phy_rate = b43_plcp_get_ratecode_ofdm(rate);
216 else
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);
220
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;
229 } else {
Johannes Berge039fa42008-05-15 12:55:29 +0200230 txhdr->dur_fb = ieee80211_generic_frame_duration(
231 dev->wl->hw, info->control.vif, fragment_len, fbrate);
Michael Buesche4d6b792007-09-18 15:39:42 -0400232 }
233
234 plcp_fragment_len = fragment_len + FCS_LEN;
235 if (use_encryption) {
Johannes Berge039fa42008-05-15 12:55:29 +0200236 u8 key_idx = info->control.hw_key->hw_key_idx;
Michael Buesche4d6b792007-09-18 15:39:42 -0400237 struct b43_key *key;
238 int wlhdr_len;
239 size_t iv_len;
240
Michael Buesch66d2d082009-08-06 10:36:50 +0200241 B43_WARN_ON(key_idx >= ARRAY_SIZE(dev->key));
Michael Buesche4d6b792007-09-18 15:39:42 -0400242 key = &(dev->key[key_idx]);
Michael Buesche4d6b792007-09-18 15:39:42 -0400243
Michael Buesch09552cc2008-01-23 21:44:15 +0100244 if (unlikely(!key->keyconf)) {
245 /* This key is invalid. This might only happen
246 * in a short timeframe after machine resume before
247 * we were able to reconfigure keys.
248 * Drop this packet completely. Do not transmit it
249 * unencrypted to avoid leaking information. */
250 return -ENOKEY;
Michael Buesch7be1bb62008-01-23 21:10:56 +0100251 }
Michael Buesch09552cc2008-01-23 21:44:15 +0100252
253 /* Hardware appends ICV. */
Felix Fietkau76708de2008-10-05 18:02:48 +0200254 plcp_fragment_len += info->control.hw_key->icv_len;
Michael Buesch09552cc2008-01-23 21:44:15 +0100255
256 key_idx = b43_kidx_to_fw(dev, key_idx);
257 mac_ctl |= (key_idx << B43_TXH_MAC_KEYIDX_SHIFT) &
258 B43_TXH_MAC_KEYIDX;
259 mac_ctl |= (key->algorithm << B43_TXH_MAC_KEYALG_SHIFT) &
260 B43_TXH_MAC_KEYALG;
Harvey Harrisonf37d9232008-06-14 23:33:39 -0700261 wlhdr_len = ieee80211_hdrlen(fctl);
gregor kowski035d0242009-08-19 22:35:45 +0200262 if (key->algorithm == B43_SEC_ALGO_TKIP) {
263 u16 phase1key[5];
264 int i;
265 /* we give the phase1key and iv16 here, the key is stored in
266 * shm. With that the hardware can do phase 2 and encryption.
267 */
268 ieee80211_get_tkip_key(info->control.hw_key, skb_frag,
269 IEEE80211_TKIP_P1_KEY, (u8*)phase1key);
Michael Bueschcde1b552009-09-06 16:18:58 +0200270 /* phase1key is in host endian. Copy to little-endian txhdr->iv. */
271 for (i = 0; i < 5; i++) {
272 txhdr->iv[i * 2 + 0] = phase1key[i];
273 txhdr->iv[i * 2 + 1] = phase1key[i] >> 8;
274 }
gregor kowski035d0242009-08-19 22:35:45 +0200275 /* iv16 */
276 memcpy(txhdr->iv + 10, ((u8 *) wlhdr) + wlhdr_len, 3);
277 } else {
278 iv_len = min((size_t) info->control.hw_key->iv_len,
279 ARRAY_SIZE(txhdr->iv));
280 memcpy(txhdr->iv, ((u8 *) wlhdr) + wlhdr_len, iv_len);
281 }
Michael Buesche4d6b792007-09-18 15:39:42 -0400282 }
Michael Buescheb189d8b2008-01-28 14:47:41 -0800283 if (b43_is_old_txhdr_format(dev)) {
284 b43_generate_plcp_hdr((struct b43_plcp_hdr4 *)(&txhdr->old_format.plcp),
285 plcp_fragment_len, rate);
286 } else {
287 b43_generate_plcp_hdr((struct b43_plcp_hdr4 *)(&txhdr->new_format.plcp),
288 plcp_fragment_len, rate);
289 }
Michael Buesche4d6b792007-09-18 15:39:42 -0400290 b43_generate_plcp_hdr((struct b43_plcp_hdr4 *)(&txhdr->plcp_fb),
291 plcp_fragment_len, rate_fb);
292
293 /* Extra Frame Types */
294 if (rate_fb_ofdm)
Michael Buescheb189d8b2008-01-28 14:47:41 -0800295 extra_ft |= B43_TXH_EFT_FB_OFDM;
296 else
297 extra_ft |= B43_TXH_EFT_FB_CCK;
Michael Buesche4d6b792007-09-18 15:39:42 -0400298
299 /* Set channel radio code. Note that the micrcode ORs 0x100 to
300 * this value before comparing it to the value in SHM, if this
301 * is a 5Ghz packet.
302 */
303 txhdr->chan_radio_code = phy->channel;
304
305 /* PHY TX Control word */
306 if (rate_ofdm)
Michael Buescheb189d8b2008-01-28 14:47:41 -0800307 phy_ctl |= B43_TXH_PHY_ENC_OFDM;
308 else
309 phy_ctl |= B43_TXH_PHY_ENC_CCK;
Johannes Berge6a98542008-10-21 12:40:02 +0200310 if (info->control.rates[0].flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
Michael Buescheb189d8b2008-01-28 14:47:41 -0800311 phy_ctl |= B43_TXH_PHY_SHORTPRMBL;
Michael Buesch9db1f6d2007-12-22 21:54:20 +0100312
Johannes Berge039fa42008-05-15 12:55:29 +0200313 switch (b43_ieee80211_antenna_sanitize(dev, info->antenna_sel_tx)) {
Michael Buesch9db1f6d2007-12-22 21:54:20 +0100314 case 0: /* Default */
Michael Buescheb189d8b2008-01-28 14:47:41 -0800315 phy_ctl |= B43_TXH_PHY_ANT01AUTO;
Michael Buesche4d6b792007-09-18 15:39:42 -0400316 break;
Michael Buesch9db1f6d2007-12-22 21:54:20 +0100317 case 1: /* Antenna 0 */
Michael Buescheb189d8b2008-01-28 14:47:41 -0800318 phy_ctl |= B43_TXH_PHY_ANT0;
Michael Buesche4d6b792007-09-18 15:39:42 -0400319 break;
Michael Buesch9db1f6d2007-12-22 21:54:20 +0100320 case 2: /* Antenna 1 */
Michael Buescheb189d8b2008-01-28 14:47:41 -0800321 phy_ctl |= B43_TXH_PHY_ANT1;
322 break;
323 case 3: /* Antenna 2 */
324 phy_ctl |= B43_TXH_PHY_ANT2;
325 break;
326 case 4: /* Antenna 3 */
327 phy_ctl |= B43_TXH_PHY_ANT3;
Michael Buesche4d6b792007-09-18 15:39:42 -0400328 break;
329 default:
330 B43_WARN_ON(1);
331 }
332
Johannes Berge6a98542008-10-21 12:40:02 +0200333 rates = info->control.rates;
Michael Buesche4d6b792007-09-18 15:39:42 -0400334 /* MAC control */
Johannes Berge039fa42008-05-15 12:55:29 +0200335 if (!(info->flags & IEEE80211_TX_CTL_NO_ACK))
Michael Buescheb189d8b2008-01-28 14:47:41 -0800336 mac_ctl |= B43_TXH_MAC_ACK;
Johannes Bergf591fa52008-07-10 11:21:26 +0200337 /* use hardware sequence counter as the non-TID counter */
338 if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ)
Michael Buescheb189d8b2008-01-28 14:47:41 -0800339 mac_ctl |= B43_TXH_MAC_HWSEQ;
Johannes Berge039fa42008-05-15 12:55:29 +0200340 if (info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT)
Michael Buescheb189d8b2008-01-28 14:47:41 -0800341 mac_ctl |= B43_TXH_MAC_STMSDU;
Michael Buesche4d6b792007-09-18 15:39:42 -0400342 if (phy->type == B43_PHYTYPE_A)
Michael Buescheb189d8b2008-01-28 14:47:41 -0800343 mac_ctl |= B43_TXH_MAC_5GHZ;
Johannes Berge6a98542008-10-21 12:40:02 +0200344
345 /* Overwrite rates[0].count to make the retry calculation
346 * in the tx status easier. need the actual retry limit to
347 * detect whether the fallback rate was used.
348 */
349 if ((rates[0].flags & IEEE80211_TX_RC_USE_RTS_CTS) ||
350 (rates[0].count <= dev->wl->hw->conf.long_frame_max_tx_count)) {
351 rates[0].count = dev->wl->hw->conf.long_frame_max_tx_count;
Michael Buescheb189d8b2008-01-28 14:47:41 -0800352 mac_ctl |= B43_TXH_MAC_LONGFRAME;
Johannes Berge6a98542008-10-21 12:40:02 +0200353 } else {
354 rates[0].count = dev->wl->hw->conf.short_frame_max_tx_count;
355 }
Michael Buesche4d6b792007-09-18 15:39:42 -0400356
357 /* Generate the RTS or CTS-to-self frame */
Johannes Berge6a98542008-10-21 12:40:02 +0200358 if ((rates[0].flags & IEEE80211_TX_RC_USE_RTS_CTS) ||
359 (rates[0].flags & IEEE80211_TX_RC_USE_CTS_PROTECT)) {
Michael Buesche4d6b792007-09-18 15:39:42 -0400360 unsigned int len;
361 struct ieee80211_hdr *hdr;
362 int rts_rate, rts_rate_fb;
363 int rts_rate_ofdm, rts_rate_fb_ofdm;
Michael Buescheb189d8b2008-01-28 14:47:41 -0800364 struct b43_plcp_hdr6 *plcp;
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200365 struct ieee80211_rate *rts_cts_rate;
Michael Buesche4d6b792007-09-18 15:39:42 -0400366
Johannes Berge039fa42008-05-15 12:55:29 +0200367 rts_cts_rate = ieee80211_get_rts_cts_rate(dev->wl->hw, info);
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200368
369 rts_rate = rts_cts_rate ? rts_cts_rate->hw_value : B43_CCK_RATE_1MB;
Michael Buesche4d6b792007-09-18 15:39:42 -0400370 rts_rate_ofdm = b43_is_ofdm_rate(rts_rate);
371 rts_rate_fb = b43_calc_fallback_rate(rts_rate);
372 rts_rate_fb_ofdm = b43_is_ofdm_rate(rts_rate_fb);
373
Johannes Berge6a98542008-10-21 12:40:02 +0200374 if (rates[0].flags & IEEE80211_TX_RC_USE_CTS_PROTECT) {
Michael Buescheb189d8b2008-01-28 14:47:41 -0800375 struct ieee80211_cts *cts;
376
377 if (b43_is_old_txhdr_format(dev)) {
378 cts = (struct ieee80211_cts *)
379 (txhdr->old_format.rts_frame);
380 } else {
381 cts = (struct ieee80211_cts *)
382 (txhdr->new_format.rts_frame);
383 }
Johannes Berge039fa42008-05-15 12:55:29 +0200384 ieee80211_ctstoself_get(dev->wl->hw, info->control.vif,
Michael Buesche4d6b792007-09-18 15:39:42 -0400385 fragment_data, fragment_len,
Johannes Berge039fa42008-05-15 12:55:29 +0200386 info, cts);
Michael Buescheb189d8b2008-01-28 14:47:41 -0800387 mac_ctl |= B43_TXH_MAC_SENDCTS;
Michael Buesche4d6b792007-09-18 15:39:42 -0400388 len = sizeof(struct ieee80211_cts);
389 } else {
Michael Buescheb189d8b2008-01-28 14:47:41 -0800390 struct ieee80211_rts *rts;
391
392 if (b43_is_old_txhdr_format(dev)) {
393 rts = (struct ieee80211_rts *)
394 (txhdr->old_format.rts_frame);
395 } else {
396 rts = (struct ieee80211_rts *)
397 (txhdr->new_format.rts_frame);
398 }
Johannes Berge039fa42008-05-15 12:55:29 +0200399 ieee80211_rts_get(dev->wl->hw, info->control.vif,
Michael Buescheb189d8b2008-01-28 14:47:41 -0800400 fragment_data, fragment_len,
Johannes Berge039fa42008-05-15 12:55:29 +0200401 info, rts);
Michael Buescheb189d8b2008-01-28 14:47:41 -0800402 mac_ctl |= B43_TXH_MAC_SENDRTS;
Michael Buesche4d6b792007-09-18 15:39:42 -0400403 len = sizeof(struct ieee80211_rts);
404 }
405 len += FCS_LEN;
Michael Buescheb189d8b2008-01-28 14:47:41 -0800406
407 /* Generate the PLCP headers for the RTS/CTS frame */
408 if (b43_is_old_txhdr_format(dev))
409 plcp = &txhdr->old_format.rts_plcp;
410 else
411 plcp = &txhdr->new_format.rts_plcp;
412 b43_generate_plcp_hdr((struct b43_plcp_hdr4 *)plcp,
413 len, rts_rate);
414 plcp = &txhdr->rts_plcp_fb;
415 b43_generate_plcp_hdr((struct b43_plcp_hdr4 *)plcp,
Michael Buesche4d6b792007-09-18 15:39:42 -0400416 len, rts_rate_fb);
Michael Buescheb189d8b2008-01-28 14:47:41 -0800417
418 if (b43_is_old_txhdr_format(dev)) {
419 hdr = (struct ieee80211_hdr *)
420 (&txhdr->old_format.rts_frame);
421 } else {
422 hdr = (struct ieee80211_hdr *)
423 (&txhdr->new_format.rts_frame);
424 }
Michael Buesche4d6b792007-09-18 15:39:42 -0400425 txhdr->rts_dur_fb = hdr->duration_id;
Michael Buescheb189d8b2008-01-28 14:47:41 -0800426
Michael Buesche4d6b792007-09-18 15:39:42 -0400427 if (rts_rate_ofdm) {
Michael Buescheb189d8b2008-01-28 14:47:41 -0800428 extra_ft |= B43_TXH_EFT_RTS_OFDM;
Michael Buesche4d6b792007-09-18 15:39:42 -0400429 txhdr->phy_rate_rts =
430 b43_plcp_get_ratecode_ofdm(rts_rate);
Michael Buescheb189d8b2008-01-28 14:47:41 -0800431 } else {
432 extra_ft |= B43_TXH_EFT_RTS_CCK;
Michael Buesche4d6b792007-09-18 15:39:42 -0400433 txhdr->phy_rate_rts =
434 b43_plcp_get_ratecode_cck(rts_rate);
Michael Buescheb189d8b2008-01-28 14:47:41 -0800435 }
Michael Buesche4d6b792007-09-18 15:39:42 -0400436 if (rts_rate_fb_ofdm)
Michael Buescheb189d8b2008-01-28 14:47:41 -0800437 extra_ft |= B43_TXH_EFT_RTSFB_OFDM;
438 else
439 extra_ft |= B43_TXH_EFT_RTSFB_CCK;
Michael Buesche4d6b792007-09-18 15:39:42 -0400440 }
441
442 /* Magic cookie */
Michael Buescheb189d8b2008-01-28 14:47:41 -0800443 if (b43_is_old_txhdr_format(dev))
444 txhdr->old_format.cookie = cpu_to_le16(cookie);
445 else
446 txhdr->new_format.cookie = cpu_to_le16(cookie);
Michael Buesche4d6b792007-09-18 15:39:42 -0400447
448 /* Apply the bitfields */
449 txhdr->mac_ctl = cpu_to_le32(mac_ctl);
450 txhdr->phy_ctl = cpu_to_le16(phy_ctl);
451 txhdr->extra_ft = extra_ft;
Michael Buesche4d6b792007-09-18 15:39:42 -0400452
Michael Buesch09552cc2008-01-23 21:44:15 +0100453 return 0;
Michael Buesche4d6b792007-09-18 15:39:42 -0400454}
455
456static s8 b43_rssi_postprocess(struct b43_wldev *dev,
457 u8 in_rssi, int ofdm,
458 int adjust_2053, int adjust_2050)
459{
460 struct b43_phy *phy = &dev->phy;
Michael Bueschef1a6282008-08-27 18:53:02 +0200461 struct b43_phy_g *gphy = phy->g;
Michael Buesche4d6b792007-09-18 15:39:42 -0400462 s32 tmp;
463
464 switch (phy->radio_ver) {
465 case 0x2050:
466 if (ofdm) {
467 tmp = in_rssi;
468 if (tmp > 127)
469 tmp -= 256;
470 tmp *= 73;
471 tmp /= 64;
472 if (adjust_2050)
473 tmp += 25;
474 else
475 tmp -= 3;
476 } else {
Larry Finger95de2842007-11-09 16:57:18 -0600477 if (dev->dev->bus->sprom.
Michael Buesche4d6b792007-09-18 15:39:42 -0400478 boardflags_lo & B43_BFL_RSSI) {
479 if (in_rssi > 63)
480 in_rssi = 63;
Michael Bueschef1a6282008-08-27 18:53:02 +0200481 B43_WARN_ON(phy->type != B43_PHYTYPE_G);
482 tmp = gphy->nrssi_lt[in_rssi];
Michael Buesche4d6b792007-09-18 15:39:42 -0400483 tmp = 31 - tmp;
484 tmp *= -131;
485 tmp /= 128;
486 tmp -= 57;
487 } else {
488 tmp = in_rssi;
489 tmp = 31 - tmp;
490 tmp *= -149;
491 tmp /= 128;
492 tmp -= 68;
493 }
494 if (phy->type == B43_PHYTYPE_G && adjust_2050)
495 tmp += 25;
496 }
497 break;
498 case 0x2060:
499 if (in_rssi > 127)
500 tmp = in_rssi - 256;
501 else
502 tmp = in_rssi;
503 break;
504 default:
505 tmp = in_rssi;
506 tmp -= 11;
507 tmp *= 103;
508 tmp /= 64;
509 if (adjust_2053)
510 tmp -= 109;
511 else
512 tmp -= 83;
513 }
514
515 return (s8) tmp;
516}
517
518//TODO
519#if 0
520static s8 b43_rssinoise_postprocess(struct b43_wldev *dev, u8 in_rssi)
521{
522 struct b43_phy *phy = &dev->phy;
523 s8 ret;
524
525 if (phy->type == B43_PHYTYPE_A) {
526 //TODO: Incomplete specs.
527 ret = 0;
528 } else
529 ret = b43_rssi_postprocess(dev, in_rssi, 0, 1, 1);
530
531 return ret;
532}
533#endif
534
535void b43_rx(struct b43_wldev *dev, struct sk_buff *skb, const void *_rxhdr)
536{
537 struct ieee80211_rx_status status;
538 struct b43_plcp_hdr6 *plcp;
539 struct ieee80211_hdr *wlhdr;
540 const struct b43_rxhdr_fw4 *rxhdr = _rxhdr;
Harvey Harrisonf37d9232008-06-14 23:33:39 -0700541 __le16 fctl;
Michael Buesche4d6b792007-09-18 15:39:42 -0400542 u16 phystat0, phystat3, chanstat, mactime;
543 u32 macstat;
544 u16 chanid;
Johannes Berg8318d782008-01-24 19:38:38 +0100545 u16 phytype;
Michael Buesche4d6b792007-09-18 15:39:42 -0400546 int padding;
547
548 memset(&status, 0, sizeof(status));
549
550 /* Get metadata about the frame from the header. */
551 phystat0 = le16_to_cpu(rxhdr->phy_status0);
552 phystat3 = le16_to_cpu(rxhdr->phy_status3);
Michael Buesche4d6b792007-09-18 15:39:42 -0400553 macstat = le32_to_cpu(rxhdr->mac_status);
554 mactime = le16_to_cpu(rxhdr->mac_time);
555 chanstat = le16_to_cpu(rxhdr->channel);
Johannes Berg8318d782008-01-24 19:38:38 +0100556 phytype = chanstat & B43_RX_CHAN_PHYTYPE;
Michael Buesche4d6b792007-09-18 15:39:42 -0400557
Michael Bueschce4fbdb2009-03-02 23:18:37 +0100558 if (unlikely(macstat & B43_RX_MAC_FCSERR)) {
Michael Buesche4d6b792007-09-18 15:39:42 -0400559 dev->wl->ieee_stats.dot11FCSErrorCount++;
Michael Bueschce4fbdb2009-03-02 23:18:37 +0100560 status.flag |= RX_FLAG_FAILED_FCS_CRC;
561 }
562 if (unlikely(phystat0 & (B43_RX_PHYST0_PLCPHCF | B43_RX_PHYST0_PLCPFV)))
563 status.flag |= RX_FLAG_FAILED_PLCP_CRC;
564 if (phystat0 & B43_RX_PHYST0_SHORTPRMBL)
565 status.flag |= RX_FLAG_SHORTPRE;
Michael Buesche4d6b792007-09-18 15:39:42 -0400566 if (macstat & B43_RX_MAC_DECERR) {
567 /* Decryption with the given key failed.
568 * Drop the packet. We also won't be able to decrypt it with
569 * the key in software. */
570 goto drop;
571 }
572
573 /* Skip PLCP and padding */
574 padding = (macstat & B43_RX_MAC_PADDING) ? 2 : 0;
575 if (unlikely(skb->len < (sizeof(struct b43_plcp_hdr6) + padding))) {
576 b43dbg(dev->wl, "RX: Packet size underrun (1)\n");
577 goto drop;
578 }
579 plcp = (struct b43_plcp_hdr6 *)(skb->data + padding);
580 skb_pull(skb, sizeof(struct b43_plcp_hdr6) + padding);
581 /* The skb contains the Wireless Header + payload data now */
582 if (unlikely(skb->len < (2 + 2 + 6 /*minimum hdr */ + FCS_LEN))) {
583 b43dbg(dev->wl, "RX: Packet size underrun (2)\n");
584 goto drop;
585 }
586 wlhdr = (struct ieee80211_hdr *)(skb->data);
Harvey Harrisonf37d9232008-06-14 23:33:39 -0700587 fctl = wlhdr->frame_control;
Michael Buesche4d6b792007-09-18 15:39:42 -0400588
589 if (macstat & B43_RX_MAC_DEC) {
590 unsigned int keyidx;
591 int wlhdr_len;
592
593 keyidx = ((macstat & B43_RX_MAC_KEYIDX)
594 >> B43_RX_MAC_KEYIDX_SHIFT);
595 /* We must adjust the key index here. We want the "physical"
596 * key index, but the ucode passed it slightly different.
597 */
598 keyidx = b43_kidx_to_raw(dev, keyidx);
Michael Buesch66d2d082009-08-06 10:36:50 +0200599 B43_WARN_ON(keyidx >= ARRAY_SIZE(dev->key));
Michael Buesche4d6b792007-09-18 15:39:42 -0400600
601 if (dev->key[keyidx].algorithm != B43_SEC_ALGO_NONE) {
Harvey Harrisonf37d9232008-06-14 23:33:39 -0700602 wlhdr_len = ieee80211_hdrlen(fctl);
Michael Buesche4d6b792007-09-18 15:39:42 -0400603 if (unlikely(skb->len < (wlhdr_len + 3))) {
604 b43dbg(dev->wl,
605 "RX: Packet size underrun (3)\n");
606 goto drop;
607 }
608 status.flag |= RX_FLAG_DECRYPTED;
609 }
610 }
611
Michael Buesch7b584162008-04-03 18:01:12 +0200612 /* Link quality statistics */
Michael Buesche4d6b792007-09-18 15:39:42 -0400613 status.noise = dev->stats.link_noise;
Michael Buesch7b584162008-04-03 18:01:12 +0200614 if ((chanstat & B43_RX_CHAN_PHYTYPE) == B43_PHYTYPE_N) {
615// s8 rssi = max(rxhdr->power0, rxhdr->power1);
616 //TODO: Find out what the rssi value is (dBm or percentage?)
617 // and also find out what the maximum possible value is.
618 // Fill status.ssi and status.signal fields.
619 } else {
Bruno Randolf566bfe52008-05-08 19:15:40 +0200620 status.signal = b43_rssi_postprocess(dev, rxhdr->jssi,
Michael Buesch7b584162008-04-03 18:01:12 +0200621 (phystat0 & B43_RX_PHYST0_OFDM),
622 (phystat0 & B43_RX_PHYST0_GAINCTL),
623 (phystat3 & B43_RX_PHYST3_TRSTATE));
Bruno Randolf566bfe52008-05-08 19:15:40 +0200624 status.qual = (rxhdr->jssi * 100) / B43_RX_MAX_SSI;
Michael Buesch7b584162008-04-03 18:01:12 +0200625 }
626
Michael Buesche4d6b792007-09-18 15:39:42 -0400627 if (phystat0 & B43_RX_PHYST0_OFDM)
Johannes Berg8318d782008-01-24 19:38:38 +0100628 status.rate_idx = b43_plcp_get_bitrate_idx_ofdm(plcp,
629 phytype == B43_PHYTYPE_A);
Michael Buesche4d6b792007-09-18 15:39:42 -0400630 else
Johannes Berg8318d782008-01-24 19:38:38 +0100631 status.rate_idx = b43_plcp_get_bitrate_idx_cck(plcp);
Michael Bueschce4fbdb2009-03-02 23:18:37 +0100632 if (unlikely(status.rate_idx == -1)) {
633 /* PLCP seems to be corrupted.
634 * Drop the frame, if we are not interested in corrupted frames. */
635 if (!(dev->wl->filter_flags & FIF_PLCPFAIL))
636 goto drop;
637 }
Michael Buesche4d6b792007-09-18 15:39:42 -0400638 status.antenna = !!(phystat0 & B43_RX_PHYST0_ANT);
John W. Linvillec0ddd042008-01-21 13:41:18 -0500639
640 /*
Johannes Bergd007b7f2008-02-18 18:53:55 +0100641 * All frames on monitor interfaces and beacons always need a full
642 * 64-bit timestamp. Monitor interfaces need it for diagnostic
643 * purposes and beacons for IBSS merging.
644 * This code assumes we get to process the packet within 16 bits
645 * of timestamp, i.e. about 65 milliseconds after the PHY received
646 * the first symbol.
John W. Linvillec0ddd042008-01-21 13:41:18 -0500647 */
Harvey Harrisonf37d9232008-06-14 23:33:39 -0700648 if (ieee80211_is_beacon(fctl) || dev->wl->radiotap_enabled) {
John W. Linvillec0ddd042008-01-21 13:41:18 -0500649 u16 low_mactime_now;
650
651 b43_tsf_read(dev, &status.mactime);
652 low_mactime_now = status.mactime;
653 status.mactime = status.mactime & ~0xFFFFULL;
654 status.mactime += mactime;
655 if (low_mactime_now <= mactime)
656 status.mactime -= 0x10000;
657 status.flag |= RX_FLAG_TSFT;
658 }
Michael Buesche4d6b792007-09-18 15:39:42 -0400659
660 chanid = (chanstat & B43_RX_CHAN_ID) >> B43_RX_CHAN_ID_SHIFT;
661 switch (chanstat & B43_RX_CHAN_PHYTYPE) {
662 case B43_PHYTYPE_A:
Johannes Berg8318d782008-01-24 19:38:38 +0100663 status.band = IEEE80211_BAND_5GHZ;
Michael Bueschd9871602008-01-02 18:55:53 +0100664 B43_WARN_ON(1);
665 /* FIXME: We don't really know which value the "chanid" contains.
666 * So the following assignment might be wrong. */
Johannes Berg8318d782008-01-24 19:38:38 +0100667 status.freq = b43_channel_to_freq_5ghz(chanid);
Michael Buesche4d6b792007-09-18 15:39:42 -0400668 break;
669 case B43_PHYTYPE_G:
Johannes Berg8318d782008-01-24 19:38:38 +0100670 status.band = IEEE80211_BAND_2GHZ;
Michael Bueschd9871602008-01-02 18:55:53 +0100671 /* chanid is the radio channel cookie value as used
672 * to tune the radio. */
Michael Buesche4d6b792007-09-18 15:39:42 -0400673 status.freq = chanid + 2400;
Michael Bueschd9871602008-01-02 18:55:53 +0100674 break;
675 case B43_PHYTYPE_N:
Gábor Stefanik826ee702009-08-15 00:52:02 +0200676 case B43_PHYTYPE_LP:
Michael Bueschd9871602008-01-02 18:55:53 +0100677 /* chanid is the SHM channel cookie. Which is the plain
678 * channel number in b43. */
Johannes Berg8318d782008-01-24 19:38:38 +0100679 if (chanstat & B43_RX_CHAN_5GHZ) {
680 status.band = IEEE80211_BAND_5GHZ;
681 status.freq = b43_freq_to_channel_5ghz(chanid);
682 } else {
683 status.band = IEEE80211_BAND_2GHZ;
684 status.freq = b43_freq_to_channel_2ghz(chanid);
685 }
Michael Buesche4d6b792007-09-18 15:39:42 -0400686 break;
687 default:
688 B43_WARN_ON(1);
Michael Bueschd9871602008-01-02 18:55:53 +0100689 goto drop;
Michael Buesche4d6b792007-09-18 15:39:42 -0400690 }
691
Johannes Bergf1d58c22009-06-17 13:13:00 +0200692 memcpy(IEEE80211_SKB_RXCB(skb), &status, sizeof(status));
Johannes Bergedbfdcc2009-10-11 12:19:21 +0200693
694 local_bh_disable();
Michael Bueschce6c4a12009-09-10 20:22:02 +0200695 ieee80211_rx(dev->wl->hw, skb);
Johannes Bergedbfdcc2009-10-11 12:19:21 +0200696 local_bh_enable();
Michael Buesche4d6b792007-09-18 15:39:42 -0400697
Michael Buesch990b86f2009-09-12 00:48:03 +0200698#if B43_DEBUG
699 dev->rx_count++;
700#endif
Michael Buesche4d6b792007-09-18 15:39:42 -0400701 return;
702drop:
703 b43dbg(dev->wl, "RX: Packet dropped\n");
704 dev_kfree_skb_any(skb);
705}
706
707void b43_handle_txstatus(struct b43_wldev *dev,
708 const struct b43_txstatus *status)
709{
710 b43_debugfs_log_txstat(dev, status);
711
712 if (status->intermediate)
713 return;
714 if (status->for_ampdu)
715 return;
716 if (!status->acked)
717 dev->wl->ieee_stats.dot11ACKFailureCount++;
718 if (status->rts_count) {
719 if (status->rts_count == 0xF) //FIXME
720 dev->wl->ieee_stats.dot11RTSFailureCount++;
721 else
722 dev->wl->ieee_stats.dot11RTSSuccessCount++;
723 }
724
Michael Buesch5100d5a2008-03-29 21:01:16 +0100725 if (b43_using_pio_transfers(dev))
726 b43_pio_handle_txstatus(dev, status);
727 else
728 b43_dma_handle_txstatus(dev, status);
Michael Buesch18c8ade2008-08-28 19:33:40 +0200729
730 b43_phy_txpower_check(dev, 0);
Michael Buesche4d6b792007-09-18 15:39:42 -0400731}
732
Michael Buesch5100d5a2008-03-29 21:01:16 +0100733/* Fill out the mac80211 TXstatus report based on the b43-specific
734 * txstatus report data. This returns a boolean whether the frame was
735 * successfully transmitted. */
Johannes Berge6a98542008-10-21 12:40:02 +0200736bool b43_fill_txstatus_report(struct b43_wldev *dev,
737 struct ieee80211_tx_info *report,
Michael Buesch5100d5a2008-03-29 21:01:16 +0100738 const struct b43_txstatus *status)
Michael Buesche4d6b792007-09-18 15:39:42 -0400739{
Michael Buesch5100d5a2008-03-29 21:01:16 +0100740 bool frame_success = 1;
Johannes Berge6a98542008-10-21 12:40:02 +0200741 int retry_limit;
742
743 /* preserve the confiured retry limit before clearing the status
744 * The xmit function has overwritten the rc's value with the actual
745 * retry limit done by the hardware */
746 retry_limit = report->status.rates[0].count;
747 ieee80211_tx_info_clear_status(report);
Michael Buesche4d6b792007-09-18 15:39:42 -0400748
Michael Buesch5100d5a2008-03-29 21:01:16 +0100749 if (status->acked) {
750 /* The frame was ACKed. */
Johannes Berge039fa42008-05-15 12:55:29 +0200751 report->flags |= IEEE80211_TX_STAT_ACK;
Michael Buesch5100d5a2008-03-29 21:01:16 +0100752 } else {
753 /* The frame was not ACKed... */
Johannes Berge039fa42008-05-15 12:55:29 +0200754 if (!(report->flags & IEEE80211_TX_CTL_NO_ACK)) {
Michael Buesch5100d5a2008-03-29 21:01:16 +0100755 /* ...but we expected an ACK. */
756 frame_success = 0;
Michael Buesch5100d5a2008-03-29 21:01:16 +0100757 }
758 }
759 if (status->frame_count == 0) {
760 /* The frame was not transmitted at all. */
Johannes Berge6a98542008-10-21 12:40:02 +0200761 report->status.rates[0].count = 0;
762 } else if (status->rts_count > dev->wl->hw->conf.short_frame_max_tx_count) {
763 /*
764 * If the short retries (RTS, not data frame) have exceeded
765 * the limit, the hw will not have tried the selected rate,
766 * but will have used the fallback rate instead.
767 * Don't let the rate control count attempts for the selected
768 * rate in this case, otherwise the statistics will be off.
769 */
770 report->status.rates[0].count = 0;
771 report->status.rates[1].count = status->frame_count;
772 } else {
773 if (status->frame_count > retry_limit) {
774 report->status.rates[0].count = retry_limit;
775 report->status.rates[1].count = status->frame_count -
776 retry_limit;
777
778 } else {
779 report->status.rates[0].count = status->frame_count;
780 report->status.rates[1].idx = -1;
781 }
782 }
Michael Buesche4d6b792007-09-18 15:39:42 -0400783
Michael Buesch5100d5a2008-03-29 21:01:16 +0100784 return frame_success;
Michael Buesche4d6b792007-09-18 15:39:42 -0400785}
786
787/* Stop any TX operation on the device (suspend the hardware queues) */
788void b43_tx_suspend(struct b43_wldev *dev)
789{
Michael Buesch5100d5a2008-03-29 21:01:16 +0100790 if (b43_using_pio_transfers(dev))
791 b43_pio_tx_suspend(dev);
792 else
793 b43_dma_tx_suspend(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -0400794}
795
796/* Resume any TX operation on the device (resume the hardware queues) */
797void b43_tx_resume(struct b43_wldev *dev)
798{
Michael Buesch5100d5a2008-03-29 21:01:16 +0100799 if (b43_using_pio_transfers(dev))
800 b43_pio_tx_resume(dev);
801 else
802 b43_dma_tx_resume(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -0400803}