blob: e6c573af494d1937992fbaef2cd0c3d5a17746b7 [file] [log] [blame]
Larry Finger75388ac2007-09-25 16:46:54 -07001/*
2
3 Broadcom B43legacy 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>
Larry Finger75388ac2007-09-25 16:46:54 -070010 Copyright (C) 2005 Danny van Dyk <kugelfang@gentoo.org>
11 Copyright (C) 2005 Andreas Jaggi <andreas.jaggi@waterwave.ch>
12 Copyright (C) 2007 Larry Finger <Larry.Finger@lwfinger.net>
13
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 2 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program; see the file COPYING. If not, write to
26 the Free Software Foundation, Inc., 51 Franklin Steet, Fifth Floor,
27 Boston, MA 02110-1301, USA.
28
29*/
30
31#include <net/dst.h>
32
33#include "xmit.h"
34#include "phy.h"
35#include "dma.h"
36#include "pio.h"
37
38
39/* Extract the bitrate out of a CCK PLCP header. */
Johannes Berg8318d782008-01-24 19:38:38 +010040static u8 b43legacy_plcp_get_bitrate_idx_cck(struct b43legacy_plcp_hdr6 *plcp)
Larry Finger75388ac2007-09-25 16:46:54 -070041{
42 switch (plcp->raw[0]) {
43 case 0x0A:
Johannes Berg8318d782008-01-24 19:38:38 +010044 return 0;
Larry Finger75388ac2007-09-25 16:46:54 -070045 case 0x14:
Johannes Berg8318d782008-01-24 19:38:38 +010046 return 1;
Larry Finger75388ac2007-09-25 16:46:54 -070047 case 0x37:
Johannes Berg8318d782008-01-24 19:38:38 +010048 return 2;
Larry Finger75388ac2007-09-25 16:46:54 -070049 case 0x6E:
Johannes Berg8318d782008-01-24 19:38:38 +010050 return 3;
Larry Finger75388ac2007-09-25 16:46:54 -070051 }
52 B43legacy_BUG_ON(1);
Johannes Berg8318d782008-01-24 19:38:38 +010053 return -1;
Larry Finger75388ac2007-09-25 16:46:54 -070054}
55
56/* Extract the bitrate out of an OFDM PLCP header. */
Johannes Berg8318d782008-01-24 19:38:38 +010057static u8 b43legacy_plcp_get_bitrate_idx_ofdm(struct b43legacy_plcp_hdr6 *plcp,
58 bool aphy)
Larry Finger75388ac2007-09-25 16:46:54 -070059{
Johannes Berg8318d782008-01-24 19:38:38 +010060 int base = aphy ? 0 : 4;
61
Larry Finger75388ac2007-09-25 16:46:54 -070062 switch (plcp->raw[0] & 0xF) {
63 case 0xB:
Johannes Berg8318d782008-01-24 19:38:38 +010064 return base + 0;
Larry Finger75388ac2007-09-25 16:46:54 -070065 case 0xF:
Johannes Berg8318d782008-01-24 19:38:38 +010066 return base + 1;
Larry Finger75388ac2007-09-25 16:46:54 -070067 case 0xA:
Johannes Berg8318d782008-01-24 19:38:38 +010068 return base + 2;
Larry Finger75388ac2007-09-25 16:46:54 -070069 case 0xE:
Johannes Berg8318d782008-01-24 19:38:38 +010070 return base + 3;
Larry Finger75388ac2007-09-25 16:46:54 -070071 case 0x9:
Johannes Berg8318d782008-01-24 19:38:38 +010072 return base + 4;
Larry Finger75388ac2007-09-25 16:46:54 -070073 case 0xD:
Johannes Berg8318d782008-01-24 19:38:38 +010074 return base + 5;
Larry Finger75388ac2007-09-25 16:46:54 -070075 case 0x8:
Johannes Berg8318d782008-01-24 19:38:38 +010076 return base + 6;
Larry Finger75388ac2007-09-25 16:46:54 -070077 case 0xC:
Johannes Berg8318d782008-01-24 19:38:38 +010078 return base + 7;
Larry Finger75388ac2007-09-25 16:46:54 -070079 }
80 B43legacy_BUG_ON(1);
Johannes Berg8318d782008-01-24 19:38:38 +010081 return -1;
Larry Finger75388ac2007-09-25 16:46:54 -070082}
83
84u8 b43legacy_plcp_get_ratecode_cck(const u8 bitrate)
85{
86 switch (bitrate) {
87 case B43legacy_CCK_RATE_1MB:
88 return 0x0A;
89 case B43legacy_CCK_RATE_2MB:
90 return 0x14;
91 case B43legacy_CCK_RATE_5MB:
92 return 0x37;
93 case B43legacy_CCK_RATE_11MB:
94 return 0x6E;
95 }
96 B43legacy_BUG_ON(1);
97 return 0;
98}
99
100u8 b43legacy_plcp_get_ratecode_ofdm(const u8 bitrate)
101{
102 switch (bitrate) {
103 case B43legacy_OFDM_RATE_6MB:
104 return 0xB;
105 case B43legacy_OFDM_RATE_9MB:
106 return 0xF;
107 case B43legacy_OFDM_RATE_12MB:
108 return 0xA;
109 case B43legacy_OFDM_RATE_18MB:
110 return 0xE;
111 case B43legacy_OFDM_RATE_24MB:
112 return 0x9;
113 case B43legacy_OFDM_RATE_36MB:
114 return 0xD;
115 case B43legacy_OFDM_RATE_48MB:
116 return 0x8;
117 case B43legacy_OFDM_RATE_54MB:
118 return 0xC;
119 }
120 B43legacy_BUG_ON(1);
121 return 0;
122}
123
124void b43legacy_generate_plcp_hdr(struct b43legacy_plcp_hdr4 *plcp,
125 const u16 octets, const u8 bitrate)
126{
127 __le32 *data = &(plcp->data);
128 __u8 *raw = plcp->raw;
129
130 if (b43legacy_is_ofdm_rate(bitrate)) {
131 u16 d;
132
133 d = b43legacy_plcp_get_ratecode_ofdm(bitrate);
134 B43legacy_WARN_ON(octets & 0xF000);
135 d |= (octets << 5);
136 *data = cpu_to_le32(d);
137 } else {
138 u32 plen;
139
140 plen = octets * 16 / bitrate;
141 if ((octets * 16 % bitrate) > 0) {
142 plen++;
143 if ((bitrate == B43legacy_CCK_RATE_11MB)
144 && ((octets * 8 % 11) < 4))
145 raw[1] = 0x84;
146 else
147 raw[1] = 0x04;
148 } else
149 raw[1] = 0x04;
150 *data |= cpu_to_le32(plen << 16);
151 raw[0] = b43legacy_plcp_get_ratecode_cck(bitrate);
152 }
153}
154
155static u8 b43legacy_calc_fallback_rate(u8 bitrate)
156{
157 switch (bitrate) {
158 case B43legacy_CCK_RATE_1MB:
159 return B43legacy_CCK_RATE_1MB;
160 case B43legacy_CCK_RATE_2MB:
161 return B43legacy_CCK_RATE_1MB;
162 case B43legacy_CCK_RATE_5MB:
163 return B43legacy_CCK_RATE_2MB;
164 case B43legacy_CCK_RATE_11MB:
165 return B43legacy_CCK_RATE_5MB;
166 case B43legacy_OFDM_RATE_6MB:
167 return B43legacy_CCK_RATE_5MB;
168 case B43legacy_OFDM_RATE_9MB:
169 return B43legacy_OFDM_RATE_6MB;
170 case B43legacy_OFDM_RATE_12MB:
171 return B43legacy_OFDM_RATE_9MB;
172 case B43legacy_OFDM_RATE_18MB:
173 return B43legacy_OFDM_RATE_12MB;
174 case B43legacy_OFDM_RATE_24MB:
175 return B43legacy_OFDM_RATE_18MB;
176 case B43legacy_OFDM_RATE_36MB:
177 return B43legacy_OFDM_RATE_24MB;
178 case B43legacy_OFDM_RATE_48MB:
179 return B43legacy_OFDM_RATE_36MB;
180 case B43legacy_OFDM_RATE_54MB:
181 return B43legacy_OFDM_RATE_48MB;
182 }
183 B43legacy_BUG_ON(1);
184 return 0;
185}
186
Stefano Brivio9eca9a82008-02-02 19:16:01 +0100187static int generate_txhdr_fw3(struct b43legacy_wldev *dev,
Larry Finger75388ac2007-09-25 16:46:54 -0700188 struct b43legacy_txhdr_fw3 *txhdr,
189 const unsigned char *fragment_data,
190 unsigned int fragment_len,
Johannes Berge6a98542008-10-21 12:40:02 +0200191 struct ieee80211_tx_info *info,
Larry Finger75388ac2007-09-25 16:46:54 -0700192 u16 cookie)
193{
194 const struct ieee80211_hdr *wlhdr;
Johannes Bergd0f09802008-07-29 11:32:07 +0200195 int use_encryption = !!info->control.hw_key;
Larry Finger75388ac2007-09-25 16:46:54 -0700196 u8 rate;
Johannes Berg8318d782008-01-24 19:38:38 +0100197 struct ieee80211_rate *rate_fb;
Larry Finger75388ac2007-09-25 16:46:54 -0700198 int rate_ofdm;
199 int rate_fb_ofdm;
200 unsigned int plcp_fragment_len;
201 u32 mac_ctl = 0;
202 u16 phy_ctl = 0;
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200203 struct ieee80211_rate *tx_rate;
Johannes Berge6a98542008-10-21 12:40:02 +0200204 struct ieee80211_tx_rate *rates;
Larry Finger75388ac2007-09-25 16:46:54 -0700205
206 wlhdr = (const struct ieee80211_hdr *)fragment_data;
Larry Finger75388ac2007-09-25 16:46:54 -0700207
208 memset(txhdr, 0, sizeof(*txhdr));
209
Johannes Berge039fa42008-05-15 12:55:29 +0200210 tx_rate = ieee80211_get_tx_rate(dev->wl->hw, info);
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200211
212 rate = tx_rate->hw_value;
Larry Finger75388ac2007-09-25 16:46:54 -0700213 rate_ofdm = b43legacy_is_ofdm_rate(rate);
Felix Fietkau870abdf2008-10-05 18:04:24 +0200214 rate_fb = ieee80211_get_alt_retry_rate(dev->wl->hw, info, 0) ? : tx_rate;
Johannes Berg8318d782008-01-24 19:38:38 +0100215 rate_fb_ofdm = b43legacy_is_ofdm_rate(rate_fb->hw_value);
Larry Finger75388ac2007-09-25 16:46:54 -0700216
217 txhdr->mac_frame_ctl = wlhdr->frame_control;
218 memcpy(txhdr->tx_receiver, wlhdr->addr1, 6);
219
220 /* Calculate duration for fallback rate */
Johannes Berg8318d782008-01-24 19:38:38 +0100221 if ((rate_fb->hw_value == rate) ||
Larry Finger75388ac2007-09-25 16:46:54 -0700222 (wlhdr->duration_id & cpu_to_le16(0x8000)) ||
223 (wlhdr->duration_id == cpu_to_le16(0))) {
224 /* If the fallback rate equals the normal rate or the
225 * dur_id field contains an AID, CFP magic or 0,
226 * use the original dur_id field. */
227 txhdr->dur_fb = wlhdr->duration_id;
228 } else {
Larry Finger75388ac2007-09-25 16:46:54 -0700229 txhdr->dur_fb = ieee80211_generic_frame_duration(dev->wl->hw,
Johannes Berge039fa42008-05-15 12:55:29 +0200230 info->control.vif,
Larry Finger75388ac2007-09-25 16:46:54 -0700231 fragment_len,
Johannes Berg8318d782008-01-24 19:38:38 +0100232 rate_fb);
Larry Finger75388ac2007-09-25 16:46:54 -0700233 }
234
235 plcp_fragment_len = fragment_len + FCS_LEN;
236 if (use_encryption) {
Johannes Berge039fa42008-05-15 12:55:29 +0200237 u8 key_idx = info->control.hw_key->hw_key_idx;
Larry Finger75388ac2007-09-25 16:46:54 -0700238 struct b43legacy_key *key;
239 int wlhdr_len;
240 size_t iv_len;
241
242 B43legacy_WARN_ON(key_idx >= dev->max_nr_keys);
243 key = &(dev->key[key_idx]);
244
245 if (key->enabled) {
246 /* Hardware appends ICV. */
Felix Fietkau76708de2008-10-05 18:02:48 +0200247 plcp_fragment_len += info->control.hw_key->icv_len;
Larry Finger75388ac2007-09-25 16:46:54 -0700248
249 key_idx = b43legacy_kidx_to_fw(dev, key_idx);
250 mac_ctl |= (key_idx << B43legacy_TX4_MAC_KEYIDX_SHIFT) &
251 B43legacy_TX4_MAC_KEYIDX;
252 mac_ctl |= (key->algorithm <<
253 B43legacy_TX4_MAC_KEYALG_SHIFT) &
254 B43legacy_TX4_MAC_KEYALG;
Harvey Harrisonba5b6ef2008-07-15 18:43:56 -0700255 wlhdr_len = ieee80211_hdrlen(wlhdr->frame_control);
Felix Fietkau76708de2008-10-05 18:02:48 +0200256 iv_len = min((size_t)info->control.hw_key->iv_len,
Larry Finger75388ac2007-09-25 16:46:54 -0700257 ARRAY_SIZE(txhdr->iv));
258 memcpy(txhdr->iv, ((u8 *)wlhdr) + wlhdr_len, iv_len);
Stefano Brivio9eca9a82008-02-02 19:16:01 +0100259 } else {
260 /* This key is invalid. This might only happen
261 * in a short timeframe after machine resume before
262 * we were able to reconfigure keys.
263 * Drop this packet completely. Do not transmit it
264 * unencrypted to avoid leaking information. */
265 return -ENOKEY;
Larry Finger75388ac2007-09-25 16:46:54 -0700266 }
267 }
268 b43legacy_generate_plcp_hdr((struct b43legacy_plcp_hdr4 *)
269 (&txhdr->plcp), plcp_fragment_len,
270 rate);
271 b43legacy_generate_plcp_hdr((struct b43legacy_plcp_hdr4 *)
272 (&txhdr->plcp_fb), plcp_fragment_len,
Johannes Berg8318d782008-01-24 19:38:38 +0100273 rate_fb->hw_value);
Larry Finger75388ac2007-09-25 16:46:54 -0700274
275 /* PHY TX Control word */
276 if (rate_ofdm)
Larry Finger2d1f96d2009-04-11 11:26:01 -0500277 phy_ctl |= B43legacy_TX4_PHY_ENC_OFDM;
Johannes Berge6a98542008-10-21 12:40:02 +0200278 if (info->control.rates[0].flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
Larry Finger75388ac2007-09-25 16:46:54 -0700279 phy_ctl |= B43legacy_TX4_PHY_SHORTPRMBL;
Johannes Bergd748b462012-03-28 11:04:23 +0200280 phy_ctl |= B43legacy_TX4_PHY_ANTLAST;
Larry Finger75388ac2007-09-25 16:46:54 -0700281
282 /* MAC control */
Johannes Berge6a98542008-10-21 12:40:02 +0200283 rates = info->control.rates;
Johannes Berge039fa42008-05-15 12:55:29 +0200284 if (!(info->flags & IEEE80211_TX_CTL_NO_ACK))
Larry Finger75388ac2007-09-25 16:46:54 -0700285 mac_ctl |= B43legacy_TX4_MAC_ACK;
Johannes Bergf591fa52008-07-10 11:21:26 +0200286 if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ)
Larry Finger75388ac2007-09-25 16:46:54 -0700287 mac_ctl |= B43legacy_TX4_MAC_HWSEQ;
Johannes Berge039fa42008-05-15 12:55:29 +0200288 if (info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT)
Larry Finger75388ac2007-09-25 16:46:54 -0700289 mac_ctl |= B43legacy_TX4_MAC_STMSDU;
290 if (rate_fb_ofdm)
291 mac_ctl |= B43legacy_TX4_MAC_FALLBACKOFDM;
Johannes Berge6a98542008-10-21 12:40:02 +0200292
293 /* Overwrite rates[0].count to make the retry calculation
294 * in the tx status easier. need the actual retry limit to
295 * detect whether the fallback rate was used.
296 */
297 if ((rates[0].flags & IEEE80211_TX_RC_USE_RTS_CTS) ||
298 (rates[0].count <= dev->wl->hw->conf.long_frame_max_tx_count)) {
299 rates[0].count = dev->wl->hw->conf.long_frame_max_tx_count;
Stefano Brivio0a6e1be2007-11-06 22:48:12 +0100300 mac_ctl |= B43legacy_TX4_MAC_LONGFRAME;
Johannes Berge6a98542008-10-21 12:40:02 +0200301 } else {
302 rates[0].count = dev->wl->hw->conf.short_frame_max_tx_count;
303 }
Larry Finger75388ac2007-09-25 16:46:54 -0700304
305 /* Generate the RTS or CTS-to-self frame */
Johannes Berge6a98542008-10-21 12:40:02 +0200306 if ((rates[0].flags & IEEE80211_TX_RC_USE_RTS_CTS) ||
307 (rates[0].flags & IEEE80211_TX_RC_USE_CTS_PROTECT)) {
Larry Finger75388ac2007-09-25 16:46:54 -0700308 unsigned int len;
309 struct ieee80211_hdr *hdr;
310 int rts_rate;
311 int rts_rate_fb;
Larry Finger75388ac2007-09-25 16:46:54 -0700312 int rts_rate_fb_ofdm;
313
Johannes Berge039fa42008-05-15 12:55:29 +0200314 rts_rate = ieee80211_get_rts_cts_rate(dev->wl->hw, info)->hw_value;
Larry Finger75388ac2007-09-25 16:46:54 -0700315 rts_rate_fb = b43legacy_calc_fallback_rate(rts_rate);
316 rts_rate_fb_ofdm = b43legacy_is_ofdm_rate(rts_rate_fb);
317 if (rts_rate_fb_ofdm)
318 mac_ctl |= B43legacy_TX4_MAC_CTSFALLBACKOFDM;
319
Johannes Berge6a98542008-10-21 12:40:02 +0200320 if (rates[0].flags & IEEE80211_TX_RC_USE_CTS_PROTECT) {
Larry Finger75388ac2007-09-25 16:46:54 -0700321 ieee80211_ctstoself_get(dev->wl->hw,
Johannes Berge039fa42008-05-15 12:55:29 +0200322 info->control.vif,
Larry Finger75388ac2007-09-25 16:46:54 -0700323 fragment_data,
Johannes Berge039fa42008-05-15 12:55:29 +0200324 fragment_len, info,
Larry Finger75388ac2007-09-25 16:46:54 -0700325 (struct ieee80211_cts *)
326 (txhdr->rts_frame));
327 mac_ctl |= B43legacy_TX4_MAC_SENDCTS;
328 len = sizeof(struct ieee80211_cts);
329 } else {
330 ieee80211_rts_get(dev->wl->hw,
Johannes Berge039fa42008-05-15 12:55:29 +0200331 info->control.vif,
332 fragment_data, fragment_len, info,
Larry Finger75388ac2007-09-25 16:46:54 -0700333 (struct ieee80211_rts *)
334 (txhdr->rts_frame));
335 mac_ctl |= B43legacy_TX4_MAC_SENDRTS;
336 len = sizeof(struct ieee80211_rts);
337 }
338 len += FCS_LEN;
339 b43legacy_generate_plcp_hdr((struct b43legacy_plcp_hdr4 *)
340 (&txhdr->rts_plcp),
341 len, rts_rate);
342 b43legacy_generate_plcp_hdr((struct b43legacy_plcp_hdr4 *)
343 (&txhdr->rts_plcp_fb),
344 len, rts_rate_fb);
345 hdr = (struct ieee80211_hdr *)(&txhdr->rts_frame);
346 txhdr->rts_dur_fb = hdr->duration_id;
Larry Finger75388ac2007-09-25 16:46:54 -0700347 }
348
349 /* Magic cookie */
350 txhdr->cookie = cpu_to_le16(cookie);
351
352 /* Apply the bitfields */
353 txhdr->mac_ctl = cpu_to_le32(mac_ctl);
354 txhdr->phy_ctl = cpu_to_le16(phy_ctl);
Stefano Brivio9eca9a82008-02-02 19:16:01 +0100355
356 return 0;
Larry Finger75388ac2007-09-25 16:46:54 -0700357}
358
Stefano Brivio9eca9a82008-02-02 19:16:01 +0100359int b43legacy_generate_txhdr(struct b43legacy_wldev *dev,
Larry Finger75388ac2007-09-25 16:46:54 -0700360 u8 *txhdr,
361 const unsigned char *fragment_data,
362 unsigned int fragment_len,
Johannes Berge6a98542008-10-21 12:40:02 +0200363 struct ieee80211_tx_info *info,
Larry Finger75388ac2007-09-25 16:46:54 -0700364 u16 cookie)
365{
Stefano Brivio9eca9a82008-02-02 19:16:01 +0100366 return generate_txhdr_fw3(dev, (struct b43legacy_txhdr_fw3 *)txhdr,
Larry Finger75388ac2007-09-25 16:46:54 -0700367 fragment_data, fragment_len,
Johannes Berge039fa42008-05-15 12:55:29 +0200368 info, cookie);
Larry Finger75388ac2007-09-25 16:46:54 -0700369}
370
371static s8 b43legacy_rssi_postprocess(struct b43legacy_wldev *dev,
372 u8 in_rssi, int ofdm,
373 int adjust_2053, int adjust_2050)
374{
375 struct b43legacy_phy *phy = &dev->phy;
376 s32 tmp;
377
378 switch (phy->radio_ver) {
379 case 0x2050:
380 if (ofdm) {
381 tmp = in_rssi;
382 if (tmp > 127)
383 tmp -= 256;
384 tmp *= 73;
385 tmp /= 64;
386 if (adjust_2050)
387 tmp += 25;
388 else
389 tmp -= 3;
390 } else {
Larry Finger7797aa32007-11-09 16:57:34 -0600391 if (dev->dev->bus->sprom.boardflags_lo
Larry Finger75388ac2007-09-25 16:46:54 -0700392 & B43legacy_BFL_RSSI) {
393 if (in_rssi > 63)
394 in_rssi = 63;
395 tmp = phy->nrssi_lt[in_rssi];
396 tmp = 31 - tmp;
397 tmp *= -131;
398 tmp /= 128;
399 tmp -= 57;
400 } else {
401 tmp = in_rssi;
402 tmp = 31 - tmp;
403 tmp *= -149;
404 tmp /= 128;
405 tmp -= 68;
406 }
407 if (phy->type == B43legacy_PHYTYPE_G &&
408 adjust_2050)
409 tmp += 25;
410 }
411 break;
412 case 0x2060:
413 if (in_rssi > 127)
414 tmp = in_rssi - 256;
415 else
416 tmp = in_rssi;
417 break;
418 default:
419 tmp = in_rssi;
420 tmp -= 11;
421 tmp *= 103;
422 tmp /= 64;
423 if (adjust_2053)
424 tmp -= 109;
425 else
426 tmp -= 83;
427 }
428
429 return (s8)tmp;
430}
431
432void b43legacy_rx(struct b43legacy_wldev *dev,
433 struct sk_buff *skb,
434 const void *_rxhdr)
435{
436 struct ieee80211_rx_status status;
437 struct b43legacy_plcp_hdr6 *plcp;
438 struct ieee80211_hdr *wlhdr;
439 const struct b43legacy_rxhdr_fw3 *rxhdr = _rxhdr;
Harvey Harrisonb99a0172008-06-14 23:33:40 -0700440 __le16 fctl;
Larry Finger75388ac2007-09-25 16:46:54 -0700441 u16 phystat0;
442 u16 phystat3;
443 u16 chanstat;
444 u16 mactime;
445 u32 macstat;
446 u16 chanid;
447 u8 jssi;
448 int padding;
449
450 memset(&status, 0, sizeof(status));
451
452 /* Get metadata about the frame from the header. */
453 phystat0 = le16_to_cpu(rxhdr->phy_status0);
454 phystat3 = le16_to_cpu(rxhdr->phy_status3);
455 jssi = rxhdr->jssi;
456 macstat = le16_to_cpu(rxhdr->mac_status);
457 mactime = le16_to_cpu(rxhdr->mac_time);
458 chanstat = le16_to_cpu(rxhdr->channel);
459
460 if (macstat & B43legacy_RX_MAC_FCSERR)
461 dev->wl->ieee_stats.dot11FCSErrorCount++;
462
463 /* Skip PLCP and padding */
464 padding = (macstat & B43legacy_RX_MAC_PADDING) ? 2 : 0;
465 if (unlikely(skb->len < (sizeof(struct b43legacy_plcp_hdr6) +
466 padding))) {
467 b43legacydbg(dev->wl, "RX: Packet size underrun (1)\n");
468 goto drop;
469 }
470 plcp = (struct b43legacy_plcp_hdr6 *)(skb->data + padding);
471 skb_pull(skb, sizeof(struct b43legacy_plcp_hdr6) + padding);
472 /* The skb contains the Wireless Header + payload data now */
473 if (unlikely(skb->len < (2+2+6/*minimum hdr*/ + FCS_LEN))) {
474 b43legacydbg(dev->wl, "RX: Packet size underrun (2)\n");
475 goto drop;
476 }
477 wlhdr = (struct ieee80211_hdr *)(skb->data);
Harvey Harrisonb99a0172008-06-14 23:33:40 -0700478 fctl = wlhdr->frame_control;
Larry Finger75388ac2007-09-25 16:46:54 -0700479
480 if ((macstat & B43legacy_RX_MAC_DEC) &&
481 !(macstat & B43legacy_RX_MAC_DECERR)) {
482 unsigned int keyidx;
483 int wlhdr_len;
484 int iv_len;
485 int icv_len;
486
487 keyidx = ((macstat & B43legacy_RX_MAC_KEYIDX)
488 >> B43legacy_RX_MAC_KEYIDX_SHIFT);
489 /* We must adjust the key index here. We want the "physical"
490 * key index, but the ucode passed it slightly different.
491 */
492 keyidx = b43legacy_kidx_to_raw(dev, keyidx);
493 B43legacy_WARN_ON(keyidx >= dev->max_nr_keys);
494
495 if (dev->key[keyidx].algorithm != B43legacy_SEC_ALGO_NONE) {
496 /* Remove PROTECTED flag to mark it as decrypted. */
Harvey Harrisonb99a0172008-06-14 23:33:40 -0700497 B43legacy_WARN_ON(!ieee80211_has_protected(fctl));
498 fctl &= ~cpu_to_le16(IEEE80211_FCTL_PROTECTED);
499 wlhdr->frame_control = fctl;
Larry Finger75388ac2007-09-25 16:46:54 -0700500
Harvey Harrisonb99a0172008-06-14 23:33:40 -0700501 wlhdr_len = ieee80211_hdrlen(fctl);
Larry Finger75388ac2007-09-25 16:46:54 -0700502 if (unlikely(skb->len < (wlhdr_len + 3))) {
503 b43legacydbg(dev->wl, "RX: Packet size"
504 " underrun3\n");
505 goto drop;
506 }
507 if (skb->data[wlhdr_len + 3] & (1 << 5)) {
508 /* The Ext-IV Bit is set in the "KeyID"
509 * octet of the IV.
510 */
511 iv_len = 8;
512 icv_len = 8;
513 } else {
514 iv_len = 4;
515 icv_len = 4;
516 }
517 if (unlikely(skb->len < (wlhdr_len + iv_len +
518 icv_len))) {
519 b43legacydbg(dev->wl, "RX: Packet size"
520 " underrun4\n");
521 goto drop;
522 }
523 /* Remove the IV */
524 memmove(skb->data + iv_len, skb->data, wlhdr_len);
525 skb_pull(skb, iv_len);
526 /* Remove the ICV */
527 skb_trim(skb, skb->len - icv_len);
528
529 status.flag |= RX_FLAG_DECRYPTED;
530 }
531 }
532
Bruno Randolf566bfe52008-05-08 19:15:40 +0200533 status.signal = b43legacy_rssi_postprocess(dev, jssi,
Larry Finger75388ac2007-09-25 16:46:54 -0700534 (phystat0 & B43legacy_RX_PHYST0_OFDM),
535 (phystat0 & B43legacy_RX_PHYST0_GAINCTL),
536 (phystat3 & B43legacy_RX_PHYST3_TRSTATE));
Johannes Berg8318d782008-01-24 19:38:38 +0100537 /* change to support A PHY */
Larry Finger75388ac2007-09-25 16:46:54 -0700538 if (phystat0 & B43legacy_RX_PHYST0_OFDM)
Johannes Berg8318d782008-01-24 19:38:38 +0100539 status.rate_idx = b43legacy_plcp_get_bitrate_idx_ofdm(plcp, false);
Larry Finger75388ac2007-09-25 16:46:54 -0700540 else
Johannes Berg8318d782008-01-24 19:38:38 +0100541 status.rate_idx = b43legacy_plcp_get_bitrate_idx_cck(plcp);
Larry Finger75388ac2007-09-25 16:46:54 -0700542 status.antenna = !!(phystat0 & B43legacy_RX_PHYST0_ANT);
John W. Linvillec0ddd042008-01-21 13:41:18 -0500543
544 /*
Johannes Bergd007b7f2008-02-18 18:53:55 +0100545 * All frames on monitor interfaces and beacons always need a full
546 * 64-bit timestamp. Monitor interfaces need it for diagnostic
547 * purposes and beacons for IBSS merging.
548 * This code assumes we get to process the packet within 16 bits
549 * of timestamp, i.e. about 65 milliseconds after the PHY received
550 * the first symbol.
John W. Linvillec0ddd042008-01-21 13:41:18 -0500551 */
Harvey Harrisonb99a0172008-06-14 23:33:40 -0700552 if (ieee80211_is_beacon(fctl) || dev->wl->radiotap_enabled) {
John W. Linvillec0ddd042008-01-21 13:41:18 -0500553 u16 low_mactime_now;
554
555 b43legacy_tsf_read(dev, &status.mactime);
556 low_mactime_now = status.mactime;
557 status.mactime = status.mactime & ~0xFFFFULL;
558 status.mactime += mactime;
559 if (low_mactime_now <= mactime)
560 status.mactime -= 0x10000;
Johannes Berg6ebacbb2011-02-23 15:06:08 +0100561 status.flag |= RX_FLAG_MACTIME_MPDU;
John W. Linvillec0ddd042008-01-21 13:41:18 -0500562 }
Larry Finger75388ac2007-09-25 16:46:54 -0700563
564 chanid = (chanstat & B43legacy_RX_CHAN_ID) >>
565 B43legacy_RX_CHAN_ID_SHIFT;
566 switch (chanstat & B43legacy_RX_CHAN_PHYTYPE) {
567 case B43legacy_PHYTYPE_B:
Larry Finger75388ac2007-09-25 16:46:54 -0700568 case B43legacy_PHYTYPE_G:
Johannes Berg8318d782008-01-24 19:38:38 +0100569 status.band = IEEE80211_BAND_2GHZ;
Larry Finger75388ac2007-09-25 16:46:54 -0700570 status.freq = chanid + 2400;
Larry Finger75388ac2007-09-25 16:46:54 -0700571 break;
572 default:
573 b43legacywarn(dev->wl, "Unexpected value for chanstat (0x%X)\n",
574 chanstat);
575 }
576
Johannes Bergf1d58c22009-06-17 13:13:00 +0200577 memcpy(IEEE80211_SKB_RXCB(skb), &status, sizeof(status));
578 ieee80211_rx_irqsafe(dev->wl->hw, skb);
Larry Finger75388ac2007-09-25 16:46:54 -0700579
580 return;
581drop:
582 b43legacydbg(dev->wl, "RX: Packet dropped\n");
583 dev_kfree_skb_any(skb);
584}
585
586void b43legacy_handle_txstatus(struct b43legacy_wldev *dev,
587 const struct b43legacy_txstatus *status)
588{
589 b43legacy_debugfs_log_txstat(dev, status);
590
591 if (status->intermediate)
592 return;
593 if (status->for_ampdu)
594 return;
595 if (!status->acked)
596 dev->wl->ieee_stats.dot11ACKFailureCount++;
597 if (status->rts_count) {
598 if (status->rts_count == 0xF) /* FIXME */
599 dev->wl->ieee_stats.dot11RTSFailureCount++;
600 else
601 dev->wl->ieee_stats.dot11RTSSuccessCount++;
602 }
603
604 if (b43legacy_using_pio(dev))
605 b43legacy_pio_handle_txstatus(dev, status);
606 else
607 b43legacy_dma_handle_txstatus(dev, status);
608}
609
610/* Handle TX status report as received through DMA/PIO queues */
611void b43legacy_handle_hwtxstatus(struct b43legacy_wldev *dev,
612 const struct b43legacy_hwtxstatus *hw)
613{
614 struct b43legacy_txstatus status;
615 u8 tmp;
616
617 status.cookie = le16_to_cpu(hw->cookie);
618 status.seq = le16_to_cpu(hw->seq);
619 status.phy_stat = hw->phy_stat;
620 tmp = hw->count;
621 status.frame_count = (tmp >> 4);
622 status.rts_count = (tmp & 0x0F);
Larry Fingerc6a2afd2008-09-06 16:51:22 -0500623 tmp = hw->flags << 1;
Larry Finger75388ac2007-09-25 16:46:54 -0700624 status.supp_reason = ((tmp & 0x1C) >> 2);
625 status.pm_indicated = !!(tmp & 0x80);
626 status.intermediate = !!(tmp & 0x40);
627 status.for_ampdu = !!(tmp & 0x20);
628 status.acked = !!(tmp & 0x02);
629
630 b43legacy_handle_txstatus(dev, &status);
631}
632
633/* Stop any TX operation on the device (suspend the hardware queues) */
634void b43legacy_tx_suspend(struct b43legacy_wldev *dev)
635{
636 if (b43legacy_using_pio(dev))
637 b43legacy_pio_freeze_txqueues(dev);
638 else
639 b43legacy_dma_tx_suspend(dev);
640}
641
642/* Resume any TX operation on the device (resume the hardware queues) */
643void b43legacy_tx_resume(struct b43legacy_wldev *dev)
644{
645 if (b43legacy_using_pio(dev))
646 b43legacy_pio_thaw_txqueues(dev);
647 else
648 b43legacy_dma_tx_resume(dev);
649}
650
651/* Initialize the QoS parameters */
652void b43legacy_qos_init(struct b43legacy_wldev *dev)
653{
654 /* FIXME: This function must probably be called from the mac80211
655 * config callback. */
656return;
657
658 b43legacy_hf_write(dev, b43legacy_hf_read(dev) | B43legacy_HF_EDCF);
659 /* FIXME kill magic */
660 b43legacy_write16(dev, 0x688,
661 b43legacy_read16(dev, 0x688) | 0x4);
662
663
664 /*TODO: We might need some stack support here to get the values. */
665}