config.c 44.3 KB
Newer Older
WEI-TAI CHEN's avatar
WEI-TAI CHEN committed
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
/*
 * Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The OpenAirInterface Software Alliance licenses this file to You under
 * the OAI Public License, Version 1.1  (the "License"); you may not use this file
 * except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.openairinterface.org/?page_id=698
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *-------------------------------------------------------------------------------
 * For more information about the OpenAirInterface (OAI) Software Alliance:
 *      contact@openairinterface.org
 */

/*! \file config.c
 * \brief gNB configuration performed by RRC or as a consequence of RRC procedures
 * \author  Navid Nikaein and Raymond Knopp, WEI-TAI CHEN
 * \date 2010 - 2014, 2018
 * \version 0.1
 * \company Eurecom, NTUST
 * \email: navid.nikaein@eurecom.fr, kroempa@gmail.com
 * @ingroup _mac

 */

33
#include "common/platform_types.h"
laurent's avatar
laurent committed
34
#include "common/platform_constants.h"
35
#include "common/ran_context.h"
36
#include "common/utils/nr/nr_common.h"
Raymond Knopp's avatar
Raymond Knopp committed
37 38
#include "common/utils/LOG/log.h"
#include "common/utils/LOG/vcd_signal_dumper.h"
39 40 41

#include "NR_BCCH-BCH-Message.h"
#include "NR_ServingCellConfigCommon.h"
42
#include "uper_encoder.h"
43 44

#include "LAYER2/NR_MAC_gNB/mac_proto.h"
45
#include "SCHED_NR/phy_frame_config_nr.h"
francescomani's avatar
francescomani committed
46
#include "openair1/PHY/defs_gNB.h"
47

Raymond Knopp's avatar
Raymond Knopp committed
48
#include "NR_MIB.h"
49
#include "LAYER2/NR_MAC_COMMON/nr_mac_common.h"
Mahesh's avatar
Mahesh committed
50
#include "../../../../nfapi/oai_integration/vendor_ext.h"
51 52
/* Softmodem params */
#include "executables/softmodem-common.h"
53
#include <complex.h>
54

55 56 57
extern RAN_CONTEXT_t RC;
//extern int l2_init_gNB(void);
extern uint8_t nfapi_mode;
Raymond Knopp's avatar
Raymond Knopp committed
58

59 60 61 62 63 64 65 66 67 68 69
c16_t convert_precoder_weight(double complex c_in)
{
  double cr = creal(c_in) * 32768 + 0.5;
  if (cr < 0)
    cr -= 1;
  double ci = cimag(c_in) * 32768 + 0.5;
  if (ci < 0)
    ci -= 1;
  return (c16_t) {.r = (short)cr, .i = (short)ci};
}

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
void get_K1_K2(int N1, int N2, int *K1, int *K2, int layers)
{
  // num of allowed k1 and k2 according to 5.2.2.2.1-3 and -4 in 38.214
  switch (layers) {
    case 1:
      *K1 = 1;
      *K2 = 1;
      break;
    case 2:
      *K2 = N2 == 1 ? 1 : 2;
      if(N2 == N1 || N1 == 2)
        *K1 = 2;
      else if (N2 == 1)
        *K1 = 4;
      else
        *K1 = 3;
      break;
    case 3:
    case 4:
      *K2 = N2 == 1 ? 1 : 2;
      if (N1 == 6)
        *K1 = 5;
      else
        *K1 = N1;
      break;
    default:
      AssertFatal(false, "Number of layers %d not supported\n", layers);
  }
}

100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
nfapi_nr_pm_list_t init_DL_MIMO_codebook(gNB_MAC_INST *gNB, nr_pdsch_AntennaPorts_t antenna_ports)
{
  int num_antenna_ports = antenna_ports.N1 * antenna_ports.N2 * antenna_ports.XP;
  if (num_antenna_ports < 2)
    return (nfapi_nr_pm_list_t) {0};

  //NR Codebook Generation for codebook type1 SinglePanel
  int N1 = antenna_ports.N1;
  int N2 = antenna_ports.N2;
  //Uniform Planner Array: UPA
  //    X X X X ... X
  //    X X X X ... X
  // N2 . . . . ... .
  //    X X X X ... X
  //   |<-----N1---->|

  //Get the uniform planar array parameters
  // To be confirmed
  int O2 = N2 > 1 ? 4 : 1; //Vertical beam oversampling (1 or 4)
  int O1 = num_antenna_ports > 2 ? 4 : 1; //Horizontal beam oversampling (1 or 4)

  int max_mimo_layers = (num_antenna_ports < NR_MAX_NB_LAYERS) ? num_antenna_ports : NR_MAX_NB_LAYERS;
  AssertFatal(max_mimo_layers <= 4, "Max number of layers supported is 4\n");
123 124 125 126 127 128
  AssertFatal(num_antenna_ports < 16, "Max number of antenna ports supported is currently 16\n");

  int K1[max_mimo_layers];
  memset(K1, 0, sizeof(K1));
  int K2[max_mimo_layers];
  memset(K2, 0, sizeof(K2));
129

130 131 132 133 134
  nfapi_nr_pm_list_t mat = {.num_pm_idx = 0};
  for (int i = 0; i < max_mimo_layers; i++) {
    get_K1_K2(N1, N2, &K1[i], &K2[i], i + 1);
    int i2_size = i == 0 ? 4 : 2;
    gNB->precoding_matrix_size[i] = i2_size * N1 * O1 * N2 * O2 * K1[i] * K2[i];
135 136 137 138 139 140 141 142
    mat.num_pm_idx += gNB->precoding_matrix_size[i];
  }

  nfapi_nr_pm_pdu_t *pmi_pdu = malloc16(mat.num_pm_idx * sizeof(*pmi_pdu));
  AssertFatal(pmi_pdu != NULL, "out of memory\n");
  mat.pmi_pdu = pmi_pdu;

  // Generation of codebook Type1 with codebookMode 1 (num_antenna_ports < 16)
143

144 145
  // Generate DFT vertical beams
  // ll: index of a vertical beams vector (represented by i1_1 in TS 38.214)
146
  const int max_l = N1 * O1 + 4 * O1;  // max k1 is 4*O1
147 148 149 150 151
  double complex v[max_l][N1];
  for (int ll = 0; ll < max_l; ll++) { // i1_1
    for (int nn = 0; nn < N1; nn++) {
      v[ll][nn] = cexp(I * (2 * M_PI * nn * ll) / (N1 * O1));
      LOG_D(PHY, "v[%d][%d] = %f +j %f\n", ll, nn, creal(v[ll][nn]), cimag(v[ll][nn]));
152
    }
153 154 155
  }
  // Generate DFT Horizontal beams
  // mm: index of a Horizontal beams vector (represented by i1_2 in TS 38.214)
156
  const int max_m = N2 * O2 + O2; // max k2 is O2
157 158 159 160 161
  double complex u[max_m][N2];
  for (int mm = 0; mm < max_m; mm++) { // i1_2
    for (int nn = 0; nn < N2; nn++) {
      u[mm][nn] = cexp(I * (2 * M_PI * nn * mm) / (N2 * O2));
      LOG_D(PHY, "u[%d][%d] = %f +j %f\n", mm, nn, creal(u[mm][nn]), cimag(u[mm][nn]));
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
  }
  // Generate co-phasing angles
  // i_2: index of a co-phasing vector
  // i1_1, i1_2, and i_2 are reported from UEs
  double complex theta_n[4];
  for (int nn = 0; nn < 4; nn++) {
    theta_n[nn] = cexp(I * M_PI * nn / 2);
    LOG_D(PHY, "theta_n[%d] = %f +j %f\n", nn, creal(theta_n[nn]), cimag(theta_n[nn]));
  }
  // Kronecker product v_lm
  double complex v_lm[max_l][max_m][N2 * N1];
  // v_ll_mm_codebook denotes the elements of a precoding matrix W_i1,1_i_1,2
  for (int ll = 0; ll < max_l; ll++) { // i_1_1
    for (int mm = 0; mm < max_m; mm++) { // i_1_2
      for (int nn1 = 0; nn1 < N1; nn1++) {
        for (int nn2 = 0; nn2 < N2; nn2++) {
          v_lm[ll][mm][nn1 * N2 + nn2] = v[ll][nn1] * u[mm][nn2];
          LOG_D(PHY,
                "v_lm[%d][%d][%d] = %f +j %f\n",
                ll,
                mm,
                nn1 * N2 + nn2,
                creal(v_lm[ll][mm][nn1 * N2 + nn2]),
                cimag(v_lm[ll][mm][nn1 * N2 + nn2]));
187 188 189
        }
      }
    }
190
  }
191

192
  double complex res_code;
193

194
  // Table 5.2.2.2.1-5:
195
  int pmiq = -1;
196
  // Codebook for 1-layer CSI reporting using antenna ports 3000 to 2999+PCSI-RS
197 198
  for (int mm = 0; mm < N2 * O2; mm++) { // i_1_2
    for (int ll = 0; ll < N1 * O1; ll++) { // i_1_1
199
      for (int nn = 0; nn < 4; nn++) {
200
        pmiq++;
201 202 203 204 205 206 207 208 209 210 211
        pmi_pdu[pmiq].pm_idx = pmiq + 1; // index 0 is the identity matrix
        pmi_pdu[pmiq].numLayers = 1;
        pmi_pdu[pmiq].num_ant_ports = num_antenna_ports;
        LOG_D(PHY, "layer 1 Codebook pmiq = %d\n", pmiq);
        for (int len = 0; len < N1 * N2; len++) {
          nfapi_nr_pm_weights_t *weights = &pmi_pdu[pmiq].weights[0][len];
          res_code = sqrt(1 / (double)num_antenna_ports) * v_lm[ll][mm][len];
          c16_t precoder_weight = convert_precoder_weight(res_code);
          weights->precoder_weight_Re = precoder_weight.r;
          weights->precoder_weight_Im = precoder_weight.i;
          LOG_D(PHY,
212
                "1 Layer Precoding Matrix[pmi %d][antPort %d]= %f+j %f -> Fixed Point %d+j %d \n",
213 214 215 216 217 218 219
                pmiq,
                len,
                creal(res_code),
                cimag(res_code),
                weights->precoder_weight_Re,
                weights->precoder_weight_Im);
        }
220

221 222 223 224 225 226 227
        for (int len = N1 * N2; len < 2 * N1 * N2; len++) {
          nfapi_nr_pm_weights_t *weights = &pmi_pdu[pmiq].weights[0][len];
          res_code = sqrt(1 / (double)num_antenna_ports) * theta_n[nn] * v_lm[ll][mm][len - N1 * N2];
          c16_t precoder_weight = convert_precoder_weight(res_code);
          weights->precoder_weight_Re = precoder_weight.r;
          weights->precoder_weight_Im = precoder_weight.i;
          LOG_D(PHY,
228
                "1 Layer Precoding Matrix[pmi %d][antPort %d]= %f+j %f -> Fixed Point %d+j %d \n",
229 230 231 232 233 234
                pmiq,
                len,
                creal(res_code),
                cimag(res_code),
                weights->precoder_weight_Re,
                weights->precoder_weight_Im);
235 236 237
        }
      }
    }
238 239 240 241 242 243 244 245
  }
  int llc = 0;
  int mmc = 0;
  double complex phase_sign = 0;
  // Table 5.2.2.2.1-6:
  // Codebook for 2-layer CSI reporting using antenna ports 3000 to 2999+PCSI-RS
  // Compute the code book size for generating 2 layers out of Tx antenna ports
  // pmi=1,...,pmi_size, we construct
246 247 248 249
  for (int k2 = 0; k2 < K2[1]; k2++) {
    for (int k1 = 0; k1 < K1[1]; k1++) {
      for (int mm = 0; mm < N2 * O2; mm++) { // i_1_2
        for (int ll = 0; ll < N1 * O1; ll++) { // i_1_1
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
          for (int nn = 0; nn < 2; nn++) { // i_2
            pmiq++;
            pmi_pdu[pmiq].pm_idx = pmiq + 1; // index 0 is the identity matrix
            pmi_pdu[pmiq].numLayers = 2;
            pmi_pdu[pmiq].num_ant_ports = num_antenna_ports;
            LOG_D(PHY, "layer 2 Codebook pmiq = %d\n", pmiq);
            for (int j_col = 0; j_col < 2; j_col++) {
              if (j_col == 0) {
                llc = ll;
                mmc = mm;
                phase_sign = 1;
              }
              if (j_col == 1) {
                llc = ll + k1 * O1;
                mmc = mm + k2 * O2;
                phase_sign = -1;
              }
              for (int i_rows = 0; i_rows < N1 * N2; i_rows++) {
                nfapi_nr_pm_weights_t *weights = &pmi_pdu[pmiq].weights[j_col][i_rows];
                res_code = sqrt(1 / (double)(2 * num_antenna_ports)) * v_lm[llc][mmc][i_rows];
                c16_t precoder_weight = convert_precoder_weight(res_code);
                weights->precoder_weight_Re = precoder_weight.r;
                weights->precoder_weight_Im = precoder_weight.i;
                LOG_D(PHY,
274
                      "2 Layer Precoding Matrix[pmi %d][antPort %d][layerIdx %d]= %f+j %f -> Fixed Point %d+j %d \n",
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
                      pmiq,
                      i_rows,
                      j_col,
                      creal(res_code),
                      cimag(res_code),
                      weights->precoder_weight_Re,
                      weights->precoder_weight_Im);
              }
              for (int i_rows = N1 * N2; i_rows < 2 * N1 * N2; i_rows++) {
                nfapi_nr_pm_weights_t *weights = &pmi_pdu[pmiq].weights[j_col][i_rows];
                res_code = sqrt(1 / (double)(2 * num_antenna_ports)) * (phase_sign)*theta_n[nn] * v_lm[llc][mmc][i_rows - N1 * N2];
                c16_t precoder_weight = convert_precoder_weight(res_code);
                weights->precoder_weight_Re = precoder_weight.r;
                weights->precoder_weight_Im = precoder_weight.i;
                LOG_D(PHY,
290
                      "2 Layer Precoding Matrix[pmi %d][antPort %d][layerIdx %d]= %f+j %f -> Fixed Point %d+j %d \n",
291 292 293 294 295 296 297
                      pmiq,
                      i_rows,
                      j_col,
                      creal(res_code),
                      cimag(res_code),
                      weights->precoder_weight_Re,
                      weights->precoder_weight_Im);
298 299 300 301 302 303
              }
            }
          }
        }
      }
    }
304
  }
305

306 307 308 309 310
  if (max_mimo_layers < 3)
    return mat;
  // Table 5.2.2.2.1-7:
  // Codebook for 3-layer CSI reporting using antenna ports 3000 to 2999+PCSI-RS
  // pmi=1,...,pmi_size are computed as follows
311 312 313 314
  for (int k2 = 0; k2 < K2[2]; k2++) {
    for (int k1 = 0; k1 < K1[2]; k1++) {
      for (int mm = 0; mm < N2 * O2; mm++) { // i_1_2
        for (int ll = 0; ll < N1 * O1; ll++) { // i_1_1
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
          for (int nn = 0; nn < 2; nn++) { // i_2
            pmiq++;
            pmi_pdu[pmiq].pm_idx = pmiq + 1; // index 0 is the identity matrix
            pmi_pdu[pmiq].numLayers = 3;
            pmi_pdu[pmiq].num_ant_ports = num_antenna_ports;
            LOG_D(PHY, "layer 3 Codebook pmiq = %d\n", pmiq);
            for (int j_col = 0; j_col < 3; j_col++) {
              if (j_col == 0) {
                llc = ll;
                mmc = mm;
                phase_sign = 1;
              }
              if (j_col == 1) {
                llc = ll + k1 * O1;
                mmc = mm + k2 * O2;
                phase_sign = 1;
              }
              if (j_col == 2) {
                llc = ll;
                mmc = mm;
                phase_sign = -1;
              }
              for (int i_rows = 0; i_rows < N1 * N2; i_rows++) {
                nfapi_nr_pm_weights_t *weights = &pmi_pdu[pmiq].weights[j_col][i_rows];
                res_code = sqrt(1 / (double)(3 * num_antenna_ports)) * v_lm[llc][mmc][i_rows];
                c16_t precoder_weight = convert_precoder_weight(res_code);
                weights->precoder_weight_Re = precoder_weight.r;
                weights->precoder_weight_Im = precoder_weight.i;
                LOG_D(PHY,
344
                      "3 Layer Precoding Matrix[pmi %d][antPort %d][layerIdx %d]= %f+j %f -> Fixed Point %d+j %d \n",
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
                      pmiq,
                      i_rows,
                      j_col,
                      creal(res_code),
                      cimag(res_code),
                      weights->precoder_weight_Re,
                      weights->precoder_weight_Im);
              }
              for (int i_rows = N1 * N2; i_rows < 2 * N1 * N2; i_rows++) {
                nfapi_nr_pm_weights_t *weights = &pmi_pdu[pmiq].weights[j_col][i_rows];
                res_code = sqrt(1 / (double)(3 * num_antenna_ports)) * (phase_sign)*theta_n[nn] * v_lm[llc][mmc][i_rows - N1 * N2];
                c16_t precoder_weight = convert_precoder_weight(res_code);
                weights->precoder_weight_Re = precoder_weight.r;
                weights->precoder_weight_Im = precoder_weight.i;
                LOG_D(PHY,
360
                      "3 Layer Precoding Matrix[pmi %d][antPort %d][layerIdx %d]= %f+j %f -> Fixed Point %d+j %d \n",
361 362 363 364 365 366 367
                      pmiq,
                      i_rows,
                      j_col,
                      creal(res_code),
                      cimag(res_code),
                      weights->precoder_weight_Re,
                      weights->precoder_weight_Im);
368 369 370 371 372 373
              }
            }
          }
        }
      }
    }
374
  }
375

376 377 378 379
  if (max_mimo_layers < 4)
    return mat;
  // Table 5.2.2.2.1-8:
  // Codebook for 4-layer CSI reporting using antenna ports 3000 to 2999+PCSI-RS
380 381 382 383
  for (int k2 = 0; k2 < K2[3]; k2++) {
    for (int k1 = 0; k1 < K1[3]; k1++) {
      for (int mm = 0; mm < N2 * O2; mm++) { // i_1_2
        for (int ll = 0; ll < N1 * O1; ll++) { // i_1_1
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
          for (int nn = 0; nn < 2; nn++) { // i_2
            pmiq++;
            pmi_pdu[pmiq].pm_idx = pmiq + 1; // index 0 is the identity matrix
            pmi_pdu[pmiq].numLayers = 4;
            pmi_pdu[pmiq].num_ant_ports = num_antenna_ports;
            LOG_D(PHY, "layer 4 pmiq = %d\n", pmiq);
            for (int j_col = 0; j_col < 4; j_col++) {
              if (j_col == 0) {
                llc = ll;
                mmc = mm;
                phase_sign = 1;
              }
              if (j_col == 1) {
                llc = ll + k1 * O1;
                mmc = mm + k2 * O2;
                phase_sign = 1;
              }
              if (j_col == 2) {
                llc = ll;
                mmc = mm;
                phase_sign = -1;
              }
              if (j_col == 3) {
                llc = ll + k1 * O1;
                mmc = mm + k2 * O2;
                phase_sign = -1;
              }
              for (int i_rows = 0; i_rows < N1 * N2; i_rows++) {
                nfapi_nr_pm_weights_t *weights = &pmi_pdu[pmiq].weights[j_col][i_rows];
                res_code = sqrt(1 / (double)(4 * num_antenna_ports)) * v_lm[llc][mmc][i_rows];
                c16_t precoder_weight = convert_precoder_weight(res_code);
                weights->precoder_weight_Re = precoder_weight.r;
                weights->precoder_weight_Im = precoder_weight.i;
                LOG_D(PHY,
418
                      "4 Layer Precoding Matrix[pmi %d][antPort %d][layerIdx %d]= %f+j %f -> Fixed Point %d+j %d \n",
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
                      pmiq,
                      i_rows,
                      j_col,
                      creal(res_code),
                      cimag(res_code),
                      weights->precoder_weight_Re,
                      weights->precoder_weight_Im);
              }
              for (int i_rows = N1 * N2; i_rows < 2 * N1 * N2; i_rows++) {
                nfapi_nr_pm_weights_t *weights = &pmi_pdu[pmiq].weights[j_col][i_rows];
                res_code = sqrt(1 / (double)(4 * num_antenna_ports)) * (phase_sign)*theta_n[nn] * v_lm[llc][mmc][i_rows - N1 * N2];
                c16_t precoder_weight = convert_precoder_weight(res_code);
                weights->precoder_weight_Re = precoder_weight.r;
                weights->precoder_weight_Im = precoder_weight.i;
                LOG_D(PHY,
434
                      "4 Layer Precoding Matrix[pmi %d][antPort %d][layerIdx %d]= %f+j %f -> Fixed Point %d+j %d \n",
435 436 437 438 439 440 441
                      pmiq,
                      i_rows,
                      j_col,
                      creal(res_code),
                      cimag(res_code),
                      weights->precoder_weight_Re,
                      weights->precoder_weight_Im);
442 443 444 445 446 447 448
              }
            }
          }
        }
      }
    }
  }
449
  return mat;
450 451
}

452 453 454 455
static void process_rlcBearerConfig(struct NR_CellGroupConfig__rlc_BearerToAddModList *rlc_bearer2add_list,
                                    struct NR_CellGroupConfig__rlc_BearerToReleaseList *rlc_bearer2release_list,
                                    NR_UE_sched_ctrl_t *sched_ctrl)
{
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
  if (rlc_bearer2release_list) {
    for (int i = 0; i < rlc_bearer2release_list->list.count; i++) {
      for (int idx = 0; idx < sched_ctrl->dl_lc_num; idx++) {
        if (sched_ctrl->dl_lc_ids[idx] == *rlc_bearer2release_list->list.array[i]) {
          const int remaining_lcs = sched_ctrl->dl_lc_num - idx - 1;
          memmove(&sched_ctrl->dl_lc_ids[idx], &sched_ctrl->dl_lc_ids[idx + 1], sizeof(sched_ctrl->dl_lc_ids[idx]) * remaining_lcs);
          sched_ctrl->dl_lc_num--;
          break;
        }
      }
    }
  }

  if (rlc_bearer2add_list) {
    // keep lcids
    for (int i = 0; i < rlc_bearer2add_list->list.count; i++) {
      const int lcid = rlc_bearer2add_list->list.array[i]->logicalChannelIdentity;
      bool found = false;
      for (int idx = 0; idx < sched_ctrl->dl_lc_num; idx++) {
        if (sched_ctrl->dl_lc_ids[idx] == lcid) {
          found = true;
          break;
        }
      }
480

481 482 483 484 485
      if (!found) {
        sched_ctrl->dl_lc_num++;
        sched_ctrl->dl_lc_ids[sched_ctrl->dl_lc_num - 1] = lcid;
        LOG_D(NR_MAC, "Adding LCID %d (%s %d)\n", lcid, lcid < 4 ? "SRB" : "DRB", lcid);
      }
486
    }
487 488 489 490 491
  }

  LOG_D(NR_MAC, "In %s: total num of active bearers %d) \n",
      __FUNCTION__,
      sched_ctrl->dl_lc_num);
492

493 494
}

495
void process_CellGroup(NR_CellGroupConfig_t *CellGroup, NR_UE_info_t *UE)
496
{
497 498 499
  /* we assume that this function is mutex-protected from outside */
  NR_SCHED_ENSURE_LOCKED(&RC.nrmac[0]->sched_lock);

500
   AssertFatal(CellGroup, "CellGroup is null\n");
501
   NR_MAC_CellGroupConfig_t *mac_CellGroupConfig = CellGroup->mac_CellGroupConfig;
502 503

   if (mac_CellGroupConfig) {
504 505 506 507 508
     //process_drx_Config(sched_ctrl,mac_CellGroupConfig->drx_Config);
     //process_schedulingRequestConfig(sched_ctrl,mac_CellGroupConfig->schedulingRequestConfig);
     //process_bsrConfig(sched_ctrl,mac_CellGroupConfig->bsr_Config);
     //process_tag_Config(sched_ctrl,mac_CellGroupConfig->tag_Config);
     //process_phr_Config(sched_ctrl,mac_CellGroupConfig->phr_Config);
509 510
   }

511 512 513 514 515
   if (CellGroup->spCellConfig && CellGroup->spCellConfig->reconfigurationWithSync
       && CellGroup->spCellConfig->reconfigurationWithSync->rach_ConfigDedicated
       && CellGroup->spCellConfig->reconfigurationWithSync->rach_ConfigDedicated->choice.uplink->cfra) {
    nr_mac_prepare_ra_ue(RC.nrmac[0], UE->rnti, CellGroup);
   }
516
   process_rlcBearerConfig(CellGroup->rlc_BearerToAddModList, CellGroup->rlc_BearerToReleaseList, &UE->UE_sched_ctrl);
517
}
francescomani's avatar
francescomani committed
518

519
static void config_common(gNB_MAC_INST *nrmac, nr_pdsch_AntennaPorts_t pdsch_AntennaPorts, int pusch_AntennaPorts, NR_ServingCellConfigCommon_t *scc)
520
{
521 522 523 524 525 526
  nfapi_nr_config_request_scf_t *cfg = &nrmac->config[0];
  nrmac->common_channels[0].ServingCellConfigCommon = scc;

  // Carrier configuration
  struct NR_FrequencyInfoDL *frequencyInfoDL = scc->downlinkConfigCommon->frequencyInfoDL;
  int bw_index = get_supported_band_index(frequencyInfoDL->scs_SpecificCarrierList.list.array[0]->subcarrierSpacing,
527
                                          *frequencyInfoDL->frequencyBandList.list.array[0] > 256 ? FR2 : FR1,
528 529
                                          frequencyInfoDL->scs_SpecificCarrierList.list.array[0]->carrierBandwidth);
  cfg->carrier_config.dl_bandwidth.value =
530
       get_supported_bw_mhz(*frequencyInfoDL->frequencyBandList.list.array[0] > 256 ? FR2 : FR1, bw_index);
531 532 533
  cfg->carrier_config.dl_bandwidth.tl.tag = NFAPI_NR_CONFIG_DL_BANDWIDTH_TAG; // temporary
  cfg->num_tlv++;
  LOG_I(NR_MAC, "DL_Bandwidth:%d\n", cfg->carrier_config.dl_bandwidth.value);
534

535 536 537
  cfg->carrier_config.dl_frequency.value = from_nrarfcn(*frequencyInfoDL->frequencyBandList.list.array[0],
                                                        *scc->ssbSubcarrierSpacing,
                                                        frequencyInfoDL->absoluteFrequencyPointA)
538
                                            / 1000; // freq in kHz
539 540
  cfg->carrier_config.dl_frequency.tl.tag = NFAPI_NR_CONFIG_DL_FREQUENCY_TAG;
  cfg->num_tlv++;
541

542
  for (int i = 0; i < 5; i++) {
543 544 545
    if (i == frequencyInfoDL->scs_SpecificCarrierList.list.array[0]->subcarrierSpacing) {
      cfg->carrier_config.dl_grid_size[i].value = frequencyInfoDL->scs_SpecificCarrierList.list.array[0]->carrierBandwidth;
      cfg->carrier_config.dl_k0[i].value = frequencyInfoDL->scs_SpecificCarrierList.list.array[0]->offsetToCarrier;
546 547 548 549
      cfg->carrier_config.dl_grid_size[i].tl.tag = NFAPI_NR_CONFIG_DL_GRID_SIZE_TAG;
      cfg->carrier_config.dl_k0[i].tl.tag = NFAPI_NR_CONFIG_DL_K0_TAG;
      cfg->num_tlv++;
      cfg->num_tlv++;
550
    } else {
551 552 553
      cfg->carrier_config.dl_grid_size[i].value = 0;
      cfg->carrier_config.dl_k0[i].value = 0;
    }
554 555 556
  }
  struct NR_FrequencyInfoUL *frequencyInfoUL = scc->uplinkConfigCommon->frequencyInfoUL;
  bw_index = get_supported_band_index(frequencyInfoUL->scs_SpecificCarrierList.list.array[0]->subcarrierSpacing,
557
                                      *frequencyInfoUL->frequencyBandList->list.array[0] > 256 ? FR2 : FR1,
558 559
                                      frequencyInfoUL->scs_SpecificCarrierList.list.array[0]->carrierBandwidth);
  cfg->carrier_config.uplink_bandwidth.value =
560
       get_supported_bw_mhz(*frequencyInfoUL->frequencyBandList->list.array[0] > 256 ? FR2 : FR1, bw_index);
561 562 563
  cfg->carrier_config.uplink_bandwidth.tl.tag = NFAPI_NR_CONFIG_UPLINK_BANDWIDTH_TAG; // temporary
  cfg->num_tlv++;
  LOG_I(NR_MAC, "DL_Bandwidth:%d\n", cfg->carrier_config.uplink_bandwidth.value);
564

565 566
  int UL_pointA;
  if (frequencyInfoUL->absoluteFrequencyPointA == NULL)
567
    UL_pointA = frequencyInfoDL->absoluteFrequencyPointA;
568
  else
569
    UL_pointA = *frequencyInfoUL->absoluteFrequencyPointA;
570

571 572 573 574 575 576
  cfg->carrier_config.uplink_frequency.value = from_nrarfcn(*frequencyInfoUL->frequencyBandList->list.array[0],
                                                            *scc->ssbSubcarrierSpacing,
                                                            UL_pointA)
                                               / 1000; // freq in kHz
  cfg->carrier_config.uplink_frequency.tl.tag = NFAPI_NR_CONFIG_UPLINK_FREQUENCY_TAG;
  cfg->num_tlv++;
577

578
  for (int i = 0; i < 5; i++) {
579 580 581
    if (i == frequencyInfoUL->scs_SpecificCarrierList.list.array[0]->subcarrierSpacing) {
      cfg->carrier_config.ul_grid_size[i].value = frequencyInfoUL->scs_SpecificCarrierList.list.array[0]->carrierBandwidth;
      cfg->carrier_config.ul_k0[i].value = frequencyInfoUL->scs_SpecificCarrierList.list.array[0]->offsetToCarrier;
582 583 584 585
      cfg->carrier_config.ul_grid_size[i].tl.tag = NFAPI_NR_CONFIG_UL_GRID_SIZE_TAG;
      cfg->carrier_config.ul_k0[i].tl.tag = NFAPI_NR_CONFIG_UL_K0_TAG;
      cfg->num_tlv++;
      cfg->num_tlv++;
586
    } else {
587 588 589
      cfg->carrier_config.ul_grid_size[i].value = 0;
      cfg->carrier_config.ul_k0[i].value = 0;
    }
590
  }
591

592 593
  uint32_t band = *frequencyInfoDL->frequencyBandList.list.array[0];
  frequency_range_t frequency_range = band < 100 ? FR1 : FR2;
Raymond Knopp's avatar
Raymond Knopp committed
594

595 596
  frame_type_t frame_type = get_frame_type(*frequencyInfoDL->frequencyBandList.list.array[0], *scc->ssbSubcarrierSpacing);
  nrmac->common_channels[0].frame_type = frame_type;
597

598 599 600 601
  // Cell configuration
  cfg->cell_config.phy_cell_id.value = *scc->physCellId;
  cfg->cell_config.phy_cell_id.tl.tag = NFAPI_NR_CONFIG_PHY_CELL_ID_TAG;
  cfg->num_tlv++;
602

603 604 605
  cfg->cell_config.frame_duplex_type.value = frame_type;
  cfg->cell_config.frame_duplex_type.tl.tag = NFAPI_NR_CONFIG_FRAME_DUPLEX_TYPE_TAG;
  cfg->num_tlv++;
606

607 608 609 610
  // SSB configuration
  cfg->ssb_config.ss_pbch_power.value = scc->ss_PBCH_BlockPower;
  cfg->ssb_config.ss_pbch_power.tl.tag = NFAPI_NR_CONFIG_SS_PBCH_POWER_TAG;
  cfg->num_tlv++;
611

612 613 614
  cfg->ssb_config.bch_payload.value = 1;
  cfg->ssb_config.bch_payload.tl.tag = NFAPI_NR_CONFIG_BCH_PAYLOAD_TAG;
  cfg->num_tlv++;
615

616 617 618
  cfg->ssb_config.scs_common.value = *scc->ssbSubcarrierSpacing;
  cfg->ssb_config.scs_common.tl.tag = NFAPI_NR_CONFIG_SCS_COMMON_TAG;
  cfg->num_tlv++;
619

620
  // PRACH configuration
621

622 623 624
  uint8_t nb_preambles = 64;
  NR_RACH_ConfigCommon_t *rach_ConfigCommon = scc->uplinkConfigCommon->initialUplinkBWP->rach_ConfigCommon->choice.setup;
  if (rach_ConfigCommon->totalNumberOfRA_Preambles != NULL)
625
    nb_preambles = *rach_ConfigCommon->totalNumberOfRA_Preambles;
626

627 628 629
  cfg->prach_config.prach_sequence_length.value = rach_ConfigCommon->prach_RootSequenceIndex.present - 1;
  cfg->prach_config.prach_sequence_length.tl.tag = NFAPI_NR_CONFIG_PRACH_SEQUENCE_LENGTH_TAG;
  cfg->num_tlv++;
630

631
  if (rach_ConfigCommon->msg1_SubcarrierSpacing)
632
    cfg->prach_config.prach_sub_c_spacing.value = *rach_ConfigCommon->msg1_SubcarrierSpacing;
633
  else
634
    cfg->prach_config.prach_sub_c_spacing.value = frequencyInfoDL->scs_SpecificCarrierList.list.array[0]->subcarrierSpacing;
635 636 637 638 639 640 641 642 643 644
  cfg->prach_config.prach_sub_c_spacing.tl.tag = NFAPI_NR_CONFIG_PRACH_SUB_C_SPACING_TAG;
  cfg->num_tlv++;
  cfg->prach_config.restricted_set_config.value = rach_ConfigCommon->restrictedSetConfig;
  cfg->prach_config.restricted_set_config.tl.tag = NFAPI_NR_CONFIG_RESTRICTED_SET_CONFIG_TAG;
  cfg->num_tlv++;
  cfg->prach_config.prach_ConfigurationIndex.value = rach_ConfigCommon->rach_ConfigGeneric.prach_ConfigurationIndex;
  cfg->prach_config.prach_ConfigurationIndex.tl.tag = NFAPI_NR_CONFIG_PRACH_CONFIG_INDEX_TAG;
  cfg->num_tlv++;

  switch (rach_ConfigCommon->rach_ConfigGeneric.msg1_FDM) {
645
    case 0:
646 647
      cfg->prach_config.num_prach_fd_occasions.value = 1;
      break;
648
    case 1:
649 650
      cfg->prach_config.num_prach_fd_occasions.value = 2;
      break;
651
    case 2:
652 653
      cfg->prach_config.num_prach_fd_occasions.value = 4;
      break;
654
    case 3:
655 656 657
      cfg->prach_config.num_prach_fd_occasions.value = 8;
      break;
    default:
658
      AssertFatal(1 == 0, "msg1 FDM identifier %ld undefined (0,1,2,3) \n", rach_ConfigCommon->rach_ConfigGeneric.msg1_FDM);
659 660 661
  }
  cfg->prach_config.num_prach_fd_occasions.tl.tag = NFAPI_NR_CONFIG_NUM_PRACH_FD_OCCASIONS_TAG;
  cfg->num_tlv++;
662

663 664 665
  cfg->prach_config.prach_ConfigurationIndex.value = rach_ConfigCommon->rach_ConfigGeneric.prach_ConfigurationIndex;
  cfg->prach_config.prach_ConfigurationIndex.tl.tag = NFAPI_NR_CONFIG_PRACH_CONFIG_INDEX_TAG;
  cfg->num_tlv++;
kn.raju's avatar
kn.raju committed
666

667
  cfg->prach_config.num_prach_fd_occasions_list = (nfapi_nr_num_prach_fd_occasions_t *)malloc(
668
       cfg->prach_config.num_prach_fd_occasions.value * sizeof(nfapi_nr_num_prach_fd_occasions_t));
669
  for (int i = 0; i < cfg->prach_config.num_prach_fd_occasions.value; i++) {
670 671
    nfapi_nr_num_prach_fd_occasions_t *prach_fd_occasion = &cfg->prach_config.num_prach_fd_occasions_list[i];
    // prach_fd_occasion->num_prach_fd_occasions = i;
672
    if (cfg->prach_config.prach_sequence_length.value)
673
      prach_fd_occasion->prach_root_sequence_index.value = rach_ConfigCommon->prach_RootSequenceIndex.choice.l139;
674
    else
675 676
      prach_fd_occasion->prach_root_sequence_index.value = rach_ConfigCommon->prach_RootSequenceIndex.choice.l839;
    prach_fd_occasion->prach_root_sequence_index.tl.tag = NFAPI_NR_CONFIG_PRACH_ROOT_SEQUENCE_INDEX_TAG;
677
    cfg->num_tlv++;
678 679 680 681 682 683
    prach_fd_occasion->k1.value =
        NRRIV2PRBOFFSET(scc->uplinkConfigCommon->initialUplinkBWP->genericParameters.locationAndBandwidth, MAX_BWP_SIZE)
        + rach_ConfigCommon->rach_ConfigGeneric.msg1_FrequencyStart
        + (get_N_RA_RB(cfg->prach_config.prach_sub_c_spacing.value,
                       frequencyInfoUL->scs_SpecificCarrierList.list.array[0]->subcarrierSpacing)
           * i);
rmagueta's avatar
rmagueta committed
684
    if (get_softmodem_params()->sa) {
685 686 687 688 689 690
      prach_fd_occasion->k1.value =
          NRRIV2PRBOFFSET(scc->uplinkConfigCommon->initialUplinkBWP->genericParameters.locationAndBandwidth, MAX_BWP_SIZE)
          + rach_ConfigCommon->rach_ConfigGeneric.msg1_FrequencyStart
          + (get_N_RA_RB(cfg->prach_config.prach_sub_c_spacing.value,
                         frequencyInfoUL->scs_SpecificCarrierList.list.array[0]->subcarrierSpacing)
             * i);
rmagueta's avatar
rmagueta committed
691
    } else {
692 693 694 695
      prach_fd_occasion->k1.value = rach_ConfigCommon->rach_ConfigGeneric.msg1_FrequencyStart
                                    + (get_N_RA_RB(cfg->prach_config.prach_sub_c_spacing.value,
                                                   frequencyInfoUL->scs_SpecificCarrierList.list.array[0]->subcarrierSpacing)
                                       * i);
rmagueta's avatar
rmagueta committed
696
    }
697
    prach_fd_occasion->k1.tl.tag = NFAPI_NR_CONFIG_K1_TAG;
698
    cfg->num_tlv++;
699 700
    prach_fd_occasion->prach_zero_corr_conf.value = rach_ConfigCommon->rach_ConfigGeneric.zeroCorrelationZoneConfig;
    prach_fd_occasion->prach_zero_corr_conf.tl.tag = NFAPI_NR_CONFIG_PRACH_ZERO_CORR_CONF_TAG;
701
    cfg->num_tlv++;
702
    prach_fd_occasion->num_root_sequences.value = compute_nr_root_seq(rach_ConfigCommon, nb_preambles, frame_type, frequency_range);
703
    prach_fd_occasion->num_root_sequences.tl.tag = NFAPI_NR_CONFIG_NUM_ROOT_SEQUENCES_TAG;
704
    cfg->num_tlv++;
705 706 707
    prach_fd_occasion->num_unused_root_sequences.tl.tag = NFAPI_NR_CONFIG_NUM_UNUSED_ROOT_SEQUENCES_TAG;
    prach_fd_occasion->num_unused_root_sequences.value = 0;
    cfg->num_tlv++;
708
  }
709

710 711 712
  cfg->prach_config.ssb_per_rach.value = rach_ConfigCommon->ssb_perRACH_OccasionAndCB_PreamblesPerSSB->present - 1;
  cfg->prach_config.ssb_per_rach.tl.tag = NFAPI_NR_CONFIG_SSB_PER_RACH_TAG;
  cfg->num_tlv++;
713

714
  // SSB Table Configuration
715

716
  cfg->ssb_table.ssb_offset_point_a.value =
717 718 719 720
       get_ssb_offset_to_pointA(*scc->downlinkConfigCommon->frequencyInfoDL->absoluteFrequencySSB,
                                scc->downlinkConfigCommon->frequencyInfoDL->absoluteFrequencyPointA,
                                *scc->ssbSubcarrierSpacing,
                                frequency_range);
721 722 723 724 725 726
  cfg->ssb_table.ssb_offset_point_a.tl.tag = NFAPI_NR_CONFIG_SSB_OFFSET_POINT_A_TAG;
  cfg->num_tlv++;
  cfg->ssb_table.ssb_period.value = *scc->ssb_periodicityServingCell;
  cfg->ssb_table.ssb_period.tl.tag = NFAPI_NR_CONFIG_SSB_PERIOD_TAG;
  cfg->num_tlv++;
  cfg->ssb_table.ssb_subcarrier_offset.value =
727
       get_ssb_subcarrier_offset(*scc->downlinkConfigCommon->frequencyInfoDL->absoluteFrequencySSB,
728 729
                                 scc->downlinkConfigCommon->frequencyInfoDL->absoluteFrequencyPointA,
                                 *scc->ssbSubcarrierSpacing);
730 731 732 733 734 735 736 737 738 739

  AssertFatal(cfg->ssb_table.ssb_subcarrier_offset.value < 16,
              "cannot handle ssb_subcarrier_offset %d resulting from Point A %ld SSB %ld: please increase dl_absoluteFrequencyPointA "
              "in the config by 16\n",
              cfg->ssb_table.ssb_subcarrier_offset.value,
              scc->downlinkConfigCommon->frequencyInfoDL->absoluteFrequencyPointA,
              *scc->downlinkConfigCommon->frequencyInfoDL->absoluteFrequencySSB);
  cfg->ssb_table.ssb_subcarrier_offset.tl.tag = NFAPI_NR_CONFIG_SSB_SUBCARRIER_OFFSET_TAG;
  cfg->num_tlv++;

740 741 742 743 744 745
  uint8_t *mib_payload = nrmac->common_channels[0].MIB_pdu;
  uint32_t mib = (mib_payload[2] << 16) | (mib_payload[1] << 8) | mib_payload[0];
  cfg->ssb_table.MIB.tl.tag = NFAPI_NR_CONFIG_MIB_TAG;
  cfg->ssb_table.MIB.value = mib;
  cfg->num_tlv++;

746 747 748 749 750 751 752 753
  nrmac->ssb_SubcarrierOffset = cfg->ssb_table.ssb_subcarrier_offset.value;
  nrmac->ssb_OffsetPointA = cfg->ssb_table.ssb_offset_point_a.value;
  LOG_I(NR_MAC,
        "ssb_OffsetPointA %d, ssb_SubcarrierOffset %d\n",
        cfg->ssb_table.ssb_offset_point_a.value,
        cfg->ssb_table.ssb_subcarrier_offset.value);

  switch (scc->ssb_PositionsInBurst->present) {
754
    case 1:
755
      cfg->ssb_table.ssb_mask_list[0].ssb_mask.value = scc->ssb_PositionsInBurst->choice.shortBitmap.buf[0] << 24;
756 757
      cfg->ssb_table.ssb_mask_list[1].ssb_mask.value = 0;
      break;
758 759
    case 2:
      cfg->ssb_table.ssb_mask_list[0].ssb_mask.value = ((uint32_t)scc->ssb_PositionsInBurst->choice.mediumBitmap.buf[0]) << 24;
760 761
      cfg->ssb_table.ssb_mask_list[1].ssb_mask.value = 0;
      break;
762
    case 3:
763 764
      cfg->ssb_table.ssb_mask_list[0].ssb_mask.value = 0;
      cfg->ssb_table.ssb_mask_list[1].ssb_mask.value = 0;
765 766 767 768 769
      for (int i = 0; i < 4; i++) {
        cfg->ssb_table.ssb_mask_list[0].ssb_mask.value += (uint32_t)scc->ssb_PositionsInBurst->choice.longBitmap.buf[3 - i]
                                                          << i * 8;
        cfg->ssb_table.ssb_mask_list[1].ssb_mask.value += (uint32_t)scc->ssb_PositionsInBurst->choice.longBitmap.buf[7 - i]
                                                          << i * 8;
770 771 772
      }
      break;
    default:
773
      AssertFatal(1 == 0, "SSB bitmap size value %d undefined (allowed values 1,2,3) \n", scc->ssb_PositionsInBurst->present);
774
  }
775

776 777 778
  cfg->ssb_table.ssb_mask_list[0].ssb_mask.tl.tag = NFAPI_NR_CONFIG_SSB_MASK_TAG;
  cfg->ssb_table.ssb_mask_list[1].ssb_mask.tl.tag = NFAPI_NR_CONFIG_SSB_MASK_TAG;
  cfg->num_tlv += 2;
779

780 781 782 783 784
  // logical antenna ports
  int num_pdsch_antenna_ports = pdsch_AntennaPorts.N1 * pdsch_AntennaPorts.N2 * pdsch_AntennaPorts.XP;
  cfg->carrier_config.num_tx_ant.value = num_pdsch_antenna_ports;
  AssertFatal(num_pdsch_antenna_ports > 0 && num_pdsch_antenna_ports < 33, "pdsch_AntennaPorts in 1...32\n");
  cfg->carrier_config.num_tx_ant.tl.tag = NFAPI_NR_CONFIG_NUM_TX_ANT_TAG;
785

786 787
  int num_ssb = 0;
  for (int i = 0; i < 32; i++) {
788
    cfg->ssb_table.ssb_beam_id_list[i].beam_id.tl.tag = NFAPI_NR_CONFIG_BEAM_ID_TAG;
789
    if ((cfg->ssb_table.ssb_mask_list[0].ssb_mask.value >> (31 - i)) & 1) {
790 791 792 793
      cfg->ssb_table.ssb_beam_id_list[i].beam_id.value = num_ssb;
      num_ssb++;
    }
    cfg->num_tlv++;
794 795
  }
  for (int i = 0; i < 32; i++) {
796 797 798
    cfg->ssb_table.ssb_beam_id_list[32 + i].beam_id.tl.tag = NFAPI_NR_CONFIG_BEAM_ID_TAG;
    if ((cfg->ssb_table.ssb_mask_list[1].ssb_mask.value >> (31 - i)) & 1) {
      cfg->ssb_table.ssb_beam_id_list[32 + i].beam_id.value = num_ssb;
799 800 801
      num_ssb++;
    }
    cfg->num_tlv++;
802
  }
803

804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821
  cfg->carrier_config.num_rx_ant.value = pusch_AntennaPorts;
  AssertFatal(pusch_AntennaPorts > 0 && pusch_AntennaPorts < 13, "pusch_AntennaPorts in 1...12\n");
  cfg->carrier_config.num_rx_ant.tl.tag = NFAPI_NR_CONFIG_NUM_RX_ANT_TAG;
  LOG_I(NR_MAC,
        "Set RX antenna number to %d, Set TX antenna number to %d (num ssb %d: %x,%x)\n",
        cfg->carrier_config.num_tx_ant.value,
        cfg->carrier_config.num_rx_ant.value,
        num_ssb,
        cfg->ssb_table.ssb_mask_list[0].ssb_mask.value,
        cfg->ssb_table.ssb_mask_list[1].ssb_mask.value);
  AssertFatal(cfg->carrier_config.num_tx_ant.value > 0,
              "carrier_config.num_tx_ant.value %d !\n",
              cfg->carrier_config.num_tx_ant.value);
  cfg->num_tlv++;
  cfg->num_tlv++;

  // TDD Table Configuration
  if (cfg->cell_config.frame_duplex_type.value == TDD) {
822 823 824 825 826 827
    cfg->tdd_table.tdd_period.tl.tag = NFAPI_NR_CONFIG_TDD_PERIOD_TAG;
    cfg->num_tlv++;
    if (scc->tdd_UL_DL_ConfigurationCommon->pattern1.ext1 == NULL) {
      cfg->tdd_table.tdd_period.value = scc->tdd_UL_DL_ConfigurationCommon->pattern1.dl_UL_TransmissionPeriodicity;
    } else {
      AssertFatal(scc->tdd_UL_DL_ConfigurationCommon->pattern1.ext1->dl_UL_TransmissionPeriodicity_v1530 != NULL,
828 829
                  "In %s: scc->tdd_UL_DL_ConfigurationCommon->pattern1.ext1->dl_UL_TransmissionPeriodicity_v1530 is null\n",
                  __FUNCTION__);
830 831 832
      cfg->tdd_table.tdd_period.value = *scc->tdd_UL_DL_ConfigurationCommon->pattern1.ext1->dl_UL_TransmissionPeriodicity_v1530;
    }
    LOG_I(NR_MAC, "Setting TDD configuration period to %d\n", cfg->tdd_table.tdd_period.value);
833
    int periods_per_frame = set_tdd_config_nr(cfg,
834
                                              frequencyInfoUL->scs_SpecificCarrierList.list.array[0]->subcarrierSpacing,
835 836 837 838 839 840
                                              scc->tdd_UL_DL_ConfigurationCommon->pattern1.nrofDownlinkSlots,
                                              scc->tdd_UL_DL_ConfigurationCommon->pattern1.nrofDownlinkSymbols,
                                              scc->tdd_UL_DL_ConfigurationCommon->pattern1.nrofUplinkSlots,
                                              scc->tdd_UL_DL_ConfigurationCommon->pattern1.nrofUplinkSymbols);

    if (periods_per_frame < 0)
841
      LOG_E(NR_MAC, "TDD configuration can not be done\n");
842
    else {
843 844
      LOG_I(NR_MAC, "TDD has been properly configurated\n");
      nrmac->tdd_beam_association = (int16_t *)malloc16(periods_per_frame * sizeof(int16_t));
845
    }
846 847 848 849
  }

  // precoding matrix configuration (to be improved)
  cfg->pmi_list = init_DL_MIMO_codebook(nrmac, pdsch_AntennaPorts);
850 851
}

852
void nr_mac_config_scc(gNB_MAC_INST *nrmac, NR_ServingCellConfigCommon_t *scc, const nr_mac_config_t *config)
853 854 855
{
  DevAssert(nrmac != NULL);
  DevAssert(scc != NULL);
856 857 858
  DevAssert(config != NULL);
  //NR_SCHED_LOCK(&nrmac->sched_lock);

859 860 861 862 863 864 865 866 867 868 869 870 871 872
  AssertFatal(scc->ssb_PositionsInBurst->present > 0 && scc->ssb_PositionsInBurst->present < 4,
              "SSB Bitmap type %d is not valid\n",
              scc->ssb_PositionsInBurst->present);

  int n = nr_slots_per_frame[*scc->ssbSubcarrierSpacing];
  if (*scc->ssbSubcarrierSpacing == 0)
    n <<= 1; // to have enough room for feedback possibly beyond the frame we need a larger array at 15kHz SCS
  nrmac->common_channels[0].vrb_map_UL = calloc(n * MAX_BWP_SIZE, sizeof(uint16_t));
  nrmac->vrb_map_UL_size = n;
  AssertFatal(nrmac->common_channels[0].vrb_map_UL,
              "could not allocate memory for RC.nrmac[]->common_channels[0].vrb_map_UL\n");

  LOG_I(NR_MAC, "Configuring common parameters from NR ServingCellConfig\n");

873
  config_common(nrmac, config->pdsch_AntennaPorts, config->pusch_AntennaPorts, scc);
874

875 876
  if (NFAPI_MODE == NFAPI_MONOLITHIC) {
    // nothing to be sent in the other cases
877 878 879 880
    NR_PHY_Config_t phycfg = {.Mod_id = 0, .CC_id = 0, .cfg = &nrmac->config[0]};
    DevAssert(nrmac->if_inst->NR_PHY_config_req);
    nrmac->if_inst->NR_PHY_config_req(&phycfg);
  }
881

882 883 884 885 886 887 888 889 890 891 892 893 894 895 896
  find_SSB_and_RO_available(nrmac);

  const NR_TDD_UL_DL_Pattern_t *tdd = scc->tdd_UL_DL_ConfigurationCommon ? &scc->tdd_UL_DL_ConfigurationCommon->pattern1 : NULL;

  int nr_slots_period = n;
  int nr_dl_slots = n;
  int nr_ulstart_slot = 0;
  if (tdd) {
    nr_dl_slots = tdd->nrofDownlinkSlots + (tdd->nrofDownlinkSymbols != 0);
    nr_ulstart_slot = get_first_ul_slot(tdd->nrofDownlinkSlots, tdd->nrofDownlinkSymbols, tdd->nrofUplinkSymbols);
    nr_slots_period /= get_nb_periods_per_frame(tdd->dl_UL_TransmissionPeriodicity);
  } else {
    // if TDD configuration is not present and the band is not FDD, it means it is a dynamic TDD configuration
    AssertFatal(nrmac->common_channels[0].frame_type == FDD,"Dynamic TDD not handled yet\n");
  }
897

898 899 900
  for (int slot = 0; slot < n; ++slot) {
    nrmac->dlsch_slot_bitmap[slot / 64] |= (uint64_t)((slot % nr_slots_period) < nr_dl_slots) << (slot % 64);
    nrmac->ulsch_slot_bitmap[slot / 64] |= (uint64_t)((slot % nr_slots_period) >= nr_ulstart_slot) << (slot % 64);
901

902
    LOG_D(NR_MAC,
903 904 905 906 907
          "slot %d DL %d UL %d\n",
          slot,
          (nrmac->dlsch_slot_bitmap[slot / 64] & ((uint64_t)1 << (slot % 64))) != 0,
          (nrmac->ulsch_slot_bitmap[slot / 64] & ((uint64_t)1 << (slot % 64))) != 0);
  }
908

909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926
  if (get_softmodem_params()->phy_test) {
    nrmac->pre_processor_dl = nr_preprocessor_phytest;
    nrmac->pre_processor_ul = nr_ul_preprocessor_phytest;
  } else {
    nrmac->pre_processor_dl = nr_init_fr1_dlsch_preprocessor(0);
    nrmac->pre_processor_ul = nr_init_fr1_ulsch_preprocessor(0);
  }

  if (get_softmodem_params()->sa > 0) {
    NR_COMMON_channels_t *cc = &nrmac->common_channels[0];
    for (int n = 0; n < NR_NB_RA_PROC_MAX; n++) {
      NR_RA_t *ra = &cc->ra[n];
      ra->cfra = false;
      ra->rnti = 0;
      ra->preambles.num_preambles = MAX_NUM_NR_PRACH_PREAMBLES;
      ra->preambles.preamble_list = malloc(MAX_NUM_NR_PRACH_PREAMBLES * sizeof(*ra->preambles.preamble_list));
      for (int i = 0; i < MAX_NUM_NR_PRACH_PREAMBLES; i++)
        ra->preambles.preamble_list[i] = i;
927
    }
928
  }
929
  //NR_SCHED_UNLOCK(&nrmac->sched_lock);
930 931
}

932 933 934 935 936 937 938 939 940 941 942 943
void nr_mac_configure_sib1(gNB_MAC_INST *nrmac, const f1ap_plmn_t *plmn, uint64_t cellID, int tac)
{
  AssertFatal(get_softmodem_params()->sa > 0, "error: SIB1 only applicable for SA\n");

  NR_COMMON_channels_t *cc = &nrmac->common_channels[0];
  NR_ServingCellConfigCommon_t *scc = cc->ServingCellConfigCommon;
  NR_BCCH_DL_SCH_Message_t *sib1 = get_SIB1_NR(scc, plmn, cellID, tac);
  cc->sib1 = sib1;
  cc->sib1_bcch_length = encode_SIB1_NR(sib1, cc->sib1_bcch_pdu, sizeof(cc->sib1_bcch_pdu));
  AssertFatal(cc->sib1_bcch_length > 0, "could not encode SIB1\n");
}

944 945 946 947 948
bool nr_mac_add_test_ue(gNB_MAC_INST *nrmac, uint32_t rnti, NR_CellGroupConfig_t *CellGroup)
{
  DevAssert(nrmac != NULL);
  DevAssert(CellGroup != NULL);
  DevAssert(get_softmodem_params()->phy_test);
949
  NR_SCHED_LOCK(&nrmac->sched_lock);
950 951 952 953

  NR_UE_info_t* UE = add_new_nr_ue(nrmac, rnti, CellGroup);
  if (UE) {
    LOG_I(NR_MAC,"Force-added new UE %x with initial CellGroup\n", rnti);
954
    process_CellGroup(CellGroup, UE);
955 956 957
  } else {
    LOG_E(NR_MAC,"Error adding UE %04x\n", rnti);
  }
958 959
  NR_SCHED_UNLOCK(&nrmac->sched_lock);
  return UE != NULL;
960 961
}

962
bool nr_mac_prepare_ra_ue(gNB_MAC_INST *nrmac, uint32_t rnti, NR_CellGroupConfig_t *CellGroup)
963 964 965
{
  DevAssert(nrmac != NULL);
  DevAssert(CellGroup != NULL);
966
  NR_SCHED_ENSURE_LOCKED(&nrmac->sched_lock);
967 968 969 970 971 972 973

  // NSA case: need to pre-configure CFRA
  const int CC_id = 0;
  NR_COMMON_channels_t *cc = &nrmac->common_channels[CC_id];
  uint8_t ra_index = 0;
  /* checking for free RA process */
  for(; ra_index < NR_NB_RA_PROC_MAX; ra_index++) {
974 975
    if ((cc->ra[ra_index].ra_state == nrRA_gNB_IDLE) && (!cc->ra[ra_index].cfra))
      break;
976 977 978 979 980 981
  }
  if (ra_index == NR_NB_RA_PROC_MAX) {
    LOG_E(NR_MAC, "RA processes are not available for CFRA RNTI %04x\n", rnti);
    return false;
  }
  NR_RA_t *ra = &cc->ra[ra_index];
982 983 984 985 986 987 988 989 990 991 992 993 994
  ra->cfra = true;
  ra->rnti = rnti;
  ra->CellGroup = CellGroup;
  struct NR_CFRA *cfra = CellGroup->spCellConfig->reconfigurationWithSync->rach_ConfigDedicated->choice.uplink->cfra;
  uint8_t num_preamble = cfra->resources.choice.ssb->ssb_ResourceList.list.count;
  ra->preambles.num_preambles = num_preamble;
  ra->preambles.preamble_list = calloc(ra->preambles.num_preambles, sizeof(*ra->preambles.preamble_list));
  for (int i = 0; i < cc->num_active_ssb; i++) {
    for (int j = 0; j < num_preamble; j++) {
      if (cc->ssb_index[i] == cfra->resources.choice.ssb->ssb_ResourceList.list.array[j]->ssb) {
        // one dedicated preamble for each beam
        ra->preambles.preamble_list[i] = cfra->resources.choice.ssb->ssb_ResourceList.list.array[j]->ra_PreambleIndex;
        break;
995 996
      }
    }
997
  }
998
  LOG_I(NR_MAC, "Added new %s process for UE RNTI %04x with initial CellGroup\n", ra->cfra ? "CFRA" : "CBRA", rnti);
999 1000
  return true;
}
Laurent THOMAS's avatar
Laurent THOMAS committed
1001

1002 1003 1004 1005 1006 1007 1008
/* Prepare a new CellGroupConfig to be applied for this UE. We cannot
 * immediatly apply it, as we have to wait for the reconfiguration through RRC.
 * This function sets up everything to apply the reconfiguration. Later, we
 * will trigger the timer with nr_mac_enable_ue_rrc_processing_timer(); upon
 * expiry nr_mac_apply_cellgroup() will apply the CellGroupConfig (radio config
 * etc). */
bool nr_mac_prepare_cellgroup_update(gNB_MAC_INST *nrmac, NR_UE_info_t *UE, NR_CellGroupConfig_t *CellGroup)
1009 1010
{
  DevAssert(nrmac != NULL);
1011
  DevAssert(UE != NULL);
1012
  DevAssert(CellGroup != NULL);
1013

1014 1015
  /* we assume that this function is mutex-protected from outside */
  NR_SCHED_ENSURE_LOCKED(&nrmac->sched_lock);
1016

1017
  process_CellGroup(CellGroup, UE);
1018 1019
  UE->reconfigCellGroup = CellGroup;
  UE->expect_reconfiguration = true;
1020 1021 1022

  return true;
}