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);
90 DRV_LOG(ERR, "port %u invalid mp request type",
98 * IPC message handler of a secondary process.
101 * Pointer to Ethernet structure.
103 * Pointer to the peer socket path.
106 * 0 on success, a negative errno value otherwise and rte_errno is set.
109 mp_secondary_handle(const struct rte_mp_msg *mp_msg, const void *peer)
111 struct rte_mp_msg mp_res;
112 struct mlx5_mp_param *res = (struct mlx5_mp_param *)mp_res.param;
113 const struct mlx5_mp_param *param =
114 (const struct mlx5_mp_param *)mp_msg->param;
115 struct rte_eth_dev *dev;
118 assert(rte_eal_process_type() == RTE_PROC_SECONDARY);
119 if (!rte_eth_dev_is_valid_port(param->port_id)) {
121 DRV_LOG(ERR, "port %u invalid port ID", param->port_id);
124 dev = &rte_eth_devices[param->port_id];
125 switch (param->type) {
126 case MLX5_MP_REQ_START_RXTX:
127 DRV_LOG(INFO, "port %u starting datapath", dev->data->port_id);
129 dev->rx_pkt_burst = mlx5_select_rx_function(dev);
130 dev->tx_pkt_burst = mlx5_select_tx_function(dev);
131 mp_init_msg(dev, &mp_res, param->type);
133 ret = rte_mp_reply(&mp_res, peer);
135 case MLX5_MP_REQ_STOP_RXTX:
136 DRV_LOG(INFO, "port %u stopping datapath", dev->data->port_id);
137 dev->rx_pkt_burst = removed_rx_burst;
138 dev->tx_pkt_burst = removed_tx_burst;
140 mp_init_msg(dev, &mp_res, param->type);
142 ret = rte_mp_reply(&mp_res, peer);
146 DRV_LOG(ERR, "port %u invalid mp request type",
154 * Broadcast request of stopping/starting data-path to secondary processes.
157 * Pointer to Ethernet structure.
162 mp_req_on_rxtx(struct rte_eth_dev *dev, enum mlx5_mp_req_type type)
164 struct rte_mp_msg mp_req;
165 struct rte_mp_msg *mp_res;
166 struct rte_mp_reply mp_rep;
167 struct mlx5_mp_param *res;
168 struct timespec ts = {.tv_sec = MLX5_MP_REQ_TIMEOUT_SEC, .tv_nsec = 0};
172 assert(rte_eal_process_type() == RTE_PROC_PRIMARY);
173 if (!mlx5_shared_data->secondary_cnt)
175 if (type != MLX5_MP_REQ_START_RXTX && type != MLX5_MP_REQ_STOP_RXTX) {
176 DRV_LOG(ERR, "port %u unknown request (req_type %d)",
177 dev->data->port_id, type);
180 mp_init_msg(dev, &mp_req, type);
181 ret = rte_mp_request_sync(&mp_req, &mp_rep, &ts);
183 if (rte_errno != ENOTSUP)
184 DRV_LOG(ERR, "port %u failed to request stop/start Rx/Tx (%d)",
185 dev->data->port_id, type);
188 if (mp_rep.nb_sent != mp_rep.nb_received) {
190 "port %u not all secondaries responded (req_type %d)",
191 dev->data->port_id, type);
194 for (i = 0; i < mp_rep.nb_received; i++) {
195 mp_res = &mp_rep.msgs[i];
196 res = (struct mlx5_mp_param *)mp_res->param;
198 DRV_LOG(ERR, "port %u request failed on secondary #%d",
199 dev->data->port_id, i);
208 * Broadcast request of starting data-path to secondary processes. The request
212 * Pointer to Ethernet structure.
215 mlx5_mp_req_start_rxtx(struct rte_eth_dev *dev)
217 mp_req_on_rxtx(dev, MLX5_MP_REQ_START_RXTX);
221 * Broadcast request of stopping data-path to secondary processes. The request
225 * Pointer to Ethernet structure.
228 mlx5_mp_req_stop_rxtx(struct rte_eth_dev *dev)
230 mp_req_on_rxtx(dev, MLX5_MP_REQ_STOP_RXTX);
234 * Request Memory Region creation to the primary process.
237 * Pointer to Ethernet structure.
239 * Target virtual address to register.
242 * 0 on success, a negative errno value otherwise and rte_errno is set.
245 mlx5_mp_req_mr_create(struct rte_eth_dev *dev, uintptr_t addr)
247 struct rte_mp_msg mp_req;
248 struct rte_mp_msg *mp_res;
249 struct rte_mp_reply mp_rep;
250 struct mlx5_mp_param *req = (struct mlx5_mp_param *)mp_req.param;
251 struct mlx5_mp_param *res;
252 struct timespec ts = {.tv_sec = MLX5_MP_REQ_TIMEOUT_SEC, .tv_nsec = 0};
255 assert(rte_eal_process_type() == RTE_PROC_SECONDARY);
256 mp_init_msg(dev, &mp_req, MLX5_MP_REQ_CREATE_MR);
257 req->args.addr = addr;
258 ret = rte_mp_request_sync(&mp_req, &mp_rep, &ts);
260 DRV_LOG(ERR, "port %u request to primary process failed",
264 assert(mp_rep.nb_received == 1);
265 mp_res = &mp_rep.msgs[0];
266 res = (struct mlx5_mp_param *)mp_res->param;
275 * Request Verbs command file descriptor for mmap to the primary process.
278 * Pointer to Ethernet structure.
281 * fd on success, a negative errno value otherwise and rte_errno is set.
284 mlx5_mp_req_verbs_cmd_fd(struct rte_eth_dev *dev)
286 struct rte_mp_msg mp_req;
287 struct rte_mp_msg *mp_res;
288 struct rte_mp_reply mp_rep;
289 struct mlx5_mp_param *res;
290 struct timespec ts = {.tv_sec = MLX5_MP_REQ_TIMEOUT_SEC, .tv_nsec = 0};
293 assert(rte_eal_process_type() == RTE_PROC_SECONDARY);
294 mp_init_msg(dev, &mp_req, MLX5_MP_REQ_VERBS_CMD_FD);
295 ret = rte_mp_request_sync(&mp_req, &mp_rep, &ts);
297 DRV_LOG(ERR, "port %u request to primary process failed",
301 assert(mp_rep.nb_received == 1);
302 mp_res = &mp_rep.msgs[0];
303 res = (struct mlx5_mp_param *)mp_res->param;
305 rte_errno = -res->result;
307 "port %u failed to get command FD from primary process",
312 assert(mp_res->num_fds == 1);
313 ret = mp_res->fds[0];
314 DRV_LOG(DEBUG, "port %u command FD from primary is %d",
315 dev->data->port_id, ret);
322 * Initialize by primary process.
325 mlx5_mp_init_primary(void)
329 assert(rte_eal_process_type() == RTE_PROC_PRIMARY);
331 /* primary is allowed to not support IPC */
332 ret = rte_mp_action_register(MLX5_MP_NAME, mp_primary_handle);
333 if (ret && rte_errno != ENOTSUP)
339 * Un-initialize by primary process.
342 mlx5_mp_uninit_primary(void)
344 assert(rte_eal_process_type() == RTE_PROC_PRIMARY);
345 rte_mp_action_unregister(MLX5_MP_NAME);
349 * Initialize by secondary process.
352 mlx5_mp_init_secondary(void)
354 assert(rte_eal_process_type() == RTE_PROC_SECONDARY);
355 return rte_mp_action_register(MLX5_MP_NAME, mp_secondary_handle);
359 * Un-initialize by secondary process.
362 mlx5_mp_uninit_secondary(void)
364 assert(rte_eal_process_type() == RTE_PROC_SECONDARY);
365 rte_mp_action_unregister(MLX5_MP_NAME);