Merge branches 'powercap' and 'pm-devfreq'
[firefly-linux-kernel-4.4.55.git] / drivers / staging / rtl8192e / rtl8192e / rtl_cam.c
1 /******************************************************************************
2  * Copyright(c) 2008 - 2010 Realtek Corporation. All rights reserved.
3  *
4  * Based on the r8180 driver, which is:
5  * Copyright 2004-2005 Andrea Merello <andrea.merello@gmail.com>, et al.
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of version 2 of the GNU General Public License as
8  * published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License along with
16  * this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
18  *
19  * The full GNU General Public License is included in this distribution in the
20  * file called LICENSE.
21  *
22  * Contact Information:
23  * wlanfae <wlanfae@realtek.com>
24 ******************************************************************************/
25 #include "rtl_core.h"
26 #include "r8192E_phy.h"
27 #include "r8192E_phyreg.h"
28 #include "r8190P_rtl8256.h" /* RTL8225 Radio frontend */
29 #include "r8192E_cmdpkt.h"
30
31 void CamResetAllEntry(struct net_device *dev)
32 {
33         u32 ulcommand = 0;
34
35         ulcommand |= BIT31|BIT30;
36         write_nic_dword(dev, RWCAM, ulcommand);
37 }
38
39 void write_cam(struct net_device *dev, u8 addr, u32 data)
40 {
41         write_nic_dword(dev, WCAMI, data);
42         write_nic_dword(dev, RWCAM, BIT31|BIT16|(addr&0xff));
43 }
44
45 u32 read_cam(struct net_device *dev, u8 addr)
46 {
47         write_nic_dword(dev, RWCAM, 0x80000000|(addr&0xff));
48         return read_nic_dword(dev, 0xa8);
49 }
50
51 void EnableHWSecurityConfig8192(struct net_device *dev)
52 {
53         u8 SECR_value = 0x0;
54         struct r8192_priv *priv = (struct r8192_priv *)rtllib_priv(dev);
55         struct rtllib_device *ieee = priv->rtllib;
56
57         SECR_value = SCR_TxEncEnable | SCR_RxDecEnable;
58         if (((KEY_TYPE_WEP40 == ieee->pairwise_key_type) ||
59              (KEY_TYPE_WEP104 == ieee->pairwise_key_type)) &&
60              (priv->rtllib->auth_mode != 2)) {
61                 SECR_value |= SCR_RxUseDK;
62                 SECR_value |= SCR_TxUseDK;
63         } else if ((ieee->iw_mode == IW_MODE_ADHOC) &&
64                    (ieee->pairwise_key_type & (KEY_TYPE_CCMP |
65                    KEY_TYPE_TKIP))) {
66                 SECR_value |= SCR_RxUseDK;
67                 SECR_value |= SCR_TxUseDK;
68         }
69
70
71         ieee->hwsec_active = 1;
72         if ((ieee->pHTInfo->IOTAction&HT_IOT_ACT_PURE_N_MODE) || !hwwep) {
73                 ieee->hwsec_active = 0;
74                 SECR_value &= ~SCR_RxDecEnable;
75         }
76
77         RT_TRACE(COMP_SEC, "%s:, hwsec:%d, pairwise_key:%d, SECR_value:%x\n",
78                  __func__, ieee->hwsec_active, ieee->pairwise_key_type,
79                  SECR_value);
80         write_nic_byte(dev, SECR,  SECR_value);
81 }
82
83 void set_swcam(struct net_device *dev, u8 EntryNo, u8 KeyIndex, u16 KeyType,
84                u8 *MacAddr, u8 DefaultKey, u32 *KeyContent, u8 is_mesh)
85 {
86         struct r8192_priv *priv = rtllib_priv(dev);
87         struct rtllib_device *ieee = priv->rtllib;
88
89         RT_TRACE(COMP_DBG, "===========>%s():EntryNo is %d,KeyIndex is "
90                  "%d,KeyType is %d,is_mesh is %d\n", __func__, EntryNo,
91                  KeyIndex, KeyType, is_mesh);
92         if (!is_mesh) {
93                 ieee->swcamtable[EntryNo].bused = true;
94                 ieee->swcamtable[EntryNo].key_index = KeyIndex;
95                 ieee->swcamtable[EntryNo].key_type = KeyType;
96                 memcpy(ieee->swcamtable[EntryNo].macaddr, MacAddr, 6);
97                 ieee->swcamtable[EntryNo].useDK = DefaultKey;
98                 memcpy(ieee->swcamtable[EntryNo].key_buf, (u8 *)KeyContent, 16);
99         }
100 }
101
102 void setKey(struct net_device *dev, u8 EntryNo, u8 KeyIndex, u16 KeyType,
103             u8 *MacAddr, u8 DefaultKey, u32 *KeyContent)
104 {
105         u32 TargetCommand = 0;
106         u32 TargetContent = 0;
107         u16 usConfig = 0;
108         u8 i;
109         struct r8192_priv *priv = (struct r8192_priv *)rtllib_priv(dev);
110         enum rt_rf_power_state rtState;
111
112         rtState = priv->rtllib->eRFPowerState;
113         if (priv->rtllib->PowerSaveControl.bInactivePs) {
114                 if (rtState == eRfOff) {
115                         if (priv->rtllib->RfOffReason > RF_CHANGE_BY_IPS) {
116                                 RT_TRACE(COMP_ERR, "%s(): RF is OFF.\n",
117                                         __func__);
118                                 return ;
119                         } else {
120                                 down(&priv->rtllib->ips_sem);
121                                 IPSLeave(dev);
122                                 up(&priv->rtllib->ips_sem);
123                         }
124                 }
125         }
126         priv->rtllib->is_set_key = true;
127         if (EntryNo >= TOTAL_CAM_ENTRY)
128                 RT_TRACE(COMP_ERR, "cam entry exceeds in setKey()\n");
129
130         RT_TRACE(COMP_SEC, "====>to setKey(), dev:%p, EntryNo:%d, KeyIndex:%d,"
131                  "KeyType:%d, MacAddr %pM\n", dev, EntryNo, KeyIndex,
132                  KeyType, MacAddr);
133
134         if (DefaultKey)
135                 usConfig |= BIT15 | (KeyType<<2);
136         else
137                 usConfig |= BIT15 | (KeyType<<2) | KeyIndex;
138
139
140         for (i = 0; i < CAM_CONTENT_COUNT; i++) {
141                 TargetCommand  = i + CAM_CONTENT_COUNT * EntryNo;
142                 TargetCommand |= BIT31|BIT16;
143
144                 if (i == 0) {
145                         TargetContent = (u32)(*(MacAddr+0)) << 16 |
146                                 (u32)(*(MacAddr+1)) << 24 |
147                                 (u32)usConfig;
148
149                         write_nic_dword(dev, WCAMI, TargetContent);
150                         write_nic_dword(dev, RWCAM, TargetCommand);
151                 } else if (i == 1) {
152                         TargetContent = (u32)(*(MacAddr+2)) |
153                                 (u32)(*(MacAddr+3)) <<  8 |
154                                 (u32)(*(MacAddr+4)) << 16 |
155                                 (u32)(*(MacAddr+5)) << 24;
156                         write_nic_dword(dev, WCAMI, TargetContent);
157                         write_nic_dword(dev, RWCAM, TargetCommand);
158                 } else {
159                         if (KeyContent != NULL) {
160                                 write_nic_dword(dev, WCAMI,
161                                                 (u32)(*(KeyContent+i-2)));
162                                 write_nic_dword(dev, RWCAM, TargetCommand);
163                                 udelay(100);
164                         }
165                 }
166         }
167         RT_TRACE(COMP_SEC, "=========>after set key, usconfig:%x\n", usConfig);
168 }
169
170 void CamRestoreAllEntry(struct net_device *dev)
171 {
172         u8 EntryId = 0;
173         struct r8192_priv *priv = rtllib_priv(dev);
174         u8 *MacAddr = priv->rtllib->current_network.bssid;
175
176         static u8       CAM_CONST_ADDR[4][6] = {
177                 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
178                 {0x00, 0x00, 0x00, 0x00, 0x00, 0x01},
179                 {0x00, 0x00, 0x00, 0x00, 0x00, 0x02},
180                 {0x00, 0x00, 0x00, 0x00, 0x00, 0x03}
181         };
182         static u8       CAM_CONST_BROAD[] = {
183                 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
184         };
185
186         RT_TRACE(COMP_SEC, "CamRestoreAllEntry:\n");
187
188
189         if ((priv->rtllib->pairwise_key_type == KEY_TYPE_WEP40) ||
190             (priv->rtllib->pairwise_key_type == KEY_TYPE_WEP104)) {
191
192                 for (EntryId = 0; EntryId < 4; EntryId++) {
193                         MacAddr = CAM_CONST_ADDR[EntryId];
194                         if (priv->rtllib->swcamtable[EntryId].bused) {
195                                 setKey(dev, EntryId , EntryId,
196                                        priv->rtllib->pairwise_key_type, MacAddr,
197                                        0, (u32 *)(&priv->rtllib->swcamtable
198                                       [EntryId].key_buf[0]));
199                         }
200                 }
201
202         } else if (priv->rtllib->pairwise_key_type == KEY_TYPE_TKIP) {
203                 if (priv->rtllib->iw_mode == IW_MODE_ADHOC) {
204                         setKey(dev, 4, 0, priv->rtllib->pairwise_key_type,
205                                (u8 *)dev->dev_addr, 0,
206                                (u32 *)(&priv->rtllib->swcamtable[4].key_buf[0]));
207                 } else {
208                         setKey(dev, 4, 0, priv->rtllib->pairwise_key_type,
209                                MacAddr, 0,
210                                (u32 *)(&priv->rtllib->swcamtable[4].key_buf[0]));
211                 }
212
213         } else if (priv->rtllib->pairwise_key_type == KEY_TYPE_CCMP) {
214                 if (priv->rtllib->iw_mode == IW_MODE_ADHOC) {
215                         setKey(dev, 4, 0,
216                                priv->rtllib->pairwise_key_type,
217                                (u8 *)dev->dev_addr, 0,
218                                (u32 *)(&priv->rtllib->swcamtable[4].
219                                key_buf[0]));
220                 } else {
221                         setKey(dev, 4, 0,
222                                priv->rtllib->pairwise_key_type, MacAddr,
223                                0, (u32 *)(&priv->rtllib->swcamtable[4].
224                                key_buf[0]));
225                         }
226         }
227
228         if (priv->rtllib->group_key_type == KEY_TYPE_TKIP) {
229                 MacAddr = CAM_CONST_BROAD;
230                 for (EntryId = 1; EntryId < 4; EntryId++) {
231                         if (priv->rtllib->swcamtable[EntryId].bused) {
232                                 setKey(dev, EntryId, EntryId,
233                                         priv->rtllib->group_key_type,
234                                         MacAddr, 0,
235                                         (u32 *)(&priv->rtllib->swcamtable[EntryId].key_buf[0])
236                                      );
237                         }
238                 }
239                 if (priv->rtllib->iw_mode == IW_MODE_ADHOC) {
240                         if (priv->rtllib->swcamtable[0].bused) {
241                                 setKey(dev, 0, 0,
242                                        priv->rtllib->group_key_type,
243                                        CAM_CONST_ADDR[0], 0,
244                                        (u32 *)(&priv->rtllib->swcamtable[0].key_buf[0])
245                                      );
246                         } else {
247                                 RT_TRACE(COMP_ERR, "===>%s():ERR!! ADHOC TKIP "
248                                          ",but 0 entry is have no data\n",
249                                          __func__);
250                                 return;
251                         }
252                 }
253         } else if (priv->rtllib->group_key_type == KEY_TYPE_CCMP) {
254                 MacAddr = CAM_CONST_BROAD;
255                 for (EntryId = 1; EntryId < 4; EntryId++) {
256                         if (priv->rtllib->swcamtable[EntryId].bused) {
257                                 setKey(dev, EntryId , EntryId,
258                                        priv->rtllib->group_key_type,
259                                        MacAddr, 0,
260                                        (u32 *)(&priv->rtllib->swcamtable[EntryId].key_buf[0]));
261                         }
262                 }
263
264                 if (priv->rtllib->iw_mode == IW_MODE_ADHOC) {
265                         if (priv->rtllib->swcamtable[0].bused) {
266                                 setKey(dev, 0 , 0,
267                                         priv->rtllib->group_key_type,
268                                         CAM_CONST_ADDR[0], 0,
269                                         (u32 *)(&priv->rtllib->swcamtable[0].key_buf[0]));
270                         } else {
271                                 RT_TRACE(COMP_ERR, "===>%s():ERR!! ADHOC CCMP ,"
272                                          "but 0 entry is have no data\n",
273                                          __func__);
274                                 return;
275                         }
276                 }
277         }
278 }