Path: blob/main/sys/dev/ath/ath_hal/ar5211/ar5211_keycache.c
39566 views
/*-1* SPDX-License-Identifier: ISC2*3* Copyright (c) 2002-2008 Sam Leffler, Errno Consulting4* Copyright (c) 2002-2006 Atheros Communications, Inc.5*6* Permission to use, copy, modify, and/or distribute this software for any7* purpose with or without fee is hereby granted, provided that the above8* copyright notice and this permission notice appear in all copies.9*10* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES11* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF12* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR13* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES14* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN15* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF16* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.17*/18#include "opt_ah.h"1920#include "ah.h"21#include "ah_internal.h"2223#include "ar5211/ar5211.h"24#include "ar5211/ar5211reg.h"2526/*27* Chips-specific key cache routines.28*/2930#define AR_KEYTABLE_SIZE 12831#define KEY_XOR 0xaa3233/*34* Return the size of the hardware key cache.35*/36uint32_t37ar5211GetKeyCacheSize(struct ath_hal *ah)38{39return AR_KEYTABLE_SIZE;40}4142/*43* Return true if the specific key cache entry is valid.44*/45HAL_BOOL46ar5211IsKeyCacheEntryValid(struct ath_hal *ah, uint16_t entry)47{48if (entry < AR_KEYTABLE_SIZE) {49uint32_t val = OS_REG_READ(ah, AR_KEYTABLE_MAC1(entry));50if (val & AR_KEYTABLE_VALID)51return AH_TRUE;52}53return AH_FALSE;54}5556/*57* Clear the specified key cache entry58*/59HAL_BOOL60ar5211ResetKeyCacheEntry(struct ath_hal *ah, uint16_t entry)61{62if (entry < AR_KEYTABLE_SIZE) {63OS_REG_WRITE(ah, AR_KEYTABLE_KEY0(entry), 0);64OS_REG_WRITE(ah, AR_KEYTABLE_KEY1(entry), 0);65OS_REG_WRITE(ah, AR_KEYTABLE_KEY2(entry), 0);66OS_REG_WRITE(ah, AR_KEYTABLE_KEY3(entry), 0);67OS_REG_WRITE(ah, AR_KEYTABLE_KEY4(entry), 0);68OS_REG_WRITE(ah, AR_KEYTABLE_TYPE(entry), 0);69OS_REG_WRITE(ah, AR_KEYTABLE_MAC0(entry), 0);70OS_REG_WRITE(ah, AR_KEYTABLE_MAC1(entry), 0);71return AH_TRUE;72}73return AH_FALSE;74}7576/*77* Sets the mac part of the specified key cache entry and mark it valid.78*/79HAL_BOOL80ar5211SetKeyCacheEntryMac(struct ath_hal *ah, uint16_t entry, const uint8_t *mac)81{82uint32_t macHi, macLo;8384if (entry >= AR_KEYTABLE_SIZE) {85HALDEBUG(ah, HAL_DEBUG_ANY, "%s: entry %u out of range\n",86__func__, entry);87return AH_FALSE;88}8990/*91* Set MAC address -- shifted right by 1. MacLo is92* the 4 MSBs, and MacHi is the 2 LSBs.93*/94if (mac != AH_NULL) {95macHi = (mac[5] << 8) | mac[4];96macLo = (mac[3] << 24)| (mac[2] << 16)97| (mac[1] << 8) | mac[0];98macLo >>= 1;99macLo |= (macHi & 1) << 31; /* carry */100macHi >>= 1;101} else {102macLo = macHi = 0;103}104105OS_REG_WRITE(ah, AR_KEYTABLE_MAC0(entry), macLo);106OS_REG_WRITE(ah, AR_KEYTABLE_MAC1(entry), macHi | AR_KEYTABLE_VALID);107return AH_TRUE;108}109110/*111* Sets the contents of the specified key cache entry.112*/113HAL_BOOL114ar5211SetKeyCacheEntry(struct ath_hal *ah, uint16_t entry,115const HAL_KEYVAL *k, const uint8_t *mac,116int xorKey)117{118uint32_t key0, key1, key2, key3, key4;119uint32_t keyType;120uint32_t xorMask= xorKey ?121(KEY_XOR << 24 | KEY_XOR << 16 | KEY_XOR << 8 | KEY_XOR) : 0;122123if (entry >= AR_KEYTABLE_SIZE) {124HALDEBUG(ah, HAL_DEBUG_ANY, "%s: entry %u out of range\n",125__func__, entry);126return AH_FALSE;127}128switch (k->kv_type) {129case HAL_CIPHER_AES_OCB:130keyType = AR_KEYTABLE_TYPE_AES;131break;132case HAL_CIPHER_WEP:133if (k->kv_len < 40 / NBBY) {134HALDEBUG(ah, HAL_DEBUG_ANY,135"%s: WEP key length %u too small\n",136__func__, k->kv_len);137return AH_FALSE;138}139if (k->kv_len <= 40 / NBBY)140keyType = AR_KEYTABLE_TYPE_40;141else if (k->kv_len <= 104 / NBBY)142keyType = AR_KEYTABLE_TYPE_104;143else144keyType = AR_KEYTABLE_TYPE_128;145break;146case HAL_CIPHER_CLR:147keyType = AR_KEYTABLE_TYPE_CLR;148break;149default:150HALDEBUG(ah, HAL_DEBUG_ANY, "%s: cipher %u not supported\n",151__func__, k->kv_type);152return AH_FALSE;153}154155key0 = LE_READ_4(k->kv_val+0) ^ xorMask;156key1 = (LE_READ_2(k->kv_val+4) ^ xorMask) & 0xffff;157key2 = LE_READ_4(k->kv_val+6) ^ xorMask;158key3 = (LE_READ_2(k->kv_val+10) ^ xorMask) & 0xffff;159key4 = LE_READ_4(k->kv_val+12) ^ xorMask;160if (k->kv_len <= 104 / NBBY)161key4 &= 0xff;162163/*164* Note: WEP key cache hardware requires that each double-word165* pair be written in even/odd order (since the destination is166* a 64-bit register). Don't reorder these writes w/o167* understanding this!168*/169OS_REG_WRITE(ah, AR_KEYTABLE_KEY0(entry), key0);170OS_REG_WRITE(ah, AR_KEYTABLE_KEY1(entry), key1);171OS_REG_WRITE(ah, AR_KEYTABLE_KEY2(entry), key2);172OS_REG_WRITE(ah, AR_KEYTABLE_KEY3(entry), key3);173OS_REG_WRITE(ah, AR_KEYTABLE_KEY4(entry), key4);174OS_REG_WRITE(ah, AR_KEYTABLE_TYPE(entry), keyType);175return ar5211SetKeyCacheEntryMac(ah, entry, mac);176}177178179