build: remove redundant config include
[dpdk.git] / drivers / net / mlx5 / mlx5_mp.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2019 6WIND S.A.
3  * Copyright 2019 Mellanox Technologies, Ltd
4  */
5
6 #include <stdio.h>
7 #include <time.h>
8
9 #include <rte_eal.h>
10 #include <rte_ethdev_driver.h>
11 #include <rte_string_fns.h>
12
13 #include "mlx5.h"
14 #include "mlx5_rxtx.h"
15 #include "mlx5_utils.h"
16
17 /**
18  * Initialize IPC message.
19  *
20  * @param[in] dev
21  *   Pointer to Ethernet structure.
22  * @param[out] msg
23  *   Pointer to message to fill in.
24  * @param[in] type
25  *   Message type.
26  */
27 static inline void
28 mp_init_msg(struct rte_eth_dev *dev, struct rte_mp_msg *msg,
29             enum mlx5_mp_req_type type)
30 {
31         struct mlx5_mp_param *param = (struct mlx5_mp_param *)msg->param;
32
33         memset(msg, 0, sizeof(*msg));
34         strlcpy(msg->name, MLX5_MP_NAME, sizeof(msg->name));
35         msg->len_param = sizeof(*param);
36         param->type = type;
37         param->port_id = dev->data->port_id;
38 }
39
40 /**
41  * IPC message handler of primary process.
42  *
43  * @param[in] dev
44  *   Pointer to Ethernet structure.
45  * @param[in] peer
46  *   Pointer to the peer socket path.
47  *
48  * @return
49  *   0 on success, a negative errno value otherwise and rte_errno is set.
50  */
51 static int
52 mp_primary_handle(const struct rte_mp_msg *mp_msg, const void *peer)
53 {
54         struct rte_mp_msg mp_res;
55         struct mlx5_mp_param *res = (struct mlx5_mp_param *)mp_res.param;
56         const struct mlx5_mp_param *param =
57                 (const struct mlx5_mp_param *)mp_msg->param;
58         struct rte_eth_dev *dev;
59         struct mlx5_priv *priv;
60         struct mlx5_mr_cache entry;
61         uint32_t lkey;
62         int ret;
63
64         MLX5_ASSERT(rte_eal_process_type() == RTE_PROC_PRIMARY);
65         if (!rte_eth_dev_is_valid_port(param->port_id)) {
66                 rte_errno = ENODEV;
67                 DRV_LOG(ERR, "port %u invalid port ID", param->port_id);
68                 return -rte_errno;
69         }
70         dev = &rte_eth_devices[param->port_id];
71         priv = dev->data->dev_private;
72         switch (param->type) {
73         case MLX5_MP_REQ_CREATE_MR:
74                 mp_init_msg(dev, &mp_res, param->type);
75                 lkey = mlx5_mr_create_primary(dev, &entry, param->args.addr);
76                 if (lkey == UINT32_MAX)
77                         res->result = -rte_errno;
78                 ret = rte_mp_reply(&mp_res, peer);
79                 break;
80         case MLX5_MP_REQ_VERBS_CMD_FD:
81                 mp_init_msg(dev, &mp_res, param->type);
82                 mp_res.num_fds = 1;
83                 mp_res.fds[0] = priv->sh->ctx->cmd_fd;
84                 res->result = 0;
85                 ret = rte_mp_reply(&mp_res, peer);
86                 break;
87         case MLX5_MP_REQ_QUEUE_STATE_MODIFY:
88                 mp_init_msg(dev, &mp_res, param->type);
89                 res->result = mlx5_queue_state_modify_primary
90                                         (dev, &param->args.state_modify);
91                 ret = rte_mp_reply(&mp_res, peer);
92                 break;
93         default:
94                 rte_errno = EINVAL;
95                 DRV_LOG(ERR, "port %u invalid mp request type",
96                         dev->data->port_id);
97                 return -rte_errno;
98         }
99         return ret;
100 }
101
102 /**
103  * IPC message handler of a secondary process.
104  *
105  * @param[in] dev
106  *   Pointer to Ethernet structure.
107  * @param[in] peer
108  *   Pointer to the peer socket path.
109  *
110  * @return
111  *   0 on success, a negative errno value otherwise and rte_errno is set.
112  */
113 static int
114 mp_secondary_handle(const struct rte_mp_msg *mp_msg, const void *peer)
115 {
116         struct rte_mp_msg mp_res;
117         struct mlx5_mp_param *res = (struct mlx5_mp_param *)mp_res.param;
118         const struct mlx5_mp_param *param =
119                 (const struct mlx5_mp_param *)mp_msg->param;
120         struct rte_eth_dev *dev;
121         int ret;
122
123         MLX5_ASSERT(rte_eal_process_type() == RTE_PROC_SECONDARY);
124         if (!rte_eth_dev_is_valid_port(param->port_id)) {
125                 rte_errno = ENODEV;
126                 DRV_LOG(ERR, "port %u invalid port ID", param->port_id);
127                 return -rte_errno;
128         }
129         dev = &rte_eth_devices[param->port_id];
130         switch (param->type) {
131         case MLX5_MP_REQ_START_RXTX:
132                 DRV_LOG(INFO, "port %u starting datapath", dev->data->port_id);
133                 rte_mb();
134                 dev->rx_pkt_burst = mlx5_select_rx_function(dev);
135                 dev->tx_pkt_burst = mlx5_select_tx_function(dev);
136                 mp_init_msg(dev, &mp_res, param->type);
137                 res->result = 0;
138                 ret = rte_mp_reply(&mp_res, peer);
139                 break;
140         case MLX5_MP_REQ_STOP_RXTX:
141                 DRV_LOG(INFO, "port %u stopping datapath", dev->data->port_id);
142                 dev->rx_pkt_burst = removed_rx_burst;
143                 dev->tx_pkt_burst = removed_tx_burst;
144                 rte_mb();
145                 mp_init_msg(dev, &mp_res, param->type);
146                 res->result = 0;
147                 ret = rte_mp_reply(&mp_res, peer);
148                 break;
149         default:
150                 rte_errno = EINVAL;
151                 DRV_LOG(ERR, "port %u invalid mp request type",
152                         dev->data->port_id);
153                 return -rte_errno;
154         }
155         return ret;
156 }
157
158 /**
159  * Broadcast request of stopping/starting data-path to secondary processes.
160  *
161  * @param[in] dev
162  *   Pointer to Ethernet structure.
163  * @param[in] type
164  *   Request type.
165  */
166 static void
167 mp_req_on_rxtx(struct rte_eth_dev *dev, enum mlx5_mp_req_type type)
168 {
169         struct rte_mp_msg mp_req;
170         struct rte_mp_msg *mp_res;
171         struct rte_mp_reply mp_rep;
172         struct mlx5_mp_param *res;
173         struct timespec ts = {.tv_sec = MLX5_MP_REQ_TIMEOUT_SEC, .tv_nsec = 0};
174         int ret;
175         int i;
176
177         MLX5_ASSERT(rte_eal_process_type() == RTE_PROC_PRIMARY);
178         if (!mlx5_shared_data->secondary_cnt)
179                 return;
180         if (type != MLX5_MP_REQ_START_RXTX && type != MLX5_MP_REQ_STOP_RXTX) {
181                 DRV_LOG(ERR, "port %u unknown request (req_type %d)",
182                         dev->data->port_id, type);
183                 return;
184         }
185         mp_init_msg(dev, &mp_req, type);
186         ret = rte_mp_request_sync(&mp_req, &mp_rep, &ts);
187         if (ret) {
188                 if (rte_errno != ENOTSUP)
189                         DRV_LOG(ERR, "port %u failed to request stop/start Rx/Tx (%d)",
190                                 dev->data->port_id, type);
191                 goto exit;
192         }
193         if (mp_rep.nb_sent != mp_rep.nb_received) {
194                 DRV_LOG(ERR,
195                         "port %u not all secondaries responded (req_type %d)",
196                         dev->data->port_id, type);
197                 goto exit;
198         }
199         for (i = 0; i < mp_rep.nb_received; i++) {
200                 mp_res = &mp_rep.msgs[i];
201                 res = (struct mlx5_mp_param *)mp_res->param;
202                 if (res->result) {
203                         DRV_LOG(ERR, "port %u request failed on secondary #%d",
204                                 dev->data->port_id, i);
205                         goto exit;
206                 }
207         }
208 exit:
209         free(mp_rep.msgs);
210 }
211
212 /**
213  * Broadcast request of starting data-path to secondary processes. The request
214  * is synchronous.
215  *
216  * @param[in] dev
217  *   Pointer to Ethernet structure.
218  */
219 void
220 mlx5_mp_req_start_rxtx(struct rte_eth_dev *dev)
221 {
222         mp_req_on_rxtx(dev, MLX5_MP_REQ_START_RXTX);
223 }
224
225 /**
226  * Broadcast request of stopping data-path to secondary processes. The request
227  * is synchronous.
228  *
229  * @param[in] dev
230  *   Pointer to Ethernet structure.
231  */
232 void
233 mlx5_mp_req_stop_rxtx(struct rte_eth_dev *dev)
234 {
235         mp_req_on_rxtx(dev, MLX5_MP_REQ_STOP_RXTX);
236 }
237
238 /**
239  * Request Memory Region creation to the primary process.
240  *
241  * @param[in] dev
242  *   Pointer to Ethernet structure.
243  * @param addr
244  *   Target virtual address to register.
245  *
246  * @return
247  *   0 on success, a negative errno value otherwise and rte_errno is set.
248  */
249 int
250 mlx5_mp_req_mr_create(struct rte_eth_dev *dev, uintptr_t addr)
251 {
252         struct rte_mp_msg mp_req;
253         struct rte_mp_msg *mp_res;
254         struct rte_mp_reply mp_rep;
255         struct mlx5_mp_param *req = (struct mlx5_mp_param *)mp_req.param;
256         struct mlx5_mp_param *res;
257         struct timespec ts = {.tv_sec = MLX5_MP_REQ_TIMEOUT_SEC, .tv_nsec = 0};
258         int ret;
259
260         MLX5_ASSERT(rte_eal_process_type() == RTE_PROC_SECONDARY);
261         mp_init_msg(dev, &mp_req, MLX5_MP_REQ_CREATE_MR);
262         req->args.addr = addr;
263         ret = rte_mp_request_sync(&mp_req, &mp_rep, &ts);
264         if (ret) {
265                 DRV_LOG(ERR, "port %u request to primary process failed",
266                         dev->data->port_id);
267                 return -rte_errno;
268         }
269         MLX5_ASSERT(mp_rep.nb_received == 1);
270         mp_res = &mp_rep.msgs[0];
271         res = (struct mlx5_mp_param *)mp_res->param;
272         ret = res->result;
273         if (ret)
274                 rte_errno = -ret;
275         free(mp_rep.msgs);
276         return ret;
277 }
278
279 /**
280  * Request Verbs queue state modification to the primary process.
281  *
282  * @param[in] dev
283  *   Pointer to Ethernet structure.
284  * @param sm
285  *   State modify parameters.
286  *
287  * @return
288  *   0 on success, a negative errno value otherwise and rte_errno is set.
289  */
290 int
291 mlx5_mp_req_queue_state_modify(struct rte_eth_dev *dev,
292                                struct mlx5_mp_arg_queue_state_modify *sm)
293 {
294         struct rte_mp_msg mp_req;
295         struct rte_mp_msg *mp_res;
296         struct rte_mp_reply mp_rep;
297         struct mlx5_mp_param *req = (struct mlx5_mp_param *)mp_req.param;
298         struct mlx5_mp_param *res;
299         struct timespec ts = {.tv_sec = MLX5_MP_REQ_TIMEOUT_SEC, .tv_nsec = 0};
300         int ret;
301
302         MLX5_ASSERT(rte_eal_process_type() == RTE_PROC_SECONDARY);
303         mp_init_msg(dev, &mp_req, MLX5_MP_REQ_QUEUE_STATE_MODIFY);
304         req->args.state_modify = *sm;
305         ret = rte_mp_request_sync(&mp_req, &mp_rep, &ts);
306         if (ret) {
307                 DRV_LOG(ERR, "port %u request to primary process failed",
308                         dev->data->port_id);
309                 return -rte_errno;
310         }
311         MLX5_ASSERT(mp_rep.nb_received == 1);
312         mp_res = &mp_rep.msgs[0];
313         res = (struct mlx5_mp_param *)mp_res->param;
314         ret = res->result;
315         free(mp_rep.msgs);
316         return ret;
317 }
318
319 /**
320  * Request Verbs command file descriptor for mmap to the primary process.
321  *
322  * @param[in] dev
323  *   Pointer to Ethernet structure.
324  *
325  * @return
326  *   fd on success, a negative errno value otherwise and rte_errno is set.
327  */
328 int
329 mlx5_mp_req_verbs_cmd_fd(struct rte_eth_dev *dev)
330 {
331         struct rte_mp_msg mp_req;
332         struct rte_mp_msg *mp_res;
333         struct rte_mp_reply mp_rep;
334         struct mlx5_mp_param *res;
335         struct timespec ts = {.tv_sec = MLX5_MP_REQ_TIMEOUT_SEC, .tv_nsec = 0};
336         int ret;
337
338         MLX5_ASSERT(rte_eal_process_type() == RTE_PROC_SECONDARY);
339         mp_init_msg(dev, &mp_req, MLX5_MP_REQ_VERBS_CMD_FD);
340         ret = rte_mp_request_sync(&mp_req, &mp_rep, &ts);
341         if (ret) {
342                 DRV_LOG(ERR, "port %u request to primary process failed",
343                         dev->data->port_id);
344                 return -rte_errno;
345         }
346         MLX5_ASSERT(mp_rep.nb_received == 1);
347         mp_res = &mp_rep.msgs[0];
348         res = (struct mlx5_mp_param *)mp_res->param;
349         if (res->result) {
350                 rte_errno = -res->result;
351                 DRV_LOG(ERR,
352                         "port %u failed to get command FD from primary process",
353                         dev->data->port_id);
354                 ret = -rte_errno;
355                 goto exit;
356         }
357         MLX5_ASSERT(mp_res->num_fds == 1);
358         ret = mp_res->fds[0];
359         DRV_LOG(DEBUG, "port %u command FD from primary is %d",
360                 dev->data->port_id, ret);
361 exit:
362         free(mp_rep.msgs);
363         return ret;
364 }
365
366 /**
367  * Initialize by primary process.
368  */
369 int
370 mlx5_mp_init_primary(void)
371 {
372         int ret;
373
374         MLX5_ASSERT(rte_eal_process_type() == RTE_PROC_PRIMARY);
375
376         /* primary is allowed to not support IPC */
377         ret = rte_mp_action_register(MLX5_MP_NAME, mp_primary_handle);
378         if (ret && rte_errno != ENOTSUP)
379                 return -1;
380         return 0;
381 }
382
383 /**
384  * Un-initialize by primary process.
385  */
386 void
387 mlx5_mp_uninit_primary(void)
388 {
389         MLX5_ASSERT(rte_eal_process_type() == RTE_PROC_PRIMARY);
390         rte_mp_action_unregister(MLX5_MP_NAME);
391 }
392
393 /**
394  * Initialize by secondary process.
395  */
396 int
397 mlx5_mp_init_secondary(void)
398 {
399         MLX5_ASSERT(rte_eal_process_type() == RTE_PROC_SECONDARY);
400         return rte_mp_action_register(MLX5_MP_NAME, mp_secondary_handle);
401 }
402
403 /**
404  * Un-initialize by secondary process.
405  */
406 void
407 mlx5_mp_uninit_secondary(void)
408 {
409         MLX5_ASSERT(rte_eal_process_type() == RTE_PROC_SECONDARY);
410         rte_mp_action_unregister(MLX5_MP_NAME);
411 }