O-RAN E Maintenance Release contribution for ODULOW
[o-du/phy.git] / wls_lib / test / phy / phy_main.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  * @brief This file is test PHY wls lib main process
21  * @file phy_main.c
22  * @ingroup group_testphywls
23  * @author Intel Corporation
24  **/
25
26
27 #include <stdint.h>
28 #include <stdio.h>
29
30 #include <rte_eal.h>
31 #include <rte_cfgfile.h>
32 #include <rte_string_fns.h>
33 #include <rte_common.h>
34 #include <rte_string_fns.h>
35 #include <rte_lcore.h>
36 #include <rte_debug.h>
37 #include <rte_launch.h>
38
39 #include "wls_lib.h"
40
41 #define SUCCESS 0
42 #define FAILURE 1
43 #define WLS_TEST_DEV_NAME "wls"
44 #define WLS_TEST_MSG_ID   1
45 #define WLS_TEST_MSG_SIZE 100
46 #define WLS_MAC_MEMORY_SIZE 0x3EA80000
47 #define WLS_PHY_MEMORY_SIZE 0x18000000
48 #define NUM_PHY_MSGS  16
49
50 typedef void* WLS_HANDLE;
51 void *g_shmem;
52 uint64_t g_shmem_size;
53
54 WLS_HANDLE  g_fapi_wls, g_phy_wls;
55
56 uint8_t    phy_dpdk_init(void);
57 uint8_t    phy_wls_init(const char *dev_name, uint64_t nWlsMacMemSize, uint64_t nWlsPhyMemSize);
58 uint64_t   phy_fapi_recv();
59 uint8_t    phy_fapi_send();
60
61 int main()
62 {
63     int64_t ret;
64     uint64_t p_msg;
65
66     // DPDK init
67     ret = phy_dpdk_init();
68     if (ret)
69     {
70         printf("\n[PHY] DPDK Init - Failed\n");
71         return FAILURE;
72     }
73     printf("\n[PHY] DPDK Init - Done\n");
74
75     // WLS init
76     ret = phy_wls_init(WLS_TEST_DEV_NAME, WLS_MAC_MEMORY_SIZE, WLS_PHY_MEMORY_SIZE);
77     if(ret)
78     {
79         printf("\n[PHY] WLS Init - Failed\n");
80         return FAILURE;
81     }
82     printf("\n[PHY] WLS Init - Done\n");
83
84     // Receive from MAC WLS
85     p_msg = phy_fapi_recv();
86     if (!p_msg)
87     {
88         printf("\n[PHY] Receive from FAPI - Failed\n");
89         return FAILURE;
90     }
91     printf("\n[PHY] Receive from FAPI - Done\n");
92
93     // Sent to PHY WLS
94     ret = phy_fapi_send();
95     if (ret)
96     {
97         printf("\n[PHY] Send to FAPI - Failed\n");
98         return FAILURE;
99     }
100     printf("\n[PHY] Send to FAPI - Done\n");
101
102     printf("\n[PHY] Exiting...\n");
103
104     return SUCCESS;
105 }
106
107 uint8_t phy_dpdk_init(void)
108 {
109     char whitelist[32];
110     uint8_t i;
111
112     char *argv[] = {"phy_app", "--proc-type=primary",
113         "--file-prefix", "wls", whitelist};
114     
115     int argc = RTE_DIM(argv);
116
117     /* initialize EAL first */
118     sprintf(whitelist, "-a%s",  "0000:00:06.0");
119     printf("[PHY] Calling rte_eal_init: ");
120
121     for (i = 0; i < RTE_DIM(argv); i++)
122     {
123         printf("%s ", argv[i]);
124     }
125     printf("\n");
126
127     if (rte_eal_init(argc, argv) < 0)
128         rte_panic("Cannot init EAL\n");
129
130     return SUCCESS;
131 }
132
133 uint8_t phy_wls_init(const char *dev_name, uint64_t nWlsMacMemSize, uint64_t nWlsPhyMemSize)
134 {
135     g_phy_wls = WLS_Open(dev_name, WLS_SLAVE_CLIENT, &nWlsMacMemSize, &nWlsPhyMemSize);
136     if(NULL == g_phy_wls)
137     {
138         return FAILURE;
139     }
140     g_shmem_size = nWlsMacMemSize+nWlsPhyMemSize;
141
142     g_shmem = WLS_Alloc(g_phy_wls, g_shmem_size);
143     if (NULL == g_shmem)
144     {
145         printf("Unable to alloc WLS Memory\n");
146         return FAILURE;
147     }
148     return SUCCESS;
149 }
150
151 uint64_t phy_fapi_recv()
152 {
153     uint8_t  num_blks = 0;
154     uint64_t p_msg;
155     uint32_t msg_size;
156     uint16_t msg_id;
157     uint16_t flags;
158         uint32_t i=0;
159
160                 
161         while (1)
162         {
163     num_blks = WLS_Wait(g_phy_wls);
164             printf("WLS_Wait returns %d blocks\n",num_blks);
165     
166     if (num_blks)
167     {
168         p_msg = WLS_Get(g_phy_wls, &msg_size, &msg_id, &flags);
169                         if (p_msg)
170                         {
171                                 printf("\n[PHY] FAPI2PHY WLS Received Block %d\n",i);
172                                 i++;
173     }
174     else
175     {
176                              printf("\n[PHY] FAPI2PHY WLS Get Error for msg %d\n",i);
177                              break;
178                     }
179                 if (flags & WLS_TF_FIN)
180                 {
181                     return p_msg;
182                 }
183                 }
184                 else
185                 {
186                         printf("\n[PHY] FAPI2PHY WLS wait returned 0 blocks exiting \n");
187                         return FAILURE;
188                 }
189         
190     }
191     return p_msg;
192 }
193
194 uint8_t phy_fapi_send()
195 {
196     uint64_t pa_block = 0;
197     uint8_t ret = FAILURE;
198     uint32_t i;
199     
200     for (i=0 ; i < NUM_PHY_MSGS; i++)
201     {
202
203     pa_block = (uint64_t) WLS_DequeueBlock((void*) g_phy_wls);
204     if (!pa_block)
205     {
206                 printf("\n[PHY] FAPI2PHY WLS Dequeue block %d error\n",i);
207         return FAILURE;
208     }
209
210         ret = WLS_Put(g_phy_wls, pa_block, WLS_TEST_MSG_SIZE, WLS_TEST_MSG_ID, (i== (NUM_PHY_MSGS-1))? WLS_TF_FIN:0);
211         printf("\n[PHY] FAPI2PHY WLS Put Msg %d \n",i);
212         if (ret)
213         {
214                 printf("\n[PHY] FAPI2PHY WLS Put Msg Error %d \n",i);
215       }
216     }
217     return ret;
218 }