staging: r8188eu: remove unnecessary parentheses in core/rtw_sta_mgt.c
[linux-2.6-microblaze.git] / drivers / staging / r8188eu / core / rtw_sta_mgt.c
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2007 - 2011 Realtek Corporation. */
3
4 #define _RTW_STA_MGT_C_
5
6 #include "../include/osdep_service.h"
7 #include "../include/drv_types.h"
8 #include "../include/recv_osdep.h"
9 #include "../include/xmit_osdep.h"
10 #include "../include/mlme_osdep.h"
11 #include "../include/sta_info.h"
12
13 static void _rtw_init_stainfo(struct sta_info *psta)
14 {
15
16         memset((u8 *)psta, 0, sizeof (struct sta_info));
17
18          spin_lock_init(&psta->lock);
19         INIT_LIST_HEAD(&psta->list);
20         INIT_LIST_HEAD(&psta->hash_list);
21         _rtw_init_queue(&psta->sleep_q);
22         psta->sleepq_len = 0;
23
24         _rtw_init_sta_xmit_priv(&psta->sta_xmitpriv);
25         _rtw_init_sta_recv_priv(&psta->sta_recvpriv);
26
27 #ifdef CONFIG_88EU_AP_MODE
28
29         INIT_LIST_HEAD(&psta->asoc_list);
30
31         INIT_LIST_HEAD(&psta->auth_list);
32
33         psta->expire_to = 0;
34
35         psta->flags = 0;
36
37         psta->capability = 0;
38
39         psta->bpairwise_key_installed = false;
40
41 #ifdef CONFIG_88EU_AP_MODE
42         psta->nonerp_set = 0;
43         psta->no_short_slot_time_set = 0;
44         psta->no_short_preamble_set = 0;
45         psta->no_ht_gf_set = 0;
46         psta->no_ht_set = 0;
47         psta->ht_20mhz_set = 0;
48 #endif
49
50         psta->under_exist_checking = 0;
51
52         psta->keep_alive_trycnt = 0;
53
54 #endif  /*  CONFIG_88EU_AP_MODE */
55
56 }
57
58 u32     _rtw_init_sta_priv(struct       sta_priv *pstapriv)
59 {
60         struct sta_info *psta;
61         s32 i;
62
63         pstapriv->pallocated_stainfo_buf = rtw_zvmalloc(sizeof(struct sta_info) * NUM_STA + 4);
64
65         if (!pstapriv->pallocated_stainfo_buf)
66                 return _FAIL;
67
68         pstapriv->pstainfo_buf = pstapriv->pallocated_stainfo_buf + 4 -
69                 ((size_t)(pstapriv->pallocated_stainfo_buf) & 3);
70
71         _rtw_init_queue(&pstapriv->free_sta_queue);
72
73         spin_lock_init(&pstapriv->sta_hash_lock);
74
75         pstapriv->asoc_sta_count = 0;
76         _rtw_init_queue(&pstapriv->sleep_q);
77         _rtw_init_queue(&pstapriv->wakeup_q);
78
79         psta = (struct sta_info *)(pstapriv->pstainfo_buf);
80
81         for (i = 0; i < NUM_STA; i++) {
82                 _rtw_init_stainfo(psta);
83
84                 INIT_LIST_HEAD(&pstapriv->sta_hash[i]);
85
86                 list_add_tail(&psta->list, get_list_head(&pstapriv->free_sta_queue));
87
88                 psta++;
89         }
90
91 #ifdef CONFIG_88EU_AP_MODE
92
93         pstapriv->sta_dz_bitmap = 0;
94         pstapriv->tim_bitmap = 0;
95
96         INIT_LIST_HEAD(&pstapriv->asoc_list);
97         INIT_LIST_HEAD(&pstapriv->auth_list);
98         spin_lock_init(&pstapriv->asoc_list_lock);
99         spin_lock_init(&pstapriv->auth_list_lock);
100         pstapriv->asoc_list_cnt = 0;
101         pstapriv->auth_list_cnt = 0;
102
103         pstapriv->auth_to = 3; /*  3*2 = 6 sec */
104         pstapriv->assoc_to = 3;
105         pstapriv->expire_to = 3; /*  3*2 = 6 sec */
106         pstapriv->max_num_sta = NUM_STA;
107 #endif
108
109         return _SUCCESS;
110 }
111
112 inline int rtw_stainfo_offset(struct sta_priv *stapriv, struct sta_info *sta)
113 {
114         int offset = (((u8 *)sta) - stapriv->pstainfo_buf)/sizeof(struct sta_info);
115
116         if (!stainfo_offset_valid(offset))
117                 DBG_88E("%s invalid offset(%d), out of range!!!", __func__, offset);
118
119         return offset;
120 }
121
122 inline struct sta_info *rtw_get_stainfo_by_offset(struct sta_priv *stapriv, int offset)
123 {
124         if (!stainfo_offset_valid(offset))
125                 DBG_88E("%s invalid offset(%d), out of range!!!", __func__, offset);
126
127         return (struct sta_info *)(stapriv->pstainfo_buf + offset * sizeof(struct sta_info));
128 }
129
130 static void rtw_mfree_sta_priv_lock(struct sta_priv *pstapriv)
131 {
132 #ifdef CONFIG_88EU_AP_MODE
133         struct wlan_acl_pool *pacl_list = &pstapriv->acl_list;
134 #endif
135 }
136
137 u32     _rtw_free_sta_priv(struct       sta_priv *pstapriv)
138 {
139         struct list_head *phead, *plist;
140         struct sta_info *psta = NULL;
141         struct recv_reorder_ctrl *preorder_ctrl;
142         int     index;
143
144         if (pstapriv) {
145                 /*      delete all reordering_ctrl_timer                */
146                 spin_lock_bh(&pstapriv->sta_hash_lock);
147                 for (index = 0; index < NUM_STA; index++) {
148                         phead = &pstapriv->sta_hash[index];
149                         plist = phead->next;
150
151                         while (phead != plist) {
152                                 int i;
153                                 psta = container_of(plist, struct sta_info, hash_list);
154                                 plist = plist->next;
155
156                                 for (i = 0; i < 16; i++) {
157                                         preorder_ctrl = &psta->recvreorder_ctrl[i];
158                                         _cancel_timer_ex(&preorder_ctrl->reordering_ctrl_timer);
159                                 }
160                         }
161                 }
162                 spin_unlock_bh(&pstapriv->sta_hash_lock);
163                 /*===============================*/
164
165                 rtw_mfree_sta_priv_lock(pstapriv);
166
167                 if (pstapriv->pallocated_stainfo_buf)
168                         vfree(pstapriv->pallocated_stainfo_buf);
169                 }
170
171         return _SUCCESS;
172 }
173
174 struct  sta_info *rtw_alloc_stainfo(struct sta_priv *pstapriv, u8 *hwaddr)
175 {
176         s32     index;
177         struct list_head *phash_list;
178         struct sta_info *psta;
179         struct __queue *pfree_sta_queue;
180         struct recv_reorder_ctrl *preorder_ctrl;
181         int i = 0;
182         u16  wRxSeqInitialValue = 0xffff;
183
184         pfree_sta_queue = &pstapriv->free_sta_queue;
185
186         spin_lock_bh(&pfree_sta_queue->lock);
187
188         if (list_empty(&pfree_sta_queue->queue)) {
189                 spin_unlock_bh(&pfree_sta_queue->lock);
190                 psta = NULL;
191         } else {
192                 psta = container_of((&pfree_sta_queue->queue)->next, struct sta_info, list);
193                 list_del_init(&psta->list);
194                 spin_unlock_bh(&pfree_sta_queue->lock);
195                 _rtw_init_stainfo(psta);
196                 memcpy(psta->hwaddr, hwaddr, ETH_ALEN);
197                 index = wifi_mac_hash(hwaddr);
198                 if (index >= NUM_STA) {
199                         psta = NULL;
200                         goto exit;
201                 }
202                 phash_list = &pstapriv->sta_hash[index];
203
204                 spin_lock_bh(&pstapriv->sta_hash_lock);
205
206                 list_add_tail(&psta->hash_list, phash_list);
207
208                 pstapriv->asoc_sta_count++ ;
209
210                 spin_unlock_bh(&pstapriv->sta_hash_lock);
211
212 /*  Commented by Albert 2009/08/13 */
213 /*  For the SMC router, the sequence number of first packet of WPS handshake will be 0. */
214 /*  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. */
215 /*  So, we initialize the tid_rxseq variable as the 0xffff. */
216
217                 for (i = 0; i < 16; i++)
218                         memcpy(&psta->sta_recvpriv.rxcache.tid_rxseq[i], &wRxSeqInitialValue, 2);
219
220                 init_addba_retry_timer(pstapriv->padapter, psta);
221
222                 /* for A-MPDU Rx reordering buffer control */
223                 for (i = 0; i < 16; i++) {
224                         preorder_ctrl = &psta->recvreorder_ctrl[i];
225
226                         preorder_ctrl->padapter = pstapriv->padapter;
227
228                         preorder_ctrl->enable = false;
229
230                         preorder_ctrl->indicate_seq = 0xffff;
231                         preorder_ctrl->wend_b = 0xffff;
232                         preorder_ctrl->wsize_b = 64;/* 64; */
233
234                         _rtw_init_queue(&preorder_ctrl->pending_recvframe_queue);
235
236                         rtw_init_recv_timer(preorder_ctrl);
237                 }
238
239                 /* init for DM */
240                 psta->rssi_stat.UndecoratedSmoothedPWDB = (-1);
241                 psta->rssi_stat.UndecoratedSmoothedCCK = (-1);
242
243                 /* init for the sequence number of received management frame */
244                 psta->RxMgmtFrameSeqNum = 0xffff;
245         }
246
247 exit:
248
249         return psta;
250 }
251
252 /*  using pstapriv->sta_hash_lock to protect */
253 u32     rtw_free_stainfo(struct adapter *padapter, struct sta_info *psta)
254 {
255         int i;
256         struct __queue *pfree_sta_queue;
257         struct recv_reorder_ctrl *preorder_ctrl;
258         struct  sta_xmit_priv   *pstaxmitpriv;
259         struct  xmit_priv       *pxmitpriv = &padapter->xmitpriv;
260         struct  sta_priv *pstapriv = &padapter->stapriv;
261
262         if (!psta)
263                 goto exit;
264
265         pfree_sta_queue = &pstapriv->free_sta_queue;
266
267         pstaxmitpriv = &psta->sta_xmitpriv;
268
269         spin_lock_bh(&pxmitpriv->lock);
270
271         rtw_free_xmitframe_queue(pxmitpriv, &psta->sleep_q);
272         psta->sleepq_len = 0;
273
274         rtw_free_xmitframe_queue(pxmitpriv, &pstaxmitpriv->vo_q.sta_pending);
275
276         list_del_init(&pstaxmitpriv->vo_q.tx_pending);
277
278         rtw_free_xmitframe_queue(pxmitpriv, &pstaxmitpriv->vi_q.sta_pending);
279
280         list_del_init(&pstaxmitpriv->vi_q.tx_pending);
281
282         rtw_free_xmitframe_queue(pxmitpriv, &pstaxmitpriv->bk_q.sta_pending);
283
284         list_del_init(&pstaxmitpriv->bk_q.tx_pending);
285
286         rtw_free_xmitframe_queue(pxmitpriv, &pstaxmitpriv->be_q.sta_pending);
287
288         list_del_init(&pstaxmitpriv->be_q.tx_pending);
289
290         spin_unlock_bh(&pxmitpriv->lock);
291
292         list_del_init(&psta->hash_list);
293         pstapriv->asoc_sta_count--;
294
295         /*  re-init sta_info; 20061114 */
296         _rtw_init_sta_xmit_priv(&psta->sta_xmitpriv);
297         _rtw_init_sta_recv_priv(&psta->sta_recvpriv);
298
299         _cancel_timer_ex(&psta->addba_retry_timer);
300
301         /* for A-MPDU Rx reordering buffer control, cancel reordering_ctrl_timer */
302         for (i = 0; i < 16 ; i++) {
303                 struct list_head *phead, *plist;
304                 struct recv_frame *prframe;
305                 struct __queue *ppending_recvframe_queue;
306                 struct __queue *pfree_recv_queue = &padapter->recvpriv.free_recv_queue;
307
308                 preorder_ctrl = &psta->recvreorder_ctrl[i];
309
310                 _cancel_timer_ex(&preorder_ctrl->reordering_ctrl_timer);
311
312                 ppending_recvframe_queue = &preorder_ctrl->pending_recvframe_queue;
313
314                 spin_lock_bh(&ppending_recvframe_queue->lock);
315
316                 phead =         get_list_head(ppending_recvframe_queue);
317                 plist = phead->next;
318
319                 while (!list_empty(phead)) {
320                         prframe = container_of(plist, struct recv_frame, list);
321
322                         plist = plist->next;
323
324                         list_del_init(&prframe->list);
325
326                         rtw_free_recvframe(prframe, pfree_recv_queue);
327                 }
328
329                 spin_unlock_bh(&ppending_recvframe_queue->lock);
330         }
331
332         if (!(psta->state & WIFI_AP_STATE))
333                 rtw_hal_set_odm_var(padapter, HAL_ODM_STA_INFO, psta, false);
334
335 #ifdef CONFIG_88EU_AP_MODE
336
337         spin_lock_bh(&pstapriv->auth_list_lock);
338         if (!list_empty(&psta->auth_list)) {
339                 list_del_init(&psta->auth_list);
340                 pstapriv->auth_list_cnt--;
341         }
342         spin_unlock_bh(&pstapriv->auth_list_lock);
343
344         psta->expire_to = 0;
345
346         psta->sleepq_ac_len = 0;
347         psta->qos_info = 0;
348
349         psta->max_sp_len = 0;
350         psta->uapsd_bk = 0;
351         psta->uapsd_be = 0;
352         psta->uapsd_vi = 0;
353         psta->uapsd_vo = 0;
354         psta->has_legacy_ac = 0;
355
356         pstapriv->sta_dz_bitmap &= ~BIT(psta->aid);
357         pstapriv->tim_bitmap &= ~BIT(psta->aid);
358
359         if ((psta->aid > 0) && (pstapriv->sta_aid[psta->aid - 1] == psta)) {
360                 pstapriv->sta_aid[psta->aid - 1] = NULL;
361                 psta->aid = 0;
362         }
363
364         psta->under_exist_checking = 0;
365
366 #endif  /*  CONFIG_88EU_AP_MODE */
367
368         spin_lock_bh(&pfree_sta_queue->lock);
369         list_add_tail(&psta->list, get_list_head(pfree_sta_queue));
370         spin_unlock_bh(&pfree_sta_queue->lock);
371
372 exit:
373
374         return _SUCCESS;
375 }
376
377 /*  free all stainfo which in sta_hash[all] */
378 void rtw_free_all_stainfo(struct adapter *padapter)
379 {
380         struct list_head *plist, *phead;
381         s32     index;
382         struct sta_info *psta = NULL;
383         struct  sta_priv *pstapriv = &padapter->stapriv;
384         struct sta_info *pbcmc_stainfo = rtw_get_bcmc_stainfo(padapter);
385
386         if (pstapriv->asoc_sta_count == 1)
387                 return;
388
389         spin_lock_bh(&pstapriv->sta_hash_lock);
390
391         for (index = 0; index < NUM_STA; index++) {
392                 phead = &pstapriv->sta_hash[index];
393                 plist = phead->next;
394
395                 while (phead != plist) {
396                         psta = container_of(plist, struct sta_info, hash_list);
397
398                         plist = plist->next;
399
400                         if (pbcmc_stainfo != psta)
401                                 rtw_free_stainfo(padapter, psta);
402                 }
403         }
404         spin_unlock_bh(&pstapriv->sta_hash_lock);
405 }
406
407 /* any station allocated can be searched by hash list */
408 struct sta_info *rtw_get_stainfo(struct sta_priv *pstapriv, u8 *hwaddr)
409 {
410         struct list_head *plist, *phead;
411         struct sta_info *psta = NULL;
412         u32     index;
413         u8 *addr;
414         u8 bc_addr[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
415
416         if (!hwaddr)
417                 return NULL;
418
419         if (IS_MCAST(hwaddr))
420                 addr = bc_addr;
421         else
422                 addr = hwaddr;
423
424         index = wifi_mac_hash(addr);
425
426         spin_lock_bh(&pstapriv->sta_hash_lock);
427
428         phead = &pstapriv->sta_hash[index];
429         plist = phead->next;
430
431         while (phead != plist) {
432                 psta = container_of(plist, struct sta_info, hash_list);
433
434                 if ((!memcmp(psta->hwaddr, addr, ETH_ALEN))) {
435                         /*  if found the matched address */
436                         break;
437                 }
438                 psta = NULL;
439                 plist = plist->next;
440         }
441
442         spin_unlock_bh(&pstapriv->sta_hash_lock);
443
444         return psta;
445 }
446
447 u32 rtw_init_bcmc_stainfo(struct adapter *padapter)
448 {
449         struct sta_info         *psta;
450         u32 res = _SUCCESS;
451         unsigned char bcast_addr[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
452         struct  sta_priv *pstapriv = &padapter->stapriv;
453
454         psta = rtw_alloc_stainfo(pstapriv, bcast_addr);
455
456         if (!psta) {
457                 res = _FAIL;
458                 goto exit;
459         }
460
461         /*  default broadcast & multicast use macid 1 */
462         psta->mac_id = 1;
463
464 exit:
465
466         return res;
467 }
468
469 struct sta_info *rtw_get_bcmc_stainfo(struct adapter *padapter)
470 {
471         struct sta_info         *psta;
472         struct sta_priv         *pstapriv = &padapter->stapriv;
473         u8 bc_addr[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
474
475          psta = rtw_get_stainfo(pstapriv, bc_addr);
476
477         return psta;
478 }
479
480 u8 rtw_access_ctrl(struct adapter *padapter, u8 *mac_addr)
481 {
482         u8 res = true;
483 #ifdef CONFIG_88EU_AP_MODE
484         struct list_head *plist, *phead;
485         struct rtw_wlan_acl_node *paclnode;
486         u8 match = false;
487         struct sta_priv *pstapriv = &padapter->stapriv;
488         struct wlan_acl_pool *pacl_list = &pstapriv->acl_list;
489         struct __queue *pacl_node_q = &pacl_list->acl_node_q;
490
491         spin_lock_bh(&pacl_node_q->lock);
492         phead = get_list_head(pacl_node_q);
493         plist = phead->next;
494         while (phead != plist) {
495                 paclnode = container_of(plist, struct rtw_wlan_acl_node, list);
496                 plist = plist->next;
497
498                 if (!memcmp(paclnode->addr, mac_addr, ETH_ALEN)) {
499                         if (paclnode->valid) {
500                                 match = true;
501                                 break;
502                         }
503                 }
504         }
505         spin_unlock_bh(&pacl_node_q->lock);
506
507         if (pacl_list->mode == 1)/* accept unless in deny list */
508                 res = (match) ? false : true;
509         else if (pacl_list->mode == 2)/* deny unless in accept list */
510                 res = (match) ? true : false;
511         else
512                  res = true;
513
514 #endif
515
516         return res;
517 }