Larry Finger | 75388ac | 2007-09-25 16:46:54 -0700 | [diff] [blame] | 1 | /* |
| 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 Brivio | 1f21ad2 | 2007-11-06 22:49:20 +0100 | [diff] [blame] | 8 | Copyright (C) 2005 Stefano Brivio <stefano.brivio@polimi.it> |
Larry Finger | 75388ac | 2007-09-25 16:46:54 -0700 | [diff] [blame] | 9 | Copyright (C) 2005, 2006 Michael Buesch <mb@bu3sch.de> |
| 10 | Copyright (C) 2005 Danny van Dyk <kugelfang@gentoo.org> |
| 11 | Copyright (C) 2005 Andreas Jaggi <andreas.jaggi@waterwave.ch> |
| 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 Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 40 | static u8 b43legacy_plcp_get_bitrate_idx_cck(struct b43legacy_plcp_hdr6 *plcp) |
Larry Finger | 75388ac | 2007-09-25 16:46:54 -0700 | [diff] [blame] | 41 | { |
| 42 | switch (plcp->raw[0]) { |
| 43 | case 0x0A: |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 44 | return 0; |
Larry Finger | 75388ac | 2007-09-25 16:46:54 -0700 | [diff] [blame] | 45 | case 0x14: |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 46 | return 1; |
Larry Finger | 75388ac | 2007-09-25 16:46:54 -0700 | [diff] [blame] | 47 | case 0x37: |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 48 | return 2; |
Larry Finger | 75388ac | 2007-09-25 16:46:54 -0700 | [diff] [blame] | 49 | case 0x6E: |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 50 | return 3; |
Larry Finger | 75388ac | 2007-09-25 16:46:54 -0700 | [diff] [blame] | 51 | } |
| 52 | B43legacy_BUG_ON(1); |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 53 | return -1; |
Larry Finger | 75388ac | 2007-09-25 16:46:54 -0700 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | /* Extract the bitrate out of an OFDM PLCP header. */ |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 57 | static u8 b43legacy_plcp_get_bitrate_idx_ofdm(struct b43legacy_plcp_hdr6 *plcp, |
| 58 | bool aphy) |
Larry Finger | 75388ac | 2007-09-25 16:46:54 -0700 | [diff] [blame] | 59 | { |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 60 | int base = aphy ? 0 : 4; |
| 61 | |
Larry Finger | 75388ac | 2007-09-25 16:46:54 -0700 | [diff] [blame] | 62 | switch (plcp->raw[0] & 0xF) { |
| 63 | case 0xB: |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 64 | return base + 0; |
Larry Finger | 75388ac | 2007-09-25 16:46:54 -0700 | [diff] [blame] | 65 | case 0xF: |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 66 | return base + 1; |
Larry Finger | 75388ac | 2007-09-25 16:46:54 -0700 | [diff] [blame] | 67 | case 0xA: |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 68 | return base + 2; |
Larry Finger | 75388ac | 2007-09-25 16:46:54 -0700 | [diff] [blame] | 69 | case 0xE: |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 70 | return base + 3; |
Larry Finger | 75388ac | 2007-09-25 16:46:54 -0700 | [diff] [blame] | 71 | case 0x9: |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 72 | return base + 4; |
Larry Finger | 75388ac | 2007-09-25 16:46:54 -0700 | [diff] [blame] | 73 | case 0xD: |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 74 | return base + 5; |
Larry Finger | 75388ac | 2007-09-25 16:46:54 -0700 | [diff] [blame] | 75 | case 0x8: |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 76 | return base + 6; |
Larry Finger | 75388ac | 2007-09-25 16:46:54 -0700 | [diff] [blame] | 77 | case 0xC: |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 78 | return base + 7; |
Larry Finger | 75388ac | 2007-09-25 16:46:54 -0700 | [diff] [blame] | 79 | } |
| 80 | B43legacy_BUG_ON(1); |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 81 | return -1; |
Larry Finger | 75388ac | 2007-09-25 16:46:54 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | u8 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 | |
| 100 | u8 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 | |
| 124 | void 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 | |
| 155 | static 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 Brivio | 9eca9a8 | 2008-02-02 19:16:01 +0100 | [diff] [blame] | 187 | static int generate_txhdr_fw3(struct b43legacy_wldev *dev, |
Larry Finger | 75388ac | 2007-09-25 16:46:54 -0700 | [diff] [blame] | 188 | struct b43legacy_txhdr_fw3 *txhdr, |
| 189 | const unsigned char *fragment_data, |
| 190 | unsigned int fragment_len, |
Johannes Berg | e6a9854 | 2008-10-21 12:40:02 +0200 | [diff] [blame] | 191 | struct ieee80211_tx_info *info, |
Larry Finger | 75388ac | 2007-09-25 16:46:54 -0700 | [diff] [blame] | 192 | u16 cookie) |
| 193 | { |
| 194 | const struct ieee80211_hdr *wlhdr; |
Johannes Berg | d0f0980 | 2008-07-29 11:32:07 +0200 | [diff] [blame] | 195 | int use_encryption = !!info->control.hw_key; |
Larry Finger | 75388ac | 2007-09-25 16:46:54 -0700 | [diff] [blame] | 196 | u8 rate; |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 197 | struct ieee80211_rate *rate_fb; |
Larry Finger | 75388ac | 2007-09-25 16:46:54 -0700 | [diff] [blame] | 198 | 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 Berg | 2e92e6f | 2008-05-15 12:55:27 +0200 | [diff] [blame] | 203 | struct ieee80211_rate *tx_rate; |
Johannes Berg | e6a9854 | 2008-10-21 12:40:02 +0200 | [diff] [blame] | 204 | struct ieee80211_tx_rate *rates; |
Larry Finger | 75388ac | 2007-09-25 16:46:54 -0700 | [diff] [blame] | 205 | |
| 206 | wlhdr = (const struct ieee80211_hdr *)fragment_data; |
Larry Finger | 75388ac | 2007-09-25 16:46:54 -0700 | [diff] [blame] | 207 | |
| 208 | memset(txhdr, 0, sizeof(*txhdr)); |
| 209 | |
Johannes Berg | e039fa4 | 2008-05-15 12:55:29 +0200 | [diff] [blame] | 210 | tx_rate = ieee80211_get_tx_rate(dev->wl->hw, info); |
Johannes Berg | 2e92e6f | 2008-05-15 12:55:27 +0200 | [diff] [blame] | 211 | |
| 212 | rate = tx_rate->hw_value; |
Larry Finger | 75388ac | 2007-09-25 16:46:54 -0700 | [diff] [blame] | 213 | rate_ofdm = b43legacy_is_ofdm_rate(rate); |
Felix Fietkau | 870abdf | 2008-10-05 18:04:24 +0200 | [diff] [blame] | 214 | rate_fb = ieee80211_get_alt_retry_rate(dev->wl->hw, info, 0) ? : tx_rate; |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 215 | rate_fb_ofdm = b43legacy_is_ofdm_rate(rate_fb->hw_value); |
Larry Finger | 75388ac | 2007-09-25 16:46:54 -0700 | [diff] [blame] | 216 | |
| 217 | txhdr->mac_frame_ctl = wlhdr->frame_control; |
| 218 | memcpy(txhdr->tx_receiver, wlhdr->addr1, 6); |
| 219 | |
| 220 | /* Calculate duration for fallback rate */ |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 221 | if ((rate_fb->hw_value == rate) || |
Larry Finger | 75388ac | 2007-09-25 16:46:54 -0700 | [diff] [blame] | 222 | (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 Finger | 75388ac | 2007-09-25 16:46:54 -0700 | [diff] [blame] | 229 | txhdr->dur_fb = ieee80211_generic_frame_duration(dev->wl->hw, |
Johannes Berg | e039fa4 | 2008-05-15 12:55:29 +0200 | [diff] [blame] | 230 | info->control.vif, |
Larry Finger | 75388ac | 2007-09-25 16:46:54 -0700 | [diff] [blame] | 231 | fragment_len, |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 232 | rate_fb); |
Larry Finger | 75388ac | 2007-09-25 16:46:54 -0700 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | plcp_fragment_len = fragment_len + FCS_LEN; |
| 236 | if (use_encryption) { |
Johannes Berg | e039fa4 | 2008-05-15 12:55:29 +0200 | [diff] [blame] | 237 | u8 key_idx = info->control.hw_key->hw_key_idx; |
Larry Finger | 75388ac | 2007-09-25 16:46:54 -0700 | [diff] [blame] | 238 | 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 Fietkau | 76708de | 2008-10-05 18:02:48 +0200 | [diff] [blame] | 247 | plcp_fragment_len += info->control.hw_key->icv_len; |
Larry Finger | 75388ac | 2007-09-25 16:46:54 -0700 | [diff] [blame] | 248 | |
| 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 Harrison | ba5b6ef | 2008-07-15 18:43:56 -0700 | [diff] [blame] | 255 | wlhdr_len = ieee80211_hdrlen(wlhdr->frame_control); |
Felix Fietkau | 76708de | 2008-10-05 18:02:48 +0200 | [diff] [blame] | 256 | iv_len = min((size_t)info->control.hw_key->iv_len, |
Larry Finger | 75388ac | 2007-09-25 16:46:54 -0700 | [diff] [blame] | 257 | ARRAY_SIZE(txhdr->iv)); |
| 258 | memcpy(txhdr->iv, ((u8 *)wlhdr) + wlhdr_len, iv_len); |
Stefano Brivio | 9eca9a8 | 2008-02-02 19:16:01 +0100 | [diff] [blame] | 259 | } 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 Finger | 75388ac | 2007-09-25 16:46:54 -0700 | [diff] [blame] | 266 | } |
| 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 Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 273 | rate_fb->hw_value); |
Larry Finger | 75388ac | 2007-09-25 16:46:54 -0700 | [diff] [blame] | 274 | |
| 275 | /* PHY TX Control word */ |
| 276 | if (rate_ofdm) |
Larry Finger | 2d1f96d | 2009-04-11 11:26:01 -0500 | [diff] [blame] | 277 | phy_ctl |= B43legacy_TX4_PHY_ENC_OFDM; |
Johannes Berg | e6a9854 | 2008-10-21 12:40:02 +0200 | [diff] [blame] | 278 | if (info->control.rates[0].flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE) |
Larry Finger | 75388ac | 2007-09-25 16:46:54 -0700 | [diff] [blame] | 279 | phy_ctl |= B43legacy_TX4_PHY_SHORTPRMBL; |
Johannes Berg | e039fa4 | 2008-05-15 12:55:29 +0200 | [diff] [blame] | 280 | switch (info->antenna_sel_tx) { |
Larry Finger | 75388ac | 2007-09-25 16:46:54 -0700 | [diff] [blame] | 281 | case 0: |
| 282 | phy_ctl |= B43legacy_TX4_PHY_ANTLAST; |
| 283 | break; |
| 284 | case 1: |
| 285 | phy_ctl |= B43legacy_TX4_PHY_ANT0; |
| 286 | break; |
| 287 | case 2: |
| 288 | phy_ctl |= B43legacy_TX4_PHY_ANT1; |
| 289 | break; |
| 290 | default: |
| 291 | B43legacy_BUG_ON(1); |
| 292 | } |
| 293 | |
| 294 | /* MAC control */ |
Johannes Berg | e6a9854 | 2008-10-21 12:40:02 +0200 | [diff] [blame] | 295 | rates = info->control.rates; |
Johannes Berg | e039fa4 | 2008-05-15 12:55:29 +0200 | [diff] [blame] | 296 | if (!(info->flags & IEEE80211_TX_CTL_NO_ACK)) |
Larry Finger | 75388ac | 2007-09-25 16:46:54 -0700 | [diff] [blame] | 297 | mac_ctl |= B43legacy_TX4_MAC_ACK; |
Johannes Berg | f591fa5 | 2008-07-10 11:21:26 +0200 | [diff] [blame] | 298 | if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) |
Larry Finger | 75388ac | 2007-09-25 16:46:54 -0700 | [diff] [blame] | 299 | mac_ctl |= B43legacy_TX4_MAC_HWSEQ; |
Johannes Berg | e039fa4 | 2008-05-15 12:55:29 +0200 | [diff] [blame] | 300 | if (info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT) |
Larry Finger | 75388ac | 2007-09-25 16:46:54 -0700 | [diff] [blame] | 301 | mac_ctl |= B43legacy_TX4_MAC_STMSDU; |
| 302 | if (rate_fb_ofdm) |
| 303 | mac_ctl |= B43legacy_TX4_MAC_FALLBACKOFDM; |
Johannes Berg | e6a9854 | 2008-10-21 12:40:02 +0200 | [diff] [blame] | 304 | |
| 305 | /* Overwrite rates[0].count to make the retry calculation |
| 306 | * in the tx status easier. need the actual retry limit to |
| 307 | * detect whether the fallback rate was used. |
| 308 | */ |
| 309 | if ((rates[0].flags & IEEE80211_TX_RC_USE_RTS_CTS) || |
| 310 | (rates[0].count <= dev->wl->hw->conf.long_frame_max_tx_count)) { |
| 311 | rates[0].count = dev->wl->hw->conf.long_frame_max_tx_count; |
Stefano Brivio | 0a6e1be | 2007-11-06 22:48:12 +0100 | [diff] [blame] | 312 | mac_ctl |= B43legacy_TX4_MAC_LONGFRAME; |
Johannes Berg | e6a9854 | 2008-10-21 12:40:02 +0200 | [diff] [blame] | 313 | } else { |
| 314 | rates[0].count = dev->wl->hw->conf.short_frame_max_tx_count; |
| 315 | } |
Larry Finger | 75388ac | 2007-09-25 16:46:54 -0700 | [diff] [blame] | 316 | |
| 317 | /* Generate the RTS or CTS-to-self frame */ |
Johannes Berg | e6a9854 | 2008-10-21 12:40:02 +0200 | [diff] [blame] | 318 | if ((rates[0].flags & IEEE80211_TX_RC_USE_RTS_CTS) || |
| 319 | (rates[0].flags & IEEE80211_TX_RC_USE_CTS_PROTECT)) { |
Larry Finger | 75388ac | 2007-09-25 16:46:54 -0700 | [diff] [blame] | 320 | unsigned int len; |
| 321 | struct ieee80211_hdr *hdr; |
| 322 | int rts_rate; |
| 323 | int rts_rate_fb; |
| 324 | int rts_rate_ofdm; |
| 325 | int rts_rate_fb_ofdm; |
| 326 | |
Johannes Berg | e039fa4 | 2008-05-15 12:55:29 +0200 | [diff] [blame] | 327 | rts_rate = ieee80211_get_rts_cts_rate(dev->wl->hw, info)->hw_value; |
Larry Finger | 75388ac | 2007-09-25 16:46:54 -0700 | [diff] [blame] | 328 | rts_rate_ofdm = b43legacy_is_ofdm_rate(rts_rate); |
| 329 | rts_rate_fb = b43legacy_calc_fallback_rate(rts_rate); |
| 330 | rts_rate_fb_ofdm = b43legacy_is_ofdm_rate(rts_rate_fb); |
| 331 | if (rts_rate_fb_ofdm) |
| 332 | mac_ctl |= B43legacy_TX4_MAC_CTSFALLBACKOFDM; |
| 333 | |
Johannes Berg | e6a9854 | 2008-10-21 12:40:02 +0200 | [diff] [blame] | 334 | if (rates[0].flags & IEEE80211_TX_RC_USE_CTS_PROTECT) { |
Larry Finger | 75388ac | 2007-09-25 16:46:54 -0700 | [diff] [blame] | 335 | ieee80211_ctstoself_get(dev->wl->hw, |
Johannes Berg | e039fa4 | 2008-05-15 12:55:29 +0200 | [diff] [blame] | 336 | info->control.vif, |
Larry Finger | 75388ac | 2007-09-25 16:46:54 -0700 | [diff] [blame] | 337 | fragment_data, |
Johannes Berg | e039fa4 | 2008-05-15 12:55:29 +0200 | [diff] [blame] | 338 | fragment_len, info, |
Larry Finger | 75388ac | 2007-09-25 16:46:54 -0700 | [diff] [blame] | 339 | (struct ieee80211_cts *) |
| 340 | (txhdr->rts_frame)); |
| 341 | mac_ctl |= B43legacy_TX4_MAC_SENDCTS; |
| 342 | len = sizeof(struct ieee80211_cts); |
| 343 | } else { |
| 344 | ieee80211_rts_get(dev->wl->hw, |
Johannes Berg | e039fa4 | 2008-05-15 12:55:29 +0200 | [diff] [blame] | 345 | info->control.vif, |
| 346 | fragment_data, fragment_len, info, |
Larry Finger | 75388ac | 2007-09-25 16:46:54 -0700 | [diff] [blame] | 347 | (struct ieee80211_rts *) |
| 348 | (txhdr->rts_frame)); |
| 349 | mac_ctl |= B43legacy_TX4_MAC_SENDRTS; |
| 350 | len = sizeof(struct ieee80211_rts); |
| 351 | } |
| 352 | len += FCS_LEN; |
| 353 | b43legacy_generate_plcp_hdr((struct b43legacy_plcp_hdr4 *) |
| 354 | (&txhdr->rts_plcp), |
| 355 | len, rts_rate); |
| 356 | b43legacy_generate_plcp_hdr((struct b43legacy_plcp_hdr4 *) |
| 357 | (&txhdr->rts_plcp_fb), |
| 358 | len, rts_rate_fb); |
| 359 | hdr = (struct ieee80211_hdr *)(&txhdr->rts_frame); |
| 360 | txhdr->rts_dur_fb = hdr->duration_id; |
Larry Finger | 75388ac | 2007-09-25 16:46:54 -0700 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | /* Magic cookie */ |
| 364 | txhdr->cookie = cpu_to_le16(cookie); |
| 365 | |
| 366 | /* Apply the bitfields */ |
| 367 | txhdr->mac_ctl = cpu_to_le32(mac_ctl); |
| 368 | txhdr->phy_ctl = cpu_to_le16(phy_ctl); |
Stefano Brivio | 9eca9a8 | 2008-02-02 19:16:01 +0100 | [diff] [blame] | 369 | |
| 370 | return 0; |
Larry Finger | 75388ac | 2007-09-25 16:46:54 -0700 | [diff] [blame] | 371 | } |
| 372 | |
Stefano Brivio | 9eca9a8 | 2008-02-02 19:16:01 +0100 | [diff] [blame] | 373 | int b43legacy_generate_txhdr(struct b43legacy_wldev *dev, |
Larry Finger | 75388ac | 2007-09-25 16:46:54 -0700 | [diff] [blame] | 374 | u8 *txhdr, |
| 375 | const unsigned char *fragment_data, |
| 376 | unsigned int fragment_len, |
Johannes Berg | e6a9854 | 2008-10-21 12:40:02 +0200 | [diff] [blame] | 377 | struct ieee80211_tx_info *info, |
Larry Finger | 75388ac | 2007-09-25 16:46:54 -0700 | [diff] [blame] | 378 | u16 cookie) |
| 379 | { |
Stefano Brivio | 9eca9a8 | 2008-02-02 19:16:01 +0100 | [diff] [blame] | 380 | return generate_txhdr_fw3(dev, (struct b43legacy_txhdr_fw3 *)txhdr, |
Larry Finger | 75388ac | 2007-09-25 16:46:54 -0700 | [diff] [blame] | 381 | fragment_data, fragment_len, |
Johannes Berg | e039fa4 | 2008-05-15 12:55:29 +0200 | [diff] [blame] | 382 | info, cookie); |
Larry Finger | 75388ac | 2007-09-25 16:46:54 -0700 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | static s8 b43legacy_rssi_postprocess(struct b43legacy_wldev *dev, |
| 386 | u8 in_rssi, int ofdm, |
| 387 | int adjust_2053, int adjust_2050) |
| 388 | { |
| 389 | struct b43legacy_phy *phy = &dev->phy; |
| 390 | s32 tmp; |
| 391 | |
| 392 | switch (phy->radio_ver) { |
| 393 | case 0x2050: |
| 394 | if (ofdm) { |
| 395 | tmp = in_rssi; |
| 396 | if (tmp > 127) |
| 397 | tmp -= 256; |
| 398 | tmp *= 73; |
| 399 | tmp /= 64; |
| 400 | if (adjust_2050) |
| 401 | tmp += 25; |
| 402 | else |
| 403 | tmp -= 3; |
| 404 | } else { |
Larry Finger | 7797aa3 | 2007-11-09 16:57:34 -0600 | [diff] [blame] | 405 | if (dev->dev->bus->sprom.boardflags_lo |
Larry Finger | 75388ac | 2007-09-25 16:46:54 -0700 | [diff] [blame] | 406 | & B43legacy_BFL_RSSI) { |
| 407 | if (in_rssi > 63) |
| 408 | in_rssi = 63; |
| 409 | tmp = phy->nrssi_lt[in_rssi]; |
| 410 | tmp = 31 - tmp; |
| 411 | tmp *= -131; |
| 412 | tmp /= 128; |
| 413 | tmp -= 57; |
| 414 | } else { |
| 415 | tmp = in_rssi; |
| 416 | tmp = 31 - tmp; |
| 417 | tmp *= -149; |
| 418 | tmp /= 128; |
| 419 | tmp -= 68; |
| 420 | } |
| 421 | if (phy->type == B43legacy_PHYTYPE_G && |
| 422 | adjust_2050) |
| 423 | tmp += 25; |
| 424 | } |
| 425 | break; |
| 426 | case 0x2060: |
| 427 | if (in_rssi > 127) |
| 428 | tmp = in_rssi - 256; |
| 429 | else |
| 430 | tmp = in_rssi; |
| 431 | break; |
| 432 | default: |
| 433 | tmp = in_rssi; |
| 434 | tmp -= 11; |
| 435 | tmp *= 103; |
| 436 | tmp /= 64; |
| 437 | if (adjust_2053) |
| 438 | tmp -= 109; |
| 439 | else |
| 440 | tmp -= 83; |
| 441 | } |
| 442 | |
| 443 | return (s8)tmp; |
| 444 | } |
| 445 | |
| 446 | void b43legacy_rx(struct b43legacy_wldev *dev, |
| 447 | struct sk_buff *skb, |
| 448 | const void *_rxhdr) |
| 449 | { |
| 450 | struct ieee80211_rx_status status; |
| 451 | struct b43legacy_plcp_hdr6 *plcp; |
| 452 | struct ieee80211_hdr *wlhdr; |
| 453 | const struct b43legacy_rxhdr_fw3 *rxhdr = _rxhdr; |
Harvey Harrison | b99a017 | 2008-06-14 23:33:40 -0700 | [diff] [blame] | 454 | __le16 fctl; |
Larry Finger | 75388ac | 2007-09-25 16:46:54 -0700 | [diff] [blame] | 455 | u16 phystat0; |
| 456 | u16 phystat3; |
| 457 | u16 chanstat; |
| 458 | u16 mactime; |
| 459 | u32 macstat; |
| 460 | u16 chanid; |
| 461 | u8 jssi; |
| 462 | int padding; |
| 463 | |
| 464 | memset(&status, 0, sizeof(status)); |
| 465 | |
| 466 | /* Get metadata about the frame from the header. */ |
| 467 | phystat0 = le16_to_cpu(rxhdr->phy_status0); |
| 468 | phystat3 = le16_to_cpu(rxhdr->phy_status3); |
| 469 | jssi = rxhdr->jssi; |
| 470 | macstat = le16_to_cpu(rxhdr->mac_status); |
| 471 | mactime = le16_to_cpu(rxhdr->mac_time); |
| 472 | chanstat = le16_to_cpu(rxhdr->channel); |
| 473 | |
| 474 | if (macstat & B43legacy_RX_MAC_FCSERR) |
| 475 | dev->wl->ieee_stats.dot11FCSErrorCount++; |
| 476 | |
| 477 | /* Skip PLCP and padding */ |
| 478 | padding = (macstat & B43legacy_RX_MAC_PADDING) ? 2 : 0; |
| 479 | if (unlikely(skb->len < (sizeof(struct b43legacy_plcp_hdr6) + |
| 480 | padding))) { |
| 481 | b43legacydbg(dev->wl, "RX: Packet size underrun (1)\n"); |
| 482 | goto drop; |
| 483 | } |
| 484 | plcp = (struct b43legacy_plcp_hdr6 *)(skb->data + padding); |
| 485 | skb_pull(skb, sizeof(struct b43legacy_plcp_hdr6) + padding); |
| 486 | /* The skb contains the Wireless Header + payload data now */ |
| 487 | if (unlikely(skb->len < (2+2+6/*minimum hdr*/ + FCS_LEN))) { |
| 488 | b43legacydbg(dev->wl, "RX: Packet size underrun (2)\n"); |
| 489 | goto drop; |
| 490 | } |
| 491 | wlhdr = (struct ieee80211_hdr *)(skb->data); |
Harvey Harrison | b99a017 | 2008-06-14 23:33:40 -0700 | [diff] [blame] | 492 | fctl = wlhdr->frame_control; |
Larry Finger | 75388ac | 2007-09-25 16:46:54 -0700 | [diff] [blame] | 493 | |
| 494 | if ((macstat & B43legacy_RX_MAC_DEC) && |
| 495 | !(macstat & B43legacy_RX_MAC_DECERR)) { |
| 496 | unsigned int keyidx; |
| 497 | int wlhdr_len; |
| 498 | int iv_len; |
| 499 | int icv_len; |
| 500 | |
| 501 | keyidx = ((macstat & B43legacy_RX_MAC_KEYIDX) |
| 502 | >> B43legacy_RX_MAC_KEYIDX_SHIFT); |
| 503 | /* We must adjust the key index here. We want the "physical" |
| 504 | * key index, but the ucode passed it slightly different. |
| 505 | */ |
| 506 | keyidx = b43legacy_kidx_to_raw(dev, keyidx); |
| 507 | B43legacy_WARN_ON(keyidx >= dev->max_nr_keys); |
| 508 | |
| 509 | if (dev->key[keyidx].algorithm != B43legacy_SEC_ALGO_NONE) { |
| 510 | /* Remove PROTECTED flag to mark it as decrypted. */ |
Harvey Harrison | b99a017 | 2008-06-14 23:33:40 -0700 | [diff] [blame] | 511 | B43legacy_WARN_ON(!ieee80211_has_protected(fctl)); |
| 512 | fctl &= ~cpu_to_le16(IEEE80211_FCTL_PROTECTED); |
| 513 | wlhdr->frame_control = fctl; |
Larry Finger | 75388ac | 2007-09-25 16:46:54 -0700 | [diff] [blame] | 514 | |
Harvey Harrison | b99a017 | 2008-06-14 23:33:40 -0700 | [diff] [blame] | 515 | wlhdr_len = ieee80211_hdrlen(fctl); |
Larry Finger | 75388ac | 2007-09-25 16:46:54 -0700 | [diff] [blame] | 516 | if (unlikely(skb->len < (wlhdr_len + 3))) { |
| 517 | b43legacydbg(dev->wl, "RX: Packet size" |
| 518 | " underrun3\n"); |
| 519 | goto drop; |
| 520 | } |
| 521 | if (skb->data[wlhdr_len + 3] & (1 << 5)) { |
| 522 | /* The Ext-IV Bit is set in the "KeyID" |
| 523 | * octet of the IV. |
| 524 | */ |
| 525 | iv_len = 8; |
| 526 | icv_len = 8; |
| 527 | } else { |
| 528 | iv_len = 4; |
| 529 | icv_len = 4; |
| 530 | } |
| 531 | if (unlikely(skb->len < (wlhdr_len + iv_len + |
| 532 | icv_len))) { |
| 533 | b43legacydbg(dev->wl, "RX: Packet size" |
| 534 | " underrun4\n"); |
| 535 | goto drop; |
| 536 | } |
| 537 | /* Remove the IV */ |
| 538 | memmove(skb->data + iv_len, skb->data, wlhdr_len); |
| 539 | skb_pull(skb, iv_len); |
| 540 | /* Remove the ICV */ |
| 541 | skb_trim(skb, skb->len - icv_len); |
| 542 | |
| 543 | status.flag |= RX_FLAG_DECRYPTED; |
| 544 | } |
| 545 | } |
| 546 | |
Bruno Randolf | 566bfe5 | 2008-05-08 19:15:40 +0200 | [diff] [blame] | 547 | status.signal = b43legacy_rssi_postprocess(dev, jssi, |
Larry Finger | 75388ac | 2007-09-25 16:46:54 -0700 | [diff] [blame] | 548 | (phystat0 & B43legacy_RX_PHYST0_OFDM), |
| 549 | (phystat0 & B43legacy_RX_PHYST0_GAINCTL), |
| 550 | (phystat3 & B43legacy_RX_PHYST3_TRSTATE)); |
| 551 | status.noise = dev->stats.link_noise; |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 552 | /* change to support A PHY */ |
Larry Finger | 75388ac | 2007-09-25 16:46:54 -0700 | [diff] [blame] | 553 | if (phystat0 & B43legacy_RX_PHYST0_OFDM) |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 554 | status.rate_idx = b43legacy_plcp_get_bitrate_idx_ofdm(plcp, false); |
Larry Finger | 75388ac | 2007-09-25 16:46:54 -0700 | [diff] [blame] | 555 | else |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 556 | status.rate_idx = b43legacy_plcp_get_bitrate_idx_cck(plcp); |
Larry Finger | 75388ac | 2007-09-25 16:46:54 -0700 | [diff] [blame] | 557 | status.antenna = !!(phystat0 & B43legacy_RX_PHYST0_ANT); |
John W. Linville | c0ddd04 | 2008-01-21 13:41:18 -0500 | [diff] [blame] | 558 | |
| 559 | /* |
Johannes Berg | d007b7f | 2008-02-18 18:53:55 +0100 | [diff] [blame] | 560 | * All frames on monitor interfaces and beacons always need a full |
| 561 | * 64-bit timestamp. Monitor interfaces need it for diagnostic |
| 562 | * purposes and beacons for IBSS merging. |
| 563 | * This code assumes we get to process the packet within 16 bits |
| 564 | * of timestamp, i.e. about 65 milliseconds after the PHY received |
| 565 | * the first symbol. |
John W. Linville | c0ddd04 | 2008-01-21 13:41:18 -0500 | [diff] [blame] | 566 | */ |
Harvey Harrison | b99a017 | 2008-06-14 23:33:40 -0700 | [diff] [blame] | 567 | if (ieee80211_is_beacon(fctl) || dev->wl->radiotap_enabled) { |
John W. Linville | c0ddd04 | 2008-01-21 13:41:18 -0500 | [diff] [blame] | 568 | u16 low_mactime_now; |
| 569 | |
| 570 | b43legacy_tsf_read(dev, &status.mactime); |
| 571 | low_mactime_now = status.mactime; |
| 572 | status.mactime = status.mactime & ~0xFFFFULL; |
| 573 | status.mactime += mactime; |
| 574 | if (low_mactime_now <= mactime) |
| 575 | status.mactime -= 0x10000; |
| 576 | status.flag |= RX_FLAG_TSFT; |
| 577 | } |
Larry Finger | 75388ac | 2007-09-25 16:46:54 -0700 | [diff] [blame] | 578 | |
| 579 | chanid = (chanstat & B43legacy_RX_CHAN_ID) >> |
| 580 | B43legacy_RX_CHAN_ID_SHIFT; |
| 581 | switch (chanstat & B43legacy_RX_CHAN_PHYTYPE) { |
| 582 | case B43legacy_PHYTYPE_B: |
Larry Finger | 75388ac | 2007-09-25 16:46:54 -0700 | [diff] [blame] | 583 | case B43legacy_PHYTYPE_G: |
Johannes Berg | 8318d78 | 2008-01-24 19:38:38 +0100 | [diff] [blame] | 584 | status.band = IEEE80211_BAND_2GHZ; |
Larry Finger | 75388ac | 2007-09-25 16:46:54 -0700 | [diff] [blame] | 585 | status.freq = chanid + 2400; |
Larry Finger | 75388ac | 2007-09-25 16:46:54 -0700 | [diff] [blame] | 586 | break; |
| 587 | default: |
| 588 | b43legacywarn(dev->wl, "Unexpected value for chanstat (0x%X)\n", |
| 589 | chanstat); |
| 590 | } |
| 591 | |
Johannes Berg | f1d58c2 | 2009-06-17 13:13:00 +0200 | [diff] [blame] | 592 | memcpy(IEEE80211_SKB_RXCB(skb), &status, sizeof(status)); |
| 593 | ieee80211_rx_irqsafe(dev->wl->hw, skb); |
Larry Finger | 75388ac | 2007-09-25 16:46:54 -0700 | [diff] [blame] | 594 | |
| 595 | return; |
| 596 | drop: |
| 597 | b43legacydbg(dev->wl, "RX: Packet dropped\n"); |
| 598 | dev_kfree_skb_any(skb); |
| 599 | } |
| 600 | |
| 601 | void b43legacy_handle_txstatus(struct b43legacy_wldev *dev, |
| 602 | const struct b43legacy_txstatus *status) |
| 603 | { |
| 604 | b43legacy_debugfs_log_txstat(dev, status); |
| 605 | |
| 606 | if (status->intermediate) |
| 607 | return; |
| 608 | if (status->for_ampdu) |
| 609 | return; |
| 610 | if (!status->acked) |
| 611 | dev->wl->ieee_stats.dot11ACKFailureCount++; |
| 612 | if (status->rts_count) { |
| 613 | if (status->rts_count == 0xF) /* FIXME */ |
| 614 | dev->wl->ieee_stats.dot11RTSFailureCount++; |
| 615 | else |
| 616 | dev->wl->ieee_stats.dot11RTSSuccessCount++; |
| 617 | } |
| 618 | |
| 619 | if (b43legacy_using_pio(dev)) |
| 620 | b43legacy_pio_handle_txstatus(dev, status); |
| 621 | else |
| 622 | b43legacy_dma_handle_txstatus(dev, status); |
| 623 | } |
| 624 | |
| 625 | /* Handle TX status report as received through DMA/PIO queues */ |
| 626 | void b43legacy_handle_hwtxstatus(struct b43legacy_wldev *dev, |
| 627 | const struct b43legacy_hwtxstatus *hw) |
| 628 | { |
| 629 | struct b43legacy_txstatus status; |
| 630 | u8 tmp; |
| 631 | |
| 632 | status.cookie = le16_to_cpu(hw->cookie); |
| 633 | status.seq = le16_to_cpu(hw->seq); |
| 634 | status.phy_stat = hw->phy_stat; |
| 635 | tmp = hw->count; |
| 636 | status.frame_count = (tmp >> 4); |
| 637 | status.rts_count = (tmp & 0x0F); |
Larry Finger | c6a2afd | 2008-09-06 16:51:22 -0500 | [diff] [blame] | 638 | tmp = hw->flags << 1; |
Larry Finger | 75388ac | 2007-09-25 16:46:54 -0700 | [diff] [blame] | 639 | status.supp_reason = ((tmp & 0x1C) >> 2); |
| 640 | status.pm_indicated = !!(tmp & 0x80); |
| 641 | status.intermediate = !!(tmp & 0x40); |
| 642 | status.for_ampdu = !!(tmp & 0x20); |
| 643 | status.acked = !!(tmp & 0x02); |
| 644 | |
| 645 | b43legacy_handle_txstatus(dev, &status); |
| 646 | } |
| 647 | |
| 648 | /* Stop any TX operation on the device (suspend the hardware queues) */ |
| 649 | void b43legacy_tx_suspend(struct b43legacy_wldev *dev) |
| 650 | { |
| 651 | if (b43legacy_using_pio(dev)) |
| 652 | b43legacy_pio_freeze_txqueues(dev); |
| 653 | else |
| 654 | b43legacy_dma_tx_suspend(dev); |
| 655 | } |
| 656 | |
| 657 | /* Resume any TX operation on the device (resume the hardware queues) */ |
| 658 | void b43legacy_tx_resume(struct b43legacy_wldev *dev) |
| 659 | { |
| 660 | if (b43legacy_using_pio(dev)) |
| 661 | b43legacy_pio_thaw_txqueues(dev); |
| 662 | else |
| 663 | b43legacy_dma_tx_resume(dev); |
| 664 | } |
| 665 | |
| 666 | /* Initialize the QoS parameters */ |
| 667 | void b43legacy_qos_init(struct b43legacy_wldev *dev) |
| 668 | { |
| 669 | /* FIXME: This function must probably be called from the mac80211 |
| 670 | * config callback. */ |
| 671 | return; |
| 672 | |
| 673 | b43legacy_hf_write(dev, b43legacy_hf_read(dev) | B43legacy_HF_EDCF); |
| 674 | /* FIXME kill magic */ |
| 675 | b43legacy_write16(dev, 0x688, |
| 676 | b43legacy_read16(dev, 0x688) | 0x4); |
| 677 | |
| 678 | |
| 679 | /*TODO: We might need some stack support here to get the values. */ |
| 680 | } |