OSDN Git Service

Add rtl8723bu driver version 4.4.5
[android-x86/external-kernel-drivers.git] / rtl8723bu / 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                 DBG_871X_SEL_NL(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                 DBG_871X_SEL_NL(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         {
254                 _rtw_init_stainfo(psta);
255
256                 _rtw_init_listhead(&(pstapriv->sta_hash[i]));
257
258                 rtw_list_insert_tail(&psta->list, get_list_head(&pstapriv->free_sta_queue));
259
260                 psta++;
261         }
262
263         pstapriv->adhoc_expire_to = 4; /* 4 * 2 = 8 sec */
264
265 #ifdef CONFIG_AP_MODE
266
267         pstapriv->sta_dz_bitmap = 0;
268         pstapriv->tim_bitmap = 0;
269
270         _rtw_init_listhead(&pstapriv->asoc_list);
271         _rtw_init_listhead(&pstapriv->auth_list);
272         _rtw_spinlock_init(&pstapriv->asoc_list_lock);
273         _rtw_spinlock_init(&pstapriv->auth_list_lock);
274         pstapriv->asoc_list_cnt = 0;
275         pstapriv->auth_list_cnt = 0;
276
277         pstapriv->auth_to = 3; // 3*2 = 6 sec
278         pstapriv->assoc_to = 3;
279         //pstapriv->expire_to = 900;// 900*2 = 1800 sec = 30 min, expire after no any traffic.
280         //pstapriv->expire_to = 30;// 30*2 = 60 sec = 1 min, expire after no any traffic.
281 #ifdef CONFIG_ACTIVE_KEEP_ALIVE_CHECK
282         pstapriv->expire_to = 3; // 3*2 = 6 sec
283 #else
284         pstapriv->expire_to = 60;// 60*2 = 120 sec = 2 min, expire after no any traffic.
285 #endif
286 #ifdef CONFIG_ATMEL_RC_PATCH
287         _rtw_memset(  pstapriv->atmel_rc_pattern, 0, ETH_ALEN);
288 #endif
289         pstapriv->max_num_sta = NUM_STA;
290
291 #endif
292
293 _func_exit_;
294
295         return _SUCCESS;
296
297 }
298
299 inline int rtw_stainfo_offset(struct sta_priv *stapriv, struct sta_info *sta)
300 {
301         int offset = (((u8 *)sta) - stapriv->pstainfo_buf)/sizeof(struct sta_info);
302
303         if (!stainfo_offset_valid(offset))
304                 DBG_871X("%s invalid offset(%d), out of range!!!", __func__, offset);
305
306         return offset;
307 }
308
309 inline struct sta_info *rtw_get_stainfo_by_offset(struct sta_priv *stapriv, int offset)
310 {
311         if (!stainfo_offset_valid(offset))
312                 DBG_871X("%s invalid offset(%d), out of range!!!", __func__, offset);
313
314         return (struct sta_info *)(stapriv->pstainfo_buf + offset * sizeof(struct sta_info));
315 }
316
317 void    _rtw_free_sta_xmit_priv_lock(struct sta_xmit_priv *psta_xmitpriv);
318 void    _rtw_free_sta_xmit_priv_lock(struct sta_xmit_priv *psta_xmitpriv)
319 {
320 _func_enter_;
321
322         _rtw_spinlock_free(&psta_xmitpriv->lock);
323
324         _rtw_spinlock_free(&(psta_xmitpriv->be_q.sta_pending.lock));
325         _rtw_spinlock_free(&(psta_xmitpriv->bk_q.sta_pending.lock));
326         _rtw_spinlock_free(&(psta_xmitpriv->vi_q.sta_pending.lock));
327         _rtw_spinlock_free(&(psta_xmitpriv->vo_q.sta_pending.lock));
328 _func_exit_;
329 }
330
331 static void     _rtw_free_sta_recv_priv_lock(struct sta_recv_priv *psta_recvpriv)
332 {
333 _func_enter_;
334
335         _rtw_spinlock_free(&psta_recvpriv->lock);
336
337         _rtw_spinlock_free(&(psta_recvpriv->defrag_q.lock));
338
339 _func_exit_;
340
341 }
342
343 void rtw_mfree_stainfo(struct sta_info *psta);
344 void rtw_mfree_stainfo(struct sta_info *psta)
345 {
346 _func_enter_;
347
348         if(&psta->lock != NULL)
349                  _rtw_spinlock_free(&psta->lock);
350
351         _rtw_free_sta_xmit_priv_lock(&psta->sta_xmitpriv);
352         _rtw_free_sta_recv_priv_lock(&psta->sta_recvpriv);
353
354 _func_exit_;
355 }
356
357
358 // this function is used to free the memory of lock || sema for all stainfos
359 void rtw_mfree_all_stainfo(struct sta_priv *pstapriv );
360 void rtw_mfree_all_stainfo(struct sta_priv *pstapriv )
361 {
362         _irqL    irqL;
363         _list   *plist, *phead;
364         struct sta_info *psta = NULL;
365
366 _func_enter_;
367
368         _enter_critical_bh(&pstapriv->sta_hash_lock, &irqL);
369
370         phead = get_list_head(&pstapriv->free_sta_queue);
371         plist = get_next(phead);
372
373         while ((rtw_end_of_queue_search(phead, plist)) == _FALSE)
374         {
375                 psta = LIST_CONTAINOR(plist, struct sta_info ,list);
376                 plist = get_next(plist);
377
378                 rtw_mfree_stainfo(psta);
379         }
380
381         _exit_critical_bh(&pstapriv->sta_hash_lock, &irqL);
382
383 _func_exit_;
384
385 }
386
387 void rtw_mfree_sta_priv_lock(struct     sta_priv *pstapriv);
388 void rtw_mfree_sta_priv_lock(struct     sta_priv *pstapriv)
389 {
390 #ifdef CONFIG_AP_MODE
391         struct wlan_acl_pool *pacl_list = &pstapriv->acl_list;
392 #endif
393
394          rtw_mfree_all_stainfo(pstapriv); //be done before free sta_hash_lock
395
396         _rtw_spinlock_free(&pstapriv->free_sta_queue.lock);
397
398         _rtw_spinlock_free(&pstapriv->sta_hash_lock);
399         _rtw_spinlock_free(&pstapriv->wakeup_q.lock);
400         _rtw_spinlock_free(&pstapriv->sleep_q.lock);
401
402 #ifdef CONFIG_AP_MODE
403         _rtw_spinlock_free(&pstapriv->asoc_list_lock);
404         _rtw_spinlock_free(&pstapriv->auth_list_lock);
405         _rtw_spinlock_free(&pacl_list->acl_node_q.lock);
406 #endif
407
408 }
409
410 u32     _rtw_free_sta_priv(struct       sta_priv *pstapriv)
411 {
412         _irqL   irqL;
413         _list   *phead, *plist;
414         struct sta_info *psta = NULL;
415         struct recv_reorder_ctrl *preorder_ctrl;
416         int     index;
417
418 _func_enter_;
419         if(pstapriv){
420
421                 /*      delete all reordering_ctrl_timer                */
422                 _enter_critical_bh(&pstapriv->sta_hash_lock, &irqL);
423                 for(index = 0; index < NUM_STA; index++)
424                 {
425                         phead = &(pstapriv->sta_hash[index]);
426                         plist = get_next(phead);
427
428                         while ((rtw_end_of_queue_search(phead, plist)) == _FALSE)
429                         {
430                                 int i;
431                                 psta = LIST_CONTAINOR(plist, struct sta_info ,hash_list);
432                                 plist = get_next(plist);
433
434                                 for(i=0; i < 16 ; i++)
435                                 {
436                                         preorder_ctrl = &psta->recvreorder_ctrl[i];
437                                         _cancel_timer_ex(&preorder_ctrl->reordering_ctrl_timer);
438                                 }
439                         }
440                 }
441                 _exit_critical_bh(&pstapriv->sta_hash_lock, &irqL);
442                 /*===============================*/
443
444                 rtw_mfree_sta_priv_lock(pstapriv);
445
446                 if(pstapriv->pallocated_stainfo_buf) {
447                         rtw_vmfree(pstapriv->pallocated_stainfo_buf, sizeof(struct sta_info)*NUM_STA+4);
448                 }
449         }
450
451 _func_exit_;
452         return _SUCCESS;
453 }
454
455
456 //struct        sta_info *rtw_alloc_stainfo(_queue *pfree_sta_queue, unsigned char *hwaddr)
457 struct  sta_info *rtw_alloc_stainfo(struct      sta_priv *pstapriv, const u8 *hwaddr)
458 {
459         _irqL irqL, irqL2;
460         uint tmp_aid;
461         s32     index;
462         _list   *phash_list;
463         struct sta_info *psta;
464         _queue *pfree_sta_queue;
465         struct recv_reorder_ctrl *preorder_ctrl;
466         int i = 0;
467         u16  wRxSeqInitialValue = 0xffff;
468
469 _func_enter_;
470
471         pfree_sta_queue = &pstapriv->free_sta_queue;
472
473         //_enter_critical_bh(&(pfree_sta_queue->lock), &irqL);
474         _enter_critical_bh(&(pstapriv->sta_hash_lock), &irqL2);
475         if (_rtw_queue_empty(pfree_sta_queue) == _TRUE)
476         {
477                 //_exit_critical_bh(&(pfree_sta_queue->lock), &irqL);
478                 _exit_critical_bh(&(pstapriv->sta_hash_lock), &irqL2);
479                 psta = NULL;
480         }
481         else
482         {
483                 psta = LIST_CONTAINOR(get_next(&pfree_sta_queue->queue), struct sta_info, list);
484
485                 rtw_list_delete(&(psta->list));
486
487                 //_exit_critical_bh(&(pfree_sta_queue->lock), &irqL);
488
489                 tmp_aid = psta->aid;
490
491                 _rtw_init_stainfo(psta);
492
493                 psta->padapter = pstapriv->padapter;
494
495                 _rtw_memcpy(psta->hwaddr, hwaddr, ETH_ALEN);
496
497                 index = wifi_mac_hash(hwaddr);
498
499                 RT_TRACE(_module_rtl871x_sta_mgt_c_,_drv_info_,("rtw_alloc_stainfo: index  = %x", index));
500
501                 if(index >= NUM_STA){
502                         RT_TRACE(_module_rtl871x_sta_mgt_c_,_drv_err_,("ERROR=> rtw_alloc_stainfo: index >= NUM_STA"));
503                         psta= NULL;
504                         goto exit;
505                 }
506                 phash_list = &(pstapriv->sta_hash[index]);
507
508                 //_enter_critical_bh(&(pstapriv->sta_hash_lock), &irqL2);
509
510                 rtw_list_insert_tail(&psta->hash_list, phash_list);
511
512                 pstapriv->asoc_sta_count ++ ;
513
514                 //_exit_critical_bh(&(pstapriv->sta_hash_lock), &irqL2);
515
516 // Commented by Albert 2009/08/13
517 // For the SMC router, the sequence number of first packet of WPS handshake will be 0.
518 // 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.
519 // So, we initialize the tid_rxseq variable as the 0xffff.
520
521                 for (i = 0; i < 16; i++) {
522                      _rtw_memcpy( &psta->sta_recvpriv.rxcache.tid_rxseq[ i ], &wRxSeqInitialValue, 2 );
523                         _rtw_memset(&psta->sta_recvpriv.rxcache.iv[i], 0, sizeof(psta->sta_recvpriv.rxcache.iv[i]));
524                 }
525
526                 RT_TRACE(_module_rtl871x_sta_mgt_c_,_drv_info_,("alloc number_%d stainfo  with hwaddr = %x %x %x %x %x %x  \n",
527                 pstapriv->asoc_sta_count , hwaddr[0], hwaddr[1], hwaddr[2],hwaddr[3],hwaddr[4],hwaddr[5]));
528
529                 init_addba_retry_timer(pstapriv->padapter, psta);
530 #ifdef CONFIG_IEEE80211W
531                 init_dot11w_expire_timer(pstapriv->padapter, psta);
532 #endif /* CONFIG_IEEE80211W */
533 #ifdef CONFIG_TDLS
534                 rtw_init_tdls_timer(pstapriv->padapter, psta);
535 #endif //CONFIG_TDLS
536
537                 //for A-MPDU Rx reordering buffer control
538                 for(i=0; i < 16 ; i++)
539                 {
540                         preorder_ctrl = &psta->recvreorder_ctrl[i];
541
542                         preorder_ctrl->padapter = pstapriv->padapter;
543
544                         preorder_ctrl->enable = _FALSE;
545
546                         preorder_ctrl->indicate_seq = 0xffff;
547                         #ifdef DBG_RX_SEQ
548                         DBG_871X("DBG_RX_SEQ %s:%d IndicateSeq: %d\n", __FUNCTION__, __LINE__,
549                                 preorder_ctrl->indicate_seq);
550                         #endif
551                         preorder_ctrl->wend_b= 0xffff;
552                         //preorder_ctrl->wsize_b = (NR_RECVBUFF-2);
553                         preorder_ctrl->wsize_b = 64;//64;
554                         preorder_ctrl->ampdu_size = RX_AMPDU_SIZE_INVALID;
555
556                         _rtw_init_queue(&preorder_ctrl->pending_recvframe_queue);
557
558                         rtw_init_recv_timer(preorder_ctrl);
559                 }
560
561
562                 //init for DM
563                 psta->rssi_stat.UndecoratedSmoothedPWDB = (-1);
564                 psta->rssi_stat.UndecoratedSmoothedCCK = (-1);
565 #ifdef CONFIG_ATMEL_RC_PATCH
566                 psta->flag_atmel_rc = 0;
567 #endif
568                 /* init for the sequence number of received management frame */
569                 psta->RxMgmtFrameSeqNum = 0xffff;
570                 psta->ra_rpt_linked = _FALSE;
571
572                 //alloc mac id for non-bc/mc station,
573                 rtw_alloc_macid(pstapriv->padapter, psta);
574
575         }
576
577 exit:
578
579         _exit_critical_bh(&(pstapriv->sta_hash_lock), &irqL2);
580
581 _func_exit_;
582
583         return psta;
584
585
586 }
587
588
589 // using pstapriv->sta_hash_lock to protect
590 u32     rtw_free_stainfo(_adapter *padapter , struct sta_info *psta)
591 {
592         int i;
593         _irqL irqL0;
594         _queue *pfree_sta_queue;
595         struct recv_reorder_ctrl *preorder_ctrl;
596         struct  sta_xmit_priv   *pstaxmitpriv;
597         struct  xmit_priv       *pxmitpriv= &padapter->xmitpriv;
598         struct  sta_priv *pstapriv = &padapter->stapriv;
599         struct hw_xmit *phwxmit;
600         int pending_qcnt[4];
601
602 _func_enter_;
603
604         if (psta == NULL)
605                 goto exit;
606
607         _enter_critical_bh(&(pstapriv->sta_hash_lock), &irqL0);
608         rtw_list_delete(&psta->hash_list);
609         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], psta->hwaddr[1], psta->hwaddr[2],psta->hwaddr[3],psta->hwaddr[4],psta->hwaddr[5]));
610         pstapriv->asoc_sta_count --;
611         _exit_critical_bh(&(pstapriv->sta_hash_lock), &irqL0);
612
613
614         _enter_critical_bh(&psta->lock, &irqL0);
615         psta->state &= ~_FW_LINKED;
616         _exit_critical_bh(&psta->lock, &irqL0);
617
618         pfree_sta_queue = &pstapriv->free_sta_queue;
619
620
621         pstaxmitpriv = &psta->sta_xmitpriv;
622
623         //rtw_list_delete(&psta->sleep_list);
624
625         //rtw_list_delete(&psta->wakeup_list);
626
627         _enter_critical_bh(&pxmitpriv->lock, &irqL0);
628
629         rtw_free_xmitframe_queue(pxmitpriv, &psta->sleep_q);
630         psta->sleepq_len = 0;
631
632         //vo
633         //_enter_critical_bh(&(pxmitpriv->vo_pending.lock), &irqL0);
634         rtw_free_xmitframe_queue( pxmitpriv, &pstaxmitpriv->vo_q.sta_pending);
635         rtw_list_delete(&(pstaxmitpriv->vo_q.tx_pending));
636         phwxmit = pxmitpriv->hwxmits;
637         phwxmit->accnt -= pstaxmitpriv->vo_q.qcnt;
638         pending_qcnt[0] = pstaxmitpriv->vo_q.qcnt;
639         pstaxmitpriv->vo_q.qcnt = 0;
640         //_exit_critical_bh(&(pxmitpriv->vo_pending.lock), &irqL0);
641
642         //vi
643         //_enter_critical_bh(&(pxmitpriv->vi_pending.lock), &irqL0);
644         rtw_free_xmitframe_queue( pxmitpriv, &pstaxmitpriv->vi_q.sta_pending);
645         rtw_list_delete(&(pstaxmitpriv->vi_q.tx_pending));
646         phwxmit = pxmitpriv->hwxmits+1;
647         phwxmit->accnt -= pstaxmitpriv->vi_q.qcnt;
648         pending_qcnt[1] = pstaxmitpriv->vi_q.qcnt;
649         pstaxmitpriv->vi_q.qcnt = 0;
650         //_exit_critical_bh(&(pxmitpriv->vi_pending.lock), &irqL0);
651
652         //be
653         //_enter_critical_bh(&(pxmitpriv->be_pending.lock), &irqL0);
654         rtw_free_xmitframe_queue( pxmitpriv, &pstaxmitpriv->be_q.sta_pending);
655         rtw_list_delete(&(pstaxmitpriv->be_q.tx_pending));
656         phwxmit = pxmitpriv->hwxmits+2;
657         phwxmit->accnt -= pstaxmitpriv->be_q.qcnt;
658         pending_qcnt[2] = pstaxmitpriv->be_q.qcnt;
659         pstaxmitpriv->be_q.qcnt = 0;
660         //_exit_critical_bh(&(pxmitpriv->be_pending.lock), &irqL0);
661
662         //bk
663         //_enter_critical_bh(&(pxmitpriv->bk_pending.lock), &irqL0);
664         rtw_free_xmitframe_queue( pxmitpriv, &pstaxmitpriv->bk_q.sta_pending);
665         rtw_list_delete(&(pstaxmitpriv->bk_q.tx_pending));
666         phwxmit = pxmitpriv->hwxmits+3;
667         phwxmit->accnt -= pstaxmitpriv->bk_q.qcnt;
668         pending_qcnt[3] = pstaxmitpriv->bk_q.qcnt;
669         pstaxmitpriv->bk_q.qcnt = 0;
670         //_exit_critical_bh(&(pxmitpriv->bk_pending.lock), &irqL0);
671
672         rtw_os_wake_queue_at_free_stainfo(padapter, pending_qcnt);
673
674         _exit_critical_bh(&pxmitpriv->lock, &irqL0);
675
676
677         // re-init sta_info; 20061114 // will be init in alloc_stainfo
678         //_rtw_init_sta_xmit_priv(&psta->sta_xmitpriv);
679         //_rtw_init_sta_recv_priv(&psta->sta_recvpriv);
680 #ifdef CONFIG_IEEE80211W
681         _cancel_timer_ex(&psta->dot11w_expire_timer);
682 #endif /* CONFIG_IEEE80211W */
683         _cancel_timer_ex(&psta->addba_retry_timer);
684
685 #ifdef CONFIG_TDLS
686         psta->tdls_sta_state = TDLS_STATE_NONE;
687         rtw_free_tdls_timer(psta);
688 #endif //CONFIG_TDLS
689
690         //for A-MPDU Rx reordering buffer control, cancel reordering_ctrl_timer
691         for(i=0; i < 16 ; i++)
692         {
693                 _irqL irqL;
694                 _list   *phead, *plist;
695                 union recv_frame *prframe;
696                 _queue *ppending_recvframe_queue;
697                 _queue *pfree_recv_queue = &padapter->recvpriv.free_recv_queue;
698
699                 preorder_ctrl = &psta->recvreorder_ctrl[i];
700
701                 _cancel_timer_ex(&preorder_ctrl->reordering_ctrl_timer);
702
703
704                 ppending_recvframe_queue = &preorder_ctrl->pending_recvframe_queue;
705
706                 _enter_critical_bh(&ppending_recvframe_queue->lock, &irqL);
707
708                 phead =         get_list_head(ppending_recvframe_queue);
709                 plist = get_next(phead);
710
711                 while(!rtw_is_list_empty(phead))
712                 {
713                         prframe = LIST_CONTAINOR(plist, union recv_frame, u);
714
715                         plist = get_next(plist);
716
717                         rtw_list_delete(&(prframe->u.hdr.list));
718
719                         rtw_free_recvframe(prframe, pfree_recv_queue);
720                 }
721
722                 _exit_critical_bh(&ppending_recvframe_queue->lock, &irqL);
723
724         }
725
726         if (!(psta->state & WIFI_AP_STATE))
727                 rtw_hal_set_odm_var(padapter, HAL_ODM_STA_INFO, psta, _FALSE);
728
729
730         //release mac id for non-bc/mc station,
731         rtw_release_macid(pstapriv->padapter, psta);
732
733 #ifdef CONFIG_AP_MODE
734
735 /*
736         _enter_critical_bh(&pstapriv->asoc_list_lock, &irqL0);
737         rtw_list_delete(&psta->asoc_list);
738         _exit_critical_bh(&pstapriv->asoc_list_lock, &irqL0);
739 */
740         _enter_critical_bh(&pstapriv->auth_list_lock, &irqL0);
741         if (!rtw_is_list_empty(&psta->auth_list)) {
742                 rtw_list_delete(&psta->auth_list);
743                 pstapriv->auth_list_cnt--;
744         }
745         _exit_critical_bh(&pstapriv->auth_list_lock, &irqL0);
746
747         psta->expire_to = 0;
748 #ifdef CONFIG_ATMEL_RC_PATCH
749         psta->flag_atmel_rc = 0;
750 #endif
751         psta->sleepq_ac_len = 0;
752         psta->qos_info = 0;
753
754         psta->max_sp_len = 0;
755         psta->uapsd_bk = 0;
756         psta->uapsd_be = 0;
757         psta->uapsd_vi = 0;
758         psta->uapsd_vo = 0;
759
760         psta->has_legacy_ac = 0;
761
762 #ifdef CONFIG_NATIVEAP_MLME
763
764         pstapriv->sta_dz_bitmap &=~BIT(psta->aid);
765         pstapriv->tim_bitmap &=~BIT(psta->aid);
766
767         //rtw_indicate_sta_disassoc_event(padapter, psta);
768
769         if ((psta->aid >0)&&(pstapriv->sta_aid[psta->aid - 1] == psta))
770         {
771                 pstapriv->sta_aid[psta->aid - 1] = NULL;
772                 psta->aid = 0;
773         }
774
775 #endif  // CONFIG_NATIVEAP_MLME
776
777 #ifdef CONFIG_TX_MCAST2UNI
778         psta->under_exist_checking = 0;
779 #endif  // CONFIG_TX_MCAST2UNI
780
781 #endif  // CONFIG_AP_MODE
782
783         rtw_st_ctl_deinit(&psta->st_ctl);
784
785          _rtw_spinlock_free(&psta->lock);
786
787         //_enter_critical_bh(&(pfree_sta_queue->lock), &irqL0);
788         _enter_critical_bh(&(pstapriv->sta_hash_lock), &irqL0);
789         rtw_list_insert_tail(&psta->list, get_list_head(pfree_sta_queue));
790         _exit_critical_bh(&(pstapriv->sta_hash_lock), &irqL0);
791         //_exit_critical_bh(&(pfree_sta_queue->lock), &irqL0);
792
793 exit:
794
795 _func_exit_;
796
797         return _SUCCESS;
798
799 }
800
801 // free all stainfo which in sta_hash[all]
802 void rtw_free_all_stainfo(_adapter *padapter)
803 {
804         _irqL    irqL;
805         _list   *plist, *phead;
806         s32     index;
807         struct sta_info *psta = NULL;
808         struct  sta_priv *pstapriv = &padapter->stapriv;
809         struct sta_info* pbcmc_stainfo =rtw_get_bcmc_stainfo( padapter);
810         u8 free_sta_num = 0;
811         char free_sta_list[NUM_STA];
812         int stainfo_offset;
813
814 _func_enter_;
815
816         if(pstapriv->asoc_sta_count==1)
817                 goto exit;
818
819         _enter_critical_bh(&pstapriv->sta_hash_lock, &irqL);
820
821         for(index=0; index< NUM_STA; index++)
822         {
823                 phead = &(pstapriv->sta_hash[index]);
824                 plist = get_next(phead);
825
826                 while ((rtw_end_of_queue_search(phead, plist)) == _FALSE)
827                 {
828                         psta = LIST_CONTAINOR(plist, struct sta_info ,hash_list);
829
830                         plist = get_next(plist);
831
832                         if(pbcmc_stainfo!=psta)
833                         {
834                                 rtw_list_delete(&psta->hash_list);
835                                 //rtw_free_stainfo(padapter , psta);
836                                 stainfo_offset = rtw_stainfo_offset(pstapriv, psta);
837                                 if (stainfo_offset_valid(stainfo_offset)) {
838                                         free_sta_list[free_sta_num++] = stainfo_offset;
839                                 }
840                         }
841
842                 }
843         }
844
845         _exit_critical_bh(&pstapriv->sta_hash_lock, &irqL);
846
847
848         for (index = 0; index < free_sta_num; index++)
849         {
850                 psta = rtw_get_stainfo_by_offset(pstapriv, free_sta_list[index]);
851                 rtw_free_stainfo(padapter , psta);
852         }
853
854 exit:
855
856 _func_exit_;
857
858 }
859
860 /* any station allocated can be searched by hash list */
861 struct sta_info *rtw_get_stainfo(struct sta_priv *pstapriv, const u8 *hwaddr)
862 {
863
864         _irqL    irqL;
865
866         _list   *plist, *phead;
867
868         struct sta_info *psta = NULL;
869
870         u32     index;
871
872         const u8 *addr;
873
874         const u8 bc_addr[ETH_ALEN] = {0xff,0xff,0xff,0xff,0xff,0xff};
875
876 _func_enter_;
877
878         if(hwaddr==NULL)
879                 return NULL;
880
881         if(IS_MCAST((unsigned char *)hwaddr))
882         {
883                 addr = bc_addr;
884         }
885         else
886         {
887                 addr = hwaddr;
888         }
889
890         index = wifi_mac_hash(addr);
891
892         _enter_critical_bh(&pstapriv->sta_hash_lock, &irqL);
893
894         phead = &(pstapriv->sta_hash[index]);
895         plist = get_next(phead);
896
897
898         while ((rtw_end_of_queue_search(phead, plist)) == _FALSE)
899         {
900
901                 psta = LIST_CONTAINOR(plist, struct sta_info, hash_list);
902
903                 if ((_rtw_memcmp(psta->hwaddr, addr, ETH_ALEN))== _TRUE)
904                 { // if found the matched address
905                         break;
906                 }
907                 psta=NULL;
908                 plist = get_next(plist);
909         }
910
911         _exit_critical_bh(&pstapriv->sta_hash_lock, &irqL);
912 _func_exit_;
913         return psta;
914
915 }
916
917 u32 rtw_init_bcmc_stainfo(_adapter* padapter)
918 {
919
920         struct sta_info         *psta;
921         struct tx_servq *ptxservq;
922         u32 res=_SUCCESS;
923         NDIS_802_11_MAC_ADDRESS bcast_addr= {0xff,0xff,0xff,0xff,0xff,0xff};
924
925         struct  sta_priv *pstapriv = &padapter->stapriv;
926         //_queue        *pstapending = &padapter->xmitpriv.bm_pending;
927
928 _func_enter_;
929
930         psta = rtw_alloc_stainfo(pstapriv, bcast_addr);
931
932         if(psta==NULL){
933                 res=_FAIL;
934                 RT_TRACE(_module_rtl871x_sta_mgt_c_,_drv_err_,("rtw_alloc_stainfo fail"));
935                 goto exit;
936         }
937         ptxservq= &(psta->sta_xmitpriv.be_q);
938
939 /*
940         _enter_critical(&pstapending->lock, &irqL0);
941
942         if (rtw_is_list_empty(&ptxservq->tx_pending))
943                 rtw_list_insert_tail(&ptxservq->tx_pending, get_list_head(pstapending));
944
945         _exit_critical(&pstapending->lock, &irqL0);
946 */
947
948 exit:
949 _func_exit_;
950         return _SUCCESS;
951
952 }
953
954
955 struct sta_info* rtw_get_bcmc_stainfo(_adapter* padapter)
956 {
957         struct sta_info         *psta;
958         struct sta_priv         *pstapriv = &padapter->stapriv;
959         u8 bc_addr[ETH_ALEN] = {0xff,0xff,0xff,0xff,0xff,0xff};
960 _func_enter_;
961          psta = rtw_get_stainfo(pstapriv, bc_addr);
962 _func_exit_;
963         return psta;
964
965 }
966
967 u8 rtw_access_ctrl(_adapter *padapter, u8 *mac_addr)
968 {
969         u8 res = _TRUE;
970 #ifdef  CONFIG_AP_MODE
971         _irqL irqL;
972         _list   *plist, *phead;
973         struct rtw_wlan_acl_node *paclnode;
974         u8 match = _FALSE;
975         struct sta_priv *pstapriv = &padapter->stapriv;
976         struct wlan_acl_pool *pacl_list = &pstapriv->acl_list;
977         _queue  *pacl_node_q =&pacl_list->acl_node_q;
978
979         _enter_critical_bh(&(pacl_node_q->lock), &irqL);
980         phead = get_list_head(pacl_node_q);
981         plist = get_next(phead);
982         while ((rtw_end_of_queue_search(phead, plist)) == _FALSE)
983         {
984                 paclnode = LIST_CONTAINOR(plist, struct rtw_wlan_acl_node, list);
985                 plist = get_next(plist);
986
987                 if(_rtw_memcmp(paclnode->addr, mac_addr, ETH_ALEN))
988                 {
989                         if(paclnode->valid == _TRUE)
990                         {
991                                 match = _TRUE;
992                                 break;
993                         }
994                 }
995         }
996         _exit_critical_bh(&(pacl_node_q->lock), &irqL);
997
998
999         if(pacl_list->mode == 1)//accept unless in deny list
1000         {
1001                 res = (match == _TRUE) ?  _FALSE:_TRUE;
1002         }
1003         else if(pacl_list->mode == 2)//deny unless in accept list
1004         {
1005                 res = (match == _TRUE) ?  _TRUE:_FALSE;
1006         }
1007         else
1008         {
1009                  res = _TRUE;
1010         }
1011
1012 #endif
1013
1014         return res;
1015
1016 }