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 DRV_LOG(ERR, "port %u failed to request stop/start Rx/Tx (%d)",
184 dev->data->port_id, type);
187 if (mp_rep.nb_sent != mp_rep.nb_received) {
189 "port %u not all secondaries responded (req_type %d)",
190 dev->data->port_id, type);
193 for (i = 0; i < mp_rep.nb_received; i++) {
194 mp_res = &mp_rep.msgs[i];
195 res = (struct mlx5_mp_param *)mp_res->param;
197 DRV_LOG(ERR, "port %u request failed on secondary #%d",
198 dev->data->port_id, i);
207 * Broadcast request of starting data-path to secondary processes. The request
211 * Pointer to Ethernet structure.
214 mlx5_mp_req_start_rxtx(struct rte_eth_dev *dev)
216 mp_req_on_rxtx(dev, MLX5_MP_REQ_START_RXTX);
220 * Broadcast request of stopping data-path to secondary processes. The request
224 * Pointer to Ethernet structure.
227 mlx5_mp_req_stop_rxtx(struct rte_eth_dev *dev)
229 mp_req_on_rxtx(dev, MLX5_MP_REQ_STOP_RXTX);
233 * Request Memory Region creation to the primary process.
236 * Pointer to Ethernet structure.
238 * Target virtual address to register.
241 * 0 on success, a negative errno value otherwise and rte_errno is set.
244 mlx5_mp_req_mr_create(struct rte_eth_dev *dev, uintptr_t addr)
246 struct rte_mp_msg mp_req;
247 struct rte_mp_msg *mp_res;
248 struct rte_mp_reply mp_rep;
249 struct mlx5_mp_param *req = (struct mlx5_mp_param *)mp_req.param;
250 struct mlx5_mp_param *res;
251 struct timespec ts = {.tv_sec = MLX5_MP_REQ_TIMEOUT_SEC, .tv_nsec = 0};
254 assert(rte_eal_process_type() == RTE_PROC_SECONDARY);
255 mp_init_msg(dev, &mp_req, MLX5_MP_REQ_CREATE_MR);
256 req->args.addr = addr;
257 ret = rte_mp_request_sync(&mp_req, &mp_rep, &ts);
259 DRV_LOG(ERR, "port %u request to primary process failed",
263 assert(mp_rep.nb_received == 1);
264 mp_res = &mp_rep.msgs[0];
265 res = (struct mlx5_mp_param *)mp_res->param;
274 * Request Verbs command file descriptor for mmap to the primary process.
277 * Pointer to Ethernet structure.
280 * fd on success, a negative errno value otherwise and rte_errno is set.
283 mlx5_mp_req_verbs_cmd_fd(struct rte_eth_dev *dev)
285 struct rte_mp_msg mp_req;
286 struct rte_mp_msg *mp_res;
287 struct rte_mp_reply mp_rep;
288 struct mlx5_mp_param *res;
289 struct timespec ts = {.tv_sec = MLX5_MP_REQ_TIMEOUT_SEC, .tv_nsec = 0};
292 assert(rte_eal_process_type() == RTE_PROC_SECONDARY);
293 mp_init_msg(dev, &mp_req, MLX5_MP_REQ_VERBS_CMD_FD);
294 ret = rte_mp_request_sync(&mp_req, &mp_rep, &ts);
296 DRV_LOG(ERR, "port %u request to primary process failed",
300 assert(mp_rep.nb_received == 1);
301 mp_res = &mp_rep.msgs[0];
302 res = (struct mlx5_mp_param *)mp_res->param;
304 rte_errno = -res->result;
306 "port %u failed to get command FD from primary process",
311 assert(mp_res->num_fds == 1);
312 ret = mp_res->fds[0];
313 DRV_LOG(DEBUG, "port %u command FD from primary is %d",
314 dev->data->port_id, ret);
321 * Initialize by primary process.
324 mlx5_mp_init_primary(void)
326 assert(rte_eal_process_type() == RTE_PROC_PRIMARY);
327 rte_mp_action_register(MLX5_MP_NAME, mp_primary_handle);
331 * Un-initialize by primary process.
334 mlx5_mp_uninit_primary(void)
336 assert(rte_eal_process_type() == RTE_PROC_PRIMARY);
337 rte_mp_action_unregister(MLX5_MP_NAME);
341 * Initialize by secondary process.
344 mlx5_mp_init_secondary(void)
346 assert(rte_eal_process_type() == RTE_PROC_SECONDARY);
347 rte_mp_action_register(MLX5_MP_NAME, mp_secondary_handle);
351 * Un-initialize by secondary process.
354 mlx5_mp_uninit_secondary(void)
356 assert(rte_eal_process_type() == RTE_PROC_SECONDARY);
357 rte_mp_action_unregister(MLX5_MP_NAME);