nr_ulsch.c 7.13 KB
Newer Older
Khalid Ahmed's avatar
Khalid Ahmed committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The OpenAirInterface Software Alliance licenses this file to You under
 * the OAI Public License, Version 1.1  (the "License"); you may not use this file
 * except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.openairinterface.org/?page_id=698
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *-------------------------------------------------------------------------------
 * For more information about the OpenAirInterface (OAI) Software Alliance:
 *      contact@openairinterface.org
 */

22 23 24
/*! \file PHY/NR_TRANSPORT/nr_ulsch.c
* \brief Top-level routines for the reception of the PUSCH TS 38.211 v 15.4.0
* \author Ahmed Hussein
Khalid Ahmed's avatar
Khalid Ahmed committed
25 26 27
* \date 2019
* \version 0.1
* \company Fraunhofer IIS
28
* \email: ahmed.hussein@iis.fraunhofer.de
Khalid Ahmed's avatar
Khalid Ahmed committed
29 30 31
* \note
* \warning
*/
32

Khalid Ahmed's avatar
Khalid Ahmed committed
33 34
#include <stdint.h>
#include "PHY/NR_TRANSPORT/nr_transport_common_proto.h"
35
#include "PHY/NR_TRANSPORT/nr_ulsch.h"
36
#include "PHY/NR_REFSIG/nr_refsig.h"
Khalid Ahmed's avatar
Khalid Ahmed committed
37

38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
int16_t find_nr_ulsch(uint16_t rnti, PHY_VARS_gNB *gNB,find_type_t type) {

  uint16_t i;
  int16_t first_free_index=-1;

  AssertFatal(gNB!=NULL,"gNB is null\n");
  for (i=0; i<NUMBER_OF_NR_ULSCH_MAX; i++) {
    AssertFatal(gNB->ulsch[i]!=NULL,"gNB->ulsch[%d] is null\n",i);
    AssertFatal(gNB->ulsch[i][0]!=NULL,"gNB->ulsch[%d][0] is null\n",i);
    LOG_D(PHY,"searching for rnti %x : ulsch_index %d=> harq_mask %x, rnti %x, first_free_index %d\n", rnti,i,gNB->ulsch[i][0]->harq_mask,gNB->ulsch[i][0]->rnti,first_free_index);
    if ((gNB->ulsch[i][0]->harq_mask >0) &&
        (gNB->ulsch[i][0]->rnti==rnti))       return i;
    else if ((gNB->ulsch[i][0]->harq_mask == 0) && (first_free_index==-1)) first_free_index=i;
  }
  if (type == SEARCH_EXIST) return -1;
  if (first_free_index != -1)
    gNB->ulsch[first_free_index][0]->rnti = 0;
  return first_free_index;
}

void nr_fill_ulsch(PHY_VARS_gNB *gNB,
                   int frame,
                   int slot,
                   nfapi_nr_pusch_pdu_t *ulsch_pdu) {

 
  int ulsch_id = find_nr_ulsch(ulsch_pdu->rnti,gNB,SEARCH_EXIST_OR_FREE);
  AssertFatal( (ulsch_id>=0) && (ulsch_id<NUMBER_OF_NR_ULSCH_MAX),
              "illegal or no ulsch_id found!!! rnti %04x ulsch_id %d\n",ulsch_pdu->rnti,ulsch_id);

  NR_gNB_ULSCH_t  *ulsch = gNB->ulsch[ulsch_id][0];
  int harq_pid = ulsch_pdu->pusch_data.harq_process_id;
  ulsch->rnti = ulsch_pdu->rnti;
  //ulsch->rnti_type;
  ulsch->harq_mask |= 1<<harq_pid;
  ulsch->harq_process_id[slot] = harq_pid;

75 76 77 78
  ulsch->harq_processes[harq_pid]->frame=frame;
  ulsch->harq_processes[harq_pid]->slot=slot;
  ulsch->harq_processes[harq_pid]->handled= 0;
  ulsch->harq_processes[harq_pid]->status= NR_ACTIVE;
79
  memcpy((void*)&ulsch->harq_processes[harq_pid]->ulsch_pdu, (void*)ulsch_pdu, sizeof(nfapi_nr_pusch_pdu_t));
80 81 82 83 84

  LOG_D(PHY,"Initializing nFAPI for ULSCH, UE %d, harq_pid %d\n",ulsch_id,harq_pid);

}

85
void nr_ulsch_unscrambling(int16_t* llr,
Francesco Mani's avatar
Francesco Mani committed
86 87 88 89
                           uint32_t size,
                           uint8_t q,
                           uint32_t Nid,
                           uint32_t n_RNTI) {
90

91
  uint8_t reset;
Khalid Ahmed's avatar
Khalid Ahmed committed
92 93 94 95 96
  uint32_t x1, x2, s=0;

  reset = 1;
  x2 = (n_RNTI<<15) + Nid;

97 98
  for (uint32_t i=0; i<size; i++) {
    if ((i&0x1f)==0) {
Khalid Ahmed's avatar
Khalid Ahmed committed
99 100 101
      s = lte_gold_generic(&x1, &x2, reset);
      reset = 0;
    }
102 103
    if (((s>>(i&0x1f))&1)==1)
      llr[i] = -llr[i];
Khalid Ahmed's avatar
Khalid Ahmed committed
104
  }
105
}
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138

void nr_ulsch_unscrambling_optim(int16_t* llr,
				 uint32_t size,
				 uint8_t q,
				 uint32_t Nid,
				 uint32_t n_RNTI) {
  
#if defined(__x86_64__) || defined(__i386__)
  uint32_t x1, x2, s=0;

  x2 = (n_RNTI<<15) + Nid;

  uint8_t *s8=(uint8_t *)&s;
  __m128i *llr128 = (__m128i*)llr;
  int j=0;
  s = lte_gold_generic(&x1, &x2, 1);

  for (int i=0; i<((size>>5)+((size&0x1f) > 0 ? 1 : 0)); i++,j+=4) {
    llr128[j]   = _mm_mullo_epi16(llr128[j],byte2m128i[s8[0]]);
    llr128[j+1] = _mm_mullo_epi16(llr128[j+1],byte2m128i[s8[1]]);
    llr128[j+2] = _mm_mullo_epi16(llr128[j+2],byte2m128i[s8[2]]);
    llr128[j+3] = _mm_mullo_epi16(llr128[j+3],byte2m128i[s8[3]]);
    s = lte_gold_generic(&x1, &x2, 0);
  }
#else

    nr_ulsch_unscrambling(llr,
                          size,
                          q,
                          Nid,
                          n_RNTI);
#endif
}
Raymond Knopp's avatar
Raymond Knopp committed
139

140 141 142 143 144 145 146
#define STATSTRLEN 16384
void dump_pusch_stats(FILE *fd,PHY_VARS_gNB *gNB) {

  char output[16384];
  int stroff=0;

  for (int i=0;i<NUMBER_OF_NR_ULSCH_MAX;i++) {
147 148 149 150 151 152
    if (gNB->ulsch_stats[i].rnti>0) {
      for (int aa=0;aa<gNB->frame_parms.nb_antennas_rx;aa++) 
          if (aa==0) stroff+=sprintf(output+stroff,"ULSCH RNTI %4x: ulsch_power[%d] %d,%d ulsch_noise_power[%d] %d.%d\n", 
                                     gNB->ulsch_stats[i].rnti, 
                                     aa,gNB->ulsch_stats[i].power[aa]/10,gNB->ulsch_stats[i].power[aa]%10,
                                     aa,gNB->ulsch_stats[i].noise_power[aa]/10,gNB->ulsch_stats[i].noise_power[aa]%10);
153
          else       stroff+=sprintf(output+stroff,"                 ulsch_power[%d] %d.%d, ulsch_noise_power[%d] %d.%d\n", 
154
                                     aa,gNB->ulsch_stats[i].power[aa]/10,gNB->ulsch_stats[i].power[aa]%10,
155
                                     aa,gNB->ulsch_stats[i].noise_power[aa]/10,gNB->ulsch_stats[i].noise_power[aa]%10);
156 157 158 159 160

      AssertFatal(stroff<(STATSTRLEN-1000),"Increase STATSTRLEN\n");


      stroff+=sprintf(output+stroff,"                 round_trials %d(%1.1e):%d(%1.1e):%d(%1.1e):%d, DTX %d, current_Qm %d, current_RI %d, total_bytes RX/SCHED %d/%d\n",
161 162 163 164 165 166 167
	    gNB->ulsch_stats[i].round_trials[0],
	    (double)gNB->ulsch_stats[i].round_trials[1]/gNB->ulsch_stats[i].round_trials[0],
	    gNB->ulsch_stats[i].round_trials[1],
	    (double)gNB->ulsch_stats[i].round_trials[2]/gNB->ulsch_stats[i].round_trials[0],
	    gNB->ulsch_stats[i].round_trials[2],
	    (double)gNB->ulsch_stats[i].round_trials[3]/gNB->ulsch_stats[i].round_trials[0],
	    gNB->ulsch_stats[i].round_trials[3],
168
      gNB->ulsch_stats[i].DTX,
169 170 171 172
	    gNB->ulsch_stats[i].current_Qm,
	    gNB->ulsch_stats[i].current_RI,
	    gNB->ulsch_stats[i].total_bytes_rx,
	    gNB->ulsch_stats[i].total_bytes_tx);
173
    }   
174 175
 }
 fprintf(fd,"%s",output);
Raymond Knopp's avatar
Raymond Knopp committed
176 177 178 179 180
}

void clear_pusch_stats(PHY_VARS_gNB *gNB) {

  for (int i=0;i<NUMBER_OF_NR_ULSCH_MAX;i++)
181
    memset((void*)&gNB->ulsch_stats[i],0,sizeof(gNB->ulsch_stats[i]));
Raymond Knopp's avatar
Raymond Knopp committed
182
}
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199

NR_gNB_SCH_STATS_t *get_ulsch_stats(PHY_VARS_gNB *gNB,NR_gNB_ULSCH_t *ulsch) {
   NR_gNB_SCH_STATS_t *stats=NULL;
   int first_free=-1;
   for (int i=0;i<NUMBER_OF_NR_SCH_STATS_MAX;i++) {
       if (gNB->ulsch_stats[i].rnti == 0 && first_free == -1) {
          first_free = i;
          stats=&gNB->ulsch_stats[i];
       }
       if (gNB->ulsch_stats[i].rnti == ulsch->rnti) {
           stats=&gNB->ulsch_stats[i];
           break;
       }
   }
   return(stats);
}