xref: /OK3568_Linux_fs/kernel/drivers/net/wireless/rockchip_wlan/ssv6xxx/smac/dev.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  * Copyright (c) 2015 South Silicon Valley Microelectronics Inc.
3  * Copyright (c) 2015 iComm Corporation
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12  * See the GNU General Public License for more details.
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  */
16 
17 #include <linux/nl80211.h>
18 #include <linux/etherdevice.h>
19 #include <linux/delay.h>
20 #include <linux/version.h>
21 #include <linux/time.h>
22 #include <linux/kthread.h>
23 #include <linux/ktime.h>
24 
25 #include <net/mac80211.h>
26 #include <ssv6200.h>
27 #include <hci/hctrl.h>
28 #include "linux_80211.h"
29 #include "lib.h"
30 #include "ssv_rc.h"
31 #include "ssv_ht_rc.h"
32 #include "dev.h"
33 #include "ap.h"
34 #include "init.h"
35 #include "p2p.h"
36 #ifdef CONFIG_SSV_SUPPORT_ANDROID
37 #include "ssv_pm.h"
38 #endif
39 #ifdef MULTI_THREAD_ENCRYPT
40 #include <linux/freezer.h>
41 #endif
42 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,2,0) && LINUX_VERSION_CODE >= KERNEL_VERSION(3,0,0)
43 #include "linux_3_0_0.h"
44 #endif
45 #ifdef CONFIG_SSV6XXX_DEBUGFS
46 #include "ssv6xxx_debugfs.h"
47 #endif
48 #ifdef CONFIG_SSV_RSSI
49 struct rssi_res_st rssi_res, *p_rssi_res;
50 #endif
51 #define NO_USE_RXQ_LOCK
52 #ifndef WLAN_CIPHER_SUITE_SMS4
53 #define WLAN_CIPHER_SUITE_SMS4 0x00147201
54 #endif
55 #ifdef ENABLE_TX_Q_FLOW_CONTROL
56 #ifdef MULTI_THREAD_ENCRYPT
57 #define MAX_CRYPTO_Q_LEN (64)
58 #define LOW_CRYPTO_Q_LEN (MAX_CRYPTO_Q_LEN/2)
59 #endif
60 #define MAX_TX_Q_LEN (64)
61 #define LOW_TX_Q_LEN (MAX_TX_Q_LEN/2)
62 #endif
63 static u16 bits_per_symbol[][2] =
64 {
65     { 26, 54 },
66     { 52, 108 },
67     { 78, 162 },
68     { 104, 216 },
69     { 156, 324 },
70     { 208, 432 },
71     { 234, 486 },
72     { 260, 540 },
73 };
74 #ifdef CONFIG_DEBUG_SKB_TIMESTAMP
75 extern struct ssv6xxx_hci_ctrl *ssv_dbg_ctrl_hci;
76 extern unsigned int cal_duration_of_ampdu(struct sk_buff *ampdu_skb, int stage);
77 #endif
78 struct ssv6xxx_calib_table {
79     u16 channel_id;
80     u32 rf_ctrl_N;
81     u32 rf_ctrl_F;
82     u16 rf_precision_default;
83 };
84 static void _process_rx_q (struct ssv_softc *sc, struct sk_buff_head *rx_q, spinlock_t *rx_q_lock);
85 static u32 _process_tx_done (struct ssv_softc *sc);
86 #ifdef MULTI_THREAD_ENCRYPT
87 static u32 _remove_sta_skb_from_q (struct ssv_softc *sc, struct sk_buff_head *q,
88                                    u32 addr0_3, u32 addr4_5);
89 #ifdef CONFIG_DEBUG_SKB_TIMESTAMP
cal_duration_of_mpdu(struct sk_buff * mpdu_skb)90 unsigned int cal_duration_of_mpdu(struct sk_buff *mpdu_skb)
91 {
92  unsigned int timeout;
93  SKB_info *mpdu_skb_info = (SKB_info *)mpdu_skb->head;
94  timeout = (unsigned int)ktime_to_ms(ktime_sub(ktime_get(), mpdu_skb_info->timestamp));
95  if (timeout > SKB_DURATION_TIMEOUT_MS)
96   printk("*mpdu_duration: %ums\n", timeout);
97  return timeout;
98 }
99 #endif
skb_queue_len_safe(struct sk_buff_head * list)100 unsigned int skb_queue_len_safe(struct sk_buff_head *list)
101 {
102     unsigned long flags;
103     unsigned int ret = 0;
104     spin_lock_irqsave(&list->lock, flags);
105     ret = skb_queue_len(list);
106     spin_unlock_irqrestore(&list->lock, flags);
107     return ret;
108 }
109 #endif
110 #if 1
_ssv6xxx_hexdump(const char * title,const u8 * buf,size_t len)111 void _ssv6xxx_hexdump(const char *title, const u8 *buf,
112                              size_t len)
113 {
114     size_t i;
115     printk("%s - hexdump(len=%lu):\n", title, (unsigned long) len);
116     if (buf == NULL) {
117         printk(" [NULL]");
118     }else{
119         for (i = 0; i < len; i++){
120             printk(" %02x", buf[i]);
121             if((i+1)%16 ==0)
122                 printk("\n");
123         }
124     }
125     printk("\n-----------------------------\n");
126 }
127 #endif
ssv6xxx_txbuf_free_skb(struct sk_buff * skb,void * args)128 void ssv6xxx_txbuf_free_skb(struct sk_buff *skb, void *args)
129 {
130 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0)
131     struct ssv_softc *sc = (struct ssv_softc *)args;
132 #endif
133     if (!skb)
134         return;
135 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0)
136     ieee80211_free_txskb(sc->hw, skb);
137 #else
138     dev_kfree_skb_any(skb);
139 #endif
140 }
141 #define ADDRESS_OFFSET 16
142 #define HW_ID_OFFSET 7
143 #define CH0_FULL_MASK CH0_FULL_MSK
144 #define MAX_FAIL_COUNT 100
145 #define MAX_RETRY_COUNT 20
ssv6xxx_mcu_input_full(struct ssv_softc * sc)146 inline bool ssv6xxx_mcu_input_full(struct ssv_softc *sc)
147 {
148     u32 regval=0;
149     SMAC_REG_READ(sc->sh, ADR_MCU_STATUS, &regval);
150     return CH0_FULL_MASK&regval;
151 }
ssv6xxx_pbuf_alloc(struct ssv_softc * sc,int size,int type)152 u32 ssv6xxx_pbuf_alloc(struct ssv_softc *sc, int size, int type)
153 {
154     u32 regval, pad;
155     int cnt = MAX_RETRY_COUNT;
156     int page_cnt = (size + ((1 << HW_MMU_PAGE_SHIFT) - 1)) >> HW_MMU_PAGE_SHIFT;
157     regval = 0;
158     mutex_lock(&sc->mem_mutex);
159     pad = size%4;
160     size += pad;
161     do{
162         SMAC_REG_WRITE(sc->sh, ADR_WR_ALC, (size | (type << 16)));
163         SMAC_REG_READ(sc->sh, ADR_WR_ALC, &regval);
164         if (regval == 0) {
165             cnt--;
166             msleep(1);
167         }
168         else
169             break;
170     } while (cnt);
171     if (type == TX_BUF)
172     {
173         sc->sh->tx_page_available -= page_cnt;
174         sc->sh->page_count[PACKET_ADDR_2_ID(regval)] = page_cnt;
175     }
176     mutex_unlock(&sc->mem_mutex);
177     if (regval == 0)
178         dev_err(sc->dev, "Failed to allocate packet buffer of %d bytes in %d type.",
179                 size, type);
180     else
181     {
182         dev_info(sc->dev, "Allocated %d type packet buffer of size %d (%d) at address %x.\n",
183                  type, size, page_cnt, regval);
184     }
185     return regval;
186 }
ssv6xxx_pbuf_free(struct ssv_softc * sc,u32 pbuf_addr)187 bool ssv6xxx_pbuf_free(struct ssv_softc *sc, u32 pbuf_addr)
188 {
189     u32 regval=0;
190     u16 failCount=0;
191     u8 *p_tx_page_cnt = &sc->sh->page_count[PACKET_ADDR_2_ID(pbuf_addr)];
192     while (ssv6xxx_mcu_input_full(sc))
193     {
194         if (failCount++ < 1000) continue;
195             printk("=============>ERROR!!MAILBOX Block[%d]\n", failCount);
196             return false;
197     }
198     mutex_lock(&sc->mem_mutex);
199     regval = ((M_ENG_TRASH_CAN << HW_ID_OFFSET) |(pbuf_addr >> ADDRESS_OFFSET));
200     printk("[A] ssv6xxx_pbuf_free addr[%08x][%x]\n", pbuf_addr, regval);
201     SMAC_REG_WRITE(sc->sh, ADR_CH0_TRIG_1, regval);
202     if (*p_tx_page_cnt)
203     {
204         sc->sh->tx_page_available += *p_tx_page_cnt;
205         *p_tx_page_cnt = 0;
206     }
207     mutex_unlock(&sc->mem_mutex);
208     return true;
209 }
210 #ifdef CONFIG_SSV_CABRIO_A
211 static const struct ssv6xxx_calib_table vt_tbl[] =
212 {
213     { 1, 0xf1, 0x333333, 3859},
214     { 2, 0xf1, 0xB33333, 3867},
215     { 3, 0xf2, 0x333333, 3875},
216     { 4, 0xf2, 0xB33333, 3883},
217     { 5, 0xf3, 0x333333, 3891},
218     { 6, 0xf3, 0xB33333, 3899},
219     { 7, 0xf4, 0x333333, 3907},
220     { 8, 0xf4, 0xB33333, 3915},
221     { 9, 0xf5, 0x333333, 3923},
222     { 10, 0xf5, 0xB33333, 3931},
223     { 11, 0xf6, 0x333333, 3939},
224     { 12, 0xf6, 0xB33333, 3947},
225     { 13, 0xf7, 0x333333, 3955},
226     { 14, 0xf8, 0x666666, 3974},
227 };
ssv6xxx_set_channel(struct ssv_softc * sc,int ch)228 int ssv6xxx_set_channel(struct ssv_softc *sc, int ch)
229 {
230     int retry_cnt, fail_cnt=0;
231     struct ssv_hw *sh=sc->sh;
232     u32 regval;
233     int ret = 0;
234     int chidx;
235     bool chidx_vld = 0;
236     for(chidx = 0; chidx < (sizeof(vt_tbl)/sizeof(vt_tbl[0])); chidx++) {
237         if (vt_tbl[chidx].channel_id == ch) {
238             chidx_vld = 1;
239             break;
240         }
241     }
242     if (chidx_vld == 0) {
243         printk("%s(): fail! channel_id not found in vt_tbl\n", __FUNCTION__);
244         return -1;
245     }
246     do {
247         if ((ret = SMAC_REG_READ(sh, ADR_SPI_TO_PHY_PARAM1, &regval)) != 0) break;
248         if ((ret = SMAC_REG_WRITE(sh,ADR_SPI_TO_PHY_PARAM1,(regval&~0xffff)|3)) != 0) break;
249         ssv6xxx_rf_disable(sc->sh);
250         if ((ret = SMAC_REG_SET_BITS(sc->sh, ADR_CBR_SYN_DIV_SDM_XOSC,
251             (0x01<<13), (0x01<<13))) != 0) break;
252         regval = vt_tbl[chidx].rf_ctrl_F;
253         if ((ret = SMAC_REG_SET_BITS(sc->sh, ADR_CBR_SYN_RGISTER_1,
254             (regval << 0), 0x00ffffff)) != 0) break;
255         regval = vt_tbl[chidx].rf_ctrl_N;
256         if ((ret = SMAC_REG_SET_BITS(sh, ADR_CBR_SYN_RGISTER_2,
257             (regval<<0), 0x000007ff)) != 0) break;
258         if ((ret = SMAC_REG_SET_BITS(sh, ADR_CBR_MANUAL_REGISTER,
259             (64<<1), (0x000007f<<1))) != 0) break;
260         if ((ret = SMAC_REG_SET_BITS(sh, ADR_CBR_MANUAL_REGISTER,
261             (1<<0), 0x00000001)) != 0) break;
262         if ((ret = SMAC_REG_SET_BITS(sh, ADR_CBR_MANUAL_REGISTER,
263             (0<<0), 0x00000001)) != 0) break;
264         if ((ret = SMAC_REG_SET_BITS(sh, ADR_CBR_SX_ENABLE_RGISTER,
265             (1<<11), 0x00000800)) != 0) break;
266         if ((ret = SMAC_REG_SET_BITS(sh, ADR_CBR_SX_ENABLE_RGISTER,
267             (0<<12), 0x00001000)) != 0) break;
268         if ((ret = SMAC_REG_SET_BITS(sh, ADR_CBR_SX_ENABLE_RGISTER,
269             (1<<12), 0x00001000)) != 0) break;
270         for(retry_cnt=20; retry_cnt>0; retry_cnt--)
271         {
272             mdelay(20);
273             if ((ret = SMAC_REG_READ(sh, ADR_CBR_READ_ONLY_FLAGS_1, &regval)) != 0) break;
274             if (regval & 0x00000004)
275             {
276                 if ((ret = SMAC_REG_SET_BITS(sh, ADR_CBR_SX_ENABLE_RGISTER,
277                 (0<<12), 0x00001000)) != 0) break;
278                 if ((ret = SMAC_REG_READ(sh, ADR_CBR_READ_ONLY_FLAGS_1, &regval)) != 0) break;
279                 if ((regval & 0x00001800) == 0)
280                 {
281                     ssv6xxx_rf_enable(sh);
282                     return 0;
283                 }
284                 else
285                 {
286                     printk("%s(): Lock channel %d fail!\n", __FUNCTION__, vt_tbl[chidx].channel_id);
287                     if ((ret = SMAC_REG_READ(sh, ADR_CBR_READ_ONLY_FLAGS_1, &regval)) != 0) break;
288                     printk("%s(): dbg: vt-mon read out as %d when rdy\n", __FUNCTION__, ((regval & 0x00001800) >> 11));
289                     if ((ret = SMAC_REG_READ(sh, ADR_CBR_READ_ONLY_FLAGS_2, &regval)) != 0) break;
290                     printk("%s(): dbg: sub-sel read out as %d when rdy\n", __FUNCTION__, ((regval & 0x00000fe0) >> 5));
291                     if ((ret = SMAC_REG_READ(sh, ADR_CBR_SYN_DIV_SDM_XOSC, &regval)) != 0) break;
292                     printk("%s(): dbg: RG_SX_REFBYTWO read out as %d when rdy\n", __FUNCTION__, ((regval & 0x00002000) >> 13));
293                     if ((ret = SMAC_REG_READ(sh, ADR_CBR_SYN_RGISTER_1, &regval)) != 0) break;
294                     printk("%s(): dbg: RG_SX_RFCTRL_F read out as 0x%08x when rdy\n", __FUNCTION__, ((regval & 0x00ffffff) >> 0));
295                     if ((ret = SMAC_REG_READ(sh, ADR_CBR_SYN_RGISTER_2, &regval)) != 0) break;
296                     printk("%s(): dbg: RG_SX_RFCTRL_CH read out as 0x%08x when rdy\n", __FUNCTION__, ((regval & 0x000007ff) >> 0));
297                     if ((ret = SMAC_REG_READ(sh, ADR_CBR_SX_ENABLE_RGISTER, &regval)) != 0) break;
298                     printk("%s(): dbg: RG_EN_SX_VT_MON_DG read out as %d when rdy\n", __FUNCTION__, ((regval & 0x00001000) >> 12));
299                 }
300             }
301         }
302         fail_cnt++;
303         printk("%s(): calibration fail [%d] rounds!!\n",
304                 __FUNCTION__, fail_cnt);
305         if(fail_cnt == 100)
306             return -1;
307     } while(ret == 0);
308     return ret;
309 }
310 #endif
311 #ifdef CONFIG_SSV_CABRIO_E
312 static const struct ssv6xxx_calib_table vt_tbl[SSV6XXX_IQK_CFG_XTAL_MAX][14]=
313 {
314     {
315         { 1, 0xB9, 0x89D89E, 3859},
316         { 2, 0xB9, 0xEC4EC5, 3867},
317         { 3, 0xBA, 0x4EC4EC, 3875},
318         { 4, 0xBA, 0xB13B14, 3883},
319         { 5, 0xBB, 0x13B13B, 3891},
320         { 6, 0xBB, 0x762762, 3899},
321         { 7, 0xBB, 0xD89D8A, 3907},
322         { 8, 0xBC, 0x3B13B1, 3915},
323         { 9, 0xBC, 0x9D89D9, 3923},
324         { 10, 0xBD, 0x000000, 3931},
325         { 11, 0xBD, 0x627627, 3939},
326         { 12, 0xBD, 0xC4EC4F, 3947},
327         { 13, 0xBE, 0x276276, 3955},
328         { 14, 0xBF, 0x13B13B, 3974},
329     },
330     {
331         { 1, 0xf1, 0x333333, 3859},
332         { 2, 0xf1, 0xB33333, 3867},
333         { 3, 0xf2, 0x333333, 3875},
334         { 4, 0xf2, 0xB33333, 3883},
335         { 5, 0xf3, 0x333333, 3891},
336         { 6, 0xf3, 0xB33333, 3899},
337         { 7, 0xf4, 0x333333, 3907},
338         { 8, 0xf4, 0xB33333, 3915},
339         { 9, 0xf5, 0x333333, 3923},
340         { 10, 0xf5, 0xB33333, 3931},
341         { 11, 0xf6, 0x333333, 3939},
342         { 12, 0xf6, 0xB33333, 3947},
343         { 13, 0xf7, 0x333333, 3955},
344         { 14, 0xf8, 0x666666, 3974},
345     },
346     {
347         { 1, 0xC9, 0x000000, 3859},
348         { 2, 0xC9, 0x6AAAAB, 3867},
349         { 3, 0xC9, 0xD55555, 3875},
350         { 4, 0xCA, 0x400000, 3883},
351         { 5, 0xCA, 0xAAAAAB, 3891},
352         { 6, 0xCB, 0x155555, 3899},
353         { 7, 0xCB, 0x800000, 3907},
354         { 8, 0xCB, 0xEAAAAB, 3915},
355         { 9, 0xCC, 0x555555, 3923},
356         { 10, 0xCC, 0xC00000, 3931},
357         { 11, 0xCD, 0x2AAAAB, 3939},
358         { 12, 0xCD, 0x955555, 3947},
359         { 13, 0xCE, 0x000000, 3955},
360         { 14, 0xCF, 0x000000, 3974},
361     }
362 };
363 #define FAIL_MAX 100
364 #define RETRY_MAX 20
ssv6xxx_set_channel(struct ssv_softc * sc,int ch)365 int ssv6xxx_set_channel(struct ssv_softc *sc, int ch)
366 {
367     struct ssv_hw *sh=sc->sh;
368     int retry_cnt, fail_cnt=0;
369     u32 regval;
370     int ret = -1;
371     int chidx;
372     bool chidx_vld = 0;
373     dev_dbg(sc->dev, "Setting channel to %d\n", ch);
374     if((sh->cfg.chip_identity == SSV6051Z) || (sc->sh->cfg.chip_identity == SSV6051P))
375     {
376         if((ch == 13) || (ch == 14))
377         {
378             if(sh->ipd_channel_touch == 0)
379             {
380                 for (chidx = 0; chidx < sh->ch_cfg_size; chidx++)
381                 {
382                     SMAC_REG_WRITE(sh, sh->p_ch_cfg[chidx].reg_addr, sh->p_ch_cfg[chidx].ch13_14_value);
383                 }
384                 sh->ipd_channel_touch = 1;
385             }
386         }
387         else
388         {
389             if(sh->ipd_channel_touch)
390             {
391                 for (chidx = 0; chidx < sh->ch_cfg_size; chidx++)
392                 {
393                     SMAC_REG_WRITE(sh, sh->p_ch_cfg[chidx].reg_addr, sh->p_ch_cfg[chidx].ch1_12_value);
394                 }
395                 sh->ipd_channel_touch = 0;
396             }
397         }
398     }
399     for(chidx = 0; chidx < 14; chidx++) {
400         if (vt_tbl[sh->cfg.crystal_type][chidx].channel_id == ch) {
401             chidx_vld = 1;
402             break;
403         }
404     }
405     if (chidx_vld == 0) {
406         dev_dbg(sc->dev, "%s(): fail! channel_id not found in vt_tbl\n", __FUNCTION__);
407         goto exit;
408     }
409     if ((ret = ssv6xxx_rf_disable(sc->sh)) != 0)
410         goto exit;
411     do {
412         if((sh->cfg.crystal_type == SSV6XXX_IQK_CFG_XTAL_26M) || (sh->cfg.crystal_type == SSV6XXX_IQK_CFG_XTAL_24M))
413         {
414             if ((ret = SMAC_REG_SET_BITS(sc->sh, ADR_SYN_DIV_SDM_XOSC,
415                 (0x00<<13), (0x01<<13))) != 0) break;
416         }
417         else if(sh->cfg.crystal_type == SSV6XXX_IQK_CFG_XTAL_40M)
418         {
419             if ((ret = SMAC_REG_SET_BITS(sc->sh, ADR_SYN_DIV_SDM_XOSC,
420                 (0x01<<13), (0x01<<13))) != 0) break;
421         }
422         else
423         {
424             printk("Illegal xtal setting -- ssv6xxx_set_channel\n");
425             BUG_ON(1);
426         }
427         if ((ret = SMAC_REG_SET_BITS(sc->sh, ADR_SX_LCK_BIN_REGISTERS_I,
428             (0x01<<19), (0x01<<19))) != 0) break;
429         regval = vt_tbl[sh->cfg.crystal_type][chidx].rf_ctrl_F;
430         if ((ret = SMAC_REG_SET_BITS(sc->sh, ADR_SYN_REGISTER_1,
431             (regval<<0), (0x00ffffff<<0))) != 0) break;
432         regval = vt_tbl[sh->cfg.crystal_type][chidx].rf_ctrl_N;
433         if ((ret = SMAC_REG_SET_BITS(sc->sh, ADR_SYN_REGISTER_2,
434             (regval<<0), (0x07ff<<0))) != 0) break;
435         if ((ret = SMAC_REG_READ(sc->sh, ADR_SX_LCK_BIN_REGISTERS_I, &regval)) != 0) break;
436         regval = vt_tbl[sh->cfg.crystal_type][chidx].rf_precision_default;
437         if ((ret = SMAC_REG_SET_BITS(sc->sh, ADR_SX_LCK_BIN_REGISTERS_II,
438             (regval<<0), (0x1fff<<0))) != 0) break;
439         if ((ret = SMAC_REG_SET_BITS(sc->sh, ADR_MANUAL_ENABLE_REGISTER,
440             (0x00<<14), (0x01<<14))) != 0) break;
441         if ((ret = SMAC_REG_SET_BITS(sc->sh, ADR_MANUAL_ENABLE_REGISTER,
442             (0x01<<14), (0x01<<14))) != 0) break;
443         retry_cnt = 0;
444         do
445         {
446             mdelay(1);
447             if ((ret = SMAC_REG_READ(sc->sh, ADR_READ_ONLY_FLAGS_1, &regval)) != 0) break;
448             if (regval & 0x00000002)
449             {
450                 if ((ret = SMAC_REG_READ(sc->sh, ADR_READ_ONLY_FLAGS_2, &regval)) != 0) break;
451                 ret = ssv6xxx_rf_enable(sc->sh);
452 #if 0
453                 printk("Lock to channel %d ([0xce010098]=%x)!!\n", vt_tbl[sh->cfg.crystal_type][chidx].channel_id, regval);
454                 printk("crystal_type [%d]\n",sh->cfg.crystal_type);
455                 SMAC_REG_READ(sc->sh, 0xce010040, &regval);
456                 printk("0xce010040 [%x]\n",regval);
457                 SMAC_REG_READ(sc->sh, 0xce0100a4, &regval);
458                 printk("0xce0100a4 [%x]\n",regval);
459                 SMAC_REG_READ(sc->sh, ADR_DPLL_DIVIDER_REGISTER, &regval);
460                 printk("0xce010060 [%x]\n",regval);
461                 SMAC_REG_READ(sc->sh, ADR_SX_ENABLE_REGISTER, &regval);
462                 printk("0xce010038 [%x]\n",regval);
463                 SMAC_REG_READ(sc->sh, 0xce01003C, &regval);
464                 printk("0xce01003C [%x]\n",regval);
465                 SMAC_REG_READ(sc->sh, ADR_DPLL_FB_DIVIDER_REGISTERS_I, &regval);
466                 printk("0xce01009c [%x]\n",regval);
467                 SMAC_REG_READ(sc->sh, ADR_DPLL_FB_DIVIDER_REGISTERS_II, &regval);
468                 printk("0xce0100a0 [%x]\n",regval);
469                 printk("[%x][%x][%x]\n",vt_tbl[sh->cfg.crystal_type][chidx].rf_ctrl_N,vt_tbl[sh->cfg.crystal_type][chidx].rf_ctrl_F,vt_tbl[sh->cfg.crystal_type][chidx].rf_precision_default);
470 #endif
471                 //dev_info(sc->dev, "Lock to channel %d ([0xce010098]=%x)!!\n", vt_tbl[sh->cfg.crystal_type][chidx].channel_id, regval);
472                 sc->hw_chan = ch;
473                 goto exit;
474             }
475             retry_cnt++;
476         }
477         while(retry_cnt < RETRY_MAX);
478         fail_cnt++;
479         printk("calibation fail:[%d]\n", fail_cnt);
480     }
481     while((fail_cnt < FAIL_MAX) && (ret == 0));
482 exit:
483  if (ch == 14 && regval == 0xff0) {
484         SMAC_IFC_RESET(sc->sh);
485         ssv6xxx_restart_hw(sc);
486     }
487     if(ch <= 7)
488     {
489         if(sh->cfg.tx_power_index_1)
490         {
491             SMAC_REG_READ(sc->sh, ADR_RX_TX_FSM_REGISTER, &regval);
492             regval &= RG_TX_GAIN_OFFSET_I_MSK;
493             regval |= (sh->cfg.tx_power_index_1 << RG_TX_GAIN_OFFSET_SFT);
494             SMAC_REG_WRITE(sc->sh, ADR_RX_TX_FSM_REGISTER, regval);
495         }
496         else if(sh->cfg.tx_power_index_2)
497         {
498             SMAC_REG_READ(sc->sh, ADR_RX_TX_FSM_REGISTER, &regval);
499             regval &= RG_TX_GAIN_OFFSET_I_MSK;
500             SMAC_REG_WRITE(sc->sh, ADR_RX_TX_FSM_REGISTER, regval);
501         }
502     }
503     else
504     {
505         if(sh->cfg.tx_power_index_2)
506         {
507             SMAC_REG_READ(sc->sh, ADR_RX_TX_FSM_REGISTER, &regval);
508             regval &= RG_TX_GAIN_OFFSET_I_MSK;
509             regval |= (sh->cfg.tx_power_index_2 << RG_TX_GAIN_OFFSET_SFT);
510             SMAC_REG_WRITE(sc->sh, ADR_RX_TX_FSM_REGISTER, regval);
511         }
512         else if(sh->cfg.tx_power_index_1)
513         {
514             SMAC_REG_READ(sc->sh, ADR_RX_TX_FSM_REGISTER, &regval);
515             regval &= RG_TX_GAIN_OFFSET_I_MSK;
516             SMAC_REG_WRITE(sc->sh, ADR_RX_TX_FSM_REGISTER, regval);
517         }
518     }
519     return ret;
520 }
521 #ifdef CONFIG_SSV_SMARTLINK
ssv6xxx_get_channel(struct ssv_softc * sc,int * pch)522 int ssv6xxx_get_channel(struct ssv_softc *sc, int *pch)
523 {
524     *pch = sc->hw_chan;
525     return 0;
526 }
ssv6xxx_set_promisc(struct ssv_softc * sc,int accept)527 int ssv6xxx_set_promisc(struct ssv_softc *sc, int accept)
528 {
529     u32 val=0;
530     if (accept)
531     {
532         val = 0x2;
533     }
534     else
535     {
536         val = 0x3;
537     }
538     SMAC_REG_WRITE(sc->sh, ADR_MRX_FLT_TB13, val);
539     return 0;
540 }
ssv6xxx_get_promisc(struct ssv_softc * sc,int * paccept)541 int ssv6xxx_get_promisc(struct ssv_softc *sc, int *paccept)
542 {
543     u32 val=0;
544     SMAC_REG_READ(sc->sh, ADR_MRX_FLT_TB13, &val);
545     if (val == 0x2)
546     {
547         *paccept = 1;
548     }
549     else
550     {
551         *paccept = 0;
552     }
553     return 0;
554 }
555 #endif
556 #endif
ssv6xxx_rf_enable(struct ssv_hw * sh)557 int ssv6xxx_rf_enable(struct ssv_hw *sh)
558 {
559     return SMAC_REG_SET_BITS(sh,
560         0xce010000,
561         (0x02<<12), (0x03<<12)
562     );
563 }
ssv6xxx_rf_disable(struct ssv_hw * sh)564 int ssv6xxx_rf_disable(struct ssv_hw *sh)
565 {
566     return SMAC_REG_SET_BITS(sh,
567         0xce010000,
568         (0x01<<12), (0x03<<12)
569     );
570 }
ssv6xxx_update_decision_table(struct ssv_softc * sc)571 int ssv6xxx_update_decision_table(struct ssv_softc *sc)
572 {
573     int i;
574     for(i=0; i<MAC_DECITBL1_SIZE; i++) {
575         SMAC_REG_WRITE(sc->sh, ADR_MRX_FLT_TB0+i*4,
576         sc->mac_deci_tbl[i]);
577         SMAC_REG_CONFIRM(sc->sh, ADR_MRX_FLT_TB0+i*4,
578         sc->mac_deci_tbl[i]);
579     }
580     for(i=0; i<MAC_DECITBL2_SIZE; i++) {
581         SMAC_REG_WRITE(sc->sh, ADR_MRX_FLT_EN0+i*4,
582         sc->mac_deci_tbl[i+MAC_DECITBL1_SIZE]);
583         SMAC_REG_CONFIRM(sc->sh, ADR_MRX_FLT_EN0+i*4,
584         sc->mac_deci_tbl[i+MAC_DECITBL1_SIZE]);
585     }
586     return 0;
587 }
ssv6xxx_frame_hdrlen(struct ieee80211_hdr * hdr,bool is_ht)588 static int ssv6xxx_frame_hdrlen(struct ieee80211_hdr *hdr, bool is_ht)
589 {
590     #define CTRL_FRAME_INDEX(fc) ((hdr->frame_control-IEEE80211_STYPE_BACK_REQ)>>4)
591     u16 fc, CTRL_FLEN[]= { 16, 16, 16, 16, 10, 10, 16, 16 };
592     int hdr_len = 24;
593     fc = hdr->frame_control;
594     if (ieee80211_is_ctl(fc))
595         hdr_len = CTRL_FLEN[CTRL_FRAME_INDEX(fc)];
596     else if (ieee80211_is_mgmt(fc)) {
597         if (ieee80211_has_order(fc))
598             hdr_len += ((is_ht==1)? 4: 0);
599     }
600     else {
601         if (ieee80211_has_a4(fc))
602             hdr_len += 6;
603         if (ieee80211_is_data_qos(fc)) {
604             hdr_len += 2;
605             if (ieee80211_has_order(hdr->frame_control) &&
606                 is_ht==true)
607                 hdr_len += 4;
608         }
609     }
610     return hdr_len;
611 }
612 #if 0
613 static void ssv6xxx_dump_tx_desc(struct sk_buff *skb)
614 {
615     struct ssv6200_tx_desc *tx_desc;
616     int s;
617     u8 *dat;
618     tx_desc = (struct ssv6200_tx_desc *)skb->data;
619     printk(">> Tx Frame:\n");
620     for(s=0, dat=skb->data; s<tx_desc->hdr_len; s++) {
621         printk("%02x ", dat[sizeof(*tx_desc)+s]);
622         if (((s+1)& 0x0F) == 0)
623             printk("\n");
624     }
625     printk("length: %d, c_type=%d, f80211=%d, qos=%d, ht=%d, use_4addr=%d, sec=%d\n",
626         tx_desc->len, tx_desc->c_type, tx_desc->f80211, tx_desc->qos, tx_desc->ht,
627         tx_desc->use_4addr, tx_desc->security);
628     printk("more_data=%d, sub_type=%x, extra_info=%d\n", tx_desc->more_data,
629         tx_desc->stype_b5b4, tx_desc->extra_info);
630     printk("fcmd=0x%08x, hdr_offset=%d, frag=%d, unicast=%d, hdr_len=%d\n",
631         tx_desc->fCmd, tx_desc->hdr_offset, tx_desc->frag, tx_desc->unicast,
632         tx_desc->hdr_len);
633     printk("tx_burst=%d, ack_policy=%d, do_rts_cts=%d, reason=%d, payload_offset=%d\n",
634         tx_desc->tx_burst, tx_desc->ack_policy, tx_desc->do_rts_cts,
635         tx_desc->reason, tx_desc->payload_offset);
636     printk("fcmdidx=%d, wsid=%d, txq_idx=%d\n",
637          tx_desc->fCmdIdx, tx_desc->wsid, tx_desc->txq_idx);
638     printk("RTS/CTS Nav=%d, frame_time=%d, crate_idx=%d, drate_idx=%d, dl_len=%d\n",
639         tx_desc->rts_cts_nav, tx_desc->frame_consume_time, tx_desc->crate_idx, tx_desc->drate_idx,
640         tx_desc->dl_length);
641 }
642 static void ssv6xxx_dump_rx_desc(struct sk_buff *skb)
643 {
644     struct ssv6200_rx_desc *rx_desc;
645     rx_desc = (struct ssv6200_rx_desc *)skb->data;
646     printk(">> RX Descriptor:\n");
647     printk("len=%d, c_type=%d, f80211=%d, qos=%d, ht=%d, use_4addr=%d, l3cs_err=%d, l4_cs_err=%d\n",
648         rx_desc->len, rx_desc->c_type, rx_desc->f80211, rx_desc->qos, rx_desc->ht, rx_desc->use_4addr,
649         rx_desc->l3cs_err, rx_desc->l4cs_err);
650     printk("align2=%d, psm=%d, stype_b5b4=%d, extra_info=%d\n",
651         rx_desc->align2, rx_desc->psm, rx_desc->stype_b5b4, rx_desc->extra_info);
652     printk("hdr_offset=%d, reason=%d, rx_result=%d\n", rx_desc->hdr_offset,
653         rx_desc->reason, rx_desc->RxResult);
654 }
655 #endif
ssv6xxx_ht_txtime(u8 rix,int pktlen,int width,int half_gi,bool is_gf)656 static u32 ssv6xxx_ht_txtime(u8 rix, int pktlen, int width,
657                 int half_gi, bool is_gf)
658 {
659     u32 nbits, nsymbits, duration, nsymbols;
660     int streams;
661     streams = 1;
662     nbits = (pktlen << 3) + OFDM_PLCP_BITS;
663     nsymbits = bits_per_symbol[rix % 8][width] * streams;
664     nsymbols = (nbits + nsymbits - 1) / nsymbits;
665     if (!half_gi)
666         duration = SYMBOL_TIME(nsymbols);
667     else
668     {
669         if (!is_gf)
670             duration = DIV_ROUND_UP(SYMBOL_TIME_HALFGI(nsymbols), 4)<<2;
671         else
672         duration = SYMBOL_TIME_HALFGI(nsymbols);
673     }
674     duration += L_STF + L_LTF + L_SIG + HT_SIG + HT_STF + HT_LTF(streams)+HT_SIGNAL_EXT;
675     if (is_gf)
676         duration -=12;
677     duration += HT_SIFS_TIME;
678     return duration;
679 }
ssv6xxx_non_ht_txtime(u8 phy,int kbps,u32 frameLen,bool shortPreamble)680 static u32 ssv6xxx_non_ht_txtime(u8 phy, int kbps,
681                                  u32 frameLen, bool shortPreamble)
682 {
683     u32 bits_per_symbol, num_bits, num_symbols;
684     u32 phy_time, tx_time;
685     if (kbps == 0)
686         return 0;
687     switch (phy) {
688     case WLAN_RC_PHY_CCK:
689         phy_time = CCK_PREAMBLE_BITS + CCK_PLCP_BITS;
690         if (shortPreamble)
691             phy_time >>= 1;
692         num_bits = frameLen << 3;
693         tx_time = CCK_SIFS_TIME + phy_time + ((num_bits * 1000) / kbps);
694         break;
695     case WLAN_RC_PHY_OFDM:
696         bits_per_symbol = (kbps * OFDM_SYMBOL_TIME) / 1000;
697         num_bits = OFDM_PLCP_BITS + (frameLen << 3);
698         num_symbols = DIV_ROUND_UP(num_bits, bits_per_symbol);
699         tx_time = OFDM_SIFS_TIME + OFDM_PREAMBLE_TIME
700             + (num_symbols * OFDM_SYMBOL_TIME);
701         break;
702     default:
703         printk("Unknown phy %u\n", phy);
704                 BUG_ON(1);
705         tx_time = 0;
706         break;
707     }
708     return tx_time;
709 }
ssv6xxx_set_frame_duration(struct ieee80211_tx_info * info,struct ssv_rate_info * ssv_rate,u16 len,struct ssv6200_tx_desc * tx_desc,struct fw_rc_retry_params * rc_params,struct ssv_softc * sc)710 static u32 ssv6xxx_set_frame_duration(struct ieee80211_tx_info *info,
711             struct ssv_rate_info *ssv_rate, u16 len,
712             struct ssv6200_tx_desc *tx_desc, struct fw_rc_retry_params *rc_params,
713             struct ssv_softc *sc)
714 {
715     struct ieee80211_tx_rate *tx_drate;
716     u32 frame_time=0, ack_time=0, rts_cts_nav=0, frame_consume_time=0;
717     u32 l_length=0, drate_kbps=0, crate_kbps=0;
718     bool ctrl_short_preamble=false, is_sgi, is_ht40;
719     bool is_ht, is_gf;
720     int d_phy ,c_phy, nRCParams, mcsidx;
721     struct ssv_rate_ctrl *ssv_rc = NULL;
722     tx_drate = &info->control.rates[0];
723  is_sgi = !!(tx_drate->flags & IEEE80211_TX_RC_SHORT_GI);
724  is_ht40 = !!(tx_drate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH);
725     is_ht = !!(tx_drate->flags & IEEE80211_TX_RC_MCS);
726     is_gf = !!(tx_drate->flags & IEEE80211_TX_RC_GREEN_FIELD);
727 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,10,0)
728  if ((info->control.short_preamble) ||
729   (tx_drate->flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE))
730   ctrl_short_preamble = true;
731 #else
732     if ((info->control.vif &&
733         info->control.vif->bss_conf.use_short_preamble) ||
734   (tx_drate->flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE))
735         ctrl_short_preamble = true;
736 #endif
737 #ifdef FW_RC_RETRY_DEBUG
738     printk("mcs = %d, data rate idx=%d\n",tx_drate->idx, tx_drate[3].count);
739 #endif
740     for (nRCParams = 0; (nRCParams < SSV62XX_TX_MAX_RATES) ; nRCParams++)
741     {
742         if ((rc_params == NULL) || (sc == NULL))
743         {
744             mcsidx = tx_drate->idx;
745             drate_kbps = ssv_rate->drate_kbps;
746             crate_kbps = ssv_rate->crate_kbps;
747         }
748         else
749         {
750             if(rc_params[nRCParams].count == 0)
751             {
752                 break;
753             }
754             ssv_rc = sc->rc;
755             mcsidx = (rc_params[nRCParams].drate - SSV62XX_RATE_MCS_INDEX) % MCS_GROUP_RATES;
756             drate_kbps = ssv_rc->rc_table[rc_params[nRCParams].drate].rate_kbps;
757             crate_kbps = ssv_rc->rc_table[rc_params[nRCParams].crate].rate_kbps;
758         }
759         if (tx_drate->flags & IEEE80211_TX_RC_MCS) {
760             frame_time = ssv6xxx_ht_txtime(mcsidx,
761                     len, is_ht40, is_sgi, is_gf);
762             d_phy = 0;
763         }
764         else {
765             if ((info->band == INDEX_80211_BAND_2GHZ) &&
766                         !(ssv_rate->d_flags & IEEE80211_RATE_ERP_G))
767                             d_phy = WLAN_RC_PHY_CCK;
768                     else
769                             d_phy = WLAN_RC_PHY_OFDM;
770             frame_time = ssv6xxx_non_ht_txtime(d_phy, drate_kbps,
771                 len, ctrl_short_preamble);
772         }
773         if ((info->band == INDEX_80211_BAND_2GHZ) &&
774                         !(ssv_rate->c_flags & IEEE80211_RATE_ERP_G))
775                             c_phy = WLAN_RC_PHY_CCK;
776                     else
777                             c_phy = WLAN_RC_PHY_OFDM;
778         if (tx_desc->unicast) {
779             if (info->flags & IEEE80211_TX_CTL_AMPDU){
780                 ack_time = ssv6xxx_non_ht_txtime(c_phy,
781                     crate_kbps, BA_LEN, ctrl_short_preamble);
782             } else {
783                 ack_time = ssv6xxx_non_ht_txtime(c_phy,
784                     crate_kbps, ACK_LEN, ctrl_short_preamble);
785             }
786         }
787         if (tx_desc->do_rts_cts & IEEE80211_TX_RC_USE_RTS_CTS) {
788             rts_cts_nav = frame_time;
789             rts_cts_nav += ack_time;
790             rts_cts_nav += ssv6xxx_non_ht_txtime(c_phy,
791                 crate_kbps, CTS_LEN, ctrl_short_preamble);
792             frame_consume_time = rts_cts_nav;
793             frame_consume_time += ssv6xxx_non_ht_txtime(c_phy,
794                 crate_kbps, RTS_LEN, ctrl_short_preamble);
795         }else if (tx_desc->do_rts_cts & IEEE80211_TX_RC_USE_CTS_PROTECT) {
796             rts_cts_nav = frame_time;
797             rts_cts_nav += ack_time;
798             frame_consume_time = rts_cts_nav;
799             frame_consume_time += ssv6xxx_non_ht_txtime(c_phy,
800                 crate_kbps, CTS_LEN, ctrl_short_preamble);
801         }
802         else{;}
803         if (tx_drate->flags & IEEE80211_TX_RC_MCS) {
804             l_length = frame_time - HT_SIFS_TIME;
805             l_length = ((l_length-(HT_SIGNAL_EXT+20))+3)>>2;
806             l_length += ((l_length<<1) - 3);
807         }
808         if((rc_params == NULL) || (sc == NULL))
809         {
810             tx_desc->rts_cts_nav = rts_cts_nav;
811             tx_desc->frame_consume_time = (frame_consume_time>>5)+1;;
812             tx_desc->dl_length = l_length;
813             break;
814         }
815         else
816         {
817             rc_params[nRCParams].rts_cts_nav = rts_cts_nav;
818             rc_params[nRCParams].frame_consume_time = (frame_consume_time>>5)+1;
819             rc_params[nRCParams].dl_length = l_length;
820             if(nRCParams == 0)
821             {
822                 tx_desc->drate_idx = rc_params[nRCParams].drate;
823                 tx_desc->crate_idx = rc_params[nRCParams].crate;
824                 tx_desc->rts_cts_nav = rc_params[nRCParams].rts_cts_nav;
825                 tx_desc->frame_consume_time = rc_params[nRCParams].frame_consume_time;
826                 tx_desc->dl_length = rc_params[nRCParams].dl_length;
827             }
828         }
829     }
830     return ack_time;
831 }
ssv6200_hw_set_pair_type(struct ssv_hw * sh,u8 type)832 static void ssv6200_hw_set_pair_type(struct ssv_hw *sh,u8 type)
833 {
834     u32 temp;
835     SMAC_REG_READ(sh,ADR_SCRT_SET,&temp);
836     temp = (temp & PAIR_SCRT_I_MSK);
837     temp |= (type << PAIR_SCRT_SFT);
838     SMAC_REG_WRITE(sh,ADR_SCRT_SET, temp);
839     printk("==>%s: write cipher type %d into hw\n",__func__,type);
840 }
ssv6200_hw_get_pair_type(struct ssv_hw * sh)841 static u32 ssv6200_hw_get_pair_type(struct ssv_hw *sh)
842 {
843     u32 temp;
844     SMAC_REG_READ(sh,ADR_SCRT_SET,&temp);
845     temp &= PAIR_SCRT_MSK;
846     temp = (temp >> PAIR_SCRT_SFT);
847     SMAC_REG_WRITE(sh,ADR_SCRT_SET, temp);
848     printk("==>%s: read cipher type %d from hw\n",__func__, temp);
849     return temp;
850 }
ssv6200_hw_set_group_type(struct ssv_hw * sh,u8 type)851 static void ssv6200_hw_set_group_type(struct ssv_hw *sh,u8 type)
852 {
853     u32 temp;
854     SMAC_REG_READ(sh,ADR_SCRT_SET,&temp);
855     temp = temp & GRP_SCRT_I_MSK;
856     temp |= (type << GRP_SCRT_SFT);
857     SMAC_REG_WRITE(sh,ADR_SCRT_SET, temp);
858     printk(KERN_ERR "Set group key type %d\n", type);
859 }
ssv6xxx_reset_sec_module(struct ssv_softc * sc)860 void ssv6xxx_reset_sec_module(struct ssv_softc *sc)
861 {
862     ssv6200_hw_set_group_type(sc->sh, ME_NONE);
863     ssv6200_hw_set_pair_type(sc->sh, ME_NONE);
864 }
865 #ifdef FW_WSID_WATCH_LIST
hw_update_watch_wsid(struct ssv_softc * sc,struct ieee80211_sta * sta,struct ssv_sta_info * sta_info,int sta_idx,int rx_hw_sec,int ops)866 static int hw_update_watch_wsid(struct ssv_softc *sc, struct ieee80211_sta *sta,
867         struct ssv_sta_info *sta_info, int sta_idx, int rx_hw_sec, int ops)
868 {
869     int ret = 0;
870     int retry_cnt=20;
871     struct sk_buff *skb = NULL;
872     struct cfg_host_cmd *host_cmd;
873     struct ssv6xxx_wsid_params *ptr;
874     printk("cmd=%d for fw wsid list, wsid %d \n", ops, sta_idx);
875     skb = ssv_skb_alloc(HOST_CMD_HDR_LEN + sizeof(struct ssv6xxx_wsid_params));
876     if(skb == NULL || sta_info == NULL || sc == NULL)
877         return -1;
878     skb->data_len = HOST_CMD_HDR_LEN + sizeof(struct ssv6xxx_wsid_params);
879     skb->len = skb->data_len;
880     host_cmd = (struct cfg_host_cmd *)skb->data;
881     host_cmd->c_type = HOST_CMD;
882     host_cmd->h_cmd = (u8)SSV6XXX_HOST_CMD_WSID_OP;
883     host_cmd->len = skb->data_len;
884     ptr = (struct ssv6xxx_wsid_params *)host_cmd->dat8;
885     ptr->cmd = ops;
886     ptr->hw_security = rx_hw_sec;
887     if ((ptr->cmd != SSV6XXX_WSID_OPS_HWWSID_PAIRWISE_SET_TYPE)
888         && (ptr->cmd != SSV6XXX_WSID_OPS_HWWSID_GROUP_SET_TYPE)) {
889         ptr->wsid_idx = (u8)(sta_idx - SSV_NUM_HW_STA);
890     } else {
891         ptr->wsid_idx = (u8)(sta_idx);
892     };
893     memcpy(&ptr->target_wsid, &sta->addr[0], 6);
894     while(((sc->sh->hci.hci_ops->hci_send_cmd(skb)) != 0) && (retry_cnt))
895     {
896         printk(KERN_INFO "WSID cmd=%d retry=%d!!\n", ops, retry_cnt);
897         retry_cnt--;
898     }
899     printk("%s: wsid_idx = %u\n", __FUNCTION__, ptr->wsid_idx);
900     ssv_skb_free(skb);
901     if(ops == SSV6XXX_WSID_OPS_ADD)
902         sta_info->hw_wsid = sta_idx;
903     return ret;
904 }
905 #endif
hw_crypto_key_clear(struct ieee80211_hw * hw,int index,struct ieee80211_key_conf * key,struct ssv_vif_priv_data * vif_priv,struct ssv_sta_priv_data * sta_priv)906 static void hw_crypto_key_clear(struct ieee80211_hw *hw, int index, struct ieee80211_key_conf *key,
907                                 struct ssv_vif_priv_data *vif_priv, struct ssv_sta_priv_data *sta_priv)
908 {
909 #ifdef FW_WSID_WATCH_LIST
910     struct ssv_softc *sc = hw->priv;
911     struct ssv_sta_info *sta_info = NULL;
912     if ((index == 0) && (sta_priv == NULL))
913         return;
914 #endif
915     if ((index < 0) || (index >= 4))
916         return;
917     #if 0
918     if(sta_info){
919         sta_info->s_flags &= ~STA_FLAG_ENCRYPT;
920     }
921     #endif
922     if (index > 0)
923     {
924         if (vif_priv)
925             vif_priv->group_key_idx = 0;
926         if (sta_priv)
927             sta_priv->group_key_idx = 0;
928     }
929 #ifdef FW_WSID_WATCH_LIST
930     if(sta_priv)
931     {
932         sta_info = &sc->sta_info[sta_priv->sta_idx];
933         if ((index == 0) && (sta_priv->has_hw_decrypt == true) && (sta_info->hw_wsid >= SSV_NUM_HW_STA))
934         {
935             hw_update_watch_wsid(sc, sta_info->sta, sta_info, sta_priv->sta_idx, SSV6XXX_WSID_SEC_PAIRWISE
936                 , SSV6XXX_WSID_OPS_DISABLE_CAPS);
937         }
938     }
939     if(vif_priv)
940     {
941         if((index != 0) && !list_empty(&vif_priv->sta_list))
942         {
943             struct ssv_sta_priv_data *sta_priv_iter;
944             list_for_each_entry(sta_priv_iter, &vif_priv->sta_list, list)
945             {
946                 if (((sta_priv_iter->sta_info->s_flags & STA_FLAG_VALID) == 0)
947                      || (sta_priv_iter->sta_info->hw_wsid < SSV_NUM_HW_STA))
948                     continue;
949                 hw_update_watch_wsid(sc, sta_priv_iter->sta_info->sta,
950                     sta_priv_iter->sta_info, sta_priv_iter->sta_idx, SSV6XXX_WSID_SEC_GROUP
951                     , SSV6XXX_WSID_OPS_DISABLE_CAPS);
952             }
953         }
954     }
955 #endif
956     #if 0
957     if (index == 0) {
958         address = sec_key_tbl+(3*sizeof(struct ssv6xxx_hw_key))
959             + wsid*sizeof(struct ssv6xxx_hw_sta_key);
960         for(i=0;i<(sizeof(struct ssv6xxx_hw_sta_key)/4);i++)
961             SMAC_REG_WRITE(sc->sh, address+i*4, 0x0);
962     }
963     else{
964         address = sec_key_tbl+((index-1)*sizeof(struct ssv6xxx_hw_key));
965         for(i=0;i<(sizeof(struct ssv6xxx_hw_key)/4);i++)
966             SMAC_REG_WRITE(sc->sh,address+i*4, 0x0);
967     }
968     #endif
969 }
_set_wep_sw_crypto_key(struct ssv_softc * sc,struct ssv_vif_info * vif_info,struct ssv_sta_info * sta_info,void * param)970 static void _set_wep_sw_crypto_key (struct ssv_softc *sc,
971                                          struct ssv_vif_info *vif_info,
972                                          struct ssv_sta_info *sta_info,
973                                          void *param)
974 {
975     struct ssv_sta_priv_data *sta_priv = (struct ssv_sta_priv_data *)sta_info->sta->drv_priv;
976     struct ssv_vif_priv_data *vif_priv = (struct ssv_vif_priv_data *)vif_info->vif->drv_priv;
977     sta_priv->has_hw_encrypt = vif_priv->has_hw_encrypt;
978     sta_priv->has_hw_decrypt = vif_priv->has_hw_decrypt;
979     sta_priv->need_sw_encrypt = vif_priv->need_sw_encrypt;
980     sta_priv->need_sw_decrypt = vif_priv->need_sw_decrypt;
981 #ifdef USE_LOCAL_WEP_CRYPTO
982     sta_priv->crypto_data.ops = vif_priv->crypto_data.ops;
983     sta_priv->crypto_data.priv = vif_priv->crypto_data.priv;
984 #endif
985 }
_set_wep_hw_crypto_pair_key(struct ssv_softc * sc,struct ssv_vif_info * vif_info,struct ssv_sta_info * sta_info,void * param)986 static void _set_wep_hw_crypto_pair_key (struct ssv_softc *sc,
987                                          struct ssv_vif_info *vif_info,
988                                          struct ssv_sta_info *sta_info,
989                                          void *param)
990 {
991     int wsid = sta_info->hw_wsid;
992     struct ssv6xxx_hw_sec *sram_key = (struct ssv6xxx_hw_sec *)param;
993     int address = 0;
994     int *pointer = NULL;
995 #ifdef SSV6200_ECO
996     u32 sec_key_tbl_base = sc->sh->hw_sec_key[0];
997 #else
998     u32 sec_key_tbl_base = sc->sh->hw_sec_key;
999 #endif
1000     u32 sec_key_tbl = sec_key_tbl_base;
1001     int i;
1002     u8 *key = sram_key->sta_key[0].pair.key;
1003     u32 key_len = *(u16 *)&sram_key->sta_key[0].reserve[0];
1004     struct ssv_sta_priv_data *sta_priv = (struct ssv_sta_priv_data *)sta_info->sta->drv_priv;
1005     struct ssv_vif_priv_data *vif_priv = (struct ssv_vif_priv_data *)vif_info->vif->drv_priv;
1006     if (wsid == (-1))
1007         return;
1008     sram_key->sta_key[wsid].pair_key_idx = 0;
1009     sram_key->sta_key[wsid].group_key_idx = 0;
1010     sta_priv->has_hw_encrypt = vif_priv->has_hw_encrypt;
1011     sta_priv->has_hw_decrypt = vif_priv->has_hw_decrypt;
1012     sta_priv->need_sw_encrypt = vif_priv->need_sw_encrypt;
1013     sta_priv->need_sw_decrypt = vif_priv->need_sw_decrypt;
1014     if (wsid != 0)
1015         memcpy(sram_key->sta_key[wsid].pair.key, key, key_len);
1016     address = sec_key_tbl
1017               + (3*sizeof(struct ssv6xxx_hw_key))
1018               + wsid*sizeof(struct ssv6xxx_hw_sta_key);
1019 #ifdef SSV6200_ECO
1020     address += (0x10000*wsid);
1021 #endif
1022     pointer = (int *)&sram_key->sta_key[wsid];
1023     #if 0
1024     printk(KERN_ERR "Set STA %d WEP pairwise key to %08X.", wsid, address);
1025     printk(KERN_ERR "Set WEP %02X %02X %02X %02X %02X %02X %02X %02X... \n",
1026            key[0], key[1], key[2], key[3], key[4], key[5], key[6], key[7]);
1027     #endif
1028     for (i = 0; i < (sizeof(struct ssv6xxx_hw_sta_key)/4); i++)
1029         SMAC_REG_WRITE(sc->sh, address+(i*4), *(pointer++));
1030 }
_set_wep_hw_crypto_group_key(struct ssv_softc * sc,struct ssv_vif_info * vif_info,struct ssv_sta_info * sta_info,void * param)1031 static void _set_wep_hw_crypto_group_key (struct ssv_softc *sc,
1032                                           struct ssv_vif_info *vif_info,
1033                                           struct ssv_sta_info *sta_info,
1034                                           void *param)
1035 {
1036     int wsid = sta_info->hw_wsid;
1037     struct ssv6xxx_hw_sec *sram_key = (struct ssv6xxx_hw_sec *)param;
1038     int address = 0;
1039     int *pointer = NULL;
1040     u32 key_idx = sram_key->sta_key[0].pair_key_idx;
1041 #ifdef SSV6200_ECO
1042     u32 sec_key_tbl_base = sc->sh->hw_sec_key[0];
1043     u32 key_len = *(u16 *)&sram_key->sta_key[0].reserve[0];
1044     u8 *key = sram_key->group_key[key_idx - 1].key;
1045 #else
1046     u32 sec_key_tbl_base = sc->sh->hw_sec_key;
1047 #endif
1048     u32 sec_key_tbl = sec_key_tbl_base;
1049     struct ssv_sta_priv_data *sta_priv = (struct ssv_sta_priv_data *)sta_info->sta->drv_priv;
1050     struct ssv_vif_priv_data *vif_priv = (struct ssv_vif_priv_data *)vif_info->vif->drv_priv;
1051     if (wsid == (-1))
1052         return;
1053     if (wsid != 0)
1054     {
1055         sram_key->sta_key[wsid].pair_key_idx = key_idx;
1056         sram_key->sta_key[wsid].group_key_idx = key_idx;
1057         sta_priv->has_hw_encrypt = vif_priv->has_hw_encrypt;
1058         sta_priv->has_hw_decrypt = vif_priv->has_hw_decrypt;
1059         sta_priv->need_sw_encrypt = vif_priv->need_sw_encrypt;
1060         sta_priv->need_sw_decrypt = vif_priv->need_sw_decrypt;
1061     }
1062 #ifdef SSV6200_ECO
1063     if (wsid != 0)
1064         memcpy(sram_key->group_key[key_idx - 1].key, key, key_len);
1065     sec_key_tbl += (0x10000 * wsid);
1066     address = sec_key_tbl
1067               + ((key_idx - 1) * sizeof(struct ssv6xxx_hw_key));
1068     pointer = (int *)&sram_key->group_key[key_idx - 1];
1069     {
1070     int i;
1071     for (i = 0; i < (sizeof(struct ssv6xxx_hw_key)/4); i++)
1072         SMAC_REG_WRITE(sc->sh, address+(i*4), *(pointer++));
1073     }
1074 #endif
1075     address = sec_key_tbl
1076               + (3*sizeof(struct ssv6xxx_hw_key))
1077               + (wsid*sizeof(struct ssv6xxx_hw_sta_key));
1078     pointer = (int *)&sram_key->sta_key[wsid];
1079     SMAC_REG_WRITE(sc->sh, address, *(pointer));
1080 }
hw_crypto_key_write_wep(struct ieee80211_hw * hw,struct ieee80211_key_conf * key,u8 algorithm,struct ssv_vif_info * vif_info)1081 static int hw_crypto_key_write_wep(struct ieee80211_hw *hw,
1082                                    struct ieee80211_key_conf *key,
1083                                    u8 algorithm,
1084                                    struct ssv_vif_info *vif_info)
1085 {
1086     struct ssv_softc *sc = hw->priv;
1087     struct ssv6xxx_hw_sec *sramKey = &vif_info->sramKey;
1088 #ifndef SSV6200_ECO
1089     int address = 0x00;
1090     int *pointer=NULL;
1091     u32 sec_key_tbl=sc->sh->hw_sec_key;
1092     int i;
1093 #endif
1094 #ifdef FW_WSID_WATCH_LIST
1095 #endif
1096     if (key->keyidx == 0)
1097     {
1098         ssv6xxx_foreach_vif_sta(sc, vif_info, _set_wep_hw_crypto_pair_key, sramKey);
1099     }
1100     else
1101     {
1102 #ifndef SSV6200_ECO
1103         address = sec_key_tbl
1104                   + ((key->keyidx-1) * sizeof(struct ssv6xxx_hw_key));
1105         pointer = (int *)&sramKey->group_key[key->keyidx-1];
1106         for (i=0;i<(sizeof(struct ssv6xxx_hw_key)/4);i++)
1107            SMAC_REG_WRITE(sc->sh, address+(i*4), *(pointer++));
1108 #endif
1109         ssv6xxx_foreach_vif_sta(sc, vif_info, _set_wep_hw_crypto_group_key, sramKey);
1110     }
1111     return 0;
1112 }
_set_aes_tkip_hw_crypto_group_key(struct ssv_softc * sc,struct ssv_vif_info * vif_info,struct ssv_sta_info * sta_info,void * param)1113 static void _set_aes_tkip_hw_crypto_group_key (struct ssv_softc *sc,
1114                                                struct ssv_vif_info *vif_info,
1115                                                struct ssv_sta_info *sta_info,
1116                                                void *param)
1117 {
1118     int wsid = sta_info->hw_wsid;
1119 #ifdef SSV6200_ECO
1120     int j;
1121     u32 sec_key_tbl_base = sc->sh->hw_sec_key[0];
1122 #else
1123     u32 sec_key_tbl_base = sc->sh->hw_sec_key;
1124 #endif
1125     u32 sec_key_tbl = sec_key_tbl_base;
1126     int address = 0;
1127     int *pointer = 0;
1128     struct ssv6xxx_hw_sec *sramKey = &(vif_info->sramKey);
1129     int index = *(u8 *)param;
1130     if (wsid == (-1))
1131         return;
1132     BUG_ON(index == 0);
1133     sramKey->sta_key[wsid].group_key_idx = index;
1134 #ifdef SSV6200_ECO
1135     sec_key_tbl += (0x10000 * wsid);
1136     address = sec_key_tbl
1137               + ((index-1) * sizeof(struct ssv6xxx_hw_key));
1138     if (vif_info->vif_priv != NULL)
1139         dev_info(sc->dev, "Write group key %d to VIF %d to %08X\n",
1140                  index, vif_info->vif_priv->vif_idx, address);
1141     else
1142         dev_err(sc->dev, "NULL VIF.\n");
1143     pointer = (int *)&sramKey->group_key[index-1];
1144     for (j = 0; j < (sizeof(struct ssv6xxx_hw_key)/4); j++)
1145         SMAC_REG_WRITE(sc->sh, address+(j*4), *(pointer++));
1146 #endif
1147     address = sec_key_tbl
1148               + (3*sizeof(struct ssv6xxx_hw_key))
1149               + (wsid * sizeof(struct ssv6xxx_hw_sta_key));
1150     pointer = (int *)&sramKey->sta_key[wsid];
1151     SMAC_REG_WRITE(sc->sh, address, *(pointer));
1152 #ifdef FW_WSID_WATCH_LIST
1153     if (wsid >= SSV_NUM_HW_STA)
1154     {
1155         hw_update_watch_wsid(sc, sta_info->sta, sta_info,
1156             wsid, SSV6XXX_WSID_SEC_GROUP, SSV6XXX_WSID_OPS_ENABLE_CAPS);
1157     }
1158 #endif
1159 }
_write_pairwise_key_to_hw(struct ssv_softc * sc,int index,u8 algorithm,const u8 * key,int key_len,struct ieee80211_key_conf * keyconf,struct ssv_vif_priv_data * vif_priv,struct ssv_sta_priv_data * sta_priv)1160 static int _write_pairwise_key_to_hw (struct ssv_softc *sc,
1161                                       int index, u8 algorithm,
1162                                       const u8 *key, int key_len,
1163                                       struct ieee80211_key_conf *keyconf,
1164                                       struct ssv_vif_priv_data *vif_priv,
1165                                       struct ssv_sta_priv_data *sta_priv)
1166 {
1167     int i;
1168     struct ssv6xxx_hw_sec *sramKey;
1169     int address = 0;
1170     int *pointer = NULL;
1171 #ifdef SSV6200_ECO
1172     u32 sec_key_tbl_base = sc->sh->hw_sec_key[0];
1173 #else
1174     u32 sec_key_tbl_base = sc->sh->hw_sec_key;
1175 #endif
1176     u32 sec_key_tbl;
1177     int wsid = (-1);
1178     if (sta_priv == NULL)
1179     {
1180         dev_err(sc->dev, "Set pair-wise key with NULL STA.\n");
1181         return -EOPNOTSUPP;
1182     }
1183     wsid = sta_priv->sta_info->hw_wsid;
1184     if ((wsid < 0) || (wsid >= SSV_NUM_STA))
1185     {
1186         dev_err(sc->dev, "Set pair-wise key to invalid WSID %d.\n", wsid);
1187         return -EOPNOTSUPP;
1188     }
1189     #if 0
1190     sta_info->s_flags |= STA_FLAG_ENCRYPT;
1191     #endif
1192     dev_info(sc->dev, "Set STA %d's pair-wise key of %d bytes.\n", wsid, key_len);
1193     sramKey = &(sc->vif_info[vif_priv->vif_idx].sramKey);
1194     sramKey->sta_key[wsid].pair_key_idx = 0;
1195     sramKey->sta_key[wsid].group_key_idx = vif_priv->group_key_idx;
1196     memcpy(sramKey->sta_key[wsid].pair.key, key, key_len);
1197     sec_key_tbl = sec_key_tbl_base;
1198     #ifdef SSV6200_ECO
1199     sec_key_tbl += (0x10000 * wsid);
1200     #endif
1201     address = sec_key_tbl
1202               + (3 * sizeof(struct ssv6xxx_hw_key))
1203               + wsid * sizeof(struct ssv6xxx_hw_sta_key);
1204     pointer = (int *)&sramKey->sta_key[wsid];
1205     for (i = 0; i < (sizeof(struct ssv6xxx_hw_sta_key)/4); i++)
1206         SMAC_REG_WRITE(sc->sh, (address + (i*4)), *(pointer++));
1207 #ifdef FW_WSID_WATCH_LIST
1208     if (wsid >= SSV_NUM_HW_STA)
1209     {
1210         hw_update_watch_wsid(sc, sta_priv->sta_info->sta, sta_priv->sta_info,
1211             sta_priv->sta_idx, SSV6XXX_WSID_SEC_PAIRWISE, SSV6XXX_WSID_OPS_ENABLE_CAPS);
1212     }
1213 #endif
1214     return 0;
1215 }
_write_group_key_to_hw(struct ssv_softc * sc,int index,u8 algorithm,const u8 * key,int key_len,struct ieee80211_key_conf * keyconf,struct ssv_vif_priv_data * vif_priv,struct ssv_sta_priv_data * sta_priv)1216 static int _write_group_key_to_hw (struct ssv_softc *sc,
1217                                    int index, u8 algorithm,
1218                                    const u8 *key, int key_len,
1219                                    struct ieee80211_key_conf *keyconf,
1220                                    struct ssv_vif_priv_data *vif_priv,
1221                                    struct ssv_sta_priv_data *sta_priv)
1222 {
1223     struct ssv6xxx_hw_sec *sramKey;
1224 #ifndef SSV6200_ECO
1225     u32 sec_key_tbl_base = sc->sh->hw_sec_key;
1226     int address = 0;
1227     int *pointer = NULL;
1228     int i;
1229 #endif
1230     int wsid = sta_priv ? sta_priv->sta_info->hw_wsid : (-1);
1231     int ret = 0;
1232     if (vif_priv == NULL)
1233     {
1234         dev_err(sc->dev, "Setting group key to NULL VIF\n");
1235         return -EOPNOTSUPP;
1236     }
1237     dev_info(sc->dev, "Setting VIF %d group key %d of length %d to WSID %d.\n",
1238              vif_priv->vif_idx, index, key_len, wsid);
1239     sramKey = &(sc->vif_info[vif_priv->vif_idx].sramKey);
1240     vif_priv->group_key_idx = index;
1241     if (sta_priv)
1242         sta_priv->group_key_idx = index;
1243     memcpy(sramKey->group_key[index-1].key, key, key_len);
1244     #ifndef SSV6200_ECO
1245     address = sec_key_tbl_base + ((index-1)*sizeof(struct ssv6xxx_hw_key));
1246     pointer = (int *)&sramKey->group_key[index-1];
1247     for (i = 0; i < (sizeof(struct ssv6xxx_hw_key)/4); i++)
1248          SMAC_REG_WRITE(sc->sh, address+(i*4), *(pointer++));
1249     #endif
1250     WARN_ON(sc->vif_info[vif_priv->vif_idx].vif_priv == NULL);
1251     ssv6xxx_foreach_vif_sta(sc, &sc->vif_info[vif_priv->vif_idx],
1252                             _set_aes_tkip_hw_crypto_group_key, &index);
1253     ret = 0;
1254     return ret;
1255 }
1256 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,0,0)
_prepare_key(struct ieee80211_key_conf * key)1257 static enum SSV_CIPHER_E _prepare_key (struct ieee80211_key_conf *key)
1258 {
1259     enum SSV_CIPHER_E cipher;
1260     switch (key->cipher) {
1261         case WLAN_CIPHER_SUITE_WEP40:
1262             cipher = SSV_CIPHER_WEP40;
1263             break;
1264         case WLAN_CIPHER_SUITE_WEP104:
1265             cipher = SSV_CIPHER_WEP104;
1266             break;
1267         case WLAN_CIPHER_SUITE_TKIP:
1268             key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
1269             cipher = SSV_CIPHER_TKIP;
1270             break;
1271         case WLAN_CIPHER_SUITE_CCMP:
1272 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,7,0)
1273             key->flags |= IEEE80211_KEY_FLAG_SW_MGMT;
1274 #else
1275             key->flags |= (IEEE80211_KEY_FLAG_SW_MGMT_TX | IEEE80211_KEY_FLAG_RX_MGMT);
1276 #endif
1277             cipher = SSV_CIPHER_CCMP;
1278             break;
1279 #ifdef CONFIG_SSV_WAPI
1280         case WLAN_CIPHER_SUITE_SMS4:
1281             printk("[I] %s, algorithm = WLAN_CIPHER_SUITE_SMS4\n", __func__);
1282             cipher = SSV_CIPHER_SMS4;
1283             break;
1284 #endif
1285         default:
1286             cipher = SSV_CIPHER_INVALID;
1287             break;
1288     }
1289     return cipher;
1290 }
1291 #else
_prepare_key(struct ieee80211_key_conf * key)1292 static enum SSV_CIPHER_E _prepare_key (struct ieee80211_key_conf *key)
1293 {
1294     enum SSV_CIPHER_E cipher;
1295     switch (key->alg) {
1296         case ALG_WEP:
1297             if(key->keylen == 5)
1298                 cipher = SSV_CIPHER_WEP40;
1299             else
1300                 cipher = SSV_CIPHER_WEP104;
1301             break;
1302         case ALG_TKIP:
1303             cipher = SSV_CIPHER_TKIP;
1304             key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
1305             break;
1306         case ALG_CCMP:
1307             cipher = SSV_CIPHER_CCMP;
1308             key->flags |= IEEE80211_KEY_FLAG_SW_MGMT;
1309             break;
1310         default:
1311             cipher = SSV_CIPHER_INVALID;
1312             break;
1313     }
1314     return cipher;
1315 }
1316 #endif
_set_key_wep(struct ssv_softc * sc,struct ssv_vif_priv_data * vif_priv,struct ssv_sta_priv_data * sta_priv,enum SSV_CIPHER_E cipher,struct ieee80211_key_conf * key)1317 int _set_key_wep (struct ssv_softc *sc, struct ssv_vif_priv_data *vif_priv,
1318                   struct ssv_sta_priv_data *sta_priv, enum SSV_CIPHER_E cipher,
1319                   struct ieee80211_key_conf *key)
1320 {
1321     int ret = 0;
1322     struct ssv_vif_info *vif_info = &sc->vif_info[vif_priv->vif_idx];
1323     struct ssv6xxx_hw_sec *sram_key = &vif_info->sramKey;
1324     sram_key->sta_key[0].pair_key_idx = key->keyidx;
1325     sram_key->sta_key[0].group_key_idx = key->keyidx;
1326     *(u16 *)&sram_key->sta_key[0].reserve[0] = key->keylen;
1327     printk(KERN_ERR "Set WEP %02X %02X %02X %02X %02X %02X %02X %02X... (%d %d)\n",
1328            key->key[0], key->key[1], key->key[2], key->key[3],
1329            key->key[4], key->key[5], key->key[6], key->key[7],
1330            key->keyidx, key->keylen);
1331     if (key->keyidx == 0)
1332     {
1333         memcpy(sram_key->sta_key[0].pair.key, key->key, key->keylen);
1334     }
1335     else
1336     {
1337         memcpy(sram_key->group_key[key->keyidx - 1].key, key->key, key->keylen);
1338     }
1339     #if 1
1340     if (sc->sh->cfg.use_wpa2_only)
1341     {
1342         dev_warn(sc->dev, "Use WPA2 HW security mode only.\n");
1343     }
1344     #endif
1345     if ( (sc->sh->cfg.use_wpa2_only == 0)
1346         && vif_priv->vif_idx == 0)
1347     {
1348         vif_priv->has_hw_decrypt = true;
1349         vif_priv->has_hw_encrypt = true;
1350         vif_priv->need_sw_decrypt = false;
1351         vif_priv->need_sw_encrypt = false;
1352         vif_priv->use_mac80211_decrypt = false;
1353         ssv6200_hw_set_pair_type(sc->sh, cipher);
1354         ssv6200_hw_set_group_type(sc->sh, cipher);
1355         hw_crypto_key_write_wep(sc->hw, key, cipher,
1356                                 &sc->vif_info[vif_priv->vif_idx]);
1357     }
1358     else
1359     #ifdef USE_LOCAL_WEP_CRYPTO
1360     {
1361         INIT_WRITE_CRYPTO_DATA(crypto_data, &vif_priv->crypto_data);
1362         vif_priv->has_hw_decrypt = false;
1363         vif_priv->has_hw_encrypt = false;
1364         START_WRITE_CRYPTO_DATA(crypto_data);
1365         if (crypto_data->ops && crypto_data->priv)
1366         {
1367             crypto_data->ops->deinit(crypto_data->priv);
1368             crypto_data->ops = NULL;
1369             crypto_data->priv = NULL;
1370         }
1371         crypto_data->ops = get_crypto_wep_ops();
1372         crypto_data->priv = NULL;
1373         if (crypto_data->ops)
1374             crypto_data->priv = crypto_data->ops->init(key->keyidx);
1375         if (crypto_data->priv)
1376         {
1377             crypto_data->ops->set_key(key->key, key->keylen, NULL, crypto_data->priv);
1378             dev_err(sc->dev, "[Local Crypto]: VIF gets WEP crypto OK!\n");
1379             dev_err(sc->dev, "[Local Crypto]: Use driver's encrypter.\n");
1380             vif_priv->need_sw_decrypt = true;
1381             vif_priv->need_sw_encrypt = true;
1382             vif_priv->use_mac80211_decrypt = false;
1383         }
1384         else
1385         {
1386             dev_err(sc->dev, "[Local Crypto]: Failed to initialize driver's crypto!\n");
1387             dev_info(sc->dev, "[Local Crypto]: Use MAC80211's encrypter.\n");
1388             vif_priv->need_sw_decrypt = false;
1389             vif_priv->need_sw_encrypt = false;
1390             vif_priv->use_mac80211_decrypt = true;
1391             ret = -EOPNOTSUPP;
1392         }
1393         ssv6xxx_foreach_vif_sta(sc, vif_info, _set_wep_sw_crypto_key, NULL);
1394         END_WRITE_CRYPTO_DATA(crypto_data);
1395     }
1396     #else
1397     {
1398         vif_priv->has_hw_decrypt = false;
1399         vif_priv->has_hw_encrypt = false;
1400         vif_priv->need_sw_decrypt = false;
1401         vif_priv->need_sw_encrypt = false;
1402         vif_priv->use_mac80211_decrypt = true;
1403         ssv6xxx_foreach_vif_sta(sc, vif_info, _set_wep_sw_crypto_key, NULL);
1404         ret = -EOPNOTSUPP;
1405     }
1406     #endif
1407     vif_priv->pair_cipher = vif_priv->group_cipher = cipher;
1408     vif_priv->is_security_valid = true;
1409     return ret;
1410 }
_set_pairwise_key_tkip_ccmp(struct ssv_softc * sc,struct ssv_vif_priv_data * vif_priv,struct ssv_sta_priv_data * sta_priv,enum SSV_CIPHER_E cipher,struct ieee80211_key_conf * key)1411 static int _set_pairwise_key_tkip_ccmp (struct ssv_softc *sc, struct ssv_vif_priv_data *vif_priv,
1412                                         struct ssv_sta_priv_data *sta_priv, enum SSV_CIPHER_E cipher,
1413                                         struct ieee80211_key_conf *key)
1414 {
1415     int ret = 0;
1416     const char *cipher_name = (cipher == SSV_CIPHER_CCMP) ? "CCMP" : "TKIP";
1417     struct ssv_vif_info *vif_info = &sc->vif_info[vif_priv->vif_idx];
1418     bool tdls_link = false, tdls_use_sw_cipher = false, tkip_use_sw_cipher = false;
1419     bool use_non_ccmp = false;
1420     int another_vif_idx = ((vif_priv->vif_idx + 1) % 2);
1421     struct ssv_vif_priv_data *another_vif_priv =
1422                                 (struct ssv_vif_priv_data *)sc->vif_info[another_vif_idx].vif_priv;
1423     if (sta_priv == NULL)
1424     {
1425         dev_err(sc->dev, "Setting pairwise TKIP/CCMP key to NULL STA.\n");
1426         return -EOPNOTSUPP;
1427     }
1428     #if 1
1429     if (sc->sh->cfg.use_wpa2_only)
1430     {
1431         dev_warn(sc->dev, "Use WPA2 HW security mode only.\n");
1432     }
1433     #endif
1434     if (vif_info->if_type == NL80211_IFTYPE_STATION){
1435         struct ssv_sta_priv_data *first_sta_priv =
1436                 list_first_entry(&vif_priv->sta_list, struct ssv_sta_priv_data, list);
1437         if (first_sta_priv->sta_idx != sta_priv->sta_idx){
1438             tdls_link = true;
1439         }
1440         printk("first sta idx %d, current sta idx %d\n",first_sta_priv->sta_idx,sta_priv->sta_idx);
1441     }
1442     if ((tdls_link) && (vif_priv->pair_cipher != SSV_CIPHER_CCMP)
1443         && (sc->sh->cfg.use_wpa2_only == false)){
1444         tdls_use_sw_cipher = true;
1445     }
1446     if (another_vif_priv != NULL){
1447         if ((another_vif_priv->pair_cipher != SSV_CIPHER_CCMP)
1448               && (another_vif_priv->pair_cipher != SSV_CIPHER_NONE)){
1449             use_non_ccmp = true;
1450             printk("another vif use none ccmp\n");
1451         }
1452     }
1453     if ((((tdls_link) && (vif_priv->pair_cipher != SSV_CIPHER_CCMP)) || (use_non_ccmp))
1454         && (sc->sh->cfg.use_wpa2_only == 1) && (cipher == SSV_CIPHER_CCMP)){
1455         u32 val;
1456         SMAC_REG_READ(sc->sh, ADR_RX_FLOW_DATA, &val);
1457         if (((val >>4) & 0xF) != M_ENG_CPU){
1458             SMAC_REG_WRITE(sc->sh, ADR_RX_FLOW_DATA, ((val & 0xf) | (M_ENG_CPU<<4)
1459                 | (val & 0xfffffff0) <<4));
1460             dev_info(sc->dev, "orginal Rx_Flow %x , modified flow %x \n", val,
1461             ((val & 0xf) | (M_ENG_CPU<<4) | (val & 0xfffffff0) <<4));
1462         }
1463     }
1464     if ((cipher == SSV_CIPHER_TKIP) && (sc->sh->cfg.use_wpa2_only == 1)){
1465       tkip_use_sw_cipher = true;
1466     }
1467     if(tkip_use_sw_cipher == true)
1468         printk ("%s==> tkip use sw cipher\n",__func__);
1469     if ((((vif_priv->vif_idx == 0) && (tdls_use_sw_cipher == false) && (tkip_use_sw_cipher == false)))
1470         || ((cipher == SSV_CIPHER_CCMP) && (sc->sh->cfg.use_wpa2_only == 1)))
1471     {
1472         sta_priv->has_hw_decrypt = true;
1473         sta_priv->need_sw_decrypt = false;
1474         if ((cipher == SSV_CIPHER_TKIP)
1475             || ((!(sc->sh->cfg.hw_caps & SSV6200_HW_CAP_AMPDU_TX) ||
1476                 (sta_priv->sta_info->sta->ht_cap.ht_supported == false))
1477                 && (vif_priv->force_sw_encrypt == false)))
1478         {
1479             dev_info(sc->dev, "STA %d uses HW encrypter for pairwise.\n", sta_priv->sta_idx);
1480             sta_priv->has_hw_encrypt = true;
1481             sta_priv->need_sw_encrypt = false;
1482             sta_priv->use_mac80211_decrypt = false;
1483             ret = 0;
1484         }
1485         else
1486         {
1487             sta_priv->has_hw_encrypt = false;
1488             #ifdef USE_LOCAL_CCMP_CRYPTO
1489             sta_priv->need_sw_encrypt = true;
1490             sta_priv->use_mac80211_decrypt = false;
1491             ret = 0;
1492             #else
1493             sta_priv->need_sw_encrypt = false;
1494             sta_priv->use_mac80211_decrypt = true;
1495             ret = -EOPNOTSUPP;
1496             #endif
1497         }
1498     }
1499     else
1500     {
1501         sta_priv->has_hw_encrypt = false;
1502         sta_priv->has_hw_decrypt = false;
1503         #ifdef USE_LOCAL_CCMP_CRYPTO
1504         sta_priv->need_sw_encrypt = true;
1505         sta_priv->need_sw_decrypt = true;
1506         sta_priv->use_mac80211_decrypt = false;
1507         ret = 0;
1508         #else
1509         dev_err(sc->dev, "STA %d MAC80211's %s cipher.\n", sta_priv->sta_idx, cipher_name);
1510         sta_priv->need_sw_encrypt = false;
1511         sta_priv->need_sw_decrypt = false;
1512         sta_priv->use_mac80211_decrypt = true;
1513         ret = -EOPNOTSUPP;
1514         #endif
1515     }
1516     #ifdef USE_LOCAL_CRYPTO
1517     if (sta_priv->need_sw_encrypt || sta_priv->need_sw_decrypt)
1518     {
1519         struct ssv_crypto_ops *temp_crypt;
1520         void *temp_crypt_priv = NULL;
1521         INIT_WRITE_CRYPTO_DATA(crypto_data, &sta_priv->crypto_data);
1522         START_WRITE_CRYPTO_DATA(crypto_data);
1523         if (crypto_data->ops && crypto_data->priv)
1524         {
1525             crypto_data->ops->deinit(crypto_data->priv);
1526         }
1527         temp_crypt = (cipher == SSV_CIPHER_CCMP)
1528                           #ifdef USE_LOCAL_CCMP_CRYPTO
1529                           ? get_crypto_ccmp_ops()
1530                           #else
1531                           ? NULL
1532                           #endif
1533                           #ifdef USE_LOCAL_TKIP_CRYPTO
1534                           : get_crypto_tkip_ops();
1535                           #else
1536                           : NULL;
1537                           #endif
1538         if (temp_crypt)
1539             temp_crypt_priv = temp_crypt->init(key->keyidx);
1540         if (temp_crypt_priv)
1541         {
1542             dev_err(sc->dev, "Use driver's %s cipher OK!\n", cipher_name);
1543             temp_crypt->set_key(key->key, key->keylen, NULL, temp_crypt_priv);
1544             crypto_data->priv = temp_crypt_priv;
1545             crypto_data->ops = temp_crypt;
1546         }
1547         else
1548         {
1549             dev_err(sc->dev, "Failed to initialize driver's %s crypto! "
1550                     "Use MAC80211's instead.\n", cipher_name);
1551             sta_priv->need_sw_encrypt = false;
1552             sta_priv->need_sw_decrypt = false;
1553             sta_priv->use_mac80211_decrypt = true;
1554             vif_priv->need_sw_encrypt = false;
1555             vif_priv->need_sw_decrypt = false;
1556             vif_priv->use_mac80211_decrypt = true;
1557             ret = -EOPNOTSUPP;
1558         }
1559         END_WRITE_CRYPTO_DATA(crypto_data);
1560     }
1561     #endif
1562     if (sta_priv->has_hw_encrypt || sta_priv->has_hw_decrypt)
1563     {
1564         ssv6200_hw_set_pair_type(sc->sh, cipher);
1565 #if 0
1566         ssv6200_hw_set_pair_type(sc->sh, SSV_CIPHER_NONE);
1567         sta_priv->has_hw_encrypt = false;
1568         sta_priv->has_hw_decrypt = false;
1569         sta_priv->need_sw_encrypt = true;
1570         sta_priv->need_sw_encrypt = true;
1571 #endif
1572         _write_pairwise_key_to_hw(sc, key->keyidx, cipher,
1573                                   key->key, key->keylen, key,
1574                                   vif_priv, sta_priv);
1575     }
1576     if ( (vif_priv->has_hw_encrypt || vif_priv->has_hw_decrypt)
1577         && (vif_priv->group_key_idx > 0))
1578     {
1579         _set_aes_tkip_hw_crypto_group_key(sc, &sc->vif_info[vif_priv->vif_idx],
1580                                           sta_priv->sta_info, &vif_priv->group_key_idx);
1581     }
1582     return ret;
1583 }
_set_group_key_tkip_ccmp(struct ssv_softc * sc,struct ssv_vif_priv_data * vif_priv,struct ssv_sta_priv_data * sta_priv,enum SSV_CIPHER_E cipher,struct ieee80211_key_conf * key)1584 static int _set_group_key_tkip_ccmp (struct ssv_softc *sc, struct ssv_vif_priv_data *vif_priv,
1585                                      struct ssv_sta_priv_data *sta_priv, enum SSV_CIPHER_E cipher,
1586                                      struct ieee80211_key_conf *key)
1587 {
1588     int ret = 0;
1589     const char *cipher_name = (cipher == SSV_CIPHER_CCMP) ? "CCMP" : "TKIP";
1590     bool tkip_use_sw_cipher = false;
1591     vif_priv->group_cipher = cipher;
1592     #if 1
1593     if (sc->sh->cfg.use_wpa2_only)
1594     {
1595         dev_warn(sc->dev, "Use WPA2 HW security mode only.\n");
1596     }
1597     #endif
1598     if ((cipher == SSV_CIPHER_TKIP) && (sc->sh->cfg.use_wpa2_only == 1)){
1599       tkip_use_sw_cipher = true;
1600     }
1601     if (((vif_priv->vif_idx == 0) && (tkip_use_sw_cipher == false))
1602         || ((cipher == SSV_CIPHER_CCMP) && (sc->sh->cfg.use_wpa2_only == 1)))
1603     {
1604         dev_info(sc->dev, "VIF %d uses HW %s cipher for group.\n", vif_priv->vif_idx, cipher_name);
1605         #ifdef USE_MAC80211_DECRYPT_BROADCAST
1606         vif_priv->has_hw_decrypt = false;
1607         ret = -EOPNOTSUPP;
1608         #else
1609         vif_priv->has_hw_decrypt = true;
1610         #endif
1611         vif_priv->has_hw_encrypt = true;
1612         vif_priv->need_sw_decrypt = false;
1613         vif_priv->need_sw_encrypt = false;
1614         vif_priv->use_mac80211_decrypt = false;
1615     }
1616     else
1617     {
1618         vif_priv->has_hw_decrypt = false;
1619         vif_priv->has_hw_encrypt = false;
1620         #ifdef USE_LOCAL_CRYPTO
1621         vif_priv->need_sw_encrypt = true;
1622         vif_priv->need_sw_decrypt = true;
1623         vif_priv->use_mac80211_decrypt = false;
1624         ret = 0;
1625         #else
1626         dev_err(sc->dev, "VIF %d uses MAC80211's %s cipher.\n", vif_priv->vif_idx, cipher_name);
1627         vif_priv->need_sw_encrypt = false;
1628         vif_priv->need_sw_encrypt = false;
1629         vif_priv->use_mac80211_decrypt = true;
1630         ret = -EOPNOTSUPP;
1631         #endif
1632     }
1633     #ifdef USE_LOCAL_CRYPTO
1634     if (vif_priv->need_sw_encrypt || vif_priv->need_sw_decrypt)
1635     {
1636         struct ssv_crypto_ops *temp_crypt = NULL;
1637         void *temp_crypt_priv = NULL;
1638         INIT_WRITE_CRYPTO_DATA(crypto_data, &vif_priv->crypto_data);
1639         START_WRITE_CRYPTO_DATA(crypto_data);
1640         if (crypto_data->ops && crypto_data->priv)
1641             crypto_data->ops->deinit(crypto_data->priv);
1642         crypto_data->priv = NULL;
1643         temp_crypt = (cipher == SSV_CIPHER_CCMP)
1644                           #ifdef USE_LOCAL_CCMP_CRYPTO
1645                           ? get_crypto_ccmp_ops()
1646                           #else
1647                           ? NULL
1648                           #endif
1649                           #ifdef USE_LOCAL_TKIP_CRYPTO
1650                           : get_crypto_tkip_ops();
1651                           #else
1652                           : NULL;
1653                           #endif
1654         if (temp_crypt)
1655             temp_crypt_priv = temp_crypt->init(key->keyidx);
1656         if (temp_crypt_priv)
1657         {
1658             dev_err(sc->dev, "VIF %d gets %s crypto OK! Use driver's crypto.\n",
1659                     vif_priv->vif_idx, cipher_name);
1660             temp_crypt->set_key(key->key, key->keylen, NULL, temp_crypt_priv);
1661             crypto_data->priv = temp_crypt_priv;
1662             crypto_data->ops = temp_crypt;
1663         }
1664         else
1665         {
1666             vif_priv->need_sw_encrypt = false;
1667             vif_priv->need_sw_decrypt = false;
1668             vif_priv->use_mac80211_decrypt = true;
1669             dev_err(sc->dev, "VIF %d failed to initialize %s crypto!"
1670                     " Use MAC80211's instead.\n", vif_priv->vif_idx, cipher_name);
1671             ret = -EOPNOTSUPP;
1672         }
1673         END_WRITE_CRYPTO_DATA(crypto_data);
1674     }
1675     #endif
1676     if (vif_priv->has_hw_encrypt || vif_priv->has_hw_decrypt)
1677     {
1678         #ifdef USE_MAC80211_DECRYPT_BROADCAST
1679         ssv6200_hw_set_group_type(sc->sh, ME_NONE);
1680         #else
1681         ssv6200_hw_set_group_type(sc->sh, cipher);
1682         #endif
1683         key->hw_key_idx = key->keyidx;
1684         _write_group_key_to_hw(sc, key->keyidx, cipher,
1685                                key->key, key->keylen, key,
1686                                vif_priv, sta_priv);
1687     }
1688     vif_priv->is_security_valid = true;
1689     {
1690         int another_vif_idx = ((vif_priv->vif_idx + 1) % 2);
1691         struct ssv_vif_priv_data *another_vif_priv =
1692                         (struct ssv_vif_priv_data *)sc->vif_info[another_vif_idx].vif_priv;
1693         if ( another_vif_priv != NULL){
1694             if (((SSV6XXX_USE_SW_DECRYPT(vif_priv)
1695                     && SSV6XXX_USE_HW_DECRYPT (another_vif_priv)))
1696                 || ((SSV6XXX_USE_HW_DECRYPT (vif_priv)
1697                     && (SSV6XXX_USE_SW_DECRYPT(another_vif_priv))))){
1698                 u32 val;
1699                 SMAC_REG_READ(sc->sh, ADR_RX_FLOW_DATA, &val);
1700                 if (((val >>4) & 0xF) != M_ENG_CPU){
1701                     SMAC_REG_WRITE(sc->sh, ADR_RX_FLOW_DATA, ((val & 0xf) | (M_ENG_CPU<<4)
1702                         | (val & 0xfffffff0) <<4));
1703                     dev_info(sc->dev, "orginal Rx_Flow %x , modified flow %x \n", val,
1704                     ((val & 0xf) | (M_ENG_CPU<<4) | (val & 0xfffffff0) <<4));
1705                 } else {
1706                     printk(" doesn't need to change rx flow\n");
1707                 }
1708             }
1709         }
1710     }
1711     return ret;
1712 }
_set_key_tkip_ccmp(struct ssv_softc * sc,struct ssv_vif_priv_data * vif_priv,struct ssv_sta_priv_data * sta_priv,enum SSV_CIPHER_E cipher,struct ieee80211_key_conf * key)1713 static int _set_key_tkip_ccmp (struct ssv_softc *sc, struct ssv_vif_priv_data *vif_priv,
1714                                struct ssv_sta_priv_data *sta_priv, enum SSV_CIPHER_E cipher,
1715                                struct ieee80211_key_conf *key)
1716 {
1717     if (key->keyidx == 0)
1718         return _set_pairwise_key_tkip_ccmp(sc, vif_priv, sta_priv, cipher, key);
1719     else
1720         return _set_group_key_tkip_ccmp(sc, vif_priv, sta_priv, cipher, key);
1721 }
1722 #ifdef USE_LOCAL_SMS4_CRYPTO
_set_pairwise_key_sms4(struct ssv_softc * sc,struct ssv_vif_priv_data * vif_priv,struct ssv_sta_priv_data * sta_priv,enum SSV_CIPHER_E cipher,struct ieee80211_key_conf * key)1723 static int _set_pairwise_key_sms4 (struct ssv_softc *sc, struct ssv_vif_priv_data *vif_priv,
1724                                struct ssv_sta_priv_data *sta_priv, enum SSV_CIPHER_E cipher,
1725                                struct ieee80211_key_conf *key)
1726 {
1727     int ret = 0;
1728     INIT_WRITE_CRYPTO_DATA(crypto_data, NULL);
1729     if (sta_priv == NULL)
1730     {
1731         dev_err(sc->dev, "Setting pairwise SMS4 key to NULL STA.\n");
1732         return -EOPNOTSUPP;
1733     }
1734     crypto_data = &sta_priv->crypto_data;
1735     START_WRITE_CRYPTO_DATA(crypto_data);
1736     sta_priv->has_hw_encrypt = false;
1737     sta_priv->has_hw_decrypt = false;
1738     sta_priv->need_sw_encrypt = true;
1739     sta_priv->need_sw_decrypt = true;
1740     sta_priv->use_mac80211_decrypt = false;
1741     crypto_data->ops = get_crypto_wpi_ops();
1742     if (crypto_data->ops)
1743         crypto_data->priv = crypto_data->ops->init(key->keyidx);
1744     if (crypto_data->priv)
1745     {
1746         dev_err(sc->dev, "Use driver's SMS4 cipher OK!\n");
1747         crypto_data->ops->set_key(key->key, key->keylen, NULL, crypto_data->priv);
1748     }
1749     else
1750     {
1751         dev_err(sc->dev, "Failed to initialize driver's SMS4 crypto!\n");
1752         crypto_data->ops = NULL;
1753         sta_priv->need_sw_encrypt = false;
1754         sta_priv->need_sw_decrypt = false;
1755         ret = -EOPNOTSUPP;
1756     }
1757     END_WRITE_CRYPTO_DATA(crypto_data);
1758     return ret;
1759 }
_set_group_key_sms4(struct ssv_softc * sc,struct ssv_vif_priv_data * vif_priv,struct ssv_sta_priv_data * sta_priv,enum SSV_CIPHER_E cipher,struct ieee80211_key_conf * key)1760 static int _set_group_key_sms4 (struct ssv_softc *sc, struct ssv_vif_priv_data *vif_priv,
1761                                 struct ssv_sta_priv_data *sta_priv, enum SSV_CIPHER_E cipher,
1762                                 struct ieee80211_key_conf *key)
1763 {
1764     int ret = 0;
1765     INIT_WRITE_CRYPTO_DATA(crypto_data, &vif_priv->crypto_data);
1766     vif_priv->has_hw_encrypt = false;
1767     vif_priv->has_hw_decrypt = false;
1768     vif_priv->need_sw_encrypt = true;
1769     vif_priv->need_sw_decrypt = true;
1770     vif_priv->use_mac80211_decrypt = false;
1771     START_WRITE_CRYPTO_DATA(crypto_data);
1772     crypto_data->ops = get_crypto_wpi_ops();
1773     if (crypto_data->ops)
1774         crypto_data->priv = crypto_data->ops->init(key->keyidx);
1775     if (crypto_data->priv)
1776     {
1777         dev_err(sc->dev, "Use driver's SMS4 cipher OK!\n");
1778         crypto_data->ops->set_key(key->key, key->keylen, NULL, crypto_data->priv);
1779         vif_priv->is_security_valid = true;
1780     }
1781     else
1782     {
1783         dev_err(sc->dev, "Failed to initialize driver's SMS4 crypto!\n");
1784         crypto_data->ops = NULL;
1785         vif_priv->need_sw_encrypt = false;
1786         vif_priv->need_sw_decrypt = false;
1787         ret = -EOPNOTSUPP;
1788         vif_priv->is_security_valid = false;
1789     }
1790     END_WRITE_CRYPTO_DATA(crypto_data);
1791     return ret;
1792 }
_set_key_sms4(struct ssv_softc * sc,struct ssv_vif_priv_data * vif_priv,struct ssv_sta_priv_data * sta_priv,enum SSV_CIPHER_E cipher,struct ieee80211_key_conf * key)1793 static int _set_key_sms4 (struct ssv_softc *sc, struct ssv_vif_priv_data *vif_priv,
1794                           struct ssv_sta_priv_data *sta_priv, enum SSV_CIPHER_E cipher,
1795                           struct ieee80211_key_conf *key)
1796 {
1797  if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
1798         return _set_pairwise_key_sms4(sc, vif_priv, sta_priv, cipher, key);
1799     else
1800         return _set_group_key_sms4(sc, vif_priv, sta_priv, cipher, key);
1801 }
1802 #endif
ssv6200_set_key(struct ieee80211_hw * hw,enum set_key_cmd cmd,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct ieee80211_key_conf * key)1803 static int ssv6200_set_key(struct ieee80211_hw *hw,
1804                            enum set_key_cmd cmd,
1805                            struct ieee80211_vif *vif,
1806                            struct ieee80211_sta *sta,
1807                            struct ieee80211_key_conf *key)
1808 {
1809     struct ssv_softc *sc = hw->priv;
1810     int ret = 0;
1811     enum SSV_CIPHER_E cipher = SSV_CIPHER_NONE;
1812     int sta_idx = (-1);
1813     struct ssv_sta_info *sta_info = NULL;
1814     struct ssv_sta_priv_data *sta_priv = NULL;
1815     struct ssv_vif_priv_data *vif_priv = (struct ssv_vif_priv_data *)vif->drv_priv;
1816     struct ssv_vif_info *vif_info = &sc->vif_info[vif_priv->vif_idx];
1817 #if 0
1818     int another_vif_idx = ((vif_priv->vif_idx + 1) % 2);
1819     struct ssv_vif_priv_data *another_vif_priv = NULL;
1820     u32 another_vif_pair_cipher = 0;
1821     u32 another_vif_group_cipher = 0;
1822     if (sc->vif_info[another_vif_idx].vif)
1823     {
1824         another_vif_priv = sc->vif_info[another_vif_idx].vif_priv;
1825         another_vif_pair_cipher = another_vif_priv->pair_cipher;
1826         another_vif_group_cipher = another_vif_priv->group_cipher;
1827     }
1828 #endif
1829     if (sta)
1830     {
1831         sta_priv = (struct ssv_sta_priv_data *)sta->drv_priv;
1832         sta_idx = sta_priv->sta_idx;
1833         sta_info = sta_priv->sta_info;
1834     }
1835     BUG_ON((cmd!=SET_KEY) && (cmd!=DISABLE_KEY));
1836     if (!(sc->sh->cfg.hw_caps & SSV6200_HW_CAP_SECURITY))
1837     {
1838         dev_warn(sc->dev, "HW does not support security.\n");
1839         return -EOPNOTSUPP;
1840     }
1841     #ifndef USE_LOCAL_CRYPTO
1842     if (sta_info && (sta_info->hw_wsid == (-1)))
1843     {
1844         dev_warn(sc->dev, "Add STA without HW resource. Use MAC80211's solution.\n");
1845         return -EOPNOTSUPP;
1846     }
1847     #endif
1848     cipher = _prepare_key(key);
1849     dev_err(sc->dev,"Set key VIF %d VIF type %d STA %d algorithm = %d, key->keyidx = %d, cmd = %d\n",
1850             vif_priv->vif_idx, vif->type, sta_idx, cipher, key->keyidx, cmd);
1851     if (cipher == SSV_CIPHER_INVALID)
1852     {
1853         dev_warn(sc->dev, "Unsupported cipher type.\n");
1854         return -EOPNOTSUPP;
1855     }
1856     mutex_lock(&sc->mutex);
1857     switch (cmd)
1858     {
1859         case SET_KEY:
1860             {
1861                 #if 0
1862                 int i;
1863                 printk("================================SET KEY=======================================\n");
1864                 if (sta_info == NULL)
1865                 {
1866                     printk("NULL STA cmd[%d] alg[%d] keyidx[%d] ", cmd, algorithm, key->keyidx);
1867                 }
1868                 else
1869                 {
1870                     printk("STA WSID[%d] cmd[%d] alg[%d] keyidx[%d] ", sta_info->hw_wsid, cmd, algorithm, key->keyidx);
1871                 }
1872                 printk("SET_KEY index[%d] flags[0x%x] algorithm[%d] key->keylen[%d]\n",
1873                                                                             key->keyidx, key->flags, algorithm, key->keylen);
1874                 for(i = 0; i < key->keylen; i++)
1875                 {
1876                     printk("[%02x]", key->key[i]);
1877                 }
1878                 printk("\n");
1879                 printk("===============================================================================\n");
1880                 #endif
1881                 switch (cipher)
1882                 {
1883                     case SSV_CIPHER_WEP40:
1884                     case SSV_CIPHER_WEP104:
1885                         ret = _set_key_wep(sc, vif_priv, sta_priv, cipher, key);
1886                         break;
1887                     case SSV_CIPHER_TKIP:
1888                     case SSV_CIPHER_CCMP:
1889                         ret = _set_key_tkip_ccmp(sc, vif_priv, sta_priv, cipher, key);
1890                         break;
1891                     #ifdef CONFIG_SSV_WAPI
1892                     case SSV_CIPHER_SMS4:
1893                         ret = _set_key_sms4(sc, vif_priv, sta_priv, cipher, key);
1894                         break;
1895                     #endif
1896                     default:
1897                         break;
1898                 }
1899                 if (sta){
1900                     struct ssv_sta_priv_data *first_sta_priv =
1901                         list_first_entry(&vif_priv->sta_list, struct ssv_sta_priv_data, list);
1902                     if (first_sta_priv->sta_idx == sta_priv->sta_idx){
1903                       vif_priv->pair_cipher = cipher;
1904                     }
1905                     if (SSV6200_USE_HW_WSID(sta_idx)){
1906                         if (SSV6XXX_USE_SW_DECRYPT(sta_priv)){
1907                             u32 cipher_setting;
1908                             cipher_setting = ssv6200_hw_get_pair_type(sc->sh);
1909                             if (cipher_setting != ME_NONE) {
1910                                 u32 val;
1911                                 SMAC_REG_READ(sc->sh, ADR_RX_FLOW_DATA, &val);
1912                                 if (((val >>4) & 0xF) != M_ENG_CPU){
1913                                     SMAC_REG_WRITE(sc->sh, ADR_RX_FLOW_DATA,
1914                                         ((val & 0xf) | (M_ENG_CPU<<4)
1915                                          | (val & 0xfffffff0) <<4));
1916                                     dev_info(sc->dev, "orginal Rx_Flow %x , modified flow %x \n",
1917                                         val, ((val & 0xf) | (M_ENG_CPU<<4) | (val & 0xfffffff0) <<4));
1918                                 } else {
1919                                     printk(" doesn't need to change rx flow\n");
1920                                 }
1921                             }
1922                         }
1923                         if (sta_priv->has_hw_decrypt){
1924                             hw_update_watch_wsid(sc, sta, sta_info, sta_idx,
1925                                 SSV6XXX_WSID_SEC_HW, SSV6XXX_WSID_OPS_HWWSID_PAIRWISE_SET_TYPE);
1926                             printk("set hw wsid %d cipher mode to HW cipher for pairwise key\n", sta_idx);
1927                         }
1928                     }
1929                 } else {
1930                     if (vif_info->if_type == NL80211_IFTYPE_STATION){
1931                         struct ssv_sta_priv_data *first_sta_priv =
1932                             list_first_entry(&vif_priv->sta_list, struct ssv_sta_priv_data, list);
1933                         if (SSV6200_USE_HW_WSID(first_sta_priv->sta_idx)){
1934                             if (vif_priv->has_hw_decrypt){
1935                                 hw_update_watch_wsid(sc, sta, sta_info, first_sta_priv->sta_idx,
1936                                     SSV6XXX_WSID_SEC_HW, SSV6XXX_WSID_OPS_HWWSID_GROUP_SET_TYPE);
1937                                 printk("set hw wsid %d cipher mode to HW cipher for group  key\n", first_sta_priv->sta_idx);
1938                             }
1939                         }
1940                     }
1941                 }
1942             }
1943             break;
1944         case DISABLE_KEY:
1945             {
1946                 int another_vif_idx = ((vif_priv->vif_idx + 1) % 2);
1947                 struct ssv_vif_priv_data *another_vif_priv =
1948                         (struct ssv_vif_priv_data *)sc->vif_info[another_vif_idx].vif_priv;
1949 #if 0
1950                printk("================================DEL KEY=======================================\n");
1951                if(sta_info == NULL){
1952                     printk("NULL STA cmd[%d] alg[%d] keyidx[%d] ", cmd, cipher, key->keyidx);
1953                }
1954                else{
1955                     printk("STA WSID[%d] cmd[%d] alg[%d] keyidx[%d] ", sta_info->hw_wsid, cmd, cipher, key->keyidx);
1956                }
1957                 printk("DISABLE_KEY index[%d]\n",key->keyidx);
1958                 printk("==============================================================================\n");
1959 #endif
1960                 #if 0
1961                 if(key->keyidx == 0)
1962                 {
1963                     sta_info->ampdu_ccmp_encrypt = false;
1964                 }
1965                 #endif
1966                 if (another_vif_priv != NULL) {
1967                     struct ssv_vif_info *vif_info = &sc->vif_info[vif_priv->vif_idx];
1968                     if (vif_info->if_type != NL80211_IFTYPE_AP) {
1969                         if ((SSV6XXX_USE_SW_DECRYPT(vif_priv)
1970                                 && SSV6XXX_USE_HW_DECRYPT (another_vif_priv))
1971                             || (SSV6XXX_USE_SW_DECRYPT(another_vif_priv)
1972                                 && SSV6XXX_USE_HW_DECRYPT (vif_priv))){
1973     #ifdef CONFIG_SSV_HW_ENCRYPT_SW_DECRYPT
1974                             SMAC_REG_WRITE(sc->sh, ADR_RX_FLOW_DATA,
1975                                     M_ENG_MACRX|(M_ENG_HWHCI<<4));
1976     #else
1977                             SMAC_REG_WRITE(sc->sh, ADR_RX_FLOW_DATA,
1978                                     M_ENG_MACRX|(M_ENG_ENCRYPT_SEC<<4)|(M_ENG_HWHCI<<8));
1979     #endif
1980                             printk("redirect Rx flow for disconnect\n");
1981                         }
1982                     }else {
1983                         if (sta == NULL) {
1984                             if (SSV6XXX_USE_SW_DECRYPT(another_vif_priv)
1985                                 && SSV6XXX_USE_HW_DECRYPT (vif_priv)){
1986     #ifdef CONFIG_SSV_HW_ENCRYPT_SW_DECRYPT
1987                                 SMAC_REG_WRITE(sc->sh, ADR_RX_FLOW_DATA,
1988                                     M_ENG_MACRX|(M_ENG_HWHCI<<4));
1989     #else
1990                                 SMAC_REG_WRITE(sc->sh, ADR_RX_FLOW_DATA,
1991                                     M_ENG_MACRX|(M_ENG_ENCRYPT_SEC<<4)|(M_ENG_HWHCI<<8));
1992     #endif
1993                                 printk("redirect Rx flow for disconnect\n");
1994                             }
1995                         }
1996                     }
1997                 }
1998                 if ( sta == NULL){
1999                     vif_priv->group_cipher = ME_NONE;
2000                     if ((another_vif_priv == NULL)
2001                         || ((another_vif_priv != NULL) && (!SSV6XXX_USE_HW_DECRYPT(another_vif_priv)))){
2002                     #ifdef SSV_SUPPORT_HAL
2003                         HAL_SET_GROUP_CIPHER_TYPE(sc->sh, ME_NONE);
2004                     #else
2005                         ssv6200_hw_set_group_type(sc->sh, ME_NONE);
2006                     #endif
2007                     }
2008                 } else {
2009                     struct ssv_vif_info *vif_info = &sc->vif_info[vif_priv->vif_idx];
2010                     if ((vif_info->if_type != NL80211_IFTYPE_AP) && (another_vif_priv == NULL)){
2011                         struct ssv_sta_priv_data *first_sta_priv =
2012                             list_first_entry(&vif_priv->sta_list, struct ssv_sta_priv_data, list);
2013                         if (sta_priv == first_sta_priv){
2014                         #ifdef SSV_SUPPORT_HAL
2015                             HAL_SET_PAIRWISE_CIPHER_TYPE(sc->sh, ME_NONE, sta_info->hw_wsid);
2016                         #else
2017                             ssv6200_hw_set_pair_type(sc->sh, ME_NONE);
2018                         #endif
2019                         }
2020                     }
2021                     vif_priv->pair_cipher = ME_NONE;
2022                 }
2023                 if ((cipher == ME_TKIP) || (cipher == ME_CCMP))
2024                 {
2025                     printk(KERN_ERR "Clear key %d VIF %d, STA %d\n",
2026                            key->keyidx, (vif != NULL), (sta != NULL));
2027                     hw_crypto_key_clear(hw, key->keyidx, key, vif_priv, sta_priv);
2028                 }
2029                 {
2030                     if ((key->keyidx == 0) && (sta_priv != NULL))
2031                     {
2032                         #ifdef USE_LOCAL_CRYPTO
2033                         unsigned long flags;
2034                         INIT_WRITE_CRYPTO_DATA(crypto_data, &sta_priv->crypto_data);
2035                         #endif
2036                         sta_priv->has_hw_decrypt = false;
2037                         sta_priv->has_hw_encrypt = false;
2038                         sta_priv->need_sw_encrypt = false;
2039                         sta_priv->use_mac80211_decrypt = false;
2040                         #ifdef USE_LOCAL_CRYPTO
2041                         if (crypto_data->ops && crypto_data->priv)
2042                         {
2043                             u32 sta_addr0_3 = *(u32 *)&sta->addr[0];
2044                             u32 sta_addr4_5 = (u32)*(u16 *)&sta->addr[4];
2045                             u32 removed_skb_num;
2046                             START_WRITE_CRYPTO_DATA(crypto_data);
2047                             crypto_data->ops->deinit(crypto_data->priv);
2048                             crypto_data->priv = NULL;
2049                             crypto_data->ops = NULL;
2050                             END_WRITE_CRYPTO_DATA(crypto_data);
2051                             spin_lock_irqsave(&sc->crypt_st_lock, flags);
2052                             removed_skb_num = _remove_sta_skb_from_q(sc, &sc->preprocess_q,
2053                                                                      sta_addr0_3, sta_addr4_5);
2054                             spin_unlock_irqrestore(&sc->crypt_st_lock, flags);
2055                             dev_err(sc->dev, "Clean up %d skb for STA %pM.\n", removed_skb_num, sta->addr);
2056                         }
2057                         #endif
2058                     }
2059                     #ifdef USE_LOCAL_CRYPTO
2060                     else
2061                     {
2062                         INIT_WRITE_CRYPTO_DATA(crypto_data, &vif_priv->crypto_data);
2063                         START_WRITE_CRYPTO_DATA(crypto_data);
2064                         if (crypto_data->ops && crypto_data->priv)
2065                             crypto_data->ops->deinit(crypto_data->priv);
2066                         crypto_data->priv = NULL;
2067                         crypto_data->ops = NULL;
2068                         END_WRITE_CRYPTO_DATA(crypto_data);
2069                     }
2070                     #endif
2071                     if ((vif_priv->is_security_valid) && (key->keyidx != 0))
2072                     {
2073                         #if 0
2074                         vif_priv->has_hw_decrypt = false;
2075                         vif_priv->has_hw_encrypt = false;
2076                         vif_priv->need_sw_encrypt = false;
2077                         #endif
2078                         vif_priv->is_security_valid = false;
2079                     }
2080                 }
2081                 ret = 0;
2082             }
2083             break;
2084         default:
2085             ret = -EINVAL;
2086     }
2087     mutex_unlock(&sc->mutex);
2088     if(sta_priv != NULL)
2089     {
2090         printk("sta: hw_en:%d, sw_en:%d, hw_de:%d, sw_de:%d,\n",
2091             (sta_priv->has_hw_encrypt==true),(sta_priv->need_sw_encrypt==true),
2092             (sta_priv->has_hw_decrypt==true),(sta_priv->need_sw_decrypt==true));
2093     }
2094     if(vif_priv)
2095     {
2096         printk("vif: hw_en:%d, sw_en:%d, hw_de:%d, sw_de:%d, use_mac80211 %d, valid:%d\n",
2097             (vif_priv->has_hw_encrypt==true),(vif_priv->need_sw_encrypt==true),
2098             (vif_priv->has_hw_decrypt==true),(vif_priv->need_sw_decrypt==true),
2099             (vif_priv->use_mac80211_decrypt == true), (vif_priv->is_security_valid==true));
2100     }
2101 #ifdef CONFIG_SSV_SW_ENCRYPT_HW_DECRYPT
2102     ret = -EOPNOTSUPP;
2103 #endif
2104 #ifndef USE_LOCAL_CRYPTO
2105     if ( vif_priv->force_sw_encrypt
2106         || (sta_info && (sta_info->hw_wsid != 1) && (sta_info->hw_wsid != 0)))
2107     {
2108         if (vif_priv->force_sw_encrypt == false)
2109             vif_priv->force_sw_encrypt = true;
2110         ret = -EOPNOTSUPP;
2111     }
2112 #endif
2113     printk(KERN_ERR "SET KEY %d\n", ret);
2114     return ret;
2115 }
_process_tx_done(struct ssv_softc * sc)2116 u32 _process_tx_done (struct ssv_softc *sc)
2117 {
2118     struct ieee80211_tx_info *tx_info;
2119     struct sk_buff *skb;
2120     while ((skb = skb_dequeue(&sc->tx_done_q)))
2121     {
2122         struct ssv6200_tx_desc *tx_desc;
2123         tx_info = IEEE80211_SKB_CB(skb);
2124         tx_desc = (struct ssv6200_tx_desc *)skb->data;
2125         if(tx_desc->c_type > M2_TXREQ)
2126         {
2127             ssv_skb_free(skb);
2128             printk(KERN_INFO "free cmd skb!\n");
2129             continue;
2130         }
2131         if (tx_info->flags & IEEE80211_TX_CTL_AMPDU)
2132         {
2133             ssv6200_ampdu_release_skb(skb, sc->hw);
2134             continue;
2135         }
2136         skb_pull(skb, SSV6XXX_TX_DESC_LEN);
2137         ieee80211_tx_info_clear_status(tx_info);
2138         tx_info->flags |= IEEE80211_TX_STAT_ACK;
2139         tx_info->status.ack_signal = 100;
2140 #ifdef REPORT_TX_DONE_IN_IRQ
2141         ieee80211_tx_status_irqsafe(sc->hw, skb);
2142 #else
2143         ieee80211_tx_status(sc->hw, skb);
2144         if (skb_queue_len(&sc->rx_skb_q))
2145             break;
2146 #endif
2147     }
2148     return skb_queue_len(&sc->tx_done_q);
2149 }
2150 #ifdef REPORT_TX_DONE_IN_IRQ
ssv6xxx_tx_cb(struct sk_buff_head * skb_head,void * args)2151 void ssv6xxx_tx_cb(struct sk_buff_head *skb_head, void *args)
2152 {
2153     struct ssv_softc *sc=(struct ssv_softc *)args;
2154     _process_tx_done*(sc);
2155 }
2156 #else
ssv6xxx_tx_cb(struct sk_buff_head * skb_head,void * args)2157 void ssv6xxx_tx_cb(struct sk_buff_head *skb_head, void *args)
2158 {
2159     struct ssv_softc *sc=(struct ssv_softc *)args;
2160     struct sk_buff *skb;
2161     while ((skb=skb_dequeue(skb_head)))
2162     {
2163         struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
2164         struct ssv6200_tx_desc *tx_desc;
2165         tx_desc = (struct ssv6200_tx_desc *)skb->data;
2166         if(tx_desc->c_type > M2_TXREQ)
2167         {
2168             ssv_skb_free(skb);
2169             printk(KERN_INFO "free cmd skb!\n");
2170             continue;
2171         }
2172         if (tx_info->flags & IEEE80211_TX_CTL_AMPDU)
2173             ssv6xxx_ampdu_sent(sc->hw, skb);
2174         skb_queue_tail(&sc->tx_done_q, skb);
2175     }
2176     wake_up_interruptible(&sc->rx_wait_q);
2177 }
2178 #endif
2179 #ifdef RATE_CONTROL_REALTIME_UPDATA
ssv6xxx_tx_rate_update(struct sk_buff * skb,void * args)2180 void ssv6xxx_tx_rate_update(struct sk_buff *skb, void *args)
2181 {
2182     struct ieee80211_hdr *hdr;
2183     struct ssv_softc *sc = args;
2184     struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2185     struct ssv6200_tx_desc *tx_desc;
2186     struct ssv_rate_info ssv_rate;
2187     u32 nav=0;
2188     int ret = 0;
2189     tx_desc = (struct ssv6200_tx_desc *)skb->data;
2190     if(tx_desc->c_type > M2_TXREQ)
2191         return;
2192     if (!(info->flags & IEEE80211_TX_CTL_AMPDU))
2193     {
2194         hdr = (struct ieee80211_hdr *)(skb->data+SSV6XXX_TX_DESC_LEN);
2195         if ( ( ieee80211_is_data_qos(hdr->frame_control)
2196                 || ieee80211_is_data(hdr->frame_control))
2197             && (tx_desc->wsid < SSV_RC_MAX_HARDWARE_SUPPORT))
2198         {
2199             ret = ssv6xxx_rc_hw_rate_update_check(skb, sc, tx_desc->do_rts_cts);
2200             if (ret & RC_FIRMWARE_REPORT_FLAG)
2201             {
2202                 {
2203                     tx_desc->RSVD_0 = SSV6XXX_RC_REPORT;
2204                     tx_desc->tx_report = 1;
2205                 }
2206                 ret &= 0xf;
2207             }
2208             if(ret)
2209             {
2210                 ssv6xxx_rc_hw_rate_idx(sc, info, &ssv_rate);
2211                 tx_desc->crate_idx = ssv_rate.crate_hw_idx;
2212                 tx_desc->drate_idx = ssv_rate.drate_hw_idx;
2213                 nav = ssv6xxx_set_frame_duration(info, &ssv_rate, skb->len+FCS_LEN, tx_desc, NULL, NULL);
2214                 if (tx_desc->tx_burst == 0)
2215                 {
2216                     if (tx_desc->ack_policy != 0x01)
2217                         hdr->duration_id = nav;
2218                 }
2219             }
2220         }
2221     }
2222     else
2223     {
2224     }
2225     return;
2226 }
2227 #endif
2228 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,10,0)
2229 #define RTS_CTS_PROTECT(_flg) \
2230     ((_flg)&IEEE80211_TX_RC_USE_RTS_CTS)? 1: \
2231     ((_flg)&IEEE80211_TX_RC_USE_CTS_PROTECT)? 2: 0
2232 #endif
ssv6xxx_update_txinfo(struct ssv_softc * sc,struct sk_buff * skb)2233 void ssv6xxx_update_txinfo (struct ssv_softc *sc, struct sk_buff *skb)
2234 {
2235     struct ieee80211_hdr *hdr;
2236     struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2237     struct ieee80211_sta *sta;
2238     struct ssv_sta_info *sta_info = NULL;
2239     struct ssv_sta_priv_data *ssv_sta_priv = NULL;
2240     struct ssv_vif_priv_data *vif_priv = (struct ssv_vif_priv_data *)info->control.vif->drv_priv;
2241     struct ssv6200_tx_desc *tx_desc = (struct ssv6200_tx_desc *)skb->data;
2242     struct ieee80211_tx_rate *tx_drate;
2243     struct ssv_rate_info ssv_rate;
2244     int ac, hw_txqid;
2245     u32 nav=0;
2246     if (info->flags & IEEE80211_TX_CTL_AMPDU)
2247     {
2248         struct ampdu_hdr_st *ampdu_hdr = (struct ampdu_hdr_st *)skb->head;
2249         sta = ampdu_hdr->ampdu_tid->sta;
2250         hdr = (struct ieee80211_hdr *)(skb->data + TXPB_OFFSET + AMPDU_DELIMITER_LEN);
2251     }
2252     else
2253     {
2254         struct SKB_info_st *skb_info = (struct SKB_info_st *)skb->head;
2255         sta = skb_info->sta;
2256         hdr = (struct ieee80211_hdr *)(skb->data + TXPB_OFFSET);
2257     }
2258     if (sta)
2259     {
2260         ssv_sta_priv = (struct ssv_sta_priv_data *)sta->drv_priv;
2261         sta_info = ssv_sta_priv->sta_info;
2262     }
2263     if ((!sc->bq4_dtim) &&
2264         (ieee80211_is_mgmt(hdr->frame_control) ||
2265         ieee80211_is_nullfunc(hdr->frame_control) ||
2266         ieee80211_is_qos_nullfunc(hdr->frame_control))) {
2267         ac = 4;
2268         hw_txqid = 4;
2269     }
2270     else if((sc->bq4_dtim) &&
2271         info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM){
2272         hw_txqid = 4;
2273         ac = 4;
2274     }
2275     else{
2276         ac = skb_get_queue_mapping(skb);
2277         hw_txqid = sc->tx.hw_txqid[ac];
2278     }
2279     tx_drate = &info->control.rates[0];
2280     ssv6xxx_rc_hw_rate_idx(sc, info, &ssv_rate);
2281     tx_desc->len = skb->len;
2282     tx_desc->c_type = M2_TXREQ;
2283     tx_desc->f80211 = 1;
2284     tx_desc->qos = (ieee80211_is_data_qos(hdr->frame_control))? 1: 0;
2285     if (tx_drate->flags & IEEE80211_TX_RC_MCS) {
2286         if (ieee80211_is_mgmt(hdr->frame_control) &&
2287             ieee80211_has_order(hdr->frame_control))
2288             tx_desc->ht = 1;
2289     }
2290     tx_desc->use_4addr = (ieee80211_has_a4(hdr->frame_control))? 1: 0;
2291     tx_desc->more_data = (ieee80211_has_morefrags(hdr->frame_control))? 1: 0;
2292     tx_desc->stype_b5b4 = (cpu_to_le16(hdr->frame_control)>>4)&0x3;
2293     tx_desc->frag = (tx_desc->more_data||(hdr->seq_ctrl&0xf))? 1: 0;
2294     tx_desc->unicast = (is_multicast_ether_addr(hdr->addr1)) ? 0: 1;
2295     tx_desc->tx_burst = (tx_desc->frag)? 1: 0;
2296     tx_desc->wsid = (!sta_info || (sta_info->hw_wsid < 0)) ? 0x0F : sta_info->hw_wsid;
2297     tx_desc->txq_idx = hw_txqid;
2298     tx_desc->hdr_offset = TXPB_OFFSET;
2299     tx_desc->hdr_len = ssv6xxx_frame_hdrlen(hdr, tx_desc->ht);
2300     tx_desc->payload_offset = tx_desc->hdr_offset + tx_desc->hdr_len;
2301 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,10,0)
2302  if(info->control.use_rts)
2303   tx_desc->do_rts_cts = IEEE80211_TX_RC_USE_RTS_CTS;
2304  else if(info->control.use_cts_prot)
2305   tx_desc->do_rts_cts = IEEE80211_TX_RC_USE_CTS_PROTECT;
2306 #else
2307     tx_desc->do_rts_cts = RTS_CTS_PROTECT(tx_drate->flags);
2308 #endif
2309     if(tx_desc->do_rts_cts == IEEE80211_TX_RC_USE_CTS_PROTECT)
2310         tx_desc->do_rts_cts = IEEE80211_TX_RC_USE_RTS_CTS;
2311     if(tx_desc->do_rts_cts == IEEE80211_TX_RC_USE_CTS_PROTECT)
2312     {
2313   tx_desc->crate_idx = 0;
2314     }
2315     else
2316         tx_desc->crate_idx = ssv_rate.crate_hw_idx;
2317     tx_desc->drate_idx = ssv_rate.drate_hw_idx;
2318     if (tx_desc->unicast == 0)
2319         tx_desc->ack_policy = 1;
2320     else if (tx_desc->qos == 1)
2321         tx_desc->ack_policy = (*ieee80211_get_qos_ctl(hdr)&0x60)>>5;
2322     else if(ieee80211_is_ctl(hdr->frame_control))
2323         tx_desc->ack_policy = 1;
2324     tx_desc->security = 0;
2325     tx_desc->fCmdIdx = 0;
2326     tx_desc->fCmd = (hw_txqid+M_ENG_TX_EDCA0);
2327     if (info->flags & IEEE80211_TX_CTL_AMPDU)
2328     {
2329         #ifdef AMPDU_HAS_LEADING_FRAME
2330         tx_desc->fCmd = (tx_desc->fCmd << 4) | M_ENG_CPU;
2331         #else
2332         tx_desc->RSVD_1 = 1;
2333         #endif
2334         tx_desc->aggregation = 1;
2335         tx_desc->ack_policy = 0x01;
2336         if ( (tx_desc->do_rts_cts == 0)
2337             && ( (sc->hw->wiphy->rts_threshold == (-1))
2338                 || ((skb->len - sc->sh->tx_desc_len) > sc->hw->wiphy->rts_threshold)))
2339         {
2340             tx_drate->flags |= IEEE80211_TX_RC_USE_RTS_CTS;
2341             tx_desc->do_rts_cts = 1;
2342         }
2343     }
2344     if ( ieee80211_has_protected(hdr->frame_control)
2345         && ( ieee80211_is_data_qos(hdr->frame_control)
2346             || ieee80211_is_data(hdr->frame_control)))
2347     {
2348         if ( (tx_desc->unicast && ssv_sta_priv && ssv_sta_priv->has_hw_encrypt)
2349             || (!tx_desc->unicast && vif_priv && vif_priv->has_hw_encrypt))
2350         {
2351             if (!tx_desc->unicast && !list_empty(&vif_priv->sta_list))
2352             {
2353                 struct ssv_sta_priv_data *one_sta_priv;
2354                 int hw_wsid;
2355                 one_sta_priv = list_first_entry(&vif_priv->sta_list, struct ssv_sta_priv_data, list);
2356                 hw_wsid = one_sta_priv->sta_info->hw_wsid;
2357                 if (hw_wsid != (-1))
2358                 {
2359                     tx_desc->wsid = hw_wsid;
2360                 }
2361                 #if 0
2362                 printk(KERN_ERR "HW ENC %d %02X:%02X:%02X:%02X:%02X:%02X\n",
2363                        tx_desc->wsid,
2364                        hdr->addr1[0], hdr->addr1[1], hdr->addr1[2],
2365                        hdr->addr1[3], hdr->addr1[4], hdr->addr1[5]);
2366                 _ssv6xxx_hexdump("M ", (const u8 *)skb->data, (skb->len > 128) ? 128 : skb->len);
2367                 tx_desc->fCmd = (tx_desc->fCmd << 4) | M_ENG_CPU;
2368                 #endif
2369             }
2370             tx_desc->fCmd = (tx_desc->fCmd << 4) | M_ENG_ENCRYPT;
2371             #if 0
2372             if (dump_count++ < 10)
2373             {
2374                 printk(KERN_ERR "HW ENC %d %02X:%02X:%02X:%02X:%02X:%02X\n",
2375                        tx_desc->wsid,
2376                        hdr->addr1[0], hdr->addr1[1], hdr->addr1[2],
2377                        hdr->addr1[3], hdr->addr1[4], hdr->addr1[5]);
2378                 tx_desc->tx_report = 1;
2379                 _ssv6xxx_hexdump("M ", (const u8 *)skb->data, (skb->len > 128) ? 128 : skb->len);
2380             }
2381             #endif
2382         }
2383         else if (ssv_sta_priv->need_sw_encrypt)
2384         {
2385         }
2386         else
2387         {
2388         }
2389     }
2390     else
2391     {
2392     }
2393     tx_desc->fCmd = (tx_desc->fCmd << 4) | M_ENG_HWHCI;
2394     #if 0
2395     if ( ieee80211_is_data_qos(hdr->frame_control)
2396         || ieee80211_is_data(hdr->frame_control))
2397     #endif
2398     #if 0
2399     if (ieee80211_is_probe_resp(hdr->frame_control))
2400     {
2401         {
2402             printk(KERN_ERR "Probe Resp %d %02X:%02X:%02X:%02X:%02X:%02X\n",
2403                    tx_desc->wsid,
2404                    hdr->addr1[0], hdr->addr1[1], hdr->addr1[2],
2405                    hdr->addr1[3], hdr->addr1[4], hdr->addr1[5]);
2406             _ssv6xxx_hexdump("M ", (const u8 *)skb->data, (skb->len > 128) ? 128 : skb->len);
2407         }
2408     }
2409     #endif
2410 #if 0
2411     if ( (sc->sh->cfg.hw_caps & SSV6200_HW_CAP_SECURITY)
2412         && (sc->algorithm != ME_NONE)) {
2413         if ( (tx_desc->unicast == 0)
2414             || (sc->algorithm == ME_WEP104 || sc->algorithm == ME_WEP40))
2415         {
2416             tx_desc->wsid = 0;
2417         }
2418     }
2419 #endif
2420 #if 0
2421     if (tx_desc->aggregation) {
2422         tx_desc->do_rts_cts = 0;
2423         tx_desc->fCmd = M_ENG_HWHCI|((hw_txqid+M_ENG_TX_EDCA0)<<4);
2424         tx_desc->ack_policy = 0x01;
2425     }
2426 #endif
2427     if (tx_desc->aggregation == 1)
2428     {
2429         struct ampdu_hdr_st *ampdu_hdr = (struct ampdu_hdr_st *)skb->head;
2430         memcpy(&tx_desc->rc_params[0], ampdu_hdr->rates, sizeof(tx_desc->rc_params));
2431         nav = ssv6xxx_set_frame_duration(info, &ssv_rate, (skb->len+FCS_LEN), tx_desc, &tx_desc->rc_params[0], sc);
2432         #ifdef FW_RC_RETRY_DEBUG
2433         {
2434             printk("[FW_RC]:param[0]: drate =%d, count =%d, crate=%d, dl_length =%d, frame_consume_time =%d, rts_cts_nav=%d\n",
2435                 tx_desc->rc_params[0].drate,tx_desc->rc_params[0].count,tx_desc->rc_params[0].crate,
2436                 tx_desc->rc_params[0].dl_length, tx_desc->rc_params[0].frame_consume_time, tx_desc->rc_params[0].rts_cts_nav);
2437             printk("[FW_RC]:param[1]: drate =%d, count =%d, crate=%d, dl_length =%d, frame_consume_time =%d, rts_cts_nav=%d\n",
2438                 tx_desc->rc_params[1].drate,tx_desc->rc_params[1].count,tx_desc->rc_params[1].crate,
2439                 tx_desc->rc_params[1].dl_length, tx_desc->rc_params[1].frame_consume_time, tx_desc->rc_params[1].rts_cts_nav);
2440             printk("[FW_RC]:param[2]: drate =%d, count =%d, crate=%d, dl_length =%d, frame_consume_time =%d, rts_cts_nav=%d\n",
2441                 tx_desc->rc_params[2].drate,tx_desc->rc_params[2].count,tx_desc->rc_params[2].crate,
2442                 tx_desc->rc_params[2].dl_length, tx_desc->rc_params[2].frame_consume_time, tx_desc->rc_params[2].rts_cts_nav);
2443         }
2444         #endif
2445     }
2446     else
2447     {
2448         nav = ssv6xxx_set_frame_duration(info, &ssv_rate, (skb->len+FCS_LEN), tx_desc, NULL, NULL);
2449     }
2450     if ( (tx_desc->aggregation==0)) {
2451         if (tx_desc->tx_burst == 0) {
2452             if (tx_desc->ack_policy != 0x01)
2453                 hdr->duration_id = nav;
2454         }
2455         else {
2456         }
2457     }
2458 }
ssv6xxx_add_txinfo(struct ssv_softc * sc,struct sk_buff * skb)2459 void ssv6xxx_add_txinfo (struct ssv_softc *sc, struct sk_buff *skb)
2460 {
2461     struct ssv6200_tx_desc *tx_desc;
2462     skb_push(skb, sc->sh->tx_desc_len);
2463     tx_desc = (struct ssv6200_tx_desc *)skb->data;
2464     memset((void *)tx_desc, 0, sc->sh->tx_desc_len);
2465     ssv6xxx_update_txinfo(sc, skb);
2466 }
ssv6xxx_get_real_index(struct ssv_softc * sc,struct sk_buff * skb)2467 int ssv6xxx_get_real_index(struct ssv_softc *sc, struct sk_buff *skb)
2468 {
2469     struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2470     struct ieee80211_tx_rate *tx_drate;
2471     struct ssv_rate_info ssv_rate;
2472     tx_drate = &info->control.rates[0];
2473     ssv6xxx_rc_hw_rate_idx(sc, info, &ssv_rate);
2474     return ssv_rate.drate_hw_idx;
2475 }
_ssv6xxx_tx(struct ieee80211_hw * hw,struct sk_buff * skb)2476 static void _ssv6xxx_tx (struct ieee80211_hw *hw, struct sk_buff *skb)
2477 {
2478     struct ssv_softc *sc = hw->priv;
2479     struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2480     struct ieee80211_vif *vif = info->control.vif;
2481     struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
2482 #ifdef USE_LOCAL_CRYPTO
2483     struct SKB_info_st *skb_info = (struct SKB_info_st *)skb->head;
2484     struct ieee80211_sta *sta = skb_info->sta;
2485     struct ssv_vif_priv_data *vif_priv = (struct ssv_vif_priv_data *)info->control.vif->drv_priv;
2486     struct ssv_sta_priv_data *ssv_sta_priv = sta
2487                                               ? (struct ssv_sta_priv_data *)sta->drv_priv
2488                                              : NULL;
2489 #endif
2490     struct ssv6200_tx_desc *tx_desc;
2491     int ret;
2492     unsigned long flags;
2493     bool send_hci=false;
2494 #ifdef CONFIG_SSV_SUPPORT_ANDROID
2495     if(sc->ps_status == PWRSV_PREPARE)
2496     {
2497         if(ieee80211_is_data(hdr->frame_control))
2498             ssv_wake_timeout(sc, 1);
2499     }
2500 #endif
2501     do {
2502         if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
2503             if (info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT)
2504                 sc->tx.seq_no += 0x10;
2505             hdr->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
2506             hdr->seq_ctrl |= cpu_to_le16(sc->tx.seq_no);
2507         }
2508         if (sc->dbg_tx_frame) {
2509             printk("================================================\n");
2510             _ssv6xxx_hexdump("TX frame", (const u8 *)skb->data, skb->len);
2511         }
2512 #if 0
2513         if ( (skb->protocol == cpu_to_be16(ETH_P_PAE))
2514             && ieee80211_is_data_qos(hdr->frame_control))
2515         {
2516            printk(KERN_ERR "EAPOL frame is %d\n", skb_get_queue_mapping(skb));
2517         }
2518 #endif
2519 #ifdef USE_LOCAL_CRYPTO
2520         if (
2521 #ifdef MULTI_THREAD_ENCRYPT
2522             (skb_info->crypt_st == PKT_CRYPT_ST_NOT_SUPPORT) &&
2523 #endif
2524             ieee80211_has_protected(hdr->frame_control)
2525             && ( ieee80211_is_data_qos(hdr->frame_control)
2526                 || ieee80211_is_data(hdr->frame_control)))
2527         {
2528             bool unicast = !is_broadcast_ether_addr(hdr->addr1);
2529             if ( ( unicast
2530                     && (ssv_sta_priv != NULL)
2531                     && ssv_sta_priv->need_sw_encrypt)
2532                 || ( !unicast
2533                     && vif_priv->is_security_valid
2534                     && vif_priv->need_sw_encrypt))
2535             {
2536                 if(ssv6xxx_skb_encrypt(skb, sc) == (-1))
2537                 {
2538                     ssv_skb_free(skb);
2539                     break;
2540                 }
2541             }
2542         }
2543         else
2544         {
2545         }
2546 #endif
2547         if (info->flags & IEEE80211_TX_CTL_AMPDU)
2548         {
2549             if(ssv6xxx_get_real_index(sc, skb) < SSV62XX_RATE_MCS_INDEX)
2550             {
2551                 info->flags &= (~IEEE80211_TX_CTL_AMPDU);
2552                 goto tx_mpdu;
2553             }
2554             #if 0
2555             u8 tidno;
2556             struct ieee80211_sta *sta = info->control.sta;
2557             struct ssv_sta_priv_data *ssv_sta_priv = (struct ssv_sta_priv_data *)sta->drv_priv;
2558             tidno = ieee80211_get_qos_ctl(hdr)[0] & IEEE80211_QOS_CTL_TID_MASK;
2559             if((ssv_sta_priv->ampdu_tid[tidno].state == AMPDU_STATE_OPERATION) &&
2560                (sc->ampdu_rekey_pause < AMPDU_REKEY_PAUSE_ONGOING))
2561             #endif
2562             if (ssv6200_ampdu_tx_handler(hw, skb))
2563             {
2564                 break;
2565             }
2566             else
2567             {
2568                 info->flags &= (~IEEE80211_TX_CTL_AMPDU);
2569             }
2570         }
2571 tx_mpdu:
2572         ssv6xxx_add_txinfo(sc, skb);
2573         if( vif &&
2574             vif->type == NL80211_IFTYPE_AP &&
2575             (sc->bq4_dtim) &&
2576          info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM )
2577         {
2578             struct ssv_vif_priv_data *priv_vif = (struct ssv_vif_priv_data *)vif->drv_priv;
2579             u8 buffered = 0;
2580             spin_lock_irqsave(&sc->ps_state_lock, flags);
2581             if (priv_vif->sta_asleep_mask)
2582             {
2583                 buffered = ssv6200_bcast_enqueue(sc, &sc->bcast_txq, skb);
2584                 if (1 == buffered) {
2585 #ifdef BCAST_DEBUG
2586                     printk("ssv6200_tx:ssv6200_bcast_start\n");
2587 #endif
2588                     ssv6200_bcast_start(sc);
2589                 }
2590             }
2591             spin_unlock_irqrestore(&sc->ps_state_lock, flags);
2592             if (buffered)
2593                 break;
2594         }
2595         if (info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM) {
2596             struct ssv_vif_priv_data *vif_priv = (struct ssv_vif_priv_data *)vif->drv_priv;
2597             dev_dbg(sc->dev, "vif[%d] sc->bq4_dtim[%d]\n", vif_priv->vif_idx, sc->bq4_dtim);
2598         }
2599         tx_desc = (struct ssv6200_tx_desc *)skb->data;
2600         ret = HCI_SEND(sc->sh, skb, tx_desc->txq_idx);
2601         send_hci = true;
2602     } while (0);
2603 #ifdef ENABLE_TX_Q_FLOW_CONTROL
2604     if ( (skb_queue_len(&sc->tx_skb_q) < LOW_TX_Q_LEN)
2605         #ifdef MULTI_THREAD_ENCRYPT
2606         && (skb_queue_len(&sc->preprocess_q) < LOW_CRYPTO_Q_LEN)
2607         && (skb_queue_len(&sc->crypted_q) < LOW_CRYPTO_Q_LEN)
2608         #endif
2609        )
2610     {
2611        if (sc->tx.flow_ctrl_status != 0)
2612        {
2613            int ac;
2614            for (ac = 0; ac < sc->hw->queues; ac++){
2615                if ((sc->tx.flow_ctrl_status & BIT(ac)) == 0)
2616                    ieee80211_wake_queue(sc->hw, ac);
2617            }
2618        }
2619        else
2620        {
2621            ieee80211_wake_queues(sc->hw);
2622        }
2623     }
2624 #endif
2625     if(sc->dbg_tx_frame){
2626         if(send_hci)
2627             printk("Tx frame send to HCI\n");
2628         else
2629             printk("Tx frame queued\n");
2630         printk("================================================\n");
2631     }
2632 }
2633 #ifdef MULTI_THREAD_ENCRYPT
_is_encrypt_needed(struct sk_buff * skb)2634 bool _is_encrypt_needed(struct sk_buff *skb)
2635 {
2636     struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2637     struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
2638     struct SKB_info_st *skb_info = (struct SKB_info_st *)skb->head;
2639     struct ieee80211_sta *sta = skb_info->sta;
2640     struct ssv_vif_priv_data *vif_priv = (struct ssv_vif_priv_data *)info->control.vif->drv_priv;
2641     struct ssv_sta_priv_data *ssv_sta_priv = sta ? (struct ssv_sta_priv_data *)sta->drv_priv : NULL;
2642     if ( ( !ieee80211_is_data_qos(hdr->frame_control)
2643             && !ieee80211_is_data(hdr->frame_control))
2644         || !ieee80211_has_protected(hdr->frame_control))
2645         return false;
2646     if (!is_unicast_ether_addr(hdr->addr1))
2647     {
2648         if ( vif_priv->is_security_valid
2649             && vif_priv->need_sw_encrypt
2650             #ifdef USE_LOCAL_CRYPTO
2651             && (vif_priv->crypto_data.ops != NULL)
2652             #endif
2653            )
2654         {
2655             return true;
2656         }
2657     }
2658     else if (ssv_sta_priv != NULL)
2659     {
2660         if ( ssv_sta_priv->need_sw_encrypt
2661             #ifdef USE_LOCAL_CRYPTO
2662             && (ssv_sta_priv->crypto_data.ops != NULL)
2663             #endif
2664            )
2665             return true;
2666     }
2667     return false;
2668 }
2669 #endif
2670 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,0,0)
ssv6200_tx(struct ieee80211_hw * hw,struct sk_buff * skb)2671 static int ssv6200_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
2672 #elif LINUX_VERSION_CODE < KERNEL_VERSION(3,7,0)
2673 static void ssv6200_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
2674 #else
2675 static void ssv6200_tx(struct ieee80211_hw *hw, struct ieee80211_tx_control *control, struct sk_buff *skb)
2676 #endif
2677 {
2678     struct ssv_softc *sc = (struct ssv_softc *)hw->priv;
2679     struct SKB_info_st *skb_info = (struct SKB_info_st *)skb->head;
2680 #ifdef MULTI_THREAD_ENCRYPT
2681     struct ssv_encrypt_task_list *ta = NULL;
2682     unsigned long flags;
2683     int ret = -EOPNOTSUPP;
2684 #endif
2685     #if LINUX_VERSION_CODE < KERNEL_VERSION(3,7,0)
2686     struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2687     skb_info->sta = info->control.sta;
2688     #else
2689     skb_info->sta = control ? control->sta : NULL;
2690     #endif
2691 #ifdef CONFIG_DEBUG_SKB_TIMESTAMP
2692  skb_info->timestamp = ktime_get();
2693 #endif
2694 #if 0
2695     _ssv6xxx_tx(hw, skb);
2696 #else
2697     #ifndef MULTI_THREAD_ENCRYPT
2698     skb_queue_tail(&sc->tx_skb_q, skb);
2699     #ifdef CONFIG_SSV6XXX_DEBUGFS
2700     if (sc->max_tx_skb_q_len < skb_queue_len(&sc->tx_skb_q))
2701         sc->max_tx_skb_q_len = skb_queue_len(&sc->tx_skb_q);
2702     #endif
2703     wake_up_interruptible(&sc->tx_wait_q);
2704     #else
2705     skb_info->crypt_st = PKT_CRYPT_ST_ENC_DONE;
2706     if(_is_encrypt_needed(skb))
2707     {
2708         skb_info->crypt_st = PKT_CRYPT_ST_ENC_PRE;
2709         ret = ssv6xxx_skb_pre_encrypt(skb, sc);
2710     }
2711     if (ret == 0)
2712     {
2713         spin_lock_irqsave(&sc->crypt_st_lock, flags);
2714         __skb_queue_tail(&sc->preprocess_q, skb);
2715         #ifdef CONFIG_SSV6XXX_DEBUGFS
2716         if (sc->max_preprocess_q_len < skb_queue_len(&sc->preprocess_q))
2717             sc->max_preprocess_q_len = skb_queue_len(&sc->preprocess_q);
2718         #endif
2719         spin_unlock_irqrestore(&sc->crypt_st_lock, flags);
2720         list_for_each_entry(ta, &sc->encrypt_task_head, list)
2721         {
2722             if((cpu_online(ta->cpu_no)) && (ta->running == 0))
2723             {
2724                 wake_up(&ta->encrypt_wait_q);
2725                 break;
2726             }
2727         }
2728     }
2729     else if(ret == -EOPNOTSUPP)
2730     {
2731         skb_info->crypt_st = PKT_CRYPT_ST_NOT_SUPPORT;
2732         skb_queue_tail(&sc->tx_skb_q, skb);
2733         #ifdef CONFIG_SSV6XXX_DEBUGFS
2734         if (sc->max_tx_skb_q_len < skb_queue_len(&sc->tx_skb_q))
2735             sc->max_tx_skb_q_len = skb_queue_len(&sc->tx_skb_q);
2736         #endif
2737         wake_up_interruptible(&sc->tx_wait_q);
2738     }
2739     else
2740     {
2741         dev_err(sc->dev, "strange fail to pre-encrypt packet/n");
2742     }
2743     #endif
2744 #endif
2745 #ifdef ENABLE_TX_Q_FLOW_CONTROL
2746     do {
2747 #ifdef MULTI_THREAD_ENCRYPT
2748         if ( (skb_queue_len(&sc->preprocess_q) >= MAX_CRYPTO_Q_LEN)
2749             || (skb_queue_len(&sc->crypted_q) >= MAX_CRYPTO_Q_LEN))
2750         {
2751             ieee80211_stop_queues(sc->hw);
2752             break;
2753         }
2754 #endif
2755         if (skb_queue_len(&sc->tx_skb_q) >= MAX_TX_Q_LEN)
2756             ieee80211_stop_queues(sc->hw);
2757     } while (0);
2758 #endif
2759 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,0,0)
2760     return NETDEV_TX_OK;
2761 #endif
2762 }
2763 #ifdef MULTI_THREAD_ENCRYPT
ssv6xxx_encrypt_task(void * data)2764 int ssv6xxx_encrypt_task (void *data)
2765 {
2766     struct ssv_softc *sc = (struct ssv_softc *)data;
2767     unsigned long flags;
2768     struct ssv_encrypt_task_list *ta = NULL;
2769     struct task_struct *this_task = current;
2770     int ori_prio;
2771     int min_prio;
2772     int cur_prio;
2773     u32 cont_crypto_failure = 0;
2774     const u32 max_cont_crypto_failure = 10;
2775     ori_prio = this_task->prio;
2776     min_prio = 0;
2777     cur_prio = ori_prio;
2778     dev_err(sc->dev, "Crypto task %d running with priority %d.\n", this_task->pid, ori_prio);
2779     list_for_each_entry(ta, &sc->encrypt_task_head, list)
2780     {
2781         if(ta->encrypt_task == current)
2782             break;
2783     }
2784     while (!kthread_should_stop())
2785     {
2786         struct sk_buff *skb = NULL;
2787         struct ieee80211_hdr *hdr = NULL;
2788         unsigned long CPUMask = 0;
2789         int enc_ret = 0;
2790         volatile bool wakeup_tx;
2791         if(skb_queue_len_safe(&sc->preprocess_q) == 0 || (ta->cpu_offline != 0) )
2792         {
2793             ta->running = 0;
2794             set_current_state(TASK_UNINTERRUPTIBLE);
2795             wait_event_timeout(ta->encrypt_wait_q,
2796                                   ( (ta->cpu_offline == 0)
2797                                    && (ta->paused == 0)
2798                                    && skb_queue_len_safe(&sc->preprocess_q))
2799                                || kthread_should_stop(),
2800                                msecs_to_jiffies(60000));
2801             set_current_state(TASK_RUNNING);
2802             ta->running = 1;
2803             CPUMask = *(cpumask_bits(&current->cpus_allowed));
2804         }
2805         if (kthread_should_stop())
2806         {
2807             printk("[MT-ENCRYPT]: Quit Encryption task loop ...\n");
2808             ta->running = 0;
2809             break;
2810         }
2811         spin_lock_irqsave(&sc->crypt_st_lock, flags);
2812         if ((skb = __skb_dequeue(&sc->preprocess_q)) != NULL)
2813         {
2814             SKB_info * skb_info = (SKB_info *)skb->head;
2815             __skb_queue_tail(&sc->crypted_q, skb);
2816             #ifdef CONFIG_SSV6XXX_DEBUGFS
2817             if (sc->max_crypted_q_len < skb_queue_len(&sc->crypted_q))
2818                 sc->max_crypted_q_len = skb_queue_len(&sc->crypted_q);
2819             #endif
2820             spin_unlock_irqrestore(&sc->crypt_st_lock, flags);
2821             if(skb_info->crypt_st == PKT_CRYPT_ST_ENC_PRE)
2822             {
2823                 enc_ret = ssv6xxx_skb_encrypt(skb, sc);
2824                 if (enc_ret == 0)
2825                 {
2826                     skb_info->crypt_st = PKT_CRYPT_ST_ENC_DONE;
2827                     cont_crypto_failure = 0;
2828                     if (cur_prio != ori_prio)
2829                     {
2830                         struct sched_param sp = { .sched_priority = ori_prio };
2831                         spin_lock_irqsave(&sc->crypt_st_lock, flags);
2832                         dev_err(sc->dev, "Set crypto task %d priority to %d.\n",
2833                                 this_task->pid, sp.sched_priority);
2834                         sched_setscheduler(this_task, this_task->policy, &sp);
2835                         cur_prio = ori_prio;
2836                         spin_unlock_irqrestore(&sc->crypt_st_lock, flags);
2837                     }
2838                 }
2839                 else
2840                 {
2841                     skb_info->crypt_st = PKT_CRYPT_ST_FAIL;
2842                 }
2843             }
2844             else if(skb_info->crypt_st == PKT_CRYPT_ST_DEC_PRE)
2845             {
2846                 struct ieee80211_sta *sta = skb_info->sta;
2847                 struct ieee80211_rx_status *rxs = IEEE80211_SKB_RXCB(skb);
2848                 enc_ret = ssv6xxx_skb_decrypt(skb, sta, sc);
2849                 if (enc_ret >= 0)
2850                 {
2851                     skb_info->crypt_st = PKT_CRYPT_ST_DEC_DONE;
2852                     hdr = (struct ieee80211_hdr *)(skb->data);
2853                     hdr->frame_control = hdr->frame_control & ~(cpu_to_le16(IEEE80211_FCTL_PROTECTED));
2854                     rxs->flag |= (RX_FLAG_DECRYPTED|RX_FLAG_IV_STRIPPED);
2855                 }
2856                 else
2857                 {
2858                     dev_err(sc->dev, "Decrypt fail, skb = %p\n", skb);
2859                     skb_info->crypt_st = PKT_CRYPT_ST_FAIL;
2860                 }
2861             }
2862             if (skb_info->crypt_st == PKT_CRYPT_ST_FAIL)
2863             {
2864                 spin_lock_irqsave(&sc->crypt_st_lock, flags);
2865                 __skb_unlink(skb, &sc->crypted_q);
2866                 #ifdef CONFIG_SMP
2867                 dev_err(sc->dev, "%d-%d(%d) crypto fail, skb = %p %d %d\n",
2868                         this_task->on_cpu, this_task->pid, cur_prio, skb,
2869                         cont_crypto_failure, ta->paused);
2870                 #endif
2871                 spin_unlock_irqrestore(&sc->crypt_st_lock, flags);
2872                 ssv_skb_free(skb);
2873                 skb = NULL;
2874                 if (cont_crypto_failure == max_cont_crypto_failure)
2875                 {
2876                     spin_lock_irqsave(&sc->crypt_st_lock, flags);
2877                     if (cur_prio != min_prio)
2878                     {
2879                         struct sched_param sp = { .sched_priority = min_prio };
2880                         sched_setscheduler(this_task, this_task->policy, &sp);
2881                         cur_prio = min_prio;
2882                         dev_err(sc->dev, "Set crypto task %d priority to %d.\n",
2883                                 this_task->pid, sp.sched_priority);
2884                     }
2885                     spin_unlock_irqrestore(&sc->crypt_st_lock, flags);
2886                 }
2887                 else
2888                 {
2889                     ++cont_crypto_failure;
2890                 }
2891                 schedule();
2892             }
2893         }
2894         else
2895         {
2896             spin_unlock_irqrestore(&sc->crypt_st_lock, flags);
2897         }
2898         spin_lock_irqsave(&sc->crypt_st_lock, flags);
2899         skb = NULL;
2900         wakeup_tx = false;
2901         while((skb = skb_peek(&sc->crypted_q)) != NULL)
2902         {
2903             SKB_info* skb_info = (SKB_info*)skb->head;
2904             if(skb_info->crypt_st == PKT_CRYPT_ST_ENC_DONE)
2905             {
2906                 __skb_unlink(skb, &sc->crypted_q);
2907                 skb_queue_tail(&sc->tx_skb_q, skb);
2908                 #ifdef CONFIG_SSV6XXX_DEBUGFS
2909                 if (sc->max_tx_skb_q_len < skb_queue_len(&sc->tx_skb_q))
2910                     sc->max_tx_skb_q_len = skb_queue_len(&sc->tx_skb_q);
2911                 #endif
2912                 wakeup_tx = true;
2913                 skb = NULL;
2914             }
2915             else if(skb_info->crypt_st == PKT_CRYPT_ST_DEC_DONE)
2916             {
2917                 __skb_unlink(skb, &sc->crypted_q);
2918                 ieee80211_rx_irqsafe(sc->hw, skb);
2919             }
2920             else
2921                 break;
2922         }
2923         spin_unlock_irqrestore(&sc->crypt_st_lock, flags);
2924         skb = NULL;
2925         if(wakeup_tx)
2926         {
2927             wake_up_interruptible(&sc->tx_wait_q);
2928         }
2929     }
2930     return 0;
2931 }
_stop_crypto_task(struct ssv_softc * sc)2932 void _stop_crypto_task (struct ssv_softc *sc)
2933 {
2934     struct ssv_encrypt_task_list *ta = NULL;
2935     int num_running = 0;
2936     list_for_each_entry(ta, &sc->encrypt_task_head, list)
2937     {
2938         ta->paused = 1;
2939     }
2940     do {
2941         num_running = 0;
2942         list_for_each_entry(ta, &sc->encrypt_task_head, list)
2943         {
2944             if (ta->running != 0)
2945             {
2946                 int cpu_id = smp_processor_id();
2947                 if (ta->cpu_no == cpu_id)
2948                 {
2949                     unsigned long flags;
2950                     spin_lock_irqsave(&sc->crypt_st_lock, flags);
2951                     dev_warn(sc->dev, "Crypto running on the same core. (%d, %d)\n", cpu_id, num_running);
2952                     spin_unlock_irqrestore(&sc->crypt_st_lock, flags);
2953                     continue;
2954                 }
2955                 ++num_running;
2956                 break;
2957             }
2958         }
2959     } while (num_running != 0);
2960 }
_resume_crypto_task(struct ssv_softc * sc)2961 void _resume_crypto_task (struct ssv_softc *sc)
2962 {
2963     struct ssv_encrypt_task_list *ta = NULL;
2964     list_for_each_entry(ta, &sc->encrypt_task_head, list)
2965     {
2966         ta->paused = 0;
2967         wake_up(&ta->encrypt_wait_q);
2968     }
2969 }
_remove_sta_skb_from_q(struct ssv_softc * sc,struct sk_buff_head * q,u32 addr0_3,u32 addr4_5)2970 u32 _remove_sta_skb_from_q (struct ssv_softc *sc, struct sk_buff_head *q,
2971                              u32 addr0_3, u32 addr4_5)
2972 {
2973     struct sk_buff *skb, *skb_tmp;
2974     u32 removed_skb_num = 0;
2975     skb_queue_walk_safe(q, skb, skb_tmp)
2976     {
2977         struct ieee80211_sta *skb_sta;
2978         u32 skb_addr0_3;
2979         u32 skb_addr4_5;
2980         struct SKB_info_st *skb_info = (struct SKB_info_st *)skb->head;
2981         skb_sta = skb_info->sta;
2982         if (skb_sta == NULL)
2983             continue;
2984         skb_addr0_3 = *(u32 *)&skb_sta->addr[0];
2985         skb_addr4_5 = (u32)*(u16 *)&skb_sta->addr[4];
2986         if ((addr0_3 != skb_addr0_3) || (addr4_5 != skb_addr4_5))
2987             continue;
2988         __skb_unlink(skb, q);
2989         ssv6xxx_txbuf_free_skb(skb, sc);
2990         ++removed_skb_num;
2991     }
2992     return removed_skb_num;
2993 }
_clean_up_crypto_skb(struct ssv_softc * sc,struct ieee80211_sta * sta)2994 void _clean_up_crypto_skb (struct ssv_softc *sc, struct ieee80211_sta *sta)
2995 {
2996     unsigned long flags;
2997     u32 sta_addr0_3 = *(u32 *)&sta->addr[0];
2998     u32 sta_addr4_5 = (u32)*(u16 *)&sta->addr[4];
2999     u32 removed_skb_num;
3000     dev_info(sc->dev, "Clean up skb for STA %pM.\n", sta->addr);
3001     _stop_crypto_task(sc);
3002     spin_lock_irqsave(&sc->crypt_st_lock, flags);
3003     removed_skb_num = _remove_sta_skb_from_q(sc, &sc->preprocess_q,
3004                                              sta_addr0_3, sta_addr4_5);
3005     dev_info(sc->dev, "Removed %u MPDU from precess queue.\n", removed_skb_num);
3006     removed_skb_num = _remove_sta_skb_from_q(sc, &sc->crypted_q,
3007                                              sta_addr0_3, sta_addr4_5);
3008     dev_info(sc->dev, "Removed %u MPDU from encrypted queue.\n", removed_skb_num);
3009     spin_unlock_irqrestore(&sc->crypt_st_lock, flags);
3010     _resume_crypto_task(sc);
3011 }
3012 #endif
ssv6xxx_tx_task(void * data)3013 int ssv6xxx_tx_task (void *data)
3014 {
3015     struct ssv_softc *sc = (struct ssv_softc *)data;
3016     u32 wait_period = SSV_AMPDU_timer_period / 2;
3017     printk("SSV6XXX TX Task started.\n");
3018     while (!kthread_should_stop())
3019     {
3020         u32 before_timeout = (-1);
3021   set_current_state(TASK_INTERRUPTIBLE);
3022         before_timeout = wait_event_interruptible_timeout(sc->tx_wait_q,
3023                                                           ( skb_queue_len(&sc->tx_skb_q)
3024                                                            || kthread_should_stop()
3025                                                            || sc->tx_q_empty),
3026                                                            msecs_to_jiffies(wait_period));
3027         if (kthread_should_stop())
3028         {
3029             printk("Quit TX task loop...\n");
3030             break;
3031         }
3032   set_current_state(TASK_RUNNING);
3033         do {
3034             struct sk_buff *tx_skb = skb_dequeue(&sc->tx_skb_q);
3035             if (tx_skb == NULL)
3036                 break;
3037             _ssv6xxx_tx(sc->hw, tx_skb);
3038         } while (1);
3039 #ifdef CONFIG_DEBUG_SKB_TIMESTAMP
3040   {
3041    struct ssv_hw_txq *hw_txq = NULL;
3042    struct ieee80211_tx_info *tx_info = NULL;
3043    struct sk_buff *skb = NULL;
3044    int txqid;
3045    unsigned int timeout;
3046    u32 status;
3047    for (txqid=0; txqid<SSV_HW_TXQ_NUM; txqid++) {
3048     hw_txq = &ssv_dbg_ctrl_hci->hw_txq[txqid];
3049     skb = skb_peek(&hw_txq->qhead);
3050     if (skb != NULL) {
3051      tx_info = IEEE80211_SKB_CB(skb);
3052      if (tx_info->flags & IEEE80211_TX_CTL_AMPDU)
3053       timeout = cal_duration_of_ampdu(skb, SKB_DURATION_STAGE_IN_HWQ);
3054      else
3055       timeout = cal_duration_of_mpdu(skb);
3056      if (timeout > SKB_DURATION_TIMEOUT_MS) {
3057       HCI_IRQ_STATUS(ssv_dbg_ctrl_hci, &status);
3058       printk("hci int_mask: %08x\n", ssv_dbg_ctrl_hci->int_mask);
3059       printk("sdio status: %08x\n", status);
3060       printk("hwq%d len: %d\n", txqid, skb_queue_len(&hw_txq->qhead));
3061      }
3062     }
3063    }
3064   }
3065 #endif
3066         if (sc->tx_q_empty || (before_timeout == 0))
3067         {
3068             u32 flused_ampdu = ssv6xxx_ampdu_flush(sc->hw);
3069             sc->tx_q_empty = false;
3070             if (flused_ampdu == 0 && before_timeout == 0)
3071             {
3072                 wait_period *= 2;
3073                 if (wait_period > 1000)
3074                     wait_period = 1000;
3075             }
3076         }
3077         else
3078             wait_period = SSV_AMPDU_timer_period / 2;
3079     }
3080     return 0;
3081 }
ssv6xxx_rx_task(void * data)3082 int ssv6xxx_rx_task (void *data)
3083 {
3084     struct ssv_softc *sc = (struct ssv_softc *)data;
3085     unsigned long wait_period = msecs_to_jiffies(200);
3086     unsigned long last_timeout_check_jiffies = jiffies;
3087     unsigned long cur_jiffies;
3088     printk("SSV6XXX RX Task started.\n");
3089     while (!kthread_should_stop())
3090     {
3091         u32 before_timeout = (-1);
3092         set_current_state(TASK_INTERRUPTIBLE);
3093         before_timeout = wait_event_interruptible_timeout(sc->rx_wait_q,
3094                                                           ( skb_queue_len(&sc->rx_skb_q)
3095                                                            || skb_queue_len(&sc->tx_done_q)
3096                                                            || kthread_should_stop()),
3097                                                            wait_period);
3098         if (kthread_should_stop())
3099         {
3100             printk("Quit RX task loop...\n");
3101             break;
3102         }
3103         set_current_state(TASK_RUNNING);
3104         cur_jiffies = jiffies;
3105         if ( (before_timeout == 0)
3106             || time_before((last_timeout_check_jiffies + wait_period), cur_jiffies))
3107         {
3108             ssv6xxx_ampdu_check_timeout(sc->hw);
3109             last_timeout_check_jiffies = cur_jiffies;
3110         }
3111         if (skb_queue_len(&sc->rx_skb_q))
3112             _process_rx_q(sc, &sc->rx_skb_q, NULL);
3113         if (skb_queue_len(&sc->tx_done_q))
3114             _process_tx_done(sc);
3115     }
3116     return 0;
3117 }
3118 #ifdef CONFIG_SSV_CABRIO_E
3119 struct ssv6xxx_iqk_cfg init_iqk_cfg = {
3120     SSV6XXX_IQK_CFG_XTAL_26M,
3121 #ifdef CONFIG_SSV_DPD
3122     SSV6XXX_IQK_CFG_PA_LI_MPB,
3123 #else
3124     SSV6XXX_IQK_CFG_PA_DEF,
3125 #endif
3126     0,
3127     0,
3128     26,
3129     3,
3130     0x75,
3131     0x75,
3132     0x80,
3133     0x80,
3134     SSV6XXX_IQK_CMD_INIT_CALI,
3135     { SSV6XXX_IQK_TEMPERATURE
3136     + SSV6XXX_IQK_RXDC
3137     + SSV6XXX_IQK_RXRC
3138     + SSV6XXX_IQK_TXDC
3139     + SSV6XXX_IQK_TXIQ
3140     + SSV6XXX_IQK_RXIQ
3141 #ifdef CONFIG_SSV_DPD
3142     + SSV6XXX_IQK_PAPD
3143 #endif
3144     },
3145 };
3146 #endif
ssv6200_start(struct ieee80211_hw * hw)3147 static int ssv6200_start(struct ieee80211_hw *hw)
3148 {
3149     struct ssv_softc *sc=hw->priv;
3150     struct ssv_hw *sh=sc->sh;
3151     struct ieee80211_channel *chan;
3152     mutex_lock(&sc->mutex);
3153     if (ssv6xxx_init_mac(sc->sh) != 0) {
3154         printk("Initialize ssv6200 mac fail!!\n");
3155         ssv6xxx_deinit_mac(sc);
3156   mutex_unlock(&sc->mutex);
3157         return -1;
3158     }
3159 #ifdef CONFIG_P2P_NOA
3160     ssv6xxx_noa_reset(sc);
3161 #endif
3162     HCI_START(sh);
3163     ieee80211_wake_queues(hw);
3164     ssv6200_ampdu_init(hw);
3165     sc->watchdog_flag = WD_KICKED;
3166     mutex_unlock(&sc->mutex);
3167     mod_timer(&sc->watchdog_timeout, jiffies + WATCHDOG_TIMEOUT);
3168 #if (defined(CONFIG_SSV_SUPPORT_ANDROID)||defined(CONFIG_SSV_BUILD_AS_ONE_KO))
3169 #ifdef CONFIG_SSV_SMARTLINK
3170         {
3171             extern int ksmartlink_init(void);
3172             (void)ksmartlink_init();
3173         }
3174 #endif
3175 #endif
3176 #ifdef CONFIG_SSV_CABRIO_E
3177     {
3178          int ret = ssv6xxx_do_iq_calib(sc->sh, &init_iqk_cfg);
3179          if (ret != 0) {
3180              printk("IQ Calibration failed (%d)!!\n", ret);
3181              return ret;
3182          }
3183     }
3184 
3185     printk("+++++++++++++++++++++++++++222222222222222!!\n");
3186 
3187 	SMAC_REG_WRITE(sc->sh, ADR_PHY_EN_1, 0x217f);
3188     if((sh->cfg.chip_identity == SSV6051Z) || (sc->sh->cfg.chip_identity == SSV6051P))
3189     {
3190         int i;
3191         for (i = 0; i < sh->ch_cfg_size; i++)
3192         {
3193             SMAC_REG_READ(sh, sh->p_ch_cfg[i].reg_addr, &sh->p_ch_cfg[i].ch1_12_value);
3194         }
3195     }
3196 #endif
3197     #if LINUX_VERSION_CODE < KERNEL_VERSION(3,7,0)
3198     chan = hw->conf.channel;
3199     #else
3200     chan = hw->conf.chandef.chan;
3201     #endif
3202     sc->cur_channel = chan;
3203     printk("%s(): current channel: %d,sc->ps_status=%d\n", __FUNCTION__, sc->cur_channel->hw_value,sc->ps_status);
3204     ssv6xxx_set_channel(sc, chan->hw_value);
3205     ssv6xxx_rf_enable(sh);
3206     return 0;
3207 }
ssv6200_stop(struct ieee80211_hw * hw)3208 static void ssv6200_stop(struct ieee80211_hw *hw)
3209 {
3210     struct ssv_softc *sc=hw->priv;
3211     u32 count=0;
3212 #ifdef CONFIG_SSV_RSSI
3213         struct rssi_res_st *rssi_tmp0, *rssi_tmp1;
3214 #endif
3215     printk(KERN_INFO "%s(): sc->ps_status=%d\n", __FUNCTION__,sc->ps_status);
3216     mutex_lock(&sc->mutex);
3217 #ifdef CONFIG_SSV_RSSI
3218     list_for_each_entry_safe(rssi_tmp0, rssi_tmp1, &rssi_res.rssi_list, rssi_list) {
3219         list_del(&rssi_tmp0->rssi_list);
3220         kfree(rssi_tmp0);
3221     }
3222 #endif
3223     ssv6200_ampdu_deinit(hw);
3224     ssv6xxx_rf_disable(sc->sh);
3225     HCI_STOP(sc->sh);
3226 #ifndef NO_USE_RXQ_LOCK
3227     while(0) {
3228 #else
3229     while (skb_queue_len(&sc->rx.rxq_head)) {
3230 #endif
3231         printk("sc->rx.rxq_count=%d\n", sc->rx.rxq_count);
3232         count ++;
3233         if (count > 90000000) {
3234             printk("ERROR....ERROR......ERROR..........\n");
3235             break;
3236         }
3237     }
3238     HCI_TXQ_FLUSH(sc->sh, (TXQ_EDCA_0|TXQ_EDCA_1|TXQ_EDCA_2|
3239         TXQ_EDCA_3|TXQ_MGMT));
3240 #if 0
3241     cancel_work_sync(&sc->rx_work);
3242 #endif
3243     if((sc->ps_status == PWRSV_PREPARE)||(sc->ps_status == PWRSV_ENABLE)){
3244            ssv6xxx_enable_ps(sc);
3245            ssv6xxx_rf_enable(sc->sh);
3246     }
3247     sc->watchdog_flag = WD_SLEEP;
3248     mutex_unlock(&sc->mutex);
3249     del_timer_sync(&sc->watchdog_timeout);
3250 #if (defined(CONFIG_SSV_SUPPORT_ANDROID)||defined(CONFIG_SSV_BUILD_AS_ONE_KO))
3251 #ifdef CONFIG_SSV_SMARTLINK
3252         {
3253             extern void ksmartlink_exit(void);
3254             ksmartlink_exit();
3255         }
3256 #endif
3257 #endif
3258     printk("%s(): leave\n", __FUNCTION__);
3259 }
3260 void inline ssv62xxx_set_bssid(struct ssv_softc *sc, u8 *bssid)
3261 {
3262     memcpy(sc->bssid, bssid, 6);
3263     SMAC_REG_WRITE(sc->sh, ADR_BSSID_0, *((u32 *)&sc->bssid[0]));
3264     SMAC_REG_WRITE(sc->sh, ADR_BSSID_1, *((u32 *)&sc->bssid[4]));
3265 }
3266 struct ssv_vif_priv_data * ssv6xxx_config_vif_res(struct ssv_softc *sc,
3267     struct ieee80211_vif *vif)
3268 {
3269     int i;
3270     struct ssv_vif_priv_data *priv_vif;
3271     struct ssv_vif_info *vif_info;
3272     lockdep_assert_held(&sc->mutex);
3273     for(i=0 ; i<SSV6200_MAX_VIF ;i++){
3274         if (sc->vif_info[i].vif == NULL)
3275             break;
3276     }
3277     BUG_ON(i >= SSV6200_MAX_VIF);
3278     printk("ssv6xxx_config_vif_res id[%d].\n", i);
3279     priv_vif = (struct ssv_vif_priv_data *)vif->drv_priv;
3280     memset(priv_vif, 0, sizeof(struct ssv_vif_priv_data));
3281     priv_vif->vif_idx = i;
3282     memset(&sc->vif_info[i], 0, sizeof(sc->vif_info[0]));
3283     sc->vif_info[i].vif = vif;
3284     sc->vif_info[i].vif_priv = priv_vif;
3285     INIT_LIST_HEAD(&priv_vif->sta_list);
3286     priv_vif->pair_cipher = SSV_CIPHER_NONE;
3287     priv_vif->group_cipher = SSV_CIPHER_NONE;
3288     priv_vif->has_hw_decrypt = false;
3289     priv_vif->has_hw_encrypt = false;
3290     priv_vif->need_sw_encrypt = false;
3291     priv_vif->need_sw_decrypt = false;
3292     priv_vif->use_mac80211_decrypt = false;
3293     priv_vif->is_security_valid = false;
3294     priv_vif->force_sw_encrypt = (vif->type == NL80211_IFTYPE_AP);
3295     #ifdef USE_LOCAL_CRYPTO
3296     priv_vif->crypto_data.ops = NULL;
3297     priv_vif->crypto_data.priv = NULL;
3298     #ifdef HAS_CRYPTO_LOCK
3299     rwlock_init(&priv_vif->crypto_data.lock);
3300     #endif
3301     #endif
3302     vif_info = &sc->vif_info[priv_vif->vif_idx];
3303     vif_info->if_type = vif->type;
3304     vif_info->vif = vif;
3305     return priv_vif;
3306 }
3307 static int ssv6200_add_interface(struct ieee80211_hw *hw,
3308                                  struct ieee80211_vif *vif)
3309 {
3310     struct ssv_softc *sc=hw->priv;
3311     int ret=0;
3312     struct ssv_vif_priv_data *vif_priv = NULL;
3313     printk("[I] %s(): vif->type = %d, NL80211_IFTYPE_AP=%d\n", __FUNCTION__, vif->type,NL80211_IFTYPE_AP);
3314     if ( (sc->nvif >= SSV6200_MAX_VIF)
3315         || ( ( (vif->type == NL80211_IFTYPE_AP)
3316                 || (vif->p2p))
3317             && (sc->ap_vif != NULL)))
3318     {
3319         dev_err(sc->dev, "Add interface of type %d (p2p: %d) failed.\n", vif->type, vif->p2p);
3320         return -EOPNOTSUPP;
3321     }
3322     mutex_lock(&sc->mutex);
3323     vif_priv = ssv6xxx_config_vif_res(sc, vif);
3324     if ((vif_priv->vif_idx == 0) && (vif->p2p == 0) && (vif->type == NL80211_IFTYPE_AP))
3325     {
3326         printk("VIF[0] set bssid and config opmode to ap\n");
3327         ssv62xxx_set_bssid(sc, sc->sh->cfg.maddr[0]);
3328         SMAC_REG_SET_BITS(sc->sh, ADR_GLBLE_SET, SSV6200_OPMODE_AP, OP_MODE_MSK);
3329     }
3330     if (vif->type == NL80211_IFTYPE_AP)
3331     {
3332         BUG_ON(sc->ap_vif != NULL);
3333         sc->ap_vif = vif;
3334         if ( !vif->p2p
3335             && (vif_priv->vif_idx == 0))
3336         {
3337             printk("Normal AP mode. Config Q4 to DTIM Q.\n");
3338             SMAC_REG_SET_BITS(sc->sh, ADR_MTX_BCN_EN_MISC,
3339                               MTX_HALT_MNG_UNTIL_DTIM_MSK,
3340                               MTX_HALT_MNG_UNTIL_DTIM_MSK);
3341             sc->bq4_dtim = true;
3342         }
3343 #ifdef CONFIG_SSV_SUPPORT_ANDROID
3344         if(vif->p2p == 0)
3345         {
3346             printk(KERN_INFO "AP mode init wifi_alive_lock\n");
3347             ssv_wake_lock(sc);
3348 			SMAC_REG_WRITE(sc->sh, ADR_RX_11B_CCA_CONTROL, 0x00160200);
3349 			SMAC_REG_WRITE(sc->sh, ADR_RX_11B_CCA_1, 0x20380050);
3350         }
3351 #endif
3352     }
3353     sc->nvif++;
3354     dev_err(sc->dev, "VIF %02x:%02x:%02x:%02x:%02x:%02x of type %d is added.\n",
3355              vif->addr[0], vif->addr[1], vif->addr[2],
3356              vif->addr[3], vif->addr[4], vif->addr[5], vif->type);
3357 #ifdef CONFIG_SSV6XXX_DEBUGFS
3358     ssv6xxx_debugfs_add_interface(sc, vif);
3359 #endif
3360     mutex_unlock(&sc->mutex);
3361     return ret;
3362 }
3363 static void ssv6200_remove_interface(struct ieee80211_hw *hw,
3364                                      struct ieee80211_vif *vif)
3365 {
3366     struct ssv_softc *sc = hw->priv;
3367     struct ssv_vif_priv_data *vif_priv = (struct ssv_vif_priv_data *)vif->drv_priv;
3368     #ifdef USE_LOCAL_CRYPTO
3369     INIT_WRITE_CRYPTO_DATA(crypto_data, &vif_priv->crypto_data);
3370     #endif
3371     dev_err(sc->dev,
3372              "Removing interface %02x:%02x:%02x:%02x:%02x:%02x. PS=%d\n",
3373              vif->addr[0], vif->addr[1], vif->addr[2],
3374              vif->addr[3], vif->addr[4], vif->addr[5], sc->ps_status);
3375     mutex_lock(&sc->mutex);
3376     #ifdef USE_LOCAL_CRYPTO
3377     START_WRITE_CRYPTO_DATA(crypto_data);
3378     if (crypto_data->ops && crypto_data->priv)
3379     {
3380         crypto_data->ops->deinit(crypto_data->priv);
3381     }
3382     crypto_data->ops = NULL;
3383     crypto_data->priv = NULL;
3384     END_WRITE_CRYPTO_DATA(crypto_data);
3385     #endif
3386     #ifdef CONFIG_SSV6XXX_DEBUGFS
3387     ssv6xxx_debugfs_remove_interface(sc, vif);
3388     #endif
3389     if (vif->type == NL80211_IFTYPE_AP)
3390     {
3391         if (sc->bq4_dtim)
3392         {
3393             sc->bq4_dtim = false;
3394             ssv6200_release_bcast_frame_res(sc, vif);
3395             SMAC_REG_SET_BITS(sc->sh, ADR_MTX_BCN_EN_MISC,
3396                               0, MTX_HALT_MNG_UNTIL_DTIM_MSK);
3397             printk("Config Q4 to normal Q \n");
3398         }
3399         ssv6xxx_beacon_release(sc);
3400         sc->ap_vif = NULL;
3401 #ifdef CONFIG_SSV_SUPPORT_ANDROID
3402         if(vif->p2p == 0)
3403         {
3404             ssv_wake_unlock(sc);
3405 			SMAC_REG_WRITE(sc->sh, ADR_RX_11B_CCA_CONTROL, 0x0);
3406             SMAC_REG_WRITE(sc->sh, ADR_RX_11B_CCA_1, 0x20300050);
3407             printk(KERN_INFO "AP mode destroy wifi_alive_lock\n");
3408         }
3409 #endif
3410     }
3411     memset(&sc->vif_info[vif_priv->vif_idx], 0, sizeof(struct ssv_vif_info));
3412     sc->nvif--;
3413     mutex_unlock(&sc->mutex);
3414 }
3415 static int ssv6200_change_interface(struct ieee80211_hw *dev,
3416                             struct ieee80211_vif *vif,
3417                             enum nl80211_iftype new_type,
3418                             bool p2p)
3419 {
3420         int ret = 0;
3421         printk("change_interface new: %d (%d), old: %d (%d)\n", new_type,
3422                  p2p, vif->type, vif->p2p);
3423 
3424         if (new_type != vif->type || vif->p2p != p2p) {
3425                 ssv6200_remove_interface(dev, vif);
3426                 vif->type = new_type;
3427                 vif->p2p = p2p;
3428                 ret = ssv6200_add_interface(dev, vif);
3429         }
3430 
3431         return ret;
3432 }
3433 void ssv6xxx_ps_callback_func(unsigned long data)
3434 {
3435     struct ssv_softc *sc = (struct ssv_softc *)data;
3436     struct sk_buff *skb;
3437     struct cfg_host_cmd *host_cmd;
3438     int retry_cnt=20;
3439 #ifdef SSV_WAKEUP_HOST
3440     SMAC_REG_WRITE(sc->sh, ADR_RX_FLOW_MNG, M_ENG_MACRX|(M_ENG_CPU<<4)|(M_ENG_HWHCI<<8));
3441     SMAC_REG_WRITE(sc->sh, ADR_RX_FLOW_DATA, M_ENG_MACRX|(M_ENG_CPU<<4)|(M_ENG_HWHCI<<8));
3442     SMAC_REG_WRITE(sc->sh, ADR_MRX_FLT_TB0+6*4, (sc->mac_deci_tbl[6]|1));
3443 #else
3444     SMAC_REG_WRITE(sc->sh, ADR_RX_FLOW_MNG, M_ENG_MACRX|(M_ENG_TRASH_CAN<<4));
3445     SMAC_REG_WRITE(sc->sh, ADR_RX_FLOW_DATA, M_ENG_MACRX|(M_ENG_TRASH_CAN<<4));
3446     SMAC_REG_WRITE(sc->sh, ADR_RX_FLOW_MNG, M_ENG_MACRX|(M_ENG_TRASH_CAN<<4));
3447 #endif
3448     skb = ssv_skb_alloc(sizeof(struct cfg_host_cmd));
3449     skb->data_len = sizeof(struct cfg_host_cmd);
3450     skb->len = skb->data_len;
3451     host_cmd = (struct cfg_host_cmd *)skb->data;
3452     host_cmd->c_type = HOST_CMD;
3453     host_cmd->RSVD0 = 0;
3454     host_cmd->h_cmd = (u8)SSV6XXX_HOST_CMD_PS;
3455     host_cmd->len = skb->data_len;
3456 #ifdef SSV_WAKEUP_HOST
3457     host_cmd->dummy = sc->ps_aid;
3458 #else
3459  host_cmd->dummy = 0;
3460 #endif
3461     sc->ps_aid = 0;
3462     while((HCI_SEND_CMD(sc->sh, skb)!=0)&&(retry_cnt)){
3463         printk(KERN_INFO "PS cmd retry=%d!!\n",retry_cnt);
3464         retry_cnt--;
3465     }
3466     ssv_skb_free(skb);
3467     printk(KERN_INFO "SSV6XXX_HOST_CMD_PS,ps_aid = %d,len=%d,tabl=0x%x\n",host_cmd->dummy,skb->len,(sc->mac_deci_tbl[6]|1));
3468 }
3469 void ssv6xxx_enable_ps(struct ssv_softc *sc)
3470 {
3471     sc->ps_status = PWRSV_ENABLE;
3472 }
3473 void ssv6xxx_disable_ps(struct ssv_softc *sc)
3474 {
3475     sc->ps_status = PWRSV_DISABLE;
3476     printk(KERN_INFO "PowerSave disabled\n");
3477 }
3478 int ssv6xxx_watchdog_controller(struct ssv_hw *sh ,u8 flag)
3479 {
3480     struct sk_buff *skb;
3481     struct cfg_host_cmd *host_cmd;
3482     int ret = 0;
3483     printk("ssv6xxx_watchdog_controller %d\n",flag);
3484     skb = ssv_skb_alloc(HOST_CMD_HDR_LEN);
3485     if(skb == NULL)
3486     {
3487         printk("init ssv6xxx_watchdog_controller fail!!!\n");
3488         return (-1);
3489     }
3490     skb->data_len = HOST_CMD_HDR_LEN;
3491     skb->len = skb->data_len;
3492     host_cmd = (struct cfg_host_cmd *)skb->data;
3493     host_cmd->c_type = HOST_CMD;
3494     host_cmd->h_cmd = (u8)flag;
3495     host_cmd->len = skb->data_len;
3496     sh->hci.hci_ops->hci_send_cmd(skb);
3497     ssv_skb_free(skb);
3498     return ret;
3499 }
3500 static int ssv6200_config(struct ieee80211_hw *hw, u32 changed)
3501 {
3502     struct ssv_softc *sc=hw->priv;
3503     int ret=0;
3504     mutex_lock(&sc->mutex);
3505     if (changed & IEEE80211_CONF_CHANGE_PS) {
3506         struct ieee80211_conf *conf = &hw->conf;
3507         if (conf->flags & IEEE80211_CONF_PS) {
3508             printk("Enable IEEE80211_CONF_PS ps_aid=%d\n",sc->ps_aid);
3509         }else{
3510             printk("Disable IEEE80211_CONF_PS ps_aid=%d\n",sc->ps_aid);
3511         }
3512     }
3513 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,0,0)
3514     if (changed & IEEE80211_CONF_CHANGE_QOS) {
3515         struct ieee80211_conf *conf = &hw->conf;
3516         bool qos_active = !!(conf->flags & IEEE80211_CONF_QOS);
3517         SMAC_REG_SET_BITS(sc->sh, ADR_GLBLE_SET,
3518             (qos_active<<QOS_EN_SFT), QOS_EN_MSK);
3519     }
3520 #endif
3521     if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
3522         struct ieee80211_channel *chan;
3523         #if LINUX_VERSION_CODE < KERNEL_VERSION(3,7,0)
3524         chan = hw->conf.channel;
3525         #else
3526         chan = hw->conf.chandef.chan;
3527         #endif
3528 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,0,0)
3529                 {
3530                     struct ieee80211_channel *curchan = hw->conf.channel;
3531                     if(sc->bScanning == true &&
3532                         sc->channel_center_freq != curchan->center_freq && sc->isAssoc){
3533                         hw->conf.flags |= IEEE80211_CONF_OFFCHANNEL;
3534                     }
3535                     else{
3536                         hw->conf.flags &= ~IEEE80211_CONF_OFFCHANNEL;
3537                     }
3538                 }
3539 #endif
3540 #ifdef CONFIG_P2P_NOA
3541         if(sc->p2p_noa.active_noa_vif){
3542             printk("NOA operating-active vif[%02x] skip scan\n", sc->p2p_noa.active_noa_vif);
3543             goto out;
3544         }
3545 #endif
3546         if (hw->conf.flags & IEEE80211_CONF_OFFCHANNEL)
3547         {
3548             if ( (sc->ap_vif == NULL)
3549                 || list_empty(&((struct ssv_vif_priv_data *)sc->ap_vif->drv_priv)->sta_list))
3550             {
3551                 HCI_PAUSE(sc->sh, (TXQ_EDCA_0|TXQ_EDCA_1|TXQ_EDCA_2|TXQ_EDCA_3| TXQ_MGMT));
3552                 sc->sc_flags |= SC_OP_OFFCHAN;
3553                 ssv6xxx_set_channel(sc, chan->hw_value);
3554                 sc->hw_chan = chan->hw_value;
3555                 HCI_RESUME(sc->sh, TXQ_MGMT);
3556             }
3557             else
3558             {
3559                 dev_dbg(sc->dev, "Off-channel to %d is ignored when AP mode enabled.\n", chan->hw_value);
3560             }
3561         }
3562         else {
3563             if ( (sc->cur_channel == NULL)
3564                 || (sc->sc_flags & SC_OP_OFFCHAN)
3565                 || (sc->hw_chan != chan->hw_value))
3566             {
3567                 HCI_PAUSE(sc->sh, (TXQ_EDCA_0|TXQ_EDCA_1|TXQ_EDCA_2|TXQ_EDCA_3| TXQ_MGMT));
3568                 ssv6xxx_set_channel(sc, chan->hw_value);
3569                 sc->cur_channel = chan;
3570                 HCI_RESUME(sc->sh, (TXQ_EDCA_0|TXQ_EDCA_1|TXQ_EDCA_2|TXQ_EDCA_3|TXQ_MGMT));
3571                 sc->sc_flags &= ~SC_OP_OFFCHAN;
3572             }
3573             else
3574             {
3575                 dev_dbg(sc->dev, "Change to the same channel %d\n", chan->hw_value);
3576             }
3577         }
3578     }
3579 #ifdef CONFIG_P2P_NOA
3580 out:
3581 #endif
3582     mutex_unlock(&sc->mutex);
3583     return ret;
3584 }
3585 #if 0
3586 static int sv6200_conf_tx(struct ieee80211_hw *hw,
3587                              struct ieee80211_vif *vif, u16 queue,
3588                              const struct ieee80211_tx_queue_params *params)
3589 {
3590     struct ssv_softc *sc = hw->priv;
3591     u32 cw;
3592     u8 hw_txqid = sc->tx.hw_txqid[queue];
3593     printk("[I] sv6200_conf_tx qos[%d] queue[%d] aifsn[%d] cwmin[%d] cwmax[%d] txop[%d] \n",
3594     vif->bss_conf.qos, queue, params->aifs, params->cw_min, params->cw_max, params->txop);
3595     if (queue > NL80211_TXQ_Q_BK)
3596         return 1;
3597     mutex_lock(&sc->mutex);
3598     #define QOS_EN_MSK 0x00000010
3599     #define QOS_EN_I_MSK 0xffffffef
3600     #define QOS_EN_SFT 4
3601     #define QOS_EN_HI 4
3602     #define QOS_EN_SZ 1
3603     SMAC_REG_SET_BITS(sc->sh, ADR_GLBLE_SET, (vif->bss_conf.qos<<QOS_EN_SFT), QOS_EN_MSK);
3604     cw = params->aifs&0xf;
3605     cw|= ((ilog2(params->cw_min+1))&0xf)<<8;
3606     cw|= ((ilog2(params->cw_max+1))&0xf)<<12;
3607     cw|= ((params->txop)&0xff)<<16;
3608     SMAC_REG_WRITE(sc->sh, ADR_TXQ0_MTX_Q_AIFSN+0x100*hw_txqid, cw);
3609     mutex_unlock(&sc->mutex);
3610     return 0;
3611 }
3612 #endif
3613 #if LINUX_VERSION_CODE < KERNEL_VERSION(4,2,0)
3614 #define SUPPORTED_FILTERS \
3615     (FIF_PROMISC_IN_BSS | \
3616     FIF_ALLMULTI | \
3617     FIF_CONTROL | \
3618     FIF_PSPOLL | \
3619     FIF_OTHER_BSS | \
3620     FIF_BCN_PRBRESP_PROMISC | \
3621     FIF_PROBE_REQ | \
3622     FIF_FCSFAIL)
3623 #else
3624 #define SUPPORTED_FILTERS \
3625     (FIF_ALLMULTI | \
3626     FIF_CONTROL | \
3627     FIF_PSPOLL | \
3628     FIF_OTHER_BSS | \
3629     FIF_BCN_PRBRESP_PROMISC | \
3630     FIF_PROBE_REQ | \
3631     FIF_FCSFAIL)
3632 #endif
3633 static void ssv6200_config_filter(struct ieee80211_hw *hw,
3634     unsigned int changed_flags,
3635     unsigned int *total_flags,
3636     u64 multicast)
3637 {
3638     changed_flags &= SUPPORTED_FILTERS;
3639     *total_flags &= SUPPORTED_FILTERS;
3640 }
3641 static void ssv6200_bss_info_changed(struct ieee80211_hw *hw,
3642         struct ieee80211_vif *vif, struct ieee80211_bss_conf *info,
3643         u32 changed)
3644 {
3645     struct ssv_vif_priv_data *priv_vif = (struct ssv_vif_priv_data *)vif->drv_priv;
3646     struct ssv_softc *sc = hw->priv;
3647 #ifdef CONFIG_P2P_NOA
3648     u8 null_address[6]={0};
3649 #endif
3650     mutex_lock(&sc->mutex);
3651     if (changed & BSS_CHANGED_ERP_PREAMBLE) {
3652         printk("BSS Changed use_short_preamble[%d]\n", info->use_short_preamble);
3653         if (info->use_short_preamble)
3654             sc->sc_flags |= SC_OP_SHORT_PREAMBLE;
3655         else
3656             sc->sc_flags &= ~SC_OP_SHORT_PREAMBLE;
3657     }
3658     if (!priv_vif->vif_idx)
3659     {
3660         if (changed & BSS_CHANGED_BSSID)
3661         {
3662 #ifdef CONFIG_P2P_NOA
3663             struct ssv_vif_priv_data *vif_priv;
3664             vif_priv = (struct ssv_vif_priv_data *)vif->drv_priv;
3665 #endif
3666       ssv62xxx_set_bssid(sc, (u8*)info->bssid);
3667          printk("BSS_CHANGED_BSSID: %02x:%02x:%02x:%02x:%02x:%02x\n",
3668             info->bssid[0], info->bssid[1], info->bssid[2],
3669             info->bssid[3], info->bssid[4], info->bssid[5]);
3670 #ifdef CONFIG_P2P_NOA
3671             if(memcmp(info->bssid, null_address, 6))
3672                 ssv6xxx_noa_hdl_bss_change(sc, MONITOR_NOA_CONF_ADD, vif_priv->vif_idx);
3673             else
3674                 ssv6xxx_noa_hdl_bss_change(sc, MONITOR_NOA_CONF_REMOVE, vif_priv->vif_idx);
3675 #endif
3676         }
3677         if (changed & BSS_CHANGED_ERP_SLOT)
3678         {
3679             u32 regval=0;
3680             printk("BSS_CHANGED_ERP_SLOT: use_short_slot[%d]\n", info->use_short_slot);
3681             if (info->use_short_slot)
3682             {
3683                 SMAC_REG_READ(sc->sh, ADR_MTX_DUR_IFS, &regval);
3684                 regval = regval & MTX_DUR_SLOT_I_MSK;
3685                 regval |= 9 << MTX_DUR_SLOT_SFT;
3686                 SMAC_REG_WRITE(sc->sh, ADR_MTX_DUR_IFS, regval);
3687                 SMAC_REG_READ(sc->sh, ADR_MTX_DUR_SIFS_G, &regval);
3688 #if 1
3689                 regval = regval & MTX_DUR_BURST_SIFS_G_I_MSK;
3690                 regval |= 0xa << MTX_DUR_BURST_SIFS_G_SFT;
3691 #endif
3692                 regval = regval & MTX_DUR_SLOT_G_I_MSK;
3693                 regval |= 9 << MTX_DUR_SLOT_G_SFT;
3694                 SMAC_REG_WRITE(sc->sh, ADR_MTX_DUR_SIFS_G, regval);
3695             }
3696             else
3697             {
3698                 SMAC_REG_READ(sc->sh, ADR_MTX_DUR_IFS, &regval);
3699                 regval = regval & MTX_DUR_SLOT_I_MSK;
3700                 regval |= 20 << MTX_DUR_SLOT_SFT;
3701                 SMAC_REG_WRITE(sc->sh, ADR_MTX_DUR_IFS, regval);
3702                 SMAC_REG_READ(sc->sh, ADR_MTX_DUR_SIFS_G, &regval);
3703 #if 1
3704                 regval = regval & MTX_DUR_BURST_SIFS_G_I_MSK;
3705                 regval |= 0xa << MTX_DUR_BURST_SIFS_G_SFT;
3706 #endif
3707                 regval = regval & MTX_DUR_SLOT_G_I_MSK;
3708                 regval |= 20 << MTX_DUR_SLOT_G_SFT;
3709                 SMAC_REG_WRITE(sc->sh, ADR_MTX_DUR_SIFS_G, regval);
3710             }
3711         }
3712     }
3713     if (changed & BSS_CHANGED_HT) {
3714         printk("BSS_CHANGED_HT: Untreated!!\n");
3715     }
3716     if (changed & BSS_CHANGED_BASIC_RATES)
3717     {
3718         printk("ssv6xxx_rc_update_basic_rate!!\n");
3719         ssv6xxx_rc_update_basic_rate(sc, info->basic_rates);
3720     }
3721     if (vif->type == NL80211_IFTYPE_STATION){
3722         printk("NL80211_IFTYPE_STATION!!\n");
3723         if ((changed & BSS_CHANGED_ASSOC) && (vif->p2p == 0)){
3724             sc->isAssoc = info->assoc;
3725             if(!sc->isAssoc){
3726                 sc->channel_center_freq = 0;
3727                 sc->ps_aid = 0;
3728 #ifdef CONFIG_SSV_MRX_EN3_CTRL
3729                 SMAC_REG_WRITE(sc->sh, ADR_MRX_FLT_EN3, 0x0400);
3730 #endif
3731                 SMAC_REG_WRITE(sc->sh, ADR_RX_11B_CCA_CONTROL, 0x0);
3732             }
3733             else{
3734                 struct ieee80211_channel *curchan;
3735                 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,7,0)
3736                 curchan = hw->conf.channel;
3737                 #else
3738                 curchan = hw->conf.chandef.chan;
3739                 #endif
3740                 sc->channel_center_freq = curchan->center_freq;
3741                 printk(KERN_INFO "!!info->aid = %d\n",info->aid);
3742                 sc->ps_aid = info->aid;
3743 #ifdef CONFIG_SSV_MRX_EN3_CTRL
3744                 SMAC_REG_WRITE(sc->sh, ADR_MRX_FLT_EN3, 0x1000);
3745 #endif
3746             }
3747         }
3748 #ifdef CONFIG_SSV_MRX_EN3_CTRL
3749         else if((changed & BSS_CHANGED_ASSOC) && vif->p2p == 1)
3750         {
3751             if(info->assoc)
3752                 SMAC_REG_WRITE(sc->sh, ADR_MRX_FLT_EN3, 0x0400);
3753             else if(sc->ps_aid != 0)
3754                 SMAC_REG_WRITE(sc->sh, ADR_MRX_FLT_EN3, 0x1000);
3755         }
3756 #endif
3757     }
3758     if (vif->type == NL80211_IFTYPE_AP)
3759     {
3760         if (changed & ( BSS_CHANGED_BEACON
3761 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,2,0)
3762                        | BSS_CHANGED_SSID
3763 #endif
3764                        | BSS_CHANGED_BSSID
3765                        | BSS_CHANGED_BASIC_RATES))
3766         {
3767 #ifdef BROADCAST_DEBUG
3768              printk("[A] ssv6200_bss_info_changed:beacon changed\n");
3769 #endif
3770              queue_work(sc->config_wq, &sc->set_tim_work);
3771         }
3772         if (changed & BSS_CHANGED_BEACON_INT)
3773         {
3774             printk("[A] BSS_CHANGED_BEACON_INT beacon_interval(%d)\n", info->beacon_int);
3775             if (sc->beacon_interval != info->beacon_int)
3776             {
3777                 sc->beacon_interval = info->beacon_int;
3778                 ssv6xxx_beacon_set_info(sc, sc->beacon_interval, sc->beacon_dtim_cnt);
3779             }
3780         }
3781         if (changed & BSS_CHANGED_BEACON_ENABLED)
3782         {
3783 #ifdef BEACON_DEBUG
3784             printk("[A] BSS_CHANGED_BEACON_ENABLED (0x%x)\n", info->enable_beacon);
3785 #endif
3786             if (0 != ssv6xxx_beacon_enable(sc, info->enable_beacon))
3787             {
3788                 dev_err(sc->dev, "Beacon enable %d error.\n", info->enable_beacon);
3789             }
3790         }
3791     }
3792     mutex_unlock(&sc->mutex);
3793  printk("[I] %s(): leave\n", __FUNCTION__);
3794 }
3795 static int ssv6200_sta_add(struct ieee80211_hw *hw,
3796                            struct ieee80211_vif *vif,
3797                            struct ieee80211_sta *sta)
3798 {
3799     struct ssv_sta_priv_data *sta_priv_dat=NULL;
3800     struct ssv_softc *sc=hw->priv;
3801     struct ssv_sta_info *sta_info;
3802     u32 reg_wsid[] = {ADR_WSID0, ADR_WSID1};
3803     int s,i;
3804     u32 reg_wsid_tid0[] = {ADR_WSID0_TID0_RX_SEQ, ADR_WSID1_TID0_RX_SEQ};
3805     u32 reg_wsid_tid7[] = {ADR_WSID0_TID7_RX_SEQ, ADR_WSID1_TID7_RX_SEQ};
3806     unsigned long flags;
3807     int ret = 0;
3808     struct ssv_vif_priv_data *vif_priv = (struct ssv_vif_priv_data *)vif->drv_priv;
3809 #ifdef FW_WSID_WATCH_LIST
3810     int fw_sec_caps = SSV6XXX_WSID_SEC_NONE;
3811 #endif
3812     bool tdls_use_sw_cipher = false, tdls_link= false;
3813  printk("[I] %s(): vif[%d] ", __FUNCTION__, vif_priv->vif_idx);
3814     if (sc->force_triger_reset == true)
3815     {
3816         vif_priv->sta_asleep_mask = 0;
3817         do {
3818             spin_lock_irqsave(&sc->ps_state_lock, flags);
3819             for (s=0; s<SSV_NUM_STA; s++, sta_info++)
3820             {
3821                 sta_info = &sc->sta_info[s];
3822                 if ((sta_info->s_flags & STA_FLAG_VALID))
3823                 {
3824                     if (sta_info->sta == sta)
3825                     {
3826                         printk("search stat %02x:%02x:%02x:%02x:%02x:%02x to  wsid=%d\n",
3827                             sta->addr[0], sta->addr[1], sta->addr[2],
3828                             sta->addr[3], sta->addr[4], sta->addr[5], sta_info->hw_wsid);
3829                         spin_unlock_irqrestore(&sc->ps_state_lock, flags);
3830                         return ret;
3831                     }
3832                 }
3833             }
3834             spin_unlock_irqrestore(&sc->ps_state_lock, flags);
3835             if (s >= SSV_NUM_STA)
3836             {
3837                 break;
3838             }
3839         } while(0);
3840     }
3841     do {
3842         spin_lock_irqsave(&sc->ps_state_lock, flags);
3843         if ( !list_empty(&vif_priv->sta_list) && vif->type == NL80211_IFTYPE_STATION){
3844             tdls_link = true;
3845         }
3846         if ((tdls_link) && (vif_priv->pair_cipher != SSV_CIPHER_NONE)
3847             && (vif_priv->pair_cipher != SSV_CIPHER_CCMP)
3848             && (sc->sh->cfg.use_wpa2_only == false)){
3849             tdls_use_sw_cipher = true;
3850         }
3851         #if 1
3852         if (((vif_priv->vif_idx == 0) && (tdls_use_sw_cipher == false))
3853             || sc->sh->cfg.use_wpa2_only)
3854             s = 0;
3855         else
3856             s = 2;
3857         #else
3858         #endif
3859         for (; s < SSV_NUM_STA; s++)
3860         {
3861             sta_info = &sc->sta_info[s];
3862             if ((sta_info->s_flags & STA_FLAG_VALID) == 0)
3863             {
3864                 sta_info->aid = sta->aid;
3865                 sta_info->sta = sta;
3866                 sta_info->vif = vif;
3867                 sta_info->s_flags = STA_FLAG_VALID;
3868                 sta_priv_dat =
3869                     (struct ssv_sta_priv_data *)sta->drv_priv;
3870                 sta_priv_dat->sta_idx = s;
3871                 sta_priv_dat->sta_info = sta_info;
3872                 sta_priv_dat->has_hw_encrypt = false;
3873                 sta_priv_dat->has_hw_decrypt = false;
3874                 sta_priv_dat->need_sw_decrypt = false;
3875                 sta_priv_dat->need_sw_encrypt = false;
3876                 sta_priv_dat->use_mac80211_decrypt = false;
3877                 #ifdef USE_LOCAL_CRYPTO
3878                 sta_priv_dat->crypto_data.ops = NULL;
3879                 sta_priv_dat->crypto_data.priv = NULL;
3880                 #ifdef HAS_CRYPTO_LOCK
3881                 rwlock_init(&sta_priv_dat->crypto_data.lock);
3882                 #endif
3883                 #endif
3884                 if ( (vif_priv->pair_cipher == SSV_CIPHER_WEP40)
3885                     || (vif_priv->pair_cipher == SSV_CIPHER_WEP104))
3886                 {
3887                     #ifdef USE_LOCAL_CRYPTO
3888                     if (vif_priv->crypto_data.ops != NULL)
3889                     {
3890                         sta_priv_dat->crypto_data.ops = vif_priv->crypto_data.ops;
3891                         sta_priv_dat->crypto_data.priv = vif_priv->crypto_data.priv;
3892                     }
3893                     #endif
3894                     sta_priv_dat->has_hw_encrypt = vif_priv->has_hw_encrypt;
3895                     sta_priv_dat->has_hw_decrypt = vif_priv->has_hw_decrypt;
3896                     sta_priv_dat->need_sw_encrypt = vif_priv->need_sw_encrypt;
3897                     sta_priv_dat->need_sw_decrypt = vif_priv->need_sw_decrypt;
3898                 }
3899                 list_add_tail(&sta_priv_dat->list, &vif_priv->sta_list);
3900                 break;
3901             }
3902         }
3903         spin_unlock_irqrestore(&sc->ps_state_lock, flags);
3904         if (s >= SSV_NUM_STA)
3905         {
3906             dev_err(sc->dev, "Number of STA exceeds driver limitation %d\n.", SSV_NUM_STA);
3907             ret = -1;
3908             break;
3909         }
3910         #ifdef CONFIG_SSV6XXX_DEBUGFS
3911         ssv6xxx_debugfs_add_sta(sc, sta_info);
3912         #endif
3913         sta_info->hw_wsid = -1;
3914         if (sta_priv_dat->sta_idx < SSV_NUM_HW_STA)
3915         {
3916             #if 0
3917             SMAC_REG_READ(sc->sh, reg_wsid[s], &reg_val);
3918             if ((reg_val & 0x01) == 0)
3919             {
3920             #endif
3921             SMAC_REG_WRITE(sc->sh, reg_wsid[s]+4, *((u32 *)&sta->addr[0]));
3922             SMAC_REG_WRITE(sc->sh, reg_wsid[s]+8, *((u32 *)&sta->addr[4]));
3923             SMAC_REG_WRITE(sc->sh, reg_wsid[s], 1);
3924             for (i = reg_wsid_tid0[s]; i <= reg_wsid_tid7[s]; i += 4)
3925                  SMAC_REG_WRITE(sc->sh, i, 0);
3926             ssv6xxx_rc_hw_reset(sc, sta_priv_dat->rc_idx, s);
3927             sta_info->hw_wsid = sta_priv_dat->sta_idx;
3928         }
3929         #ifdef FW_WSID_WATCH_LIST
3930         else if ( (vif_priv->vif_idx == 0)
3931                  || sc->sh->cfg.use_wpa2_only
3932                 )
3933         {
3934             sta_info->hw_wsid = sta_priv_dat->sta_idx;
3935         }
3936         #endif
3937 #ifdef SSV6200_ECO
3938         if ((sta_priv_dat->has_hw_encrypt || sta_priv_dat->has_hw_decrypt) &&
3939             ((vif_priv->pair_cipher == SSV_CIPHER_WEP40) || (vif_priv->pair_cipher == SSV_CIPHER_WEP104)))
3940         {
3941             struct ssv_vif_info *vif_info = &sc->vif_info[vif_priv->vif_idx];
3942             struct ssv6xxx_hw_sec *sramKey = &vif_info->sramKey;
3943             _set_wep_hw_crypto_pair_key(sc, vif_info, sta_info, (void*)sramKey);
3944             if (sramKey->sta_key[0].pair_key_idx != 0)
3945             {
3946                 _set_wep_hw_crypto_group_key(sc, vif_info, sta_info, (void*)sramKey);
3947             }
3948         }
3949 #endif
3950         ssv6200_ampdu_tx_add_sta(hw, sta);
3951         #ifdef FW_WSID_WATCH_LIST
3952         if (sta_info->hw_wsid >= SSV_NUM_HW_STA)
3953         {
3954             if (sta_priv_dat->has_hw_decrypt)
3955                 fw_sec_caps = SSV6XXX_WSID_SEC_PAIRWISE;
3956             if (vif_priv->has_hw_decrypt)
3957                 fw_sec_caps |= SSV6XXX_WSID_SEC_GROUP;
3958             hw_update_watch_wsid(sc, sta, sta_info, sta_priv_dat->sta_idx,
3959                 fw_sec_caps, SSV6XXX_WSID_OPS_ADD);
3960         } else if (SSV6200_USE_HW_WSID(sta_priv_dat->sta_idx)) {
3961            hw_update_watch_wsid(sc, sta, sta_info, sta_priv_dat->sta_idx,
3962                 SSV6XXX_WSID_SEC_SW, SSV6XXX_WSID_OPS_HWWSID_PAIRWISE_SET_TYPE);
3963               hw_update_watch_wsid(sc, sta, sta_info, sta_priv_dat->sta_idx,
3964                 SSV6XXX_WSID_SEC_SW, SSV6XXX_WSID_OPS_HWWSID_GROUP_SET_TYPE);
3965         }
3966         #endif
3967         printk("Add %02x:%02x:%02x:%02x:%02x:%02x to VIF %d sw_idx=%d, wsid=%d\n",
3968                sta->addr[0], sta->addr[1], sta->addr[2],
3969                sta->addr[3], sta->addr[4], sta->addr[5],
3970                vif_priv->vif_idx,
3971                sta_priv_dat->sta_idx, sta_info->hw_wsid);
3972     } while (0);
3973     return ret;
3974 }
3975 void ssv6200_rx_flow_check(struct ssv_sta_priv_data *sta_priv_dat,
3976     struct ssv_softc *sc)
3977  {
3978     if (SSV6200_USE_HW_WSID(sta_priv_dat->sta_idx) && (sta_priv_dat->need_sw_decrypt)){
3979         int other_hw_wsid = (sta_priv_dat->sta_idx+ 1) & 1;
3980         struct ssv_sta_info *sta_info = &sc->sta_info[other_hw_wsid];
3981         struct ieee80211_sta *sta = sta_info->sta;
3982         struct ssv_sta_priv_data *sta_priv = (struct ssv_sta_priv_data *) sta->drv_priv;
3983         mutex_lock(&sc->mutex);
3984         if ((sta_info-> s_flags == 0)
3985             || ((sta_info-> s_flags && STA_FLAG_VALID) && (sta_priv->has_hw_decrypt))){
3986             #ifdef CONFIG_SSV_HW_ENCRYPT_SW_DECRYPT
3987                 SMAC_REG_WRITE(sc->sh, ADR_RX_FLOW_DATA, M_ENG_MACRX|(M_ENG_HWHCI<<4));
3988             #else
3989                 SMAC_REG_WRITE(sc->sh, ADR_RX_FLOW_DATA, M_ENG_MACRX|(M_ENG_ENCRYPT_SEC<<4)|(M_ENG_HWHCI<<8));
3990             #endif
3991             printk("redirect Rx flow for sta %d  disconnect\n",sta_priv_dat->sta_idx);
3992         }
3993         mutex_unlock(&sc->mutex);
3994     }
3995 }
3996 static int ssv6200_sta_remove(struct ieee80211_hw *hw,
3997    struct ieee80211_vif *vif,
3998    struct ieee80211_sta *sta)
3999 {
4000     u32 reg_wsid[] = {ADR_WSID0, ADR_WSID1};
4001     struct ssv_sta_priv_data *sta_priv_dat = (struct ssv_sta_priv_data *)sta->drv_priv;
4002     struct ssv_softc *sc = hw->priv;
4003     struct ssv_sta_info *sta_info = sta_priv_dat->sta_info;
4004     unsigned long flags;
4005     u32 bit;
4006     struct ssv_vif_priv_data *priv_vif = (struct ssv_vif_priv_data *)vif->drv_priv;
4007     u8 hw_wsid = -1;
4008     #ifdef USE_LOCAL_CRYPTO
4009     INIT_WRITE_CRYPTO_DATA(crypto_data, &sta_priv_dat->crypto_data);
4010     #endif
4011     BUG_ON(sta_priv_dat->sta_idx >= SSV_NUM_STA);
4012     dev_notice(sc->dev,
4013                "Removing STA %d (%02X:%02X:%02X:%02X:%02X:%02X) from VIF %d\n.",
4014                sta_priv_dat->sta_idx, sta->addr[0], sta->addr[1], sta->addr[2],
4015                sta->addr[3], sta->addr[4], sta->addr[5], priv_vif->vif_idx);
4016     ssv6200_rx_flow_check(sta_priv_dat, sc);
4017     spin_lock_irqsave(&sc->ps_state_lock, flags);
4018     #ifdef CONFIG_SSV6XXX_DEBUGFS
4019     #endif
4020     #ifdef USE_LOCAL_CRYPTO
4021     START_WRITE_CRYPTO_DATA(crypto_data);
4022     if (crypto_data->ops)
4023     {
4024         if ( (priv_vif->crypto_data.ops != crypto_data->ops)
4025             && crypto_data->priv)
4026         {
4027             crypto_data->ops->deinit(crypto_data->priv);
4028             dev_info(sc->dev, "STA releases crypto OK!\n");
4029         }
4030         crypto_data->priv = NULL;
4031         crypto_data->ops = NULL;
4032     }
4033     END_WRITE_CRYPTO_DATA(crypto_data);
4034     #ifdef MULTI_THREAD_ENCRYPT
4035     _clean_up_crypto_skb(sc, sta);
4036     #endif
4037     #endif
4038 #if 0
4039     if ((sc->ps_status == PWRSV_PREPARE)||(sc->ps_status == PWRSV_ENABLE)) {
4040         memset(sta_info, 0, sizeof(*sta_info));
4041         sta_priv_dat->sta_idx = -1;
4042         list_del(&sta_priv_dat->list);
4043         spin_unlock_irqrestore(&sc->ps_state_lock, flags);
4044         return 0;
4045     }
4046 #endif
4047     bit = BIT(sta_priv_dat->sta_idx);
4048     priv_vif->sta_asleep_mask &= ~bit;
4049     if (sta_info->hw_wsid != -1) {
4050 #ifndef FW_WSID_WATCH_LIST
4051         BUG_ON(sta_info->hw_wsid >= SSV_NUM_HW_STA);
4052 #endif
4053         hw_wsid = sta_info->hw_wsid;
4054     }
4055 #ifdef FW_WSID_WATCH_LIST
4056     if (sta_info->hw_wsid >= SSV_NUM_HW_STA)
4057     {
4058         spin_unlock_irqrestore(&sc->ps_state_lock, flags);
4059         hw_update_watch_wsid(sc, sta, sta_info, sta_info->hw_wsid, 0, SSV6XXX_WSID_OPS_DEL);
4060         spin_lock_irqsave(&sc->ps_state_lock, flags);
4061     }
4062 #endif
4063 #if 0
4064     printk("%s(): sw_idx=%d, hw_idx=%d sta_asleep_mask[%08x]\n", __FUNCTION__,
4065         sta_priv_dat->sta_idx , sta_info->hw_wsid, sc->sta_asleep_mask);
4066     printk("Remove %02x:%02x:%02x:%02x:%02x:%02x to sw_idx=%d, wsid=%d\n",
4067     sta->addr[0], sta->addr[1], sta->addr[2],
4068     sta->addr[3], sta->addr[4], sta->addr[5], sta_priv_dat->sta_idx, sta_info->hw_wsid);
4069 #endif
4070     #ifdef CONFIG_SSV6XXX_DEBUGFS
4071     {
4072     ssv6xxx_debugfs_remove_sta(sc, sta_info);
4073     }
4074     #endif
4075     memset(sta_info, 0, sizeof(*sta_info));
4076     sta_priv_dat->sta_idx = -1;
4077     list_del(&sta_priv_dat->list);
4078     if (list_empty(&priv_vif->sta_list) && vif->type == NL80211_IFTYPE_STATION)
4079     {
4080         priv_vif->pair_cipher = 0;
4081         priv_vif->group_cipher = 0;
4082     }
4083     spin_unlock_irqrestore(&sc->ps_state_lock, flags);
4084 #if 0
4085     sta_info = sc->sta_info;
4086     for(s=0; s<SSV_NUM_STA; s++, sta_info++) {
4087         if (sta_info->s_flags & STA_FLAG_VALID)
4088             continue;
4089         if (sta_info->sta == sta &&
4090             sta_info->vif == vif)
4091     sta_info->s_flags = 0;
4092     }
4093 #endif
4094 #ifndef FW_WSID_WATCH_LIST
4095     if(hw_wsid != -1)
4096 #else
4097     if((hw_wsid != -1) && (hw_wsid < SSV_NUM_HW_STA))
4098 #endif
4099         SMAC_REG_WRITE(sc->sh, reg_wsid[hw_wsid], 0x00);
4100     return 0;
4101 }
4102 static void ssv6200_sta_notify(struct ieee80211_hw *hw,
4103                                  struct ieee80211_vif *vif,
4104                                  enum sta_notify_cmd cmd,
4105                                  struct ieee80211_sta *sta)
4106 {
4107     struct ssv_softc *sc = hw->priv;
4108     struct ssv_vif_priv_data *priv_vif = (struct ssv_vif_priv_data *)vif->drv_priv;
4109     struct ssv_sta_priv_data *sta_priv_dat =
4110         sta != NULL ? (struct ssv_sta_priv_data *)sta->drv_priv : NULL;
4111     struct ssv_sta_info *sta_info;
4112     u32 bit, prev;
4113     unsigned long flags;
4114 #ifdef BROADCAST_DEBUG
4115 #endif
4116     spin_lock_irqsave(&sc->ps_state_lock, flags);
4117     if (sta_priv_dat != NULL){
4118         bit = BIT(sta_priv_dat->sta_idx);
4119      prev = priv_vif->sta_asleep_mask & bit;
4120      sta_info = sta_priv_dat->sta_info;
4121         switch (cmd)
4122         {
4123             case STA_NOTIFY_SLEEP:
4124                 if(!prev)
4125                 {
4126                     sta_info->sleeping = true;
4127                     if ( (vif->type == NL80211_IFTYPE_AP)
4128                         && sc->bq4_dtim
4129                         && !priv_vif->sta_asleep_mask
4130                         && ssv6200_bcast_queue_len(&sc->bcast_txq)){
4131                         printk("%s(): ssv6200_bcast_start\n", __FUNCTION__);
4132                         ssv6200_bcast_start(sc);
4133                     }
4134         priv_vif->sta_asleep_mask |= bit;
4135                 }
4136                 break;
4137             case STA_NOTIFY_AWAKE:
4138                 if(prev)
4139                 {
4140                     sta_info->sleeping = false;
4141         priv_vif->sta_asleep_mask &= ~bit;
4142                 }
4143                 break;
4144             default:
4145                 break;
4146         }
4147     }
4148     spin_unlock_irqrestore(&sc->ps_state_lock, flags);
4149 }
4150 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,2,0)
4151 static u64 ssv6200_get_tsf(struct ieee80211_hw *hw)
4152 #else
4153 static u64 ssv6200_get_tsf(struct ieee80211_hw *hw,
4154                            struct ieee80211_vif *vif)
4155 #endif
4156 {
4157     u64 time = jiffies*1000*1000/HZ;
4158     //struct timeval tv;
4159 
4160     //do_gettimeofday(&tv);
4161     //time = tv.tv_sec * 1000 * 1000 + tv.tv_usec;
4162     printk("%s(): time = %llu\n", __FUNCTION__, time);
4163     return time;
4164     //return 0;
4165 }
4166 
4167 static u64 ssv6200_get_systime_us(void)
4168 {
4169 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 39))
4170 	struct timespec ts;
4171 	get_monotonic_boottime(&ts);
4172 	return ((u64)ts.tv_sec * 1000000) + ts.tv_nsec / 1000;
4173 #else
4174 	struct timeval tv;
4175 	do_gettimeofday(&tv);
4176 	return ((u64)tv.tv_sec * 1000000) + tv.tv_usec;
4177 #endif
4178 }
4179 
4180 static u32 pre_11b_cca_control;
4181 static u32 pre_11b_cca_1;
4182 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,19,0)
4183 static void ssv6200_sw_scan_start(struct ieee80211_hw *hw,
4184          struct ieee80211_vif *vif, const u8 *mac_addr)
4185 #else
4186 static void ssv6200_sw_scan_start(struct ieee80211_hw *hw)
4187 #endif
4188 {
4189 //#if LINUX_VERSION_CODE < KERNEL_VERSION(3,0,0)
4190     ((struct ssv_softc *)(hw->priv))->bScanning = true;
4191 //#endif
4192  SMAC_REG_READ(((struct ssv_softc *)(hw->priv))->sh, ADR_RX_11B_CCA_CONTROL, &pre_11b_cca_control);
4193     SMAC_REG_WRITE(((struct ssv_softc *)(hw->priv))->sh, ADR_RX_11B_CCA_CONTROL, 0x0);
4194  SMAC_REG_READ(((struct ssv_softc *)(hw->priv))->sh, ADR_RX_11B_CCA_1, &pre_11b_cca_1);
4195  SMAC_REG_WRITE(((struct ssv_softc *)(hw->priv))->sh, ADR_RX_11B_CCA_1, RX_11B_CCA_IN_SCAN);
4196 #ifdef CONFIG_SSV_MRX_EN3_CTRL
4197     SMAC_REG_WRITE(((struct ssv_softc *)(hw->priv))->sh, ADR_MRX_FLT_EN3, 0x0400);
4198 #endif
4199     //printk("--------------%s(): \n", __FUNCTION__);
4200 }
4201 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,19,0)
4202 static void ssv6200_sw_scan_complete(struct ieee80211_hw *hw,
4203          struct ieee80211_vif *vif)
4204 #else
4205 static void ssv6200_sw_scan_complete(struct ieee80211_hw *hw)
4206 #endif
4207 {
4208 
4209 #ifdef CONFIG_SSV_MRX_EN3_CTRL
4210     bool is_p2p_assoc;
4211 #endif
4212 	((struct ssv_softc *)(hw->priv))->bScanning = false;
4213     SMAC_REG_WRITE(((struct ssv_softc *)(hw->priv))->sh, ADR_RX_11B_CCA_CONTROL, pre_11b_cca_control);
4214     SMAC_REG_WRITE(((struct ssv_softc *)(hw->priv))->sh, ADR_RX_11B_CCA_1, pre_11b_cca_1);
4215 #ifdef CONFIG_SSV_MRX_EN3_CTRL
4216     is_p2p_assoc = ((struct ssv_softc *)(hw->priv))->vif_info[1].vif->bss_conf.assoc;
4217     if(((struct ssv_softc *)(hw->priv))->ps_aid != 0 && (!is_p2p_assoc))
4218     SMAC_REG_WRITE(((struct ssv_softc *)(hw->priv))->sh, ADR_MRX_FLT_EN3, 0x1000);
4219 #endif
4220     //printk("==============%s(): \n", __FUNCTION__);
4221 }
4222 static int ssv6200_set_tim(struct ieee80211_hw *hw, struct ieee80211_sta *sta,
4223                             bool set)
4224 {
4225     struct ssv_softc *sc = hw->priv;
4226     struct ssv_sta_info *sta_info = sta
4227                                     ? ((struct ssv_sta_priv_data *)sta->drv_priv)->sta_info
4228                                     : NULL;
4229     if (sta_info && (sta_info->tim_set^set))
4230     {
4231 #ifdef BROADCAST_DEBUG
4232            printk("[I] [A] ssvcabrio_set_tim");
4233 #endif
4234         sta_info->tim_set = set;
4235         queue_work(sc->config_wq, &sc->set_tim_work);
4236     }
4237     return 0;
4238 }
4239 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,2,0)
4240 static int ssv6200_conf_tx(struct ieee80211_hw *hw, u16 queue,
4241            const struct ieee80211_tx_queue_params *params)
4242 #else
4243 static int ssv6200_conf_tx(struct ieee80211_hw *hw,
4244                              struct ieee80211_vif *vif, u16 queue,
4245                              const struct ieee80211_tx_queue_params *params)
4246 #endif
4247 {
4248     struct ssv_softc *sc = hw->priv;
4249     u32 cw;
4250     u8 hw_txqid = sc->tx.hw_txqid[queue];
4251 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,2,0)
4252     struct ssv_vif_priv_data *priv_vif = (struct ssv_vif_priv_data *)vif->drv_priv;
4253     printk("[I] sv6200_conf_tx vif[%d] qos[%d] queue[%d] aifsn[%d] cwmin[%d] cwmax[%d] txop[%d] \n",
4254   priv_vif->vif_idx ,vif->bss_conf.qos, queue, params->aifs, params->cw_min, params->cw_max, params->txop);
4255 #else
4256     printk("[I] sv6200_conf_tx queue[%d] aifsn[%d] cwmin[%d] cwmax[%d] txop[%d] \n",
4257         queue, params->aifs, params->cw_min, params->cw_max, params->txop);
4258 #endif
4259     if (queue > NL80211_TXQ_Q_BK)
4260         return 1;
4261 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,2,0)
4262     if (priv_vif->vif_idx != 0) {
4263         dev_warn(sc->dev, "WMM setting applicable to primary interface only.\n");
4264         return 1;
4265     }
4266 #endif
4267     mutex_lock(&sc->mutex);
4268 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,2,0)
4269     SMAC_REG_SET_BITS(sc->sh, ADR_GLBLE_SET,
4270             (vif->bss_conf.qos<<QOS_EN_SFT), QOS_EN_MSK);
4271 #endif
4272 #if 0
4273     {
4274         cw = 0x4;
4275         SMAC_REG_WRITE(sc->sh, ADR_TXQ0_MTX_Q_MISC_EN+0x100*hw_txqid, cw);
4276         cw = 0x0;
4277         SMAC_REG_WRITE(sc->sh, ADR_TXQ0_MTX_Q_BKF_CNT+0x100*hw_txqid, cw);
4278     }
4279 #endif
4280 #if 1
4281     cw = (params->aifs-1)&0xf;
4282 #else
4283     cw = params->aifs&0xf;
4284 #endif
4285     cw|= ((ilog2(params->cw_min+1))&0xf)<<TXQ1_MTX_Q_ECWMIN_SFT;
4286     cw|= ((ilog2(params->cw_max+1))&0xf)<<TXQ1_MTX_Q_ECWMAX_SFT;
4287     cw|= ((params->txop)&0xff)<<TXQ1_MTX_Q_TXOP_LIMIT_SFT;
4288     SMAC_REG_WRITE(sc->sh, ADR_TXQ0_MTX_Q_AIFSN+0x100*hw_txqid, cw);
4289     mutex_unlock(&sc->mutex);
4290     return 0;
4291 }
4292 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,0,0)
4293 static int ssv6200_ampdu_action(struct ieee80211_hw *hw,
4294               struct ieee80211_vif *vif,
4295               enum ieee80211_ampdu_mlme_action action,
4296               struct ieee80211_sta *sta,
4297               u16 tid, u16 *ssn)
4298 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(3,0,0) && LINUX_VERSION_CODE < KERNEL_VERSION(4,4,0)
4299 static int ssv6200_ampdu_action(struct ieee80211_hw *hw,
4300               struct ieee80211_vif *vif,
4301               enum ieee80211_ampdu_mlme_action action,
4302               struct ieee80211_sta *sta,
4303               u16 tid, u16 *ssn, u8 buf_size)
4304 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(4,4,0) && LINUX_VERSION_CODE < KERNEL_VERSION(4,4,69)
4305 static int ssv6200_ampdu_action(struct ieee80211_hw *hw,
4306               struct ieee80211_vif *vif,
4307               enum ieee80211_ampdu_mlme_action action,
4308               struct ieee80211_sta *sta,
4309               u16 tid, u16 *ssn, u8 buf_size, bool amsdu)
4310 #else
4311 static int ssv6200_ampdu_action(struct ieee80211_hw *hw,
4312               struct ieee80211_vif *vif,
4313               struct ieee80211_ampdu_params *params)
4314 #endif
4315 {
4316 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,0,0)
4317     u8 buf_size = 32;
4318 #endif
4319     struct ssv_softc *sc = hw->priv;
4320     int ret = 0;
4321 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4,4,69)
4322     struct ieee80211_sta *sta = params->sta;
4323     enum ieee80211_ampdu_mlme_action action = params->action;
4324     u16 tid = params->tid;
4325     u16 *ssn = &(params->ssn);
4326     u8 buf_size = params->buf_size;
4327 #endif
4328     if(sta == NULL)
4329         return ret;
4330     #if (!Enable_AMPDU_Rx)
4331     if(action == IEEE80211_AMPDU_RX_START || action == IEEE80211_AMPDU_RX_STOP )
4332     {
4333         ampdu_db_log("Disable AMPDU_RX for test(1).\n");
4334         return -EOPNOTSUPP;
4335     }
4336     #endif
4337     #if (!Enable_AMPDU_Tx)
4338     if(action == IEEE80211_AMPDU_TX_START || action == IEEE80211_AMPDU_TX_STOP || action == IEEE80211_AMPDU_TX_OPERATIONAL )
4339     {
4340         ampdu_db_log("Disable AMPDU_TX for test(1).\n");
4341         return -EOPNOTSUPP;
4342     }
4343     #endif
4344     if((action == IEEE80211_AMPDU_RX_START || action == IEEE80211_AMPDU_RX_STOP ) &&
4345         (!(sc->sh->cfg.hw_caps & SSV6200_HW_CAP_AMPDU_RX)))
4346     {
4347         ampdu_db_log("Disable AMPDU_RX(2).\n");
4348         return -EOPNOTSUPP;
4349     }
4350     if( ( action == IEEE80211_AMPDU_TX_START
4351            #if LINUX_VERSION_CODE < KERNEL_VERSION(3,7,0)
4352            || action == IEEE80211_AMPDU_TX_STOP
4353            #else
4354            || action == IEEE80211_AMPDU_TX_STOP_CONT
4355            || action == IEEE80211_AMPDU_TX_STOP_FLUSH
4356            || action == IEEE80211_AMPDU_TX_STOP_FLUSH_CONT
4357            #endif
4358            || action == IEEE80211_AMPDU_TX_OPERATIONAL )
4359        && (!(sc->sh->cfg.hw_caps & SSV6200_HW_CAP_AMPDU_TX)))
4360     {
4361         ampdu_db_log("Disable AMPDU_TX(2).\n");
4362         return -EOPNOTSUPP;
4363     }
4364     switch (action)
4365     {
4366         case IEEE80211_AMPDU_RX_START:
4367 #ifdef WIFI_CERTIFIED
4368             if (sc->rx_ba_session_count >= SSV6200_RX_BA_MAX_SESSIONS)
4369             {
4370 #if LINUX_VERSION_CODE > KERNEL_VERSION(3,1,0)
4371                 ieee80211_stop_rx_ba_session(vif,
4372            (1<<(sc->ba_tid)),
4373            sc->ba_ra_addr);
4374 #endif
4375                 sc->rx_ba_session_count--;
4376             }
4377 #else
4378             if ((sc->rx_ba_session_count >= SSV6200_RX_BA_MAX_SESSIONS) && (sc->rx_ba_sta != sta))
4379             {
4380                 ret = -EBUSY;
4381                 break;
4382             }
4383             else if ((sc->rx_ba_session_count >= SSV6200_RX_BA_MAX_SESSIONS) && (sc->rx_ba_sta == sta))
4384             {
4385 #if LINUX_VERSION_CODE > KERNEL_VERSION(3,1,0)
4386                 ieee80211_stop_rx_ba_session(vif,(1<<(sc->ba_tid)),sc->ba_ra_addr);
4387 #endif
4388                 sc->rx_ba_session_count--;
4389             }
4390 #endif
4391             printk(KERN_ERR "IEEE80211_AMPDU_RX_START %02X:%02X:%02X:%02X:%02X:%02X %d.\n",
4392                    sta->addr[0], sta->addr[1], sta->addr[2], sta->addr[3],
4393                    sta->addr[4], sta->addr[5], tid);
4394             sc->rx_ba_session_count++;
4395             sc->rx_ba_sta = sta;
4396             sc->ba_tid = tid;
4397             sc->ba_ssn = *ssn;
4398             memcpy(sc->ba_ra_addr, sta->addr, ETH_ALEN);
4399             queue_work(sc->config_wq, &sc->set_ampdu_rx_add_work);
4400             break;
4401         case IEEE80211_AMPDU_RX_STOP:
4402             sc->rx_ba_session_count--;
4403             if (sc->rx_ba_session_count == 0)
4404                 sc->rx_ba_sta = NULL;
4405             queue_work(sc->config_wq, &sc->set_ampdu_rx_del_work);
4406             break;
4407         case IEEE80211_AMPDU_TX_START:
4408             printk(KERN_ERR "AMPDU_TX_START %02X:%02X:%02X:%02X:%02X:%02X %d.\n",
4409                    sta->addr[0], sta->addr[1], sta->addr[2], sta->addr[3],
4410                    sta->addr[4], sta->addr[5], tid);
4411             ssv6200_ampdu_tx_start(tid, sta, hw, ssn);
4412             ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
4413             break;
4414         #if LINUX_VERSION_CODE < KERNEL_VERSION(3,7,0)
4415         case IEEE80211_AMPDU_TX_STOP:
4416         #else
4417         case IEEE80211_AMPDU_TX_STOP_CONT:
4418         case IEEE80211_AMPDU_TX_STOP_FLUSH:
4419         case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
4420         #endif
4421             printk(KERN_ERR "AMPDU_TX_STOP %02X:%02X:%02X:%02X:%02X:%02X %d.\n",
4422                    sta->addr[0], sta->addr[1], sta->addr[2], sta->addr[3],
4423                    sta->addr[4], sta->addr[5], tid);
4424             ssv6200_ampdu_tx_stop(tid, sta, hw);
4425             ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
4426             break;
4427         case IEEE80211_AMPDU_TX_OPERATIONAL:
4428             printk(KERN_ERR "AMPDU_TX_OPERATIONAL %02X:%02X:%02X:%02X:%02X:%02X %d.\n",
4429                    sta->addr[0], sta->addr[1], sta->addr[2], sta->addr[3],
4430                    sta->addr[4], sta->addr[5], tid);
4431             ssv6200_ampdu_tx_operation(tid, sta, hw, buf_size);
4432             break;
4433         default:
4434             ret = -EOPNOTSUPP;
4435             break;
4436     }
4437     return ret;
4438 }
4439 #ifdef CONFIG_PM
4440 int ssv6xxx_suspend (struct ieee80211_hw *hw, struct cfg80211_wowlan *wowlan)
4441 {
4442     printk("ssv6xxx_suspend \n");
4443     return 0;
4444 }
4445 int ssv6xxx_resume (struct ieee80211_hw *hw)
4446 {
4447     printk("ssv6xxx_resume \n");
4448     return 0;
4449 }
4450 #endif
4451 struct ieee80211_ops ssv6200_ops =
4452 {
4453     .tx = ssv6200_tx,
4454     .start = ssv6200_start,
4455     .stop = ssv6200_stop,
4456     .add_interface = ssv6200_add_interface,
4457     .remove_interface = ssv6200_remove_interface,
4458     .change_interface = ssv6200_change_interface,
4459     .config = ssv6200_config,
4460     .configure_filter = ssv6200_config_filter,
4461     .bss_info_changed = ssv6200_bss_info_changed,
4462     .sta_add = ssv6200_sta_add,
4463     .sta_remove = ssv6200_sta_remove,
4464     .sta_notify = ssv6200_sta_notify,
4465     .set_key = ssv6200_set_key,
4466     .sw_scan_start = ssv6200_sw_scan_start,
4467     .sw_scan_complete = ssv6200_sw_scan_complete,
4468     .get_tsf = ssv6200_get_tsf,
4469     .set_tim = ssv6200_set_tim,
4470     .conf_tx = ssv6200_conf_tx,
4471     .ampdu_action = ssv6200_ampdu_action,
4472 #ifdef CONFIG_PM
4473     .suspend = ssv6xxx_suspend,
4474     .resume = ssv6xxx_resume,
4475 #endif
4476 };
4477 #ifdef USE_LOCAL_CRYPTO
4478 #ifdef MULTI_THREAD_ENCRYPT
4479 struct ssv_crypto_data * ssv6xxx_skb_get_tx_cryptops(struct sk_buff *mpdu)
4480 {
4481     struct ieee80211_tx_info *info = IEEE80211_SKB_CB(mpdu);
4482     struct SKB_info_st *skb_info = (struct SKB_info_st *)mpdu->head;
4483     struct ieee80211_sta *sta = skb_info->sta;
4484     struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)mpdu->data;
4485     u32 unicast = (is_multicast_ether_addr(hdr->addr1))? 0: 1;
4486     struct ssv_sta_priv_data *sta_priv_dat = NULL;
4487     BUG_ON((size_t)info < (size_t)0x01000);
4488     if (unicast)
4489     {
4490         if (sta)
4491         {
4492             sta_priv_dat = (struct ssv_sta_priv_data *)sta->drv_priv;
4493             return &sta_priv_dat->crypto_data;
4494         }
4495         else
4496         {
4497             printk(KERN_ERR
4498                    "Unicast to NULL STA frame. "
4499                    "%02X:%02X:%02X:%02X:%02X:%02X -> %02X:%02X:%02X:%02X:%02X:%02X)\n",
4500                    hdr->addr1[0], hdr->addr1[1], hdr->addr1[2],
4501                    hdr->addr1[3], hdr->addr1[4], hdr->addr1[5],
4502                    hdr->addr2[0], hdr->addr2[1], hdr->addr2[2],
4503                    hdr->addr2[3], hdr->addr2[4], hdr->addr2[5]);
4504             return NULL;
4505         }
4506     }
4507     else
4508     {
4509         struct ssv_vif_priv_data *vif_priv = (struct ssv_vif_priv_data *)info->control.vif->drv_priv;
4510         return &vif_priv->crypto_data;
4511     }
4512 }
4513 int ssv6xxx_skb_pre_encrypt(struct sk_buff *mpdu, struct ssv_softc *sc)
4514 {
4515     struct ssv_crypto_data *crypto_data = NULL;
4516     struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)mpdu->data;
4517     int ret = -1;
4518     crypto_data = ssv6xxx_skb_get_tx_cryptops(mpdu);
4519     if (crypto_data == NULL)
4520         return -EOPNOTSUPP;
4521     START_READ_CRYPTO_DATA(crypto_data);
4522     if ((crypto_data->ops != NULL) && (crypto_data->ops->encrypt_prepare != NULL))
4523     {
4524         u32 hdrlen = ieee80211_hdrlen(hdr->frame_control);
4525         ret = crypto_data->ops->encrypt_prepare(mpdu, hdrlen, crypto_data->priv);
4526     }
4527     else
4528     {
4529         ret = -EOPNOTSUPP;
4530     }
4531     END_READ_CRYPTO_DATA(crypto_data);
4532     return ret;
4533 }
4534 int ssv6xxx_skb_pre_decrypt(struct sk_buff *mpdu, struct ieee80211_sta *sta, struct ssv_softc *sc)
4535 {
4536     struct ssv_crypto_data *crypto_data = NULL;
4537     struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)mpdu->data;
4538     int ret = -1;
4539     crypto_data = ssv6xxx_skb_get_rx_cryptops(sc, sta, mpdu);
4540     if (crypto_data == NULL)
4541         return -EOPNOTSUPP;
4542     START_READ_CRYPTO_DATA(crypto_data);
4543     if ((crypto_data->ops != NULL) && (crypto_data->ops->decrypt_prepare != NULL))
4544     {
4545         u32 hdrlen = ieee80211_hdrlen(hdr->frame_control);
4546         ret = crypto_data->ops->decrypt_prepare(mpdu, hdrlen, crypto_data->priv);
4547     }
4548     else
4549     {
4550         ret = -EOPNOTSUPP;
4551     }
4552     END_READ_CRYPTO_DATA(crypto_data);
4553     return ret;
4554 }
4555 #endif
4556 int ssv6xxx_skb_encrypt(struct sk_buff *mpdu, struct ssv_softc *sc)
4557 {
4558     struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)mpdu->data;
4559  int ret = 0;
4560 #ifndef MULTI_THREAD_ENCRYPT
4561     struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(mpdu);
4562     struct SKB_info_st *skb_info = (struct SKB_info_st *)mpdu->head;
4563     struct ieee80211_sta *sta = skb_info->sta;
4564     struct ssv_sta_priv_data *sta_priv_dat = NULL;
4565 #endif
4566     struct ssv_crypto_data *crypto_data = NULL;
4567 #ifndef MULTI_THREAD_ENCRYPT
4568     if (sta || unicast)
4569     {
4570         sta_priv_dat = (struct ssv_sta_priv_data *)sta->drv_priv;
4571         crypto_data = &sta_priv_data->crypto_data;
4572     }
4573     else
4574     {
4575         struct ssv_vif_priv_data *vif_priv = (struct ssv_vif_priv_data *)tx_info->control.vif->drv_priv;
4576         crypto_data = &vif_priv->crypto_data;
4577     }
4578 #else
4579     crypto_data = ssv6xxx_skb_get_tx_cryptops(mpdu);
4580 #endif
4581     START_READ_CRYPTO_DATA(crypto_data);
4582     if ((crypto_data == NULL) || (crypto_data->ops == NULL) || (crypto_data->priv == NULL))
4583     {
4584         #if 0
4585         u32 unicast = (is_multicast_ether_addr(hdr->addr1))? 0: 1;
4586         dev_err(sc->dev, "[Local Crypto]: Encrypt %c %d %02X:%02X:%02X:%02X:%02X:%02X with NULL crypto.\n",
4587                 unicast ? 'U' : 'B', mpdu->protocol,
4588                 hdr->addr1[0], hdr->addr1[1], hdr->addr1[2],
4589                 hdr->addr1[3], hdr->addr1[4], hdr->addr1[5]);
4590         #endif
4591         ret = -1;
4592     }
4593     else
4594     {
4595         u32 hdrlen = ieee80211_hdrlen(hdr->frame_control);
4596         ret = crypto_data->ops->encrypt_mpdu(mpdu, hdrlen, crypto_data->priv);
4597     }
4598     END_READ_CRYPTO_DATA(crypto_data);
4599  return ret;
4600 }
4601 struct ssv_crypto_data *ssv6xxx_skb_get_rx_cryptops(struct ssv_softc *sc, struct ieee80211_sta *sta, struct sk_buff *mpdu)
4602 {
4603     struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)mpdu->data;
4604     struct ssv_sta_priv_data *sta_priv;
4605     struct ssv_vif_priv_data *vif_priv;
4606     struct ssv_vif_info *vif_info;
4607     u32 unicast = 0;
4608     if(sta == NULL)
4609     {
4610         printk("No sta, fail to get rx cryptops\n");
4611         return NULL;
4612     }
4613     sta_priv = (struct ssv_sta_priv_data *)sta->drv_priv;
4614     vif_priv = (struct ssv_vif_priv_data *)sta_priv->sta_info->vif->drv_priv;
4615     vif_info = &sc->vif_info[vif_priv->vif_idx];
4616     if (vif_info->if_type == NL80211_IFTYPE_STATION)
4617         unicast = (is_multicast_ether_addr(hdr->addr1))?0:1;
4618     if((sta->drv_priv != NULL) && (vif_info->if_type == NL80211_IFTYPE_AP))
4619     {
4620         return &sta_priv->crypto_data;
4621     }
4622     else if((sta->drv_priv != NULL) && (unicast == 1))
4623     {
4624         return &sta_priv->crypto_data;
4625     }
4626     else if((unicast != 1) && (vif_priv != NULL))
4627     {
4628         return &vif_priv->crypto_data;
4629     }
4630     else
4631     {
4632         printk("[Local Crypto]: No useful drv_priv, sta = %p, unicast = %d, vif_priv = %p", sta, unicast, vif_priv);
4633         if(sta != NULL)
4634             printk(", sta_priv = %p", sta->drv_priv);
4635         printk("\n");
4636     }
4637     return NULL;
4638 }
4639 int ssv6xxx_skb_decrypt(struct sk_buff *mpdu, struct ieee80211_sta *sta, struct ssv_softc *sc)
4640 {
4641     struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)mpdu->data;
4642     struct ssv_crypto_data *crypto_data = NULL;
4643     u32 hdrlen = ieee80211_hdrlen(hdr->frame_control);
4644     crypto_data = ssv6xxx_skb_get_rx_cryptops(sc, sta, mpdu);
4645     if (crypto_data != NULL)
4646     {
4647         int ret = -1;
4648         START_READ_CRYPTO_DATA(crypto_data);
4649         if ((crypto_data->ops != NULL) && (crypto_data->priv != NULL))
4650             ret = crypto_data->ops->decrypt_mpdu(mpdu, hdrlen, crypto_data->priv);
4651         END_READ_CRYPTO_DATA(crypto_data);
4652         return ret;
4653     }
4654  printk("[Local Crypto]: crytp is null\n");
4655     return -1;
4656 }
4657 #endif
4658 int ssv6200_tx_flow_control(void *dev, int hw_txqid, bool fc_en,int debug)
4659 {
4660     struct ssv_softc *sc=dev;
4661     int ac;
4662     BUG_ON(hw_txqid > 4);
4663     if (hw_txqid == 4)
4664         return 0;
4665     ac = sc->tx.ac_txqid[hw_txqid];
4666     if (fc_en == false) {
4667         if (sc->tx.flow_ctrl_status & (1<<ac)) {
4668                 ieee80211_wake_queue(sc->hw, ac);
4669                 sc->tx.flow_ctrl_status &= ~(1<<ac);
4670         } else {
4671         }
4672     }
4673     else {
4674         if ((sc->tx.flow_ctrl_status & (1<<ac))==0) {
4675             ieee80211_stop_queue(sc->hw, ac);
4676             sc->tx.flow_ctrl_status |= (1<<ac);
4677         } else {
4678         }
4679     }
4680     return 0;
4681 }
4682 void ssv6xxx_tx_q_empty_cb (u32 txq_no, void *cb_data)
4683 {
4684     struct ssv_softc *sc = cb_data;
4685     BUG_ON(sc == NULL);
4686     sc->tx_q_empty = true;
4687     smp_mb();
4688     wake_up_interruptible(&sc->tx_wait_q);
4689 }
4690 struct ssv6xxx_b_cca_control {
4691     u32 down_level;
4692     u32 upper_level;
4693  u32 adjust_cca_control;
4694     u32 adjust_cca_1;
4695 };
4696 struct ssv6xxx_b_cca_control adjust_cci[] = {
4697     { 0 , 43, 0x00162000, 0x20380050},
4698     { 40, 48, 0x00161000, 0x20380050},
4699     { 45, 53, 0x00160800, 0x20380050},
4700     { 50, 63, 0x00160400, 0x20380050},
4701     { 60, 68, 0x00160200, 0x20380050},
4702     { 65, 73, 0x00160100, 0x20380050},
4703     { 70, 128,0x00000000, 0x20300050},
4704 };
4705 #define MAX_CCI_LEVEL 128
4706 static unsigned long last_jiffies = INITIAL_JIFFIES;
4707 static s32 size = sizeof(adjust_cci)/sizeof(adjust_cci[0]);
4708 static u32 current_level = MAX_CCI_LEVEL;
4709 static u32 current_gate = (sizeof(adjust_cci)/sizeof(adjust_cci[0])) - 1;
4710 void mitigate_cci(struct ssv_softc *sc, u32 input_level)
4711 {
4712  s32 i;
4713     if(input_level > MAX_CCI_LEVEL) {
4714         printk("mitigate_cci input error[%d]!!\n",input_level);
4715         return;
4716     }
4717     if (time_after(jiffies, last_jiffies + msecs_to_jiffies(3000))) {
4718 #ifdef DEBUG_MITIGATE_CCI
4719   printk("jiffies=%lu, input_level=%d\n", jiffies, input_level);
4720 #endif
4721   last_jiffies = jiffies;
4722   if(( input_level >= adjust_cci[current_gate].down_level) && (input_level <= adjust_cci[current_gate].upper_level)) {
4723             current_level = input_level;
4724 #ifdef DEBUG_MITIGATE_CCI
4725    printk("Keep the 0xce0020a0[%x] 0xce002008[%x]!!\n"
4726        ,adjust_cci[current_gate].adjust_cca_control,adjust_cci[current_gate].adjust_cca_1);
4727 #endif
4728   }
4729   else {
4730             if(current_level < input_level) {
4731                 for (i = 0; i < size; i++) {
4732                  if (input_level <= adjust_cci[i].upper_level) {
4733 #ifdef DEBUG_MITIGATE_CCI
4734                  printk("gate=%d, input_level=%d, adjust_cci[%d].upper_level=%d, value=%08x\n",
4735                              current_gate, input_level, i, adjust_cci[i].upper_level, adjust_cci[i].adjust_cca_control);
4736 #endif
4737                         current_level = input_level;
4738                         current_gate = i;
4739                         SMAC_REG_WRITE(sc->sh, ADR_RX_11B_CCA_CONTROL, adjust_cci[i].adjust_cca_control);
4740                         SMAC_REG_WRITE(sc->sh, ADR_RX_11B_CCA_1, adjust_cci[i].adjust_cca_1);
4741 #ifdef DEBUG_MITIGATE_CCI
4742                         printk("##Set to the 0xce0020a0[%x] 0xce002008[%x]##!!\n"
4743                    ,adjust_cci[current_gate].adjust_cca_control,adjust_cci[current_gate].adjust_cca_1);
4744 #endif
4745                      return;
4746                  }
4747              }
4748             }
4749             else {
4750                 for (i = (size -1); i >= 0; i--) {
4751                  if (input_level >= adjust_cci[i].down_level) {
4752 #ifdef DEBUG_MITIGATE_CCI
4753                      printk("gate=%d, input_level=%d, adjust_cci[%d].down_level=%d, value=%08x\n",
4754                              current_gate, input_level, i, adjust_cci[i].down_level, adjust_cci[i].adjust_cca_control);
4755 #endif
4756                         current_level = input_level;
4757                         current_gate = i;
4758                         SMAC_REG_WRITE(sc->sh, ADR_RX_11B_CCA_CONTROL, adjust_cci[i].adjust_cca_control);
4759                         SMAC_REG_WRITE(sc->sh, ADR_RX_11B_CCA_1, adjust_cci[i].adjust_cca_1);
4760 #ifdef DEBUG_MITIGATE_CCI
4761                         printk("##Set to the 0xce0020a0[%x] 0xce002008[%x]##!!\n"
4762                    ,adjust_cci[current_gate].adjust_cca_control,adjust_cci[current_gate].adjust_cca_1);
4763 #endif
4764                      return;
4765                  }
4766              }
4767             }
4768   }
4769     }
4770 }
4771 #define RSSI_SMOOTHING_SHIFT 5
4772 #define RSSI_DECIMAL_POINT_SHIFT 6
4773 #ifdef CONFIG_SSV_SUPPORT_ANDROID
4774 extern void ssv6xxx_send_deauth_toself(struct ssv_softc *sc,const u8 *bssid,const u8 *self_addr);
4775 #endif
4776 static void _proc_data_rx_skb (struct ssv_softc *sc, struct sk_buff *rx_skb)
4777 {
4778     struct ieee80211_rx_status *rxs;
4779     struct ieee80211_hdr *hdr;
4780     __le16 fc;
4781     struct ssv6200_rx_desc *rxdesc;
4782     struct ssv6200_rxphy_info_padding *rxphypad;
4783     struct ssv6200_rxphy_info *rxphy;
4784     struct ieee80211_channel *chan;
4785     struct ieee80211_vif *vif = NULL;
4786     struct ieee80211_sta *sta = NULL;
4787     bool rx_hw_dec = false;
4788     bool do_sw_dec = false;
4789     struct ssv_sta_priv_data *sta_priv = NULL;
4790     struct ssv_vif_priv_data *vif_priv = NULL;
4791     SKB_info *skb_info = NULL;
4792 #ifdef CONFIG_SSV_SUPPORT_ANDROID
4793  const u8 *p = NULL;
4794  struct ieee80211_mgmt *mgmt = NULL;
4795  static u8 diff_channel_cnt = 0;
4796 #endif
4797  u8 is_beacon;
4798  u8 is_probe_resp;
4799 #ifdef CONFIG_SSV_RSSI
4800     s32 found = 0;
4801 #endif
4802 #ifdef USE_LOCAL_CRYPTO
4803     int ret = 0;
4804 #ifdef MULTI_THREAD_ENCRYPT
4805     struct ssv_encrypt_task_list *ta = NULL;
4806     unsigned long flags;
4807 #endif
4808 #endif
4809 #ifdef CONFIG_SSV_SMARTLINK
4810     {
4811         extern int ksmartlink_smartlink_started(void);
4812         void smartlink_nl_send_msg(struct sk_buff *skb);
4813         if (unlikely(ksmartlink_smartlink_started()))
4814         {
4815             skb_pull(rx_skb, SSV6XXX_RX_DESC_LEN);
4816             skb_trim(rx_skb, rx_skb->len-sc->sh->rx_pinfo_pad);
4817             smartlink_nl_send_msg(rx_skb);
4818             return;
4819         }
4820     }
4821 #endif
4822     rxdesc = (struct ssv6200_rx_desc *)rx_skb->data;
4823     rxphy = (struct ssv6200_rxphy_info *)(rx_skb->data + sizeof(*rxdesc));
4824     rxphypad = (struct ssv6200_rxphy_info_padding *)(rx_skb->data + rx_skb->len - sizeof(struct ssv6200_rxphy_info_padding));
4825     hdr = (struct ieee80211_hdr *)(rx_skb->data + SSV6XXX_RX_DESC_LEN);
4826     fc = hdr->frame_control;
4827     skb_info = (SKB_info *)rx_skb->head;
4828     if (rxdesc->wsid >= SSV_RC_MAX_HARDWARE_SUPPORT)
4829     {
4830         if ( (ieee80211_is_data(hdr->frame_control))
4831             && (!(ieee80211_is_nullfunc(hdr->frame_control))))
4832         {
4833             ssv6xxx_rc_rx_data_handler(sc->hw, rx_skb, rxdesc->rate_idx);
4834         }
4835     }
4836     rxs = IEEE80211_SKB_RXCB(rx_skb);
4837     memset(rxs, 0, sizeof(struct ieee80211_rx_status));
4838     ssv6xxx_rc_mac8011_rate_idx(sc, rxdesc->rate_idx, rxs);
4839 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,0,0)
4840 //    printk("mactime=%u, len=%d\r\n", *((u32 *)&rx_skb->data[28]), rxdesc->len);    //+++
4841     rxs->mactime = *((u32 *)&rx_skb->data[28]);
4842 #endif
4843 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,7,0)
4844     chan = sc->hw->conf.channel;
4845 #else
4846     chan = sc->hw->conf.chandef.chan;
4847 #endif
4848     rxs->band = chan->band;
4849     rxs->freq = chan->center_freq;
4850     rxs->antenna = 1;
4851  is_beacon = ieee80211_is_beacon(hdr->frame_control);
4852  is_probe_resp = ieee80211_is_probe_resp(hdr->frame_control);
4853    if (is_beacon)                      //+++
4854    {
4855       struct ieee80211_mgmt *mgmt_tmp = NULL;
4856       mgmt_tmp = (struct ieee80211_mgmt *)(rx_skb->data + SSV6XXX_RX_DESC_LEN);
4857       mgmt_tmp->u.beacon.timestamp = cpu_to_le64(ssv6200_get_systime_us());
4858    }
4859    if (is_probe_resp)
4860    {
4861        struct ieee80211_mgmt *mgmt_tmp = NULL;
4862       mgmt_tmp = (struct ieee80211_mgmt *)(rx_skb->data + SSV6XXX_RX_DESC_LEN);
4863       mgmt_tmp->u.probe_resp.timestamp = cpu_to_le64(ssv6200_get_systime_us());
4864    }
4865 
4866     if (rxdesc->rate_idx < SSV62XX_G_RATE_INDEX && rxphypad->RSVD == 0)
4867     {
4868         if (is_beacon || is_probe_resp)
4869         {
4870             sta = ssv6xxx_find_sta_by_rx_skb(sc, rx_skb);
4871             if(sta)
4872             {
4873                 sta_priv = (struct ssv_sta_priv_data *)sta->drv_priv;
4874 #ifdef SSV_RSSI_DEBUG
4875                 printk(KERN_DEBUG "b_beacon %02X:%02X:%02X:%02X:%02X:%02X rssi=%d, snr=%d\n",
4876                                        hdr->addr2[0], hdr->addr2[1],hdr->addr2[2], hdr->addr2[3],
4877                                        hdr->addr2[4], hdr->addr2[5],rxphypad->rpci, rxphypad->snr);
4878 #endif
4879 #ifdef CONFIG_SSV_SUPPORT_ANDROID
4880                 if(is_beacon)
4881                 {
4882                     mgmt = (struct ieee80211_mgmt *)(rx_skb->data + SSV6XXX_RX_DESC_LEN);
4883                     p = cfg80211_find_ie(WLAN_EID_DS_PARAMS, mgmt->u.beacon.variable, rx_skb->len);
4884                     if (p)
4885                     {
4886                         u32 beacon_channel = (int)p[2];
4887 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,7,0)
4888                         struct ieee80211_channel *chan;
4889                         u32 bss_chan_hw_value;
4890                         chan = sc->hw->conf.channel;
4891                         bss_chan_hw_value = chan->hw_value;
4892 #else
4893                         u32 bss_chan_hw_value = sc->vif_info[0].vif->bss_conf.chandef.chan->hw_value;
4894 #endif
4895                         if( beacon_channel != bss_chan_hw_value )
4896                         {
4897                             diff_channel_cnt++;
4898                             if(diff_channel_cnt >= 25)
4899                             {
4900                                 printk(KERN_DEBUG "ssv6xxx_send_deauth_toself by channel change\n");
4901                                 ssv6xxx_send_deauth_toself(sc, sc->vif_info[0].vif->bss_conf.bssid, sc->vif_info[0].vif->addr);
4902                                 diff_channel_cnt = 0;
4903                             }
4904                         }
4905                         else
4906                         diff_channel_cnt = 0;
4907                     }
4908                 }
4909 #endif
4910                 if(sta_priv->beacon_rssi)
4911                 {
4912                     sta_priv->beacon_rssi = ((rxphypad->rpci << RSSI_DECIMAL_POINT_SHIFT)
4913                         + ((sta_priv->beacon_rssi<<RSSI_SMOOTHING_SHIFT) - sta_priv->beacon_rssi)) >> RSSI_SMOOTHING_SHIFT;
4914                     rxphypad->rpci = (sta_priv->beacon_rssi >> RSSI_DECIMAL_POINT_SHIFT);
4915                 }
4916                 else
4917                     sta_priv->beacon_rssi = (rxphypad->rpci << RSSI_DECIMAL_POINT_SHIFT);
4918 #ifdef SSV_RSSI_DEBUG
4919                 printk("Beacon smoothing RSSI %d\n",rxphypad->rpci);
4920 #endif
4921                 mitigate_cci(sc, rxphypad->rpci);
4922             }
4923 #ifdef CONFIG_SSV_RSSI
4924             else {
4925                 mutex_lock(&sc->mutex);
4926                 list_for_each_entry(p_rssi_res, &rssi_res.rssi_list, rssi_list) {
4927                     if (!memcmp(p_rssi_res->bssid, hdr->addr2, ETH_ALEN)) {
4928                         {
4929                             p_rssi_res->rssi = ((rxphypad->rpci << RSSI_DECIMAL_POINT_SHIFT)
4930                                 + ((p_rssi_res->rssi<<RSSI_SMOOTHING_SHIFT) - p_rssi_res->rssi)) >> RSSI_SMOOTHING_SHIFT;
4931                             rxphypad->rpci = (p_rssi_res->rssi >> RSSI_DECIMAL_POINT_SHIFT);
4932                         }
4933                         p_rssi_res->cache_jiffies = jiffies;
4934                         found = 1;
4935                         break;
4936                     }
4937                     else {
4938                         if(p_rssi_res->rssi) {
4939                             if (time_after(jiffies, p_rssi_res->cache_jiffies + msecs_to_jiffies(40000))) {
4940                                 p_rssi_res->timeout = 1;
4941                             }
4942                         }
4943                     }
4944                 }
4945                 if (!found) {
4946                     p_rssi_res = kmalloc(sizeof(struct rssi_res_st), GFP_KERNEL);
4947                     memcpy(p_rssi_res->bssid, hdr->addr2, ETH_ALEN);
4948                     p_rssi_res->cache_jiffies = jiffies;
4949                     p_rssi_res->rssi = (rxphypad->rpci << RSSI_DECIMAL_POINT_SHIFT);
4950                     p_rssi_res->timeout = 0;
4951                     INIT_LIST_HEAD(&p_rssi_res->rssi_list);
4952                     list_add_tail_rcu(&(p_rssi_res->rssi_list), &(rssi_res.rssi_list));
4953                 }
4954                 mutex_unlock(&sc->mutex);
4955             }
4956 #endif
4957             if(rxphypad->rpci > 88)
4958                 rxphypad->rpci = 88;
4959 #if 0
4960             printk("beacon %02X:%02X:%02X:%02X:%02X:%02X rxphypad-rpci=%d RxResult=%x wsid=%x\n",
4961                    hdr->addr2[0], hdr->addr2[1], hdr->addr2[2],
4962                    hdr->addr2[3], hdr->addr2[4], hdr->addr2[5], rxphypad->rpci, rxdesc->RxResult, rxdesc->wsid);
4963 #endif
4964         }
4965         if(sc->sh->cfg.rssi_ctl){
4966             rxs->signal = (-rxphypad->rpci) + sc->sh->cfg.rssi_ctl;
4967         }
4968         else{
4969             rxs->signal = (-rxphypad->rpci);
4970         }
4971     }
4972     else if (rxdesc->rate_idx >= SSV62XX_G_RATE_INDEX && rxphy->service == 0)
4973     {
4974         if (is_beacon || is_probe_resp)
4975         {
4976             sta = ssv6xxx_find_sta_by_rx_skb(sc, rx_skb);
4977             if(sta)
4978             {
4979                 sta_priv = (struct ssv_sta_priv_data *)sta->drv_priv;
4980 #ifdef SSV_RSSI_DEBUG
4981     printk("gn_beacon %02X:%02X:%02X:%02X:%02X:%02X rssi=%d, snr=%d\n",
4982            hdr->addr2[0], hdr->addr2[1],hdr->addr2[2], hdr->addr2[3],
4983            hdr->addr2[4], hdr->addr2[5],rxphy->rpci, rxphy->snr);
4984 #endif
4985                 if(sta_priv->beacon_rssi)
4986                 {
4987                     sta_priv->beacon_rssi = ((rxphy->rpci << RSSI_DECIMAL_POINT_SHIFT)
4988                         + ((sta_priv->beacon_rssi<<RSSI_SMOOTHING_SHIFT) - sta_priv->beacon_rssi)) >> RSSI_SMOOTHING_SHIFT;
4989                     rxphy->rpci = (sta_priv->beacon_rssi >> RSSI_DECIMAL_POINT_SHIFT);
4990                 }
4991                 else
4992                     sta_priv->beacon_rssi = (rxphy->rpci << RSSI_DECIMAL_POINT_SHIFT);
4993 #ifdef SSV_RSSI_DEBUG
4994                 printk("Beacon smoothing RSSI %d\n",rxphy->rpci);
4995 #endif
4996             }
4997             if(rxphy->rpci > 88)
4998                 rxphy->rpci = 88;
4999 #if 0
5000             printk("beacon %02X:%02X:%02X:%02X:%02X:%02X rxphy-rpci=%d RxResult=%x wsid=%x\n",
5001                    hdr->addr2[0], hdr->addr2[1], hdr->addr2[2],
5002                    hdr->addr2[3], hdr->addr2[4], hdr->addr2[5], rxphy->rpci, rxdesc->RxResult, rxdesc->wsid);
5003 #endif
5004         }
5005         if(sc->sh->cfg.rssi_ctl){
5006             rxs->signal = (-rxphy->rpci) + sc->sh->cfg.rssi_ctl;
5007         }else{
5008             rxs->signal = (-rxphy->rpci);
5009         }
5010     }
5011     else {
5012 #ifdef SSV_RSSI_DEBUG
5013         printk("########unicast: %d, b_rssi/snr: %d/%d, gn_rssi/snr: %d/%d, rate:%d###############\n",
5014                 rxdesc->unicast, (-rxphy->rpci), rxphy->snr, (-rxphypad->rpci), rxphypad->snr, rxdesc->rate_idx);
5015   printk("RSSI, %d, rate_idx, %d\n", rxs->signal, rxdesc->rate_idx);
5016   printk("rxdesc->RxResult = %x,rxdesc->wsid = %d\n",rxdesc->RxResult,rxdesc->wsid);
5017 #endif
5018         sta = ssv6xxx_find_sta_by_rx_skb(sc, rx_skb);
5019         if(sta)
5020         {
5021             sta_priv = (struct ssv_sta_priv_data *)sta->drv_priv;
5022             rxs->signal = -(sta_priv->beacon_rssi >> RSSI_DECIMAL_POINT_SHIFT);
5023         }
5024 #ifdef SSV_RSSI_DEBUG
5025         printk("Others signal %d\n",rxs->signal);
5026 #endif
5027     }
5028 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,0,0)
5029 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,7,0)
5030     rxs->flag = RX_FLAG_MACTIME_MPDU;
5031 #else
5032 //    rxs->flag = RX_FLAG_MACTIME_START;          //+++
5033 #endif
5034     rxs->rx_flags = 0;
5035 #endif
5036 #if LINUX_VERSION_CODE >= 0x030400
5037     if (rxphy->aggregate)
5038         rxs->flag |= RX_FLAG_NO_SIGNAL_VAL;
5039 #endif
5040     sc->hw_mng_used = rxdesc->mng_used;
5041     if ( (ieee80211_is_data(fc) || ieee80211_is_data_qos(fc))
5042         && ieee80211_has_protected(fc))
5043     {
5044         sta = ssv6xxx_find_sta_by_rx_skb(sc, rx_skb);
5045         if (sta == NULL)
5046             goto drop_rx;
5047         sta_priv = (struct ssv_sta_priv_data *)sta->drv_priv;
5048         vif = sta_priv->sta_info->vif;
5049         if (vif == NULL)
5050             goto drop_rx;
5051         if (is_broadcast_ether_addr(hdr->addr1))
5052         {
5053             vif_priv = (struct ssv_vif_priv_data *)vif->drv_priv;
5054             rx_hw_dec = vif_priv->has_hw_decrypt;
5055             do_sw_dec = vif_priv->need_sw_decrypt;
5056         }
5057         else
5058         {
5059             rx_hw_dec = sta_priv->has_hw_decrypt;
5060             do_sw_dec = sta_priv->need_sw_decrypt;
5061         }
5062         #if 0
5063         if (rx_count++ < 20)
5064         {
5065             printk(KERN_ERR "HW DEC (%d - %d) %d %02X:%02X:%02X:%02X:%02X:%02X\n",
5066                    rx_hw_dec, do_sw_dec, rxdesc->wsid,
5067                    hdr->addr1[0], hdr->addr1[1], hdr->addr1[2],
5068                    hdr->addr1[3], hdr->addr1[4], hdr->addr1[5]);
5069             _ssv6xxx_hexdump("M ", (const u8 *)rx_skb->data, (rx_skb->len > 128) ? 128 : rx_skb->len);
5070         }
5071         #endif
5072         #if 0
5073         dev_err(sc->dev, "R %02X:%02X:%02X:%02X:%02X:%02X %d %d\n",
5074                 hdr->addr2[0], hdr->addr2[1], hdr->addr2[2],
5075                 hdr->addr2[3], hdr->addr2[4], hdr->addr2[5],
5076                 rx_hw_dec, do_sw_dec);
5077         _ssv6xxx_hexdump("R ", (const u8 *)rx_skb->data,
5078                          (rx_skb->len > 128) ? 128 : rx_skb->len);
5079         #endif
5080     }
5081     if (sc->dbg_rx_frame)
5082     {
5083         _ssv6xxx_hexdump("================================================================\n"
5084                          "RX frame", (const u8 *)rx_skb->data, rx_skb->len);
5085     }
5086     skb_pull(rx_skb, SSV6XXX_RX_DESC_LEN);
5087     skb_trim(rx_skb, rx_skb->len-sc->sh->rx_pinfo_pad);
5088 #ifdef CONFIG_P2P_NOA
5089     if (is_beacon)
5090         ssv6xxx_noa_detect(sc, hdr, rx_skb->len);
5091 #endif
5092 #ifdef USE_LOCAL_CRYPTO
5093     if ((rx_hw_dec == false) && (do_sw_dec == true))
5094     {
5095 #ifndef MULTI_THREAD_ENCRYPT
5096         ret = ssv6xxx_skb_decrypt(rx_skb, sta, sc);
5097         if (ret < 0)
5098         {
5099             dev_err(sc->dev, "[Local Crypto]: Fail to decrypt local: %02X:%02X:%02X:%02X:%02X:%02X, ret = %d.\n",
5100                 hdr->addr2[0], hdr->addr2[1], hdr->addr2[2],
5101                 hdr->addr2[3], hdr->addr2[4], hdr->addr2[5], ret);
5102             goto drop_rx;
5103         }
5104 #else
5105         skb_info->sta = sta;
5106         ret = ssv6xxx_skb_pre_decrypt(rx_skb, sta, sc);
5107         if (ret == 0)
5108         {
5109             skb_info->crypt_st = PKT_CRYPT_ST_DEC_PRE;
5110             spin_lock_irqsave(&sc->crypt_st_lock, flags);
5111             __skb_queue_tail(&sc->preprocess_q, rx_skb);
5112             #ifdef CONFIG_SSV6XXX_DEBUGFS
5113             if (sc->max_preprocess_q_len < skb_queue_len(&sc->preprocess_q))
5114                 sc->max_preprocess_q_len = skb_queue_len(&sc->preprocess_q);
5115             #endif
5116             spin_unlock_irqrestore(&sc->crypt_st_lock, flags);
5117             list_for_each_entry_reverse(ta, &sc->encrypt_task_head, list)
5118             {
5119                 if ((cpu_online(ta->cpu_no)) && (ta->running == 0))
5120                 {
5121                     wake_up(&ta->encrypt_wait_q);
5122                     return;
5123                 }
5124             }
5125             return;
5126         }
5127         else if (ret ==(-EOPNOTSUPP))
5128         {
5129             ret = ssv6xxx_skb_decrypt(rx_skb, sta, sc);
5130             if (ret < 0)
5131             {
5132                 dev_err(sc->dev, "[Local Crypto]: Fail to decrypt local: %02X:%02X:%02X:%02X:%02X:%02X, ret = %d.\n",
5133                     hdr->addr2[0], hdr->addr2[1], hdr->addr2[2],
5134                     hdr->addr2[3], hdr->addr2[4], hdr->addr2[5], ret);
5135                 goto drop_rx;
5136             }
5137         }
5138         else
5139         {
5140             printk("[MT-CRYPTO]: Failed to do pre-decrypt (%d)\n", ret);
5141             dev_kfree_skb_any(rx_skb);
5142             return;
5143         }
5144 #endif
5145     }
5146 #endif
5147     if (rx_hw_dec || do_sw_dec)
5148     {
5149         hdr = (struct ieee80211_hdr *)rx_skb->data;
5150         rxs = IEEE80211_SKB_RXCB(rx_skb);
5151         hdr->frame_control = hdr->frame_control & ~(cpu_to_le16(IEEE80211_FCTL_PROTECTED));
5152         rxs->flag |= (RX_FLAG_DECRYPTED|RX_FLAG_IV_STRIPPED);
5153     }
5154 #if 0
5155     if ( is_broadcast_ether_addr(hdr->addr1)
5156         && (ieee80211_is_data_qos(fc) || ieee80211_is_data(fc)))
5157 #endif
5158 #if 0
5159     if (ieee80211_is_probe_req(fc))
5160     {
5161         #if 0
5162         printk(KERN_ERR "RX M: 1 %02X:%02X:%02X:%02X:%02X:%02X  (%d - %d - %d)\n",
5163                hdr->addr1[0], hdr->addr1[1], hdr->addr1[2],
5164                hdr->addr1[3], hdr->addr1[4], hdr->addr1[5],
5165                (le16_to_cpu(hdr->seq_ctrl) >> 4),
5166                rxdesc->wsid, ieee80211_has_protected(fc));
5167         #endif
5168         printk(KERN_ERR "Probe Req: 2 %02X:%02X:%02X:%02X:%02X:%02X\n",
5169                hdr->addr2[0], hdr->addr2[1], hdr->addr2[2],
5170                hdr->addr2[3], hdr->addr2[4], hdr->addr2[5]);
5171         #if 0
5172         printk(KERN_ERR "RX M: 3 %02X:%02X:%02X:%02X:%02X:%02X\n",
5173                hdr->addr3[0], hdr->addr3[1], hdr->addr3[2],
5174                hdr->addr3[3], hdr->addr3[4], hdr->addr3[5]);
5175         #endif
5176         _ssv6xxx_hexdump("RX frame", (const u8 *)rx_skb->data,
5177                          (rx_skb->len > 128) ? 128 : rx_skb->len);
5178     }
5179 #endif
5180     #if defined(USE_THREAD_RX) && !defined(IRQ_PROC_RX_DATA)
5181     local_bh_disable();
5182     ieee80211_rx(sc->hw, rx_skb);
5183     local_bh_enable();
5184     #else
5185     ieee80211_rx_irqsafe(sc->hw, rx_skb);
5186     #endif
5187  return;
5188 drop_rx:
5189 #if 0
5190     dev_err(sc->dev, "D %02X:%02X:%02X:%02X:%02X:%02X\n",
5191             hdr->addr2[0], hdr->addr2[1], hdr->addr2[2],
5192             hdr->addr2[3], hdr->addr2[4], hdr->addr2[5]);
5193 #endif
5194     dev_kfree_skb_any(rx_skb);
5195 }
5196 #ifdef IRQ_PROC_RX_DATA
5197 static struct sk_buff *_proc_rx_skb (struct ssv_softc *sc, struct sk_buff *rx_skb)
5198 {
5199     struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)(rx_skb->data + SSV6XXX_RX_DESC_LEN);
5200     struct ssv6200_rx_desc *rxdesc = (struct ssv6200_rx_desc *)rx_skb->data;
5201     if ( ieee80211_is_back(hdr->frame_control)
5202         || (rxdesc->c_type == HOST_EVENT))
5203         return rx_skb;
5204     _proc_data_rx_skb(sc, rx_skb);
5205     return NULL;
5206 }
5207 #endif
5208 void _process_rx_q (struct ssv_softc *sc, struct sk_buff_head *rx_q, spinlock_t *rx_q_lock)
5209 {
5210     struct sk_buff *skb;
5211     struct ieee80211_hdr *hdr;
5212     struct ssv6200_rx_desc *rxdesc;
5213     unsigned long flags=0;
5214     #ifdef USE_FLUSH_RETRY
5215     bool has_ba_processed = false;
5216     #endif
5217     while (1) {
5218         if (rx_q_lock != NULL)
5219         {
5220             spin_lock_irqsave(rx_q_lock, flags);
5221             skb = __skb_dequeue(rx_q);
5222         }
5223         else
5224             skb = skb_dequeue(rx_q);
5225         if (!skb)
5226         {
5227             if (rx_q_lock != NULL)
5228                 spin_unlock_irqrestore(rx_q_lock, flags);
5229             break;
5230         }
5231         sc->rx.rxq_count --;
5232         if (rx_q_lock != NULL)
5233             spin_unlock_irqrestore(rx_q_lock, flags);
5234         rxdesc = (struct ssv6200_rx_desc *)skb->data;
5235         if (rxdesc->c_type == HOST_EVENT)
5236         {
5237             struct cfg_host_event *h_evt = (struct cfg_host_event *)rxdesc;
5238             if (h_evt->h_event == SOC_EVT_NO_BA)
5239             {
5240                 ssv6200_ampdu_no_BA_handler(sc->hw, skb);
5241                 #ifdef USE_FLUSH_RETRY
5242                 has_ba_processed = true;
5243                 #endif
5244             }
5245             else if (h_evt->h_event == SOC_EVT_RC_MPDU_REPORT)
5246             {
5247                 #if 0
5248                 struct cfg_host_event *host_event;
5249                 struct firmware_rate_control_report_data *report_data;
5250                 host_event = (struct cfg_host_event *)rxdesc;
5251                 report_data = (struct firmware_rate_control_report_data *)&host_event->dat[0];
5252                 printk("MPDU report get!!wsid[%d]didx[%d]F[%d]S[%d]\n",report_data->wsid,report_data->rates[0].data_rate,report_data->ampdu_len,report_data->ampdu_ack_len);
5253                 #endif
5254                 skb_queue_tail(&sc->rc_report_queue, skb);
5255                 if (sc->rc_sample_sechedule == 0)
5256                     queue_work(sc->rc_sample_workqueue, &sc->rc_sample_work);
5257             }
5258             else if (h_evt->h_event == SOC_EVT_SDIO_TEST_COMMAND)
5259             {
5260                 if(h_evt->evt_seq_no == 0)
5261                 {
5262                     printk("SOC_EVT_SDIO_TEST_COMMAND\n");
5263                     sc->sdio_rx_evt_size = h_evt->len;
5264                     sc->sdio_throughput_timestamp = jiffies;
5265                 }
5266                 else
5267                 {
5268                     sc->sdio_rx_evt_size += h_evt->len;
5269                     if (time_after(jiffies, sc->sdio_throughput_timestamp + msecs_to_jiffies(1000)))
5270                     {
5271                         printk("data[%ld] SDIO RX throughput %ld Kbps\n",sc->sdio_rx_evt_size,(sc->sdio_rx_evt_size << 3) / jiffies_to_msecs(jiffies - sc->sdio_throughput_timestamp));
5272                         sc->sdio_throughput_timestamp = jiffies;
5273                         sc->sdio_rx_evt_size = 0;
5274                     }
5275                 }
5276                 dev_kfree_skb_any(skb);
5277             }
5278             else if (h_evt->h_event == SOC_EVT_WATCHDOG_TRIGGER)
5279             {
5280                 dev_kfree_skb_any(skb);
5281 //		if(sc->watchdog_flag != WD_SLEEP)     //+++
5282                 sc->watchdog_flag = WD_KICKED;
5283             }
5284             else if (h_evt->h_event == SOC_EVT_RESET_HOST)
5285             {
5286                 dev_kfree_skb_any(skb);
5287                 if ((sc->ap_vif == NULL) || !(sc->sh->cfg.ignore_reset_in_ap))
5288                 {
5289                     ssv6xxx_restart_hw(sc);
5290                 }
5291                 else
5292                 {
5293                     dev_warn(sc->dev, "Reset event ignored.\n");
5294                 }
5295             }
5296 #ifdef CONFIG_P2P_NOA
5297             else if(h_evt->h_event == SOC_EVT_NOA)
5298             {
5299                 ssv6xxx_process_noa_event(sc, skb);
5300                 dev_kfree_skb_any(skb);
5301             }
5302 #endif
5303    else if (h_evt->h_event == SOC_EVT_SDIO_TXTPUT_RESULT) {
5304     printk("data SDIO TX throughput %d Kbps\n", h_evt->evt_seq_no);
5305                 dev_kfree_skb_any(skb);
5306             }
5307             else if (h_evt->h_event == SOC_EVT_TXLOOPBK_RESULT){
5308                 if (h_evt->evt_seq_no == SSV6XXX_STATE_OK) {
5309                     printk("FW TX LOOPBACK OK\n");
5310                     sc->iq_cali_done = IQ_CALI_OK;
5311                 } else {
5312                     printk(KERN_ERR "FW TX LOOPBACK FAILED\n");
5313                     sc->iq_cali_done = IQ_CALI_FAILED;
5314                 }
5315                 dev_kfree_skb_any(skb);
5316                 wake_up_interruptible(&sc->fw_wait_q);
5317             }
5318    else
5319    {
5320                 dev_warn(sc->dev, "Unkown event %d received\n", h_evt->h_event);
5321                 dev_kfree_skb_any(skb);
5322             }
5323             continue;
5324         }
5325         hdr = (struct ieee80211_hdr *)(skb->data + SSV6XXX_RX_DESC_LEN);
5326         if (ieee80211_is_back(hdr->frame_control))
5327         {
5328             ssv6200_ampdu_BA_handler(sc->hw, skb);
5329             #ifdef USE_FLUSH_RETRY
5330             has_ba_processed = true;
5331             #endif
5332             continue;
5333         }
5334         _proc_data_rx_skb(sc, skb);
5335     }
5336     #ifdef USE_FLUSH_RETRY
5337     if (has_ba_processed)
5338     {
5339         ssv6xxx_ampdu_postprocess_BA(sc->hw);
5340     }
5341     #endif
5342 }
5343 #if !defined(USE_THREAD_RX) || defined(USE_BATCH_RX)
5344 int ssv6200_rx(struct sk_buff_head *rx_skb_q, void *args)
5345 #else
5346 int ssv6200_rx(struct sk_buff *rx_skb, void *args)
5347 #endif
5348 {
5349     struct ssv_softc *sc=args;
5350     #ifdef IRQ_PROC_RX_DATA
5351     struct sk_buff *skb;
5352     skb = _proc_rx_skb(sc, rx_skb);
5353     if (skb == NULL)
5354         return 0;
5355     #endif
5356     #if !defined(USE_THREAD_RX) || defined(USE_BATCH_RX)
5357     {
5358     unsigned long flags;
5359     spin_lock_irqsave(&sc->rx_skb_q.lock, flags);
5360     while (skb_queue_len(rx_skb_q))
5361         __skb_queue_tail(&sc->rx_skb_q, __skb_dequeue(rx_skb_q));
5362     spin_unlock_irqrestore(&sc->rx_skb_q.lock, flags);
5363     }
5364     #else
5365     skb_queue_tail(&sc->rx_skb_q, rx_skb);
5366     #endif
5367     wake_up_interruptible(&sc->rx_wait_q);
5368     return 0;
5369 }
5370 struct ieee80211_sta *ssv6xxx_find_sta_by_rx_skb (struct ssv_softc *sc, struct sk_buff *skb)
5371 {
5372     struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)(skb->data + SSV6XXX_RX_DESC_LEN);
5373     struct ssv6200_rx_desc *rxdesc = (struct ssv6200_rx_desc *)skb->data;;
5374     if ((rxdesc->wsid >= 0) && (rxdesc->wsid < SSV_NUM_STA))
5375         return sc->sta_info[rxdesc->wsid].sta;
5376     else
5377         return ssv6xxx_find_sta_by_addr(sc, hdr->addr2);
5378 }
5379 struct ieee80211_sta *ssv6xxx_find_sta_by_addr (struct ssv_softc *sc, u8 addr[6])
5380 {
5381     struct ieee80211_sta *sta;
5382     int i;
5383     for (i = 0; i < SSV6200_MAX_VIF; i++)
5384     {
5385         if (sc->vif_info[i].vif == NULL)
5386             continue;
5387         sta = ieee80211_find_sta(sc->vif_info[i].vif, addr);
5388         if (sta != NULL)
5389             return sta;
5390     }
5391     return NULL;
5392 }
5393 void ssv6xxx_foreach_sta (struct ssv_softc *sc, void (*sta_func)(struct ssv_softc *, struct ssv_sta_info *, void *), void *param)
5394 {
5395     int i;
5396     BUG_ON(sta_func == NULL);
5397 #if 0
5398     for (i = 0; i < SSV6200_MAX_VIF; i++)
5399     {
5400         struct ssv_vif_priv_data *vif_priv;
5401         int j;
5402         if (sc->vif_info[i].vif == NULL)
5403             continue;
5404         vif_priv = (struct ssv_vif_priv_data *)sc->vif[i]->drv_priv;
5405         for (j = 0; j < SSV_NUM_STA; j++)
5406         {
5407             if ((vif_priv->sta_info[j].s_flags & STA_FLAG_VALID) == 0)
5408                 continue;
5409             (*sta_func)(sc, &vif_priv->sta_info[j], param);
5410         }
5411     }
5412 #else
5413     for (i = 0; i < SSV_NUM_STA; i++)
5414     {
5415         if ((sc->sta_info[i].s_flags & STA_FLAG_VALID) == 0)
5416             continue;
5417         (*sta_func)(sc, &sc->sta_info[i], param);
5418     }
5419 #endif
5420 }
5421 void ssv6xxx_foreach_vif_sta (struct ssv_softc *sc,
5422                               struct ssv_vif_info *vif_info,
5423                               void (*sta_func)(struct ssv_softc *,
5424                                                struct ssv_vif_info *,
5425                                                struct ssv_sta_info *,
5426                                                void *),
5427                               void *param)
5428 {
5429     struct ssv_vif_priv_data *vif_priv;
5430     struct ssv_sta_priv_data *sta_priv_iter;
5431     BUG_ON(vif_info == NULL);
5432     BUG_ON((size_t)vif_info < 0x30000);
5433     vif_priv = (struct ssv_vif_priv_data *)vif_info->vif->drv_priv;
5434     BUG_ON((size_t)vif_info->vif < 0x30000);
5435     BUG_ON((size_t)vif_priv < 0x30000);
5436     list_for_each_entry(sta_priv_iter, &vif_priv->sta_list, list)
5437     {
5438         BUG_ON(sta_priv_iter == NULL);
5439         BUG_ON((size_t)sta_priv_iter < 0x30000);
5440         BUG_ON(sta_priv_iter->sta_info == NULL);
5441         BUG_ON((size_t)sta_priv_iter->sta_info < 0x30000);
5442         if ((sta_priv_iter->sta_info->s_flags & STA_FLAG_VALID) == 0)
5443             continue;
5444         (*sta_func)(sc, vif_info, sta_priv_iter->sta_info, param);
5445     }
5446 }
5447 #ifdef CONFIG_SSV6XXX_DEBUGFS
5448 ssize_t ssv6xxx_tx_queue_status_dump (struct ssv_softc *sc, char *status_buf,
5449                                       ssize_t length)
5450 {
5451     ssize_t buf_size = length;
5452     ssize_t prt_size;
5453     prt_size = snprintf(status_buf, buf_size, "\nSMAC driver queue status:.\n");
5454     status_buf += prt_size;
5455     buf_size -= prt_size;
5456     #ifdef MULTI_THREAD_ENCRYPT
5457     prt_size = snprintf(status_buf, buf_size, "\tCrypto pre-process queue: %d.\n",
5458                         skb_queue_len(&sc->preprocess_q));
5459     status_buf += prt_size;
5460     buf_size -= prt_size;
5461     prt_size = snprintf(status_buf, buf_size, "\tMax pre-process queue: %d.\n",
5462                         sc->max_preprocess_q_len);
5463     status_buf += prt_size;
5464     buf_size -= prt_size;
5465     prt_size = snprintf(status_buf, buf_size, "\tCrypto process queue: %d.\n",
5466                         skb_queue_len(&sc->crypted_q));
5467     status_buf += prt_size;
5468     buf_size -= prt_size;
5469     prt_size = snprintf(status_buf, buf_size, "\tMax process queue: %d.\n",
5470                         sc->max_crypted_q_len);
5471     status_buf += prt_size;
5472     buf_size -= prt_size;
5473     #endif
5474     prt_size = snprintf(status_buf, buf_size, "\tTX queue: %d\n",
5475                         skb_queue_len(&sc->tx_skb_q));
5476     status_buf += prt_size;
5477     buf_size -= prt_size;
5478     prt_size = snprintf(status_buf, buf_size, "\tMax TX queue: %d\n",
5479                         sc->max_tx_skb_q_len);
5480     status_buf += prt_size;
5481     buf_size -= prt_size;
5482     return (length - buf_size);
5483 }
5484 #endif
5485