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 "mlx4_rxtx.h"
16 #include "mlx4_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 mlx4_mp_req_type type)
32 struct mlx4_mp_param *param = (struct mlx4_mp_param *)msg->param;
34 memset(msg, 0, sizeof(*msg));
35 strlcpy(msg->name, MLX4_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, 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 mlx4_mp_param *res = (struct mlx4_mp_param *)mp_res.param;
57 const struct mlx4_mp_param *param =
58 (const struct mlx4_mp_param *)mp_msg->param;
59 struct rte_eth_dev *dev;
60 struct mlx4_priv *priv;
61 struct mlx4_mr_cache entry;
65 assert(rte_eal_process_type() == RTE_PROC_PRIMARY);
66 if (!rte_eth_dev_is_valid_port(param->port_id)) {
68 ERROR("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 MLX4_MP_REQ_CREATE_MR:
75 mp_init_msg(dev, &mp_res, param->type);
76 lkey = mlx4_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 MLX4_MP_REQ_VERBS_CMD_FD:
82 mp_init_msg(dev, &mp_res, param->type);
84 mp_res.fds[0] = priv->ctx->cmd_fd;
86 ret = rte_mp_reply(&mp_res, peer);
90 ERROR("port %u invalid mp request type", dev->data->port_id);
97 * IPC message handler of a secondary process.
100 * Pointer to Ethernet structure.
102 * Pointer to the peer socket path.
105 * 0 on success, a negative errno value otherwise and rte_errno is set.
108 mp_secondary_handle(const struct rte_mp_msg *mp_msg, const void *peer)
110 struct rte_mp_msg mp_res;
111 struct mlx4_mp_param *res = (struct mlx4_mp_param *)mp_res.param;
112 const struct mlx4_mp_param *param =
113 (const struct mlx4_mp_param *)mp_msg->param;
114 struct rte_eth_dev *dev;
117 assert(rte_eal_process_type() == RTE_PROC_SECONDARY);
118 if (!rte_eth_dev_is_valid_port(param->port_id)) {
120 ERROR("port %u invalid port ID", param->port_id);
123 dev = &rte_eth_devices[param->port_id];
124 switch (param->type) {
125 case MLX4_MP_REQ_START_RXTX:
126 INFO("port %u starting datapath", dev->data->port_id);
128 dev->tx_pkt_burst = mlx4_tx_burst;
129 dev->rx_pkt_burst = mlx4_rx_burst;
130 mp_init_msg(dev, &mp_res, param->type);
132 ret = rte_mp_reply(&mp_res, peer);
134 case MLX4_MP_REQ_STOP_RXTX:
135 INFO("port %u stopping datapath", dev->data->port_id);
136 dev->tx_pkt_burst = mlx4_tx_burst_removed;
137 dev->rx_pkt_burst = mlx4_rx_burst_removed;
139 mp_init_msg(dev, &mp_res, param->type);
141 ret = rte_mp_reply(&mp_res, peer);
145 ERROR("port %u invalid mp request type", dev->data->port_id);
152 * Broadcast request of stopping/starting data-path to secondary processes.
155 * Pointer to Ethernet structure.
160 mp_req_on_rxtx(struct rte_eth_dev *dev, enum mlx4_mp_req_type type)
162 struct rte_mp_msg mp_req;
163 struct rte_mp_msg *mp_res;
164 struct rte_mp_reply mp_rep;
165 struct mlx4_mp_param *res __rte_unused;
166 struct timespec ts = {.tv_sec = MLX4_MP_REQ_TIMEOUT_SEC, .tv_nsec = 0};
170 assert(rte_eal_process_type() == RTE_PROC_PRIMARY);
171 if (!mlx4_shared_data->secondary_cnt)
173 if (type != MLX4_MP_REQ_START_RXTX && type != MLX4_MP_REQ_STOP_RXTX) {
174 ERROR("port %u unknown request (req_type %d)",
175 dev->data->port_id, type);
178 mp_init_msg(dev, &mp_req, type);
179 ret = rte_mp_request_sync(&mp_req, &mp_rep, &ts);
181 ERROR("port %u failed to request stop/start Rx/Tx (%d)",
182 dev->data->port_id, type);
185 if (mp_rep.nb_sent != mp_rep.nb_received) {
186 ERROR("port %u not all secondaries responded (req_type %d)",
187 dev->data->port_id, type);
190 for (i = 0; i < mp_rep.nb_received; i++) {
191 mp_res = &mp_rep.msgs[i];
192 res = (struct mlx4_mp_param *)mp_res->param;
194 ERROR("port %u request failed on secondary #%d",
195 dev->data->port_id, i);
204 * Broadcast request of starting data-path to secondary processes. The request
208 * Pointer to Ethernet structure.
211 mlx4_mp_req_start_rxtx(struct rte_eth_dev *dev)
213 mp_req_on_rxtx(dev, MLX4_MP_REQ_START_RXTX);
217 * Broadcast request of stopping data-path to secondary processes. The request
221 * Pointer to Ethernet structure.
224 mlx4_mp_req_stop_rxtx(struct rte_eth_dev *dev)
226 mp_req_on_rxtx(dev, MLX4_MP_REQ_STOP_RXTX);
230 * Request Memory Region creation to the primary process.
233 * Pointer to Ethernet structure.
235 * Target virtual address to register.
238 * 0 on success, a negative errno value otherwise and rte_errno is set.
241 mlx4_mp_req_mr_create(struct rte_eth_dev *dev, uintptr_t addr)
243 struct rte_mp_msg mp_req;
244 struct rte_mp_msg *mp_res;
245 struct rte_mp_reply mp_rep;
246 struct mlx4_mp_param *req = (struct mlx4_mp_param *)mp_req.param;
247 struct mlx4_mp_param *res;
248 struct timespec ts = {.tv_sec = MLX4_MP_REQ_TIMEOUT_SEC, .tv_nsec = 0};
251 assert(rte_eal_process_type() == RTE_PROC_SECONDARY);
252 mp_init_msg(dev, &mp_req, MLX4_MP_REQ_CREATE_MR);
253 req->args.addr = addr;
254 ret = rte_mp_request_sync(&mp_req, &mp_rep, &ts);
256 ERROR("port %u request to primary process failed",
260 assert(mp_rep.nb_received == 1);
261 mp_res = &mp_rep.msgs[0];
262 res = (struct mlx4_mp_param *)mp_res->param;
271 * IPC message handler of primary process.
274 * Pointer to Ethernet structure.
277 * fd on success, a negative errno value otherwise and rte_errno is set.
280 mlx4_mp_req_verbs_cmd_fd(struct rte_eth_dev *dev)
282 struct rte_mp_msg mp_req;
283 struct rte_mp_msg *mp_res;
284 struct rte_mp_reply mp_rep;
285 struct mlx4_mp_param *res;
286 struct timespec ts = {.tv_sec = MLX4_MP_REQ_TIMEOUT_SEC, .tv_nsec = 0};
289 assert(rte_eal_process_type() == RTE_PROC_SECONDARY);
290 mp_init_msg(dev, &mp_req, MLX4_MP_REQ_VERBS_CMD_FD);
291 ret = rte_mp_request_sync(&mp_req, &mp_rep, &ts);
293 ERROR("port %u request to primary process failed",
297 assert(mp_rep.nb_received == 1);
298 mp_res = &mp_rep.msgs[0];
299 res = (struct mlx4_mp_param *)mp_res->param;
301 rte_errno = -res->result;
302 ERROR("port %u failed to get command FD from primary process",
307 assert(mp_res->num_fds == 1);
308 ret = mp_res->fds[0];
309 DEBUG("port %u command FD from primary is %d",
310 dev->data->port_id, ret);
317 * Initialize by primary process.
320 mlx4_mp_init_primary(void)
324 assert(rte_eal_process_type() == RTE_PROC_PRIMARY);
326 /* primary is allowed to not support IPC */
327 ret = rte_mp_action_register(MLX4_MP_NAME, mp_primary_handle);
328 if (ret && rte_errno != ENOTSUP)
334 * Un-initialize by primary process.
337 mlx4_mp_uninit_primary(void)
339 assert(rte_eal_process_type() == RTE_PROC_PRIMARY);
340 rte_mp_action_unregister(MLX4_MP_NAME);
344 * Initialize by secondary process.
347 mlx4_mp_init_secondary(void)
349 assert(rte_eal_process_type() == RTE_PROC_SECONDARY);
350 return rte_mp_action_register(MLX4_MP_NAME, mp_secondary_handle);
354 * Un-initialize by secondary process.
357 mlx4_mp_uninit_secondary(void)
359 assert(rte_eal_process_type() == RTE_PROC_SECONDARY);
360 rte_mp_action_unregister(MLX4_MP_NAME);