Luis R. Rodriguez | db86f07 | 2009-11-05 08:44:39 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2009 Atheros Communications Inc. |
| 3 | * |
| 4 | * Permission to use, copy, modify, and/or distribute this software for any |
| 5 | * purpose with or without fee is hereby granted, provided that the above |
| 6 | * copyright notice and this permission notice appear in all copies. |
| 7 | * |
| 8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 15 | */ |
| 16 | |
| 17 | /* |
| 18 | * Module for common driver code between ath9k and ath9k_htc |
| 19 | */ |
| 20 | |
| 21 | #include <linux/kernel.h> |
| 22 | #include <linux/module.h> |
| 23 | |
| 24 | #include "common.h" |
| 25 | |
| 26 | MODULE_AUTHOR("Atheros Communications"); |
| 27 | MODULE_DESCRIPTION("Shared library for Atheros wireless 802.11n LAN cards."); |
| 28 | MODULE_LICENSE("Dual BSD/GPL"); |
| 29 | |
| 30 | /* Common RX processing */ |
| 31 | |
| 32 | /* Assumes you've already done the endian to CPU conversion */ |
| 33 | static bool ath9k_rx_accept(struct ath_common *common, |
| 34 | struct sk_buff *skb, |
| 35 | struct ieee80211_rx_status *rxs, |
| 36 | struct ath_rx_status *rx_stats, |
| 37 | bool *decrypt_error) |
| 38 | { |
| 39 | struct ath_hw *ah = common->ah; |
| 40 | struct ieee80211_hdr *hdr; |
| 41 | __le16 fc; |
| 42 | |
| 43 | hdr = (struct ieee80211_hdr *) skb->data; |
| 44 | fc = hdr->frame_control; |
| 45 | |
| 46 | if (!rx_stats->rs_datalen) |
| 47 | return false; |
| 48 | /* |
| 49 | * rs_status follows rs_datalen so if rs_datalen is too large |
| 50 | * we can take a hint that hardware corrupted it, so ignore |
| 51 | * those frames. |
| 52 | */ |
| 53 | if (rx_stats->rs_datalen > common->rx_bufsize) |
| 54 | return false; |
| 55 | |
Luis R. Rodriguez | 748d451 | 2009-11-05 14:10:07 -0800 | [diff] [blame] | 56 | /* |
| 57 | * rs_more indicates chained descriptors which can be used |
| 58 | * to link buffers together for a sort of scatter-gather |
| 59 | * operation. |
| 60 | * |
| 61 | * The rx_stats->rs_status will not be set until the end of the |
| 62 | * chained descriptors so it can be ignored if rs_more is set. The |
| 63 | * rs_more will be false at the last element of the chained |
| 64 | * descriptors. |
| 65 | */ |
| 66 | if (!rx_stats->rs_more && rx_stats->rs_status != 0) { |
Luis R. Rodriguez | db86f07 | 2009-11-05 08:44:39 -0800 | [diff] [blame] | 67 | if (rx_stats->rs_status & ATH9K_RXERR_CRC) |
| 68 | rxs->flag |= RX_FLAG_FAILED_FCS_CRC; |
| 69 | if (rx_stats->rs_status & ATH9K_RXERR_PHY) |
| 70 | return false; |
| 71 | |
| 72 | if (rx_stats->rs_status & ATH9K_RXERR_DECRYPT) { |
| 73 | *decrypt_error = true; |
| 74 | } else if (rx_stats->rs_status & ATH9K_RXERR_MIC) { |
| 75 | if (ieee80211_is_ctl(fc)) |
| 76 | /* |
| 77 | * Sometimes, we get invalid |
| 78 | * MIC failures on valid control frames. |
| 79 | * Remove these mic errors. |
| 80 | */ |
| 81 | rx_stats->rs_status &= ~ATH9K_RXERR_MIC; |
| 82 | else |
| 83 | rxs->flag |= RX_FLAG_MMIC_ERROR; |
| 84 | } |
| 85 | /* |
| 86 | * Reject error frames with the exception of |
| 87 | * decryption and MIC failures. For monitor mode, |
| 88 | * we also ignore the CRC error. |
| 89 | */ |
| 90 | if (ah->opmode == NL80211_IFTYPE_MONITOR) { |
| 91 | if (rx_stats->rs_status & |
| 92 | ~(ATH9K_RXERR_DECRYPT | ATH9K_RXERR_MIC | |
| 93 | ATH9K_RXERR_CRC)) |
| 94 | return false; |
| 95 | } else { |
| 96 | if (rx_stats->rs_status & |
| 97 | ~(ATH9K_RXERR_DECRYPT | ATH9K_RXERR_MIC)) { |
| 98 | return false; |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | return true; |
| 103 | } |
| 104 | |
| 105 | static u8 ath9k_process_rate(struct ath_common *common, |
| 106 | struct ieee80211_hw *hw, |
| 107 | struct ath_rx_status *rx_stats, |
| 108 | struct ieee80211_rx_status *rxs, |
| 109 | struct sk_buff *skb) |
| 110 | { |
| 111 | struct ieee80211_supported_band *sband; |
| 112 | enum ieee80211_band band; |
| 113 | unsigned int i = 0; |
| 114 | |
| 115 | band = hw->conf.channel->band; |
| 116 | sband = hw->wiphy->bands[band]; |
| 117 | |
| 118 | if (rx_stats->rs_rate & 0x80) { |
| 119 | /* HT rate */ |
| 120 | rxs->flag |= RX_FLAG_HT; |
| 121 | if (rx_stats->rs_flags & ATH9K_RX_2040) |
| 122 | rxs->flag |= RX_FLAG_40MHZ; |
| 123 | if (rx_stats->rs_flags & ATH9K_RX_GI) |
| 124 | rxs->flag |= RX_FLAG_SHORT_GI; |
| 125 | return rx_stats->rs_rate & 0x7f; |
| 126 | } |
| 127 | |
| 128 | for (i = 0; i < sband->n_bitrates; i++) { |
| 129 | if (sband->bitrates[i].hw_value == rx_stats->rs_rate) |
| 130 | return i; |
| 131 | if (sband->bitrates[i].hw_value_short == rx_stats->rs_rate) { |
| 132 | rxs->flag |= RX_FLAG_SHORTPRE; |
| 133 | return i; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | /* No valid hardware bitrate found -- we should not get here */ |
| 138 | ath_print(common, ATH_DBG_XMIT, "unsupported hw bitrate detected " |
| 139 | "0x%02x using 1 Mbit\n", rx_stats->rs_rate); |
| 140 | if ((common->debug_mask & ATH_DBG_XMIT)) |
| 141 | print_hex_dump_bytes("", DUMP_PREFIX_NONE, skb->data, skb->len); |
| 142 | |
| 143 | return 0; |
| 144 | } |
| 145 | |
Luis R. Rodriguez | db86f07 | 2009-11-05 08:44:39 -0800 | [diff] [blame] | 146 | static void ath9k_process_rssi(struct ath_common *common, |
| 147 | struct ieee80211_hw *hw, |
| 148 | struct sk_buff *skb, |
| 149 | struct ath_rx_status *rx_stats) |
| 150 | { |
| 151 | struct ath_hw *ah = common->ah; |
| 152 | struct ieee80211_sta *sta; |
| 153 | struct ieee80211_hdr *hdr; |
| 154 | struct ath_node *an; |
| 155 | int last_rssi = ATH_RSSI_DUMMY_MARKER; |
| 156 | __le16 fc; |
| 157 | |
| 158 | hdr = (struct ieee80211_hdr *)skb->data; |
| 159 | fc = hdr->frame_control; |
| 160 | |
| 161 | rcu_read_lock(); |
| 162 | /* |
| 163 | * XXX: use ieee80211_find_sta! This requires quite a bit of work |
| 164 | * under the current ath9k virtual wiphy implementation as we have |
| 165 | * no way of tying a vif to wiphy. Typically vifs are attached to |
| 166 | * at least one sdata of a wiphy on mac80211 but with ath9k virtual |
| 167 | * wiphy you'd have to iterate over every wiphy and each sdata. |
| 168 | */ |
| 169 | sta = ieee80211_find_sta_by_hw(hw, hdr->addr2); |
| 170 | if (sta) { |
| 171 | an = (struct ath_node *) sta->drv_priv; |
| 172 | if (rx_stats->rs_rssi != ATH9K_RSSI_BAD && |
| 173 | !rx_stats->rs_moreaggr) |
| 174 | ATH_RSSI_LPF(an->last_rssi, rx_stats->rs_rssi); |
| 175 | last_rssi = an->last_rssi; |
| 176 | } |
| 177 | rcu_read_unlock(); |
| 178 | |
| 179 | if (likely(last_rssi != ATH_RSSI_DUMMY_MARKER)) |
| 180 | rx_stats->rs_rssi = ATH_EP_RND(last_rssi, |
| 181 | ATH_RSSI_EP_MULTIPLIER); |
| 182 | if (rx_stats->rs_rssi < 0) |
| 183 | rx_stats->rs_rssi = 0; |
Luis R. Rodriguez | db86f07 | 2009-11-05 08:44:39 -0800 | [diff] [blame] | 184 | |
| 185 | /* Update Beacon RSSI, this is used by ANI. */ |
| 186 | if (ieee80211_is_beacon(fc)) |
| 187 | ah->stats.avgbrssi = rx_stats->rs_rssi; |
| 188 | } |
| 189 | |
| 190 | /* |
| 191 | * For Decrypt or Demic errors, we only mark packet status here and always push |
| 192 | * up the frame up to let mac80211 handle the actual error case, be it no |
| 193 | * decryption key or real decryption error. This let us keep statistics there. |
| 194 | */ |
| 195 | int ath9k_cmn_rx_skb_preprocess(struct ath_common *common, |
| 196 | struct ieee80211_hw *hw, |
| 197 | struct sk_buff *skb, |
| 198 | struct ath_rx_status *rx_stats, |
| 199 | struct ieee80211_rx_status *rx_status, |
| 200 | bool *decrypt_error) |
| 201 | { |
| 202 | struct ath_hw *ah = common->ah; |
| 203 | |
Felix Fietkau | dd6ae4f | 2009-11-15 22:27:17 +0100 | [diff] [blame] | 204 | memset(rx_status, 0, sizeof(struct ieee80211_rx_status)); |
Luis R. Rodriguez | db86f07 | 2009-11-05 08:44:39 -0800 | [diff] [blame] | 205 | if (!ath9k_rx_accept(common, skb, rx_status, rx_stats, decrypt_error)) |
| 206 | return -EINVAL; |
| 207 | |
| 208 | ath9k_process_rssi(common, hw, skb, rx_stats); |
| 209 | |
| 210 | rx_status->rate_idx = ath9k_process_rate(common, hw, |
| 211 | rx_stats, rx_status, skb); |
| 212 | rx_status->mactime = ath9k_hw_extend_tsf(ah, rx_stats->rs_tstamp); |
| 213 | rx_status->band = hw->conf.channel->band; |
| 214 | rx_status->freq = hw->conf.channel->center_freq; |
| 215 | rx_status->noise = common->ani.noise_floor; |
| 216 | rx_status->signal = ATH_DEFAULT_NOISE_FLOOR + rx_stats->rs_rssi; |
| 217 | rx_status->antenna = rx_stats->rs_antenna; |
Luis R. Rodriguez | db86f07 | 2009-11-05 08:44:39 -0800 | [diff] [blame] | 218 | rx_status->flag |= RX_FLAG_TSFT; |
| 219 | |
| 220 | return 0; |
| 221 | } |
| 222 | EXPORT_SYMBOL(ath9k_cmn_rx_skb_preprocess); |
| 223 | |
| 224 | void ath9k_cmn_rx_skb_postprocess(struct ath_common *common, |
| 225 | struct sk_buff *skb, |
| 226 | struct ath_rx_status *rx_stats, |
| 227 | struct ieee80211_rx_status *rxs, |
| 228 | bool decrypt_error) |
| 229 | { |
| 230 | struct ath_hw *ah = common->ah; |
| 231 | struct ieee80211_hdr *hdr; |
Benoit PAPILLAULT | 8c35024 | 2009-11-19 22:19:26 +0100 | [diff] [blame] | 232 | int hdrlen, padpos, padsize; |
Luis R. Rodriguez | db86f07 | 2009-11-05 08:44:39 -0800 | [diff] [blame] | 233 | u8 keyix; |
| 234 | __le16 fc; |
| 235 | |
| 236 | /* see if any padding is done by the hw and remove it */ |
| 237 | hdr = (struct ieee80211_hdr *) skb->data; |
| 238 | hdrlen = ieee80211_get_hdrlen_from_skb(skb); |
| 239 | fc = hdr->frame_control; |
Benoit Papillault | 1bc1488 | 2009-11-24 15:49:18 +0100 | [diff] [blame] | 240 | padpos = ath9k_cmn_padpos(hdr->frame_control); |
Luis R. Rodriguez | db86f07 | 2009-11-05 08:44:39 -0800 | [diff] [blame] | 241 | |
| 242 | /* The MAC header is padded to have 32-bit boundary if the |
| 243 | * packet payload is non-zero. The general calculation for |
| 244 | * padsize would take into account odd header lengths: |
Benoit PAPILLAULT | 8c35024 | 2009-11-19 22:19:26 +0100 | [diff] [blame] | 245 | * padsize = (4 - padpos % 4) % 4; However, since only |
Luis R. Rodriguez | db86f07 | 2009-11-05 08:44:39 -0800 | [diff] [blame] | 246 | * even-length headers are used, padding can only be 0 or 2 |
| 247 | * bytes and we can optimize this a bit. In addition, we must |
| 248 | * not try to remove padding from short control frames that do |
| 249 | * not have payload. */ |
Benoit PAPILLAULT | 8c35024 | 2009-11-19 22:19:26 +0100 | [diff] [blame] | 250 | padsize = padpos & 3; |
| 251 | if (padsize && skb->len>=padpos+padsize+FCS_LEN) { |
| 252 | memmove(skb->data + padsize, skb->data, padpos); |
Luis R. Rodriguez | db86f07 | 2009-11-05 08:44:39 -0800 | [diff] [blame] | 253 | skb_pull(skb, padsize); |
| 254 | } |
| 255 | |
| 256 | keyix = rx_stats->rs_keyix; |
| 257 | |
Jouni Malinen | 932d37c | 2010-03-29 23:37:36 -0700 | [diff] [blame] | 258 | if (!(keyix == ATH9K_RXKEYIX_INVALID) && !decrypt_error && |
| 259 | ieee80211_has_protected(fc)) { |
Luis R. Rodriguez | db86f07 | 2009-11-05 08:44:39 -0800 | [diff] [blame] | 260 | rxs->flag |= RX_FLAG_DECRYPTED; |
| 261 | } else if (ieee80211_has_protected(fc) |
| 262 | && !decrypt_error && skb->len >= hdrlen + 4) { |
| 263 | keyix = skb->data[hdrlen + 3] >> 6; |
| 264 | |
| 265 | if (test_bit(keyix, common->keymap)) |
| 266 | rxs->flag |= RX_FLAG_DECRYPTED; |
| 267 | } |
| 268 | if (ah->sw_mgmt_crypto && |
| 269 | (rxs->flag & RX_FLAG_DECRYPTED) && |
| 270 | ieee80211_is_mgmt(fc)) |
| 271 | /* Use software decrypt for management frames. */ |
| 272 | rxs->flag &= ~RX_FLAG_DECRYPTED; |
| 273 | } |
| 274 | EXPORT_SYMBOL(ath9k_cmn_rx_skb_postprocess); |
| 275 | |
Benoit Papillault | 1bc1488 | 2009-11-24 15:49:18 +0100 | [diff] [blame] | 276 | int ath9k_cmn_padpos(__le16 frame_control) |
| 277 | { |
| 278 | int padpos = 24; |
| 279 | if (ieee80211_has_a4(frame_control)) { |
| 280 | padpos += ETH_ALEN; |
| 281 | } |
| 282 | if (ieee80211_is_data_qos(frame_control)) { |
| 283 | padpos += IEEE80211_QOS_CTL_LEN; |
| 284 | } |
| 285 | |
| 286 | return padpos; |
| 287 | } |
| 288 | EXPORT_SYMBOL(ath9k_cmn_padpos); |
| 289 | |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 290 | int ath9k_cmn_get_hw_crypto_keytype(struct sk_buff *skb) |
| 291 | { |
| 292 | struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb); |
| 293 | |
| 294 | if (tx_info->control.hw_key) { |
| 295 | if (tx_info->control.hw_key->alg == ALG_WEP) |
| 296 | return ATH9K_KEY_TYPE_WEP; |
| 297 | else if (tx_info->control.hw_key->alg == ALG_TKIP) |
| 298 | return ATH9K_KEY_TYPE_TKIP; |
| 299 | else if (tx_info->control.hw_key->alg == ALG_CCMP) |
| 300 | return ATH9K_KEY_TYPE_AES; |
| 301 | } |
| 302 | |
| 303 | return ATH9K_KEY_TYPE_CLEAR; |
| 304 | } |
| 305 | EXPORT_SYMBOL(ath9k_cmn_get_hw_crypto_keytype); |
| 306 | |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 307 | static u32 ath9k_get_extchanmode(struct ieee80211_channel *chan, |
| 308 | enum nl80211_channel_type channel_type) |
| 309 | { |
| 310 | u32 chanmode = 0; |
| 311 | |
| 312 | switch (chan->band) { |
| 313 | case IEEE80211_BAND_2GHZ: |
| 314 | switch (channel_type) { |
| 315 | case NL80211_CHAN_NO_HT: |
| 316 | case NL80211_CHAN_HT20: |
| 317 | chanmode = CHANNEL_G_HT20; |
| 318 | break; |
| 319 | case NL80211_CHAN_HT40PLUS: |
| 320 | chanmode = CHANNEL_G_HT40PLUS; |
| 321 | break; |
| 322 | case NL80211_CHAN_HT40MINUS: |
| 323 | chanmode = CHANNEL_G_HT40MINUS; |
| 324 | break; |
| 325 | } |
| 326 | break; |
| 327 | case IEEE80211_BAND_5GHZ: |
| 328 | switch (channel_type) { |
| 329 | case NL80211_CHAN_NO_HT: |
| 330 | case NL80211_CHAN_HT20: |
| 331 | chanmode = CHANNEL_A_HT20; |
| 332 | break; |
| 333 | case NL80211_CHAN_HT40PLUS: |
| 334 | chanmode = CHANNEL_A_HT40PLUS; |
| 335 | break; |
| 336 | case NL80211_CHAN_HT40MINUS: |
| 337 | chanmode = CHANNEL_A_HT40MINUS; |
| 338 | break; |
| 339 | } |
| 340 | break; |
| 341 | default: |
| 342 | break; |
| 343 | } |
| 344 | |
| 345 | return chanmode; |
| 346 | } |
| 347 | |
| 348 | /* |
| 349 | * Update internal channel flags. |
| 350 | */ |
| 351 | void ath9k_cmn_update_ichannel(struct ieee80211_hw *hw, |
| 352 | struct ath9k_channel *ichan) |
| 353 | { |
| 354 | struct ieee80211_channel *chan = hw->conf.channel; |
| 355 | struct ieee80211_conf *conf = &hw->conf; |
| 356 | |
| 357 | ichan->channel = chan->center_freq; |
| 358 | ichan->chan = chan; |
| 359 | |
| 360 | if (chan->band == IEEE80211_BAND_2GHZ) { |
| 361 | ichan->chanmode = CHANNEL_G; |
| 362 | ichan->channelFlags = CHANNEL_2GHZ | CHANNEL_OFDM | CHANNEL_G; |
| 363 | } else { |
| 364 | ichan->chanmode = CHANNEL_A; |
| 365 | ichan->channelFlags = CHANNEL_5GHZ | CHANNEL_OFDM; |
| 366 | } |
| 367 | |
| 368 | if (conf_is_ht(conf)) |
| 369 | ichan->chanmode = ath9k_get_extchanmode(chan, |
| 370 | conf->channel_type); |
| 371 | } |
| 372 | EXPORT_SYMBOL(ath9k_cmn_update_ichannel); |
| 373 | |
| 374 | /* |
| 375 | * Get the internal channel reference. |
| 376 | */ |
| 377 | struct ath9k_channel *ath9k_cmn_get_curchannel(struct ieee80211_hw *hw, |
| 378 | struct ath_hw *ah) |
| 379 | { |
| 380 | struct ieee80211_channel *curchan = hw->conf.channel; |
| 381 | struct ath9k_channel *channel; |
| 382 | u8 chan_idx; |
| 383 | |
| 384 | chan_idx = curchan->hw_value; |
| 385 | channel = &ah->channels[chan_idx]; |
| 386 | ath9k_cmn_update_ichannel(hw, channel); |
| 387 | |
| 388 | return channel; |
| 389 | } |
| 390 | EXPORT_SYMBOL(ath9k_cmn_get_curchannel); |
| 391 | |
| 392 | static int ath_setkey_tkip(struct ath_common *common, u16 keyix, const u8 *key, |
| 393 | struct ath9k_keyval *hk, const u8 *addr, |
| 394 | bool authenticator) |
| 395 | { |
| 396 | struct ath_hw *ah = common->ah; |
| 397 | const u8 *key_rxmic; |
| 398 | const u8 *key_txmic; |
| 399 | |
| 400 | key_txmic = key + NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY; |
| 401 | key_rxmic = key + NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY; |
| 402 | |
| 403 | if (addr == NULL) { |
| 404 | /* |
| 405 | * Group key installation - only two key cache entries are used |
| 406 | * regardless of splitmic capability since group key is only |
| 407 | * used either for TX or RX. |
| 408 | */ |
| 409 | if (authenticator) { |
| 410 | memcpy(hk->kv_mic, key_txmic, sizeof(hk->kv_mic)); |
| 411 | memcpy(hk->kv_txmic, key_txmic, sizeof(hk->kv_mic)); |
| 412 | } else { |
| 413 | memcpy(hk->kv_mic, key_rxmic, sizeof(hk->kv_mic)); |
| 414 | memcpy(hk->kv_txmic, key_rxmic, sizeof(hk->kv_mic)); |
| 415 | } |
| 416 | return ath9k_hw_set_keycache_entry(ah, keyix, hk, addr); |
| 417 | } |
| 418 | if (!common->splitmic) { |
| 419 | /* TX and RX keys share the same key cache entry. */ |
| 420 | memcpy(hk->kv_mic, key_rxmic, sizeof(hk->kv_mic)); |
| 421 | memcpy(hk->kv_txmic, key_txmic, sizeof(hk->kv_txmic)); |
| 422 | return ath9k_hw_set_keycache_entry(ah, keyix, hk, addr); |
| 423 | } |
| 424 | |
| 425 | /* Separate key cache entries for TX and RX */ |
| 426 | |
| 427 | /* TX key goes at first index, RX key at +32. */ |
| 428 | memcpy(hk->kv_mic, key_txmic, sizeof(hk->kv_mic)); |
| 429 | if (!ath9k_hw_set_keycache_entry(ah, keyix, hk, NULL)) { |
| 430 | /* TX MIC entry failed. No need to proceed further */ |
| 431 | ath_print(common, ATH_DBG_FATAL, |
| 432 | "Setting TX MIC Key Failed\n"); |
| 433 | return 0; |
| 434 | } |
| 435 | |
| 436 | memcpy(hk->kv_mic, key_rxmic, sizeof(hk->kv_mic)); |
| 437 | /* XXX delete tx key on failure? */ |
| 438 | return ath9k_hw_set_keycache_entry(ah, keyix + 32, hk, addr); |
| 439 | } |
| 440 | |
| 441 | static int ath_reserve_key_cache_slot_tkip(struct ath_common *common) |
| 442 | { |
| 443 | int i; |
| 444 | |
| 445 | for (i = IEEE80211_WEP_NKID; i < common->keymax / 2; i++) { |
| 446 | if (test_bit(i, common->keymap) || |
| 447 | test_bit(i + 64, common->keymap)) |
| 448 | continue; /* At least one part of TKIP key allocated */ |
| 449 | if (common->splitmic && |
| 450 | (test_bit(i + 32, common->keymap) || |
| 451 | test_bit(i + 64 + 32, common->keymap))) |
| 452 | continue; /* At least one part of TKIP key allocated */ |
| 453 | |
| 454 | /* Found a free slot for a TKIP key */ |
| 455 | return i; |
| 456 | } |
| 457 | return -1; |
| 458 | } |
| 459 | |
| 460 | static int ath_reserve_key_cache_slot(struct ath_common *common) |
| 461 | { |
| 462 | int i; |
| 463 | |
| 464 | /* First, try to find slots that would not be available for TKIP. */ |
| 465 | if (common->splitmic) { |
| 466 | for (i = IEEE80211_WEP_NKID; i < common->keymax / 4; i++) { |
| 467 | if (!test_bit(i, common->keymap) && |
| 468 | (test_bit(i + 32, common->keymap) || |
| 469 | test_bit(i + 64, common->keymap) || |
| 470 | test_bit(i + 64 + 32, common->keymap))) |
| 471 | return i; |
| 472 | if (!test_bit(i + 32, common->keymap) && |
| 473 | (test_bit(i, common->keymap) || |
| 474 | test_bit(i + 64, common->keymap) || |
| 475 | test_bit(i + 64 + 32, common->keymap))) |
| 476 | return i + 32; |
| 477 | if (!test_bit(i + 64, common->keymap) && |
| 478 | (test_bit(i , common->keymap) || |
| 479 | test_bit(i + 32, common->keymap) || |
| 480 | test_bit(i + 64 + 32, common->keymap))) |
| 481 | return i + 64; |
| 482 | if (!test_bit(i + 64 + 32, common->keymap) && |
| 483 | (test_bit(i, common->keymap) || |
| 484 | test_bit(i + 32, common->keymap) || |
| 485 | test_bit(i + 64, common->keymap))) |
| 486 | return i + 64 + 32; |
| 487 | } |
| 488 | } else { |
| 489 | for (i = IEEE80211_WEP_NKID; i < common->keymax / 2; i++) { |
| 490 | if (!test_bit(i, common->keymap) && |
| 491 | test_bit(i + 64, common->keymap)) |
| 492 | return i; |
| 493 | if (test_bit(i, common->keymap) && |
| 494 | !test_bit(i + 64, common->keymap)) |
| 495 | return i + 64; |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | /* No partially used TKIP slots, pick any available slot */ |
| 500 | for (i = IEEE80211_WEP_NKID; i < common->keymax; i++) { |
| 501 | /* Do not allow slots that could be needed for TKIP group keys |
| 502 | * to be used. This limitation could be removed if we know that |
| 503 | * TKIP will not be used. */ |
| 504 | if (i >= 64 && i < 64 + IEEE80211_WEP_NKID) |
| 505 | continue; |
| 506 | if (common->splitmic) { |
| 507 | if (i >= 32 && i < 32 + IEEE80211_WEP_NKID) |
| 508 | continue; |
| 509 | if (i >= 64 + 32 && i < 64 + 32 + IEEE80211_WEP_NKID) |
| 510 | continue; |
| 511 | } |
| 512 | |
| 513 | if (!test_bit(i, common->keymap)) |
| 514 | return i; /* Found a free slot for a key */ |
| 515 | } |
| 516 | |
| 517 | /* No free slot found */ |
| 518 | return -1; |
| 519 | } |
| 520 | |
| 521 | /* |
| 522 | * Configure encryption in the HW. |
| 523 | */ |
| 524 | int ath9k_cmn_key_config(struct ath_common *common, |
| 525 | struct ieee80211_vif *vif, |
| 526 | struct ieee80211_sta *sta, |
| 527 | struct ieee80211_key_conf *key) |
| 528 | { |
| 529 | struct ath_hw *ah = common->ah; |
| 530 | struct ath9k_keyval hk; |
| 531 | const u8 *mac = NULL; |
| 532 | int ret = 0; |
| 533 | int idx; |
| 534 | |
| 535 | memset(&hk, 0, sizeof(hk)); |
| 536 | |
| 537 | switch (key->alg) { |
| 538 | case ALG_WEP: |
| 539 | hk.kv_type = ATH9K_CIPHER_WEP; |
| 540 | break; |
| 541 | case ALG_TKIP: |
| 542 | hk.kv_type = ATH9K_CIPHER_TKIP; |
| 543 | break; |
| 544 | case ALG_CCMP: |
| 545 | hk.kv_type = ATH9K_CIPHER_AES_CCM; |
| 546 | break; |
| 547 | default: |
| 548 | return -EOPNOTSUPP; |
| 549 | } |
| 550 | |
| 551 | hk.kv_len = key->keylen; |
| 552 | memcpy(hk.kv_val, key->key, key->keylen); |
| 553 | |
| 554 | if (!(key->flags & IEEE80211_KEY_FLAG_PAIRWISE)) { |
| 555 | /* For now, use the default keys for broadcast keys. This may |
| 556 | * need to change with virtual interfaces. */ |
| 557 | idx = key->keyidx; |
| 558 | } else if (key->keyidx) { |
| 559 | if (WARN_ON(!sta)) |
| 560 | return -EOPNOTSUPP; |
| 561 | mac = sta->addr; |
| 562 | |
| 563 | if (vif->type != NL80211_IFTYPE_AP) { |
| 564 | /* Only keyidx 0 should be used with unicast key, but |
| 565 | * allow this for client mode for now. */ |
| 566 | idx = key->keyidx; |
| 567 | } else |
| 568 | return -EIO; |
| 569 | } else { |
| 570 | if (WARN_ON(!sta)) |
| 571 | return -EOPNOTSUPP; |
| 572 | mac = sta->addr; |
| 573 | |
| 574 | if (key->alg == ALG_TKIP) |
| 575 | idx = ath_reserve_key_cache_slot_tkip(common); |
| 576 | else |
| 577 | idx = ath_reserve_key_cache_slot(common); |
| 578 | if (idx < 0) |
| 579 | return -ENOSPC; /* no free key cache entries */ |
| 580 | } |
| 581 | |
| 582 | if (key->alg == ALG_TKIP) |
| 583 | ret = ath_setkey_tkip(common, idx, key->key, &hk, mac, |
| 584 | vif->type == NL80211_IFTYPE_AP); |
| 585 | else |
| 586 | ret = ath9k_hw_set_keycache_entry(ah, idx, &hk, mac); |
| 587 | |
| 588 | if (!ret) |
| 589 | return -EIO; |
| 590 | |
| 591 | set_bit(idx, common->keymap); |
| 592 | if (key->alg == ALG_TKIP) { |
| 593 | set_bit(idx + 64, common->keymap); |
| 594 | if (common->splitmic) { |
| 595 | set_bit(idx + 32, common->keymap); |
| 596 | set_bit(idx + 64 + 32, common->keymap); |
| 597 | } |
| 598 | } |
| 599 | |
| 600 | return idx; |
| 601 | } |
| 602 | EXPORT_SYMBOL(ath9k_cmn_key_config); |
| 603 | |
| 604 | /* |
| 605 | * Delete Key. |
| 606 | */ |
| 607 | void ath9k_cmn_key_delete(struct ath_common *common, |
| 608 | struct ieee80211_key_conf *key) |
| 609 | { |
| 610 | struct ath_hw *ah = common->ah; |
| 611 | |
| 612 | ath9k_hw_keyreset(ah, key->hw_key_idx); |
| 613 | if (key->hw_key_idx < IEEE80211_WEP_NKID) |
| 614 | return; |
| 615 | |
| 616 | clear_bit(key->hw_key_idx, common->keymap); |
| 617 | if (key->alg != ALG_TKIP) |
| 618 | return; |
| 619 | |
| 620 | clear_bit(key->hw_key_idx + 64, common->keymap); |
| 621 | if (common->splitmic) { |
| 622 | ath9k_hw_keyreset(ah, key->hw_key_idx + 32); |
| 623 | clear_bit(key->hw_key_idx + 32, common->keymap); |
| 624 | clear_bit(key->hw_key_idx + 64 + 32, common->keymap); |
| 625 | } |
| 626 | } |
| 627 | EXPORT_SYMBOL(ath9k_cmn_key_delete); |
| 628 | |
Luis R. Rodriguez | db86f07 | 2009-11-05 08:44:39 -0800 | [diff] [blame] | 629 | static int __init ath9k_cmn_init(void) |
| 630 | { |
| 631 | return 0; |
| 632 | } |
| 633 | module_init(ath9k_cmn_init); |
| 634 | |
| 635 | static void __exit ath9k_cmn_exit(void) |
| 636 | { |
| 637 | return; |
| 638 | } |
| 639 | module_exit(ath9k_cmn_exit); |