1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2019 6WIND S.A.
3 * Copyright 2019 Mellanox Technologies, Ltd
11 #include <rte_ethdev_driver.h>
12 #include <rte_string_fns.h>
15 #include "mlx5_rxtx.h"
16 #include "mlx5_utils.h"
19 * Initialize IPC message.
22 * Pointer to Ethernet structure.
24 * Pointer to message to fill in.
29 mp_init_msg(struct rte_eth_dev *dev, struct rte_mp_msg *msg,
30 enum mlx5_mp_req_type type)
32 struct mlx5_mp_param *param = (struct mlx5_mp_param *)msg->param;
34 memset(msg, 0, sizeof(*msg));
35 strlcpy(msg->name, MLX5_MP_NAME, sizeof(msg->name));
36 msg->len_param = sizeof(*param);
38 param->port_id = dev->data->port_id;
42 * IPC message handler of primary process.
45 * Pointer to Ethernet structure.
47 * Pointer to the peer socket path.
50 * 0 on success, a negative errno value otherwise and rte_errno is set.
53 mp_primary_handle(const struct rte_mp_msg *mp_msg, const void *peer)
55 struct rte_mp_msg mp_res;
56 struct mlx5_mp_param *res = (struct mlx5_mp_param *)mp_res.param;
57 const struct mlx5_mp_param *param =
58 (const struct mlx5_mp_param *)mp_msg->param;
59 struct rte_eth_dev *dev;
60 struct mlx5_priv *priv;
61 struct mlx5_mr_cache entry;
65 assert(rte_eal_process_type() == RTE_PROC_PRIMARY);
66 if (!rte_eth_dev_is_valid_port(param->port_id)) {
68 DRV_LOG(ERR, "port %u invalid port ID", param->port_id);
71 dev = &rte_eth_devices[param->port_id];
72 priv = dev->data->dev_private;
73 switch (param->type) {
74 case MLX5_MP_REQ_CREATE_MR:
75 mp_init_msg(dev, &mp_res, param->type);
76 lkey = mlx5_mr_create_primary(dev, &entry, param->args.addr);
77 if (lkey == UINT32_MAX)
78 res->result = -rte_errno;
79 ret = rte_mp_reply(&mp_res, peer);
81 case MLX5_MP_REQ_VERBS_CMD_FD:
82 mp_init_msg(dev, &mp_res, param->type);
84 mp_res.fds[0] = priv->sh->ctx->cmd_fd;
86 ret = rte_mp_reply(&mp_res, peer);
88 case MLX5_MP_REQ_QUEUE_STATE_MODIFY:
89 mp_init_msg(dev, &mp_res, param->type);
90 res->result = mlx5_queue_state_modify_primary
91 (dev, ¶m->args.state_modify);
92 ret = rte_mp_reply(&mp_res, peer);
96 DRV_LOG(ERR, "port %u invalid mp request type",
104 * IPC message handler of a secondary process.
107 * Pointer to Ethernet structure.
109 * Pointer to the peer socket path.
112 * 0 on success, a negative errno value otherwise and rte_errno is set.
115 mp_secondary_handle(const struct rte_mp_msg *mp_msg, const void *peer)
117 struct rte_mp_msg mp_res;
118 struct mlx5_mp_param *res = (struct mlx5_mp_param *)mp_res.param;
119 const struct mlx5_mp_param *param =
120 (const struct mlx5_mp_param *)mp_msg->param;
121 struct rte_eth_dev *dev;
124 assert(rte_eal_process_type() == RTE_PROC_SECONDARY);
125 if (!rte_eth_dev_is_valid_port(param->port_id)) {
127 DRV_LOG(ERR, "port %u invalid port ID", param->port_id);
130 dev = &rte_eth_devices[param->port_id];
131 switch (param->type) {
132 case MLX5_MP_REQ_START_RXTX:
133 DRV_LOG(INFO, "port %u starting datapath", dev->data->port_id);
135 dev->rx_pkt_burst = mlx5_select_rx_function(dev);
136 dev->tx_pkt_burst = mlx5_select_tx_function(dev);
137 mp_init_msg(dev, &mp_res, param->type);
139 ret = rte_mp_reply(&mp_res, peer);
141 case MLX5_MP_REQ_STOP_RXTX:
142 DRV_LOG(INFO, "port %u stopping datapath", dev->data->port_id);
143 dev->rx_pkt_burst = removed_rx_burst;
144 dev->tx_pkt_burst = removed_tx_burst;
146 mp_init_msg(dev, &mp_res, param->type);
148 ret = rte_mp_reply(&mp_res, peer);
152 DRV_LOG(ERR, "port %u invalid mp request type",
160 * Broadcast request of stopping/starting data-path to secondary processes.
163 * Pointer to Ethernet structure.
168 mp_req_on_rxtx(struct rte_eth_dev *dev, enum mlx5_mp_req_type type)
170 struct rte_mp_msg mp_req;
171 struct rte_mp_msg *mp_res;
172 struct rte_mp_reply mp_rep;
173 struct mlx5_mp_param *res;
174 struct timespec ts = {.tv_sec = MLX5_MP_REQ_TIMEOUT_SEC, .tv_nsec = 0};
178 assert(rte_eal_process_type() == RTE_PROC_PRIMARY);
179 if (!mlx5_shared_data->secondary_cnt)
181 if (type != MLX5_MP_REQ_START_RXTX && type != MLX5_MP_REQ_STOP_RXTX) {
182 DRV_LOG(ERR, "port %u unknown request (req_type %d)",
183 dev->data->port_id, type);
186 mp_init_msg(dev, &mp_req, type);
187 ret = rte_mp_request_sync(&mp_req, &mp_rep, &ts);
189 if (rte_errno != ENOTSUP)
190 DRV_LOG(ERR, "port %u failed to request stop/start Rx/Tx (%d)",
191 dev->data->port_id, type);
194 if (mp_rep.nb_sent != mp_rep.nb_received) {
196 "port %u not all secondaries responded (req_type %d)",
197 dev->data->port_id, type);
200 for (i = 0; i < mp_rep.nb_received; i++) {
201 mp_res = &mp_rep.msgs[i];
202 res = (struct mlx5_mp_param *)mp_res->param;
204 DRV_LOG(ERR, "port %u request failed on secondary #%d",
205 dev->data->port_id, i);
214 * Broadcast request of starting data-path to secondary processes. The request
218 * Pointer to Ethernet structure.
221 mlx5_mp_req_start_rxtx(struct rte_eth_dev *dev)
223 mp_req_on_rxtx(dev, MLX5_MP_REQ_START_RXTX);
227 * Broadcast request of stopping data-path to secondary processes. The request
231 * Pointer to Ethernet structure.
234 mlx5_mp_req_stop_rxtx(struct rte_eth_dev *dev)
236 mp_req_on_rxtx(dev, MLX5_MP_REQ_STOP_RXTX);
240 * Request Memory Region creation to the primary process.
243 * Pointer to Ethernet structure.
245 * Target virtual address to register.
248 * 0 on success, a negative errno value otherwise and rte_errno is set.
251 mlx5_mp_req_mr_create(struct rte_eth_dev *dev, uintptr_t addr)
253 struct rte_mp_msg mp_req;
254 struct rte_mp_msg *mp_res;
255 struct rte_mp_reply mp_rep;
256 struct mlx5_mp_param *req = (struct mlx5_mp_param *)mp_req.param;
257 struct mlx5_mp_param *res;
258 struct timespec ts = {.tv_sec = MLX5_MP_REQ_TIMEOUT_SEC, .tv_nsec = 0};
261 assert(rte_eal_process_type() == RTE_PROC_SECONDARY);
262 mp_init_msg(dev, &mp_req, MLX5_MP_REQ_CREATE_MR);
263 req->args.addr = addr;
264 ret = rte_mp_request_sync(&mp_req, &mp_rep, &ts);
266 DRV_LOG(ERR, "port %u request to primary process failed",
270 assert(mp_rep.nb_received == 1);
271 mp_res = &mp_rep.msgs[0];
272 res = (struct mlx5_mp_param *)mp_res->param;
281 * Request Verbs queue state modification to the primary process.
284 * Pointer to Ethernet structure.
286 * State modify parameters.
289 * 0 on success, a negative errno value otherwise and rte_errno is set.
292 mlx5_mp_req_queue_state_modify(struct rte_eth_dev *dev,
293 struct mlx5_mp_arg_queue_state_modify *sm)
295 struct rte_mp_msg mp_req;
296 struct rte_mp_msg *mp_res;
297 struct rte_mp_reply mp_rep;
298 struct mlx5_mp_param *req = (struct mlx5_mp_param *)mp_req.param;
299 struct mlx5_mp_param *res;
300 struct timespec ts = {.tv_sec = MLX5_MP_REQ_TIMEOUT_SEC, .tv_nsec = 0};
303 assert(rte_eal_process_type() == RTE_PROC_SECONDARY);
304 mp_init_msg(dev, &mp_req, MLX5_MP_REQ_QUEUE_STATE_MODIFY);
305 req->args.state_modify = *sm;
306 ret = rte_mp_request_sync(&mp_req, &mp_rep, &ts);
308 DRV_LOG(ERR, "port %u request to primary process failed",
312 assert(mp_rep.nb_received == 1);
313 mp_res = &mp_rep.msgs[0];
314 res = (struct mlx5_mp_param *)mp_res->param;
321 * Request Verbs command file descriptor for mmap to the primary process.
324 * Pointer to Ethernet structure.
327 * fd on success, a negative errno value otherwise and rte_errno is set.
330 mlx5_mp_req_verbs_cmd_fd(struct rte_eth_dev *dev)
332 struct rte_mp_msg mp_req;
333 struct rte_mp_msg *mp_res;
334 struct rte_mp_reply mp_rep;
335 struct mlx5_mp_param *res;
336 struct timespec ts = {.tv_sec = MLX5_MP_REQ_TIMEOUT_SEC, .tv_nsec = 0};
339 assert(rte_eal_process_type() == RTE_PROC_SECONDARY);
340 mp_init_msg(dev, &mp_req, MLX5_MP_REQ_VERBS_CMD_FD);
341 ret = rte_mp_request_sync(&mp_req, &mp_rep, &ts);
343 DRV_LOG(ERR, "port %u request to primary process failed",
347 assert(mp_rep.nb_received == 1);
348 mp_res = &mp_rep.msgs[0];
349 res = (struct mlx5_mp_param *)mp_res->param;
351 rte_errno = -res->result;
353 "port %u failed to get command FD from primary process",
358 assert(mp_res->num_fds == 1);
359 ret = mp_res->fds[0];
360 DRV_LOG(DEBUG, "port %u command FD from primary is %d",
361 dev->data->port_id, ret);
368 * Initialize by primary process.
371 mlx5_mp_init_primary(void)
375 assert(rte_eal_process_type() == RTE_PROC_PRIMARY);
377 /* primary is allowed to not support IPC */
378 ret = rte_mp_action_register(MLX5_MP_NAME, mp_primary_handle);
379 if (ret && rte_errno != ENOTSUP)
385 * Un-initialize by primary process.
388 mlx5_mp_uninit_primary(void)
390 assert(rte_eal_process_type() == RTE_PROC_PRIMARY);
391 rte_mp_action_unregister(MLX5_MP_NAME);
395 * Initialize by secondary process.
398 mlx5_mp_init_secondary(void)
400 assert(rte_eal_process_type() == RTE_PROC_SECONDARY);
401 return rte_mp_action_register(MLX5_MP_NAME, mp_secondary_handle);
405 * Un-initialize by secondary process.
408 mlx5_mp_uninit_secondary(void)
410 assert(rte_eal_process_type() == RTE_PROC_SECONDARY);
411 rte_mp_action_unregister(MLX5_MP_NAME);