net: wireless: rockchip: add rtl8822be pcie wifi driver
[firefly-linux-kernel-4.4.55.git] / drivers / net / wireless / rockchip_wlan / rtl8822be / core / rtw_sta_mgt.c
1 /******************************************************************************
2  *
3  * Copyright(c) 2007 - 2011 Realtek Corporation. All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of version 2 of the GNU General Public License as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along with
15  * this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
17  *
18  *
19  ******************************************************************************/
20 #define _RTW_STA_MGT_C_
21
22 #include <drv_types.h>
23
24 #if defined(PLATFORM_LINUX) && defined (PLATFORM_WINDOWS)
25
26         #error "Shall be Linux or Windows, but not both!\n"
27
28 #endif
29
30
31 bool test_st_match_rule(_adapter *adapter, u8 *local_naddr, u8 *local_port, u8 *remote_naddr, u8 *remote_port)
32 {
33         if (ntohs(*((u16 *)local_port)) == 5001 || ntohs(*((u16 *)remote_port)) == 5001)
34                 return _TRUE;
35         return _FALSE;
36 }
37
38 struct st_register test_st_reg = {
39         .s_proto = 0x06,
40         .rule = test_st_match_rule,
41 };
42
43 inline void rtw_st_ctl_init(struct st_ctl_t *st_ctl)
44 {
45         _rtw_memset(st_ctl->reg, 0 , sizeof(struct st_register) * SESSION_TRACKER_REG_ID_NUM);
46         _rtw_init_queue(&st_ctl->tracker_q);
47 }
48
49 inline void rtw_st_ctl_clear_tracker_q(struct st_ctl_t *st_ctl)
50 {
51         _irqL irqL;
52         _list *plist, *phead;
53         struct session_tracker *st;
54
55         _enter_critical_bh(&st_ctl->tracker_q.lock, &irqL);
56         phead = &st_ctl->tracker_q.queue;
57         plist = get_next(phead);
58         while (rtw_end_of_queue_search(phead, plist) == _FALSE) {
59                 st = LIST_CONTAINOR(plist, struct session_tracker, list);
60                 plist = get_next(plist);
61                 rtw_list_delete(&st->list);
62                 rtw_mfree((u8 *)st, sizeof(struct session_tracker));
63         }
64         _exit_critical_bh(&st_ctl->tracker_q.lock, &irqL);
65 }
66
67 inline void rtw_st_ctl_deinit(struct st_ctl_t *st_ctl)
68 {
69         rtw_st_ctl_clear_tracker_q(st_ctl);
70         _rtw_deinit_queue(&st_ctl->tracker_q);
71 }
72
73 inline void rtw_st_ctl_register(struct st_ctl_t *st_ctl, u8 st_reg_id, struct st_register *reg)
74 {
75         if (st_reg_id >= SESSION_TRACKER_REG_ID_NUM) {
76                 rtw_warn_on(1);
77                 return;
78         }
79
80         st_ctl->reg[st_reg_id].s_proto = reg->s_proto;
81         st_ctl->reg[st_reg_id].rule = reg->rule;
82 }
83
84 inline void rtw_st_ctl_unregister(struct st_ctl_t *st_ctl, u8 st_reg_id)
85 {
86         int i;
87
88         if (st_reg_id >= SESSION_TRACKER_REG_ID_NUM) {
89                 rtw_warn_on(1);
90                 return;
91         }
92
93         st_ctl->reg[st_reg_id].s_proto = 0;
94         st_ctl->reg[st_reg_id].rule = NULL;
95
96         /* clear tracker queue if no session trecker registered */
97         for (i = 0; i < SESSION_TRACKER_REG_ID_NUM; i++)
98                 if (st_ctl->reg[i].s_proto != 0)
99                         break;
100         if (i >= SESSION_TRACKER_REG_ID_NUM)
101                 rtw_st_ctl_clear_tracker_q(st_ctl);
102 }
103
104 inline bool rtw_st_ctl_chk_reg_s_proto(struct st_ctl_t *st_ctl, u8 s_proto)
105 {
106         bool ret = _FALSE;
107         int i;
108
109         for (i = 0; i < SESSION_TRACKER_REG_ID_NUM; i++) {
110                 if (st_ctl->reg[i].s_proto == s_proto) {
111                         ret = _TRUE;
112                         break;
113                 }
114         }
115
116         return ret;
117 }
118
119 inline bool rtw_st_ctl_chk_reg_rule(struct st_ctl_t *st_ctl, _adapter *adapter, u8 *local_naddr, u8 *local_port, u8 *remote_naddr, u8 *remote_port)
120 {
121         bool ret = _FALSE;
122         int i;
123         st_match_rule rule;
124
125         for (i = 0; i < SESSION_TRACKER_REG_ID_NUM; i++) {
126                 rule = st_ctl->reg[i].rule;
127                 if (rule && rule(adapter, local_naddr, local_port, remote_naddr, remote_port) == _TRUE) {
128                         ret = _TRUE;
129                         break;
130                 }
131         }
132
133         return ret;
134 }
135
136 #define SESSION_TRACKER_FMT IP_FMT":"PORT_FMT" "IP_FMT":"PORT_FMT" %u %d"
137 #define SESSION_TRACKER_ARG(st) IP_ARG(&(st)->local_naddr), PORT_ARG(&(st)->local_port), IP_ARG(&(st)->remote_naddr), PORT_ARG(&(st)->remote_port), (st)->status, rtw_get_passing_time_ms((st)->set_time)
138
139 void dump_st_ctl(void *sel, struct st_ctl_t *st_ctl)
140 {
141         int i;
142         _irqL irqL;
143         _list *plist, *phead;
144         struct session_tracker *st;
145
146         if (!DBG_SESSION_TRACKER)
147                 return;
148
149         for (i = 0; i < SESSION_TRACKER_REG_ID_NUM; i++)
150                 RTW_PRINT_SEL(sel, "reg%d: %u %p\n", i, st_ctl->reg[i].s_proto, st_ctl->reg[i].rule);
151
152         _enter_critical_bh(&st_ctl->tracker_q.lock, &irqL);
153         phead = &st_ctl->tracker_q.queue;
154         plist = get_next(phead);
155         while (rtw_end_of_queue_search(phead, plist) == _FALSE) {
156                 st = LIST_CONTAINOR(plist, struct session_tracker, list);
157                 plist = get_next(plist);
158
159                 RTW_PRINT_SEL(sel, SESSION_TRACKER_FMT"\n", SESSION_TRACKER_ARG(st));
160         }
161         _exit_critical_bh(&st_ctl->tracker_q.lock, &irqL);
162
163 }
164
165 void _rtw_init_stainfo(struct sta_info *psta);
166 void _rtw_init_stainfo(struct sta_info *psta)
167 {
168
169         _func_enter_;
170
171         _rtw_memset((u8 *)psta, 0, sizeof(struct sta_info));
172
173         _rtw_spinlock_init(&psta->lock);
174         _rtw_init_listhead(&psta->list);
175         _rtw_init_listhead(&psta->hash_list);
176         /* _rtw_init_listhead(&psta->asoc_list); */
177         /* _rtw_init_listhead(&psta->sleep_list); */
178         /* _rtw_init_listhead(&psta->wakeup_list);       */
179
180         _rtw_init_queue(&psta->sleep_q);
181         psta->sleepq_len = 0;
182
183         _rtw_init_sta_xmit_priv(&psta->sta_xmitpriv);
184         _rtw_init_sta_recv_priv(&psta->sta_recvpriv);
185
186 #ifdef CONFIG_AP_MODE
187
188         _rtw_init_listhead(&psta->asoc_list);
189
190         _rtw_init_listhead(&psta->auth_list);
191
192         psta->expire_to = 0;
193
194         psta->flags = 0;
195
196         psta->capability = 0;
197
198         psta->bpairwise_key_installed = _FALSE;
199
200
201 #ifdef CONFIG_NATIVEAP_MLME
202         psta->nonerp_set = 0;
203         psta->no_short_slot_time_set = 0;
204         psta->no_short_preamble_set = 0;
205         psta->no_ht_gf_set = 0;
206         psta->no_ht_set = 0;
207         psta->ht_20mhz_set = 0;
208         psta->ht_40mhz_intolerant = 0;
209 #endif
210
211 #ifdef CONFIG_TX_MCAST2UNI
212         psta->under_exist_checking = 0;
213 #endif /* CONFIG_TX_MCAST2UNI */
214
215         psta->keep_alive_trycnt = 0;
216
217 #endif /* CONFIG_AP_MODE         */
218
219         rtw_st_ctl_init(&psta->st_ctl);
220
221         _func_exit_;
222
223 }
224
225 u32     _rtw_init_sta_priv(struct       sta_priv *pstapriv)
226 {
227         struct sta_info *psta;
228         s32 i;
229
230         _func_enter_;
231
232         pstapriv->pallocated_stainfo_buf = rtw_zvmalloc(sizeof(struct sta_info) * NUM_STA + 4);
233
234         if (!pstapriv->pallocated_stainfo_buf)
235                 return _FAIL;
236
237         pstapriv->pstainfo_buf = pstapriv->pallocated_stainfo_buf + 4 -
238                          ((SIZE_PTR)(pstapriv->pallocated_stainfo_buf) & 3);
239
240         _rtw_init_queue(&pstapriv->free_sta_queue);
241
242         _rtw_spinlock_init(&pstapriv->sta_hash_lock);
243
244         /* _rtw_init_queue(&pstapriv->asoc_q); */
245         pstapriv->asoc_sta_count = 0;
246         _rtw_init_queue(&pstapriv->sleep_q);
247         _rtw_init_queue(&pstapriv->wakeup_q);
248
249         psta = (struct sta_info *)(pstapriv->pstainfo_buf);
250
251
252         for (i = 0; i < NUM_STA; i++) {
253                 _rtw_init_stainfo(psta);
254
255                 _rtw_init_listhead(&(pstapriv->sta_hash[i]));
256
257                 rtw_list_insert_tail(&psta->list, get_list_head(&pstapriv->free_sta_queue));
258
259                 psta++;
260         }
261
262         pstapriv->adhoc_expire_to = 4; /* 4 * 2 = 8 sec */
263
264 #ifdef CONFIG_AP_MODE
265
266         pstapriv->sta_dz_bitmap = 0;
267         pstapriv->tim_bitmap = 0;
268
269         _rtw_init_listhead(&pstapriv->asoc_list);
270         _rtw_init_listhead(&pstapriv->auth_list);
271         _rtw_spinlock_init(&pstapriv->asoc_list_lock);
272         _rtw_spinlock_init(&pstapriv->auth_list_lock);
273         pstapriv->asoc_list_cnt = 0;
274         pstapriv->auth_list_cnt = 0;
275
276         pstapriv->auth_to = 3; /* 3*2 = 6 sec */
277         pstapriv->assoc_to = 3;
278         /* pstapriv->expire_to = 900; */ /* 900*2 = 1800 sec = 30 min, expire after no any traffic. */
279         /* pstapriv->expire_to = 30; */ /* 30*2 = 60 sec = 1 min, expire after no any traffic. */
280 #ifdef CONFIG_ACTIVE_KEEP_ALIVE_CHECK
281         pstapriv->expire_to = 3; /* 3*2 = 6 sec */
282 #else
283         pstapriv->expire_to = 60;/* 60*2 = 120 sec = 2 min, expire after no any traffic. */
284 #endif
285 #ifdef CONFIG_ATMEL_RC_PATCH
286         _rtw_memset(pstapriv->atmel_rc_pattern, 0, ETH_ALEN);
287 #endif
288         pstapriv->max_num_sta = NUM_STA;
289
290 #endif
291
292 #if CONFIG_RTW_MACADDR_ACL
293         _rtw_init_queue(&(pstapriv->acl_list.acl_node_q));
294 #endif
295
296         _func_exit_;
297
298         return _SUCCESS;
299
300 }
301
302 inline int rtw_stainfo_offset(struct sta_priv *stapriv, struct sta_info *sta)
303 {
304         int offset = (((u8 *)sta) - stapriv->pstainfo_buf) / sizeof(struct sta_info);
305
306         if (!stainfo_offset_valid(offset))
307                 RTW_INFO("%s invalid offset(%d), out of range!!!", __func__, offset);
308
309         return offset;
310 }
311
312 inline struct sta_info *rtw_get_stainfo_by_offset(struct sta_priv *stapriv, int offset)
313 {
314         if (!stainfo_offset_valid(offset))
315                 RTW_INFO("%s invalid offset(%d), out of range!!!", __func__, offset);
316
317         return (struct sta_info *)(stapriv->pstainfo_buf + offset * sizeof(struct sta_info));
318 }
319
320 void    _rtw_free_sta_xmit_priv_lock(struct sta_xmit_priv *psta_xmitpriv);
321 void    _rtw_free_sta_xmit_priv_lock(struct sta_xmit_priv *psta_xmitpriv)
322 {
323         _func_enter_;
324
325         _rtw_spinlock_free(&psta_xmitpriv->lock);
326
327         _rtw_spinlock_free(&(psta_xmitpriv->be_q.sta_pending.lock));
328         _rtw_spinlock_free(&(psta_xmitpriv->bk_q.sta_pending.lock));
329         _rtw_spinlock_free(&(psta_xmitpriv->vi_q.sta_pending.lock));
330         _rtw_spinlock_free(&(psta_xmitpriv->vo_q.sta_pending.lock));
331         _func_exit_;
332 }
333
334 static void     _rtw_free_sta_recv_priv_lock(struct sta_recv_priv *psta_recvpriv)
335 {
336         _func_enter_;
337
338         _rtw_spinlock_free(&psta_recvpriv->lock);
339
340         _rtw_spinlock_free(&(psta_recvpriv->defrag_q.lock));
341
342         _func_exit_;
343
344 }
345
346 void rtw_mfree_stainfo(struct sta_info *psta);
347 void rtw_mfree_stainfo(struct sta_info *psta)
348 {
349         _func_enter_;
350
351         if (&psta->lock != NULL)
352                 _rtw_spinlock_free(&psta->lock);
353
354         _rtw_free_sta_xmit_priv_lock(&psta->sta_xmitpriv);
355         _rtw_free_sta_recv_priv_lock(&psta->sta_recvpriv);
356
357         _func_exit_;
358 }
359
360
361 /* this function is used to free the memory of lock || sema for all stainfos */
362 void rtw_mfree_all_stainfo(struct sta_priv *pstapriv);
363 void rtw_mfree_all_stainfo(struct sta_priv *pstapriv)
364 {
365         _irqL    irqL;
366         _list   *plist, *phead;
367         struct sta_info *psta = NULL;
368
369         _func_enter_;
370
371         _enter_critical_bh(&pstapriv->sta_hash_lock, &irqL);
372
373         phead = get_list_head(&pstapriv->free_sta_queue);
374         plist = get_next(phead);
375
376         while ((rtw_end_of_queue_search(phead, plist)) == _FALSE) {
377                 psta = LIST_CONTAINOR(plist, struct sta_info , list);
378                 plist = get_next(plist);
379
380                 rtw_mfree_stainfo(psta);
381         }
382
383         _exit_critical_bh(&pstapriv->sta_hash_lock, &irqL);
384
385         _func_exit_;
386
387 }
388
389 void rtw_mfree_sta_priv_lock(struct     sta_priv *pstapriv);
390 void rtw_mfree_sta_priv_lock(struct     sta_priv *pstapriv)
391 {
392         rtw_mfree_all_stainfo(pstapriv); /* be done before free sta_hash_lock */
393
394         _rtw_spinlock_free(&pstapriv->free_sta_queue.lock);
395
396         _rtw_spinlock_free(&pstapriv->sta_hash_lock);
397         _rtw_spinlock_free(&pstapriv->wakeup_q.lock);
398         _rtw_spinlock_free(&pstapriv->sleep_q.lock);
399
400 #ifdef CONFIG_AP_MODE
401         _rtw_spinlock_free(&pstapriv->asoc_list_lock);
402         _rtw_spinlock_free(&pstapriv->auth_list_lock);
403 #endif
404
405 }
406
407 u32     _rtw_free_sta_priv(struct       sta_priv *pstapriv)
408 {
409         _irqL   irqL;
410         _list   *phead, *plist;
411         struct sta_info *psta = NULL;
412         struct recv_reorder_ctrl *preorder_ctrl;
413         int     index;
414
415         _func_enter_;
416         if (pstapriv) {
417
418                 /*      delete all reordering_ctrl_timer                */
419                 _enter_critical_bh(&pstapriv->sta_hash_lock, &irqL);
420                 for (index = 0; index < NUM_STA; index++) {
421                         phead = &(pstapriv->sta_hash[index]);
422                         plist = get_next(phead);
423
424                         while ((rtw_end_of_queue_search(phead, plist)) == _FALSE) {
425                                 int i;
426                                 psta = LIST_CONTAINOR(plist, struct sta_info , hash_list);
427                                 plist = get_next(plist);
428
429                                 for (i = 0; i < 16 ; i++) {
430                                         preorder_ctrl = &psta->recvreorder_ctrl[i];
431                                         _cancel_timer_ex(&preorder_ctrl->reordering_ctrl_timer);
432                                 }
433                         }
434                 }
435                 _exit_critical_bh(&pstapriv->sta_hash_lock, &irqL);
436                 /*===============================*/
437
438                 rtw_mfree_sta_priv_lock(pstapriv);
439
440 #if CONFIG_RTW_MACADDR_ACL
441                 _rtw_deinit_queue(&(pstapriv->acl_list.acl_node_q));
442 #endif
443
444                 if (pstapriv->pallocated_stainfo_buf)
445                         rtw_vmfree(pstapriv->pallocated_stainfo_buf, sizeof(struct sta_info) * NUM_STA + 4);
446         }
447
448         _func_exit_;
449         return _SUCCESS;
450 }
451
452
453 /* struct       sta_info *rtw_alloc_stainfo(_queue *pfree_sta_queue, unsigned char *hwaddr) */
454 struct  sta_info *rtw_alloc_stainfo(struct      sta_priv *pstapriv, u8 *hwaddr)
455 {
456         _irqL irqL, irqL2;
457         uint tmp_aid;
458         s32     index;
459         _list   *phash_list;
460         struct sta_info *psta;
461         _queue *pfree_sta_queue;
462         struct recv_reorder_ctrl *preorder_ctrl;
463         int i = 0;
464         u16  wRxSeqInitialValue = 0xffff;
465
466         _func_enter_;
467
468         pfree_sta_queue = &pstapriv->free_sta_queue;
469
470         /* _enter_critical_bh(&(pfree_sta_queue->lock), &irqL); */
471         _enter_critical_bh(&(pstapriv->sta_hash_lock), &irqL2);
472         if (_rtw_queue_empty(pfree_sta_queue) == _TRUE) {
473                 /* _exit_critical_bh(&(pfree_sta_queue->lock), &irqL); */
474                 _exit_critical_bh(&(pstapriv->sta_hash_lock), &irqL2);
475                 psta = NULL;
476         } else {
477                 psta = LIST_CONTAINOR(get_next(&pfree_sta_queue->queue), struct sta_info, list);
478
479                 rtw_list_delete(&(psta->list));
480
481                 /* _exit_critical_bh(&(pfree_sta_queue->lock), &irqL); */
482
483                 tmp_aid = psta->aid;
484
485                 _rtw_init_stainfo(psta);
486
487                 psta->padapter = pstapriv->padapter;
488
489                 _rtw_memcpy(psta->hwaddr, hwaddr, ETH_ALEN);
490
491                 index = wifi_mac_hash(hwaddr);
492
493                 RT_TRACE(_module_rtl871x_sta_mgt_c_, _drv_info_, ("rtw_alloc_stainfo: index  = %x", index));
494
495                 if (index >= NUM_STA) {
496                         RT_TRACE(_module_rtl871x_sta_mgt_c_, _drv_err_, ("ERROR=> rtw_alloc_stainfo: index >= NUM_STA"));
497                         psta = NULL;
498                         goto exit;
499                 }
500                 phash_list = &(pstapriv->sta_hash[index]);
501
502                 /* _enter_critical_bh(&(pstapriv->sta_hash_lock), &irqL2); */
503
504                 rtw_list_insert_tail(&psta->hash_list, phash_list);
505
506                 pstapriv->asoc_sta_count++;
507
508                 /* _exit_critical_bh(&(pstapriv->sta_hash_lock), &irqL2); */
509
510                 /* Commented by Albert 2009/08/13
511                  * For the SMC router, the sequence number of first packet of WPS handshake will be 0.
512                  * In this case, this packet will be dropped by recv_decache function if we use the 0x00 as the default value for tid_rxseq variable.
513                  * So, we initialize the tid_rxseq variable as the 0xffff. */
514
515                 for (i = 0; i < 16; i++)
516                         _rtw_memcpy(&psta->sta_recvpriv.rxcache.tid_rxseq[i], &wRxSeqInitialValue, 2);
517
518                 RT_TRACE(_module_rtl871x_sta_mgt_c_, _drv_info_, ("alloc number_%d stainfo  with hwaddr = %x %x %x %x %x %x\n",
519                         pstapriv->asoc_sta_count , hwaddr[0], hwaddr[1], hwaddr[2], hwaddr[3], hwaddr[4], hwaddr[5]));
520
521                 init_addba_retry_timer(pstapriv->padapter, psta);
522 #ifdef CONFIG_IEEE80211W
523                 init_dot11w_expire_timer(pstapriv->padapter, psta);
524 #endif /* CONFIG_IEEE80211W */
525 #ifdef CONFIG_TDLS
526                 rtw_init_tdls_timer(pstapriv->padapter, psta);
527 #endif /* CONFIG_TDLS */
528
529                 /* for A-MPDU Rx reordering buffer control */
530                 for (i = 0; i < 16 ; i++) {
531                         preorder_ctrl = &psta->recvreorder_ctrl[i];
532
533                         preorder_ctrl->padapter = pstapriv->padapter;
534
535                         preorder_ctrl->enable = _FALSE;
536
537                         preorder_ctrl->indicate_seq = 0xffff;
538 #ifdef DBG_RX_SEQ
539                         RTW_INFO("DBG_RX_SEQ %s:%d IndicateSeq: %d\n", __FUNCTION__, __LINE__,
540                                  preorder_ctrl->indicate_seq);
541 #endif
542                         preorder_ctrl->wend_b = 0xffff;
543                         /* preorder_ctrl->wsize_b = (NR_RECVBUFF-2); */
544                         preorder_ctrl->wsize_b = 64;/* 64; */
545                         preorder_ctrl->ampdu_size = RX_AMPDU_SIZE_INVALID;
546
547                         _rtw_init_queue(&preorder_ctrl->pending_recvframe_queue);
548
549                         rtw_init_recv_timer(preorder_ctrl);
550                 }
551
552
553                 /* init for DM */
554                 psta->rssi_stat.UndecoratedSmoothedPWDB = (-1);
555                 psta->rssi_stat.UndecoratedSmoothedCCK = (-1);
556 #ifdef CONFIG_ATMEL_RC_PATCH
557                 psta->flag_atmel_rc = 0;
558 #endif
559                 /* init for the sequence number of received management frame */
560                 psta->RxMgmtFrameSeqNum = 0xffff;
561                 psta->ra_rpt_linked = _FALSE;
562
563                 rtw_alloc_macid(pstapriv->padapter, psta);
564
565         }
566
567 exit:
568
569         _exit_critical_bh(&(pstapriv->sta_hash_lock), &irqL2);
570
571         _func_exit_;
572
573         return psta;
574
575
576 }
577
578
579 /* using pstapriv->sta_hash_lock to protect */
580 u32     rtw_free_stainfo(_adapter *padapter , struct sta_info *psta)
581 {
582         int i;
583         _irqL irqL0;
584         _queue *pfree_sta_queue;
585         struct recv_reorder_ctrl *preorder_ctrl;
586         struct  sta_xmit_priv   *pstaxmitpriv;
587         struct  xmit_priv       *pxmitpriv = &padapter->xmitpriv;
588         struct  sta_priv *pstapriv = &padapter->stapriv;
589         struct hw_xmit *phwxmit;
590         int pending_qcnt[4];
591
592         _func_enter_;
593
594         if (psta == NULL)
595                 goto exit;
596
597         _enter_critical_bh(&(pstapriv->sta_hash_lock), &irqL0);
598         rtw_list_delete(&psta->hash_list);
599         RT_TRACE(_module_rtl871x_sta_mgt_c_, _drv_err_, ("\n free number_%d stainfo  with hwaddr = 0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x\n", pstapriv->asoc_sta_count , psta->hwaddr[0],
600                 psta->hwaddr[1], psta->hwaddr[2], psta->hwaddr[3], psta->hwaddr[4], psta->hwaddr[5]));
601         pstapriv->asoc_sta_count--;
602         _exit_critical_bh(&(pstapriv->sta_hash_lock), &irqL0);
603
604
605         _enter_critical_bh(&psta->lock, &irqL0);
606         psta->state &= ~_FW_LINKED;
607         _exit_critical_bh(&psta->lock, &irqL0);
608
609         pfree_sta_queue = &pstapriv->free_sta_queue;
610
611
612         pstaxmitpriv = &psta->sta_xmitpriv;
613
614         /* rtw_list_delete(&psta->sleep_list); */
615
616         /* rtw_list_delete(&psta->wakeup_list); */
617
618         _enter_critical_bh(&pxmitpriv->lock, &irqL0);
619
620         rtw_free_xmitframe_queue(pxmitpriv, &psta->sleep_q);
621         psta->sleepq_len = 0;
622
623         /* vo */
624         /* _enter_critical_bh(&(pxmitpriv->vo_pending.lock), &irqL0); */
625         rtw_free_xmitframe_queue(pxmitpriv, &pstaxmitpriv->vo_q.sta_pending);
626         rtw_list_delete(&(pstaxmitpriv->vo_q.tx_pending));
627         phwxmit = pxmitpriv->hwxmits;
628         phwxmit->accnt -= pstaxmitpriv->vo_q.qcnt;
629         pending_qcnt[0] = pstaxmitpriv->vo_q.qcnt;
630         pstaxmitpriv->vo_q.qcnt = 0;
631         /* _exit_critical_bh(&(pxmitpriv->vo_pending.lock), &irqL0); */
632
633         /* vi */
634         /* _enter_critical_bh(&(pxmitpriv->vi_pending.lock), &irqL0); */
635         rtw_free_xmitframe_queue(pxmitpriv, &pstaxmitpriv->vi_q.sta_pending);
636         rtw_list_delete(&(pstaxmitpriv->vi_q.tx_pending));
637         phwxmit = pxmitpriv->hwxmits + 1;
638         phwxmit->accnt -= pstaxmitpriv->vi_q.qcnt;
639         pending_qcnt[1] = pstaxmitpriv->vi_q.qcnt;
640         pstaxmitpriv->vi_q.qcnt = 0;
641         /* _exit_critical_bh(&(pxmitpriv->vi_pending.lock), &irqL0); */
642
643         /* be */
644         /* _enter_critical_bh(&(pxmitpriv->be_pending.lock), &irqL0); */
645         rtw_free_xmitframe_queue(pxmitpriv, &pstaxmitpriv->be_q.sta_pending);
646         rtw_list_delete(&(pstaxmitpriv->be_q.tx_pending));
647         phwxmit = pxmitpriv->hwxmits + 2;
648         phwxmit->accnt -= pstaxmitpriv->be_q.qcnt;
649         pending_qcnt[2] = pstaxmitpriv->be_q.qcnt;
650         pstaxmitpriv->be_q.qcnt = 0;
651         /* _exit_critical_bh(&(pxmitpriv->be_pending.lock), &irqL0); */
652
653         /* bk */
654         /* _enter_critical_bh(&(pxmitpriv->bk_pending.lock), &irqL0); */
655         rtw_free_xmitframe_queue(pxmitpriv, &pstaxmitpriv->bk_q.sta_pending);
656         rtw_list_delete(&(pstaxmitpriv->bk_q.tx_pending));
657         phwxmit = pxmitpriv->hwxmits + 3;
658         phwxmit->accnt -= pstaxmitpriv->bk_q.qcnt;
659         pending_qcnt[3] = pstaxmitpriv->bk_q.qcnt;
660         pstaxmitpriv->bk_q.qcnt = 0;
661         /* _exit_critical_bh(&(pxmitpriv->bk_pending.lock), &irqL0); */
662
663         rtw_os_wake_queue_at_free_stainfo(padapter, pending_qcnt);
664
665         _exit_critical_bh(&pxmitpriv->lock, &irqL0);
666
667
668         /* re-init sta_info; 20061114 */ /* will be init in alloc_stainfo */
669         /* _rtw_init_sta_xmit_priv(&psta->sta_xmitpriv); */
670         /* _rtw_init_sta_recv_priv(&psta->sta_recvpriv); */
671 #ifdef CONFIG_IEEE80211W
672         _cancel_timer_ex(&psta->dot11w_expire_timer);
673 #endif /* CONFIG_IEEE80211W */
674         _cancel_timer_ex(&psta->addba_retry_timer);
675
676 #ifdef CONFIG_TDLS
677         psta->tdls_sta_state = TDLS_STATE_NONE;
678         rtw_free_tdls_timer(psta);
679 #endif /* CONFIG_TDLS */
680
681         /* for A-MPDU Rx reordering buffer control, cancel reordering_ctrl_timer */
682         for (i = 0; i < 16 ; i++) {
683                 _irqL irqL;
684                 _list   *phead, *plist;
685                 union recv_frame *prframe;
686                 _queue *ppending_recvframe_queue;
687                 _queue *pfree_recv_queue = &padapter->recvpriv.free_recv_queue;
688
689                 preorder_ctrl = &psta->recvreorder_ctrl[i];
690
691                 _cancel_timer_ex(&preorder_ctrl->reordering_ctrl_timer);
692
693
694                 ppending_recvframe_queue = &preorder_ctrl->pending_recvframe_queue;
695
696                 _enter_critical_bh(&ppending_recvframe_queue->lock, &irqL);
697
698                 phead = get_list_head(ppending_recvframe_queue);
699                 plist = get_next(phead);
700
701                 while (!rtw_is_list_empty(phead)) {
702                         prframe = LIST_CONTAINOR(plist, union recv_frame, u);
703
704                         plist = get_next(plist);
705
706                         rtw_list_delete(&(prframe->u.hdr.list));
707
708                         rtw_free_recvframe(prframe, pfree_recv_queue);
709                 }
710
711                 _exit_critical_bh(&ppending_recvframe_queue->lock, &irqL);
712
713         }
714
715         if (!((psta->state & WIFI_AP_STATE) || MacAddr_isBcst(psta->hwaddr)))
716                 rtw_hal_set_odm_var(padapter, HAL_ODM_STA_INFO, psta, _FALSE);
717
718
719         /* release mac id for non-bc/mc station, */
720         rtw_release_macid(pstapriv->padapter, psta);
721
722 #ifdef CONFIG_AP_MODE
723
724         /*
725                 _enter_critical_bh(&pstapriv->asoc_list_lock, &irqL0);
726                 rtw_list_delete(&psta->asoc_list);
727                 _exit_critical_bh(&pstapriv->asoc_list_lock, &irqL0);
728         */
729         _enter_critical_bh(&pstapriv->auth_list_lock, &irqL0);
730         if (!rtw_is_list_empty(&psta->auth_list)) {
731                 rtw_list_delete(&psta->auth_list);
732                 pstapriv->auth_list_cnt--;
733         }
734         _exit_critical_bh(&pstapriv->auth_list_lock, &irqL0);
735
736         psta->expire_to = 0;
737 #ifdef CONFIG_ATMEL_RC_PATCH
738         psta->flag_atmel_rc = 0;
739 #endif
740         psta->sleepq_ac_len = 0;
741         psta->qos_info = 0;
742
743         psta->max_sp_len = 0;
744         psta->uapsd_bk = 0;
745         psta->uapsd_be = 0;
746         psta->uapsd_vi = 0;
747         psta->uapsd_vo = 0;
748
749         psta->has_legacy_ac = 0;
750
751 #ifdef CONFIG_NATIVEAP_MLME
752
753         pstapriv->sta_dz_bitmap &= ~BIT(psta->aid);
754         pstapriv->tim_bitmap &= ~BIT(psta->aid);
755
756         /* rtw_indicate_sta_disassoc_event(padapter, psta); */
757
758         if ((psta->aid > 0) && (pstapriv->sta_aid[psta->aid - 1] == psta)) {
759                 pstapriv->sta_aid[psta->aid - 1] = NULL;
760                 psta->aid = 0;
761         }
762
763 #endif /* CONFIG_NATIVEAP_MLME   */
764
765 #ifdef CONFIG_TX_MCAST2UNI
766         psta->under_exist_checking = 0;
767 #endif /* CONFIG_TX_MCAST2UNI */
768
769 #endif /* CONFIG_AP_MODE         */
770
771         rtw_st_ctl_deinit(&psta->st_ctl);
772
773         _rtw_spinlock_free(&psta->lock);
774
775         /* _enter_critical_bh(&(pfree_sta_queue->lock), &irqL0); */
776         _enter_critical_bh(&(pstapriv->sta_hash_lock), &irqL0);
777         rtw_list_insert_tail(&psta->list, get_list_head(pfree_sta_queue));
778         _exit_critical_bh(&(pstapriv->sta_hash_lock), &irqL0);
779         /* _exit_critical_bh(&(pfree_sta_queue->lock), &irqL0); */
780
781 exit:
782
783         _func_exit_;
784
785         return _SUCCESS;
786
787 }
788
789 /* free all stainfo which in sta_hash[all] */
790 void rtw_free_all_stainfo(_adapter *padapter)
791 {
792         _irqL    irqL;
793         _list   *plist, *phead;
794         s32     index;
795         struct sta_info *psta = NULL;
796         struct  sta_priv *pstapriv = &padapter->stapriv;
797         struct sta_info *pbcmc_stainfo = rtw_get_bcmc_stainfo(padapter);
798         u8 free_sta_num = 0;
799         char free_sta_list[NUM_STA];
800         int stainfo_offset;
801
802         _func_enter_;
803
804         if (pstapriv->asoc_sta_count == 1)
805                 goto exit;
806
807         _enter_critical_bh(&pstapriv->sta_hash_lock, &irqL);
808
809         for (index = 0; index < NUM_STA; index++) {
810                 phead = &(pstapriv->sta_hash[index]);
811                 plist = get_next(phead);
812
813                 while ((rtw_end_of_queue_search(phead, plist)) == _FALSE) {
814                         psta = LIST_CONTAINOR(plist, struct sta_info , hash_list);
815
816                         plist = get_next(plist);
817
818                         if (pbcmc_stainfo != psta) {
819                                 rtw_list_delete(&psta->hash_list);
820                                 /* rtw_free_stainfo(padapter , psta); */
821                                 stainfo_offset = rtw_stainfo_offset(pstapriv, psta);
822                                 if (stainfo_offset_valid(stainfo_offset))
823                                         free_sta_list[free_sta_num++] = stainfo_offset;
824                         }
825
826                 }
827         }
828
829         _exit_critical_bh(&pstapriv->sta_hash_lock, &irqL);
830
831
832         for (index = 0; index < free_sta_num; index++) {
833                 psta = rtw_get_stainfo_by_offset(pstapriv, free_sta_list[index]);
834                 rtw_free_stainfo(padapter , psta);
835         }
836
837 exit:
838
839         _func_exit_;
840
841 }
842
843 /* any station allocated can be searched by hash list */
844 struct sta_info *rtw_get_stainfo(struct sta_priv *pstapriv, u8 *hwaddr)
845 {
846
847         _irqL    irqL;
848
849         _list   *plist, *phead;
850
851         struct sta_info *psta = NULL;
852
853         u32     index;
854
855         u8 *addr;
856
857         u8 bc_addr[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
858
859         _func_enter_;
860
861         if (hwaddr == NULL)
862                 return NULL;
863
864         if (IS_MCAST(hwaddr))
865                 addr = bc_addr;
866         else
867                 addr = hwaddr;
868
869         index = wifi_mac_hash(addr);
870
871         _enter_critical_bh(&pstapriv->sta_hash_lock, &irqL);
872
873         phead = &(pstapriv->sta_hash[index]);
874         plist = get_next(phead);
875
876
877         while ((rtw_end_of_queue_search(phead, plist)) == _FALSE) {
878
879                 psta = LIST_CONTAINOR(plist, struct sta_info, hash_list);
880
881                 if ((_rtw_memcmp(psta->hwaddr, addr, ETH_ALEN)) == _TRUE) {
882                         /* if found the matched address */
883                         break;
884                 }
885                 psta = NULL;
886                 plist = get_next(plist);
887         }
888
889         _exit_critical_bh(&pstapriv->sta_hash_lock, &irqL);
890         _func_exit_;
891         return psta;
892
893 }
894
895 u32 rtw_init_bcmc_stainfo(_adapter *padapter)
896 {
897
898         struct sta_info *psta;
899         struct tx_servq *ptxservq;
900         u32 res = _SUCCESS;
901         NDIS_802_11_MAC_ADDRESS bcast_addr = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
902
903         struct  sta_priv *pstapriv = &padapter->stapriv;
904         /* _queue       *pstapending = &padapter->xmitpriv.bm_pending; */
905
906         _func_enter_;
907
908         psta = rtw_alloc_stainfo(pstapriv, bcast_addr);
909
910         if (psta == NULL) {
911                 res = _FAIL;
912                 RT_TRACE(_module_rtl871x_sta_mgt_c_, _drv_err_, ("rtw_alloc_stainfo fail"));
913                 goto exit;
914         }
915 #ifdef CONFIG_BEAMFORMING
916         psta->txbf_gid = 63;
917         psta->txbf_paid = 0;
918 #endif
919         ptxservq = &(psta->sta_xmitpriv.be_q);
920
921         /*
922                 _enter_critical(&pstapending->lock, &irqL0);
923
924                 if (rtw_is_list_empty(&ptxservq->tx_pending))
925                         rtw_list_insert_tail(&ptxservq->tx_pending, get_list_head(pstapending));
926
927                 _exit_critical(&pstapending->lock, &irqL0);
928         */
929
930 exit:
931         _func_exit_;
932         return _SUCCESS;
933
934 }
935
936
937 struct sta_info *rtw_get_bcmc_stainfo(_adapter *padapter)
938 {
939         struct sta_info *psta;
940         struct sta_priv *pstapriv = &padapter->stapriv;
941         u8 bc_addr[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
942         _func_enter_;
943         psta = rtw_get_stainfo(pstapriv, bc_addr);
944         _func_exit_;
945         return psta;
946
947 }
948
949 #if CONFIG_RTW_MACADDR_ACL
950 u8 rtw_access_ctrl(_adapter *adapter, u8 *mac_addr)
951 {
952         u8 res = _TRUE;
953         _irqL irqL;
954         _list *list, *head;
955         struct rtw_wlan_acl_node *acl_node;
956         u8 match = _FALSE;
957         struct sta_priv *stapriv = &adapter->stapriv;
958         struct wlan_acl_pool *acl = &stapriv->acl_list;
959         _queue  *acl_node_q = &acl->acl_node_q;
960
961         _enter_critical_bh(&(acl_node_q->lock), &irqL);
962         head = get_list_head(acl_node_q);
963         list = get_next(head);
964         while (rtw_end_of_queue_search(head, list) == _FALSE) {
965                 acl_node = LIST_CONTAINOR(list, struct rtw_wlan_acl_node, list);
966                 list = get_next(list);
967
968                 if (_rtw_memcmp(acl_node->addr, mac_addr, ETH_ALEN)) {
969                         if (acl_node->valid == _TRUE) {
970                                 match = _TRUE;
971                                 break;
972                         }
973                 }
974         }
975         _exit_critical_bh(&(acl_node_q->lock), &irqL);
976
977         if (acl->mode == RTW_ACL_MODE_ACCEPT_UNLESS_LISTED)
978                 res = (match == _TRUE) ?  _FALSE : _TRUE;
979         else if (acl->mode == RTW_ACL_MODE_DENY_UNLESS_LISTED)
980                 res = (match == _TRUE) ?  _TRUE : _FALSE;
981         else
982                 res = _TRUE;
983
984         return res;
985 }
986 #endif /* CONFIG_RTW_MACADDR_ACL */