1 /******************************************************************************
2 *
3 * Copyright(c) 2019 Realtek Corporation.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 *****************************************************************************/
15 #define _PHL_CHAN_C_
16 #include "phl_headers.h"
17
18 const char *const _band_str[] = {
19 "BAND_ON_24G",
20 "BAND_ON_5G",
21 "BAND_ON_6G",
22 "BAND_UNKNOWN"
23 };
24 #define _get_band_str(band) (((band) >= BAND_MAX) ? _band_str[BAND_MAX] : _band_str[(band)])
25
26 const char *const _bw_str[] = {
27 "BW_20M",
28 "BW_40M",
29 "BW_80M",
30 "BW_160M",
31 "BW_80_80M",
32 "BW_5M",
33 "BW_10M",
34 "BW_UNKNOWN"
35 };
36 #define _get_bw_str(bw) (((bw) >= CHANNEL_WIDTH_MAX) ? _bw_str[CHANNEL_WIDTH_MAX] : _bw_str[((bw))])
37
38 #ifdef DBG_PHL_CHAN
phl_chan_dump_chandef(const char * caller,const int line,bool show_caller,struct rtw_chan_def * chandef)39 void phl_chan_dump_chandef(const char *caller, const int line, bool show_caller,
40 struct rtw_chan_def *chandef)
41 {
42 if (show_caller)
43 PHL_INFO("###### FUN - %s LINE - %d #######\n", caller, line);
44
45 PHL_INFO("\t[CH] band:%s\n", _get_band_str(chandef->band));
46 PHL_INFO("\t[CH] chan:%d\n", chandef->chan);
47 PHL_INFO("\t[CH] center_ch:%d\n", chandef->center_ch);
48 PHL_INFO("\t[CH] bw:%s\n", _get_bw_str(chandef->bw));
49 PHL_INFO("\t[CH] offset:%d\n", chandef->offset);
50
51 PHL_INFO("\t[CH] center_freq1:%d\n", chandef->center_freq1);
52 PHL_INFO("\t[CH] center_freq2:%d\n", chandef->center_freq2);
53 PHL_INFO("\t[CH] hw_value:%d\n", chandef->hw_value);
54
55 if (show_caller)
56 PHL_INFO("#################################\n");
57 }
58 #endif
59
60 #ifdef CONFIG_PHL_DFS
61 static enum rtw_phl_status
phl_radar_detect_hdl(struct phl_info_t * phl_info,u8 channel,enum channel_width bwmode,enum chan_offset offset)62 phl_radar_detect_hdl(struct phl_info_t *phl_info,
63 u8 channel, enum channel_width bwmode, enum chan_offset offset)
64 {
65 struct rtw_phl_com_t *phl_com = phl_info->phl_com;
66 struct rtw_dfs_t *dfs_info = &phl_com->dfs_info;
67 enum rtw_phl_status rst = RTW_PHL_STATUS_FAILURE;
68 bool overlap_radar_range;
69
70 overlap_radar_range = rtw_hal_in_radar_domain(phl_info->hal,
71 channel, bwmode);
72 if (overlap_radar_range)
73 PHL_INFO("chan in DFS domain ch:%d,bw:%d\n", channel, bwmode);
74
75
76 if (overlap_radar_range && !dfs_info->dfs_enabled) {
77 /*radar_detect_enable*/
78 if (rtw_hal_radar_detect_cfg(phl_info->hal, true) ==
79 RTW_HAL_STATUS_SUCCESS) {
80 dfs_info->dfs_enabled = true;
81 PHL_INFO("[DFS] chan(%d) in radar range, enable dfs\n",
82 channel);
83 rst = RTW_PHL_STATUS_SUCCESS;
84 }
85 else {
86 PHL_ERR("[DFS] chan(%d) in radar range, enable dfs failed\n",
87 channel);
88 }
89
90 } else if (!overlap_radar_range && dfs_info->dfs_enabled) {
91 /*radar_detect_disable*/
92 if (rtw_hal_radar_detect_cfg(phl_info->hal, false) ==
93 RTW_HAL_STATUS_SUCCESS) {
94 dfs_info->dfs_enabled = false;
95 PHL_INFO("[DFS] chan(%d) not in radar range, disable dfs\n",
96 channel);
97 rst = RTW_PHL_STATUS_SUCCESS;
98 }
99 else {
100 PHL_ERR("[DFS] chan(%d) not in radar range, disable dfs failed\n",
101 channel);
102 }
103 }
104 return rst;
105 }
106 #endif /*CONFIG_PHL_DFS*/
107
108 enum rtw_phl_status
phl_set_ch_bw(struct rtw_wifi_role_t * wifi_role,struct rtw_chan_def * chdef,bool do_rfk)109 phl_set_ch_bw(struct rtw_wifi_role_t *wifi_role,
110 struct rtw_chan_def *chdef, bool do_rfk)
111 {
112 struct phl_info_t *phl_info = wifi_role->phl_com->phl_priv;
113 enum rtw_hal_status hstatus = RTW_HAL_STATUS_FAILURE;
114
115 chdef->band = rtw_phl_get_band_type(chdef->chan);
116 #ifdef CONFIG_PHL_DFS
117 phl_radar_detect_hdl(phl_info, chdef->chan, chdef->bw, chdef->offset);
118 #endif
119 hstatus = rtw_hal_set_ch_bw(phl_info->hal, wifi_role->hw_band,
120 chdef, do_rfk);
121 if (RTW_HAL_STATUS_SUCCESS != hstatus)
122 PHL_ERR("%s rtw_hal_set_ch_bw: statuts = %u\n", __func__, hstatus);
123
124 return RTW_PHL_STATUS_SUCCESS;
125 }
126
127 #ifdef CONFIG_CMD_DISP
128 struct setch_param {
129 struct rtw_wifi_role_t *wrole;
130 struct rtw_chan_def chdef;
131 bool do_rfk;
132 };
133
134 static void
_phl_chg_op_chdef_done(void * drv_priv,u8 * cmd,u32 cmd_len,enum rtw_phl_status status)135 _phl_chg_op_chdef_done(void *drv_priv, u8 *cmd, u32 cmd_len,
136 enum rtw_phl_status status)
137 {
138 if (cmd) {
139 struct chg_opch_param *param = (struct chg_opch_param *)cmd;
140
141 if (param->chg_opch_done) {
142 enum rtw_phl_status psts = RTW_PHL_STATUS_FAILURE;
143
144 if (RTW_PHL_STATUS_CMD_SUCCESS == status &&
145 RTW_PHL_STATUS_CMD_SUCCESS == param->cmd_start_sts)
146 psts = RTW_PHL_STATUS_SUCCESS;
147 param->chg_opch_done(drv_priv, param->wrole->id,
148 psts);
149 }
150 _os_kmem_free(drv_priv, cmd, cmd_len);
151 cmd = NULL;
152 PHL_INFO("%s.....\n", __func__);
153 }
154 }
155
_phl_chg_op_chdef_start_done(void * drv_priv,u8 * cmd,u32 cmd_len,enum rtw_phl_status status)156 static void _phl_chg_op_chdef_start_done(void *drv_priv, u8 *cmd, u32 cmd_len, enum rtw_phl_status status)
157 {
158 if (cmd) {
159 struct chg_opch_param *param = (struct chg_opch_param *)cmd;
160
161 param->cmd_start_sts = status;
162 PHL_INFO("%s.....\n", __func__);
163 }
164 }
165
166 enum rtw_phl_status
phl_cmd_chg_op_chdef_start_hdl(struct phl_info_t * phl,u8 * param)167 phl_cmd_chg_op_chdef_start_hdl(struct phl_info_t *phl, u8 *param)
168 {
169 enum rtw_phl_status pstatus = RTW_PHL_STATUS_FAILURE;
170 struct chg_opch_param *ch_param = (struct chg_opch_param *)param;
171 void *drv = phl_to_drvpriv(phl);
172 enum phl_upd_mode mode = PHL_UPD_STA_INFO_CHANGE;
173 struct phl_queue *sta_queue = NULL;
174 struct rtw_phl_stainfo_t *sta = NULL;
175 struct rtw_chan_def chctx_result = {0};
176
177 sta = rtw_phl_get_stainfo_self(phl, ch_param->wrole);
178 if (NULL == sta) {
179 PHL_TRACE(COMP_PHL_DBG, _PHL_ERR_, "%s: cannot get stainfo_self\n",
180 __FUNCTION__);
181 goto exit;
182 }
183 /* Update MR chctx */
184 if (RTW_PHL_STATUS_SUCCESS != phl_mr_chandef_chg(phl, ch_param->wrole,
185 &ch_param->new_chdef, &chctx_result)) {
186 PHL_TRACE(COMP_PHL_DBG, _PHL_ERR_, "%s: MR chang chdef failed!\n",
187 __FUNCTION__);
188 goto exit;
189 }
190 /* Up Role chdef */
191 _os_mem_cpy(drv, &ch_param->wrole->chandef, &ch_param->new_chdef,
192 sizeof(struct rtw_chan_def));
193 /* Update self Sta chdef */
194 _os_mem_cpy(drv, &sta->chandef, &ch_param->new_chdef,
195 sizeof(struct rtw_chan_def));
196 /* Notify rf for the suspended channel */
197 rtw_hal_disconnect_notify(phl->hal, &ch_param->ori_chdef);
198 /* Switch channel */
199 if (RTW_PHL_STATUS_SUCCESS != phl_set_ch_bw(ch_param->wrole,
200 &chctx_result, true)) {
201 PHL_TRACE(COMP_PHL_DBG, _PHL_ERR_, "%s: Switch ch failed!\n",
202 __FUNCTION__);
203 goto exit;
204 }
205 if (ch_param->wrole->mstate == MLME_LINKED) {
206 /*Up STA setting(RA....) */
207 sta_queue = &ch_param->wrole->assoc_sta_queue;
208 _os_spinlock(drv, &sta_queue->lock, _bh, NULL);
209 phl_list_for_loop(sta, struct rtw_phl_stainfo_t,
210 &sta_queue->queue, list) {
211 if (sta)
212 phl_change_stainfo(phl, sta, mode);
213 }
214 _os_spinunlock(drv, &sta_queue->lock, _bh, NULL);
215 }
216 pstatus = RTW_PHL_STATUS_SUCCESS;
217 exit:
218 PHL_TRACE(COMP_PHL_DBG, _PHL_INFO_, "%s: pstatus(%d)\n",
219 __FUNCTION__, pstatus);
220 return pstatus;
221 }
222
223 enum rtw_phl_status
rtw_phl_cmd_chg_op_chdef(struct rtw_wifi_role_t * wrole,struct rtw_chan_def * new_chdef,bool cmd_wait,u32 cmd_timeout,void (* chg_opch_done)(void * priv,u8 ridx,enum rtw_phl_status status))224 rtw_phl_cmd_chg_op_chdef(struct rtw_wifi_role_t *wrole,
225 struct rtw_chan_def *new_chdef, bool cmd_wait, u32 cmd_timeout,
226 void (*chg_opch_done)(void *priv, u8 ridx, enum rtw_phl_status status))
227 {
228 enum rtw_phl_status psts = RTW_PHL_STATUS_FAILURE;
229 struct phl_info_t *phl = wrole->phl_com->phl_priv;
230 void *drv = phl_to_drvpriv(phl);
231 u32 param_len = sizeof(struct chg_opch_param);
232 struct chg_opch_param *param = _os_kmem_alloc(drv, param_len);
233
234 if (param == NULL) {
235 PHL_ERR("%s: alloc param failed!\n", __func__);
236 goto _exit;
237 }
238 param->wrole = wrole;
239 _os_mem_cpy(drv, ¶m->new_chdef, new_chdef,
240 sizeof(struct rtw_chan_def));
241 _os_mem_cpy(drv, ¶m->ori_chdef, &wrole->chandef,
242 sizeof(struct rtw_chan_def));
243 param->chg_opch_done = chg_opch_done;
244 psts = phl_cmd_enqueue(phl,
245 wrole->hw_band,
246 MSG_EVT_CHG_OP_CH_DEF_START,
247 (u8 *)param,
248 param_len,
249 _phl_chg_op_chdef_start_done,
250 PHL_CMD_NO_WAIT,
251 0);
252 if (psts != RTW_PHL_STATUS_SUCCESS) {
253 PHL_INFO("%s: Fail to issue change op chdef start!!\n",
254 __func__);
255 if (!is_cmd_failure(psts)) {
256 /* Send cmd fail */
257 _os_kmem_free(drv, param, param_len);
258 psts = RTW_PHL_STATUS_FAILURE;
259 }
260 goto _exit;
261 }
262 psts = phl_cmd_enqueue(phl,
263 wrole->hw_band,
264 MSG_EVT_CHG_OP_CH_DEF_END,
265 (u8 *)param,
266 param_len,
267 _phl_chg_op_chdef_done,
268 cmd_wait ? PHL_CMD_WAIT : PHL_CMD_NO_WAIT,
269 cmd_timeout);
270 if (psts != RTW_PHL_STATUS_SUCCESS) {
271 PHL_INFO("%s: Fail to issue change op chdef start!!\n",
272 __func__);
273 if (!is_cmd_failure(psts)) {
274 /* Send cmd fail */
275 _os_kmem_free(drv, param, param_len);
276 psts = RTW_PHL_STATUS_FAILURE;
277 }
278 goto _exit;
279 }
280 _exit:
281 PHL_INFO("%s: Issue cmd, status(%d)\n", __func__, psts);
282 return psts;
283 }
284
285 enum rtw_phl_status
phl_cmd_set_ch_bw_hdl(struct phl_info_t * phl_info,u8 * param)286 phl_cmd_set_ch_bw_hdl(struct phl_info_t *phl_info, u8 *param)
287 {
288 struct setch_param *ch_param = (struct setch_param *)param;
289
290 return phl_set_ch_bw(ch_param->wrole,
291 &(ch_param->chdef),
292 ch_param->do_rfk);
293 }
294
_phl_set_ch_bw_done(void * drv_priv,u8 * cmd,u32 cmd_len,enum rtw_phl_status status)295 static void _phl_set_ch_bw_done(void *drv_priv, u8 *cmd, u32 cmd_len, enum rtw_phl_status status)
296 {
297 if (cmd) {
298 _os_kmem_free(drv_priv, cmd, cmd_len);
299 cmd = NULL;
300 PHL_INFO("%s.....\n", __func__);
301 }
302 }
303
304 enum rtw_phl_status
rtw_phl_cmd_set_ch_bw(struct rtw_wifi_role_t * wifi_role,struct rtw_chan_def * chdef,bool do_rfk,enum phl_cmd_type cmd_type,u32 cmd_timeout)305 rtw_phl_cmd_set_ch_bw(struct rtw_wifi_role_t *wifi_role,
306 struct rtw_chan_def *chdef,
307 bool do_rfk,
308 enum phl_cmd_type cmd_type,
309 u32 cmd_timeout)
310 {
311 struct phl_info_t *phl_info = wifi_role->phl_com->phl_priv;
312 void *drv = wifi_role->phl_com->drv_priv;
313 enum rtw_phl_status psts = RTW_PHL_STATUS_FAILURE;
314 struct setch_param *param = NULL;
315 u32 param_len;
316
317 if (cmd_type == PHL_CMD_DIRECTLY) {
318 psts = phl_set_ch_bw(wifi_role, chdef, do_rfk);
319 goto _exit;
320 }
321
322 param_len = sizeof(struct setch_param);
323 param = _os_kmem_alloc(drv, param_len);
324 if (param == NULL) {
325 PHL_ERR("%s: alloc param failed!\n", __func__);
326 goto _exit;
327 }
328 param->wrole = wifi_role;
329 _os_mem_cpy(drv, ¶m->chdef, chdef, sizeof(struct rtw_chan_def));
330 param->do_rfk = do_rfk;
331
332 psts = phl_cmd_enqueue(phl_info,
333 wifi_role->hw_band,
334 MSG_EVT_SWCH_START,
335 (u8 *)param,
336 param_len,
337 _phl_set_ch_bw_done,
338 cmd_type,
339 cmd_timeout);
340
341 if (is_cmd_failure(psts)) {
342 /* Send cmd success, but wait cmd fail*/
343 psts = RTW_PHL_STATUS_FAILURE;
344 } else if (psts != RTW_PHL_STATUS_SUCCESS) {
345 /* Send cmd fail */
346 _os_kmem_free(drv, param, param_len);
347 psts = RTW_PHL_STATUS_FAILURE;
348 }
349 _exit:
350 return psts;
351 }
352 #endif /*CONFIG_CMD_DISP*/
353
rtw_phl_get_cur_ch(struct rtw_wifi_role_t * wifi_role)354 u8 rtw_phl_get_cur_ch(struct rtw_wifi_role_t *wifi_role)
355 {
356 struct phl_info_t *phl_info = wifi_role->phl_com->phl_priv;
357
358 return rtw_hal_get_cur_ch(phl_info->hal, wifi_role->hw_band);
359 }
360
361 enum rtw_phl_status
rtw_phl_get_cur_hal_chdef(struct rtw_wifi_role_t * wifi_role,struct rtw_chan_def * cur_chandef)362 rtw_phl_get_cur_hal_chdef(struct rtw_wifi_role_t *wifi_role,
363 struct rtw_chan_def *cur_chandef)
364 {
365 struct phl_info_t *phl_info = wifi_role->phl_com->phl_priv;
366
367 rtw_hal_get_cur_chdef(phl_info->hal, wifi_role->hw_band, cur_chandef);
368 return RTW_PHL_STATUS_SUCCESS;
369 }
370
371 static enum rtw_phl_status
_dfs_hw_tx_pause(struct rtw_wifi_role_t * wifi_role,bool tx_pause)372 _dfs_hw_tx_pause(struct rtw_wifi_role_t *wifi_role, bool tx_pause)
373 {
374
375 struct phl_info_t *phl_info = wifi_role->phl_com->phl_priv;
376 enum rtw_hal_status hstatus = RTW_HAL_STATUS_FAILURE;
377
378 hstatus = rtw_hal_dfs_pause_tx(phl_info->hal, wifi_role->hw_band, tx_pause);
379
380 if (RTW_HAL_STATUS_SUCCESS == hstatus) {
381 return RTW_PHL_STATUS_SUCCESS;
382 } else {
383 PHL_ERR("%s Failure :%u\n",__func__, hstatus);
384 return RTW_PHL_STATUS_FAILURE;
385 }
386 }
387
388 #ifdef CONFIG_CMD_DISP
389 struct dfs_txpause_param {
390 struct rtw_wifi_role_t *wrole;
391 bool pause;
392 };
393
394 enum rtw_phl_status
phl_cmd_dfs_tx_pause_hdl(struct phl_info_t * phl_info,u8 * param)395 phl_cmd_dfs_tx_pause_hdl(struct phl_info_t *phl_info, u8 *param)
396 {
397 struct dfs_txpause_param *dfs = (struct dfs_txpause_param *)param;
398
399 PHL_INFO("%s(), dfs param, wrole = %p, pause = %d\n",
400 __func__, dfs->wrole, dfs->pause);
401
402 return _dfs_hw_tx_pause(dfs->wrole, dfs->pause);
403 }
404
_phl_dfs_tx_pause_done(void * drv_priv,u8 * cmd,u32 cmd_len,enum rtw_phl_status status)405 static void _phl_dfs_tx_pause_done(void *drv_priv, u8 *cmd, u32 cmd_len, enum rtw_phl_status status)
406 {
407 if (cmd) {
408 _os_kmem_free(drv_priv, cmd, cmd_len);
409 cmd = NULL;
410 PHL_INFO("%s.....\n", __func__);
411 }
412 }
413 #endif /*CONFIG_CMD_DISP*/
414
415 enum rtw_phl_status
rtw_phl_cmd_dfs_tx_pause(struct rtw_wifi_role_t * wifi_role,bool pause,enum phl_cmd_type cmd_type,u32 cmd_timeout)416 rtw_phl_cmd_dfs_tx_pause(struct rtw_wifi_role_t *wifi_role, bool pause,
417 enum phl_cmd_type cmd_type, u32 cmd_timeout)
418 {
419 #ifdef CONFIG_CMD_DISP
420 struct phl_info_t *phl_info = wifi_role->phl_com->phl_priv;
421 void *drv = wifi_role->phl_com->drv_priv;
422 enum rtw_phl_status psts = RTW_PHL_STATUS_FAILURE;
423 struct dfs_txpause_param *param = NULL;
424 u32 param_len;
425
426 param_len = sizeof(struct dfs_txpause_param);
427 param = _os_kmem_alloc(drv, param_len);
428 if (param == NULL) {
429 PHL_ERR("%s: alloc param failed!\n", __func__);
430 goto _exit;
431 }
432 param->wrole = wifi_role;
433 param->pause = pause;
434
435 psts = phl_cmd_enqueue(phl_info,
436 wifi_role->hw_band,
437 MSG_EVT_DFS_PAUSE_TX,
438 (u8 *)param, param_len,
439 _phl_dfs_tx_pause_done,
440 cmd_type, cmd_timeout);
441
442 if (is_cmd_failure(psts)) {
443 /* Send cmd success, but wait cmd fail*/
444 psts = RTW_PHL_STATUS_FAILURE;
445 } else if (psts != RTW_PHL_STATUS_SUCCESS) {
446 /* Send cmd fail */
447 _os_kmem_free(drv, param, param_len);
448 psts = RTW_PHL_STATUS_FAILURE;
449 }
450
451 _exit:
452 return psts;
453 #else
454 PHL_ERR("%s(), CONFIG_CMD_DISP need to be enabled for MSG_EVT_DFS_PAUSE_TX \n",__func__);
455
456 return RTW_PHL_STATUS_FAILURE;
457 #endif
458 }
459
460
461 #ifdef CONFIG_DBCC_SUPPORT
462 enum rtw_phl_status
rtw_phl_dbcc_test(void * phl,enum dbcc_test_id id,void * param)463 rtw_phl_dbcc_test(void *phl, enum dbcc_test_id id, void *param)
464 {
465 struct phl_info_t *phl_info = (struct phl_info_t *)phl;
466 enum rtw_hal_status hsts = RTW_HAL_STATUS_FAILURE;
467
468 switch (id){
469 case DBCC_PRE_CFG :
470 {
471 bool dbcc_en = *(bool *)param;
472
473 PHL_INFO("[DBCC] PRE_CFG :%s\n", (dbcc_en) ? "EN" : "DIS");
474 hsts = rtw_hal_dbcc_pre_cfg(phl_info->hal, phl_info->phl_com, dbcc_en);
475 }
476 break;
477
478 case DBCC_CFG :
479 {
480 bool dbcc_en = *(bool *)param;
481
482 PHL_INFO("[DBCC] CFG :%s\n", (dbcc_en) ? "EN" : "DIS");
483 hsts = rtw_hal_dbcc_cfg(phl_info->hal, phl_info->phl_com, dbcc_en);
484 }
485 break;
486 case DBCC_CLEAN_TXQ :
487 hsts = rtw_hal_clean_tx_queue(phl_info->hal);
488 break;
489 default :
490 PHL_ERR("%s unknown DBCC Test ID:%d\n",__func__, id);
491 break;
492 }
493
494 return RTW_PHL_STATUS_SUCCESS;
495 }
496 #endif
497
498 #define MAX_CHANCTX_QUEUE_NUM 2
499
500
501 static enum rtw_phl_status
_phl_chanctx_add(struct phl_info_t * phl_info,struct phl_queue * chan_ctx_queue,struct rtw_chan_ctx * chanctx)502 _phl_chanctx_add(struct phl_info_t *phl_info,
503 struct phl_queue *chan_ctx_queue,
504 struct rtw_chan_ctx *chanctx)
505 {
506 if (!chanctx)
507 return RTW_PHL_STATUS_FAILURE;
508
509 list_add_tail(&chanctx->list, &chan_ctx_queue->queue);
510 chan_ctx_queue->cnt++;
511 if (chan_ctx_queue->cnt > MAX_CHANCTX_QUEUE_NUM) {
512 PHL_ERR("%s chan_ctx_queue cnt(%d) > 2\n", __func__, chan_ctx_queue->cnt);
513 _os_warn_on(1);
514 }
515
516 return RTW_PHL_STATUS_SUCCESS;
517 }
518
519 static enum rtw_phl_status
_phl_chanctx_add_with_lock(struct phl_info_t * phl_info,struct phl_queue * chan_ctx_queue,struct rtw_chan_ctx * chanctx)520 _phl_chanctx_add_with_lock(struct phl_info_t *phl_info,
521 struct phl_queue *chan_ctx_queue,
522 struct rtw_chan_ctx *chanctx)
523 {
524 void *drv = phl_to_drvpriv(phl_info);
525
526 if (!chanctx)
527 return RTW_PHL_STATUS_FAILURE;
528
529 _os_spinlock(drv, &chan_ctx_queue->lock, _ps, NULL);
530 _phl_chanctx_add(phl_info, chan_ctx_queue, chanctx);
531 _os_spinunlock(drv, &chan_ctx_queue->lock, _ps, NULL);
532 return RTW_PHL_STATUS_SUCCESS;
533 }
534
535 static enum rtw_phl_status
_phl_chanctx_del(struct phl_info_t * phl_info,struct phl_queue * chan_ctx_queue,struct rtw_chan_ctx * chanctx)536 _phl_chanctx_del(struct phl_info_t *phl_info,
537 struct phl_queue *chan_ctx_queue,
538 struct rtw_chan_ctx *chanctx)
539 {
540 if (!chanctx)
541 return RTW_PHL_STATUS_FAILURE;
542
543 /*if (!list_empty(&chan_ctx_queue->queue)) {*/
544 if (chan_ctx_queue->cnt) {
545 list_del(&chanctx->list);
546 chan_ctx_queue->cnt--;
547 if (chan_ctx_queue->cnt < 0) {
548 PHL_ERR("%s chan_ctx_queue cnt(%d) < 0\n", __func__, chan_ctx_queue->cnt);
549 _os_warn_on(1);
550 }
551 }
552 return RTW_PHL_STATUS_SUCCESS;
553 }
554
555 static enum rtw_phl_status
_phl_chanctx_del_with_lock(struct phl_info_t * phl_info,struct phl_queue * chan_ctx_queue,struct rtw_chan_ctx * chanctx)556 _phl_chanctx_del_with_lock(struct phl_info_t *phl_info,
557 struct phl_queue *chan_ctx_queue,
558 struct rtw_chan_ctx *chanctx)
559 {
560 void *drv = phl_to_drvpriv(phl_info);
561
562 if (!chanctx)
563 return RTW_PHL_STATUS_FAILURE;
564
565 _os_spinlock(drv, &chan_ctx_queue->lock, _ps, NULL);
566 _phl_chanctx_del(phl_info, chan_ctx_queue, chanctx);
567 _os_spinunlock(drv, &chan_ctx_queue->lock, _ps, NULL);
568 return RTW_PHL_STATUS_SUCCESS;
569 }
570
571 static inline enum rtw_phl_status
_phl_chanctx_rmap_set(struct phl_info_t * phl_info,struct rtw_wifi_role_t * wifi_role,struct phl_queue * chan_ctx_queue,struct rtw_chan_ctx * chanctx)572 _phl_chanctx_rmap_set(struct phl_info_t *phl_info,
573 struct rtw_wifi_role_t *wifi_role,
574 struct phl_queue *chan_ctx_queue,
575 struct rtw_chan_ctx *chanctx)
576 {
577 u8 ridx = wifi_role->id;
578
579 if (!chanctx)
580 return RTW_PHL_STATUS_FAILURE;
581
582 #ifdef DBG_CHCTX_RMAP
583 if (chanctx->role_map & BIT(ridx))
584 PHL_ERR("wifi_role idx(%d) has in chanctx->role_map(0x%02x)\n",
585 ridx, chanctx->role_map);
586 #endif
587 chanctx->role_map |= BIT(ridx);
588 wifi_role->chanctx = chanctx;
589 return RTW_PHL_STATUS_SUCCESS;
590 }
591
592 static enum rtw_phl_status
_phl_chanctx_rmap_set_with_lock(struct phl_info_t * phl_info,struct rtw_wifi_role_t * wifi_role,struct phl_queue * chan_ctx_queue,struct rtw_chan_ctx * chanctx)593 _phl_chanctx_rmap_set_with_lock(struct phl_info_t *phl_info,
594 struct rtw_wifi_role_t *wifi_role,
595 struct phl_queue *chan_ctx_queue,
596 struct rtw_chan_ctx *chanctx)
597 {
598 void *drv = phl_to_drvpriv(phl_info);
599
600 if (!chanctx)
601 return RTW_PHL_STATUS_FAILURE;
602
603 _os_spinlock(drv, &chan_ctx_queue->lock, _ps, NULL);
604 _phl_chanctx_rmap_set(phl_info, wifi_role, chan_ctx_queue, chanctx);
605 _os_spinunlock(drv, &chan_ctx_queue->lock, _ps, NULL);
606 return RTW_PHL_STATUS_SUCCESS;
607 }
608
609 static inline enum rtw_phl_status
_phl_chanctx_rmap_clr(struct phl_info_t * phl_info,struct rtw_wifi_role_t * wifi_role,struct phl_queue * chan_ctx_queue,struct rtw_chan_ctx * chanctx)610 _phl_chanctx_rmap_clr(struct phl_info_t *phl_info,
611 struct rtw_wifi_role_t *wifi_role,
612 struct phl_queue *chan_ctx_queue,
613 struct rtw_chan_ctx *chanctx)
614 {
615 u8 ridx = wifi_role->id;
616
617 if (!chanctx)
618 return RTW_PHL_STATUS_FAILURE;
619
620 #ifdef DBG_CHCTX_RMAP
621 if (!(chanctx->role_map & BIT(ridx)))
622 PHL_ERR("ridx(%d) hasn't in chanctx->role_map(0x%02x)\n", ridx, chanctx->role_map);
623 #endif
624 wifi_role->chanctx = NULL;
625 chanctx->role_map &= ~BIT(ridx);
626
627 return RTW_PHL_STATUS_SUCCESS;
628 }
629
630 static enum rtw_phl_status
_phl_chanctx_rmap_clr_with_lock(struct phl_info_t * phl_info,struct rtw_wifi_role_t * wifi_role,struct phl_queue * chan_ctx_queue,struct rtw_chan_ctx * chanctx)631 _phl_chanctx_rmap_clr_with_lock(struct phl_info_t *phl_info,
632 struct rtw_wifi_role_t *wifi_role,
633 struct phl_queue *chan_ctx_queue,
634 struct rtw_chan_ctx *chanctx)
635 {
636 void *drv = phl_to_drvpriv(phl_info);
637
638 if (!chanctx)
639 return RTW_PHL_STATUS_FAILURE;
640
641 _os_spinlock(drv, &chan_ctx_queue->lock, _ps, NULL);
642 _phl_chanctx_rmap_clr(phl_info, wifi_role, chan_ctx_queue, chanctx);
643 _os_spinunlock(drv, &chan_ctx_queue->lock, _ps, NULL);
644 return RTW_PHL_STATUS_SUCCESS;
645 }
646
phl_chanctx_get_rnum(struct phl_info_t * phl_info,struct phl_queue * chan_ctx_queue,struct rtw_chan_ctx * chanctx)647 u8 phl_chanctx_get_rnum(struct phl_info_t *phl_info,
648 struct phl_queue *chan_ctx_queue,
649 struct rtw_chan_ctx *chanctx)
650 {
651 u8 i;
652 u8 role_num = 0;
653
654 for (i = 0; i < MAX_WIFI_ROLE_NUMBER; i++)
655 if (chanctx->role_map & BIT(i))
656 role_num++;
657 return role_num;
658 }
659
phl_chanctx_get_rnum_with_lock(struct phl_info_t * phl_info,struct phl_queue * chan_ctx_queue,struct rtw_chan_ctx * chanctx)660 u8 phl_chanctx_get_rnum_with_lock(struct phl_info_t *phl_info,
661 struct phl_queue *chan_ctx_queue,
662 struct rtw_chan_ctx *chanctx)
663 {
664 void *drv = phl_to_drvpriv(phl_info);
665 u8 role_num = 0;
666
667 if (!chanctx)
668 return role_num;
669
670 _os_spinlock(drv, &chan_ctx_queue->lock, _ps, NULL);
671 role_num = phl_chanctx_get_rnum(phl_info, chan_ctx_queue, chanctx);
672 _os_spinunlock(drv, &chan_ctx_queue->lock, _ps, NULL);
673 return role_num;
674 }
675
676 /**
677 * _phl_is_chbw_grouped - test if the two ch settings can be grouped together
678 * @ch_a: ch of set a
679 * @bw_a: bw of set a
680 * @offset_a: offset of set a
681 * @ch_b: ch of set b
682 * @bw_b: bw of set b
683 * @offset_b: offset of set b
684 */
_phl_is_chbw_grouped(u8 ch_a,enum channel_width bw_a,enum chan_offset offset_a,u8 ch_b,enum channel_width bw_b,enum chan_offset offset_b)685 static bool _phl_is_chbw_grouped(u8 ch_a, enum channel_width bw_a, enum chan_offset offset_a
686 , u8 ch_b, enum channel_width bw_b, enum chan_offset offset_b)
687 {
688 bool is_grouped = false;
689
690 if (ch_a != ch_b) {
691 /* ch is different */
692 goto exit;
693 } else if ((bw_a == CHANNEL_WIDTH_40 || bw_a == CHANNEL_WIDTH_80)
694 && (bw_b == CHANNEL_WIDTH_40 || bw_b == CHANNEL_WIDTH_80)
695 ) {
696 if (offset_a != offset_b)
697 goto exit;
698 }
699
700 is_grouped = true;
701
702 exit:
703 return is_grouped;
704 }
705
706
707 static inline bool
_phl_feature_check(struct rtw_phl_com_t * phl_com,u8 flg)708 _phl_feature_check(struct rtw_phl_com_t *phl_com, u8 flg)
709 {
710 return (phl_com->dev_cap.hw_sup_flags & flg) ? true : false;
711 }
712
_phl_get_offset_by_chbw(u8 ch,enum channel_width bw,enum chan_offset * r_offset)713 static u8 _phl_get_offset_by_chbw(u8 ch, enum channel_width bw, enum chan_offset *r_offset)
714 {
715 u8 valid = 1;
716 enum chan_offset offset = CHAN_OFFSET_NO_EXT;
717
718 if (bw == CHANNEL_WIDTH_20)
719 goto exit;
720
721 if (bw >= CHANNEL_WIDTH_80 && ch <= 14) {
722 valid = 0;
723 goto exit;
724 }
725
726 if (ch >= 1 && ch <= 4)
727 offset = CHAN_OFFSET_UPPER;
728 else if (ch >= 5 && ch <= 9) {
729 if (*r_offset == CHAN_OFFSET_UPPER || *r_offset == CHAN_OFFSET_LOWER)
730 offset = *r_offset; /* both lower and upper is valid, obey input value */
731 else
732 offset = CHAN_OFFSET_LOWER; /* default use upper */
733 } else if (ch >= 10 && ch <= 13)
734 offset = CHAN_OFFSET_LOWER;
735 else if (ch == 14) {
736 valid = 0; /* ch14 doesn't support 40MHz bandwidth */
737 goto exit;
738 } else if (ch >= 36 && ch <= 177) {
739 switch (ch) {
740 case 36:
741 case 44:
742 case 52:
743 case 60:
744 case 100:
745 case 108:
746 case 116:
747 case 124:
748 case 132:
749 case 140:
750 case 149:
751 case 157:
752 case 165:
753 case 173:
754 offset = CHAN_OFFSET_UPPER;
755 break;
756 case 40:
757 case 48:
758 case 56:
759 case 64:
760 case 104:
761 case 112:
762 case 120:
763 case 128:
764 case 136:
765 case 144:
766 case 153:
767 case 161:
768 case 169:
769 case 177:
770 offset = CHAN_OFFSET_LOWER;
771 break;
772 default:
773 valid = 0;
774 break;
775 }
776 } else
777 valid = 0;
778
779 exit:
780 if (valid && r_offset)
781 *r_offset = offset;
782 return valid;
783 }
784
785 /**
786 * _phl_adjust_chandef - obey g_ch, adjust g_bw, g_offset, bw, offset
787 * @req_ch: pointer of the request ch, may be modified further
788 * @req_bw: pointer of the request bw, may be modified further
789 * @req_offset: pointer of the request offset, may be modified further
790 * @g_ch: pointer of the ongoing group ch
791 * @g_bw: pointer of the ongoing group bw, may be modified further
792 * @g_offset: pointer of the ongoing group offset, may be modified further
793 */
_phl_adjust_chandef(u8 * req_ch,enum channel_width * req_bw,enum chan_offset * req_offset,u8 * g_ch,enum channel_width * g_bw,enum chan_offset * g_offset)794 static void _phl_adjust_chandef(u8 *req_ch, enum channel_width *req_bw, enum chan_offset *req_offset,
795 u8 *g_ch, enum channel_width *g_bw, enum chan_offset *g_offset)
796 {
797
798 *req_ch = *g_ch;
799
800 if (*req_bw == CHANNEL_WIDTH_80 && *g_ch <= 14) {
801 /*2.4G ch, downgrade to 40Mhz */
802 *req_bw = CHANNEL_WIDTH_40;
803 }
804
805 switch (*req_bw) {
806 case CHANNEL_WIDTH_80:
807 if (*g_bw == CHANNEL_WIDTH_40 || *g_bw == CHANNEL_WIDTH_80)
808 *req_offset = *g_offset;
809 else if (*g_bw == CHANNEL_WIDTH_20)
810 _phl_get_offset_by_chbw(*req_ch, *req_bw, req_offset);
811
812 if (*req_offset == CHAN_OFFSET_NO_EXT) {
813 PHL_ERR("%s req 80MHz BW without offset, down to 20MHz\n", __func__);
814 _os_warn_on(1);
815 *req_bw = CHANNEL_WIDTH_20;
816 }
817 break;
818 case CHANNEL_WIDTH_40:
819 if (*g_bw == CHANNEL_WIDTH_40 || *g_bw == CHANNEL_WIDTH_80)
820 *req_offset = *g_offset;
821 else if (*g_bw == CHANNEL_WIDTH_20)
822 _phl_get_offset_by_chbw(*req_ch, *req_bw, req_offset);
823
824 if (*req_offset == CHAN_OFFSET_NO_EXT) {
825 PHL_ERR("%s req 40MHz BW without offset, down to 20MHz\n", __func__);
826 _os_warn_on(1);
827 *req_bw = CHANNEL_WIDTH_20;
828 }
829 break;
830 case CHANNEL_WIDTH_20:
831 *req_offset = CHAN_OFFSET_NO_EXT;
832 break;
833 default:
834 PHL_ERR("%s req unsupported BW:%u\n", __func__, *req_bw);
835 _os_warn_on(1);
836 }
837
838 if (*req_bw > *g_bw) {
839 *g_bw = *req_bw;
840 *g_offset = *req_offset;
841 }
842 }
843
844 static enum rtw_phl_status
_phl_chanctx_create(struct phl_info_t * phl_info,struct rtw_wifi_role_t * wifi_role,enum band_type band,u8 chan,enum channel_width bw,enum chan_offset offset)845 _phl_chanctx_create(struct phl_info_t *phl_info,
846 struct rtw_wifi_role_t *wifi_role,
847 enum band_type band, u8 chan,
848 enum channel_width bw, enum chan_offset offset)
849 {
850 enum rtw_phl_status phl_sts = RTW_PHL_STATUS_FAILURE;
851 void *drv = phl_to_drvpriv(phl_info);
852 struct rtw_chan_ctx *chanctx = NULL;
853 struct mr_ctl_t *mr_ctl = phlcom_to_mr_ctrl(phl_info->phl_com);
854 struct hw_band_ctl_t *band_ctrl = &(mr_ctl->band_ctrl[wifi_role->hw_band]);
855
856 chanctx = _os_kmem_alloc(drv, sizeof(struct rtw_chan_ctx));
857 if (chanctx == NULL) {
858 PHL_ERR("alloc chanctx failed\n");
859 goto _exit;
860 }
861
862 chanctx->chan_def.band = band;
863 chanctx->chan_def.chan = chan;
864 chanctx->chan_def.bw = bw;
865 chanctx->chan_def.offset = offset;
866 chanctx->chan_def.center_ch = rtw_phl_get_center_ch(chan, bw, offset);
867 phl_sts = _phl_chanctx_add_with_lock(phl_info, &band_ctrl->chan_ctx_queue, chanctx);
868
869 if (phl_sts == RTW_PHL_STATUS_SUCCESS)
870 _phl_chanctx_rmap_set_with_lock(phl_info, wifi_role,
871 &band_ctrl->chan_ctx_queue, chanctx);
872 _exit:
873 return phl_sts;
874 }
phl_chanctx_add(struct phl_info_t * phl_info,struct rtw_wifi_role_t * wifi_role,u8 * chan,enum channel_width * bw,enum chan_offset * offset)875 bool phl_chanctx_add(struct phl_info_t *phl_info, struct rtw_wifi_role_t *wifi_role,
876 u8 *chan, enum channel_width *bw, enum chan_offset *offset)
877 {
878 struct rtw_phl_com_t *phl_com = phl_info->phl_com;
879 enum rtw_phl_status phl_sts = RTW_PHL_STATUS_FAILURE;
880 void *drv = phl_to_drvpriv(phl_info);
881 struct mr_ctl_t *mr_ctl = phlcom_to_mr_ctrl(phl_com);
882 struct hw_band_ctl_t *band_ctrl = &(mr_ctl->band_ctrl[wifi_role->hw_band]);
883 struct rtw_chan_ctx *chanctx = NULL;
884 struct rtw_chan_def *chandef = NULL;
885 _os_list *chan_ctx_list = &band_ctrl->chan_ctx_queue.queue;
886 bool is_ch_grouped = false;
887 enum band_type band = (*chan > 14) ? BAND_ON_5G : BAND_ON_24G;
888 int chanctx_num = 0;
889
890 if (wifi_role == NULL) {
891 PHL_ERR("%s wrole == NULL\n", __func__);
892 goto _exit;
893 }
894
895 PHL_INFO("%s new chan_def - hw_band_idx:%d, chan:%d, bw:%d, offset:%d\n",
896 __func__, wifi_role->hw_band, *chan, *bw, *offset);
897
898 chanctx_num = phl_mr_get_chanctx_num(phl_info, band_ctrl);
899 if (chanctx_num == 0) {
900 phl_sts = _phl_chanctx_create(phl_info, wifi_role,
901 band, *chan, *bw, *offset);
902 if (phl_sts != RTW_PHL_STATUS_SUCCESS) {
903 PHL_ERR("%s failed\n", __func__);
904 _os_warn_on(1);
905 }
906 else {
907 is_ch_grouped = true;
908 }
909 }
910 else {
911 _os_spinlock(drv, &band_ctrl->chan_ctx_queue.lock, _ps, NULL);
912 phl_list_for_loop(chanctx, struct rtw_chan_ctx, chan_ctx_list, list) {
913 chandef = &chanctx->chan_def;
914 is_ch_grouped = _phl_is_chbw_grouped(
915 chandef->chan, chandef->bw, chandef->offset,
916 *chan, *bw, *offset);
917 if (is_ch_grouped) {
918 _phl_adjust_chandef(chan, bw, offset,
919 &chandef->chan, &chandef->bw, &chandef->offset);
920
921 *chan = chandef->chan;
922 *bw = chandef->bw;
923 *offset = chandef->offset;
924 PHL_INFO("%s grouped chan_def - hw_band_idx:%d, chan:%d, bw:%d, offset:%d\n",
925 __func__, wifi_role->hw_band, *chan, *bw, *offset);
926 _phl_chanctx_rmap_set(phl_info, wifi_role,
927 &band_ctrl->chan_ctx_queue, chanctx);
928 break;
929 }
930 }
931 _os_spinunlock(drv, &band_ctrl->chan_ctx_queue.lock, _ps, NULL);
932
933 if (is_ch_grouped == false) { /*MCC or DBCC*/
934 PHL_INFO("%s chan:%d, bw:%d, offset:%d could not grouped\n",
935 __func__, *chan, *bw, *offset);
936
937 #ifdef CONFIG_MCC_SUPPORT
938 if (phl_com->dev_cap.mcc_sup == false) {
939 PHL_ERR("%s don't support MCC\n", __func__);
940 goto _exit;
941 }
942
943 if (chanctx_num >= 2) {
944 PHL_ERR("chan_ctx cnt(%d) >= 2\n", chanctx_num);
945 /*DBCC ?*/
946 goto _exit;
947 }
948 if (band == chandef->band) { /*MCC*/
949 phl_sts = _phl_chanctx_create(phl_info, wifi_role,
950 band, *chan, *bw, *offset);
951 if (phl_sts == RTW_PHL_STATUS_SUCCESS)
952 is_ch_grouped = true;
953 } else {
954 /*DBCC*/
955 #ifdef CONFIG_DBCC_SUPPORT
956 if (phl_com->dev_cap.dbcc_sup == true) {
957 PHL_INFO("%s support DBC\n", __func__);
958 goto _exit;
959 }
960 #endif
961 /*MCC*/
962 phl_sts = _phl_chanctx_create(phl_info, wifi_role,
963 band, *chan, *bw, *offset);
964 if (phl_sts == RTW_PHL_STATUS_SUCCESS)
965 is_ch_grouped = true;
966
967 }
968 #endif
969 }
970 }
971
972 _exit:
973 return is_ch_grouped;
974 }
975
976 enum rtw_phl_status
phl_chanctx_free(struct phl_info_t * phl_info,struct hw_band_ctl_t * band_ctl)977 phl_chanctx_free(struct phl_info_t *phl_info, struct hw_band_ctl_t *band_ctl)
978 {
979 int chanctx_num = 0;
980 struct rtw_chan_ctx *chanctx = NULL;
981 struct phl_queue *chan_ctx_queue = &band_ctl->chan_ctx_queue;
982 void *drv = phl_to_drvpriv(phl_info);
983
984 chanctx_num = phl_mr_get_chanctx_num(phl_info, band_ctl);
985 if (chanctx_num == 0)
986 return RTW_PHL_STATUS_SUCCESS;
987
988 PHL_INFO("%s band_idx:%d chctx_num:%d\n", __func__, band_ctl->id, chanctx_num);
989 do {
990 _os_spinlock(drv, &band_ctl->chan_ctx_queue.lock, _ps, NULL);
991 if (list_empty(&chan_ctx_queue->queue)) {
992 chanctx = NULL;
993 } else {
994 chanctx = list_first_entry(&chan_ctx_queue->queue,
995 struct rtw_chan_ctx, list);
996 list_del(&chanctx->list);
997 chan_ctx_queue->cnt--;
998 }
999 _os_spinunlock(drv, &band_ctl->chan_ctx_queue.lock, _ps, NULL);
1000
1001 if (chanctx) {
1002 _os_kmem_free(drv, chanctx, sizeof(struct rtw_chan_ctx));
1003 }
1004 } while (chanctx != NULL);
1005 return RTW_PHL_STATUS_SUCCESS;
1006 }
1007
1008 /* used for get all role under band_idx */
phl_get_chanctx_rolemap(struct phl_info_t * phl_info,u8 band_idx)1009 u8 phl_get_chanctx_rolemap(struct phl_info_t *phl_info, u8 band_idx)
1010 {
1011 void *drv = phl_to_drvpriv(phl_info);
1012 struct hw_band_ctl_t *band_ctrl = get_band_ctrl(phl_info, band_idx);
1013 _os_list *chan_ctx_list = &band_ctrl->chan_ctx_queue.queue;
1014 struct rtw_chan_ctx *chanctx = NULL;
1015 u8 role_map =0;
1016
1017 _os_spinlock(drv, &band_ctrl->chan_ctx_queue.lock, _ps, NULL);
1018 phl_list_for_loop(chanctx, struct rtw_chan_ctx, chan_ctx_list, list) {
1019 role_map |= chanctx->role_map;
1020 }
1021 _os_spinunlock(drv, &band_ctrl->chan_ctx_queue.lock, _ps, NULL);
1022
1023 return role_map;
1024 }
1025
1026
rtw_phl_chanctx_chk(void * phl,struct rtw_wifi_role_t * wifi_role,u8 chan,enum channel_width bw,enum chan_offset offset)1027 bool rtw_phl_chanctx_chk(void *phl, struct rtw_wifi_role_t *wifi_role,
1028 u8 chan, enum channel_width bw, enum chan_offset offset)
1029 {
1030 struct phl_info_t *phl_info = (struct phl_info_t *)phl;
1031 struct rtw_phl_com_t *phl_com = phl_info->phl_com;
1032 void *drv = phl_to_drvpriv(phl_info);
1033 struct mr_ctl_t *mr_ctl = phlcom_to_mr_ctrl(phl_com);
1034 u8 band_idx = wifi_role->hw_band;
1035 bool is_ch_group = false;
1036 struct hw_band_ctl_t *band_ctrl = &(mr_ctl->band_ctrl[band_idx]);
1037 int chanctx_num = 0;
1038 struct rtw_chan_ctx *chanctx = NULL;
1039 struct rtw_chan_def *chandef = NULL;
1040
1041 if (chan == 0) {
1042 PHL_ERR("%s req chan = 0 \n", __func__);
1043 goto _exit;
1044 }
1045
1046 /*status check*/
1047 if (mr_ctl->is_sb) {
1048 if (band_idx == 1) {
1049 PHL_ERR("wrole:%d in band_idx:%d\n", wifi_role->id, band_idx);
1050 _os_warn_on(1);
1051 goto _exit;
1052 }
1053 }
1054
1055 chanctx_num = phl_mr_get_chanctx_num(phl_info, band_ctrl);
1056
1057 if (chanctx_num > 0) {
1058 _os_spinlock(drv, &band_ctrl->chan_ctx_queue.lock, _ps, NULL);
1059 phl_list_for_loop(chanctx, struct rtw_chan_ctx, &band_ctrl->chan_ctx_queue.queue, list) {
1060 chandef = &chanctx->chan_def;
1061 is_ch_group = _phl_is_chbw_grouped(
1062 chandef->chan, chandef->bw, chandef->offset,
1063 chan, bw, offset);
1064 if (is_ch_group)
1065 break;
1066 }
1067 /* consider MCC case (support max 2 diff ch for MCC currently) */
1068 #ifdef CONFIG_MCC_SUPPORT
1069 if (phl_com->dev_cap.mcc_sup == true && is_ch_group == false && chanctx_num < 2) {
1070 is_ch_group = true;
1071 }
1072 #endif
1073 _os_spinunlock(drv, &band_ctrl->chan_ctx_queue.lock, _ps, NULL);
1074 } else {
1075 is_ch_group = true;
1076 }
1077 _exit:
1078 PHL_DUMP_MR_EX(phl_info);
1079 return is_ch_group;
1080 }
1081
1082
1083 /*
1084 * Add new operating chdef to MR.
1085 * @new_chan: Input: new chdef; Output: the final operating ch ctx.
1086 * ex: In the scc case, it will be the group chdef.
1087 */
rtw_phl_chanctx_add(void * phl,struct rtw_wifi_role_t * wifi_role,u8 * chan,enum channel_width * bw,enum chan_offset * offset)1088 bool rtw_phl_chanctx_add(void *phl, struct rtw_wifi_role_t *wifi_role,
1089 u8 *chan, enum channel_width *bw, enum chan_offset *offset)
1090 {
1091 struct phl_info_t *phl_info = (struct phl_info_t *)phl;
1092 struct rtw_phl_com_t *phl_com = phl_info->phl_com;
1093 void *drv = phl_to_drvpriv(phl_info);
1094 struct mr_ctl_t *mr_ctl = phlcom_to_mr_ctrl(phl_com);
1095 u8 band_idx = wifi_role->hw_band;
1096 bool is_ch_grouped = false;
1097 struct hw_band_ctl_t *band_ctrl = &(mr_ctl->band_ctrl[band_idx]);
1098 int chanctx_num = 0;
1099 u8 chctx_role_num = 0;
1100
1101 if(!chan || !bw || !offset)
1102 goto _exit;
1103
1104 if (*chan == 0) {
1105 PHL_ERR("%s req chan = 0 \n", __func__);
1106 goto _exit;
1107 }
1108
1109 /*status check*/
1110 if (mr_ctl->is_sb) {
1111 if (band_idx == 1) {
1112 PHL_ERR("wrole:%d in band_idx:%d\n", wifi_role->id, band_idx);
1113 goto _exit;
1114 }
1115 }
1116
1117
1118 is_ch_grouped = phl_chanctx_add(phl_info, wifi_role, chan, bw, offset);
1119 if (is_ch_grouped) {
1120 chanctx_num = phl_mr_get_chanctx_num(phl_info, band_ctrl);
1121 if (chanctx_num == 2) {
1122 band_ctrl->op_mode = MR_OP_MCC;
1123 } else if (chanctx_num == 1) {
1124 struct rtw_chan_ctx *chanctx = NULL;
1125 struct phl_queue *chan_ctx_queue = &band_ctrl->chan_ctx_queue;
1126
1127 _os_spinlock(drv, &chan_ctx_queue->lock, _ps, NULL);
1128 chanctx = list_first_entry(&chan_ctx_queue->queue,
1129 struct rtw_chan_ctx, list);
1130 chctx_role_num = phl_chanctx_get_rnum(phl_info, chan_ctx_queue, chanctx);
1131 if (chctx_role_num >= 2)
1132 band_ctrl->op_mode = MR_OP_SCC;
1133 else
1134 band_ctrl->op_mode = MR_OP_NON;
1135 _os_spinunlock(drv, &chan_ctx_queue->lock, _ps, NULL);
1136 }
1137 }
1138 #ifdef CONFIG_DBCC_SUPPORT
1139 else {
1140 if ((phl_com->dev_cap.hw_sup_flags & HW_SUP_DBCC) && (phl_com->dev_cap.dbcc_sup)) {
1141 /*TODO - info core layer */
1142 }
1143 }
1144 #endif
1145 _exit:
1146 PHL_DUMP_MR_EX(phl_info);
1147 return is_ch_grouped;
1148 }
1149
1150 enum rtw_phl_status
rtw_phl_chanctx_del_no_self(void * phl,struct rtw_wifi_role_t * wifi_role)1151 rtw_phl_chanctx_del_no_self(void *phl, struct rtw_wifi_role_t *wifi_role)
1152 {
1153 enum rtw_phl_status phl_sts = RTW_PHL_STATUS_FAILURE;
1154 struct phl_info_t *phl_info = (struct phl_info_t *)phl;
1155 struct rtw_phl_com_t *phl_com = phl_info->phl_com;
1156 void *drv = phl_to_drvpriv(phl_info);
1157 struct mr_ctl_t *mr_ctl = phlcom_to_mr_ctrl(phl_com);
1158 struct hw_band_ctl_t *band_ctrl = &(mr_ctl->band_ctrl[wifi_role->hw_band]);
1159 struct rtw_chan_ctx *chanctx = NULL;
1160 int chctx_num = 0;
1161
1162 chctx_num = phl_mr_get_chanctx_num(phl_info, band_ctrl);
1163 if (chctx_num > 2) {
1164 PHL_ERR("%s ERR - chanctx_num(%d) > 2\n", __func__, chctx_num);
1165 _os_warn_on(1);
1166 goto _exit;
1167 }
1168
1169 if (chctx_num == 0) {
1170 phl_sts = RTW_PHL_STATUS_SUCCESS;
1171 PHL_INFO("%s - chctx_num = 0\n", __func__);
1172 goto _exit;
1173 }
1174 else if (chctx_num == 1) { /*SCC*/
1175 _os_spinlock(drv, &band_ctrl->chan_ctx_queue.lock, _ps, NULL);
1176 if (!list_empty(&band_ctrl->chan_ctx_queue.queue)) {
1177 chanctx = list_first_entry(&band_ctrl->chan_ctx_queue.queue,
1178 struct rtw_chan_ctx, list);
1179 phl_sts = _phl_chanctx_del(phl_info, &band_ctrl->chan_ctx_queue, chanctx);
1180 if (phl_sts != RTW_PHL_STATUS_SUCCESS) {
1181 PHL_ERR("_phl_chanctx_del failed\n");
1182 _os_warn_on(1);
1183 }
1184 }
1185 _os_spinunlock(drv, &band_ctrl->chan_ctx_queue.lock, _ps, NULL);
1186 _os_kmem_free(drv, chanctx, sizeof(struct rtw_chan_ctx));
1187 }
1188 else if (chctx_num == 2) { /*MCC*/
1189 }
1190
1191 _exit:
1192 PHL_DUMP_MR_EX(phl_info);
1193 return phl_sts;
1194 }
1195
rtw_phl_chanctx_del(void * phl,struct rtw_wifi_role_t * wifi_role,struct rtw_chan_def * chan_def)1196 int rtw_phl_chanctx_del(void *phl, struct rtw_wifi_role_t *wifi_role,
1197 struct rtw_chan_def *chan_def)
1198 {
1199 enum rtw_phl_status phl_sts = RTW_PHL_STATUS_FAILURE;
1200 struct phl_info_t *phl_info = (struct phl_info_t *)phl;
1201 struct rtw_phl_com_t *phl_com = phl_info->phl_com;
1202 void *drv = phl_to_drvpriv(phl_info);
1203 struct mr_ctl_t *mr_ctl = phlcom_to_mr_ctrl(phl_com);
1204 struct hw_band_ctl_t *band_ctrl = &(mr_ctl->band_ctrl[wifi_role->hw_band]);
1205 struct phl_queue *chan_ctx_queue = &band_ctrl->chan_ctx_queue;
1206 struct rtw_chan_ctx *target_chanctx = NULL;
1207 struct rtw_chan_ctx *chanctx = NULL;
1208 int chctx_num = 0;
1209 u8 chctx_role_num = 0;
1210 u8 band_role_num = 0;
1211
1212 if (wifi_role == NULL) {
1213 PHL_ERR("%s wifi_role == NULL!!\n", __func__);
1214 /*_os_warn_on(1);*/
1215 goto _exit;
1216 }
1217
1218 target_chanctx = wifi_role->chanctx;
1219 if (target_chanctx == NULL) {
1220 PHL_ERR("%s wifi_role->chanctx == NULL\n", __func__);
1221 /*_os_warn_on(1);*/
1222 goto _exit;
1223 }
1224 /*init chan_def*/
1225 if (chan_def)
1226 chan_def->chan = 0;
1227
1228 chctx_num = phl_mr_get_chanctx_num(phl_info, band_ctrl);
1229 band_role_num = phl_mr_get_role_num(phl_info, band_ctrl);
1230
1231 chctx_role_num = phl_chanctx_get_rnum_with_lock(phl_info, chan_ctx_queue, target_chanctx);
1232
1233 if (chctx_num == 0 || chctx_role_num == 0) {
1234 PHL_ERR("%s ERR - chanctx_num(%d), role_num(%d)\n", __func__, chctx_num, chctx_role_num);
1235 _os_warn_on(1);
1236 goto _exit;
1237 }
1238 if (chctx_num > 2) {
1239 PHL_ERR("%s ERR - chanctx_num(%d) > 2\n", __func__, chctx_num);
1240 _os_warn_on(1);
1241 goto _exit;
1242 }
1243
1244 if (chctx_role_num == 1) { /*single role on this chctx*/
1245 _os_spinlock(drv, &chan_ctx_queue->lock, _ps, NULL);
1246 phl_sts = _phl_chanctx_rmap_clr(phl_info, wifi_role,
1247 chan_ctx_queue, target_chanctx);
1248 if (phl_sts != RTW_PHL_STATUS_SUCCESS)
1249 PHL_ERR("_phl_chanctx_rmap_clr failed\n");
1250
1251 phl_sts = _phl_chanctx_del(phl_info, chan_ctx_queue, target_chanctx);
1252 if (phl_sts != RTW_PHL_STATUS_SUCCESS)
1253 PHL_ERR("_phl_chanctx_del failed\n");
1254 _os_spinunlock(drv, &chan_ctx_queue->lock, _ps, NULL);
1255
1256 _os_kmem_free(drv, target_chanctx, sizeof(struct rtw_chan_ctx));
1257
1258 } else { /*multi roles on this chctx*/
1259 phl_sts = _phl_chanctx_rmap_clr_with_lock(phl_info, wifi_role,
1260 chan_ctx_queue, target_chanctx);
1261 if (phl_sts != RTW_PHL_STATUS_SUCCESS)
1262 PHL_ERR("_phl_chanctx_rmap_clr_with_lock failed\n");
1263
1264 phl_sts = phl_mr_chandef_upt(phl_info, band_ctrl, target_chanctx);
1265 if (phl_sts != RTW_PHL_STATUS_SUCCESS) {
1266 PHL_ERR("phl_mr_chandef_upt failed\n");
1267 _os_warn_on(1);
1268 goto _exit;
1269 }
1270 }
1271
1272 chctx_num = phl_mr_get_chanctx_num(phl_info, band_ctrl);
1273 if (chctx_num == 0) {
1274 band_ctrl->op_mode = MR_OP_NON;
1275 }
1276 else if (chctx_num == 1) {
1277 _os_spinlock(drv, &chan_ctx_queue->lock, _ps, NULL);
1278 chanctx = list_first_entry(&chan_ctx_queue->queue,
1279 struct rtw_chan_ctx, list);
1280 chctx_role_num = phl_chanctx_get_rnum(phl_info, chan_ctx_queue, chanctx);
1281 if (chan_def)
1282 _os_mem_cpy(drv, chan_def, &chanctx->chan_def, sizeof(struct rtw_chan_def));
1283 _os_spinunlock(drv, &chan_ctx_queue->lock, _ps, NULL);
1284
1285 #ifdef DBG_PHL_MR
1286 if (chctx_role_num == 0) {
1287 PHL_ERR("chctx_num=1, chctx_role_num=0\n");
1288 _os_warn_on(1);
1289 }
1290 #endif
1291 band_ctrl->op_mode = (chctx_role_num == 1) ? MR_OP_NON : MR_OP_SCC;
1292 } else if (chctx_num == 2) {
1293 if (chan_def)
1294 _os_mem_cpy(drv, chan_def, &target_chanctx->chan_def, sizeof(struct rtw_chan_def));
1295 band_ctrl->op_mode = MR_OP_MCC;
1296 }
1297
1298 phl_sts = RTW_PHL_STATUS_SUCCESS;
1299 PHL_INFO("%s - Bidx(%d) - Total role_num:%d, chctx_num:%d, target-chctx rnum:%d, op_mode:%d\n",
1300 __func__, band_ctrl->id, band_role_num, chctx_num, chctx_role_num, band_ctrl->op_mode);
1301
1302 _exit:
1303 PHL_DUMP_MR_EX(phl_info);
1304 return chctx_num;
1305 }
1306
1307 #ifdef PHL_MR_PROC_CMD
rtw_phl_chanctx_test(void * phl,struct rtw_wifi_role_t * wifi_role,bool is_add,u8 * chan,enum channel_width * bw,enum chan_offset * offset)1308 bool rtw_phl_chanctx_test(void *phl, struct rtw_wifi_role_t *wifi_role, bool is_add,
1309 u8 *chan, enum channel_width *bw, enum chan_offset *offset)
1310 {
1311 bool rst = true;
1312 int chanctx_num = 0;
1313 struct rtw_chan_def chan_def = {0};
1314
1315 if (is_add) {
1316 rst = rtw_phl_chanctx_add(phl, wifi_role, chan, bw, offset);
1317 }
1318 else {
1319 chanctx_num = rtw_phl_chanctx_del(phl, wifi_role, &chan_def);
1320 PHL_ERR("%s chctx_num = %d\n", __func__, chanctx_num);
1321 PHL_DUMP_CHAN_DEF(&chan_def);
1322 }
1323 return rst;
1324 }
1325 #endif
rtw_phl_get_band_type(u8 chan)1326 enum band_type rtw_phl_get_band_type(u8 chan)
1327 {
1328 /*TODO - BAND_ON_6G*/
1329 return (chan > 14) ? BAND_ON_5G : BAND_ON_24G;
1330 }
rtw_phl_get_center_ch(u8 ch,enum channel_width bw,enum chan_offset offset)1331 u8 rtw_phl_get_center_ch(u8 ch,
1332 enum channel_width bw, enum chan_offset offset)
1333 {
1334 u8 cch = ch;
1335
1336 if (bw == CHANNEL_WIDTH_160) {
1337 if (ch % 4 == 0) {
1338 if (ch >= 36 && ch <= 64)
1339 cch = 50;
1340 else if (ch >= 100 && ch <= 128)
1341 cch = 114;
1342 } else if (ch % 4 == 1) {
1343 if (ch >= 149 && ch <= 177)
1344 cch = 163;
1345 }
1346
1347 } else if (bw == CHANNEL_WIDTH_80) {
1348 if (ch <= 14)
1349 cch = 7; /* special case for 2.4G */
1350 else if (ch % 4 == 0) {
1351 if (ch >= 36 && ch <= 48)
1352 cch = 42;
1353 else if (ch >= 52 && ch <= 64)
1354 cch = 58;
1355 else if (ch >= 100 && ch <= 112)
1356 cch = 106;
1357 else if (ch >= 116 && ch <= 128)
1358 cch = 122;
1359 else if (ch >= 132 && ch <= 144)
1360 cch = 138;
1361 } else if (ch % 4 == 1) {
1362 if (ch >= 149 && ch <= 161)
1363 cch = 155;
1364 else if (ch >= 165 && ch <= 177)
1365 cch = 171;
1366 }
1367
1368 } else if (bw == CHANNEL_WIDTH_40) {
1369 if (offset == CHAN_OFFSET_UPPER)
1370 cch = ch + 2;
1371 else if (offset == CHAN_OFFSET_LOWER)
1372 cch = ch - 2;
1373
1374 } else if (bw == CHANNEL_WIDTH_20
1375 || bw == CHANNEL_WIDTH_10
1376 || bw == CHANNEL_WIDTH_5) {
1377 ; /* the same as ch */
1378 }
1379 else {
1380 PHL_ERR("%s failed\n", __func__);
1381 }
1382 return cch;
1383 }
1384
1385 /*
1386 * Refer to 80211 spec Annex E Table E-4 Global operating classes
1387 * Handle 2.4G/5G Bandwidth 20/40/80/160
1388 */
1389 u8
rtw_phl_get_operating_class(struct rtw_chan_def chan_def)1390 rtw_phl_get_operating_class(
1391 struct rtw_chan_def chan_def
1392 )
1393 {
1394 u8 operating_class = 0;
1395
1396 if(chan_def.bw == CHANNEL_WIDTH_20){
1397 if(chan_def.chan <= 13)
1398 operating_class = 81;
1399 else if(chan_def.chan ==14)
1400 operating_class = 82;
1401 else if(chan_def.chan >= 36 && chan_def.chan <= 48)
1402 operating_class = 115;
1403 else if(chan_def.chan >= 52 && chan_def.chan <= 64)
1404 operating_class = 118;
1405 else if(chan_def.chan >= 100 && chan_def.chan <= 144)
1406 operating_class = 121;
1407 else if(chan_def.chan >= 149 && chan_def.chan <= 169)
1408 operating_class = 125;
1409 else
1410 PHL_WARN("%s: Undefined channel (%d)\n", __FUNCTION__, chan_def.chan);
1411 }
1412 else if(chan_def.bw == CHANNEL_WIDTH_40){
1413 if(chan_def.offset == CHAN_OFFSET_UPPER){
1414 if(chan_def.chan >= 1 && chan_def.chan <= 9)
1415 operating_class = 83;
1416 else if(chan_def.chan == 36 || chan_def.chan == 44)
1417 operating_class = 116;
1418 else if(chan_def.chan == 52 || chan_def.chan == 60)
1419 operating_class = 119;
1420 else if(chan_def.chan == 100 || chan_def.chan == 108 ||
1421 chan_def.chan == 116 || chan_def.chan == 124 ||
1422 chan_def.chan == 132 || chan_def.chan == 140)
1423 operating_class = 122;
1424 else if(chan_def.chan == 149 || chan_def.chan == 157)
1425 operating_class = 126;
1426 else
1427 PHL_WARN("%s: Undefined channel (%d)\n", __FUNCTION__, chan_def.chan);
1428 }
1429 else if(chan_def.offset == CHAN_OFFSET_LOWER){
1430 if(chan_def.chan >= 5 && chan_def.chan <= 13)
1431 operating_class = 84;
1432 else if(chan_def.chan == 40 || chan_def.chan == 48)
1433 operating_class = 117;
1434 else if(chan_def.chan == 56 || chan_def.chan == 64)
1435 operating_class = 120;
1436 else if(chan_def.chan == 104 || chan_def.chan == 112 ||
1437 chan_def.chan == 120 || chan_def.chan == 128 ||
1438 chan_def.chan == 136 || chan_def.chan == 144)
1439 operating_class = 123;
1440 else if(chan_def.chan == 153 || chan_def.chan == 161)
1441 operating_class = 127;
1442 else
1443 PHL_WARN("%s: Undefined channel (%d)\n", __FUNCTION__, chan_def.chan);
1444 }
1445 else{
1446 PHL_WARN("%s: Invalid offset(%d)\n",
1447 __FUNCTION__, chan_def.offset);
1448 }
1449 }
1450 else if(chan_def.bw == CHANNEL_WIDTH_80){
1451 if(chan_def.center_ch == 42 || chan_def.center_ch == 58 ||
1452 chan_def.center_ch == 106 || chan_def.center_ch == 122 ||
1453 chan_def.center_ch == 138 || chan_def.center_ch == 155)
1454 operating_class = 128;
1455 else
1456 PHL_WARN("%s: Undefined channel (%d)\n", __FUNCTION__, chan_def.center_ch);
1457 }
1458 else if(chan_def.bw == CHANNEL_WIDTH_160){
1459 if(chan_def.center_ch == 50 || chan_def.center_ch == 114)
1460 operating_class = 129;
1461 else
1462 PHL_WARN("%s: Undefined channel (%d)\n", __FUNCTION__, chan_def.center_ch);
1463 }
1464 else{
1465 PHL_WARN("%s: Not handle bandwidth (%d)\n", __FUNCTION__, chan_def.bw);
1466 }
1467
1468 return operating_class;
1469 }
1470
1471 bool
rtw_phl_get_chandef_from_operating_class(u8 channel,u8 operating_class,struct rtw_chan_def * chan_def)1472 rtw_phl_get_chandef_from_operating_class(
1473 u8 channel,
1474 u8 operating_class,
1475 struct rtw_chan_def *chan_def
1476 )
1477 {
1478 bool ret = true;
1479 if(operating_class == 81 || operating_class == 82 ||
1480 operating_class == 115 || operating_class == 118 ||
1481 operating_class == 118 || operating_class == 121 ||
1482 operating_class == 125){
1483 chan_def->chan = channel;
1484 chan_def->bw = CHANNEL_WIDTH_20;
1485 chan_def->offset = CHAN_OFFSET_NO_EXT;
1486 chan_def->center_ch = rtw_phl_get_center_ch(channel,
1487 CHANNEL_WIDTH_20,
1488 CHAN_OFFSET_NO_EXT);
1489 }
1490 else if(operating_class == 83 || operating_class == 116 ||
1491 operating_class == 119 || operating_class == 122 ||
1492 operating_class == 126){
1493 chan_def->chan = channel;
1494 chan_def->bw = CHANNEL_WIDTH_40;
1495 chan_def->offset = CHAN_OFFSET_UPPER;
1496 chan_def->center_ch = rtw_phl_get_center_ch(channel,
1497 CHANNEL_WIDTH_40,
1498 CHAN_OFFSET_UPPER);
1499 }
1500 else if(operating_class == 84 || operating_class == 117 ||
1501 operating_class == 120 || operating_class == 123 ||
1502 operating_class == 127){
1503 chan_def->chan = channel;
1504 chan_def->bw = CHANNEL_WIDTH_40;
1505 chan_def->offset = CHAN_OFFSET_LOWER;
1506 chan_def->center_ch = rtw_phl_get_center_ch(channel,
1507 CHANNEL_WIDTH_40,
1508 CHAN_OFFSET_LOWER);
1509 }
1510 else if(operating_class == 128){
1511 chan_def->chan = channel;
1512 chan_def->bw = CHANNEL_WIDTH_80;
1513 chan_def->offset = CHAN_OFFSET_NO_DEF;
1514 chan_def->center_ch = rtw_phl_get_center_ch(channel,
1515 CHANNEL_WIDTH_80,
1516 CHAN_OFFSET_NO_DEF);
1517 }
1518 else if(operating_class == 129){
1519 chan_def->chan = channel;
1520 chan_def->bw = CHANNEL_WIDTH_160;
1521 chan_def->offset = CHAN_OFFSET_NO_DEF;
1522 chan_def->center_ch = rtw_phl_get_center_ch(channel,
1523 CHANNEL_WIDTH_40,
1524 CHAN_OFFSET_NO_DEF);
1525 }
1526 else{
1527 PHL_ERR("%s: Unknown operating class (%d)\n", __FUNCTION__, operating_class);
1528 ret = false;
1529 }
1530
1531 return ret;
1532 }