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 mr_cache_entry entry;
75 MLX5_ASSERT(rte_eal_process_type() == RTE_PROC_PRIMARY);
76 /* Port-agnostic messages. */
77 ret = mlx5_mp_os_handle_port_agnostic(mp_msg, peer);
80 /* Port-specific messages. */
81 if (!rte_eth_dev_is_valid_port(param->port_id)) {
83 DRV_LOG(ERR, "port %u invalid port ID", param->port_id);
86 dev = &rte_eth_devices[param->port_id];
87 priv = dev->data->dev_private;
88 switch (param->type) {
89 case MLX5_MP_REQ_CREATE_MR:
90 mp_init_msg(&priv->mp_id, &mp_res, param->type);
91 lkey = mlx5_mr_create_primary(priv->sh->pd,
92 &priv->sh->share_cache,
93 &entry, param->args.addr,
94 priv->config.mr_ext_memseg_en);
95 if (lkey == UINT32_MAX)
96 res->result = -rte_errno;
97 ret = rte_mp_reply(&mp_res, peer);
99 case MLX5_MP_REQ_VERBS_CMD_FD:
100 mp_init_msg(&priv->mp_id, &mp_res, param->type);
102 mp_res.fds[0] = ((struct ibv_context *)priv->sh->ctx)->cmd_fd;
104 ret = rte_mp_reply(&mp_res, peer);
106 case MLX5_MP_REQ_QUEUE_STATE_MODIFY:
107 mp_init_msg(&priv->mp_id, &mp_res, param->type);
108 res->result = mlx5_queue_state_modify_primary
109 (dev, ¶m->args.state_modify);
110 ret = rte_mp_reply(&mp_res, peer);
112 case MLX5_MP_REQ_QUEUE_RX_STOP:
113 mp_init_msg(&priv->mp_id, &mp_res, param->type);
114 res->result = mlx5_rx_queue_stop_primary
115 (dev, param->args.queue_id.queue_id);
116 ret = rte_mp_reply(&mp_res, peer);
118 case MLX5_MP_REQ_QUEUE_RX_START:
119 mp_init_msg(&priv->mp_id, &mp_res, param->type);
120 res->result = mlx5_rx_queue_start_primary
121 (dev, param->args.queue_id.queue_id);
122 ret = rte_mp_reply(&mp_res, peer);
124 case MLX5_MP_REQ_QUEUE_TX_STOP:
125 mp_init_msg(&priv->mp_id, &mp_res, param->type);
126 res->result = mlx5_tx_queue_stop_primary
127 (dev, param->args.queue_id.queue_id);
128 ret = rte_mp_reply(&mp_res, peer);
130 case MLX5_MP_REQ_QUEUE_TX_START:
131 mp_init_msg(&priv->mp_id, &mp_res, param->type);
132 res->result = mlx5_tx_queue_start_primary
133 (dev, param->args.queue_id.queue_id);
134 ret = rte_mp_reply(&mp_res, peer);
138 DRV_LOG(ERR, "port %u invalid mp request type",
146 * IPC message handler of a secondary process.
149 * Pointer to Ethernet structure.
151 * Pointer to the peer socket path.
154 * 0 on success, a negative errno value otherwise and rte_errno is set.
157 mlx5_mp_os_secondary_handle(const struct rte_mp_msg *mp_msg, const void *peer)
159 struct rte_mp_msg mp_res;
160 struct mlx5_mp_param *res = (struct mlx5_mp_param *)mp_res.param;
161 const struct mlx5_mp_param *param =
162 (const struct mlx5_mp_param *)mp_msg->param;
163 struct rte_eth_dev *dev;
164 struct mlx5_proc_priv *ppriv;
165 struct mlx5_priv *priv;
168 MLX5_ASSERT(rte_eal_process_type() == RTE_PROC_SECONDARY);
169 if (!rte_eth_dev_is_valid_port(param->port_id)) {
171 DRV_LOG(ERR, "port %u invalid port ID", param->port_id);
174 dev = &rte_eth_devices[param->port_id];
175 priv = dev->data->dev_private;
176 switch (param->type) {
177 case MLX5_MP_REQ_START_RXTX:
178 DRV_LOG(INFO, "port %u starting datapath", dev->data->port_id);
179 dev->rx_pkt_burst = mlx5_select_rx_function(dev);
180 dev->tx_pkt_burst = mlx5_select_tx_function(dev);
181 ppriv = (struct mlx5_proc_priv *)dev->process_private;
182 /* If Tx queue number changes, re-initialize UAR. */
183 if (ppriv->uar_table_sz != priv->txqs_n) {
184 mlx5_tx_uar_uninit_secondary(dev);
185 mlx5_proc_priv_uninit(dev);
186 ret = mlx5_proc_priv_init(dev);
189 ret = mlx5_tx_uar_init_secondary(dev, mp_msg->fds[0]);
191 mlx5_proc_priv_uninit(dev);
196 mp_init_msg(&priv->mp_id, &mp_res, param->type);
198 ret = rte_mp_reply(&mp_res, peer);
200 case MLX5_MP_REQ_STOP_RXTX:
201 DRV_LOG(INFO, "port %u stopping datapath", dev->data->port_id);
202 dev->rx_pkt_burst = removed_rx_burst;
203 dev->tx_pkt_burst = removed_tx_burst;
205 mp_init_msg(&priv->mp_id, &mp_res, param->type);
207 ret = rte_mp_reply(&mp_res, peer);
211 DRV_LOG(ERR, "port %u invalid mp request type",
219 * Broadcast request of stopping/starting data-path to secondary processes.
222 * Pointer to Ethernet structure.
227 mp_req_on_rxtx(struct rte_eth_dev *dev, enum mlx5_mp_req_type type)
229 struct rte_mp_msg mp_req;
230 struct rte_mp_msg *mp_res;
231 struct rte_mp_reply mp_rep;
232 struct mlx5_mp_param *res;
233 struct timespec ts = {.tv_sec = MLX5_MP_REQ_TIMEOUT_SEC, .tv_nsec = 0};
234 struct mlx5_priv *priv = dev->data->dev_private;
238 MLX5_ASSERT(rte_eal_process_type() == RTE_PROC_PRIMARY);
239 if (!mlx5_shared_data->secondary_cnt)
241 if (type != MLX5_MP_REQ_START_RXTX && type != MLX5_MP_REQ_STOP_RXTX) {
242 DRV_LOG(ERR, "port %u unknown request (req_type %d)",
243 dev->data->port_id, type);
246 mp_init_msg(&priv->mp_id, &mp_req, type);
247 if (type == MLX5_MP_REQ_START_RXTX) {
249 mp_req.fds[0] = ((struct ibv_context *)priv->sh->ctx)->cmd_fd;
251 ret = rte_mp_request_sync(&mp_req, &mp_rep, &ts);
253 if (rte_errno != ENOTSUP)
254 DRV_LOG(ERR, "port %u failed to request stop/start Rx/Tx (%d)",
255 dev->data->port_id, type);
258 if (mp_rep.nb_sent != mp_rep.nb_received) {
260 "port %u not all secondaries responded (req_type %d)",
261 dev->data->port_id, type);
264 for (i = 0; i < mp_rep.nb_received; i++) {
265 mp_res = &mp_rep.msgs[i];
266 res = (struct mlx5_mp_param *)mp_res->param;
268 DRV_LOG(ERR, "port %u request failed on secondary #%d",
269 dev->data->port_id, i);
274 mlx5_free(mp_rep.msgs);
278 * Broadcast request of starting data-path to secondary processes. The request
282 * Pointer to Ethernet structure.
285 mlx5_mp_os_req_start_rxtx(struct rte_eth_dev *dev)
287 mp_req_on_rxtx(dev, MLX5_MP_REQ_START_RXTX);
291 * Broadcast request of stopping data-path to secondary processes. The request
295 * Pointer to Ethernet structure.
298 mlx5_mp_os_req_stop_rxtx(struct rte_eth_dev *dev)
300 mp_req_on_rxtx(dev, MLX5_MP_REQ_STOP_RXTX);
304 * Request Verbs Rx/Tx queue stop or start to the primary process.
307 * Pointer to Ethernet structure.
309 * Queue ID to control.
312 * MLX5_MP_REQ_QUEUE_RX_START - start Rx queue
313 * MLX5_MP_REQ_QUEUE_TX_START - stop Tx queue
314 * MLX5_MP_REQ_QUEUE_RX_STOP - stop Rx queue
315 * MLX5_MP_REQ_QUEUE_TX_STOP - stop Tx queue
317 * 0 on success, a negative errno value otherwise and
321 mlx5_mp_os_req_queue_control(struct rte_eth_dev *dev, uint16_t queue_id,
322 enum mlx5_mp_req_type req_type)
324 struct rte_mp_msg mp_req;
325 struct rte_mp_msg *mp_res;
326 struct rte_mp_reply mp_rep;
327 struct mlx5_mp_param *req = (struct mlx5_mp_param *)mp_req.param;
328 struct mlx5_mp_param *res;
329 struct timespec ts = {.tv_sec = MLX5_MP_REQ_TIMEOUT_SEC, .tv_nsec = 0};
330 struct mlx5_priv *priv;
333 MLX5_ASSERT(rte_eal_process_type() == RTE_PROC_SECONDARY);
334 priv = dev->data->dev_private;
335 mp_init_msg(&priv->mp_id, &mp_req, req_type);
336 req->args.queue_id.queue_id = queue_id;
337 ret = rte_mp_request_sync(&mp_req, &mp_rep, &ts);
339 DRV_LOG(ERR, "port %u request to primary process failed",
343 MLX5_ASSERT(mp_rep.nb_received == 1);
344 mp_res = &mp_rep.msgs[0];
345 res = (struct mlx5_mp_param *)mp_res->param;