blob: b2a3123f42dbc985335258203cd938584742ca83 [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
30#include "xmit.h"
31#include "phy.h"
32#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 }
49 B43_WARN_ON(1);
Johannes Berg8318d782008-01-24 19:38:38 +010050 return -1;
Michael Buesche4d6b792007-09-18 15:39:42 -040051}
52
Johannes Berg8318d782008-01-24 19:38:38 +010053/* Extract the bitrate index out of an OFDM PLCP header. */
54static u8 b43_plcp_get_bitrate_idx_ofdm(struct b43_plcp_hdr6 *plcp, bool aphy)
Michael Buesche4d6b792007-09-18 15:39:42 -040055{
Johannes Berg8318d782008-01-24 19:38:38 +010056 int base = aphy ? 0 : 4;
57
Michael Buesche4d6b792007-09-18 15:39:42 -040058 switch (plcp->raw[0] & 0xF) {
59 case 0xB:
Johannes Berg8318d782008-01-24 19:38:38 +010060 return base + 0;
Michael Buesche4d6b792007-09-18 15:39:42 -040061 case 0xF:
Johannes Berg8318d782008-01-24 19:38:38 +010062 return base + 1;
Michael Buesche4d6b792007-09-18 15:39:42 -040063 case 0xA:
Johannes Berg8318d782008-01-24 19:38:38 +010064 return base + 2;
Michael Buesche4d6b792007-09-18 15:39:42 -040065 case 0xE:
Johannes Berg8318d782008-01-24 19:38:38 +010066 return base + 3;
Michael Buesche4d6b792007-09-18 15:39:42 -040067 case 0x9:
Johannes Berg8318d782008-01-24 19:38:38 +010068 return base + 4;
Michael Buesche4d6b792007-09-18 15:39:42 -040069 case 0xD:
Johannes Berg8318d782008-01-24 19:38:38 +010070 return base + 5;
Michael Buesche4d6b792007-09-18 15:39:42 -040071 case 0x8:
Johannes Berg8318d782008-01-24 19:38:38 +010072 return base + 6;
Michael Buesche4d6b792007-09-18 15:39:42 -040073 case 0xC:
Johannes Berg8318d782008-01-24 19:38:38 +010074 return base + 7;
Michael Buesche4d6b792007-09-18 15:39:42 -040075 }
76 B43_WARN_ON(1);
Johannes Berg8318d782008-01-24 19:38:38 +010077 return -1;
Michael Buesche4d6b792007-09-18 15:39:42 -040078}
79
80u8 b43_plcp_get_ratecode_cck(const u8 bitrate)
81{
82 switch (bitrate) {
83 case B43_CCK_RATE_1MB:
84 return 0x0A;
85 case B43_CCK_RATE_2MB:
86 return 0x14;
87 case B43_CCK_RATE_5MB:
88 return 0x37;
89 case B43_CCK_RATE_11MB:
90 return 0x6E;
91 }
92 B43_WARN_ON(1);
93 return 0;
94}
95
96u8 b43_plcp_get_ratecode_ofdm(const u8 bitrate)
97{
98 switch (bitrate) {
99 case B43_OFDM_RATE_6MB:
100 return 0xB;
101 case B43_OFDM_RATE_9MB:
102 return 0xF;
103 case B43_OFDM_RATE_12MB:
104 return 0xA;
105 case B43_OFDM_RATE_18MB:
106 return 0xE;
107 case B43_OFDM_RATE_24MB:
108 return 0x9;
109 case B43_OFDM_RATE_36MB:
110 return 0xD;
111 case B43_OFDM_RATE_48MB:
112 return 0x8;
113 case B43_OFDM_RATE_54MB:
114 return 0xC;
115 }
116 B43_WARN_ON(1);
117 return 0;
118}
119
120void b43_generate_plcp_hdr(struct b43_plcp_hdr4 *plcp,
121 const u16 octets, const u8 bitrate)
122{
123 __le32 *data = &(plcp->data);
124 __u8 *raw = plcp->raw;
125
126 if (b43_is_ofdm_rate(bitrate)) {
Michael Buesch1a094042007-09-20 11:13:40 -0700127 u32 d;
128
129 d = b43_plcp_get_ratecode_ofdm(bitrate);
Michael Buesche4d6b792007-09-18 15:39:42 -0400130 B43_WARN_ON(octets & 0xF000);
Michael Buesch1a094042007-09-20 11:13:40 -0700131 d |= (octets << 5);
132 *data = cpu_to_le32(d);
Michael Buesche4d6b792007-09-18 15:39:42 -0400133 } else {
134 u32 plen;
135
136 plen = octets * 16 / bitrate;
137 if ((octets * 16 % bitrate) > 0) {
138 plen++;
139 if ((bitrate == B43_CCK_RATE_11MB)
140 && ((octets * 8 % 11) < 4)) {
141 raw[1] = 0x84;
142 } else
143 raw[1] = 0x04;
144 } else
145 raw[1] = 0x04;
146 *data |= cpu_to_le32(plen << 16);
147 raw[0] = b43_plcp_get_ratecode_cck(bitrate);
148 }
149}
150
151static u8 b43_calc_fallback_rate(u8 bitrate)
152{
153 switch (bitrate) {
154 case B43_CCK_RATE_1MB:
155 return B43_CCK_RATE_1MB;
156 case B43_CCK_RATE_2MB:
157 return B43_CCK_RATE_1MB;
158 case B43_CCK_RATE_5MB:
159 return B43_CCK_RATE_2MB;
160 case B43_CCK_RATE_11MB:
161 return B43_CCK_RATE_5MB;
162 case B43_OFDM_RATE_6MB:
163 return B43_CCK_RATE_5MB;
164 case B43_OFDM_RATE_9MB:
165 return B43_OFDM_RATE_6MB;
166 case B43_OFDM_RATE_12MB:
167 return B43_OFDM_RATE_9MB;
168 case B43_OFDM_RATE_18MB:
169 return B43_OFDM_RATE_12MB;
170 case B43_OFDM_RATE_24MB:
171 return B43_OFDM_RATE_18MB;
172 case B43_OFDM_RATE_36MB:
173 return B43_OFDM_RATE_24MB;
174 case B43_OFDM_RATE_48MB:
175 return B43_OFDM_RATE_36MB;
176 case B43_OFDM_RATE_54MB:
177 return B43_OFDM_RATE_48MB;
178 }
179 B43_WARN_ON(1);
180 return 0;
181}
182
Michael Buescheb189d82008-01-28 14:47:41 -0800183/* Generate a TX data header. */
Michael Buesch09552cc2008-01-23 21:44:15 +0100184int b43_generate_txhdr(struct b43_wldev *dev,
185 u8 *_txhdr,
186 const unsigned char *fragment_data,
187 unsigned int fragment_len,
188 const struct ieee80211_tx_control *txctl,
189 u16 cookie)
Michael Buesche4d6b792007-09-18 15:39:42 -0400190{
Michael Buescheb189d82008-01-28 14:47:41 -0800191 struct b43_txhdr *txhdr = (struct b43_txhdr *)_txhdr;
Michael Buesche4d6b792007-09-18 15:39:42 -0400192 const struct b43_phy *phy = &dev->phy;
193 const struct ieee80211_hdr *wlhdr =
194 (const struct ieee80211_hdr *)fragment_data;
195 int use_encryption = (!(txctl->flags & IEEE80211_TXCTL_DO_NOT_ENCRYPT));
196 u16 fctl = le16_to_cpu(wlhdr->frame_control);
Johannes Berg8318d782008-01-24 19:38:38 +0100197 struct ieee80211_rate *fbrate;
Michael Buesche4d6b792007-09-18 15:39:42 -0400198 u8 rate, rate_fb;
199 int rate_ofdm, rate_fb_ofdm;
200 unsigned int plcp_fragment_len;
201 u32 mac_ctl = 0;
202 u16 phy_ctl = 0;
203 u8 extra_ft = 0;
204
205 memset(txhdr, 0, sizeof(*txhdr));
206
Johannes Berg8318d782008-01-24 19:38:38 +0100207 WARN_ON(!txctl->tx_rate);
208 rate = txctl->tx_rate ? txctl->tx_rate->hw_value : B43_CCK_RATE_1MB;
Michael Buesche4d6b792007-09-18 15:39:42 -0400209 rate_ofdm = b43_is_ofdm_rate(rate);
Johannes Berg8318d782008-01-24 19:38:38 +0100210 fbrate = txctl->alt_retry_rate ? : txctl->tx_rate;
211 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 {
Michael Buesche4d6b792007-09-18 15:39:42 -0400230 txhdr->dur_fb = ieee80211_generic_frame_duration(dev->wl->hw,
Johannes Berg32bfd352007-12-19 01:31:26 +0100231 txctl->vif,
Michael Buesche4d6b792007-09-18 15:39:42 -0400232 fragment_len,
Johannes Berg8318d782008-01-24 19:38:38 +0100233 fbrate);
Michael Buesche4d6b792007-09-18 15:39:42 -0400234 }
235
236 plcp_fragment_len = fragment_len + FCS_LEN;
237 if (use_encryption) {
238 u8 key_idx = (u16) (txctl->key_idx);
239 struct b43_key *key;
240 int wlhdr_len;
241 size_t iv_len;
242
243 B43_WARN_ON(key_idx >= dev->max_nr_keys);
244 key = &(dev->key[key_idx]);
Michael Buesche4d6b792007-09-18 15:39:42 -0400245
Michael Buesch09552cc2008-01-23 21:44:15 +0100246 if (unlikely(!key->keyconf)) {
247 /* This key is invalid. This might only happen
248 * in a short timeframe after machine resume before
249 * we were able to reconfigure keys.
250 * Drop this packet completely. Do not transmit it
251 * unencrypted to avoid leaking information. */
252 return -ENOKEY;
Michael Buesch7be1bb62008-01-23 21:10:56 +0100253 }
Michael Buesch09552cc2008-01-23 21:44:15 +0100254
255 /* Hardware appends ICV. */
256 plcp_fragment_len += txctl->icv_len;
257
258 key_idx = b43_kidx_to_fw(dev, key_idx);
259 mac_ctl |= (key_idx << B43_TXH_MAC_KEYIDX_SHIFT) &
260 B43_TXH_MAC_KEYIDX;
261 mac_ctl |= (key->algorithm << B43_TXH_MAC_KEYALG_SHIFT) &
262 B43_TXH_MAC_KEYALG;
263 wlhdr_len = ieee80211_get_hdrlen(fctl);
264 iv_len = min((size_t) txctl->iv_len,
265 ARRAY_SIZE(txhdr->iv));
266 memcpy(txhdr->iv, ((u8 *) wlhdr) + wlhdr_len, iv_len);
Michael Buesche4d6b792007-09-18 15:39:42 -0400267 }
Michael Buescheb189d82008-01-28 14:47:41 -0800268 if (b43_is_old_txhdr_format(dev)) {
269 b43_generate_plcp_hdr((struct b43_plcp_hdr4 *)(&txhdr->old_format.plcp),
270 plcp_fragment_len, rate);
271 } else {
272 b43_generate_plcp_hdr((struct b43_plcp_hdr4 *)(&txhdr->new_format.plcp),
273 plcp_fragment_len, rate);
274 }
Michael Buesche4d6b792007-09-18 15:39:42 -0400275 b43_generate_plcp_hdr((struct b43_plcp_hdr4 *)(&txhdr->plcp_fb),
276 plcp_fragment_len, rate_fb);
277
278 /* Extra Frame Types */
279 if (rate_fb_ofdm)
Michael Buescheb189d82008-01-28 14:47:41 -0800280 extra_ft |= B43_TXH_EFT_FB_OFDM;
281 else
282 extra_ft |= B43_TXH_EFT_FB_CCK;
Michael Buesche4d6b792007-09-18 15:39:42 -0400283
284 /* Set channel radio code. Note that the micrcode ORs 0x100 to
285 * this value before comparing it to the value in SHM, if this
286 * is a 5Ghz packet.
287 */
288 txhdr->chan_radio_code = phy->channel;
289
290 /* PHY TX Control word */
291 if (rate_ofdm)
Michael Buescheb189d82008-01-28 14:47:41 -0800292 phy_ctl |= B43_TXH_PHY_ENC_OFDM;
293 else
294 phy_ctl |= B43_TXH_PHY_ENC_CCK;
Johannes Berg8318d782008-01-24 19:38:38 +0100295 if (txctl->flags & IEEE80211_TXCTL_SHORT_PREAMBLE)
Michael Buescheb189d82008-01-28 14:47:41 -0800296 phy_ctl |= B43_TXH_PHY_SHORTPRMBL;
Michael Buesch9db1f6d2007-12-22 21:54:20 +0100297
298 switch (b43_ieee80211_antenna_sanitize(dev, txctl->antenna_sel_tx)) {
299 case 0: /* Default */
Michael Buescheb189d82008-01-28 14:47:41 -0800300 phy_ctl |= B43_TXH_PHY_ANT01AUTO;
Michael Buesche4d6b792007-09-18 15:39:42 -0400301 break;
Michael Buesch9db1f6d2007-12-22 21:54:20 +0100302 case 1: /* Antenna 0 */
Michael Buescheb189d82008-01-28 14:47:41 -0800303 phy_ctl |= B43_TXH_PHY_ANT0;
Michael Buesche4d6b792007-09-18 15:39:42 -0400304 break;
Michael Buesch9db1f6d2007-12-22 21:54:20 +0100305 case 2: /* Antenna 1 */
Michael Buescheb189d82008-01-28 14:47:41 -0800306 phy_ctl |= B43_TXH_PHY_ANT1;
307 break;
308 case 3: /* Antenna 2 */
309 phy_ctl |= B43_TXH_PHY_ANT2;
310 break;
311 case 4: /* Antenna 3 */
312 phy_ctl |= B43_TXH_PHY_ANT3;
Michael Buesche4d6b792007-09-18 15:39:42 -0400313 break;
314 default:
315 B43_WARN_ON(1);
316 }
317
318 /* MAC control */
319 if (!(txctl->flags & IEEE80211_TXCTL_NO_ACK))
Michael Buescheb189d82008-01-28 14:47:41 -0800320 mac_ctl |= B43_TXH_MAC_ACK;
Michael Buesche4d6b792007-09-18 15:39:42 -0400321 if (!(((fctl & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_CTL) &&
322 ((fctl & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_PSPOLL)))
Michael Buescheb189d82008-01-28 14:47:41 -0800323 mac_ctl |= B43_TXH_MAC_HWSEQ;
Michael Buesche4d6b792007-09-18 15:39:42 -0400324 if (txctl->flags & IEEE80211_TXCTL_FIRST_FRAGMENT)
Michael Buescheb189d82008-01-28 14:47:41 -0800325 mac_ctl |= B43_TXH_MAC_STMSDU;
Michael Buesche4d6b792007-09-18 15:39:42 -0400326 if (phy->type == B43_PHYTYPE_A)
Michael Buescheb189d82008-01-28 14:47:41 -0800327 mac_ctl |= B43_TXH_MAC_5GHZ;
Michael Buesch74cfdba2007-10-28 16:19:44 +0100328 if (txctl->flags & IEEE80211_TXCTL_LONG_RETRY_LIMIT)
Michael Buescheb189d82008-01-28 14:47:41 -0800329 mac_ctl |= B43_TXH_MAC_LONGFRAME;
Michael Buesche4d6b792007-09-18 15:39:42 -0400330
331 /* Generate the RTS or CTS-to-self frame */
332 if ((txctl->flags & IEEE80211_TXCTL_USE_RTS_CTS) ||
333 (txctl->flags & IEEE80211_TXCTL_USE_CTS_PROTECT)) {
334 unsigned int len;
335 struct ieee80211_hdr *hdr;
336 int rts_rate, rts_rate_fb;
337 int rts_rate_ofdm, rts_rate_fb_ofdm;
Michael Buescheb189d82008-01-28 14:47:41 -0800338 struct b43_plcp_hdr6 *plcp;
Michael Buesche4d6b792007-09-18 15:39:42 -0400339
Johannes Berg8318d782008-01-24 19:38:38 +0100340 WARN_ON(!txctl->rts_cts_rate);
341 rts_rate = txctl->rts_cts_rate ? txctl->rts_cts_rate->hw_value : B43_CCK_RATE_1MB;
Michael Buesche4d6b792007-09-18 15:39:42 -0400342 rts_rate_ofdm = b43_is_ofdm_rate(rts_rate);
343 rts_rate_fb = b43_calc_fallback_rate(rts_rate);
344 rts_rate_fb_ofdm = b43_is_ofdm_rate(rts_rate_fb);
345
346 if (txctl->flags & IEEE80211_TXCTL_USE_CTS_PROTECT) {
Michael Buescheb189d82008-01-28 14:47:41 -0800347 struct ieee80211_cts *cts;
348
349 if (b43_is_old_txhdr_format(dev)) {
350 cts = (struct ieee80211_cts *)
351 (txhdr->old_format.rts_frame);
352 } else {
353 cts = (struct ieee80211_cts *)
354 (txhdr->new_format.rts_frame);
355 }
Johannes Berg32bfd352007-12-19 01:31:26 +0100356 ieee80211_ctstoself_get(dev->wl->hw, txctl->vif,
Michael Buesche4d6b792007-09-18 15:39:42 -0400357 fragment_data, fragment_len,
Michael Buescheb189d82008-01-28 14:47:41 -0800358 txctl, cts);
359 mac_ctl |= B43_TXH_MAC_SENDCTS;
Michael Buesche4d6b792007-09-18 15:39:42 -0400360 len = sizeof(struct ieee80211_cts);
361 } else {
Michael Buescheb189d82008-01-28 14:47:41 -0800362 struct ieee80211_rts *rts;
363
364 if (b43_is_old_txhdr_format(dev)) {
365 rts = (struct ieee80211_rts *)
366 (txhdr->old_format.rts_frame);
367 } else {
368 rts = (struct ieee80211_rts *)
369 (txhdr->new_format.rts_frame);
370 }
Johannes Berg32bfd352007-12-19 01:31:26 +0100371 ieee80211_rts_get(dev->wl->hw, txctl->vif,
Michael Buescheb189d82008-01-28 14:47:41 -0800372 fragment_data, fragment_len,
373 txctl, rts);
374 mac_ctl |= B43_TXH_MAC_SENDRTS;
Michael Buesche4d6b792007-09-18 15:39:42 -0400375 len = sizeof(struct ieee80211_rts);
376 }
377 len += FCS_LEN;
Michael Buescheb189d82008-01-28 14:47:41 -0800378
379 /* Generate the PLCP headers for the RTS/CTS frame */
380 if (b43_is_old_txhdr_format(dev))
381 plcp = &txhdr->old_format.rts_plcp;
382 else
383 plcp = &txhdr->new_format.rts_plcp;
384 b43_generate_plcp_hdr((struct b43_plcp_hdr4 *)plcp,
385 len, rts_rate);
386 plcp = &txhdr->rts_plcp_fb;
387 b43_generate_plcp_hdr((struct b43_plcp_hdr4 *)plcp,
Michael Buesche4d6b792007-09-18 15:39:42 -0400388 len, rts_rate_fb);
Michael Buescheb189d82008-01-28 14:47:41 -0800389
390 if (b43_is_old_txhdr_format(dev)) {
391 hdr = (struct ieee80211_hdr *)
392 (&txhdr->old_format.rts_frame);
393 } else {
394 hdr = (struct ieee80211_hdr *)
395 (&txhdr->new_format.rts_frame);
396 }
Michael Buesche4d6b792007-09-18 15:39:42 -0400397 txhdr->rts_dur_fb = hdr->duration_id;
Michael Buescheb189d82008-01-28 14:47:41 -0800398
Michael Buesche4d6b792007-09-18 15:39:42 -0400399 if (rts_rate_ofdm) {
Michael Buescheb189d82008-01-28 14:47:41 -0800400 extra_ft |= B43_TXH_EFT_RTS_OFDM;
Michael Buesche4d6b792007-09-18 15:39:42 -0400401 txhdr->phy_rate_rts =
402 b43_plcp_get_ratecode_ofdm(rts_rate);
Michael Buescheb189d82008-01-28 14:47:41 -0800403 } else {
404 extra_ft |= B43_TXH_EFT_RTS_CCK;
Michael Buesche4d6b792007-09-18 15:39:42 -0400405 txhdr->phy_rate_rts =
406 b43_plcp_get_ratecode_cck(rts_rate);
Michael Buescheb189d82008-01-28 14:47:41 -0800407 }
Michael Buesche4d6b792007-09-18 15:39:42 -0400408 if (rts_rate_fb_ofdm)
Michael Buescheb189d82008-01-28 14:47:41 -0800409 extra_ft |= B43_TXH_EFT_RTSFB_OFDM;
410 else
411 extra_ft |= B43_TXH_EFT_RTSFB_CCK;
Michael Buesche4d6b792007-09-18 15:39:42 -0400412 }
413
414 /* Magic cookie */
Michael Buescheb189d82008-01-28 14:47:41 -0800415 if (b43_is_old_txhdr_format(dev))
416 txhdr->old_format.cookie = cpu_to_le16(cookie);
417 else
418 txhdr->new_format.cookie = cpu_to_le16(cookie);
Michael Buesche4d6b792007-09-18 15:39:42 -0400419
420 /* Apply the bitfields */
421 txhdr->mac_ctl = cpu_to_le32(mac_ctl);
422 txhdr->phy_ctl = cpu_to_le16(phy_ctl);
423 txhdr->extra_ft = extra_ft;
Michael Buesche4d6b792007-09-18 15:39:42 -0400424
Michael Buesch09552cc2008-01-23 21:44:15 +0100425 return 0;
Michael Buesche4d6b792007-09-18 15:39:42 -0400426}
427
428static s8 b43_rssi_postprocess(struct b43_wldev *dev,
429 u8 in_rssi, int ofdm,
430 int adjust_2053, int adjust_2050)
431{
432 struct b43_phy *phy = &dev->phy;
433 s32 tmp;
434
435 switch (phy->radio_ver) {
436 case 0x2050:
437 if (ofdm) {
438 tmp = in_rssi;
439 if (tmp > 127)
440 tmp -= 256;
441 tmp *= 73;
442 tmp /= 64;
443 if (adjust_2050)
444 tmp += 25;
445 else
446 tmp -= 3;
447 } else {
Larry Finger95de2842007-11-09 16:57:18 -0600448 if (dev->dev->bus->sprom.
Michael Buesche4d6b792007-09-18 15:39:42 -0400449 boardflags_lo & B43_BFL_RSSI) {
450 if (in_rssi > 63)
451 in_rssi = 63;
452 tmp = phy->nrssi_lt[in_rssi];
453 tmp = 31 - tmp;
454 tmp *= -131;
455 tmp /= 128;
456 tmp -= 57;
457 } else {
458 tmp = in_rssi;
459 tmp = 31 - tmp;
460 tmp *= -149;
461 tmp /= 128;
462 tmp -= 68;
463 }
464 if (phy->type == B43_PHYTYPE_G && adjust_2050)
465 tmp += 25;
466 }
467 break;
468 case 0x2060:
469 if (in_rssi > 127)
470 tmp = in_rssi - 256;
471 else
472 tmp = in_rssi;
473 break;
474 default:
475 tmp = in_rssi;
476 tmp -= 11;
477 tmp *= 103;
478 tmp /= 64;
479 if (adjust_2053)
480 tmp -= 109;
481 else
482 tmp -= 83;
483 }
484
485 return (s8) tmp;
486}
487
488//TODO
489#if 0
490static s8 b43_rssinoise_postprocess(struct b43_wldev *dev, u8 in_rssi)
491{
492 struct b43_phy *phy = &dev->phy;
493 s8 ret;
494
495 if (phy->type == B43_PHYTYPE_A) {
496 //TODO: Incomplete specs.
497 ret = 0;
498 } else
499 ret = b43_rssi_postprocess(dev, in_rssi, 0, 1, 1);
500
501 return ret;
502}
503#endif
504
505void b43_rx(struct b43_wldev *dev, struct sk_buff *skb, const void *_rxhdr)
506{
507 struct ieee80211_rx_status status;
508 struct b43_plcp_hdr6 *plcp;
509 struct ieee80211_hdr *wlhdr;
510 const struct b43_rxhdr_fw4 *rxhdr = _rxhdr;
511 u16 fctl;
512 u16 phystat0, phystat3, chanstat, mactime;
513 u32 macstat;
514 u16 chanid;
Johannes Berg8318d782008-01-24 19:38:38 +0100515 u16 phytype;
Michael Buesche4d6b792007-09-18 15:39:42 -0400516 u8 jssi;
517 int padding;
518
519 memset(&status, 0, sizeof(status));
520
521 /* Get metadata about the frame from the header. */
522 phystat0 = le16_to_cpu(rxhdr->phy_status0);
523 phystat3 = le16_to_cpu(rxhdr->phy_status3);
524 jssi = rxhdr->jssi;
525 macstat = le32_to_cpu(rxhdr->mac_status);
526 mactime = le16_to_cpu(rxhdr->mac_time);
527 chanstat = le16_to_cpu(rxhdr->channel);
Johannes Berg8318d782008-01-24 19:38:38 +0100528 phytype = chanstat & B43_RX_CHAN_PHYTYPE;
Michael Buesche4d6b792007-09-18 15:39:42 -0400529
530 if (macstat & B43_RX_MAC_FCSERR)
531 dev->wl->ieee_stats.dot11FCSErrorCount++;
532 if (macstat & B43_RX_MAC_DECERR) {
533 /* Decryption with the given key failed.
534 * Drop the packet. We also won't be able to decrypt it with
535 * the key in software. */
536 goto drop;
537 }
538
539 /* Skip PLCP and padding */
540 padding = (macstat & B43_RX_MAC_PADDING) ? 2 : 0;
541 if (unlikely(skb->len < (sizeof(struct b43_plcp_hdr6) + padding))) {
542 b43dbg(dev->wl, "RX: Packet size underrun (1)\n");
543 goto drop;
544 }
545 plcp = (struct b43_plcp_hdr6 *)(skb->data + padding);
546 skb_pull(skb, sizeof(struct b43_plcp_hdr6) + padding);
547 /* The skb contains the Wireless Header + payload data now */
548 if (unlikely(skb->len < (2 + 2 + 6 /*minimum hdr */ + FCS_LEN))) {
549 b43dbg(dev->wl, "RX: Packet size underrun (2)\n");
550 goto drop;
551 }
552 wlhdr = (struct ieee80211_hdr *)(skb->data);
553 fctl = le16_to_cpu(wlhdr->frame_control);
Michael Buesche4d6b792007-09-18 15:39:42 -0400554
555 if (macstat & B43_RX_MAC_DEC) {
556 unsigned int keyidx;
557 int wlhdr_len;
558
559 keyidx = ((macstat & B43_RX_MAC_KEYIDX)
560 >> B43_RX_MAC_KEYIDX_SHIFT);
561 /* We must adjust the key index here. We want the "physical"
562 * key index, but the ucode passed it slightly different.
563 */
564 keyidx = b43_kidx_to_raw(dev, keyidx);
565 B43_WARN_ON(keyidx >= dev->max_nr_keys);
566
567 if (dev->key[keyidx].algorithm != B43_SEC_ALGO_NONE) {
568 wlhdr_len = ieee80211_get_hdrlen(fctl);
569 if (unlikely(skb->len < (wlhdr_len + 3))) {
570 b43dbg(dev->wl,
571 "RX: Packet size underrun (3)\n");
572 goto drop;
573 }
574 status.flag |= RX_FLAG_DECRYPTED;
575 }
576 }
577
578 status.ssi = b43_rssi_postprocess(dev, jssi,
579 (phystat0 & B43_RX_PHYST0_OFDM),
580 (phystat0 & B43_RX_PHYST0_GAINCTL),
581 (phystat3 & B43_RX_PHYST3_TRSTATE));
582 status.noise = dev->stats.link_noise;
583 /* the next line looks wrong, but is what mac80211 wants */
584 status.signal = (jssi * 100) / B43_RX_MAX_SSI;
585 if (phystat0 & B43_RX_PHYST0_OFDM)
Johannes Berg8318d782008-01-24 19:38:38 +0100586 status.rate_idx = b43_plcp_get_bitrate_idx_ofdm(plcp,
587 phytype == B43_PHYTYPE_A);
Michael Buesche4d6b792007-09-18 15:39:42 -0400588 else
Johannes Berg8318d782008-01-24 19:38:38 +0100589 status.rate_idx = b43_plcp_get_bitrate_idx_cck(plcp);
Michael Buesche4d6b792007-09-18 15:39:42 -0400590 status.antenna = !!(phystat0 & B43_RX_PHYST0_ANT);
John W. Linvillec0ddd042008-01-21 13:41:18 -0500591
592 /*
Johannes Bergd007b7f2008-02-18 18:53:55 +0100593 * All frames on monitor interfaces and beacons always need a full
594 * 64-bit timestamp. Monitor interfaces need it for diagnostic
595 * purposes and beacons for IBSS merging.
596 * This code assumes we get to process the packet within 16 bits
597 * of timestamp, i.e. about 65 milliseconds after the PHY received
598 * the first symbol.
John W. Linvillec0ddd042008-01-21 13:41:18 -0500599 */
Johannes Bergd007b7f2008-02-18 18:53:55 +0100600 if (((fctl & (IEEE80211_FCTL_FTYPE | IEEE80211_FCTL_STYPE))
601 == (IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_BEACON)) ||
602 dev->wl->radiotap_enabled) {
John W. Linvillec0ddd042008-01-21 13:41:18 -0500603 u16 low_mactime_now;
604
605 b43_tsf_read(dev, &status.mactime);
606 low_mactime_now = status.mactime;
607 status.mactime = status.mactime & ~0xFFFFULL;
608 status.mactime += mactime;
609 if (low_mactime_now <= mactime)
610 status.mactime -= 0x10000;
611 status.flag |= RX_FLAG_TSFT;
612 }
Michael Buesche4d6b792007-09-18 15:39:42 -0400613
614 chanid = (chanstat & B43_RX_CHAN_ID) >> B43_RX_CHAN_ID_SHIFT;
615 switch (chanstat & B43_RX_CHAN_PHYTYPE) {
616 case B43_PHYTYPE_A:
Johannes Berg8318d782008-01-24 19:38:38 +0100617 status.band = IEEE80211_BAND_5GHZ;
Michael Bueschd9871602008-01-02 18:55:53 +0100618 B43_WARN_ON(1);
619 /* FIXME: We don't really know which value the "chanid" contains.
620 * So the following assignment might be wrong. */
Johannes Berg8318d782008-01-24 19:38:38 +0100621 status.freq = b43_channel_to_freq_5ghz(chanid);
Michael Buesche4d6b792007-09-18 15:39:42 -0400622 break;
623 case B43_PHYTYPE_G:
Johannes Berg8318d782008-01-24 19:38:38 +0100624 status.band = IEEE80211_BAND_2GHZ;
Michael Bueschd9871602008-01-02 18:55:53 +0100625 /* chanid is the radio channel cookie value as used
626 * to tune the radio. */
Michael Buesche4d6b792007-09-18 15:39:42 -0400627 status.freq = chanid + 2400;
Michael Bueschd9871602008-01-02 18:55:53 +0100628 break;
629 case B43_PHYTYPE_N:
Michael Bueschd9871602008-01-02 18:55:53 +0100630 /* chanid is the SHM channel cookie. Which is the plain
631 * channel number in b43. */
Johannes Berg8318d782008-01-24 19:38:38 +0100632 if (chanstat & B43_RX_CHAN_5GHZ) {
633 status.band = IEEE80211_BAND_5GHZ;
634 status.freq = b43_freq_to_channel_5ghz(chanid);
635 } else {
636 status.band = IEEE80211_BAND_2GHZ;
637 status.freq = b43_freq_to_channel_2ghz(chanid);
638 }
Michael Buesche4d6b792007-09-18 15:39:42 -0400639 break;
640 default:
641 B43_WARN_ON(1);
Michael Bueschd9871602008-01-02 18:55:53 +0100642 goto drop;
Michael Buesche4d6b792007-09-18 15:39:42 -0400643 }
644
645 dev->stats.last_rx = jiffies;
646 ieee80211_rx_irqsafe(dev->wl->hw, skb, &status);
647
648 return;
649drop:
650 b43dbg(dev->wl, "RX: Packet dropped\n");
651 dev_kfree_skb_any(skb);
652}
653
654void b43_handle_txstatus(struct b43_wldev *dev,
655 const struct b43_txstatus *status)
656{
657 b43_debugfs_log_txstat(dev, status);
658
659 if (status->intermediate)
660 return;
661 if (status->for_ampdu)
662 return;
663 if (!status->acked)
664 dev->wl->ieee_stats.dot11ACKFailureCount++;
665 if (status->rts_count) {
666 if (status->rts_count == 0xF) //FIXME
667 dev->wl->ieee_stats.dot11RTSFailureCount++;
668 else
669 dev->wl->ieee_stats.dot11RTSSuccessCount++;
670 }
671
Michael Buesch5100d5a2008-03-29 21:01:16 +0100672 if (b43_using_pio_transfers(dev))
673 b43_pio_handle_txstatus(dev, status);
674 else
675 b43_dma_handle_txstatus(dev, status);
Michael Buesche4d6b792007-09-18 15:39:42 -0400676}
677
Michael Buesch5100d5a2008-03-29 21:01:16 +0100678/* Fill out the mac80211 TXstatus report based on the b43-specific
679 * txstatus report data. This returns a boolean whether the frame was
680 * successfully transmitted. */
681bool b43_fill_txstatus_report(struct ieee80211_tx_status *report,
682 const struct b43_txstatus *status)
Michael Buesche4d6b792007-09-18 15:39:42 -0400683{
Michael Buesch5100d5a2008-03-29 21:01:16 +0100684 bool frame_success = 1;
Michael Buesche4d6b792007-09-18 15:39:42 -0400685
Michael Buesch5100d5a2008-03-29 21:01:16 +0100686 if (status->acked) {
687 /* The frame was ACKed. */
688 report->flags |= IEEE80211_TX_STATUS_ACK;
689 } else {
690 /* The frame was not ACKed... */
691 if (!(report->control.flags & IEEE80211_TXCTL_NO_ACK)) {
692 /* ...but we expected an ACK. */
693 frame_success = 0;
694 report->excessive_retries = 1;
695 }
696 }
697 if (status->frame_count == 0) {
698 /* The frame was not transmitted at all. */
699 report->retry_count = 0;
700 } else
701 report->retry_count = status->frame_count - 1;
Michael Buesche4d6b792007-09-18 15:39:42 -0400702
Michael Buesch5100d5a2008-03-29 21:01:16 +0100703 return frame_success;
Michael Buesche4d6b792007-09-18 15:39:42 -0400704}
705
706/* Stop any TX operation on the device (suspend the hardware queues) */
707void b43_tx_suspend(struct b43_wldev *dev)
708{
Michael Buesch5100d5a2008-03-29 21:01:16 +0100709 if (b43_using_pio_transfers(dev))
710 b43_pio_tx_suspend(dev);
711 else
712 b43_dma_tx_suspend(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -0400713}
714
715/* Resume any TX operation on the device (resume the hardware queues) */
716void b43_tx_resume(struct b43_wldev *dev)
717{
Michael Buesch5100d5a2008-03-29 21:01:16 +0100718 if (b43_using_pio_transfers(dev))
719 b43_pio_tx_resume(dev);
720 else
721 b43_dma_tx_resume(dev);
Michael Buesche4d6b792007-09-18 15:39:42 -0400722}