O-RAN E Maintenance Release contribution for ODULOW
[o-du/phy.git] / fapi_5g / source / api / fapi2mac / nr5g_fapi_fapi2mac_api.c
1 /******************************************************************************
2 *
3 *   Copyright (c) 2019 Intel.
4 *
5 *   Licensed under the Apache License, Version 2.0 (the "License");
6 *   you may not use this file except in compliance with the License.
7 *   You may obtain a copy of the License at
8 *
9 *       http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *   Unless required by applicable law or agreed to in writing, software
12 *   distributed under the License is distributed on an "AS IS" BASIS,
13 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *   See the License for the specific language governing permissions and
15 *   limitations under the License.
16 *
17 *******************************************************************************/
18
19 /**
20  * @file This file contains implementation of all the functions used 
21  * to send APIs from FAPI to MAC
22  *
23  **/
24
25 #include <stdio.h>
26 #include "nr5g_fapi_internal.h"
27 #include "gnb_l1_l2_api.h"
28 #include "nr5g_fapi_fapi2mac_api.h"
29 #include "nr5g_fapi_fapi2mac_wls.h"
30 #include "nr5g_fapi_log.h"
31
32 static nr5g_fapi_fapi2mac_queue_t fapi2mac_q[FAPI_MAX_PHY_INSTANCES];
33 static nr5g_fapi_fapi2mac_queue_t fapi2mac_urllc_q[FAPI_MAX_PHY_INSTANCES];
34
35
36 //------------------------------------------------------------------------------
37 /** @ingroup     group_source_api_fapi2phy
38  *
39  *  @param[in]   phy_id Value of phy_id.
40  *  @param[in]   is_urllc True for urllc, false otherwise.
41  *
42  *  @return      Pointer to fapi2mac api queue.
43  *
44  *  @description This function access proper instance of fapi2mac queue.
45  *
46 **/
47 //------------------------------------------------------------------------------
48 static inline p_nr5g_fapi_fapi2mac_queue_t nr5g_fapi_fapi2mac_queue(
49     uint8_t phy_id,
50     bool is_urllc)
51 {
52     return is_urllc ? &fapi2mac_urllc_q[phy_id] : &fapi2mac_q[phy_id];
53 }
54
55 //------------------------------------------------------------------------------
56 /** @ingroup     group_lte_source_phy_fapi
57  *
58  *  @param[in]   pListElem Pointer to the ListElement
59  *
60  *  @return      void
61  *
62  *  @description This function adds a ListElement API to a Linked list which will
63  *               be sent to L1 once all APIs for a TTI are added
64  *
65 **/
66 //------------------------------------------------------------------------------
67 void nr5g_fapi_fapi2mac_add_api_to_list(
68     uint8_t phy_id,
69     p_fapi_api_queue_elem_t p_list_elem,
70     bool is_urllc)
71 {
72     p_nr5g_fapi_fapi2mac_queue_t queue = NULL;
73     p_fapi_msg_header_t p_fapi_msg_hdr = NULL;
74
75     if (!p_list_elem) {
76         return;
77     }
78
79     queue = nr5g_fapi_fapi2mac_queue(phy_id, is_urllc);
80     if (pthread_mutex_lock((pthread_mutex_t *) & queue->lock)) {
81         NR5G_FAPI_LOG(ERROR_LOG, ("unable to lock fapi2mac aggregate list"
82                 "pthread mutex"));
83         return;
84     }
85     if (queue->p_send_list_head && queue->p_send_list_tail) {
86         p_fapi_msg_hdr = (p_fapi_msg_header_t) (queue->p_send_list_head + 1);
87         p_fapi_msg_hdr->num_msg += 1;
88         queue->p_send_list_tail->p_next = p_list_elem;
89         queue->p_send_list_tail = p_list_elem;
90     } else {
91         queue->p_send_list_head = queue->p_send_list_tail = p_list_elem;
92     }
93     if (pthread_mutex_unlock((pthread_mutex_t *) & queue->lock)) {
94         NR5G_FAPI_LOG(ERROR_LOG, ("unable to unlock fapi2mac aggregate list"
95                 "pthread mutex"));
96         return;
97     }
98 }
99
100 //------------------------------------------------------------------------------
101 /** @ingroup      group_lte_source_phy_fapi
102  *
103  *  @param        A pointer to phy Instance
104  *
105  *  @return       FAPI status
106  *
107  *  @description  This function send API list to L1
108  *
109 **/
110 //------------------------------------------------------------------------------
111 void nr5g_fapi_fapi2mac_send_api_list(
112     bool is_urllc)
113 {
114     uint8_t phy_id = 0;
115     p_fapi_msg_header_t p_fapi_msg_hdr = NULL;
116     p_fapi_api_queue_elem_t p_commit_list_head = NULL;
117     p_fapi_api_queue_elem_t p_commit_list_tail = NULL;
118     p_nr5g_fapi_fapi2mac_queue_t queue = NULL;
119
120     for (phy_id = 0; phy_id < FAPI_MAX_PHY_INSTANCES; phy_id++) {
121         queue = nr5g_fapi_fapi2mac_queue(phy_id, is_urllc);
122         if (pthread_mutex_lock((pthread_mutex_t *) & queue->lock)) {
123             NR5G_FAPI_LOG(ERROR_LOG, ("unable to lock fapi2mac aggregate list"
124                     "pthread mutex"));
125             return;
126         }
127         if (queue->p_send_list_head && queue->p_send_list_tail) {
128             p_fapi_msg_hdr =
129                 (p_fapi_msg_header_t) (queue->p_send_list_head + 1);
130             if (p_fapi_msg_hdr->num_msg) {
131                 if (p_commit_list_head && p_commit_list_tail) {
132                     p_commit_list_tail->p_next = queue->p_send_list_head;
133                     p_commit_list_tail = queue->p_send_list_tail;
134                 } else {
135                     p_commit_list_head = queue->p_send_list_head;
136                     p_commit_list_tail = queue->p_send_list_tail;
137                 }
138             } else {
139                 nr5g_fapi_fapi2mac_wls_free_buffer((void *)
140                     queue->p_send_list_head);
141             }
142             queue->p_send_list_head = queue->p_send_list_tail = NULL;
143         }
144         if (pthread_mutex_unlock((pthread_mutex_t *) & queue->lock)) {
145             NR5G_FAPI_LOG(ERROR_LOG, ("unable to unlock fapi2mac aggregate list"
146                     "pthread mutex"));
147             return;
148         }
149     }
150
151     if (p_commit_list_head)
152         nr5g_fapi_fapi2mac_wls_send(p_commit_list_head, is_urllc);
153 }
154
155 //------------------------------------------------------------------------------
156 /** @ingroup     group_lte_source_phy_fapi
157  *
158  *  @param[in]   phyInstance PhyInstance Id
159  *  @param[in]   NumMessageInBlock Number of Messages in Block
160  *  @param[in]   AlignOffset Align Offset
161  *  @param[in]   MsgType Message Type
162  *  @param[in]   FrameNum Frame Number
163  *  @param[in]   subFrameNum subframe Number
164  *
165  *  @return      Pointer to the List Element structure
166  *
167  *  @description This function allocates a buffer from shared memory WLS
168  *               interface and creates a List Element structures. It then fills
169  *               all the fields with data being passed in.
170  *
171 **/
172 //------------------------------------------------------------------------------
173 p_fapi_api_queue_elem_t nr5g_fapi_fapi2mac_create_api_list_elem(
174     uint32_t msg_type,
175     uint16_t num_message_in_block,
176     uint32_t align_offset)
177 {
178     p_fapi_api_queue_elem_t p_list_elem = NULL;
179
180     p_list_elem = (p_fapi_api_queue_elem_t)
181         nr5g_fapi_fapi2mac_wls_alloc_buffer();
182     //Fill header for link list of API messages
183     if (p_list_elem) {
184         p_list_elem->msg_type = (uint8_t) msg_type;
185         p_list_elem->num_message_in_block = num_message_in_block;
186         p_list_elem->align_offset = (uint16_t) align_offset;
187         p_list_elem->msg_len = num_message_in_block * align_offset;
188         p_list_elem->p_next = NULL;
189         p_list_elem->p_tx_data_elm_list = NULL;
190         p_list_elem->time_stamp = 0;
191     }
192
193     return p_list_elem;
194 }
195
196 //------------------------------------------------------------------------------
197 /** @ingroup     group_lte_source_phy_fapi
198  *
199  *  @return      void
200  *
201  *  @description This function initializes the pthead_mutext_lock.
202  */
203 void nr5g_fapi_fapi2mac_init_api_list(
204     )
205 {
206     uint8_t phy_id = 0;
207     p_nr5g_fapi_fapi2mac_queue_t queue = NULL;
208
209     for (phy_id = 0; phy_id < FAPI_MAX_PHY_INSTANCES; phy_id++) {
210         queue = nr5g_fapi_fapi2mac_queue(phy_id, false);
211         pthread_mutex_init((pthread_mutex_t *) & queue->lock, NULL);
212         queue = nr5g_fapi_fapi2mac_queue(phy_id, true);
213         pthread_mutex_init((pthread_mutex_t *) & queue->lock, NULL);
214     }
215 }