eNB_scheduler_primitives.c 157 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 39
#include "common/utils/LOG/log.h"
#include "common/utils/LOG/vcd_signal_dumper.h"
40 41 42 43
#include "UTIL/OPT/opt.h"
#include "OCG.h"
#include "OCG_extern.h"

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

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

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

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

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

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

extern RAN_CONTEXT_t RC;

65
int choose(int n, int k)
Cedric Roux's avatar
Cedric Roux committed
66
{
67 68 69
  int res = 1;
  int res2 = 1;
  int i;
70

71 72 73 74
  if (k > n)
    return (0);
  if (n == k)
    return (1);
75

76 77 78 79
  for (i = n; i > k; i--)
    res *= i;
  for (i = 2; i <= (n - k); i++)
    res2 *= i;
80

81
  return (res / res2);
82 83 84
}

// Patented algorithm from Yang et al, US Patent 2009, "Channel Quality Indexing and Reverse Indexing"
85 86
void reverse_index(int N, int M, int r, int *v)
{
87 88 89 90 91 92 93 94 95 96 97 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;
  while (i > 0 && r > 0) {
    IncreaseValue = choose(N - M + 1 - sumV - v[i - 1] + i - 2, i - 1);
    ThresholdValue = BaseValue + IncreaseValue;
    if (r >= ThresholdValue) {
      v[i - 1]++;
      BaseValue = ThresholdValue;
    } else {
      r = r - BaseValue;
      sumV += v[i - 1];
      i--;
      BaseValue = 0;
108
    }
109
  }
110
}
Cedric Roux's avatar
Cedric Roux committed
111 112 113

int to_prb(int dl_Bandwidth)
{
114
  int prbmap[6] = { 6, 15, 25, 50, 75, 100 };
115

116 117
  AssertFatal(dl_Bandwidth < 6, "dl_Bandwidth is 0..5\n");
  return (prbmap[dl_Bandwidth]);
118 119
}

Cedric Roux's avatar
Cedric Roux committed
120 121
int to_rbg(int dl_Bandwidth)
{
122
  int rbgmap[6] = { 6, 8, 13, 17, 19, 25 };
123

124 125
  AssertFatal(dl_Bandwidth < 6, "dl_Bandwidth is 0..5\n");
  return (rbgmap[dl_Bandwidth]);
126
}
127

128
int get_phich_resource_times6(COMMON_channels_t * cc)
Cedric Roux's avatar
Cedric Roux committed
129
{
130 131 132 133 134 135 136 137 138
  int phichmap[4] = { 1, 3, 6, 12 };
  AssertFatal(cc != NULL, "cc is null\n");
  AssertFatal(cc->mib != NULL, "cc->mib is null\n");
  AssertFatal((cc->mib->message.phich_Config.phich_Resource >= 0) &&
	      (cc->mib->message.phich_Config.phich_Resource < 4),
	      "phich_Resource %d not in 0..3\n",
	      (int) cc->mib->message.phich_Config.phich_Resource);

  return (phichmap[cc->mib->message.phich_Config.phich_Resource]);
139 140
}

141
uint16_t mac_computeRIV(uint16_t N_RB_DL, uint16_t RBstart, uint16_t Lcrbs)
Cedric Roux's avatar
Cedric Roux committed
142
{
143
  uint16_t RIV;
144

145 146
  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);
147

148
  return (RIV);
149 150
}

Cedric Roux's avatar
Cedric Roux committed
151 152
uint8_t getQm(uint8_t mcs)
{
153 154 155
  if (mcs < 10)      return (2);
  else if (mcs < 17) return (4);
  else               return (6);
156 157
}

158
void
159 160 161 162 163
get_Msg3alloc(COMMON_channels_t *cc,
	      sub_frame_t       current_subframe,
	      frame_t           current_frame, 
	      frame_t           *frame,
	      sub_frame_t       *subframe)
164
{
165
  // Fill in other TDD Configuration!!!!
166

167 168
  if (cc->tdd_Config == NULL) {	// FDD
    *subframe = current_subframe + 6;
169

170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
    if (*subframe > 9) {
      *subframe = *subframe - 10;
      *frame = (current_frame + 1) & 1023;
    } else {
      *frame = current_frame;
    }
  } else {			// TDD
    if (cc->tdd_Config->subframeAssignment == 1) {
      switch (current_subframe) {
	  
      case 0:
	*subframe = 7;
	*frame = current_frame;
	break;

      case 4:
	*subframe = 2;
	*frame = (current_frame + 1) & 1023;
	break;

      case 5:
	*subframe = 2;
	*frame = (current_frame + 1) & 1023;
	break;

      case 9:
	*subframe = 7;
	*frame = (current_frame + 1) & 1023;
	break;
      }
    } else if (cc->tdd_Config->subframeAssignment == 3) {
      switch (current_subframe) {

      case 0:
      case 5:
      case 6:
	*subframe = 2;
	*frame = (current_frame + 1) & 1023;
	break;

      case 7:
	*subframe = 3;
	*frame = (current_frame + 1) & 1023;
	break;

      case 8:
	*subframe = 4;
	*frame = (current_frame + 1) & 1023;
	break;

      case 9:
	*subframe = 2;
	*frame = (current_frame + 2) & 1023;
	break;
      }
    } else if (cc->tdd_Config->subframeAssignment == 4) {
      switch (current_subframe) {

      case 0:
      case 4:
      case 5:
      case 6:
	*subframe = 2;
	*frame = (current_frame + 1) & 1023;
	break;

      case 7:
	*subframe = 3;
	*frame = (current_frame + 1) & 1023;
	break;

      case 8:
      case 9:
	*subframe = 2;
	*frame = (current_frame + 2) & 1023;
	break;
      }
    } else if (cc->tdd_Config->subframeAssignment == 5) {
      switch (current_subframe) {

      case 0:
      case 4:
      case 5:
      case 6:
	*subframe = 2;
	*frame = (current_frame + 1) & 1023;
	break;

      case 7:
      case 8:
      case 9:
	*subframe = 2;
	*frame = (current_frame + 2) & 1023;
	break;
      }
Cedric Roux's avatar
Cedric Roux committed
265
    }
266
  }
267 268
}

269 270


271 272 273 274 275
void
get_Msg3allocret(COMMON_channels_t * cc,
		 sub_frame_t current_subframe,
		 frame_t current_frame,
		 frame_t * frame, sub_frame_t * subframe)
276
{
277 278 279
  if (cc->tdd_Config == NULL) {	//FDD
    /* always retransmit in n+8 */
    *subframe = current_subframe + 8;
280

281 282 283
    if (*subframe > 9) {
      *subframe = *subframe - 10;
      *frame = (current_frame + 1) & 1023;
284
    } else {
285
      *frame = current_frame;
286
    }
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
  } else {
    if (cc->tdd_Config->subframeAssignment == 1) {
      // 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;
    } else if (cc->tdd_Config->subframeAssignment == 3) {
      // 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;
    } else if (cc->tdd_Config->subframeAssignment == 4) {
      // 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;
    } else if (cc->tdd_Config->subframeAssignment == 5) {
      // original PUSCH in 2, PHICH in 8, ret in 2 next frame
      *frame = (current_frame + 1) & 1023;
    }
  }
308 309
}

310 311 312
uint8_t
subframe2harqpid(COMMON_channels_t * cc, frame_t frame,
		 sub_frame_t subframe)
313
{
314
  uint8_t ret = 255;
315

316
  AssertFatal(cc != NULL, "cc is null\n");
317

318 319 320 321 322 323 324 325
  if (cc->tdd_Config == NULL) {	// FDD
    ret = (((frame << 1) + subframe) & 7);
  } else {
    switch (cc->tdd_Config->subframeAssignment) {
    case 1:
      if ((subframe == 2) ||
	  (subframe == 3) || (subframe == 7) || (subframe == 8))
	switch (subframe) {
326 327
	case 2:
	case 3:
328 329 330 331 332 333
	  ret = (subframe - 2);
	  break;
	case 7:
	case 8:
	  ret = (subframe - 5);
	  break;
334
	default:
335 336 337 338 339
	  AssertFatal(1 == 0,
		      "subframe2_harq_pid, Illegal subframe %d for TDD mode %d\n",
		      subframe,
		      (int) cc->tdd_Config->subframeAssignment);
	  break;
340
	}
341

342
      break;
343

344 345 346 347 348
    case 2:
      AssertFatal((subframe == 2) || (subframe == 7),
		  "subframe2_harq_pid, Illegal subframe %d for TDD mode %d\n",
		  subframe,
		  (int) cc->tdd_Config->subframeAssignment);
349

350 351
      ret = (subframe / 7);
      break;
352

353 354 355 356 357 358 359
    case 3:
      AssertFatal((subframe > 1) && (subframe < 5),
		  "subframe2_harq_pid, Illegal subframe %d for TDD mode %d\n",
		  subframe,
		  (int) cc->tdd_Config->subframeAssignment);
      ret = (subframe - 2);
      break;
360

361 362 363 364 365 366 367
    case 4:
      AssertFatal((subframe > 1) && (subframe < 4),
		  "subframe2_harq_pid, Illegal subframe %d for TDD mode %d\n",
		  subframe,
		  (int) cc->tdd_Config->subframeAssignment);
      ret = (subframe - 2);
      break;
368

369 370 371 372
    case 5:
      AssertFatal(subframe == 2,
		  "subframe2_harq_pid, Illegal subframe %d for TDD mode %d\n",
		  subframe,
373
		  (int) cc->tdd_Config->subframeAssignment);
374 375 376 377 378 379 380
      ret = (subframe - 2);
      break;

    default:
      AssertFatal(1 == 0,
		  "subframe2_harq_pid, Unsupported TDD mode %d\n",
		  (int) cc->tdd_Config->subframeAssignment);
381
    }
382 383
  }
  return ret;
384
}
385

386 387 388 389
uint8_t
get_Msg3harqpid(COMMON_channels_t * cc,
		frame_t frame, sub_frame_t current_subframe)
{
390 391
  uint8_t ul_subframe = 0;
  uint32_t ul_frame = 0;
392

393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
  if (cc->tdd_Config == NULL) {	// FDD
    ul_subframe =
      (current_subframe >
       3) ? (current_subframe - 4) : (current_subframe + 6);
    ul_frame = (current_subframe > 3) ? ((frame + 1) & 1023) : frame;
  } else {
    switch (cc->tdd_Config->subframeAssignment) {
    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;
    case 5:
      ul_subframe = 2;
      break;
    default:
      LOG_E(PHY,
	    "get_Msg3_harq_pid: Unsupported TDD configuration %d\n",
	    (int) cc->tdd_Config->subframeAssignment);
      AssertFatal(1 == 0,
		  "get_Msg3_harq_pid: Unsupported TDD configuration");
      break;
454
    }
455
  }
Cedric Roux's avatar
Cedric Roux committed
456

457
  return (subframe2harqpid(cc, ul_frame, ul_subframe));
458
}
459

460 461 462
uint32_t
pdcchalloc2ulframe(COMMON_channels_t * ccP, uint32_t frame, uint8_t n)
{
463
  uint32_t ul_frame;
464

465 466 467 468 469 470 471 472 473 474
  if ((ccP->tdd_Config) && (ccP->tdd_Config->subframeAssignment == 1) && ((n == 1) || (n == 6)))	// tdd_config 0,1 SF 1,5
    ul_frame = (frame + (n == 1 ? 0 : 1));
  else if ((ccP->tdd_Config) &&
	   (ccP->tdd_Config->subframeAssignment == 6) &&
	   ((n == 0) || (n == 1) || (n == 5) || (n == 6)))
    ul_frame = (frame + (n >= 5 ? 1 : 0));
  else if ((ccP->tdd_Config) && (ccP->tdd_Config->subframeAssignment == 6) && (n == 9))	// tdd_config 6 SF 9
    ul_frame = (frame + 1);
  else
    ul_frame = (frame + (n >= 6 ? 1 : 0));
475

476 477 478
  LOG_D(PHY, "frame %d subframe %d: PUSCH frame = %d\n", frame, n,
	ul_frame);
  return ul_frame;
479
}
480

481 482
uint8_t pdcchalloc2ulsubframe(COMMON_channels_t * ccP, uint8_t n)
{
483
  uint8_t ul_subframe;
484

485 486 487 488 489 490 491 492 493 494
  if ((ccP->tdd_Config) && (ccP->tdd_Config->subframeAssignment == 1) && ((n == 1) || (n == 6)))	// tdd_config 0,1 SF 1,5
    ul_subframe = ((n + 6) % 10);
  else if ((ccP->tdd_Config) &&
	   (ccP->tdd_Config->subframeAssignment == 6) &&
	   ((n == 0) || (n == 1) || (n == 5) || (n == 6)))
    ul_subframe = ((n + 7) % 10);
  else if ((ccP->tdd_Config) && (ccP->tdd_Config->subframeAssignment == 6) && (n == 9))	// tdd_config 6 SF 9
    ul_subframe = ((n + 5) % 10);
  else
    ul_subframe = ((n + 4) % 10);
495

496 497
  LOG_D(PHY, "subframe %d: PUSCH subframe = %d\n", n, ul_subframe);
  return ul_subframe;
498 499
}

500
int is_UL_sf(COMMON_channels_t * ccP, sub_frame_t subframeP)
501
{
502 503 504
  // if FDD return dummy value
  if (ccP->tdd_Config == NULL)
    return (0);
505

506 507 508 509
  switch (ccP->tdd_Config->subframeAssignment) {
  case 1:
    switch (subframeP) {
    case 0:
510 511
    case 4:
    case 5:
512 513 514 515 516 517 518 519 520
    case 9:
      return (0);
      break;
    case 2:
    case 3:
    case 7:
    case 8:
      return (1);
      break;
521
    default:
522 523
      return (0);
      break;
524
    }
525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
    break;
  case 3:
    if ((subframeP <= 1) || (subframeP >= 5))
      return (0);
    else if ((subframeP > 1) && (subframeP < 5))
      return (1);
    else
      AssertFatal(1 == 0, "Unknown subframe number\n");
    break;
  case 4:
    if ((subframeP <= 1) || (subframeP >= 4))
      return (0);
    else if ((subframeP > 1) && (subframeP < 4))
      return (1);
    else
      AssertFatal(1 == 0, "Unknown subframe number\n");
    break;
  case 5:
    if ((subframeP <= 1) || (subframeP >= 3))
      return (0);
    else if ((subframeP > 1) && (subframeP < 3))
      return (1);
    else
      AssertFatal(1 == 0, "Unknown subframe number\n");
    break;
  default:
    AssertFatal(1 == 0,
		"subframe %d Unsupported TDD configuration %d\n",
		subframeP, (int) ccP->tdd_Config->subframeAssignment);
    break;
  }
556 557
}

558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581
int is_S_sf(COMMON_channels_t * ccP, sub_frame_t subframeP)
{
    // if FDD return dummy value
    if (ccP->tdd_Config == NULL)
    return (0);

    switch (subframeP) {
    case 1:
        return (1);
        break;

    case 6:
        if(ccP->tdd_Config->subframeAssignment == 0 || ccP->tdd_Config->subframeAssignment == 1
                || ccP->tdd_Config->subframeAssignment == 2 || ccP->tdd_Config->subframeAssignment == 6)
        return (1);
        break;

    default:
        return (0);
        break;
    }
    return 0;
}

582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607
uint8_t ul_subframe2_k_phich(COMMON_channels_t * cc, sub_frame_t ul_subframe){

    if(cc->tdd_Config){//TODO fill other tdd config
        switch(cc->tdd_Config->subframeAssignment){
        case 0:
            break;
        case 1:
            if(ul_subframe == 2 || ul_subframe == 7)
                return 4;
            else if(ul_subframe == 3 || ul_subframe == 8)
                return 6;
            else return 255;
            break;
        case 2:
            break;
        case 3:
            break;
        case 4:
            break;
        case 5:
            break;
        }
    }
    return 4; //idk  sf_ahead?
}

608
uint16_t get_pucch1_absSF(COMMON_channels_t * cc, uint16_t dlsch_absSF)
609
{
610
  uint16_t sf, f, nextf;
611

612
  if (cc->tdd_Config == NULL) { //FDD n+4
613 614 615 616 617 618 619 620
    return ((dlsch_absSF + 4) % 10240);
  } else {
    sf = dlsch_absSF % 10;
    f = dlsch_absSF / 10;
    nextf = (f + 1) & 1023;

    switch (cc->tdd_Config->subframeAssignment) {
    case 0:
621 622 623 624 625 626 627 628 629 630 631
      if ((sf == 0) || (sf == 5))
        return ((10 * f) + sf + 4)% 10240;  // ACK/NAK in SF 4,9 same frame
      else if (sf == 6)
        return ((10 * nextf) + 2)% 10240;  // ACK/NAK in SF 2 next frame
      else if (sf == 1)
        return ((10 * f) + 7)% 10240;  // ACK/NAK in SF 7 same frame
      else
        AssertFatal(1 == 0,
                   "Impossible dlsch subframe %d for TDD configuration 0\n",
                   sf);

632 633 634
      break;
    case 1:
      if ((sf == 5) || (sf == 6))
635
        return ((10 * nextf) + 2)% 10240;        // ACK/NAK in SF 2 next frame
636
      else if (sf == 9)
637
        return ((10 * nextf) + 3)% 10240;        // ACK/NAK in SF 3 next frame
638
      else if ((sf == 0) || (sf == 1))
639 640
        return ((10 * f) + 7)% 10240;        // ACK/NAK in SF 7 same frame
      else if (sf == 4)
641
        return ((10 * f) + 8)% 10240;  // ACK/NAK in SF 8 same frame
642
      else
643 644 645
        AssertFatal(1 == 0,
                    "Impossible dlsch subframe %d for TDD configuration 1\n",
                    sf);
646 647 648
      break;
    case 2:
      if ((sf == 4) || (sf == 5) || (sf == 6) || (sf == 8))
649
        return ((10 * nextf) + 2)% 10240;        // ACK/NAK in SF 2 next frame
650
      else if (sf == 9)
651
        return ((10 * nextf) + 7)% 10240;        // ACK/NAK in SF 7 next frame
652
      else if ((sf == 0) || (sf == 1) || (sf == 3))
653
        return ((10 * f) + 7)% 10240;        // ACK/NAK in SF 7 same frame
654
      else
655 656 657
        AssertFatal(1 == 0,
                    "Impossible dlsch subframe %d for TDD configuration 2\n",
                    sf);
658 659 660
      break;
    case 3:
      if ((sf == 5) || (sf == 6) || (sf == 7) || (sf == 8)
661 662
          || (sf == 9))
        return ((10 * nextf) + ((sf-1) >> 1))% 10240;   // ACK/NAK in 2,3,4 resp. next frame
663
      else if (sf == 1)
664
        return ((10 * nextf) + 2)% 10240;         // ACK/NAK in 2 next frame
665
      else if (sf == 0)
666
        return ((10 * f) + 4)% 10240;        // ACK/NAK in 4 same frame
667
      else
668 669 670
        AssertFatal(1 == 0,
                    "Impossible dlsch subframe %d for TDD configuration 3\n",
                    sf);
671 672 673
      break;
    case 4:
      if ((sf == 6) || (sf == 7) || (sf == 8) || (sf == 9))
674
        return (((10 * nextf) + 3) % 10240);        // ACK/NAK in SF 3 next frame
675
      else if ((sf == 0) || (sf == 1) || (sf == 4) || (sf == 5))
676
        return (((10 * nextf) + 2) % 10240);        // ACK/NAK in SF 2 next frame
677
      else
678 679 680
        AssertFatal(1 == 0,
                    "Impossible dlsch subframe %d for TDD configuration 4\n",
                    sf);
681 682 683
      break;
    case 5:
      if ((sf == 0) || (sf == 1) || (sf == 3) || (sf == 4)
684 685
          || (sf == 5) || (sf == 6) || (sf == 7) || (sf == 8))
        return (((10 * nextf) + 2) % 10240);        // ACK/NAK in SF 3 next frame
686
      else if (sf == 9)
687
        return (((10 * (1 + nextf)) + 2) % 10240);        // ACK/NAK in SF 2 next frame
688
      else
689 690 691
        AssertFatal(1 == 0,
                    "Impossible dlsch subframe %d for TDD configuration 5\n",
                    sf);
692 693
      break;
    case 6:
694 695 696 697 698 699 700 701 702 703
      if ((sf == 5) || (sf == 6))
        return ((10 * f) + sf + 7)% 10240;  // ACK/NAK in SF 2,3 next frame
      else if (sf == 9)
        return ((10 * nextf) + 4)% 10240;  // ACK/NAK in SF 4 next frame
      else if ((sf == 1) || (sf == 0))
        return ((10 * f) + sf + 7)% 10240;  // ACK/NAK in SF 7 same frame
      else
        AssertFatal(1 == 0,
                "Impossible dlsch subframe %d for TDD configuration 6\n",
                sf);
704 705 706
      break;
    default:
      AssertFatal(1 == 0, "Illegal TDD subframe Assigment %d\n",
707
                  (int) cc->tdd_Config->subframeAssignment);
708
      break;
709
    }
710 711
  }
  AssertFatal(1 == 0, "Shouldn't get here\n");
712 713
}

714 715 716
void
get_srs_pos(COMMON_channels_t * cc, uint16_t isrs,
	    uint16_t * psrsPeriodicity, uint16_t * psrsOffset)
Cedric Roux's avatar
Cedric Roux committed
717
{
718 719
  if (cc->tdd_Config) {	// TDD
    AssertFatal(isrs >= 10, "2 ms SRS periodicity not supported");
720

721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748
    if ((isrs > 9) && (isrs < 15)) {
      *psrsPeriodicity = 5;
      *psrsOffset = isrs - 10;
    }
    if ((isrs > 14) && (isrs < 25)) {
      *psrsPeriodicity = 10;
      *psrsOffset = isrs - 15;
    }
    if ((isrs > 24) && (isrs < 45)) {
      *psrsPeriodicity = 20;
      *psrsOffset = isrs - 25;
    }
    if ((isrs > 44) && (isrs < 85)) {
      *psrsPeriodicity = 40;
      *psrsOffset = isrs - 45;
    }
    if ((isrs > 84) && (isrs < 165)) {
      *psrsPeriodicity = 80;
      *psrsOffset = isrs - 85;
    }
    if ((isrs > 164) && (isrs < 325)) {
      *psrsPeriodicity = 160;
      *psrsOffset = isrs - 165;
    }
    if ((isrs > 324) && (isrs < 645)) {
      *psrsPeriodicity = 320;
      *psrsOffset = isrs - 325;
    }
749

750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771
    AssertFatal(isrs <= 644, "Isrs out of range %d>644\n", isrs);
  }				// TDD
  else {			// FDD
    if (isrs < 2) {
      *psrsPeriodicity = 2;
      *psrsOffset = isrs;
    }
    if ((isrs > 1) && (isrs < 7)) {
      *psrsPeriodicity = 5;
      *psrsOffset = isrs - 2;
    }
    if ((isrs > 6) && (isrs < 17)) {
      *psrsPeriodicity = 10;
      *psrsOffset = isrs - 7;
    }
    if ((isrs > 16) && (isrs < 37)) {
      *psrsPeriodicity = 20;
      *psrsOffset = isrs - 17;
    }
    if ((isrs > 36) && (isrs < 77)) {
      *psrsPeriodicity = 40;
      *psrsOffset = isrs - 37;
772
    }
773 774 775
    if ((isrs > 76) && (isrs < 157)) {
      *psrsPeriodicity = 80;
      *psrsOffset = isrs - 77;
776
    }
777 778 779 780 781 782 783
    if ((isrs > 156) && (isrs < 317)) {
      *psrsPeriodicity = 160;
      *psrsOffset = isrs - 157;
    }
    if ((isrs > 316) && (isrs < 637)) {
      *psrsPeriodicity = 320;
      *psrsOffset = isrs - 317;
784
    }
785 786
    AssertFatal(isrs <= 636, "Isrs out of range %d>636\n", isrs);
  }
787 788
}

789 790
void
get_csi_params(COMMON_channels_t * cc,
791
	       struct LTE_CQI_ReportPeriodic *cqi_ReportPeriodic,
792
	       uint16_t * Npd, uint16_t * N_OFFSET_CQI, int *H)
793
{
794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834
  uint16_t cqi_PMI_ConfigIndex =
    cqi_ReportPeriodic->choice.setup.cqi_pmi_ConfigIndex;
  uint8_t Jtab[6] = { 0, 2, 2, 3, 4, 4 };

  AssertFatal(cqi_ReportPeriodic != NULL,
	      "cqi_ReportPeriodic is null!\n");

  if (cc->tdd_Config == NULL) {	//FDD
    if (cqi_PMI_ConfigIndex <= 1) {	// 2 ms CQI_PMI period
      *Npd = 2;
      *N_OFFSET_CQI = cqi_PMI_ConfigIndex;
    } else if (cqi_PMI_ConfigIndex <= 6) {	// 5 ms CQI_PMI period
      *Npd = 5;
      *N_OFFSET_CQI = cqi_PMI_ConfigIndex - 2;
    } else if (cqi_PMI_ConfigIndex <= 16) {	// 10ms CQI_PMI period
      *Npd = 10;
      *N_OFFSET_CQI = cqi_PMI_ConfigIndex - 7;
    } else if (cqi_PMI_ConfigIndex <= 36) {	// 20 ms CQI_PMI period
      *Npd = 20;
      *N_OFFSET_CQI = cqi_PMI_ConfigIndex - 17;
    } else if (cqi_PMI_ConfigIndex <= 76) {	// 40 ms CQI_PMI period
      *Npd = 40;
      *N_OFFSET_CQI = cqi_PMI_ConfigIndex - 37;
    } else if (cqi_PMI_ConfigIndex <= 156) {	// 80 ms CQI_PMI period
      *Npd = 80;
      *N_OFFSET_CQI = cqi_PMI_ConfigIndex - 77;
    } else if (cqi_PMI_ConfigIndex <= 316) {	// 160 ms CQI_PMI period
      *Npd = 160;
      *N_OFFSET_CQI = cqi_PMI_ConfigIndex - 157;
    } else if (cqi_PMI_ConfigIndex > 317) {

      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;
      }
835
    }
836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857
  } else {			// TDD
    if (cqi_PMI_ConfigIndex == 0) {	// all UL subframes
      *Npd = 1;
      *N_OFFSET_CQI = 0;
    } else if (cqi_PMI_ConfigIndex <= 6) {	// 5 ms CQI_PMI period
      *Npd = 5;
      *N_OFFSET_CQI = cqi_PMI_ConfigIndex - 1;
    } else if (cqi_PMI_ConfigIndex <= 16) {	// 10ms CQI_PMI period
      *Npd = 10;
      *N_OFFSET_CQI = cqi_PMI_ConfigIndex - 6;
    } else if (cqi_PMI_ConfigIndex <= 36) {	// 20 ms CQI_PMI period
      *Npd = 20;
      *N_OFFSET_CQI = cqi_PMI_ConfigIndex - 16;
    } else if (cqi_PMI_ConfigIndex <= 76) {	// 40 ms CQI_PMI period
      *Npd = 40;
      *N_OFFSET_CQI = cqi_PMI_ConfigIndex - 36;
    } else if (cqi_PMI_ConfigIndex <= 156) {	// 80 ms CQI_PMI period
      *Npd = 80;
      *N_OFFSET_CQI = cqi_PMI_ConfigIndex - 76;
    } else if (cqi_PMI_ConfigIndex <= 316) {	// 160 ms CQI_PMI period
      *Npd = 160;
      *N_OFFSET_CQI = cqi_PMI_ConfigIndex - 156;
858
    }
859
  }
860

861 862 863
  // get H
  if (cqi_ReportPeriodic->choice.setup.cqi_FormatIndicatorPeriodic.
      present ==
864
      LTE_CQI_ReportPeriodic__setup__cqi_FormatIndicatorPeriodic_PR_subbandCQI)
865 866 867 868 869 870
    *H = 1 +
      (Jtab[cc->mib->message.dl_Bandwidth] *
       cqi_ReportPeriodic->choice.setup.
       cqi_FormatIndicatorPeriodic.choice.subbandCQI.k);
  else
    *H = 1;
871 872
}

873 874 875
uint8_t
get_dl_cqi_pmi_size_pusch(COMMON_channels_t * cc, uint8_t tmode,
			  uint8_t ri,
876
			  LTE_CQI_ReportModeAperiodic_t *
877
			  cqi_ReportModeAperiodic)
Cedric Roux's avatar
Cedric Roux committed
878
{
879 880 881 882 883 884 885 886 887
  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];

  AssertFatal(cqi_ReportModeAperiodic != NULL,
	      "cqi_ReportPeriodic is null!\n");

  switch (*cqi_ReportModeAperiodic) {
888
  case LTE_CQI_ReportModeAperiodic_rm12:
889 890 891 892 893 894 895 896 897 898 899 900 901 902 903
    AssertFatal(tmode == 4 || tmode == 6 || tmode == 8 || tmode == 9
		|| tmode == 10,
		"Illegal TM (%d) for CQI_ReportModeAperiodic_rm12\n",
		tmode);
    AssertFatal(cc->p_eNB <= 4,
		"only up to 4 antenna ports supported here\n");
    if (ri == 1 && cc->p_eNB == 2)
      return (4 + (N << 1));
    else if (ri == 2 && cc->p_eNB == 2)
      return (8 + N);
    else if (ri == 1 && cc->p_eNB == 4)
      return (4 + (N << 2));
    else if (ri > 1 && cc->p_eNB == 4)
      return (8 + (N << 2));
    break;
904
  case LTE_CQI_ReportModeAperiodic_rm20:
905 906 907 908 909 910 911 912 913 914 915
    // Table 5.2.2.6.3-1 (36.212)
    AssertFatal(tmode == 1 || tmode == 2 || tmode == 3 || tmode == 7
		|| tmode == 9
		|| tmode == 10,
		"Illegal TM (%d) for CQI_ReportModeAperiodic_rm20\n",
		tmode);
    AssertFatal(tmode != 9
		&& tmode != 10,
		"TM9/10 will be handled later for CQI_ReportModeAperiodic_rm20\n");
    return (4 + 2 + L);
    break;
916
  case LTE_CQI_ReportModeAperiodic_rm22:
917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933
    // Table 5.2.2.6.3-2 (36.212)
    AssertFatal(tmode == 4 || tmode == 6 || tmode == 8 || tmode == 9
		|| tmode == 10,
		"Illegal TM (%d) for CQI_ReportModeAperiodic_rm22\n",
		tmode);
    AssertFatal(tmode != 9
		&& tmode != 10,
		"TM9/10 will be handled later for CQI_ReportModeAperiodic_rm22\n");
    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;
934
  case LTE_CQI_ReportModeAperiodic_rm30:
935 936 937 938 939 940 941 942 943 944 945
    // Table 5.2.2.6.2-1 (36.212)
    AssertFatal(tmode == 1 || tmode == 2 || tmode == 3 || tmode == 7
		|| tmode == 8 || tmode == 9
		|| tmode == 10,
		"Illegal TM (%d) for CQI_ReportModeAperiodic_rm30\n",
		tmode);
    AssertFatal(tmode != 8 && tmode != 9
		&& tmode != 10,
		"TM8/9/10 will be handled later for CQI_ReportModeAperiodic_rm30\n");
    return (4 + (N << 1));
    break;
946
  case LTE_CQI_ReportModeAperiodic_rm31:
947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963
    // Table 5.2.2.6.2-2 (36.212)
    AssertFatal(tmode == 4 || tmode == 6 || tmode == 8 || tmode == 9
		|| tmode == 10,
		"Illegal TM (%d) for CQI_ReportModeAperiodic_rm31\n",
		tmode);
    AssertFatal(tmode != 8 && tmode != 9
		&& tmode != 10,
		"TM8/9/10 will be handled later for CQI_ReportModeAperiodic_rm31\n");
    if (ri == 1 && cc->p_eNB == 2)
      return (4 + (N << 1) + 0 + 0 + 2);
    else if (ri == 2 && cc->p_eNB == 2)
      return (4 + (N << 1) + 4 + (N << 1) + 1);
    else if (ri == 1 && cc->p_eNB == 4)
      return (4 + (N << 1) + 0 + 0 + 4);
    else if (ri >= 2 && cc->p_eNB == 4)
      return (4 + (N << 1) + 4 + (N << 1) + 4);
    break;
964 965
#if (LTE_RRC_VERSION >= MAKE_VERSION(12, 5, 0))
  case LTE_CQI_ReportModeAperiodic_rm32_v1250:
966 967 968 969 970 971 972
    AssertFatal(tmode == 4 || tmode == 6 || tmode == 8 || tmode == 9
		|| tmode == 10,
		"Illegal TM (%d) for CQI_ReportModeAperiodic_rm32\n",
		tmode);
    AssertFatal(1 == 0,
		"CQI_ReportModeAperiodic_rm32_v1250 not supported yet\n");
    break;
973
  case LTE_CQI_ReportModeAperiodic_rm10_v1310:
974 975 976 977 978 979
    // Table 5.2.2.6.1-1F/G (36.212)
    if (ri == 1)
      return (4);		// F
    else
      return (7);		// G
    break;
980
  case LTE_CQI_ReportModeAperiodic_rm11_v1310:
981 982 983 984 985 986 987 988 989 990 991 992 993 994 995
    // Table 5.2.2.6.1-1H (36.212)
    AssertFatal(tmode == 4 || tmode == 6 || tmode == 8 || tmode == 9
		|| tmode == 10,
		"Illegal TM (%d) for CQI_ReportModeAperiodic_rm11\n",
		tmode);
    AssertFatal(cc->p_eNB <= 4,
		"only up to 4 antenna ports supported here\n");
    if (ri == 1 && cc->p_eNB == 2)
      return (4 + 0 + 2);
    else if (ri == 2 && cc->p_eNB == 2)
      return (4 + 4 + 1);
    else if (ri == 1 && cc->p_eNB == 4)
      return (4 + 0 + 4);
    else if (ri > 1 && cc->p_eNB == 4)
      return (4 + 4 + 4);
996

997
    break;
998
#endif /* #if (LTE_RRC_VERSION >= MAKE_VERSION(12, 5, 0)) */
999 1000 1001
  }
  AssertFatal(1 == 0, "Shouldn't get here\n");
  return (0);
1002
}
1003

1004 1005 1006
uint8_t
get_rel8_dl_cqi_pmi_size(UE_sched_ctrl * sched_ctl, int CC_idP,
			 COMMON_channels_t * cc, uint8_t tmode,
1007
			 struct LTE_CQI_ReportPeriodic * cqi_ReportPeriodic)
1008
{
1009 1010
  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)};
1011

1012 1013
  uint8_t Ltab[6] = { 0, 1, 2, 2, 2, 2 };
  uint8_t ri = sched_ctl->periodic_ri_received[CC_idP];
1014

1015 1016
  AssertFatal(cqi_ReportPeriodic != NULL,
	      "cqi_ReportPeriodic is null!\n");
1017
  AssertFatal(cqi_ReportPeriodic->present != LTE_CQI_ReportPeriodic_PR_NOTHING,
1018 1019
	      "cqi_ReportPeriodic->present == CQI_ReportPeriodic_PR_NOTHING!\n");
  AssertFatal(cqi_ReportPeriodic->choice.
1020
	      setup.cqi_FormatIndicatorPeriodic.present != LTE_CQI_ReportPeriodic__setup__cqi_FormatIndicatorPeriodic_PR_NOTHING,
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034
	      "cqi_ReportPeriodic->cqi_FormatIndicatorPeriodic.choice.setup.present == CQI_ReportPeriodic__setup__cqi_FormatIndicatorPeriodic_PR_NOTHING!\n");

  switch (tmode) {
  case 1:
  case 2:
  case 5:
  case 6:
  case 7:
    no_pmi = 1;
    break;
  default:
    no_pmi = 0;
  }

1035
  if ((cqi_ReportPeriodic->choice.setup.cqi_FormatIndicatorPeriodic.present == LTE_CQI_ReportPeriodic__setup__cqi_FormatIndicatorPeriodic_PR_widebandCQI)
1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046
      || (sched_ctl->feedback_cnt[CC_idP] == 0)) {
    // send wideband report every opportunity if wideband reporting mode is selected, else every H opportunities
    if (no_pmi == 1)                        return (4);
    else if ((cc->p_eNB == 2) && (ri == 1)) return (6);
    else if ((cc->p_eNB == 2) && (ri == 2)) return (8);
    else if ((cc->p_eNB == 4) && (ri == 1)) return (8);
    else if ((cc->p_eNB == 4) && (ri == 2)) return (11);
    else
      AssertFatal(1 == 0,
		  "illegal combination p %d, ri %d, no_pmi %d\n",
		  cc->p_eNB, ri, no_pmi);
1047
  } else if (cqi_ReportPeriodic->choice.setup.cqi_FormatIndicatorPeriodic.present == LTE_CQI_ReportPeriodic__setup__cqi_FormatIndicatorPeriodic_PR_subbandCQI) {
1048 1049 1050 1051 1052 1053 1054
    if ((no_pmi == 1) || ri == 1) return (4 + Ltab[cc->mib->message.dl_Bandwidth]);
    else                          return (7 + Ltab[cc->mib->message.dl_Bandwidth]);
  }

  AssertFatal(1 == 0,
	      "Shouldn't get here : cqi_ReportPeriodic->present %d\n",
	      cqi_ReportPeriodic->choice.setup.cqi_FormatIndicatorPeriodic.present);
1055 1056
}

1057 1058


1059
void
1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088
fill_nfapi_dl_dci_1A(nfapi_dl_config_request_pdu_t *dl_config_pdu,
		     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,
		     uint8_t                       vrb_flag)
{
  memset((void *) dl_config_pdu, 0,
	 sizeof(nfapi_dl_config_request_pdu_t));
  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;
  dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.transmission_power                     = 6000;	// equal to RS power
  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;
1089 1090
}

1091 1092 1093 1094
void
program_dlsch_acknak(module_id_t module_idP, int CC_idP, int UE_idP,
		     frame_t frameP, sub_frame_t subframeP,
		     uint8_t cce_idx)
Cedric Roux's avatar
Cedric Roux committed
1095
{
1096 1097 1098 1099 1100 1101 1102 1103 1104
  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;
Cedric Roux's avatar
Cedric Roux committed
1105

1106
#if (LTE_RRC_VERSION >= MAKE_VERSION(10, 2, 0))
Cedric Roux's avatar
Cedric Roux committed
1107

1108 1109 1110
  if ((UE_list->UE_template[CC_idP][UE_idP].physicalConfigDedicated->ext2)
      && (UE_list->UE_template[CC_idP][UE_idP].physicalConfigDedicated->ext2->pucch_ConfigDedicated_v1020)
      && (UE_list->UE_template[CC_idP][UE_idP].physicalConfigDedicated->ext2->pucch_ConfigDedicated_v1020->simultaneousPUCCH_PUSCH_r10)
1111
      && (*UE_list->UE_template[CC_idP][UE_idP].physicalConfigDedicated->ext2->pucch_ConfigDedicated_v1020->simultaneousPUCCH_PUSCH_r10 == LTE_PUCCH_ConfigDedicated_v1020__simultaneousPUCCH_PUSCH_r10_true))
1112
    use_simultaneous_pucch_pusch = 1;
1113
#endif
Cedric Roux's avatar
Cedric Roux committed
1114

1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157
  // 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
  int16_t ul_absSF = get_pucch1_absSF(&cc[CC_idP], subframeP + (10 * frameP));

  if ((ul_config_pdu = has_ul_grant(module_idP, CC_idP,ul_absSF, rnti)) == NULL) {
    // no UL grant so
    // Program ACK/NAK alone Format 1a/b or 3

    ul_req        = &RC.mac[module_idP]->UL_req_tmp[CC_idP][ul_absSF %10].ul_config_request_body;
    ul_config_pdu = &ul_req->ul_config_pdu_list[ul_req->number_of_pdus];
    // Do PUCCH
    fill_nfapi_uci_acknak(module_idP,
			  CC_idP,
			  rnti, (frameP * 10) + subframeP, cce_idx);
  } 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] */
      
    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
	harq_information        = &ul_config_pdu->ulsch_uci_harq_pdu.harq_information;
	ul_config_pdu->pdu_type = NFAPI_UL_CONFIG_ULSCH_UCI_HARQ_PDU_TYPE;
	LOG_D(MAC,
	      "Frame %d, Subframe %d: Switched UCI HARQ to ULSCH UCI HARQ\n",
	      frameP, subframeP);
      } else {
	// Convert it to an NFAPI_UL_CONFIG_ULSCH_HARQ_PDU_TYPE
	ulsch_harq_information                                                                                                               = &ul_config_pdu->ulsch_harq_pdu.harq_information;
	ul_config_pdu->pdu_type = NFAPI_UL_CONFIG_ULSCH_HARQ_PDU_TYPE;
	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",
	      frameP, subframeP);
      }
      break;
    case NFAPI_UL_CONFIG_ULSCH_HARQ_PDU_TYPE:
1158
      AssertFatal(use_simultaneous_pucch_pusch == 0,
1159 1160 1161
		  "Cannot be NFAPI_UL_CONFIG_ULSCH_HARQ_PDU_TYPE, simultaneous_pucch_pusch is active");
      break;
    case NFAPI_UL_CONFIG_ULSCH_UCI_HARQ_PDU_TYPE:
1162
      AssertFatal(use_simultaneous_pucch_pusch == 1,
1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226
		  "Cannot be NFAPI_UL_CONFIG_ULSCH_UCI_PDU_TYPE, simultaneous_pucch_pusch is inactive\n");
      break;

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

    case NFAPI_UL_CONFIG_ULSCH_CQI_RI_PDU_TYPE:
      // Convert it to an NFAPI_UL_CONFIG_ULSCH_CQI_HARQ_RI_PDU_TYPE
      ulsch_harq_information  = &ul_config_pdu->ulsch_cqi_harq_ri_pdu.harq_information;
      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.
       */
      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
      break;
    case NFAPI_UL_CONFIG_ULSCH_CQI_HARQ_RI_PDU_TYPE:
      AssertFatal(use_simultaneous_pucch_pusch == 0,
		  "Cannot be NFAPI_UL_CONFIG_ULSCH_CQI_HARQ_RI_PDU_TYPE, simultaneous_pucch_pusch is active\n");
      break;

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

    case NFAPI_UL_CONFIG_ULSCH_UCI_CSI_PDU_TYPE:
      // convert it to an NFAPI_UL_CONFIG_ULSCH_CSI_UCI_HARQ_PDU_TYPE
      harq_information        = &ul_config_pdu->ulsch_csi_uci_harq_pdu.harq_information;
      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:
      AssertFatal(use_simultaneous_pucch_pusch == 1,
		  "Cannot be NFAPI_UL_CONFIG_ULSCH_CSI_UCI_HARQ_PDU_TYPE, simultaneous_pucch_pusch is inactive\n");
      break;

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

    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;
      harq_information        = &ul_config_pdu->uci_sr_harq_pdu.harq_information;
      break;
    case NFAPI_UL_CONFIG_UCI_SR_HARQ_PDU_TYPE:
      /* nothing to do */
      break;
      /* [cqi] to [cqi + harq] */
    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;
      /* [cqi + sr] to [cqr + sr + harq] */
    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;
1227
    }
1228
  }
Cedric Roux's avatar
Cedric Roux committed
1229

1230
  if (ulsch_harq_information) fill_nfapi_ulsch_harq_information(module_idP, CC_idP, rnti, ulsch_harq_information, subframeP);
Cedric Roux's avatar
Cedric Roux committed
1231

1232 1233 1234 1235
  if (harq_information) fill_nfapi_harq_information(module_idP, CC_idP,
						    rnti,
						    (frameP * 10) + subframeP,
						    harq_information, cce_idx);
1236 1237
}

1238
uint8_t get_V_UL_DAI(module_id_t module_idP, int CC_idP, uint16_t rntiP,sub_frame_t subframeP)
Cedric Roux's avatar
Cedric Roux committed
1239
{
1240
  nfapi_hi_dci0_request_body_t *HI_DCI0_req = &RC.mac[module_idP]->HI_DCI0_req[CC_idP][subframeP].hi_dci0_request_body;
1241 1242 1243 1244 1245 1246 1247 1248
  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++) {
    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);
  }
  return (4);			// this is rule from Section 7.3 in 36.213
1249
}
1250

1251
void
1252 1253 1254
fill_nfapi_ulsch_harq_information(module_id_t                            module_idP,
				  int                                    CC_idP,
				  uint16_t                               rntiP,
1255 1256
				  nfapi_ul_config_ulsch_harq_information *harq_information,
				  sub_frame_t                            subframeP)
1257
{
1258 1259 1260 1261 1262 1263
  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);

1264
  LTE_PUSCH_ConfigDedicated_t *puschConfigDedicated;
1265 1266 1267 1268 1269 1270 1271 1272
  //  PUSCH_ConfigDedicated_v1020_t        *puschConfigDedicated_v1020;
  //  PUSCH_ConfigDedicated_v1130_t        *puschConfigDedicated_v1130;
  //  PUSCH_ConfigDedicated_v1250_t        *puschConfigDedicated_v1250;

  AssertFatal(UE_id >= 0, "UE_id cannot be found, impossible\n");
  AssertFatal(UE_list != NULL, "UE_list is null\n");
  AssertFatal(UE_list->UE_template[CC_idP][UE_id].physicalConfigDedicated != NULL,
	      "physicalConfigDedicated for rnti %x is null\n", rntiP);
1273
  AssertFatal((puschConfigDedicated = (LTE_PUSCH_ConfigDedicated_t *)
1274 1275 1276
	       UE_list->UE_template[CC_idP][UE_id].physicalConfigDedicated->pusch_ConfigDedicated) != NULL,
	      "physicalConfigDedicated->puschConfigDedicated for rnti %x is null\n",
	      rntiP);
1277
#if (LTE_RRC_VERSION >= MAKE_VERSION(10, 2, 0))
1278 1279
  /*  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
1280
      #if (LTE_RRC_VERSION >= MAKE_VERSION(11, 3, 0))
1281
      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;
1282
      #endif
1283
      #if (LTE_RRC_VERSION >= MAKE_VERSION(12, 5, 0))
1284
      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;
1285
      #endif
1286
  */
1287
#endif
1288 1289 1290 1291
  harq_information->harq_information_rel10.delta_offset_harq = puschConfigDedicated->betaOffset_ACK_Index;
  AssertFatal(UE_list->UE_template[CC_idP][UE_id].physicalConfigDedicated->pucch_ConfigDedicated != NULL,
	      "pucch_ConfigDedicated is null!\n");
  if ((UE_list->UE_template[CC_idP][UE_id].physicalConfigDedicated->pucch_ConfigDedicated->tdd_AckNackFeedbackMode != NULL)
1292
      && (*UE_list->UE_template[CC_idP][UE_id].physicalConfigDedicated->pucch_ConfigDedicated->tdd_AckNackFeedbackMode == LTE_PUCCH_ConfigDedicated__tdd_AckNackFeedbackMode_multiplexing))
1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306
    harq_information->harq_information_rel10.ack_nack_mode = 1;	// multiplexing
  else
    harq_information->harq_information_rel10.ack_nack_mode = 0;	// bundling

  switch (get_tmode(module_idP, CC_idP, UE_id)) {
  case 1:
  case 2:
  case 5:
  case 6:
  case 7:
    if (cc->tdd_Config == NULL)	// FDD
      harq_information->harq_information_rel10.harq_size = 1;
    else {
      if (harq_information->harq_information_rel10.ack_nack_mode == 1)
1307
	harq_information->harq_information_rel10.harq_size = get_V_UL_DAI(module_idP, CC_idP, rntiP, subframeP);
1308 1309 1310 1311 1312 1313 1314 1315 1316
      else
        harq_information->harq_information_rel10.harq_size = 1;
    }
    break;
  default:			// for any other TM we need 2 bits harq
    if (cc->tdd_Config == NULL) {
      harq_information->harq_information_rel10.harq_size = 2;
    } else {
      if (harq_information->harq_information_rel10.ack_nack_mode == 1)
1317
	harq_information->harq_information_rel10.harq_size = get_V_UL_DAI(module_idP, CC_idP, rntiP, subframeP);
1318 1319 1320 1321 1322
      else
	harq_information->harq_information_rel10.harq_size = 2;
    }
    break;
  }				// get Tmode
1323 1324
}

1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344
uint8_t 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}};

// This is part of the PUCCH allocation procedure (see Section 10.1 36.213)
uint16_t getNp(int dl_Bandwidth,uint8_t nCCE,uint8_t plus1)
{
  AssertFatal(dl_Bandwidth<6,"dl_Bandwidth %d>5\n",dl_Bandwidth);

  if (nCCE>=Np[dl_Bandwidth][2])
    return(Np[dl_Bandwidth][2+plus1]);
  else if (nCCE>=Np[dl_Bandwidth][1])
    return(Np[dl_Bandwidth][1+plus1]);
  else
    return(Np[dl_Bandwidth][0+plus1]);
}

1345
void
1346 1347 1348 1349 1350 1351
fill_nfapi_harq_information(module_id_t                      module_idP,
			    int                              CC_idP,
			    uint16_t                         rntiP,
			    uint16_t                         absSFP,
			    nfapi_ul_config_harq_information *harq_information, 
			    uint8_t                          cce_idxP)
Cedric Roux's avatar
Cedric Roux committed
1352
{
1353 1354 1355
  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;
1356

1357
  int UE_id = find_UE_id(module_idP, rntiP);
1358

1359 1360
  AssertFatal(UE_id >= 0, "UE_id cannot be found, impossible\n");
  AssertFatal(UE_list != NULL, "UE_list is null\n");
1361

1362 1363
  harq_information->harq_information_rel11.tl.tag        = NFAPI_UL_CONFIG_REQUEST_HARQ_INFORMATION_REL11_TAG;
  harq_information->harq_information_rel11.num_ant_ports = 1;
1364

1365 1366 1367 1368 1369 1370 1371
  switch (get_tmode(module_idP, CC_idP, UE_id)) {
  case 1:
  case 2:
  case 5:
  case 6:
  case 7:
    if (cc->tdd_Config != NULL) {
1372 1373 1374 1375 1376
//      AssertFatal(UE_list->UE_template[CC_idP][UE_id].physicalConfigDedicated->pucch_ConfigDedicated != NULL,
//		  "pucch_ConfigDedicated is null for TDD!\n");
      if (UE_list->UE_template[CC_idP][UE_id].physicalConfigDedicated != NULL
          && UE_list->UE_template[CC_idP][UE_id].physicalConfigDedicated->pucch_ConfigDedicated != NULL
          && (UE_list->UE_template[CC_idP][UE_id].physicalConfigDedicated->pucch_ConfigDedicated->tdd_AckNackFeedbackMode != NULL)
1377
	  && (*UE_list->UE_template[CC_idP][UE_id].physicalConfigDedicated->pucch_ConfigDedicated->tdd_AckNackFeedbackMode == LTE_PUCCH_ConfigDedicated__tdd_AckNackFeedbackMode_multiplexing))
1378 1379 1380 1381 1382 1383
      {
        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
1384 1385
      }
      harq_information->harq_information_rel10_tdd.tl.tag                    = NFAPI_UL_CONFIG_REQUEST_HARQ_INFORMATION_REL10_TDD_TAG;
1386
      harq_information->harq_information_rel10_tdd.n_pucch_1_0               = getNp(cc->mib->message.dl_Bandwidth,cce_idxP,0) +
1387
                                                                               cc->radioResourceConfigCommon->pucch_ConfigCommon.n1PUCCH_AN + cce_idxP;
1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400
      harq_information->harq_information_rel10_tdd.number_of_pucch_resources = 1;
    } else {
      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;
    }
    break;
  default:			// for any other TM we need 2 bits harq
    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");
      if ((UE_list->UE_template[CC_idP][UE_id].physicalConfigDedicated->pucch_ConfigDedicated->tdd_AckNackFeedbackMode != NULL)
1401
	  && (*UE_list->UE_template[CC_idP][UE_id].physicalConfigDedicated->pucch_ConfigDedicated->tdd_AckNackFeedbackMode == LTE_PUCCH_ConfigDedicated__tdd_AckNackFeedbackMode_multiplexing)) {
1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418
	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;
    } else {
      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;
    }
    break;
  }				// get Tmode
1419
}
Cedric Roux's avatar
Cedric Roux committed
1420

1421 1422
uint16_t
fill_nfapi_uci_acknak(module_id_t module_idP,
1423 1424 1425 1426
		      int         CC_idP,
		      uint16_t    rntiP, 
		      uint16_t    absSFP, 
		      uint8_t     cce_idxP)
Cedric Roux's avatar
Cedric Roux committed
1427
{
1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458
  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;
  ul_config_pdu->uci_harq_pdu.ue_information.ue_information_rel8.handle = 0;	// don't know how to use this
  ul_config_pdu->uci_harq_pdu.ue_information.ue_information_rel8.rnti   = rntiP;

  fill_nfapi_harq_information(module_idP, CC_idP,
			      rntiP,
			      absSFP,
			      &ul_config_pdu->uci_harq_pdu.harq_information, cce_idxP);
  LOG_D(MAC,
	"Filled in UCI HARQ request for rnti %x SF %d.%d acknakSF %d.%d, cce_idxP %d-> n1_pucch %d\n",
	rntiP, absSFP / 10, absSFP % 10, ackNAK_absSF / 10,
	ackNAK_absSF % 10, cce_idxP,
	ul_config_pdu->uci_harq_pdu.
	harq_information.harq_information_rel9_fdd.n_pucch_1_0);

  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;

  return (((ackNAK_absSF / 10) << 4) + (ackNAK_absSF % 10));
1459
}
1460

1461 1462 1463 1464
void
fill_nfapi_dlsch_config(eNB_MAC_INST * eNB,
			nfapi_dl_config_request_body_t * dl_req,
			uint16_t length,
1465
			int16_t pdu_index,
1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487
			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,
			//                             uint8_t codebook_index,
			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,
			uint8_t num_bf_vector)
{
1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518
  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_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;
  //  dl_config_pdu->dlsch_pdu.dlsch_pdu_rel8.codebook_index                         = codebook_index;
  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++;
1519
}
1520

1521
uint16_t
1522 1523 1524
fill_nfapi_tx_req(nfapi_tx_request_body_t *tx_req_body,
		  uint16_t                absSF, 
		  uint16_t                pdu_length,
1525
		  int16_t                 pdu_index, 
1526
		  uint8_t                 *pdu)
1527
{
1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540
  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",
	tx_req_body->number_of_pdus, pdu_length);

  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));
1541
}
1542

1543
void
1544 1545 1546
fill_nfapi_ulsch_config_request_rel8(nfapi_ul_config_request_pdu_t *ul_config_pdu, 
				     uint8_t                        cqi_req,
				     COMMON_channels_t              *cc,
1547
				     struct LTE_PhysicalConfigDedicated *physicalConfigDedicated,
1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563
				     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, 
				     uint16_t                       size)
1564
{
1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575
  memset((void *) ul_config_pdu, 0, sizeof(nfapi_ul_config_request_pdu_t));

  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;
  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;
1576 1577
  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;
1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610
  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;
    LOG_D(MAC, "report_type %d\n",ul_config_pdu->ulsch_cqi_ri_pdu.cqi_ri_information.cqi_ri_information_rel9.report_type);

    if (cc->p_eNB <= 2
	&& (tmode == 3 || tmode == 4 || tmode == 8 || tmode == 9 || tmode == 10))
      ul_config_pdu->ulsch_cqi_ri_pdu.cqi_ri_information.cqi_ri_information_rel9.aperiodic_cqi_pmi_ri_report.cc[0].ri_size = 1;
    else if (cc->p_eNB <= 2) ul_config_pdu->ulsch_cqi_ri_pdu.cqi_ri_information.cqi_ri_information_rel9.aperiodic_cqi_pmi_ri_report.cc[0].ri_size = 0;
    else if (cc->p_eNB == 4) ul_config_pdu->ulsch_cqi_ri_pdu.cqi_ri_information.cqi_ri_information_rel9.aperiodic_cqi_pmi_ri_report.cc[0].ri_size = 2;

    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");

    for (int ri = 0; 
	 ri <  (1 << ul_config_pdu->ulsch_cqi_ri_pdu.cqi_ri_information.cqi_ri_information_rel9.aperiodic_cqi_pmi_ri_report.cc[0].ri_size); 
	 ri++)
Raymond Knopp's avatar
Raymond Knopp committed
1611
      ul_config_pdu->ulsch_cqi_ri_pdu.cqi_ri_information.cqi_ri_information_rel9.aperiodic_cqi_pmi_ri_report.cc[0].dl_cqi_pmi_size[ri] =	get_dl_cqi_pmi_size_pusch(cc,tmode,1 + ri,physicalConfigDedicated->cqi_ReportConfig->cqi_ReportModeAperiodic);
1612 1613 1614 1615

    ul_config_pdu->ulsch_cqi_ri_pdu.cqi_ri_information.cqi_ri_information_rel9.delta_offset_cqi                                       = physicalConfigDedicated->pusch_ConfigDedicated->betaOffset_CQI_Index;
    ul_config_pdu->ulsch_cqi_ri_pdu.cqi_ri_information.cqi_ri_information_rel9.delta_offset_ri                                        = physicalConfigDedicated->pusch_ConfigDedicated->betaOffset_RI_Index;
  }
1616 1617
}

1618
#if (LTE_RRC_VERSION >= MAKE_VERSION(13, 0, 0))
1619 1620 1621 1622 1623 1624 1625
void
fill_nfapi_ulsch_config_request_emtc(nfapi_ul_config_request_pdu_t *
				     ul_config_pdu, uint8_t ue_type,
				     uint16_t
				     total_number_of_repetitions,
				     uint16_t repetition_number,
				     uint16_t initial_transmission_sf_io)
Cedric Roux's avatar
Cedric Roux committed
1626
{
1627 1628 1629 1630 1631 1632 1633
  // 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;
1634
}
1635

Cedric Roux's avatar
Cedric Roux committed
1636 1637
int get_numnarrowbands(long dl_Bandwidth)
{
1638
  int nb_tab[6] = { 1, 2, 4, 8, 12, 16 };
1639

1640 1641
  AssertFatal(dl_Bandwidth < 7 || dl_Bandwidth >= 0, "dl_Bandwidth not in [0..6]\n");
  return (nb_tab[dl_Bandwidth]);
1642 1643
}

Cedric Roux's avatar
Cedric Roux committed
1644 1645
int get_numnarrowbandbits(long dl_Bandwidth)
{
1646
  int nbbits_tab[6] = { 0, 1, 2, 3, 4, 4 };
1647

1648 1649
  AssertFatal(dl_Bandwidth < 7 || dl_Bandwidth >= 0, "dl_Bandwidth not in [0..6]\n");
  return (nbbits_tab[dl_Bandwidth]);
1650 1651 1652
}

//This implements the frame/subframe condition for first subframe of MPDCCH transmission (Section 9.1.5 36.213, Rel 13/14)
1653 1654
int startSF_fdd_RA_times2[8] = { 2, 3, 4, 5, 8, 10, 16, 20 };
int startSF_tdd_RA[7] = { 1, 2, 4, 5, 8, 10, 20 };
1655

1656 1657 1658 1659
int
mpdcch_sf_condition(eNB_MAC_INST * eNB, int CC_id, frame_t frameP,
		    sub_frame_t subframeP, int rmax,
		    MPDCCH_TYPES_t mpdcch_type, int UE_id)
Cedric Roux's avatar
Cedric Roux committed
1660
{
1661
  struct LTE_PRACH_ConfigSIB_v1310 *ext4_prach =
1662 1663
    eNB->common_channels[CC_id].radioResourceConfigCommon_BR->
    ext4->prach_ConfigCommon_v1310;
1664

1665
  int T;
1666
  LTE_EPDCCH_SetConfig_r11_t *epdcch_setconfig_r11;
1667

1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683
  switch (mpdcch_type) {
  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
    AssertFatal(ext4_prach->mpdcch_startSF_CSS_RA_r13 != NULL,
		"mpdcch_startSF_CSS_RA_r13 is null\n");
    AssertFatal(rmax > 0, "rmax is 0!\b");
    if (eNB->common_channels[CC_id].tdd_Config == NULL)	//FDD
      T = rmax *startSF_fdd_RA_times2[ext4_prach->
1684 1685
				      mpdcch_startSF_CSS_RA_r13->
				      choice.fdd_r13] >> 1;
1686 1687
    else			//TDD
      T = rmax *startSF_tdd_RA[ext4_prach->
1688
			       mpdcch_startSF_CSS_RA_r13->choice.tdd_r13];
1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709
    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];

    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");

    if (eNB->common_channels[CC_id].tdd_Config == NULL)	//FDD
      T = rmax *startSF_fdd_RA_times2[epdcch_setconfig_r11->ext2->mpdcch_config_r13->choice.setup.mpdcch_StartSF_UESS_r13.choice.fdd_r13] >> 1;
    else			//TDD
      T = rmax *startSF_tdd_RA[epdcch_setconfig_r11->ext2->mpdcch_config_r13->choice.setup.mpdcch_StartSF_UESS_r13.choice.tdd_r13];
    break;
  default:
    return (0);
  }
1710

1711 1712 1713
  AssertFatal(T > 0, "T is 0!\n");
  if (((10 * frameP) + subframeP) % T == 0) return (1);
  else return (0);
1714 1715
}

1716
int narrowband_to_first_rb(COMMON_channels_t * cc, int nb_index)
Cedric Roux's avatar
Cedric Roux committed
1717
{
1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750
  switch (cc->mib->message.dl_Bandwidth) {
  case 0:			// 6 PRBs, N_NB=1, i_0=0
    return (0);
    break;
  case 3:			// 50 PRBs, N_NB=8, i_0=1
    return ((int) (1 + (6 * nb_index)));
    break;
  case 5:			// 100 PRBs, N_NB=16, i_0=2
    return ((int) (2 + (6 * nb_index)));
    break;
  case 1:			// 15 PRBs  N_NB=2, i_0=1
    if (nb_index > 0)
      return (1);
    else
      return (0);
    break;
  case 2:			// 25 PRBs, N_NB=4, i_0=0
    if (nb_index > 1)
      return (1 + (6 * nb_index));
    else
      return ((6 * nb_index));
    break;
  case 4:			// 75 PRBs, N_NB=12, i_0=1
    if (nb_index > 5)
      return (2 + (6 * nb_index));
    else
      return (1 + (6 * nb_index));
    break;
  default:
    AssertFatal(1 == 0, "Impossible dl_Bandwidth %d\n",
		(int) cc->mib->message.dl_Bandwidth);
    break;
  }
1751
}
1752 1753
#endif

1754
//------------------------------------------------------------------------------
1755
void init_ue_sched_info(void)
1756
//------------------------------------------------------------------------------
1757
{
1758 1759 1760 1761
  module_id_t i, j, k;

  for (i = 0; i < NUMBER_OF_eNB_MAX; i++) {
    for (k = 0; k < MAX_NUM_CCs; k++) {
1762
      for (j = 0; j < MAX_MOBILES_PER_ENB; j++) {
1763 1764 1765 1766 1767 1768 1769 1770 1771 1772
	// 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;
      }
1773
    }
1774
  }
1775 1776 1777 1778
}



1779
//------------------------------------------------------------------------------
1780
unsigned char get_ue_weight(module_id_t module_idP, int CC_idP, int ue_idP)
1781
//------------------------------------------------------------------------------
1782
{
1783
  return (eNB_dlsch_info[module_idP][CC_idP][ue_idP].weight);
1784 1785
}

1786
//------------------------------------------------------------------------------
1787
int find_UE_id(module_id_t mod_idP, rnti_t rntiP)
1788
//------------------------------------------------------------------------------
1789
{
1790 1791 1792
  int UE_id;
  UE_list_t *UE_list = &RC.mac[mod_idP]->UE_list;

1793
  for (UE_id = 0; UE_id < MAX_MOBILES_PER_ENB; UE_id++) {
1794 1795 1796 1797 1798
    if (UE_list->active[UE_id] != TRUE)
      continue;
    if (UE_list->UE_template[UE_PCCID(mod_idP, UE_id)][UE_id].rnti ==
	rntiP) {
      return (UE_id);
Raymond Knopp's avatar
 
Raymond Knopp committed
1799
    }
1800
  }
1801

1802
  return (-1);
1803 1804
}

1805 1806 1807 1808
//------------------------------------------------------------------------------
int find_RA_id(module_id_t mod_idP, int CC_idP, rnti_t rntiP)
//------------------------------------------------------------------------------
{
1809 1810
  int RA_id;
  AssertFatal(RC.mac[mod_idP], "RC.mac[%d] is null\n", mod_idP);
1811

1812
  RA_t *ra = (RA_t *) & RC.mac[mod_idP]->common_channels[CC_idP].ra[0];
1813

1814 1815 1816 1817
  for (RA_id = 0; RA_id < NB_RA_PROC_MAX; RA_id++) {
    LOG_D(MAC,
	  "Checking RA_id %d for %x : state %d\n",
	  RA_id, rntiP, ra[RA_id].state);
Cedric Roux's avatar
Cedric Roux committed
1818

1819 1820 1821 1822 1823
    if (ra[RA_id].state != IDLE &&
	ra[RA_id].rnti == rntiP)
      return (RA_id);
  }
  return (-1);
1824 1825
}

1826
//------------------------------------------------------------------------------
1827
int UE_num_active_CC(UE_list_t * listP, int ue_idP)
1828
//------------------------------------------------------------------------------
1829
{
1830
  return (listP->numactiveCCs[ue_idP]);
Raymond Knopp's avatar
 
Raymond Knopp committed
1831
}
1832

1833
//------------------------------------------------------------------------------
1834
int UE_PCCID(module_id_t mod_idP, int ue_idP)
1835
//------------------------------------------------------------------------------
1836
{
1837
  if (!RC.mac || !RC.mac[mod_idP]) return 0;
1838
  return (RC.mac[mod_idP]->UE_list.pCC_id[ue_idP]);
Raymond Knopp's avatar
 
Raymond Knopp committed
1839
}
1840

1841
//------------------------------------------------------------------------------
1842
rnti_t UE_RNTI(module_id_t mod_idP, int ue_idP)
1843
//------------------------------------------------------------------------------
1844
{
1845
  if (!RC.mac || !RC.mac[mod_idP]) return 0;
1846 1847 1848
  rnti_t rnti =
    RC.mac[mod_idP]->
    UE_list.UE_template[UE_PCCID(mod_idP, ue_idP)][ue_idP].rnti;
1849

1850 1851
  if (rnti > 0) {
    return (rnti);
1852
  }
1853

Robert Schmidt's avatar
Robert Schmidt committed
1854
  //LOG_D(MAC, "[eNB %d] Couldn't find RNTI for UE %d\n", mod_idP, ue_idP);
1855 1856
  //display_backtrace();
  return (NOT_A_RNTI);
1857
}
Raymond Knopp's avatar
 
Raymond Knopp committed
1858

1859
//------------------------------------------------------------------------------
1860
boolean_t is_UE_active(module_id_t mod_idP, int ue_idP)
1861
//------------------------------------------------------------------------------
1862
{
1863
  if (!RC.mac || !RC.mac[mod_idP]) return 0;
1864
  return (RC.mac[mod_idP]->UE_list.active[ue_idP]);
1865 1866
}

1867 1868
unsigned char
get_aggregation(uint8_t bw_index, uint8_t cqi, uint8_t dci_fmt)
1869
{
1870
  unsigned char aggregation = 3;
1871

1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896
  switch (dci_fmt) {
  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:
    LOG_W(MAC, "unsupported DCI format %d\n", dci_fmt);
  }
Cedric Roux's avatar
Cedric Roux committed
1897

1898
  LOG_D(MAC, "Aggregation level %d (cqi %d, bw_index %d, format %d)\n", 1 << aggregation, cqi, bw_index, dci_fmt);
Cedric Roux's avatar
Cedric Roux committed
1899

1900
  return 1 << aggregation;
1901
}
Raymond Knopp's avatar
 
Raymond Knopp committed
1902

1903
void dump_ue_list(UE_list_t * listP, int ul_flag)
1904
{
1905
  int j;
1906

1907 1908 1909
  if (ul_flag == 0) {
    for (j = listP->head; j >= 0; j = listP->next[j]) {
      LOG_T(MAC, "node %d => %d\n", j, listP->next[j]);
1910
    }
1911 1912 1913
  } else {
    for (j = listP->head_ul; j >= 0; j = listP->next_ul[j]) {
      LOG_T(MAC, "node %d => %d\n", j, listP->next_ul[j]);
1914
    }
1915
  }
Raymond Knopp's avatar
 
Raymond Knopp committed
1916 1917
}

1918
int add_new_ue(module_id_t mod_idP, int cc_idP, rnti_t rntiP, int harq_pidP
1919
#if (LTE_RRC_VERSION >= MAKE_VERSION(14, 0, 0))
1920 1921
	       , uint8_t rach_resource_type
#endif
1922
	       )
1923
{
1924 1925 1926 1927 1928 1929 1930 1931 1932 1933
  int UE_id;
  int i, j;

  UE_list_t *UE_list = &RC.mac[mod_idP]->UE_list;

  LOG_D(MAC,
	"[eNB %d, CC_id %d] Adding UE with rnti %x (next avail %d, num_UEs %d)\n",
	mod_idP, cc_idP, rntiP, UE_list->avail, UE_list->num_UEs);
  dump_ue_list(UE_list, 0);

1934
  for (i = 0; i < MAX_MOBILES_PER_ENB; i++) {
1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948
    if (UE_list->active[i] == TRUE)
      continue;
    UE_id = i;
    memset(&UE_list->UE_template[cc_idP][UE_id], 0,
	   sizeof(UE_TEMPLATE));
    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;
1949
#if defined(USRP_REC_PLAY) // not specific to record/playback ?
1950
    UE_list->UE_template[cc_idP][UE_id].pre_assigned_mcs_ul = 0;
1951
#endif    
1952

1953
#if (LTE_RRC_VERSION >= MAKE_VERSION(14, 0, 0))
1954 1955
    UE_list->UE_template[cc_idP][UE_id].rach_resource_type =
      rach_resource_type;
1956 1957
#endif

1958 1959 1960 1961
    memset((void *) &UE_list->UE_sched_ctrl[UE_id], 0,
	   sizeof(UE_sched_ctrl));
    memset((void *) &UE_list->eNB_UE_stats[cc_idP][UE_id], 0,
	   sizeof(eNB_UE_STATS));
1962
    UE_list->UE_sched_ctrl[UE_id].ue_reestablishment_reject_timer = 0;
1963

1964
    /* default slice in case there was something different */
1965 1966
    UE_list->assoc_dl_slice_idx[UE_id] = 0;
    UE_list->assoc_ul_slice_idx[UE_id] = 0;
1967

1968
    UE_list->UE_sched_ctrl[UE_id].ta_update = 31;
1969

1970 1971 1972 1973 1974
    for (j = 0; j < 8; j++) {
      UE_list->UE_template[cc_idP][UE_id].oldNDI[j] = (j == 0) ? 1 : 0;	// 1 because first transmission is with format1A (Msg4) for harq_pid 0
      UE_list->UE_template[cc_idP][UE_id].oldNDI_UL[j] = (j == harq_pidP) ? 0 : 1;	// 1st transmission is with Msg3;
      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
1975
    }
1976

1977 1978 1979 1980
    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",
	  mod_idP, UE_id, cc_idP, rntiP);
1981
    dump_ue_list(UE_list, 0);
1982 1983 1984 1985 1986 1987 1988 1989
    return (UE_id);
  }

  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");
  dump_ue_list(UE_list, 0);
  return (-1);
1990 1991
}

1992
//------------------------------------------------------------------------------
1993
int rrc_mac_remove_ue(module_id_t mod_idP, rnti_t rntiP)
1994
//------------------------------------------------------------------------------
1995
{
1996

1997
  int j;
1998
  UE_list_t *UE_list = &RC.mac[mod_idP]->UE_list;
1999
  int UE_id = find_UE_id(mod_idP,rntiP);
2000
  int pCC_id;
2001
  
2002
  if (UE_id == -1) {
2003
    LOG_W(MAC,"rrc_mac_remove_ue: UE %x not found\n", rntiP);
2004
    return 0;
2005
  }
2006 2007 2008 2009 2010 2011
  
  pCC_id = UE_PCCID(mod_idP,UE_id);
  
  LOG_I(MAC,"Removing UE %d from Primary CC_id %d (rnti %x)\n",UE_id,pCC_id, rntiP);
  dump_ue_list(UE_list,0);
  
2012 2013
  UE_list->active[UE_id] = FALSE;
  UE_list->num_UEs--;
2014 2015 2016 2017 2018 2019
  
  if (UE_list->head == UE_id) UE_list->head=UE_list->next[UE_id];
  else UE_list->next[prev(UE_list,UE_id,0)]=UE_list->next[UE_id];
  if (UE_list->head_ul == UE_id) UE_list->head_ul=UE_list->next_ul[UE_id];
  else UE_list->next_ul[prev(UE_list,UE_id,0)]=UE_list->next_ul[UE_id];
  
2020
  // clear all remaining pending transmissions
2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059
  /*  UE_list->UE_template[pCC_id][UE_id].bsr_info[LCGID0]  = 0;
      UE_list->UE_template[pCC_id][UE_id].bsr_info[LCGID1]  = 0;
      UE_list->UE_template[pCC_id][UE_id].bsr_info[LCGID2]  = 0;
      UE_list->UE_template[pCC_id][UE_id].bsr_info[LCGID3]  = 0;
      
      UE_list->UE_template[pCC_id][UE_id].ul_SR             = 0;
      UE_list->UE_template[pCC_id][UE_id].rnti              = NOT_A_RNTI;
      UE_list->UE_template[pCC_id][UE_id].ul_active         = FALSE;
  */
  memset (&UE_list->UE_template[pCC_id][UE_id],0,sizeof(UE_TEMPLATE));
  
  UE_list->eNB_UE_stats[pCC_id][UE_id].total_rbs_used = 0;
  UE_list->eNB_UE_stats[pCC_id][UE_id].total_rbs_used_retx = 0;
  for ( j = 0; j < NB_RB_MAX; j++ ) {
    UE_list->eNB_UE_stats[pCC_id][UE_id].num_pdu_tx[j] = 0;
    UE_list->eNB_UE_stats[pCC_id][UE_id].num_bytes_tx[j] = 0;
  }
  UE_list->eNB_UE_stats[pCC_id][UE_id].num_retransmission = 0;
  UE_list->eNB_UE_stats[pCC_id][UE_id].total_sdu_bytes = 0;
  UE_list->eNB_UE_stats[pCC_id][UE_id].total_pdu_bytes = 0;
  UE_list->eNB_UE_stats[pCC_id][UE_id].total_num_pdus = 0;
  UE_list->eNB_UE_stats[pCC_id][UE_id].total_rbs_used_rx = 0;
  for ( j = 0; j < NB_RB_MAX; j++ ) {
    UE_list->eNB_UE_stats[pCC_id][UE_id].num_pdu_rx[j] = 0;
    UE_list->eNB_UE_stats[pCC_id][UE_id].num_bytes_rx[j] = 0;
  }
  UE_list->eNB_UE_stats[pCC_id][UE_id].num_errors_rx = 0;
  UE_list->eNB_UE_stats[pCC_id][UE_id].total_pdu_bytes_rx = 0;
  UE_list->eNB_UE_stats[pCC_id][UE_id].total_num_pdus_rx = 0;
  UE_list->eNB_UE_stats[pCC_id][UE_id].total_num_errors_rx = 0;
  
  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;
  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;
  
  eNB_ulsch_info[mod_idP][pCC_id][UE_id].serving_num = 0;
  eNB_dlsch_info[mod_idP][pCC_id][UE_id].serving_num = 0;
  
2060
  // check if this has an RA process active
2061 2062 2063
  if(find_RA_id(mod_idP, pCC_id, rntiP) != -1) {
    cancel_ra_proc(mod_idP, pCC_id, 0, rntiP);
  }
Raymond Knopp's avatar
 
Raymond Knopp committed
2064

2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080
  pthread_mutex_lock(&rrc_release_freelist);
  if(rrc_release_info.num_UEs > 0){
    uint16_t release_total = 0;
    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){
        release_total++;
      }else{
        continue;
      }
      if(rrc_release_info.RRC_release_ctrl[release_num].rnti == rntiP){
        rrc_release_info.RRC_release_ctrl[release_num].flag = 0;
        rrc_release_info.num_UEs--;
        release_total--;
      }
      if(release_total >= rrc_release_info.num_UEs){
        break;
wujing's avatar
wujing committed
2081 2082
      }
    }
2083
  }
2084
  pthread_mutex_unlock(&rrc_release_freelist);
2085
  
2086
  return 0;
Raymond Knopp's avatar
 
Raymond Knopp committed
2087
}
2088

2089
int prev(UE_list_t * listP, int nodeP, int ul_flag)
2090
{
2091
  int j;
2092

2093 2094 2095 2096
  if (ul_flag == 0) {
    if (nodeP == listP->head) {
      return (nodeP);
    }
2097

2098 2099 2100 2101 2102 2103 2104 2105 2106
    for (j = listP->head; j >= 0; j = listP->next[j]) {
      if (listP->next[j] == nodeP) {
	return (j);
      }
    }
  } else {
    if (nodeP == listP->head_ul) {
      return (nodeP);
    }
2107

2108 2109 2110 2111
    for (j = listP->head_ul; j >= 0; j = listP->next_ul[j]) {
      if (listP->next_ul[j] == nodeP) {
	return (j);
      }
2112
    }
2113
  }
2114

2115 2116 2117 2118
  LOG_E(MAC,
	"error in prev(), could not find previous to %d in UE_list %s, should never happen, Dumping UE list\n",
	nodeP, (ul_flag == 0) ? "DL" : "UL");
  dump_ue_list(listP, ul_flag);
Raymond Knopp's avatar
 
Raymond Knopp committed
2119

2120
  return (-1);
2121
}
Raymond Knopp's avatar
 
Raymond Knopp committed
2122

2123 2124
void swap_UEs(UE_list_t * listP, int nodeiP, int nodejP, int ul_flag)
{
2125
  int prev_i, prev_j, next_i, next_j;
2126

2127 2128
  LOG_T(MAC, "Swapping UE %d,%d\n", nodeiP, nodejP);
  dump_ue_list(listP, ul_flag);
Raymond Knopp's avatar
 
Raymond Knopp committed
2129

2130 2131
  prev_i = prev(listP, nodeiP, ul_flag);
  prev_j = prev(listP, nodejP, ul_flag);
2132

2133
  AssertFatal((prev_i >= 0) && (prev_j >= 0), "swap_UEs: problem");
2134

2135 2136 2137 2138 2139 2140 2141
  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];
  }
2142

2143 2144
  LOG_T(MAC, "[%s] next_i %d, next_i, next_j %d, head %d \n",
	(ul_flag == 0) ? "DL" : "UL", next_i, next_j, listP->head);
2145

2146
  if (ul_flag == 0) {
Raymond Knopp's avatar
 
Raymond Knopp committed
2147

2148 2149 2150
    if (next_i == nodejP) {	// case ... p(i) i j n(j) ... => ... p(j) j i n(i) ...
      LOG_T(MAC,
	    "Case ... p(i) i j n(j) ... => ... p(j) j i n(i) ...\n");
2151

2152 2153
      listP->next[nodeiP] = next_j;
      listP->next[nodejP] = nodeiP;
2154

2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188
      if (nodeiP == listP->head) {	// case i j n(j)
	listP->head = nodejP;
      } else {
	listP->next[prev_i] = nodejP;
      }
    } else if (next_j == nodeiP) {	// case ... p(j) j i n(i) ... => ... p(i) i j n(j) ...
      LOG_T(MAC,
	    "Case ... p(j) j i n(i) ... => ... p(i) i j n(j) ...\n");
      listP->next[nodejP] = next_i;
      listP->next[nodeiP] = nodejP;

      if (nodejP == listP->head) {	// case j i n(i)
	listP->head = nodeiP;
      } else {
	listP->next[prev_j] = nodeiP;
      }
    } else {		// case ...  p(i) i n(i) ... p(j) j n(j) ...
      listP->next[nodejP] = next_i;
      listP->next[nodeiP] = next_j;

      if (nodeiP == listP->head) {
	LOG_T(MAC, "changing head to %d\n", nodejP);
	listP->head = nodejP;
	listP->next[prev_j] = nodeiP;
      } else if (nodejP == listP->head) {
	LOG_D(MAC, "changing head to %d\n", nodeiP);
	listP->head = nodeiP;
	listP->next[prev_i] = nodejP;
      } else {
	listP->next[prev_i] = nodejP;
	listP->next[prev_j] = nodeiP;
      }
    }
  } else {			// ul_flag
2189

2190 2191 2192
    if (next_i == nodejP) {	// case ... p(i) i j n(j) ... => ... p(j) j i n(i) ...
      LOG_T(MAC,
	    "[UL] Case ... p(i) i j n(j) ... => ... p(j) j i n(i) ...\n");
2193

2194 2195
      listP->next_ul[nodeiP] = next_j;
      listP->next_ul[nodejP] = nodeiP;
2196

2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230
      if (nodeiP == listP->head_ul) {	// case i j n(j)
	listP->head_ul = nodejP;
      } else {
	listP->next_ul[prev_i] = nodejP;
      }
    } else if (next_j == nodeiP) {	// case ... p(j) j i n(i) ... => ... p(i) i j n(j) ...
      LOG_T(MAC,
	    "[UL]Case ... p(j) j i n(i) ... => ... p(i) i j n(j) ...\n");
      listP->next_ul[nodejP] = next_i;
      listP->next_ul[nodeiP] = nodejP;

      if (nodejP == listP->head_ul) {	// case j i n(i)
	listP->head_ul = nodeiP;
      } else {
	listP->next_ul[prev_j] = nodeiP;
      }
    } else {		// case ...  p(i) i n(i) ... p(j) j n(j) ...

      listP->next_ul[nodejP] = next_i;
      listP->next_ul[nodeiP] = next_j;

      if (nodeiP == listP->head_ul) {
	LOG_T(MAC, "[UL]changing head to %d\n", nodejP);
	listP->head_ul = nodejP;
	listP->next_ul[prev_j] = nodeiP;
      } else if (nodejP == listP->head_ul) {
	LOG_T(MAC, "[UL]changing head to %d\n", nodeiP);
	listP->head_ul = nodeiP;
	listP->next_ul[prev_i] = nodejP;
      } else {
	listP->next_ul[prev_i] = nodejP;
	listP->next_ul[prev_j] = nodeiP;
      }
    }
2231
  }
2232 2233 2234 2235

  LOG_T(MAC, "After swap\n");
  dump_ue_list(listP, ul_flag);
}
2236 2237

// This has to be updated to include BSR information
2238 2239
uint8_t
UE_is_to_be_scheduled(module_id_t module_idP, int CC_id, uint8_t UE_id)
2240
{
2241 2242 2243 2244
  UE_TEMPLATE *UE_template =
    &RC.mac[module_idP]->UE_list.UE_template[CC_id][UE_id];
  UE_sched_ctrl *UE_sched_ctl =
    &RC.mac[module_idP]->UE_list.UE_sched_ctrl[UE_id];
2245

2246 2247 2248 2249 2250
  // do not schedule UE if UL is not working
  if (UE_sched_ctl->ul_failure_timer > 0)
    return (0);
  if (UE_sched_ctl->ul_out_of_sync > 0)
    return (0);
2251

2252 2253
  LOG_D(MAC, "[eNB %d][PUSCH] Checking UL requirements UE %d/%x\n",
	module_idP, UE_id, UE_RNTI(module_idP, UE_id));
2254

2255 2256
  if ((UE_template->scheduled_ul_bytes < UE_template->estimated_ul_buffer) ||
      (UE_template->ul_SR > 0) ||	// uplink scheduling request
2257 2258
      ((UE_sched_ctl->ul_inactivity_timer > 20) && (UE_sched_ctl->ul_scheduled == 0)) ||	// every 2 frames when RRC_CONNECTED
      ((UE_sched_ctl->ul_inactivity_timer > 10) && (UE_sched_ctl->ul_scheduled == 0) && (mac_eNB_get_rrc_status(module_idP, UE_RNTI(module_idP, UE_id)) < RRC_CONNECTED)))	// every Frame when not RRC_CONNECTED
2259
    {
2260
      LOG_D(MAC,
2261
	    "[eNB %d][PUSCH] UE %d/%x should be scheduled (BSR0 estimated size %d, SR %d)\n",
2262
	    module_idP, UE_id, UE_RNTI(module_idP, UE_id),
2263
	    UE_template->ul_buffer_info[LCGID0], UE_template->ul_SR);
2264
      return (1);
2265
    } else {
2266 2267
    return (0);
  }
2268 2269
}

2270
uint8_t get_tmode(module_id_t module_idP, int CC_idP, int UE_idP)
Cedric Roux's avatar
Cedric Roux committed
2271
{
2272 2273 2274
  eNB_MAC_INST *eNB = RC.mac[module_idP];
  COMMON_channels_t *cc = &eNB->common_channels[CC_idP];

2275
  LTE_PhysicalConfigDedicated_t *physicalConfigDedicated = eNB->UE_list.physicalConfigDedicated[CC_idP][UE_idP];
2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286

  if (physicalConfigDedicated == NULL) {	// RRCConnectionSetup not received by UE yet
    AssertFatal(cc->p_eNB <= 2, "p_eNB is %d, should be <2\n",
		cc->p_eNB);
    return (cc->p_eNB);
  } else {
    AssertFatal(physicalConfigDedicated->antennaInfo != NULL,
		"antennaInfo is null for CCId %d, UEid %d\n", CC_idP,
		UE_idP);

    AssertFatal(physicalConfigDedicated->antennaInfo->present !=
2287
		LTE_PhysicalConfigDedicated__antennaInfo_PR_NOTHING,
2288 2289 2290 2291
		"antennaInfo (mod_id %d, CC_id %d) is set to NOTHING\n",
		module_idP, CC_idP);

    if (physicalConfigDedicated->antennaInfo->present ==
2292
	LTE_PhysicalConfigDedicated__antennaInfo_PR_explicitValue) {
2293 2294 2295
      return (physicalConfigDedicated->antennaInfo->
	      choice.explicitValue.transmissionMode);
    } else if (physicalConfigDedicated->antennaInfo->present ==
2296
	       LTE_PhysicalConfigDedicated__antennaInfo_PR_defaultValue) {
2297 2298 2299 2300 2301 2302
      AssertFatal(cc->p_eNB <= 2, "p_eNB is %d, should be <2\n",
		  cc->p_eNB);
      return (cc->p_eNB);
    } else
      AssertFatal(1 == 0, "Shouldn't be here\n");
  }
2303 2304
}

2305 2306 2307
int8_t
get_ULharq(module_id_t module_idP, int CC_idP, uint16_t frameP,
	   uint8_t subframeP)
Cedric Roux's avatar
Cedric Roux committed
2308
{
2309 2310 2311
  uint8_t ret = -1;
  eNB_MAC_INST *eNB = RC.mac[module_idP];
  COMMON_channels_t *cc = &eNB->common_channels[CC_idP];
2312

2313 2314 2315 2316 2317 2318 2319 2320
  if (cc->tdd_Config == NULL) {	// FDD
    ret = (((frameP << 1) + subframeP) & 7);
  } else {
    switch (cc->tdd_Config->subframeAssignment) {
    case 1:
      if ((subframeP == 2) ||
	  (subframeP == 3) || (subframeP == 7) || (subframeP == 8))
	switch (subframeP) {
2321 2322
	case 2:
	case 3:
2323 2324 2325 2326 2327 2328 2329
	  ret = (subframeP - 2);
	  break;

	case 7:
	case 8:
	  ret = (subframeP - 5);
	  break;
2330 2331

	default:
2332 2333 2334 2335 2336
	  AssertFatal(1 == 0,
		      "subframe2_harq_pid, Illegal subframe %d for TDD mode %d\n",
		      subframeP,
		      (int) cc->tdd_Config->subframeAssignment);
	  break;
2337
	}
2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377

      break;

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

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

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

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

    default:
      AssertFatal(1 == 0,
		  "subframe2_harq_pid, Unsupported TDD mode %d\n",
		  (int) cc->tdd_Config->subframeAssignment);
      break;
2378
    }
2379
  }
2380

2381 2382 2383 2384
  AssertFatal(ret != -1,
	      "invalid harq_pid(%d) at SFN/SF = %d/%d\n", (int8_t) ret,
	      frameP, subframeP);
  return ret;
2385 2386 2387
}


2388
uint16_t getRIV(uint16_t N_RB_DL, uint16_t RBstart, uint16_t Lcrbs)
2389
{
2390
  uint16_t RIV;
2391

2392 2393 2394 2395
  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);
2396

2397
  return (RIV);
2398
}
2399

2400 2401 2402
uint32_t
allocate_prbs(int UE_id, unsigned char nb_rb, int N_RB_DL,
	      uint32_t * rballoc)
2403
{
2404 2405 2406
  int i;
  uint32_t rballoc_dci = 0;
  unsigned char nb_rb_alloc = 0;
2407

2408 2409 2410 2411 2412
  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;
2413
    }
2414

2415 2416
    if (nb_rb_alloc == nb_rb) {
      return (rballoc_dci);
2417
    }
2418
  }
2419

2420 2421 2422 2423
  if ((N_RB_DL & 1) == 1) {
    if ((*rballoc >> (N_RB_DL - 1) & 1) == 0) {
      *rballoc |= (1 << (N_RB_DL - 1));
      rballoc_dci |= 1;
2424
    }
2425
  }
2426

2427
  return (rballoc_dci);
2428 2429
}

2430 2431
int get_bw_index(module_id_t module_id, uint8_t CC_id)
{
2432
  int bw_index = 0;
Cedric Roux's avatar
Cedric Roux committed
2433

2434
  int N_RB_DL = to_prb(RC.mac[module_id]->common_channels[CC_id].mib->message.dl_Bandwidth);
2435

2436 2437 2438 2439
  switch (N_RB_DL) {
  case 6:			// 1.4 MHz
    bw_index = 0;
    break;
2440

2441 2442 2443
  case 25:			// 5HMz
    bw_index = 1;
    break;
2444

2445 2446 2447
  case 50:			// 10HMz
    bw_index = 2;
    break;
2448

2449 2450 2451
  case 100:			// 20HMz
    bw_index = 3;
    break;
2452

2453 2454 2455 2456 2457 2458 2459
  default:
    bw_index = 1;
    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);
    break;
  }
2460

2461
  return bw_index;
2462 2463
}

2464 2465
int get_min_rb_unit(module_id_t module_id, uint8_t CC_id)
{
2466
  int min_rb_unit = 0;
2467
  int N_RB_DL = to_prb(RC.mac[module_id]->common_channels[CC_id].mib->message.dl_Bandwidth);
2468

2469 2470 2471 2472
  switch (N_RB_DL) {
  case 6:			// 1.4 MHz
    min_rb_unit = 1;
    break;
2473

2474 2475 2476
  case 25:			// 5HMz
    min_rb_unit = 2;
    break;
2477

2478 2479 2480
  case 50:			// 10HMz
    min_rb_unit = 3;
    break;
2481

2482 2483 2484
  case 100:			// 20HMz
    min_rb_unit = 4;
    break;
2485

2486 2487 2488 2489 2490 2491 2492
  default:
    min_rb_unit = 2;
    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);
    break;
  }
2493

2494
  return min_rb_unit;
2495
}
2496

2497 2498
uint32_t
allocate_prbs_sub(int nb_rb, int N_RB_DL, int N_RBG, uint8_t * rballoc)
2499
{
2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521
  int check = 0;		//check1=0,check2=0;
  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",
	rballoc[3], rballoc[2], rballoc[1], rballoc[0], nb_rb, N_RBG);

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

      switch (N_RB_DL) {
      case 6:
	nb_rb--;
	break;

      case 25:
	if ((check == N_RBG - 1)) {
	  nb_rb--;
	} else {
	  nb_rb -= 2;
2522
	}
2523 2524 2525 2526 2527 2528 2529 2530

	break;

      case 50:
	if ((check == N_RBG - 1)) {
	  nb_rb -= 2;
	} else {
	  nb_rb -= 3;
2531
	}
2532 2533 2534 2535 2536 2537 2538

	break;

      case 100:
	nb_rb -= 4;
	break;
      }
2539
    }
2540 2541 2542 2543
    //      printf("rb_alloc %x\n",rballoc_dci);
    check = check + 1;
    //    check1 = check1+2;
  }
2544

2545 2546 2547 2548
  // rballoc_dci = (rballoc_dci)&(0x1fff);
  LOG_T(MAC, "*********RBALLOC : %x\n", rballoc_dci);
  // exit(-1);
  return (rballoc_dci);
2549 2550
}

Cedric Roux's avatar
Cedric Roux committed
2551 2552
int get_subbandsize(uint8_t dl_Bandwidth)
{
2553
  uint8_t ss[6] = { 6, 4, 4, 6, 8, 8 };
2554

2555 2556
  AssertFatal(dl_Bandwidth < 6, "dl_Bandwidth %d is out of bounds\n",
	      dl_Bandwidth);
2557

2558
  return (ss[dl_Bandwidth]);
2559
}
Cedric Roux's avatar
Cedric Roux committed
2560

2561
int get_nb_subband(int N_RB_DL)
2562
{
2563
  int nb_sb = 0;
2564

2565 2566 2567 2568
  switch (N_RB_DL) {
  case 6:
    nb_sb = 0;
    break;
2569

2570 2571
  case 15:
    nb_sb = 4;		// sb_size =4
2572

2573 2574 2575
  case 25:
    nb_sb = 7;		// sb_size =4, 1 sb with 1PRB, 6 with 2 RBG, each has 2 PRBs
    break;
2576

2577 2578 2579
  case 50:			// sb_size =6
    nb_sb = 9;
    break;
2580

2581 2582 2583
  case 75:			// sb_size =8
    nb_sb = 10;
    break;
2584

2585 2586 2587
  case 100:			// sb_size =8 , 1 sb with 1 RBG + 12 sb with 2RBG, each RBG has 4 PRBs
    nb_sb = 13;
    break;
2588

2589 2590 2591 2592
  default:
    nb_sb = 0;
    break;
  }
2593

2594
  return nb_sb;
2595
}
2596

2597
void init_CCE_table(int module_idP, int CC_idP)
2598
{
2599
  memset(RC.mac[module_idP]->CCE_table[CC_idP], 0, 800 * sizeof(int));
Cedric Roux's avatar
Cedric Roux committed
2600
}
2601 2602


2603 2604 2605 2606 2607 2608
int
get_nCCE_offset(int *CCE_table,
		const unsigned char L,
		const int nCCE,
		const int common_dci,
		const unsigned short rnti, const unsigned char subframe)
2609
{
2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621
  int search_space_free, m, nb_candidates = 0, l, i;
  unsigned int Yk;
  /*
    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);
2622

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

2625
    for (m = nb_candidates - 1; m >= 0; m--) {
Cedric Roux's avatar
Cedric Roux committed
2626

2627 2628
      search_space_free = 1;
      for (l = 0; l < L; l++) {
Raymond Knopp's avatar
Raymond Knopp committed
2629

2630 2631 2632 2633
	//        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;
2634
	}
2635
      }
Raymond Knopp's avatar
Raymond Knopp committed
2636

2637
      if (search_space_free == 1) {
2638

2639
	//        printf("returning %d\n",m*L);
Raymond Knopp's avatar
Raymond Knopp committed
2640

2641 2642 2643 2644 2645
	for (l = 0; l < L; l++)
	  CCE_table[(m * L) + l] = 1;
	return (m * L);
      }
    }
Cedric Roux's avatar
Cedric Roux committed
2646

2647
    return (-1);
Raymond Knopp's avatar
Raymond Knopp committed
2648

2649 2650 2651 2652
  } else {			// 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;
Raymond Knopp's avatar
Raymond Knopp committed
2653

2654 2655
    for (i = 0; i <= subframe; i++)
      Yk = (Yk * 39827) % 65537;
2656

2657
    Yk = Yk % (nCCE / L);
2658

2659 2660 2661 2662 2663
    switch (L) {
    case 1:
    case 2:
      nb_candidates = 6;
      break;
2664

2665 2666 2667 2668
    case 4:
    case 8:
      nb_candidates = 2;
      break;
2669

2670 2671 2672 2673
    default:
      DevParam(L, nCCE, rnti);
      break;
    }
2674

2675 2676
    LOG_D(MAC, "rnti %x, Yk = %d, nCCE %d (nCCE/L %d),nb_cand %d\n",
	  rnti, Yk, nCCE, nCCE / L, nb_candidates);
2677

2678 2679
    for (m = 0; m < nb_candidates; m++) {
      search_space_free = 1;
2680

2681 2682 2683 2684 2685
      for (l = 0; l < L; l++) {
	int cce = (((Yk + m) % (nCCE / L)) * L) + l;
	if (cce >= nCCE || CCE_table[cce] == 1) {
	  search_space_free = 0;
	  break;
2686
	}
2687
      }
2688

2689 2690 2691
      if (search_space_free == 1) {
	for (l = 0; l < L; l++)
	  CCE_table[(((Yk + m) % (nCCE / L)) * L) + l] = 1;
2692

2693 2694
	return (((Yk + m) % (nCCE / L)) * L);
      }
2695
    }
2696 2697 2698

    return (-1);
  }
2699 2700
}

2701 2702 2703
void
dump_CCE_table(int *CCE_table, const int nCCE,
	       const unsigned short rnti, const int subframe, int L)
Cedric Roux's avatar
Cedric Roux committed
2704
{
2705 2706
  int nb_candidates = 0, i;
  unsigned int Yk;
2707

2708 2709 2710 2711 2712 2713
  printf("CCE 0: ");
  for (i = 0; i < nCCE; i++) {
    printf("%1d.", CCE_table[i]);
    if ((i & 7) == 7)
      printf("\n CCE %d: ", i);
  }
2714

2715
  Yk = (unsigned int) rnti;
Cedric Roux's avatar
Cedric Roux committed
2716

2717 2718
  for (i = 0; i <= subframe; i++)
    Yk = (Yk * 39827) % 65537;
Cedric Roux's avatar
Cedric Roux committed
2719

2720
  Yk = Yk % (nCCE / L);
Cedric Roux's avatar
Cedric Roux committed
2721

2722 2723 2724 2725 2726
  switch (L) {
  case 1:
  case 2:
    nb_candidates = 6;
    break;
Cedric Roux's avatar
Cedric Roux committed
2727

2728 2729 2730 2731
  case 4:
  case 8:
    nb_candidates = 2;
    break;
Cedric Roux's avatar
Cedric Roux committed
2732

2733 2734 2735 2736
  default:
    DevParam(L, nCCE, rnti);
    break;
  }
2737

2738 2739
  printf("rnti %x, Yk*L = %d, nCCE %d (nCCE/L %d),nb_cand*L %d\n", rnti,
	 Yk * L, nCCE, nCCE / L, nb_candidates * L);
2740 2741
}

2742 2743
uint16_t
getnquad(COMMON_channels_t * cc, uint8_t num_pdcch_symbols, uint8_t mi)
2744
{
2745
  uint16_t Nreg = 0;
2746

2747 2748
  AssertFatal(cc != NULL, "cc is null\n");
  AssertFatal(cc->mib != NULL, "cc->mib is null\n");
2749

2750 2751
  int N_RB_DL = to_prb(cc->mib->message.dl_Bandwidth);
  int phich_resource = get_phich_resource_times6(cc);
2752

2753
  uint8_t Ngroup_PHICH = (phich_resource * N_RB_DL) / 48;
2754

2755 2756
  if (((phich_resource * N_RB_DL) % 48) > 0)
    Ngroup_PHICH++;
2757

2758 2759 2760
  if (cc->Ncp == 1) {
    Ngroup_PHICH <<= 1;
  }
2761

2762
  Ngroup_PHICH *= mi;
2763

2764 2765 2766 2767 2768
  if ((num_pdcch_symbols > 0) && (num_pdcch_symbols < 4))
    switch (N_RB_DL) {
    case 6:
      Nreg = 12 + (num_pdcch_symbols - 1) * 18;
      break;
2769

2770 2771 2772
    case 25:
      Nreg = 50 + (num_pdcch_symbols - 1) * 75;
      break;
2773

2774 2775 2776
    case 50:
      Nreg = 100 + (num_pdcch_symbols - 1) * 150;
      break;
2777

2778 2779 2780
    case 100:
      Nreg = 200 + (num_pdcch_symbols - 1) * 300;
      break;
2781

2782 2783 2784 2785 2786
    default:
      return (0);
    }
  //   printf("Nreg %d (%d)\n",Nreg,Nreg - 4 - (3*Ngroup_PHICH));
  return (Nreg - 4 - (3 * Ngroup_PHICH));
2787 2788
}

2789 2790
uint16_t
getnCCE(COMMON_channels_t * cc, uint8_t num_pdcch_symbols, uint8_t mi)
2791
{
2792 2793
  AssertFatal(cc != NULL, "cc is null\n");
  return (getnquad(cc, num_pdcch_symbols, mi) / 9);
2794 2795
}

2796
uint8_t getmi(COMMON_channels_t * cc, int subframe)
Cedric Roux's avatar
Cedric Roux committed
2797
{
2798
  AssertFatal(cc != NULL, "cc is null\n");
2799

2800 2801 2802
  // for FDD
  if (cc->tdd_Config == NULL)	// FDD
    return 1;
2803

2804 2805 2806 2807 2808 2809 2810
  // for TDD
  switch (cc->tdd_Config->subframeAssignment) {
  case 0:
    if ((subframe == 0) || (subframe == 5))
      return (2);
    else
      return (1);
2811

2812
    break;
2813

2814 2815 2816 2817 2818
  case 1:
    if ((subframe == 0) || (subframe == 5))
      return (0);
    else
      return (1);
2819

2820
    break;
2821

2822 2823 2824 2825 2826
  case 2:
    if ((subframe == 3) || (subframe == 8))
      return (1);
    else
      return (0);
2827

2828
    break;
2829

2830 2831 2832 2833 2834
  case 3:
    if ((subframe == 0) || (subframe == 8) || (subframe == 9))
      return (1);
    else
      return (0);
2835

2836
    break;
2837

2838 2839 2840 2841 2842
  case 4:
    if ((subframe == 8) || (subframe == 9))
      return (1);
    else
      return (0);
2843

2844
    break;
2845

2846 2847 2848 2849 2850
  case 5:
    if (subframe == 8)
      return (1);
    else
      return (0);
2851

2852
    break;
2853

2854 2855 2856
  case 6:
    return (1);
    break;
2857

2858 2859 2860
  default:
    return (0);
  }
2861 2862
}

2863 2864
uint16_t
get_nCCE_max(COMMON_channels_t * cc, int num_pdcch_symbols, int subframe)
2865
{
2866 2867
  AssertFatal(cc != NULL, "cc is null\n");
  return (getnCCE(cc, num_pdcch_symbols, getmi(cc, subframe)));
2868
}
Cedric Roux's avatar
Cedric Roux committed
2869

2870
// Allocate the CCEs
2871
int
Xu Bo's avatar
Xu Bo committed
2872
allocate_CCEs(int module_idP, int CC_idP, frame_t frameP, sub_frame_t subframeP, int test_onlyP)
2873
{
2874 2875 2876 2877
  int *CCE_table = RC.mac[module_idP]->CCE_table[CC_idP];
  nfapi_dl_config_request_body_t *DL_req =
    &RC.mac[module_idP]->DL_req[CC_idP].dl_config_request_body;
  nfapi_hi_dci0_request_body_t *HI_DCI0_req =
2878
    &RC.mac[module_idP]->HI_DCI0_req[CC_idP][subframeP].hi_dci0_request_body;
2879 2880 2881 2882 2883 2884 2885 2886 2887 2888
  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];
  int nCCE_max =
    get_nCCE_max(&RC.mac[module_idP]->common_channels[CC_idP], 1,
		 subframeP);
  int fCCE;
  int i, j, idci;
  int nCCE = 0;
2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899
  int max_symbol;

  eNB_MAC_INST *eNB = RC.mac[module_idP];
  COMMON_channels_t *cc = &eNB->common_channels[CC_idP];
  int ackNAK_absSF = get_pucch1_absSF(cc, (frameP*10+subframeP));
  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;
2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927

  LOG_D(MAC,
	"Allocate CCEs subframe %d, test %d : (DL PDU %d, DL DCI %d, UL %d)\n",
	subframeP, test_onlyP, DL_req->number_pdu, DL_req->number_dci,
	HI_DCI0_req->number_of_dci);
  DL_req->number_pdcch_ofdm_symbols = 1;

 try_again:
  init_CCE_table(module_idP, CC_idP);
  nCCE = 0;

  for (i = 0, idci = 0; i < DL_req->number_pdu; i++) {
    // allocate DL common DCIs first
    if ((dl_config_pdu[i].pdu_type == NFAPI_DL_CONFIG_DCI_DL_PDU_TYPE)
	&& (dl_config_pdu[i].dci_dl_pdu.dci_dl_pdu_rel8.rnti_type ==
	    2)) {
      LOG_D(MAC,
	    "Trying to allocate COMMON DCI %d/%d (%d,%d) : rnti %x, aggreg %d nCCE %d / %d (num_pdcch_symbols %d)\n",
	    idci, DL_req->number_dci + HI_DCI0_req->number_of_dci,
	    DL_req->number_dci, HI_DCI0_req->number_of_dci,
	    dl_config_pdu[i].dci_dl_pdu.dci_dl_pdu_rel8.rnti,
	    dl_config_pdu[i].dci_dl_pdu.
	    dci_dl_pdu_rel8.aggregation_level, nCCE, nCCE_max,
	    DL_req->number_pdcch_ofdm_symbols);

      if (nCCE +
	  (dl_config_pdu[i].dci_dl_pdu.
	   dci_dl_pdu_rel8.aggregation_level) > nCCE_max) {
2928
	if (DL_req->number_pdcch_ofdm_symbols == max_symbol)
2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949
	  goto failed;
	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++;
	nCCE_max =
	  get_nCCE_max(&RC.mac[module_idP]->
		       common_channels[CC_idP],
		       DL_req->number_pdcch_ofdm_symbols,
		       subframeP);
	goto try_again;
      }
      // number of CCEs left can potentially hold this allocation
      fCCE = get_nCCE_offset(CCE_table,
			     dl_config_pdu[i].
			     dci_dl_pdu.dci_dl_pdu_rel8.
			     aggregation_level, nCCE_max, 1,
			     dl_config_pdu[i].
			     dci_dl_pdu.dci_dl_pdu_rel8.rnti,
			     subframeP);
      if (fCCE == -1) {
2950
	if (DL_req->number_pdcch_ofdm_symbols == max_symbol) {
2951 2952 2953
	  LOG_D(MAC,
		"subframe %d: Dropping Allocation for RNTI %x\n",
		subframeP,
2954
		dl_config_pdu[i].dci_dl_pdu.dci_dl_pdu_rel8.
2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976
		rnti);
	  for (j = 0; j <= i; j++) {
	    if (dl_config_pdu[j].pdu_type ==
		NFAPI_DL_CONFIG_DCI_DL_PDU_TYPE)
	      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,
		    dl_config_pdu[j].
		    dci_dl_pdu.dci_dl_pdu_rel8.rnti,
		    dl_config_pdu[j].
		    dci_dl_pdu.dci_dl_pdu_rel8.dci_format,
		    dl_config_pdu[j].
		    dci_dl_pdu.dci_dl_pdu_rel8.
		    aggregation_level, nCCE, nCCE_max,
		    DL_req->number_pdcch_ofdm_symbols);
	  }
	  //dump_CCE_table(CCE_table,nCCE_max,subframeP,dci_alloc->rnti,1<<dci_alloc->L);
	  goto failed;
2977
	}
2978 2979 2980 2981 2982 2983
	LOG_D(MAC,
	      "Can't fit DCI allocations with %d PDCCH symbols (rnti condition), increasing by 1\n",
	      DL_req->number_pdcch_ofdm_symbols);

	DL_req->number_pdcch_ofdm_symbols++;
	nCCE_max =  get_nCCE_max(&RC.mac[module_idP]->
2984 2985 2986
				 common_channels[CC_idP],
				 DL_req->number_pdcch_ofdm_symbols,
				 subframeP);
2987 2988 2989 2990 2991 2992
	goto try_again;
      }			// fCCE==-1

      // the allocation is feasible, rnti rule passes
      nCCE += dl_config_pdu[i].dci_dl_pdu.dci_dl_pdu_rel8.aggregation_level;
      LOG_D(MAC, "Allocating at nCCE %d\n", fCCE);
2993
      if ((test_onlyP%2) == 0) {
2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020
	dl_config_pdu[i].dci_dl_pdu.dci_dl_pdu_rel8.cce_idx = fCCE;
	LOG_D(MAC,
	      "Allocate COMMON DCI CCEs subframe %d, test %d => L %d fCCE %d\n",
	      subframeP, test_onlyP,
	      dl_config_pdu[i].dci_dl_pdu.
	      dci_dl_pdu_rel8.aggregation_level, fCCE);
      }
      idci++;
    }
  }				// for i = 0 ... num_DL_DCIs

  // no try to allocate UL DCIs
  for (i = 0; i < HI_DCI0_req->number_of_dci + HI_DCI0_req->number_of_hi;
       i++) {

    // allocate UL DCIs
    if (hi_dci0_pdu[i].pdu_type == NFAPI_HI_DCI0_DCI_PDU_TYPE) {

      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",
	    idci, DL_req->number_dci + HI_DCI0_req->number_of_dci,
	    DL_req->number_dci, HI_DCI0_req->number_of_dci,
	    hi_dci0_pdu[i].dci_pdu.dci_pdu_rel8.rnti,
	    hi_dci0_pdu[i].dci_pdu.dci_pdu_rel8.aggregation_level,
	    nCCE, nCCE_max, DL_req->number_pdcch_ofdm_symbols);

      if (nCCE + (hi_dci0_pdu[i].dci_pdu.dci_pdu_rel8.aggregation_level) > nCCE_max) {
3021
	if (DL_req->number_pdcch_ofdm_symbols == max_symbol)
3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041
	  goto failed;
	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++;
	nCCE_max =
	  get_nCCE_max(&RC.mac[module_idP]->common_channels[CC_idP],
		       DL_req->number_pdcch_ofdm_symbols,
		       subframeP);
	goto try_again;
      }
      // number of CCEs left can potentially hold this allocation
      fCCE = get_nCCE_offset(CCE_table,
			     hi_dci0_pdu[i].dci_pdu.
			     dci_pdu_rel8.aggregation_level,
			     nCCE_max, 0,
			     hi_dci0_pdu[i].dci_pdu.dci_pdu_rel8.
			     rnti, subframeP);
      if (fCCE == -1) {
3042
	if (DL_req->number_pdcch_ofdm_symbols == max_symbol) {
3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065
	  LOG_D(MAC,
		"subframe %d: Dropping Allocation for RNTI %x\n",
		subframeP,
		hi_dci0_pdu[i].dci_pdu.dci_pdu_rel8.rnti);
	  for (j = 0; j <= i; j++) {
	    if (hi_dci0_pdu[j].pdu_type ==
		NFAPI_HI_DCI0_DCI_PDU_TYPE)
	      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,
		    hi_dci0_pdu[j].dci_pdu.dci_pdu_rel8.rnti,
		    hi_dci0_pdu[j].dci_pdu.dci_pdu_rel8.
		    dci_format,
		    hi_dci0_pdu[j].dci_pdu.
		    dci_pdu_rel8.aggregation_level, nCCE,
		    nCCE_max,
		    DL_req->number_pdcch_ofdm_symbols);
	  }
	  //dump_CCE_table(CCE_table,nCCE_max,subframeP,dci_alloc->rnti,1<<dci_alloc->L);
	  goto failed;
3066
	}
3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081
	LOG_D(MAC,
	      "Can't fit DCI allocations with %d PDCCH symbols (rnti condition), increasing by 1\n",
	      DL_req->number_pdcch_ofdm_symbols);

	DL_req->number_pdcch_ofdm_symbols++;
	nCCE_max =
	  get_nCCE_max(&RC.mac[module_idP]->common_channels[CC_idP],
		       DL_req->number_pdcch_ofdm_symbols,
		       subframeP);
	goto try_again;
      }			// fCCE==-1

      // the allocation is feasible, rnti rule passes
      nCCE += hi_dci0_pdu[i].dci_pdu.dci_pdu_rel8.aggregation_level;
      LOG_D(MAC, "Allocating at nCCE %d\n", fCCE);
3082
      if ((test_onlyP%2) == 0) {
3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105
	hi_dci0_pdu[i].dci_pdu.dci_pdu_rel8.cce_index = fCCE;
	LOG_D(MAC, "Allocate CCEs subframe %d, test %d\n",
	      subframeP, test_onlyP);
      }
      idci++;
    }
  }				// for i = 0 ... num_UL_DCIs

  for (i = 0; i < DL_req->number_pdu; i++) {
    // allocate DL UE specific DCIs
    if ((dl_config_pdu[i].pdu_type == NFAPI_DL_CONFIG_DCI_DL_PDU_TYPE)
	&& (dl_config_pdu[i].dci_dl_pdu.dci_dl_pdu_rel8.rnti_type ==
	    1)) {
      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",
	    idci, DL_req->number_dci + HI_DCI0_req->number_of_dci,
	    DL_req->number_dci, HI_DCI0_req->number_of_dci,
	    dl_config_pdu[i].dci_dl_pdu.dci_dl_pdu_rel8.rnti,
	    dl_config_pdu[i].dci_dl_pdu.
	    dci_dl_pdu_rel8.aggregation_level, nCCE, nCCE_max,
	    DL_req->number_pdcch_ofdm_symbols);

      if (nCCE + (dl_config_pdu[i].dci_dl_pdu.dci_dl_pdu_rel8.aggregation_level) > nCCE_max) {
3106
	if (DL_req->number_pdcch_ofdm_symbols == max_symbol)
3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123
	  goto failed;
	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++;
	nCCE_max = get_nCCE_max(&RC.mac[module_idP]->common_channels[CC_idP],
		       DL_req->number_pdcch_ofdm_symbols,
		       subframeP);
	goto try_again;
      }
      // number of CCEs left can potentially hold this allocation
      fCCE = get_nCCE_offset(CCE_table,
			     dl_config_pdu[i].dci_dl_pdu.dci_dl_pdu_rel8.aggregation_level, nCCE_max, 0,
			     dl_config_pdu[i].dci_dl_pdu.dci_dl_pdu_rel8.rnti,
			     subframeP);
      if (fCCE == -1) {
3124
	if (DL_req->number_pdcch_ofdm_symbols == max_symbol) {
3125 3126 3127
	  LOG_I(MAC,
		"subframe %d: Dropping Allocation for RNTI %x\n",
		subframeP,
3128
		dl_config_pdu[i].dci_dl_pdu.dci_dl_pdu_rel8.
3129 3130 3131
		rnti);
	  for (j = 0; j <= i; j++) {
	    if (dl_config_pdu[j].pdu_type == NFAPI_DL_CONFIG_DCI_DL_PDU_TYPE)
3132
	      LOG_D(MAC,
3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147
		    "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,
		    dl_config_pdu[j].dci_dl_pdu.dci_dl_pdu_rel8.rnti,
		    dl_config_pdu[j].dci_dl_pdu.dci_dl_pdu_rel8.dci_format,
		    dl_config_pdu[j].dci_dl_pdu.dci_dl_pdu_rel8.
		    aggregation_level, 
		    nCCE, 
		    nCCE_max,
		    DL_req->number_pdcch_ofdm_symbols);
	  }
	  //dump_CCE_table(CCE_table,nCCE_max,subframeP,dci_alloc->rnti,1<<dci_alloc->L);
	  goto failed;
3148
	}
3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163
	LOG_D(MAC,
	      "Can't fit DCI allocations with %d PDCCH symbols (rnti condition), increasing by 1\n",
	      DL_req->number_pdcch_ofdm_symbols);

	DL_req->number_pdcch_ofdm_symbols++;
	nCCE_max =
	  get_nCCE_max(&RC.mac[module_idP]->common_channels[CC_idP],
		       DL_req->number_pdcch_ofdm_symbols,
		       subframeP);
	goto try_again;
      }			// fCCE==-1

      // the allocation is feasible, rnti rule passes
      nCCE += dl_config_pdu[i].dci_dl_pdu.dci_dl_pdu_rel8.aggregation_level;
      LOG_D(MAC, "Allocating at nCCE %d\n", fCCE);
3164
      if ((test_onlyP%2) == 0) {
3165 3166 3167 3168
	dl_config_pdu[i].dci_dl_pdu.dci_dl_pdu_rel8.cce_idx = fCCE;
	LOG_D(MAC, "Allocate CCEs subframe %d, test %d\n",
	      subframeP, test_onlyP);
      }
3169 3170 3171 3172 3173 3174 3175 3176 3177 3178
      if ((test_onlyP/2) == 1) {
       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 =
3179
            cc->radioResourceConfigCommon->pucch_ConfigCommon.n1PUCCH_AN + fCCE + getNp(cc->mib->message.dl_Bandwidth,fCCE,0) ;
3180 3181 3182
        }
       }
      }
3183 3184 3185
      idci++;
    }
  }				// for i = 0 ... num_DL_DCIs
3186

3187
  return 0;
Cedric Roux's avatar
Cedric Roux committed
3188

3189 3190
 failed:
  return -1;
3191 3192
}

3193 3194 3195 3196
nfapi_ul_config_request_pdu_t *has_ul_grant(module_id_t module_idP,
					    int CC_idP, uint16_t absSFP,
					    uint16_t rnti)
{
3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252
  nfapi_ul_config_request_body_t *ul_req;
  nfapi_ul_config_request_pdu_t *ul_config_pdu;

  ul_req = &RC.mac[module_idP]->UL_req_tmp[CC_idP][absSFP % 10].ul_config_request_body;
  ul_config_pdu = &ul_req->ul_config_pdu_list[0];
  LOG_D(MAC,
	"Checking for rnti %x UL grant in subframeP %d (num pdu %d)\n",
	rnti, absSFP % 10, ul_req->number_of_pdus);

  for (int i = 0; i < ul_req->number_of_pdus; i++) {
    LOG_D(MAC, "PDU %d : type %d,rnti %x\n", i,ul_config_pdu[i].pdu_type, rnti);
    if ((ul_config_pdu[i].pdu_type == NFAPI_UL_CONFIG_ULSCH_PDU_TYPE)
	&& (ul_config_pdu[i].ulsch_pdu.ulsch_pdu_rel8.rnti == rnti))
      return (&ul_config_pdu[i]);
    if ((ul_config_pdu[i].pdu_type == NFAPI_UL_CONFIG_ULSCH_CQI_RI_PDU_TYPE)
	&& (ul_config_pdu[i].ulsch_cqi_ri_pdu.ulsch_pdu.ulsch_pdu_rel8.rnti == rnti))
      return (&ul_config_pdu[i]);
    if ((ul_config_pdu[i].pdu_type == NFAPI_UL_CONFIG_ULSCH_HARQ_PDU_TYPE)
	&& (ul_config_pdu[i].ulsch_harq_pdu.ulsch_pdu.ulsch_pdu_rel8.rnti == rnti))
      return (&ul_config_pdu[i]);
    if ((ul_config_pdu[i].pdu_type == NFAPI_UL_CONFIG_ULSCH_CQI_HARQ_RI_PDU_TYPE)
	&& (ul_config_pdu[i].ulsch_cqi_harq_ri_pdu.ulsch_pdu.ulsch_pdu_rel8.rnti == rnti))
      return (&ul_config_pdu[i]);

    if ((ul_config_pdu[i].pdu_type == NFAPI_UL_CONFIG_UCI_CQI_PDU_TYPE)
	&& (ul_config_pdu[i].uci_cqi_pdu.ue_information.ue_information_rel8.rnti == rnti))
      return (&ul_config_pdu[i]);
    if ((ul_config_pdu[i].pdu_type == NFAPI_UL_CONFIG_UCI_SR_PDU_TYPE)
	&& (ul_config_pdu[i].uci_sr_pdu.ue_information.ue_information_rel8.rnti == rnti))
      return (&ul_config_pdu[i]);
    if ((ul_config_pdu[i].pdu_type == NFAPI_UL_CONFIG_UCI_HARQ_PDU_TYPE)
	&& (ul_config_pdu[i].uci_harq_pdu.ue_information.ue_information_rel8.rnti == rnti))
      return (&ul_config_pdu[i]);
    if ((ul_config_pdu[i].pdu_type == NFAPI_UL_CONFIG_UCI_SR_HARQ_PDU_TYPE)
	&& (ul_config_pdu[i].uci_sr_harq_pdu.ue_information.ue_information_rel8.rnti == rnti))
      return (&ul_config_pdu[i]);
    if ((ul_config_pdu[i].pdu_type == NFAPI_UL_CONFIG_UCI_CQI_HARQ_PDU_TYPE)
	&& (ul_config_pdu[i].uci_cqi_harq_pdu.ue_information.ue_information_rel8.rnti == rnti))
      return (&ul_config_pdu[i]);
    if ((ul_config_pdu[i].pdu_type == NFAPI_UL_CONFIG_UCI_CQI_SR_PDU_TYPE)
	&& (ul_config_pdu[i].uci_cqi_sr_pdu.ue_information.ue_information_rel8.rnti == rnti))
      return (&ul_config_pdu[i]);
    if ((ul_config_pdu[i].pdu_type == NFAPI_UL_CONFIG_UCI_CQI_SR_HARQ_PDU_TYPE)
	&& (ul_config_pdu[i].uci_cqi_sr_harq_pdu.ue_information.ue_information_rel8.rnti == rnti))
      return (&ul_config_pdu[i]);

    if ((ul_config_pdu[i].pdu_type == NFAPI_UL_CONFIG_ULSCH_UCI_CSI_PDU_TYPE)
	&& (ul_config_pdu[i].ulsch_uci_csi_pdu.ulsch_pdu.ulsch_pdu_rel8.rnti == rnti))
      return (&ul_config_pdu[i]);
    if ((ul_config_pdu[i].pdu_type == NFAPI_UL_CONFIG_ULSCH_UCI_HARQ_PDU_TYPE)
	&& (ul_config_pdu[i].ulsch_uci_harq_pdu.ulsch_pdu.ulsch_pdu_rel8.rnti == rnti))
      return (&ul_config_pdu[i]);
    if ((ul_config_pdu[i].pdu_type == NFAPI_UL_CONFIG_ULSCH_CSI_UCI_HARQ_PDU_TYPE)
	&& (ul_config_pdu[i].ulsch_csi_uci_harq_pdu.ulsch_pdu.ulsch_pdu_rel8.rnti == rnti))
      return (&ul_config_pdu[i]);
  }
Cedric Roux's avatar
Cedric Roux committed
3253

3254
  return (NULL);		// no ul grant at all for this UE
3255
}
Cedric Roux's avatar
Cedric Roux committed
3256

3257 3258 3259 3260 3261
boolean_t
CCE_allocation_infeasible(int module_idP,
			  int CC_idP,
			  int format_flag,
			  int subframe, int aggregation, int rnti)
3262
{
3263 3264
  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];
3265
  nfapi_hi_dci0_request_body_t *HI_DCI0_req    = &RC.mac[module_idP]->HI_DCI0_req[CC_idP][subframe].hi_dci0_request_body;
3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288
  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];
  int ret;
  boolean_t res = FALSE;

  if (format_flag != 2) {	// DL DCI
    if (DL_req->number_pdu == MAX_NUM_DL_PDU) {
      LOG_W(MAC,
	    "Subframe %d: FAPI DL structure is full, skip scheduling UE %d\n",
	    subframe, rnti);
    } 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++;
      LOG_D(MAC,
	    "Subframe %d: Checking CCE feasibility format %d : (%x,%d) (%x,%d,%d)\n",
	    subframe, format_flag, rnti, aggregation,
	    dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.rnti,
	    dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.
	    aggregation_level,
	    dl_config_pdu->dci_dl_pdu.dci_dl_pdu_rel8.rnti_type);
3289
      ret = allocate_CCEs(module_idP, CC_idP, 0, subframe, 0);
3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303
      if (ret == -1) res = TRUE;
      DL_req->number_pdu--;
    }
  } else {			// ue-specific UL DCI
    if (HI_DCI0_req->number_of_dci + HI_DCI0_req->number_of_hi == MAX_NUM_HI_DCI0_PDU) {
      LOG_W(MAC,
	    "Subframe %d: FAPI UL structure is full, skip scheduling UE %d\n",
	    subframe, rnti);
    } 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++;
3304
      ret = allocate_CCEs(module_idP, CC_idP, 0, subframe, 0);
3305 3306
      if (ret == -1) res = TRUE;
      HI_DCI0_req->number_of_dci--;
3307
    }
3308
  }
3309

3310
  return res;
3311
}
3312
void get_retransmission_timing(LTE_TDD_Config_t *tdd_Config, frame_t *frameP,
3313
    sub_frame_t *subframeP)
3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325
{

    if (tdd_Config == NULL)
    {
        if (*subframeP > 1)
            *frameP = (*frameP + 1) % 1024;
        *subframeP = (*subframeP + 8) % 10;
    }
    else
    {
        switch (tdd_Config->subframeAssignment)//TODO fill in other TDD configs
        {
3326
        default: printf("%s:%d: TODO\n", __FILE__, __LINE__); abort();
3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342
        case 1:
            if (*subframeP == 0 || *subframeP == 5)
            {
                *subframeP  += 19;
                *frameP = (*frameP + *subframeP/10) % 1024;
                *subframeP %= 10;
            }
            else if (*subframeP == 4 || *subframeP == 9)
            {
                *subframeP  += 16;
                *frameP = (*frameP + *subframeP/10) % 1024;
                *subframeP %= 10;
            }
            else
            {
                AssertFatal(2 == 1,
3343
                        "Illegal dl subframe %d for tdd config %ld\n", *subframeP,
3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362
                        tdd_Config->subframeAssignment);
            }
            break;
        }
    }
}

uint8_t get_dl_subframe_count(int tdd_config_sfa, sub_frame_t subframeP){

    uint8_t tdd1[10] = {1,-1,-1,-1,2,3,-1,-1,-1,4}; // special subframes 1,6 are excluded

    switch(tdd_config_sfa){// TODO fill in other tdd configs
    case 1 :
        return tdd1[subframeP];
        break;
    }
    return -1;
}

3363
uint8_t frame_subframe2_dl_harq_pid(LTE_TDD_Config_t *tdd_Config, int abs_frameP, sub_frame_t subframeP){
3364 3365 3366 3367 3368
    int harq_pid;
    if(tdd_Config){

        switch(tdd_Config->subframeAssignment){ //TODO fill in other tdd config
        case 1:
tomita's avatar
tomita committed
3369
            harq_pid = (((frame_cnt*1024 + abs_frameP) * 4) - 1 + get_dl_subframe_count(tdd_Config->subframeAssignment,subframeP))%7;//4 dl subframe in a frame
3370 3371
            if(harq_pid < 0)
              harq_pid += 7;
3372
            LOG_D(MAC,"[frame_subframe2_dl_harq_pid] (%d,%d) calculate harq_pid ((( %d * 1024 + %d) *4) - 1 + %d) = %d \n",
3373 3374 3375 3376 3377
                    (abs_frameP+1024)%1024,subframeP,frame_cnt,abs_frameP,
                    get_dl_subframe_count(tdd_Config->subframeAssignment,subframeP),harq_pid);
            return harq_pid;
            break;
        }
3378 3379
    }else{
        return ((abs_frameP*10)+subframeP)&7;
3380 3381 3382 3383
    }
    return -1;
}

3384
unsigned char ul_ACK_subframe2M(LTE_TDD_Config_t *tdd_Config,unsigned char subframe)
3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401
{

  if (tdd_Config == NULL) {
    return(1);
  } else {
    switch (tdd_Config->subframeAssignment) {
    case 1:
        return 1; // don't ACK special subframe for now
      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 {
3402
	AssertFatal(1==0,"illegal subframe %d for tdd_config %ld\n",
3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415
		    subframe,tdd_Config->subframeAssignment);

      }

      break;
    case 3:
      if (subframe == 2) {  // ACK subframes 5 and 6
        return(2); // should be 3
      } else if (subframe == 3) { // ACK subframes 7 and 8
        return(2);  // To be updated
      } else if (subframe == 4) { // ACK subframes 9 and 0
        return(2);
      } else {
3416
	AssertFatal(1==0,"illegal subframe %d for tdd_config %ld\n",
3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427
		    subframe,tdd_Config->subframeAssignment);
      }

      break;

    case 4:
          if (subframe == 2) {  // ACK subframes 0,4 and 5
            return(3); // should be 4
          } else if (subframe == 3) { // ACK subframes 6,7,8 and 9
            return(4);
          } else {
3428
	    AssertFatal(1==0,"illegal subframe %d for tdd_config %ld\n",
3429 3430 3431 3432 3433 3434 3435 3436 3437
			subframe,tdd_Config->subframeAssignment);
          }

          break;

    case 5:
              if (subframe == 2) {  // ACK subframes 0,3,4,5,6,7,8 and 9
                return(8); // should be 3
              } else {
3438
		AssertFatal(1==0,"illegal subframe %d for tdd_config %ld\n",
3439 3440 3441 3442 3443 3444 3445 3446 3447 3448
			    subframe,tdd_Config->subframeAssignment);
              }

              break;
    }
  }

  return(0);
}

3449
unsigned char ul_ACK_subframe2dl_subframe(LTE_TDD_Config_t *tdd_Config,unsigned char subframe,unsigned char ACK_index)
3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466
{

  if (tdd_Config == NULL) {
    return((subframe<4) ? subframe+6 : subframe-4);
  } else {
    switch (tdd_Config->subframeAssignment) {
    case 3:
      if (subframe == 2) {  // ACK subframes 5 and 6
        if (ACK_index==2)
          return(1);

        return(5+ACK_index);
      } else if (subframe == 3) { // ACK subframes 7 and 8
        return(7+ACK_index);  // To be updated
      } else if (subframe == 4) { // ACK subframes 9 and 0
        return((9+ACK_index)%10);
      } else {
3467
        AssertFatal(1==0,"illegal subframe %d for tdd_config->subframeAssignment %ld\n",
3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483
              subframe,tdd_Config->subframeAssignment);
      }

      break;

    case 4:
          if (subframe == 2) {  // ACK subframes 0, 4 and 5
            //if (ACK_index==2)
            //  return(1); TBC
            if (ACK_index==2)
            return(0);

            return(4+ACK_index);
          } else if (subframe == 3) { // ACK subframes 6, 7 8 and 9
            return(6+ACK_index);  // To be updated
          } else {
3484
            AssertFatal(1==0,"illegal subframe %d for tdd_config %ld\n",
3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499
                  subframe,tdd_Config->subframeAssignment);
          }

          break;

    case 1:
      if (subframe == 2) {  // ACK subframes 5 and 6
        return(5+ACK_index);
      } else if (subframe == 3) { // ACK subframe 9
        return(9);  // To be updated
      } else if (subframe == 7) { // ACK subframes 0 and 1
        return(ACK_index);  // To be updated
      } else if (subframe == 8) { // ACK subframe 4
        return(4);  // To be updated
      } else {
3500
        AssertFatal(1==0,"illegal subframe %d for tdd_config %ld\n",
3501 3502 3503 3504 3505 3506 3507 3508 3509
              subframe,tdd_Config->subframeAssignment);
      }

      break;
    }
  }

  return(0);
}
3510

3511 3512 3513 3514
void
extract_harq(module_id_t mod_idP, int CC_idP, int UE_id,
	     frame_t frameP, sub_frame_t subframeP,
	     void *harq_indication, int format)
Cedric Roux's avatar
Cedric Roux committed
3515
{
3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526
  UE_list_t *UE_list = &RC.mac[mod_idP]->UE_list;
  UE_sched_ctrl *sched_ctl = &UE_list->UE_sched_ctrl[UE_id];
  rnti_t rnti = UE_RNTI(mod_idP, UE_id);
  COMMON_channels_t *cc = &RC.mac[mod_idP]->common_channels[CC_idP];
  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];
3527
  int i, j, m;
3528
  uint8_t *pdu;
3529 3530 3531
  sub_frame_t subframe_tx;
  int frame_tx;
  uint8_t harq_pid;
3532
  
3533
#if (LTE_RRC_VERSION >= MAKE_VERSION(13, 0, 0))
3534 3535 3536 3537 3538 3539 3540 3541
  if (UE_list->UE_template[pCCid][UE_id].physicalConfigDedicated != NULL &&
      UE_list->UE_template[pCCid][UE_id].physicalConfigDedicated->pucch_ConfigDedicated != NULL &&
      (UE_list->UE_template[pCCid][UE_id].physicalConfigDedicated->ext7)
      && (UE_list->UE_template[pCCid][UE_id].physicalConfigDedicated->ext7->pucch_ConfigDedicated_r13)
      && (((UE_list->UE_template[pCCid][UE_id].physicalConfigDedicated->ext7->pucch_ConfigDedicated_r13->spatialBundlingPUCCH_r13) && (format == 0))
	  || ((UE_list->UE_template[pCCid][UE_id].physicalConfigDedicated->ext7->pucch_ConfigDedicated_r13->spatialBundlingPUSCH_r13)
	      && (format == 1))))
    spatial_bundling = 1;
3542
#endif
3543 3544
  for (i = 0; i < numCC; i++)
    tmode[i] = get_tmode(mod_idP, i, UE_id);
3545
  
3546 3547 3548
  if (cc->tdd_Config) {
    harq_indication_tdd = (nfapi_harq_indication_tdd_rel13_t *) harq_indication;
    //    pdu = &harq_indication_tdd->harq_tb_n[0];
3549
    
3550
    num_ack_nak = harq_indication_tdd->number_of_ack_nack;
3551
    
3552
    switch (harq_indication_tdd->mode) {
3553 3554
    case 0:		// Format 1a/b bundling
      AssertFatal(numCC == 1, "numCC %d > 1, should not be using Format1a/b\n", numCC);
3555
      int M = ul_ACK_subframe2M(cc->tdd_Config,subframeP);
3556
      for(m=0;m<M;m++){
3557 3558 3559 3560 3561 3562
	subframe_tx = ul_ACK_subframe2dl_subframe(cc->tdd_Config,subframeP,m);
	if(frameP==1023&&subframeP>5)
	  frame_tx=-1;
	else
	  frame_tx = subframeP < 4 ? frameP -1 : frameP;
	harq_pid = frame_subframe2_dl_harq_pid(cc->tdd_Config,frame_tx,subframe_tx);
Xu Bo's avatar
Xu Bo committed
3563
        RA_t *ra = &RC.mac[mod_idP]->common_channels[CC_idP].ra[0];
3564 3565 3566 3567 3568 3569 3570

       if(num_ack_nak==1){
         if(harq_indication_tdd->harq_data[0].bundling.value_0==1){ //ack
           sched_ctl->round[CC_idP][harq_pid] = 8; // release HARQ process
           sched_ctl->tbcnt[CC_idP][harq_pid] = 0;
           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]);
         }else{ //nack
3571
	   if( sched_ctl->round[CC_idP][harq_pid]<8) sched_ctl->round[CC_idP][harq_pid]++;
3572 3573 3574 3575 3576
           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;
           }
           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]);
3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588
      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)){
         //Msg NACK num to MAC ,remove UE
         // add UE info to freeList
         LOG_I(RRC, "put UE %x into freeList\n", rnti);
         put_UE_in_freelist(mod_idP, rnti, 1);
        }
       }
      }
         }
        }
Xu Bo's avatar
Xu Bo committed
3589
        for (uint8_t ra_i = 0; ra_i < NB_RA_PROC_MAX; ra_i++) {
3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602
        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);
         if(num_ack_nak == 1 && harq_indication_tdd->harq_data[0].bundling.value_0 == 1) {
          cancel_ra_proc(mod_idP, CC_idP, frameP, ra[ra_i].rnti);
         }else{
          if(sched_ctl->round[CC_idP][harq_pid] == 7){
           cancel_ra_proc(mod_idP, CC_idP, frameP, ra[ra_i].rnti);
          }
         }
         break;
        }
       }
      }
3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617
      break;
    case 1:		// Channel Selection
      break;
    case 2:		// Format 3
      break;
    case 3:		// Format 4
      break;
    case 4:		// Format 5
      break;
    }
  } 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];

3618
    harq_pid = ((10 * frameP) + subframeP + 10236) & 7;
3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641

    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]);

    switch (harq_indication_fdd->mode) {
    case 0:		// Format 1a/b (10.1.2.1)
      AssertFatal(numCC == 1,
		  "numCC %d > 1, should not be using Format1a/b\n",
		  numCC);
      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
	// single ACK/NAK bit
	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);
	AssertFatal(sched_ctl->round[CC_idP][harq_pid] < 8,
		    "Got ACK/NAK for inactive harq_pid %d for UE %d/%x\n",
		    harq_pid, UE_id, rnti);
	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",
		    pdu[0], harq_pid, UE_id, rnti);
	LOG_D(MAC, "Received %d for harq_pid %d\n", pdu[0],
	      harq_pid);

3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657
    RA_t *ra = &RC.mac[mod_idP]->common_channels[CC_idP].ra[0];
    for (uint8_t ra_i = 0; ra_i < NB_RA_PROC_MAX; ra_i++) {
     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",pdu[0],rnti,sched_ctl->round[CC_idP][harq_pid],frameP,subframeP);
      if(pdu[0] == 1){
       cancel_ra_proc(mod_idP, CC_idP, frameP, ra[ra_i].rnti);
      }else{
       if(sched_ctl->round[CC_idP][harq_pid] == 7){
        cancel_ra_proc(mod_idP, CC_idP, frameP, ra[ra_i].rnti);
       }
      }
      break;
     }
    }

3658 3659 3660
	if (pdu[0] == 1) {	// ACK
	  sched_ctl->round[CC_idP][harq_pid] = 8;	// release HARQ process
	  sched_ctl->tbcnt[CC_idP][harq_pid] = 0;
3661
	} else if (pdu[0] == 2 || pdu[0] == 4) {	// NAK (treat DTX as NAK)
3662
	  sched_ctl->round[CC_idP][harq_pid]++;	// increment round
3663 3664 3665 3666
          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;
          }
3667 3668 3669 3670 3671 3672 3673
          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)){
             //Msg NACK num to MAC ,remove UE
             // add UE info to freeList
             LOG_I(RRC, "put UE %x into freeList\n", rnti);
             put_UE_in_freelist(mod_idP, rnti, 1);
Xu Bo's avatar
Xu Bo committed
3674
            }
3675 3676
           }
          }
3677
        }
3678 3679
      } else {
	// one or two ACK/NAK bits
3680
	AssertFatal(num_ack_nak <= 2,
3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692
		    "num_ack_nak %d > 2 for 1 CC and TM3/4/8/9/10\n",
		    num_ack_nak);
	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)) {
	  sched_ctl->round[CC_idP][harq_pid] = 8;
	  sched_ctl->tbcnt[CC_idP][harq_pid] = 0;
	}
	if ((num_ack_nak == 2)
	    && (sched_ctl->round[CC_idP][harq_pid] < 8)
	    && (sched_ctl->tbcnt[CC_idP][harq_pid] == 1)
3693
	    && (pdu[0] == 2) && (pdu[1] == 2)) {
3694
	  sched_ctl->round[CC_idP][harq_pid]++;
3695 3696 3697 3698 3699
          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;
          }
        }
3700 3701 3702 3703 3704 3705 3706 3707 3708 3709
	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;
3710 3711 3712 3713
          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? */
          }
3714 3715 3716
	} else if ((num_ack_nak == 2)
		   && (sched_ctl->round[CC_idP][harq_pid] < 8)
		   && (sched_ctl->tbcnt[CC_idP][harq_pid] == 2)
3717
		   && (pdu[0] == 2) && (pdu[1] == 2)) {
3718
	  sched_ctl->round[CC_idP][harq_pid]++;
3719 3720 3721 3722 3723
          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;
          }
        }
3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748
	else
	  AssertFatal(1 == 0,
		      "Illegal ACK/NAK/round combination (%d,%d,%d,%d,%d) for harq_pid %d, UE %d/%x\n",
		      num_ack_nak,
		      sched_ctl->round[CC_idP][harq_pid],
		      sched_ctl->round[CC_idP][harq_pid], pdu[0],
		      pdu[1], harq_pid, UE_id, rnti);
      }
      break;
    case 1:		// FDD Channel Selection (10.1.2.2.1), must be received for 2 serving cells
      AssertFatal(numCC == 2,
		  "Should not receive harq indication with channel selection with %d active CCs\n",
		  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;
3749
	else {
3750
	  sched_ctl->round[pCCid][harq_pid]++;
3751 3752 3753
          if (sched_ctl->round[pCCid][harq_pid] == 4)
	    sched_ctl->round[pCCid][harq_pid] = 8;
        }
3754 3755
	if (pdu[1] == 1)
	  sched_ctl->round[1 - pCCid][harq_pid] = 8;
3756
	else {
3757
	  sched_ctl->round[1 - pCCid][harq_pid]++;
3758 3759 3760
	  if (sched_ctl->round[1 - pCCid][harq_pid] == 4)
	    sched_ctl->round[1 - pCCid][harq_pid] = 8;
        }
3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785
      }			// 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]);
	AssertFatal(sched_ctl->tbcnt[pCCid][harq_pid] == 2,
		    "sched_ctl->tbcnt[%d][%d] != 2 for UE %d/%x\n",
		    pCCid, harq_pid, UE_id, rnti);
	AssertFatal(sched_ctl->tbcnt[1 - pCCid][harq_pid] == 1,
		    "sched_ctl->tbcnt[%d][%d] != 1 for UE %d/%x\n",
		    1 - pCCid, harq_pid, UE_id, rnti);
	if ((pdu[0] == 1) && (pdu[1] == 1)) {	// both ACK
	  sched_ctl->round[pCCid][harq_pid] = 8;
	  sched_ctl->tbcnt[pCCid][harq_pid] = 0;
	} else if (((pdu[0] == 2) && (pdu[1] == 1)) ||
		   ((pdu[0] == 1) && (pdu[1] == 2))) {
	  sched_ctl->round[pCCid][harq_pid]++;
	  sched_ctl->tbcnt[pCCid][harq_pid] = 1;
3786 3787 3788 3789 3790
	  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 {
3791
	  sched_ctl->round[pCCid][harq_pid]++;
3792 3793 3794
	  if (sched_ctl->round[pCCid][harq_pid] == 4) {
	    sched_ctl->round[pCCid][harq_pid] = 8;
	    sched_ctl->tbcnt[pCCid][harq_pid] = 0;
Xu Bo's avatar
Xu Bo committed
3795 3796 3797
          }
        }

3798 3799
	if (pdu[2] == 1)
	  sched_ctl->round[1 - pCCid][harq_pid] = 8;
3800
	else {
3801
	  sched_ctl->round[1 - pCCid][harq_pid]++;
3802 3803 3804 3805
	  if (sched_ctl->round[1 - pCCid][harq_pid] == 4) {
	    sched_ctl->round[1 - pCCid][harq_pid] = 8;
          }
        }
3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830
      }			// 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]);
	AssertFatal(sched_ctl->tbcnt[1 - pCCid][harq_pid] == 2,
		    "sched_ctl->tbcnt[%d][%d] != 2 for UE %d/%x\n",
		    1 - pCCid, harq_pid, UE_id, rnti);
	AssertFatal(sched_ctl->tbcnt[pCCid][harq_pid] == 1,
		    "sched_ctl->tbcnt[%d][%d] != 1 for UE %d/%x\n",
		    pCCid, harq_pid, UE_id, rnti);
	if ((pdu[0] == 1) && (pdu[1] == 1)) {	// both ACK
	  sched_ctl->round[1 - pCCid][harq_pid] = 8;
	  sched_ctl->tbcnt[1 - pCCid][harq_pid] = 0;
	} else if (((pdu[0] >= 2) && (pdu[1] == 1))
		   || ((pdu[0] == 1) && (pdu[1] >= 2))) {	// one ACK
	  sched_ctl->round[1 - pCCid][harq_pid]++;
	  sched_ctl->tbcnt[1 - pCCid][harq_pid] = 1;
3831 3832 3833 3834 3835
	  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
3836
	  sched_ctl->round[1 - pCCid][harq_pid]++;
3837 3838 3839 3840 3841
	  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;
          }
        }
3842 3843 3844

	if (pdu[2] == 1)
	  sched_ctl->round[pCCid][harq_pid] = 8;
3845
	else {
3846
	  sched_ctl->round[pCCid][harq_pid]++;
3847 3848 3849 3850
	  if (sched_ctl->round[pCCid][harq_pid] == 4) {
	    sched_ctl->round[pCCid][harq_pid] = 8;
          }
        }
3851
      }			// A=3 secondary cell has 2 TBs
3852
#if MAX_NUM_CCs>1
3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878
      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]);
	AssertFatal(sched_ctl->tbcnt[0][harq_pid] == 2,
		    "sched_ctl->tbcnt[0][%d] != 2 for UE %d/%x\n",
		    harq_pid, UE_id, rnti);
	AssertFatal(sched_ctl->tbcnt[1][harq_pid] == 2,
		    "sched_ctl->tbcnt[1][%d] != 2 for UE %d/%x\n",
		    harq_pid, UE_id, rnti);
	if ((pdu[0] == 1) && (pdu[1] == 1)) {	// both ACK
	  sched_ctl->round[0][harq_pid] = 8;
	  sched_ctl->tbcnt[0][harq_pid] = 0;
	} else if (((pdu[0] >= 2) && (pdu[1] == 1))
		   || ((pdu[0] == 1) && (pdu[1] >= 2))) {	// one ACK
	  sched_ctl->round[0][harq_pid]++;
	  sched_ctl->tbcnt[0][harq_pid] = 1;
3879 3880 3881 3882 3883
	  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
3884
	  sched_ctl->round[0][harq_pid]++;
3885 3886 3887 3888 3889
	  if (sched_ctl->round[0][harq_pid] == 4) {
	    sched_ctl->round[0][harq_pid] = 8;
	    sched_ctl->tbcnt[0][harq_pid] = 0;
          }
        }
3890 3891 3892 3893 3894 3895 3896 3897

	if ((pdu[2] == 1) && (pdu[3] == 1)) {	// both ACK
	  sched_ctl->round[1][harq_pid] = 8;
	  sched_ctl->tbcnt[1][harq_pid] = 0;
	} else if (((pdu[2] >= 2) && (pdu[3] == 1))
		   || ((pdu[2] == 1) && (pdu[3] >= 2))) {	// one ACK
	  sched_ctl->round[1][harq_pid]++;
	  sched_ctl->tbcnt[1][harq_pid] = 1;
3898 3899 3900 3901 3902
	  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
3903
	  sched_ctl->round[1][harq_pid]++;
3904 3905 3906 3907 3908
	  if (sched_ctl->round[1][harq_pid] == 4) {
	    sched_ctl->round[1][harq_pid] = 8;
	    sched_ctl->tbcnt[1][harq_pid] = 0;
          }
        }
3909
      }			// A=4 both serving cells have 2 TBs
3910
#endif
3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922
      break;
    case 2:		// Format 3
      AssertFatal(numCC > 2,
		  "Should not receive harq indication with FDD format 3 with %d < 3 active CCs\n",
		  numCC);
      for (i = 0, j = 0; i < numCC; i++) {
	if ((sched_ctl->round[i][harq_pid] < 8)) {
	  if (tmode[i] == 1 || tmode[i] == 2 || tmode[0] == 5
	      || tmode[0] == 6 || tmode[0] == 7) {
	    if (pdu[j] == 1) {
	      sched_ctl->round[i][harq_pid] = 8;
	      sched_ctl->tbcnt[i][harq_pid] = 0;
3923
	    } else if (pdu[j] == 2) {
3924
	      sched_ctl->round[i][harq_pid]++;
3925 3926 3927 3928 3929
	      if (sched_ctl->round[i][harq_pid] == 4) {
	        sched_ctl->round[i][harq_pid] = 8;
	        sched_ctl->tbcnt[i][harq_pid] = 0;
              }
            }
3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943
	    else
	      AssertFatal(1 == 0,
			  "Illegal harq_ack value for CC %d harq_pid %d (%d) UE %d/%x\n",
			  i, harq_pid, pdu[j], UE_id, rnti);
	    j++;
	  } else if (spatial_bundling == 0) {
	    if ((sched_ctl->tbcnt[i][harq_pid] == 2)
		&& (pdu[j] == 1) && (pdu[j + 1] == 1)) {
	      sched_ctl->round[i][harq_pid] = 8;
	      sched_ctl->tbcnt[i][harq_pid] = 0;
	    } else if ((sched_ctl->tbcnt[i][harq_pid] == 2)
		       && (pdu[j] == 1) && (pdu[j + 1] == 2)) {
	      sched_ctl->round[i][harq_pid]++;
	      sched_ctl->tbcnt[i][harq_pid] = 1;
3944 3945 3946 3947
	      if (sched_ctl->round[i][harq_pid] == 4) {
	        sched_ctl->round[i][harq_pid] = 8;
	        sched_ctl->tbcnt[i][harq_pid] = 0;
              }
3948 3949 3950 3951
	    } else if ((sched_ctl->tbcnt[i][harq_pid] == 2)
		       && (pdu[j] == 2) && (pdu[j + 1] == 1)) {
	      sched_ctl->round[i][harq_pid]++;
	      sched_ctl->tbcnt[i][harq_pid] = 1;
3952 3953 3954 3955
	      if (sched_ctl->round[i][harq_pid] == 4) {
	        sched_ctl->round[i][harq_pid] = 8;
	        sched_ctl->tbcnt[i][harq_pid] = 0;
              }
3956 3957 3958
	    } else if ((sched_ctl->tbcnt[i][harq_pid] == 2)
		       && (pdu[j] == 2) && (pdu[j + 1] == 2)) {
	      sched_ctl->round[i][harq_pid]++;
3959 3960 3961 3962
	      if (sched_ctl->round[i][harq_pid] == 4) {
	        sched_ctl->round[i][harq_pid] = 8;
	        sched_ctl->tbcnt[i][harq_pid] = 0;
              }
3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975
	    } else
	      AssertFatal(1 == 0,
			  "Illegal combination for CC %d harq_pid %d (%d,%d,%d) UE %d/%x\n",
			  i, harq_pid,
			  sched_ctl->tbcnt[i][harq_pid],
			  pdu[j], pdu[j + 1], UE_id, rnti);
	    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]++;
3976 3977 3978 3979
	      if (sched_ctl->round[i][harq_pid] == 4) {
	        sched_ctl->round[i][harq_pid] = 8;
	        sched_ctl->tbcnt[i][harq_pid] = 0;
              }
3980 3981 3982 3983 3984 3985
	    } else
	      AssertFatal(1 == 0,
			  "Illegal hack_nak value %d for CC %d harq_pid %d UE %d/%x\n",
			  pdu[j], i, harq_pid, UE_id, rnti);
	    j++;
	  } else
3986
	    AssertFatal(1 == 0,
3987 3988
			"Illegal value for spatial_bundling %d\n",
			spatial_bundling);
3989
	}
3990 3991 3992 3993 3994 3995 3996 3997 3998 3999
      }
      break;
    case 3:		// Format 4
      AssertFatal(1 == 0,
		  "Should not receive harq indication with Format 4\n");
      break;
    case 4:		// Format 5
      AssertFatal(1 == 0,
		  "Should not receive harq indication with Format 5\n");
      break;
4000
    }
4001
  }
4002 4003
}

4004 4005 4006 4007
void
extract_pucch_csi(module_id_t mod_idP, int CC_idP, int UE_id,
		  frame_t frameP, sub_frame_t subframeP,
		  uint8_t * pdu, uint8_t length)
Cedric Roux's avatar
Cedric Roux committed
4008
{
4009 4010 4011
  UE_list_t *UE_list = &RC.mac[mod_idP]->UE_list;
  UE_sched_ctrl *sched_ctl = &UE_list->UE_sched_ctrl[UE_id];
  COMMON_channels_t *cc = &RC.mac[mod_idP]->common_channels[CC_idP];
4012
  struct LTE_CQI_ReportPeriodic *cqi_ReportPeriodic;
4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026
  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;

  AssertFatal(UE_list->UE_template[CC_idP][UE_id].physicalConfigDedicated != NULL,
	      "physicalConfigDedicated is null for UE %d\n", UE_id);
  AssertFatal(UE_list->
	      UE_template[CC_idP][UE_id].physicalConfigDedicated->cqi_ReportConfig != NULL,
	      "cqi_ReportConfig is null for UE %d\n", UE_id);
  AssertFatal((cqi_ReportPeriodic = UE_list->UE_template[CC_idP][UE_id].physicalConfigDedicated->cqi_ReportConfig->cqi_ReportPeriodic) != NULL,
	      "cqi_ReportPeriodic is null for UE %d\n", UE_id);

  // determine feedback mode
4027 4028 4029 4030
  AssertFatal(cqi_ReportPeriodic->present != LTE_CQI_ReportPeriodic_PR_NOTHING,
	      "cqi_ReportPeriodic->present == LTE_CQI_ReportPeriodic_PR_NOTHING!\n");
  AssertFatal(cqi_ReportPeriodic->choice.setup.cqi_FormatIndicatorPeriodic.present != LTE_CQI_ReportPeriodic__setup__cqi_FormatIndicatorPeriodic_PR_NOTHING,
	      "cqi_ReportPeriodic->cqi_FormatIndicatorPeriodic.choice.setup.present == LTE_CQI_ReportPeriodic__setup__cqi_FormatIndicatorPeriodic_PR_NOTHING!\n");
4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062

  uint16_t Npd, N_OFFSET_CQI;
  int H, K, bandwidth_part, L, Lmask;
  int ri = sched_ctl->periodic_ri_received[CC_idP];

  get_csi_params(cc, cqi_ReportPeriodic, &Npd, &N_OFFSET_CQI, &H);
  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;

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

  switch (get_tmode(mod_idP, CC_idP, UE_id)) {
  case 1:
  case 2:
  case 3:
  case 7:
    no_pmi = 1;
    break;
  case 4:
  case 5:
  case 6:
    no_pmi = 0;
    break;
  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;
  }
4063

4064
  if ((cqi_ReportPeriodic->choice.setup.cqi_FormatIndicatorPeriodic.present == LTE_CQI_ReportPeriodic__setup__cqi_FormatIndicatorPeriodic_PR_widebandCQI)
4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096
      || (feedback_cnt == 0)) {
    // 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)

    if (no_pmi == 1) {	// get spatial_diffcqi if needed
      sched_ctl->periodic_wideband_cqi[CC_idP] = pdu[0] & 0xF;
      sched_ctl->periodic_wideband_spatial_diffcqi[CC_idP] =
	(pdu[0] >> 4) & 7;
    } else if ((cc->p_eNB == 2) && (ri == 1)) {
      // 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;
    } else if ((cc->p_eNB == 2) && (ri > 1)) {
      // p=2 Rank 2 wideband CQI/PMI 8 bits
      sched_ctl->periodic_wideband_cqi[CC_idP] = pdu[0] & 0xF;
      sched_ctl->periodic_wideband_spatial_diffcqi[CC_idP] =
	(pdu[0] >> 4) & 7;
      sched_ctl->periodic_wideband_pmi[CC_idP] = (pdu[0] >> 7) & 1;
    } else if ((cc->p_eNB == 4) && (ri == 1)) {
      // p=4 Rank 1 wideband CQI/PMI 8 bits
      sched_ctl->periodic_wideband_cqi[CC_idP] = pdu[0] & 0xF;
      sched_ctl->periodic_wideband_pmi[CC_idP] =
	(pdu[0] >> 4) & 0x0F;
    } else if ((cc->p_eNB == 4) && (ri > 1)) {
      // p=4 Rank 2 wideband CQI/PMI 11 bits
      sched_ctl->periodic_wideband_cqi[CC_idP] = pdu[0] & 0xF;
      sched_ctl->periodic_wideband_spatial_diffcqi[CC_idP] =
	(pdu[0] >> 4) & 7;
      sched_ctl->periodic_wideband_pmi[CC_idP] = (pdu[0] >> 7) & 0xF;
    } else
      AssertFatal(1 == 0,
		  "illegal combination p %d, ri %d, no_pmi %d\n",
		  cc->p_eNB, ri, no_pmi);
4097
  } else if (cqi_ReportPeriodic->choice.setup.cqi_FormatIndicatorPeriodic.present == LTE_CQI_ReportPeriodic__setup__cqi_FormatIndicatorPeriodic_PR_subbandCQI)
4098
    {
4099 4100 4101 4102 4103 4104 4105 4106 4107 4108
      // 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;
	sched_ctl->periodic_subband_cqi[CC_idP][(bandwidth_part * L) + ((pdu[0] >> 7) & Lmask)] =
	  pdu[0] & 0xF;
      }
4109 4110 4111
    }
}

4112 4113 4114 4115 4116
void
extract_pusch_csi(module_id_t mod_idP, int CC_idP, int UE_id,
		  frame_t frameP, sub_frame_t subframeP,
		  uint8_t * pdu, uint8_t length)
{
4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127
  UE_list_t *UE_list = &RC.mac[mod_idP]->UE_list;
  COMMON_channels_t *cc = &RC.mac[mod_idP]->common_channels[CC_idP];
  UE_sched_ctrl *sched_ctl = &UE_list->UE_sched_ctrl[UE_id];
  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;
4128
  LTE_CQI_ReportModeAperiodic_t *cqi_ReportModeAperiodic;
4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144

  AssertFatal(UE_list->UE_template[CC_idP][UE_id].physicalConfigDedicated != NULL,
	      "physicalConfigDedicated is null for UE %d\n", UE_id);
  AssertFatal(UE_list->UE_template[CC_idP][UE_id].physicalConfigDedicated->cqi_ReportConfig != NULL,
	      "cqi_ReportConfig is null for UE %d\n", UE_id);
  AssertFatal((cqi_ReportModeAperiodic = UE_list->UE_template[CC_idP][UE_id].physicalConfigDedicated->cqi_ReportConfig->cqi_ReportModeAperiodic) != NULL,
	      "cqi_ReportModeAperiodic is null for UE %d\n", UE_id);

  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) {
4145
  case LTE_CQI_ReportModeAperiodic_rm12:
4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157
    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)
    AssertFatal(tmode == 4 || tmode == 6 || tmode == 8 || tmode == 9 || tmode == 10,
		"Illegal transmission mode %d for CQI_ReportModeAperiodic_rm12\n",
		tmode);
    if (tmode <= 6) {	//Table 5.2.2.6.1-1 36.213
      if ((ri == 1) && (cc->p_eNB == 2)) {
	sched_ctl->aperiodic_wideband_cqi0[CC_idP]    = (uint8_t) (p & 0x0F);
	p >>= 4;
	for (i = 0; i < N; i++) {
	  sched_ctl->aperiodic_subband_pmi[CC_idP][i] = (uint8_t) (p & 0x03);
	  p >>= 2;
4158
	}
4159 4160 4161
      }
      if ((ri == 2) && (cc->p_eNB == 2)) {
	sched_ctl->aperiodic_wideband_cqi0[CC_idP]    = (uint8_t) (p & 0x0F);
4162
	p >>= 4;
4163
	sched_ctl->aperiodic_wideband_cqi1[CC_idP]    = (uint8_t) (p & 0x0F);
4164 4165
	p >>= 4;
	for (i = 0; i < N; i++) {
4166 4167
	  sched_ctl->aperiodic_subband_pmi[CC_idP][i] = (uint8_t) (p & 0x01);
	  p >>= 1;
4168
	}
4169 4170 4171 4172 4173 4174 4175
      }
      if ((ri == 1) && (cc->p_eNB == 4)) {
	sched_ctl->aperiodic_wideband_cqi0[CC_idP]    = (uint8_t) (p & 0x0F);
	p >>= 4;
	for (i = 0; i < N; i++) {
	  sched_ctl->aperiodic_subband_pmi[CC_idP][i] = (uint8_t) (p & 0x03);
	  p >>= 4;
4176
	}
4177 4178 4179 4180 4181 4182 4183 4184 4185
      }
      if ((ri == 2) && (cc->p_eNB == 4)) {
	sched_ctl->aperiodic_wideband_cqi0[CC_idP]    = (uint8_t) (p & 0x0F);
	p >>= 4;
	sched_ctl->aperiodic_wideband_cqi1[CC_idP]    = (uint8_t) (p & 0x0F);
	p >>= 4;
	for (i = 0; i < N; i++) {
	  sched_ctl->aperiodic_subband_pmi[CC_idP][i] = (uint8_t) (p & 0x01);
	  p >>= 4;
4186
	}
4187 4188 4189 4190 4191
      }
    }			// 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");
    }
4192

4193
    break;
4194
  case LTE_CQI_ReportModeAperiodic_rm20:
4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210
    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
    AssertFatal(tmode == 1 || tmode == 2 || tmode == 3
		|| tmode == 7,
		"Illegal transmission mode %d for CQI_ReportModeAperiodic_rm20\n",
		tmode);

    sched_ctl->aperiodic_wideband_cqi0[CC_idP] = (uint8_t) (p & 0x0F);
    p >>= 4;
    diffcqi0 = (uint8_t) (p & 0x03);
    p >>= 2;
    r = (uint8_t) (p & ((1 >> Ltab_uesel[bw]) - 1));
    reverse_index(Ntab_uesel[bw], Mtab_uesel[bw], r, v);
    for (m = 0; m < Mtab_uesel[bw]; m++)
      sched_ctl->aperiodic_subband_diffcqi0[CC_idP][v[m]] = diffcqi0;
    break;
4211
  case LTE_CQI_ReportModeAperiodic_rm22:
4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255
    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

    AssertFatal(tmode == 4 || tmode == 6 || tmode == 8 || tmode == 9
		|| tmode == 10,
		"Illegal transmission mode %d for CQI_ReportModeAperiodic_rm22\n",
		tmode);

    sched_ctl->aperiodic_wideband_cqi0[CC_idP] = (uint8_t) (p & 0x0F);
    p >>= 4;
    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;
    }
    r = (uint8_t) (p & ((1 >> Ltab_uesel[bw]) - 1));
    p >>= Ltab_uesel[bw];
    reverse_index(Ntab_uesel[bw], Mtab_uesel[bw], r, v);
    if ((ri == 1) && (cc->p_eNB == 2)) {
      pmi_uesel = p & 0x3;
      p >>= 2;
      sched_ctl->aperiodic_wideband_pmi[CC_idP] = p & 0x3;
    } else if ((ri == 2) && (cc->p_eNB == 2)) {
      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;
    }
    for (m = 0; m < Mtab_uesel[bw]; m++) {
      sched_ctl->aperiodic_subband_diffcqi0[CC_idP][v[m]] = diffcqi0;
      if (ri > 1)
	sched_ctl->aperiodic_subband_diffcqi1[CC_idP][v[m]] =
	  diffcqi1;
      sched_ctl->aperiodic_subband_pmi[CC_idP][v[m]] = pmi_uesel;
    }
    break;
4256
  case LTE_CQI_ReportModeAperiodic_rm30:
4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272
    //subband CQI no PMI (TM1/2/3/7)
    AssertFatal(tmode == 1 || tmode == 2 || tmode == 3
		|| tmode == 7,
		"Illegal transmission mode %d for CQI_ReportModeAperiodic_rm30\n",
		tmode);
    sched_ctl->aperiodic_wideband_cqi0[CC_idP] = pdu[0] >> 4;
    curbyte = 0;
    curbit = 3;
    for (i = 0; i < N; i++) {
      sched_ctl->aperiodic_subband_diffcqi0[CC_idP][i] =
	(pdu[curbyte] >> (curbit - 1)) & 0x03;
      curbit -= 2;
      if (curbit < 0) {
	curbit = 7;
	curbyte++;
      }
4273
    }
4274 4275 4276
    sched_ctl->dl_cqi[CC_idP] =
      sched_ctl->aperiodic_wideband_cqi0[CC_idP];
    break;
4277
  case LTE_CQI_ReportModeAperiodic_rm31:
4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295
    AssertFatal(0 == 1, "to be fixed, don't use p but pdu directly\n");
    //subband CQI single PMI (TM4/5/6)
    AssertFatal(tmode == 4 || tmode == 5 || tmode == 6 || tmode == 8
		|| tmode == 9
		|| tmode == 10,
		"Illegal transmission mode %d for CQI_ReportModeAperiodic_rm31\n",
		tmode);

    if ((ri == 1) && (cc->p_eNB == 2)) {
      sched_ctl->aperiodic_wideband_cqi0[CC_idP] =
	(uint8_t) (p & 0x0F);
      p >>= 4;
      for (i = 0; i < N; i++) {
	sched_ctl->aperiodic_subband_diffcqi0[CC_idP][i] =
	  (uint8_t) (p & 0x03);
	p >>= 2;
      }
      sched_ctl->aperiodic_wideband_pmi[CC_idP] = p & 0x03;
4296
    }
4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344
    if ((ri == 2) && (cc->p_eNB == 2)) {
      sched_ctl->aperiodic_wideband_cqi0[CC_idP] =
	(uint8_t) (p & 0x0F);
      p >>= 4;
      for (i = 0; i < N; i++) {
	sched_ctl->aperiodic_subband_pmi[CC_idP][i] =
	  (uint8_t) (p & 0x01);
	p >>= 1;
      }
      sched_ctl->aperiodic_wideband_cqi1[CC_idP] =
	(uint8_t) (p & 0x0F);
      p >>= 4;
      for (i = 0; i < N; i++) {
	sched_ctl->aperiodic_subband_pmi[CC_idP][i] =
	  (uint8_t) (p & 0x01);
	p >>= 1;
      }
      sched_ctl->aperiodic_wideband_pmi[CC_idP] = p & 0x01;
    }
    if ((ri == 1) && (cc->p_eNB == 4)) {
      sched_ctl->aperiodic_wideband_cqi0[CC_idP] =
	(uint8_t) (p & 0x0F);
      p >>= 4;
      for (i = 0; i < N; i++) {
	sched_ctl->aperiodic_subband_diffcqi0[CC_idP][i] =
	  (uint8_t) (p & 0x03);
	p >>= 2;
      }
      sched_ctl->aperiodic_wideband_pmi[CC_idP] = p & 0x0F;
    }
    if ((ri > 1) && (cc->p_eNB == 4)) {	// Note : 64 bits for 20 MHz
      sched_ctl->aperiodic_wideband_cqi0[CC_idP] =
	(uint8_t) (p & 0x0F);
      p >>= 4;
      for (i = 0; i < N; i++) {
	sched_ctl->aperiodic_subband_pmi[CC_idP][i] =
	  (uint8_t) (p & 0x01);
	p >>= 1;
      }
      sched_ctl->aperiodic_wideband_cqi1[CC_idP] =
	(uint8_t) (p & 0x0F);
      p >>= 4;
      for (i = 0; i < N; i++) {
	sched_ctl->aperiodic_subband_pmi[CC_idP][i] =
	  (uint8_t) (p & 0x01);
	p >>= 2;
      }
      sched_ctl->aperiodic_wideband_pmi[CC_idP] = p & 0x0F;
4345
    }
4346 4347

    break;
4348 4349
#if (LTE_RRC_VERSION >= MAKE_VERSION(12, 5, 0))
  case LTE_CQI_ReportModeAperiodic_rm32_v1250:
4350 4351 4352 4353 4354 4355 4356
    AssertFatal(tmode == 4 || tmode == 5 || tmode == 6 || tmode == 8
		|| tmode == 9
		|| tmode == 10,
		"Illegal transmission mode %d for CQI_ReportModeAperiodic_rm32\n",
		tmode);
    AssertFatal(1 == 0, "CQI_ReportModeAperiodic_rm32 to be done\n");
    break;
4357
#endif
4358 4359
#if (LTE_RRC_VERSION >= MAKE_VERSION(13, 1, 0))
  case LTE_CQI_ReportModeAperiodic_rm10_v1310:
4360 4361 4362 4363 4364 4365
    AssertFatal(tmode == 1 || tmode == 2 || tmode == 3
		|| tmode == 7,
		"Illegal transmission mode %d for CQI_ReportModeAperiodic_rm10\n",
		tmode);
    AssertFatal(1 == 0, "CQI_ReportModeAperiodic_rm10 to be done\n");
    break;
4366
  case LTE_CQI_ReportModeAperiodic_rm11_v1310:
4367 4368 4369 4370 4371 4372 4373
    AssertFatal(tmode == 4 || tmode == 5 || tmode == 6 || tmode == 8
		|| tmode == 9
		|| tmode == 10,
		"Illegal transmission mode %d for CQI_ReportModeAperiodic_rm11\n",
		tmode);
    AssertFatal(1 == 0, "CQI_ReportModeAperiodic_rm11 to be done\n");
    break;
4374
#endif /* #if (LTE_RRC_VERSION >= MAKE_VERSION(13, 1, 0)) */
4375
  }
4376 4377
}

4378 4379 4380 4381 4382
void
cqi_indication(module_id_t mod_idP, int CC_idP, frame_t frameP,
	       sub_frame_t subframeP, rnti_t rntiP,
	       nfapi_cqi_indication_rel9_t * rel9, uint8_t * pdu,
	       nfapi_ul_cqi_information_t * ul_cqi_information)
Cedric Roux's avatar
Cedric Roux committed
4383
{
4384 4385 4386 4387 4388 4389 4390
  int UE_id = find_UE_id(mod_idP, rntiP);
  UE_list_t *UE_list = &RC.mac[mod_idP]->UE_list;
  if (UE_id == -1) {
    LOG_W(MAC, "cqi_indication: UE %x not found\n", rntiP);
    return;
  }
  UE_sched_ctrl *sched_ctl = &UE_list->UE_sched_ctrl[UE_id];
4391

4392
  if (UE_id >= 0) {
4393

4394
    LOG_D(MAC,"%s() UE_id:%d channel:%d cqi:%d\n", __FUNCTION__, UE_id, ul_cqi_information->channel, ul_cqi_information->ul_cqi);
4395

4396
    if (ul_cqi_information->channel == 0) {	// PUCCH
4397

4398 4399 4400
      // extract pucch csi information before changing RI information
      extract_pucch_csi(mod_idP, CC_idP, UE_id, frameP, subframeP,
			pdu, rel9->length);
4401

4402 4403
      memcpy((void *) sched_ctl->periodic_ri_received,
	     (void *) rel9->ri, rel9->number_of_cc_reported);
4404

4405 4406 4407 4408 4409
      // SNR for PUCCH2
      sched_ctl->pucch2_snr[CC_idP] = ul_cqi_information->ul_cqi;
    } else {		//PUSCH
      memcpy((void *) sched_ctl->aperiodic_ri_received,
	     (void *) rel9->ri, rel9->number_of_cc_reported);
Cedric Roux's avatar
Cedric Roux committed
4410

4411 4412
      extract_pusch_csi(mod_idP, CC_idP, UE_id, frameP, subframeP,
			pdu, rel9->length);
4413

4414
      LOG_D(MAC,"Frame %d Subframe %d update CQI:%d\n",frameP,subframeP,sched_ctl->dl_cqi[CC_idP]);
4415 4416 4417

      sched_ctl->cqi_req_flag &= (~(1 << subframeP));
      sched_ctl->cqi_received = 1;
4418
    }
4419 4420 4421 4422 4423

    // timing advance
    sched_ctl->timing_advance = rel9->timing_advance;
    sched_ctl->timing_advance_r9 = rel9->timing_advance_r9;
  }
4424 4425
}

4426 4427 4428
void
SR_indication(module_id_t mod_idP, int cc_idP, frame_t frameP,
	      sub_frame_t subframeP, rnti_t rntiP, uint8_t ul_cqi)
4429
{
4430 4431
  T(T_ENB_MAC_SCHEDULING_REQUEST, T_INT(mod_idP), T_INT(cc_idP),
    T_INT(frameP), T_INT(subframeP), T_INT(rntiP));
Cedric Roux's avatar
Cedric Roux committed
4432

4433 4434
  int UE_id = find_UE_id(mod_idP, rntiP);
  UE_list_t *UE_list = &RC.mac[mod_idP]->UE_list;
Cedric Roux's avatar
Cedric Roux committed
4435

4436 4437 4438 4439 4440 4441
  if (UE_id != -1) {
    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);
4442

4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455
    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);
  } 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);
    LOG_D(MAC,
	  "[eNB %d][SR %x] Frame %d subframeP %d Signaling SR for UE %d (unknown UEid) on CC_id %d\n",
	  mod_idP, rntiP, frameP, subframeP, UE_id, cc_idP);
  }
4456 4457
}

4458 4459 4460
void
UL_failure_indication(module_id_t mod_idP, int cc_idP, frame_t frameP,
		      rnti_t rntiP, sub_frame_t subframeP)
4461
{
4462 4463
  int UE_id = find_UE_id(mod_idP, rntiP);
  UE_list_t *UE_list = &RC.mac[mod_idP]->UE_list;
4464

4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478
  if (UE_id != -1) {
    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",
	  mod_idP, UE_id, rntiP, frameP, subframeP, UE_id, cc_idP,
	  UE_list->UE_sched_ctrl[UE_id].ul_failure_timer);
    if (UE_list->UE_sched_ctrl[UE_id].ul_failure_timer == 0)
      UE_list->UE_sched_ctrl[UE_id].ul_failure_timer = 1;
  } 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);
    LOG_W(MAC,
	  "[eNB %d][SR %x] Frame %d subframeP %d Signaling UL Failure for UE %d (unknown UEid) on CC_id %d\n",
	  mod_idP, rntiP, frameP, subframeP, UE_id, cc_idP);
  }
4479
}
4480

4481 4482 4483 4484 4485 4486 4487
static int nack_or_dtx_reported(
    COMMON_channels_t *cc,
    nfapi_harq_indication_pdu_t *harq_pdu)
{
  int i;

  if (cc->tdd_Config) {
4488 4489 4490 4491 4492
    nfapi_harq_indication_tdd_rel13_t *hi = &harq_pdu->harq_indication_tdd_rel13;
    for (i = 0; i < hi->number_of_ack_nack; hi++)
      if (hi->harq_data[0].bundling.value_0 != 1) //only bundling is used for tdd for now
        return 1;
    return 0;
4493 4494 4495 4496 4497 4498 4499
  } else {
    nfapi_harq_indication_fdd_rel13_t *hi = &harq_pdu->harq_indication_fdd_rel13;
    for (i = 0; i < hi->number_of_ack_nack; hi++)
      if (hi->harq_tb_n[i] != 1)
        return 1;
    return 0;
  }
4500
}
4501

4502 4503 4504 4505
void
harq_indication(module_id_t mod_idP, int CC_idP, frame_t frameP,
		sub_frame_t subframeP,
		nfapi_harq_indication_pdu_t * harq_pdu)
Cedric Roux's avatar
Cedric Roux committed
4506
{
4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529
  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);
  if (UE_id == -1) {
    LOG_W(MAC, "harq_indication: UE %x not found\n", rnti);
    return;
  }
  UE_list_t *UE_list = &RC.mac[mod_idP]->UE_list;
  UE_sched_ctrl *sched_ctl = &UE_list->UE_sched_ctrl[UE_id];
  COMMON_channels_t *cc = &RC.mac[mod_idP]->common_channels[CC_idP];
  // extract HARQ Information
  LOG_D(MAC,
	"Frame %d, subframe %d: Received harq indication (%d) from UE %d/%x, ul_cqi %d\n",
	frameP, subframeP, channel, UE_id, rnti, ul_cqi);
  if (cc->tdd_Config)
    extract_harq(mod_idP, CC_idP, UE_id, frameP, subframeP,
		 (void *) &harq_pdu->harq_indication_tdd_rel13,
		 channel);
  else
    extract_harq(mod_idP, CC_idP, UE_id, frameP, subframeP,
		 (void *) &harq_pdu->harq_indication_fdd_rel13,
		 channel);
4530 4531
  /* don't care about cqi reporting if NACK/DTX is there */
  if (channel == 0 && !nack_or_dtx_reported(cc, harq_pdu)) {
4532 4533 4534
    sched_ctl->pucch1_snr[CC_idP] = ul_cqi;
    sched_ctl->pucch1_cqi_update[CC_idP] = 1;
  }
4535
}
4536 4537 4538

// Flexran Slicing functions

4539
uint16_t nb_rbs_allowed_slice(float rb_percentage, int total_rbs)
4540 4541 4542 4543
{
    return (uint16_t) floor(rb_percentage * total_rbs);
}

4544
int ue_dl_slice_membership(module_id_t mod_id, int UE_id, int slice_idx)
4545
{
4546 4547 4548 4549
  if ((slice_idx < 0)
      || (slice_idx >= RC.mac[mod_id]->slice_info.n_dl)) {
    LOG_W(MAC, "out of range slice index %d (slice ID %d)\n",
          slice_idx, RC.mac[mod_id]->slice_info.dl[slice_idx].id);
4550 4551 4552
    return 0;
  }
  return RC.mac[mod_id]->UE_list.active[UE_id] == TRUE
4553
         && RC.mac[mod_id]->UE_list.assoc_dl_slice_idx[UE_id] == slice_idx;
4554
}
4555

4556
int ue_ul_slice_membership(module_id_t mod_id, int UE_id, int slice_idx)
4557
{
4558 4559 4560 4561
  if ((slice_idx < 0)
      || (slice_idx >= RC.mac[mod_id]->slice_info.n_ul)) {
    LOG_W(MAC, "out of range slice index %d (slice ID %d)\n",
          slice_idx, RC.mac[mod_id]->slice_info.dl[slice_idx].id);
4562
    return 0;
4563
  }
4564
  return RC.mac[mod_id]->UE_list.active[UE_id] == TRUE
4565
         && RC.mac[mod_id]->UE_list.assoc_ul_slice_idx[UE_id] == slice_idx;
4566
}