1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2019 6WIND S.A.
3 * Copyright 2019 Mellanox Technologies, Ltd
10 #include <ethdev_driver.h>
11 #include <rte_string_fns.h>
13 #include <mlx5_common_mp.h>
14 #include <mlx5_common_mr.h>
15 #include <mlx5_malloc.h>
18 #include "mlx5_rxtx.h"
21 #include "mlx5_utils.h"
24 * Handle a port-agnostic message.
27 * 0 on success, 1 when message is not port-agnostic, (-1) on error.
30 mlx5_mp_os_handle_port_agnostic(const struct rte_mp_msg *mp_msg,
33 struct rte_mp_msg mp_res;
34 struct mlx5_mp_param *res = (struct mlx5_mp_param *)mp_res.param;
35 const struct mlx5_mp_param *param =
36 (const struct mlx5_mp_param *)mp_msg->param;
37 const struct mlx5_mp_arg_mempool_reg *mpr;
38 struct mlx5_mp_id mp_id;
40 switch (param->type) {
41 case MLX5_MP_REQ_MEMPOOL_REGISTER:
42 mlx5_mp_id_init(&mp_id, param->port_id);
43 mp_init_msg(&mp_id, &mp_res, param->type);
44 mpr = ¶m->args.mempool_reg;
45 res->result = mlx5_mr_mempool_register(mpr->share_cache,
46 mpr->pd, mpr->mempool,
48 return rte_mp_reply(&mp_res, peer);
49 case MLX5_MP_REQ_MEMPOOL_UNREGISTER:
50 mlx5_mp_id_init(&mp_id, param->port_id);
51 mp_init_msg(&mp_id, &mp_res, param->type);
52 mpr = ¶m->args.mempool_reg;
53 res->result = mlx5_mr_mempool_unregister(mpr->share_cache,
55 return rte_mp_reply(&mp_res, peer);
63 mlx5_mp_os_primary_handle(const struct rte_mp_msg *mp_msg, const void *peer)
65 struct rte_mp_msg mp_res;
66 struct mlx5_mp_param *res = (struct mlx5_mp_param *)mp_res.param;
67 const struct mlx5_mp_param *param =
68 (const struct mlx5_mp_param *)mp_msg->param;
69 struct rte_eth_dev *dev;
70 struct mlx5_priv *priv;
71 struct mlx5_common_device *cdev;
72 struct mr_cache_entry entry;
76 MLX5_ASSERT(rte_eal_process_type() == RTE_PROC_PRIMARY);
77 /* Port-agnostic messages. */
78 ret = mlx5_mp_os_handle_port_agnostic(mp_msg, peer);
81 /* Port-specific messages. */
82 if (!rte_eth_dev_is_valid_port(param->port_id)) {
84 DRV_LOG(ERR, "port %u invalid port ID", param->port_id);
87 dev = &rte_eth_devices[param->port_id];
88 priv = dev->data->dev_private;
89 cdev = priv->sh->cdev;
90 switch (param->type) {
91 case MLX5_MP_REQ_CREATE_MR:
92 mp_init_msg(&priv->mp_id, &mp_res, param->type);
93 lkey = mlx5_mr_create_primary(priv->sh->pd,
94 &priv->sh->share_cache,
95 &entry, param->args.addr,
96 cdev->config.mr_ext_memseg_en);
97 if (lkey == UINT32_MAX)
98 res->result = -rte_errno;
99 ret = rte_mp_reply(&mp_res, peer);
101 case MLX5_MP_REQ_VERBS_CMD_FD:
102 mp_init_msg(&priv->mp_id, &mp_res, param->type);
104 mp_res.fds[0] = ((struct ibv_context *)priv->sh->ctx)->cmd_fd;
106 ret = rte_mp_reply(&mp_res, peer);
108 case MLX5_MP_REQ_QUEUE_STATE_MODIFY:
109 mp_init_msg(&priv->mp_id, &mp_res, param->type);
110 res->result = mlx5_queue_state_modify_primary
111 (dev, ¶m->args.state_modify);
112 ret = rte_mp_reply(&mp_res, peer);
114 case MLX5_MP_REQ_QUEUE_RX_STOP:
115 mp_init_msg(&priv->mp_id, &mp_res, param->type);
116 res->result = mlx5_rx_queue_stop_primary
117 (dev, param->args.queue_id.queue_id);
118 ret = rte_mp_reply(&mp_res, peer);
120 case MLX5_MP_REQ_QUEUE_RX_START:
121 mp_init_msg(&priv->mp_id, &mp_res, param->type);
122 res->result = mlx5_rx_queue_start_primary
123 (dev, param->args.queue_id.queue_id);
124 ret = rte_mp_reply(&mp_res, peer);
126 case MLX5_MP_REQ_QUEUE_TX_STOP:
127 mp_init_msg(&priv->mp_id, &mp_res, param->type);
128 res->result = mlx5_tx_queue_stop_primary
129 (dev, param->args.queue_id.queue_id);
130 ret = rte_mp_reply(&mp_res, peer);
132 case MLX5_MP_REQ_QUEUE_TX_START:
133 mp_init_msg(&priv->mp_id, &mp_res, param->type);
134 res->result = mlx5_tx_queue_start_primary
135 (dev, param->args.queue_id.queue_id);
136 ret = rte_mp_reply(&mp_res, peer);
140 DRV_LOG(ERR, "port %u invalid mp request type",
148 * IPC message handler of a secondary process.
151 * Pointer to Ethernet structure.
153 * Pointer to the peer socket path.
156 * 0 on success, a negative errno value otherwise and rte_errno is set.
159 mlx5_mp_os_secondary_handle(const struct rte_mp_msg *mp_msg, const void *peer)
161 struct rte_mp_msg mp_res;
162 struct mlx5_mp_param *res = (struct mlx5_mp_param *)mp_res.param;
163 const struct mlx5_mp_param *param =
164 (const struct mlx5_mp_param *)mp_msg->param;
165 struct rte_eth_dev *dev;
166 struct mlx5_proc_priv *ppriv;
167 struct mlx5_priv *priv;
170 MLX5_ASSERT(rte_eal_process_type() == RTE_PROC_SECONDARY);
171 if (!rte_eth_dev_is_valid_port(param->port_id)) {
173 DRV_LOG(ERR, "port %u invalid port ID", param->port_id);
176 dev = &rte_eth_devices[param->port_id];
177 priv = dev->data->dev_private;
178 switch (param->type) {
179 case MLX5_MP_REQ_START_RXTX:
180 DRV_LOG(INFO, "port %u starting datapath", dev->data->port_id);
181 dev->rx_pkt_burst = mlx5_select_rx_function(dev);
182 dev->tx_pkt_burst = mlx5_select_tx_function(dev);
183 ppriv = (struct mlx5_proc_priv *)dev->process_private;
184 /* If Tx queue number changes, re-initialize UAR. */
185 if (ppriv->uar_table_sz != priv->txqs_n) {
186 mlx5_tx_uar_uninit_secondary(dev);
187 mlx5_proc_priv_uninit(dev);
188 ret = mlx5_proc_priv_init(dev);
191 ret = mlx5_tx_uar_init_secondary(dev, mp_msg->fds[0]);
193 mlx5_proc_priv_uninit(dev);
198 mp_init_msg(&priv->mp_id, &mp_res, param->type);
200 ret = rte_mp_reply(&mp_res, peer);
202 case MLX5_MP_REQ_STOP_RXTX:
203 DRV_LOG(INFO, "port %u stopping datapath", dev->data->port_id);
204 dev->rx_pkt_burst = removed_rx_burst;
205 dev->tx_pkt_burst = removed_tx_burst;
207 mp_init_msg(&priv->mp_id, &mp_res, param->type);
209 ret = rte_mp_reply(&mp_res, peer);
213 DRV_LOG(ERR, "port %u invalid mp request type",
221 * Broadcast request of stopping/starting data-path to secondary processes.
224 * Pointer to Ethernet structure.
229 mp_req_on_rxtx(struct rte_eth_dev *dev, enum mlx5_mp_req_type type)
231 struct rte_mp_msg mp_req;
232 struct rte_mp_msg *mp_res;
233 struct rte_mp_reply mp_rep;
234 struct mlx5_mp_param *res;
235 struct timespec ts = {.tv_sec = MLX5_MP_REQ_TIMEOUT_SEC, .tv_nsec = 0};
236 struct mlx5_priv *priv = dev->data->dev_private;
240 MLX5_ASSERT(rte_eal_process_type() == RTE_PROC_PRIMARY);
241 if (!mlx5_shared_data->secondary_cnt)
243 if (type != MLX5_MP_REQ_START_RXTX && type != MLX5_MP_REQ_STOP_RXTX) {
244 DRV_LOG(ERR, "port %u unknown request (req_type %d)",
245 dev->data->port_id, type);
248 mp_init_msg(&priv->mp_id, &mp_req, type);
249 if (type == MLX5_MP_REQ_START_RXTX) {
251 mp_req.fds[0] = ((struct ibv_context *)priv->sh->ctx)->cmd_fd;
253 ret = rte_mp_request_sync(&mp_req, &mp_rep, &ts);
255 if (rte_errno != ENOTSUP)
256 DRV_LOG(ERR, "port %u failed to request stop/start Rx/Tx (%d)",
257 dev->data->port_id, type);
260 if (mp_rep.nb_sent != mp_rep.nb_received) {
262 "port %u not all secondaries responded (req_type %d)",
263 dev->data->port_id, type);
266 for (i = 0; i < mp_rep.nb_received; i++) {
267 mp_res = &mp_rep.msgs[i];
268 res = (struct mlx5_mp_param *)mp_res->param;
270 DRV_LOG(ERR, "port %u request failed on secondary #%d",
271 dev->data->port_id, i);
276 mlx5_free(mp_rep.msgs);
280 * Broadcast request of starting data-path to secondary processes. The request
284 * Pointer to Ethernet structure.
287 mlx5_mp_os_req_start_rxtx(struct rte_eth_dev *dev)
289 mp_req_on_rxtx(dev, MLX5_MP_REQ_START_RXTX);
293 * Broadcast request of stopping data-path to secondary processes. The request
297 * Pointer to Ethernet structure.
300 mlx5_mp_os_req_stop_rxtx(struct rte_eth_dev *dev)
302 mp_req_on_rxtx(dev, MLX5_MP_REQ_STOP_RXTX);
306 * Request Verbs Rx/Tx queue stop or start to the primary process.
309 * Pointer to Ethernet structure.
311 * Queue ID to control.
314 * MLX5_MP_REQ_QUEUE_RX_START - start Rx queue
315 * MLX5_MP_REQ_QUEUE_TX_START - stop Tx queue
316 * MLX5_MP_REQ_QUEUE_RX_STOP - stop Rx queue
317 * MLX5_MP_REQ_QUEUE_TX_STOP - stop Tx queue
319 * 0 on success, a negative errno value otherwise and
323 mlx5_mp_os_req_queue_control(struct rte_eth_dev *dev, uint16_t queue_id,
324 enum mlx5_mp_req_type req_type)
326 struct rte_mp_msg mp_req;
327 struct rte_mp_msg *mp_res;
328 struct rte_mp_reply mp_rep;
329 struct mlx5_mp_param *req = (struct mlx5_mp_param *)mp_req.param;
330 struct mlx5_mp_param *res;
331 struct timespec ts = {.tv_sec = MLX5_MP_REQ_TIMEOUT_SEC, .tv_nsec = 0};
332 struct mlx5_priv *priv;
335 MLX5_ASSERT(rte_eal_process_type() == RTE_PROC_SECONDARY);
336 priv = dev->data->dev_private;
337 mp_init_msg(&priv->mp_id, &mp_req, req_type);
338 req->args.queue_id.queue_id = queue_id;
339 ret = rte_mp_request_sync(&mp_req, &mp_rep, &ts);
341 DRV_LOG(ERR, "port %u request to primary process failed",
345 MLX5_ASSERT(mp_rep.nb_received == 1);
346 mp_res = &mp_rep.msgs[0];
347 res = (struct mlx5_mp_param *)mp_res->param;