1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /* Copyright(c) 2018-2019 Realtek Corporation
9 int rtw_sec_get_free_cam(struct rtw_sec_desc
*sec
)
11 /* if default key search is enabled, the first 4 cam entries
12 * are used to direct map to group key with its key->key_idx, so
13 * driver should use cam entries after 4 to install pairwise key
15 if (sec
->default_key_search
)
16 return find_next_zero_bit(sec
->cam_map
, RTW_MAX_SEC_CAM_NUM
,
17 RTW_SEC_DEFAULT_KEY_NUM
);
19 return find_first_zero_bit(sec
->cam_map
, RTW_MAX_SEC_CAM_NUM
);
22 void rtw_sec_write_cam(struct rtw_dev
*rtwdev
,
23 struct rtw_sec_desc
*sec
,
24 struct ieee80211_sta
*sta
,
25 struct ieee80211_key_conf
*key
,
26 u8 hw_key_type
, u8 hw_key_idx
)
28 struct rtw_cam_entry
*cam
= &sec
->cam_table
[hw_key_idx
];
35 set_bit(hw_key_idx
, sec
->cam_map
);
37 cam
->group
= !(key
->flags
& IEEE80211_KEY_FLAG_PAIRWISE
);
38 cam
->hw_key_type
= hw_key_type
;
41 ether_addr_copy(cam
->addr
, sta
->addr
);
43 eth_broadcast_addr(cam
->addr
);
45 write_cmd
= RTW_SEC_CMD_WRITE_ENABLE
| RTW_SEC_CMD_POLLING
;
46 addr
= hw_key_idx
<< RTW_SEC_CAM_ENTRY_SHIFT
;
47 for (i
= 5; i
>= 0; i
--) {
50 content
= ((key
->keyidx
& 0x3)) |
51 ((hw_key_type
& 0x7) << 2) |
54 (cam
->addr
[0] << 16) |
58 content
= (cam
->addr
[2]) |
60 (cam
->addr
[4] << 16) |
65 content
= (key
->key
[j
]) |
66 (key
->key
[j
+ 1] << 8) |
67 (key
->key
[j
+ 2] << 16) |
68 (key
->key
[j
+ 3] << 24);
72 command
= write_cmd
| (addr
+ i
);
73 rtw_write32(rtwdev
, RTW_SEC_WRITE_REG
, content
);
74 rtw_write32(rtwdev
, RTW_SEC_CMD_REG
, command
);
78 void rtw_sec_clear_cam(struct rtw_dev
*rtwdev
,
79 struct rtw_sec_desc
*sec
,
82 struct rtw_cam_entry
*cam
= &sec
->cam_table
[hw_key_idx
];
87 clear_bit(hw_key_idx
, sec
->cam_map
);
90 eth_zero_addr(cam
->addr
);
92 write_cmd
= RTW_SEC_CMD_WRITE_ENABLE
| RTW_SEC_CMD_POLLING
;
93 addr
= hw_key_idx
<< RTW_SEC_CAM_ENTRY_SHIFT
;
94 command
= write_cmd
| addr
;
95 rtw_write32(rtwdev
, RTW_SEC_WRITE_REG
, 0);
96 rtw_write32(rtwdev
, RTW_SEC_CMD_REG
, command
);
99 u8
rtw_sec_cam_pg_backup(struct rtw_dev
*rtwdev
, u8
*used_cam
)
101 struct rtw_sec_desc
*sec
= &rtwdev
->sec
;
108 for (count
= 0; count
< MAX_PG_CAM_BACKUP_NUM
; count
++) {
109 n
= find_next_bit(sec
->cam_map
, RTW_MAX_SEC_CAM_NUM
, offset
);
110 if (n
== RTW_MAX_SEC_CAM_NUM
)
120 void rtw_sec_enable_sec_engine(struct rtw_dev
*rtwdev
)
122 struct rtw_sec_desc
*sec
= &rtwdev
->sec
;
126 /* default use default key search for now */
127 sec
->default_key_search
= true;
129 ctrl_reg
= rtw_read16(rtwdev
, REG_CR
);
130 ctrl_reg
|= RTW_SEC_ENGINE_EN
;
131 rtw_write16(rtwdev
, REG_CR
, ctrl_reg
);
133 sec_config
= rtw_read16(rtwdev
, RTW_SEC_CONFIG
);
135 sec_config
|= RTW_SEC_TX_DEC_EN
| RTW_SEC_RX_DEC_EN
;
136 if (sec
->default_key_search
)
137 sec_config
|= RTW_SEC_TX_UNI_USE_DK
| RTW_SEC_RX_UNI_USE_DK
|
138 RTW_SEC_TX_BC_USE_DK
| RTW_SEC_RX_BC_USE_DK
;
140 rtw_write16(rtwdev
, RTW_SEC_CONFIG
, sec_config
);