35b2dfd3b296d08a8dd986517d40237b28197491
[dpdk.git] / drivers / net / mlx5 / linux / mlx5_mp_os.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2019 6WIND S.A.
3  * Copyright 2019 Mellanox Technologies, Ltd
4  */
5
6 #include <stdio.h>
7 #include <time.h>
8
9 #include <rte_eal.h>
10 #include <ethdev_driver.h>
11 #include <rte_string_fns.h>
12
13 #include <mlx5_common_mp.h>
14 #include <mlx5_common_mr.h>
15 #include <mlx5_malloc.h>
16
17 #include "mlx5.h"
18 #include "mlx5_rxtx.h"
19 #include "mlx5_rx.h"
20 #include "mlx5_tx.h"
21 #include "mlx5_utils.h"
22
23 /**
24  * Handle a port-agnostic message.
25  *
26  * @return
27  *   0 on success, 1 when message is not port-agnostic, (-1) on error.
28  */
29 static int
30 mlx5_mp_os_handle_port_agnostic(const struct rte_mp_msg *mp_msg,
31                                 const void *peer)
32 {
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;
39
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 = &param->args.mempool_reg;
45                 res->result = mlx5_mr_mempool_register(mpr->share_cache,
46                                                        mpr->pd, mpr->mempool,
47                                                        NULL);
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 = &param->args.mempool_reg;
53                 res->result = mlx5_mr_mempool_unregister(mpr->share_cache,
54                                                          mpr->mempool, NULL);
55                 return rte_mp_reply(&mp_res, peer);
56         default:
57                 return 1;
58         }
59         return -1;
60 }
61
62 int
63 mlx5_mp_os_primary_handle(const struct rte_mp_msg *mp_msg, const void *peer)
64 {
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;
73         uint32_t lkey;
74         int ret;
75
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);
79         if (ret <= 0)
80                 return ret;
81         /* Port-specific messages. */
82         if (!rte_eth_dev_is_valid_port(param->port_id)) {
83                 rte_errno = ENODEV;
84                 DRV_LOG(ERR, "port %u invalid port ID", param->port_id);
85                 return -rte_errno;
86         }
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);
100                 break;
101         case MLX5_MP_REQ_VERBS_CMD_FD:
102                 mp_init_msg(&priv->mp_id, &mp_res, param->type);
103                 mp_res.num_fds = 1;
104                 mp_res.fds[0] = ((struct ibv_context *)cdev->ctx)->cmd_fd;
105                 res->result = 0;
106                 ret = rte_mp_reply(&mp_res, peer);
107                 break;
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, &param->args.state_modify);
112                 ret = rte_mp_reply(&mp_res, peer);
113                 break;
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);
119                 break;
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);
125                 break;
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);
131                 break;
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);
137                 break;
138         default:
139                 rte_errno = EINVAL;
140                 DRV_LOG(ERR, "port %u invalid mp request type",
141                         dev->data->port_id);
142                 return -rte_errno;
143         }
144         return ret;
145 }
146
147 /**
148  * IPC message handler of a secondary process.
149  *
150  * @param[in] dev
151  *   Pointer to Ethernet structure.
152  * @param[in] peer
153  *   Pointer to the peer socket path.
154  *
155  * @return
156  *   0 on success, a negative errno value otherwise and rte_errno is set.
157  */
158 int
159 mlx5_mp_os_secondary_handle(const struct rte_mp_msg *mp_msg, const void *peer)
160 {
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;
168         int ret;
169
170         MLX5_ASSERT(rte_eal_process_type() == RTE_PROC_SECONDARY);
171         if (!rte_eth_dev_is_valid_port(param->port_id)) {
172                 rte_errno = ENODEV;
173                 DRV_LOG(ERR, "port %u invalid port ID", param->port_id);
174                 return -rte_errno;
175         }
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);
189                         if (ret)
190                                 return -rte_errno;
191                         ret = mlx5_tx_uar_init_secondary(dev, mp_msg->fds[0]);
192                         if (ret) {
193                                 mlx5_proc_priv_uninit(dev);
194                                 return -rte_errno;
195                         }
196                 }
197                 rte_mb();
198                 mp_init_msg(&priv->mp_id, &mp_res, param->type);
199                 res->result = 0;
200                 ret = rte_mp_reply(&mp_res, peer);
201                 break;
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;
206                 rte_mb();
207                 mp_init_msg(&priv->mp_id, &mp_res, param->type);
208                 res->result = 0;
209                 ret = rte_mp_reply(&mp_res, peer);
210                 break;
211         default:
212                 rte_errno = EINVAL;
213                 DRV_LOG(ERR, "port %u invalid mp request type",
214                         dev->data->port_id);
215                 return -rte_errno;
216         }
217         return ret;
218 }
219
220 /**
221  * Broadcast request of stopping/starting data-path to secondary processes.
222  *
223  * @param[in] dev
224  *   Pointer to Ethernet structure.
225  * @param[in] type
226  *   Request type.
227  */
228 static void
229 mp_req_on_rxtx(struct rte_eth_dev *dev, enum mlx5_mp_req_type type)
230 {
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;
237         int ret;
238         int i;
239
240         MLX5_ASSERT(rte_eal_process_type() == RTE_PROC_PRIMARY);
241         if (!mlx5_shared_data->secondary_cnt)
242                 return;
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);
246                 return;
247         }
248         mp_init_msg(&priv->mp_id, &mp_req, type);
249         if (type == MLX5_MP_REQ_START_RXTX) {
250                 mp_req.num_fds = 1;
251                 mp_req.fds[0] =
252                         ((struct ibv_context *)priv->sh->cdev->ctx)->cmd_fd;
253         }
254         ret = rte_mp_request_sync(&mp_req, &mp_rep, &ts);
255         if (ret) {
256                 if (rte_errno != ENOTSUP)
257                         DRV_LOG(ERR, "port %u failed to request stop/start Rx/Tx (%d)",
258                                 dev->data->port_id, type);
259                 goto exit;
260         }
261         if (mp_rep.nb_sent != mp_rep.nb_received) {
262                 DRV_LOG(ERR,
263                         "port %u not all secondaries responded (req_type %d)",
264                         dev->data->port_id, type);
265                 goto exit;
266         }
267         for (i = 0; i < mp_rep.nb_received; i++) {
268                 mp_res = &mp_rep.msgs[i];
269                 res = (struct mlx5_mp_param *)mp_res->param;
270                 if (res->result) {
271                         DRV_LOG(ERR, "port %u request failed on secondary #%d",
272                                 dev->data->port_id, i);
273                         goto exit;
274                 }
275         }
276 exit:
277         mlx5_free(mp_rep.msgs);
278 }
279
280 /**
281  * Broadcast request of starting data-path to secondary processes. The request
282  * is synchronous.
283  *
284  * @param[in] dev
285  *   Pointer to Ethernet structure.
286  */
287 void
288 mlx5_mp_os_req_start_rxtx(struct rte_eth_dev *dev)
289 {
290         mp_req_on_rxtx(dev, MLX5_MP_REQ_START_RXTX);
291 }
292
293 /**
294  * Broadcast request of stopping data-path to secondary processes. The request
295  * is synchronous.
296  *
297  * @param[in] dev
298  *   Pointer to Ethernet structure.
299  */
300 void
301 mlx5_mp_os_req_stop_rxtx(struct rte_eth_dev *dev)
302 {
303         mp_req_on_rxtx(dev, MLX5_MP_REQ_STOP_RXTX);
304 }
305
306 /**
307  * Request Verbs Rx/Tx queue stop or start to the primary process.
308  *
309  * @param[in] dev
310  *   Pointer to Ethernet structure.
311  * @param queue_id
312  *   Queue ID to control.
313  * @param req_type
314  *   request type
315  *     MLX5_MP_REQ_QUEUE_RX_START - start Rx queue
316  *     MLX5_MP_REQ_QUEUE_TX_START - stop Tx queue
317  *     MLX5_MP_REQ_QUEUE_RX_STOP - stop Rx queue
318  *     MLX5_MP_REQ_QUEUE_TX_STOP - stop Tx queue
319  * @return
320  *   0 on success, a negative errno value otherwise and
321  *     rte_errno is set.
322  */
323 int
324 mlx5_mp_os_req_queue_control(struct rte_eth_dev *dev, uint16_t queue_id,
325                           enum mlx5_mp_req_type req_type)
326 {
327         struct rte_mp_msg mp_req;
328         struct rte_mp_msg *mp_res;
329         struct rte_mp_reply mp_rep;
330         struct mlx5_mp_param *req = (struct mlx5_mp_param *)mp_req.param;
331         struct mlx5_mp_param *res;
332         struct timespec ts = {.tv_sec = MLX5_MP_REQ_TIMEOUT_SEC, .tv_nsec = 0};
333         struct mlx5_priv *priv;
334         int ret;
335
336         MLX5_ASSERT(rte_eal_process_type() == RTE_PROC_SECONDARY);
337         priv = dev->data->dev_private;
338         mp_init_msg(&priv->mp_id, &mp_req, req_type);
339         req->args.queue_id.queue_id = queue_id;
340         ret = rte_mp_request_sync(&mp_req, &mp_rep, &ts);
341         if (ret) {
342                 DRV_LOG(ERR, "port %u request to primary process failed",
343                         dev->data->port_id);
344                 return -rte_errno;
345         }
346         MLX5_ASSERT(mp_rep.nb_received == 1);
347         mp_res = &mp_rep.msgs[0];
348         res = (struct mlx5_mp_param *)mp_res->param;
349         ret = res->result;
350         free(mp_rep.msgs);
351         return ret;
352 }