hc
2024-12-19 9370bb92b2d16684ee45cf24e879c93c509162da
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
/******************************************************************************
 *
 * Copyright(c) 2019 Realtek Corporation.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of version 2 of the GNU General Public License as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 *****************************************************************************/
#define _PHL_CMD_GENERAL_C_
#include "phl_headers.h"
 
#ifdef CONFIG_CMD_DISP
 
enum phl_cmd_sts {
   PHL_CMD_SUBMITTED = -1,
   PHL_CMD_DONE_SUCCESS = 0,
   PHL_CMD_DONE_TIMEOUT,
   PHL_CMD_DONE_CMD_ERROR,
   PHL_CMD_DONE_CMD_DROP,
   PHL_CMD_DONE_CANNOT_IO,
   PHL_CMD_DONE_UNKNOWN,
};
 
struct phl_sync {
   u32 submit_time;
   u32 timeout_ms; /* 0: wait forever, >0: up to ms waiting */
   enum phl_cmd_sts status; /* status for operation */
   _os_event done;
};
 
struct phl_cmd_sync {
   enum phl_msg_evt_id evt_id;
   struct phl_sync sync;
   _os_lock lock;
};
 
struct phl_cmd_obj {
   enum phl_msg_evt_id evt_id; /* u8 id */
   u8 *buf;
   u32 buf_len;
   bool no_io;
   void (*cmd_complete)(void* priv, u8 *buf, u32 buf_len, enum rtw_phl_status status);
   /*cmd sync*/
   bool is_cmd_wait;
   _os_atomic ref_cnt;
   struct phl_cmd_sync cmd_sync;
};
 
 
/*#define DBG_CMD_SYNC*/
 
#ifdef DBG_CMD_SYNC
static void _phl_cmd_sync_dump(struct phl_cmd_sync *cmd_sync, const char *caller)
{
   PHL_INFO("[CMD_SYNC] %s\n", caller);
   PHL_INFO("[CMD_SYNC] evt_id:%d status:%d\n", cmd_sync->evt_id, cmd_sync->sync.status);
   PHL_INFO("[CMD_SYNC] take:%d ms\n", phl_get_passing_time_ms(cmd_sync->sync.submit_time));
}
#endif
 
static void _phl_cmd_sync_init(struct phl_info_t *phl_info,
       enum phl_msg_evt_id evt_id,
       struct phl_cmd_sync *cmd_sync, u32 timeout_ms)
{
   void *drv = phl_to_drvpriv(phl_info);
 
   _os_spinlock_init(drv, &cmd_sync->lock);
   cmd_sync->evt_id = evt_id;
   cmd_sync->sync.timeout_ms = timeout_ms;
   cmd_sync->sync.submit_time = _os_get_cur_time_ms();
   _os_event_init(drv, &(cmd_sync->sync.done));
   cmd_sync->sync.status = PHL_CMD_SUBMITTED;
}
 
static void _phl_cmd_sync_deinit(struct phl_info_t *phl_info,
           struct phl_cmd_sync *cmd_sync)
{
   #ifdef DBG_CMD_SYNC
   _phl_cmd_sync_dump(cmd_sync, __func__);
   #endif
   _os_event_free(phl_to_drvpriv(phl_info), &(cmd_sync->sync.done));
   _os_spinlock_free(phl_to_drvpriv(phl_info), &cmd_sync->lock);
}
 
inline static enum rtw_phl_status _cmd_stat_2_phl_stat(enum phl_cmd_sts status)
{
   if (status == PHL_CMD_DONE_TIMEOUT)
       return RTW_PHL_STATUS_CMD_TIMEOUT;
   else if(status == PHL_CMD_DONE_CANNOT_IO)
       return RTW_PHL_STATUS_CMD_CANNOT_IO;
   else if (status == PHL_CMD_DONE_CMD_ERROR)
       return RTW_PHL_STATUS_CMD_ERROR;
   else if (status == PHL_CMD_DONE_CMD_DROP)
       return RTW_PHL_STATUS_CMD_DROP;
   else
       return RTW_PHL_STATUS_CMD_SUCCESS;
}
 
static enum rtw_phl_status
_phl_cmd_wait(struct phl_info_t *phl_info, struct phl_cmd_sync *cmd_sync)
{
   void *drv = phl_to_drvpriv(phl_info);
   u32 cmd_wait_ms = cmd_sync->sync.timeout_ms;/*0: wait forever, >0: up to ms waiting*/
 
   #ifdef DBG_CMD_SYNC
   PHL_INFO("evt_id:%d %s in...............\n", cmd_sync->evt_id, __func__);
   #endif
 
   if (_os_event_wait(drv, &cmd_sync->sync.done, cmd_wait_ms) == 0) {
       _os_spinlock(drv, &cmd_sync->lock, _bh, NULL);
       cmd_sync->sync.status = PHL_CMD_DONE_TIMEOUT;
       _os_spinunlock(drv, &cmd_sync->lock, _bh, NULL);
       PHL_ERR("%s evt_id:%d timeout\n", __func__, cmd_sync->evt_id);
   }
   #ifdef DBG_CMD_SYNC
   PHL_INFO("evt_id:%d %s out...............\n", cmd_sync->evt_id, __func__);
   _phl_cmd_sync_dump(cmd_sync, __func__);
   #endif
   return _cmd_stat_2_phl_stat(cmd_sync->sync.status);
}
 
static bool _phl_cmd_chk_wating_status(enum phl_cmd_sts status)
{
   switch (status) {
   case PHL_CMD_SUBMITTED:
       /* fall through */
   case PHL_CMD_DONE_UNKNOWN:
       return true;
   default:
       return false;
   }
}
 
static void _phl_cmd_done(struct phl_info_t *phl_info,
       struct phl_cmd_sync *cmd_sync, enum phl_cmd_sts status)
{
   void *drv = phl_to_drvpriv(phl_info);
 
   if (!cmd_sync) {
       PHL_ERR("%s cmd_sync is NULL\n", __func__);
       _os_warn_on(1);
       return;
   }
   #ifdef DBG_CMD_SYNC
   PHL_INFO("evt_id:%d %s in...............\n", cmd_sync->evt_id, __func__);
   #endif
   _os_spinlock(drv, &cmd_sync->lock, _bh, NULL);
   if (_phl_cmd_chk_wating_status(cmd_sync->sync.status)) {
       PHL_INFO("%s status:%d\n", __func__, status);
       cmd_sync->sync.status = status;
   }
   _os_spinunlock(drv, &cmd_sync->lock, _bh, NULL);
 
   _os_event_set(drv, &cmd_sync->sync.done);
   #ifdef DBG_CMD_SYNC
   PHL_INFO("evt_id:%d %s out...............\n", cmd_sync->evt_id, __func__);
   _phl_cmd_sync_dump(cmd_sync, __func__);
   #endif
}
/********************************************************/
static enum rtw_phl_status
_phl_cmd_general_pre_phase_msg_hdlr(struct phl_info_t *phl_info, void *dispr,
                   struct phl_msg *msg)
{
   enum phl_msg_evt_id evt_id = MSG_EVT_ID_FIELD(msg->msg_id);
   enum rtw_phl_status psts = RTW_PHL_STATUS_FAILURE;
   struct phl_cmd_obj *phl_cmd = NULL;
 
   phl_cmd = (struct phl_cmd_obj *)msg->inbuf;
 
   switch (evt_id) {
   case MSG_EVT_DBG_RX_DUMP:
       /* Do Nothing */
       psts = RTW_PHL_STATUS_SUCCESS;
   break;
   case MSG_EVT_DBG_TX_DUMP:
       /* Do Nothing */
       psts = RTW_PHL_STATUS_SUCCESS;
   break;
   case MSG_EVT_NONE:
       /* fall through */
   default:
       psts = RTW_PHL_STATUS_SUCCESS;
       break;
   }
 
   return psts;
}
 
static u8 _skip_normal_hw_watchdog(struct phl_info_t *phl_info)
{
   struct rtw_phl_com_t *phl_com = phl_info->phl_com;
#ifdef CONFIG_PHL_CHSWOFLD
   struct chsw_ofld_info_t *chsw_ofld_info = &phl_com->chsw_ofld_info;
 
   if (chsw_ofld_info->chsw_ofld_en && chsw_ofld_info->skip_normal_watchdog)
       return true;
#endif
 
   return false;
}
 
static enum rtw_phl_status
_phl_cmd_general_post_phase_msg_hdlr(struct phl_info_t *phl_info, void *dispr,
                    struct phl_msg *msg)
{
   enum rtw_phl_status psts = RTW_PHL_STATUS_FAILURE;
   enum phl_msg_evt_id evt_id = MSG_EVT_ID_FIELD(msg->msg_id);
   struct phl_cmd_obj *phl_cmd = NULL;
 
   phl_cmd = (struct phl_cmd_obj *)msg->inbuf;
 
   switch (evt_id) {
#ifdef CONFIG_PHL_P2PPS
   case MSG_EVT_NOA_UP:
       PHL_TRACE(COMP_PHL_P2PPS, _PHL_INFO_, "[NOA]_phl_cmd_general_post_phase_msg_hdlr: MSG_EVT_NOA_UP\n");
       psts = phl_noa_update(phl_info,
               (struct rtw_phl_noa_desc *)phl_cmd->buf);
   break;
 
   case MSG_EVT_NOA_DISABLE:
       PHL_TRACE(COMP_PHL_P2PPS, _PHL_INFO_, "[NOA]_phl_cmd_general_post_phase_msg_hdlr: MSG_EVT_NOA_DISABLE\n");
       psts = phl_cmd_noa_disable_hdl(phl_info, phl_cmd->buf);
   break;
#endif /* CONFIG_PHL_P2PPS */
 
   case MSG_EVT_CHG_OP_CH_DEF_START:
       psts = phl_cmd_chg_op_chdef_start_hdl(phl_info, phl_cmd->buf);
   break;
 
   case MSG_EVT_SWCH_START:
       psts = phl_cmd_set_ch_bw_hdl(phl_info, phl_cmd->buf);
   break;
 
   #if defined(CONFIG_PCI_HCI) && defined(PCIE_TRX_MIT_EN)
   case MSG_EVT_PCIE_TRX_MIT:
       psts = phl_evt_pcie_trx_mit_hdlr(phl_info, phl_cmd->buf);
   break;
   #endif
   case MSG_EVT_DBG_TX_DUMP:
   {
       struct hal_mac_dbg_dump_cfg cfg = {0};
 
       PHL_TRACE(COMP_PHL_DBG, _PHL_INFO_, "===> %s : MSG_EVT_DBG_TX_DUMP\n", __func__);
       RTW_DUMP_HAL_CR(phl_info, MSG_EVT_DBG_TX_DUMP, msg->band_idx);
       cfg.tx_flow_dbg = 1;
       rtw_hal_dbg_status_dump(phl_info->hal, &cfg);
       PHL_TRACE(COMP_PHL_DBG, _PHL_INFO_, "<=== %s : MSG_EVT_DBG_TX_DUMP\n", __func__);
       psts = RTW_PHL_STATUS_SUCCESS;
   }
   break;
   case MSG_EVT_DBG_RX_DUMP:
   {
       PHL_TRACE(COMP_PHL_DBG, _PHL_INFO_, "===> %s : MSG_EVT_DBG_RX_DUMP\n", __func__);
       RTW_DUMP_HAL_CR(phl_info, MSG_EVT_DBG_RX_DUMP, msg->band_idx);
       PHL_TRACE(COMP_PHL_DBG, _PHL_INFO_, "<=== %s : MSG_EVT_DBG_RX_DUMP\n", __func__);
       psts = RTW_PHL_STATUS_SUCCESS;
   }
   break;
   case MSG_EVT_SW_WATCHDOG:
       psts = phl_watchdog_sw_cmd_hdl(phl_info, psts);
   break;
   case MSG_EVT_HW_WATCHDOG:
   {
       if (_skip_normal_hw_watchdog(phl_info)) {
           rtw_hal_simple_watchdog(phl_info->hal, false);
           psts = RTW_PHL_STATUS_SUCCESS;
       } else {
#ifdef CONFIG_POST_CORE_KEEP_ALIVE
           psts = phl_keep_alive_hdl(phl_info);
#endif
           psts = phl_watchdog_hw_cmd_hdl(phl_info, psts);
       }
   }
   break;
 
#if defined(CONFIG_USB_HCI)
   case MSG_EVT_FORCE_USB_SW:
       psts = phl_force_usb_switch(phl_info, *(u32*)(phl_cmd->buf));
   break;
   case MSG_EVT_GET_USB_SPEED:
       psts = phl_get_cur_usb_speed(phl_info, (u32*)(phl_cmd->buf));
   break;
   case MSG_EVT_GET_USB_SW_ABILITY:
       psts = phl_get_usb_support_ability(phl_info, (u32*)(phl_cmd->buf));
   break;
#endif
   case MSG_EVT_HWSEQ_GET_HW_SEQUENCE:
       psts = phl_cmd_get_hwseq_hdl(phl_info, phl_cmd->buf);
   break;
   case MSG_EVT_CFG_AMPDU:
       psts = phl_cmd_cfg_ampdu_hdl(phl_info, phl_cmd->buf);
   break;
   case MSG_EVT_CFG_AMSDU_TX:
       psts = phl_cmd_cfg_amsdu_tx_hdl(phl_info, phl_cmd->buf);
   break;
   case MSG_EVT_UPDT_EXT_TXPWR_LMT:
       psts = phl_cmd_updt_ext_txpwr_lmt(phl_info, phl_cmd->buf);
   break;
   case MSG_EVT_DFS_PAUSE_TX:
       psts = phl_cmd_dfs_tx_pause_hdl(phl_info, phl_cmd->buf);
   break;
 
   case MSG_EVT_ROLE_RECOVER:
       psts = phl_role_recover(phl_info);
   break;
 
   case MSG_EVT_ROLE_SUSPEND:
   {
       enum phl_role_susp_rsn *rsn = (enum phl_role_susp_rsn *)phl_cmd->buf;
 
       psts = phl_role_suspend(phl_info, *rsn);
   }
   break;
 
#if defined(CONFIG_PCI_HCI)
   case MSG_EVT_HAL_SET_L2_LEAVE:
       if (rtw_hal_set_l2_leave(phl_info->hal) == RTW_HAL_STATUS_SUCCESS)
           psts = RTW_PHL_STATUS_SUCCESS;
   break;
#endif
 
   case MSG_EVT_GET_TX_PWR_DBM:
   {
       psts = rtw_phl_get_txinfo_pwr((void*)phl_info, (s16*)(phl_cmd->buf));
   }
   break;
 
   case MSG_EVT_NOTIFY_HAL:
       psts = phl_notify_cmd_hdl(phl_info, phl_cmd->buf);
   break;
 
   case MSG_EVT_ISSUE_BCN:
#ifdef RTW_PHL_BCN
       psts = phl_cmd_issue_bcn_hdl(phl_info, phl_cmd->buf);
#endif
   break;
   case MSG_EVT_EDCCA_CFG:
       psts = phl_cmd_edcca_cfg_hdl(phl_info, phl_cmd->buf);
   break;
   case MSG_EVT_STOP_BCN:
#ifdef RTW_PHL_BCN
       psts = phl_cmd_stop_bcn_hdl(phl_info, phl_cmd->buf);
#endif
   break;
 
   case MSG_EVT_SEC_KEY:
       psts = phl_cmd_set_key_hdl(phl_info, phl_cmd->buf);
   break;
 
   case MSG_EVT_ROLE_START:
       psts = phl_wifi_role_start_hdl(phl_info, phl_cmd->buf);
   break;
 
   case MSG_EVT_ROLE_CHANGE:
       psts = phl_wifi_role_chg_hdl(phl_info, phl_cmd->buf);
   break;
 
   case MSG_EVT_ROLE_STOP:
       psts = phl_wifi_role_stop_hdl(phl_info, phl_cmd->buf);
   break;
 
   case MSG_EVT_STA_INFO_CTRL:
       psts = phl_cmd_alloc_stainfo_hdl(phl_info, phl_cmd->buf);
   break;
 
   case MSG_EVT_STA_MEDIA_STATUS_UPT:
       psts = phl_update_media_status_hdl(phl_info, phl_cmd->buf);
   break;
 
   case MSG_EVT_CFG_CHINFO:
#ifdef CONFIG_PHL_CHANNEL_INFO
       psts = phl_cmd_cfg_chinfo_hdl(phl_info, phl_cmd->buf);
#endif
   break;
 
   case MSG_EVT_STA_CHG_STAINFO:
       psts = phl_cmd_change_stainfo_hdl(phl_info, phl_cmd->buf);
   break;
 
   case MSG_EVT_TPE_INFO_UPDATE:
       psts = phl_cmd_tpe_update_hdl(phl_info, phl_cmd->buf);
   break;
 
#ifdef CONFIG_PHL_TWT
   case MSG_EVT_TWT_STA_ACCEPT:
       psts = phl_twt_accept_for_sta_mode(phl_info, phl_cmd->buf);
   break;
 
   case MSG_EVT_TWT_STA_TEARDOWN:
       psts = phl_twt_teardown_for_sta_mode(phl_info, phl_cmd->buf);
   break;
 
   case MSG_EVT_TWT_GET_TWT:
       psts = phl_twt_get_target_wake_time(phl_info, phl_cmd->buf);
   break;
 
   case MSG_EVT_TWT_INFO_F_HDR:
       psts = phl_twt_info_f_hrl(phl_info, phl_cmd->buf);
   break;
#endif
 
   case MSG_EVT_GET_CUR_TSF:
       psts = phl_cmd_get_cur_tsf_hdl(phl_info,
           (struct rtw_phl_port_tsf *)phl_cmd->buf);
   break;
 
   case MSG_EVT_SET_MACID_PAUSE:
       psts = phl_cmd_set_macid_pause_hdl(phl_info, phl_cmd->buf);
   break;
 
   case MSG_EVT_SET_MACID_PAUSE_AC:
       psts = phl_cmd_set_macid_pause_ac_hdl(phl_info, phl_cmd->buf);
   break;
 
   case MSG_EVT_SET_MACID_PKT_DROP:
       psts = phl_cmd_set_macid_pkt_drop_hdl(phl_info, phl_cmd->buf);
   break;
 
   case MSG_EVT_GT3_SETUP:
       psts = phl_cmd_cfg_gt3_hdl(phl_info, phl_cmd->buf);
   break;
 
   case MSG_EVT_HW_SEQ_SETUP:
       psts = phl_cmd_cfg_hw_seq_hdl(phl_info, phl_cmd->buf);
   break;
 
   case MSG_EVT_LIFETIME_SETUP:
       psts = phl_cmd_cfg_lifetime_hdl(phl_info, phl_cmd->buf);
   break;
 
   case MSG_EVT_POWER_OFFSET_SETUP:
       psts = phl_cmd_cfg_power_offset_hdl(phl_info, phl_cmd->buf);
   break;
 
   case MSG_EVT_SET_UL_FIXINFO:
       psts = phl_set_fw_ul_fixinfo_hdl(phl_info,
           (struct rtw_phl_ax_ul_fixinfo *)phl_cmd->buf);
   break;
 
#ifdef CONFIG_PHL_DFS
   case MSG_EVT_DFS_RD_SETUP:
       psts = phl_cmd_dfs_rd_ctl_hdl(phl_info, phl_cmd->buf);
   break;
#endif
 
   case MSG_EVT_RX_DBG_CNT_GET_BY_IDX:
       psts = phl_cmd_get_rx_cnt_by_idx_hdl(phl_info, phl_cmd->buf);
       break;
   case MSG_EVT_RX_DBG_CNT_RESET:
       psts = phl_cmd_set_reset_rx_cnt_hdl(phl_info, phl_cmd->buf);
       break;
#ifdef CONFIG_PHL_USB_RX_AGGREGATION
   case MSG_EVT_USB_RX_AGG_CFG:
       psts = phl_cmd_usb_rx_agg_cfg_hdl(phl_info, phl_cmd->buf);
       break;
#endif
   case MSG_EVT_TXPWR_SETUP:
       psts = phl_cmd_txpwr_ctl_hdl(phl_info, phl_cmd->buf);
       break;
 
   case MSG_EVT_HW_CTS2SELF:
       psts = phl_cmd_cfg_hw_cts2self_hdl(phl_info, phl_cmd->buf);
       break;
 
   case MSG_EVT_SET_STA_SEC_IV:
       psts = phl_cmd_set_seciv_hdl(phl_info,
           phl_cmd->buf);
   break;
 
#ifdef CONFIG_DBCC_P2P_BG_LISTEN
   case MSG_EVT_CONNECT_CMD_DBCC_DIS:
       psts = phl_cmd_dbcc_dis_hdl(phl_info, phl_cmd->buf);
   break;
 
   case MSG_EVT_DISCONNECT_CMD_DBCC_EN:
       psts = phl_cmd_dbcc_en_hdl(phl_info, phl_cmd->buf);
   break;
#endif
 
   default:
       psts = RTW_PHL_STATUS_SUCCESS;
   break;
   }
 
   return psts;
}
 
static enum phl_mdl_ret_code _phl_cmd_general_init(void *phl, void *dispr,
                          void **priv)
{
   *priv = phl;
   return MDL_RET_SUCCESS;
}
 
static void _phl_cmd_general_deinit(void *dispr, void *priv)
{
 
}
 
static enum phl_mdl_ret_code _phl_cmd_general_start(void *dispr, void *priv)
{
   u8 dispr_idx = 0;
 
   if (RTW_PHL_STATUS_SUCCESS != phl_dispr_get_idx(dispr, &dispr_idx))
       return MDL_RET_FAIL;
 
   #if defined(CONFIG_PCI_HCI) && defined(PCIE_TRX_MIT_EN)
   {
       struct phl_info_t *phl_info = (struct phl_info_t *)priv;
 
       if (RTW_PHL_STATUS_SUCCESS !=
       phl_pcie_trx_mit_start(phl_info, dispr_idx))
           return MDL_RET_FAIL;
   }
   #endif
 
   return MDL_RET_SUCCESS;
}
 
static void _stop_operation_on_general(void *phl)
{
   rtw_phl_watchdog_stop(phl);
}
 
static enum phl_mdl_ret_code _phl_cmd_general_stop(void *dispr, void *priv)
{
   _stop_operation_on_general(priv);
   return MDL_RET_SUCCESS;
}
 
static void _fail_evt_hdlr(void *dispr, void *priv, struct phl_msg *msg)
{
   struct phl_info_t *phl_info = (struct phl_info_t *)priv;
   u16 evt_id = MSG_EVT_ID_FIELD(msg->msg_id);
   u8 idx = 0;
#ifdef CONFIG_POWER_SAVE
   struct phl_module_op_info op_info = {0};
#endif
 
   phl_dispr_get_idx(dispr, &idx);
 
   switch (evt_id) {
   case MSG_EVT_HW_WATCHDOG:
       /* watchdog do not need to handle fail case */
       PHL_DBG("%s do simple watchdog!\n", __func__);
       rtw_hal_simple_watchdog(phl_info->hal, false);
#ifdef CONFIG_POST_CORE_KEEP_ALIVE
       rtw_phl_set_wdog_state_keep_alive(phl_info, false, NULL);
#endif
       break;
   default:
#ifdef CONFIG_POWER_SAVE
       op_info.op_code = PS_MDL_OP_CANCEL_PWR_REQ;
       op_info.inbuf = (u8 *)&evt_id;
       phl_disp_eng_set_bk_module_info(phl_info, idx,
               PHL_MDL_POWER_MGNT, &op_info);
#endif
       PHL_ERR("%s :: MDL_ID(%d)_FAIL - MSG_EVT_ID=%d \n",
           __func__,
           MSG_MDL_ID_FIELD(msg->msg_id),
           MSG_EVT_ID_FIELD(msg->msg_id));
       break;
   }
}
 
static enum phl_mdl_ret_code _phl_cmd_general_msg_hdlr(void *dispr, void *priv,
                              struct phl_msg *msg)
{
   struct phl_info_t *phl_info = (struct phl_info_t *)priv;
   enum rtw_phl_status status = RTW_PHL_STATUS_FAILURE;
 
   if (IS_MSG_FAIL(msg->msg_id)) {
 
       _fail_evt_hdlr(dispr, priv, msg);
 
       return MDL_RET_FAIL;
   }
 
   if (MSG_MDL_ID_FIELD(msg->msg_id) != PHL_MDL_GENERAL)
       return MDL_RET_IGNORE;
 
   /*
    * GENERAL is optional module, msg pass through mandatory module,
    * optional module, and wifi role(protocol). So msg shall be handled
    * at post phase to make sure that wifi role is at valid state
    * if the msg is relative to wifi role(protocol)
    */
   if (IS_MSG_IN_PRE_PHASE(msg->msg_id))
       status =
           _phl_cmd_general_pre_phase_msg_hdlr(phl_info, dispr, msg);
   else
       status =
           _phl_cmd_general_post_phase_msg_hdlr(phl_info, dispr, msg);
 
   if (status != RTW_PHL_STATUS_SUCCESS)
       return MDL_RET_FAIL;
 
   return MDL_RET_SUCCESS;
}
 
static enum phl_mdl_ret_code
_phl_cmd_general_set_info(void *dispr, void *priv,
             struct phl_module_op_info *info)
{
   return MDL_RET_SUCCESS;
}
 
enum rtw_phl_status
phl_cmd_get_cur_cmdinfo(struct phl_info_t *phl_info,
                       u8 band_idx,
                       struct phl_msg *msg,
                       u8 **cmd,
                       u32 *cmd_len)
{
   enum rtw_phl_status psts = RTW_PHL_STATUS_FAILURE;
   struct phl_cmd_obj *phl_cmd = NULL;
 
   phl_cmd = (struct phl_cmd_obj *)msg->inbuf;
   if (NULL != phl_cmd) {
       *cmd = phl_cmd->buf;
       *cmd_len = phl_cmd->buf_len;
       psts = RTW_PHL_STATUS_SUCCESS;
   }
 
   return psts;
}
 
static enum phl_mdl_ret_code
_phl_cmd_general_query_info(void *dispr, void *priv,
               struct phl_module_op_info *info)
{
   return MDL_RET_SUCCESS;
}
 
enum rtw_phl_status phl_register_cmd_general(struct phl_info_t *phl_info)
{
   enum rtw_phl_status status = RTW_PHL_STATUS_FAILURE;
   struct phl_cmd_dispatch_engine *disp_eng = &(phl_info->disp_eng);
   struct phl_bk_module_ops bk_ops;
   u8 band_idx = 0;
 
   bk_ops.init = _phl_cmd_general_init;
   bk_ops.deinit = _phl_cmd_general_deinit;
   bk_ops.start = _phl_cmd_general_start;
   bk_ops.stop = _phl_cmd_general_stop;
   bk_ops.msg_hdlr = _phl_cmd_general_msg_hdlr;
   bk_ops.set_info = _phl_cmd_general_set_info;
   bk_ops.query_info = _phl_cmd_general_query_info;
 
   for (band_idx = 0; band_idx < disp_eng->phy_num; band_idx++) {
       status = phl_disp_eng_register_module(phl_info, band_idx,
                        PHL_MDL_GENERAL, &bk_ops);
       if (status != RTW_PHL_STATUS_SUCCESS) {
           PHL_ERR(
               "%s register MDL_GENRAL in cmd disp failed :%d\n",
               __func__, band_idx + 1);
           break;
       }
   }
 
   return status;
}
 
static enum rtw_phl_status
_phl_cmd_obj_free(struct phl_info_t *phl_info, struct phl_cmd_obj *phl_cmd)
{
   void *drv = phl_to_drvpriv(phl_info);
 
   if(phl_cmd == NULL) {
       PHL_ERR("%s phl_cmd is NULL\n", __func__);
       _os_warn_on(1);
       return RTW_PHL_STATUS_FAILURE;
   }
 
   if (phl_cmd->is_cmd_wait == true)
       _phl_cmd_sync_deinit(phl_info, &phl_cmd->cmd_sync);
   _os_kmem_free(drv, phl_cmd, sizeof(struct phl_cmd_obj));
   phl_cmd = NULL;
   return RTW_PHL_STATUS_SUCCESS;
}
 
static void _phl_cmd_complete(void *priv, struct phl_msg *msg)
{
   struct phl_info_t *phl_info = (struct phl_info_t *)priv;
   void *drv = phl_to_drvpriv(phl_info);
   struct phl_cmd_obj *phl_cmd = (struct phl_cmd_obj *)msg->inbuf;
   enum phl_cmd_sts csts = PHL_CMD_DONE_UNKNOWN;
   enum rtw_phl_status pstst = RTW_PHL_STATUS_SUCCESS;
 
   PHL_DBG("%s evt_id:%d\n", __func__, phl_cmd->evt_id);
 
   if (IS_MSG_CANNOT_IO(msg->msg_id))
       csts = PHL_CMD_DONE_CANNOT_IO;
   else if (IS_MSG_FAIL(msg->msg_id))
       csts = PHL_CMD_DONE_CMD_ERROR;
   else if (IS_MSG_CANCEL(msg->msg_id))
       csts = PHL_CMD_DONE_CMD_DROP;
   else
       csts = PHL_CMD_DONE_SUCCESS;
 
   if (phl_cmd->is_cmd_wait)
       _phl_cmd_done(phl_info, &phl_cmd->cmd_sync, csts);
 
   pstst = _cmd_stat_2_phl_stat(csts);
   if (phl_cmd->cmd_complete)
       phl_cmd->cmd_complete(drv, phl_cmd->buf, phl_cmd->buf_len, pstst);
 
   if (phl_cmd->is_cmd_wait) {
       #define PHL_MAX_SCHEDULE_TIMEOUT 100000
       u32 try_cnt = 0;
       u32 start = _os_get_cur_time_ms();
 
       do {
           if (_os_atomic_read(drv, &(phl_cmd->ref_cnt)) == 1)
               break;
           _os_sleep_ms(drv, 10);
           try_cnt++;
           if (try_cnt == 50)
               PHL_ERR("F-%s, L-%d polling is_cmd_wait to false\n",
                           __FUNCTION__, __LINE__);
       } while (phl_get_passing_time_ms(start) < PHL_MAX_SCHEDULE_TIMEOUT);
   }
 
   _phl_cmd_obj_free(phl_info, phl_cmd);
}
 
enum rtw_phl_status
phl_cmd_enqueue(struct phl_info_t *phl_info,
                enum phl_band_idx band_idx,
                enum phl_msg_evt_id evt_id,
                u8 *cmd_buf,
                u32 cmd_len,
                void (*cmd_complete)(void* priv, u8 *buf, u32 buf_len, enum rtw_phl_status status),
                enum phl_cmd_type cmd_type,
                u32 cmd_timeout)
{
   void *drv = phl_to_drvpriv(phl_info);
   enum rtw_phl_status psts = RTW_PHL_STATUS_FAILURE;
   struct phl_cmd_obj *phl_cmd = NULL;
   struct phl_msg msg = {0};
   struct phl_msg_attribute attr = {0};
 
   phl_cmd = _os_kmem_alloc(drv, sizeof(struct phl_cmd_obj));
   if (phl_cmd == NULL) {
       PHL_ERR("%s: alloc phl_cmd failed!\n", __func__);
       psts = RTW_PHL_STATUS_RESOURCE;
       goto _exit;
   }
   phl_cmd->evt_id = evt_id;
   phl_cmd->buf = cmd_buf;
   phl_cmd->buf_len = cmd_len;
   phl_cmd->cmd_complete = cmd_complete;
 
   SET_MSG_MDL_ID_FIELD(msg.msg_id, PHL_MDL_GENERAL);
   SET_MSG_EVT_ID_FIELD(msg.msg_id, evt_id);
   msg.inbuf = (u8 *)phl_cmd;
   msg.inlen = sizeof(struct phl_cmd_obj);
   msg.band_idx = band_idx;
   attr.completion.completion = _phl_cmd_complete;
   attr.completion.priv = phl_info;
 
   if (cmd_type == PHL_CMD_WAIT) {
       phl_cmd->is_cmd_wait = true;
       _os_atomic_set(drv, &phl_cmd->ref_cnt, 0);
       _phl_cmd_sync_init(phl_info, evt_id, &phl_cmd->cmd_sync, cmd_timeout);
   }
 
   psts = phl_disp_eng_send_msg(phl_info, &msg, &attr, NULL);
   if (psts == RTW_PHL_STATUS_SUCCESS) {
       if (cmd_type == PHL_CMD_WAIT) {
           psts = _phl_cmd_wait(phl_info, &phl_cmd->cmd_sync);
           /*ref_cnt++ for cmd wait done*/
           _os_atomic_inc(drv, &phl_cmd->ref_cnt);
           /* Check wait cmd status */
           if (psts == RTW_PHL_STATUS_CMD_SUCCESS)
               psts = RTW_PHL_STATUS_SUCCESS;
       } else {
           psts = RTW_PHL_STATUS_SUCCESS;
       }
   } else {
       PHL_TRACE(COMP_PHL_CMDDISP, _PHL_INFO_, "%s: evt_id(%d)\n", __func__, evt_id);
       _phl_cmd_obj_free(phl_info, phl_cmd);
   }
 
_exit:
   return psts;
}
 
enum rtw_phl_status
rtw_phl_cmd_enqueue(void *phl,
                    enum phl_band_idx band_idx,
                    enum phl_msg_evt_id evt_id,
                    u8 *cmd_buf,
                    u32 cmd_len,
                    void (*core_cmd_complete)(void *priv, u8 *cmd, u32 cmd_len, enum rtw_phl_status status),
                    enum phl_cmd_type cmd_type,
                    u32 cmd_timeout)
{
   return phl_cmd_enqueue((struct phl_info_t *)phl,
                          band_idx,
                          evt_id,
                          cmd_buf,
                          cmd_len,
                          core_cmd_complete,
                          cmd_type,
                          cmd_timeout);
}
 
#endif /*CONFIG_CMD_DISP*/