eNB_scheduler_primitives.c 186 KB
Newer Older
1 2 3 4 5
/*
 * 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
6
 * the OAI Public License, Version 1.1  (the "License"); you may not use this file
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 * 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
 */

22 23
/*! \file eNB_scheduler_primitives.c
 * \brief primitives used by eNB for BCH, RACH, ULSCH, DLSCH scheduling
24 25 26
 * \author  Navid Nikaein and Raymond Knopp
 * \date 2010 - 2014
 * \email: navid.nikaein@eurecom.fr
27
 * \version 1.0
28 29 30 31 32 33
 * @ingroup _mac

 */

#include "assertions.h"

34 35
#include "LAYER2/MAC/mac.h"
#include "LAYER2/MAC/mac_extern.h"
36

37
#include "LAYER2/MAC/mac_proto.h"
38
#include "common/utils/LOG/log.h"
frtabu's avatar
frtabu committed
39
#include "nfapi/oai_integration/vendor_ext.h"
40
#include "common/utils/LOG/vcd_signal_dumper.h"
41 42 43 44
#include "UTIL/OPT/opt.h"
#include "OCG.h"
#include "OCG_extern.h"

45
#include "RRC/LTE/rrc_extern.h"
46 47 48 49 50 51
#include "RRC/L2_INTERFACE/openair_rrc_L2_interface.h"

//#include "LAYER2/MAC/pre_processor.c"
#include "pdcp.h"

#if defined(ENABLE_ITTI)
52
  #include "intertask_interface.h"
53 54
#endif

Cedric Roux's avatar
Cedric Roux committed
55 56
#include "T.h"

57 58
#define ENABLE_MAC_PAYLOAD_DEBUG
#define DEBUG_eNB_SCHEDULER 1
59
extern uint16_t frame_cnt;
60

61
#include "common/ran_context.h"
62
#include "SCHED/sched_common.h"
63 64

extern RAN_CONTEXT_t RC;
frtabu's avatar
frtabu committed
65

66

67
//------------------------------------------------------------------------------
68 69 70
int
choose(int n,
       int k)
71
//------------------------------------------------------------------------------
Cedric Roux's avatar
Cedric Roux committed
72
{
73 74 75
  int res = 1;
  int res2 = 1;
  int i;
76

77 78
  if (k > n)
    return (0);
79

80 81
  if (n == k)
    return (1);
82

83 84
  for (i = n; i > k; i--)
    res *= i;
85

86 87
  for (i = 2; i <= (n - k); i++)
    res2 *= i;
88

89
  return (res / res2);
90 91
}

92
//------------------------------------------------------------------------------
93
// Patented algorithm from Yang et al, US Patent 2009, "Channel Quality Indexing and Reverse Indexing"
94 95 96 97
void reverse_index(int N,
                   int M,
                   int r,
                   int *v)
98
//------------------------------------------------------------------------------
99
{
100 101 102 103 104 105 106 107
  int BaseValue = 0;
  int IncreaseValue, ThresholdValue;
  int sumV;
  int i;
  r = choose(N, M) - 1 - r;
  memset((void *) v, 0, M * sizeof(int));
  sumV = 0;
  i = M;
108

109
  while (i > 0 && r > 0) {
110
    IncreaseValue = choose(N - M + 1 - sumV - v[i - 1] + i - 2,
111
                           i - 1);
112
    ThresholdValue = BaseValue + IncreaseValue;
113

114 115 116 117
    if (r >= ThresholdValue) {
      v[i - 1]++;
      BaseValue = ThresholdValue;
    } else {
118 119
      r -= BaseValue;
      sumV += v[--i];
120
      BaseValue = 0;
121
    }
122
  }
123
}
Cedric Roux's avatar
Cedric Roux committed
124

125
//------------------------------------------------------------------------------
Stefan's avatar
Stefan committed
126
int
127
to_prb(int dl_Bandwidth)
128
//------------------------------------------------------------------------------
Cedric Roux's avatar
Cedric Roux committed
129
{
130 131 132
  int prbmap[6] = { 6, 15, 25, 50, 75, 100 };
  AssertFatal(dl_Bandwidth < 6, "dl_Bandwidth is 0..5\n");
  return (prbmap[dl_Bandwidth]);
133 134
}

135
//------------------------------------------------------------------------------
136 137
int
to_rbg(int dl_Bandwidth)
138
//------------------------------------------------------------------------------
Cedric Roux's avatar
Cedric Roux committed
139
{
140 141 142
  int rbgmap[6] = { 6, 8, 13, 17, 19, 25 };
  AssertFatal(dl_Bandwidth < 6, "dl_Bandwidth is 0..5\n");
  return (rbgmap[dl_Bandwidth]);
143
}
144

145
//------------------------------------------------------------------------------
146 147
int
get_phich_resource_times6(COMMON_channels_t *cc)
148
//------------------------------------------------------------------------------
Cedric Roux's avatar
Cedric Roux committed
149
{
150 151 152
  int phichmap[4] = { 1, 3, 6, 12 };
  AssertFatal(cc != NULL, "cc is null\n");
  AssertFatal(cc->mib != NULL, "cc->mib is null\n");
153 154 155 156
  int phich_Resource = (int) cc->mib->message.phich_Config.phich_Resource;
  AssertFatal(phich_Resource >= 0 && phich_Resource < 4, "phich_Resource %d not in 0..3\n",
              phich_Resource);
  return (phichmap[phich_Resource]);
157 158
}

159
//------------------------------------------------------------------------------
160 161 162 163
uint16_t
mac_computeRIV(uint16_t N_RB_DL,
               uint16_t RBstart,
               uint16_t Lcrbs)
164
//------------------------------------------------------------------------------
Cedric Roux's avatar
Cedric Roux committed
165
{
166 167 168
  if (Lcrbs <= (1 + (N_RB_DL >> 1))) {
    return (N_RB_DL * (Lcrbs - 1)) + RBstart;
  }
frtabu's avatar
frtabu committed
169

170
  return (N_RB_DL * (N_RB_DL + 1 - Lcrbs)) + (N_RB_DL - 1 - RBstart);
171 172
}

173
//------------------------------------------------------------------------------
174 175
uint8_t
getQm(uint8_t mcs)
176
//------------------------------------------------------------------------------
Cedric Roux's avatar
Cedric Roux committed
177
{
178 179
  if (mcs < 10)      return (2);
  else if (mcs < 17) return (4);
frtabu's avatar
frtabu committed
180

Stefan's avatar
Stefan committed
181
  return (6);
182 183
}

184
//------------------------------------------------------------------------------
185
void
186
get_Msg3alloc(COMMON_channels_t *cc,
187 188 189
              sub_frame_t       current_subframe,
              frame_t           current_frame,
              frame_t           *frame,
190
              sub_frame_t       *subframe)
191
//------------------------------------------------------------------------------
192
{
193
  // Fill in other TDD Configuration!!!!
194
  int subframeAssignment;
195

196
  if (cc->tdd_Config == NULL) { // FDD
197
    *subframe = current_subframe + 6;
198

199 200 201 202 203 204
    if (*subframe > 9) {
      *subframe = *subframe - 10;
      *frame = (current_frame + 1) & 1023;
    } else {
      *frame = current_frame;
    }
205
  } else {      // TDD
206
    subframeAssignment = (int) cc->tdd_Config->subframeAssignment;
frtabu's avatar
frtabu committed
207

208
    if (subframeAssignment == 1) {
209
      switch (current_subframe) {
210 211 212 213
        case 0:
          *subframe = 7;
          *frame = current_frame;
          break;
214

215 216 217 218
        case 4:
          *subframe = 2;
          *frame = (current_frame + 1) & 1023;
          break;
219

220 221 222 223 224 225 226 227 228
        case 5:
          *subframe = 2;
          *frame = (current_frame + 1) & 1023;
          break;

        case 9:
          *subframe = 7;
          *frame = (current_frame + 1) & 1023;
          break;
229
      }
230
    } else if (subframeAssignment == 3) {
231
      switch (current_subframe) {
232 233 234 235 236 237
        case 0:
        case 5:
        case 6:
          *subframe = 2;
          *frame = (current_frame + 1) & 1023;
          break;
238

239 240 241 242
        case 7:
          *subframe = 3;
          *frame = (current_frame + 1) & 1023;
          break;
243

244 245 246 247
        case 8:
          *subframe = 4;
          *frame = (current_frame + 1) & 1023;
          break;
248

249 250 251 252
        case 9:
          *subframe = 2;
          *frame = (current_frame + 2) & 1023;
          break;
253
      }
254
    } else if (subframeAssignment == 4) {
255
      switch (current_subframe) {
256 257 258 259 260 261 262
        case 0:
        case 4:
        case 5:
        case 6:
          *subframe = 2;
          *frame = (current_frame + 1) & 1023;
          break;
263

264 265 266 267
        case 7:
          *subframe = 3;
          *frame = (current_frame + 1) & 1023;
          break;
268

269 270 271 272 273
        case 8:
        case 9:
          *subframe = 2;
          *frame = (current_frame + 2) & 1023;
          break;
274
      }
275
    } else if (subframeAssignment == 5) {
276
      switch (current_subframe) {
277 278 279 280 281 282 283
        case 0:
        case 4:
        case 5:
        case 6:
          *subframe = 2;
          *frame = (current_frame + 1) & 1023;
          break;
284

285 286 287 288 289 290
        case 7:
        case 8:
        case 9:
          *subframe = 2;
          *frame = (current_frame + 2) & 1023;
          break;
291
      }
Cedric Roux's avatar
Cedric Roux committed
292
    }
293
  }
frtabu's avatar
frtabu committed
294

295
  return;
296 297
}

298
//------------------------------------------------------------------------------
299
void
300 301 302
get_Msg3allocret(COMMON_channels_t *cc,
                 sub_frame_t current_subframe,
                 frame_t current_frame,
303 304
                 frame_t *frame,
                 sub_frame_t *subframe)
305
//------------------------------------------------------------------------------
306
{
307 308
  int subframeAssignment;

309
  if (cc->tdd_Config == NULL) { //FDD
310 311
    /* always retransmit in n+8 */
    *subframe = current_subframe + 8;
312

313 314 315
    if (*subframe > 9) {
      *subframe = *subframe - 10;
      *frame = (current_frame + 1) & 1023;
316
    } else {
317
      *frame = current_frame;
318
    }
319
  } else {
320
    subframeAssignment = (int) cc->tdd_Config->subframeAssignment;
frtabu's avatar
frtabu committed
321

322
    if (subframeAssignment == 1) {
323 324 325 326 327
      // original PUSCH in 2, PHICH in 6 (S), ret in 2
      // original PUSCH in 3, PHICH in 9, ret in 3
      // original PUSCH in 7, PHICH in 1 (S), ret in 7
      // original PUSCH in 8, PHICH in 4, ret in 8
      *frame = (current_frame + 1) & 1023;
328
    } else if (subframeAssignment == 3) {
329 330 331 332
      // original PUSCH in 2, PHICH in 8, ret in 2 next frame
      // original PUSCH in 3, PHICH in 9, ret in 3 next frame
      // original PUSCH in 4, PHICH in 0, ret in 4 next frame
      *frame = (current_frame + 1) & 1023;
333
    } else if (subframeAssignment == 4) {
334 335 336
      // original PUSCH in 2, PHICH in 8, ret in 2 next frame
      // original PUSCH in 3, PHICH in 9, ret in 3 next frame
      *frame = (current_frame + 1) & 1023;
337
    } else if (subframeAssignment == 5) {
338 339 340 341
      // original PUSCH in 2, PHICH in 8, ret in 2 next frame
      *frame = (current_frame + 1) & 1023;
    }
  }
frtabu's avatar
frtabu committed
342

343
  return;
344 345
}

346
//------------------------------------------------------------------------------
347
uint8_t
348
subframe2harqpid(COMMON_channels_t *cc,
349
                 frame_t frame,
350
                 sub_frame_t subframe)
351
//------------------------------------------------------------------------------
352
{
353
  AssertFatal(cc != NULL, "cc is null\n");
354

355 356
  uint8_t ret = 255;

357
  if (cc->tdd_Config == NULL) { // FDD
358
    ret = (((frame << 1) + subframe) & 7);
359
  } else {
360
    switch (cc->tdd_Config->subframeAssignment) {
361
      case 1:
362
        if (subframe == 2 || subframe == 3 || subframe == 7 || subframe == 8) {
Stefan's avatar
Stefan committed
363 364 365 366 367
          switch (subframe) {
            case 2:
            case 3:
              ret = (subframe - 2);
              break;
368

Stefan's avatar
Stefan committed
369 370 371 372
            case 7:
            case 8:
              ret = (subframe - 5);
              break;
373

Stefan's avatar
Stefan committed
374
            default:
375
              AssertFatal(1 == 0, "subframe2_harq_pid, Illegal subframe %d for TDD mode %d\n",
Stefan's avatar
Stefan committed
376 377 378 379
                          subframe,
                          (int) cc->tdd_Config->subframeAssignment);
              break;
          }
Stefan's avatar
Stefan committed
380
        }
frtabu's avatar
frtabu committed
381

382
        break;
383

384
      case 2:
385
        AssertFatal(subframe == 2 || subframe == 7, "subframe2_harq_pid, Illegal subframe %d for TDD mode %d\n",
Stefan's avatar
Stefan committed
386 387
                    subframe,
                    (int) cc->tdd_Config->subframeAssignment);
388 389
        ret = (subframe / 7);
        break;
390

391
      case 3:
392
        AssertFatal(subframe > 1 && subframe < 5, "subframe2_harq_pid, Illegal subframe %d for TDD mode %d\n",
Stefan's avatar
Stefan committed
393 394
                    subframe,
                    (int) cc->tdd_Config->subframeAssignment);
395 396
        ret = (subframe - 2);
        break;
397

398
      case 4:
399
        AssertFatal(subframe > 1 && subframe < 4, "subframe2_harq_pid, Illegal subframe %d for TDD mode %d\n",
Stefan's avatar
Stefan committed
400 401
                    subframe,
                    (int) cc->tdd_Config->subframeAssignment);
402 403
        ret = (subframe - 2);
        break;
404

405
      case 5:
406
        AssertFatal(subframe == 2, "subframe2_harq_pid, Illegal subframe %d for TDD mode %d\n",
Stefan's avatar
Stefan committed
407 408
                    subframe,
                    (int) cc->tdd_Config->subframeAssignment);
409 410
        ret = (subframe - 2);
        break;
411

412
      default:
413
        AssertFatal(1 == 0, "subframe2_harq_pid, Unsupported TDD mode %d\n",
414 415
                    (int) cc->tdd_Config->subframeAssignment);
        break;
416
    }
417
  }
418

419
  return ret;
420
}
421

422
//------------------------------------------------------------------------------
423
uint8_t
424
get_Msg3harqpid(COMMON_channels_t *cc,
425 426
                frame_t frame,
                sub_frame_t current_subframe)
427
//------------------------------------------------------------------------------
428
{
429 430
  uint8_t ul_subframe = 0;
  uint32_t ul_frame = 0;
431

432
  if (cc->tdd_Config == NULL) { // FDD
433
    ul_subframe = (current_subframe > 3) ? (current_subframe - 4) : (current_subframe + 6);
434 435 436
    ul_frame = (current_subframe > 3) ? ((frame + 1) & 1023) : frame;
  } else {
    switch (cc->tdd_Config->subframeAssignment) {
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
      case 1:
        switch (current_subframe) {
          case 9:
          case 0:
            ul_subframe = 7;
            break;

          case 5:
          case 7:
            ul_subframe = 2;
            break;
        }

        break;

      case 3:
        switch (current_subframe) {
          case 0:
          case 5:
          case 6:
            ul_subframe = 2;
            break;

          case 7:
            ul_subframe = 3;
            break;

          case 8:
            ul_subframe = 4;
            break;

          case 9:
            ul_subframe = 2;
            break;
        }

        break;

      case 4:
        switch (current_subframe) {
          case 0:
          case 5:
          case 6:
          case 8:
          case 9:
            ul_subframe = 2;
            break;

          case 7:
            ul_subframe = 3;
            break;
        }

        break;

492
      case 5:
493 494 495 496
        ul_subframe = 2;
        break;

      default:
497
        LOG_E(PHY, "get_Msg3_harq_pid: Unsupported TDD configuration %d\n",
498
              (int) cc->tdd_Config->subframeAssignment);
499
        AssertFatal(1 == 0, "get_Msg3_harq_pid: Unsupported TDD configuration");
500
        break;
501
    }
502
  }
Cedric Roux's avatar
Cedric Roux committed
503

504 505
  return (subframe2harqpid(cc,
                           ul_frame,
506
                           ul_subframe));
507
}
508

509
//------------------------------------------------------------------------------
510
uint32_t
511 512 513
pdcchalloc2ulframe(COMMON_channels_t *ccP,
                   uint32_t frame,
                   uint8_t n)
514
//------------------------------------------------------------------------------
515
{
516
  uint32_t ul_frame = (frame + (n >= 6 ? 1 : 0));
517

518 519 520 521 522 523 524 525 526 527 528 529 530
  if (ccP->tdd_Config) {
    if (ccP->tdd_Config->subframeAssignment == 1) {
      if (n == 1 || n == 6) {
        ul_frame = (frame + (n == 1 ? 0 : 1));
      }
    } else if (ccP->tdd_Config->subframeAssignment == 6) {
      if (n == 0 || n == 1 || n == 5 || n == 6) {
        ul_frame = (frame + (n >= 5 ? 1 : 0));
      } else if (n == 9) {
        ul_frame = (frame + 1);
      }
    }
  }
frtabu's avatar
frtabu committed
531

532 533
  LOG_D(PHY, "frame %d subframe %d: PUSCH frame = %d\n",
        frame,
534
        n,
535
        ul_frame);
536
  return ul_frame;
537
}
538

539
//------------------------------------------------------------------------------
540 541 542
uint8_t
pdcchalloc2ulsubframe(COMMON_channels_t *ccP,
                      uint8_t n)
543
//------------------------------------------------------------------------------
544
{
545
  uint8_t ul_subframe;
546

547
  if (ccP->tdd_Config && ccP->tdd_Config->subframeAssignment == 1 && (n == 1 || n == 6))  // tdd_config 0,1 SF 1,5
548
    ul_subframe = ((n + 6) % 10);
549
  else if (ccP->tdd_Config && ccP->tdd_Config->subframeAssignment == 6 && (n == 0 || n == 1 || n == 5 || n == 6))
550
    ul_subframe = ((n + 7) % 10);
551
  else if (ccP->tdd_Config && ccP->tdd_Config->subframeAssignment == 6 && n == 9) // tdd_config 6 SF 9
552 553 554
    ul_subframe = ((n + 5) % 10);
  else
    ul_subframe = ((n + 4) % 10);
555

556 557
  LOG_D(PHY, "subframe %d: PUSCH subframe = %d\n",
        n,
558
        ul_subframe);
559
  return ul_subframe;
560 561
}

562
//------------------------------------------------------------------------------
563 564 565
int
is_UL_sf(COMMON_channels_t *ccP,
         sub_frame_t subframeP)
566
//------------------------------------------------------------------------------
567
{
568 569
  // if FDD return dummy value
  if (ccP->tdd_Config == NULL)
570
    return 0;
571

572
  switch (ccP->tdd_Config->subframeAssignment) {
573 574 575 576 577 578
    case 1:
      switch (subframeP) {
        case 0:
        case 4:
        case 5:
        case 9:
579
          return 0;
580 581 582 583 584

        case 2:
        case 3:
        case 7:
        case 8:
585
          return 1;
586 587

        default:
588
          return 0;
589 590
      }

591
      break;
592

593
    case 3:
594 595
      if (subframeP <= 1 || subframeP >= 5)
        return 0;
596

597
      return 1;
598 599

    case 4:
600 601
      if (subframeP <= 1 || subframeP >= 4)
        return 0;
602

603
      return 1;
604 605

    case 5:
606 607
      if (subframeP <= 1 || subframeP >= 3)
        return 0;
608

609
      return 1;
610

611
    default:
612 613 614
      AssertFatal(1 == 0,  "subframe %d Unsupported TDD configuration %d\n",
                  subframeP,
                  (int) ccP->tdd_Config->subframeAssignment);
615 616
      break;
  }
frtabu's avatar
frtabu committed
617

618
  return 0;
619 620
}

621
//------------------------------------------------------------------------------
622 623 624
int
is_S_sf(COMMON_channels_t *ccP,
        sub_frame_t subframeP)
625
//------------------------------------------------------------------------------
626
{
627 628
  // if FDD return dummy value
  if (ccP->tdd_Config == NULL)
629
    return 0;
630

631
  switch (subframeP) {
632
    case 1:
633
      return 1;
634 635

    case 6:
636 637 638
      if (ccP->tdd_Config->subframeAssignment == 0 || ccP->tdd_Config->subframeAssignment == 1 ||
          ccP->tdd_Config->subframeAssignment == 2 || ccP->tdd_Config->subframeAssignment == 6)
        return 1;
639 640

      break;
641 642

    default:
643 644 645 646
      break;
  }

  return 0;
647 648
}

649
//------------------------------------------------------------------------------
650 651 652
uint8_t
ul_subframe2_k_phich(COMMON_channels_t *cc,
                     sub_frame_t ul_subframe)
653 654
//------------------------------------------------------------------------------
{
655 656 657 658
  if(cc->tdd_Config) { //TODO fill other tdd config
    switch(cc->tdd_Config->subframeAssignment) {
      case 0:
        break;
659

660 661 662 663 664
      case 1:
        if(ul_subframe == 2 || ul_subframe == 7)
          return 4;
        else if(ul_subframe == 3 || ul_subframe == 8)
          return 6;
frtabu's avatar
frtabu committed
665

666
        return 255;
667 668 669 670 671 672

      case 2:
      case 3:
      case 4:
      case 5:
        break;
673
    }
674 675 676
  }

  return 4; //idk  sf_ahead?
677 678
}

679
//------------------------------------------------------------------------------
680 681 682
uint16_t
get_pucch1_absSF(COMMON_channels_t *cc,
                 uint16_t dlsch_absSF)
683
//------------------------------------------------------------------------------
684
{
685
  uint16_t sf, f, nextf;
Stefan's avatar
Stefan committed
686
  LTE_TDD_Config_t *tdd_Config = cc->tdd_Config;
687

Stefan's avatar
Stefan committed
688 689
  if (tdd_Config == NULL) { //FDD n+4
    return (dlsch_absSF + 4) % 10240;
690
  }
691

Stefan's avatar
Stefan committed
692 693 694
  sf = dlsch_absSF % 10;
  f = dlsch_absSF / 10;
  nextf = (f + 1) & 1023;
695

Stefan's avatar
Stefan committed
696
  switch (tdd_Config->subframeAssignment) {
697
    case 0:
Stefan's avatar
Stefan committed
698 699
      if (sf == 0 || sf == 5)
        return ((10 * f) + sf + 4) % 10240; // ACK/NAK in SF 4,9 same frame
frtabu's avatar
frtabu committed
700

Stefan's avatar
Stefan committed
701 702
      if (sf == 6)
        return ((10 * nextf) + 2) % 10240;  // ACK/NAK in SF 2 next frame
frtabu's avatar
frtabu committed
703

Stefan's avatar
Stefan committed
704 705
      if (sf == 1)
        return ((10 * f) + 7) % 10240;      // ACK/NAK in SF 7 same frame
706

707
      break;
708

709
    case 1:
Stefan's avatar
Stefan committed
710 711
      if (sf == 5 || sf == 6)
        return ((10 * nextf) + 2) % 10240;  // ACK/NAK in SF 2 next frame
frtabu's avatar
frtabu committed
712

Stefan's avatar
Stefan committed
713 714
      if (sf == 9)
        return ((10 * nextf) + 3) % 10240;  // ACK/NAK in SF 3 next frame
frtabu's avatar
frtabu committed
715

Stefan's avatar
Stefan committed
716 717
      if ((sf == 0) || (sf == 1))
        return ((10 * f) + 7) % 10240;      // ACK/NAK in SF 7 same frame
frtabu's avatar
frtabu committed
718

Stefan's avatar
Stefan committed
719 720
      if (sf == 4)
        return ((10 * f) + 8) % 10240;      // ACK/NAK in SF 8 same frame
721

722
      break;
723

724
    case 2:
Stefan's avatar
Stefan committed
725 726
      if (sf == 4 || sf == 5 || sf == 6 || sf == 8)
        return ((10 * nextf) + 2) % 10240;  // ACK/NAK in SF 2 next frame
frtabu's avatar
frtabu committed
727

Stefan's avatar
Stefan committed
728 729
      if (sf == 9)
        return ((10 * nextf) + 7) % 10240;  // ACK/NAK in SF 7 next frame
frtabu's avatar
frtabu committed
730

Stefan's avatar
Stefan committed
731 732
      if (sf == 0 || sf == 1 || sf == 3)
        return ((10 * f) + 7)% 10240;       // ACK/NAK in SF 7 same frame
733

734
      break;
735

736
    case 3:
Stefan's avatar
Stefan committed
737 738
      if (sf == 5 || sf == 6 || sf == 7 || sf == 8 || sf == 9)
        return ((10 * nextf) + ((sf - 1) >> 1)) % 10240;  // ACK/NAK in 2,3,4 resp. next frame
frtabu's avatar
frtabu committed
739

Stefan's avatar
Stefan committed
740 741
      if (sf == 1)
        return ((10 * nextf) + 2) % 10240;                // ACK/NAK in 2 next frame
frtabu's avatar
frtabu committed
742

Stefan's avatar
Stefan committed
743 744
      if (sf == 0)
        return ((10 * f) + 4) % 10240;                    // ACK/NAK in 4 same frame
745

746
      break;
747

748
    case 4:
Stefan's avatar
Stefan committed
749 750 751 752
      if (sf == 6 || sf == 7 || sf == 8 || sf == 9)
        return ((10 * nextf) + 3) % 10240;  // ACK/NAK in SF 3 next frame
      else if (sf == 0 || sf == 1 || sf == 4 || sf == 5)
        return ((10 * nextf) + 2) % 10240;  // ACK/NAK in SF 2 next frame
753

754
      break;
755

756
    case 5:
Stefan's avatar
Stefan committed
757 758
      if (sf == 0 || sf == 1 || sf == 3 || sf == 4 || sf == 5 || sf == 6 || sf == 7 || sf == 8)
        return ((10 * nextf) + 2) % 10240;        // ACK/NAK in SF 3 next frame
frtabu's avatar
frtabu committed
759

Stefan's avatar
Stefan committed
760 761
      if (sf == 9)
        return ((10 * (1 + nextf)) + 2) % 10240;  // ACK/NAK in SF 2 next frame
762

763
      break;
764

765
    case 6:
Stefan's avatar
Stefan committed
766 767
      if (sf == 5 || sf == 6)
        return ((10 * f) + sf + 7) % 10240; // ACK/NAK in SF 2,3 next frame
frtabu's avatar
frtabu committed
768

Stefan's avatar
Stefan committed
769 770
      if (sf == 9)
        return ((10 * nextf) + 4) % 10240;  // ACK/NAK in SF 4 next frame
frtabu's avatar
frtabu committed
771

Stefan's avatar
Stefan committed
772 773
      if (sf == 1 || sf == 0)
        return ((10 * f) + sf + 7) % 10240; // ACK/NAK in SF 7 same frame
774

775
      break;
Stefan's avatar
Stefan committed
776

777
    default:
Stefan's avatar
Stefan committed
778 779 780
      AssertFatal(1 == 0, "Illegal TDD subframe Assigment %ld\n",
                  tdd_Config->subframeAssignment);
      return 0;
781
  }
782

783
  AssertFatal(1 == 0, "Shouldn't get here\n");
784 785
}

786
//------------------------------------------------------------------------------
787
void
788
get_srs_pos(COMMON_channels_t *cc,
789
            uint16_t isrs,
790 791
            uint16_t *psrsPeriodicity,
            uint16_t *psrsOffset)
792
//------------------------------------------------------------------------------
Cedric Roux's avatar
Cedric Roux committed
793
{
794
  if (cc->tdd_Config) { // TDD
795
    AssertFatal(isrs >= 10, "2 ms SRS periodicity not supported");
796

797
    if (isrs > 9 && isrs < 15) {
798 799
      *psrsPeriodicity = 5;
      *psrsOffset = isrs - 10;
800
    } else if (isrs > 14 && isrs < 25) {
801 802
      *psrsPeriodicity = 10;
      *psrsOffset = isrs - 15;
803
    } else if (isrs > 24 && isrs < 45) {
804 805
      *psrsPeriodicity = 20;
      *psrsOffset = isrs - 25;
806
    } else if (isrs > 44 && isrs < 85) {
807 808
      *psrsPeriodicity = 40;
      *psrsOffset = isrs - 45;
809
    } else if (isrs > 84 && isrs < 165) {
810 811
      *psrsPeriodicity = 80;
      *psrsOffset = isrs - 85;
812
    } else if (isrs > 164 && isrs < 325) {
813 814
      *psrsPeriodicity = 160;
      *psrsOffset = isrs - 165;
815
    } else if (isrs > 324 && isrs < 645) {
816 817 818
      *psrsPeriodicity = 320;
      *psrsOffset = isrs - 325;
    }
819

820
    AssertFatal(isrs <= 644, "Isrs out of range %d>644\n", isrs);
821 822
  }       // TDD
  else {      // FDD
823 824 825
    if (isrs < 2) {
      *psrsPeriodicity = 2;
      *psrsOffset = isrs;
826
    } else if (isrs > 1 && isrs < 7) {
827 828
      *psrsPeriodicity = 5;
      *psrsOffset = isrs - 2;
829
    } else if (isrs > 6 && isrs < 17) {
830 831
      *psrsPeriodicity = 10;
      *psrsOffset = isrs - 7;
832
    } else if (isrs > 16 && isrs < 37) {
833 834
      *psrsPeriodicity = 20;
      *psrsOffset = isrs - 17;
835
    } else if (isrs > 36 && isrs < 77) {
836 837
      *psrsPeriodicity = 40;
      *psrsOffset = isrs - 37;
838
    } else if (isrs > 76 && isrs < 157) {
839 840
      *psrsPeriodicity = 80;
      *psrsOffset = isrs - 77;
841
    } else if (isrs > 156 && isrs < 317) {
842 843
      *psrsPeriodicity = 160;
      *psrsOffset = isrs - 157;
844
    } else if (isrs > 316 && isrs < 637) {
845 846 847
      *psrsPeriodicity = 320;
      *psrsOffset = isrs - 317;
    }
848

849 850
    AssertFatal(isrs <= 636, "Isrs out of range %d>636\n", isrs);
  }
frtabu's avatar
frtabu committed
851

852
  return;
853 854
}

855
//------------------------------------------------------------------------------
856 857 858 859
/*
* Get some CSI (CQI/PMI/RI) parameters for SFN and subframe number calculation
* with periodic report.
*/
860
void
861 862
get_csi_params(COMMON_channels_t *cc,
               struct LTE_CQI_ReportPeriodic *cqi_ReportPeriodic,
863 864 865
               uint16_t *Npd,
               uint16_t *N_OFFSET_CQI,
               int *H)
866
//------------------------------------------------------------------------------
867
{
868
  AssertFatal(cqi_ReportPeriodic != NULL, "cqi_ReportPeriodic is null!\n");
869
  uint16_t cqi_PMI_ConfigIndex = cqi_ReportPeriodic->choice.setup.cqi_pmi_ConfigIndex;
870 871
  uint8_t Jtab[6] = { 0, 2, 2, 3, 4, 4 };

872 873
  if (cc->tdd_Config == NULL) { //FDD
    if (cqi_PMI_ConfigIndex <= 1) { // 2 ms CQI_PMI period
874 875
      *Npd = 2;
      *N_OFFSET_CQI = cqi_PMI_ConfigIndex;
876
    } else if (cqi_PMI_ConfigIndex <= 6) {  // 5 ms CQI_PMI period
877 878
      *Npd = 5;
      *N_OFFSET_CQI = cqi_PMI_ConfigIndex - 2;
879
    } else if (cqi_PMI_ConfigIndex <= 16) { // 10ms CQI_PMI period
880 881
      *Npd = 10;
      *N_OFFSET_CQI = cqi_PMI_ConfigIndex - 7;
882
    } else if (cqi_PMI_ConfigIndex <= 36) { // 20 ms CQI_PMI period
883 884
      *Npd = 20;
      *N_OFFSET_CQI = cqi_PMI_ConfigIndex - 17;
885
    } else if (cqi_PMI_ConfigIndex <= 76) { // 40 ms CQI_PMI period
886 887
      *Npd = 40;
      *N_OFFSET_CQI = cqi_PMI_ConfigIndex - 37;
888
    } else if (cqi_PMI_ConfigIndex <= 156) {  // 80 ms CQI_PMI period
889 890
      *Npd = 80;
      *N_OFFSET_CQI = cqi_PMI_ConfigIndex - 77;
891
    } else if (cqi_PMI_ConfigIndex <= 316) {  // 160 ms CQI_PMI period
892 893 894
      *Npd = 160;
      *N_OFFSET_CQI = cqi_PMI_ConfigIndex - 157;
    } else if (cqi_PMI_ConfigIndex > 317) {
895 896 897 898 899 900 901 902 903 904

      if (cqi_PMI_ConfigIndex <= 349) {	        // 32 ms CQI_PMI period
	*Npd = 32;
	      *N_OFFSET_CQI = cqi_PMI_ConfigIndex - 318;
      } else if (cqi_PMI_ConfigIndex <= 413) {	// 64 ms CQI_PMI period
	      *Npd = 64;
	      *N_OFFSET_CQI = cqi_PMI_ConfigIndex - 350;
      } else if (cqi_PMI_ConfigIndex <= 541) {	// 128 ms CQI_PMI period
	      *Npd = 128;
	      *N_OFFSET_CQI = cqi_PMI_ConfigIndex - 414;
905
      }
906

907
    }
908 909 910

  } else {  // TDD
    if (cqi_PMI_ConfigIndex == 0) {	// all UL subframes
911 912
      *Npd = 1;
      *N_OFFSET_CQI = 0;
913
    } else if (cqi_PMI_ConfigIndex <= 6) {  // 5 ms CQI_PMI period
914 915
      *Npd = 5;
      *N_OFFSET_CQI = cqi_PMI_ConfigIndex - 1;
916
    } else if (cqi_PMI_ConfigIndex <= 16) { // 10ms CQI_PMI period
917 918
      *Npd = 10;
      *N_OFFSET_CQI = cqi_PMI_ConfigIndex - 6;
919
    } else if (cqi_PMI_ConfigIndex <= 36) { // 20 ms CQI_PMI period
920 921
      *Npd = 20;
      *N_OFFSET_CQI = cqi_PMI_ConfigIndex - 16;
922
    } else if (cqi_PMI_ConfigIndex <= 76) { // 40 ms CQI_PMI period
923 924
      *Npd = 40;
      *N_OFFSET_CQI = cqi_PMI_ConfigIndex - 36;
925
    } else if (cqi_PMI_ConfigIndex <= 156) {  // 80 ms CQI_PMI period
926 927
      *Npd = 80;
      *N_OFFSET_CQI = cqi_PMI_ConfigIndex - 76;
928
    } else if (cqi_PMI_ConfigIndex <= 316) {  // 160 ms CQI_PMI period
929 930
      *Npd = 160;
      *N_OFFSET_CQI = cqi_PMI_ConfigIndex - 156;
931
    }
932
  }
933

934
  // get H
935 936 937
  if (cqi_ReportPeriodic->choice.setup.cqi_FormatIndicatorPeriodic.present == LTE_CQI_ReportPeriodic__setup__cqi_FormatIndicatorPeriodic_PR_subbandCQI) {
    *H = 1 + (Jtab[cc->mib->message.dl_Bandwidth] * cqi_ReportPeriodic->choice.setup.cqi_FormatIndicatorPeriodic.choice.subbandCQI.k);
  } else {
938
    *H = 1;
939
  }
frtabu's avatar
frtabu committed
940

941
  return;
942 943
}

944
//------------------------------------------------------------------------------
945
uint8_t
946
get_dl_cqi_pmi_size_pusch(COMMON_channels_t *cc,
947
                          uint8_t tmode,
948
                          uint8_t ri,
949
                          LTE_CQI_ReportModeAperiodic_t *cqi_ReportModeAperiodic)
950
//------------------------------------------------------------------------------
Cedric Roux's avatar
Cedric Roux committed
951
{
952 953 954 955
  int Ntab[6] = { 0, 4, 7, 9, 10, 13 };
  int N = Ntab[cc->mib->message.dl_Bandwidth];
  int Ltab_uesel[6] = { 0, 6, 9, 13, 15, 18 };
  int L = Ltab_uesel[cc->mib->message.dl_Bandwidth];
956
  AssertFatal(cqi_ReportModeAperiodic != NULL, "cqi_ReportPeriodic is null!\n");
957 958

  switch (*cqi_ReportModeAperiodic) {
959
    case LTE_CQI_ReportModeAperiodic_rm12:
960
      AssertFatal(tmode == 4 || tmode == 6 || tmode == 8 || tmode == 9 || tmode == 10, "Illegal TM (%d) for CQI_ReportModeAperiodic_rm12\n",
961
                  tmode);
962
      AssertFatal(cc->p_eNB <= 4, "only up to 4 antenna ports supported here\n");
963 964 965

      if (ri == 1 && cc->p_eNB == 2)
        return (4 + (N << 1));
frtabu's avatar
frtabu committed
966

967
      if (ri == 2 && cc->p_eNB == 2)
968
        return (8 + N);
frtabu's avatar
frtabu committed
969

970
      if (ri == 1 && cc->p_eNB == 4)
971
        return (4 + (N << 2));
frtabu's avatar
frtabu committed
972

973
      if (ri > 1 && cc->p_eNB == 4)
974 975 976 977 978 979
        return (8 + (N << 2));

      break;

    case LTE_CQI_ReportModeAperiodic_rm20:
      // Table 5.2.2.6.3-1 (36.212)
980
      AssertFatal(tmode == 1 || tmode == 2 || tmode == 3 || tmode == 7 || tmode == 9 || tmode == 10, "Illegal TM (%d) for CQI_ReportModeAperiodic_rm20\n",
981
                  tmode);
982
      AssertFatal(tmode != 9 && tmode != 10, "TM9/10 will be handled later for CQI_ReportModeAperiodic_rm20\n");
983 984 985 986
      return (4 + 2 + L);

    case LTE_CQI_ReportModeAperiodic_rm22:
      // Table 5.2.2.6.3-2 (36.212)
987
      AssertFatal(tmode == 4 || tmode == 6 || tmode == 8 || tmode == 9 || tmode == 10, "Illegal TM (%d) for CQI_ReportModeAperiodic_rm22\n",
988
                  tmode);
989
      AssertFatal(tmode != 9 && tmode != 10, "TM9/10 will be handled later for CQI_ReportModeAperiodic_rm22\n");
990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006

      if (ri == 1 && cc->p_eNB == 2)
        return (4 + 2 + 0 + 0 + L + 4);

      if (ri == 2 && cc->p_eNB == 2)
        return (4 + 2 + 4 + 2 + L + 2);

      if (ri == 1 && cc->p_eNB == 4)
        return (4 + 2 + 0 + 0 + L + 8);

      if (ri >= 2 && cc->p_eNB == 4)
        return (4 + 2 + 4 + 2 + L + 8);

      break;

    case LTE_CQI_ReportModeAperiodic_rm30:
      // Table 5.2.2.6.2-1 (36.212)
1007
      AssertFatal(tmode == 1 || tmode == 2 || tmode == 3 || tmode == 7 || tmode == 8 || tmode == 9 || tmode == 10,
1008 1009
                  "Illegal TM (%d) for CQI_ReportModeAperiodic_rm30\n",
                  tmode);
1010
      AssertFatal(tmode != 8 && tmode != 9 && tmode != 10, "TM8/9/10 will be handled later for CQI_ReportModeAperiodic_rm30\n");
1011
      return (4 + (N << 1));
1012 1013 1014

    case LTE_CQI_ReportModeAperiodic_rm31:
      // Table 5.2.2.6.2-2 (36.212)
1015
      AssertFatal(tmode == 4 || tmode == 6 || tmode == 8 || tmode == 9 || tmode == 10, "Illegal TM (%d) for CQI_ReportModeAperiodic_rm31\n",
1016
                  tmode);
1017
      AssertFatal(tmode != 8 && tmode != 9 && tmode != 10, "TM8/9/10 will be handled later for CQI_ReportModeAperiodic_rm31\n");
1018 1019 1020

      if (ri == 1 && cc->p_eNB == 2)
        return (4 + (N << 1) + 0 + 0 + 2);
frtabu's avatar
frtabu committed
1021

1022
      if (ri == 2 && cc->p_eNB == 2)
1023
        return (4 + (N << 1) + 4 + (N << 1) + 1);
frtabu's avatar
frtabu committed
1024

1025
      if (ri == 1 && cc->p_eNB == 4)
1026
        return (4 + (N << 1) + 0 + 0 + 4);
frtabu's avatar
frtabu committed
1027

1028
      if (ri >= 2 && cc->p_eNB == 4)
1029 1030 1031
        return (4 + (N << 1) + 4 + (N << 1) + 4);

      break;
1032
#if (LTE_RRC_VERSION >= MAKE_VERSION(12, 5, 0))
frtabu's avatar
frtabu committed
1033

1034
    case LTE_CQI_ReportModeAperiodic_rm32_v1250:
1035
      AssertFatal(tmode == 4 || tmode == 6 || tmode == 8 || tmode == 9 || tmode == 10, "Illegal TM (%d) for CQI_ReportModeAperiodic_rm32\n",
1036
                  tmode);
1037
      AssertFatal(1 == 0, "CQI_ReportModeAperiodic_rm32_v1250 not supported yet\n");
1038 1039 1040 1041 1042 1043
      break;

    case LTE_CQI_ReportModeAperiodic_rm10_v1310:

      // Table 5.2.2.6.1-1F/G (36.212)
      if (ri == 1)
1044
        return 4;   // F
frtabu's avatar
frtabu committed
1045

1046
      return 7;   // G
1047 1048 1049

    case LTE_CQI_ReportModeAperiodic_rm11_v1310:
      // Table 5.2.2.6.1-1H (36.212)
1050
      AssertFatal(tmode == 4 || tmode == 6 || tmode == 8 || tmode == 9 || tmode == 10,  "Illegal TM (%d) for CQI_ReportModeAperiodic_rm11\n",
1051
                  tmode);
1052
      AssertFatal(cc->p_eNB <= 4, "only up to 4 antenna ports supported here\n");
1053 1054 1055

      if (ri == 1 && cc->p_eNB == 2)
        return (4 + 0 + 2);
frtabu's avatar
frtabu committed
1056

1057
      if (ri == 2 && cc->p_eNB == 2)
1058
        return (4 + 4 + 1);
frtabu's avatar
frtabu committed
1059

1060
      if (ri == 1 && cc->p_eNB == 4)
1061
        return (4 + 0 + 4);
frtabu's avatar
frtabu committed
1062

1063
      if (ri > 1 && cc->p_eNB == 4)
1064 1065 1066
        return (4 + 4 + 4);

      break;
1067
#endif /* #if (LTE_RRC_VERSION >= MAKE_VERSION(12, 5, 0)) */
1068
  }
1069

1070
  AssertFatal(1 == 0, "Shouldn't get here\n");
1071
  return 0;
1072
}
1073

1074
//------------------------------------------------------------------------------
1075
uint8_t
1076
get_rel8_dl_cqi_pmi_size(UE_sched_ctrl_t *sched_ctl,
1077
                         int CC_idP,
1078
                         COMMON_channels_t *cc,
1079
                         uint8_t tmode,
1080
                         struct LTE_CQI_ReportPeriodic *cqi_ReportPeriodic)
1081
//------------------------------------------------------------------------------
1082
{
1083 1084 1085 1086
  int no_pmi = 0;
  //    Ltab[6] = {0,log2(15/4/2),log2(25/4/2),log2(50/6/3),log2(75/8/4),log2(100/8/4)};
  uint8_t Ltab[6] = { 0, 1, 2, 2, 2, 2 };
  uint8_t ri = sched_ctl->periodic_ri_received[CC_idP];
1087 1088 1089
  AssertFatal(cqi_ReportPeriodic != NULL, "cqi_ReportPeriodic is null!\n");
  AssertFatal(cqi_ReportPeriodic->present != LTE_CQI_ReportPeriodic_PR_NOTHING, "cqi_ReportPeriodic->present == CQI_ReportPeriodic_PR_NOTHING!\n");
  AssertFatal(cqi_ReportPeriodic->choice.setup.cqi_FormatIndicatorPeriodic.present != LTE_CQI_ReportPeriodic__setup__cqi_FormatIndicatorPeriodic_PR_NOTHING,
1090
              "cqi_ReportPeriodic->cqi_FormatIndicatorPeriodic.choice.setup.present == CQI_ReportPeriodic__setup__cqi_FormatIndicatorPeriodic_PR_NOTHING!\n");
1091 1092

  switch (tmode) {
1093 1094 1095 1096 1097 1098 1099 1100 1101 1102
    case 1:
    case 2:
    case 5:
    case 6:
    case 7:
      no_pmi = 1;
      break;

    default:
      no_pmi = 0;
1103
      break;
1104 1105
  }

1106
  if (cqi_ReportPeriodic->choice.setup.cqi_FormatIndicatorPeriodic.present == LTE_CQI_ReportPeriodic__setup__cqi_FormatIndicatorPeriodic_PR_widebandCQI ||
1107
      sched_ctl->feedback_cnt[CC_idP] == 0) {
1108
    // send wideband report every opportunity if wideband reporting mode is selected, else every H opportunities
1109
    if (no_pmi == 1) return 4;
frtabu's avatar
frtabu committed
1110

1111
    if (cc->p_eNB == 2 && ri == 1) return 6;
frtabu's avatar
frtabu committed
1112

1113
    if (cc->p_eNB == 2 && ri == 2) return 8;
frtabu's avatar
frtabu committed
1114

1115
    if (cc->p_eNB == 4 && ri == 1) return 8;
frtabu's avatar
frtabu committed
1116

1117
    if (cc->p_eNB == 4 && ri == 2) return 11;
frtabu's avatar
frtabu committed
1118

1119
    AssertFatal(1 == 0, "illegal combination p %d, ri %d, no_pmi %d\n",
1120 1121
                cc->p_eNB,
                ri,
1122
                no_pmi);
1123
  } else if (cqi_ReportPeriodic->choice.setup.cqi_FormatIndicatorPeriodic.present == LTE_CQI_ReportPeriodic__setup__cqi_FormatIndicatorPeriodic_PR_subbandCQI) {
1124
    if (no_pmi == 1 || ri == 1) return (4 + Ltab[cc->mib->message.dl_Bandwidth]);
frtabu's avatar
frtabu committed
1125

1126
    return (7 + Ltab[cc->mib->message.dl_Bandwidth]);
1127 1128
  }

1129
  AssertFatal(1 == 0, "Shouldn't get here : cqi_ReportPeriodic->present %d\n",
1130
              cqi_ReportPeriodic->choice.setup.cqi_FormatIndicatorPeriodic.present);
1131
  return 0;
1132 1133
}

1134
//------------------------------------------------------------------------------
1135
void
1136
fill_nfapi_dl_dci_1A(nfapi_dl_config_request_pdu_t *dl_config_pdu,
1137 1138 1139 1140 1141 1142 1143 1144 1145
                     uint8_t                       aggregation_level,
                     uint16_t                      rnti,
                     uint8_t                       rnti_type,
                     uint8_t                       harq_process,
                     uint8_t                       tpc,
                     uint16_t                      resource_block_coding,
                     uint8_t                       mcs,
                     uint8_t                       ndi,
                     uint8_t                       rv,
1146
                     uint8_t                       vrb_flag)
1147
//------------------------------------------------------------------------------
1148
{
1149
  memset((void *) dl_config_pdu, 0, sizeof(nfapi_dl_config_request_pdu_t));
1150

1151 1152 1153 1154 1155 1156 1157
  dl_config_pdu->pdu_type                                                          = NFAPI_DL_CONFIG_DCI_DL_PDU_TYPE;
  dl_config_pdu->pdu_size                                                          = (uint8_t) (2 + sizeof(nfapi_dl_config_dci_dl_pdu));
  dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.tl.tag                                 = NFAPI_DL_CONFIG_REQUEST_DCI_DL_PDU_REL8_TAG;
  dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.dci_format                             = NFAPI_DL_DCI_FORMAT_1A;
  dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.aggregation_level                      = aggregation_level;
  dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.rnti                                   = rnti;
  dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.rnti_type                              = rnti_type;
1158
  dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.transmission_power                     = 6000;  // equal to RS power
1159 1160 1161 1162 1163 1164 1165
  dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.harq_process                           = harq_process;
  dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.tpc                                    = tpc;
  dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.resource_block_coding                  = resource_block_coding;
  dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.mcs_1                                  = mcs;
  dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.new_data_indicator_1                   = ndi;
  dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.redundancy_version_1                   = rv;
  dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.virtual_resource_block_assignment_flag = vrb_flag;
1166
  return;
1167 1168
}

1169
//------------------------------------------------------------------------------
1170
void
1171 1172
program_dlsch_acknak(module_id_t module_idP,
                     int CC_idP,
Stefan's avatar
Stefan committed
1173
                     int UE_idP,
1174
                     frame_t frameP,
Stefan's avatar
Stefan committed
1175
                     sub_frame_t subframeP,
1176
                     uint8_t cce_idx)
1177
//------------------------------------------------------------------------------
Cedric Roux's avatar
Cedric Roux committed
1178
{
1179 1180 1181 1182 1183 1184 1185 1186 1187
  eNB_MAC_INST                           *eNB                         = RC.mac[module_idP];
  COMMON_channels_t                      *cc                          = eNB->common_channels;
  UE_list_t                              *UE_list                     = &eNB->UE_list;
  rnti_t                                 rnti                         = UE_RNTI(module_idP, UE_idP);
  nfapi_ul_config_request_body_t         *ul_req;
  nfapi_ul_config_request_pdu_t          *ul_config_pdu;
  int                                    use_simultaneous_pucch_pusch = 0;
  nfapi_ul_config_ulsch_harq_information *ulsch_harq_information      = NULL;
  nfapi_ul_config_harq_information       *harq_information            = NULL;
1188
#if (LTE_RRC_VERSION >= MAKE_VERSION(10, 2, 0))
Stefan's avatar
Stefan committed
1189
  struct LTE_PhysicalConfigDedicated__ext2 *ext2 = UE_list->UE_template[CC_idP][UE_idP].physicalConfigDedicated->ext2;
frtabu's avatar
frtabu committed
1190

1191 1192
  if (ext2 &&
      ext2->pucch_ConfigDedicated_v1020 &&
Stefan's avatar
Stefan committed
1193 1194
      ext2->pucch_ConfigDedicated_v1020->simultaneousPUCCH_PUSCH_r10 &&
      *ext2->pucch_ConfigDedicated_v1020->simultaneousPUCCH_PUSCH_r10 == LTE_PUCCH_ConfigDedicated_v1020__simultaneousPUCCH_PUSCH_r10_true)
1195
    use_simultaneous_pucch_pusch = 1;
Cedric Roux's avatar
Cedric Roux committed
1196

1197
#endif
1198 1199
  // pucch1 and pusch feedback is similar, namely in n+k subframes from now
  // This is used in the following "if/else" condition to check if there isn't or is already an UL grant in n+k
1200
  int16_t ul_absSF = get_pucch1_absSF(&cc[CC_idP],
1201
                                      subframeP + (10 * frameP));
1202

1203 1204 1205
  if ((ul_config_pdu = has_ul_grant(module_idP,
                                    CC_idP,
                                    ul_absSF,
1206
                                    rnti)) == NULL) {
1207 1208
    // no UL grant so
    // Program ACK/NAK alone Format 1a/b or 3
Stefan's avatar
Stefan committed
1209
    ul_req = &eNB->UL_req_tmp[CC_idP][ul_absSF % 10].ul_config_request_body;
1210 1211 1212
    ul_config_pdu = &ul_req->ul_config_pdu_list[ul_req->number_of_pdus];
    // Do PUCCH
    fill_nfapi_uci_acknak(module_idP,
1213
                          CC_idP,
1214 1215
                          rnti,
                          subframeP + (10 * frameP),
1216
                          cce_idx);
1217 1218 1219 1220 1221 1222
  } else {
    /* there is already an existing UL grant so update it if needed
     * on top of some other UL resource (PUSCH,combined SR/CQI/HARQ on PUCCH, etc)
     */
    switch (ul_config_pdu->pdu_type) {
      /* [ulsch] to [ulsch + harq] or [ulsch + harq on pucch] */
1223 1224 1225
      case NFAPI_UL_CONFIG_ULSCH_PDU_TYPE:
        if (use_simultaneous_pucch_pusch == 1) {
          // Convert it to an NFAPI_UL_CONFIG_ULSCH_UCI_HARQ_PDU_TYPE
Stefan's avatar
Stefan committed
1226
          harq_information = &ul_config_pdu->ulsch_uci_harq_pdu.harq_information;
1227
          ul_config_pdu->pdu_type = NFAPI_UL_CONFIG_ULSCH_UCI_HARQ_PDU_TYPE;
Stefan's avatar
Stefan committed
1228
          LOG_D(MAC, "Frame %d, Subframe %d: Switched UCI HARQ to ULSCH UCI HARQ\n",
1229
                frameP,
Stefan's avatar
Stefan committed
1230
                subframeP);
1231 1232
        } else {
          // Convert it to an NFAPI_UL_CONFIG_ULSCH_HARQ_PDU_TYPE
Stefan's avatar
Stefan committed
1233
          ulsch_harq_information = &ul_config_pdu->ulsch_harq_pdu.harq_information;
1234
          ul_config_pdu->pdu_type = NFAPI_UL_CONFIG_ULSCH_HARQ_PDU_TYPE;
Stefan's avatar
Stefan committed
1235 1236 1237 1238 1239 1240
          ul_config_pdu->ulsch_harq_pdu.initial_transmission_parameters.initial_transmission_parameters_rel8.tl.tag
            = NFAPI_UL_CONFIG_REQUEST_INITIAL_TRANSMISSION_PARAMETERS_REL8_TAG;
          ul_config_pdu->ulsch_harq_pdu.initial_transmission_parameters.initial_transmission_parameters_rel8.n_srs_initial = 0; // last symbol not punctured
          ul_config_pdu->ulsch_harq_pdu.initial_transmission_parameters.initial_transmission_parameters_rel8.initial_number_of_resource_blocks
            = ul_config_pdu->ulsch_harq_pdu.ulsch_pdu.ulsch_pdu_rel8.number_of_resource_blocks;  // we don't change the number of resource blocks across retransmissions yet
          LOG_D(MAC,"Frame %d, Subframe %d: Switched UCI HARQ to ULSCH HARQ\n",
1241
                frameP,
Stefan's avatar
Stefan committed
1242
                subframeP);
1243 1244 1245 1246 1247
        }

        break;

      case NFAPI_UL_CONFIG_ULSCH_HARQ_PDU_TYPE:
Stefan's avatar
Stefan committed
1248
        AssertFatal(use_simultaneous_pucch_pusch == 0, "Cannot be NFAPI_UL_CONFIG_ULSCH_HARQ_PDU_TYPE, simultaneous_pucch_pusch is active");
1249 1250 1251
        break;

      case NFAPI_UL_CONFIG_ULSCH_UCI_HARQ_PDU_TYPE:
Stefan's avatar
Stefan committed
1252
        AssertFatal(use_simultaneous_pucch_pusch == 1, "Cannot be NFAPI_UL_CONFIG_ULSCH_UCI_PDU_TYPE, simultaneous_pucch_pusch is inactive\n");
1253
        break;
1254 1255 1256

      /* [ulsch + cqi] to [ulsch + cqi + harq] */

1257 1258
      case NFAPI_UL_CONFIG_ULSCH_CQI_RI_PDU_TYPE:
        // Convert it to an NFAPI_UL_CONFIG_ULSCH_CQI_HARQ_RI_PDU_TYPE
Stefan's avatar
Stefan committed
1259
        ulsch_harq_information = &ul_config_pdu->ulsch_cqi_harq_ri_pdu.harq_information;
1260 1261 1262 1263 1264 1265 1266
        ul_config_pdu->pdu_type = NFAPI_UL_CONFIG_ULSCH_CQI_HARQ_RI_PDU_TYPE;
        /* TODO: check this - when converting from nfapi_ul_config_ulsch_cqi_ri_pdu to
         * nfapi_ul_config_ulsch_cqi_harq_ri_pdu, shouldn't we copy initial_transmission_parameters
         * from the one to the other?
         * Those two types are not compatible. 'initial_transmission_parameters' is not at the
         * place in both.
         */
Stefan's avatar
Stefan committed
1267 1268 1269 1270 1271
        ul_config_pdu->ulsch_cqi_harq_ri_pdu.initial_transmission_parameters.initial_transmission_parameters_rel8.tl.tag
          = NFAPI_UL_CONFIG_REQUEST_INITIAL_TRANSMISSION_PARAMETERS_REL8_TAG;
        ul_config_pdu->ulsch_cqi_harq_ri_pdu.initial_transmission_parameters.initial_transmission_parameters_rel8.n_srs_initial = 0;  // last symbol not punctured
        ul_config_pdu->ulsch_cqi_harq_ri_pdu.initial_transmission_parameters.initial_transmission_parameters_rel8.initial_number_of_resource_blocks
          = ul_config_pdu->ulsch_harq_pdu.ulsch_pdu.ulsch_pdu_rel8.number_of_resource_blocks; // we don't change the number of resource blocks across retransmissions yet
1272 1273 1274
        break;

      case NFAPI_UL_CONFIG_ULSCH_CQI_HARQ_RI_PDU_TYPE:
1275
        AssertFatal(use_simultaneous_pucch_pusch == 0, "Cannot be NFAPI_UL_CONFIG_ULSCH_CQI_HARQ_RI_PDU_TYPE, simultaneous_pucch_pusch is active\n");
1276
        break;
1277 1278 1279

      /* [ulsch + cqi on pucch] to [ulsch + cqi on pucch + harq on pucch] */

1280 1281
      case NFAPI_UL_CONFIG_ULSCH_UCI_CSI_PDU_TYPE:
        // convert it to an NFAPI_UL_CONFIG_ULSCH_CSI_UCI_HARQ_PDU_TYPE
Stefan's avatar
Stefan committed
1282
        harq_information = &ul_config_pdu->ulsch_csi_uci_harq_pdu.harq_information;
1283 1284 1285 1286
        ul_config_pdu->pdu_type = NFAPI_UL_CONFIG_ULSCH_CSI_UCI_HARQ_PDU_TYPE;
        break;

      case NFAPI_UL_CONFIG_ULSCH_CSI_UCI_HARQ_PDU_TYPE:
1287
        AssertFatal(use_simultaneous_pucch_pusch == 1, "Cannot be NFAPI_UL_CONFIG_ULSCH_CSI_UCI_HARQ_PDU_TYPE, simultaneous_pucch_pusch is inactive\n");
1288
        break;
1289 1290 1291

      /* [sr] to [sr + harq] */

1292 1293 1294
      case NFAPI_UL_CONFIG_UCI_SR_PDU_TYPE:
        // convert to NFAPI_UL_CONFIG_UCI_SR_HARQ_PDU_TYPE
        ul_config_pdu->pdu_type = NFAPI_UL_CONFIG_UCI_SR_HARQ_PDU_TYPE;
Stefan's avatar
Stefan committed
1295
        harq_information = &ul_config_pdu->uci_sr_harq_pdu.harq_information;
1296 1297 1298 1299 1300 1301
        break;

      case NFAPI_UL_CONFIG_UCI_SR_HARQ_PDU_TYPE:
        /* nothing to do */
        break;

1302
      /* [cqi] to [cqi + harq] */
1303 1304 1305 1306 1307 1308 1309 1310 1311 1312
      case NFAPI_UL_CONFIG_UCI_CQI_PDU_TYPE:
        // convert to NFAPI_UL_CONFIG_UCI_CQI_HARQ_PDU_TYPE
        ul_config_pdu->pdu_type = NFAPI_UL_CONFIG_UCI_CQI_HARQ_PDU_TYPE;
        harq_information = &ul_config_pdu->uci_cqi_harq_pdu.harq_information;
        break;

      case NFAPI_UL_CONFIG_UCI_CQI_HARQ_PDU_TYPE:
        /* nothing to do */
        break;

1313
      /* [cqi + sr] to [cqr + sr + harq] */
1314 1315 1316 1317 1318 1319 1320 1321 1322
      case NFAPI_UL_CONFIG_UCI_CQI_SR_PDU_TYPE:
        // convert to NFAPI_UL_CONFIG_UCI_CQI_SR_HARQ_PDU_TYPE
        ul_config_pdu->pdu_type = NFAPI_UL_CONFIG_UCI_CQI_SR_HARQ_PDU_TYPE;
        harq_information = &ul_config_pdu->uci_cqi_sr_harq_pdu.harq_information;
        break;

      case NFAPI_UL_CONFIG_UCI_CQI_SR_HARQ_PDU_TYPE:
        /* nothing to do */
        break;
1323
    }
1324
  }
Cedric Roux's avatar
Cedric Roux committed
1325

Stefan's avatar
Stefan committed
1326
  if (ulsch_harq_information) {
1327 1328 1329 1330
    fill_nfapi_ulsch_harq_information(module_idP,
                                      CC_idP,
                                      rnti,
                                      ulsch_harq_information,
1331
                                      subframeP);
Stefan's avatar
Stefan committed
1332
  }
frtabu's avatar
frtabu committed
1333

Stefan's avatar
Stefan committed
1334
  if (harq_information) {
1335 1336 1337 1338
    fill_nfapi_harq_information(module_idP,
                                CC_idP,
                                rnti,
                                harq_information,
1339
                                cce_idx);
Stefan's avatar
Stefan committed
1340
  }
frtabu's avatar
frtabu committed
1341

1342
  return;
1343 1344
}

1345
//------------------------------------------------------------------------------
1346 1347 1348
uint8_t
get_V_UL_DAI(module_id_t module_idP,
             int CC_idP,
1349
             uint16_t rntiP,
1350
             sub_frame_t subframeP)
1351
//------------------------------------------------------------------------------
Cedric Roux's avatar
Cedric Roux committed
1352
{
1353
  nfapi_hi_dci0_request_body_t *HI_DCI0_req = &RC.mac[module_idP]->HI_DCI0_req[CC_idP][subframeP].hi_dci0_request_body;
1354 1355 1356
  nfapi_hi_dci0_request_pdu_t *hi_dci0_pdu  = &HI_DCI0_req->hi_dci0_pdu_list[0];

  for (int i = 0; i < HI_DCI0_req->number_of_dci; i++) {
1357 1358 1359 1360
    if (hi_dci0_pdu[i].pdu_type == NFAPI_HI_DCI0_DCI_PDU_TYPE &&
        hi_dci0_pdu[i].dci_pdu.dci_pdu_rel8.rnti == rntiP) {
      return hi_dci0_pdu[i].dci_pdu.dci_pdu_rel8.dl_assignment_index;
    }
1361
  }
1362

1363
  return 4;     // this is rule from Section 7.3 in 36.213
1364
}
1365

1366
//------------------------------------------------------------------------------
1367
void
1368
fill_nfapi_ulsch_harq_information(module_id_t                            module_idP,
1369 1370 1371
                                  int                                    CC_idP,
                                  uint16_t                               rntiP,
                                  nfapi_ul_config_ulsch_harq_information *harq_information,
1372
                                  sub_frame_t                            subframeP)
1373
//------------------------------------------------------------------------------
1374 1375 1376 1377 1378
{
  eNB_MAC_INST *eNB     = RC.mac[module_idP];
  COMMON_channels_t *cc = &eNB->common_channels[CC_idP];
  UE_list_t *UE_list    = &eNB->UE_list;
  int UE_id = find_UE_id(module_idP, rntiP);
frtabu's avatar
frtabu committed
1379
  nfapi_ul_config_ulsch_harq_information_rel10_t *harq_information_rel10 = &harq_information->harq_information_rel10;
1380 1381
  AssertFatal(UE_id >= 0, "UE_id cannot be found, impossible\n");
  AssertFatal(UE_list != NULL, "UE_list is null\n");
Stefan's avatar
Stefan committed
1382
  LTE_PhysicalConfigDedicated_t *physicalConfigDedicated = UE_list->UE_template[CC_idP][UE_id].physicalConfigDedicated;
1383
  AssertFatal(physicalConfigDedicated != NULL, "physicalConfigDedicated for rnti %x is null\n",
1384
              rntiP);
1385
  struct LTE_PUSCH_ConfigDedicated *puschConfigDedicated = physicalConfigDedicated->pusch_ConfigDedicated;
Stefan's avatar
Stefan committed
1386 1387
  AssertFatal(puschConfigDedicated != NULL, "physicalConfigDedicated->puschConfigDedicated for rnti %x is null\n",
              rntiP);
1388
#if (LTE_RRC_VERSION >= MAKE_VERSION(10, 2, 0))
1389 1390
  /*  if (UE_list->UE_template[CC_idP][UE_id].physicalConfigDedicated->ext2) puschConfigDedicated_v1020 =  UE_list->UE_template[CC_idP][UE_id].physicalConfigDedicated->ext2->pusch_ConfigDedicated_v1020;
      #endif
1391
      #if (LTE_RRC_VERSION >= MAKE_VERSION(11, 3, 0))
1392
      if (UE_list->UE_template[CC_idP][UE_id].physicalConfigDedicated->ext4) puschConfigDedicated_v1130 =  UE_list->UE_template[CC_idP][UE_id].physicalConfigDedicated->ext4->pusch_ConfigDedicated_v1130;
1393
      #endif
1394
      #if (LTE_RRC_VERSION >= MAKE_VERSION(12, 5, 0))
1395
      if (UE_list->UE_template[CC_idP][UE_id].physicalConfigDedicated->ext5) puschConfigDedicated_v1250 =  UE_list->UE_template[CC_idP][UE_id].physicalConfigDedicated->ext5->pusch_ConfigDedicated_v1250;
1396
      #endif
1397
  */
1398
#endif
1399 1400
  harq_information_rel10->delta_offset_harq = puschConfigDedicated->betaOffset_ACK_Index;
  harq_information_rel10->tl.tag = NFAPI_UL_CONFIG_REQUEST_ULSCH_HARQ_INFORMATION_REL10_TAG;
Stefan's avatar
Stefan committed
1401 1402 1403 1404 1405
  struct LTE_PUCCH_ConfigDedicated *pucch_ConfigDedicated = physicalConfigDedicated->pucch_ConfigDedicated;
  AssertFatal(pucch_ConfigDedicated != NULL, "pucch_ConfigDedicated is null!\n");

  if (pucch_ConfigDedicated->tdd_AckNackFeedbackMode != NULL &&
      *pucch_ConfigDedicated->tdd_AckNackFeedbackMode == LTE_PUCCH_ConfigDedicated__tdd_AckNackFeedbackMode_multiplexing)
1406
    harq_information_rel10->ack_nack_mode = 1; // multiplexing
1407
  else
1408
    harq_information_rel10->ack_nack_mode = 0; // bundling
1409 1410

  switch (get_tmode(module_idP, CC_idP, UE_id)) {
1411 1412 1413 1414 1415 1416
    case 1:
    case 2:
    case 5:
    case 6:
    case 7:
      if (cc->tdd_Config == NULL) // FDD
1417
        harq_information_rel10->harq_size = 1;
1418
      else {
1419
        if (harq_information_rel10->ack_nack_mode == 1)
1420
          harq_information_rel10->harq_size = get_V_UL_DAI(module_idP,
frtabu's avatar
frtabu committed
1421 1422 1423
                                              CC_idP,
                                              rntiP,
                                              subframeP);
1424
        else
1425
          harq_information_rel10->harq_size = 1;
1426 1427 1428 1429 1430
      }
      break;

    default:      // for any other TM we need 2 bits harq
      if (cc->tdd_Config == NULL) {
1431
        harq_information_rel10->harq_size = 2;
1432
      } else {
1433
        if (harq_information_rel10->ack_nack_mode == 1)
1434
          harq_information_rel10->harq_size = get_V_UL_DAI(module_idP,
frtabu's avatar
frtabu committed
1435 1436 1437
                                              CC_idP,
                                              rntiP,
                                              subframeP);
1438
        else
1439
          harq_information_rel10->harq_size = 2;
1440 1441 1442
      }
      break;
  }       // get Tmode
frtabu's avatar
frtabu committed
1443

1444
  return;
1445 1446
}

1447
//------------------------------------------------------------------------------
1448
uint8_t
1449 1450 1451 1452 1453 1454 1455
Np[6][4] = {
  {0, 1, 3, 5},
  {0, 3, 8, 13},
  {0, 5, 13, 22},
  {0, 11, 27, 44},
  {0, 16, 41, 66},
  {0, 22, 55, 88}
1456
};
1457
//------------------------------------------------------------------------------
1458 1459

// This is part of the PUCCH allocation procedure (see Section 10.1 36.213)
1460
//------------------------------------------------------------------------------
1461
uint16_t
1462 1463
getNp(int dl_Bandwidth,
      uint8_t nCCE,
1464
      uint8_t plus1)
1465
//------------------------------------------------------------------------------
1466
{
1467
  AssertFatal(dl_Bandwidth < 6, "dl_Bandwidth %d>5\n", dl_Bandwidth);
1468

1469
  if (nCCE >= Np[dl_Bandwidth][2]) {
frtabu's avatar
frtabu committed
1470
    return(Np[dl_Bandwidth][2+plus1]);
1471
  }
frtabu's avatar
frtabu committed
1472 1473

  if (nCCE >= Np[dl_Bandwidth][1]) {
1474
    return(Np[dl_Bandwidth][1+plus1]);
1475
  }
frtabu's avatar
frtabu committed
1476

1477
  return(Np[dl_Bandwidth][0+plus1]);
1478 1479
}

1480
//------------------------------------------------------------------------------
1481
void
1482
fill_nfapi_harq_information(module_id_t                      module_idP,
1483 1484 1485
                            int                              CC_idP,
                            uint16_t                         rntiP,
                            nfapi_ul_config_harq_information *harq_information,
1486
                            uint8_t                          cce_idxP)
1487
//------------------------------------------------------------------------------
Cedric Roux's avatar
Cedric Roux committed
1488
{
1489 1490 1491
  eNB_MAC_INST *eNB     = RC.mac[module_idP];
  COMMON_channels_t *cc = &eNB->common_channels[CC_idP];
  UE_list_t *UE_list    = &eNB->UE_list;
1492
  int UE_id = find_UE_id(module_idP,
1493
                         rntiP);
1494 1495 1496 1497
  AssertFatal(UE_id >= 0, "UE_id cannot be found, impossible\n");
  AssertFatal(UE_list != NULL, "UE_list is null\n");
  harq_information->harq_information_rel11.tl.tag        = NFAPI_UL_CONFIG_REQUEST_HARQ_INFORMATION_REL11_TAG;
  harq_information->harq_information_rel11.num_ant_ports = 1;
Stefan's avatar
Stefan committed
1498 1499
  LTE_PhysicalConfigDedicated_t *physicalConfigDedicated = UE_list->UE_template[CC_idP][UE_id].physicalConfigDedicated;
  struct LTE_PUCCH_ConfigDedicated *pucch_ConfigDedicated = NULL;
frtabu's avatar
frtabu committed
1500

Stefan's avatar
Stefan committed
1501
  if (physicalConfigDedicated != NULL) pucch_ConfigDedicated = physicalConfigDedicated->pucch_ConfigDedicated;
1502

1503 1504
  switch (get_tmode(module_idP,
                    CC_idP,
1505
                    UE_id)) {
1506 1507 1508 1509 1510 1511 1512 1513
    case 1:
    case 2:
    case 5:
    case 6:
    case 7:
      if (cc->tdd_Config != NULL) {
        //      AssertFatal(UE_list->UE_template[CC_idP][UE_id].physicalConfigDedicated->pucch_ConfigDedicated != NULL,
        //      "pucch_ConfigDedicated is null for TDD!\n");
Stefan's avatar
Stefan committed
1514 1515 1516
        if (physicalConfigDedicated != NULL && pucch_ConfigDedicated != NULL &&
            pucch_ConfigDedicated->tdd_AckNackFeedbackMode != NULL &&
            *pucch_ConfigDedicated->tdd_AckNackFeedbackMode == LTE_PUCCH_ConfigDedicated__tdd_AckNackFeedbackMode_multiplexing) {
1517 1518 1519 1520 1521 1522 1523 1524
          harq_information->harq_information_rel10_tdd.harq_size             = 2;        // 2-bit ACK/NAK
          harq_information->harq_information_rel10_tdd.ack_nack_mode         = 1;        // multiplexing
        } else {
          harq_information->harq_information_rel10_tdd.harq_size             = 1;        // 1-bit ACK/NAK
          harq_information->harq_information_rel10_tdd.ack_nack_mode         = 0;        // bundling
        }

        harq_information->harq_information_rel10_tdd.tl.tag                    = NFAPI_UL_CONFIG_REQUEST_HARQ_INFORMATION_REL10_TDD_TAG;
1525
        harq_information->harq_information_rel10_tdd.n_pucch_1_0
Stefan's avatar
Stefan committed
1526
          = getNp(cc->mib->message.dl_Bandwidth, cce_idxP, 0) + cc->radioResourceConfigCommon->pucch_ConfigCommon.n1PUCCH_AN + cce_idxP;
1527
        harq_information->harq_information_rel10_tdd.number_of_pucch_resources = 1;
1528
      } else {
1529 1530 1531 1532
        harq_information->harq_information_rel9_fdd.tl.tag                     = NFAPI_UL_CONFIG_REQUEST_HARQ_INFORMATION_REL9_FDD_TAG;
        harq_information->harq_information_rel9_fdd.number_of_pucch_resources  = 1;
        harq_information->harq_information_rel9_fdd.harq_size                  = 1; // 1-bit ACK/NAK
        harq_information->harq_information_rel9_fdd.n_pucch_1_0                = cc->radioResourceConfigCommon->pucch_ConfigCommon.n1PUCCH_AN + cce_idxP;
1533
      }
1534 1535 1536 1537 1538

      break;

    default:      // for any other TM we need 2 bits harq
      if (cc->tdd_Config != NULL) {
Stefan's avatar
Stefan committed
1539
        AssertFatal(pucch_ConfigDedicated != NULL, "pucch_ConfigDedicated is null for TDD!\n");
1540

Stefan's avatar
Stefan committed
1541 1542
        if (pucch_ConfigDedicated->tdd_AckNackFeedbackMode != NULL &&
            *pucch_ConfigDedicated->tdd_AckNackFeedbackMode == LTE_PUCCH_ConfigDedicated__tdd_AckNackFeedbackMode_multiplexing) {
1543 1544 1545 1546 1547 1548 1549 1550 1551
          harq_information->harq_information_rel10_tdd.ack_nack_mode            = 1;  // multiplexing
        } else {
          harq_information->harq_information_rel10_tdd.ack_nack_mode            = 0;  // bundling
        }

        harq_information->harq_information_rel10_tdd.tl.tag                     = NFAPI_UL_CONFIG_REQUEST_HARQ_INFORMATION_REL10_TDD_TAG;
        harq_information->harq_information_rel10_tdd.harq_size                  = 2;
        harq_information->harq_information_rel10_tdd.n_pucch_1_0                = cc->radioResourceConfigCommon->pucch_ConfigCommon.n1PUCCH_AN + cce_idxP;
        harq_information->harq_information_rel10_tdd.number_of_pucch_resources  = 1;
1552
      } else {
1553 1554 1555 1556 1557
        harq_information->harq_information_rel9_fdd.tl.tag                      = NFAPI_UL_CONFIG_REQUEST_HARQ_INFORMATION_REL9_FDD_TAG;
        harq_information->harq_information_rel9_fdd.number_of_pucch_resources   = 1;
        harq_information->harq_information_rel9_fdd.ack_nack_mode               = 0;  // 1a/b
        harq_information->harq_information_rel9_fdd.harq_size                   = 2;
        harq_information->harq_information_rel9_fdd.n_pucch_1_0                 = cc->radioResourceConfigCommon->pucch_ConfigCommon.n1PUCCH_AN + cce_idxP;
1558
      }
1559 1560 1561

      break;
  }       // get Tmode
frtabu's avatar
frtabu committed
1562

Stefan's avatar
Stefan committed
1563
  return;
1564
}
Cedric Roux's avatar
Cedric Roux committed
1565

1566
//------------------------------------------------------------------------------
1567 1568
uint16_t
fill_nfapi_uci_acknak(module_id_t module_idP,
1569 1570 1571
                      int         CC_idP,
                      uint16_t    rntiP,
                      uint16_t    absSFP,
1572
                      uint8_t     cce_idxP)
1573
//------------------------------------------------------------------------------
1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584
{
  eNB_MAC_INST                   *eNB           = RC.mac[module_idP];
  COMMON_channels_t              *cc            = &eNB->common_channels[CC_idP];
  int                            ackNAK_absSF   = get_pucch1_absSF(cc, absSFP);
  nfapi_ul_config_request_t      *ul_req        = &eNB->UL_req_tmp[CC_idP][ackNAK_absSF % 10];
  nfapi_ul_config_request_body_t *ul_req_body   = &ul_req->ul_config_request_body;
  nfapi_ul_config_request_pdu_t  *ul_config_pdu = &ul_req_body->ul_config_pdu_list[ul_req_body->number_of_pdus];
  memset((void *) ul_config_pdu, 0, sizeof(nfapi_ul_config_request_pdu_t));
  ul_config_pdu->pdu_type                                               = NFAPI_UL_CONFIG_UCI_HARQ_PDU_TYPE;
  ul_config_pdu->pdu_size                                               = (uint8_t) (2 + sizeof(nfapi_ul_config_uci_harq_pdu));
  ul_config_pdu->uci_harq_pdu.ue_information.ue_information_rel8.tl.tag = NFAPI_UL_CONFIG_REQUEST_UE_INFORMATION_REL8_TAG;
1585
  ul_config_pdu->uci_harq_pdu.ue_information.ue_information_rel8.handle = 0;	// don't know how to use this
1586
  ul_config_pdu->uci_harq_pdu.ue_information.ue_information_rel8.rnti   = rntiP;
1587
  fill_nfapi_harq_information(module_idP,
Stefan's avatar
Stefan committed
1588
                              CC_idP,
1589
                              rntiP,
1590
                              &ul_config_pdu->uci_harq_pdu.harq_information,
Stefan's avatar
Stefan committed
1591
                              cce_idxP);
1592
  LOG_D(MAC, "Filled in UCI HARQ request for rnti %x SF %d.%d acknakSF %d.%d, cce_idxP %d-> n1_pucch %d\n",
1593 1594 1595
        rntiP,
        absSFP / 10,
        absSFP % 10,
Stefan's avatar
Stefan committed
1596
        ackNAK_absSF / 10,
1597
        ackNAK_absSF % 10,
Stefan's avatar
Stefan committed
1598
        cce_idxP,
Stefan's avatar
Stefan committed
1599
        ul_config_pdu->uci_harq_pdu.harq_information.harq_information_rel9_fdd.n_pucch_1_0);
1600 1601 1602 1603
  ul_req_body->number_of_pdus++;
  ul_req_body->tl.tag       = NFAPI_UL_CONFIG_REQUEST_BODY_TAG;
  ul_req->header.message_id = NFAPI_UL_CONFIG_REQUEST;
  ul_req->sfn_sf            = (ackNAK_absSF/10) << 4 | ackNAK_absSF%10;
1604

1605
  return (((ackNAK_absSF / 10) << 4) + (ackNAK_absSF % 10));
1606
}
1607

1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640
//------------------------------------------------------------------------------

void
fill_nfapi_mch_config(nfapi_dl_config_request_body_t *dl_req,
                  uint16_t length,
                  uint16_t pdu_index,
                  uint16_t rnti,
                  uint8_t resource_allocation_type,
                  uint16_t resource_block_coding,
                  uint8_t modulation,
                  uint16_t transmission_power,
                  uint8_t mbsfn_area_id){
  nfapi_dl_config_request_pdu_t *dl_config_pdu =
    &dl_req->dl_config_pdu_list[dl_req->number_pdu];
  memset((void *) dl_config_pdu, 0,
         sizeof(nfapi_dl_config_request_pdu_t));
  dl_config_pdu->pdu_type                                                    = NFAPI_DL_CONFIG_MCH_PDU_TYPE;
  dl_config_pdu->pdu_size                                                    = (uint8_t) (2 + sizeof(nfapi_dl_config_mch_pdu));
  dl_config_pdu->mch_pdu.mch_pdu_rel8.tl.tag                                 = NFAPI_DL_CONFIG_REQUEST_MCH_PDU_REL8_TAG;
  dl_config_pdu->mch_pdu.mch_pdu_rel8.length                                 = length;
  dl_config_pdu->mch_pdu.mch_pdu_rel8.pdu_index                              = pdu_index;
  dl_config_pdu->mch_pdu.mch_pdu_rel8.rnti                                   = rnti;
  dl_config_pdu->mch_pdu.mch_pdu_rel8.resource_allocation_type               = resource_allocation_type;
  dl_config_pdu->mch_pdu.mch_pdu_rel8.resource_block_coding                  = resource_block_coding;
  dl_config_pdu->mch_pdu.mch_pdu_rel8.modulation                             = modulation;
  dl_config_pdu->mch_pdu.mch_pdu_rel8.transmission_power                     = transmission_power;
  dl_config_pdu->mch_pdu.mch_pdu_rel8.mbsfn_area_id                          = mbsfn_area_id;
  dl_req->number_pdu++;
}

//------------------------------------------------------------------------------


1641
//------------------------------------------------------------------------------
1642
void
1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658
fill_nfapi_dlsch_config(eNB_MAC_INST *eNB,
                        nfapi_dl_config_request_body_t *dl_req,
                        uint16_t length,
                        int16_t pdu_index,
                        uint16_t rnti,
                        uint8_t resource_allocation_type,
                        uint8_t
                        virtual_resource_block_assignment_flag,
                        uint16_t resource_block_coding,
                        uint8_t modulation,
                        uint8_t redundancy_version,
                        uint8_t transport_blocks,
                        uint8_t transport_block_to_codeword_swap_flag,
                        uint8_t transmission_scheme,
                        uint8_t number_of_layers,
                        uint8_t number_of_subbands,
Stefan's avatar
Stefan committed
1659
                        // uint8_t codebook_index,
1660 1661 1662 1663 1664 1665 1666
                        uint8_t ue_category_capacity,
                        uint8_t pa,
                        uint8_t delta_power_offset_index,
                        uint8_t ngap,
                        uint8_t nprb,
                        uint8_t transmission_mode,
                        uint8_t num_bf_prb_per_subband,
1667
                        uint8_t num_bf_vector)
1668
//------------------------------------------------------------------------------
1669
{
Stefan's avatar
Stefan committed
1670
  nfapi_dl_config_request_pdu_t *dl_config_pdu = &dl_req->dl_config_pdu_list[dl_req->number_pdu];
1671
  memset((void *) dl_config_pdu, 0, sizeof(nfapi_dl_config_request_pdu_t));
1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687
  dl_config_pdu->pdu_type                                                        = NFAPI_DL_CONFIG_DLSCH_PDU_TYPE;
  dl_config_pdu->pdu_size                                                        = (uint8_t) (2 + sizeof(nfapi_dl_config_dlsch_pdu));
  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.tl.tag                                 = NFAPI_DL_CONFIG_REQUEST_DLSCH_PDU_REL8_TAG;
  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.length                                 = length;
  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.pdu_index                              = pdu_index;
  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.rnti                                   = rnti;
  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.resource_allocation_type               = resource_allocation_type;
  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.virtual_resource_block_assignment_flag = virtual_resource_block_assignment_flag;
  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.resource_block_coding                  = resource_block_coding;
  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.modulation                             = modulation;
  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.redundancy_version                     = redundancy_version;
  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.transport_blocks                       = transport_blocks;
  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.transport_block_to_codeword_swap_flag  = transport_block_to_codeword_swap_flag;
  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.transmission_scheme                    = transmission_scheme;
  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.number_of_layers                       = number_of_layers;
  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.number_of_subbands                     = number_of_subbands;
Stefan's avatar
Stefan committed
1688
  // dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.codebook_index                         = codebook_index;
1689 1690 1691 1692 1693 1694 1695 1696 1697
  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.ue_category_capacity                   = ue_category_capacity;
  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.pa                                     = pa;
  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.delta_power_offset_index               = delta_power_offset_index;
  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.ngap                                   = ngap;
  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.nprb                                   = nprb;
  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.transmission_mode                      = transmission_mode;
  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.num_bf_prb_per_subband                 = num_bf_prb_per_subband;
  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.num_bf_vector                          = num_bf_vector;
  dl_req->number_pdu++;
1698
  return;
1699
}
1700

1701
//------------------------------------------------------------------------------
1702
uint16_t
1703
fill_nfapi_tx_req(nfapi_tx_request_body_t *tx_req_body,
1704 1705 1706
                  uint16_t                absSF,
                  uint16_t                pdu_length,
                  int16_t                 pdu_index,
1707
                  uint8_t                 *pdu)
1708
//------------------------------------------------------------------------------
1709
{
1710 1711
  nfapi_tx_request_pdu_t *TX_req = &tx_req_body->tx_pdu_list[tx_req_body->number_of_pdus];
  LOG_D(MAC, "Filling TX_req %d for pdu length %d\n",
1712
        tx_req_body->number_of_pdus,
1713
        pdu_length);
1714 1715 1716 1717 1718 1719 1720 1721
  TX_req->pdu_length                 = pdu_length;
  TX_req->pdu_index                  = pdu_index;
  TX_req->num_segments               = 1;
  TX_req->segments[0].segment_length = pdu_length;
  TX_req->segments[0].segment_data   = pdu;
  tx_req_body->tl.tag                = NFAPI_TX_REQUEST_BODY_TAG;
  tx_req_body->number_of_pdus++;
  return (((absSF / 10) << 4) + (absSF % 10));
1722
}
1723

1724
//------------------------------------------------------------------------------
1725
void
1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744
fill_nfapi_ulsch_config_request_rel8(nfapi_ul_config_request_pdu_t *ul_config_pdu,
                                     uint8_t                        cqi_req,
                                     COMMON_channels_t              *cc,
                                     struct LTE_PhysicalConfigDedicated *physicalConfigDedicated,
                                     uint8_t                        tmode,
                                     uint32_t                       handle,
                                     uint16_t                       rnti,
                                     uint8_t                        resource_block_start,
                                     uint8_t                        number_of_resource_blocks,
                                     uint8_t                        mcs,
                                     uint8_t                        cyclic_shift_2_for_drms,
                                     uint8_t                        frequency_hopping_enabled_flag,
                                     uint8_t                        frequency_hopping_bits,
                                     uint8_t                        new_data_indication,
                                     uint8_t                        redundancy_version,
                                     uint8_t                        harq_process_number,
                                     uint8_t                        ul_tx_mode,
                                     uint8_t                        current_tx_nb,
                                     uint8_t                        n_srs,
1745
                                     uint16_t                       size)
1746
//------------------------------------------------------------------------------
1747
{
1748 1749
  uint8_t ri_size = 0;
  memset((void *) ul_config_pdu, 0, sizeof(nfapi_ul_config_request_pdu_t));
1750 1751 1752 1753 1754 1755 1756
  ul_config_pdu->pdu_type                                                    = NFAPI_UL_CONFIG_ULSCH_PDU_TYPE;
  ul_config_pdu->pdu_size                                                    = (uint8_t) (2 + sizeof(nfapi_ul_config_ulsch_pdu));
  ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.tl.tag                             = NFAPI_UL_CONFIG_REQUEST_ULSCH_PDU_REL8_TAG;
  ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.handle                             = handle;
  ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.rnti                               = rnti;
  ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.resource_block_start               = resource_block_start;
  ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.number_of_resource_blocks          = number_of_resource_blocks;
1757

1758 1759
  if (mcs < 11)      ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.modulation_type = 2;
  else if (mcs < 21) ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.modulation_type = 4;
1760 1761
  else if(mcs < 29)  ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.modulation_type = 6;
  else               ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.modulation_type = 0;
1762

1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780
  ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.cyclic_shift_2_for_drms            = cyclic_shift_2_for_drms;
  ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.frequency_hopping_enabled_flag     = frequency_hopping_enabled_flag;
  ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.frequency_hopping_bits             = frequency_hopping_bits;
  ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.new_data_indication                = new_data_indication;
  ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.redundancy_version                 = redundancy_version;
  ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.harq_process_number                = harq_process_number;
  ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.ul_tx_mode                         = ul_tx_mode;
  ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.current_tx_nb                      = current_tx_nb;
  ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.n_srs                              = n_srs;
  ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.size                               = size;

  if (cqi_req == 1) {
    // Add CQI portion
    ul_config_pdu->pdu_type = NFAPI_UL_CONFIG_ULSCH_CQI_RI_PDU_TYPE;
    ul_config_pdu->pdu_size = (uint8_t) (2 + sizeof(nfapi_ul_config_ulsch_cqi_ri_pdu));
    ul_config_pdu->ulsch_cqi_ri_pdu.cqi_ri_information.cqi_ri_information_rel9.tl.tag = NFAPI_UL_CONFIG_REQUEST_CQI_RI_INFORMATION_REL9_TAG;
    ul_config_pdu->ulsch_cqi_ri_pdu.cqi_ri_information.cqi_ri_information_rel9.report_type = 1;
    ul_config_pdu->ulsch_cqi_ri_pdu.cqi_ri_information.cqi_ri_information_rel9.aperiodic_cqi_pmi_ri_report.number_of_cc = 1;
1781 1782
    LOG_D(MAC, "report_type %d\n",
          ul_config_pdu->ulsch_cqi_ri_pdu.cqi_ri_information.cqi_ri_information_rel9.report_type);
1783

1784 1785 1786
    if (cc->p_eNB <= 2 && (tmode == 3 || tmode == 4 || tmode == 8 || tmode == 9 || tmode == 10)) ri_size = 1;
    else if (cc->p_eNB <= 2) ri_size = 0;
    else if (cc->p_eNB == 4) ri_size = 2;
1787

frtabu's avatar
frtabu committed
1788
    ul_config_pdu->ulsch_cqi_ri_pdu.cqi_ri_information.cqi_ri_information_rel9.aperiodic_cqi_pmi_ri_report.cc[0].ri_size = ri_size;
1789 1790 1791
    AssertFatal(physicalConfigDedicated->cqi_ReportConfig != NULL,"physicalConfigDedicated->cqi_ReportConfig is null!\n");
    AssertFatal(physicalConfigDedicated->cqi_ReportConfig->cqi_ReportModeAperiodic != NULL,"physicalConfigDedicated->cqi_ReportModeAperiodic is null!\n");
    AssertFatal(physicalConfigDedicated->pusch_ConfigDedicated != NULL,"physicalConfigDedicated->puschConfigDedicated is null!\n");
1792 1793
    nfapi_ul_config_cqi_ri_information_rel9_t *ri_information = &ul_config_pdu->ulsch_cqi_ri_pdu.cqi_ri_information.cqi_ri_information_rel9;
    int max_ri = (1 << ri_information->aperiodic_cqi_pmi_ri_report.cc[0].ri_size);
frtabu's avatar
frtabu committed
1794

1795
    for (int ri = 0; ri < max_ri; ri++) {
1796
      ri_information->aperiodic_cqi_pmi_ri_report.cc[0].dl_cqi_pmi_size[ri]
1797 1798 1799 1800
        = get_dl_cqi_pmi_size_pusch(cc,
                                    tmode,
                                    1 + ri,
                                    physicalConfigDedicated->cqi_ReportConfig->cqi_ReportModeAperiodic);
1801
    }
frtabu's avatar
frtabu committed
1802

1803 1804
    ri_information->delta_offset_cqi = physicalConfigDedicated->pusch_ConfigDedicated->betaOffset_CQI_Index;
    ri_information->delta_offset_ri = physicalConfigDedicated->pusch_ConfigDedicated->betaOffset_RI_Index;
1805
  }
frtabu's avatar
frtabu committed
1806

1807
  return;
1808 1809
}

1810
#if (LTE_RRC_VERSION >= MAKE_VERSION(13, 0, 0))
1811
//------------------------------------------------------------------------------
1812
void
1813
fill_nfapi_ulsch_config_request_emtc(nfapi_ul_config_request_pdu_t *ul_config_pdu,
1814 1815
                                     uint8_t ue_type,
                                     uint16_t total_number_of_repetitions,
1816
                                     uint16_t repetition_number,
1817
                                     uint16_t initial_transmission_sf_io)
1818
//------------------------------------------------------------------------------
Cedric Roux's avatar
Cedric Roux committed
1819
{
1820 1821 1822 1823 1824 1825
  // Re13 fields
  ul_config_pdu->ulsch_pdu.ulsch_pdu_rel13.tl.tag                      = NFAPI_UL_CONFIG_REQUEST_ULSCH_PDU_REL13_TAG;
  ul_config_pdu->ulsch_pdu.ulsch_pdu_rel13.ue_type                     = ue_type;
  ul_config_pdu->ulsch_pdu.ulsch_pdu_rel13.total_number_of_repetitions = total_number_of_repetitions;
  ul_config_pdu->ulsch_pdu.ulsch_pdu_rel13.repetition_number           = repetition_number;
  ul_config_pdu->ulsch_pdu.ulsch_pdu_rel13.initial_transmission_sf_io  = initial_transmission_sf_io;
1826
  return;
1827
}
1828

1829
//------------------------------------------------------------------------------
1830 1831
int
get_numnarrowbands(long dl_Bandwidth)
1832
//------------------------------------------------------------------------------
Cedric Roux's avatar
Cedric Roux committed
1833
{
1834 1835 1836
  int nb_tab[6] = { 1, 2, 4, 8, 12, 16 };
  AssertFatal(dl_Bandwidth < 7 || dl_Bandwidth >= 0, "dl_Bandwidth not in [0..6]\n");
  return (nb_tab[dl_Bandwidth]);
1837 1838
}

1839
//------------------------------------------------------------------------------
1840 1841
int
get_numnarrowbandbits(long dl_Bandwidth)
1842
//------------------------------------------------------------------------------
Cedric Roux's avatar
Cedric Roux committed
1843
{
1844 1845 1846
  int nbbits_tab[6] = { 0, 1, 2, 3, 4, 4 };
  AssertFatal(dl_Bandwidth < 7 || dl_Bandwidth >= 0, "dl_Bandwidth not in [0..6]\n");
  return (nbbits_tab[dl_Bandwidth]);
1847 1848 1849
}

//This implements the frame/subframe condition for first subframe of MPDCCH transmission (Section 9.1.5 36.213, Rel 13/14)
1850
//------------------------------------------------------------------------------
1851
int
1852 1853
startSF_fdd_RA_times2[8] = { 2, 3, 4, 5, 8, 10, 16, 20 };
//------------------------------------------------------------------------------
1854

1855
//------------------------------------------------------------------------------
1856
int
1857 1858
startSF_tdd_RA[7] = { 1, 2, 4, 5, 8, 10, 20 };
//------------------------------------------------------------------------------
1859

1860
//------------------------------------------------------------------------------
1861
int
1862 1863
mpdcch_sf_condition(eNB_MAC_INST *eNB,
                    int CC_id,
1864
                    frame_t frameP,
1865
                    sub_frame_t subframeP,
1866
                    int rmax,
1867 1868
                    MPDCCH_TYPES_t mpdcch_type,
                    int UE_id)
1869 1870 1871
//------------------------------------------------------------------------------
{
  struct LTE_PRACH_ConfigSIB_v1310 *ext4_prach = eNB->common_channels[CC_id].radioResourceConfigCommon_BR-> ext4->prach_ConfigCommon_v1310;
1872
  int T;
1873
  LTE_EPDCCH_SetConfig_r11_t *epdcch_setconfig_r11;
1874

1875
  switch (mpdcch_type) {
1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888
    case TYPE0:
      AssertFatal(1 == 0, "MPDCCH Type 0 not handled yet\n");
      break;

    case TYPE1:
      AssertFatal(1 == 0, "MPDCCH Type 1 not handled yet\n");
      break;

    case TYPE1A:
      AssertFatal(1 == 0, "MPDCCH Type 1A not handled yet\n");
      break;

    case TYPE2:   // RAR
1889
      AssertFatal(ext4_prach->mpdcch_startSF_CSS_RA_r13 != NULL, "mpdcch_startSF_CSS_RA_r13 is null\n");
1890 1891 1892
      AssertFatal(rmax > 0, "rmax is 0!\b");

      if (eNB->common_channels[CC_id].tdd_Config == NULL) //FDD
1893
        T = (rmax * startSF_fdd_RA_times2[ext4_prach->mpdcch_startSF_CSS_RA_r13->choice.fdd_r13]) >> 1;
1894
      else      //TDD
1895
        T = rmax * startSF_tdd_RA[ext4_prach->mpdcch_startSF_CSS_RA_r13->choice.tdd_r13];
1896 1897 1898 1899 1900 1901 1902 1903 1904 1905

      break;

    case TYPE2A:
      AssertFatal(1 == 0, "MPDCCH Type 2A not handled yet\n");
      break;

    case TYPEUESPEC:
      epdcch_setconfig_r11 =
        eNB->UE_list.UE_template[CC_id][UE_id].physicalConfigDedicated->ext4->epdcch_Config_r11->config_r11.choice.setup.setConfigToAddModList_r11->list.array[0];
1906 1907
      AssertFatal(epdcch_setconfig_r11 != NULL, " epdcch_setconfig_r11 is null for UE specific \n");
      AssertFatal(epdcch_setconfig_r11->ext2 != NULL, " ext2 doesn't exist in epdcch config ' \n");
1908 1909

      if (eNB->common_channels[CC_id].tdd_Config == NULL) //FDD
1910
        T = (rmax * startSF_fdd_RA_times2[epdcch_setconfig_r11->ext2->mpdcch_config_r13->choice.setup.mpdcch_StartSF_UESS_r13.choice.fdd_r13]) >> 1;
1911
      else      //TDD
1912
        T = rmax * startSF_tdd_RA[epdcch_setconfig_r11->ext2->mpdcch_config_r13->choice.setup.mpdcch_StartSF_UESS_r13.choice.tdd_r13];
1913 1914 1915 1916

      break;

    default:
1917
      return 0;
1918
  }
1919

1920
  AssertFatal(T > 0, "T is 0!\n");
1921

1922
  if (((10 * frameP) + subframeP) % T == 0) return 1;
frtabu's avatar
frtabu committed
1923

1924
  return 0;
1925 1926
}

1927
//------------------------------------------------------------------------------
1928 1929 1930
int
narrowband_to_first_rb(COMMON_channels_t *cc,
                       int nb_index)
1931
//------------------------------------------------------------------------------
Cedric Roux's avatar
Cedric Roux committed
1932
{
1933
  switch (cc->mib->message.dl_Bandwidth) {
1934
    case 0:     // 6 PRBs, N_NB=1, i_0=0
1935
      return 0;
1936 1937

    case 3:     // 50 PRBs, N_NB=8, i_0=1
1938
      return (1 + (6 * nb_index));
1939 1940

    case 5:     // 100 PRBs, N_NB=16, i_0=2
1941
      return (2 + (6 * nb_index));
1942 1943 1944

    case 1:     // 15 PRBs  N_NB=2, i_0=1
      if (nb_index > 0)
1945
        return 1;
frtabu's avatar
frtabu committed
1946

1947
      return 0;
1948 1949 1950 1951

    case 2:     // 25 PRBs, N_NB=4, i_0=0
      if (nb_index > 1)
        return (1 + (6 * nb_index));
frtabu's avatar
frtabu committed
1952

1953
      return ((6 * nb_index));
1954 1955 1956 1957

    case 4:     // 75 PRBs, N_NB=12, i_0=1
      if (nb_index > 5)
        return (2 + (6 * nb_index));
frtabu's avatar
frtabu committed
1958

1959
      return (1 + (6 * nb_index));
1960 1961 1962 1963 1964

    default:
      AssertFatal(1 == 0, "Impossible dl_Bandwidth %d\n",
                  (int) cc->mib->message.dl_Bandwidth);
      break;
1965
  }
frtabu's avatar
frtabu committed
1966

1967
  return 0;
1968
}
1969 1970
#endif

1971
//------------------------------------------------------------------------------
1972
void
1973
init_ue_sched_info(void)
1974
//------------------------------------------------------------------------------
1975
{
1976 1977 1978 1979
  module_id_t i, j, k;

  for (i = 0; i < NUMBER_OF_eNB_MAX; i++) {
    for (k = 0; k < MAX_NUM_CCs; k++) {
1980
      for (j = 0; j < MAX_MOBILES_PER_ENB; j++) {
1981 1982 1983 1984 1985 1986 1987 1988 1989
        // init DL
        eNB_dlsch_info[i][k][j].weight = 0;
        eNB_dlsch_info[i][k][j].subframe = 0;
        eNB_dlsch_info[i][k][j].serving_num = 0;
        eNB_dlsch_info[i][k][j].status = S_DL_NONE;
        // init UL
        eNB_ulsch_info[i][k][j].subframe = 0;
        eNB_ulsch_info[i][k][j].serving_num = 0;
        eNB_ulsch_info[i][k][j].status = S_UL_NONE;
1990
      }
1991
    }
1992
  }
frtabu's avatar
frtabu committed
1993

1994
  return;
1995 1996
}

1997
//------------------------------------------------------------------------------
1998 1999 2000
unsigned char
get_ue_weight(module_id_t module_idP,
              int CC_idP,
2001
              int ue_idP)
2002
//------------------------------------------------------------------------------
2003
{
2004
  return (eNB_dlsch_info[module_idP][CC_idP][ue_idP].weight);
2005 2006
}

2007
//------------------------------------------------------------------------------
2008 2009
int
find_UE_id(module_id_t mod_idP,
2010
           rnti_t rntiP)
2011
//------------------------------------------------------------------------------
2012
{
2013 2014 2015
  int UE_id;
  UE_list_t *UE_list = &RC.mac[mod_idP]->UE_list;

2016
  for (UE_id = 0; UE_id < MAX_MOBILES_PER_ENB; UE_id++) {
2017
    if (UE_list->active[UE_id] == TRUE) {
2018
      if (UE_list->UE_template[UE_PCCID(mod_idP, UE_id)][UE_id].rnti == rntiP) {
2019 2020
        return UE_id;
      }
Raymond Knopp's avatar
 
Raymond Knopp committed
2021
    }
2022
  }
2023

2024
  return -1;
2025 2026
}

2027
//------------------------------------------------------------------------------
2028 2029 2030
int
find_RA_id(module_id_t mod_idP,
           int CC_idP,
2031
           rnti_t rntiP)
2032 2033
//------------------------------------------------------------------------------
{
2034 2035
  int RA_id;
  AssertFatal(RC.mac[mod_idP], "RC.mac[%d] is null\n", mod_idP);
2036
  RA_t *ra = (RA_t *) &RC.mac[mod_idP]->common_channels[CC_idP].ra[0];
2037

2038
  for (RA_id = 0; RA_id < NB_RA_PROC_MAX; RA_id++) {
2039
    LOG_D(MAC, "Checking RA_id %d for %x : state %d\n",
2040 2041
          RA_id,
          rntiP,
2042
          ra[RA_id].state);
Cedric Roux's avatar
Cedric Roux committed
2043

2044 2045
    if (ra[RA_id].state != IDLE && ra[RA_id].rnti == rntiP)
      return RA_id;
2046
  }
2047

2048
  return -1;
2049 2050
}

2051
//------------------------------------------------------------------------------
2052 2053
int
UE_num_active_CC(UE_list_t *listP,
2054
                 int ue_idP)
2055
//------------------------------------------------------------------------------
2056
{
2057
  return (listP->numactiveCCs[ue_idP]);
Raymond Knopp's avatar
 
Raymond Knopp committed
2058
}
2059

2060
//------------------------------------------------------------------------------
2061 2062
int
UE_PCCID(module_id_t mod_idP,
2063
         int ue_idP)
2064
//------------------------------------------------------------------------------
2065
{
2066
  return (RC.mac[mod_idP]->UE_list.pCC_id[ue_idP]);
Raymond Knopp's avatar
 
Raymond Knopp committed
2067
}
2068

2069
//------------------------------------------------------------------------------
2070 2071
rnti_t
UE_RNTI(module_id_t mod_idP,
2072
        int ue_idP)
2073
//------------------------------------------------------------------------------
2074
{
2075
  if (!RC.mac || !RC.mac[mod_idP]) return 0;
Stefan's avatar
Stefan committed
2076

2077
  rnti_t rnti = RC.mac[mod_idP]->UE_list.UE_template[UE_PCCID(mod_idP,
frtabu's avatar
frtabu committed
2078
                ue_idP)][ue_idP].rnti;
2079

2080 2081
  if (rnti > 0) {
    return (rnti);
2082
  }
2083

Robert Schmidt's avatar
Robert Schmidt committed
2084
  //LOG_D(MAC, "[eNB %d] Couldn't find RNTI for UE %d\n", mod_idP, ue_idP);
2085 2086
  //display_backtrace();
  return (NOT_A_RNTI);
2087
}
Raymond Knopp's avatar
 
Raymond Knopp committed
2088

2089
//------------------------------------------------------------------------------
2090 2091
boolean_t
is_UE_active(module_id_t mod_idP,
2092
             int ue_idP)
2093
//------------------------------------------------------------------------------
2094
{
2095
  return (RC.mac[mod_idP]->UE_list.active[ue_idP]);
2096
}
Raymond Knopp's avatar
 
Raymond Knopp committed
2097

2098
//------------------------------------------------------------------------------
2099
unsigned char
Stefan's avatar
Stefan committed
2100 2101
get_aggregation(uint8_t bw_index,
                uint8_t cqi,
2102
                uint8_t dci_fmt)
2103
//------------------------------------------------------------------------------
2104
{
2105
  unsigned char aggregation = 3;
2106

2107
  switch (dci_fmt) {
2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132
    case format0:
      aggregation = cqi2fmt0_agg[bw_index][cqi];
      break;

    case format1:
    case format1A:
    case format1B:
    case format1D:
      aggregation = cqi2fmt1x_agg[bw_index][cqi];
      break;

    case format2:
    case format2A:
    case format2B:
    case format2C:
    case format2D:
      aggregation = cqi2fmt2x_agg[bw_index][cqi];
      break;

    case format1C:
    case format1E_2A_M10PRB:
    case format3:
    case format3A:
    case format4:
    default:
Stefan's avatar
Stefan committed
2133 2134 2135
      LOG_W(MAC, "unsupported DCI format %d\n",
            dci_fmt);
      break;
2136
  }
Cedric Roux's avatar
Cedric Roux committed
2137

Stefan's avatar
Stefan committed
2138 2139 2140 2141 2142
  LOG_D(MAC, "Aggregation level %d (cqi %d, bw_index %d, format %d)\n",
        1 << aggregation,
        cqi,
        bw_index,
        dci_fmt);
2143
  return 1 << aggregation;
2144
}
Raymond Knopp's avatar
 
Raymond Knopp committed
2145

2146
//------------------------------------------------------------------------------
2147 2148 2149
/*
 * Dump the UL or DL UE_list into LOG_T(MAC)
 */
2150 2151 2152
void
dump_ue_list(UE_list_t *listP,
             int ul_flag)
2153
//------------------------------------------------------------------------------
2154
{
2155
  if (ul_flag == 0) {
2156 2157
    for (int j = listP->head; j >= 0; j = listP->next[j]) {
      LOG_T(MAC, "DL list node %d => %d\n",
2158
            j,
2159
            listP->next[j]);
2160
    }
2161
  } else {
2162 2163
    for (int j = listP->head_ul; j >= 0; j = listP->next_ul[j]) {
      LOG_T(MAC, "UL list node %d => %d\n",
2164
            j,
2165
            listP->next_ul[j]);
2166
    }
2167
  }
frtabu's avatar
frtabu committed
2168

2169
  return;
Raymond Knopp's avatar
 
Raymond Knopp committed
2170 2171
}

2172
//------------------------------------------------------------------------------
2173 2174 2175 2176
int
add_new_ue(module_id_t mod_idP,
           int cc_idP,
           rnti_t rntiP,
2177
           int harq_pidP
2178
#if (LTE_RRC_VERSION >= MAKE_VERSION(14, 0, 0))
frtabu's avatar
frtabu committed
2179
  , uint8_t rach_resource_type
2180
#endif
frtabu's avatar
frtabu committed
2181
          )
2182
//------------------------------------------------------------------------------
2183 2184 2185 2186
{
  int UE_id;
  int i, j;
  UE_list_t *UE_list = &RC.mac[mod_idP]->UE_list;
2187
  LOG_D(MAC, "[eNB %d, CC_id %d] Adding UE with rnti %x (next avail %d, num_UEs %d)\n",
2188 2189 2190 2191
        mod_idP,
        cc_idP,
        rntiP,
        UE_list->avail,
2192
        UE_list->num_UEs);
2193 2194
  dump_ue_list(UE_list, 0);

2195
  for (i = 0; i < MAX_MOBILES_PER_ENB; i++) {
2196 2197
    if (UE_list->active[i] == TRUE)
      continue;
2198

2199
    UE_id = i;
2200
    memset(&UE_list->UE_template[cc_idP][UE_id], 0, sizeof(UE_TEMPLATE));
2201 2202 2203 2204 2205 2206 2207 2208 2209
    UE_list->UE_template[cc_idP][UE_id].rnti = rntiP;
    UE_list->UE_template[cc_idP][UE_id].configured = FALSE;
    UE_list->numactiveCCs[UE_id] = 1;
    UE_list->numactiveULCCs[UE_id] = 1;
    UE_list->pCC_id[UE_id] = cc_idP;
    UE_list->ordered_CCids[0][UE_id] = cc_idP;
    UE_list->ordered_ULCCids[0][UE_id] = cc_idP;
    UE_list->num_UEs++;
    UE_list->active[UE_id] = TRUE;
2210
#if defined(USRP_REC_PLAY) // not specific to record/playback ?
2211
    UE_list->UE_template[cc_idP][UE_id].pre_assigned_mcs_ul = 0;
2212
#endif
2213
#if (LTE_RRC_VERSION >= MAKE_VERSION(14, 0, 0))
2214
    UE_list->UE_template[cc_idP][UE_id].rach_resource_type = rach_resource_type;
2215
#endif
2216
    memset((void *) &UE_list->UE_sched_ctrl[UE_id],
2217
           0,
2218
           sizeof(UE_sched_ctrl_t));
2219
    memset((void *) &UE_list->eNB_UE_stats[cc_idP][UE_id],
2220
           0,
2221
           sizeof(eNB_UE_STATS));
2222
    UE_list->UE_sched_ctrl[UE_id].ue_reestablishment_reject_timer = 0;
2223
    /* default slice in case there was something different */
2224 2225
    UE_list->assoc_dl_slice_idx[UE_id] = 0;
    UE_list->assoc_ul_slice_idx[UE_id] = 0;
2226
    UE_list->UE_sched_ctrl[UE_id].ta_update = 31;
2227

2228
    for (j = 0; j < 8; j++) {
2229 2230
      UE_list->UE_template[cc_idP][UE_id].oldNDI[j] = 0;
      UE_list->UE_template[cc_idP][UE_id].oldNDI_UL[j] = 0;
2231 2232
      UE_list->UE_sched_ctrl[UE_id].round[cc_idP][j] = 8;
      UE_list->UE_sched_ctrl[UE_id].round_UL[cc_idP][j] = 0;
Raymond Knopp's avatar
 
Raymond Knopp committed
2233
    }
2234

2235 2236 2237
    eNB_ulsch_info[mod_idP][cc_idP][UE_id].status = S_UL_WAITING;
    eNB_dlsch_info[mod_idP][cc_idP][UE_id].status = S_DL_WAITING;
    LOG_D(MAC, "[eNB %d] Add UE_id %d on Primary CC_id %d: rnti %x\n",
2238 2239 2240
          mod_idP,
          UE_id,
          cc_idP,
2241
          rntiP);
2242
    dump_ue_list(UE_list,
2243
                 0);
2244 2245 2246
    return (UE_id);
  }

2247 2248
  // printf("MAC: cannot add new UE for rnti %x\n", rntiP);
  LOG_E(MAC, "error in add_new_ue(), could not find space in UE_list, Dumping UE list\n");
2249
  dump_ue_list(UE_list,
2250 2251
               0);
  return -1;
2252 2253
}

2254
//------------------------------------------------------------------------------
2255 2256 2257
/*
 * Remove MAC context of UE
 */
2258 2259
int
rrc_mac_remove_ue(module_id_t mod_idP,
2260
                  rnti_t rntiP)
2261
//------------------------------------------------------------------------------
2262
{
2263
  UE_list_t *UE_list = &RC.mac[mod_idP]->UE_list;
2264 2265 2266
  int UE_id = find_UE_id(mod_idP, rntiP);
  eNB_UE_STATS *ue_stats = NULL;
  int pCC_id = -1;
2267

2268
  if (UE_id == -1) {
2269
    LOG_W(MAC,"rrc_mac_remove_ue: UE %x not found\n",
2270
          rntiP);
2271
    return 0;
2272
  }
2273

2274 2275
  pCC_id = UE_PCCID(mod_idP, UE_id);

2276 2277
  LOG_I(MAC,"Removing UE %d from Primary CC_id %d (rnti %x)\n",
        UE_id,
2278
        pCC_id,
2279
        rntiP);
2280 2281 2282

  dump_ue_list(UE_list, 0); // DL list displayed in LOG_T(MAC)

2283 2284
  UE_list->active[UE_id] = FALSE;
  UE_list->num_UEs--;
2285

2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309
  /* If present, remove UE from DL list */
  if (UE_list->head == UE_id) {
    UE_list->head = UE_list->next[UE_id];
  } else {
    int previous = prev(UE_list, UE_id, 0);

    if (previous != -1) {
      UE_list->next[previous] = UE_list->next[UE_id];
    }
  }

  /* If present, remove UE from UL list */
  if (UE_list->head_ul == UE_id) {
    UE_list->head_ul = UE_list->next_ul[UE_id];
  } else {
    int previous = prev(UE_list, UE_id, 1);

    if (previous != -1) {
      UE_list->next_ul[previous] = UE_list->next_ul[UE_id];
    }
  }

  /* Clear all remaining pending transmissions */
  memset(&UE_list->UE_template[pCC_id][UE_id],
2310 2311 2312 2313 2314
          0,
          sizeof(UE_TEMPLATE));
  ue_stats = &UE_list->eNB_UE_stats[pCC_id][UE_id];
  ue_stats->total_rbs_used = 0;
  ue_stats->total_rbs_used_retx = 0;
2315

2316
  for (int j = 0; j < NB_RB_MAX; j++ ) {
2317 2318
    ue_stats->num_pdu_tx[j] = 0;
    ue_stats->num_bytes_tx[j] = 0;
2319
  }
2320

2321 2322 2323 2324 2325
  ue_stats->num_retransmission = 0;
  ue_stats->total_sdu_bytes = 0;
  ue_stats->total_pdu_bytes = 0;
  ue_stats->total_num_pdus = 0;
  ue_stats->total_rbs_used_rx = 0;
2326

2327
  for (int j = 0; j < NB_RB_MAX; j++ ) {
2328 2329
    ue_stats->num_pdu_rx[j] = 0;
    ue_stats->num_bytes_rx[j] = 0;
2330
  }
2331

2332 2333 2334 2335
  ue_stats->num_errors_rx = 0;
  ue_stats->total_pdu_bytes_rx = 0;
  ue_stats->total_num_pdus_rx = 0;
  ue_stats->total_num_errors_rx = 0;
2336 2337 2338

  eNB_ulsch_info[mod_idP][pCC_id][UE_id].rnti = NOT_A_RNTI;
  eNB_ulsch_info[mod_idP][pCC_id][UE_id].status = S_UL_NONE;
2339
  eNB_ulsch_info[mod_idP][pCC_id][UE_id].serving_num = 0;
2340 2341 2342

  eNB_dlsch_info[mod_idP][pCC_id][UE_id].rnti = NOT_A_RNTI;
  eNB_dlsch_info[mod_idP][pCC_id][UE_id].status = S_DL_NONE;
2343
  eNB_dlsch_info[mod_idP][pCC_id][UE_id].serving_num = 0;
2344

2345
  // check if this has an RA process active
2346 2347
  if (find_RA_id(mod_idP,
                 pCC_id,
2348
                 rntiP) != -1) {
2349 2350 2351
    cancel_ra_proc(mod_idP,
                   pCC_id,
                   0,
2352
                   rntiP);
2353
  }
Raymond Knopp's avatar
 
Raymond Knopp committed
2354

2355
  if(rrc_release_info.num_UEs > 0){
2356 2357 2358
    while(pthread_mutex_trylock(&rrc_release_freelist)) {
      /* spin... */
    }
2359
    uint16_t release_total = 0;
2360

2361 2362
    for (uint16_t release_num = 0; release_num < NUMBER_OF_UE_MAX; release_num++) {
      if (rrc_release_info.RRC_release_ctrl[release_num].flag > 0) {
2363
        release_total++;
2364
      } else {
2365 2366
        continue;
      }
2367

2368
      if (rrc_release_info.RRC_release_ctrl[release_num].rnti == rntiP) {
2369 2370 2371 2372
        rrc_release_info.RRC_release_ctrl[release_num].flag = 0;
        rrc_release_info.num_UEs--;
        release_total--;
      }
2373

2374
      if (release_total >= rrc_release_info.num_UEs) {
2375
        break;
wujing's avatar
wujing committed
2376
      }
2377
    }
2378
    pthread_mutex_unlock(&rrc_release_freelist);
2379
  }
2380

2381
  pthread_mutex_unlock(&rrc_release_freelist);
2382
  return 0;
Raymond Knopp's avatar
 
Raymond Knopp committed
2383
}
2384

2385
//------------------------------------------------------------------------------
2386 2387 2388
/*
 * Returns the previous UE_id in the scheduling list in UL or DL
 */
2389 2390 2391 2392
int
prev(UE_list_t *listP,
     int nodeP,
     int ul_flag)
2393
//------------------------------------------------------------------------------
2394
{
2395 2396
  if (ul_flag == 0) {
    if (nodeP == listP->head) {
2397
      return nodeP;
2398
    }
2399

2400
    for (int j = listP->head; j >= 0; j = listP->next[j]) {
2401
      if (listP->next[j] == nodeP) {
2402
        return j;
2403 2404 2405 2406
      }
    }
  } else {
    if (nodeP == listP->head_ul) {
2407
      return nodeP;
2408
    }
2409

2410
    for (int j = listP->head_ul; j >= 0; j = listP->next_ul[j]) {
2411
      if (listP->next_ul[j] == nodeP) {
2412
        return j;
2413
      }
2414
    }
2415
  }
2416

2417
  LOG_E(MAC, "error in prev(), could not find previous to %d in UE_list %s, should never happen, Dumping UE list\n",
2418
        nodeP,
2419
        (ul_flag == 0) ? "DL" : "UL");
2420
  dump_ue_list(listP,
2421 2422
               ul_flag);
  return -1;
2423
}
Raymond Knopp's avatar
 
Raymond Knopp committed
2424

2425
//------------------------------------------------------------------------------
2426 2427 2428 2429 2430
void
swap_UEs(UE_list_t *listP,
         int nodeiP,
         int nodejP,
         int ul_flag)
2431
//------------------------------------------------------------------------------
2432
{
2433
  int prev_i, prev_j, next_i, next_j;
2434 2435
  LOG_T(MAC, "Swapping UE %d,%d\n",
        nodeiP,
2436
        nodejP);
2437
  dump_ue_list(listP,
2438
               ul_flag);
2439 2440
  prev_i = prev(listP,
                nodeiP,
2441
                ul_flag);
2442 2443
  prev_j = prev(listP,
                nodejP,
2444
                ul_flag);
2445
  AssertFatal((prev_i >= 0) && (prev_j >= 0), "swap_UEs: problem");
2446

2447 2448 2449 2450 2451 2452 2453
  if (ul_flag == 0) {
    next_i = listP->next[nodeiP];
    next_j = listP->next[nodejP];
  } else {
    next_i = listP->next_ul[nodeiP];
    next_j = listP->next_ul[nodejP];
  }
2454

2455
  LOG_T(MAC, "[%s] next_i %d, next_i, next_j %d, head %d \n",
2456 2457 2458
        (ul_flag == 0) ? "DL" : "UL",
        next_i,
        next_j,
2459
        listP->head);
2460

2461
  if (ul_flag == 0) {
2462
    if (next_i == nodejP) { // case ... p(i) i j n(j) ... => ... p(j) j i n(i) ...
2463
      LOG_T(MAC, "Case ... p(i) i j n(j) ... => ... p(j) j i n(i) ...\n");
2464 2465
      listP->next[nodeiP] = next_j;
      listP->next[nodejP] = nodeiP;
2466

2467 2468
      if (nodeiP == listP->head) {  // case i j n(j)
        listP->head = nodejP;
2469
      } else {
2470
        listP->next[prev_i] = nodejP;
2471
      }
2472
    } else if (next_j == nodeiP) {  // case ... p(j) j i n(i) ... => ... p(i) i j n(j) ...
2473
      LOG_T(MAC, "Case ... p(j) j i n(i) ... => ... p(i) i j n(j) ...\n");
2474 2475 2476
      listP->next[nodejP] = next_i;
      listP->next[nodeiP] = nodejP;

2477 2478
      if (nodejP == listP->head) {  // case j i n(i)
        listP->head = nodeiP;
2479
      } else {
2480
        listP->next[prev_j] = nodeiP;
2481
      }
2482
    } else {    // case ...  p(i) i n(i) ... p(j) j n(j) ...
2483 2484 2485 2486
      listP->next[nodejP] = next_i;
      listP->next[nodeiP] = next_j;

      if (nodeiP == listP->head) {
2487
        LOG_T(MAC, "changing head to %d\n",
2488
              nodejP);
2489 2490
        listP->head = nodejP;
        listP->next[prev_j] = nodeiP;
2491
      } else if (nodejP == listP->head) {
2492
        LOG_D(MAC, "changing head to %d\n",
2493
              nodeiP);
2494 2495
        listP->head = nodeiP;
        listP->next[prev_i] = nodejP;
2496
      } else {
2497 2498
        listP->next[prev_i] = nodejP;
        listP->next[prev_j] = nodeiP;
2499 2500
      }
    }
2501 2502
  } else {      // ul_flag
    if (next_i == nodejP) { // case ... p(i) i j n(j) ... => ... p(j) j i n(i) ...
2503
      LOG_T(MAC, "[UL] Case ... p(i) i j n(j) ... => ... p(j) j i n(i) ...\n");
2504 2505
      listP->next_ul[nodeiP] = next_j;
      listP->next_ul[nodejP] = nodeiP;
2506

2507 2508
      if (nodeiP == listP->head_ul) { // case i j n(j)
        listP->head_ul = nodejP;
2509
      } else {
2510
        listP->next_ul[prev_i] = nodejP;
2511
      }
2512
    } else if (next_j == nodeiP) {  // case ... p(j) j i n(i) ... => ... p(i) i j n(j) ...
2513
      LOG_T(MAC, "[UL]Case ... p(j) j i n(i) ... => ... p(i) i j n(j) ...\n");
2514 2515 2516
      listP->next_ul[nodejP] = next_i;
      listP->next_ul[nodeiP] = nodejP;

2517 2518
      if (nodejP == listP->head_ul) { // case j i n(i)
        listP->head_ul = nodeiP;
2519
      } else {
2520
        listP->next_ul[prev_j] = nodeiP;
2521
      }
2522
    } else {    // case ...  p(i) i n(i) ... p(j) j n(j) ...
2523 2524 2525 2526
      listP->next_ul[nodejP] = next_i;
      listP->next_ul[nodeiP] = next_j;

      if (nodeiP == listP->head_ul) {
2527
        LOG_T(MAC, "[UL]changing head to %d\n",
2528
              nodejP);
2529 2530
        listP->head_ul = nodejP;
        listP->next_ul[prev_j] = nodeiP;
2531
      } else if (nodejP == listP->head_ul) {
2532
        LOG_T(MAC, "[UL]changing head to %d\n",
2533
              nodeiP);
2534 2535
        listP->head_ul = nodeiP;
        listP->next_ul[prev_i] = nodejP;
2536
      } else {
2537 2538
        listP->next_ul[prev_i] = nodejP;
        listP->next_ul[prev_j] = nodeiP;
2539 2540
      }
    }
2541
  }
2542 2543

  LOG_T(MAC, "After swap\n");
2544
  dump_ue_list(listP,
2545 2546
               ul_flag);
  return;
2547
}
2548 2549

// This has to be updated to include BSR information
2550
//------------------------------------------------------------------------------
2551
uint8_t
2552 2553 2554
UE_is_to_be_scheduled(module_id_t module_idP,
                      int CC_id,
                      uint8_t UE_id)
2555
//------------------------------------------------------------------------------
2556
{
2557
  UE_TEMPLATE *UE_template = &RC.mac[module_idP]->UE_list.UE_template[CC_id][UE_id];
2558
  UE_sched_ctrl_t *UE_sched_ctl = &RC.mac[module_idP]->UE_list.UE_sched_ctrl[UE_id];
2559
  int rrc_status;
2560 2561

  // do not schedule UE if UL is not working
2562 2563
  if (UE_sched_ctl->ul_failure_timer > 0 || UE_sched_ctl->ul_out_of_sync > 0)
    return 0;
2564

2565 2566
  rnti_t ue_rnti = UE_RNTI(module_idP, UE_id);

2567
  LOG_D(MAC, "[eNB %d][PUSCH] Checking UL requirements UE %d/%x\n",
2568 2569
        module_idP,
        UE_id,
2570 2571
        ue_rnti);

2572 2573
  rrc_status = mac_eNB_get_rrc_status(module_idP, ue_rnti);

2574 2575
  if (UE_template->scheduled_ul_bytes < UE_template->estimated_ul_buffer ||
      UE_template->ul_SR > 0 || // uplink scheduling request
2576
      (UE_sched_ctl->ul_inactivity_timer > 19 && UE_sched_ctl->ul_scheduled == 0) ||  // every 2 frames when RRC_CONNECTED
2577
      (UE_sched_ctl->ul_inactivity_timer > 10 &&
2578 2579
       UE_sched_ctl->ul_scheduled == 0 && rrc_status < RRC_CONNECTED) || // every Frame when not RRC_CONNECTED
      (UE_sched_ctl->cqi_req_timer > 300 && rrc_status >= RRC_CONNECTED)) { // cqi req timer expired long ago (do not put too low value)
2580
    LOG_D(MAC, "[eNB %d][PUSCH] UE %d/%x should be scheduled (BSR0 estimated size %d, SR %d)\n",
2581 2582
          module_idP,
          UE_id,
2583
          ue_rnti,
2584 2585 2586
          UE_template->ul_buffer_info[LCGID0],
          UE_template->ul_SR);

2587
    return 1;
2588
  }
frtabu's avatar
frtabu committed
2589

2590
  return 0;
2591 2592
}

2593
//------------------------------------------------------------------------------
2594 2595 2596 2597
uint8_t
get_tmode(module_id_t module_idP,
          int CC_idP,
          int UE_idP)
2598
//------------------------------------------------------------------------------
Cedric Roux's avatar
Cedric Roux committed
2599
{
2600 2601
  eNB_MAC_INST *eNB = RC.mac[module_idP];
  COMMON_channels_t *cc = &eNB->common_channels[CC_idP];
2602
  struct LTE_PhysicalConfigDedicated *physicalConfigDedicated = eNB->UE_list.UE_template[CC_idP][UE_idP].physicalConfigDedicated;
2603

2604
  if (physicalConfigDedicated == NULL) {  // RRCConnectionSetup not received by UE yet
2605
    AssertFatal(cc->p_eNB <= 2, "p_eNB is %d, should be <2\n",
Stefan's avatar
Stefan committed
2606
                cc->p_eNB);
2607
    return (cc->p_eNB);
2608
  }
2609 2610

  AssertFatal(physicalConfigDedicated->antennaInfo != NULL,
2611
              "antennaInfo (mod_id %d) is null for CCId %d, UEid %d, physicalConfigDedicated %p\n",
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
2612
              module_idP,
Stefan's avatar
Stefan committed
2613
              CC_idP,
frtabu's avatar
frtabu committed
2614
              UE_idP,
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
2615
              physicalConfigDedicated);
2616
  AssertFatal(physicalConfigDedicated->antennaInfo->present != LTE_PhysicalConfigDedicated__antennaInfo_PR_NOTHING,
Stefan's avatar
Stefan committed
2617 2618 2619
              "antennaInfo (mod_id %d, CC_id %d) is set to NOTHING\n",
              module_idP,
              CC_idP);
2620

2621
  if (physicalConfigDedicated->antennaInfo->present == LTE_PhysicalConfigDedicated__antennaInfo_PR_explicitValue) {
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
2622
    return (1 + physicalConfigDedicated->antennaInfo->choice.explicitValue.transmissionMode);
2623
  }
2624

2625
  if (physicalConfigDedicated->antennaInfo->present == LTE_PhysicalConfigDedicated__antennaInfo_PR_defaultValue) {
2626
    AssertFatal(cc->p_eNB <= 2, "p_eNB is %d, should be <2\n",
Stefan's avatar
Stefan committed
2627
                cc->p_eNB);
2628 2629
    return (cc->p_eNB);
  }
2630 2631 2632

  AssertFatal(1 == 0, "Shouldn't be here\n");
  return 0;
2633 2634
}

2635
//------------------------------------------------------------------------------
2636
int8_t
2637 2638
get_ULharq(module_id_t module_idP,
           int CC_idP,
2639
           uint16_t frameP,
2640
           uint8_t subframeP)
2641
//------------------------------------------------------------------------------
Cedric Roux's avatar
Cedric Roux committed
2642
{
2643
  int8_t ret = -1;
2644 2645
  eNB_MAC_INST *eNB = RC.mac[module_idP];
  COMMON_channels_t *cc = &eNB->common_channels[CC_idP];
2646

2647
  if (cc->tdd_Config == NULL) { // FDD
2648 2649 2650
    ret = (((frameP << 1) + subframeP) & 7);
  } else {
    switch (cc->tdd_Config->subframeAssignment) {
2651
      case 1:
2652 2653 2654 2655 2656
        switch (subframeP) {
          case 2:
          case 3:
            ret = (subframeP - 2);
            break;
2657

2658 2659 2660 2661
          case 7:
          case 8:
            ret = (subframeP - 5);
            break;
2662

2663 2664 2665 2666 2667 2668
          default:
            AssertFatal(1 == 0, "subframe2_harq_pid, Illegal subframe %d for TDD mode %d\n",
                        subframeP,
                        (int) cc->tdd_Config->subframeAssignment);
            break;
        }
2669

2670
        break;
2671

2672
      case 2:
2673
        AssertFatal((subframeP == 2) || (subframeP == 7), "subframe2_harq_pid, Illegal subframe %d for TDD mode %d\n",
2674 2675 2676 2677
                    subframeP,
                    (int) cc->tdd_Config->subframeAssignment);
        ret = (subframeP / 7);
        break;
2678

2679
      case 3:
2680
        AssertFatal((subframeP > 1) && (subframeP < 5), "subframe2_harq_pid, Illegal subframe %d for TDD mode %d\n",
2681 2682 2683 2684
                    subframeP,
                    (int) cc->tdd_Config->subframeAssignment);
        ret = (subframeP - 2);
        break;
2685

2686
      case 4:
2687
        AssertFatal((subframeP > 1) && (subframeP < 4), "subframe2_harq_pid, Illegal subframe %d for TDD mode %d\n",
2688 2689 2690 2691 2692 2693
                    subframeP,
                    (int) cc->tdd_Config->subframeAssignment);
        ret = (subframeP - 2);
        break;

      case 5:
2694
        AssertFatal(subframeP == 2, "subframe2_harq_pid, Illegal subframe %d for TDD mode %d\n",
2695 2696 2697 2698 2699 2700
                    subframeP,
                    (int) cc->tdd_Config->subframeAssignment);
        ret = (subframeP - 2);
        break;

      default:
2701
        AssertFatal(1 == 0, "subframe2_harq_pid, Unsupported TDD mode %d\n",
2702 2703
                    (int) cc->tdd_Config->subframeAssignment);
        break;
2704
    }
2705
  }
2706

2707
  AssertFatal(ret != -1, "invalid harq_pid(%d) at SFN/SF = %d/%d\n", (int8_t) ret,
2708
              frameP,
2709
              subframeP);
2710
  return ret;
2711 2712
}

2713
//------------------------------------------------------------------------------
2714 2715 2716 2717
uint16_t
getRIV(uint16_t N_RB_DL,
       uint16_t RBstart,
       uint16_t Lcrbs)
2718
//------------------------------------------------------------------------------
2719
{
2720
  uint16_t RIV;
2721

2722 2723 2724 2725
  if (Lcrbs <= (1 + (N_RB_DL >> 1)))
    RIV = (N_RB_DL * (Lcrbs - 1)) + RBstart;
  else
    RIV = (N_RB_DL * (N_RB_DL + 1 - Lcrbs)) + (N_RB_DL - 1 - RBstart);
2726

2727
  return RIV;
2728
}
2729

2730
//------------------------------------------------------------------------------
2731
uint32_t
2732 2733
allocate_prbs(int UE_id,
              unsigned char nb_rb,
2734
              int N_RB_DL,
2735
              uint32_t *rballoc)
2736
//------------------------------------------------------------------------------
2737
{
2738 2739 2740
  int i;
  uint32_t rballoc_dci = 0;
  unsigned char nb_rb_alloc = 0;
2741

2742 2743 2744 2745 2746
  for (i = 0; i < (N_RB_DL - 2); i += 2) {
    if (((*rballoc >> i) & 3) == 0) {
      *rballoc |= (3 << i);
      rballoc_dci |= (1 << ((12 - i) >> 1));
      nb_rb_alloc += 2;
2747 2748
    }

2749 2750 2751 2752 2753 2754 2755 2756 2757
    if (nb_rb_alloc == nb_rb) {
      return (rballoc_dci);
    }
  }

  if ((N_RB_DL & 1) == 1) {
    if ((*rballoc >> (N_RB_DL - 1) & 1) == 0) {
      *rballoc |= (1 << (N_RB_DL - 1));
      rballoc_dci |= 1;
2758
    }
2759
  }
2760

2761
  return (rballoc_dci);
2762 2763
}

2764
//------------------------------------------------------------------------------
Stefan's avatar
Stefan committed
2765 2766
int
get_bw_index(module_id_t module_id,
2767
             uint8_t CC_id)
2768
//------------------------------------------------------------------------------
2769
{
2770
  int bw_index = 0;
2771
  int N_RB_DL = to_prb(RC.mac[module_id]->common_channels[CC_id].mib->message.dl_Bandwidth);
2772

2773
  switch (N_RB_DL) {
2774 2775 2776
    case 6:     // 1.4 MHz
      bw_index = 0;
      break;
2777

2778 2779 2780
    case 25:      // 5HMz
      bw_index = 1;
      break;
2781

2782 2783 2784
    case 50:      // 10HMz
      bw_index = 2;
      break;
2785

2786 2787 2788
    case 100:     // 20HMz
      bw_index = 3;
      break;
2789

2790 2791
    default:
      bw_index = 1;
Stefan's avatar
Stefan committed
2792 2793 2794 2795
      LOG_W(MAC, "[eNB %d] N_RB_DL %d unknown for CC_id %d, setting bw_index to 1\n",
            module_id,
            N_RB_DL,
            CC_id);
2796
      break;
2797
  }
2798

2799
  return bw_index;
2800 2801
}

2802
//------------------------------------------------------------------------------
Stefan's avatar
Stefan committed
2803 2804
int
get_min_rb_unit(module_id_t module_id,
2805
                uint8_t CC_id)
2806
//------------------------------------------------------------------------------
2807
{
2808
  int min_rb_unit = 0;
2809
  int N_RB_DL = to_prb(RC.mac[module_id]->common_channels[CC_id].mib->message.dl_Bandwidth);
2810

2811
  switch (N_RB_DL) {
2812 2813 2814
    case 6:     // 1.4 MHz
      min_rb_unit = 1;
      break;
2815

2816 2817 2818
    case 25:      // 5HMz
      min_rb_unit = 2;
      break;
2819

2820 2821 2822
    case 50:      // 10HMz
      min_rb_unit = 3;
      break;
2823

2824 2825 2826
    case 100:     // 20HMz
      min_rb_unit = 4;
      break;
2827

2828 2829
    default:
      min_rb_unit = 2;
Stefan's avatar
Stefan committed
2830 2831 2832 2833
      LOG_W(MAC, "[eNB %d] N_DL_RB %d unknown for CC_id %d, setting min_rb_unit to 2\n",
            module_id,
            N_RB_DL,
            CC_id);
2834
      break;
2835
  }
2836

2837
  return min_rb_unit;
2838
}
2839

2840
//------------------------------------------------------------------------------
2841
uint32_t
2842 2843 2844 2845
allocate_prbs_sub(int nb_rb,
                  int N_RB_DL,
                  int N_RBG,
                  uint8_t *rballoc)
2846
//------------------------------------------------------------------------------
2847
{
2848
  int check = 0;    //check1=0,check2=0;
2849 2850 2851
  uint32_t rballoc_dci = 0;
  //uint8_t number_of_subbands=13;
  LOG_T(MAC, "*****Check1RBALLOC****: %d%d%d%d (nb_rb %d,N_RBG %d)\n",
2852 2853 2854 2855 2856
        rballoc[3],
        rballoc[2],
        rballoc[1],
        rballoc[0],
        nb_rb,
2857 2858 2859
        N_RBG);

  while (nb_rb > 0 && check < N_RBG) {
2860 2861 2862 2863 2864
    //printf("rballoc[%d] %d\n",check,rballoc[check]);
    if (rballoc[check] == 1) {
      rballoc_dci |= (1 << ((N_RBG - 1) - check));

      switch (N_RB_DL) {
2865 2866 2867
        case 6:
          nb_rb--;
          break;
2868

2869 2870 2871 2872 2873 2874
        case 25:
          if ((check == N_RBG - 1)) {
            nb_rb--;
          } else {
            nb_rb -= 2;
          }
frtabu's avatar
frtabu committed
2875

2876
          break;
2877

2878 2879 2880 2881 2882 2883
        case 50:
          if ((check == N_RBG - 1)) {
            nb_rb -= 2;
          } else {
            nb_rb -= 3;
          }
frtabu's avatar
frtabu committed
2884

2885
          break;
2886

2887 2888 2889
        case 100:
          nb_rb -= 4;
          break;
2890
      }
2891
    }
2892

2893
    //      printf("rb_alloc %x\n",rballoc_dci);
2894
    check++;
2895 2896
    //    check1 = check1+2;
  }
2897

2898
  // rballoc_dci = (rballoc_dci)&(0x1fff);
2899
  LOG_T(MAC, "*********RBALLOC : %x\n",
2900
        rballoc_dci);
2901
  // exit(-1);
2902
  return rballoc_dci;
2903 2904
}

2905
//------------------------------------------------------------------------------
2906 2907
int
get_subbandsize(uint8_t dl_Bandwidth)
2908
//------------------------------------------------------------------------------
Cedric Roux's avatar
Cedric Roux committed
2909
{
2910 2911
  uint8_t ss[6] = { 6, 4, 4, 6, 8, 8 };
  AssertFatal(dl_Bandwidth < 6, "dl_Bandwidth %d is out of bounds\n",
2912
              dl_Bandwidth);
2913
  return (ss[dl_Bandwidth]);
2914
}
Cedric Roux's avatar
Cedric Roux committed
2915

2916
//------------------------------------------------------------------------------
2917 2918
int
get_nb_subband(int N_RB_DL)
2919
//------------------------------------------------------------------------------
2920
{
2921
  int nb_sb = 0;
2922

2923
  switch (N_RB_DL) {
2924 2925 2926
    case 6:
      nb_sb = 0;
      break;
2927

2928 2929
    case 15:
      nb_sb = 4;    // sb_size =4
2930
      break;
2931

2932 2933 2934
    case 25:
      nb_sb = 7;    // sb_size =4, 1 sb with 1PRB, 6 with 2 RBG, each has 2 PRBs
      break;
2935

2936 2937 2938
    case 50:      // sb_size =6
      nb_sb = 9;
      break;
2939

2940 2941 2942
    case 75:      // sb_size =8
      nb_sb = 10;
      break;
2943

2944 2945 2946
    case 100:     // sb_size =8 , 1 sb with 1 RBG + 12 sb with 2RBG, each RBG has 4 PRBs
      nb_sb = 13;
      break;
2947

2948 2949 2950
    default:
      nb_sb = 0;
      break;
2951
  }
2952

2953
  return nb_sb;
2954
}
2955 2956

//------------------------------------------------------------------------------
2957 2958
void
init_CCE_table(int *CCE_table)
2959
//------------------------------------------------------------------------------
2960
{
Stefan's avatar
Stefan committed
2961
  memset(CCE_table, 0, 800 * sizeof(int));
Cedric Roux's avatar
Cedric Roux committed
2962
}
2963

2964
//------------------------------------------------------------------------------
2965 2966
int
get_nCCE_offset(int *CCE_table,
2967 2968 2969
                const unsigned char L,
                const int nCCE,
                const int common_dci,
2970 2971
                const unsigned short rnti,
                const unsigned char subframe)
2972
//------------------------------------------------------------------------------
2973
{
2974 2975
  int search_space_free, m, nb_candidates = 0, l, i;
  unsigned int Yk;
2976

2977 2978 2979 2980 2981 2982 2983 2984 2985 2986
  /*
    printf("CCE Allocation: ");
    for (i=0;i<nCCE;i++)
    printf("%d.",CCE_table[i]);
    printf("\n");
  */
  if (common_dci == 1) {
    // check CCE(0 ... L-1)
    nb_candidates = (L == 4) ? 4 : 2;
    nb_candidates = min(nb_candidates, nCCE / L);
2987

2988
    //    printf("Common DCI nb_candidates %d, L %d\n",nb_candidates,L);
Raymond Knopp's avatar
Raymond Knopp committed
2989

2990 2991
    for (m = nb_candidates - 1; m >= 0; m--) {
      search_space_free = 1;
Raymond Knopp's avatar
Raymond Knopp committed
2992

2993 2994 2995 2996 2997 2998
      for (l = 0; l < L; l++) {
        //        printf("CCE_table[%d] %d\n",(m*L)+l,CCE_table[(m*L)+l]);
        if (CCE_table[(m * L) + l] == 1) {
          search_space_free = 0;
          break;
        }
2999
      }
3000

3001
      if (search_space_free == 1) {
3002
        //        printf("returning %d\n",m*L);
3003
        for (l = 0; l < L; l++) {
3004
          CCE_table[(m * L) + l] = 1;
3005
        }
frtabu's avatar
frtabu committed
3006

3007
        return (m * L);
3008 3009
      }
    }
3010

3011
    return -1;
3012
  }
frtabu's avatar
frtabu committed
3013

3014 3015 3016 3017
  // Find first available in ue specific search space
  // according to procedure in Section 9.1.1 of 36.213 (v. 8.6)
  // compute Yk
  Yk = (unsigned int) rnti;
3018

3019 3020 3021
  for (i = 0; i <= subframe; i++) {
    Yk = (Yk * 39827) % 65537;
  }
frtabu's avatar
frtabu committed
3022

3023
  Yk = Yk % (nCCE / L);
3024

3025
  switch (L) {
3026 3027 3028 3029
    case 1:
    case 2:
      nb_candidates = 6;
      break;
3030

3031 3032 3033 3034
    case 4:
    case 8:
      nb_candidates = 2;
      break;
3035

3036
    default:
3037 3038
      DevParam(L,
               nCCE,
3039
               rnti);
3040
      break;
3041
  }
3042

3043
  LOG_D(MAC, "rnti %x, Yk = %d, nCCE %d (nCCE/L %d),nb_cand %d\n",
3044 3045 3046 3047
        rnti,
        Yk,
        nCCE,
        nCCE / L,
3048
        nb_candidates);
3049

3050 3051
  for (m = 0; m < nb_candidates; m++) {
    search_space_free = 1;
3052

3053 3054
    for (l = 0; l < L; l++) {
      int cce = (((Yk + m) % (nCCE / L)) * L) + l;
3055

3056 3057 3058
      if (cce >= nCCE || CCE_table[cce] == 1) {
        search_space_free = 0;
        break;
3059
      }
3060
    }
3061

3062 3063 3064
    if (search_space_free == 1) {
      for (l = 0; l < L; l++) {
        CCE_table[(((Yk + m) % (nCCE / L)) * L) + l] = 1;
3065
      }
frtabu's avatar
frtabu committed
3066

3067
      return (((Yk + m) % (nCCE / L)) * L);
3068
    }
3069
  }
3070 3071

  return -1;
3072 3073
}

3074
//------------------------------------------------------------------------------
3075
void
3076
dump_CCE_table(int *CCE_table,
3077
               const int nCCE,
3078 3079 3080
               const unsigned short rnti,
               const int subframe,
               int L)
3081
//------------------------------------------------------------------------------
Cedric Roux's avatar
Cedric Roux committed
3082
{
3083 3084 3085
  int nb_candidates = 0, i;
  unsigned int Yk;
  printf("CCE 0: ");
3086

3087 3088
  for (i = 0; i < nCCE; i++) {
    printf("%1d.", CCE_table[i]);
3089

3090
    if ((i & 7) == 7)
3091
      printf("\n CCE %d: ",
3092
             i);
3093
  }
Cedric Roux's avatar
Cedric Roux committed
3094

3095
  Yk = (unsigned int) rnti;
Cedric Roux's avatar
Cedric Roux committed
3096

3097
  for (i = 0; i <= subframe; i++) {
3098
    Yk = (Yk * 39827) % 65537;
3099
  }
frtabu's avatar
frtabu committed
3100

3101
  Yk = Yk % (nCCE / L);
Cedric Roux's avatar
Cedric Roux committed
3102

3103
  switch (L) {
3104 3105 3106 3107 3108 3109 3110 3111 3112
    case 1:
    case 2:
      nb_candidates = 6;
      break;

    case 4:
    case 8:
      nb_candidates = 2;
      break;
Cedric Roux's avatar
Cedric Roux committed
3113

3114 3115 3116
    default:
      DevParam(L, nCCE, rnti);
      break;
3117
  }
3118

3119
  LOG_I(PHY, "rnti %x, Yk*L = %u, nCCE %d (nCCE/L %d),nb_cand*L %d\n",
frtabu's avatar
frtabu committed
3120 3121 3122 3123 3124
        rnti,
        Yk * L,
        nCCE,
        nCCE / L,
        nb_candidates * L);
3125 3126
}

3127
//------------------------------------------------------------------------------
3128
uint16_t
3129 3130 3131
getnquad(COMMON_channels_t *cc,
         uint8_t num_pdcch_symbols,
         uint8_t mi)
3132
//------------------------------------------------------------------------------
3133
{
3134 3135 3136 3137 3138 3139
  uint16_t Nreg = 0;
  AssertFatal(cc != NULL, "cc is null\n");
  AssertFatal(cc->mib != NULL, "cc->mib is null\n");
  int N_RB_DL = to_prb(cc->mib->message.dl_Bandwidth);
  int phich_resource = get_phich_resource_times6(cc);
  uint8_t Ngroup_PHICH = (phich_resource * N_RB_DL) / 48;
3140

3141
  if (((phich_resource * N_RB_DL) % 48) > 0) {
3142
    Ngroup_PHICH++;
3143
  }
frtabu's avatar
frtabu committed
3144

3145 3146 3147
  if (cc->Ncp == 1) {
    Ngroup_PHICH <<= 1;
  }
3148

3149
  Ngroup_PHICH *= mi;
3150

3151
  if (num_pdcch_symbols > 0 && num_pdcch_symbols < 4)
3152
    switch (N_RB_DL) {
3153 3154 3155
      case 6:
        Nreg = 12 + (num_pdcch_symbols - 1) * 18;
        break;
3156

3157 3158 3159
      case 25:
        Nreg = 50 + (num_pdcch_symbols - 1) * 75;
        break;
3160

3161 3162 3163
      case 50:
        Nreg = 100 + (num_pdcch_symbols - 1) * 150;
        break;
3164

3165 3166 3167
      case 100:
        Nreg = 200 + (num_pdcch_symbols - 1) * 300;
        break;
3168

3169
      default:
3170
        return 0;
3171
    }
3172

3173 3174
  //   printf("Nreg %d (%d)\n",Nreg,Nreg - 4 - (3*Ngroup_PHICH));
  return (Nreg - 4 - (3 * Ngroup_PHICH));
3175 3176
}

3177
//------------------------------------------------------------------------------
3178
uint16_t
3179 3180 3181
getnCCE(COMMON_channels_t *cc,
        uint8_t num_pdcch_symbols,
        uint8_t mi)
3182
//------------------------------------------------------------------------------
3183
{
3184
  AssertFatal(cc != NULL, "cc is null\n");
3185 3186
  return (getnquad(cc,
                   num_pdcch_symbols,
3187
                   mi) / 9);
3188 3189
}

3190
//------------------------------------------------------------------------------
3191 3192 3193
uint8_t
getmi(COMMON_channels_t *cc,
      int subframe)
3194
//------------------------------------------------------------------------------
Cedric Roux's avatar
Cedric Roux committed
3195
{
3196
  AssertFatal(cc != NULL, "cc is null\n");
3197

3198
  // for FDD
3199
  if (cc->tdd_Config == NULL) // FDD
3200
    return 1;
3201

3202 3203
  // for TDD
  switch (cc->tdd_Config->subframeAssignment) {
3204
    case 0:
3205 3206 3207
      if (subframe == 0 || subframe == 5) {
        return 2;
      }
frtabu's avatar
frtabu committed
3208

3209
      return 1;
3210

3211
    case 1:
3212 3213 3214
      if (subframe == 0 || subframe == 5) {
        return 0;
      }
frtabu's avatar
frtabu committed
3215

3216
      return 1;
3217

3218
    case 2:
3219 3220 3221
      if (subframe == 3 || subframe == 8) {
        return 0;
      }
frtabu's avatar
frtabu committed
3222

3223
      return 1;
3224

3225
    case 3:
3226 3227 3228
      if (subframe == 0 || subframe == 8 || subframe == 9) {
        return 1;
      }
frtabu's avatar
frtabu committed
3229

3230
      return 0;
3231

3232
    case 4:
3233 3234 3235
      if (subframe == 8 || subframe == 9) {
        return 1;
      }
frtabu's avatar
frtabu committed
3236

3237
      return 0;
3238

3239
    case 5:
3240 3241 3242
      if (subframe == 8) {
        return 1;
      }
frtabu's avatar
frtabu committed
3243

3244
      return 0;
3245

3246
    case 6:
3247
      return 1;
3248

3249
    default:
3250
      break;
3251
  }
frtabu's avatar
frtabu committed
3252

3253
  return 0;
3254 3255
}

3256
//------------------------------------------------------------------------------
3257
uint16_t
3258 3259 3260
get_nCCE_max(COMMON_channels_t *cc,
             int num_pdcch_symbols,
             int subframe)
3261
//------------------------------------------------------------------------------
3262
{
3263
  AssertFatal(cc != NULL, "cc is null\n");
3264 3265 3266
  return (getnCCE(cc,
                  num_pdcch_symbols,
                  getmi(cc,
3267
                        subframe)));
3268
}
Cedric Roux's avatar
Cedric Roux committed
3269

3270
// Allocate the CCEs
3271
//------------------------------------------------------------------------------
3272
int
3273 3274 3275 3276 3277
allocate_CCEs(int module_idP,
              int CC_idP,
              frame_t frameP,
              sub_frame_t subframeP,
              int test_onlyP)
3278
//------------------------------------------------------------------------------
3279
{
Stefan's avatar
Stefan committed
3280 3281 3282 3283
  eNB_MAC_INST *eNB = RC.mac[module_idP];
  int *CCE_table = eNB->CCE_table[CC_idP];
  nfapi_dl_config_request_body_t *DL_req = &eNB->DL_req[CC_idP].dl_config_request_body;
  nfapi_hi_dci0_request_body_t *HI_DCI0_req = &eNB->HI_DCI0_req[CC_idP][subframeP].hi_dci0_request_body;
3284 3285
  nfapi_dl_config_request_pdu_t *dl_config_pdu = &DL_req->dl_config_pdu_list[0];
  nfapi_hi_dci0_request_pdu_t *hi_dci0_pdu = &HI_DCI0_req->hi_dci0_pdu_list[0];
Stefan's avatar
Stefan committed
3286 3287
  COMMON_channels_t *cc = &eNB->common_channels[CC_idP];
  int nCCE_max = get_nCCE_max(cc, 1, subframeP);
3288 3289 3290
  int fCCE;
  int i, j, idci;
  int nCCE = 0;
3291 3292
  int max_symbol;
  int ackNAK_absSF = get_pucch1_absSF(cc, (frameP*10+subframeP));
Stefan's avatar
Stefan committed
3293 3294
  nfapi_dl_config_request_pdu_t *dl_config_pduLoop;
  nfapi_hi_dci0_request_pdu_t *hi_dci0_pduLoop;
3295

3296 3297 3298 3299 3300 3301
  if (cc->tdd_Config!=NULL && is_S_sf(cc,subframeP) > 0)
    max_symbol = 2;
  else
    max_symbol = 3;

  nfapi_ul_config_request_body_t *ul_req = &eNB->UL_req_tmp[CC_idP][ackNAK_absSF % 10].ul_config_request_body;
3302
  LOG_D(MAC, "Allocate CCEs subframe %d, test %d : (DL PDU %d, DL DCI %d, UL %d)\n",
3303 3304 3305
        subframeP,
        test_onlyP,
        DL_req->number_pdu,
Stefan's avatar
Stefan committed
3306
        DL_req->number_dci,
3307
        HI_DCI0_req->number_of_dci);
3308
  DL_req->number_pdcch_ofdm_symbols = 1;
3309
try_again:
Stefan's avatar
Stefan committed
3310
  init_CCE_table(CCE_table);
3311 3312 3313
  nCCE = 0;

  for (i = 0, idci = 0; i < DL_req->number_pdu; i++) {
Stefan's avatar
Stefan committed
3314
    dl_config_pduLoop = &dl_config_pdu[i];
frtabu's avatar
frtabu committed
3315

3316
    // allocate DL common DCIs first
3317
    if (dl_config_pduLoop->pdu_type == NFAPI_DL_CONFIG_DCI_DL_PDU_TYPE &&
Stefan's avatar
Stefan committed
3318
        dl_config_pduLoop->dci_dl_pdu.dci_dl_pdu_rel8.rnti_type == 2) {
3319
      LOG_D(MAC, "Trying to allocate COMMON DCI %d/%d (%d,%d) : rnti %x, aggreg %d nCCE %d / %d (num_pdcch_symbols %d)\n",
3320
            idci,
Stefan's avatar
Stefan committed
3321
            DL_req->number_dci + HI_DCI0_req->number_of_dci,
3322
            DL_req->number_dci,
Stefan's avatar
Stefan committed
3323 3324
            HI_DCI0_req->number_of_dci,
            dl_config_pduLoop->dci_dl_pdu.dci_dl_pdu_rel8.rnti,
3325 3326 3327
            dl_config_pduLoop->dci_dl_pdu.dci_dl_pdu_rel8.aggregation_level,
            nCCE,
            nCCE_max,
Stefan's avatar
Stefan committed
3328
            DL_req->number_pdcch_ofdm_symbols);
3329

Stefan's avatar
Stefan committed
3330
      if (nCCE + (dl_config_pduLoop->dci_dl_pdu.dci_dl_pdu_rel8.aggregation_level) > nCCE_max) {
3331
        if (DL_req->number_pdcch_ofdm_symbols == max_symbol)
3332
          return -1;
3333 3334 3335 3336

        LOG_D(MAC, "Can't fit DCI allocations with %d PDCCH symbols, increasing by 1\n",
              DL_req->number_pdcch_ofdm_symbols);
        DL_req->number_pdcch_ofdm_symbols++;
Stefan's avatar
Stefan committed
3337
        nCCE_max = get_nCCE_max(cc, DL_req->number_pdcch_ofdm_symbols, subframeP);
3338
        goto try_again;
3339
      }
3340

3341 3342
      // number of CCEs left can potentially hold this allocation
      fCCE = get_nCCE_offset(CCE_table,
3343 3344
                             dl_config_pduLoop->dci_dl_pdu.dci_dl_pdu_rel8.aggregation_level,
                             nCCE_max,
Stefan's avatar
Stefan committed
3345 3346
                             1,
                             dl_config_pduLoop->dci_dl_pdu.dci_dl_pdu_rel8.rnti,
3347 3348
                             subframeP);

3349
      if (fCCE == -1) {
3350 3351
        if (DL_req->number_pdcch_ofdm_symbols == max_symbol) {
          LOG_D(MAC, "subframe %d: Dropping Allocation for RNTI %x\n",
3352
                subframeP,
Stefan's avatar
Stefan committed
3353
                dl_config_pduLoop->dci_dl_pdu.dci_dl_pdu_rel8.rnti);
3354 3355

          for (j = 0; j <= i; j++) {
Stefan's avatar
Stefan committed
3356
            dl_config_pduLoop = &dl_config_pdu[j];
frtabu's avatar
frtabu committed
3357

Stefan's avatar
Stefan committed
3358
            if (dl_config_pduLoop->pdu_type == NFAPI_DL_CONFIG_DCI_DL_PDU_TYPE)
3359 3360 3361 3362 3363
              LOG_D(MAC, "DCI %d/%d (%d,%d) : rnti %x dci format %d, aggreg %d nCCE %d / %d (num_pdcch_symbols %d)\n",
                    j,
                    DL_req->number_dci + HI_DCI0_req->number_of_dci,
                    DL_req->number_dci,
                    HI_DCI0_req->number_of_dci,
Stefan's avatar
Stefan committed
3364 3365
                    dl_config_pduLoop->dci_dl_pdu.dci_dl_pdu_rel8.rnti,
                    dl_config_pduLoop->dci_dl_pdu.dci_dl_pdu_rel8.dci_format,
3366
                    dl_config_pduLoop->dci_dl_pdu.dci_dl_pdu_rel8.aggregation_level,
3367 3368
                    nCCE, nCCE_max, DL_req->number_pdcch_ofdm_symbols);
          }
frtabu's avatar
frtabu committed
3369

3370
          //dump_CCE_table(CCE_table,nCCE_max,subframeP,dci_alloc->rnti,1<<dci_alloc->L);
3371
          return -1;
3372 3373
        }

Stefan's avatar
Stefan committed
3374
        LOG_D(MAC, "Can't fit DCI allocations with %d PDCCH symbols (rnti condition), increasing by 1\n",
3375 3376
              DL_req->number_pdcch_ofdm_symbols);
        DL_req->number_pdcch_ofdm_symbols++;
Stefan's avatar
Stefan committed
3377
        nCCE_max = get_nCCE_max(cc, DL_req->number_pdcch_ofdm_symbols, subframeP);
3378 3379
        goto try_again;
      }     // fCCE==-1
3380 3381

      // the allocation is feasible, rnti rule passes
Stefan's avatar
Stefan committed
3382
      nCCE += dl_config_pduLoop->dci_dl_pdu.dci_dl_pdu_rel8.aggregation_level;
3383
      LOG_D(MAC, "Allocating at nCCE %d\n", fCCE);
3384

3385
      if ((test_onlyP%2) == 0) {
Stefan's avatar
Stefan committed
3386
        dl_config_pduLoop->dci_dl_pdu.dci_dl_pdu_rel8.cce_idx = fCCE;
3387
        LOG_D(MAC, "Allocate COMMON DCI CCEs subframe %d, test %d => L %d fCCE %d\n",
3388 3389 3390
              subframeP,
              test_onlyP,
              dl_config_pduLoop->dci_dl_pdu.dci_dl_pdu_rel8.aggregation_level,
Stefan's avatar
Stefan committed
3391
              fCCE);
3392
      }
3393

3394 3395
      idci++;
    }
3396
  }       // for i = 0 ... num_DL_DCIs
3397

3398
  // now try to allocate UL DCIs
3399
  for (i = 0; i < HI_DCI0_req->number_of_dci + HI_DCI0_req->number_of_hi; i++) {
Stefan's avatar
Stefan committed
3400
    hi_dci0_pduLoop = &hi_dci0_pdu[i];
frtabu's avatar
frtabu committed
3401

3402 3403
    // allocate UL DCIs
    if (hi_dci0_pdu[i].pdu_type == NFAPI_HI_DCI0_DCI_PDU_TYPE) {
3404
      LOG_D(MAC, "Trying to allocate format 0 DCI %d/%d (%d,%d) : rnti %x, aggreg %d nCCE %d / %d (num_pdcch_symbols %d)\n",
3405
            idci,
Stefan's avatar
Stefan committed
3406
            DL_req->number_dci + HI_DCI0_req->number_of_dci,
3407
            DL_req->number_dci,
Stefan's avatar
Stefan committed
3408 3409 3410
            HI_DCI0_req->number_of_dci,
            hi_dci0_pduLoop->dci_pdu.dci_pdu_rel8.rnti,
            hi_dci0_pduLoop->dci_pdu.dci_pdu_rel8.aggregation_level,
3411
            nCCE, nCCE_max,
Stefan's avatar
Stefan committed
3412
            DL_req->number_pdcch_ofdm_symbols);
3413

Stefan's avatar
Stefan committed
3414
      if (nCCE + hi_dci0_pduLoop->dci_pdu.dci_pdu_rel8.aggregation_level > nCCE_max) {
3415
        if (DL_req->number_pdcch_ofdm_symbols == max_symbol)
3416
          return -1;
3417

3418
        LOG_D(MAC, "Can't fit DCI allocations with %d PDCCH symbols, increasing by 1\n",
3419 3420
              DL_req->number_pdcch_ofdm_symbols);
        DL_req->number_pdcch_ofdm_symbols++;
Stefan's avatar
Stefan committed
3421
        nCCE_max = get_nCCE_max(cc, DL_req->number_pdcch_ofdm_symbols, subframeP);
3422
        goto try_again;
3423
      }
3424

3425 3426
      // number of CCEs left can potentially hold this allocation
      fCCE = get_nCCE_offset(CCE_table,
Stefan's avatar
Stefan committed
3427
                             hi_dci0_pduLoop->dci_pdu.dci_pdu_rel8.aggregation_level,
3428
                             nCCE_max,
Stefan's avatar
Stefan committed
3429
                             0,
3430
                             hi_dci0_pduLoop->dci_pdu.dci_pdu_rel8.rnti,
Stefan's avatar
Stefan committed
3431
                             subframeP);
3432

3433
      if (fCCE == -1) {
3434
        if (DL_req->number_pdcch_ofdm_symbols == max_symbol) {
3435
          LOG_D(MAC, "subframe %d: Dropping Allocation for RNTI %x\n",
3436
                subframeP,
Stefan's avatar
Stefan committed
3437
                hi_dci0_pduLoop->dci_pdu.dci_pdu_rel8.rnti);
3438 3439

          for (j = 0; j <= i; j++) {
Stefan's avatar
Stefan committed
3440
            hi_dci0_pduLoop = &hi_dci0_pdu[j];
frtabu's avatar
frtabu committed
3441

3442
            if (hi_dci0_pdu[j].pdu_type == NFAPI_HI_DCI0_DCI_PDU_TYPE)
3443
              LOG_D(MAC, "DCI %d/%d (%d,%d) : rnti %x dci format %d, aggreg %d nCCE %d / %d (num_pdcch_symbols %d)\n",
3444 3445 3446 3447
                    j,
                    DL_req->number_dci + HI_DCI0_req->number_of_dci,
                    DL_req->number_dci,
                    HI_DCI0_req->number_of_dci,
Stefan's avatar
Stefan committed
3448 3449
                    hi_dci0_pduLoop->dci_pdu.dci_pdu_rel8.rnti,
                    hi_dci0_pduLoop->dci_pdu.dci_pdu_rel8.dci_format,
3450
                    hi_dci0_pduLoop->dci_pdu.dci_pdu_rel8.aggregation_level,
3451 3452
                    nCCE, nCCE_max, DL_req->number_pdcch_ofdm_symbols);
          }
frtabu's avatar
frtabu committed
3453

3454
          //dump_CCE_table(CCE_table,nCCE_max,subframeP,dci_alloc->rnti,1<<dci_alloc->L);
3455
          return -1;
3456 3457
        }

3458
        LOG_D(MAC, "Can't fit DCI allocations with %d PDCCH symbols (rnti condition), increasing by 1\n",
3459 3460
              DL_req->number_pdcch_ofdm_symbols);
        DL_req->number_pdcch_ofdm_symbols++;
Stefan's avatar
Stefan committed
3461
        nCCE_max = get_nCCE_max(cc, DL_req->number_pdcch_ofdm_symbols, subframeP);
3462 3463
        goto try_again;
      }     // fCCE==-1
3464 3465

      // the allocation is feasible, rnti rule passes
Stefan's avatar
Stefan committed
3466
      nCCE += hi_dci0_pduLoop->dci_pdu.dci_pdu_rel8.aggregation_level;
3467
      LOG_D(MAC, "Allocating at nCCE %d\n",
Stefan's avatar
Stefan committed
3468
            fCCE);
3469

3470
      if ((test_onlyP%2) == 0) {
Stefan's avatar
Stefan committed
3471
        hi_dci0_pduLoop->dci_pdu.dci_pdu_rel8.cce_index = fCCE;
3472 3473
        LOG_D(MAC, "Allocate CCEs subframe %d, test %d\n",
              subframeP,
Stefan's avatar
Stefan committed
3474
              test_onlyP);
3475
      }
3476

3477 3478
      idci++;
    }
3479
  }       // for i = 0 ... num_UL_DCIs
3480 3481

  for (i = 0; i < DL_req->number_pdu; i++) {
Stefan's avatar
Stefan committed
3482
    dl_config_pduLoop = &dl_config_pdu[i];
frtabu's avatar
frtabu committed
3483

3484 3485
    // allocate DL UE specific DCIs
    if ((dl_config_pdu[i].pdu_type == NFAPI_DL_CONFIG_DCI_DL_PDU_TYPE)
Stefan's avatar
Stefan committed
3486
        && (dl_config_pduLoop->dci_dl_pdu.dci_dl_pdu_rel8.rnti_type == 1)) {
3487
      LOG_D(MAC, "Trying to allocate DL UE-SPECIFIC DCI %d/%d (%d,%d) : rnti %x, aggreg %d nCCE %d / %d (num_pdcch_symbols %d)\n",
3488
            idci,
Stefan's avatar
Stefan committed
3489
            DL_req->number_dci + HI_DCI0_req->number_of_dci,
3490
            DL_req->number_dci, HI_DCI0_req->number_of_dci,
Stefan's avatar
Stefan committed
3491
            dl_config_pduLoop->dci_dl_pdu.dci_dl_pdu_rel8.rnti,
3492 3493
            dl_config_pduLoop->dci_dl_pdu.dci_dl_pdu_rel8.aggregation_level,
            nCCE,
Stefan's avatar
Stefan committed
3494
            nCCE_max,
3495
            DL_req->number_pdcch_ofdm_symbols);
3496

Stefan's avatar
Stefan committed
3497
      if (nCCE + (dl_config_pduLoop->dci_dl_pdu.dci_dl_pdu_rel8.aggregation_level) > nCCE_max) {
3498
        if (DL_req->number_pdcch_ofdm_symbols == max_symbol)
3499
          return -1;
3500

3501
        LOG_D(MAC, "Can't fit DCI allocations with %d PDCCH symbols, increasing by 1\n",
Stefan's avatar
Stefan committed
3502
              DL_req->number_pdcch_ofdm_symbols);
3503
        DL_req->number_pdcch_ofdm_symbols++;
Stefan's avatar
Stefan committed
3504
        nCCE_max = get_nCCE_max(cc, DL_req->number_pdcch_ofdm_symbols, subframeP);
3505
        goto try_again;
3506
      }
3507

3508 3509
      // number of CCEs left can potentially hold this allocation
      fCCE = get_nCCE_offset(CCE_table,
3510 3511
                             dl_config_pduLoop->dci_dl_pdu.dci_dl_pdu_rel8.aggregation_level,
                             nCCE_max,
Stefan's avatar
Stefan committed
3512 3513
                             0,
                             dl_config_pduLoop->dci_dl_pdu.dci_dl_pdu_rel8.rnti,
3514 3515
                             subframeP);

3516
      if (fCCE == -1) {
3517
        if (DL_req->number_pdcch_ofdm_symbols == max_symbol) {
3518
          LOG_I(MAC, "subframe %d: Dropping Allocation for RNTI %x\n",
3519
                subframeP,
Stefan's avatar
Stefan committed
3520
                dl_config_pduLoop->dci_dl_pdu.dci_dl_pdu_rel8.rnti);
3521 3522

          for (j = 0; j <= i; j++) {
Stefan's avatar
Stefan committed
3523
            dl_config_pduLoop = &dl_config_pdu[j];
frtabu's avatar
frtabu committed
3524

Stefan's avatar
Stefan committed
3525
            if (dl_config_pduLoop->pdu_type == NFAPI_DL_CONFIG_DCI_DL_PDU_TYPE)
3526
              LOG_D(MAC, "DCI %d/%d (%d,%d) : rnti %x dci format %d, aggreg %d nCCE %d / %d (num_pdcch_symbols %d)\n",
3527 3528 3529 3530
                    j,
                    DL_req->number_dci + HI_DCI0_req->number_of_dci,
                    DL_req->number_dci,
                    HI_DCI0_req->number_of_dci,
Stefan's avatar
Stefan committed
3531 3532 3533
                    dl_config_pduLoop->dci_dl_pdu.dci_dl_pdu_rel8.rnti,
                    dl_config_pduLoop->dci_dl_pdu.dci_dl_pdu_rel8.dci_format,
                    dl_config_pduLoop->dci_dl_pdu.dci_dl_pdu_rel8.aggregation_level,
3534 3535 3536 3537
                    nCCE,
                    nCCE_max,
                    DL_req->number_pdcch_ofdm_symbols);
          }
frtabu's avatar
frtabu committed
3538

3539
          //dump_CCE_table(CCE_table,nCCE_max,subframeP,dci_alloc->rnti,1<<dci_alloc->L);
3540
          return -1;
3541 3542
        }

3543
        LOG_D(MAC, "Can't fit DCI allocations with %d PDCCH symbols (rnti condition), increasing by 1\n",
3544 3545
              DL_req->number_pdcch_ofdm_symbols);
        DL_req->number_pdcch_ofdm_symbols++;
Stefan's avatar
Stefan committed
3546
        nCCE_max = get_nCCE_max(cc, DL_req->number_pdcch_ofdm_symbols, subframeP);
3547 3548
        goto try_again;
      }     // fCCE==-1
3549 3550

      // the allocation is feasible, rnti rule passes
Stefan's avatar
Stefan committed
3551
      nCCE += dl_config_pduLoop->dci_dl_pdu.dci_dl_pdu_rel8.aggregation_level;
3552
      LOG_D(MAC, "Allocating at nCCE %d\n",
Stefan's avatar
Stefan committed
3553
            fCCE);
3554

3555
      if ((test_onlyP%2) == 0) {
Stefan's avatar
Stefan committed
3556
        dl_config_pduLoop->dci_dl_pdu.dci_dl_pdu_rel8.cce_idx = fCCE;
3557 3558
        LOG_D(MAC, "Allocate CCEs subframe %d, test %d\n",
              subframeP,
Stefan's avatar
Stefan committed
3559
              test_onlyP);
3560
      }
3561

3562
      if ((test_onlyP/2) == 1) {
3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573
        for(int ack_int = 0; ack_int < ul_req->number_of_pdus; ack_int++) {
          if(((ul_req->ul_config_pdu_list[ack_int].pdu_type == NFAPI_UL_CONFIG_UCI_HARQ_PDU_TYPE) ||
              (ul_req->ul_config_pdu_list[ack_int].pdu_type == NFAPI_UL_CONFIG_UCI_SR_HARQ_PDU_TYPE)) &&
              (ul_req->ul_config_pdu_list[ack_int].uci_harq_pdu.ue_information.ue_information_rel8.rnti == dl_config_pdu[i].dci_dl_pdu.dci_dl_pdu_rel8.rnti)) {
            if (cc->tdd_Config==NULL)
              ul_req->ul_config_pdu_list[ack_int].uci_harq_pdu.harq_information.harq_information_rel9_fdd.n_pucch_1_0 =
                cc->radioResourceConfigCommon->pucch_ConfigCommon.n1PUCCH_AN + fCCE;
            else
              ul_req->ul_config_pdu_list[ack_int].uci_harq_pdu.harq_information.harq_information_rel10_tdd.n_pucch_1_0 =
                cc->radioResourceConfigCommon->pucch_ConfigCommon.n1PUCCH_AN + fCCE + getNp(cc->mib->message.dl_Bandwidth,fCCE,0) ;
          }
3574 3575
        }
      }
3576

3577 3578
      idci++;
    }
3579
  }       // for i = 0 ... num_DL_DCIs
frtabu's avatar
frtabu committed
3580

3581
  return 0;
3582 3583
}

3584
//------------------------------------------------------------------------------
Stefan's avatar
Stefan committed
3585 3586
nfapi_ul_config_request_pdu_t *
has_ul_grant(module_id_t module_idP,
3587 3588 3589
             int CC_idP,
             uint16_t absSFP,
             uint16_t rnti)
3590
//------------------------------------------------------------------------------
3591
{
Stefan's avatar
Stefan committed
3592 3593 3594 3595
  nfapi_ul_config_request_body_t *ul_req = &RC.mac[module_idP]->UL_req_tmp[CC_idP][absSFP % 10].ul_config_request_body;
  nfapi_ul_config_request_pdu_t *ul_config_pdu = &ul_req->ul_config_pdu_list[0];
  uint8_t pdu_type;
  LOG_D(MAC, "Checking for rnti %x UL grant in subframeP %d (num pdu %d)\n",
3596 3597
        rnti,
        absSFP % 10,
Stefan's avatar
Stefan committed
3598 3599 3600 3601
        ul_req->number_of_pdus);

  for (int i = 0; i < ul_req->number_of_pdus; i++, ul_config_pdu++) {
    pdu_type = ul_config_pdu->pdu_type;
3602
    LOG_D(MAC, "PDU %d : type %d,rnti %x\n",
Stefan's avatar
Stefan committed
3603
          i,
3604
          pdu_type,
Stefan's avatar
Stefan committed
3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647
          rnti);

    if (pdu_type == NFAPI_UL_CONFIG_ULSCH_PDU_TYPE && ul_config_pdu->ulsch_pdu.ulsch_pdu_rel8.rnti == rnti)
      return ul_config_pdu;

    if (pdu_type == NFAPI_UL_CONFIG_ULSCH_CQI_RI_PDU_TYPE && ul_config_pdu->ulsch_cqi_ri_pdu.ulsch_pdu.ulsch_pdu_rel8.rnti == rnti)
      return ul_config_pdu;

    if (pdu_type == NFAPI_UL_CONFIG_ULSCH_HARQ_PDU_TYPE && ul_config_pdu->ulsch_harq_pdu.ulsch_pdu.ulsch_pdu_rel8.rnti == rnti)
      return ul_config_pdu;

    if (pdu_type == NFAPI_UL_CONFIG_ULSCH_CQI_HARQ_RI_PDU_TYPE && ul_config_pdu->ulsch_cqi_harq_ri_pdu.ulsch_pdu.ulsch_pdu_rel8.rnti == rnti)
      return ul_config_pdu;

    if (pdu_type == NFAPI_UL_CONFIG_UCI_CQI_PDU_TYPE && ul_config_pdu->uci_cqi_pdu.ue_information.ue_information_rel8.rnti == rnti)
      return ul_config_pdu;

    if (pdu_type == NFAPI_UL_CONFIG_UCI_SR_PDU_TYPE && ul_config_pdu->uci_sr_pdu.ue_information.ue_information_rel8.rnti == rnti)
      return ul_config_pdu;

    if (pdu_type == NFAPI_UL_CONFIG_UCI_HARQ_PDU_TYPE && ul_config_pdu->uci_harq_pdu.ue_information.ue_information_rel8.rnti == rnti)
      return ul_config_pdu;

    if (pdu_type == NFAPI_UL_CONFIG_UCI_SR_HARQ_PDU_TYPE && ul_config_pdu->uci_sr_harq_pdu.ue_information.ue_information_rel8.rnti == rnti)
      return ul_config_pdu;

    if (pdu_type == NFAPI_UL_CONFIG_UCI_CQI_HARQ_PDU_TYPE && ul_config_pdu->uci_cqi_harq_pdu.ue_information.ue_information_rel8.rnti == rnti)
      return ul_config_pdu;

    if (pdu_type == NFAPI_UL_CONFIG_UCI_CQI_SR_PDU_TYPE && ul_config_pdu->uci_cqi_sr_pdu.ue_information.ue_information_rel8.rnti == rnti)
      return ul_config_pdu;

    if (pdu_type == NFAPI_UL_CONFIG_UCI_CQI_SR_HARQ_PDU_TYPE && ul_config_pdu->uci_cqi_sr_harq_pdu.ue_information.ue_information_rel8.rnti == rnti)
      return ul_config_pdu;

    if (pdu_type == NFAPI_UL_CONFIG_ULSCH_UCI_CSI_PDU_TYPE && ul_config_pdu->ulsch_uci_csi_pdu.ulsch_pdu.ulsch_pdu_rel8.rnti == rnti)
      return ul_config_pdu;

    if (pdu_type == NFAPI_UL_CONFIG_ULSCH_UCI_HARQ_PDU_TYPE && ul_config_pdu->ulsch_uci_harq_pdu.ulsch_pdu.ulsch_pdu_rel8.rnti == rnti)
      return ul_config_pdu;

    if (pdu_type == NFAPI_UL_CONFIG_ULSCH_CSI_UCI_HARQ_PDU_TYPE && ul_config_pdu->ulsch_csi_uci_harq_pdu.ulsch_pdu.ulsch_pdu_rel8.rnti == rnti)
      return ul_config_pdu;
3648
  }
Cedric Roux's avatar
Cedric Roux committed
3649

3650
  return (NULL);    // no ul grant at all for this UE
3651
}
Cedric Roux's avatar
Cedric Roux committed
3652

3653
//------------------------------------------------------------------------------
3654 3655
boolean_t
CCE_allocation_infeasible(int module_idP,
3656 3657 3658 3659 3660
                          int CC_idP,
                          int format_flag,
                          int subframe,
                          int aggregation,
                          int rnti)
3661
//------------------------------------------------------------------------------
3662
{
3663 3664
  nfapi_dl_config_request_body_t *DL_req       = &RC.mac[module_idP]->DL_req[CC_idP].dl_config_request_body;
  nfapi_dl_config_request_pdu_t *dl_config_pdu = &DL_req->dl_config_pdu_list[DL_req->number_pdu];
3665
  nfapi_hi_dci0_request_body_t *HI_DCI0_req    = &RC.mac[module_idP]->HI_DCI0_req[CC_idP][subframe].hi_dci0_request_body;
3666
  nfapi_hi_dci0_request_pdu_t *hi_dci0_pdu     = &HI_DCI0_req->hi_dci0_pdu_list[HI_DCI0_req->number_of_dci + HI_DCI0_req->number_of_hi];
3667
  boolean_t res = TRUE;
3668

3669
  if (format_flag != 2) { // DL DCI
3670
    if (DL_req->number_pdu == MAX_NUM_DL_PDU) {
3671
      LOG_W(MAC, "Subframe %d: FAPI DL structure is full, skip scheduling UE %d\n", subframe, rnti);
3672 3673 3674 3675 3676 3677 3678
    } else {
      dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.tl.tag            = NFAPI_DL_CONFIG_REQUEST_DCI_DL_PDU_REL8_TAG;
      dl_config_pdu->pdu_type                                     = NFAPI_DL_CONFIG_DCI_DL_PDU_TYPE;
      dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.rnti              = rnti;
      dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.rnti_type         = (format_flag == 0) ? 2 : 1;
      dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.aggregation_level = aggregation;
      DL_req->number_pdu++;
3679
      LOG_D(MAC, "Subframe %d: Checking CCE feasibility format %d : (%x,%d) \n",
3680
            subframe, format_flag, rnti, aggregation);
3681

3682 3683
      if (allocate_CCEs(module_idP, CC_idP, 0, subframe, 0) != -1)
        res = FALSE;
frtabu's avatar
frtabu committed
3684

3685 3686
      DL_req->number_pdu--;
    }
3687
  } else { // ue-specific UL DCI
3688
    if (HI_DCI0_req->number_of_dci + HI_DCI0_req->number_of_hi == MAX_NUM_HI_DCI0_PDU) {
3689
      LOG_W(MAC, "Subframe %d: FAPI UL structure is full, skip scheduling UE %d\n", subframe, rnti);
3690 3691 3692 3693 3694 3695
    } else {
      hi_dci0_pdu->pdu_type                               = NFAPI_HI_DCI0_DCI_PDU_TYPE;
      hi_dci0_pdu->dci_pdu.dci_pdu_rel8.tl.tag            = NFAPI_HI_DCI0_REQUEST_DCI_PDU_REL8_TAG;
      hi_dci0_pdu->dci_pdu.dci_pdu_rel8.rnti              = rnti;
      hi_dci0_pdu->dci_pdu.dci_pdu_rel8.aggregation_level = aggregation;
      HI_DCI0_req->number_of_dci++;
3696

3697 3698
      if (allocate_CCEs(module_idP, CC_idP, 0, subframe, 0) != -1)
        res = FALSE;
frtabu's avatar
frtabu committed
3699

3700
      HI_DCI0_req->number_of_dci--;
3701
    }
3702
  }
3703

3704
  return res;
3705
}
3706

3707
//------------------------------------------------------------------------------
3708 3709
void
get_retransmission_timing(LTE_TDD_Config_t *tdd_Config,
3710
                          frame_t *frameP,
3711
                          sub_frame_t *subframeP)
3712 3713
//------------------------------------------------------------------------------
{
3714
  if (tdd_Config == NULL) {
3715
    if (*subframeP > 1) {
3716
      *frameP = (*frameP + 1) % 1024;
3717
    }
frtabu's avatar
frtabu committed
3718

3719 3720 3721 3722
    *subframeP = (*subframeP + 8) % 10;
  } else {
    switch (tdd_Config->subframeAssignment) { //TODO fill in other TDD configs
      default:
3723 3724
        printf("%s:%d: TODO\n",
               __FILE__,
3725
               __LINE__);
3726
        abort();
3727
        break;
3728 3729 3730 3731

      case 1:
        if (*subframeP == 0 || *subframeP == 5) {
          *subframeP  += 19;
3732
          *frameP = (*frameP + (*subframeP / 10)) % 1024;
3733 3734 3735
          *subframeP %= 10;
        } else if (*subframeP == 4 || *subframeP == 9) {
          *subframeP  += 16;
3736
          *frameP = (*frameP + (*subframeP / 10)) % 1024;
3737 3738
          *subframeP %= 10;
        } else {
3739
          AssertFatal(2 == 1, "Illegal dl subframe %d for tdd config %ld\n",
3740
                      *subframeP,
3741
                      tdd_Config->subframeAssignment);
3742
        }
frtabu's avatar
frtabu committed
3743

3744
        break;
3745
    }
3746
  }
frtabu's avatar
frtabu committed
3747

3748
  return;
3749 3750
}

3751
//------------------------------------------------------------------------------
Stefan's avatar
Stefan committed
3752 3753
uint8_t
get_dl_subframe_count(int tdd_config_sfa,
3754
                      sub_frame_t subframeP)
3755
//------------------------------------------------------------------------------
Stefan's avatar
Stefan committed
3756
{
3757
  uint8_t tdd1[10] = {1, -1, -1, -1, 2, 3, -1, -1, -1, 4}; // special subframes 1,6 are excluded
3758

Stefan's avatar
Stefan committed
3759 3760
  switch (tdd_config_sfa) {// TODO fill in other tdd configs
    case 1:
3761 3762
      return tdd1[subframeP];
  }
3763

3764
  return -1;
3765 3766
}

3767
//------------------------------------------------------------------------------
Stefan's avatar
Stefan committed
3768 3769 3770
uint8_t
frame_subframe2_dl_harq_pid(LTE_TDD_Config_t *tdd_Config,
                            int abs_frameP,
3771
                            sub_frame_t subframeP)
3772 3773
//------------------------------------------------------------------------------
{
3774
  int harq_pid;
Stefan's avatar
Stefan committed
3775
  uint8_t count;
3776

Stefan's avatar
Stefan committed
3777
  if (tdd_Config) {
3778 3779
    switch(tdd_Config->subframeAssignment) { //TODO fill in other tdd config
      case 1:
3780
        count = get_dl_subframe_count(tdd_Config->subframeAssignment,
3781
                                      subframeP);
Stefan's avatar
Stefan committed
3782
        harq_pid = (((frame_cnt * 1024 + abs_frameP) * 4) - 1 + count) % 7;//4 dl subframe in a frame
3783

Stefan's avatar
Stefan committed
3784
        if (harq_pid < 0) {
3785
          harq_pid += 7;
3786
        }
3787 3788

        LOG_D(MAC,"[frame_subframe2_dl_harq_pid] (%d,%d) calculate harq_pid ((( %d * 1024 + %d) *4) - 1 + %d) = %d \n",
Stefan's avatar
Stefan committed
3789 3790 3791 3792 3793 3794
              (abs_frameP + 1024) % 1024,
              subframeP,
              frame_cnt,
              abs_frameP,
              count,
              harq_pid);
3795
        return harq_pid;
3796
    }
3797
  } else {
Stefan's avatar
Stefan committed
3798
    return ((abs_frameP * 10) + subframeP) & 7;
3799
  }
3800

3801
  return -1;
3802 3803
}

3804
//------------------------------------------------------------------------------
3805
unsigned char
3806
ul_ACK_subframe2M(LTE_TDD_Config_t *tdd_Config,
3807
                  unsigned char subframe)
3808
//------------------------------------------------------------------------------
3809 3810
{
  if (tdd_Config == NULL) {
3811
    return 1;
3812
  }
frtabu's avatar
frtabu committed
3813

3814
  switch (tdd_Config->subframeAssignment) {
3815
    case 1:
3816
      return 1; // don't ACK special subframe for now
frtabu's avatar
frtabu committed
3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832

    /*
    if (subframe == 2) {  // ACK subframes 5 and 6
      return(2);
    } else if (subframe == 3) { // ACK subframe 9
      return(1);  // To be updated
    } else if (subframe == 7) { // ACK subframes 0 and 1
      return(2);  // To be updated
    } else if (subframe == 8) { // ACK subframe 4
      return(1);  // To be updated
    } else {
      AssertFatal(1==0,"illegal subframe %d for tdd_config %ld\n",
                  subframe,tdd_Config->subframeAssignment);
    }
    break;
    */
3833 3834
    case 3:
      if (subframe == 2) {  // ACK subframes 5 and 6
3835
        return 2; // should be 3
3836
      }
frtabu's avatar
frtabu committed
3837

3838 3839
      if (subframe == 3) { // ACK subframes 7 and 8
        return 2;  // To be updated
3840
      }
frtabu's avatar
frtabu committed
3841

3842 3843
      if (subframe == 4) { // ACK subframes 9 and 0
        return 2;
3844
      }
frtabu's avatar
frtabu committed
3845

3846 3847 3848
      AssertFatal(1==0,"illegal subframe %d for tdd_config %ld\n",
                  subframe,
                  tdd_Config->subframeAssignment);
3849 3850 3851
      break;

    case 4:
3852 3853
      if (subframe == 2) {  // ACK subframes 0,4 and 5
        return 3; // should be 4
3854
      }
frtabu's avatar
frtabu committed
3855

3856 3857
      if (subframe == 3) { // ACK subframes 6,7,8 and 9
        return 4;
3858
      }
frtabu's avatar
frtabu committed
3859

3860 3861 3862 3863
      AssertFatal(1==0,"illegal subframe %d for tdd_config %ld\n",
                  subframe,
                  tdd_Config->subframeAssignment);
      break;
3864 3865

    case 5:
3866 3867
      if (subframe == 2) {  // ACK subframes 0,3,4,5,6,7,8 and 9
        return 8; // should be 3
3868
      }
frtabu's avatar
frtabu committed
3869

3870 3871 3872 3873
      AssertFatal(1==0,"illegal subframe %d for tdd_config %ld\n",
                  subframe,
                  tdd_Config->subframeAssignment);
      break;
3874 3875
  }

3876
  return 0;
3877 3878
}

3879
//------------------------------------------------------------------------------
3880
unsigned char
3881 3882
ul_ACK_subframe2dl_subframe(LTE_TDD_Config_t *tdd_Config,
                            unsigned char subframe,
3883
                            unsigned char ACK_index)
3884
//------------------------------------------------------------------------------
3885 3886
{
  if (tdd_Config == NULL) {
3887
    return ((subframe < 4) ? subframe + 6 : subframe - 4);
3888
  }
3889

3890
  switch (tdd_Config->subframeAssignment) {
3891 3892
    case 3:
      if (subframe == 2) {  // ACK subframes 5 and 6
3893
        if (ACK_index == 2) return 1;
frtabu's avatar
frtabu committed
3894

3895
        return (5 + ACK_index);
3896
      }
frtabu's avatar
frtabu committed
3897

3898 3899
      if (subframe == 3) { // ACK subframes 7 and 8
        return (7 + ACK_index);  // To be updated
3900
      }
frtabu's avatar
frtabu committed
3901

3902 3903
      if (subframe == 4) { // ACK subframes 9 and 0
        return ((9 + ACK_index) % 10);
3904
      }
frtabu's avatar
frtabu committed
3905

3906
      AssertFatal(1==0, "illegal subframe %d for tdd_config->subframeAssignment %ld\n",
3907 3908
                  subframe,
                  tdd_Config->subframeAssignment);
3909 3910 3911
      break;

    case 4:
3912 3913 3914 3915
      if (subframe == 2) {  // ACK subframes 0, 4 and 5
        //if (ACK_index==2)
        //  return(1); TBC
        if (ACK_index == 2) return 0;
frtabu's avatar
frtabu committed
3916

3917
        return (4 + ACK_index);
3918
      }
frtabu's avatar
frtabu committed
3919

3920 3921
      if (subframe == 3) { // ACK subframes 6, 7 8 and 9
        return (6 + ACK_index);  // To be updated
3922
      }
frtabu's avatar
frtabu committed
3923

3924
      AssertFatal(1 == 0, "illegal subframe %d for tdd_config %ld\n",
3925 3926 3927
                  subframe,
                  tdd_Config->subframeAssignment);
      break;
3928 3929 3930

    case 1:
      if (subframe == 2) {  // ACK subframes 5 and 6
3931
        return (5 + ACK_index);
3932
      }
frtabu's avatar
frtabu committed
3933

3934 3935 3936
      if (subframe == 3) { // ACK subframe 9
        return 9;  // To be updated
      }
frtabu's avatar
frtabu committed
3937

3938 3939 3940
      if (subframe == 7) { // ACK subframes 0 and 1
        return ACK_index;  // To be updated
      }
frtabu's avatar
frtabu committed
3941

3942 3943
      if (subframe == 8) { // ACK subframe 4
        return 4;  // To be updated
3944
      }
frtabu's avatar
frtabu committed
3945

3946
      AssertFatal(1 == 0, "illegal subframe %d for tdd_config %ld\n",
3947 3948
                  subframe,
                  tdd_Config->subframeAssignment);
3949 3950
      break;
  }
frtabu's avatar
frtabu committed
3951

3952
  return 0;
3953
}
3954

3955
//------------------------------------------------------------------------------
3956
void
3957 3958
extract_harq(module_id_t mod_idP,
             int CC_idP,
3959
             int UE_id,
3960
             frame_t frameP,
3961
             sub_frame_t subframeP,
3962 3963
             void *harq_indication,
             int format)
3964
//------------------------------------------------------------------------------
Cedric Roux's avatar
Cedric Roux committed
3965
{
3966 3967
  eNB_MAC_INST *eNB = RC.mac[mod_idP];
  UE_list_t *UE_list = &eNB->UE_list;
3968
  UE_sched_ctrl_t *sched_ctl = &UE_list->UE_sched_ctrl[UE_id];
3969
  rnti_t rnti = UE_RNTI(mod_idP, UE_id);
3970
  COMMON_channels_t *cc = &eNB->common_channels[CC_idP];
3971 3972 3973 3974 3975 3976 3977
  nfapi_harq_indication_fdd_rel13_t *harq_indication_fdd;
  nfapi_harq_indication_tdd_rel13_t *harq_indication_tdd;
  uint16_t num_ack_nak;
  int numCC = UE_list->numactiveCCs[UE_id];
  int pCCid = UE_list->pCC_id[UE_id];
  int spatial_bundling = 0;
  int tmode[5];
3978
  int i, j, m;
3979
  uint8_t *pdu;
3980 3981 3982
  sub_frame_t subframe_tx;
  int frame_tx;
  uint8_t harq_pid;
3983
#if (LTE_RRC_VERSION >= MAKE_VERSION(13, 0, 0))
3984
  LTE_PhysicalConfigDedicated_t *physicalConfigDedicated = UE_list->UE_template[pCCid][UE_id].physicalConfigDedicated;
frtabu's avatar
frtabu committed
3985

3986 3987
  if (physicalConfigDedicated != NULL && physicalConfigDedicated->pucch_ConfigDedicated != NULL &&
      physicalConfigDedicated->ext7 != NULL && physicalConfigDedicated->ext7->pucch_ConfigDedicated_r13 != NULL &&
3988 3989
      ((physicalConfigDedicated->ext7->pucch_ConfigDedicated_r13->spatialBundlingPUCCH_r13 && format == 0) ||
       (physicalConfigDedicated->ext7->pucch_ConfigDedicated_r13->spatialBundlingPUSCH_r13 && format == 1))) {
3990
    spatial_bundling = 1;
3991
  }
frtabu's avatar
frtabu committed
3992

3993
#endif
3994

3995
  for (i = 0; i < numCC; i++) {
3996 3997
    tmode[i] = get_tmode(mod_idP,
                         i,
3998 3999
                         UE_id);
  }
frtabu's avatar
frtabu committed
4000

4001 4002 4003 4004
  if (cc->tdd_Config) {
    harq_indication_tdd = (nfapi_harq_indication_tdd_rel13_t *) harq_indication;
    //    pdu = &harq_indication_tdd->harq_tb_n[0];
    num_ack_nak = harq_indication_tdd->number_of_ack_nack;
4005

4006
    switch (harq_indication_tdd->mode) {
4007
      case 0:   // Format 1a/b bundling
4008
        AssertFatal(numCC == 1, "numCC %d > 1, should not be using Format1a/b\n",
4009 4010 4011
                    numCC);
        int M = ul_ACK_subframe2M(cc->tdd_Config,
                                  subframeP);
4012

4013 4014
        for (m=0; m<M; m++) {
          subframe_tx = ul_ACK_subframe2dl_subframe(cc->tdd_Config,
frtabu's avatar
frtabu committed
4015 4016
                        subframeP,
                        m);
4017

4018 4019
          if (frameP==1023&&subframeP>5) frame_tx=-1;
          else frame_tx = subframeP < 4 ? frameP -1 : frameP;
4020

4021 4022 4023 4024
          harq_pid = frame_subframe2_dl_harq_pid(cc->tdd_Config,
                                                 frame_tx,
                                                 subframe_tx);
          RA_t *ra = &eNB->common_channels[CC_idP].ra[0];
4025

4026 4027
          if(num_ack_nak == 1) {
            if (harq_indication_tdd->harq_data[0].bundling.value_0 == 1) { //ack
4028 4029
              sched_ctl->round[CC_idP][harq_pid] = 8; // release HARQ process
              sched_ctl->tbcnt[CC_idP][harq_pid] = 0;
4030 4031 4032 4033 4034 4035 4036
              LOG_D(MAC, "frame %d subframe %d Acking (%d,%d) harq_pid %d round %d\n",
                    frameP,
                    subframeP,
                    frame_tx,
                    subframe_tx,
                    harq_pid,
                    sched_ctl->round[CC_idP][harq_pid]);
4037
            } else { //nack
4038
              if (sched_ctl->round[CC_idP][harq_pid] < 8) sched_ctl->round[CC_idP][harq_pid]++;
4039 4040 4041 4042 4043 4044

              if (sched_ctl->round[CC_idP][harq_pid] == 4) {
                sched_ctl->round[CC_idP][harq_pid] = 8;     // release HARQ process
                sched_ctl->tbcnt[CC_idP][harq_pid] = 0;
              }

4045 4046 4047 4048 4049 4050 4051
              LOG_D(MAC,"frame %d subframe %d Nacking (%d,%d) harq_pid %d round %d\n",
                    frameP,
                    subframeP,
                    frame_tx,
                    subframe_tx,
                    harq_pid,
                    sched_ctl->round[CC_idP][harq_pid]);
4052

4053
              if (sched_ctl->round[CC_idP][harq_pid] == 8) {
4054
                for (uint8_t ra_i = 0; ra_i < NB_RA_PROC_MAX; ra_i++) {
4055
                  if (ra[ra_i].rnti == rnti && ra[ra_i].state == WAITMSG4ACK) {
4056 4057
                    //Msg NACK num to MAC ,remove UE
                    // add UE info to freeList
4058
                    LOG_I(RRC, "put UE %x into freeList\n",
4059
                          rnti);
4060
                    put_UE_in_freelist(mod_idP,
frtabu's avatar
frtabu committed
4061 4062
                                       rnti,
                                       1);
4063 4064 4065 4066 4067 4068 4069
                  }
                }
              }
            }
          }

          for (uint8_t ra_i = 0; ra_i < NB_RA_PROC_MAX; ra_i++) {
4070 4071 4072 4073 4074 4075 4076
            if (ra[ra_i].rnti == rnti && ra[ra_i].state == MSGCRNTI_ACK && ra[ra_i].crnti_harq_pid == harq_pid) {
              LOG_D(MAC,"CRNTI Reconfiguration: ACK %d rnti %x round %d frame %d subframe %d \n",
                    harq_indication_tdd->harq_data[0].bundling.value_0,
                    rnti,
                    sched_ctl->round[CC_idP][harq_pid],
                    frameP,
                    subframeP);
Stefan's avatar
Stefan committed
4077

4078
              if (num_ack_nak == 1 && harq_indication_tdd->harq_data[0].bundling.value_0 == 1) {
4079 4080 4081
                cancel_ra_proc(mod_idP,
                               CC_idP,
                               frameP,
4082
                               ra[ra_i].rnti);
4083 4084
              } else {
                if(sched_ctl->round[CC_idP][harq_pid] == 7) {
4085 4086 4087
                  cancel_ra_proc(mod_idP,
                                 CC_idP,
                                 frameP,
4088
                                 ra[ra_i].rnti);
4089 4090
                }
              }
frtabu's avatar
frtabu committed
4091

4092 4093
              break;
            }
4094 4095
          }
        }
4096 4097 4098 4099 4100 4101 4102
        break;

      case 1:   // Channel Selection
      case 2:   // Format 3
      case 3:   // Format 4
      case 4:   // Format 5
        break;
4103 4104 4105 4106 4107
    }
  } else {
    harq_indication_fdd = (nfapi_harq_indication_fdd_rel13_t *) harq_indication;
    num_ack_nak         = harq_indication_fdd->number_of_ack_nack;
    pdu                 = &harq_indication_fdd->harq_tb_n[0];
4108
    harq_pid = ((10 * frameP) + subframeP + 10236) & 7;
4109 4110 4111 4112 4113 4114 4115 4116
    LOG_D(MAC, "frame %d subframe %d harq_pid %d mode %d tmode[0] %d num_ack_nak %d round %d\n",
          frameP,
          subframeP,
          harq_pid,
          harq_indication_fdd->mode,
          tmode[0],
          num_ack_nak,
          sched_ctl->round[CC_idP][harq_pid]);
4117

4118 4119
    // use 1 HARQ proces of BL/CE UE for now
    if (UE_list->UE_template[pCCid][UE_id].rach_resource_type > 0) harq_pid = 0;
4120 4121

    switch (harq_indication_fdd->mode) {
Stefan's avatar
Stefan committed
4122
      case 0:   // Format 1a/b (10.1.2.1)
4123
        AssertFatal(numCC == 1, "numCC %d > 1, should not be using Format1a/b\n",
4124
                    numCC);
Stefan's avatar
Stefan committed
4125 4126

        if (tmode[0] == 1 || tmode[0] == 2 || tmode[0] == 5 || tmode[0] == 6 || tmode[0] == 7) {  // NOTE: have to handle the case of TM9-10 with 1 antenna port
4127
          // single ACK/NAK bit
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
4128 4129 4130 4131
          AssertFatal(num_ack_nak == 1, "num_ack_nak %d > 1 for 1 CC and single-layer transmission frame:%d subframe:%d\n",
                      num_ack_nak,
                      frameP,
                      subframeP);
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
4132 4133

          // In case of nFAPI, sometimes timing of eNB and UE become different.
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
4134
          // So if nfapi_mode == 2(VNF), this function don't check assertion to avoid process exit.
frtabu's avatar
frtabu committed
4135
          if (NFAPI_MODE != NFAPI_MODE_VNF) {
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
4136
            AssertFatal(sched_ctl->round[CC_idP][harq_pid] < 8, "Got ACK/NAK for inactive harq_pid %d for UE %d/%x\n",
4137 4138
                        harq_pid,
                        UE_id,
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
4139
                        rnti);
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
4140 4141
          } else {
            if (sched_ctl->round[CC_idP][harq_pid] == 8) {
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
4142
              LOG_E(MAC,"Got ACK/NAK for inactive harq_pid %d for UE %d/%x\n",
4143 4144
                    harq_pid,
                    UE_id,
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
4145
                    rnti);
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
4146 4147
              return;
            }
4148
          }
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
4149 4150

          AssertFatal(pdu[0] == 1 || pdu[0] == 2 || pdu[0] == 4, "Received ACK/NAK %d which is not 1 or 2 for harq_pid %d from UE %d/%x\n",
4151 4152 4153
                      pdu[0],
                      harq_pid,
                      UE_id,
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
4154
                      rnti);
4155 4156
          LOG_D(MAC, "Received %d for harq_pid %d\n",
                pdu[0],
4157 4158
                harq_pid);
          RA_t *ra = &eNB->common_channels[CC_idP].ra[0];
4159 4160

          for (uint8_t ra_i = 0; ra_i < NB_RA_PROC_MAX; ra_i++) {
4161
            if (ra[ra_i].rnti == rnti && ra[ra_i].state == MSGCRNTI_ACK && ra[ra_i].crnti_harq_pid == harq_pid) {
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
4162 4163 4164 4165
              LOG_D(MAC,"CRNTI Reconfiguration: ACK %d rnti %x round %d frame %d subframe %d \n",
                    pdu[0],
                    rnti,
                    sched_ctl->round[CC_idP][harq_pid],
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
4166 4167
                    frameP,
                    subframeP);
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
4168 4169

              if (pdu[0] == 1) {
4170 4171 4172
                cancel_ra_proc(mod_idP,
                               CC_idP,
                               frameP,
4173
                               ra[ra_i].rnti);
4174
              } else {
Louis Adrien Dufrene's avatar
Louis Adrien Dufrene committed
4175
                if (sched_ctl->round[CC_idP][harq_pid] == 7) {
4176 4177 4178
                  cancel_ra_proc(mod_idP,
                                 CC_idP,
                                 frameP,
4179
                                 ra[ra_i].rnti);
4180 4181
                }
              }
frtabu's avatar
frtabu committed
4182

4183
              break;
Xu Bo's avatar
Xu Bo committed
4184
            }
4185
          }
4186

nepes's avatar
nepes committed
4187
          LOG_D(MAC, "In extract_harq(): pdu[0] = %d for harq_pid = %d\n", pdu[0], harq_pid);
4188

4189 4190
          if (pdu[0] == 1) {  // ACK
            sched_ctl->round[CC_idP][harq_pid] = 8; // release HARQ process
4191
            sched_ctl->tbcnt[CC_idP][harq_pid] = 0;
4192 4193 4194 4195

            /* CDRX: PUCCH gives an ACK, so reset corresponding HARQ RTT */
            sched_ctl->harq_rtt_timer[CC_idP][harq_pid] = 0;

4196 4197 4198 4199 4200 4201
          } else if (pdu[0] == 2 || pdu[0] == 4) {  // NAK (treat DTX as NAK)
            sched_ctl->round[CC_idP][harq_pid]++; // increment round

            if (sched_ctl->round[CC_idP][harq_pid] == 4) {
              sched_ctl->round[CC_idP][harq_pid] = 8; // release HARQ process
              sched_ctl->tbcnt[CC_idP][harq_pid] = 0;
4202 4203 4204

              /* CDRX: PUCCH gives an NACK and max number of repetitions reached so reset corresponding HARQ RTT */
              sched_ctl->harq_rtt_timer[CC_idP][harq_pid] = 0;
4205 4206 4207 4208 4209
            }

            if (sched_ctl->round[CC_idP][harq_pid] == 8) {
              for (uint8_t ra_i = 0; ra_i < NB_RA_PROC_MAX; ra_i++) {
                if((ra[ra_i].rnti == rnti) && (ra[ra_i].state == WAITMSG4ACK)) {
4210
                  // Msg NACK num to MAC ,remove UE
4211
                  // add UE info to freeList
4212
                  LOG_I(RRC, "put UE %x into freeList\n",
4213
                        rnti);
4214 4215
                  put_UE_in_freelist(mod_idP,
                                     rnti,
4216
                                     1);
4217 4218 4219
                }
              }
            }
4220
          }
4221 4222
        } else {
          // one or two ACK/NAK bits
4223
          AssertFatal(num_ack_nak <= 2, "num_ack_nak %d > 2 for 1 CC and TM3/4/8/9/10\n",
4224 4225
                      num_ack_nak);

4226
          if (num_ack_nak == 2 && sched_ctl->round[CC_idP][harq_pid] < 8 && sched_ctl->tbcnt[CC_idP][harq_pid] == 1 && pdu[0] == 1 && pdu[1] == 1) {
4227
            sched_ctl->round[CC_idP][harq_pid] = 8;
4228
            sched_ctl->tbcnt[CC_idP][harq_pid] = 0;
4229 4230 4231

            /* CDRX: PUCCH gives an ACK, so reset corresponding HARQ RTT */
            sched_ctl->harq_rtt_timer[CC_idP][harq_pid] = 0;
4232
          }
4233 4234 4235 4236 4237 4238 4239 4240 4241 4242

          if ((num_ack_nak == 2)
              && (sched_ctl->round[CC_idP][harq_pid] < 8)
              && (sched_ctl->tbcnt[CC_idP][harq_pid] == 1)
              && (pdu[0] == 2) && (pdu[1] == 2)) {
            sched_ctl->round[CC_idP][harq_pid]++;

            if (sched_ctl->round[CC_idP][harq_pid] == 4) {
              sched_ctl->round[CC_idP][harq_pid] = 8;     // release HARQ process
              sched_ctl->tbcnt[CC_idP][harq_pid] = 0;
4243 4244 4245

              /* CDRX: PUCCH gives an NACK and max number of repetitions reached so reset corresponding HARQ RTT */
              sched_ctl->harq_rtt_timer[CC_idP][harq_pid] = 0;
4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260
            }
          } else if (((num_ack_nak == 2)
                      && (sched_ctl->round[CC_idP][harq_pid] < 8)
                      && (sched_ctl->tbcnt[0][harq_pid] == 2)
                      && (pdu[0] == 1) && (pdu[1] == 2))
                     || ((num_ack_nak == 2)
                         && (sched_ctl->round[CC_idP][harq_pid] < 8)
                         && (sched_ctl->tbcnt[CC_idP][harq_pid] == 2)
                         && (pdu[0] == 2) && (pdu[1] == 1))) {
            sched_ctl->round[CC_idP][harq_pid]++;
            sched_ctl->tbcnt[CC_idP][harq_pid] = 1;

            if (sched_ctl->round[CC_idP][harq_pid] == 4) {
              sched_ctl->round[CC_idP][harq_pid] = 8;     // release HARQ process
              sched_ctl->tbcnt[CC_idP][harq_pid] = 0;  /* TODO: do we have to set it to 0? */
4261 4262 4263

              /* CDRX: PUCCH gives an NACK and max number of repetitions reached so reset corresponding HARQ RTT */
              sched_ctl->harq_rtt_timer[CC_idP][harq_pid] = 0;
4264 4265 4266 4267 4268 4269 4270 4271 4272 4273
            }
          } else if ((num_ack_nak == 2)
                     && (sched_ctl->round[CC_idP][harq_pid] < 8)
                     && (sched_ctl->tbcnt[CC_idP][harq_pid] == 2)
                     && (pdu[0] == 2) && (pdu[1] == 2)) {
            sched_ctl->round[CC_idP][harq_pid]++;

            if (sched_ctl->round[CC_idP][harq_pid] == 4) {
              sched_ctl->round[CC_idP][harq_pid] = 8;     // release HARQ process
              sched_ctl->tbcnt[CC_idP][harq_pid] = 0;
4274 4275 4276

              /* CDRX: PUCCH gives an NACK and max number of repetitions reached so reset corresponding HARQ RTT */
              sched_ctl->harq_rtt_timer[CC_idP][harq_pid] = 0;
4277 4278
            }
          } else
4279
            AssertFatal(1 == 0, "Illegal ACK/NAK/round combination (%d,%d,%d,%d,%d) for harq_pid %d, UE %d/%x\n",
4280 4281
                        num_ack_nak,
                        sched_ctl->round[CC_idP][harq_pid],
4282
                        sched_ctl->round[CC_idP][harq_pid],
4283
                        pdu[0],
4284 4285 4286
                        pdu[1],
                        harq_pid,
                        UE_id,
4287
                        rnti);
4288
        }
4289 4290 4291 4292

        break;

      case 1:   // FDD Channel Selection (10.1.2.2.1), must be received for 2 serving cells
4293
        AssertFatal(numCC == 2, "Should not receive harq indication with channel selection with %d active CCs\n",
4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312
                    numCC);

        if ((num_ack_nak == 2)
            && (sched_ctl->round[pCCid][harq_pid] < 8)
            && (sched_ctl->round[1 - pCCid][harq_pid] < 8)
            && (sched_ctl->tbcnt[pCCid][harq_pid] == 1)
            && (sched_ctl->tbcnt[1 - pCCid][harq_pid] == 1)) {
          AssertFatal(pdu[0] <= 3, "pdu[0] %d is not ACK/NAK/DTX\n",
                      pdu[0]);
          AssertFatal(pdu[1] <= 3, "pdu[1] %d is not ACK/NAK/DTX\n",
                      pdu[1]);

          if (pdu[0] == 1)
            sched_ctl->round[pCCid][harq_pid] = 8;
          else {
            sched_ctl->round[pCCid][harq_pid]++;

            if (sched_ctl->round[pCCid][harq_pid] == 4)
              sched_ctl->round[pCCid][harq_pid] = 8;
4313
          }
4314 4315 4316 4317 4318 4319 4320 4321

          if (pdu[1] == 1)
            sched_ctl->round[1 - pCCid][harq_pid] = 8;
          else {
            sched_ctl->round[1 - pCCid][harq_pid]++;

            if (sched_ctl->round[1 - pCCid][harq_pid] == 4)
              sched_ctl->round[1 - pCCid][harq_pid] = 8;
4322
          }
4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334
        }     // A=2
        else if ((num_ack_nak == 3)
                 && (sched_ctl->round[pCCid][harq_pid] < 8)
                 && (sched_ctl->tbcnt[pCCid][harq_pid] == 2)
                 && (sched_ctl->round[1 - pCCid][harq_pid] < 8)
                 && (sched_ctl->tbcnt[1 - pCCid][harq_pid] == 1)) {
          AssertFatal(pdu[0] <= 3, "pdu[0] %d is not ACK/NAK/DTX\n",
                      pdu[0]);
          AssertFatal(pdu[1] <= 3, "pdu[1] %d is not ACK/NAK/DTX\n",
                      pdu[1]);
          AssertFatal(pdu[2] <= 3, "pdu[2] %d is not ACK/NAK/DTX\n",
                      pdu[2]);
4335
          AssertFatal(sched_ctl->tbcnt[pCCid][harq_pid] == 2, "sched_ctl->tbcnt[%d][%d] != 2 for UE %d/%x\n",
4336 4337 4338
                      pCCid,
                      harq_pid,
                      UE_id,
4339 4340
                      rnti);
          AssertFatal(sched_ctl->tbcnt[1 - pCCid][harq_pid] == 1, "sched_ctl->tbcnt[%d][%d] != 1 for UE %d/%x\n",
4341 4342 4343
                      1 - pCCid,
                      harq_pid,
                      UE_id,
4344 4345 4346
                      rnti);

          if (pdu[0] == 1 && pdu[1] == 1) { // both ACK
4347 4348
            sched_ctl->round[pCCid][harq_pid] = 8;
            sched_ctl->tbcnt[pCCid][harq_pid] = 0;
4349
          } else if ((pdu[0] == 2 && pdu[1] == 1) || (pdu[0] == 1 && pdu[1] == 2)) {
4350 4351 4352 4353 4354 4355 4356 4357 4358
            sched_ctl->round[pCCid][harq_pid]++;
            sched_ctl->tbcnt[pCCid][harq_pid] = 1;

            if (sched_ctl->round[pCCid][harq_pid] == 4) {
              sched_ctl->round[pCCid][harq_pid] = 8;
              sched_ctl->tbcnt[pCCid][harq_pid] = 0; /* TODO: do we have to set it to 0? */
            }
          } else {
            sched_ctl->round[pCCid][harq_pid]++;
4359

4360 4361 4362 4363
            if (sched_ctl->round[pCCid][harq_pid] == 4) {
              sched_ctl->round[pCCid][harq_pid] = 8;
              sched_ctl->tbcnt[pCCid][harq_pid] = 0;
            }
4364
          }
4365

4366
          if (pdu[2] == 1) sched_ctl->round[1 - pCCid][harq_pid] = 8;
4367 4368 4369 4370 4371 4372
          else {
            sched_ctl->round[1 - pCCid][harq_pid]++;

            if (sched_ctl->round[1 - pCCid][harq_pid] == 4) {
              sched_ctl->round[1 - pCCid][harq_pid] = 8;
            }
4373
          }
4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385
        }     // A=3 primary cell has 2 TBs
        else if ((num_ack_nak == 3)
                 && (sched_ctl->round[1 - pCCid][harq_pid] < 8)
                 && (sched_ctl->round[pCCid][harq_pid] < 8)
                 && (sched_ctl->tbcnt[1 - pCCid][harq_pid] == 2)
                 && (sched_ctl->tbcnt[pCCid][harq_pid] == 1)) {
          AssertFatal(pdu[0] <= 3, "pdu[0] %d is not ACK/NAK/DTX\n",
                      pdu[0]);
          AssertFatal(pdu[1] <= 3, "pdu[1] %d is not ACK/NAK/DTX\n",
                      pdu[1]);
          AssertFatal(pdu[2] <= 3, "pdu[2] %d is not ACK/NAK/DTX\n",
                      pdu[2]);
4386
          AssertFatal(sched_ctl->tbcnt[1 - pCCid][harq_pid] == 2, "sched_ctl->tbcnt[%d][%d] != 2 for UE %d/%x\n",
4387 4388 4389
                      1 - pCCid,
                      harq_pid,
                      UE_id,
4390 4391
                      rnti);
          AssertFatal(sched_ctl->tbcnt[pCCid][harq_pid] == 1, "sched_ctl->tbcnt[%d][%d] != 1 for UE %d/%x\n",
4392 4393 4394
                      pCCid,
                      harq_pid,
                      UE_id,
4395 4396 4397
                      rnti);

          if (pdu[0] == 1 && pdu[1] == 1) { // both ACK
4398 4399
            sched_ctl->round[1 - pCCid][harq_pid] = 8;
            sched_ctl->tbcnt[1 - pCCid][harq_pid] = 0;
4400
          } else if ((pdu[0] >= 2 && pdu[1] == 1) || (pdu[0] == 1 && pdu[1] >= 2)) { // one ACK
4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414
            sched_ctl->round[1 - pCCid][harq_pid]++;
            sched_ctl->tbcnt[1 - pCCid][harq_pid] = 1;

            if (sched_ctl->round[1 - pCCid][harq_pid] == 4) {
              sched_ctl->round[1 - pCCid][harq_pid] = 8;
              sched_ctl->tbcnt[1 - pCCid][harq_pid] = 0;
            }
          } else {    // both NAK/DTX
            sched_ctl->round[1 - pCCid][harq_pid]++;

            if (sched_ctl->round[1 - pCCid][harq_pid] == 4) {
              sched_ctl->round[1 - pCCid][harq_pid] = 8;
              sched_ctl->tbcnt[1 - pCCid][harq_pid] = 0;
            }
4415
          }
4416

4417
          if (pdu[2] == 1) sched_ctl->round[pCCid][harq_pid] = 8;
4418 4419 4420 4421 4422 4423
          else {
            sched_ctl->round[pCCid][harq_pid]++;

            if (sched_ctl->round[pCCid][harq_pid] == 4) {
              sched_ctl->round[pCCid][harq_pid] = 8;
            }
4424
          }
4425 4426
        }     // A=3 secondary cell has 2 TBs

4427
#if MAX_NUM_CCs>1
4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440
        else if ((num_ack_nak == 4)
                 && (sched_ctl->round[0][harq_pid] < 8)
                 && (sched_ctl->round[1][harq_pid] < 8)
                 && (sched_ctl->tbcnt[1 - pCCid][harq_pid] == 2)
                 && (sched_ctl->tbcnt[pCCid][harq_pid] == 2)) {
          AssertFatal(pdu[0] <= 3, "pdu[0] %d is not ACK/NAK/DTX\n",
                      pdu[0]);
          AssertFatal(pdu[1] <= 3, "pdu[1] %d is not ACK/NAK/DTX\n",
                      pdu[1]);
          AssertFatal(pdu[2] <= 3, "pdu[2] %d is not ACK/NAK/DTX\n",
                      pdu[2]);
          AssertFatal(pdu[3] <= 3, "pdu[3] %d is not ACK/NAK/DTX\n",
                      pdu[3]);
4441
          AssertFatal(sched_ctl->tbcnt[0][harq_pid] == 2, "sched_ctl->tbcnt[0][%d] != 2 for UE %d/%x\n",
4442 4443
                      harq_pid,
                      UE_id,
4444 4445
                      rnti);
          AssertFatal(sched_ctl->tbcnt[1][harq_pid] == 2, "sched_ctl->tbcnt[1][%d] != 2 for UE %d/%x\n",
4446 4447
                      harq_pid,
                      UE_id,
4448 4449 4450
                      rnti);

          if (pdu[0] == 1 && pdu[1] == 1) { // both ACK
4451 4452
            sched_ctl->round[0][harq_pid] = 8;
            sched_ctl->tbcnt[0][harq_pid] = 0;
4453
          } else if ((pdu[0] >= 2 && pdu[1] == 1) || (pdu[0] == 1 && pdu[1] >= 2)) { // one ACK
4454 4455 4456 4457 4458 4459 4460 4461 4462
            sched_ctl->round[0][harq_pid]++;
            sched_ctl->tbcnt[0][harq_pid] = 1;

            if (sched_ctl->round[0][harq_pid] == 4) {
              sched_ctl->round[0][harq_pid] = 8;
              sched_ctl->tbcnt[0][harq_pid] = 0;
            }
          } else {    // both NAK/DTX
            sched_ctl->round[0][harq_pid]++;
4463

4464 4465 4466 4467
            if (sched_ctl->round[0][harq_pid] == 4) {
              sched_ctl->round[0][harq_pid] = 8;
              sched_ctl->tbcnt[0][harq_pid] = 0;
            }
4468
          }
4469

4470
          if (pdu[2] == 1 && pdu[3] == 1) { // both ACK
4471 4472
            sched_ctl->round[1][harq_pid] = 8;
            sched_ctl->tbcnt[1][harq_pid] = 0;
4473
          } else if ((pdu[2] >= 2 && pdu[3] == 1) || (pdu[2] == 1 && pdu[3] >= 2)) { // one ACK
4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487
            sched_ctl->round[1][harq_pid]++;
            sched_ctl->tbcnt[1][harq_pid] = 1;

            if (sched_ctl->round[1][harq_pid] == 4) {
              sched_ctl->round[1][harq_pid] = 8;
              sched_ctl->tbcnt[1][harq_pid] = 0;
            }
          } else {    // both NAK/DTX
            sched_ctl->round[1][harq_pid]++;

            if (sched_ctl->round[1][harq_pid] == 4) {
              sched_ctl->round[1][harq_pid] = 8;
              sched_ctl->tbcnt[1][harq_pid] = 0;
            }
4488
          }
4489 4490
        }     // A=4 both serving cells have 2 TBs

4491
#endif
4492 4493 4494
        break;

      case 2:   // Format 3
4495
        AssertFatal(numCC > 2, "Should not receive harq indication with FDD format 3 with %d < 3 active CCs\n",
4496 4497 4498
                    numCC);

        for (i = 0, j = 0; i < numCC; i++) {
4499 4500
          if (sched_ctl->round[i][harq_pid] < 8) {
            if (tmode[i] == 1 || tmode[i] == 2 || tmode[0] == 5 || tmode[0] == 6 || tmode[0] == 7) {
4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511
              if (pdu[j] == 1) {
                sched_ctl->round[i][harq_pid] = 8;
                sched_ctl->tbcnt[i][harq_pid] = 0;
              } else if (pdu[j] == 2) {
                sched_ctl->round[i][harq_pid]++;

                if (sched_ctl->round[i][harq_pid] == 4) {
                  sched_ctl->round[i][harq_pid] = 8;
                  sched_ctl->tbcnt[i][harq_pid] = 0;
                }
              } else
4512
                AssertFatal(1 == 0, "Illegal harq_ack value for CC %d harq_pid %d (%d) UE %d/%x\n",
4513 4514 4515 4516
                            i,
                            harq_pid,
                            pdu[j],
                            UE_id,
4517
                            rnti);
4518 4519 4520

              j++;
            } else if (spatial_bundling == 0) {
4521
              if (sched_ctl->tbcnt[i][harq_pid] == 2 && pdu[j] == 1 && pdu[j + 1] == 1) {
4522 4523
                sched_ctl->round[i][harq_pid] = 8;
                sched_ctl->tbcnt[i][harq_pid] = 0;
4524
              } else if (sched_ctl->tbcnt[i][harq_pid] == 2 && pdu[j] == 1 && pdu[j + 1] == 2) {
4525 4526 4527 4528 4529 4530 4531
                sched_ctl->round[i][harq_pid]++;
                sched_ctl->tbcnt[i][harq_pid] = 1;

                if (sched_ctl->round[i][harq_pid] == 4) {
                  sched_ctl->round[i][harq_pid] = 8;
                  sched_ctl->tbcnt[i][harq_pid] = 0;
                }
4532
              } else if (sched_ctl->tbcnt[i][harq_pid] == 2 && pdu[j] == 2 && pdu[j + 1] == 1) {
4533 4534 4535 4536 4537 4538 4539
                sched_ctl->round[i][harq_pid]++;
                sched_ctl->tbcnt[i][harq_pid] = 1;

                if (sched_ctl->round[i][harq_pid] == 4) {
                  sched_ctl->round[i][harq_pid] = 8;
                  sched_ctl->tbcnt[i][harq_pid] = 0;
                }
4540
              } else if (sched_ctl->tbcnt[i][harq_pid] == 2 && pdu[j] == 2 && pdu[j + 1] == 2) {
4541 4542 4543 4544 4545 4546 4547
                sched_ctl->round[i][harq_pid]++;

                if (sched_ctl->round[i][harq_pid] == 4) {
                  sched_ctl->round[i][harq_pid] = 8;
                  sched_ctl->tbcnt[i][harq_pid] = 0;
                }
              } else
4548
                AssertFatal(1 == 0, "Illegal combination for CC %d harq_pid %d (%d,%d,%d) UE %d/%x\n",
4549
                            i,
4550
                            harq_pid,
4551
                            sched_ctl->tbcnt[i][harq_pid],
4552 4553 4554
                            pdu[j],
                            pdu[j + 1],
                            UE_id,
4555
                            rnti);
4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568

              j += 2;
            } else if (spatial_bundling == 1) {
              if (pdu[j] == 1) {
                sched_ctl->round[i][harq_pid] = 8;
                sched_ctl->tbcnt[i][harq_pid] = 0;
              } else if (pdu[j] == 2) {
                sched_ctl->round[i][harq_pid]++;

                if (sched_ctl->round[i][harq_pid] == 4) {
                  sched_ctl->round[i][harq_pid] = 8;
                  sched_ctl->tbcnt[i][harq_pid] = 0;
                }
4569 4570
              } else {
                AssertFatal(1 == 0, "Illegal hack_nak value %d for CC %d harq_pid %d UE %d/%x\n",
4571 4572 4573 4574
                            pdu[j],
                            i,
                            harq_pid,
                            UE_id,
4575
                            rnti);
4576
              }
frtabu's avatar
frtabu committed
4577

4578
              j++;
4579 4580
            } else {
              AssertFatal(1 == 0, "Illegal value for spatial_bundling %d\n",
4581
                          spatial_bundling);
4582 4583 4584
            }
          }
        }
4585 4586 4587 4588

        break;

      case 3:   // Format 4
4589
        AssertFatal(1 == 0, "Should not receive harq indication with Format 4\n");
4590 4591 4592
        break;

      case 4:   // Format 5
4593
        AssertFatal(1 == 0, "Should not receive harq indication with Format 5\n");
4594
        break;
4595
    }
4596
  }
frtabu's avatar
frtabu committed
4597

4598
  return;
4599 4600
}

4601
//------------------------------------------------------------------------------
4602
void
4603 4604
extract_pucch_csi(module_id_t mod_idP,
                  int CC_idP,
4605
                  int UE_id,
4606
                  frame_t frameP,
4607
                  sub_frame_t subframeP,
4608 4609
                  uint8_t *pdu,
                  uint8_t length)
4610
//------------------------------------------------------------------------------
Cedric Roux's avatar
Cedric Roux committed
4611
{
4612
  UE_list_t *UE_list = &RC.mac[mod_idP]->UE_list;
4613
  UE_sched_ctrl_t *sched_ctl = &UE_list->UE_sched_ctrl[UE_id];
4614 4615 4616 4617 4618
  COMMON_channels_t *cc = &RC.mac[mod_idP]->common_channels[CC_idP];
  int no_pmi;
  uint8_t Ltab[6] = { 0, 2, 4, 4, 4, 4 };
  uint8_t Jtab[6] = { 0, 2, 2, 3, 4, 4 };
  int feedback_cnt;
4619
  AssertFatal(UE_list->UE_template[CC_idP][UE_id].physicalConfigDedicated != NULL, "physicalConfigDedicated is null for UE %d\n",
4620
              UE_id);
4621
  AssertFatal(UE_list->UE_template[CC_idP][UE_id].physicalConfigDedicated->cqi_ReportConfig != NULL, "cqi_ReportConfig is null for UE %d\n",
4622
              UE_id);
4623 4624
  struct LTE_CQI_ReportPeriodic *cqi_ReportPeriodic = UE_list->UE_template[CC_idP][UE_id].physicalConfigDedicated->cqi_ReportConfig->cqi_ReportPeriodic;
  AssertFatal(cqi_ReportPeriodic != NULL, "cqi_ReportPeriodic is null for UE %d\n",
4625
              UE_id);
4626
  // determine feedback mode
4627
  AssertFatal(cqi_ReportPeriodic->present != LTE_CQI_ReportPeriodic_PR_NOTHING, "cqi_ReportPeriodic->present == LTE_CQI_ReportPeriodic_PR_NOTHING!\n");
4628
  AssertFatal(cqi_ReportPeriodic->choice.setup.cqi_FormatIndicatorPeriodic.present != LTE_CQI_ReportPeriodic__setup__cqi_FormatIndicatorPeriodic_PR_NOTHING,
4629
              "cqi_ReportPeriodic->cqi_FormatIndicatorPeriodic.choice.setup.present == LTE_CQI_ReportPeriodic__setup__cqi_FormatIndicatorPeriodic_PR_NOTHING!\n");
4630 4631 4632
  uint16_t Npd, N_OFFSET_CQI;
  int H, K, bandwidth_part, L, Lmask;
  int ri = sched_ctl->periodic_ri_received[CC_idP];
4633 4634 4635 4636
  get_csi_params(cc,
                 cqi_ReportPeriodic,
                 &Npd,
                 &N_OFFSET_CQI,
4637
                 &H);
4638 4639 4640 4641 4642
  K = (H - 1) / Jtab[cc->mib->message.dl_Bandwidth];
  L = Ltab[cc->mib->message.dl_Bandwidth];
  Lmask = L - 1;
  feedback_cnt = (((frameP * 10) + subframeP) / Npd) % H;

4643 4644
  if (feedback_cnt > 0) bandwidth_part = (feedback_cnt - 1) % K;
  else bandwidth_part = 0;
4645

4646 4647
  switch (get_tmode(mod_idP,
                    CC_idP,
4648
                    UE_id)) {
4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660
    case 1:
    case 2:
    case 3:
    case 7:
      no_pmi = 1;
      break;

    case 4:
    case 5:
    case 6:
      no_pmi = 0;
      break;
4661

4662 4663 4664
    default:
      // note: need to check TM8-10 without PMI/RI or with 1 antenna port (see Section 5.2.3.3.1 from 36.213)
      no_pmi = 0;
4665
      break;
frtabu's avatar
frtabu committed
4666
  }
4667

4668
  if (cqi_ReportPeriodic->choice.setup.cqi_FormatIndicatorPeriodic.present == LTE_CQI_ReportPeriodic__setup__cqi_FormatIndicatorPeriodic_PR_widebandCQI ||
4669
      feedback_cnt == 0) {
4670
    // Note: This implements only Tables: 5.3.3.1-1,5.3.3.1-1A and 5.3.3.1-2 from 36.213 (1,2,4 antenna ports Wideband CQI/PMI)
4671
    if (no_pmi == 1) {  // get spatial_diffcqi if needed
4672
      sched_ctl->periodic_wideband_cqi[CC_idP] = pdu[0] & 0xF;
4673 4674
      sched_ctl->periodic_wideband_spatial_diffcqi[CC_idP] = (pdu[0] >> 4) & 7;
    } else if (cc->p_eNB == 2 && ri == 1) {
4675 4676 4677
      // p=2 Rank 1 wideband CQI/PMI 6 bits
      sched_ctl->periodic_wideband_cqi[CC_idP] = pdu[0] & 0xF;
      sched_ctl->periodic_wideband_pmi[CC_idP] = (pdu[0] >> 4) & 3;
4678
    } else if (cc->p_eNB == 2 && ri > 1) {
4679 4680
      // p=2 Rank 2 wideband CQI/PMI 8 bits
      sched_ctl->periodic_wideband_cqi[CC_idP] = pdu[0] & 0xF;
4681
      sched_ctl->periodic_wideband_spatial_diffcqi[CC_idP] = (pdu[0] >> 4) & 7;
4682
      sched_ctl->periodic_wideband_pmi[CC_idP] = (pdu[0] >> 7) & 1;
4683
    } else if (cc->p_eNB == 4 && ri == 1) {
4684 4685
      // p=4 Rank 1 wideband CQI/PMI 8 bits
      sched_ctl->periodic_wideband_cqi[CC_idP] = pdu[0] & 0xF;
4686 4687
      sched_ctl->periodic_wideband_pmi[CC_idP] = (pdu[0] >> 4) & 0x0F;
    } else if (cc->p_eNB == 4 && ri > 1) {
4688 4689
      // p=4 Rank 2 wideband CQI/PMI 11 bits
      sched_ctl->periodic_wideband_cqi[CC_idP] = pdu[0] & 0xF;
4690
      sched_ctl->periodic_wideband_spatial_diffcqi[CC_idP] = (pdu[0] >> 4) & 7;
4691 4692
      sched_ctl->periodic_wideband_pmi[CC_idP] = (pdu[0] >> 7) & 0xF;
    } else
4693
      AssertFatal(1 == 0, "illegal combination p %d, ri %d, no_pmi %d\n",
4694 4695
                  cc->p_eNB,
                  ri,
4696
                  no_pmi);
4697 4698 4699 4700 4701 4702 4703 4704
  } else if (cqi_ReportPeriodic->choice.setup.cqi_FormatIndicatorPeriodic.present == LTE_CQI_ReportPeriodic__setup__cqi_FormatIndicatorPeriodic_PR_subbandCQI) {
    // This is Table 5.2.3.3.2-2 for 36.213
    if (ri == 1) {
      //4+Ltab[cc->mib->message.dl_Bandwidth] bits
      sched_ctl->periodic_subband_cqi[CC_idP][(bandwidth_part * L) +((pdu[0] >> 4) & Lmask)] = pdu[0] & 0xF;
    } else if (ri > 1) {
      //7+Ltab[cc->mib->message.dl_Bandwidth] bits;
      sched_ctl->periodic_subband_spatial_diffcqi[CC_idP][(bandwidth_part * L) + ((pdu[0] >> 7) & Lmask)] = (pdu[0] >> 4) & 7;
4705
      sched_ctl->periodic_subband_cqi[CC_idP][(bandwidth_part * L) + ((pdu[0] >> 7) & Lmask)] = pdu[0] & 0xF;
4706
    }
4707
  }
frtabu's avatar
frtabu committed
4708

4709
  return;
4710 4711
}

4712
//------------------------------------------------------------------------------
4713
void
4714 4715
extract_pusch_csi(module_id_t mod_idP,
                  int CC_idP,
4716
                  int UE_id,
4717
                  frame_t frameP,
4718
                  sub_frame_t subframeP,
4719 4720
                  uint8_t *pdu,
                  uint8_t length)
4721
//------------------------------------------------------------------------------
4722
{
4723 4724
  UE_list_t *UE_list = &RC.mac[mod_idP]->UE_list;
  COMMON_channels_t *cc = &RC.mac[mod_idP]->common_channels[CC_idP];
4725
  UE_sched_ctrl_t *sched_ctl = &UE_list->UE_sched_ctrl[UE_id];
4726 4727 4728 4729 4730 4731 4732 4733
  int Ntab[6] = { 0, 4, 7, 9, 10, 13 };
  int Ntab_uesel[6] = { 0, 8, 13, 17, 19, 25 };
  int Ltab_uesel[6] = { 0, 6, 9, 13, 15, 18 };
  int Mtab_uesel[6] = { 0, 1, 3, 5, 6, 6 };
  int v[6];
  int i;
  uint64_t p = *(uint64_t *) pdu;
  int curbyte, curbit;
4734
  AssertFatal(UE_list->UE_template[CC_idP][UE_id].physicalConfigDedicated != NULL, "physicalConfigDedicated is null for UE %d\n",
4735
              UE_id);
4736
  AssertFatal(UE_list->UE_template[CC_idP][UE_id].physicalConfigDedicated->cqi_ReportConfig != NULL, "cqi_ReportConfig is null for UE %d\n",
4737
              UE_id);
4738
  LTE_CQI_ReportModeAperiodic_t *cqi_ReportModeAperiodic
4739
    = UE_list->UE_template[CC_idP][UE_id].physicalConfigDedicated->cqi_ReportConfig->cqi_ReportModeAperiodic;
4740
  AssertFatal(cqi_ReportModeAperiodic  != NULL, "cqi_ReportModeAperiodic is null for UE %d\n",
4741
              UE_id);
4742 4743 4744 4745 4746 4747 4748 4749
  int N = Ntab[cc->mib->message.dl_Bandwidth];
  int tmode = get_tmode(mod_idP, CC_idP, UE_id);
  int ri = sched_ctl->aperiodic_ri_received[CC_idP];
  int r, diffcqi0 = 0, diffcqi1 = 0, pmi_uesel = 0;
  int bw = cc->mib->message.dl_Bandwidth;
  int m;

  switch (*cqi_ReportModeAperiodic) {
4750 4751 4752
    case LTE_CQI_ReportModeAperiodic_rm12:
      AssertFatal(0 == 1, "to be fixed, don't use p but pdu directly\n");
      // wideband multiple PMI (TM4/6), Table 5.2.2.6.1-1 (for TM4/6)
4753
      AssertFatal(tmode == 4 || tmode == 6 || tmode == 8 || tmode == 9 || tmode == 10, "Illegal transmission mode %d for CQI_ReportModeAperiodic_rm12\n",
4754 4755 4756
                  tmode);

      if (tmode <= 6) { //Table 5.2.2.6.1-1 36.213
4757 4758
        if (ri == 1 && cc->p_eNB == 2) {
          sched_ctl->aperiodic_wideband_cqi0[CC_idP] = (uint8_t) (p & 0x0F);
4759 4760 4761 4762 4763 4764 4765 4766
          p >>= 4;

          for (i = 0; i < N; i++) {
            sched_ctl->aperiodic_subband_pmi[CC_idP][i] = (uint8_t) (p & 0x03);
            p >>= 2;
          }
        }

4767 4768
        if (ri == 2 && cc->p_eNB == 2) {
          sched_ctl->aperiodic_wideband_cqi0[CC_idP] = (uint8_t) (p & 0x0F);
4769
          p >>= 4;
4770
          sched_ctl->aperiodic_wideband_cqi1[CC_idP] = (uint8_t) (p & 0x0F);
4771 4772 4773 4774 4775 4776 4777 4778
          p >>= 4;

          for (i = 0; i < N; i++) {
            sched_ctl->aperiodic_subband_pmi[CC_idP][i] = (uint8_t) (p & 0x01);
            p >>= 1;
          }
        }

4779 4780
        if (ri == 1 && cc->p_eNB == 4) {
          sched_ctl->aperiodic_wideband_cqi0[CC_idP] = (uint8_t) (p & 0x0F);
4781 4782 4783 4784 4785 4786 4787 4788
          p >>= 4;

          for (i = 0; i < N; i++) {
            sched_ctl->aperiodic_subband_pmi[CC_idP][i] = (uint8_t) (p & 0x03);
            p >>= 4;
          }
        }

4789 4790
        if (ri == 2 && cc->p_eNB == 4) {
          sched_ctl->aperiodic_wideband_cqi0[CC_idP] = (uint8_t) (p & 0x0F);
4791
          p >>= 4;
4792
          sched_ctl->aperiodic_wideband_cqi1[CC_idP] = (uint8_t) (p & 0x0F);
4793 4794 4795 4796 4797 4798 4799 4800 4801 4802
          p >>= 4;

          for (i = 0; i < N; i++) {
            sched_ctl->aperiodic_subband_pmi[CC_idP][i] = (uint8_t) (p & 0x01);
            p >>= 4;
          }
        }
      }     // if (tmode <= 6) { //Table 5.2.2.6.1-1 36.213
      else {
        AssertFatal(1 == 0, "support for TM 8-10 to be done\n");
4803
      }
4804

4805 4806 4807 4808 4809
      break;

    case LTE_CQI_ReportModeAperiodic_rm20:
      AssertFatal(0 == 1, "to be fixed, don't use p but pdu directly\n");
      // UE-selected subband CQI no PMI (TM1/2/3/7) , Table 5.2.2.6.3-1 from 36.213
4810
      AssertFatal(tmode == 1 || tmode == 2 || tmode == 3 || tmode == 7, "Illegal transmission mode %d for CQI_ReportModeAperiodic_rm20\n",
4811 4812
                  tmode);
      sched_ctl->aperiodic_wideband_cqi0[CC_idP] = (uint8_t) (p & 0x0F);
4813
      p >>= 4;
4814
      diffcqi0 = (uint8_t) (p & 0x03);
4815
      p >>= 2;
4816
      r = (uint8_t) (p & ((1 >> Ltab_uesel[bw]) - 1));
4817 4818 4819
      reverse_index(Ntab_uesel[bw],
                    Mtab_uesel[bw],
                    r,
4820
                    v);
4821

4822
      for (m = 0; m < Mtab_uesel[bw]; m++) {
4823
        sched_ctl->aperiodic_subband_diffcqi0[CC_idP][v[m]] = diffcqi0;
4824
      }
4825 4826 4827 4828 4829 4830

      break;

    case LTE_CQI_ReportModeAperiodic_rm22:
      AssertFatal(0 == 1, "to be fixed, don't use p but pdu directly\n");
      // UE-selected subband CQI multiple PMI (TM4/6) Table 5.2.2.6.3-2 from 36.213
4831
      AssertFatal(tmode == 4 || tmode == 6 || tmode == 8 || tmode == 9 || tmode == 10, "Illegal transmission mode %d for CQI_ReportModeAperiodic_rm22\n",
4832 4833
                  tmode);
      sched_ctl->aperiodic_wideband_cqi0[CC_idP] = (uint8_t) (p & 0x0F);
4834
      p >>= 4;
4835 4836 4837 4838 4839 4840 4841 4842 4843
      diffcqi0 = (uint8_t) (p & 0x03);
      p >>= 2;

      if (ri > 1) {
        sched_ctl->aperiodic_wideband_cqi1[CC_idP] =
          (uint8_t) (p & 0x0F);
        p >>= 4;
        diffcqi1 = (uint8_t) (p & 0x03);
        p >>= 2;
4844
      }
4845 4846 4847

      r = (uint8_t) (p & ((1 >> Ltab_uesel[bw]) - 1));
      p >>= Ltab_uesel[bw];
4848 4849 4850
      reverse_index(Ntab_uesel[bw],
                    Mtab_uesel[bw],
                    r,
4851
                    v);
4852

4853
      if (ri == 1 && cc->p_eNB == 2) {
4854 4855 4856
        pmi_uesel = p & 0x3;
        p >>= 2;
        sched_ctl->aperiodic_wideband_pmi[CC_idP] = p & 0x3;
4857
      } else if (ri == 2 && cc->p_eNB == 2) {
4858 4859 4860 4861 4862 4863 4864
        pmi_uesel = p & 0x1;
        p >>= 1;
        sched_ctl->aperiodic_wideband_pmi[CC_idP] = p & 0x1;
      } else if (cc->p_eNB == 4) {
        pmi_uesel = p & 0x0F;
        p >>= 4;
        sched_ctl->aperiodic_wideband_pmi[CC_idP] = p & 0x0F;
4865
      }
4866 4867 4868 4869

      for (m = 0; m < Mtab_uesel[bw]; m++) {
        sched_ctl->aperiodic_subband_diffcqi0[CC_idP][v[m]] = diffcqi0;

4870
        if (ri > 1) sched_ctl->aperiodic_subband_diffcqi1[CC_idP][v[m]] = diffcqi1;
4871 4872

        sched_ctl->aperiodic_subband_pmi[CC_idP][v[m]] = pmi_uesel;
4873
      }
4874 4875 4876 4877 4878

      break;

    case LTE_CQI_ReportModeAperiodic_rm30:
      //subband CQI no PMI (TM1/2/3/7)
4879
      AssertFatal(tmode == 1 || tmode == 2 || tmode == 3 || tmode == 7, "Illegal transmission mode %d for CQI_ReportModeAperiodic_rm30\n",
4880 4881 4882 4883 4884
                  tmode);
      sched_ctl->aperiodic_wideband_cqi0[CC_idP] = pdu[0] >> 4;
      curbyte = 0;
      curbit = 3;

4885
      for (i = 0; i < N; i++) {
4886 4887 4888 4889 4890 4891 4892 4893
        sched_ctl->aperiodic_subband_diffcqi0[CC_idP][i] =
          (pdu[curbyte] >> (curbit - 1)) & 0x03;
        curbit -= 2;

        if (curbit < 0) {
          curbit = 7;
          curbyte++;
        }
4894
      }
4895

4896
      sched_ctl->dl_cqi[CC_idP] = sched_ctl->aperiodic_wideband_cqi0[CC_idP];
4897 4898 4899 4900 4901
      break;

    case LTE_CQI_ReportModeAperiodic_rm31:
      AssertFatal(0 == 1, "to be fixed, don't use p but pdu directly\n");
      //subband CQI single PMI (TM4/5/6)
4902
      AssertFatal(tmode == 4 || tmode == 5 || tmode == 6 || tmode == 8 || tmode == 9 || tmode == 10,
4903 4904 4905
                  "Illegal transmission mode %d for CQI_ReportModeAperiodic_rm31\n",
                  tmode);

4906 4907
      if (ri == 1 && cc->p_eNB == 2) {
        sched_ctl->aperiodic_wideband_cqi0[CC_idP] = (uint8_t) (p & 0x0F);
4908 4909 4910
        p >>= 4;

        for (i = 0; i < N; i++) {
4911
          sched_ctl->aperiodic_subband_diffcqi0[CC_idP][i] = (uint8_t) (p & 0x03);
4912 4913 4914 4915
          p >>= 2;
        }

        sched_ctl->aperiodic_wideband_pmi[CC_idP] = p & 0x03;
4916
      }
4917

4918 4919
      if (ri == 2 && cc->p_eNB == 2) {
        sched_ctl->aperiodic_wideband_cqi0[CC_idP] = (uint8_t) (p & 0x0F);
4920 4921 4922
        p >>= 4;

        for (i = 0; i < N; i++) {
4923
          sched_ctl->aperiodic_subband_pmi[CC_idP][i] = (uint8_t) (p & 0x01);
4924 4925 4926
          p >>= 1;
        }

4927
        sched_ctl->aperiodic_wideband_cqi1[CC_idP] = (uint8_t) (p & 0x0F);
4928 4929 4930
        p >>= 4;

        for (i = 0; i < N; i++) {
4931
          sched_ctl->aperiodic_subband_pmi[CC_idP][i] = (uint8_t) (p & 0x01);
4932 4933 4934 4935
          p >>= 1;
        }

        sched_ctl->aperiodic_wideband_pmi[CC_idP] = p & 0x01;
4936
      }
4937

4938 4939
      if (ri == 1 && cc->p_eNB == 4) {
        sched_ctl->aperiodic_wideband_cqi0[CC_idP] = (uint8_t) (p & 0x0F);
4940 4941 4942
        p >>= 4;

        for (i = 0; i < N; i++) {
4943
          sched_ctl->aperiodic_subband_diffcqi0[CC_idP][i] = (uint8_t) (p & 0x03);
4944 4945 4946 4947
          p >>= 2;
        }

        sched_ctl->aperiodic_wideband_pmi[CC_idP] = p & 0x0F;
4948 4949
      }

4950 4951
      if (ri > 1 && cc->p_eNB == 4) { // Note : 64 bits for 20 MHz
        sched_ctl->aperiodic_wideband_cqi0[CC_idP] = (uint8_t) (p & 0x0F);
4952 4953 4954
        p >>= 4;

        for (i = 0; i < N; i++) {
4955
          sched_ctl->aperiodic_subband_pmi[CC_idP][i] = (uint8_t) (p & 0x01);
4956 4957 4958
          p >>= 1;
        }

4959
        sched_ctl->aperiodic_wideband_cqi1[CC_idP] = (uint8_t) (p & 0x0F);
4960 4961 4962
        p >>= 4;

        for (i = 0; i < N; i++) {
4963
          sched_ctl->aperiodic_subband_pmi[CC_idP][i] = (uint8_t) (p & 0x01);
4964 4965 4966 4967 4968 4969 4970
          p >>= 2;
        }

        sched_ctl->aperiodic_wideband_pmi[CC_idP] = p & 0x0F;
      }

      break;
4971
#if (LTE_RRC_VERSION >= MAKE_VERSION(12, 5, 0))
frtabu's avatar
frtabu committed
4972

4973
    case LTE_CQI_ReportModeAperiodic_rm32_v1250:
4974
      AssertFatal(tmode == 4 || tmode == 5 || tmode == 6 || tmode == 8 || tmode == 9 || tmode == 10,
4975 4976 4977 4978
                  "Illegal transmission mode %d for CQI_ReportModeAperiodic_rm32\n",
                  tmode);
      AssertFatal(1 == 0, "CQI_ReportModeAperiodic_rm32 to be done\n");
      break;
4979
#endif
4980
#if (LTE_RRC_VERSION >= MAKE_VERSION(13, 1, 0))
frtabu's avatar
frtabu committed
4981

4982
    case LTE_CQI_ReportModeAperiodic_rm10_v1310:
4983
      AssertFatal(tmode == 1 || tmode == 2 || tmode == 3 || tmode == 7, "Illegal transmission mode %d for CQI_ReportModeAperiodic_rm10\n",
4984 4985 4986 4987 4988
                  tmode);
      AssertFatal(1 == 0, "CQI_ReportModeAperiodic_rm10 to be done\n");
      break;

    case LTE_CQI_ReportModeAperiodic_rm11_v1310:
4989
      AssertFatal(tmode == 4 || tmode == 5 || tmode == 6 || tmode == 8 || tmode == 9 || tmode == 10,
4990 4991 4992 4993
                  "Illegal transmission mode %d for CQI_ReportModeAperiodic_rm11\n",
                  tmode);
      AssertFatal(1 == 0, "CQI_ReportModeAperiodic_rm11 to be done\n");
      break;
4994
#endif /* #if (LTE_RRC_VERSION >= MAKE_VERSION(13, 1, 0)) */
4995
  }
frtabu's avatar
frtabu committed
4996

4997
  return;
4998 4999
}

5000
//------------------------------------------------------------------------------
5001
void
5002 5003
cqi_indication(module_id_t mod_idP,
               int CC_idP,
5004
               frame_t frameP,
5005
               sub_frame_t subframeP,
5006
               rnti_t rntiP,
5007
               nfapi_cqi_indication_rel9_t *rel9,
5008
               uint8_t *pdu,
5009
               nfapi_ul_cqi_information_t *ul_cqi_information)
5010
//------------------------------------------------------------------------------
Cedric Roux's avatar
Cedric Roux committed
5011
{
5012 5013
  int UE_id = find_UE_id(mod_idP, rntiP);
  UE_list_t *UE_list = &RC.mac[mod_idP]->UE_list;
5014

5015 5016 5017 5018
  if (UE_id == -1) {
    LOG_W(MAC, "cqi_indication: UE %x not found\n", rntiP);
    return;
  }
5019

5020
  UE_sched_ctrl_t *sched_ctl = &UE_list->UE_sched_ctrl[UE_id];
5021

5022
  if (UE_id >= 0) {
5023 5024 5025 5026
    LOG_D(MAC,"%s() UE_id:%d channel:%d cqi:%d\n",
          __FUNCTION__,
          UE_id,
          ul_cqi_information->channel,
5027
          ul_cqi_information->ul_cqi);
5028

5029
    if (ul_cqi_information->channel == 0) { // PUCCH
5030
      // extract pucch csi information before changing RI information
5031 5032 5033 5034
      extract_pucch_csi(mod_idP,
                        CC_idP,
                        UE_id,
                        frameP,
5035
                        subframeP,
5036
                        pdu, rel9->length);
5037
      memcpy((void *) sched_ctl->periodic_ri_received,
5038
             (void *) rel9->ri,
5039
             rel9->number_of_cc_reported);
5040 5041
      // SNR for PUCCH2
      sched_ctl->pucch2_snr[CC_idP] = ul_cqi_information->ul_cqi;
5042
    } else {    //PUSCH
5043
      memcpy((void *) sched_ctl->aperiodic_ri_received,
5044
             (void *) rel9->ri,
5045
             rel9->number_of_cc_reported);
5046 5047 5048 5049
      extract_pusch_csi(mod_idP,
                        CC_idP,
                        UE_id,
                        frameP,
5050
                        subframeP,
5051
                        pdu,
5052 5053 5054 5055 5056
                        rel9->length);
      LOG_D(MAC,"Frame %d Subframe %d update CQI:%d\n",
            frameP,
            subframeP,
            sched_ctl->dl_cqi[CC_idP]);
5057 5058
      sched_ctl->cqi_req_flag &= (~(1 << subframeP));
      sched_ctl->cqi_received = 1;
5059
    }
5060 5061 5062 5063 5064

    // timing advance
    sched_ctl->timing_advance = rel9->timing_advance;
    sched_ctl->timing_advance_r9 = rel9->timing_advance_r9;
  }
frtabu's avatar
frtabu committed
5065

5066
  return;
5067 5068
}

5069
//------------------------------------------------------------------------------
5070
void
5071 5072
SR_indication(module_id_t mod_idP,
              int cc_idP,
5073
              frame_t frameP,
5074 5075 5076
              sub_frame_t subframeP,
              rnti_t rntiP,
              uint8_t ul_cqi)
5077
//------------------------------------------------------------------------------
5078
{
5079 5080
  T(T_ENB_MAC_SCHEDULING_REQUEST,
    T_INT(mod_idP),
5081
    T_INT(cc_idP),
5082 5083
    T_INT(frameP),
    T_INT(subframeP),
5084
    T_INT(rntiP));
5085 5086
  int UE_id = find_UE_id(mod_idP, rntiP);
  UE_list_t *UE_list = &RC.mac[mod_idP]->UE_list;
5087
  UE_sched_ctrl_t *UE_scheduling_ctrl = NULL;
Cedric Roux's avatar
Cedric Roux committed
5088

5089
  if (UE_id != -1) {
5090 5091
    UE_scheduling_ctrl = &(UE_list->UE_sched_ctrl[UE_id]);

5092
    if ((UE_scheduling_ctrl->cdrx_configured == TRUE) && 
5093
        (UE_scheduling_ctrl->dci0_ongoing_timer > 0)  &&
5094
        (UE_scheduling_ctrl->dci0_ongoing_timer < 8)) {
5095 5096
      LOG_D(MAC, "[eNB %d][SR %x] Frame %d subframeP %d Signaling SR for UE %d on CC_id %d.  \
                  The SR is not set do to ongoing DCI0 with CDRX activated\n",
5097 5098 5099 5100 5101
            mod_idP,
            rntiP,
            frameP,
            subframeP,
            UE_id,
5102
            cc_idP);
5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116
    } else {
      if (mac_eNB_get_rrc_status(mod_idP, UE_RNTI(mod_idP, UE_id)) <  RRC_CONNECTED) {
        LOG_D(MAC, "[eNB %d][SR %x] Frame %d subframeP %d Signaling SR for UE %d on CC_id %d\n",
              mod_idP,
              rntiP,
              frameP,
              subframeP,
              UE_id,
              cc_idP);
      }
      UE_list->UE_template[cc_idP][UE_id].ul_SR = 1;
      UE_list->UE_template[cc_idP][UE_id].ul_active = TRUE;
      VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_SR_INDICATION, 1);
      VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_SR_INDICATION, 0);
5117
    }
5118
  } else {
5119
    LOG_D(MAC, "[eNB %d][SR %x] Frame %d subframeP %d Signaling SR for UE %d (unknown UE_id) on CC_id %d\n",
5120 5121 5122 5123 5124
          mod_idP,
          rntiP,
          frameP,
          subframeP,
          UE_id,
5125
          cc_idP);
5126
  }
5127
  
5128
  return;
5129 5130
}

5131
//------------------------------------------------------------------------------
5132
void
5133 5134
UL_failure_indication(module_id_t mod_idP,
                      int cc_idP,
5135
                      frame_t frameP,
5136 5137
                      rnti_t rntiP,
                      sub_frame_t subframeP)
5138
//------------------------------------------------------------------------------
5139
{
5140 5141
  int UE_id = find_UE_id(mod_idP, rntiP);
  UE_list_t *UE_list = &RC.mac[mod_idP]->UE_list;
5142

5143
  if (UE_id != -1) {
5144
    LOG_D(MAC, "[eNB %d][UE %d/%x] Frame %d subframeP %d Signaling UL Failure for UE %d on CC_id %d (timer %d)\n",
5145 5146 5147 5148 5149 5150
          mod_idP,
          UE_id,
          rntiP,
          frameP,
          subframeP,
          UE_id,
5151
          cc_idP,
5152 5153
          UE_list->UE_sched_ctrl[UE_id].ul_failure_timer);

5154
    if (UE_list->UE_sched_ctrl[UE_id].ul_failure_timer == 0) UE_list->UE_sched_ctrl[UE_id].ul_failure_timer = 1;
5155 5156 5157
  } else {
    //     AssertFatal(0, "find_UE_id(%u,rnti %d) not found", enb_mod_idP, rntiP);
    //    AssertError(0, 0, "Frame %d: find_UE_id(%u,rnti %d) not found\n", frameP, enb_mod_idP, rntiP);
5158
    LOG_W(MAC, "[eNB %d][SR %x] Frame %d subframeP %d Signaling UL Failure for UE %d (unknown UEid) on CC_id %d\n",
5159 5160 5161 5162 5163
          mod_idP,
          rntiP,
          frameP,
          subframeP,
          UE_id,
5164
          cc_idP);
5165
  }
5166
}
5167

5168
//------------------------------------------------------------------------------
5169
static int
5170
nack_or_dtx_reported(COMMON_channels_t *cc,
5171
                     nfapi_harq_indication_pdu_t *harq_pdu)
5172
//------------------------------------------------------------------------------
5173 5174 5175 5176
{
  int i;

  if (cc->tdd_Config) {
Raymond Knopp's avatar
Raymond Knopp committed
5177
    nfapi_harq_indication_tdd_rel13_t *hi = &harq_pdu->harq_indication_tdd_rel13;
5178

5179 5180
    for (i = 0; i < hi->number_of_ack_nack; i++) {
      if (hi->harq_data[i].bundling.value_0 != 1) //only bundling is used for tdd for now
Raymond Knopp's avatar
Raymond Knopp committed
5181
        return 1;
5182
    }
frtabu's avatar
frtabu committed
5183

Raymond Knopp's avatar
Raymond Knopp committed
5184
    return 0;
5185
  }
5186

5187
  nfapi_harq_indication_fdd_rel13_t *hi = &harq_pdu->harq_indication_fdd_rel13;
frtabu's avatar
frtabu committed
5188

5189
  for (i = 0; i < hi->number_of_ack_nack; i++) {
5190 5191
    if (hi->harq_tb_n[i] != 1)
      return 1;
5192
  }
frtabu's avatar
frtabu committed
5193

5194
  return 0;
5195
}
5196

5197
//------------------------------------------------------------------------------
5198
void
5199 5200
harq_indication(module_id_t mod_idP,
                int CC_idP,
5201
                frame_t frameP,
5202
                sub_frame_t subframeP,
5203
                nfapi_harq_indication_pdu_t *harq_pdu)
5204
//------------------------------------------------------------------------------
Cedric Roux's avatar
Cedric Roux committed
5205
{
5206 5207 5208 5209
  rnti_t rnti = harq_pdu->rx_ue_information.rnti;
  uint8_t ul_cqi = harq_pdu->ul_cqi_information.ul_cqi;
  uint8_t channel = harq_pdu->ul_cqi_information.channel;
  int UE_id = find_UE_id(mod_idP, rnti);
5210

5211
  if (UE_id == -1) {
5212
    LOG_W(MAC, "harq_indication: UE %x not found\n",
5213
          rnti);
5214 5215
    return;
  }
5216

5217
  UE_list_t *UE_list = &RC.mac[mod_idP]->UE_list;
5218
  UE_sched_ctrl_t *sched_ctl = &UE_list->UE_sched_ctrl[UE_id];
5219 5220
  COMMON_channels_t *cc = &RC.mac[mod_idP]->common_channels[CC_idP];
  // extract HARQ Information
5221

5222
  if (cc->tdd_Config) {
5223 5224 5225 5226
    extract_harq(mod_idP,
                 CC_idP,
                 UE_id,
                 frameP,
5227
                 subframeP,
5228 5229
                 (void *) &harq_pdu->harq_indication_tdd_rel13,
                 channel);
frtabu's avatar
frtabu committed
5230
  } else {
5231 5232 5233 5234
    extract_harq(mod_idP,
                 CC_idP,
                 UE_id,
                 frameP,
5235
                 subframeP,
5236 5237
                 (void *) &harq_pdu->harq_indication_fdd_rel13,
                 channel);
5238
  }
5239

5240
  /* don't care about cqi reporting if NACK/DTX is there */
5241
  if (channel == 0 && !nack_or_dtx_reported(cc,
frtabu's avatar
frtabu committed
5242
      harq_pdu)) {
5243 5244 5245
    sched_ctl->pucch1_snr[CC_idP] = ul_cqi;
    sched_ctl->pucch1_cqi_update[CC_idP] = 1;
  }
frtabu's avatar
frtabu committed
5246

5247
  return;
5248
}
5249 5250

// Flexran Slicing functions
5251
//------------------------------------------------------------------------------
5252 5253 5254
uint16_t
nb_rbs_allowed_slice(float rb_percentage,
                     int total_rbs)
5255
//------------------------------------------------------------------------------
5256
{
5257
  return (uint16_t) floor(rb_percentage * total_rbs);
5258 5259
}

5260
//------------------------------------------------------------------------------
5261 5262 5263 5264
int
ue_dl_slice_membership(module_id_t mod_id,
                       int UE_id,
                       int slice_idx)
5265
//------------------------------------------------------------------------------
5266
{
5267 5268 5269
  eNB_MAC_INST *eNB = RC.mac[mod_id];

  if (slice_idx < 0 || slice_idx >= eNB->slice_info.n_dl) {
5270
    LOG_W(MAC, "out of range slice index %d (slice ID %d)\n",
5271
          slice_idx,
5272
          eNB->slice_info.dl[slice_idx].id);
5273 5274
    return 0;
  }
5275

5276
  return eNB->UE_list.active[UE_id] == TRUE && eNB->UE_list.assoc_dl_slice_idx[UE_id] == slice_idx;
5277
}
5278

5279
//------------------------------------------------------------------------------
5280 5281 5282 5283
int
ue_ul_slice_membership(module_id_t mod_id,
                       int UE_id,
                       int slice_idx)
5284
//------------------------------------------------------------------------------
5285
{
5286 5287 5288
  eNB_MAC_INST *eNB = RC.mac[mod_id];

  if (slice_idx < 0 || slice_idx >= eNB->slice_info.n_ul) {
5289
    LOG_W(MAC, "out of range slice index %d (slice ID %d)\n",
5290
          slice_idx,
5291
          eNB->slice_info.dl[slice_idx].id);
5292
    return 0;
5293
  }
5294

5295
  return eNB->UE_list.active[UE_id] == TRUE && eNB->UE_list.assoc_ul_slice_idx[UE_id] == slice_idx;
5296
}