18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
28c2ecf20Sopenharmony_ci/* Copyright(c) 2018-2019  Realtek Corporation
38c2ecf20Sopenharmony_ci */
48c2ecf20Sopenharmony_ci
58c2ecf20Sopenharmony_ci#include "main.h"
68c2ecf20Sopenharmony_ci#include "sec.h"
78c2ecf20Sopenharmony_ci#include "reg.h"
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ciint rtw_sec_get_free_cam(struct rtw_sec_desc *sec)
108c2ecf20Sopenharmony_ci{
118c2ecf20Sopenharmony_ci	/* if default key search is enabled, the first 4 cam entries
128c2ecf20Sopenharmony_ci	 * are used to direct map to group key with its key->key_idx, so
138c2ecf20Sopenharmony_ci	 * driver should use cam entries after 4 to install pairwise key
148c2ecf20Sopenharmony_ci	 */
158c2ecf20Sopenharmony_ci	if (sec->default_key_search)
168c2ecf20Sopenharmony_ci		return find_next_zero_bit(sec->cam_map, RTW_MAX_SEC_CAM_NUM,
178c2ecf20Sopenharmony_ci					  RTW_SEC_DEFAULT_KEY_NUM);
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci	return find_first_zero_bit(sec->cam_map, RTW_MAX_SEC_CAM_NUM);
208c2ecf20Sopenharmony_ci}
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_civoid rtw_sec_write_cam(struct rtw_dev *rtwdev,
238c2ecf20Sopenharmony_ci		       struct rtw_sec_desc *sec,
248c2ecf20Sopenharmony_ci		       struct ieee80211_sta *sta,
258c2ecf20Sopenharmony_ci		       struct ieee80211_key_conf *key,
268c2ecf20Sopenharmony_ci		       u8 hw_key_type, u8 hw_key_idx)
278c2ecf20Sopenharmony_ci{
288c2ecf20Sopenharmony_ci	struct rtw_cam_entry *cam = &sec->cam_table[hw_key_idx];
298c2ecf20Sopenharmony_ci	u32 write_cmd;
308c2ecf20Sopenharmony_ci	u32 command;
318c2ecf20Sopenharmony_ci	u32 content;
328c2ecf20Sopenharmony_ci	u32 addr;
338c2ecf20Sopenharmony_ci	int i, j;
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci	set_bit(hw_key_idx, sec->cam_map);
368c2ecf20Sopenharmony_ci	cam->valid = true;
378c2ecf20Sopenharmony_ci	cam->group = !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE);
388c2ecf20Sopenharmony_ci	cam->hw_key_type = hw_key_type;
398c2ecf20Sopenharmony_ci	cam->key = key;
408c2ecf20Sopenharmony_ci	if (sta)
418c2ecf20Sopenharmony_ci		ether_addr_copy(cam->addr, sta->addr);
428c2ecf20Sopenharmony_ci	else
438c2ecf20Sopenharmony_ci		eth_broadcast_addr(cam->addr);
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci	write_cmd = RTW_SEC_CMD_WRITE_ENABLE | RTW_SEC_CMD_POLLING;
468c2ecf20Sopenharmony_ci	addr = hw_key_idx << RTW_SEC_CAM_ENTRY_SHIFT;
478c2ecf20Sopenharmony_ci	for (i = 7; i >= 0; i--) {
488c2ecf20Sopenharmony_ci		switch (i) {
498c2ecf20Sopenharmony_ci		case 0:
508c2ecf20Sopenharmony_ci			content = ((key->keyidx & 0x3))		|
518c2ecf20Sopenharmony_ci				  ((hw_key_type & 0x7)	<< 2)	|
528c2ecf20Sopenharmony_ci				  (cam->group		<< 6)	|
538c2ecf20Sopenharmony_ci				  (cam->valid		<< 15)	|
548c2ecf20Sopenharmony_ci				  (cam->addr[0]		<< 16)	|
558c2ecf20Sopenharmony_ci				  (cam->addr[1]		<< 24);
568c2ecf20Sopenharmony_ci			break;
578c2ecf20Sopenharmony_ci		case 1:
588c2ecf20Sopenharmony_ci			content = (cam->addr[2])		|
598c2ecf20Sopenharmony_ci				  (cam->addr[3]		<< 8)	|
608c2ecf20Sopenharmony_ci				  (cam->addr[4]		<< 16)	|
618c2ecf20Sopenharmony_ci				  (cam->addr[5]		<< 24);
628c2ecf20Sopenharmony_ci			break;
638c2ecf20Sopenharmony_ci		case 6:
648c2ecf20Sopenharmony_ci		case 7:
658c2ecf20Sopenharmony_ci			content = 0;
668c2ecf20Sopenharmony_ci			break;
678c2ecf20Sopenharmony_ci		default:
688c2ecf20Sopenharmony_ci			j = (i - 2) << 2;
698c2ecf20Sopenharmony_ci			content = (key->key[j])			|
708c2ecf20Sopenharmony_ci				  (key->key[j + 1]	<< 8)	|
718c2ecf20Sopenharmony_ci				  (key->key[j + 2]	<< 16)	|
728c2ecf20Sopenharmony_ci				  (key->key[j + 3]	<< 24);
738c2ecf20Sopenharmony_ci			break;
748c2ecf20Sopenharmony_ci		}
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci		command = write_cmd | (addr + i);
778c2ecf20Sopenharmony_ci		rtw_write32(rtwdev, RTW_SEC_WRITE_REG, content);
788c2ecf20Sopenharmony_ci		rtw_write32(rtwdev, RTW_SEC_CMD_REG, command);
798c2ecf20Sopenharmony_ci	}
808c2ecf20Sopenharmony_ci}
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_civoid rtw_sec_clear_cam(struct rtw_dev *rtwdev,
838c2ecf20Sopenharmony_ci		       struct rtw_sec_desc *sec,
848c2ecf20Sopenharmony_ci		       u8 hw_key_idx)
858c2ecf20Sopenharmony_ci{
868c2ecf20Sopenharmony_ci	struct rtw_cam_entry *cam = &sec->cam_table[hw_key_idx];
878c2ecf20Sopenharmony_ci	u32 write_cmd;
888c2ecf20Sopenharmony_ci	u32 command;
898c2ecf20Sopenharmony_ci	u32 addr;
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci	clear_bit(hw_key_idx, sec->cam_map);
928c2ecf20Sopenharmony_ci	cam->valid = false;
938c2ecf20Sopenharmony_ci	cam->key = NULL;
948c2ecf20Sopenharmony_ci	eth_zero_addr(cam->addr);
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci	write_cmd = RTW_SEC_CMD_WRITE_ENABLE | RTW_SEC_CMD_POLLING;
978c2ecf20Sopenharmony_ci	addr = hw_key_idx << RTW_SEC_CAM_ENTRY_SHIFT;
988c2ecf20Sopenharmony_ci	command = write_cmd | addr;
998c2ecf20Sopenharmony_ci	rtw_write32(rtwdev, RTW_SEC_WRITE_REG, 0);
1008c2ecf20Sopenharmony_ci	rtw_write32(rtwdev, RTW_SEC_CMD_REG, command);
1018c2ecf20Sopenharmony_ci}
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ciu8 rtw_sec_cam_pg_backup(struct rtw_dev *rtwdev, u8 *used_cam)
1048c2ecf20Sopenharmony_ci{
1058c2ecf20Sopenharmony_ci	struct rtw_sec_desc *sec = &rtwdev->sec;
1068c2ecf20Sopenharmony_ci	u8 offset = 0;
1078c2ecf20Sopenharmony_ci	u8 count, n;
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci	if (!used_cam)
1108c2ecf20Sopenharmony_ci		return 0;
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci	for (count = 0; count < MAX_PG_CAM_BACKUP_NUM; count++) {
1138c2ecf20Sopenharmony_ci		n = find_next_bit(sec->cam_map, RTW_MAX_SEC_CAM_NUM, offset);
1148c2ecf20Sopenharmony_ci		if (n == RTW_MAX_SEC_CAM_NUM)
1158c2ecf20Sopenharmony_ci			break;
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci		used_cam[count] = n;
1188c2ecf20Sopenharmony_ci		offset = n + 1;
1198c2ecf20Sopenharmony_ci	}
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci	return count;
1228c2ecf20Sopenharmony_ci}
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_civoid rtw_sec_enable_sec_engine(struct rtw_dev *rtwdev)
1258c2ecf20Sopenharmony_ci{
1268c2ecf20Sopenharmony_ci	struct rtw_sec_desc *sec = &rtwdev->sec;
1278c2ecf20Sopenharmony_ci	u16 ctrl_reg;
1288c2ecf20Sopenharmony_ci	u16 sec_config;
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ci	/* default use default key search for now */
1318c2ecf20Sopenharmony_ci	sec->default_key_search = true;
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci	ctrl_reg = rtw_read16(rtwdev, REG_CR);
1348c2ecf20Sopenharmony_ci	ctrl_reg |= RTW_SEC_ENGINE_EN;
1358c2ecf20Sopenharmony_ci	rtw_write16(rtwdev, REG_CR, ctrl_reg);
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_ci	sec_config = rtw_read16(rtwdev, RTW_SEC_CONFIG);
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci	sec_config |= RTW_SEC_TX_DEC_EN | RTW_SEC_RX_DEC_EN;
1408c2ecf20Sopenharmony_ci	if (sec->default_key_search)
1418c2ecf20Sopenharmony_ci		sec_config |= RTW_SEC_TX_UNI_USE_DK | RTW_SEC_RX_UNI_USE_DK |
1428c2ecf20Sopenharmony_ci			      RTW_SEC_TX_BC_USE_DK | RTW_SEC_RX_BC_USE_DK;
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci	rtw_write16(rtwdev, RTW_SEC_CONFIG, sec_config);
1458c2ecf20Sopenharmony_ci}
146