1 /******************************************************************************
3 * Copyright(c) 2007 - 2011 Realtek Corporation. All rights reserved.
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.
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
14 ******************************************************************************/
15 #define _RTW_STA_MGT_C_
17 #include <drv_types.h>
18 #include <rtw_debug.h>
20 void _rtw_init_stainfo(struct sta_info *psta);
21 void _rtw_init_stainfo(struct sta_info *psta)
23 memset((u8 *)psta, 0, sizeof(struct sta_info));
25 spin_lock_init(&psta->lock);
26 INIT_LIST_HEAD(&psta->list);
27 INIT_LIST_HEAD(&psta->hash_list);
28 /* INIT_LIST_HEAD(&psta->asoc_list); */
29 /* INIT_LIST_HEAD(&psta->sleep_list); */
30 /* INIT_LIST_HEAD(&psta->wakeup_list); */
32 _rtw_init_queue(&psta->sleep_q);
35 _rtw_init_sta_xmit_priv(&psta->sta_xmitpriv);
36 _rtw_init_sta_recv_priv(&psta->sta_recvpriv);
38 INIT_LIST_HEAD(&psta->asoc_list);
40 INIT_LIST_HEAD(&psta->auth_list);
48 psta->bpairwise_key_installed = false;
51 psta->no_short_slot_time_set = 0;
52 psta->no_short_preamble_set = 0;
53 psta->no_ht_gf_set = 0;
55 psta->ht_20mhz_set = 0;
57 psta->under_exist_checking = 0;
59 psta->keep_alive_trycnt = 0;
62 u32 _rtw_init_sta_priv(struct sta_priv *pstapriv)
64 struct sta_info *psta;
67 pstapriv->pallocated_stainfo_buf = vzalloc(sizeof(struct sta_info) * NUM_STA+4);
69 if (!pstapriv->pallocated_stainfo_buf)
72 pstapriv->pstainfo_buf = pstapriv->pallocated_stainfo_buf + 4 -
73 ((SIZE_PTR)(pstapriv->pallocated_stainfo_buf) & 3);
75 _rtw_init_queue(&pstapriv->free_sta_queue);
77 spin_lock_init(&pstapriv->sta_hash_lock);
79 /* _rtw_init_queue(&pstapriv->asoc_q); */
80 pstapriv->asoc_sta_count = 0;
81 _rtw_init_queue(&pstapriv->sleep_q);
82 _rtw_init_queue(&pstapriv->wakeup_q);
84 psta = (struct sta_info *)(pstapriv->pstainfo_buf);
87 for (i = 0; i < NUM_STA; i++) {
88 _rtw_init_stainfo(psta);
90 INIT_LIST_HEAD(&(pstapriv->sta_hash[i]));
92 list_add_tail(&psta->list, get_list_head(&pstapriv->free_sta_queue));
97 pstapriv->sta_dz_bitmap = 0;
98 pstapriv->tim_bitmap = 0;
100 INIT_LIST_HEAD(&pstapriv->asoc_list);
101 INIT_LIST_HEAD(&pstapriv->auth_list);
102 spin_lock_init(&pstapriv->asoc_list_lock);
103 spin_lock_init(&pstapriv->auth_list_lock);
104 pstapriv->asoc_list_cnt = 0;
105 pstapriv->auth_list_cnt = 0;
107 pstapriv->auth_to = 3; /* 3*2 = 6 sec */
108 pstapriv->assoc_to = 3;
109 pstapriv->expire_to = 3; /* 3*2 = 6 sec */
110 pstapriv->max_num_sta = NUM_STA;
114 inline int rtw_stainfo_offset(struct sta_priv *stapriv, struct sta_info *sta)
116 int offset = (((u8 *)sta) - stapriv->pstainfo_buf)/sizeof(struct sta_info);
118 if (!stainfo_offset_valid(offset))
119 DBG_871X("%s invalid offset(%d), out of range!!!", __func__, offset);
124 inline struct sta_info *rtw_get_stainfo_by_offset(struct sta_priv *stapriv, int offset)
126 if (!stainfo_offset_valid(offset))
127 DBG_871X("%s invalid offset(%d), out of range!!!", __func__, offset);
129 return (struct sta_info *)(stapriv->pstainfo_buf + offset * sizeof(struct sta_info));
132 /* this function is used to free the memory of lock || sema for all stainfos */
133 void kfree_all_stainfo(struct sta_priv *pstapriv);
134 void kfree_all_stainfo(struct sta_priv *pstapriv)
136 struct list_head *plist, *phead;
137 struct sta_info *psta = NULL;
139 spin_lock_bh(&pstapriv->sta_hash_lock);
141 phead = get_list_head(&pstapriv->free_sta_queue);
142 plist = get_next(phead);
144 while (phead != plist) {
145 psta = LIST_CONTAINOR(plist, struct sta_info, list);
146 plist = get_next(plist);
149 spin_unlock_bh(&pstapriv->sta_hash_lock);
152 void kfree_sta_priv_lock(struct sta_priv *pstapriv);
153 void kfree_sta_priv_lock(struct sta_priv *pstapriv)
155 kfree_all_stainfo(pstapriv); /* be done before free sta_hash_lock */
158 u32 _rtw_free_sta_priv(struct sta_priv *pstapriv)
160 struct list_head *phead, *plist;
161 struct sta_info *psta = NULL;
162 struct recv_reorder_ctrl *preorder_ctrl;
167 /*delete all reordering_ctrl_timer */
168 spin_lock_bh(&pstapriv->sta_hash_lock);
169 for (index = 0; index < NUM_STA; index++) {
170 phead = &(pstapriv->sta_hash[index]);
171 plist = get_next(phead);
173 while (phead != plist) {
175 psta = LIST_CONTAINOR(plist, struct sta_info, hash_list);
176 plist = get_next(plist);
178 for (i = 0; i < 16 ; i++) {
179 preorder_ctrl = &psta->recvreorder_ctrl[i];
180 del_timer_sync(&preorder_ctrl->reordering_ctrl_timer);
184 spin_unlock_bh(&pstapriv->sta_hash_lock);
185 /*===============================*/
187 kfree_sta_priv_lock(pstapriv);
189 if (pstapriv->pallocated_stainfo_buf)
190 vfree(pstapriv->pallocated_stainfo_buf);
196 /* struct sta_info *rtw_alloc_stainfo(_queue *pfree_sta_queue, unsigned char *hwaddr) */
197 struct sta_info *rtw_alloc_stainfo(struct sta_priv *pstapriv, u8 *hwaddr)
201 struct list_head *phash_list;
202 struct sta_info *psta;
203 struct __queue *pfree_sta_queue;
204 struct recv_reorder_ctrl *preorder_ctrl;
206 u16 wRxSeqInitialValue = 0xffff;
208 pfree_sta_queue = &pstapriv->free_sta_queue;
210 /* spin_lock_bh(&(pfree_sta_queue->lock)); */
211 spin_lock_bh(&(pstapriv->sta_hash_lock));
212 if (list_empty(&pfree_sta_queue->queue)) {
213 /* spin_unlock_bh(&(pfree_sta_queue->lock)); */
214 spin_unlock_bh(&(pstapriv->sta_hash_lock));
218 psta = LIST_CONTAINOR(get_next(&pfree_sta_queue->queue), struct sta_info, list);
220 list_del_init(&(psta->list));
222 /* spin_unlock_bh(&(pfree_sta_queue->lock)); */
226 _rtw_init_stainfo(psta);
228 psta->padapter = pstapriv->padapter;
230 memcpy(psta->hwaddr, hwaddr, ETH_ALEN);
232 index = wifi_mac_hash(hwaddr);
234 RT_TRACE(_module_rtl871x_sta_mgt_c_, _drv_info_, ("rtw_alloc_stainfo: index = %x", index));
236 if (index >= NUM_STA) {
237 RT_TRACE(_module_rtl871x_sta_mgt_c_, _drv_err_, ("ERROR => rtw_alloc_stainfo: index >= NUM_STA"));
238 spin_unlock_bh(&(pstapriv->sta_hash_lock));
242 phash_list = &(pstapriv->sta_hash[index]);
244 /* spin_lock_bh(&(pstapriv->sta_hash_lock)); */
246 list_add_tail(&psta->hash_list, phash_list);
248 pstapriv->asoc_sta_count++;
250 /* spin_unlock_bh(&(pstapriv->sta_hash_lock)); */
252 /* Commented by Albert 2009/08/13 */
253 /* For the SMC router, the sequence number of first packet of WPS handshake will be 0. */
254 /* 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. */
255 /* So, we initialize the tid_rxseq variable as the 0xffff. */
257 for (i = 0; i < 16; i++) {
258 memcpy(&psta->sta_recvpriv.rxcache.tid_rxseq[i], &wRxSeqInitialValue, 2);
262 _module_rtl871x_sta_mgt_c_,
264 "alloc number_%d stainfo with hwaddr = %x %x %x %x %x %x \n",
265 pstapriv->asoc_sta_count,
275 init_addba_retry_timer(pstapriv->padapter, psta);
277 /* for A-MPDU Rx reordering buffer control */
278 for (i = 0; i < 16 ; i++) {
279 preorder_ctrl = &psta->recvreorder_ctrl[i];
281 preorder_ctrl->padapter = pstapriv->padapter;
283 preorder_ctrl->enable = false;
285 preorder_ctrl->indicate_seq = 0xffff;
287 DBG_871X("DBG_RX_SEQ %s:%d IndicateSeq: %d\n", __func__, __LINE__,
288 preorder_ctrl->indicate_seq);
290 preorder_ctrl->wend_b = 0xffff;
291 /* preorder_ctrl->wsize_b = (NR_RECVBUFF-2); */
292 preorder_ctrl->wsize_b = 64;/* 64; */
294 _rtw_init_queue(&preorder_ctrl->pending_recvframe_queue);
296 rtw_init_recv_timer(preorder_ctrl);
301 psta->rssi_stat.UndecoratedSmoothedPWDB = (-1);
302 psta->rssi_stat.UndecoratedSmoothedCCK = (-1);
304 /* init for the sequence number of received management frame */
305 psta->RxMgmtFrameSeqNum = 0xffff;
306 spin_unlock_bh(&(pstapriv->sta_hash_lock));
307 /* alloc mac id for non-bc/mc station, */
308 rtw_alloc_macid(pstapriv->padapter, psta);
318 /* using pstapriv->sta_hash_lock to protect */
319 u32 rtw_free_stainfo(struct adapter *padapter, struct sta_info *psta)
322 struct __queue *pfree_sta_queue;
323 struct recv_reorder_ctrl *preorder_ctrl;
324 struct sta_xmit_priv *pstaxmitpriv;
325 struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
326 struct sta_priv *pstapriv = &padapter->stapriv;
327 struct hw_xmit *phwxmit;
333 spin_lock_bh(&psta->lock);
334 psta->state &= ~_FW_LINKED;
335 spin_unlock_bh(&psta->lock);
337 pfree_sta_queue = &pstapriv->free_sta_queue;
340 pstaxmitpriv = &psta->sta_xmitpriv;
342 /* list_del_init(&psta->sleep_list); */
344 /* list_del_init(&psta->wakeup_list); */
346 spin_lock_bh(&pxmitpriv->lock);
348 rtw_free_xmitframe_queue(pxmitpriv, &psta->sleep_q);
349 psta->sleepq_len = 0;
352 /* spin_lock_bh(&(pxmitpriv->vo_pending.lock)); */
353 rtw_free_xmitframe_queue(pxmitpriv, &pstaxmitpriv->vo_q.sta_pending);
354 list_del_init(&(pstaxmitpriv->vo_q.tx_pending));
355 phwxmit = pxmitpriv->hwxmits;
356 phwxmit->accnt -= pstaxmitpriv->vo_q.qcnt;
357 pstaxmitpriv->vo_q.qcnt = 0;
358 /* spin_unlock_bh(&(pxmitpriv->vo_pending.lock)); */
361 /* spin_lock_bh(&(pxmitpriv->vi_pending.lock)); */
362 rtw_free_xmitframe_queue(pxmitpriv, &pstaxmitpriv->vi_q.sta_pending);
363 list_del_init(&(pstaxmitpriv->vi_q.tx_pending));
364 phwxmit = pxmitpriv->hwxmits+1;
365 phwxmit->accnt -= pstaxmitpriv->vi_q.qcnt;
366 pstaxmitpriv->vi_q.qcnt = 0;
367 /* spin_unlock_bh(&(pxmitpriv->vi_pending.lock)); */
370 /* spin_lock_bh(&(pxmitpriv->be_pending.lock)); */
371 rtw_free_xmitframe_queue(pxmitpriv, &pstaxmitpriv->be_q.sta_pending);
372 list_del_init(&(pstaxmitpriv->be_q.tx_pending));
373 phwxmit = pxmitpriv->hwxmits+2;
374 phwxmit->accnt -= pstaxmitpriv->be_q.qcnt;
375 pstaxmitpriv->be_q.qcnt = 0;
376 /* spin_unlock_bh(&(pxmitpriv->be_pending.lock)); */
379 /* spin_lock_bh(&(pxmitpriv->bk_pending.lock)); */
380 rtw_free_xmitframe_queue(pxmitpriv, &pstaxmitpriv->bk_q.sta_pending);
381 list_del_init(&(pstaxmitpriv->bk_q.tx_pending));
382 phwxmit = pxmitpriv->hwxmits+3;
383 phwxmit->accnt -= pstaxmitpriv->bk_q.qcnt;
384 pstaxmitpriv->bk_q.qcnt = 0;
385 /* spin_unlock_bh(&(pxmitpriv->bk_pending.lock)); */
387 spin_unlock_bh(&pxmitpriv->lock);
389 list_del_init(&psta->hash_list);
391 _module_rtl871x_sta_mgt_c_,
393 "\n free number_%d stainfo with hwaddr = 0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x \n",
394 pstapriv->asoc_sta_count,
403 pstapriv->asoc_sta_count--;
406 /* re-init sta_info; 20061114 will be init in alloc_stainfo */
407 /* _rtw_init_sta_xmit_priv(&psta->sta_xmitpriv); */
408 /* _rtw_init_sta_recv_priv(&psta->sta_recvpriv); */
410 del_timer_sync(&psta->addba_retry_timer);
412 /* for A-MPDU Rx reordering buffer control, cancel reordering_ctrl_timer */
413 for (i = 0; i < 16 ; i++) {
414 struct list_head *phead, *plist;
415 union recv_frame *prframe;
416 struct __queue *ppending_recvframe_queue;
417 struct __queue *pfree_recv_queue = &padapter->recvpriv.free_recv_queue;
419 preorder_ctrl = &psta->recvreorder_ctrl[i];
421 del_timer_sync(&preorder_ctrl->reordering_ctrl_timer);
424 ppending_recvframe_queue = &preorder_ctrl->pending_recvframe_queue;
426 spin_lock_bh(&ppending_recvframe_queue->lock);
428 phead = get_list_head(ppending_recvframe_queue);
429 plist = get_next(phead);
431 while (!list_empty(phead)) {
432 prframe = LIST_CONTAINOR(plist, union recv_frame, u);
434 plist = get_next(plist);
436 list_del_init(&(prframe->u.hdr.list));
438 rtw_free_recvframe(prframe, pfree_recv_queue);
441 spin_unlock_bh(&ppending_recvframe_queue->lock);
445 if (!(psta->state & WIFI_AP_STATE))
446 rtw_hal_set_odm_var(padapter, HAL_ODM_STA_INFO, psta, false);
449 /* release mac id for non-bc/mc station, */
450 rtw_release_macid(pstapriv->padapter, psta);
453 spin_lock_bh(&pstapriv->asoc_list_lock);
454 list_del_init(&psta->asoc_list);
455 spin_unlock_bh(&pstapriv->asoc_list_lock);
457 spin_lock_bh(&pstapriv->auth_list_lock);
458 if (!list_empty(&psta->auth_list)) {
459 list_del_init(&psta->auth_list);
460 pstapriv->auth_list_cnt--;
462 spin_unlock_bh(&pstapriv->auth_list_lock);
465 psta->sleepq_ac_len = 0;
468 psta->max_sp_len = 0;
474 psta->has_legacy_ac = 0;
476 pstapriv->sta_dz_bitmap &= ~BIT(psta->aid);
477 pstapriv->tim_bitmap &= ~BIT(psta->aid);
479 if ((psta->aid > 0) && (pstapriv->sta_aid[psta->aid - 1] == psta)) {
480 pstapriv->sta_aid[psta->aid - 1] = NULL;
484 psta->under_exist_checking = 0;
486 /* spin_lock_bh(&(pfree_sta_queue->lock)); */
487 list_add_tail(&psta->list, get_list_head(pfree_sta_queue));
488 /* spin_unlock_bh(&(pfree_sta_queue->lock)); */
494 /* free all stainfo which in sta_hash[all] */
495 void rtw_free_all_stainfo(struct adapter *padapter)
497 struct list_head *plist, *phead;
499 struct sta_info *psta = NULL;
500 struct sta_priv *pstapriv = &padapter->stapriv;
501 struct sta_info *pbcmc_stainfo = rtw_get_bcmc_stainfo(padapter);
503 if (pstapriv->asoc_sta_count == 1)
506 spin_lock_bh(&pstapriv->sta_hash_lock);
508 for (index = 0; index < NUM_STA; index++) {
509 phead = &(pstapriv->sta_hash[index]);
510 plist = get_next(phead);
512 while (phead != plist) {
513 psta = LIST_CONTAINOR(plist, struct sta_info, hash_list);
515 plist = get_next(plist);
517 if (pbcmc_stainfo != psta)
518 rtw_free_stainfo(padapter, psta);
523 spin_unlock_bh(&pstapriv->sta_hash_lock);
526 /* any station allocated can be searched by hash list */
527 struct sta_info *rtw_get_stainfo(struct sta_priv *pstapriv, u8 *hwaddr)
529 struct list_head *plist, *phead;
530 struct sta_info *psta = NULL;
533 u8 bc_addr[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
538 if (IS_MCAST(hwaddr))
543 index = wifi_mac_hash(addr);
545 spin_lock_bh(&pstapriv->sta_hash_lock);
547 phead = &(pstapriv->sta_hash[index]);
548 plist = get_next(phead);
551 while (phead != plist) {
553 psta = LIST_CONTAINOR(plist, struct sta_info, hash_list);
555 if ((!memcmp(psta->hwaddr, addr, ETH_ALEN)))
556 /* if found the matched address */
560 plist = get_next(plist);
563 spin_unlock_bh(&pstapriv->sta_hash_lock);
567 u32 rtw_init_bcmc_stainfo(struct adapter *padapter)
570 struct sta_info *psta;
571 struct tx_servq *ptxservq;
573 NDIS_802_11_MAC_ADDRESS bcast_addr = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
575 struct sta_priv *pstapriv = &padapter->stapriv;
576 /* struct __queue *pstapending = &padapter->xmitpriv.bm_pending; */
578 psta = rtw_alloc_stainfo(pstapriv, bcast_addr);
582 RT_TRACE(_module_rtl871x_sta_mgt_c_, _drv_err_, ("rtw_alloc_stainfo fail"));
586 /* default broadcast & multicast use macid 1 */
589 ptxservq = &(psta->sta_xmitpriv.be_q);
595 struct sta_info *rtw_get_bcmc_stainfo(struct adapter *padapter)
597 struct sta_info *psta;
598 struct sta_priv *pstapriv = &padapter->stapriv;
599 u8 bc_addr[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
601 psta = rtw_get_stainfo(pstapriv, bc_addr);
605 u8 rtw_access_ctrl(struct adapter *padapter, u8 *mac_addr)
608 struct list_head *plist, *phead;
609 struct rtw_wlan_acl_node *paclnode;
611 struct sta_priv *pstapriv = &padapter->stapriv;
612 struct wlan_acl_pool *pacl_list = &pstapriv->acl_list;
613 struct __queue *pacl_node_q = &pacl_list->acl_node_q;
615 spin_lock_bh(&(pacl_node_q->lock));
616 phead = get_list_head(pacl_node_q);
617 plist = get_next(phead);
618 while (phead != plist) {
619 paclnode = LIST_CONTAINOR(plist, struct rtw_wlan_acl_node, list);
620 plist = get_next(plist);
622 if (!memcmp(paclnode->addr, mac_addr, ETH_ALEN))
623 if (paclnode->valid == true) {
629 spin_unlock_bh(&(pacl_node_q->lock));
632 if (pacl_list->mode == 1) /* accept unless in deny list */
633 res = (match == true) ? false:true;
635 else if (pacl_list->mode == 2)/* deny unless in accept list */
636 res = (match == true) ? true:false;