7ad322d4749e9386d312b26fc282ea765d27b2bb
[dpdk.git] / drivers / net / mlx5 / mlx5_mp.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 <rte_ethdev_driver.h>
11 #include <rte_string_fns.h>
12
13 #include <mlx5_common_mp.h>
14 #include <mlx5_common_mr.h>
15
16 #include "mlx5.h"
17 #include "mlx5_rxtx.h"
18 #include "mlx5_utils.h"
19
20 int
21 mlx5_mp_primary_handle(const struct rte_mp_msg *mp_msg, const void *peer)
22 {
23         struct rte_mp_msg mp_res;
24         struct mlx5_mp_param *res = (struct mlx5_mp_param *)mp_res.param;
25         const struct mlx5_mp_param *param =
26                 (const struct mlx5_mp_param *)mp_msg->param;
27         struct rte_eth_dev *dev;
28         struct mlx5_priv *priv;
29         struct mr_cache_entry entry;
30         uint32_t lkey;
31         int ret;
32
33         MLX5_ASSERT(rte_eal_process_type() == RTE_PROC_PRIMARY);
34         if (!rte_eth_dev_is_valid_port(param->port_id)) {
35                 rte_errno = ENODEV;
36                 DRV_LOG(ERR, "port %u invalid port ID", param->port_id);
37                 return -rte_errno;
38         }
39         dev = &rte_eth_devices[param->port_id];
40         priv = dev->data->dev_private;
41         switch (param->type) {
42         case MLX5_MP_REQ_CREATE_MR:
43                 mp_init_msg(&priv->mp_id, &mp_res, param->type);
44                 lkey = mlx5_mr_create_primary(priv->sh->pd,
45                                               &priv->sh->share_cache,
46                                               &entry, param->args.addr,
47                                               priv->config.mr_ext_memseg_en);
48                 if (lkey == UINT32_MAX)
49                         res->result = -rte_errno;
50                 ret = rte_mp_reply(&mp_res, peer);
51                 break;
52         case MLX5_MP_REQ_VERBS_CMD_FD:
53                 mp_init_msg(&priv->mp_id, &mp_res, param->type);
54                 mp_res.num_fds = 1;
55                 mp_res.fds[0] = priv->sh->ctx->cmd_fd;
56                 res->result = 0;
57                 ret = rte_mp_reply(&mp_res, peer);
58                 break;
59         case MLX5_MP_REQ_QUEUE_STATE_MODIFY:
60                 mp_init_msg(&priv->mp_id, &mp_res, param->type);
61                 res->result = mlx5_queue_state_modify_primary
62                                         (dev, &param->args.state_modify);
63                 ret = rte_mp_reply(&mp_res, peer);
64                 break;
65         default:
66                 rte_errno = EINVAL;
67                 DRV_LOG(ERR, "port %u invalid mp request type",
68                         dev->data->port_id);
69                 return -rte_errno;
70         }
71         return ret;
72 }
73
74 /**
75  * IPC message handler of a secondary process.
76  *
77  * @param[in] dev
78  *   Pointer to Ethernet structure.
79  * @param[in] peer
80  *   Pointer to the peer socket path.
81  *
82  * @return
83  *   0 on success, a negative errno value otherwise and rte_errno is set.
84  */
85 int
86 mlx5_mp_secondary_handle(const struct rte_mp_msg *mp_msg, const void *peer)
87 {
88         struct rte_mp_msg mp_res;
89         struct mlx5_mp_param *res = (struct mlx5_mp_param *)mp_res.param;
90         const struct mlx5_mp_param *param =
91                 (const struct mlx5_mp_param *)mp_msg->param;
92         struct rte_eth_dev *dev;
93         struct mlx5_priv *priv;
94         int ret;
95
96         MLX5_ASSERT(rte_eal_process_type() == RTE_PROC_SECONDARY);
97         if (!rte_eth_dev_is_valid_port(param->port_id)) {
98                 rte_errno = ENODEV;
99                 DRV_LOG(ERR, "port %u invalid port ID", param->port_id);
100                 return -rte_errno;
101         }
102         dev = &rte_eth_devices[param->port_id];
103         priv = dev->data->dev_private;
104         switch (param->type) {
105         case MLX5_MP_REQ_START_RXTX:
106                 DRV_LOG(INFO, "port %u starting datapath", dev->data->port_id);
107                 rte_mb();
108                 dev->rx_pkt_burst = mlx5_select_rx_function(dev);
109                 dev->tx_pkt_burst = mlx5_select_tx_function(dev);
110                 mp_init_msg(&priv->mp_id, &mp_res, param->type);
111                 res->result = 0;
112                 ret = rte_mp_reply(&mp_res, peer);
113                 break;
114         case MLX5_MP_REQ_STOP_RXTX:
115                 DRV_LOG(INFO, "port %u stopping datapath", dev->data->port_id);
116                 dev->rx_pkt_burst = removed_rx_burst;
117                 dev->tx_pkt_burst = removed_tx_burst;
118                 rte_mb();
119                 mp_init_msg(&priv->mp_id, &mp_res, param->type);
120                 res->result = 0;
121                 ret = rte_mp_reply(&mp_res, peer);
122                 break;
123         default:
124                 rte_errno = EINVAL;
125                 DRV_LOG(ERR, "port %u invalid mp request type",
126                         dev->data->port_id);
127                 return -rte_errno;
128         }
129         return ret;
130 }
131
132 /**
133  * Broadcast request of stopping/starting data-path to secondary processes.
134  *
135  * @param[in] dev
136  *   Pointer to Ethernet structure.
137  * @param[in] type
138  *   Request type.
139  */
140 static void
141 mp_req_on_rxtx(struct rte_eth_dev *dev, enum mlx5_mp_req_type type)
142 {
143         struct rte_mp_msg mp_req;
144         struct rte_mp_msg *mp_res;
145         struct rte_mp_reply mp_rep;
146         struct mlx5_mp_param *res;
147         struct timespec ts = {.tv_sec = MLX5_MP_REQ_TIMEOUT_SEC, .tv_nsec = 0};
148         struct mlx5_priv *priv = dev->data->dev_private;
149         int ret;
150         int i;
151
152         MLX5_ASSERT(rte_eal_process_type() == RTE_PROC_PRIMARY);
153         if (!mlx5_shared_data->secondary_cnt)
154                 return;
155         if (type != MLX5_MP_REQ_START_RXTX && type != MLX5_MP_REQ_STOP_RXTX) {
156                 DRV_LOG(ERR, "port %u unknown request (req_type %d)",
157                         dev->data->port_id, type);
158                 return;
159         }
160         mp_init_msg(&priv->mp_id, &mp_req, type);
161         ret = rte_mp_request_sync(&mp_req, &mp_rep, &ts);
162         if (ret) {
163                 if (rte_errno != ENOTSUP)
164                         DRV_LOG(ERR, "port %u failed to request stop/start Rx/Tx (%d)",
165                                 dev->data->port_id, type);
166                 goto exit;
167         }
168         if (mp_rep.nb_sent != mp_rep.nb_received) {
169                 DRV_LOG(ERR,
170                         "port %u not all secondaries responded (req_type %d)",
171                         dev->data->port_id, type);
172                 goto exit;
173         }
174         for (i = 0; i < mp_rep.nb_received; i++) {
175                 mp_res = &mp_rep.msgs[i];
176                 res = (struct mlx5_mp_param *)mp_res->param;
177                 if (res->result) {
178                         DRV_LOG(ERR, "port %u request failed on secondary #%d",
179                                 dev->data->port_id, i);
180                         goto exit;
181                 }
182         }
183 exit:
184         free(mp_rep.msgs);
185 }
186
187 /**
188  * Broadcast request of starting data-path to secondary processes. The request
189  * is synchronous.
190  *
191  * @param[in] dev
192  *   Pointer to Ethernet structure.
193  */
194 void
195 mlx5_mp_req_start_rxtx(struct rte_eth_dev *dev)
196 {
197         mp_req_on_rxtx(dev, MLX5_MP_REQ_START_RXTX);
198 }
199
200 /**
201  * Broadcast request of stopping data-path to secondary processes. The request
202  * is synchronous.
203  *
204  * @param[in] dev
205  *   Pointer to Ethernet structure.
206  */
207 void
208 mlx5_mp_req_stop_rxtx(struct rte_eth_dev *dev)
209 {
210         mp_req_on_rxtx(dev, MLX5_MP_REQ_STOP_RXTX);
211 }