xref: /OK3568_Linux_fs/external/rkwifibt/drivers/rtl8852bs/phl/phl_sta.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /******************************************************************************
2*4882a593Smuzhiyun  *
3*4882a593Smuzhiyun  * Copyright(c) 2019 Realtek Corporation.
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * This program is free software; you can redistribute it and/or modify it
6*4882a593Smuzhiyun  * under the terms of version 2 of the GNU General Public License as
7*4882a593Smuzhiyun  * published by the Free Software Foundation.
8*4882a593Smuzhiyun  *
9*4882a593Smuzhiyun  * This program is distributed in the hope that it will be useful, but WITHOUT
10*4882a593Smuzhiyun  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11*4882a593Smuzhiyun  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12*4882a593Smuzhiyun  * more details.
13*4882a593Smuzhiyun  *
14*4882a593Smuzhiyun  *****************************************************************************/
15*4882a593Smuzhiyun #define _PHL_STA_C_
16*4882a593Smuzhiyun #include "phl_headers.h"
17*4882a593Smuzhiyun 
18*4882a593Smuzhiyun /*********** macid ctrl section ***********/
19*4882a593Smuzhiyun enum rtw_phl_status
phl_macid_ctrl_init(struct phl_info_t * phl)20*4882a593Smuzhiyun phl_macid_ctrl_init(struct phl_info_t *phl)
21*4882a593Smuzhiyun {
22*4882a593Smuzhiyun 	struct rtw_phl_com_t *phl_com = phl->phl_com;
23*4882a593Smuzhiyun 	struct hal_spec_t *hal_spec = phl_get_ic_spec(phl_com);
24*4882a593Smuzhiyun 	struct macid_ctl_t *macid_ctl = phl_to_mac_ctrl(phl);
25*4882a593Smuzhiyun 	enum rtw_phl_status phl_status = RTW_PHL_STATUS_FAILURE;
26*4882a593Smuzhiyun 	u8 i = 0;
27*4882a593Smuzhiyun 
28*4882a593Smuzhiyun 	/* check invalid value or not */
29*4882a593Smuzhiyun 	if (hal_spec->macid_num == 0) {
30*4882a593Smuzhiyun 		PHL_ERR("Cannot get macid_num of hal\n");
31*4882a593Smuzhiyun 		goto exit;
32*4882a593Smuzhiyun 	}
33*4882a593Smuzhiyun 
34*4882a593Smuzhiyun 	_os_spinlock_init(phl_to_drvpriv(phl), &macid_ctl->lock);
35*4882a593Smuzhiyun 
36*4882a593Smuzhiyun 	macid_ctl->max_num = MIN(hal_spec->macid_num, PHL_MACID_MAX_NUM);
37*4882a593Smuzhiyun 
38*4882a593Smuzhiyun 	PHL_INFO("%s macid max_num:%d\n", __func__, macid_ctl->max_num);
39*4882a593Smuzhiyun 
40*4882a593Smuzhiyun 	for (i = 0; i < MAX_WIFI_ROLE_NUMBER; i++)
41*4882a593Smuzhiyun 		macid_ctl->wrole_bmc[i] = macid_ctl->max_num;
42*4882a593Smuzhiyun 
43*4882a593Smuzhiyun 	phl_status = RTW_PHL_STATUS_SUCCESS;
44*4882a593Smuzhiyun 
45*4882a593Smuzhiyun exit:
46*4882a593Smuzhiyun 	return phl_status;
47*4882a593Smuzhiyun }
48*4882a593Smuzhiyun 
49*4882a593Smuzhiyun enum rtw_phl_status
phl_macid_ctrl_deinit(struct phl_info_t * phl)50*4882a593Smuzhiyun phl_macid_ctrl_deinit(struct phl_info_t *phl)
51*4882a593Smuzhiyun {
52*4882a593Smuzhiyun 	struct macid_ctl_t *macid_ctl = phl_to_mac_ctrl(phl);
53*4882a593Smuzhiyun 
54*4882a593Smuzhiyun 	_os_spinlock_free(phl_to_drvpriv(phl), &macid_ctl->lock);
55*4882a593Smuzhiyun 	macid_ctl->max_num = 0;
56*4882a593Smuzhiyun 
57*4882a593Smuzhiyun 	return RTW_PHL_STATUS_SUCCESS;
58*4882a593Smuzhiyun }
59*4882a593Smuzhiyun 
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun static u8
_phl_macid_is_used(u32 * map,const u16 id)62*4882a593Smuzhiyun _phl_macid_is_used(u32 *map, const u16 id)
63*4882a593Smuzhiyun {
64*4882a593Smuzhiyun 	int map_idx = (int)id / 32;
65*4882a593Smuzhiyun 
66*4882a593Smuzhiyun 	if (map[map_idx] & BIT(id % 32))
67*4882a593Smuzhiyun 		return true;
68*4882a593Smuzhiyun 	else
69*4882a593Smuzhiyun 		return false;
70*4882a593Smuzhiyun }
71*4882a593Smuzhiyun 
72*4882a593Smuzhiyun static void
_phl_macid_map_set(u32 * map,const u16 id)73*4882a593Smuzhiyun _phl_macid_map_set(u32 *map, const u16 id)
74*4882a593Smuzhiyun {
75*4882a593Smuzhiyun 	int map_idx = (int)id / 32;
76*4882a593Smuzhiyun 	map[map_idx] |=  BIT(id % 32);
77*4882a593Smuzhiyun }
78*4882a593Smuzhiyun 
79*4882a593Smuzhiyun static void
_phl_macid_map_clr(u32 * map,const u16 id)80*4882a593Smuzhiyun _phl_macid_map_clr(u32 *map, const u16 id)
81*4882a593Smuzhiyun {
82*4882a593Smuzhiyun 	int map_idx = (int)id / 32;
83*4882a593Smuzhiyun 	map[map_idx] &= ~BIT(id % 32);
84*4882a593Smuzhiyun }
85*4882a593Smuzhiyun 
_phl_wrole_bcmc_id_set(struct macid_ctl_t * macid_ctl,struct rtw_wifi_role_t * wrole,const u16 id)86*4882a593Smuzhiyun static void _phl_wrole_bcmc_id_set(struct macid_ctl_t *macid_ctl,
87*4882a593Smuzhiyun 				struct rtw_wifi_role_t *wrole, const u16 id)
88*4882a593Smuzhiyun {
89*4882a593Smuzhiyun 	macid_ctl->wrole_bmc[wrole->id] = id;
90*4882a593Smuzhiyun }
91*4882a593Smuzhiyun 
92*4882a593Smuzhiyun static enum rtw_phl_status
_phl_alloc_macid(struct phl_info_t * phl_info,struct rtw_phl_stainfo_t * phl_sta)93*4882a593Smuzhiyun _phl_alloc_macid(struct phl_info_t *phl_info,
94*4882a593Smuzhiyun 			struct rtw_phl_stainfo_t *phl_sta)
95*4882a593Smuzhiyun {
96*4882a593Smuzhiyun 	struct macid_ctl_t *mc = phl_to_mac_ctrl(phl_info);
97*4882a593Smuzhiyun 	struct rtw_wifi_role_t *wrole = phl_sta->wrole;
98*4882a593Smuzhiyun 	u8 bc_addr[MAC_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
99*4882a593Smuzhiyun 	u16 mid = 0;
100*4882a593Smuzhiyun 	u16 max_macid_num = 0;
101*4882a593Smuzhiyun 	bool bmc_sta = false;
102*4882a593Smuzhiyun 
103*4882a593Smuzhiyun 	if (wrole == NULL) {
104*4882a593Smuzhiyun 		PHL_ERR("%s wrole=NULL!\n", __func__);
105*4882a593Smuzhiyun 		return RTW_PHL_STATUS_FAILURE;
106*4882a593Smuzhiyun 	}
107*4882a593Smuzhiyun 
108*4882a593Smuzhiyun 	if (_os_mem_cmp(phl_to_drvpriv(phl_info),
109*4882a593Smuzhiyun 			bc_addr, phl_sta->mac_addr, MAC_ALEN) == 0)
110*4882a593Smuzhiyun 		bmc_sta = true;
111*4882a593Smuzhiyun 
112*4882a593Smuzhiyun 	/* TODO
113*4882a593Smuzhiyun 	if (wrole->type == PHL_RTYPE_STATION)
114*4882a593Smuzhiyun 	else if (wrole->type == PHL_RTYPE_AP)*/
115*4882a593Smuzhiyun 
116*4882a593Smuzhiyun 	/*TODO - struct mac_ax_hw_info-> u16 macid_num; need to check */
117*4882a593Smuzhiyun 	max_macid_num = mc->max_num;
118*4882a593Smuzhiyun 	_os_spinlock(phl_to_drvpriv(phl_info), &mc->lock, _bh, NULL);
119*4882a593Smuzhiyun 	for(mid = 0; mid < max_macid_num; mid++) {
120*4882a593Smuzhiyun 		if (!_phl_macid_is_used(mc->used_map, mid)) {
121*4882a593Smuzhiyun 			_phl_macid_map_set(mc->used_map, mid);
122*4882a593Smuzhiyun 			_phl_macid_map_set(&mc->wifi_role_usedmap[wrole->id][0], mid);
123*4882a593Smuzhiyun 			mc->sta[mid] = phl_sta;
124*4882a593Smuzhiyun 
125*4882a593Smuzhiyun 			if (bmc_sta) {
126*4882a593Smuzhiyun 				_phl_macid_map_set(mc->bmc_map, mid);
127*4882a593Smuzhiyun 				_phl_wrole_bcmc_id_set(mc, wrole, mid);
128*4882a593Smuzhiyun 			}
129*4882a593Smuzhiyun 
130*4882a593Smuzhiyun 			break;
131*4882a593Smuzhiyun 		}
132*4882a593Smuzhiyun 	}
133*4882a593Smuzhiyun 	_os_spinunlock(phl_to_drvpriv(phl_info), &mc->lock, _bh, NULL);
134*4882a593Smuzhiyun 
135*4882a593Smuzhiyun 	if (mid == max_macid_num) {
136*4882a593Smuzhiyun 		phl_sta->macid = max_macid_num;
137*4882a593Smuzhiyun 		PHL_ERR("%s cannot get macid\n", __func__);
138*4882a593Smuzhiyun 		return RTW_PHL_STATUS_FAILURE;
139*4882a593Smuzhiyun 	}
140*4882a593Smuzhiyun 
141*4882a593Smuzhiyun 	phl_sta->macid = mid;
142*4882a593Smuzhiyun 
143*4882a593Smuzhiyun 	PHL_INFO("%s allocate %02x:%02x:%02x:%02x:%02x:%02x for macid:%u\n", __func__,
144*4882a593Smuzhiyun 	         phl_sta->mac_addr[0], phl_sta->mac_addr[1], phl_sta->mac_addr[2],
145*4882a593Smuzhiyun 	         phl_sta->mac_addr[3], phl_sta->mac_addr[4], phl_sta->mac_addr[5],
146*4882a593Smuzhiyun 	         phl_sta->macid);
147*4882a593Smuzhiyun 	return RTW_PHL_STATUS_SUCCESS;
148*4882a593Smuzhiyun }
149*4882a593Smuzhiyun 
150*4882a593Smuzhiyun static enum rtw_phl_status
_phl_release_macid(struct phl_info_t * phl_info,struct rtw_phl_stainfo_t * phl_sta)151*4882a593Smuzhiyun _phl_release_macid(struct phl_info_t *phl_info,
152*4882a593Smuzhiyun 			struct rtw_phl_stainfo_t *phl_sta)
153*4882a593Smuzhiyun {
154*4882a593Smuzhiyun 	struct macid_ctl_t *macid_ctl = phl_to_mac_ctrl(phl_info);
155*4882a593Smuzhiyun 	enum rtw_phl_status phl_status = RTW_PHL_STATUS_FAILURE;
156*4882a593Smuzhiyun 	struct rtw_wifi_role_t *wrole = phl_sta->wrole;
157*4882a593Smuzhiyun 	u8 bc_addr[MAC_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
158*4882a593Smuzhiyun 	u16 invalid_macid = macid_ctl->max_num;
159*4882a593Smuzhiyun 
160*4882a593Smuzhiyun 	if (phl_sta->macid >= invalid_macid) {
161*4882a593Smuzhiyun 		PHL_ERR("_phl_release_macid macid error (%d\n)", phl_sta->macid);
162*4882a593Smuzhiyun 		phl_status = RTW_PHL_STATUS_FAILURE;
163*4882a593Smuzhiyun 		goto exit;
164*4882a593Smuzhiyun 	}
165*4882a593Smuzhiyun 
166*4882a593Smuzhiyun 
167*4882a593Smuzhiyun 	_os_spinlock(phl_to_drvpriv(phl_info), &macid_ctl->lock, _bh, NULL);
168*4882a593Smuzhiyun 
169*4882a593Smuzhiyun 	if (!_phl_macid_is_used(macid_ctl->used_map, phl_sta->macid)) {
170*4882a593Smuzhiyun 		PHL_WARN("_phl_release_macid macid unused (%d\n)", phl_sta->macid);
171*4882a593Smuzhiyun 		_os_spinunlock(phl_to_drvpriv(phl_info), &macid_ctl->lock, _bh, NULL);
172*4882a593Smuzhiyun 		phl_status = RTW_PHL_STATUS_FAILURE;
173*4882a593Smuzhiyun 		goto exit;
174*4882a593Smuzhiyun 	}
175*4882a593Smuzhiyun 
176*4882a593Smuzhiyun 
177*4882a593Smuzhiyun 	_phl_macid_map_clr(macid_ctl->used_map, phl_sta->macid);
178*4882a593Smuzhiyun 	_phl_macid_map_clr(&macid_ctl->wifi_role_usedmap[wrole->id][0], phl_sta->macid);
179*4882a593Smuzhiyun 	macid_ctl->sta[phl_sta->macid] = NULL;
180*4882a593Smuzhiyun 
181*4882a593Smuzhiyun 	if (_os_mem_cmp(phl_to_drvpriv(phl_info),
182*4882a593Smuzhiyun 			bc_addr, phl_sta->mac_addr, MAC_ALEN) == 0)
183*4882a593Smuzhiyun 		_phl_macid_map_clr(macid_ctl->bmc_map, phl_sta->macid);
184*4882a593Smuzhiyun 
185*4882a593Smuzhiyun 	phl_status = RTW_PHL_STATUS_SUCCESS;
186*4882a593Smuzhiyun 	_os_spinunlock(phl_to_drvpriv(phl_info), &macid_ctl->lock, _bh, NULL);
187*4882a593Smuzhiyun 
188*4882a593Smuzhiyun exit:
189*4882a593Smuzhiyun 	PHL_INFO("%s release macid:%d - %02x:%02x:%02x:%02x:%02x:%02x \n",
190*4882a593Smuzhiyun 		 __func__,
191*4882a593Smuzhiyun 		 phl_sta->macid,
192*4882a593Smuzhiyun 	         phl_sta->mac_addr[0], phl_sta->mac_addr[1], phl_sta->mac_addr[2],
193*4882a593Smuzhiyun 	         phl_sta->mac_addr[3], phl_sta->mac_addr[4], phl_sta->mac_addr[5]);
194*4882a593Smuzhiyun 
195*4882a593Smuzhiyun 	phl_sta->macid = invalid_macid;
196*4882a593Smuzhiyun 	return phl_status;
197*4882a593Smuzhiyun }
198*4882a593Smuzhiyun 
_phl_get_macid(struct phl_info_t * phl_info,struct rtw_phl_stainfo_t * phl_sta)199*4882a593Smuzhiyun u16 _phl_get_macid(struct phl_info_t *phl_info,
200*4882a593Smuzhiyun 		struct rtw_phl_stainfo_t *phl_sta)
201*4882a593Smuzhiyun {
202*4882a593Smuzhiyun 	/* TODO: macid management */
203*4882a593Smuzhiyun 	return phl_sta->macid;
204*4882a593Smuzhiyun }
205*4882a593Smuzhiyun 
206*4882a593Smuzhiyun /**
207*4882a593Smuzhiyun  * This function export to core layer use
208*4882a593Smuzhiyun  * to get phl role bmc macid
209*4882a593Smuzhiyun  * @phl: see phl_info_t
210*4882a593Smuzhiyun  * @wrole: wifi role
211*4882a593Smuzhiyun  */
212*4882a593Smuzhiyun u16
rtw_phl_wrole_bcmc_id_get(void * phl,struct rtw_wifi_role_t * wrole)213*4882a593Smuzhiyun rtw_phl_wrole_bcmc_id_get(void *phl, struct rtw_wifi_role_t *wrole)
214*4882a593Smuzhiyun {
215*4882a593Smuzhiyun 	struct phl_info_t *phl_info = (struct phl_info_t *)phl;
216*4882a593Smuzhiyun 	struct macid_ctl_t *macid_ctl = phl_to_mac_ctrl(phl_info);
217*4882a593Smuzhiyun 
218*4882a593Smuzhiyun 	return macid_ctl->wrole_bmc[wrole->id];
219*4882a593Smuzhiyun }
220*4882a593Smuzhiyun 
221*4882a593Smuzhiyun /**
222*4882a593Smuzhiyun  * This function export to core layer use
223*4882a593Smuzhiyun  * to get maximum macid number
224*4882a593Smuzhiyun  * @phl: see phl_info_t
225*4882a593Smuzhiyun  */
226*4882a593Smuzhiyun u16
rtw_phl_get_macid_max_num(void * phl)227*4882a593Smuzhiyun rtw_phl_get_macid_max_num(void *phl)
228*4882a593Smuzhiyun {
229*4882a593Smuzhiyun 	struct phl_info_t *phl_info = (struct phl_info_t *)phl;
230*4882a593Smuzhiyun 	struct macid_ctl_t *macid_ctl = phl_to_mac_ctrl(phl_info);
231*4882a593Smuzhiyun 
232*4882a593Smuzhiyun 	return macid_ctl->max_num;
233*4882a593Smuzhiyun }
234*4882a593Smuzhiyun 
235*4882a593Smuzhiyun /**
236*4882a593Smuzhiyun  * This function export to core layer use
237*4882a593Smuzhiyun  * to check macid is bmc or not
238*4882a593Smuzhiyun  * @phl: see phl_info_t
239*4882a593Smuzhiyun  * @macid: macid
240*4882a593Smuzhiyun  */
241*4882a593Smuzhiyun u8
rtw_phl_macid_is_bmc(void * phl,u16 macid)242*4882a593Smuzhiyun rtw_phl_macid_is_bmc(void *phl, u16 macid)
243*4882a593Smuzhiyun {
244*4882a593Smuzhiyun 	struct phl_info_t *phl_info = (struct phl_info_t *)phl;
245*4882a593Smuzhiyun 	struct macid_ctl_t *macid_ctl = phl_to_mac_ctrl(phl_info);
246*4882a593Smuzhiyun 
247*4882a593Smuzhiyun 	if (macid >= macid_ctl->max_num) {
248*4882a593Smuzhiyun 		PHL_ERR("%s macid(%d) is invalid\n", __func__, macid);
249*4882a593Smuzhiyun 		return true;
250*4882a593Smuzhiyun 	}
251*4882a593Smuzhiyun 
252*4882a593Smuzhiyun 	return _phl_macid_is_used(macid_ctl->bmc_map, macid);
253*4882a593Smuzhiyun }
254*4882a593Smuzhiyun 
255*4882a593Smuzhiyun 
256*4882a593Smuzhiyun /**
257*4882a593Smuzhiyun  * This function export to core layer use
258*4882a593Smuzhiyun  * to check macid is used or not
259*4882a593Smuzhiyun  * @phl: see phl_info_t
260*4882a593Smuzhiyun  * @macid: macid
261*4882a593Smuzhiyun  */
262*4882a593Smuzhiyun u8
rtw_phl_macid_is_used(void * phl,u16 macid)263*4882a593Smuzhiyun rtw_phl_macid_is_used(void *phl, u16 macid)
264*4882a593Smuzhiyun {
265*4882a593Smuzhiyun 	struct phl_info_t *phl_info = (struct phl_info_t *)phl;
266*4882a593Smuzhiyun 	struct macid_ctl_t *macid_ctl = phl_to_mac_ctrl(phl_info);
267*4882a593Smuzhiyun 
268*4882a593Smuzhiyun 	if (macid >= macid_ctl->max_num) {
269*4882a593Smuzhiyun 		PHL_ERR("%s macid(%d) is invalid\n", __func__, macid);
270*4882a593Smuzhiyun 		return true;
271*4882a593Smuzhiyun 	}
272*4882a593Smuzhiyun 
273*4882a593Smuzhiyun 
274*4882a593Smuzhiyun 	return _phl_macid_is_used(macid_ctl->used_map, macid);
275*4882a593Smuzhiyun }
276*4882a593Smuzhiyun 
277*4882a593Smuzhiyun /**
278*4882a593Smuzhiyun  * This function is used to
279*4882a593Smuzhiyun  * check macid shared by all wifi role
280*4882a593Smuzhiyun  * @phl: see phl_info_t
281*4882a593Smuzhiyun  * @macid: macid
282*4882a593Smuzhiyun  */
283*4882a593Smuzhiyun 
284*4882a593Smuzhiyun u8
rtw_phl_macid_is_wrole_shared(void * phl,u16 macid)285*4882a593Smuzhiyun rtw_phl_macid_is_wrole_shared(void *phl, u16 macid)
286*4882a593Smuzhiyun {
287*4882a593Smuzhiyun 	struct phl_info_t *phl_info = (struct phl_info_t *)phl;
288*4882a593Smuzhiyun 	struct macid_ctl_t *macid_ctl = phl_to_mac_ctrl(phl_info);
289*4882a593Smuzhiyun 	int i = 0;
290*4882a593Smuzhiyun 	u8 iface_bmp = 0;
291*4882a593Smuzhiyun 
292*4882a593Smuzhiyun 	if (macid >= macid_ctl->max_num) {
293*4882a593Smuzhiyun 		PHL_ERR("%s macid(%d) is invalid\n", __func__, macid);
294*4882a593Smuzhiyun 		return false;
295*4882a593Smuzhiyun 	}
296*4882a593Smuzhiyun 
297*4882a593Smuzhiyun 	for (i = 0; i < MAX_WIFI_ROLE_NUMBER; i++) {
298*4882a593Smuzhiyun 		if (_phl_macid_is_used(&macid_ctl->wifi_role_usedmap[i][0], macid)) {
299*4882a593Smuzhiyun 			if (iface_bmp)
300*4882a593Smuzhiyun 				return true;
301*4882a593Smuzhiyun 			iface_bmp |= BIT(i);
302*4882a593Smuzhiyun 		}
303*4882a593Smuzhiyun 	}
304*4882a593Smuzhiyun 	return false;
305*4882a593Smuzhiyun }
306*4882a593Smuzhiyun 
307*4882a593Smuzhiyun /**
308*4882a593Smuzhiyun  * This function is used to
309*4882a593Smuzhiyun  * check macid not shared by all wifi role
310*4882a593Smuzhiyun  * and belong to wifi role
311*4882a593Smuzhiyun  * @phl: see phl_info_t
312*4882a593Smuzhiyun  * @macid: macid
313*4882a593Smuzhiyun  * @wrole: check id belong to this wifi role
314*4882a593Smuzhiyun  */
315*4882a593Smuzhiyun u8
rtw_phl_macid_is_wrole_specific(void * phl,u16 macid,struct rtw_wifi_role_t * wrole)316*4882a593Smuzhiyun rtw_phl_macid_is_wrole_specific(void *phl,
317*4882a593Smuzhiyun 					u16 macid, struct rtw_wifi_role_t *wrole)
318*4882a593Smuzhiyun {
319*4882a593Smuzhiyun 	struct phl_info_t *phl_info = (struct phl_info_t *)phl;
320*4882a593Smuzhiyun 	struct macid_ctl_t *macid_ctl = phl_to_mac_ctrl(phl_info);
321*4882a593Smuzhiyun 	int i = 0;
322*4882a593Smuzhiyun 	u8 iface_bmp = 0;
323*4882a593Smuzhiyun 
324*4882a593Smuzhiyun 	if (macid >= macid_ctl->max_num) {
325*4882a593Smuzhiyun 		PHL_ERR("%s macid(%d) invalid\n", __func__, macid);
326*4882a593Smuzhiyun 		return false;
327*4882a593Smuzhiyun 	}
328*4882a593Smuzhiyun 
329*4882a593Smuzhiyun 	for (i = 0; i < MAX_WIFI_ROLE_NUMBER; i++) {
330*4882a593Smuzhiyun 		if (_phl_macid_is_used(&macid_ctl->wifi_role_usedmap[i][0], macid)) {
331*4882a593Smuzhiyun 			if (iface_bmp || i != wrole->id)
332*4882a593Smuzhiyun 				return false;
333*4882a593Smuzhiyun 			iface_bmp |= BIT(i);
334*4882a593Smuzhiyun 		}
335*4882a593Smuzhiyun 	}
336*4882a593Smuzhiyun 
337*4882a593Smuzhiyun 	return iface_bmp ? true : false;
338*4882a593Smuzhiyun }
339*4882a593Smuzhiyun 
340*4882a593Smuzhiyun 
341*4882a593Smuzhiyun /*********** stainfo_ctrl section ***********/
342*4882a593Smuzhiyun static enum rtw_phl_status
_phl_stainfo_init(struct phl_info_t * phl_info,struct rtw_phl_stainfo_t * phl_sta)343*4882a593Smuzhiyun _phl_stainfo_init(struct phl_info_t *phl_info,
344*4882a593Smuzhiyun                   struct rtw_phl_stainfo_t *phl_sta)
345*4882a593Smuzhiyun {
346*4882a593Smuzhiyun 	void *drv = phl_to_drvpriv(phl_info);
347*4882a593Smuzhiyun 
348*4882a593Smuzhiyun 	INIT_LIST_HEAD(&phl_sta->list);
349*4882a593Smuzhiyun 	_os_spinlock_init(drv, &phl_sta->tid_rx_lock);
350*4882a593Smuzhiyun 	_os_mem_set(drv, phl_sta->tid_rx, 0, sizeof(phl_sta->tid_rx));
351*4882a593Smuzhiyun 	_os_event_init(drv, &phl_sta->comp_sync);
352*4882a593Smuzhiyun 	_os_init_timer(drv, &phl_sta->reorder_timer,
353*4882a593Smuzhiyun 	               phl_sta_rx_reorder_timer_expired, phl_sta, "reorder_timer");
354*4882a593Smuzhiyun 
355*4882a593Smuzhiyun 	_os_atomic_set(drv, &phl_sta->ps_sta, 0);
356*4882a593Smuzhiyun 
357*4882a593Smuzhiyun 	if (rtw_hal_stainfo_init(phl_info->hal, phl_sta) !=
358*4882a593Smuzhiyun 	    RTW_HAL_STATUS_SUCCESS) {
359*4882a593Smuzhiyun 		PHL_ERR("hal_stainfo_init failed\n");
360*4882a593Smuzhiyun 		FUNCOUT();
361*4882a593Smuzhiyun 		return RTW_PHL_STATUS_FAILURE;
362*4882a593Smuzhiyun 	}
363*4882a593Smuzhiyun 	phl_sta->active = false;
364*4882a593Smuzhiyun 	return RTW_PHL_STATUS_SUCCESS;
365*4882a593Smuzhiyun }
366*4882a593Smuzhiyun 
367*4882a593Smuzhiyun enum rtw_phl_status
_phl_stainfo_deinit(struct phl_info_t * phl_info,struct rtw_phl_stainfo_t * phl_sta)368*4882a593Smuzhiyun _phl_stainfo_deinit(struct phl_info_t *phl_info,
369*4882a593Smuzhiyun 				struct rtw_phl_stainfo_t *phl_sta)
370*4882a593Smuzhiyun {
371*4882a593Smuzhiyun 	void *drv = phl_to_drvpriv(phl_info);
372*4882a593Smuzhiyun 
373*4882a593Smuzhiyun 	_os_release_timer(drv, &phl_sta->reorder_timer);
374*4882a593Smuzhiyun 	_os_spinlock_free(phl_to_drvpriv(phl_info), &phl_sta->tid_rx_lock);
375*4882a593Smuzhiyun 	_os_event_free(drv, &phl_sta->comp_sync);
376*4882a593Smuzhiyun 
377*4882a593Smuzhiyun 	if (rtw_hal_stainfo_deinit(phl_info->hal, phl_sta)!=
378*4882a593Smuzhiyun 					RTW_HAL_STATUS_SUCCESS) {
379*4882a593Smuzhiyun 		PHL_ERR("hal_stainfo_deinit failed\n");
380*4882a593Smuzhiyun 		FUNCOUT();
381*4882a593Smuzhiyun 		return RTW_PHL_STATUS_FAILURE;
382*4882a593Smuzhiyun 	}
383*4882a593Smuzhiyun 	return RTW_PHL_STATUS_SUCCESS;
384*4882a593Smuzhiyun }
385*4882a593Smuzhiyun 
386*4882a593Smuzhiyun enum rtw_phl_status
phl_stainfo_enqueue(struct phl_info_t * phl_info,struct phl_queue * sta_queue,struct rtw_phl_stainfo_t * psta)387*4882a593Smuzhiyun phl_stainfo_enqueue(struct phl_info_t *phl_info,
388*4882a593Smuzhiyun 			 struct phl_queue *sta_queue,
389*4882a593Smuzhiyun 			 struct rtw_phl_stainfo_t *psta)
390*4882a593Smuzhiyun {
391*4882a593Smuzhiyun 	void *drv = phl_to_drvpriv(phl_info);
392*4882a593Smuzhiyun 
393*4882a593Smuzhiyun 	if (!psta)
394*4882a593Smuzhiyun 		return RTW_PHL_STATUS_FAILURE;
395*4882a593Smuzhiyun 
396*4882a593Smuzhiyun 	_os_spinlock(drv, &sta_queue->lock, _bh, NULL);
397*4882a593Smuzhiyun 	list_add_tail(&psta->list, &sta_queue->queue);
398*4882a593Smuzhiyun 	sta_queue->cnt++;
399*4882a593Smuzhiyun 	_os_spinunlock(drv, &sta_queue->lock, _bh, NULL);
400*4882a593Smuzhiyun 	return RTW_PHL_STATUS_SUCCESS;
401*4882a593Smuzhiyun }
402*4882a593Smuzhiyun 
403*4882a593Smuzhiyun struct rtw_phl_stainfo_t *
phl_stainfo_dequeue(struct phl_info_t * phl_info,struct phl_queue * sta_queue)404*4882a593Smuzhiyun phl_stainfo_dequeue(struct phl_info_t *phl_info,
405*4882a593Smuzhiyun 			struct phl_queue *sta_queue)
406*4882a593Smuzhiyun {
407*4882a593Smuzhiyun 	struct rtw_phl_stainfo_t *psta = NULL;
408*4882a593Smuzhiyun 	void *drv = phl_to_drvpriv(phl_info);
409*4882a593Smuzhiyun 
410*4882a593Smuzhiyun 	_os_spinlock(drv, &sta_queue->lock, _bh, NULL);
411*4882a593Smuzhiyun 	if (list_empty(&sta_queue->queue)) {
412*4882a593Smuzhiyun 		psta = NULL;
413*4882a593Smuzhiyun 	} else {
414*4882a593Smuzhiyun 		psta = list_first_entry(&sta_queue->queue,
415*4882a593Smuzhiyun 					struct rtw_phl_stainfo_t, list);
416*4882a593Smuzhiyun 
417*4882a593Smuzhiyun 		list_del(&psta->list);
418*4882a593Smuzhiyun 		sta_queue->cnt--;
419*4882a593Smuzhiyun 	}
420*4882a593Smuzhiyun 	_os_spinunlock(drv, &sta_queue->lock, _bh, NULL);
421*4882a593Smuzhiyun 
422*4882a593Smuzhiyun 	return psta;
423*4882a593Smuzhiyun }
424*4882a593Smuzhiyun 
425*4882a593Smuzhiyun enum rtw_phl_status
phl_stainfo_queue_del(struct phl_info_t * phl_info,struct phl_queue * sta_queue,struct rtw_phl_stainfo_t * psta)426*4882a593Smuzhiyun phl_stainfo_queue_del(struct phl_info_t *phl_info,
427*4882a593Smuzhiyun 			 struct phl_queue *sta_queue,
428*4882a593Smuzhiyun 			 struct rtw_phl_stainfo_t *psta)
429*4882a593Smuzhiyun {
430*4882a593Smuzhiyun 	void *drv = phl_to_drvpriv(phl_info);
431*4882a593Smuzhiyun 
432*4882a593Smuzhiyun 	if (!psta)
433*4882a593Smuzhiyun 		return RTW_PHL_STATUS_FAILURE;
434*4882a593Smuzhiyun 
435*4882a593Smuzhiyun 	_os_spinlock(drv, &sta_queue->lock, _bh, NULL);
436*4882a593Smuzhiyun 	if (sta_queue->cnt) {
437*4882a593Smuzhiyun 		list_del(&psta->list);
438*4882a593Smuzhiyun 		sta_queue->cnt--;
439*4882a593Smuzhiyun 	}
440*4882a593Smuzhiyun 	_os_spinunlock(drv, &sta_queue->lock, _bh, NULL);
441*4882a593Smuzhiyun 	return RTW_PHL_STATUS_SUCCESS;
442*4882a593Smuzhiyun }
443*4882a593Smuzhiyun 
444*4882a593Smuzhiyun struct rtw_phl_stainfo_t *
phl_stainfo_queue_search(struct phl_info_t * phl_info,struct phl_queue * sta_queue,u8 * addr)445*4882a593Smuzhiyun phl_stainfo_queue_search(struct phl_info_t *phl_info,
446*4882a593Smuzhiyun 			 struct phl_queue *sta_queue,
447*4882a593Smuzhiyun 			 u8 *addr)
448*4882a593Smuzhiyun {
449*4882a593Smuzhiyun 	struct rtw_phl_stainfo_t *sta = NULL;
450*4882a593Smuzhiyun 	_os_list *sta_list = &sta_queue->queue;
451*4882a593Smuzhiyun 	void *drv = phl_to_drvpriv(phl_info);
452*4882a593Smuzhiyun 	bool sta_found = false;
453*4882a593Smuzhiyun 
454*4882a593Smuzhiyun 	_os_spinlock(drv, &sta_queue->lock, _bh, NULL);
455*4882a593Smuzhiyun 	if (list_empty(sta_list) == true)
456*4882a593Smuzhiyun 		goto _exit;
457*4882a593Smuzhiyun 
458*4882a593Smuzhiyun 	phl_list_for_loop(sta, struct rtw_phl_stainfo_t, sta_list, list) {
459*4882a593Smuzhiyun 		if (_os_mem_cmp(phl_to_drvpriv(phl_info),
460*4882a593Smuzhiyun 			sta->mac_addr, addr, MAC_ALEN) == 0) {
461*4882a593Smuzhiyun 			sta_found = true;
462*4882a593Smuzhiyun 			break;
463*4882a593Smuzhiyun 		}
464*4882a593Smuzhiyun 	}
465*4882a593Smuzhiyun _exit:
466*4882a593Smuzhiyun 	_os_spinunlock(drv, &sta_queue->lock, _bh, NULL);
467*4882a593Smuzhiyun 
468*4882a593Smuzhiyun 	if (sta_found == false)
469*4882a593Smuzhiyun 		sta = NULL;
470*4882a593Smuzhiyun 
471*4882a593Smuzhiyun 	return sta;
472*4882a593Smuzhiyun }
473*4882a593Smuzhiyun 
474*4882a593Smuzhiyun struct rtw_phl_stainfo_t *
phl_stainfo_queue_get_first(struct phl_info_t * phl_info,struct phl_queue * sta_queue)475*4882a593Smuzhiyun phl_stainfo_queue_get_first(struct phl_info_t *phl_info,
476*4882a593Smuzhiyun 			 struct phl_queue *sta_queue)
477*4882a593Smuzhiyun {
478*4882a593Smuzhiyun 
479*4882a593Smuzhiyun 	_os_list *sta_list = &sta_queue->queue;
480*4882a593Smuzhiyun 	void *drv = phl_to_drvpriv(phl_info);
481*4882a593Smuzhiyun 	struct rtw_phl_stainfo_t *sta = NULL;
482*4882a593Smuzhiyun 
483*4882a593Smuzhiyun 	/* first sta info in assoc_sta_queu is self sta info */
484*4882a593Smuzhiyun 	_os_spinlock(drv, &sta_queue->lock, _bh, NULL);
485*4882a593Smuzhiyun 	if (list_empty(sta_list) == true)
486*4882a593Smuzhiyun 		goto _exit;
487*4882a593Smuzhiyun 
488*4882a593Smuzhiyun 	sta = list_first_entry(sta_list, struct rtw_phl_stainfo_t, list);
489*4882a593Smuzhiyun _exit :
490*4882a593Smuzhiyun 	_os_spinunlock(drv, &sta_queue->lock, _bh, NULL);
491*4882a593Smuzhiyun 
492*4882a593Smuzhiyun 	return sta;
493*4882a593Smuzhiyun }
494*4882a593Smuzhiyun 
495*4882a593Smuzhiyun enum rtw_phl_status
phl_stainfo_ctrl_deinie(struct phl_info_t * phl_info)496*4882a593Smuzhiyun phl_stainfo_ctrl_deinie(struct phl_info_t *phl_info)
497*4882a593Smuzhiyun {
498*4882a593Smuzhiyun 	struct stainfo_ctl_t *sta_ctrl = phl_to_sta_ctrl(phl_info);
499*4882a593Smuzhiyun 	void *drv = phl_to_drvpriv(phl_info);
500*4882a593Smuzhiyun 	struct rtw_phl_stainfo_t *psta = NULL;
501*4882a593Smuzhiyun 	struct phl_queue *fsta_queue = &sta_ctrl->free_sta_queue;
502*4882a593Smuzhiyun 
503*4882a593Smuzhiyun 	FUNCIN();
504*4882a593Smuzhiyun 	do {
505*4882a593Smuzhiyun 		psta = phl_stainfo_dequeue(phl_info, fsta_queue);
506*4882a593Smuzhiyun 		if (psta)
507*4882a593Smuzhiyun 			_phl_stainfo_deinit(phl_info, psta);
508*4882a593Smuzhiyun 
509*4882a593Smuzhiyun 	}while (psta != NULL);
510*4882a593Smuzhiyun 
511*4882a593Smuzhiyun 	pq_deinit(drv, fsta_queue);
512*4882a593Smuzhiyun 
513*4882a593Smuzhiyun 	if (sta_ctrl->allocated_stainfo_buf)
514*4882a593Smuzhiyun 		_os_mem_free(drv, sta_ctrl->allocated_stainfo_buf,
515*4882a593Smuzhiyun 					sta_ctrl->allocated_stainfo_sz);
516*4882a593Smuzhiyun 	FUNCOUT();
517*4882a593Smuzhiyun 	return RTW_PHL_STATUS_SUCCESS;
518*4882a593Smuzhiyun }
519*4882a593Smuzhiyun 
520*4882a593Smuzhiyun enum rtw_phl_status
phl_stainfo_ctrl_init(struct phl_info_t * phl_info)521*4882a593Smuzhiyun phl_stainfo_ctrl_init(struct phl_info_t *phl_info)
522*4882a593Smuzhiyun {
523*4882a593Smuzhiyun 	struct stainfo_ctl_t *sta_ctrl = phl_to_sta_ctrl(phl_info);
524*4882a593Smuzhiyun 	void *drv = phl_to_drvpriv(phl_info);
525*4882a593Smuzhiyun 	struct rtw_phl_stainfo_t *psta = NULL;
526*4882a593Smuzhiyun 	enum rtw_phl_status pstatus = RTW_PHL_STATUS_FAILURE;
527*4882a593Smuzhiyun 	struct phl_queue *fsta_queue = NULL;
528*4882a593Smuzhiyun 
529*4882a593Smuzhiyun 	u16 i;
530*4882a593Smuzhiyun 	bool sta_init_fail = false;
531*4882a593Smuzhiyun 
532*4882a593Smuzhiyun 	FUNCIN();
533*4882a593Smuzhiyun 	sta_ctrl->phl_info = phl_info;
534*4882a593Smuzhiyun 
535*4882a593Smuzhiyun 	sta_ctrl->allocated_stainfo_sz = sizeof(struct rtw_phl_stainfo_t) * PHL_MAX_STA_NUM;
536*4882a593Smuzhiyun 
537*4882a593Smuzhiyun 	#ifdef MEM_ALIGNMENT
538*4882a593Smuzhiyun 	sta_ctrl->allocated_stainfo_sz += MEM_ALIGNMENT_OFFSET;
539*4882a593Smuzhiyun 	#endif
540*4882a593Smuzhiyun 
541*4882a593Smuzhiyun 	sta_ctrl->allocated_stainfo_buf =
542*4882a593Smuzhiyun 			_os_mem_alloc(drv, sta_ctrl->allocated_stainfo_sz);
543*4882a593Smuzhiyun 
544*4882a593Smuzhiyun 	if (!sta_ctrl->allocated_stainfo_buf) {
545*4882a593Smuzhiyun 		PHL_ERR("allocate stainfo buf failed\n");
546*4882a593Smuzhiyun 		goto _exit;
547*4882a593Smuzhiyun 	}
548*4882a593Smuzhiyun 	sta_ctrl->stainfo_buf = sta_ctrl->allocated_stainfo_buf;
549*4882a593Smuzhiyun 
550*4882a593Smuzhiyun 	#ifdef MEM_ALIGNMENT
551*4882a593Smuzhiyun 	if (sta_ctrl->stainfo_buf & MEM_ALIGNMENT_PADDING)
552*4882a593Smuzhiyun 		sta_ctrl->stainfo_buf += MEM_ALIGNMENT_OFFSET -
553*4882a593Smuzhiyun 			(sta_ctrl->stainfo_buf & MEM_ALIGNMENT_PADDING);
554*4882a593Smuzhiyun 	#endif
555*4882a593Smuzhiyun 
556*4882a593Smuzhiyun 	fsta_queue = &sta_ctrl->free_sta_queue;
557*4882a593Smuzhiyun 
558*4882a593Smuzhiyun 	pq_init(drv, fsta_queue);
559*4882a593Smuzhiyun 	psta = (struct rtw_phl_stainfo_t *)(sta_ctrl->stainfo_buf);
560*4882a593Smuzhiyun 
561*4882a593Smuzhiyun 	for (i = 0; i < PHL_MAX_STA_NUM; i++) {
562*4882a593Smuzhiyun 		if (_phl_stainfo_init(phl_info, psta) != RTW_PHL_STATUS_SUCCESS) {
563*4882a593Smuzhiyun 			sta_init_fail = true;
564*4882a593Smuzhiyun 			break;
565*4882a593Smuzhiyun 		}
566*4882a593Smuzhiyun 		phl_stainfo_enqueue(phl_info, fsta_queue, psta);
567*4882a593Smuzhiyun 		psta++;
568*4882a593Smuzhiyun 	}
569*4882a593Smuzhiyun 
570*4882a593Smuzhiyun 	if (sta_init_fail == true) {
571*4882a593Smuzhiyun 		PHL_ERR("sta_init failed\n");
572*4882a593Smuzhiyun 		phl_stainfo_ctrl_deinie(phl_info);
573*4882a593Smuzhiyun 		goto _exit;
574*4882a593Smuzhiyun 	}
575*4882a593Smuzhiyun 	PHL_DUMP_STACTRL_EX(phl_info);
576*4882a593Smuzhiyun 
577*4882a593Smuzhiyun 	pstatus = RTW_PHL_STATUS_SUCCESS;
578*4882a593Smuzhiyun _exit:
579*4882a593Smuzhiyun 	FUNCOUT();
580*4882a593Smuzhiyun 	return pstatus;
581*4882a593Smuzhiyun }
582*4882a593Smuzhiyun 
583*4882a593Smuzhiyun /*********** phl stainfo section ***********/
584*4882a593Smuzhiyun #ifdef DBG_PHL_STAINFO
585*4882a593Smuzhiyun void
phl_dump_stactrl(const char * caller,const int line,bool show_caller,struct phl_info_t * phl_info)586*4882a593Smuzhiyun phl_dump_stactrl(const char *caller, const int line, bool show_caller,
587*4882a593Smuzhiyun 						struct phl_info_t *phl_info)
588*4882a593Smuzhiyun {
589*4882a593Smuzhiyun 	struct rtw_phl_com_t *phl_com = phl_info->phl_com;
590*4882a593Smuzhiyun 	u8 ridx = MAX_WIFI_ROLE_NUMBER;
591*4882a593Smuzhiyun 	struct rtw_wifi_role_t *role;
592*4882a593Smuzhiyun 	struct stainfo_ctl_t *sta_ctrl = NULL;
593*4882a593Smuzhiyun 
594*4882a593Smuzhiyun 	sta_ctrl = phl_to_sta_ctrl(phl_info);
595*4882a593Smuzhiyun 
596*4882a593Smuzhiyun 	if (show_caller)
597*4882a593Smuzhiyun 		PHL_INFO("[PSTA] ###### FUN - %s LINE - %d #######\n", caller, line);
598*4882a593Smuzhiyun 	PHL_INFO("[PSTA] PHL_MAX_STA_NUM:%d\n", PHL_MAX_STA_NUM);
599*4882a593Smuzhiyun 	PHL_INFO("[PSTA] sta_ctrl - q_cnt :%d\n", sta_ctrl->free_sta_queue.cnt);
600*4882a593Smuzhiyun 	for (ridx = 0; ridx < MAX_WIFI_ROLE_NUMBER; ridx++) {
601*4882a593Smuzhiyun 		role = &(phl_com->wifi_roles[ridx]);
602*4882a593Smuzhiyun 		PHL_INFO("[PSTA] wrole_%d asoc_q cnt :%d\n",
603*4882a593Smuzhiyun 				ridx, role->assoc_sta_queue.cnt);
604*4882a593Smuzhiyun 	}
605*4882a593Smuzhiyun 	if (show_caller)
606*4882a593Smuzhiyun 		PHL_INFO("#################################\n");
607*4882a593Smuzhiyun }
608*4882a593Smuzhiyun 
_phl_dump_stainfo(struct rtw_phl_stainfo_t * phl_sta)609*4882a593Smuzhiyun static void _phl_dump_stainfo(struct rtw_phl_stainfo_t *phl_sta)
610*4882a593Smuzhiyun {
611*4882a593Smuzhiyun 	PHL_INFO("\t[STA] MAC-ID:%d, AID:%d, MAC-ADDR:%02x-%02x-%02x-%02x-%02x-%02x, Active:%s\n",
612*4882a593Smuzhiyun 			phl_sta->macid, phl_sta->aid,
613*4882a593Smuzhiyun 			phl_sta->mac_addr[0],phl_sta->mac_addr[1],phl_sta->mac_addr[2],
614*4882a593Smuzhiyun 			phl_sta->mac_addr[3],phl_sta->mac_addr[4],phl_sta->mac_addr[5],
615*4882a593Smuzhiyun 			(phl_sta->active) ? "Y" : "N");
616*4882a593Smuzhiyun 	PHL_INFO("\t[STA] WROLE-IDX:%d wlan_mode:0x%02x\n", phl_sta->wrole->id, phl_sta->wmode);
617*4882a593Smuzhiyun 	PHL_DUMP_CHAN_DEF(&phl_sta->chandef);
618*4882a593Smuzhiyun 
619*4882a593Smuzhiyun 	/****** statistic ******/
620*4882a593Smuzhiyun 	PHL_INFO("\t[STA] TP -[Tx:%d Rx :%d BI:N/A] (KBits)\n",
621*4882a593Smuzhiyun 		phl_sta->stats.tx_tp_kbits, phl_sta->stats.rx_tp_kbits);
622*4882a593Smuzhiyun 	PHL_INFO("\t[STA] Total -[Tx:%llu Rx :%llu BI:N/A] (Bytes)\n",
623*4882a593Smuzhiyun 		phl_sta->stats.tx_byte_total, phl_sta->stats.rx_byte_total);
624*4882a593Smuzhiyun 	/****** asoc_cap ******/
625*4882a593Smuzhiyun 	/****** protect ******/
626*4882a593Smuzhiyun 	/****** sec_mode ******/
627*4882a593Smuzhiyun 	/****** rssi_stat ******/
628*4882a593Smuzhiyun 	PHL_INFO("\t\t[HAL STA] rssi:%d assoc_rssi:%d, ofdm:%d, cck:%d, rssi_ma:%d, ma_rssi:%d\n",
629*4882a593Smuzhiyun 			(phl_sta->hal_sta->rssi_stat.rssi >> 1), phl_sta->hal_sta->rssi_stat.assoc_rssi,
630*4882a593Smuzhiyun 			(phl_sta->hal_sta->rssi_stat.rssi_ofdm >> 1), (phl_sta->hal_sta->rssi_stat.rssi_cck >> 1),
631*4882a593Smuzhiyun 			(phl_sta->hal_sta->rssi_stat.rssi_ma >> 5), phl_sta->hal_sta->rssi_stat.ma_rssi);
632*4882a593Smuzhiyun 
633*4882a593Smuzhiyun 	/****** ra_info ******/
634*4882a593Smuzhiyun 	PHL_INFO("\t\t[HAL STA] - RA info\n");
635*4882a593Smuzhiyun 
636*4882a593Smuzhiyun 	PHL_INFO("\t\t[HAL STA] Tx rate:0x%04x ra_bw_mode:%d, curr_tx_bw:%d\n",
637*4882a593Smuzhiyun 				phl_sta->hal_sta->ra_info.curr_tx_rate,
638*4882a593Smuzhiyun 				phl_sta->hal_sta->ra_info.ra_bw_mode,
639*4882a593Smuzhiyun 				phl_sta->hal_sta->ra_info.curr_tx_bw);
640*4882a593Smuzhiyun 
641*4882a593Smuzhiyun 	PHL_INFO("\t\t[HAL STA] dis_ra:%s ra_registered:%s\n",
642*4882a593Smuzhiyun 				(phl_sta->hal_sta->ra_info.dis_ra) ? "Y" : "N",
643*4882a593Smuzhiyun 				(phl_sta->hal_sta->ra_info.ra_registered) ? "Y" : "N");
644*4882a593Smuzhiyun 
645*4882a593Smuzhiyun 	PHL_INFO("\t\t[HAL STA] ra_mask:0x%08llx cur_ra_mask:0x%08llx, retry_ratio:%d\n",
646*4882a593Smuzhiyun 				phl_sta->hal_sta->ra_info.ra_mask,
647*4882a593Smuzhiyun 				phl_sta->hal_sta->ra_info.cur_ra_mask,
648*4882a593Smuzhiyun 				phl_sta->hal_sta->ra_info.curr_retry_ratio);
649*4882a593Smuzhiyun 	/****** ra_info - Report ******/
650*4882a593Smuzhiyun 	PHL_INFO("\t\t[HAL STA] RA Report: gi_ltf:%d rate_mode:%d, bw:%d, mcs_ss_idx:%d\n",
651*4882a593Smuzhiyun 				phl_sta->hal_sta->ra_info.rpt_rt_i.gi_ltf,
652*4882a593Smuzhiyun 				phl_sta->hal_sta->ra_info.rpt_rt_i.mode,
653*4882a593Smuzhiyun 				phl_sta->hal_sta->ra_info.rpt_rt_i.bw,
654*4882a593Smuzhiyun 				phl_sta->hal_sta->ra_info.rpt_rt_i.mcs_ss_idx);
655*4882a593Smuzhiyun 
656*4882a593Smuzhiyun 	PHL_INFO("\t\t[HAL STA] HAL rx_ok_cnt:%d rx_err_cnt:%d, rx_rate_plurality:%d\n\n",
657*4882a593Smuzhiyun 				phl_sta->hal_sta->trx_stat.rx_ok_cnt,
658*4882a593Smuzhiyun 				phl_sta->hal_sta->trx_stat.rx_err_cnt,
659*4882a593Smuzhiyun 				phl_sta->hal_sta->trx_stat.rx_rate_plurality);
660*4882a593Smuzhiyun 
661*4882a593Smuzhiyun }
phl_dump_stainfo_all(const char * caller,const int line,bool show_caller,struct phl_info_t * phl_info)662*4882a593Smuzhiyun void phl_dump_stainfo_all(const char *caller, const int line, bool show_caller,
663*4882a593Smuzhiyun 				struct phl_info_t *phl_info)
664*4882a593Smuzhiyun {
665*4882a593Smuzhiyun 	struct macid_ctl_t *macid_ctl = phl_to_mac_ctrl(phl_info);
666*4882a593Smuzhiyun 	struct rtw_phl_stainfo_t *phl_sta = NULL;
667*4882a593Smuzhiyun 	u16 max_macid_num = 0;
668*4882a593Smuzhiyun 	u16 mid = 0;
669*4882a593Smuzhiyun 
670*4882a593Smuzhiyun 	if (show_caller)
671*4882a593Smuzhiyun 		PHL_INFO("###### FUN - %s LINE - %d #######\n", caller, line);
672*4882a593Smuzhiyun 
673*4882a593Smuzhiyun 	max_macid_num = macid_ctl->max_num;
674*4882a593Smuzhiyun 	PHL_INFO("max_macid_num:%d\n", max_macid_num);
675*4882a593Smuzhiyun 	_os_spinlock(phl_to_drvpriv(phl_info), &macid_ctl->lock, _bh, NULL);
676*4882a593Smuzhiyun 	for(mid = 0; mid < max_macid_num; mid++) {
677*4882a593Smuzhiyun 		if (_phl_macid_is_used(macid_ctl->used_map, mid)) {
678*4882a593Smuzhiyun 			phl_sta = macid_ctl->sta[mid];
679*4882a593Smuzhiyun 			if (phl_sta)
680*4882a593Smuzhiyun 				_phl_dump_stainfo(phl_sta);
681*4882a593Smuzhiyun 		}
682*4882a593Smuzhiyun 	}
683*4882a593Smuzhiyun 	_os_spinunlock(phl_to_drvpriv(phl_info), &macid_ctl->lock, _bh, NULL);
684*4882a593Smuzhiyun 
685*4882a593Smuzhiyun 	if (show_caller)
686*4882a593Smuzhiyun 		PHL_INFO("#################################\n");
687*4882a593Smuzhiyun }
688*4882a593Smuzhiyun 
689*4882a593Smuzhiyun const char *const _rtype_str[] = {
690*4882a593Smuzhiyun 	"NONE",
691*4882a593Smuzhiyun 	"STA",
692*4882a593Smuzhiyun 	"AP",
693*4882a593Smuzhiyun 	"VAP",
694*4882a593Smuzhiyun 	"ADHOC",
695*4882a593Smuzhiyun 	"MASTER",
696*4882a593Smuzhiyun 	"MESH",
697*4882a593Smuzhiyun 	"MONITOR",
698*4882a593Smuzhiyun 	"PD",
699*4882a593Smuzhiyun 	"GC",
700*4882a593Smuzhiyun 	"GO",
701*4882a593Smuzhiyun 	"TDLS",
702*4882a593Smuzhiyun 	"NAN",
703*4882a593Smuzhiyun 	"NONE"
704*4882a593Smuzhiyun };
705*4882a593Smuzhiyun 
phl_dump_stainfo_per_role(const char * caller,const int line,bool show_caller,struct phl_info_t * phl_info,struct rtw_wifi_role_t * wrole)706*4882a593Smuzhiyun void phl_dump_stainfo_per_role(const char *caller, const int line, bool show_caller,
707*4882a593Smuzhiyun 				struct phl_info_t *phl_info, struct rtw_wifi_role_t *wrole)
708*4882a593Smuzhiyun {
709*4882a593Smuzhiyun 	void *drv = phl_to_drvpriv(phl_info);
710*4882a593Smuzhiyun 	struct rtw_phl_stainfo_t *sta = NULL;
711*4882a593Smuzhiyun 	int sta_cnt = 0;
712*4882a593Smuzhiyun 
713*4882a593Smuzhiyun 	if (show_caller)
714*4882a593Smuzhiyun 		PHL_INFO("[STA] ###### FUN - %s LINE - %d #######\n", caller, line);
715*4882a593Smuzhiyun 
716*4882a593Smuzhiyun 	PHL_INFO("WR_IDX:%d RTYPE:%s, mac-addr:%02x-%02x-%02x-%02x-%02x-%02x\n",
717*4882a593Smuzhiyun 			wrole->id,
718*4882a593Smuzhiyun 			_rtype_str[wrole->type],
719*4882a593Smuzhiyun 			wrole->mac_addr[0], wrole->mac_addr[1], wrole->mac_addr[2],
720*4882a593Smuzhiyun 			wrole->mac_addr[3], wrole->mac_addr[4], wrole->mac_addr[5]);
721*4882a593Smuzhiyun 
722*4882a593Smuzhiyun 	_os_spinlock(drv, &wrole->assoc_sta_queue.lock, _bh, NULL);
723*4882a593Smuzhiyun 
724*4882a593Smuzhiyun 	if (wrole->type == PHL_RTYPE_STATION && wrole->mstate == MLME_LINKED)
725*4882a593Smuzhiyun 		sta_cnt = 1;
726*4882a593Smuzhiyun 	else if (wrole->type == PHL_RTYPE_TDLS)
727*4882a593Smuzhiyun 		sta_cnt = wrole->assoc_sta_queue.cnt;
728*4882a593Smuzhiyun 	else
729*4882a593Smuzhiyun 		sta_cnt = wrole->assoc_sta_queue.cnt - 1;
730*4882a593Smuzhiyun 
731*4882a593Smuzhiyun 	PHL_INFO("assoced STA num: %d\n", sta_cnt);
732*4882a593Smuzhiyun 	phl_list_for_loop(sta, struct rtw_phl_stainfo_t, &wrole->assoc_sta_queue.queue, list) {
733*4882a593Smuzhiyun 		if (sta)
734*4882a593Smuzhiyun 			_phl_dump_stainfo(sta);
735*4882a593Smuzhiyun 	}
736*4882a593Smuzhiyun 	_os_spinunlock(drv, &wrole->assoc_sta_queue.lock, _bh, NULL);
737*4882a593Smuzhiyun 
738*4882a593Smuzhiyun 	if (show_caller)
739*4882a593Smuzhiyun 		PHL_INFO("#################################\n");
740*4882a593Smuzhiyun }
741*4882a593Smuzhiyun 
rtw_phl_sta_dump_info(void * phl,bool show_caller,struct rtw_wifi_role_t * wr,u8 mode)742*4882a593Smuzhiyun void rtw_phl_sta_dump_info(void *phl, bool show_caller, struct rtw_wifi_role_t *wr, u8 mode)
743*4882a593Smuzhiyun {
744*4882a593Smuzhiyun 	struct phl_info_t *phl_info = (struct phl_info_t *)phl;
745*4882a593Smuzhiyun 
746*4882a593Smuzhiyun 	if (mode == 1) {
747*4882a593Smuzhiyun 		if (show_caller) {
748*4882a593Smuzhiyun 			PHL_DUMP_STACTRL_EX(phl_info);
749*4882a593Smuzhiyun 		} else {
750*4882a593Smuzhiyun 			PHL_DUMP_STACTRL(phl_info);
751*4882a593Smuzhiyun 		}
752*4882a593Smuzhiyun 	} else if (mode == 2) {
753*4882a593Smuzhiyun 		if (show_caller) {
754*4882a593Smuzhiyun 			PHL_DUMP_STAINFO_EX(phl_info);
755*4882a593Smuzhiyun 		} else {
756*4882a593Smuzhiyun 			PHL_DUMP_STAINFO(phl_info);
757*4882a593Smuzhiyun 		}
758*4882a593Smuzhiyun 	} else if (mode == 3) {
759*4882a593Smuzhiyun 		if (show_caller) {
760*4882a593Smuzhiyun 			PHL_DUMP_ROLE_STAINFO_EX(phl_info, wr);
761*4882a593Smuzhiyun 		} else {
762*4882a593Smuzhiyun 			PHL_DUMP_ROLE_STAINFO(phl_info, wr);
763*4882a593Smuzhiyun 		}
764*4882a593Smuzhiyun 	} else {
765*4882a593Smuzhiyun 		if (show_caller) {
766*4882a593Smuzhiyun 			PHL_DUMP_STACTRL_EX(phl_info);
767*4882a593Smuzhiyun 			PHL_DUMP_STAINFO_EX(phl_info);
768*4882a593Smuzhiyun 			PHL_DUMP_ROLE_STAINFO_EX(phl_info, wr);
769*4882a593Smuzhiyun 		}
770*4882a593Smuzhiyun 		else {
771*4882a593Smuzhiyun 			PHL_DUMP_STACTRL(phl_info);
772*4882a593Smuzhiyun 			PHL_DUMP_STAINFO(phl_info);
773*4882a593Smuzhiyun 			PHL_DUMP_ROLE_STAINFO(phl_info, wr);
774*4882a593Smuzhiyun 		}
775*4882a593Smuzhiyun 	}
776*4882a593Smuzhiyun }
777*4882a593Smuzhiyun #endif /*DBG_PHL_STAINFO*/
778*4882a593Smuzhiyun 
_phl_self_stainfo_chk(struct phl_info_t * phl_info,struct rtw_wifi_role_t * wrole,struct rtw_phl_stainfo_t * sta)779*4882a593Smuzhiyun static bool _phl_self_stainfo_chk(struct phl_info_t *phl_info,
780*4882a593Smuzhiyun 	struct rtw_wifi_role_t *wrole, struct rtw_phl_stainfo_t *sta)
781*4882a593Smuzhiyun {
782*4882a593Smuzhiyun 	void *drv = phl_to_drvpriv(phl_info);
783*4882a593Smuzhiyun 	bool is_self = false;
784*4882a593Smuzhiyun 
785*4882a593Smuzhiyun 	switch (wrole->type) {
786*4882a593Smuzhiyun 	case PHL_RTYPE_STATION:
787*4882a593Smuzhiyun 	case PHL_RTYPE_P2P_GC:
788*4882a593Smuzhiyun 		_os_mem_cpy(drv, sta->mac_addr, wrole->mac_addr, MAC_ALEN);
789*4882a593Smuzhiyun 		is_self = true;
790*4882a593Smuzhiyun 	break;
791*4882a593Smuzhiyun 
792*4882a593Smuzhiyun 	case PHL_RTYPE_AP:
793*4882a593Smuzhiyun 	case PHL_RTYPE_MESH:
794*4882a593Smuzhiyun 	case PHL_RTYPE_P2P_GO:
795*4882a593Smuzhiyun 	case PHL_RTYPE_TDLS:
796*4882a593Smuzhiyun 		if (_os_mem_cmp(drv, wrole->mac_addr, sta->mac_addr, MAC_ALEN) == 0)
797*4882a593Smuzhiyun 			is_self = true;
798*4882a593Smuzhiyun 	break;
799*4882a593Smuzhiyun 
800*4882a593Smuzhiyun 	case PHL_RTYPE_NONE:
801*4882a593Smuzhiyun 	case PHL_RTYPE_VAP:
802*4882a593Smuzhiyun 	case PHL_RTYPE_ADHOC:
803*4882a593Smuzhiyun 	case PHL_RTYPE_ADHOC_MASTER:
804*4882a593Smuzhiyun 	case PHL_RTYPE_MONITOR:
805*4882a593Smuzhiyun 	case PHL_RTYPE_P2P_DEVICE:
806*4882a593Smuzhiyun 	case PHL_RTYPE_NAN:
807*4882a593Smuzhiyun 	case PHL_MLME_MAX:
808*4882a593Smuzhiyun 		PHL_TRACE(COMP_PHL_DBG, _PHL_ERR_, "_phl_self_stainfo_chk(): Unsupported case:%d, please check it\n",
809*4882a593Smuzhiyun 				wrole->type);
810*4882a593Smuzhiyun 		break;
811*4882a593Smuzhiyun 	default:
812*4882a593Smuzhiyun 		PHL_TRACE(COMP_PHL_DBG, _PHL_ERR_, "_phl_self_stainfo_chk(): role-type(%d) not recognize\n",
813*4882a593Smuzhiyun 				wrole->type);
814*4882a593Smuzhiyun 		break;
815*4882a593Smuzhiyun 	}
816*4882a593Smuzhiyun 	return is_self;
817*4882a593Smuzhiyun }
818*4882a593Smuzhiyun 
819*4882a593Smuzhiyun enum rtw_phl_status
phl_free_stainfo_sw(struct phl_info_t * phl_info,struct rtw_phl_stainfo_t * sta)820*4882a593Smuzhiyun phl_free_stainfo_sw(struct phl_info_t *phl_info, struct rtw_phl_stainfo_t *sta)
821*4882a593Smuzhiyun {
822*4882a593Smuzhiyun 	enum rtw_phl_status pstatus = RTW_PHL_STATUS_FAILURE;
823*4882a593Smuzhiyun 
824*4882a593Smuzhiyun 	if(sta == NULL) {
825*4882a593Smuzhiyun 		PHL_ERR("%s sta is NULL\n", __func__);
826*4882a593Smuzhiyun 		return RTW_PHL_STATUS_FAILURE;
827*4882a593Smuzhiyun 	}
828*4882a593Smuzhiyun 
829*4882a593Smuzhiyun 	phl_free_rx_reorder(phl_info, sta);
830*4882a593Smuzhiyun 
831*4882a593Smuzhiyun 	pstatus = phl_deregister_tx_ring((void *)phl_info, sta->macid);
832*4882a593Smuzhiyun 	if (pstatus != RTW_PHL_STATUS_SUCCESS) {
833*4882a593Smuzhiyun 		PHL_ERR("macid(%d) phl_deregister_tx_ring failed\n", sta->macid);
834*4882a593Smuzhiyun 	}
835*4882a593Smuzhiyun 
836*4882a593Smuzhiyun 	/* release macid for used_map */
837*4882a593Smuzhiyun 	pstatus = _phl_release_macid(phl_info, sta);
838*4882a593Smuzhiyun 	if (pstatus != RTW_PHL_STATUS_SUCCESS)
839*4882a593Smuzhiyun 		PHL_ERR("_phl_release_macid failed\n");
840*4882a593Smuzhiyun 
841*4882a593Smuzhiyun 	return pstatus;
842*4882a593Smuzhiyun }
843*4882a593Smuzhiyun 
844*4882a593Smuzhiyun enum rtw_phl_status
__phl_free_stainfo_sw(struct phl_info_t * phl_info,struct rtw_phl_stainfo_t * sta)845*4882a593Smuzhiyun __phl_free_stainfo_sw(struct phl_info_t *phl_info, struct rtw_phl_stainfo_t *sta)
846*4882a593Smuzhiyun {
847*4882a593Smuzhiyun 	enum rtw_phl_status pstatus = RTW_PHL_STATUS_FAILURE;
848*4882a593Smuzhiyun 	struct stainfo_ctl_t *sta_ctrl = phl_to_sta_ctrl(phl_info);
849*4882a593Smuzhiyun 	struct rtw_wifi_role_t *wrole = NULL;
850*4882a593Smuzhiyun 
851*4882a593Smuzhiyun 	FUNCIN();
852*4882a593Smuzhiyun 	if(sta == NULL) {
853*4882a593Smuzhiyun 		PHL_ERR("%s sta is NULL\n", __func__);
854*4882a593Smuzhiyun 		goto _exit;
855*4882a593Smuzhiyun 	}
856*4882a593Smuzhiyun 
857*4882a593Smuzhiyun 	wrole = sta->wrole;
858*4882a593Smuzhiyun 
859*4882a593Smuzhiyun 	if (!is_broadcast_mac_addr(sta->mac_addr)) {
860*4882a593Smuzhiyun 		if (_phl_self_stainfo_chk(phl_info, wrole, sta) == true) {
861*4882a593Smuzhiyun 			pstatus = RTW_PHL_STATUS_SUCCESS;
862*4882a593Smuzhiyun 			goto _exit;
863*4882a593Smuzhiyun 		}
864*4882a593Smuzhiyun 	}
865*4882a593Smuzhiyun 
866*4882a593Smuzhiyun 	pstatus = phl_stainfo_queue_del(phl_info, &wrole->assoc_sta_queue, sta);
867*4882a593Smuzhiyun 	if (pstatus != RTW_PHL_STATUS_SUCCESS) {
868*4882a593Smuzhiyun 		PHL_ERR("phl_stainfo_queue_del failed\n");
869*4882a593Smuzhiyun 	}
870*4882a593Smuzhiyun 
871*4882a593Smuzhiyun 	pstatus = phl_free_stainfo_sw(phl_info, sta);
872*4882a593Smuzhiyun 	if (pstatus != RTW_PHL_STATUS_SUCCESS) {
873*4882a593Smuzhiyun 		PHL_ERR("macid(%d) _phl_free_stainfo_sw failed\n", sta->macid);
874*4882a593Smuzhiyun 	}
875*4882a593Smuzhiyun 
876*4882a593Smuzhiyun 	pstatus = phl_stainfo_enqueue(phl_info, &sta_ctrl->free_sta_queue, sta);
877*4882a593Smuzhiyun 	if (pstatus != RTW_PHL_STATUS_SUCCESS)
878*4882a593Smuzhiyun 		PHL_ERR("phl_stainfo_enqueue to free queue failed\n");
879*4882a593Smuzhiyun 
880*4882a593Smuzhiyun #ifdef RTW_WKARD_AP_CLIENT_ADD_DEL_NTY
881*4882a593Smuzhiyun 	if ((wrole->type == PHL_RTYPE_AP) ||
882*4882a593Smuzhiyun 	    (wrole->type == PHL_RTYPE_VAP) ||
883*4882a593Smuzhiyun 	    (wrole->type == PHL_RTYPE_MESH) ||
884*4882a593Smuzhiyun 	    (wrole->type == PHL_RTYPE_P2P_GO))
885*4882a593Smuzhiyun 		phl_role_ap_client_notify(phl_info, wrole, MLME_NO_LINK);
886*4882a593Smuzhiyun #endif
887*4882a593Smuzhiyun 
888*4882a593Smuzhiyun _exit:
889*4882a593Smuzhiyun 	PHL_DUMP_STACTRL_EX(phl_info);
890*4882a593Smuzhiyun 	FUNCOUT();
891*4882a593Smuzhiyun 	return pstatus;
892*4882a593Smuzhiyun }
893*4882a593Smuzhiyun 
894*4882a593Smuzhiyun enum rtw_phl_status
rtw_phl_free_stainfo_sw(void * phl,struct rtw_phl_stainfo_t * sta)895*4882a593Smuzhiyun rtw_phl_free_stainfo_sw(void *phl, struct rtw_phl_stainfo_t *sta)
896*4882a593Smuzhiyun {
897*4882a593Smuzhiyun 	return __phl_free_stainfo_sw((struct phl_info_t *)phl, sta);
898*4882a593Smuzhiyun }
899*4882a593Smuzhiyun 
900*4882a593Smuzhiyun enum rtw_phl_status
phl_free_stainfo_hw(struct phl_info_t * phl_info,struct rtw_phl_stainfo_t * sta)901*4882a593Smuzhiyun phl_free_stainfo_hw(struct phl_info_t *phl_info,
902*4882a593Smuzhiyun 					struct rtw_phl_stainfo_t *sta)
903*4882a593Smuzhiyun {
904*4882a593Smuzhiyun 	enum rtw_phl_status pstatus = RTW_PHL_STATUS_FAILURE;
905*4882a593Smuzhiyun 
906*4882a593Smuzhiyun 	if (sta == NULL) {
907*4882a593Smuzhiyun 		PHL_ERR("%s sta == NULL\n", __func__);
908*4882a593Smuzhiyun 		goto _exit;
909*4882a593Smuzhiyun 	}
910*4882a593Smuzhiyun 
911*4882a593Smuzhiyun 	phl_pkt_ofld_del_entry(phl_info, sta->macid);
912*4882a593Smuzhiyun 
913*4882a593Smuzhiyun 	sta->active = false;
914*4882a593Smuzhiyun 	if (rtw_hal_del_sta_entry(phl_info->hal, sta) == RTW_HAL_STATUS_SUCCESS)
915*4882a593Smuzhiyun 		pstatus = RTW_PHL_STATUS_SUCCESS;
916*4882a593Smuzhiyun 	else
917*4882a593Smuzhiyun 		PHL_ERR("rtw_hal_del_sta_entry failed\n");
918*4882a593Smuzhiyun _exit:
919*4882a593Smuzhiyun 	return pstatus;
920*4882a593Smuzhiyun }
921*4882a593Smuzhiyun 
922*4882a593Smuzhiyun enum rtw_phl_status
__phl_free_stainfo_hw(struct phl_info_t * phl_info,struct rtw_phl_stainfo_t * sta)923*4882a593Smuzhiyun __phl_free_stainfo_hw(struct phl_info_t *phl_info, struct rtw_phl_stainfo_t *sta)
924*4882a593Smuzhiyun {
925*4882a593Smuzhiyun 	struct rtw_wifi_role_t *wrole = sta->wrole;
926*4882a593Smuzhiyun 
927*4882a593Smuzhiyun 	if (!is_broadcast_mac_addr(sta->mac_addr)) {
928*4882a593Smuzhiyun 		if (_phl_self_stainfo_chk(phl_info, wrole, sta) == true)
929*4882a593Smuzhiyun 			return RTW_PHL_STATUS_SUCCESS;
930*4882a593Smuzhiyun 	}
931*4882a593Smuzhiyun 	return phl_free_stainfo_hw(phl_info, sta);
932*4882a593Smuzhiyun }
933*4882a593Smuzhiyun 
934*4882a593Smuzhiyun static enum rtw_phl_status
__phl_free_stainfo(struct phl_info_t * phl,struct rtw_phl_stainfo_t * sta)935*4882a593Smuzhiyun __phl_free_stainfo(struct phl_info_t *phl, struct rtw_phl_stainfo_t *sta)
936*4882a593Smuzhiyun {
937*4882a593Smuzhiyun 	enum rtw_phl_status pstatus = RTW_PHL_STATUS_FAILURE;
938*4882a593Smuzhiyun 
939*4882a593Smuzhiyun 	pstatus = __phl_free_stainfo_hw(phl, sta);
940*4882a593Smuzhiyun 	if (pstatus != RTW_PHL_STATUS_SUCCESS)
941*4882a593Smuzhiyun 		PHL_ERR("__phl_free_stainfo_hw failed\n");
942*4882a593Smuzhiyun 
943*4882a593Smuzhiyun 	pstatus = __phl_free_stainfo_sw(phl, sta);
944*4882a593Smuzhiyun 	if (pstatus != RTW_PHL_STATUS_SUCCESS)
945*4882a593Smuzhiyun 		PHL_ERR("__phl_free_stainfo_sw failed\n");
946*4882a593Smuzhiyun 	return pstatus;
947*4882a593Smuzhiyun }
948*4882a593Smuzhiyun 
949*4882a593Smuzhiyun 
950*4882a593Smuzhiyun static enum rtw_phl_status
_phl_alloc_stainfo_sw(struct phl_info_t * phl_info,struct rtw_phl_stainfo_t * sta)951*4882a593Smuzhiyun _phl_alloc_stainfo_sw(struct phl_info_t *phl_info,struct rtw_phl_stainfo_t *sta)
952*4882a593Smuzhiyun {
953*4882a593Smuzhiyun 	enum rtw_phl_status pstatus = RTW_PHL_STATUS_FAILURE;
954*4882a593Smuzhiyun 
955*4882a593Smuzhiyun 	pstatus = _phl_alloc_macid(phl_info, sta);
956*4882a593Smuzhiyun 	if (pstatus != RTW_PHL_STATUS_SUCCESS) {
957*4882a593Smuzhiyun 		PHL_ERR("%s allocate macid failure!\n", __func__);
958*4882a593Smuzhiyun 		goto error_alloc_macid;
959*4882a593Smuzhiyun 	}
960*4882a593Smuzhiyun 	pstatus = phl_register_tx_ring(phl_info, sta->macid,
961*4882a593Smuzhiyun 				       sta->wrole->hw_band,
962*4882a593Smuzhiyun 				       sta->wrole->hw_wmm,
963*4882a593Smuzhiyun 				       sta->wrole->hw_port);
964*4882a593Smuzhiyun 
965*4882a593Smuzhiyun 	if (pstatus != RTW_PHL_STATUS_SUCCESS) {
966*4882a593Smuzhiyun 		PHL_ERR("%s register_tx_ring failure!\n", __func__);
967*4882a593Smuzhiyun 		goto error_register_tx_ring;
968*4882a593Smuzhiyun 	}
969*4882a593Smuzhiyun 	pstatus = RTW_PHL_STATUS_SUCCESS;
970*4882a593Smuzhiyun 	return pstatus;
971*4882a593Smuzhiyun 
972*4882a593Smuzhiyun error_register_tx_ring:
973*4882a593Smuzhiyun 	_phl_release_macid(phl_info, sta);
974*4882a593Smuzhiyun error_alloc_macid:
975*4882a593Smuzhiyun 	return pstatus;
976*4882a593Smuzhiyun }
977*4882a593Smuzhiyun 
_phl_sta_set_default_value(struct phl_info_t * phl_info,struct rtw_phl_stainfo_t * phl_sta)978*4882a593Smuzhiyun static void _phl_sta_set_default_value(struct phl_info_t *phl_info,
979*4882a593Smuzhiyun 		struct rtw_phl_stainfo_t *phl_sta)
980*4882a593Smuzhiyun {
981*4882a593Smuzhiyun 	phl_sta->bcn_hit_cond = 0; /* beacon:A3 probersp: A1 & A3 */
982*4882a593Smuzhiyun 
983*4882a593Smuzhiyun 	/* fit rule
984*4882a593Smuzhiyun 	 * 0: A1 & A2
985*4882a593Smuzhiyun 	 * 1: A1 & A3
986*4882a593Smuzhiyun 	 *
987*4882a593Smuzhiyun 	 * Rule 0 should be used for both AP and STA modes.
988*4882a593Smuzhiyun 	 *
989*4882a593Smuzhiyun 	 * For STA, A3 is source address(SA) which can be any peer on the LAN.
990*4882a593Smuzhiyun 	 *
991*4882a593Smuzhiyun 	 * For AP, A3 is destination address(DA) which can also be any node
992*4882a593Smuzhiyun 	 * on the LAN. A1 & A2 match find the address CAM entry that contains the
993*4882a593Smuzhiyun 	 * correct security CAM ID and MAC ID.
994*4882a593Smuzhiyun 	 */
995*4882a593Smuzhiyun 	phl_sta->hit_rule = 0;
996*4882a593Smuzhiyun }
997*4882a593Smuzhiyun 
998*4882a593Smuzhiyun struct rtw_phl_stainfo_t *
phl_alloc_stainfo_sw(struct phl_info_t * phl_info,u8 * sta_addr,struct rtw_wifi_role_t * wrole)999*4882a593Smuzhiyun phl_alloc_stainfo_sw(struct phl_info_t *phl_info,
1000*4882a593Smuzhiyun                      u8 *sta_addr,
1001*4882a593Smuzhiyun                      struct rtw_wifi_role_t *wrole)
1002*4882a593Smuzhiyun {
1003*4882a593Smuzhiyun 	struct stainfo_ctl_t *sta_ctrl = phl_to_sta_ctrl(phl_info);
1004*4882a593Smuzhiyun 	struct rtw_phl_stainfo_t *phl_sta = NULL;
1005*4882a593Smuzhiyun 	void *drv = phl_to_drvpriv(phl_info);
1006*4882a593Smuzhiyun 	bool bmc_sta = false;
1007*4882a593Smuzhiyun 
1008*4882a593Smuzhiyun 	FUNCIN();
1009*4882a593Smuzhiyun 	if (is_broadcast_mac_addr(sta_addr))
1010*4882a593Smuzhiyun 		bmc_sta = true;
1011*4882a593Smuzhiyun 
1012*4882a593Smuzhiyun 	/* if sta_addr is bmc addr, allocate new sta_info */
1013*4882a593Smuzhiyun 	if (wrole->type == PHL_RTYPE_STATION && !bmc_sta) {
1014*4882a593Smuzhiyun 		phl_sta = rtw_phl_get_stainfo_self(phl_info, wrole);
1015*4882a593Smuzhiyun 		if (phl_sta) {
1016*4882a593Smuzhiyun 			_os_mem_cpy(drv, phl_sta->mac_addr, sta_addr, MAC_ALEN);
1017*4882a593Smuzhiyun 			goto _exit;
1018*4882a593Smuzhiyun 		}
1019*4882a593Smuzhiyun 	}
1020*4882a593Smuzhiyun 
1021*4882a593Smuzhiyun 	/* check station info exist */
1022*4882a593Smuzhiyun 	phl_sta = rtw_phl_get_stainfo_by_addr(phl_info, wrole, sta_addr);
1023*4882a593Smuzhiyun 	if (phl_sta) {
1024*4882a593Smuzhiyun 		PHL_INFO("%s phl_sta(%02x:%02x:%02x:%02x:%02x:%02x) exist\n",
1025*4882a593Smuzhiyun 		         __func__, sta_addr[0], sta_addr[1], sta_addr[2],
1026*4882a593Smuzhiyun 		         sta_addr[3], sta_addr[4], sta_addr[5]);
1027*4882a593Smuzhiyun 		goto _exit;
1028*4882a593Smuzhiyun 	}
1029*4882a593Smuzhiyun 
1030*4882a593Smuzhiyun 	phl_sta = phl_stainfo_dequeue(phl_info, &sta_ctrl->free_sta_queue);
1031*4882a593Smuzhiyun 	if (phl_sta == NULL) {
1032*4882a593Smuzhiyun 		PHL_ERR("allocate phl_sta failure!\n");
1033*4882a593Smuzhiyun 		goto _exit;
1034*4882a593Smuzhiyun 	}
1035*4882a593Smuzhiyun 
1036*4882a593Smuzhiyun 	_os_mem_cpy(drv, phl_sta->mac_addr, sta_addr, MAC_ALEN);
1037*4882a593Smuzhiyun 	phl_sta->wrole = wrole;
1038*4882a593Smuzhiyun 
1039*4882a593Smuzhiyun 	if (_phl_alloc_stainfo_sw(phl_info, phl_sta) != RTW_PHL_STATUS_SUCCESS) {
1040*4882a593Smuzhiyun 		PHL_ERR("_phl_alloc_stainfo_sw failed\n");
1041*4882a593Smuzhiyun 		goto error_alloc_sta;
1042*4882a593Smuzhiyun 	}
1043*4882a593Smuzhiyun 	_phl_sta_set_default_value(phl_info, phl_sta);
1044*4882a593Smuzhiyun 
1045*4882a593Smuzhiyun 	phl_stainfo_enqueue(phl_info, &wrole->assoc_sta_queue, phl_sta);
1046*4882a593Smuzhiyun 
1047*4882a593Smuzhiyun 	#ifdef RTW_WKARD_AP_CLIENT_ADD_DEL_NTY
1048*4882a593Smuzhiyun 	if (_phl_self_stainfo_chk(phl_info, wrole, phl_sta) == false) {
1049*4882a593Smuzhiyun 		if ((wrole->type == PHL_RTYPE_AP) ||
1050*4882a593Smuzhiyun 		     (wrole->type == PHL_RTYPE_VAP) ||
1051*4882a593Smuzhiyun 		     (wrole->type == PHL_RTYPE_MESH) ||
1052*4882a593Smuzhiyun 		     (wrole->type == PHL_RTYPE_P2P_GO)) {
1053*4882a593Smuzhiyun 			phl_role_ap_client_notify(phl_info, wrole, MLME_LINKING);
1054*4882a593Smuzhiyun 		}
1055*4882a593Smuzhiyun 	}
1056*4882a593Smuzhiyun 	#endif
1057*4882a593Smuzhiyun _exit:
1058*4882a593Smuzhiyun 	PHL_DUMP_STACTRL_EX(phl_info);
1059*4882a593Smuzhiyun 	FUNCOUT();
1060*4882a593Smuzhiyun 
1061*4882a593Smuzhiyun 	return phl_sta;
1062*4882a593Smuzhiyun 
1063*4882a593Smuzhiyun error_alloc_sta:
1064*4882a593Smuzhiyun 	phl_stainfo_enqueue(phl_info, &sta_ctrl->free_sta_queue, phl_sta);
1065*4882a593Smuzhiyun 	phl_sta = NULL;
1066*4882a593Smuzhiyun 	PHL_DUMP_STACTRL_EX(phl_info);
1067*4882a593Smuzhiyun 	FUNCOUT();
1068*4882a593Smuzhiyun 	return phl_sta;
1069*4882a593Smuzhiyun }
1070*4882a593Smuzhiyun 
1071*4882a593Smuzhiyun struct rtw_phl_stainfo_t *
rtw_phl_alloc_stainfo_sw(void * phl,u8 * sta_addr,struct rtw_wifi_role_t * wrole)1072*4882a593Smuzhiyun rtw_phl_alloc_stainfo_sw(void *phl, u8 *sta_addr,
1073*4882a593Smuzhiyun 				struct rtw_wifi_role_t *wrole)
1074*4882a593Smuzhiyun {
1075*4882a593Smuzhiyun 	return phl_alloc_stainfo_sw((struct phl_info_t *)phl, sta_addr, wrole);
1076*4882a593Smuzhiyun }
1077*4882a593Smuzhiyun 
1078*4882a593Smuzhiyun enum rtw_phl_status
phl_alloc_stainfo_hw(struct phl_info_t * phl_info,struct rtw_phl_stainfo_t * sta)1079*4882a593Smuzhiyun phl_alloc_stainfo_hw(struct phl_info_t *phl_info, struct rtw_phl_stainfo_t *sta)
1080*4882a593Smuzhiyun {
1081*4882a593Smuzhiyun 	enum rtw_phl_status pstatus = RTW_PHL_STATUS_FAILURE;
1082*4882a593Smuzhiyun 
1083*4882a593Smuzhiyun 	if (sta == NULL) {
1084*4882a593Smuzhiyun 		PHL_ERR("%s sta == NULL\n", __func__);
1085*4882a593Smuzhiyun 		goto _exit;
1086*4882a593Smuzhiyun 	}
1087*4882a593Smuzhiyun 
1088*4882a593Smuzhiyun 	if (rtw_hal_add_sta_entry(phl_info->hal, sta) != RTW_HAL_STATUS_SUCCESS) {
1089*4882a593Smuzhiyun 		PHL_ERR("%s rtw_hal_add_sta_entry failure!\n", __func__);
1090*4882a593Smuzhiyun 		goto _exit;
1091*4882a593Smuzhiyun 	}
1092*4882a593Smuzhiyun 
1093*4882a593Smuzhiyun 	sta->active = true;
1094*4882a593Smuzhiyun 
1095*4882a593Smuzhiyun 	pstatus = phl_pkt_ofld_add_entry(phl_info, sta->macid);
1096*4882a593Smuzhiyun 	if (RTW_PHL_STATUS_SUCCESS != pstatus)
1097*4882a593Smuzhiyun 		PHL_ERR("%s phl_pkt_ofld_add_entry failure!\n", __func__);
1098*4882a593Smuzhiyun 
1099*4882a593Smuzhiyun _exit:
1100*4882a593Smuzhiyun 	return pstatus;
1101*4882a593Smuzhiyun }
1102*4882a593Smuzhiyun 
1103*4882a593Smuzhiyun enum rtw_phl_status
__phl_alloc_stainfo_hw(struct phl_info_t * phl_info,struct rtw_phl_stainfo_t * sta)1104*4882a593Smuzhiyun __phl_alloc_stainfo_hw(struct phl_info_t *phl_info, struct rtw_phl_stainfo_t *sta)
1105*4882a593Smuzhiyun {
1106*4882a593Smuzhiyun 	return phl_alloc_stainfo_hw(phl_info, sta);
1107*4882a593Smuzhiyun }
1108*4882a593Smuzhiyun 
1109*4882a593Smuzhiyun static enum rtw_phl_status
__phl_alloc_stainfo(struct phl_info_t * phl,struct rtw_phl_stainfo_t ** sta,u8 * sta_addr,struct rtw_wifi_role_t * wrole)1110*4882a593Smuzhiyun __phl_alloc_stainfo(struct phl_info_t *phl,
1111*4882a593Smuzhiyun                     struct rtw_phl_stainfo_t **sta,
1112*4882a593Smuzhiyun                     u8 *sta_addr,
1113*4882a593Smuzhiyun                     struct rtw_wifi_role_t *wrole)
1114*4882a593Smuzhiyun {
1115*4882a593Smuzhiyun 	struct rtw_phl_stainfo_t *alloc_sta = NULL;
1116*4882a593Smuzhiyun 	enum rtw_phl_status pstatus = RTW_PHL_STATUS_FAILURE;
1117*4882a593Smuzhiyun 
1118*4882a593Smuzhiyun 	alloc_sta = phl_alloc_stainfo_sw(phl, sta_addr, wrole);
1119*4882a593Smuzhiyun 	if (alloc_sta == NULL) {
1120*4882a593Smuzhiyun 		PHL_ERR("%s can't alloc stainfo\n", __func__);
1121*4882a593Smuzhiyun 		*sta = alloc_sta;
1122*4882a593Smuzhiyun 		goto _exit;
1123*4882a593Smuzhiyun 	}
1124*4882a593Smuzhiyun 
1125*4882a593Smuzhiyun 	if (alloc_sta->active == false) {
1126*4882a593Smuzhiyun 		pstatus = __phl_alloc_stainfo_hw(phl, alloc_sta);
1127*4882a593Smuzhiyun 		if (pstatus != RTW_PHL_STATUS_SUCCESS) {
1128*4882a593Smuzhiyun 			PHL_ERR("__phl_alloc_stainfo_hw failed\n");
1129*4882a593Smuzhiyun 			goto _err_alloc_sta_hw;
1130*4882a593Smuzhiyun 		}
1131*4882a593Smuzhiyun 	}
1132*4882a593Smuzhiyun 
1133*4882a593Smuzhiyun 	PHL_INFO("%s success - macid:%u %02x:%02x:%02x:%02x:%02x:%02x\n",
1134*4882a593Smuzhiyun 	         __func__, alloc_sta->macid,
1135*4882a593Smuzhiyun 	         alloc_sta->mac_addr[0], alloc_sta->mac_addr[1], alloc_sta->mac_addr[2],
1136*4882a593Smuzhiyun 	         alloc_sta->mac_addr[3], alloc_sta->mac_addr[4], alloc_sta->mac_addr[5]);
1137*4882a593Smuzhiyun 
1138*4882a593Smuzhiyun 	*sta = alloc_sta;
1139*4882a593Smuzhiyun 	return RTW_PHL_STATUS_SUCCESS;
1140*4882a593Smuzhiyun 
1141*4882a593Smuzhiyun _err_alloc_sta_hw:
1142*4882a593Smuzhiyun 	__phl_free_stainfo_sw(phl, alloc_sta);
1143*4882a593Smuzhiyun 	*sta = alloc_sta = NULL;
1144*4882a593Smuzhiyun _exit:
1145*4882a593Smuzhiyun 	return RTW_PHL_STATUS_FAILURE;
1146*4882a593Smuzhiyun }
1147*4882a593Smuzhiyun 
1148*4882a593Smuzhiyun static enum rtw_phl_status
_phl_alloc_stainfo(struct phl_info_t * phl,struct rtw_phl_stainfo_t ** sta,u8 * sta_addr,struct rtw_wifi_role_t * wrole,bool alloc,bool only_hw)1149*4882a593Smuzhiyun _phl_alloc_stainfo(struct phl_info_t *phl,
1150*4882a593Smuzhiyun                    struct rtw_phl_stainfo_t **sta,
1151*4882a593Smuzhiyun                    u8 *sta_addr,
1152*4882a593Smuzhiyun                    struct rtw_wifi_role_t *wrole,
1153*4882a593Smuzhiyun                    bool alloc,
1154*4882a593Smuzhiyun                    bool only_hw)
1155*4882a593Smuzhiyun {
1156*4882a593Smuzhiyun 	enum rtw_phl_status pstatus = RTW_PHL_STATUS_FAILURE;
1157*4882a593Smuzhiyun 
1158*4882a593Smuzhiyun 	if (alloc) {
1159*4882a593Smuzhiyun 		if (only_hw)
1160*4882a593Smuzhiyun 			pstatus = __phl_alloc_stainfo_hw(phl, *sta);
1161*4882a593Smuzhiyun 		else
1162*4882a593Smuzhiyun 			pstatus = __phl_alloc_stainfo(phl, sta, sta_addr, wrole);
1163*4882a593Smuzhiyun 	} else {
1164*4882a593Smuzhiyun 		if (only_hw)
1165*4882a593Smuzhiyun 			pstatus = __phl_free_stainfo_hw(phl, *sta);
1166*4882a593Smuzhiyun 		else
1167*4882a593Smuzhiyun 			pstatus = __phl_free_stainfo(phl, *sta);
1168*4882a593Smuzhiyun 	}
1169*4882a593Smuzhiyun 	return pstatus;
1170*4882a593Smuzhiyun }
1171*4882a593Smuzhiyun 
1172*4882a593Smuzhiyun #ifdef CONFIG_CMD_DISP
1173*4882a593Smuzhiyun struct cmd_stainfo_param {
1174*4882a593Smuzhiyun 	struct rtw_phl_stainfo_t **sta;
1175*4882a593Smuzhiyun 	u8 sta_addr[MAC_ALEN];
1176*4882a593Smuzhiyun 	struct rtw_wifi_role_t *wrole;
1177*4882a593Smuzhiyun 	bool alloc;
1178*4882a593Smuzhiyun 	bool only_hw;
1179*4882a593Smuzhiyun };
1180*4882a593Smuzhiyun 
1181*4882a593Smuzhiyun static void
_phl_cmd_alloc_stainfo_done(void * drv_priv,u8 * cmd,u32 cmd_len,enum rtw_phl_status status)1182*4882a593Smuzhiyun _phl_cmd_alloc_stainfo_done(void *drv_priv,
1183*4882a593Smuzhiyun 						u8 *cmd,
1184*4882a593Smuzhiyun 						u32 cmd_len,
1185*4882a593Smuzhiyun 						enum rtw_phl_status status)
1186*4882a593Smuzhiyun {
1187*4882a593Smuzhiyun 	if (cmd)
1188*4882a593Smuzhiyun 		_os_kmem_free(drv_priv, cmd, cmd_len);
1189*4882a593Smuzhiyun }
1190*4882a593Smuzhiyun 
1191*4882a593Smuzhiyun static enum rtw_phl_status
_phl_cmd_alloc_stainfo(struct phl_info_t * phl_info,struct rtw_phl_stainfo_t ** sta,u8 * sta_addr,struct rtw_wifi_role_t * wrole,bool alloc,bool only_hw,enum phl_cmd_type cmd_type,u32 cmd_timeout)1192*4882a593Smuzhiyun _phl_cmd_alloc_stainfo(struct phl_info_t *phl_info,
1193*4882a593Smuzhiyun 			struct rtw_phl_stainfo_t **sta,
1194*4882a593Smuzhiyun 			u8 *sta_addr,
1195*4882a593Smuzhiyun 			struct rtw_wifi_role_t *wrole,
1196*4882a593Smuzhiyun 			bool alloc, bool only_hw,
1197*4882a593Smuzhiyun 			enum phl_cmd_type cmd_type,
1198*4882a593Smuzhiyun 			u32 cmd_timeout)
1199*4882a593Smuzhiyun {
1200*4882a593Smuzhiyun 	void *drv = phl_to_drvpriv(phl_info);
1201*4882a593Smuzhiyun 	enum rtw_phl_status psts = RTW_PHL_STATUS_FAILURE;
1202*4882a593Smuzhiyun 	struct cmd_stainfo_param *param = NULL;
1203*4882a593Smuzhiyun 	u32 param_len = 0;
1204*4882a593Smuzhiyun 
1205*4882a593Smuzhiyun 	if (cmd_type == PHL_CMD_DIRECTLY) {
1206*4882a593Smuzhiyun 		psts = _phl_alloc_stainfo(phl_info, sta, sta_addr, wrole, alloc, only_hw);
1207*4882a593Smuzhiyun 		goto _exit;
1208*4882a593Smuzhiyun 	}
1209*4882a593Smuzhiyun 
1210*4882a593Smuzhiyun 	param_len = sizeof(struct cmd_stainfo_param);
1211*4882a593Smuzhiyun 	param = _os_kmem_alloc(drv, param_len);
1212*4882a593Smuzhiyun 	if (param == NULL) {
1213*4882a593Smuzhiyun 		PHL_ERR("%s: alloc param failed!\n", __func__);
1214*4882a593Smuzhiyun 		psts = RTW_PHL_STATUS_RESOURCE;
1215*4882a593Smuzhiyun 		goto _exit;
1216*4882a593Smuzhiyun 	}
1217*4882a593Smuzhiyun 
1218*4882a593Smuzhiyun 	_os_mem_set(drv, param, 0, param_len);
1219*4882a593Smuzhiyun 	param->sta = sta;
1220*4882a593Smuzhiyun 	_os_mem_cpy(drv, param->sta_addr, sta_addr, MAC_ALEN);
1221*4882a593Smuzhiyun 	param->wrole = wrole;
1222*4882a593Smuzhiyun 	param->alloc = alloc;
1223*4882a593Smuzhiyun 	param->only_hw = only_hw;
1224*4882a593Smuzhiyun 
1225*4882a593Smuzhiyun 	psts = phl_cmd_enqueue(phl_info,
1226*4882a593Smuzhiyun 	                       wrole->hw_band,
1227*4882a593Smuzhiyun 	                       MSG_EVT_STA_INFO_CTRL,
1228*4882a593Smuzhiyun 	                       (u8 *)param,
1229*4882a593Smuzhiyun 	                       param_len,
1230*4882a593Smuzhiyun 	                       _phl_cmd_alloc_stainfo_done,
1231*4882a593Smuzhiyun 	                       cmd_type,
1232*4882a593Smuzhiyun 	                       cmd_timeout);
1233*4882a593Smuzhiyun 
1234*4882a593Smuzhiyun 	if (is_cmd_failure(psts)) {
1235*4882a593Smuzhiyun 		/* Send cmd success, but wait cmd fail*/
1236*4882a593Smuzhiyun 		psts = RTW_PHL_STATUS_FAILURE;
1237*4882a593Smuzhiyun 	} else if (psts != RTW_PHL_STATUS_SUCCESS) {
1238*4882a593Smuzhiyun 		/* Send cmd fail */
1239*4882a593Smuzhiyun 		psts = RTW_PHL_STATUS_FAILURE;
1240*4882a593Smuzhiyun 		_os_kmem_free(drv, param, param_len);
1241*4882a593Smuzhiyun 	}
1242*4882a593Smuzhiyun _exit:
1243*4882a593Smuzhiyun 	return psts;
1244*4882a593Smuzhiyun }
1245*4882a593Smuzhiyun 
1246*4882a593Smuzhiyun enum rtw_phl_status
phl_cmd_alloc_stainfo_hdl(struct phl_info_t * phl_info,u8 * param)1247*4882a593Smuzhiyun phl_cmd_alloc_stainfo_hdl(struct phl_info_t *phl_info, u8 *param)
1248*4882a593Smuzhiyun {
1249*4882a593Smuzhiyun 	struct cmd_stainfo_param *cmd_sta_param = (struct cmd_stainfo_param *)param;
1250*4882a593Smuzhiyun 
1251*4882a593Smuzhiyun 	return _phl_alloc_stainfo(phl_info,
1252*4882a593Smuzhiyun 					cmd_sta_param->sta,
1253*4882a593Smuzhiyun 					cmd_sta_param->sta_addr,
1254*4882a593Smuzhiyun 					cmd_sta_param->wrole,
1255*4882a593Smuzhiyun 					cmd_sta_param->alloc,
1256*4882a593Smuzhiyun 					cmd_sta_param->only_hw);
1257*4882a593Smuzhiyun }
1258*4882a593Smuzhiyun 
1259*4882a593Smuzhiyun #endif /* CONFIG_CMD_DISP */
1260*4882a593Smuzhiyun 
1261*4882a593Smuzhiyun enum rtw_phl_status
rtw_phl_cmd_alloc_stainfo(void * phl,struct rtw_phl_stainfo_t ** sta,u8 * sta_addr,struct rtw_wifi_role_t * wrole,bool alloc,bool only_hw,enum phl_cmd_type cmd_type,u32 cmd_timeout)1262*4882a593Smuzhiyun rtw_phl_cmd_alloc_stainfo(void *phl,
1263*4882a593Smuzhiyun                           struct rtw_phl_stainfo_t **sta,
1264*4882a593Smuzhiyun                           u8 *sta_addr,
1265*4882a593Smuzhiyun                           struct rtw_wifi_role_t *wrole,
1266*4882a593Smuzhiyun                           bool alloc, bool only_hw,
1267*4882a593Smuzhiyun                           enum phl_cmd_type cmd_type,
1268*4882a593Smuzhiyun                           u32 cmd_timeout)
1269*4882a593Smuzhiyun {
1270*4882a593Smuzhiyun #ifdef CONFIG_CMD_DISP
1271*4882a593Smuzhiyun 	return _phl_cmd_alloc_stainfo(phl, sta, sta_addr, wrole, alloc, only_hw, cmd_type, cmd_timeout);
1272*4882a593Smuzhiyun #else
1273*4882a593Smuzhiyun 	PHL_TRACE(COMP_PHL_DBG, _PHL_INFO_, "%s: not support alloc stainfo cmd\n",
1274*4882a593Smuzhiyun 				__func__);
1275*4882a593Smuzhiyun 
1276*4882a593Smuzhiyun 	return _phl_alloc_stainfo((struct phl_info_t *)phl, sta, sta_addr, wrole, alloc, only_hw);
1277*4882a593Smuzhiyun #endif /* CONFIG_CMD_DISP */
1278*4882a593Smuzhiyun }
1279*4882a593Smuzhiyun 
1280*4882a593Smuzhiyun enum rtw_phl_status
phl_wifi_role_free_stainfo_hw(struct phl_info_t * phl_info,struct rtw_wifi_role_t * wrole)1281*4882a593Smuzhiyun phl_wifi_role_free_stainfo_hw(struct phl_info_t *phl_info,
1282*4882a593Smuzhiyun                               struct rtw_wifi_role_t *wrole)
1283*4882a593Smuzhiyun {
1284*4882a593Smuzhiyun 	struct macid_ctl_t *mc = phl_to_mac_ctrl(phl_info);
1285*4882a593Smuzhiyun 	u16 max_macid_num = mc->max_num;
1286*4882a593Smuzhiyun 	struct rtw_phl_stainfo_t *sta = NULL;
1287*4882a593Smuzhiyun 	u32 *used_map;
1288*4882a593Smuzhiyun 	u16 mid;
1289*4882a593Smuzhiyun 
1290*4882a593Smuzhiyun 	used_map = &mc->wifi_role_usedmap[wrole->id][0];
1291*4882a593Smuzhiyun 
1292*4882a593Smuzhiyun 	for(mid = 0; mid < max_macid_num; mid++) {
1293*4882a593Smuzhiyun 		if (_phl_macid_is_used(used_map, mid)) {
1294*4882a593Smuzhiyun 			sta = mc->sta[mid];
1295*4882a593Smuzhiyun 			if (sta) {
1296*4882a593Smuzhiyun 				PHL_INFO("%s [WR-%d] free sta_info(MID:%d)\n",
1297*4882a593Smuzhiyun 					__func__, wrole->id, sta->macid);
1298*4882a593Smuzhiyun 				phl_free_stainfo_hw(phl_info, sta);
1299*4882a593Smuzhiyun 			}
1300*4882a593Smuzhiyun 		}
1301*4882a593Smuzhiyun 	}
1302*4882a593Smuzhiyun 	return RTW_PHL_STATUS_SUCCESS;
1303*4882a593Smuzhiyun }
1304*4882a593Smuzhiyun 
1305*4882a593Smuzhiyun enum rtw_phl_status
phl_wifi_role_free_stainfo_sw(struct phl_info_t * phl_info,struct rtw_wifi_role_t * role)1306*4882a593Smuzhiyun phl_wifi_role_free_stainfo_sw(struct phl_info_t *phl_info,
1307*4882a593Smuzhiyun 				struct rtw_wifi_role_t *role)
1308*4882a593Smuzhiyun {
1309*4882a593Smuzhiyun 	struct rtw_phl_stainfo_t *phl_sta = NULL;
1310*4882a593Smuzhiyun 	struct stainfo_ctl_t *sta_ctrl = phl_to_sta_ctrl(phl_info);
1311*4882a593Smuzhiyun 
1312*4882a593Smuzhiyun 	PHL_DUMP_STACTRL_EX(phl_info);
1313*4882a593Smuzhiyun 	do {
1314*4882a593Smuzhiyun 		phl_sta = phl_stainfo_dequeue(phl_info, &role->assoc_sta_queue);
1315*4882a593Smuzhiyun 
1316*4882a593Smuzhiyun 		if (phl_sta) {
1317*4882a593Smuzhiyun 			phl_free_stainfo_sw(phl_info, phl_sta);
1318*4882a593Smuzhiyun 			phl_stainfo_enqueue(phl_info,
1319*4882a593Smuzhiyun 						&sta_ctrl->free_sta_queue, phl_sta);
1320*4882a593Smuzhiyun 		}
1321*4882a593Smuzhiyun 	} while(phl_sta != NULL);
1322*4882a593Smuzhiyun 
1323*4882a593Smuzhiyun 	return RTW_PHL_STATUS_SUCCESS;
1324*4882a593Smuzhiyun }
1325*4882a593Smuzhiyun 
1326*4882a593Smuzhiyun enum rtw_phl_status
phl_wifi_role_free_stainfo(struct phl_info_t * phl_info,struct rtw_wifi_role_t * role)1327*4882a593Smuzhiyun phl_wifi_role_free_stainfo(struct phl_info_t *phl_info,
1328*4882a593Smuzhiyun                            struct rtw_wifi_role_t *role)
1329*4882a593Smuzhiyun {
1330*4882a593Smuzhiyun 	struct rtw_phl_stainfo_t *phl_sta = NULL;
1331*4882a593Smuzhiyun 	struct stainfo_ctl_t *sta_ctrl = phl_to_sta_ctrl(phl_info);
1332*4882a593Smuzhiyun 
1333*4882a593Smuzhiyun 	PHL_DUMP_STACTRL_EX(phl_info);
1334*4882a593Smuzhiyun 	do {
1335*4882a593Smuzhiyun 		phl_sta = phl_stainfo_dequeue(phl_info, &role->assoc_sta_queue);
1336*4882a593Smuzhiyun 
1337*4882a593Smuzhiyun 		if (phl_sta) {
1338*4882a593Smuzhiyun 			phl_free_stainfo_hw(phl_info, phl_sta);
1339*4882a593Smuzhiyun 			phl_free_stainfo_sw(phl_info, phl_sta);
1340*4882a593Smuzhiyun 			phl_stainfo_enqueue(phl_info,
1341*4882a593Smuzhiyun 			                    &sta_ctrl->free_sta_queue,
1342*4882a593Smuzhiyun 			                    phl_sta);
1343*4882a593Smuzhiyun 		}
1344*4882a593Smuzhiyun 	} while(phl_sta != NULL);
1345*4882a593Smuzhiyun 
1346*4882a593Smuzhiyun 	return RTW_PHL_STATUS_SUCCESS;
1347*4882a593Smuzhiyun }
1348*4882a593Smuzhiyun 
1349*4882a593Smuzhiyun /**
1350*4882a593Smuzhiyun  * According to 802.11 spec 26.5.2.3.2
1351*4882a593Smuzhiyun  * We shall not transmit HE TB PPDU with RU-26 on DFS channel
1352*4882a593Smuzhiyun  */
1353*4882a593Smuzhiyun static void
_phl_set_dfs_tb_ctrl(struct phl_info_t * phl_info,struct rtw_wifi_role_t * wrole)1354*4882a593Smuzhiyun _phl_set_dfs_tb_ctrl(struct phl_info_t *phl_info,
1355*4882a593Smuzhiyun 		     struct rtw_wifi_role_t *wrole)
1356*4882a593Smuzhiyun {
1357*4882a593Smuzhiyun 	struct rtw_regulation_channel reg_ch = {0};
1358*4882a593Smuzhiyun 	enum band_type band = wrole->chandef.band;
1359*4882a593Smuzhiyun 	u8 channel = wrole->chandef.chan;
1360*4882a593Smuzhiyun 	bool is_dfs = false;
1361*4882a593Smuzhiyun 
1362*4882a593Smuzhiyun 
1363*4882a593Smuzhiyun 	if (rtw_phl_regulation_query_ch(phl_info, band, channel, &reg_ch)) {
1364*4882a593Smuzhiyun 		if (reg_ch.property & CH_DFS)
1365*4882a593Smuzhiyun 			is_dfs = true;
1366*4882a593Smuzhiyun 
1367*4882a593Smuzhiyun 		rtw_hal_set_dfs_tb_ctrl(phl_info->hal, is_dfs);
1368*4882a593Smuzhiyun 	}
1369*4882a593Smuzhiyun }
1370*4882a593Smuzhiyun 
1371*4882a593Smuzhiyun static void
_phl_no_link_reset_sta_info(struct phl_info_t * phl_info,struct rtw_phl_stainfo_t * sta)1372*4882a593Smuzhiyun _phl_no_link_reset_sta_info(struct phl_info_t *phl_info, struct rtw_phl_stainfo_t *sta)
1373*4882a593Smuzhiyun {
1374*4882a593Smuzhiyun 	void *drv = phl_to_drvpriv(phl_info);
1375*4882a593Smuzhiyun 
1376*4882a593Smuzhiyun 	/* asoc cap */
1377*4882a593Smuzhiyun 	_os_mem_set(drv, &sta->asoc_cap, 0, sizeof(struct protocol_cap_t));
1378*4882a593Smuzhiyun 
1379*4882a593Smuzhiyun 	/* other capabilities under stainfo need to reset with default value */
1380*4882a593Smuzhiyun 	sta->tf_trs = 0;
1381*4882a593Smuzhiyun 
1382*4882a593Smuzhiyun 	/* protection mode */
1383*4882a593Smuzhiyun 	sta->protect = RTW_PROTECT_DISABLE;
1384*4882a593Smuzhiyun }
1385*4882a593Smuzhiyun 
1386*4882a593Smuzhiyun /**
1387*4882a593Smuzhiyun  * This function is called once station associated with AP
1388*4882a593Smuzhiyun  * or incoming station got associated under AP mode.
1389*4882a593Smuzhiyun  * Before calling this function, update address / net_type / ...
1390*4882a593Smuzhiyun  * information of stainfo
1391*4882a593Smuzhiyun  * It will configure some hw register, ex
1392*4882a593Smuzhiyun  * address cam
1393*4882a593Smuzhiyun  * @phl: see phl_info_t
1394*4882a593Smuzhiyun  * @stainfo: information is updated through phl_station_info
1395*4882a593Smuzhiyun  */
1396*4882a593Smuzhiyun static enum rtw_phl_status
phl_update_media_status(struct phl_info_t * phl_info,struct rtw_phl_stainfo_t * sta,u8 * sta_addr,bool is_connect)1397*4882a593Smuzhiyun phl_update_media_status(struct phl_info_t *phl_info, struct rtw_phl_stainfo_t *sta,
1398*4882a593Smuzhiyun 			u8 *sta_addr, bool is_connect)
1399*4882a593Smuzhiyun {
1400*4882a593Smuzhiyun 	struct rtw_wifi_role_t *wrole = sta->wrole;
1401*4882a593Smuzhiyun 	void *drv = phl_to_drvpriv(phl_info);
1402*4882a593Smuzhiyun 	enum rtw_hal_status hstatus = RTW_HAL_STATUS_FAILURE;
1403*4882a593Smuzhiyun 	enum rtw_phl_status pstatus = RTW_PHL_STATUS_FAILURE;
1404*4882a593Smuzhiyun 	bool is_sta_linked = false;
1405*4882a593Smuzhiyun 
1406*4882a593Smuzhiyun 	is_sta_linked = rtw_hal_is_sta_linked(phl_info->hal, sta);
1407*4882a593Smuzhiyun 	if (is_connect == true && is_sta_linked == true) {
1408*4882a593Smuzhiyun 		PHL_ERR("%s STA (MAC_ID:%d) had connected\n", __func__, sta->macid);
1409*4882a593Smuzhiyun 		goto _exit;
1410*4882a593Smuzhiyun 	}
1411*4882a593Smuzhiyun 	if (is_connect == false && is_sta_linked == false) {
1412*4882a593Smuzhiyun 		/* handle connect abort case */
1413*4882a593Smuzhiyun 		if (wrole->mstate == MLME_LINKING) {
1414*4882a593Smuzhiyun 			PHL_INFO("%s MAC_ID(%d) connect abort\n", __func__, sta->macid);
1415*4882a593Smuzhiyun 			pstatus = RTW_PHL_STATUS_SUCCESS;
1416*4882a593Smuzhiyun 		} else {
1417*4882a593Smuzhiyun 			PHL_ERR("%s MAC_ID(%d) had disconnected\n", __func__, sta->macid);
1418*4882a593Smuzhiyun 		}
1419*4882a593Smuzhiyun 
1420*4882a593Smuzhiyun 		if (wrole->type == PHL_RTYPE_STATION || wrole->type == PHL_RTYPE_P2P_GC)
1421*4882a593Smuzhiyun 			wrole->mstate = MLME_NO_LINK;
1422*4882a593Smuzhiyun 		goto _exit;
1423*4882a593Smuzhiyun 	}
1424*4882a593Smuzhiyun 
1425*4882a593Smuzhiyun 	/* reset trx statistics */
1426*4882a593Smuzhiyun 	if (is_connect == false) {
1427*4882a593Smuzhiyun 		phl_reset_tx_stats(&sta->stats);
1428*4882a593Smuzhiyun 		phl_reset_rx_stats(&sta->stats);
1429*4882a593Smuzhiyun 		_phl_no_link_reset_sta_info(phl_info, sta);
1430*4882a593Smuzhiyun 		CLEAR_STATUS_FLAG(wrole->status, WR_STATUS_TSF_SYNC);
1431*4882a593Smuzhiyun 	} else {
1432*4882a593Smuzhiyun 		phl_clean_sta_bcn_info(phl_info, sta);
1433*4882a593Smuzhiyun 	}
1434*4882a593Smuzhiyun 
1435*4882a593Smuzhiyun 	/* Configure address cam, including net_type and sync_tsf */
1436*4882a593Smuzhiyun 	if ((wrole->type == PHL_RTYPE_STATION) || (wrole->type == PHL_RTYPE_P2P_GC)
1437*4882a593Smuzhiyun 	#ifdef CONFIG_PHL_TDLS
1438*4882a593Smuzhiyun 		/* STA disconnects with the associated AP before tearing down with TDLS peers */
1439*4882a593Smuzhiyun 		|| ((wrole->type == PHL_RTYPE_TDLS) && (!sta_addr))
1440*4882a593Smuzhiyun 	#endif
1441*4882a593Smuzhiyun 	) {
1442*4882a593Smuzhiyun 		if (is_connect) {
1443*4882a593Smuzhiyun 			wrole->mstate = MLME_LINKED;
1444*4882a593Smuzhiyun 			_os_mem_cpy(drv, sta->mac_addr, sta_addr, MAC_ALEN);
1445*4882a593Smuzhiyun 			_phl_set_dfs_tb_ctrl(phl_info, wrole);
1446*4882a593Smuzhiyun 		} else {
1447*4882a593Smuzhiyun 			wrole->mstate = MLME_NO_LINK;
1448*4882a593Smuzhiyun 		}
1449*4882a593Smuzhiyun 	}
1450*4882a593Smuzhiyun 	#ifdef RTW_WKARD_AP_CLIENT_ADD_DEL_NTY
1451*4882a593Smuzhiyun 	else if ((wrole->type == PHL_RTYPE_AP) ||
1452*4882a593Smuzhiyun 		  (wrole->type == PHL_RTYPE_VAP) ||
1453*4882a593Smuzhiyun 		  (wrole->type == PHL_RTYPE_MESH) ||
1454*4882a593Smuzhiyun 		  (wrole->type == PHL_RTYPE_P2P_GO)) {
1455*4882a593Smuzhiyun 			if (is_connect)
1456*4882a593Smuzhiyun 				phl_role_ap_client_notify(phl_info, wrole, MLME_LINKED);
1457*4882a593Smuzhiyun 	}
1458*4882a593Smuzhiyun 	#endif
1459*4882a593Smuzhiyun 	hstatus = rtw_hal_update_sta_entry(phl_info->hal, sta, is_connect);
1460*4882a593Smuzhiyun 	if (hstatus != RTW_HAL_STATUS_SUCCESS) {
1461*4882a593Smuzhiyun 		PHL_ERR("rtw_hal_update_sta_entry failure!\n");
1462*4882a593Smuzhiyun 		goto _exit;
1463*4882a593Smuzhiyun 	}
1464*4882a593Smuzhiyun 
1465*4882a593Smuzhiyun 	if (wrole->type == PHL_RTYPE_STATION
1466*4882a593Smuzhiyun 	#ifdef CONFIG_PHL_TDLS
1467*4882a593Smuzhiyun 		/* STA disconnects with the associated AP before tearing down with TDLS peers */
1468*4882a593Smuzhiyun 		|| ((wrole->type == PHL_RTYPE_TDLS) && (!sta_addr))
1469*4882a593Smuzhiyun 	#endif
1470*4882a593Smuzhiyun 	) {
1471*4882a593Smuzhiyun 		hstatus = rtw_hal_role_cfg(phl_info->hal, wrole);
1472*4882a593Smuzhiyun 		if (hstatus != RTW_HAL_STATUS_SUCCESS) {
1473*4882a593Smuzhiyun 			PHL_ERR("rtw_hal_role_cfg failure!\n");
1474*4882a593Smuzhiyun 			goto _exit;
1475*4882a593Smuzhiyun 		}
1476*4882a593Smuzhiyun 	}
1477*4882a593Smuzhiyun 
1478*4882a593Smuzhiyun 	pstatus = RTW_PHL_STATUS_SUCCESS;
1479*4882a593Smuzhiyun 
1480*4882a593Smuzhiyun 	/* TODO: Configure RCR */
1481*4882a593Smuzhiyun _exit:
1482*4882a593Smuzhiyun 	return pstatus;
1483*4882a593Smuzhiyun }
1484*4882a593Smuzhiyun 
1485*4882a593Smuzhiyun #ifdef CONFIG_CMD_DISP
1486*4882a593Smuzhiyun struct sta_media_param {
1487*4882a593Smuzhiyun 	struct rtw_phl_stainfo_t *sta;
1488*4882a593Smuzhiyun 	u8 sta_addr[MAC_ALEN];
1489*4882a593Smuzhiyun 	bool is_connect;
1490*4882a593Smuzhiyun };
1491*4882a593Smuzhiyun 
1492*4882a593Smuzhiyun enum rtw_phl_status
phl_update_media_status_hdl(struct phl_info_t * phl_info,u8 * param)1493*4882a593Smuzhiyun phl_update_media_status_hdl(struct phl_info_t *phl_info, u8 *param)
1494*4882a593Smuzhiyun {
1495*4882a593Smuzhiyun 	struct sta_media_param *media_sts = (struct sta_media_param *)param;
1496*4882a593Smuzhiyun 
1497*4882a593Smuzhiyun 	return phl_update_media_status(phl_info,
1498*4882a593Smuzhiyun 			media_sts->sta, media_sts->sta_addr, media_sts->is_connect);
1499*4882a593Smuzhiyun }
1500*4882a593Smuzhiyun 
phl_update_media_status_done(void * drv_priv,u8 * cmd,u32 cmd_len,enum rtw_phl_status status)1501*4882a593Smuzhiyun void phl_update_media_status_done(void *drv_priv, u8 *cmd, u32 cmd_len,
1502*4882a593Smuzhiyun 						enum rtw_phl_status status)
1503*4882a593Smuzhiyun {
1504*4882a593Smuzhiyun 	if (cmd) {
1505*4882a593Smuzhiyun 		_os_kmem_free(drv_priv, cmd, cmd_len);
1506*4882a593Smuzhiyun 		cmd = NULL;
1507*4882a593Smuzhiyun 	}
1508*4882a593Smuzhiyun }
1509*4882a593Smuzhiyun #endif
1510*4882a593Smuzhiyun 
1511*4882a593Smuzhiyun enum rtw_phl_status
rtw_phl_cmd_update_media_status(void * phl,struct rtw_phl_stainfo_t * sta,u8 * sta_addr,bool is_connect,enum phl_cmd_type cmd_type,u32 cmd_timeout)1512*4882a593Smuzhiyun rtw_phl_cmd_update_media_status(void *phl,
1513*4882a593Smuzhiyun                                 struct rtw_phl_stainfo_t *sta,
1514*4882a593Smuzhiyun                                 u8 *sta_addr,
1515*4882a593Smuzhiyun                                 bool is_connect,
1516*4882a593Smuzhiyun                                 enum phl_cmd_type cmd_type,
1517*4882a593Smuzhiyun                                 u32 cmd_timeout)
1518*4882a593Smuzhiyun {
1519*4882a593Smuzhiyun #ifdef CONFIG_CMD_DISP
1520*4882a593Smuzhiyun 	enum rtw_phl_status psts = RTW_PHL_STATUS_FAILURE;
1521*4882a593Smuzhiyun 	struct phl_info_t *phl_info = (struct phl_info_t *)phl;
1522*4882a593Smuzhiyun 	void *drv = phl_to_drvpriv(phl_info);
1523*4882a593Smuzhiyun 	struct rtw_wifi_role_t *wrole = NULL;
1524*4882a593Smuzhiyun 	struct sta_media_param *sta_ms = NULL;
1525*4882a593Smuzhiyun 	u32 sta_ms_len = 0;
1526*4882a593Smuzhiyun 
1527*4882a593Smuzhiyun 	if (cmd_type == PHL_CMD_DIRECTLY) {
1528*4882a593Smuzhiyun 		psts = phl_update_media_status(phl_info, sta, sta_addr, is_connect);
1529*4882a593Smuzhiyun 		goto _exit;
1530*4882a593Smuzhiyun 	}
1531*4882a593Smuzhiyun 
1532*4882a593Smuzhiyun 	sta_ms_len = sizeof(struct sta_media_param);
1533*4882a593Smuzhiyun 	sta_ms = _os_kmem_alloc(drv, sta_ms_len);
1534*4882a593Smuzhiyun 	if (sta_ms == NULL) {
1535*4882a593Smuzhiyun 		PHL_ERR("%s: alloc sta media status param failed!\n", __func__);
1536*4882a593Smuzhiyun 		psts = RTW_PHL_STATUS_RESOURCE;
1537*4882a593Smuzhiyun 		goto _exit;
1538*4882a593Smuzhiyun 	}
1539*4882a593Smuzhiyun 	_os_mem_set(drv, sta_ms, 0, sta_ms_len);
1540*4882a593Smuzhiyun 	sta_ms->sta = sta;
1541*4882a593Smuzhiyun 	sta_ms->is_connect = is_connect;
1542*4882a593Smuzhiyun 	if (is_connect && sta_addr)
1543*4882a593Smuzhiyun 		_os_mem_cpy(drv, sta_ms->sta_addr, sta_addr, MAC_ALEN);
1544*4882a593Smuzhiyun 
1545*4882a593Smuzhiyun 	wrole = sta->wrole;
1546*4882a593Smuzhiyun 
1547*4882a593Smuzhiyun 	psts = phl_cmd_enqueue(phl_info,
1548*4882a593Smuzhiyun 	                       wrole->hw_band,
1549*4882a593Smuzhiyun 	                       MSG_EVT_STA_MEDIA_STATUS_UPT,
1550*4882a593Smuzhiyun 	                       (u8*)sta_ms,
1551*4882a593Smuzhiyun 	                       sta_ms_len,
1552*4882a593Smuzhiyun 	                       phl_update_media_status_done,
1553*4882a593Smuzhiyun 	                       cmd_type,
1554*4882a593Smuzhiyun 	                       cmd_timeout);
1555*4882a593Smuzhiyun 
1556*4882a593Smuzhiyun 	if (is_cmd_failure(psts)) {
1557*4882a593Smuzhiyun 		/* Send cmd success, but wait cmd fail*/
1558*4882a593Smuzhiyun 		psts = RTW_PHL_STATUS_FAILURE;
1559*4882a593Smuzhiyun 	} else if (psts != RTW_PHL_STATUS_SUCCESS) {
1560*4882a593Smuzhiyun 		/* Send cmd fail */
1561*4882a593Smuzhiyun 		psts = RTW_PHL_STATUS_FAILURE;
1562*4882a593Smuzhiyun 		_os_kmem_free(drv, sta_ms, sta_ms_len);
1563*4882a593Smuzhiyun 	}
1564*4882a593Smuzhiyun _exit:
1565*4882a593Smuzhiyun 	return psts;
1566*4882a593Smuzhiyun #else
1567*4882a593Smuzhiyun 	PHL_TRACE(COMP_PHL_DBG, _PHL_INFO_, "%s: not support cmd to update media status\n",
1568*4882a593Smuzhiyun 	          __func__);
1569*4882a593Smuzhiyun 
1570*4882a593Smuzhiyun 	return phl_update_media_status((struct phl_info_t *)phl, sta, sta_addr, is_connect);
1571*4882a593Smuzhiyun #endif
1572*4882a593Smuzhiyun }
1573*4882a593Smuzhiyun 
1574*4882a593Smuzhiyun /**
1575*4882a593Smuzhiyun  * This function is called once station info changed
1576*4882a593Smuzhiyun  * (BW/NSS/RAMASK/SEC/ROLE/MACADDR........)
1577*4882a593Smuzhiyun  * @phl: see phl_info_t
1578*4882a593Smuzhiyun  * @stainfo: information is updated through phl_station_info
1579*4882a593Smuzhiyun  * @mode: see phl_upd_mode
1580*4882a593Smuzhiyun  */
1581*4882a593Smuzhiyun enum rtw_phl_status
phl_change_stainfo(struct phl_info_t * phl_info,struct rtw_phl_stainfo_t * sta,enum phl_upd_mode mode)1582*4882a593Smuzhiyun phl_change_stainfo(struct phl_info_t *phl_info, struct rtw_phl_stainfo_t *sta,
1583*4882a593Smuzhiyun 			enum phl_upd_mode mode)
1584*4882a593Smuzhiyun {
1585*4882a593Smuzhiyun 	enum rtw_hal_status hstatus = RTW_HAL_STATUS_FAILURE;
1586*4882a593Smuzhiyun 
1587*4882a593Smuzhiyun 	hstatus = rtw_hal_change_sta_entry(phl_info->hal, sta, mode);
1588*4882a593Smuzhiyun 	if (hstatus != RTW_HAL_STATUS_SUCCESS) {
1589*4882a593Smuzhiyun 		PHL_ERR("rtw_hal_change_sta_entry failure!\n");
1590*4882a593Smuzhiyun 		return RTW_PHL_STATUS_FAILURE;
1591*4882a593Smuzhiyun 	}
1592*4882a593Smuzhiyun 	return RTW_PHL_STATUS_SUCCESS;
1593*4882a593Smuzhiyun }
1594*4882a593Smuzhiyun 
1595*4882a593Smuzhiyun static enum rtw_phl_status
_change_stainfo(struct phl_info_t * phl_info,struct rtw_phl_stainfo_t * sta,enum sta_chg_id chg_id,u8 * chg_info,u8 chg_info_len)1596*4882a593Smuzhiyun _change_stainfo(struct phl_info_t *phl_info,
1597*4882a593Smuzhiyun 	struct rtw_phl_stainfo_t *sta, enum sta_chg_id chg_id, u8 *chg_info, u8 chg_info_len)
1598*4882a593Smuzhiyun {
1599*4882a593Smuzhiyun 	enum phl_upd_mode mode = PHL_UPD_STA_INFO_CHANGE;
1600*4882a593Smuzhiyun 
1601*4882a593Smuzhiyun 	switch (chg_id) {
1602*4882a593Smuzhiyun 	case STA_CHG_BW:
1603*4882a593Smuzhiyun 	case STA_CHG_NSS:
1604*4882a593Smuzhiyun 	case STA_CHG_RAMASK:
1605*4882a593Smuzhiyun 	{
1606*4882a593Smuzhiyun 		PHL_INFO("%s MACID:%d %02x:%02x:%02x:%02x:%02x:%02x update bw\n",
1607*4882a593Smuzhiyun 		         __func__, sta->macid,
1608*4882a593Smuzhiyun 		         sta->mac_addr[0], sta->mac_addr[1], sta->mac_addr[2],
1609*4882a593Smuzhiyun 		         sta->mac_addr[3], sta->mac_addr[4], sta->mac_addr[5]);
1610*4882a593Smuzhiyun 	}
1611*4882a593Smuzhiyun 		break;
1612*4882a593Smuzhiyun 	case STA_CHG_SEC_MODE:
1613*4882a593Smuzhiyun 		sta->sec_mode = *((u8*)chg_info);
1614*4882a593Smuzhiyun 		break;
1615*4882a593Smuzhiyun 	case STA_CHG_MBSSID:
1616*4882a593Smuzhiyun 		sta->addr_sel = 1;
1617*4882a593Smuzhiyun 		sta->addr_msk = *((u8*)chg_info);
1618*4882a593Smuzhiyun 		break;
1619*4882a593Smuzhiyun 	case STA_CHG_RA_GILTF:
1620*4882a593Smuzhiyun 		sta->hal_sta->ra_info.cal_giltf = *((u8*)chg_info);
1621*4882a593Smuzhiyun 		sta->hal_sta->ra_info.fix_giltf_en = true;
1622*4882a593Smuzhiyun 		PHL_INFO("%s: Config RA GI LTF = %d\n", __FUNCTION__, *((u8*)chg_info));
1623*4882a593Smuzhiyun 		break;
1624*4882a593Smuzhiyun 	case STA_CHG_MAX:
1625*4882a593Smuzhiyun 		PHL_TRACE(COMP_PHL_DBG, _PHL_DEBUG_, "rtw_phl_change_stainfo(): Unsupported case:%d, please check it\n",
1626*4882a593Smuzhiyun 				chg_id);
1627*4882a593Smuzhiyun 		break;
1628*4882a593Smuzhiyun 	default:
1629*4882a593Smuzhiyun 		PHL_TRACE(COMP_PHL_DBG, _PHL_DEBUG_, "rtw_phl_change_stainfo(): Unrecognize case:%d, please check it\n",
1630*4882a593Smuzhiyun 				chg_id);
1631*4882a593Smuzhiyun 		break;
1632*4882a593Smuzhiyun 	}
1633*4882a593Smuzhiyun 
1634*4882a593Smuzhiyun 	return phl_change_stainfo(phl_info, sta, mode);
1635*4882a593Smuzhiyun }
1636*4882a593Smuzhiyun 
1637*4882a593Smuzhiyun #ifdef CONFIG_CMD_DISP
1638*4882a593Smuzhiyun struct sta_chg_param {
1639*4882a593Smuzhiyun 	struct rtw_phl_stainfo_t *sta;
1640*4882a593Smuzhiyun 	enum sta_chg_id id;
1641*4882a593Smuzhiyun 	u8 *info;
1642*4882a593Smuzhiyun 	u8 info_len;
1643*4882a593Smuzhiyun };
1644*4882a593Smuzhiyun 
1645*4882a593Smuzhiyun enum rtw_phl_status
phl_cmd_change_stainfo_hdl(struct phl_info_t * phl_info,u8 * param)1646*4882a593Smuzhiyun phl_cmd_change_stainfo_hdl(struct phl_info_t *phl_info, u8 *param)
1647*4882a593Smuzhiyun {
1648*4882a593Smuzhiyun 	struct sta_chg_param *sta_param = (struct sta_chg_param *)param;
1649*4882a593Smuzhiyun 
1650*4882a593Smuzhiyun 	return _change_stainfo(phl_info,
1651*4882a593Smuzhiyun 			sta_param->sta, sta_param->id,
1652*4882a593Smuzhiyun 			sta_param->info, sta_param->info_len);
1653*4882a593Smuzhiyun }
1654*4882a593Smuzhiyun 
1655*4882a593Smuzhiyun static void
_phl_cmd_change_stainfo_done(void * drv_priv,u8 * cmd,u32 cmd_len,enum rtw_phl_status status)1656*4882a593Smuzhiyun _phl_cmd_change_stainfo_done(void *drv_priv, u8 *cmd, u32 cmd_len,
1657*4882a593Smuzhiyun 						enum rtw_phl_status status)
1658*4882a593Smuzhiyun {
1659*4882a593Smuzhiyun 	struct sta_chg_param *sta_chg_info = NULL;
1660*4882a593Smuzhiyun 
1661*4882a593Smuzhiyun 	if (cmd == NULL || cmd_len == 0) {
1662*4882a593Smuzhiyun 		PHL_ERR("%s buf == NULL || buf_len == 0\n", __func__);
1663*4882a593Smuzhiyun 		_os_warn_on(1);
1664*4882a593Smuzhiyun 		return;
1665*4882a593Smuzhiyun 	}
1666*4882a593Smuzhiyun 
1667*4882a593Smuzhiyun 	sta_chg_info = (struct sta_chg_param *)cmd;
1668*4882a593Smuzhiyun 	PHL_INFO("%s - id:%d .....\n", __func__, sta_chg_info->id);
1669*4882a593Smuzhiyun 
1670*4882a593Smuzhiyun 	if (sta_chg_info->info && sta_chg_info->info_len > 0)
1671*4882a593Smuzhiyun 		_os_kmem_free(drv_priv, sta_chg_info->info, sta_chg_info->info_len);
1672*4882a593Smuzhiyun 
1673*4882a593Smuzhiyun 	_os_kmem_free(drv_priv, cmd, cmd_len);
1674*4882a593Smuzhiyun 	cmd = NULL;
1675*4882a593Smuzhiyun }
1676*4882a593Smuzhiyun 
1677*4882a593Smuzhiyun static enum rtw_phl_status
_phl_cmd_change_stainfo(struct phl_info_t * phl_info,struct rtw_phl_stainfo_t * sta,enum sta_chg_id chg_id,u8 * chg_info,u8 chg_info_len,enum phl_cmd_type cmd_type,u32 cmd_timeout)1678*4882a593Smuzhiyun _phl_cmd_change_stainfo(struct phl_info_t *phl_info,
1679*4882a593Smuzhiyun 	struct rtw_phl_stainfo_t *sta, enum sta_chg_id chg_id,
1680*4882a593Smuzhiyun 	u8 *chg_info, u8 chg_info_len,
1681*4882a593Smuzhiyun 	enum phl_cmd_type cmd_type, u32 cmd_timeout)
1682*4882a593Smuzhiyun {
1683*4882a593Smuzhiyun 	void *drv = phl_to_drvpriv(phl_info);
1684*4882a593Smuzhiyun 	enum rtw_phl_status psts = RTW_PHL_STATUS_FAILURE;
1685*4882a593Smuzhiyun 	struct rtw_wifi_role_t *wrole = sta->wrole;
1686*4882a593Smuzhiyun 	struct sta_chg_param *param = NULL;
1687*4882a593Smuzhiyun 	u8 param_len = 0;
1688*4882a593Smuzhiyun 
1689*4882a593Smuzhiyun 	if (cmd_type == PHL_CMD_DIRECTLY) {
1690*4882a593Smuzhiyun 		psts = _change_stainfo(phl_info, sta, chg_id, chg_info, chg_info_len);
1691*4882a593Smuzhiyun 		goto _exit;
1692*4882a593Smuzhiyun 	}
1693*4882a593Smuzhiyun 
1694*4882a593Smuzhiyun 	param_len = sizeof(struct sta_chg_param);
1695*4882a593Smuzhiyun 	param = _os_kmem_alloc(drv, param_len);
1696*4882a593Smuzhiyun 	if (param == NULL) {
1697*4882a593Smuzhiyun 		PHL_ERR("%s: alloc param failed!\n", __func__);
1698*4882a593Smuzhiyun 		psts = RTW_PHL_STATUS_RESOURCE;
1699*4882a593Smuzhiyun 		goto _exit;
1700*4882a593Smuzhiyun 	}
1701*4882a593Smuzhiyun 
1702*4882a593Smuzhiyun 	_os_mem_set(drv, param, 0, param_len);
1703*4882a593Smuzhiyun 	param->sta = sta;
1704*4882a593Smuzhiyun 	param->id = chg_id;
1705*4882a593Smuzhiyun 	param->info_len = chg_info_len;
1706*4882a593Smuzhiyun 
1707*4882a593Smuzhiyun 	if (chg_info_len > 0) {
1708*4882a593Smuzhiyun 		param->info = _os_kmem_alloc(drv, chg_info_len);
1709*4882a593Smuzhiyun 		if (param->info == NULL) {
1710*4882a593Smuzhiyun 			PHL_ERR("%s: alloc param->info failed!\n", __func__);
1711*4882a593Smuzhiyun 			psts = RTW_PHL_STATUS_RESOURCE;
1712*4882a593Smuzhiyun 			goto _err_info;
1713*4882a593Smuzhiyun 		}
1714*4882a593Smuzhiyun 
1715*4882a593Smuzhiyun 		_os_mem_set(drv, param->info, 0, chg_info_len);
1716*4882a593Smuzhiyun 		_os_mem_cpy(drv, param->info, chg_info, chg_info_len);
1717*4882a593Smuzhiyun 	} else {
1718*4882a593Smuzhiyun 		param->info = NULL;
1719*4882a593Smuzhiyun 	}
1720*4882a593Smuzhiyun 
1721*4882a593Smuzhiyun 	psts = phl_cmd_enqueue(phl_info,
1722*4882a593Smuzhiyun 	                       wrole->hw_band,
1723*4882a593Smuzhiyun 	                       MSG_EVT_STA_CHG_STAINFO,
1724*4882a593Smuzhiyun 	                       (u8 *)param,
1725*4882a593Smuzhiyun 	                       param_len,
1726*4882a593Smuzhiyun 	                       _phl_cmd_change_stainfo_done,
1727*4882a593Smuzhiyun 	                       cmd_type,
1728*4882a593Smuzhiyun 	                       cmd_timeout);
1729*4882a593Smuzhiyun 
1730*4882a593Smuzhiyun 	if (is_cmd_failure(psts)) {
1731*4882a593Smuzhiyun 		/* Send cmd success, but wait cmd fail*/
1732*4882a593Smuzhiyun 		psts = RTW_PHL_STATUS_FAILURE;
1733*4882a593Smuzhiyun 	} else if (psts != RTW_PHL_STATUS_SUCCESS) {
1734*4882a593Smuzhiyun 		/* Send cmd fail */
1735*4882a593Smuzhiyun 		psts = RTW_PHL_STATUS_FAILURE;
1736*4882a593Smuzhiyun 		goto _err_cmd;
1737*4882a593Smuzhiyun 	}
1738*4882a593Smuzhiyun 
1739*4882a593Smuzhiyun 	return psts;
1740*4882a593Smuzhiyun 
1741*4882a593Smuzhiyun _err_cmd:
1742*4882a593Smuzhiyun 	if (param->info)
1743*4882a593Smuzhiyun 		_os_kmem_free(drv, param->info, param->info_len);
1744*4882a593Smuzhiyun _err_info:
1745*4882a593Smuzhiyun 	if (param)
1746*4882a593Smuzhiyun 		_os_kmem_free(drv, param, param_len);
1747*4882a593Smuzhiyun _exit:
1748*4882a593Smuzhiyun 	return psts;
1749*4882a593Smuzhiyun }
1750*4882a593Smuzhiyun #endif
1751*4882a593Smuzhiyun 
1752*4882a593Smuzhiyun enum rtw_phl_status
rtw_phl_cmd_change_stainfo(void * phl,struct rtw_phl_stainfo_t * sta,enum sta_chg_id chg_id,u8 * chg_info,u8 chg_info_len,enum phl_cmd_type cmd_type,u32 cmd_timeout)1753*4882a593Smuzhiyun rtw_phl_cmd_change_stainfo(void *phl,
1754*4882a593Smuzhiyun 	struct rtw_phl_stainfo_t *sta, enum sta_chg_id chg_id,
1755*4882a593Smuzhiyun 	u8 *chg_info, u8 chg_info_len,
1756*4882a593Smuzhiyun 	enum phl_cmd_type cmd_type, u32 cmd_timeout)
1757*4882a593Smuzhiyun {
1758*4882a593Smuzhiyun 	struct phl_info_t *phl_info = (struct phl_info_t *)phl;
1759*4882a593Smuzhiyun 
1760*4882a593Smuzhiyun #ifdef CONFIG_CMD_DISP
1761*4882a593Smuzhiyun 	return _phl_cmd_change_stainfo(phl_info, sta, chg_id, chg_info, chg_info_len,
1762*4882a593Smuzhiyun 		cmd_type, cmd_timeout);
1763*4882a593Smuzhiyun #else
1764*4882a593Smuzhiyun 	PHL_TRACE(COMP_PHL_DBG, _PHL_INFO_, "%s: not support alloc stainfo cmd\n",
1765*4882a593Smuzhiyun 				__func__);
1766*4882a593Smuzhiyun 
1767*4882a593Smuzhiyun 	return _change_stainfo(phl_info, sta, chg_id, chg_info, chg_info_len);
1768*4882a593Smuzhiyun #endif /* CONFIG_CMD_DISP */
1769*4882a593Smuzhiyun }
1770*4882a593Smuzhiyun /**
1771*4882a593Smuzhiyun  * This function updates tx/rx traffic status of each active station info
1772*4882a593Smuzhiyun  */
1773*4882a593Smuzhiyun void
phl_sta_trx_tfc_upd(struct phl_info_t * phl_info)1774*4882a593Smuzhiyun phl_sta_trx_tfc_upd(struct phl_info_t *phl_info)
1775*4882a593Smuzhiyun {
1776*4882a593Smuzhiyun 	struct macid_ctl_t *macid_ctl = phl_to_mac_ctrl(phl_info);
1777*4882a593Smuzhiyun 	struct rtw_phl_stainfo_t *phl_sta = NULL;
1778*4882a593Smuzhiyun 	struct rtw_stats *sta_stats = NULL;
1779*4882a593Smuzhiyun 	u16 max_macid_num = 0;
1780*4882a593Smuzhiyun 	u16 mid = 0;
1781*4882a593Smuzhiyun 
1782*4882a593Smuzhiyun 	max_macid_num = macid_ctl->max_num;
1783*4882a593Smuzhiyun 
1784*4882a593Smuzhiyun 	_os_spinlock(phl_to_drvpriv(phl_info), &macid_ctl->lock, _bh, NULL);
1785*4882a593Smuzhiyun 	for(mid = 0; mid < max_macid_num; mid++) {
1786*4882a593Smuzhiyun 		if (_phl_macid_is_used(macid_ctl->used_map, mid)) {
1787*4882a593Smuzhiyun 			phl_sta = macid_ctl->sta[mid];
1788*4882a593Smuzhiyun 			if (phl_sta) {
1789*4882a593Smuzhiyun 				#ifdef CONFIG_PHL_RA_TXSTS_DBG
1790*4882a593Smuzhiyun 				/* issue H2C to get ra txsts report */
1791*4882a593Smuzhiyun 				rtw_phl_txsts_rpt_config(phl_info, phl_sta);
1792*4882a593Smuzhiyun 				#endif
1793*4882a593Smuzhiyun 				sta_stats = &phl_sta->stats;
1794*4882a593Smuzhiyun 				phl_tx_traffic_upd(sta_stats);
1795*4882a593Smuzhiyun 				phl_rx_traffic_upd(sta_stats);
1796*4882a593Smuzhiyun 			}
1797*4882a593Smuzhiyun 		}
1798*4882a593Smuzhiyun 	}
1799*4882a593Smuzhiyun 	_os_spinunlock(phl_to_drvpriv(phl_info), &macid_ctl->lock, _bh, NULL);
1800*4882a593Smuzhiyun }
1801*4882a593Smuzhiyun 
1802*4882a593Smuzhiyun 
1803*4882a593Smuzhiyun /**
1804*4882a593Smuzhiyun  * This function is used to get phl sta info
1805*4882a593Smuzhiyun  * by macid
1806*4882a593Smuzhiyun  * @phl: see phl_info_t
1807*4882a593Smuzhiyun  * @macid: macid
1808*4882a593Smuzhiyun  */
1809*4882a593Smuzhiyun struct rtw_phl_stainfo_t *
rtw_phl_get_stainfo_by_macid(void * phl,u16 macid)1810*4882a593Smuzhiyun rtw_phl_get_stainfo_by_macid(void *phl, u16 macid)
1811*4882a593Smuzhiyun {
1812*4882a593Smuzhiyun 	struct phl_info_t *phl_info = (struct phl_info_t *)phl;
1813*4882a593Smuzhiyun 	struct macid_ctl_t *macid_ctl = phl_to_mac_ctrl(phl_info);
1814*4882a593Smuzhiyun 	struct rtw_phl_stainfo_t *phl_sta = NULL;
1815*4882a593Smuzhiyun 
1816*4882a593Smuzhiyun 	if (macid >= macid_ctl->max_num) {
1817*4882a593Smuzhiyun 		PHL_ERR("%s macid(%d) is invalid\n", __func__, macid);
1818*4882a593Smuzhiyun 		return NULL;
1819*4882a593Smuzhiyun 	}
1820*4882a593Smuzhiyun 	_os_spinlock(phl_to_drvpriv(phl_info), &macid_ctl->lock, _bh, NULL);
1821*4882a593Smuzhiyun 	if (_phl_macid_is_used(macid_ctl->used_map, macid))
1822*4882a593Smuzhiyun 		phl_sta = macid_ctl->sta[macid];
1823*4882a593Smuzhiyun 
1824*4882a593Smuzhiyun 	if (phl_sta == NULL) {
1825*4882a593Smuzhiyun 		PHL_TRACE(COMP_PHL_DBG, _PHL_DEBUG_,"%s sta info (macid:%d) is NULL\n", __func__, macid);
1826*4882a593Smuzhiyun 		#ifdef CONFIG_PHL_USB_RELEASE_RPT_ENABLE
1827*4882a593Smuzhiyun 		/* comment temporarily since release report may report unused macid */
1828*4882a593Smuzhiyun 		/* and trigger call tracing */
1829*4882a593Smuzhiyun 		/* _os_warn_on(1); */
1830*4882a593Smuzhiyun 		#else
1831*4882a593Smuzhiyun 		#ifdef CONFIG_RTW_DEBUG
1832*4882a593Smuzhiyun 		if (_PHL_DEBUG_ <= phl_log_level)
1833*4882a593Smuzhiyun 		_os_warn_on(1);
1834*4882a593Smuzhiyun 		#endif /*CONFIG_RTW_DEBUG*/
1835*4882a593Smuzhiyun 		#endif /* CONFIG_PHL_USB_RELEASE_RPT_ENABLE */
1836*4882a593Smuzhiyun 	}
1837*4882a593Smuzhiyun 	_os_spinunlock(phl_to_drvpriv(phl_info), &macid_ctl->lock, _bh, NULL);
1838*4882a593Smuzhiyun 
1839*4882a593Smuzhiyun 	return phl_sta;
1840*4882a593Smuzhiyun }
1841*4882a593Smuzhiyun 
1842*4882a593Smuzhiyun struct rtw_phl_stainfo_t *
rtw_phl_get_stainfo_by_addr_ex(void * phl,u8 * addr)1843*4882a593Smuzhiyun rtw_phl_get_stainfo_by_addr_ex(void *phl, u8 *addr)
1844*4882a593Smuzhiyun {
1845*4882a593Smuzhiyun 	struct phl_info_t *phl_info = (struct phl_info_t *)phl;
1846*4882a593Smuzhiyun 	struct macid_ctl_t *mc = phl_to_mac_ctrl(phl_info);
1847*4882a593Smuzhiyun 	struct rtw_phl_stainfo_t *sta = NULL;
1848*4882a593Smuzhiyun 	u16 mid = 0;
1849*4882a593Smuzhiyun 	u16 max_macid_num = mc->max_num;
1850*4882a593Smuzhiyun 	bool sta_found = false;
1851*4882a593Smuzhiyun 
1852*4882a593Smuzhiyun 	_os_spinlock(phl_to_drvpriv(phl_info), &mc->lock, _bh, NULL);
1853*4882a593Smuzhiyun 	for(mid = 0; mid < max_macid_num; mid++) {
1854*4882a593Smuzhiyun 		if (_phl_macid_is_used(mc->used_map, mid)) {
1855*4882a593Smuzhiyun 			sta = mc->sta[mid];
1856*4882a593Smuzhiyun 			if (_os_mem_cmp(phl_to_drvpriv(phl_info),
1857*4882a593Smuzhiyun 				sta->mac_addr, addr, MAC_ALEN) == 0) {
1858*4882a593Smuzhiyun 				sta_found = true;
1859*4882a593Smuzhiyun 				break;
1860*4882a593Smuzhiyun 			}
1861*4882a593Smuzhiyun 		}
1862*4882a593Smuzhiyun 	}
1863*4882a593Smuzhiyun 	_os_spinunlock(phl_to_drvpriv(phl_info), &mc->lock, _bh, NULL);
1864*4882a593Smuzhiyun 
1865*4882a593Smuzhiyun 	if (sta_found == false)
1866*4882a593Smuzhiyun 		sta = NULL;
1867*4882a593Smuzhiyun 	return sta;
1868*4882a593Smuzhiyun }
1869*4882a593Smuzhiyun 
rtw_phl_get_macid_by_addr(void * phl,u8 * addr)1870*4882a593Smuzhiyun u16 rtw_phl_get_macid_by_addr(void *phl, u8 *addr)
1871*4882a593Smuzhiyun {
1872*4882a593Smuzhiyun 	struct phl_info_t *phl_info = (struct phl_info_t *)phl;
1873*4882a593Smuzhiyun 	struct macid_ctl_t *mc = phl_to_mac_ctrl(phl_info);
1874*4882a593Smuzhiyun 	struct rtw_phl_stainfo_t *sta = NULL;
1875*4882a593Smuzhiyun 
1876*4882a593Smuzhiyun 	sta = rtw_phl_get_stainfo_by_addr_ex(phl, addr);
1877*4882a593Smuzhiyun 	if (sta)
1878*4882a593Smuzhiyun 		return sta->macid;
1879*4882a593Smuzhiyun 	return mc->max_num;
1880*4882a593Smuzhiyun }
1881*4882a593Smuzhiyun 
1882*4882a593Smuzhiyun /**
1883*4882a593Smuzhiyun  * This function is called to create phl_station_info
1884*4882a593Smuzhiyun  * return pointer to rtw_phl_stainfo_t
1885*4882a593Smuzhiyun  * @phl: see phl_info_t
1886*4882a593Smuzhiyun  * @roleidx: index of wifi role(linux) port nubmer(windows)
1887*4882a593Smuzhiyun  * @addr: current address of this station
1888*4882a593Smuzhiyun  */
1889*4882a593Smuzhiyun struct rtw_phl_stainfo_t *
rtw_phl_get_stainfo_by_addr(void * phl,struct rtw_wifi_role_t * wrole,u8 * addr)1890*4882a593Smuzhiyun rtw_phl_get_stainfo_by_addr(void *phl, struct rtw_wifi_role_t *wrole, u8 *addr)
1891*4882a593Smuzhiyun {
1892*4882a593Smuzhiyun 	struct phl_info_t *phl_info = (struct phl_info_t *)phl;
1893*4882a593Smuzhiyun 	struct macid_ctl_t *macid_ctl = phl_to_mac_ctrl(phl_info);
1894*4882a593Smuzhiyun 	struct rtw_phl_stainfo_t *sta = NULL;
1895*4882a593Smuzhiyun 
1896*4882a593Smuzhiyun 	if (is_broadcast_mac_addr(addr)) {
1897*4882a593Smuzhiyun 		u16 macid = macid_ctl->wrole_bmc[wrole->id];
1898*4882a593Smuzhiyun 
1899*4882a593Smuzhiyun 		if (macid >= macid_ctl->max_num)
1900*4882a593Smuzhiyun 			sta = NULL;
1901*4882a593Smuzhiyun 		else
1902*4882a593Smuzhiyun 			sta = macid_ctl->sta[macid];
1903*4882a593Smuzhiyun 		goto _exit;
1904*4882a593Smuzhiyun 	}
1905*4882a593Smuzhiyun 
1906*4882a593Smuzhiyun 	sta = phl_stainfo_queue_search(phl_info,
1907*4882a593Smuzhiyun 			 &wrole->assoc_sta_queue, addr);
1908*4882a593Smuzhiyun _exit:
1909*4882a593Smuzhiyun 	return sta;
1910*4882a593Smuzhiyun }
1911*4882a593Smuzhiyun 
1912*4882a593Smuzhiyun struct rtw_phl_stainfo_t *
rtw_phl_get_stainfo_self(void * phl,struct rtw_wifi_role_t * wrole)1913*4882a593Smuzhiyun rtw_phl_get_stainfo_self(void *phl, struct rtw_wifi_role_t *wrole)
1914*4882a593Smuzhiyun {
1915*4882a593Smuzhiyun 	struct phl_info_t *phl_info = (struct phl_info_t *)phl;
1916*4882a593Smuzhiyun 	struct rtw_phl_stainfo_t *sta = NULL;
1917*4882a593Smuzhiyun 
1918*4882a593Smuzhiyun 	#if 0
1919*4882a593Smuzhiyun 	if ((wrole->type == PHL_RTYPE_STATION) &&
1920*4882a593Smuzhiyun 		(wrole->mstate == MLME_LINKED))
1921*4882a593Smuzhiyun 			//????
1922*4882a593Smuzhiyun 		else
1923*4882a593Smuzhiyun 			sta = phl_stainfo_queue_search(phl_info,
1924*4882a593Smuzhiyun 				&wrole->assoc_sta_queue, wrole->mac_addr);
1925*4882a593Smuzhiyun 	}
1926*4882a593Smuzhiyun 	#else
1927*4882a593Smuzhiyun 	sta = phl_stainfo_queue_get_first(phl_info, &wrole->assoc_sta_queue);
1928*4882a593Smuzhiyun 	if (sta == NULL)
1929*4882a593Smuzhiyun 		PHL_ERR("%s sta == NULL\n", __func__);
1930*4882a593Smuzhiyun 	#endif
1931*4882a593Smuzhiyun 	return sta;
1932*4882a593Smuzhiyun }
1933*4882a593Smuzhiyun 
1934*4882a593Smuzhiyun u8
rtw_phl_get_sta_rssi(struct rtw_phl_stainfo_t * sta)1935*4882a593Smuzhiyun rtw_phl_get_sta_rssi(struct rtw_phl_stainfo_t *sta)
1936*4882a593Smuzhiyun {
1937*4882a593Smuzhiyun 	u8 rssi = rtw_hal_get_sta_rssi(sta);
1938*4882a593Smuzhiyun 
1939*4882a593Smuzhiyun 	return rssi;
1940*4882a593Smuzhiyun }
1941*4882a593Smuzhiyun 
phl_get_min_rssi_bcn(struct phl_info_t * phl_info)1942*4882a593Smuzhiyun u8 phl_get_min_rssi_bcn(struct phl_info_t *phl_info)
1943*4882a593Smuzhiyun {
1944*4882a593Smuzhiyun 	struct macid_ctl_t *macid_ctl = phl_to_mac_ctrl(phl_info);
1945*4882a593Smuzhiyun 	struct rtw_phl_stainfo_t *sta = NULL;
1946*4882a593Smuzhiyun 	u8 rssi_bcn_min = 0xFF;
1947*4882a593Smuzhiyun 	u16 i = 0;
1948*4882a593Smuzhiyun 	u8 rssi = 0;
1949*4882a593Smuzhiyun 
1950*4882a593Smuzhiyun 	for (i = 0; i < macid_ctl->max_num; i++) {
1951*4882a593Smuzhiyun 		if (!_phl_macid_is_used(macid_ctl->used_map, i))
1952*4882a593Smuzhiyun 			continue;
1953*4882a593Smuzhiyun 
1954*4882a593Smuzhiyun 		sta = rtw_phl_get_stainfo_by_macid(phl_info, i);
1955*4882a593Smuzhiyun 
1956*4882a593Smuzhiyun 		if (NULL == sta)
1957*4882a593Smuzhiyun 			continue;
1958*4882a593Smuzhiyun 
1959*4882a593Smuzhiyun 		rssi = rtw_hal_get_sta_rssi_bcn(sta);
1960*4882a593Smuzhiyun 
1961*4882a593Smuzhiyun 		PHL_DBG("%s macid(%d) with rssi_bcn = %d\n",
1962*4882a593Smuzhiyun 			__func__, i, rssi);
1963*4882a593Smuzhiyun 
1964*4882a593Smuzhiyun 		if (rssi == 0)
1965*4882a593Smuzhiyun 			continue;
1966*4882a593Smuzhiyun 
1967*4882a593Smuzhiyun 		rssi_bcn_min = MIN(rssi, rssi_bcn_min);
1968*4882a593Smuzhiyun 	}
1969*4882a593Smuzhiyun 
1970*4882a593Smuzhiyun 	return rssi_bcn_min;
1971*4882a593Smuzhiyun }
1972*4882a593Smuzhiyun 
1973*4882a593Smuzhiyun 
1974*4882a593Smuzhiyun enum rtw_phl_status
rtw_phl_query_rainfo(void * phl,struct rtw_phl_stainfo_t * phl_sta,struct rtw_phl_rainfo * ra_info)1975*4882a593Smuzhiyun rtw_phl_query_rainfo(void *phl, struct rtw_phl_stainfo_t *phl_sta,
1976*4882a593Smuzhiyun 		     struct rtw_phl_rainfo *ra_info)
1977*4882a593Smuzhiyun {
1978*4882a593Smuzhiyun 	struct phl_info_t *phl_info = (struct phl_info_t *)phl;
1979*4882a593Smuzhiyun 	enum rtw_phl_status phl_sts = RTW_PHL_STATUS_FAILURE;
1980*4882a593Smuzhiyun 
1981*4882a593Smuzhiyun 	do {
1982*4882a593Smuzhiyun 		if (NULL == phl_sta) {
1983*4882a593Smuzhiyun 			PHL_TRACE(COMP_PHL_XMIT, _PHL_ERR_,
1984*4882a593Smuzhiyun 				  "%s : phl_sta is NULL\n",
1985*4882a593Smuzhiyun 				  __func__);
1986*4882a593Smuzhiyun 			break;
1987*4882a593Smuzhiyun 		}
1988*4882a593Smuzhiyun 
1989*4882a593Smuzhiyun 		if (NULL == ra_info) {
1990*4882a593Smuzhiyun 			PHL_TRACE(COMP_PHL_XMIT, _PHL_ERR_,
1991*4882a593Smuzhiyun 				  "%s : Input parameter is NULL\n",
1992*4882a593Smuzhiyun 				  __func__);
1993*4882a593Smuzhiyun 			break;
1994*4882a593Smuzhiyun 		}
1995*4882a593Smuzhiyun 
1996*4882a593Smuzhiyun 		if (RTW_HAL_STATUS_SUCCESS ==
1997*4882a593Smuzhiyun 		    rtw_hal_query_rainfo(phl_info->hal, phl_sta->hal_sta,
1998*4882a593Smuzhiyun 					 ra_info)) {
1999*4882a593Smuzhiyun 			phl_sts = RTW_PHL_STATUS_SUCCESS;
2000*4882a593Smuzhiyun 			break;
2001*4882a593Smuzhiyun 		} else {
2002*4882a593Smuzhiyun 			break;
2003*4882a593Smuzhiyun 		}
2004*4882a593Smuzhiyun 	} while (false);
2005*4882a593Smuzhiyun 
2006*4882a593Smuzhiyun 	return phl_sts;
2007*4882a593Smuzhiyun }
2008*4882a593Smuzhiyun 
2009*4882a593Smuzhiyun /**
2010*4882a593Smuzhiyun  * rtw_phl_txsts_rpt_config() - issue h2c for txok and tx retry info
2011*4882a593Smuzhiyun  * @phl:		struct phl_info_t *
2012*4882a593Smuzhiyun  * @phl_sta:		indicate the first macid that you want to query.
2013*4882a593Smuzhiyun  * Return rtw_phl_txsts_rpt_config's return value in enum rtw_phl_status type.
2014*4882a593Smuzhiyun  */
2015*4882a593Smuzhiyun enum rtw_phl_status
rtw_phl_txsts_rpt_config(void * phl,struct rtw_phl_stainfo_t * phl_sta)2016*4882a593Smuzhiyun rtw_phl_txsts_rpt_config(void *phl, struct rtw_phl_stainfo_t *phl_sta)
2017*4882a593Smuzhiyun {
2018*4882a593Smuzhiyun 	struct phl_info_t *phl_info = (struct phl_info_t *)phl;
2019*4882a593Smuzhiyun 	enum rtw_phl_status phl_sts = RTW_PHL_STATUS_FAILURE;
2020*4882a593Smuzhiyun 
2021*4882a593Smuzhiyun 	if (phl_sta) {
2022*4882a593Smuzhiyun 		if (RTW_HAL_STATUS_SUCCESS == rtw_hal_query_txsts_rpt(phl_info->hal, phl_sta->macid))
2023*4882a593Smuzhiyun 			phl_sts = RTW_PHL_STATUS_SUCCESS;
2024*4882a593Smuzhiyun 	}
2025*4882a593Smuzhiyun 	return phl_sts;
2026*4882a593Smuzhiyun }
2027*4882a593Smuzhiyun 
2028*4882a593Smuzhiyun #ifdef CONFIG_USB_HCI
2029*4882a593Smuzhiyun /**
2030*4882a593Smuzhiyun  * rtw_phl_get_tx_ok_rpt() - get txok info.
2031*4882a593Smuzhiyun  * @phl:		struct phl_info_t *
2032*4882a593Smuzhiyun  * @phl_sta:		information is updated through phl_station_info.
2033*4882a593Smuzhiyun  * @tx_ok_cnt:		buffer address that we used to store tx ok statistics.
2034*4882a593Smuzhiyun  * @qsel			indicate which AC queue, or fetch all by PHL_AC_QUEUE_TOTAL
2035*4882a593Smuzhiyun  *
2036*4882a593Smuzhiyun  * Return rtw_phl_get_tx_ok_rpt's return value in enum rtw_phl_status type.
2037*4882a593Smuzhiyun  */
2038*4882a593Smuzhiyun enum rtw_phl_status
rtw_phl_get_tx_ok_rpt(void * phl,struct rtw_phl_stainfo_t * phl_sta,u32 * tx_ok_cnt,enum phl_ac_queue qsel)2039*4882a593Smuzhiyun rtw_phl_get_tx_ok_rpt(void *phl, struct rtw_phl_stainfo_t *phl_sta, u32 *tx_ok_cnt,
2040*4882a593Smuzhiyun  enum phl_ac_queue qsel)
2041*4882a593Smuzhiyun {
2042*4882a593Smuzhiyun 	struct phl_info_t *phl_info = (struct phl_info_t *)phl;
2043*4882a593Smuzhiyun 	enum rtw_phl_status phl_sts = RTW_PHL_STATUS_SUCCESS;
2044*4882a593Smuzhiyun 	struct rtw_hal_stainfo_t *hal_sta;
2045*4882a593Smuzhiyun 
2046*4882a593Smuzhiyun 	if(phl_sta) {
2047*4882a593Smuzhiyun 		hal_sta = phl_sta->hal_sta;
2048*4882a593Smuzhiyun 
2049*4882a593Smuzhiyun 		if (tx_ok_cnt && qsel <= PHL_AC_QUEUE_TOTAL) {
2050*4882a593Smuzhiyun 			if (qsel == PHL_AC_QUEUE_TOTAL) {
2051*4882a593Smuzhiyun 				/* copy all AC counter */
2052*4882a593Smuzhiyun 				tx_ok_cnt[PHL_BE_QUEUE_SEL] = hal_sta->trx_stat.wp_rpt_stats[PHL_BE_QUEUE_SEL].tx_ok_cnt;
2053*4882a593Smuzhiyun 				tx_ok_cnt[PHL_BK_QUEUE_SEL] = hal_sta->trx_stat.wp_rpt_stats[PHL_BK_QUEUE_SEL].tx_ok_cnt;
2054*4882a593Smuzhiyun 				tx_ok_cnt[PHL_VI_QUEUE_SEL] = hal_sta->trx_stat.wp_rpt_stats[PHL_VI_QUEUE_SEL].tx_ok_cnt;
2055*4882a593Smuzhiyun 				tx_ok_cnt[PHL_VO_QUEUE_SEL] = hal_sta->trx_stat.wp_rpt_stats[PHL_VO_QUEUE_SEL].tx_ok_cnt;
2056*4882a593Smuzhiyun 
2057*4882a593Smuzhiyun 				/* reset all counter */
2058*4882a593Smuzhiyun 				_os_spinlock(phl_to_drvpriv(phl_info), &hal_sta->trx_stat.tx_sts_lock, _bh, NULL);
2059*4882a593Smuzhiyun 				hal_sta->trx_stat.wp_rpt_stats[PHL_BE_QUEUE_SEL].tx_ok_cnt = 0;
2060*4882a593Smuzhiyun 				hal_sta->trx_stat.wp_rpt_stats[PHL_BK_QUEUE_SEL].tx_ok_cnt = 0;
2061*4882a593Smuzhiyun 				hal_sta->trx_stat.wp_rpt_stats[PHL_VI_QUEUE_SEL].tx_ok_cnt = 0;
2062*4882a593Smuzhiyun 				hal_sta->trx_stat.wp_rpt_stats[PHL_VO_QUEUE_SEL].tx_ok_cnt = 0;
2063*4882a593Smuzhiyun 				_os_spinunlock(phl_to_drvpriv(phl_info), &hal_sta->trx_stat.tx_sts_lock, _bh, NULL);
2064*4882a593Smuzhiyun 			} else {
2065*4882a593Smuzhiyun 				/*copy target AC queue counter*/
2066*4882a593Smuzhiyun 				*tx_ok_cnt = hal_sta->trx_stat.wp_rpt_stats[qsel].tx_ok_cnt;
2067*4882a593Smuzhiyun 				/* reset target AC queue counter */
2068*4882a593Smuzhiyun 				_os_spinlock(phl_to_drvpriv(phl_info), &hal_sta->trx_stat.tx_sts_lock, _bh, NULL);
2069*4882a593Smuzhiyun 				hal_sta->trx_stat.wp_rpt_stats[qsel].tx_ok_cnt = 0;
2070*4882a593Smuzhiyun 				_os_spinunlock(phl_to_drvpriv(phl_info), &hal_sta->trx_stat.tx_sts_lock, _bh, NULL);
2071*4882a593Smuzhiyun 			}
2072*4882a593Smuzhiyun 		} else {
2073*4882a593Smuzhiyun 			phl_sts = RTW_PHL_STATUS_FAILURE;
2074*4882a593Smuzhiyun 			PHL_ERR("tx_ok_cnt = %p, qsel = %d\n", tx_ok_cnt, qsel);
2075*4882a593Smuzhiyun 		}
2076*4882a593Smuzhiyun 
2077*4882a593Smuzhiyun 	} else {
2078*4882a593Smuzhiyun 		phl_sts = RTW_PHL_STATUS_FAILURE;
2079*4882a593Smuzhiyun 		PHL_ERR("PHL STA NULL.\n");
2080*4882a593Smuzhiyun 	}
2081*4882a593Smuzhiyun 	return phl_sts;
2082*4882a593Smuzhiyun }
2083*4882a593Smuzhiyun 
rtw_phl_get_hw_tx_fail_cnt(struct rtw_hal_stainfo_t * hal_sta,enum phl_ac_queue qsel)2084*4882a593Smuzhiyun static u32 rtw_phl_get_hw_tx_fail_cnt(struct rtw_hal_stainfo_t *hal_sta,
2085*4882a593Smuzhiyun 	enum phl_ac_queue qsel) {
2086*4882a593Smuzhiyun 	u32 total = 0;
2087*4882a593Smuzhiyun 
2088*4882a593Smuzhiyun 	if (hal_sta) {
2089*4882a593Smuzhiyun 		total = hal_sta->trx_stat.wp_rpt_stats[qsel].rty_fail_cnt\
2090*4882a593Smuzhiyun 				+ hal_sta->trx_stat.wp_rpt_stats[qsel].lifetime_drop_cnt \
2091*4882a593Smuzhiyun 				+ hal_sta->trx_stat.wp_rpt_stats[qsel].macid_drop_cnt;
2092*4882a593Smuzhiyun 	}
2093*4882a593Smuzhiyun 
2094*4882a593Smuzhiyun 	return total;
2095*4882a593Smuzhiyun }
2096*4882a593Smuzhiyun 
rtw_phl_reset_tx_fail_cnt(struct phl_info_t * phl_info,struct rtw_hal_stainfo_t * hal_sta,enum phl_ac_queue qsel)2097*4882a593Smuzhiyun static void rtw_phl_reset_tx_fail_cnt(struct phl_info_t *phl_info,
2098*4882a593Smuzhiyun 	struct rtw_hal_stainfo_t *hal_sta, enum phl_ac_queue qsel) {
2099*4882a593Smuzhiyun 
2100*4882a593Smuzhiyun 	if (hal_sta) {
2101*4882a593Smuzhiyun 		_os_spinlock(phl_to_drvpriv(phl_info), &hal_sta->trx_stat.tx_sts_lock, _bh, NULL);
2102*4882a593Smuzhiyun 		hal_sta->trx_stat.wp_rpt_stats[qsel].rty_fail_cnt = 0;
2103*4882a593Smuzhiyun 		hal_sta->trx_stat.wp_rpt_stats[qsel].lifetime_drop_cnt = 0;
2104*4882a593Smuzhiyun 		hal_sta->trx_stat.wp_rpt_stats[qsel].macid_drop_cnt = 0;
2105*4882a593Smuzhiyun 		_os_spinunlock(phl_to_drvpriv(phl_info), &hal_sta->trx_stat.tx_sts_lock, _bh, NULL);
2106*4882a593Smuzhiyun 	}
2107*4882a593Smuzhiyun }
2108*4882a593Smuzhiyun 
2109*4882a593Smuzhiyun /**
2110*4882a593Smuzhiyun  * rtw_phl_get_tx_fail_rpt() - get tx fail info.
2111*4882a593Smuzhiyun  * @phl:		struct phl_info_t *
2112*4882a593Smuzhiyun  * @phl_sta:		information is updated through phl_station_info.
2113*4882a593Smuzhiyun  * @tx_fail_cnt:	buffer address that we used to store tx fail statistics.
2114*4882a593Smuzhiyun  * @qsel			indicate which AC queue, or fetch all by PHL_AC_QUEUE_TOTAL
2115*4882a593Smuzhiyun  *
2116*4882a593Smuzhiyun  * Return rtw_phl_get_tx_fail_rpt's return value in enum rtw_phl_status type.
2117*4882a593Smuzhiyun  */
2118*4882a593Smuzhiyun enum rtw_phl_status
rtw_phl_get_tx_fail_rpt(void * phl,struct rtw_phl_stainfo_t * phl_sta,u32 * tx_fail_cnt,enum phl_ac_queue qsel)2119*4882a593Smuzhiyun rtw_phl_get_tx_fail_rpt(void *phl, struct rtw_phl_stainfo_t *phl_sta, u32 *tx_fail_cnt,
2120*4882a593Smuzhiyun  enum phl_ac_queue qsel)
2121*4882a593Smuzhiyun {
2122*4882a593Smuzhiyun 	struct phl_info_t *phl_info = (struct phl_info_t *)phl;
2123*4882a593Smuzhiyun 	enum rtw_phl_status phl_sts = RTW_PHL_STATUS_SUCCESS;
2124*4882a593Smuzhiyun 	struct rtw_hal_stainfo_t *hal_sta;
2125*4882a593Smuzhiyun 
2126*4882a593Smuzhiyun 	if(phl_sta) {
2127*4882a593Smuzhiyun 		hal_sta = phl_sta->hal_sta;
2128*4882a593Smuzhiyun 
2129*4882a593Smuzhiyun 		if (tx_fail_cnt && qsel <= PHL_AC_QUEUE_TOTAL) {
2130*4882a593Smuzhiyun 			if (qsel == PHL_AC_QUEUE_TOTAL) {
2131*4882a593Smuzhiyun 				/* copy all AC counter */
2132*4882a593Smuzhiyun 				tx_fail_cnt[PHL_BE_QUEUE_SEL] = rtw_phl_get_hw_tx_fail_cnt(hal_sta, PHL_BE_QUEUE_SEL);
2133*4882a593Smuzhiyun 				tx_fail_cnt[PHL_BK_QUEUE_SEL] = rtw_phl_get_hw_tx_fail_cnt(hal_sta, PHL_BK_QUEUE_SEL);
2134*4882a593Smuzhiyun 				tx_fail_cnt[PHL_VI_QUEUE_SEL] = rtw_phl_get_hw_tx_fail_cnt(hal_sta, PHL_VI_QUEUE_SEL);
2135*4882a593Smuzhiyun 				tx_fail_cnt[PHL_VO_QUEUE_SEL] = rtw_phl_get_hw_tx_fail_cnt(hal_sta, PHL_VO_QUEUE_SEL);
2136*4882a593Smuzhiyun 				/* reset all counter */
2137*4882a593Smuzhiyun 				rtw_phl_reset_tx_fail_cnt(phl_info, hal_sta, PHL_BE_QUEUE_SEL);
2138*4882a593Smuzhiyun 				rtw_phl_reset_tx_fail_cnt(phl_info, hal_sta, PHL_BK_QUEUE_SEL);
2139*4882a593Smuzhiyun 				rtw_phl_reset_tx_fail_cnt(phl_info, hal_sta, PHL_VI_QUEUE_SEL);
2140*4882a593Smuzhiyun 				rtw_phl_reset_tx_fail_cnt(phl_info, hal_sta, PHL_VO_QUEUE_SEL);
2141*4882a593Smuzhiyun 			} else {
2142*4882a593Smuzhiyun 				/*copy target AC queue counter*/
2143*4882a593Smuzhiyun 				tx_fail_cnt[qsel] = rtw_phl_get_hw_tx_fail_cnt(hal_sta, qsel);
2144*4882a593Smuzhiyun 				/* reset target AC queue counter */
2145*4882a593Smuzhiyun 				rtw_phl_reset_tx_fail_cnt(phl_info, hal_sta, qsel);
2146*4882a593Smuzhiyun 			}
2147*4882a593Smuzhiyun 		} else {
2148*4882a593Smuzhiyun 			phl_sts = RTW_PHL_STATUS_FAILURE;
2149*4882a593Smuzhiyun 			PHL_ERR("tx_fail_cnt = %p, qsel = %d\n", tx_fail_cnt, qsel);
2150*4882a593Smuzhiyun 		}
2151*4882a593Smuzhiyun 	} else {
2152*4882a593Smuzhiyun 		phl_sts = RTW_PHL_STATUS_FAILURE;
2153*4882a593Smuzhiyun 		PHL_ERR("PHL STA NULL.\n");
2154*4882a593Smuzhiyun 	}
2155*4882a593Smuzhiyun 	return phl_sts;
2156*4882a593Smuzhiyun }
2157*4882a593Smuzhiyun 
2158*4882a593Smuzhiyun /**
2159*4882a593Smuzhiyun  * rtw_phl_get_tx_retry_rpt() - get tx retry info.
2160*4882a593Smuzhiyun  * @phl:		struct phl_info_t *
2161*4882a593Smuzhiyun  * @phl_sta:		information is updated through phl_station_info.
2162*4882a593Smuzhiyun  * @tx_retry_cnt:	buffer address that we used to store tx fail statistics.
2163*4882a593Smuzhiyun  * @qsel			indicate which AC queue, or fetch all by PHL_AC_QUEUE_TOTAL
2164*4882a593Smuzhiyun  *
2165*4882a593Smuzhiyun  * Return rtw_phl_get_tx_retry_rpt's return value in enum rtw_phl_status type.
2166*4882a593Smuzhiyun  */
2167*4882a593Smuzhiyun enum rtw_phl_status
rtw_phl_get_tx_retry_rpt(void * phl,struct rtw_phl_stainfo_t * phl_sta,u32 * tx_retry_cnt,enum phl_ac_queue qsel)2168*4882a593Smuzhiyun rtw_phl_get_tx_retry_rpt(void *phl, struct rtw_phl_stainfo_t *phl_sta, u32 *tx_retry_cnt,
2169*4882a593Smuzhiyun  enum phl_ac_queue qsel)
2170*4882a593Smuzhiyun {
2171*4882a593Smuzhiyun 	struct phl_info_t *phl_info = (struct phl_info_t *)phl;
2172*4882a593Smuzhiyun 	void *drv = phl_to_drvpriv(phl_info);
2173*4882a593Smuzhiyun 	enum rtw_phl_status phl_sts = RTW_PHL_STATUS_SUCCESS;
2174*4882a593Smuzhiyun 	struct rtw_hal_stainfo_t *hal_sta;
2175*4882a593Smuzhiyun 
2176*4882a593Smuzhiyun 	if(phl_sta) {
2177*4882a593Smuzhiyun 		hal_sta = phl_sta->hal_sta;
2178*4882a593Smuzhiyun 
2179*4882a593Smuzhiyun 		if (tx_retry_cnt && qsel <= PHL_AC_QUEUE_TOTAL) {
2180*4882a593Smuzhiyun 			if (qsel == PHL_AC_QUEUE_TOTAL) {
2181*4882a593Smuzhiyun 				/* copy all AC counter */
2182*4882a593Smuzhiyun 				_os_mem_cpy(drv, tx_retry_cnt, hal_sta->ra_info.tx_retry_cnt,
2183*4882a593Smuzhiyun 					sizeof(u32)*PHL_AC_QUEUE_TOTAL);
2184*4882a593Smuzhiyun 				/* reset all counter */
2185*4882a593Smuzhiyun 				/* TODO: Here needs lock, and so does halbb_get_txsts_rpt */
2186*4882a593Smuzhiyun 				_os_mem_set(drv, hal_sta->ra_info.tx_retry_cnt, 0, sizeof(u32)*PHL_AC_QUEUE_TOTAL);
2187*4882a593Smuzhiyun 			} else {
2188*4882a593Smuzhiyun 				/*copy target AC queue counter*/
2189*4882a593Smuzhiyun 				*tx_retry_cnt = hal_sta->ra_info.tx_retry_cnt[qsel];
2190*4882a593Smuzhiyun 				/* reset target AC queue counter */
2191*4882a593Smuzhiyun 				/* TODO: Here needs lock, and so does halbb_get_txsts_rpt */
2192*4882a593Smuzhiyun 				hal_sta->ra_info.tx_retry_cnt[qsel] = 0;
2193*4882a593Smuzhiyun 			}
2194*4882a593Smuzhiyun 		} else {
2195*4882a593Smuzhiyun 			phl_sts = RTW_PHL_STATUS_FAILURE;
2196*4882a593Smuzhiyun 			PHL_ERR("tx_retry_cnt = %p, qsel = %d\n", tx_retry_cnt, qsel);
2197*4882a593Smuzhiyun 		}
2198*4882a593Smuzhiyun 	} else {
2199*4882a593Smuzhiyun 		phl_sts = RTW_PHL_STATUS_FAILURE;
2200*4882a593Smuzhiyun 		PHL_ERR("PHL STA NULL.\n");
2201*4882a593Smuzhiyun 	}
2202*4882a593Smuzhiyun 	return phl_sts;
2203*4882a593Smuzhiyun }
2204*4882a593Smuzhiyun #endif /* CONFIG_USB_HCI */
2205*4882a593Smuzhiyun 
2206*4882a593Smuzhiyun /*
2207*4882a593Smuzhiyun  * Get next idx
2208*4882a593Smuzhiyun  */
_get_fidx(u8 num,u8 cur_idx)2209*4882a593Smuzhiyun u8 _get_fidx(u8 num, u8 cur_idx)
2210*4882a593Smuzhiyun {
2211*4882a593Smuzhiyun 	u8 idx = 0;
2212*4882a593Smuzhiyun 
2213*4882a593Smuzhiyun 	if (num == 0)
2214*4882a593Smuzhiyun 		idx = cur_idx;
2215*4882a593Smuzhiyun 	else {
2216*4882a593Smuzhiyun 		idx = cur_idx + 1;
2217*4882a593Smuzhiyun 		if (idx >= MAX_STORE_BCN_NUM)
2218*4882a593Smuzhiyun 			idx = 0;
2219*4882a593Smuzhiyun 	}
2220*4882a593Smuzhiyun 	return idx;
2221*4882a593Smuzhiyun }
2222*4882a593Smuzhiyun 
2223*4882a593Smuzhiyun /*
2224*4882a593Smuzhiyun  * Get previous idx
2225*4882a593Smuzhiyun  */
_get_bidx(u8 num,u8 cur_idx)2226*4882a593Smuzhiyun u8 _get_bidx(u8 num, u8 cur_idx)
2227*4882a593Smuzhiyun {
2228*4882a593Smuzhiyun 	u8 idx = 0;
2229*4882a593Smuzhiyun 
2230*4882a593Smuzhiyun 	if (cur_idx == 0) {
2231*4882a593Smuzhiyun 		idx = num - 1;
2232*4882a593Smuzhiyun 	} else {
2233*4882a593Smuzhiyun 		idx = cur_idx - 1;
2234*4882a593Smuzhiyun 	}
2235*4882a593Smuzhiyun 	return idx;
2236*4882a593Smuzhiyun }
2237*4882a593Smuzhiyun 
_phl_sta_up_bcn_offset_info(struct phl_info_t * phl,struct rtw_rx_bcn_info * bcn_i,u16 bcn_intvl)2238*4882a593Smuzhiyun void _phl_sta_up_bcn_offset_info(struct phl_info_t *phl,
2239*4882a593Smuzhiyun 			struct rtw_rx_bcn_info *bcn_i, u16 bcn_intvl)
2240*4882a593Smuzhiyun {
2241*4882a593Smuzhiyun 	struct rtw_bcn_offset *offset_i = &bcn_i->offset_i;
2242*4882a593Smuzhiyun 	u16 offset = bcn_intvl;
2243*4882a593Smuzhiyun 	u16 similar_th = 2;/*Unit: TU*/
2244*4882a593Smuzhiyun 	u64 diff = 0;
2245*4882a593Smuzhiyun 	u8 idx = 0, jdx = 0, cur_idx = 0, bidx = 0, start_idx = 0;
2246*4882a593Smuzhiyun 
2247*4882a593Smuzhiyun 	if (bcn_i->num == 1) {
2248*4882a593Smuzhiyun 		offset_i->offset = (u16)bcn_i->info[1][bcn_i->idx];
2249*4882a593Smuzhiyun 		offset_i->conf_lvl = CONF_LVL_LOW;
2250*4882a593Smuzhiyun 		PHL_TRACE(COMP_PHL_DBG, _PHL_WARNING_, "_phl_sta_up_bcn_offset_info(): bcn_i->num ==1, conf_lvl = CONF_LVL_LOW, offset(%d)\n",
2251*4882a593Smuzhiyun 			offset_i->offset);
2252*4882a593Smuzhiyun 		goto exit;
2253*4882a593Smuzhiyun 	}
2254*4882a593Smuzhiyun 	cur_idx = bcn_i->idx;
2255*4882a593Smuzhiyun 	start_idx = cur_idx;
2256*4882a593Smuzhiyun 	for (idx = 0; idx < bcn_i->num; idx++) {
2257*4882a593Smuzhiyun 		bidx = cur_idx;
2258*4882a593Smuzhiyun 		for (jdx = 1; jdx < bcn_i->num; jdx++) {
2259*4882a593Smuzhiyun 			bidx = _get_bidx(bcn_i->num, bidx);
2260*4882a593Smuzhiyun 			if (start_idx == bidx)
2261*4882a593Smuzhiyun 				break;
2262*4882a593Smuzhiyun 			diff = bcn_i->info[0][cur_idx] - bcn_i->info[0][bidx];
2263*4882a593Smuzhiyun 			diff = _os_division64(
2264*4882a593Smuzhiyun 					_os_modular64(diff, bcn_intvl * TU), TU);
2265*4882a593Smuzhiyun 			/*ex: diff = 99, BcnIntvl = 100, It's similar case
2266*4882a593Smuzhiyun 			 * diff = 2, BcnIntvl = 100, It's similar case
2267*4882a593Smuzhiyun 			 */
2268*4882a593Smuzhiyun 			if (!((diff < similar_th) ||
2269*4882a593Smuzhiyun 				((bcn_intvl - diff) < similar_th))) {
2270*4882a593Smuzhiyun 					continue;
2271*4882a593Smuzhiyun 			}
2272*4882a593Smuzhiyun 			if (offset > bcn_i->info[1][cur_idx])
2273*4882a593Smuzhiyun 				offset = (u16)bcn_i->info[1][cur_idx];
2274*4882a593Smuzhiyun 			if (offset > bcn_i->info[1][bidx])
2275*4882a593Smuzhiyun 				offset = (u16)bcn_i->info[1][bidx];
2276*4882a593Smuzhiyun 		}
2277*4882a593Smuzhiyun 		cur_idx = _get_bidx(bcn_i->num, cur_idx);
2278*4882a593Smuzhiyun 	}
2279*4882a593Smuzhiyun 	if (offset != bcn_intvl) {
2280*4882a593Smuzhiyun 		offset_i->conf_lvl = CONF_LVL_HIGH;
2281*4882a593Smuzhiyun 		if (offset < offset_i->offset) {
2282*4882a593Smuzhiyun 			offset_i->offset = offset;
2283*4882a593Smuzhiyun 		}
2284*4882a593Smuzhiyun 		goto exit;
2285*4882a593Smuzhiyun 	}
2286*4882a593Smuzhiyun 	for (idx = 0; idx < bcn_i->num; idx++) {
2287*4882a593Smuzhiyun 		if (bcn_i->info[1][idx] < offset_i->offset) {
2288*4882a593Smuzhiyun 			offset_i->offset = (u16)bcn_i->info[1][idx];
2289*4882a593Smuzhiyun 			offset_i->conf_lvl = CONF_LVL_MID;
2290*4882a593Smuzhiyun 		}
2291*4882a593Smuzhiyun 	}
2292*4882a593Smuzhiyun exit:
2293*4882a593Smuzhiyun 	/*
2294*4882a593Smuzhiyun 	   if offset is small, maybe impact by environment, offset < 5% bcn_intvl, we consider offset is 0
2295*4882a593Smuzhiyun 	*/
2296*4882a593Smuzhiyun 	if ((offset_i->offset != 0) &&
2297*4882a593Smuzhiyun 		(offset_i->offset < ((bcn_intvl * 5) / 100))) {
2298*4882a593Smuzhiyun 		PHL_TRACE(COMP_PHL_MCC, _PHL_WARNING_, "_phl_sta_up_bcn_offset_info(): offset(%d) < (%d), set offset = 0\n",
2299*4882a593Smuzhiyun 			offset_i->offset, (bcn_intvl * 5) / 100);
2300*4882a593Smuzhiyun 		offset_i->offset = 0;
2301*4882a593Smuzhiyun 	}
2302*4882a593Smuzhiyun 	PHL_TRACE(COMP_PHL_DBG, _PHL_DEBUG_, "_phl_sta_up_bcn_offset_info(): bcn num(%d), offset(%d), conf_lvl(%d), current CR(%d)\n",
2303*4882a593Smuzhiyun 		bcn_i->num, offset_i->offset, offset_i->conf_lvl, offset_i->cr_tbtt_shift);
2304*4882a593Smuzhiyun 	return;
2305*4882a593Smuzhiyun }
2306*4882a593Smuzhiyun 
rtw_phl_sta_up_rx_bcn(void * phl,struct rtw_bcn_pkt_info * info)2307*4882a593Smuzhiyun void rtw_phl_sta_up_rx_bcn(void *phl, struct rtw_bcn_pkt_info *info)
2308*4882a593Smuzhiyun {
2309*4882a593Smuzhiyun 	struct rtw_rx_bcn_info *bcn_i = &info->sta->bcn_i;
2310*4882a593Smuzhiyun 	u16 bcn_intvl = info->sta->asoc_cap.bcn_interval;
2311*4882a593Smuzhiyun 
2312*4882a593Smuzhiyun 	if (bcn_intvl == 0) {
2313*4882a593Smuzhiyun 		PHL_TRACE(COMP_PHL_DBG, _PHL_WARNING_, "bcn_intvl == 0");
2314*4882a593Smuzhiyun 		return;
2315*4882a593Smuzhiyun 	}
2316*4882a593Smuzhiyun 
2317*4882a593Smuzhiyun 	bcn_i->idx = _get_fidx(bcn_i->num, bcn_i->idx);
2318*4882a593Smuzhiyun 	if (bcn_i->num < MAX_STORE_BCN_NUM)
2319*4882a593Smuzhiyun 		bcn_i->num++;
2320*4882a593Smuzhiyun 	bcn_i->info[0][bcn_i->idx] = info->tsf;
2321*4882a593Smuzhiyun 	bcn_i->info[1][bcn_i->idx] = _os_division64(
2322*4882a593Smuzhiyun 				_os_modular64(info->tsf, bcn_intvl * TU), TU);
2323*4882a593Smuzhiyun 	bcn_i->info[2][bcn_i->idx] = info->hw_tsf;
2324*4882a593Smuzhiyun 	_phl_sta_up_bcn_offset_info(phl, bcn_i, bcn_intvl);
2325*4882a593Smuzhiyun }
2326*4882a593Smuzhiyun 
phl_clean_sta_bcn_info(struct phl_info_t * phl,struct rtw_phl_stainfo_t * sta)2327*4882a593Smuzhiyun void phl_clean_sta_bcn_info(struct phl_info_t *phl, struct rtw_phl_stainfo_t *sta)
2328*4882a593Smuzhiyun {
2329*4882a593Smuzhiyun 	void *priv = phl_to_drvpriv(phl);
2330*4882a593Smuzhiyun 	struct rtw_rx_bcn_info *bcn_i = &sta->bcn_i;
2331*4882a593Smuzhiyun 
2332*4882a593Smuzhiyun 	PHL_TRACE(COMP_PHL_DBG, _PHL_INFO_, "phl_clean_sta_bcn_info(): sta->wrole->id(%d)\n",
2333*4882a593Smuzhiyun 		sta->wrole->id);
2334*4882a593Smuzhiyun 	_os_mem_set(priv, bcn_i, 0, sizeof(struct rtw_rx_bcn_info));
2335*4882a593Smuzhiyun }
2336*4882a593Smuzhiyun 
phl_get_sta_bcn_offset_info(struct phl_info_t * phl,struct rtw_wifi_role_t * wrole)2337*4882a593Smuzhiyun struct rtw_bcn_offset * phl_get_sta_bcn_offset_info(struct phl_info_t *phl,
2338*4882a593Smuzhiyun 					struct rtw_wifi_role_t *wrole)
2339*4882a593Smuzhiyun {
2340*4882a593Smuzhiyun 	struct rtw_phl_stainfo_t *sta = rtw_phl_get_stainfo_self(phl, wrole);
2341*4882a593Smuzhiyun 	struct rtw_bcn_offset *offset_i = &sta->bcn_i.offset_i;
2342*4882a593Smuzhiyun 
2343*4882a593Smuzhiyun 	return offset_i;
2344*4882a593Smuzhiyun }
2345*4882a593Smuzhiyun 
phl_bcn_watchdog(struct phl_info_t * phl)2346*4882a593Smuzhiyun void phl_bcn_watchdog(struct phl_info_t *phl)
2347*4882a593Smuzhiyun {
2348*4882a593Smuzhiyun 	u8 ridx = MAX_WIFI_ROLE_NUMBER;
2349*4882a593Smuzhiyun 	struct rtw_wifi_role_t *wrole = NULL;
2350*4882a593Smuzhiyun 	struct rtw_bcn_offset *b_ofst_i = NULL;
2351*4882a593Smuzhiyun 	enum rtw_hal_status hstatus = RTW_HAL_STATUS_SUCCESS;
2352*4882a593Smuzhiyun 
2353*4882a593Smuzhiyun 	for (ridx = 0; ridx < MAX_WIFI_ROLE_NUMBER; ridx++) {
2354*4882a593Smuzhiyun 		wrole = rtw_phl_get_wrole_by_ridx(phl->phl_com, ridx);
2355*4882a593Smuzhiyun 		if (wrole == NULL)
2356*4882a593Smuzhiyun 			continue;
2357*4882a593Smuzhiyun 
2358*4882a593Smuzhiyun 		if (rtw_phl_role_is_client_category(wrole) && wrole->mstate == MLME_LINKED) {
2359*4882a593Smuzhiyun 			b_ofst_i = phl_get_sta_bcn_offset_info(phl, wrole);
2360*4882a593Smuzhiyun 
2361*4882a593Smuzhiyun 			if (b_ofst_i->conf_lvl >= CONF_LVL_MID &&
2362*4882a593Smuzhiyun 				b_ofst_i->offset != b_ofst_i->cr_tbtt_shift) {
2363*4882a593Smuzhiyun 				PHL_TRACE(COMP_PHL_DBG, _PHL_INFO_, "%s(): update bcn offset to %d TU\n",
2364*4882a593Smuzhiyun 							__func__, b_ofst_i->offset);
2365*4882a593Smuzhiyun 				hstatus = rtw_hal_role_cfg_ex(phl->hal, wrole, PCFG_TBTT_SHIFT, &(b_ofst_i->offset));
2366*4882a593Smuzhiyun 				if (hstatus == RTW_HAL_STATUS_SUCCESS)
2367*4882a593Smuzhiyun 					b_ofst_i->cr_tbtt_shift = b_ofst_i->offset;
2368*4882a593Smuzhiyun 				else
2369*4882a593Smuzhiyun 					PHL_ERR("%s(): role cfg fail, status: %d\n", __func__, hstatus);
2370*4882a593Smuzhiyun 			}
2371*4882a593Smuzhiyun 		}
2372*4882a593Smuzhiyun 	}
2373*4882a593Smuzhiyun }
2374*4882a593Smuzhiyun 
2375*4882a593Smuzhiyun /*
2376*4882a593Smuzhiyun  * calculate the value between current TSF and TBTT
2377*4882a593Smuzhiyun  * TSF   0       50    180    150              250
2378*4882a593Smuzhiyun  * TBTT          ^                ^                ^
2379*4882a593Smuzhiyun  * Curr T                 |
2380*4882a593Smuzhiyun  *                   | 30 |
2381*4882a593Smuzhiyun  *
2382*4882a593Smuzhiyun  * TSF   0       80     120     180              280
2383*4882a593Smuzhiyun  * TBTT           ^                 ^                 ^
2384*4882a593Smuzhiyun  * Curr T                  |
2385*4882a593Smuzhiyun  *                   | 40  |
2386*4882a593Smuzhiyun  * @wrole: specific role, we get bcn offset info from the role.
2387*4882a593Smuzhiyun  * @cur_t: current TSF
2388*4882a593Smuzhiyun  * @ofst: output value, unit: TU
2389*4882a593Smuzhiyun  */
phl_calc_offset_from_tbtt(struct phl_info_t * phl,struct rtw_wifi_role_t * wrole,u64 cur_t,u16 * ofst)2390*4882a593Smuzhiyun bool phl_calc_offset_from_tbtt(struct phl_info_t *phl,
2391*4882a593Smuzhiyun 			struct rtw_wifi_role_t *wrole, u64 cur_t, u16 *ofst)
2392*4882a593Smuzhiyun {
2393*4882a593Smuzhiyun 	struct rtw_bcn_offset *b_ofst_i = phl_get_sta_bcn_offset_info(phl, wrole);
2394*4882a593Smuzhiyun 	struct rtw_phl_stainfo_t *sta = rtw_phl_get_stainfo_self(phl, wrole);
2395*4882a593Smuzhiyun 	u64 b_ofst = b_ofst_i->offset;
2396*4882a593Smuzhiyun 	u64 b_intvl = 0;
2397*4882a593Smuzhiyun 	u32 mod = 0; /*TU*/
2398*4882a593Smuzhiyun 
2399*4882a593Smuzhiyun #ifdef RTW_PHL_BCN
2400*4882a593Smuzhiyun 	if (phl_role_is_ap_category(wrole))
2401*4882a593Smuzhiyun 		b_intvl = (u16)wrole->bcn_cmn.bcn_interval;
2402*4882a593Smuzhiyun 	else
2403*4882a593Smuzhiyun #endif
2404*4882a593Smuzhiyun 		b_intvl = sta->asoc_cap.bcn_interval;
2405*4882a593Smuzhiyun 	if (0 == b_intvl) {
2406*4882a593Smuzhiyun 		PHL_TRACE(COMP_PHL_DBG, _PHL_ERR_, "phl_calc_offset_from_tbtt(): Fail, b_intvl ==0, wrole->id(%d), type(%d)\n",
2407*4882a593Smuzhiyun 			wrole->id, wrole->type);
2408*4882a593Smuzhiyun 		return false;
2409*4882a593Smuzhiyun 	}
2410*4882a593Smuzhiyun 	mod = (u32)_os_division64(_os_modular64(cur_t, b_intvl * TU), TU);
2411*4882a593Smuzhiyun 	if (mod < b_ofst) {
2412*4882a593Smuzhiyun 		*ofst = (u16)(mod + (b_intvl - b_ofst));
2413*4882a593Smuzhiyun 	} else {
2414*4882a593Smuzhiyun 		*ofst = (u16)(mod - b_ofst);
2415*4882a593Smuzhiyun 	}
2416*4882a593Smuzhiyun 	PHL_TRACE(COMP_PHL_DBG, _PHL_INFO_, "phl_calc_offset_from_tbtt(): wrole->id(%d), ofst(%d), cur_t: 0x%08x %08x modular(%d, TU), Bcn offset: conf_lvl(%d), offset(%d)\n",
2417*4882a593Smuzhiyun 		wrole->id, *ofst, (u32)(cur_t >> 32), (u32)cur_t, mod,
2418*4882a593Smuzhiyun 		b_ofst_i->conf_lvl, (u32)b_ofst);
2419*4882a593Smuzhiyun 	return true;
2420*4882a593Smuzhiyun }
2421*4882a593Smuzhiyun 
2422*4882a593Smuzhiyun /*
2423*4882a593Smuzhiyun  * Synchronize TBTT of target role with TBTT of sourec role
2424*4882a593Smuzhiyun  * Assume TBTT of target role is locate in Mod(Tgt Tsf) = 0
2425*4882a593Smuzhiyun  * @sync_ofst: Offset between TBTT of target role and TBTT of sourec role. Unit: TU
2426*4882a593Smuzhiyun  * @sync_now_once: Sync once time right now.
2427*4882a593Smuzhiyun  * @*diff_t : output diff_tsf. Unit: TU
2428*4882a593Smuzhiyun  */
rtw_phl_tbtt_sync(struct phl_info_t * phl,struct rtw_wifi_role_t * src_role,struct rtw_wifi_role_t * tgt_role,u16 sync_ofst,bool sync_now_once,u16 * diff_t)2429*4882a593Smuzhiyun enum rtw_phl_status rtw_phl_tbtt_sync(struct phl_info_t *phl,
2430*4882a593Smuzhiyun 		struct rtw_wifi_role_t *src_role,
2431*4882a593Smuzhiyun 		struct rtw_wifi_role_t *tgt_role,
2432*4882a593Smuzhiyun 		u16 sync_ofst, bool sync_now_once, u16 *diff_t)
2433*4882a593Smuzhiyun {
2434*4882a593Smuzhiyun 	enum rtw_phl_status status = RTW_PHL_STATUS_FAILURE;
2435*4882a593Smuzhiyun 	u32 tsf_h = 0, tsf_l = 0;
2436*4882a593Smuzhiyun 	u64 tsf = 0, tgt_tsf = 0, bcn_intvl = 0;
2437*4882a593Smuzhiyun 	u16 ofst = 0;
2438*4882a593Smuzhiyun 	u64 diff_tsf = 0;
2439*4882a593Smuzhiyun 	enum hal_tsf_sync_act act = sync_now_once ? HAL_TSF_SYNC_NOW_ONCE :
2440*4882a593Smuzhiyun 							HAL_TSF_EN_SYNC_AUTO;
2441*4882a593Smuzhiyun 
2442*4882a593Smuzhiyun 	if (RTW_HAL_STATUS_SUCCESS != rtw_hal_get_tsf(phl->hal,
2443*4882a593Smuzhiyun 					src_role->hw_port, &tsf_h, &tsf_l)) {
2444*4882a593Smuzhiyun 		PHL_TRACE(COMP_PHL_DBG, _PHL_INFO_, "rtw_phl_tbtt_sync(): Get tsf fail, src_role->id(%d)\n",
2445*4882a593Smuzhiyun 			src_role->id);
2446*4882a593Smuzhiyun 		goto exit;
2447*4882a593Smuzhiyun 	}
2448*4882a593Smuzhiyun 	bcn_intvl = phl_role_get_bcn_intvl(phl, tgt_role);
2449*4882a593Smuzhiyun 	if (bcn_intvl == 0) {
2450*4882a593Smuzhiyun 		PHL_TRACE(COMP_PHL_DBG, _PHL_ERR_, "rtw_phl_tbtt_sync(): bcn_intvl == 0, tgt_role->id(%d)\n",
2451*4882a593Smuzhiyun 			tgt_role->id);
2452*4882a593Smuzhiyun 		goto exit;
2453*4882a593Smuzhiyun 	}
2454*4882a593Smuzhiyun 	tsf = tsf_h;
2455*4882a593Smuzhiyun 	tsf = tsf << 32;
2456*4882a593Smuzhiyun 	tsf |= tsf_l;
2457*4882a593Smuzhiyun 	/*calculate the value between current TSF and TBTT*/
2458*4882a593Smuzhiyun 	phl_calc_offset_from_tbtt(phl, src_role, tsf, &ofst);
2459*4882a593Smuzhiyun 	tgt_tsf = (tsf + sync_ofst * TU) - ofst * TU;
2460*4882a593Smuzhiyun 	/*Find diff_tsf, let Mod((tgt_tsf + diff_tsf), bcn_intvl) = 0*/
2461*4882a593Smuzhiyun 	diff_tsf = bcn_intvl * TU - _os_modular64(tgt_tsf, bcn_intvl * TU);
2462*4882a593Smuzhiyun 	diff_tsf = _os_division64(diff_tsf, TU);
2463*4882a593Smuzhiyun 	PHL_TRACE(COMP_PHL_DBG, _PHL_INFO_, "rtw_phl_tbtt_sync(): diff_tsf(%d), sync_ofst(%d), ofst(%d)\n",
2464*4882a593Smuzhiyun 		(u32)diff_tsf, sync_ofst, (u32)ofst);
2465*4882a593Smuzhiyun 	if (RTW_HAL_STATUS_SUCCESS != rtw_hal_tsf_sync(phl->hal,
2466*4882a593Smuzhiyun 					src_role->hw_port, tgt_role->hw_port,
2467*4882a593Smuzhiyun 					src_role->hw_band, (s32)diff_tsf,
2468*4882a593Smuzhiyun 					act)) {
2469*4882a593Smuzhiyun 		PHL_TRACE(COMP_PHL_DBG, _PHL_ERR_, "rtw_phl_tbtt_sync(): Sync tsf fail\n");
2470*4882a593Smuzhiyun 		goto exit;
2471*4882a593Smuzhiyun 	}
2472*4882a593Smuzhiyun 	if (RTW_HAL_STATUS_SUCCESS == rtw_hal_get_tsf(phl->hal,
2473*4882a593Smuzhiyun 					src_role->hw_port, &tsf_h, &tsf_l)) {
2474*4882a593Smuzhiyun 		PHL_TRACE(COMP_PHL_DBG, _PHL_INFO_, "rtw_phl_tbtt_sync(): tsf_src(0x%08x %08x)\n",
2475*4882a593Smuzhiyun 			tsf_h, tsf_l);
2476*4882a593Smuzhiyun 	}
2477*4882a593Smuzhiyun 	if (RTW_HAL_STATUS_SUCCESS == rtw_hal_get_tsf(phl->hal,
2478*4882a593Smuzhiyun 					tgt_role->hw_port, &tsf_h, &tsf_l)) {
2479*4882a593Smuzhiyun 		PHL_TRACE(COMP_PHL_DBG, _PHL_INFO_, "rtw_phl_tbtt_sync(): tsf_tgt(0x%08x %08x)\n",
2480*4882a593Smuzhiyun 			tsf_h, tsf_l);
2481*4882a593Smuzhiyun 	}
2482*4882a593Smuzhiyun 	*diff_t = (u16)diff_tsf;
2483*4882a593Smuzhiyun 	status = RTW_PHL_STATUS_SUCCESS;
2484*4882a593Smuzhiyun exit:
2485*4882a593Smuzhiyun 	return status;
2486*4882a593Smuzhiyun 
2487*4882a593Smuzhiyun }
2488*4882a593Smuzhiyun 
2489