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_mr_manage *mng = ¶m->args.mr_manage;
38 struct mr_cache_entry entry;
41 switch (param->type) {
42 case MLX5_MP_REQ_CREATE_MR:
43 mp_init_port_agnostic_msg(&mp_res, param->type);
44 lkey = mlx5_mr_create(mng->cdev, &mng->cdev->mr_scache, &entry,
46 if (lkey == UINT32_MAX)
47 res->result = -rte_errno;
48 return rte_mp_reply(&mp_res, peer);
49 case MLX5_MP_REQ_MEMPOOL_REGISTER:
50 mp_init_port_agnostic_msg(&mp_res, param->type);
51 res->result = mlx5_mr_mempool_register(mng->cdev, mng->mempool,
53 return rte_mp_reply(&mp_res, peer);
54 case MLX5_MP_REQ_MEMPOOL_UNREGISTER:
55 mp_init_port_agnostic_msg(&mp_res, param->type);
56 res->result = mlx5_mr_mempool_unregister(mng->cdev,
58 return rte_mp_reply(&mp_res, peer);
66 mlx5_mp_os_primary_handle(const struct rte_mp_msg *mp_msg, const void *peer)
68 struct rte_mp_msg mp_res;
69 struct mlx5_mp_param *res = (struct mlx5_mp_param *)mp_res.param;
70 const struct mlx5_mp_param *param =
71 (const struct mlx5_mp_param *)mp_msg->param;
72 struct rte_eth_dev *dev;
73 struct mlx5_priv *priv;
74 struct mlx5_common_device *cdev;
77 MLX5_ASSERT(rte_eal_process_type() == RTE_PROC_PRIMARY);
78 /* Port-agnostic messages. */
79 ret = mlx5_mp_os_handle_port_agnostic(mp_msg, peer);
82 /* Port-specific messages. */
83 if (!rte_eth_dev_is_valid_port(param->port_id)) {
85 DRV_LOG(ERR, "port %u invalid port ID", param->port_id);
88 dev = &rte_eth_devices[param->port_id];
89 priv = dev->data->dev_private;
90 cdev = priv->sh->cdev;
91 switch (param->type) {
92 case MLX5_MP_REQ_VERBS_CMD_FD:
93 mp_init_msg(&priv->mp_id, &mp_res, param->type);
95 mp_res.fds[0] = ((struct ibv_context *)cdev->ctx)->cmd_fd;
97 ret = rte_mp_reply(&mp_res, peer);
99 case MLX5_MP_REQ_QUEUE_STATE_MODIFY:
100 mp_init_msg(&priv->mp_id, &mp_res, param->type);
101 res->result = mlx5_queue_state_modify_primary
102 (dev, ¶m->args.state_modify);
103 ret = rte_mp_reply(&mp_res, peer);
105 case MLX5_MP_REQ_QUEUE_RX_STOP:
106 mp_init_msg(&priv->mp_id, &mp_res, param->type);
107 res->result = mlx5_rx_queue_stop_primary
108 (dev, param->args.queue_id.queue_id);
109 ret = rte_mp_reply(&mp_res, peer);
111 case MLX5_MP_REQ_QUEUE_RX_START:
112 mp_init_msg(&priv->mp_id, &mp_res, param->type);
113 res->result = mlx5_rx_queue_start_primary
114 (dev, param->args.queue_id.queue_id);
115 ret = rte_mp_reply(&mp_res, peer);
117 case MLX5_MP_REQ_QUEUE_TX_STOP:
118 mp_init_msg(&priv->mp_id, &mp_res, param->type);
119 res->result = mlx5_tx_queue_stop_primary
120 (dev, param->args.queue_id.queue_id);
121 ret = rte_mp_reply(&mp_res, peer);
123 case MLX5_MP_REQ_QUEUE_TX_START:
124 mp_init_msg(&priv->mp_id, &mp_res, param->type);
125 res->result = mlx5_tx_queue_start_primary
126 (dev, param->args.queue_id.queue_id);
127 ret = rte_mp_reply(&mp_res, peer);
131 DRV_LOG(ERR, "port %u invalid mp request type",
139 * IPC message handler of a secondary process.
142 * Pointer to Ethernet structure.
144 * Pointer to the peer socket path.
147 * 0 on success, a negative errno value otherwise and rte_errno is set.
150 mlx5_mp_os_secondary_handle(const struct rte_mp_msg *mp_msg, const void *peer)
152 struct rte_mp_msg mp_res;
153 struct mlx5_mp_param *res = (struct mlx5_mp_param *)mp_res.param;
154 const struct mlx5_mp_param *param =
155 (const struct mlx5_mp_param *)mp_msg->param;
156 struct rte_eth_dev *dev;
157 struct mlx5_proc_priv *ppriv;
158 struct mlx5_priv *priv;
161 MLX5_ASSERT(rte_eal_process_type() == RTE_PROC_SECONDARY);
162 if (!rte_eth_dev_is_valid_port(param->port_id)) {
164 DRV_LOG(ERR, "port %u invalid port ID", param->port_id);
167 dev = &rte_eth_devices[param->port_id];
168 priv = dev->data->dev_private;
169 switch (param->type) {
170 case MLX5_MP_REQ_START_RXTX:
171 DRV_LOG(INFO, "port %u starting datapath", dev->data->port_id);
172 dev->rx_pkt_burst = mlx5_select_rx_function(dev);
173 dev->tx_pkt_burst = mlx5_select_tx_function(dev);
174 ppriv = (struct mlx5_proc_priv *)dev->process_private;
175 /* If Tx queue number changes, re-initialize UAR. */
176 if (ppriv->uar_table_sz != priv->txqs_n) {
177 mlx5_tx_uar_uninit_secondary(dev);
178 mlx5_proc_priv_uninit(dev);
179 ret = mlx5_proc_priv_init(dev);
182 ret = mlx5_tx_uar_init_secondary(dev, mp_msg->fds[0]);
184 mlx5_proc_priv_uninit(dev);
189 mp_init_msg(&priv->mp_id, &mp_res, param->type);
191 ret = rte_mp_reply(&mp_res, peer);
193 case MLX5_MP_REQ_STOP_RXTX:
194 DRV_LOG(INFO, "port %u stopping datapath", dev->data->port_id);
195 dev->rx_pkt_burst = rte_eth_pkt_burst_dummy;
196 dev->tx_pkt_burst = rte_eth_pkt_burst_dummy;
198 mp_init_msg(&priv->mp_id, &mp_res, param->type);
200 ret = rte_mp_reply(&mp_res, peer);
204 DRV_LOG(ERR, "port %u invalid mp request type",
212 * Broadcast request of stopping/starting data-path to secondary processes.
215 * Pointer to Ethernet structure.
220 mp_req_on_rxtx(struct rte_eth_dev *dev, enum mlx5_mp_req_type type)
222 struct rte_mp_msg mp_req;
223 struct rte_mp_msg *mp_res;
224 struct rte_mp_reply mp_rep;
225 struct mlx5_mp_param *res;
226 struct timespec ts = {.tv_sec = MLX5_MP_REQ_TIMEOUT_SEC, .tv_nsec = 0};
227 struct mlx5_priv *priv = dev->data->dev_private;
231 MLX5_ASSERT(rte_eal_process_type() == RTE_PROC_PRIMARY);
232 if (!mlx5_shared_data->secondary_cnt)
234 if (type != MLX5_MP_REQ_START_RXTX && type != MLX5_MP_REQ_STOP_RXTX) {
235 DRV_LOG(ERR, "port %u unknown request (req_type %d)",
236 dev->data->port_id, type);
239 mp_init_msg(&priv->mp_id, &mp_req, type);
240 if (type == MLX5_MP_REQ_START_RXTX) {
243 ((struct ibv_context *)priv->sh->cdev->ctx)->cmd_fd;
245 ret = rte_mp_request_sync(&mp_req, &mp_rep, &ts);
247 if (rte_errno != ENOTSUP)
248 DRV_LOG(ERR, "port %u failed to request stop/start Rx/Tx (%d)",
249 dev->data->port_id, type);
252 if (mp_rep.nb_sent != mp_rep.nb_received) {
254 "port %u not all secondaries responded (req_type %d)",
255 dev->data->port_id, type);
258 for (i = 0; i < mp_rep.nb_received; i++) {
259 mp_res = &mp_rep.msgs[i];
260 res = (struct mlx5_mp_param *)mp_res->param;
262 DRV_LOG(ERR, "port %u request failed on secondary #%d",
263 dev->data->port_id, i);
268 mlx5_free(mp_rep.msgs);
272 * Broadcast request of starting data-path to secondary processes. The request
276 * Pointer to Ethernet structure.
279 mlx5_mp_os_req_start_rxtx(struct rte_eth_dev *dev)
281 mp_req_on_rxtx(dev, MLX5_MP_REQ_START_RXTX);
285 * Broadcast request of stopping data-path to secondary processes. The request
289 * Pointer to Ethernet structure.
292 mlx5_mp_os_req_stop_rxtx(struct rte_eth_dev *dev)
294 mp_req_on_rxtx(dev, MLX5_MP_REQ_STOP_RXTX);
298 * Request Verbs Rx/Tx queue stop or start to the primary process.
301 * Pointer to Ethernet structure.
303 * Queue ID to control.
306 * MLX5_MP_REQ_QUEUE_RX_START - start Rx queue
307 * MLX5_MP_REQ_QUEUE_TX_START - stop Tx queue
308 * MLX5_MP_REQ_QUEUE_RX_STOP - stop Rx queue
309 * MLX5_MP_REQ_QUEUE_TX_STOP - stop Tx queue
311 * 0 on success, a negative errno value otherwise and
315 mlx5_mp_os_req_queue_control(struct rte_eth_dev *dev, uint16_t queue_id,
316 enum mlx5_mp_req_type req_type)
318 struct rte_mp_msg mp_req;
319 struct rte_mp_msg *mp_res;
320 struct rte_mp_reply mp_rep;
321 struct mlx5_mp_param *req = (struct mlx5_mp_param *)mp_req.param;
322 struct mlx5_mp_param *res;
323 struct timespec ts = {.tv_sec = MLX5_MP_REQ_TIMEOUT_SEC, .tv_nsec = 0};
324 struct mlx5_priv *priv;
327 MLX5_ASSERT(rte_eal_process_type() == RTE_PROC_SECONDARY);
328 priv = dev->data->dev_private;
329 mp_init_msg(&priv->mp_id, &mp_req, req_type);
330 req->args.queue_id.queue_id = queue_id;
331 ret = rte_mp_request_sync(&mp_req, &mp_rep, &ts);
333 DRV_LOG(ERR, "port %u request to primary process failed",
337 MLX5_ASSERT(mp_rep.nb_received == 1);
338 mp_res = &mp_rep.msgs[0];
339 res = (struct mlx5_mp_param *)mp_res->param;