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(cdev->pd, &cdev->mr_scache,
94 &entry, param->args.addr,
95 cdev->config.mr_ext_memseg_en);
96 if (lkey == UINT32_MAX)
97 res->result = -rte_errno;
98 ret = rte_mp_reply(&mp_res, peer);
100 case MLX5_MP_REQ_VERBS_CMD_FD:
101 mp_init_msg(&priv->mp_id, &mp_res, param->type);
103 mp_res.fds[0] = ((struct ibv_context *)cdev->ctx)->cmd_fd;
105 ret = rte_mp_reply(&mp_res, peer);
107 case MLX5_MP_REQ_QUEUE_STATE_MODIFY:
108 mp_init_msg(&priv->mp_id, &mp_res, param->type);
109 res->result = mlx5_queue_state_modify_primary
110 (dev, ¶m->args.state_modify);
111 ret = rte_mp_reply(&mp_res, peer);
113 case MLX5_MP_REQ_QUEUE_RX_STOP:
114 mp_init_msg(&priv->mp_id, &mp_res, param->type);
115 res->result = mlx5_rx_queue_stop_primary
116 (dev, param->args.queue_id.queue_id);
117 ret = rte_mp_reply(&mp_res, peer);
119 case MLX5_MP_REQ_QUEUE_RX_START:
120 mp_init_msg(&priv->mp_id, &mp_res, param->type);
121 res->result = mlx5_rx_queue_start_primary
122 (dev, param->args.queue_id.queue_id);
123 ret = rte_mp_reply(&mp_res, peer);
125 case MLX5_MP_REQ_QUEUE_TX_STOP:
126 mp_init_msg(&priv->mp_id, &mp_res, param->type);
127 res->result = mlx5_tx_queue_stop_primary
128 (dev, param->args.queue_id.queue_id);
129 ret = rte_mp_reply(&mp_res, peer);
131 case MLX5_MP_REQ_QUEUE_TX_START:
132 mp_init_msg(&priv->mp_id, &mp_res, param->type);
133 res->result = mlx5_tx_queue_start_primary
134 (dev, param->args.queue_id.queue_id);
135 ret = rte_mp_reply(&mp_res, peer);
139 DRV_LOG(ERR, "port %u invalid mp request type",
147 * IPC message handler of a secondary process.
150 * Pointer to Ethernet structure.
152 * Pointer to the peer socket path.
155 * 0 on success, a negative errno value otherwise and rte_errno is set.
158 mlx5_mp_os_secondary_handle(const struct rte_mp_msg *mp_msg, const void *peer)
160 struct rte_mp_msg mp_res;
161 struct mlx5_mp_param *res = (struct mlx5_mp_param *)mp_res.param;
162 const struct mlx5_mp_param *param =
163 (const struct mlx5_mp_param *)mp_msg->param;
164 struct rte_eth_dev *dev;
165 struct mlx5_proc_priv *ppriv;
166 struct mlx5_priv *priv;
169 MLX5_ASSERT(rte_eal_process_type() == RTE_PROC_SECONDARY);
170 if (!rte_eth_dev_is_valid_port(param->port_id)) {
172 DRV_LOG(ERR, "port %u invalid port ID", param->port_id);
175 dev = &rte_eth_devices[param->port_id];
176 priv = dev->data->dev_private;
177 switch (param->type) {
178 case MLX5_MP_REQ_START_RXTX:
179 DRV_LOG(INFO, "port %u starting datapath", dev->data->port_id);
180 dev->rx_pkt_burst = mlx5_select_rx_function(dev);
181 dev->tx_pkt_burst = mlx5_select_tx_function(dev);
182 ppriv = (struct mlx5_proc_priv *)dev->process_private;
183 /* If Tx queue number changes, re-initialize UAR. */
184 if (ppriv->uar_table_sz != priv->txqs_n) {
185 mlx5_tx_uar_uninit_secondary(dev);
186 mlx5_proc_priv_uninit(dev);
187 ret = mlx5_proc_priv_init(dev);
190 ret = mlx5_tx_uar_init_secondary(dev, mp_msg->fds[0]);
192 mlx5_proc_priv_uninit(dev);
197 mp_init_msg(&priv->mp_id, &mp_res, param->type);
199 ret = rte_mp_reply(&mp_res, peer);
201 case MLX5_MP_REQ_STOP_RXTX:
202 DRV_LOG(INFO, "port %u stopping datapath", dev->data->port_id);
203 dev->rx_pkt_burst = removed_rx_burst;
204 dev->tx_pkt_burst = removed_tx_burst;
206 mp_init_msg(&priv->mp_id, &mp_res, param->type);
208 ret = rte_mp_reply(&mp_res, peer);
212 DRV_LOG(ERR, "port %u invalid mp request type",
220 * Broadcast request of stopping/starting data-path to secondary processes.
223 * Pointer to Ethernet structure.
228 mp_req_on_rxtx(struct rte_eth_dev *dev, enum mlx5_mp_req_type type)
230 struct rte_mp_msg mp_req;
231 struct rte_mp_msg *mp_res;
232 struct rte_mp_reply mp_rep;
233 struct mlx5_mp_param *res;
234 struct timespec ts = {.tv_sec = MLX5_MP_REQ_TIMEOUT_SEC, .tv_nsec = 0};
235 struct mlx5_priv *priv = dev->data->dev_private;
239 MLX5_ASSERT(rte_eal_process_type() == RTE_PROC_PRIMARY);
240 if (!mlx5_shared_data->secondary_cnt)
242 if (type != MLX5_MP_REQ_START_RXTX && type != MLX5_MP_REQ_STOP_RXTX) {
243 DRV_LOG(ERR, "port %u unknown request (req_type %d)",
244 dev->data->port_id, type);
247 mp_init_msg(&priv->mp_id, &mp_req, type);
248 if (type == MLX5_MP_REQ_START_RXTX) {
251 ((struct ibv_context *)priv->sh->cdev->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;