Update to odulow per maintenance bronze
[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
34 //------------------------------------------------------------------------------
35 /** @ingroup     group_source_api_fapi2phy
36  *
37  *  @param[in]   p_list_elem Pointer to the ListElement
38  *
39  *  @return      void
40  *
41  *  @description This function adds a ListElement API to a Linked list which will
42  *               be sent to L1 once all APIs for a TTI are added
43  *
44 **/
45 //------------------------------------------------------------------------------
46 inline p_nr5g_fapi_fapi2mac_queue_t nr5g_fapi_fapi2mac_queue(
47     uint8_t phy_id)
48 {
49     return &fapi2mac_q[phy_id];
50 }
51
52 //------------------------------------------------------------------------------
53 /** @ingroup     group_lte_source_phy_fapi
54  *
55  *  @param[in]   pListElem Pointer to the ListElement
56  *
57  *  @return      void
58  *
59  *  @description This function adds a ListElement API to a Linked list which will
60  *               be sent to L1 once all APIs for a TTI are added
61  *
62 **/
63 //------------------------------------------------------------------------------
64 void nr5g_fapi_fapi2mac_add_api_to_list(
65     uint8_t phy_id,
66     p_fapi_api_queue_elem_t p_list_elem)
67 {
68     p_nr5g_fapi_fapi2mac_queue_t queue = NULL;
69     p_fapi_msg_header_t p_fapi_msg_hdr = NULL;
70
71     if (!p_list_elem) {
72         return;
73     }
74
75     queue = nr5g_fapi_fapi2mac_queue(phy_id);
76     if (pthread_mutex_lock((pthread_mutex_t *) & queue->lock)) {
77         NR5G_FAPI_LOG(ERROR_LOG, ("unable to lock fapi2mac aggregate list"
78                 "pthread mutex"));
79         return;
80     }
81     if (queue->p_send_list_head && queue->p_send_list_tail) {
82         p_fapi_msg_hdr = (p_fapi_msg_header_t) (queue->p_send_list_head + 1);
83         p_fapi_msg_hdr->num_msg += 1;
84         queue->p_send_list_tail->p_next = p_list_elem;
85         queue->p_send_list_tail = p_list_elem;
86     } else {
87         queue->p_send_list_head = queue->p_send_list_tail = p_list_elem;
88     }
89     if (pthread_mutex_unlock((pthread_mutex_t *) & queue->lock)) {
90         NR5G_FAPI_LOG(ERROR_LOG, ("unable to unlock fapi2mac aggregate list"
91                 "pthread mutex"));
92         return;
93     }
94 }
95
96 //------------------------------------------------------------------------------
97 /** @ingroup      group_lte_source_phy_fapi
98  *
99  *  @param        A pointer to phy Instance
100  *
101  *  @return       FAPI status
102  *
103  *  @description  This function send API list to L1
104  *
105 **/
106 //------------------------------------------------------------------------------
107 void nr5g_fapi_fapi2mac_send_api_list(
108     )
109 {
110     uint8_t phy_id = 0;
111     p_fapi_msg_header_t p_fapi_msg_hdr = NULL;
112     p_fapi_api_queue_elem_t p_commit_list_head = NULL;
113     p_fapi_api_queue_elem_t p_commit_list_tail = NULL;
114     p_nr5g_fapi_fapi2mac_queue_t queue = NULL;
115
116     for (phy_id = 0; phy_id < FAPI_MAX_PHY_INSTANCES; phy_id++) {
117         queue = nr5g_fapi_fapi2mac_queue(phy_id);
118         if (pthread_mutex_lock((pthread_mutex_t *) & queue->lock)) {
119             NR5G_FAPI_LOG(ERROR_LOG, ("unable to lock fapi2mac aggregate list"
120                     "pthread mutex"));
121             return;
122         }
123         if (queue->p_send_list_head && queue->p_send_list_tail) {
124             p_fapi_msg_hdr =
125                 (p_fapi_msg_header_t) (queue->p_send_list_head + 1);
126             if (p_fapi_msg_hdr->num_msg) {
127                 if (p_commit_list_head && p_commit_list_tail) {
128                     p_commit_list_tail->p_next = queue->p_send_list_head;
129                     p_commit_list_tail = queue->p_send_list_tail;
130                 } else {
131                     p_commit_list_head = queue->p_send_list_head;
132                     p_commit_list_tail = queue->p_send_list_tail;
133                 }
134             } else {
135                 nr5g_fapi_fapi2mac_wls_free_buffer((void *)
136                     queue->p_send_list_head);
137             }
138             queue->p_send_list_head = queue->p_send_list_tail = NULL;
139         }
140         if (pthread_mutex_unlock((pthread_mutex_t *) & queue->lock)) {
141             NR5G_FAPI_LOG(ERROR_LOG, ("unable to unlock fapi2mac aggregate list"
142                     "pthread mutex"));
143             return;
144         }
145     }
146
147     if (p_commit_list_head)
148         nr5g_fapi_fapi2mac_wls_send(p_commit_list_head);
149 }
150
151 //------------------------------------------------------------------------------
152 /** @ingroup     group_lte_source_phy_fapi
153  *
154  *  @param[in]   phyInstance PhyInstance Id
155  *  @param[in]   NumMessageInBlock Number of Messages in Block
156  *  @param[in]   AlignOffset Align Offset
157  *  @param[in]   MsgType Message Type
158  *  @param[in]   FrameNum Frame Number
159  *  @param[in]   subFrameNum subframe Number
160  *
161  *  @return      Pointer to the List Element structure
162  *
163  *  @description This function allocates a buffer from shared memory WLS
164  *               interface and creates a List Element structures. It then fills
165  *               all the fields with data being passed in.
166  *
167 **/
168 //------------------------------------------------------------------------------
169 p_fapi_api_queue_elem_t nr5g_fapi_fapi2mac_create_api_list_elem(
170     uint32_t msg_type,
171     uint16_t num_message_in_block,
172     uint32_t align_offset)
173 {
174     p_fapi_api_queue_elem_t p_list_elem = NULL;
175
176     p_list_elem = (p_fapi_api_queue_elem_t)
177         nr5g_fapi_fapi2mac_wls_alloc_buffer();
178     //Fill header for link list of API messages
179     if (p_list_elem) {
180         p_list_elem->msg_type = (uint8_t) msg_type;
181         p_list_elem->num_message_in_block = num_message_in_block;
182         p_list_elem->align_offset = (uint16_t) align_offset;
183         p_list_elem->msg_len = num_message_in_block * align_offset;
184         p_list_elem->p_next = NULL;
185         p_list_elem->p_tx_data_elm_list = NULL;
186         p_list_elem->time_stamp = 0;
187     }
188
189     return p_list_elem;
190 }
191
192 //------------------------------------------------------------------------------
193 /** @ingroup     group_lte_source_phy_fapi
194  *
195  *  @return      void
196  *
197  *  @description This function initializes the pthead_mutext_lock.
198  */
199 void nr5g_fapi_fapi2mac_init_api_list(
200     )
201 {
202     uint8_t phy_id = 0;
203     p_nr5g_fapi_fapi2mac_queue_t queue = NULL;
204
205     for (phy_id = 0; phy_id < FAPI_MAX_PHY_INSTANCES; phy_id++) {
206         queue = nr5g_fapi_fapi2mac_queue(phy_id);
207         pthread_mutex_init((pthread_mutex_t *) & queue->lock, NULL);
208     }
209 }