blob: 136510edf3cf819285d8f72f5cb3afff0ee3683e [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 Büscheb032b92011-07-04 20:50:05 +02009 Copyright (C) 2005, 2006 Michael Buesch <m@bues.ch>
Michael Buesche4d6b792007-09-18 15:39:42 -040010 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
Rafał Miłecki3311abb2011-02-25 12:34:11 +010035static const struct b43_tx_legacy_rate_phy_ctl_entry b43_tx_legacy_rate_phy_ctl[] = {
36 { B43_CCK_RATE_1MB, 0x0, 0x0 },
37 { B43_CCK_RATE_2MB, 0x0, 0x1 },
38 { B43_CCK_RATE_5MB, 0x0, 0x2 },
39 { B43_CCK_RATE_11MB, 0x0, 0x3 },
40 { B43_OFDM_RATE_6MB, B43_TXH_PHY1_CRATE_1_2, B43_TXH_PHY1_MODUL_BPSK },
41 { B43_OFDM_RATE_9MB, B43_TXH_PHY1_CRATE_3_4, B43_TXH_PHY1_MODUL_BPSK },
42 { B43_OFDM_RATE_12MB, B43_TXH_PHY1_CRATE_1_2, B43_TXH_PHY1_MODUL_QPSK },
43 { B43_OFDM_RATE_18MB, B43_TXH_PHY1_CRATE_3_4, B43_TXH_PHY1_MODUL_QPSK },
44 { B43_OFDM_RATE_24MB, B43_TXH_PHY1_CRATE_1_2, B43_TXH_PHY1_MODUL_QAM16 },
45 { B43_OFDM_RATE_36MB, B43_TXH_PHY1_CRATE_3_4, B43_TXH_PHY1_MODUL_QAM16 },
46 { B43_OFDM_RATE_48MB, B43_TXH_PHY1_CRATE_2_3, B43_TXH_PHY1_MODUL_QAM64 },
47 { B43_OFDM_RATE_54MB, B43_TXH_PHY1_CRATE_3_4, B43_TXH_PHY1_MODUL_QAM64 },
48};
49
50static const struct b43_tx_legacy_rate_phy_ctl_entry *
51b43_tx_legacy_rate_phy_ctl_ent(u8 bitrate)
52{
53 const struct b43_tx_legacy_rate_phy_ctl_entry *e;
54 unsigned int i;
55
56 for (i = 0; i < ARRAY_SIZE(b43_tx_legacy_rate_phy_ctl); i++) {
57 e = &(b43_tx_legacy_rate_phy_ctl[i]);
58 if (e->bitrate == bitrate)
59 return e;
60 }
61
62 B43_WARN_ON(1);
63 return NULL;
64}
Michael Buesche4d6b792007-09-18 15:39:42 -040065
Johannes Berg8318d782008-01-24 19:38:38 +010066/* Extract the bitrate index out of a CCK PLCP header. */
67static int b43_plcp_get_bitrate_idx_cck(struct b43_plcp_hdr6 *plcp)
Michael Buesche4d6b792007-09-18 15:39:42 -040068{
69 switch (plcp->raw[0]) {
70 case 0x0A:
Johannes Berg8318d782008-01-24 19:38:38 +010071 return 0;
Michael Buesche4d6b792007-09-18 15:39:42 -040072 case 0x14:
Johannes Berg8318d782008-01-24 19:38:38 +010073 return 1;
Michael Buesche4d6b792007-09-18 15:39:42 -040074 case 0x37:
Johannes Berg8318d782008-01-24 19:38:38 +010075 return 2;
Michael Buesche4d6b792007-09-18 15:39:42 -040076 case 0x6E:
Johannes Berg8318d782008-01-24 19:38:38 +010077 return 3;
Michael Buesche4d6b792007-09-18 15:39:42 -040078 }
Johannes Berg8318d782008-01-24 19:38:38 +010079 return -1;
Michael Buesche4d6b792007-09-18 15:39:42 -040080}
81
Johannes Berg8318d782008-01-24 19:38:38 +010082/* Extract the bitrate index out of an OFDM PLCP header. */
Lorenzo Navaa3c0b872009-03-22 19:15:41 +010083static int b43_plcp_get_bitrate_idx_ofdm(struct b43_plcp_hdr6 *plcp, bool aphy)
Michael Buesche4d6b792007-09-18 15:39:42 -040084{
Johannes Berg8318d782008-01-24 19:38:38 +010085 int base = aphy ? 0 : 4;
86
Michael Buesche4d6b792007-09-18 15:39:42 -040087 switch (plcp->raw[0] & 0xF) {
88 case 0xB:
Johannes Berg8318d782008-01-24 19:38:38 +010089 return base + 0;
Michael Buesche4d6b792007-09-18 15:39:42 -040090 case 0xF:
Johannes Berg8318d782008-01-24 19:38:38 +010091 return base + 1;
Michael Buesche4d6b792007-09-18 15:39:42 -040092 case 0xA:
Johannes Berg8318d782008-01-24 19:38:38 +010093 return base + 2;
Michael Buesche4d6b792007-09-18 15:39:42 -040094 case 0xE:
Johannes Berg8318d782008-01-24 19:38:38 +010095 return base + 3;
Michael Buesche4d6b792007-09-18 15:39:42 -040096 case 0x9:
Johannes Berg8318d782008-01-24 19:38:38 +010097 return base + 4;
Michael Buesche4d6b792007-09-18 15:39:42 -040098 case 0xD:
Johannes Berg8318d782008-01-24 19:38:38 +010099 return base + 5;
Michael Buesche4d6b792007-09-18 15:39:42 -0400100 case 0x8:
Johannes Berg8318d782008-01-24 19:38:38 +0100101 return base + 6;
Michael Buesche4d6b792007-09-18 15:39:42 -0400102 case 0xC:
Johannes Berg8318d782008-01-24 19:38:38 +0100103 return base + 7;
Michael Buesche4d6b792007-09-18 15:39:42 -0400104 }
Johannes Berg8318d782008-01-24 19:38:38 +0100105 return -1;
Michael Buesche4d6b792007-09-18 15:39:42 -0400106}
107
108u8 b43_plcp_get_ratecode_cck(const u8 bitrate)
109{
110 switch (bitrate) {
111 case B43_CCK_RATE_1MB:
112 return 0x0A;
113 case B43_CCK_RATE_2MB:
114 return 0x14;
115 case B43_CCK_RATE_5MB:
116 return 0x37;
117 case B43_CCK_RATE_11MB:
118 return 0x6E;
119 }
120 B43_WARN_ON(1);
121 return 0;
122}
123
124u8 b43_plcp_get_ratecode_ofdm(const u8 bitrate)
125{
126 switch (bitrate) {
127 case B43_OFDM_RATE_6MB:
128 return 0xB;
129 case B43_OFDM_RATE_9MB:
130 return 0xF;
131 case B43_OFDM_RATE_12MB:
132 return 0xA;
133 case B43_OFDM_RATE_18MB:
134 return 0xE;
135 case B43_OFDM_RATE_24MB:
136 return 0x9;
137 case B43_OFDM_RATE_36MB:
138 return 0xD;
139 case B43_OFDM_RATE_48MB:
140 return 0x8;
141 case B43_OFDM_RATE_54MB:
142 return 0xC;
143 }
144 B43_WARN_ON(1);
145 return 0;
146}
147
148void b43_generate_plcp_hdr(struct b43_plcp_hdr4 *plcp,
149 const u16 octets, const u8 bitrate)
150{
Michael Buesche4d6b792007-09-18 15:39:42 -0400151 __u8 *raw = plcp->raw;
152
153 if (b43_is_ofdm_rate(bitrate)) {
Michael Buesch1a094042007-09-20 11:13:40 -0700154 u32 d;
155
156 d = b43_plcp_get_ratecode_ofdm(bitrate);
Michael Buesche4d6b792007-09-18 15:39:42 -0400157 B43_WARN_ON(octets & 0xF000);
Michael Buesch1a094042007-09-20 11:13:40 -0700158 d |= (octets << 5);
Matthieu CASTETb52a0332009-06-04 23:18:33 +0200159 plcp->data = cpu_to_le32(d);
Michael Buesche4d6b792007-09-18 15:39:42 -0400160 } else {
161 u32 plen;
162
163 plen = octets * 16 / bitrate;
164 if ((octets * 16 % bitrate) > 0) {
165 plen++;
166 if ((bitrate == B43_CCK_RATE_11MB)
167 && ((octets * 8 % 11) < 4)) {
168 raw[1] = 0x84;
169 } else
170 raw[1] = 0x04;
171 } else
172 raw[1] = 0x04;
Matthieu CASTETb52a0332009-06-04 23:18:33 +0200173 plcp->data |= cpu_to_le32(plen << 16);
Michael Buesche4d6b792007-09-18 15:39:42 -0400174 raw[0] = b43_plcp_get_ratecode_cck(bitrate);
175 }
176}
177
Rafał Miłecki694718d2011-10-17 16:59:25 +0200178/* TODO: verify if needed for SSLPN or LCN */
Rafał Miłecki3311abb2011-02-25 12:34:11 +0100179static u16 b43_generate_tx_phy_ctl1(struct b43_wldev *dev, u8 bitrate)
180{
181 const struct b43_phy *phy = &dev->phy;
182 const struct b43_tx_legacy_rate_phy_ctl_entry *e;
183 u16 control = 0;
184 u16 bw;
185
186 if (phy->type == B43_PHYTYPE_LP)
187 bw = B43_TXH_PHY1_BW_20;
188 else /* FIXME */
189 bw = B43_TXH_PHY1_BW_20;
190
191 if (0) { /* FIXME: MIMO */
192 } else if (b43_is_cck_rate(bitrate) && phy->type != B43_PHYTYPE_LP) {
193 control = bw;
194 } else {
195 control = bw;
196 e = b43_tx_legacy_rate_phy_ctl_ent(bitrate);
197 if (e) {
198 control |= e->coding_rate;
199 control |= e->modulation;
200 }
201 control |= B43_TXH_PHY1_MODE_SISO;
202 }
203
204 return control;
205}
206
Michael Buesche4d6b792007-09-18 15:39:42 -0400207static u8 b43_calc_fallback_rate(u8 bitrate)
208{
209 switch (bitrate) {
210 case B43_CCK_RATE_1MB:
211 return B43_CCK_RATE_1MB;
212 case B43_CCK_RATE_2MB:
213 return B43_CCK_RATE_1MB;
214 case B43_CCK_RATE_5MB:
215 return B43_CCK_RATE_2MB;
216 case B43_CCK_RATE_11MB:
217 return B43_CCK_RATE_5MB;
218 case B43_OFDM_RATE_6MB:
219 return B43_CCK_RATE_5MB;
220 case B43_OFDM_RATE_9MB:
221 return B43_OFDM_RATE_6MB;
222 case B43_OFDM_RATE_12MB:
223 return B43_OFDM_RATE_9MB;
224 case B43_OFDM_RATE_18MB:
225 return B43_OFDM_RATE_12MB;
226 case B43_OFDM_RATE_24MB:
227 return B43_OFDM_RATE_18MB;
228 case B43_OFDM_RATE_36MB:
229 return B43_OFDM_RATE_24MB;
230 case B43_OFDM_RATE_48MB:
231 return B43_OFDM_RATE_36MB;
232 case B43_OFDM_RATE_54MB:
233 return B43_OFDM_RATE_48MB;
234 }
235 B43_WARN_ON(1);
236 return 0;
237}
238
Michael Buescheb189d8b2008-01-28 14:47:41 -0800239/* Generate a TX data header. */
Michael Buesch09552cc2008-01-23 21:44:15 +0100240int b43_generate_txhdr(struct b43_wldev *dev,
241 u8 *_txhdr,
gregor kowski035d0242009-08-19 22:35:45 +0200242 struct sk_buff *skb_frag,
Johannes Berge6a98542008-10-21 12:40:02 +0200243 struct ieee80211_tx_info *info,
Michael Buesch09552cc2008-01-23 21:44:15 +0100244 u16 cookie)
Michael Buesche4d6b792007-09-18 15:39:42 -0400245{
gregor kowski035d0242009-08-19 22:35:45 +0200246 const unsigned char *fragment_data = skb_frag->data;
247 unsigned int fragment_len = skb_frag->len;
Michael Buescheb189d8b2008-01-28 14:47:41 -0800248 struct b43_txhdr *txhdr = (struct b43_txhdr *)_txhdr;
Michael Buesche4d6b792007-09-18 15:39:42 -0400249 const struct b43_phy *phy = &dev->phy;
250 const struct ieee80211_hdr *wlhdr =
251 (const struct ieee80211_hdr *)fragment_data;
Johannes Bergd0f09802008-07-29 11:32:07 +0200252 int use_encryption = !!info->control.hw_key;
Harvey Harrisonf37d9232008-06-14 23:33:39 -0700253 __le16 fctl = wlhdr->frame_control;
Johannes Berg8318d782008-01-24 19:38:38 +0100254 struct ieee80211_rate *fbrate;
Michael Buesche4d6b792007-09-18 15:39:42 -0400255 u8 rate, rate_fb;
256 int rate_ofdm, rate_fb_ofdm;
257 unsigned int plcp_fragment_len;
258 u32 mac_ctl = 0;
259 u16 phy_ctl = 0;
Rafał Miłecki694718d2011-10-17 16:59:25 +0200260 bool fill_phy_ctl1 = (phy->type == B43_PHYTYPE_LP ||
261 phy->type == B43_PHYTYPE_N ||
262 phy->type == B43_PHYTYPE_HT);
Michael Buesche4d6b792007-09-18 15:39:42 -0400263 u8 extra_ft = 0;
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200264 struct ieee80211_rate *txrate;
Johannes Berge6a98542008-10-21 12:40:02 +0200265 struct ieee80211_tx_rate *rates;
Michael Buesche4d6b792007-09-18 15:39:42 -0400266
267 memset(txhdr, 0, sizeof(*txhdr));
268
Johannes Berge039fa42008-05-15 12:55:29 +0200269 txrate = ieee80211_get_tx_rate(dev->wl->hw, info);
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200270 rate = txrate ? txrate->hw_value : B43_CCK_RATE_1MB;
Michael Buesche4d6b792007-09-18 15:39:42 -0400271 rate_ofdm = b43_is_ofdm_rate(rate);
Felix Fietkau870abdf2008-10-05 18:04:24 +0200272 fbrate = ieee80211_get_alt_retry_rate(dev->wl->hw, info, 0) ? : txrate;
Johannes Berg8318d782008-01-24 19:38:38 +0100273 rate_fb = fbrate->hw_value;
Michael Buesche4d6b792007-09-18 15:39:42 -0400274 rate_fb_ofdm = b43_is_ofdm_rate(rate_fb);
275
276 if (rate_ofdm)
277 txhdr->phy_rate = b43_plcp_get_ratecode_ofdm(rate);
278 else
279 txhdr->phy_rate = b43_plcp_get_ratecode_cck(rate);
280 txhdr->mac_frame_ctl = wlhdr->frame_control;
281 memcpy(txhdr->tx_receiver, wlhdr->addr1, 6);
282
283 /* Calculate duration for fallback rate */
284 if ((rate_fb == rate) ||
285 (wlhdr->duration_id & cpu_to_le16(0x8000)) ||
286 (wlhdr->duration_id == cpu_to_le16(0))) {
287 /* If the fallback rate equals the normal rate or the
288 * dur_id field contains an AID, CFP magic or 0,
289 * use the original dur_id field. */
290 txhdr->dur_fb = wlhdr->duration_id;
291 } else {
Johannes Berge039fa42008-05-15 12:55:29 +0200292 txhdr->dur_fb = ieee80211_generic_frame_duration(
Michal Kazior4ee73f32012-04-11 08:47:56 +0200293 dev->wl->hw, info->control.vif, info->band,
294 fragment_len, fbrate);
Michael Buesche4d6b792007-09-18 15:39:42 -0400295 }
296
297 plcp_fragment_len = fragment_len + FCS_LEN;
298 if (use_encryption) {
Johannes Berge039fa42008-05-15 12:55:29 +0200299 u8 key_idx = info->control.hw_key->hw_key_idx;
Michael Buesche4d6b792007-09-18 15:39:42 -0400300 struct b43_key *key;
301 int wlhdr_len;
302 size_t iv_len;
303
Michael Buesch66d2d082009-08-06 10:36:50 +0200304 B43_WARN_ON(key_idx >= ARRAY_SIZE(dev->key));
Michael Buesche4d6b792007-09-18 15:39:42 -0400305 key = &(dev->key[key_idx]);
Michael Buesche4d6b792007-09-18 15:39:42 -0400306
Michael Buesch09552cc2008-01-23 21:44:15 +0100307 if (unlikely(!key->keyconf)) {
308 /* This key is invalid. This might only happen
309 * in a short timeframe after machine resume before
310 * we were able to reconfigure keys.
311 * Drop this packet completely. Do not transmit it
312 * unencrypted to avoid leaking information. */
313 return -ENOKEY;
Michael Buesch7be1bb62008-01-23 21:10:56 +0100314 }
Michael Buesch09552cc2008-01-23 21:44:15 +0100315
316 /* Hardware appends ICV. */
Felix Fietkau76708de2008-10-05 18:02:48 +0200317 plcp_fragment_len += info->control.hw_key->icv_len;
Michael Buesch09552cc2008-01-23 21:44:15 +0100318
319 key_idx = b43_kidx_to_fw(dev, key_idx);
320 mac_ctl |= (key_idx << B43_TXH_MAC_KEYIDX_SHIFT) &
321 B43_TXH_MAC_KEYIDX;
322 mac_ctl |= (key->algorithm << B43_TXH_MAC_KEYALG_SHIFT) &
323 B43_TXH_MAC_KEYALG;
Harvey Harrisonf37d9232008-06-14 23:33:39 -0700324 wlhdr_len = ieee80211_hdrlen(fctl);
gregor kowski035d0242009-08-19 22:35:45 +0200325 if (key->algorithm == B43_SEC_ALGO_TKIP) {
326 u16 phase1key[5];
327 int i;
328 /* we give the phase1key and iv16 here, the key is stored in
329 * shm. With that the hardware can do phase 2 and encryption.
330 */
Johannes Berg523b02e2011-07-07 22:28:01 +0200331 ieee80211_get_tkip_p1k(info->control.hw_key, skb_frag, phase1key);
Michael Bueschcde1b552009-09-06 16:18:58 +0200332 /* phase1key is in host endian. Copy to little-endian txhdr->iv. */
333 for (i = 0; i < 5; i++) {
334 txhdr->iv[i * 2 + 0] = phase1key[i];
335 txhdr->iv[i * 2 + 1] = phase1key[i] >> 8;
336 }
gregor kowski035d0242009-08-19 22:35:45 +0200337 /* iv16 */
338 memcpy(txhdr->iv + 10, ((u8 *) wlhdr) + wlhdr_len, 3);
339 } else {
340 iv_len = min((size_t) info->control.hw_key->iv_len,
341 ARRAY_SIZE(txhdr->iv));
342 memcpy(txhdr->iv, ((u8 *) wlhdr) + wlhdr_len, iv_len);
343 }
Michael Buesche4d6b792007-09-18 15:39:42 -0400344 }
Rafał Miłeckiefe02492011-08-11 15:07:15 +0200345 switch (dev->fw.hdr_format) {
Rafał Miłecki5d852902011-08-11 15:07:16 +0200346 case B43_FW_HDR_598:
347 b43_generate_plcp_hdr((struct b43_plcp_hdr4 *)(&txhdr->format_598.plcp),
348 plcp_fragment_len, rate);
349 break;
Rafał Miłeckiefe02492011-08-11 15:07:15 +0200350 case B43_FW_HDR_351:
Rafał Miłecki2391b7e2011-08-11 15:07:14 +0200351 b43_generate_plcp_hdr((struct b43_plcp_hdr4 *)(&txhdr->format_351.plcp),
Michael Buescheb189d8b2008-01-28 14:47:41 -0800352 plcp_fragment_len, rate);
Rafał Miłeckiefe02492011-08-11 15:07:15 +0200353 break;
354 case B43_FW_HDR_410:
Rafał Miłecki2391b7e2011-08-11 15:07:14 +0200355 b43_generate_plcp_hdr((struct b43_plcp_hdr4 *)(&txhdr->format_410.plcp),
Michael Buescheb189d8b2008-01-28 14:47:41 -0800356 plcp_fragment_len, rate);
Rafał Miłeckiefe02492011-08-11 15:07:15 +0200357 break;
Michael Buescheb189d8b2008-01-28 14:47:41 -0800358 }
Michael Buesche4d6b792007-09-18 15:39:42 -0400359 b43_generate_plcp_hdr((struct b43_plcp_hdr4 *)(&txhdr->plcp_fb),
360 plcp_fragment_len, rate_fb);
361
362 /* Extra Frame Types */
363 if (rate_fb_ofdm)
Michael Buescheb189d8b2008-01-28 14:47:41 -0800364 extra_ft |= B43_TXH_EFT_FB_OFDM;
365 else
366 extra_ft |= B43_TXH_EFT_FB_CCK;
Michael Buesche4d6b792007-09-18 15:39:42 -0400367
368 /* Set channel radio code. Note that the micrcode ORs 0x100 to
369 * this value before comparing it to the value in SHM, if this
370 * is a 5Ghz packet.
371 */
372 txhdr->chan_radio_code = phy->channel;
373
374 /* PHY TX Control word */
375 if (rate_ofdm)
Michael Buescheb189d8b2008-01-28 14:47:41 -0800376 phy_ctl |= B43_TXH_PHY_ENC_OFDM;
377 else
378 phy_ctl |= B43_TXH_PHY_ENC_CCK;
Johannes Berge6a98542008-10-21 12:40:02 +0200379 if (info->control.rates[0].flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
Michael Buescheb189d8b2008-01-28 14:47:41 -0800380 phy_ctl |= B43_TXH_PHY_SHORTPRMBL;
Michael Buesch9db1f6d2007-12-22 21:54:20 +0100381
Johannes Bergd748b462012-03-28 11:04:23 +0200382 switch (b43_ieee80211_antenna_sanitize(dev, 0)) {
Michael Buesch9db1f6d2007-12-22 21:54:20 +0100383 case 0: /* Default */
Michael Buescheb189d8b2008-01-28 14:47:41 -0800384 phy_ctl |= B43_TXH_PHY_ANT01AUTO;
Michael Buesche4d6b792007-09-18 15:39:42 -0400385 break;
Michael Buesch9db1f6d2007-12-22 21:54:20 +0100386 case 1: /* Antenna 0 */
Michael Buescheb189d8b2008-01-28 14:47:41 -0800387 phy_ctl |= B43_TXH_PHY_ANT0;
Michael Buesche4d6b792007-09-18 15:39:42 -0400388 break;
Michael Buesch9db1f6d2007-12-22 21:54:20 +0100389 case 2: /* Antenna 1 */
Michael Buescheb189d8b2008-01-28 14:47:41 -0800390 phy_ctl |= B43_TXH_PHY_ANT1;
391 break;
392 case 3: /* Antenna 2 */
393 phy_ctl |= B43_TXH_PHY_ANT2;
394 break;
395 case 4: /* Antenna 3 */
396 phy_ctl |= B43_TXH_PHY_ANT3;
Michael Buesche4d6b792007-09-18 15:39:42 -0400397 break;
398 default:
399 B43_WARN_ON(1);
400 }
401
Johannes Berge6a98542008-10-21 12:40:02 +0200402 rates = info->control.rates;
Michael Buesche4d6b792007-09-18 15:39:42 -0400403 /* MAC control */
Johannes Berge039fa42008-05-15 12:55:29 +0200404 if (!(info->flags & IEEE80211_TX_CTL_NO_ACK))
Michael Buescheb189d8b2008-01-28 14:47:41 -0800405 mac_ctl |= B43_TXH_MAC_ACK;
Johannes Bergf591fa52008-07-10 11:21:26 +0200406 /* use hardware sequence counter as the non-TID counter */
407 if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ)
Michael Buescheb189d8b2008-01-28 14:47:41 -0800408 mac_ctl |= B43_TXH_MAC_HWSEQ;
Johannes Berge039fa42008-05-15 12:55:29 +0200409 if (info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT)
Michael Buescheb189d8b2008-01-28 14:47:41 -0800410 mac_ctl |= B43_TXH_MAC_STMSDU;
Michael Buesche4d6b792007-09-18 15:39:42 -0400411 if (phy->type == B43_PHYTYPE_A)
Michael Buescheb189d8b2008-01-28 14:47:41 -0800412 mac_ctl |= B43_TXH_MAC_5GHZ;
Johannes Berge6a98542008-10-21 12:40:02 +0200413
414 /* Overwrite rates[0].count to make the retry calculation
415 * in the tx status easier. need the actual retry limit to
416 * detect whether the fallback rate was used.
417 */
418 if ((rates[0].flags & IEEE80211_TX_RC_USE_RTS_CTS) ||
419 (rates[0].count <= dev->wl->hw->conf.long_frame_max_tx_count)) {
420 rates[0].count = dev->wl->hw->conf.long_frame_max_tx_count;
Michael Buescheb189d8b2008-01-28 14:47:41 -0800421 mac_ctl |= B43_TXH_MAC_LONGFRAME;
Johannes Berge6a98542008-10-21 12:40:02 +0200422 } else {
423 rates[0].count = dev->wl->hw->conf.short_frame_max_tx_count;
424 }
Michael Buesche4d6b792007-09-18 15:39:42 -0400425
426 /* Generate the RTS or CTS-to-self frame */
Johannes Berge6a98542008-10-21 12:40:02 +0200427 if ((rates[0].flags & IEEE80211_TX_RC_USE_RTS_CTS) ||
428 (rates[0].flags & IEEE80211_TX_RC_USE_CTS_PROTECT)) {
Michael Buesche4d6b792007-09-18 15:39:42 -0400429 unsigned int len;
Rafał Miłeckiefe02492011-08-11 15:07:15 +0200430 struct ieee80211_hdr *uninitialized_var(hdr);
Michael Buesche4d6b792007-09-18 15:39:42 -0400431 int rts_rate, rts_rate_fb;
432 int rts_rate_ofdm, rts_rate_fb_ofdm;
Rafał Miłeckiefe02492011-08-11 15:07:15 +0200433 struct b43_plcp_hdr6 *uninitialized_var(plcp);
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200434 struct ieee80211_rate *rts_cts_rate;
Michael Buesche4d6b792007-09-18 15:39:42 -0400435
Johannes Berge039fa42008-05-15 12:55:29 +0200436 rts_cts_rate = ieee80211_get_rts_cts_rate(dev->wl->hw, info);
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200437
438 rts_rate = rts_cts_rate ? rts_cts_rate->hw_value : B43_CCK_RATE_1MB;
Michael Buesche4d6b792007-09-18 15:39:42 -0400439 rts_rate_ofdm = b43_is_ofdm_rate(rts_rate);
440 rts_rate_fb = b43_calc_fallback_rate(rts_rate);
441 rts_rate_fb_ofdm = b43_is_ofdm_rate(rts_rate_fb);
442
Johannes Berge6a98542008-10-21 12:40:02 +0200443 if (rates[0].flags & IEEE80211_TX_RC_USE_CTS_PROTECT) {
Rafał Miłeckiefe02492011-08-11 15:07:15 +0200444 struct ieee80211_cts *uninitialized_var(cts);
Michael Buescheb189d8b2008-01-28 14:47:41 -0800445
Rafał Miłeckiefe02492011-08-11 15:07:15 +0200446 switch (dev->fw.hdr_format) {
Rafał Miłecki5d852902011-08-11 15:07:16 +0200447 case B43_FW_HDR_598:
448 cts = (struct ieee80211_cts *)
449 (txhdr->format_598.rts_frame);
450 break;
Rafał Miłeckiefe02492011-08-11 15:07:15 +0200451 case B43_FW_HDR_351:
Michael Buescheb189d8b2008-01-28 14:47:41 -0800452 cts = (struct ieee80211_cts *)
Rafał Miłecki2391b7e2011-08-11 15:07:14 +0200453 (txhdr->format_351.rts_frame);
Rafał Miłeckiefe02492011-08-11 15:07:15 +0200454 break;
455 case B43_FW_HDR_410:
Michael Buescheb189d8b2008-01-28 14:47:41 -0800456 cts = (struct ieee80211_cts *)
Rafał Miłecki2391b7e2011-08-11 15:07:14 +0200457 (txhdr->format_410.rts_frame);
Rafał Miłeckiefe02492011-08-11 15:07:15 +0200458 break;
Michael Buescheb189d8b2008-01-28 14:47:41 -0800459 }
Johannes Berge039fa42008-05-15 12:55:29 +0200460 ieee80211_ctstoself_get(dev->wl->hw, info->control.vif,
Michael Buesche4d6b792007-09-18 15:39:42 -0400461 fragment_data, fragment_len,
Johannes Berge039fa42008-05-15 12:55:29 +0200462 info, cts);
Michael Buescheb189d8b2008-01-28 14:47:41 -0800463 mac_ctl |= B43_TXH_MAC_SENDCTS;
Michael Buesche4d6b792007-09-18 15:39:42 -0400464 len = sizeof(struct ieee80211_cts);
465 } else {
Rafał Miłeckiefe02492011-08-11 15:07:15 +0200466 struct ieee80211_rts *uninitialized_var(rts);
Michael Buescheb189d8b2008-01-28 14:47:41 -0800467
Rafał Miłeckiefe02492011-08-11 15:07:15 +0200468 switch (dev->fw.hdr_format) {
Rafał Miłecki5d852902011-08-11 15:07:16 +0200469 case B43_FW_HDR_598:
470 rts = (struct ieee80211_rts *)
471 (txhdr->format_598.rts_frame);
472 break;
Rafał Miłeckiefe02492011-08-11 15:07:15 +0200473 case B43_FW_HDR_351:
Michael Buescheb189d8b2008-01-28 14:47:41 -0800474 rts = (struct ieee80211_rts *)
Rafał Miłecki2391b7e2011-08-11 15:07:14 +0200475 (txhdr->format_351.rts_frame);
Rafał Miłeckiefe02492011-08-11 15:07:15 +0200476 break;
477 case B43_FW_HDR_410:
Michael Buescheb189d8b2008-01-28 14:47:41 -0800478 rts = (struct ieee80211_rts *)
Rafał Miłecki2391b7e2011-08-11 15:07:14 +0200479 (txhdr->format_410.rts_frame);
Rafał Miłeckiefe02492011-08-11 15:07:15 +0200480 break;
Michael Buescheb189d8b2008-01-28 14:47:41 -0800481 }
Johannes Berge039fa42008-05-15 12:55:29 +0200482 ieee80211_rts_get(dev->wl->hw, info->control.vif,
Michael Buescheb189d8b2008-01-28 14:47:41 -0800483 fragment_data, fragment_len,
Johannes Berge039fa42008-05-15 12:55:29 +0200484 info, rts);
Michael Buescheb189d8b2008-01-28 14:47:41 -0800485 mac_ctl |= B43_TXH_MAC_SENDRTS;
Michael Buesche4d6b792007-09-18 15:39:42 -0400486 len = sizeof(struct ieee80211_rts);
487 }
488 len += FCS_LEN;
Michael Buescheb189d8b2008-01-28 14:47:41 -0800489
490 /* Generate the PLCP headers for the RTS/CTS frame */
Rafał Miłeckiefe02492011-08-11 15:07:15 +0200491 switch (dev->fw.hdr_format) {
Rafał Miłecki5d852902011-08-11 15:07:16 +0200492 case B43_FW_HDR_598:
493 plcp = &txhdr->format_598.rts_plcp;
494 break;
Rafał Miłeckiefe02492011-08-11 15:07:15 +0200495 case B43_FW_HDR_351:
Rafał Miłecki2391b7e2011-08-11 15:07:14 +0200496 plcp = &txhdr->format_351.rts_plcp;
Rafał Miłeckiefe02492011-08-11 15:07:15 +0200497 break;
498 case B43_FW_HDR_410:
Rafał Miłecki2391b7e2011-08-11 15:07:14 +0200499 plcp = &txhdr->format_410.rts_plcp;
Rafał Miłeckiefe02492011-08-11 15:07:15 +0200500 break;
501 }
Michael Buescheb189d8b2008-01-28 14:47:41 -0800502 b43_generate_plcp_hdr((struct b43_plcp_hdr4 *)plcp,
503 len, rts_rate);
504 plcp = &txhdr->rts_plcp_fb;
505 b43_generate_plcp_hdr((struct b43_plcp_hdr4 *)plcp,
Michael Buesche4d6b792007-09-18 15:39:42 -0400506 len, rts_rate_fb);
Michael Buescheb189d8b2008-01-28 14:47:41 -0800507
Rafał Miłeckiefe02492011-08-11 15:07:15 +0200508 switch (dev->fw.hdr_format) {
Rafał Miłecki5d852902011-08-11 15:07:16 +0200509 case B43_FW_HDR_598:
510 hdr = (struct ieee80211_hdr *)
511 (&txhdr->format_598.rts_frame);
512 break;
Rafał Miłeckiefe02492011-08-11 15:07:15 +0200513 case B43_FW_HDR_351:
Michael Buescheb189d8b2008-01-28 14:47:41 -0800514 hdr = (struct ieee80211_hdr *)
Rafał Miłecki2391b7e2011-08-11 15:07:14 +0200515 (&txhdr->format_351.rts_frame);
Rafał Miłeckiefe02492011-08-11 15:07:15 +0200516 break;
517 case B43_FW_HDR_410:
Michael Buescheb189d8b2008-01-28 14:47:41 -0800518 hdr = (struct ieee80211_hdr *)
Rafał Miłecki2391b7e2011-08-11 15:07:14 +0200519 (&txhdr->format_410.rts_frame);
Rafał Miłeckiefe02492011-08-11 15:07:15 +0200520 break;
Michael Buescheb189d8b2008-01-28 14:47:41 -0800521 }
Michael Buesche4d6b792007-09-18 15:39:42 -0400522 txhdr->rts_dur_fb = hdr->duration_id;
Michael Buescheb189d8b2008-01-28 14:47:41 -0800523
Michael Buesche4d6b792007-09-18 15:39:42 -0400524 if (rts_rate_ofdm) {
Michael Buescheb189d8b2008-01-28 14:47:41 -0800525 extra_ft |= B43_TXH_EFT_RTS_OFDM;
Michael Buesche4d6b792007-09-18 15:39:42 -0400526 txhdr->phy_rate_rts =
527 b43_plcp_get_ratecode_ofdm(rts_rate);
Michael Buescheb189d8b2008-01-28 14:47:41 -0800528 } else {
529 extra_ft |= B43_TXH_EFT_RTS_CCK;
Michael Buesche4d6b792007-09-18 15:39:42 -0400530 txhdr->phy_rate_rts =
531 b43_plcp_get_ratecode_cck(rts_rate);
Michael Buescheb189d8b2008-01-28 14:47:41 -0800532 }
Michael Buesche4d6b792007-09-18 15:39:42 -0400533 if (rts_rate_fb_ofdm)
Michael Buescheb189d8b2008-01-28 14:47:41 -0800534 extra_ft |= B43_TXH_EFT_RTSFB_OFDM;
535 else
536 extra_ft |= B43_TXH_EFT_RTSFB_CCK;
Rafał Miłecki3311abb2011-02-25 12:34:11 +0100537
538 if (rates[0].flags & IEEE80211_TX_RC_USE_RTS_CTS &&
Rafał Miłecki694718d2011-10-17 16:59:25 +0200539 fill_phy_ctl1) {
Rafał Miłecki3311abb2011-02-25 12:34:11 +0100540 txhdr->phy_ctl1_rts = cpu_to_le16(
541 b43_generate_tx_phy_ctl1(dev, rts_rate));
542 txhdr->phy_ctl1_rts_fb = cpu_to_le16(
543 b43_generate_tx_phy_ctl1(dev, rts_rate_fb));
544 }
Michael Buesche4d6b792007-09-18 15:39:42 -0400545 }
546
547 /* Magic cookie */
Rafał Miłeckiefe02492011-08-11 15:07:15 +0200548 switch (dev->fw.hdr_format) {
Rafał Miłecki5d852902011-08-11 15:07:16 +0200549 case B43_FW_HDR_598:
550 txhdr->format_598.cookie = cpu_to_le16(cookie);
551 break;
Rafał Miłeckiefe02492011-08-11 15:07:15 +0200552 case B43_FW_HDR_351:
Rafał Miłecki2391b7e2011-08-11 15:07:14 +0200553 txhdr->format_351.cookie = cpu_to_le16(cookie);
Rafał Miłeckiefe02492011-08-11 15:07:15 +0200554 break;
555 case B43_FW_HDR_410:
Rafał Miłecki2391b7e2011-08-11 15:07:14 +0200556 txhdr->format_410.cookie = cpu_to_le16(cookie);
Rafał Miłeckiefe02492011-08-11 15:07:15 +0200557 break;
558 }
Michael Buesche4d6b792007-09-18 15:39:42 -0400559
Rafał Miłecki694718d2011-10-17 16:59:25 +0200560 if (fill_phy_ctl1) {
Rafał Miłecki3311abb2011-02-25 12:34:11 +0100561 txhdr->phy_ctl1 =
562 cpu_to_le16(b43_generate_tx_phy_ctl1(dev, rate));
563 txhdr->phy_ctl1_fb =
564 cpu_to_le16(b43_generate_tx_phy_ctl1(dev, rate_fb));
565 }
566
Michael Buesche4d6b792007-09-18 15:39:42 -0400567 /* Apply the bitfields */
568 txhdr->mac_ctl = cpu_to_le32(mac_ctl);
569 txhdr->phy_ctl = cpu_to_le16(phy_ctl);
570 txhdr->extra_ft = extra_ft;
Michael Buesche4d6b792007-09-18 15:39:42 -0400571
Michael Buesch09552cc2008-01-23 21:44:15 +0100572 return 0;
Michael Buesche4d6b792007-09-18 15:39:42 -0400573}
574
575static s8 b43_rssi_postprocess(struct b43_wldev *dev,
576 u8 in_rssi, int ofdm,
577 int adjust_2053, int adjust_2050)
578{
579 struct b43_phy *phy = &dev->phy;
Michael Bueschef1a6282008-08-27 18:53:02 +0200580 struct b43_phy_g *gphy = phy->g;
Michael Buesche4d6b792007-09-18 15:39:42 -0400581 s32 tmp;
582
583 switch (phy->radio_ver) {
584 case 0x2050:
585 if (ofdm) {
586 tmp = in_rssi;
587 if (tmp > 127)
588 tmp -= 256;
589 tmp *= 73;
590 tmp /= 64;
591 if (adjust_2050)
592 tmp += 25;
593 else
594 tmp -= 3;
595 } else {
Rafał Miłecki05814832011-05-18 02:06:39 +0200596 if (dev->dev->bus_sprom->
Michael Buesche4d6b792007-09-18 15:39:42 -0400597 boardflags_lo & B43_BFL_RSSI) {
598 if (in_rssi > 63)
599 in_rssi = 63;
Michael Bueschef1a6282008-08-27 18:53:02 +0200600 B43_WARN_ON(phy->type != B43_PHYTYPE_G);
601 tmp = gphy->nrssi_lt[in_rssi];
Michael Buesche4d6b792007-09-18 15:39:42 -0400602 tmp = 31 - tmp;
603 tmp *= -131;
604 tmp /= 128;
605 tmp -= 57;
606 } else {
607 tmp = in_rssi;
608 tmp = 31 - tmp;
609 tmp *= -149;
610 tmp /= 128;
611 tmp -= 68;
612 }
613 if (phy->type == B43_PHYTYPE_G && adjust_2050)
614 tmp += 25;
615 }
616 break;
617 case 0x2060:
618 if (in_rssi > 127)
619 tmp = in_rssi - 256;
620 else
621 tmp = in_rssi;
622 break;
623 default:
624 tmp = in_rssi;
625 tmp -= 11;
626 tmp *= 103;
627 tmp /= 64;
628 if (adjust_2053)
629 tmp -= 109;
630 else
631 tmp -= 83;
632 }
633
634 return (s8) tmp;
635}
636
637//TODO
638#if 0
639static s8 b43_rssinoise_postprocess(struct b43_wldev *dev, u8 in_rssi)
640{
641 struct b43_phy *phy = &dev->phy;
642 s8 ret;
643
644 if (phy->type == B43_PHYTYPE_A) {
645 //TODO: Incomplete specs.
646 ret = 0;
647 } else
648 ret = b43_rssi_postprocess(dev, in_rssi, 0, 1, 1);
649
650 return ret;
651}
652#endif
653
654void b43_rx(struct b43_wldev *dev, struct sk_buff *skb, const void *_rxhdr)
655{
656 struct ieee80211_rx_status status;
657 struct b43_plcp_hdr6 *plcp;
658 struct ieee80211_hdr *wlhdr;
659 const struct b43_rxhdr_fw4 *rxhdr = _rxhdr;
Harvey Harrisonf37d9232008-06-14 23:33:39 -0700660 __le16 fctl;
Rafał Miłecki17030f42011-08-11 17:16:27 +0200661 u16 phystat0, phystat3;
662 u16 uninitialized_var(chanstat), uninitialized_var(mactime);
663 u32 uninitialized_var(macstat);
Michael Buesche4d6b792007-09-18 15:39:42 -0400664 u16 chanid;
Johannes Berg8318d782008-01-24 19:38:38 +0100665 u16 phytype;
Johannes Berg1924b4e2012-07-13 10:57:36 +0200666 int padding, rate_idx;
Michael Buesche4d6b792007-09-18 15:39:42 -0400667
668 memset(&status, 0, sizeof(status));
669
670 /* Get metadata about the frame from the header. */
671 phystat0 = le16_to_cpu(rxhdr->phy_status0);
672 phystat3 = le16_to_cpu(rxhdr->phy_status3);
Rafał Miłecki17030f42011-08-11 17:16:27 +0200673 switch (dev->fw.hdr_format) {
674 case B43_FW_HDR_598:
675 macstat = le32_to_cpu(rxhdr->format_598.mac_status);
676 mactime = le16_to_cpu(rxhdr->format_598.mac_time);
677 chanstat = le16_to_cpu(rxhdr->format_598.channel);
678 break;
679 case B43_FW_HDR_410:
680 case B43_FW_HDR_351:
681 macstat = le32_to_cpu(rxhdr->format_351.mac_status);
682 mactime = le16_to_cpu(rxhdr->format_351.mac_time);
683 chanstat = le16_to_cpu(rxhdr->format_351.channel);
684 break;
685 }
Johannes Berg8318d782008-01-24 19:38:38 +0100686 phytype = chanstat & B43_RX_CHAN_PHYTYPE;
Michael Buesche4d6b792007-09-18 15:39:42 -0400687
Michael Bueschce4fbdb2009-03-02 23:18:37 +0100688 if (unlikely(macstat & B43_RX_MAC_FCSERR)) {
Michael Buesche4d6b792007-09-18 15:39:42 -0400689 dev->wl->ieee_stats.dot11FCSErrorCount++;
Michael Bueschce4fbdb2009-03-02 23:18:37 +0100690 status.flag |= RX_FLAG_FAILED_FCS_CRC;
691 }
692 if (unlikely(phystat0 & (B43_RX_PHYST0_PLCPHCF | B43_RX_PHYST0_PLCPFV)))
693 status.flag |= RX_FLAG_FAILED_PLCP_CRC;
694 if (phystat0 & B43_RX_PHYST0_SHORTPRMBL)
695 status.flag |= RX_FLAG_SHORTPRE;
Michael Buesche4d6b792007-09-18 15:39:42 -0400696 if (macstat & B43_RX_MAC_DECERR) {
697 /* Decryption with the given key failed.
698 * Drop the packet. We also won't be able to decrypt it with
699 * the key in software. */
700 goto drop;
701 }
702
703 /* Skip PLCP and padding */
704 padding = (macstat & B43_RX_MAC_PADDING) ? 2 : 0;
705 if (unlikely(skb->len < (sizeof(struct b43_plcp_hdr6) + padding))) {
706 b43dbg(dev->wl, "RX: Packet size underrun (1)\n");
707 goto drop;
708 }
709 plcp = (struct b43_plcp_hdr6 *)(skb->data + padding);
710 skb_pull(skb, sizeof(struct b43_plcp_hdr6) + padding);
711 /* The skb contains the Wireless Header + payload data now */
712 if (unlikely(skb->len < (2 + 2 + 6 /*minimum hdr */ + FCS_LEN))) {
713 b43dbg(dev->wl, "RX: Packet size underrun (2)\n");
714 goto drop;
715 }
716 wlhdr = (struct ieee80211_hdr *)(skb->data);
Harvey Harrisonf37d9232008-06-14 23:33:39 -0700717 fctl = wlhdr->frame_control;
Michael Buesche4d6b792007-09-18 15:39:42 -0400718
719 if (macstat & B43_RX_MAC_DEC) {
720 unsigned int keyidx;
721 int wlhdr_len;
722
723 keyidx = ((macstat & B43_RX_MAC_KEYIDX)
724 >> B43_RX_MAC_KEYIDX_SHIFT);
725 /* We must adjust the key index here. We want the "physical"
726 * key index, but the ucode passed it slightly different.
727 */
728 keyidx = b43_kidx_to_raw(dev, keyidx);
Michael Buesch66d2d082009-08-06 10:36:50 +0200729 B43_WARN_ON(keyidx >= ARRAY_SIZE(dev->key));
Michael Buesche4d6b792007-09-18 15:39:42 -0400730
731 if (dev->key[keyidx].algorithm != B43_SEC_ALGO_NONE) {
Harvey Harrisonf37d9232008-06-14 23:33:39 -0700732 wlhdr_len = ieee80211_hdrlen(fctl);
Michael Buesche4d6b792007-09-18 15:39:42 -0400733 if (unlikely(skb->len < (wlhdr_len + 3))) {
734 b43dbg(dev->wl,
735 "RX: Packet size underrun (3)\n");
736 goto drop;
737 }
738 status.flag |= RX_FLAG_DECRYPTED;
739 }
740 }
741
Michael Buesch7b584162008-04-03 18:01:12 +0200742 /* Link quality statistics */
Rafał Miłecki207ae4a2011-10-12 23:18:15 +0200743 switch (chanstat & B43_RX_CHAN_PHYTYPE) {
Rafał Miłecki73d51f32011-10-17 17:15:44 +0200744 case B43_PHYTYPE_HT:
745 /* TODO: is max the right choice? */
746 status.signal = max_t(__s8,
747 max(rxhdr->phy_ht_power0, rxhdr->phy_ht_power1),
748 rxhdr->phy_ht_power2);
749 break;
Rafał Miłecki207ae4a2011-10-12 23:18:15 +0200750 case B43_PHYTYPE_N:
Rafał Miłecki73d51f32011-10-17 17:15:44 +0200751 /* Broadcom has code for min and avg, but always uses max */
Rafał Miłecki207ae4a2011-10-12 23:18:15 +0200752 if (rxhdr->power0 == 16 || rxhdr->power0 == 32)
753 status.signal = max(rxhdr->power1, rxhdr->power2);
754 else
755 status.signal = max(rxhdr->power0, rxhdr->power1);
756 break;
757 case B43_PHYTYPE_A:
758 case B43_PHYTYPE_B:
759 case B43_PHYTYPE_G:
760 case B43_PHYTYPE_LP:
Bruno Randolf566bfe52008-05-08 19:15:40 +0200761 status.signal = b43_rssi_postprocess(dev, rxhdr->jssi,
Michael Buesch7b584162008-04-03 18:01:12 +0200762 (phystat0 & B43_RX_PHYST0_OFDM),
763 (phystat0 & B43_RX_PHYST0_GAINCTL),
764 (phystat3 & B43_RX_PHYST3_TRSTATE));
Rafał Miłecki207ae4a2011-10-12 23:18:15 +0200765 break;
Michael Buesch7b584162008-04-03 18:01:12 +0200766 }
767
Michael Buesche4d6b792007-09-18 15:39:42 -0400768 if (phystat0 & B43_RX_PHYST0_OFDM)
Johannes Berg1924b4e2012-07-13 10:57:36 +0200769 rate_idx = b43_plcp_get_bitrate_idx_ofdm(plcp,
Johannes Berg8318d782008-01-24 19:38:38 +0100770 phytype == B43_PHYTYPE_A);
Michael Buesche4d6b792007-09-18 15:39:42 -0400771 else
Johannes Berg1924b4e2012-07-13 10:57:36 +0200772 rate_idx = b43_plcp_get_bitrate_idx_cck(plcp);
773 if (unlikely(rate_idx == -1)) {
Michael Bueschce4fbdb2009-03-02 23:18:37 +0100774 /* PLCP seems to be corrupted.
775 * Drop the frame, if we are not interested in corrupted frames. */
776 if (!(dev->wl->filter_flags & FIF_PLCPFAIL))
777 goto drop;
778 }
Johannes Berg1924b4e2012-07-13 10:57:36 +0200779 status.rate_idx = rate_idx;
Michael Buesche4d6b792007-09-18 15:39:42 -0400780 status.antenna = !!(phystat0 & B43_RX_PHYST0_ANT);
John W. Linvillec0ddd042008-01-21 13:41:18 -0500781
782 /*
Johannes Bergd007b7f2008-02-18 18:53:55 +0100783 * All frames on monitor interfaces and beacons always need a full
784 * 64-bit timestamp. Monitor interfaces need it for diagnostic
785 * purposes and beacons for IBSS merging.
786 * This code assumes we get to process the packet within 16 bits
787 * of timestamp, i.e. about 65 milliseconds after the PHY received
788 * the first symbol.
John W. Linvillec0ddd042008-01-21 13:41:18 -0500789 */
Harvey Harrisonf37d9232008-06-14 23:33:39 -0700790 if (ieee80211_is_beacon(fctl) || dev->wl->radiotap_enabled) {
John W. Linvillec0ddd042008-01-21 13:41:18 -0500791 u16 low_mactime_now;
792
793 b43_tsf_read(dev, &status.mactime);
794 low_mactime_now = status.mactime;
795 status.mactime = status.mactime & ~0xFFFFULL;
796 status.mactime += mactime;
797 if (low_mactime_now <= mactime)
798 status.mactime -= 0x10000;
Johannes Berg6ebacbb2011-02-23 15:06:08 +0100799 status.flag |= RX_FLAG_MACTIME_MPDU;
John W. Linvillec0ddd042008-01-21 13:41:18 -0500800 }
Michael Buesche4d6b792007-09-18 15:39:42 -0400801
802 chanid = (chanstat & B43_RX_CHAN_ID) >> B43_RX_CHAN_ID_SHIFT;
803 switch (chanstat & B43_RX_CHAN_PHYTYPE) {
804 case B43_PHYTYPE_A:
Johannes Berg8318d782008-01-24 19:38:38 +0100805 status.band = IEEE80211_BAND_5GHZ;
Michael Bueschd9871602008-01-02 18:55:53 +0100806 B43_WARN_ON(1);
807 /* FIXME: We don't really know which value the "chanid" contains.
808 * So the following assignment might be wrong. */
Johannes Berg8318d782008-01-24 19:38:38 +0100809 status.freq = b43_channel_to_freq_5ghz(chanid);
Michael Buesche4d6b792007-09-18 15:39:42 -0400810 break;
811 case B43_PHYTYPE_G:
Johannes Berg8318d782008-01-24 19:38:38 +0100812 status.band = IEEE80211_BAND_2GHZ;
Michael Bueschd9871602008-01-02 18:55:53 +0100813 /* chanid is the radio channel cookie value as used
814 * to tune the radio. */
Michael Buesche4d6b792007-09-18 15:39:42 -0400815 status.freq = chanid + 2400;
Michael Bueschd9871602008-01-02 18:55:53 +0100816 break;
817 case B43_PHYTYPE_N:
Gábor Stefanik826ee702009-08-15 00:52:02 +0200818 case B43_PHYTYPE_LP:
Rafał Miłecki6a461c22011-08-12 00:03:25 +0200819 case B43_PHYTYPE_HT:
Michael Bueschd9871602008-01-02 18:55:53 +0100820 /* chanid is the SHM channel cookie. Which is the plain
821 * channel number in b43. */
Johannes Berg8318d782008-01-24 19:38:38 +0100822 if (chanstat & B43_RX_CHAN_5GHZ) {
823 status.band = IEEE80211_BAND_5GHZ;
824 status.freq = b43_freq_to_channel_5ghz(chanid);
825 } else {
826 status.band = IEEE80211_BAND_2GHZ;
827 status.freq = b43_freq_to_channel_2ghz(chanid);
828 }
Michael Buesche4d6b792007-09-18 15:39:42 -0400829 break;
830 default:
831 B43_WARN_ON(1);
Michael Bueschd9871602008-01-02 18:55:53 +0100832 goto drop;
Michael Buesche4d6b792007-09-18 15:39:42 -0400833 }
834
Johannes Bergf1d58c22009-06-17 13:13:00 +0200835 memcpy(IEEE80211_SKB_RXCB(skb), &status, sizeof(status));
John W. Linville72f5f452009-10-16 14:35:51 -0400836 ieee80211_rx_ni(dev->wl->hw, skb);
Michael Buesche4d6b792007-09-18 15:39:42 -0400837
Michael Buesch990b86f2009-09-12 00:48:03 +0200838#if B43_DEBUG
839 dev->rx_count++;
840#endif
Michael Buesche4d6b792007-09-18 15:39:42 -0400841 return;
842drop:
Michael Buesche4d6b792007-09-18 15:39:42 -0400843 dev_kfree_skb_any(skb);
844}
845
846void b43_handle_txstatus(struct b43_wldev *dev,
847 const struct b43_txstatus *status)
848{
849 b43_debugfs_log_txstat(dev, status);
850
851 if (status->intermediate)
852 return;
853 if (status->for_ampdu)
854 return;
855 if (!status->acked)
856 dev->wl->ieee_stats.dot11ACKFailureCount++;
857 if (status->rts_count) {
858 if (status->rts_count == 0xF) //FIXME
859 dev->wl->ieee_stats.dot11RTSFailureCount++;
860 else
861 dev->wl->ieee_stats.dot11RTSSuccessCount++;
862 }
863
Michael Buesch5100d5a2008-03-29 21:01:16 +0100864 if (b43_using_pio_transfers(dev))
865 b43_pio_handle_txstatus(dev, status);
866 else
867 b43_dma_handle_txstatus(dev, status);
Michael Buesch18c8ade2008-08-28 19:33:40 +0200868
869 b43_phy_txpower_check(dev, 0);
Michael Buesche4d6b792007-09-18 15:39:42 -0400870}
871
Michael Buesch5100d5a2008-03-29 21:01:16 +0100872/* Fill out the mac80211 TXstatus report based on the b43-specific
873 * txstatus report data. This returns a boolean whether the frame was
874 * successfully transmitted. */
Johannes Berge6a98542008-10-21 12:40:02 +0200875bool b43_fill_txstatus_report(struct b43_wldev *dev,
876 struct ieee80211_tx_info *report,
Michael Buesch5100d5a2008-03-29 21:01:16 +0100877 const struct b43_txstatus *status)
Michael Buesche4d6b792007-09-18 15:39:42 -0400878{
Rusty Russell3db1cd52011-12-19 13:56:45 +0000879 bool frame_success = true;
Johannes Berge6a98542008-10-21 12:40:02 +0200880 int retry_limit;
881
882 /* preserve the confiured retry limit before clearing the status
883 * The xmit function has overwritten the rc's value with the actual
884 * retry limit done by the hardware */
885 retry_limit = report->status.rates[0].count;
886 ieee80211_tx_info_clear_status(report);
Michael Buesche4d6b792007-09-18 15:39:42 -0400887
Michael Buesch5100d5a2008-03-29 21:01:16 +0100888 if (status->acked) {
889 /* The frame was ACKed. */
Johannes Berge039fa42008-05-15 12:55:29 +0200890 report->flags |= IEEE80211_TX_STAT_ACK;
Michael Buesch5100d5a2008-03-29 21:01:16 +0100891 } else {
892 /* The frame was not ACKed... */
Johannes Berge039fa42008-05-15 12:55:29 +0200893 if (!(report->flags & IEEE80211_TX_CTL_NO_ACK)) {
Michael Buesch5100d5a2008-03-29 21:01:16 +0100894 /* ...but we expected an ACK. */
Rusty Russell3db1cd52011-12-19 13:56:45 +0000895 frame_success = false;
Michael Buesch5100d5a2008-03-29 21:01:16 +0100896 }
897 }
898 if (status->frame_count == 0) {
899 /* The frame was not transmitted at all. */
Johannes Berge6a98542008-10-21 12:40:02 +0200900 report->status.rates[0].count = 0;
901 } else if (status->rts_count > dev->wl->hw->conf.short_frame_max_tx_count) {
902 /*
903 * If the short retries (RTS, not data frame) have exceeded
904 * the limit, the hw will not have tried the selected rate,
905 * but will have used the fallback rate instead.
906 * Don't let the rate control count attempts for the selected
907 * rate in this case, otherwise the statistics will be off.
908 */
909 report->status.rates[0].count = 0;
910 report->status.rates[1].count = status->frame_count;
911 } else {
912 if (status->frame_count > retry_limit) {
913 report->status.rates[0].count = retry_limit;
914 report->status.rates[1].count = status->frame_count -
915 retry_limit;
916
917 } else {
918 report->status.rates[0].count = status->frame_count;
919 report->status.rates[1].idx = -1;
920 }
921 }
Michael Buesche4d6b792007-09-18 15:39:42 -0400922
Michael Buesch5100d5a2008-03-29 21:01:16 +0100923 return frame_success;
Michael Buesche4d6b792007-09-18 15:39:42 -0400924}
925
926/* Stop any TX operation on the device (suspend the hardware queues) */
927void b43_tx_suspend(struct b43_wldev *dev)
928{
Michael Buesch5100d5a2008-03-29 21:01:16 +0100929 if (b43_using_pio_transfers(dev))
930 b43_pio_tx_suspend(dev);
931 else
932 b43_dma_tx_suspend(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -0400933}
934
935/* Resume any TX operation on the device (resume the hardware queues) */
936void b43_tx_resume(struct b43_wldev *dev)
937{
Michael Buesch5100d5a2008-03-29 21:01:16 +0100938 if (b43_using_pio_transfers(dev))
939 b43_pio_tx_resume(dev);
940 else
941 b43_dma_tx_resume(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -0400942}