From 1f8cdc16f6e6877353a1624ebce3ecba211238ce Mon Sep 17 00:00:00 2001
From: Robert Schmidt <robert.schmidt@eurecom.fr>
Date: Tue, 16 Mar 2021 07:51:38 +0100
Subject: [PATCH] Trigger calculate_preferred_[du]l_tda() for all BWPs in
 secondaryCellGroup

---
 openair2/LAYER2/NR_MAC_gNB/config.c            | 18 +++++++++++++++---
 .../LAYER2/NR_MAC_gNB/gNB_scheduler_dlsch.c    | 11 ++---------
 .../LAYER2/NR_MAC_gNB/gNB_scheduler_ulsch.c    | 11 ++---------
 openair2/LAYER2/NR_MAC_gNB/mac_proto.h         |  4 ++--
 4 files changed, 21 insertions(+), 23 deletions(-)

diff --git a/openair2/LAYER2/NR_MAC_gNB/config.c b/openair2/LAYER2/NR_MAC_gNB/config.c
index cfecb67dc2..bae7c76d23 100644
--- a/openair2/LAYER2/NR_MAC_gNB/config.c
+++ b/openair2/LAYER2/NR_MAC_gNB/config.c
@@ -418,9 +418,21 @@ int rrc_mac_config_req_gNB(module_id_t Mod_idP,
 
     RC.nrmac[Mod_idP]->secondaryCellGroupCommon = secondaryCellGroup;
 
-    const int bwp_id = 1;
-    calculate_preferred_dl_tda(Mod_idP, secondaryCellGroup, bwp_id);
-    calculate_preferred_ul_tda(Mod_idP, secondaryCellGroup, bwp_id);
+    const NR_ServingCellConfig_t *servingCellConfig = secondaryCellGroup->spCellConfig->spCellConfigDedicated;
+    const struct NR_ServingCellConfig__downlinkBWP_ToAddModList *bwpList = servingCellConfig->downlinkBWP_ToAddModList;
+    AssertFatal(bwpList->list.count > 0, "downlinkBWP_ToAddModList has no BWPs!\n");
+    for (int i = 0; i < bwpList->list.count; ++i) {
+      const NR_BWP_Downlink_t *bwp = bwpList->list.array[i];
+      calculate_preferred_dl_tda(Mod_idP, bwp);
+    }
+
+    const struct NR_UplinkConfig__uplinkBWP_ToAddModList *ubwpList =
+        servingCellConfig->uplinkConfig->uplinkBWP_ToAddModList;
+    AssertFatal(ubwpList->list.count > 0, "downlinkBWP_ToAddModList no BWPs!\n");
+    for (int i = 0; i < ubwpList->list.count; ++i) {
+      const NR_BWP_Uplink_t *ubwp = ubwpList->list.array[i];
+      calculate_preferred_ul_tda(Mod_idP, ubwp);
+    }
 
     NR_UE_info_t *UE_info = &RC.nrmac[Mod_idP]->UE_info;
     if (add_ue == 1 && get_softmodem_params()->phy_test) {
diff --git a/openair2/LAYER2/NR_MAC_gNB/gNB_scheduler_dlsch.c b/openair2/LAYER2/NR_MAC_gNB/gNB_scheduler_dlsch.c
index 7cad85a512..9cb07255b8 100644
--- a/openair2/LAYER2/NR_MAC_gNB/gNB_scheduler_dlsch.c
+++ b/openair2/LAYER2/NR_MAC_gNB/gNB_scheduler_dlsch.c
@@ -56,9 +56,10 @@
 #define WORD 32
 //#define SIZE_OF_POINTER sizeof (void *)
 
-void calculate_preferred_dl_tda(module_id_t module_id, NR_CellGroupConfig_t *secondaryCellGroup, int bwp_id)
+void calculate_preferred_dl_tda(module_id_t module_id, const NR_BWP_Downlink_t *bwp)
 {
   gNB_MAC_INST *nrmac = RC.nrmac[module_id];
+  const int bwp_id = bwp->bwp_Id;
   if (nrmac->preferred_dl_tda[bwp_id])
     return;
 
@@ -68,14 +69,6 @@ void calculate_preferred_dl_tda(module_id_t module_id, NR_CellGroupConfig_t *sec
       scc->tdd_UL_DL_ConfigurationCommon ? &scc->tdd_UL_DL_ConfigurationCommon->pattern1 : NULL;
   const int symb_dlMixed = tdd ? (1 << tdd->nrofDownlinkSymbols) - 1 : 0;
 
-  const NR_ServingCellConfig_t *servingCellConfig = secondaryCellGroup->spCellConfig->spCellConfigDedicated;
-  const struct NR_ServingCellConfig__downlinkBWP_ToAddModList *bwpList = servingCellConfig->downlinkBWP_ToAddModList;
-  AssertFatal(bwpList->list.count == 1,
-              "downlinkBWP_ToAddModList has %d BWP but cannot handle more in %s!\n",
-              bwpList->list.count,
-              __func__);
-  const NR_BWP_Downlink_t *bwp = bwpList->list.array[bwp_id - 1];
-
   const int target_ss = NR_SearchSpace__searchSpaceType_PR_ue_Specific;
   const NR_SearchSpace_t *search_space = get_searchspace(bwp, target_ss);
   const NR_ControlResourceSet_t *coreset = get_coreset(bwp, search_space, 1 /* dedicated */);
diff --git a/openair2/LAYER2/NR_MAC_gNB/gNB_scheduler_ulsch.c b/openair2/LAYER2/NR_MAC_gNB/gNB_scheduler_ulsch.c
index b76bfff786..4cbad2cbdb 100644
--- a/openair2/LAYER2/NR_MAC_gNB/gNB_scheduler_ulsch.c
+++ b/openair2/LAYER2/NR_MAC_gNB/gNB_scheduler_ulsch.c
@@ -61,9 +61,10 @@ const uint32_t NR_LONG_BSR_TABLE[256] ={
 35910462, 38241455, 40723756, 43367187, 46182206, 49179951, 52372284, 55771835, 59392055, 63247269, 67352729, 71724679, 76380419, 81338368, 162676736, 4294967295
 };
 
-void calculate_preferred_ul_tda(module_id_t module_id, NR_CellGroupConfig_t *secondaryCellGroup, int bwp_id)
+void calculate_preferred_ul_tda(module_id_t module_id, const NR_BWP_Uplink_t *ubwp)
 {
   gNB_MAC_INST *nrmac = RC.nrmac[module_id];
+  const int bwp_id = ubwp->bwp_Id;
   if (nrmac->preferred_ul_tda[bwp_id])
     return;
 
@@ -75,14 +76,6 @@ void calculate_preferred_ul_tda(module_id_t module_id, NR_CellGroupConfig_t *sec
   /* Uplink symbols are at the end of the slot */
   const int symb_ulMixed = tdd ? ((1 << tdd->nrofUplinkSymbols) - 1) << (14 - tdd->nrofUplinkSymbols) : 0;
 
-  const NR_ServingCellConfig_t *servingCellConfig = secondaryCellGroup->spCellConfig->spCellConfigDedicated;
-  const struct NR_UplinkConfig__uplinkBWP_ToAddModList *ubwpList = servingCellConfig->uplinkConfig->uplinkBWP_ToAddModList;
-  AssertFatal(ubwpList->list.count == 1,
-              "downlinkBWP_ToAddModList has %d BWP but cannot handle more in %s!\n",
-              ubwpList->list.count,
-              __func__);
-  const NR_BWP_Uplink_t *ubwp = ubwpList->list.array[bwp_id - 1];
-
   const struct NR_PUCCH_Config__resourceToAddModList *resList = ubwp->bwp_Dedicated->pucch_Config->choice.setup->resourceToAddModList;
   // for the moment, just block any symbol that might hold a PUCCH, regardless
   // of the RB. This is a big simplification, as most RBs will NOT have a PUCCH
diff --git a/openair2/LAYER2/NR_MAC_gNB/mac_proto.h b/openair2/LAYER2/NR_MAC_gNB/mac_proto.h
index 5389dab878..5d5ae59e3c 100644
--- a/openair2/LAYER2/NR_MAC_gNB/mac_proto.h
+++ b/openair2/LAYER2/NR_MAC_gNB/mac_proto.h
@@ -383,8 +383,8 @@ int16_t ssb_index_from_prach(module_id_t module_idP,
 
 void find_SSB_and_RO_available(module_id_t module_idP);
 
-void calculate_preferred_dl_tda(module_id_t module_id, NR_CellGroupConfig_t *secondaryCellGroup, int bwp_id);
-void calculate_preferred_ul_tda(module_id_t module_id, NR_CellGroupConfig_t *secondaryCellGroup, int bwp_id);
+void calculate_preferred_dl_tda(module_id_t module_id, const NR_BWP_Downlink_t *bwp);
+void calculate_preferred_ul_tda(module_id_t module_id, const NR_BWP_Uplink_t *ubwp);
 
 bool find_free_CCE(module_id_t module_id, sub_frame_t slot, int UE_id);
 
-- 
2.26.2