net/mlx: fix build with clang 9
[dpdk.git] / drivers / net / mlx4 / mlx4_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 <assert.h>
7 #include <stdio.h>
8 #include <time.h>
9
10 #include <rte_eal.h>
11 #include <rte_ethdev_driver.h>
12 #include <rte_string_fns.h>
13
14 #include "mlx4.h"
15 #include "mlx4_rxtx.h"
16 #include "mlx4_utils.h"
17
18 /**
19  * Initialize IPC message.
20  *
21  * @param[in] dev
22  *   Pointer to Ethernet structure.
23  * @param[out] msg
24  *   Pointer to message to fill in.
25  * @param[in] type
26  *   Message type.
27  */
28 static inline void
29 mp_init_msg(struct rte_eth_dev *dev, struct rte_mp_msg *msg,
30             enum mlx4_mp_req_type type)
31 {
32         struct mlx4_mp_param *param = (struct mlx4_mp_param *)msg->param;
33
34         memset(msg, 0, sizeof(*msg));
35         strlcpy(msg->name, MLX4_MP_NAME, sizeof(msg->name));
36         msg->len_param = sizeof(*param);
37         param->type = type;
38         param->port_id = dev->data->port_id;
39 }
40
41 /**
42  * IPC message handler of primary process.
43  *
44  * @param[in] dev
45  *   Pointer to Ethernet structure.
46  * @param[in] peer
47  *   Pointer to the peer socket path.
48  *
49  * @return
50  *   0 on success, negative errno value otherwise and rte_errno is set.
51  */
52 static int
53 mp_primary_handle(const struct rte_mp_msg *mp_msg, const void *peer)
54 {
55         struct rte_mp_msg mp_res;
56         struct mlx4_mp_param *res = (struct mlx4_mp_param *)mp_res.param;
57         const struct mlx4_mp_param *param =
58                 (const struct mlx4_mp_param *)mp_msg->param;
59         struct rte_eth_dev *dev;
60         struct mlx4_priv *priv;
61         struct mlx4_mr_cache entry;
62         uint32_t lkey;
63         int ret;
64
65         assert(rte_eal_process_type() == RTE_PROC_PRIMARY);
66         if (!rte_eth_dev_is_valid_port(param->port_id)) {
67                 rte_errno = ENODEV;
68                 ERROR("port %u invalid port ID", param->port_id);
69                 return -rte_errno;
70         }
71         dev = &rte_eth_devices[param->port_id];
72         priv = dev->data->dev_private;
73         switch (param->type) {
74         case MLX4_MP_REQ_CREATE_MR:
75                 mp_init_msg(dev, &mp_res, param->type);
76                 lkey = mlx4_mr_create_primary(dev, &entry, param->args.addr);
77                 if (lkey == UINT32_MAX)
78                         res->result = -rte_errno;
79                 ret = rte_mp_reply(&mp_res, peer);
80                 break;
81         case MLX4_MP_REQ_VERBS_CMD_FD:
82                 mp_init_msg(dev, &mp_res, param->type);
83                 mp_res.num_fds = 1;
84                 mp_res.fds[0] = priv->ctx->cmd_fd;
85                 res->result = 0;
86                 ret = rte_mp_reply(&mp_res, peer);
87                 break;
88         default:
89                 rte_errno = EINVAL;
90                 ERROR("port %u invalid mp request type", dev->data->port_id);
91                 return -rte_errno;
92         }
93         return ret;
94 }
95
96 /**
97  * IPC message handler of a secondary process.
98  *
99  * @param[in] dev
100  *   Pointer to Ethernet structure.
101  * @param[in] peer
102  *   Pointer to the peer socket path.
103  *
104  * @return
105  *   0 on success, a negative errno value otherwise and rte_errno is set.
106  */
107 static int
108 mp_secondary_handle(const struct rte_mp_msg *mp_msg, const void *peer)
109 {
110         struct rte_mp_msg mp_res;
111         struct mlx4_mp_param *res = (struct mlx4_mp_param *)mp_res.param;
112         const struct mlx4_mp_param *param =
113                 (const struct mlx4_mp_param *)mp_msg->param;
114         struct rte_eth_dev *dev;
115         int ret;
116
117         assert(rte_eal_process_type() == RTE_PROC_SECONDARY);
118         if (!rte_eth_dev_is_valid_port(param->port_id)) {
119                 rte_errno = ENODEV;
120                 ERROR("port %u invalid port ID", param->port_id);
121                 return -rte_errno;
122         }
123         dev = &rte_eth_devices[param->port_id];
124         switch (param->type) {
125         case MLX4_MP_REQ_START_RXTX:
126                 INFO("port %u starting datapath", dev->data->port_id);
127                 rte_mb();
128                 dev->tx_pkt_burst = mlx4_tx_burst;
129                 dev->rx_pkt_burst = mlx4_rx_burst;
130                 mp_init_msg(dev, &mp_res, param->type);
131                 res->result = 0;
132                 ret = rte_mp_reply(&mp_res, peer);
133                 break;
134         case MLX4_MP_REQ_STOP_RXTX:
135                 INFO("port %u stopping datapath", dev->data->port_id);
136                 dev->tx_pkt_burst = mlx4_tx_burst_removed;
137                 dev->rx_pkt_burst = mlx4_rx_burst_removed;
138                 rte_mb();
139                 mp_init_msg(dev, &mp_res, param->type);
140                 res->result = 0;
141                 ret = rte_mp_reply(&mp_res, peer);
142                 break;
143         default:
144                 rte_errno = EINVAL;
145                 ERROR("port %u invalid mp request type", dev->data->port_id);
146                 return -rte_errno;
147         }
148         return ret;
149 }
150
151 /**
152  * Broadcast request of stopping/starting data-path to secondary processes.
153  *
154  * @param[in] dev
155  *   Pointer to Ethernet structure.
156  * @param[in] type
157  *   Request type.
158  */
159 static void
160 mp_req_on_rxtx(struct rte_eth_dev *dev, enum mlx4_mp_req_type type)
161 {
162         struct rte_mp_msg mp_req;
163         struct rte_mp_msg *mp_res;
164         struct rte_mp_reply mp_rep;
165         struct mlx4_mp_param *res __rte_unused;
166         struct timespec ts = {.tv_sec = MLX4_MP_REQ_TIMEOUT_SEC, .tv_nsec = 0};
167         int ret;
168         int i;
169
170         assert(rte_eal_process_type() == RTE_PROC_PRIMARY);
171         if (!mlx4_shared_data->secondary_cnt)
172                 return;
173         if (type != MLX4_MP_REQ_START_RXTX && type != MLX4_MP_REQ_STOP_RXTX) {
174                 ERROR("port %u unknown request (req_type %d)",
175                       dev->data->port_id, type);
176                 return;
177         }
178         mp_init_msg(dev, &mp_req, type);
179         ret = rte_mp_request_sync(&mp_req, &mp_rep, &ts);
180         if (ret) {
181                 if (rte_errno != ENOTSUP)
182                         ERROR("port %u failed to request stop/start Rx/Tx (%d)",
183                                         dev->data->port_id, type);
184                 goto exit;
185         }
186         if (mp_rep.nb_sent != mp_rep.nb_received) {
187                 ERROR("port %u not all secondaries responded (req_type %d)",
188                       dev->data->port_id, type);
189                 goto exit;
190         }
191         for (i = 0; i < mp_rep.nb_received; i++) {
192                 mp_res = &mp_rep.msgs[i];
193                 res = (struct mlx4_mp_param *)mp_res->param;
194                 if (res->result) {
195                         ERROR("port %u request failed on secondary #%d",
196                               dev->data->port_id, i);
197                         goto exit;
198                 }
199         }
200 exit:
201         free(mp_rep.msgs);
202 }
203
204 /**
205  * Broadcast request of starting data-path to secondary processes. The request
206  * is synchronous.
207  *
208  * @param[in] dev
209  *   Pointer to Ethernet structure.
210  */
211 void
212 mlx4_mp_req_start_rxtx(struct rte_eth_dev *dev)
213 {
214         mp_req_on_rxtx(dev, MLX4_MP_REQ_START_RXTX);
215 }
216
217 /**
218  * Broadcast request of stopping data-path to secondary processes. The request
219  * is synchronous.
220  *
221  * @param[in] dev
222  *   Pointer to Ethernet structure.
223  */
224 void
225 mlx4_mp_req_stop_rxtx(struct rte_eth_dev *dev)
226 {
227         mp_req_on_rxtx(dev, MLX4_MP_REQ_STOP_RXTX);
228 }
229
230 /**
231  * Request Memory Region creation to the primary process.
232  *
233  * @param[in] dev
234  *   Pointer to Ethernet structure.
235  * @param addr
236  *   Target virtual address to register.
237  *
238  * @return
239  *   0 on success, a negative errno value otherwise and rte_errno is set.
240  */
241 int
242 mlx4_mp_req_mr_create(struct rte_eth_dev *dev, uintptr_t addr)
243 {
244         struct rte_mp_msg mp_req;
245         struct rte_mp_msg *mp_res;
246         struct rte_mp_reply mp_rep;
247         struct mlx4_mp_param *req = (struct mlx4_mp_param *)mp_req.param;
248         struct mlx4_mp_param *res;
249         struct timespec ts = {.tv_sec = MLX4_MP_REQ_TIMEOUT_SEC, .tv_nsec = 0};
250         int ret;
251
252         assert(rte_eal_process_type() == RTE_PROC_SECONDARY);
253         mp_init_msg(dev, &mp_req, MLX4_MP_REQ_CREATE_MR);
254         req->args.addr = addr;
255         ret = rte_mp_request_sync(&mp_req, &mp_rep, &ts);
256         if (ret) {
257                 ERROR("port %u request to primary process failed",
258                       dev->data->port_id);
259                 return -rte_errno;
260         }
261         assert(mp_rep.nb_received == 1);
262         mp_res = &mp_rep.msgs[0];
263         res = (struct mlx4_mp_param *)mp_res->param;
264         ret = res->result;
265         if (ret)
266                 rte_errno = -ret;
267         free(mp_rep.msgs);
268         return ret;
269 }
270
271 /**
272  * IPC message handler of primary process.
273  *
274  * @param[in] dev
275  *   Pointer to Ethernet structure.
276  *
277  * @return
278  *   fd on success, a negative errno value otherwise and rte_errno is set.
279  */
280 int
281 mlx4_mp_req_verbs_cmd_fd(struct rte_eth_dev *dev)
282 {
283         struct rte_mp_msg mp_req;
284         struct rte_mp_msg *mp_res;
285         struct rte_mp_reply mp_rep;
286         struct mlx4_mp_param *res;
287         struct timespec ts = {.tv_sec = MLX4_MP_REQ_TIMEOUT_SEC, .tv_nsec = 0};
288         int ret;
289
290         assert(rte_eal_process_type() == RTE_PROC_SECONDARY);
291         mp_init_msg(dev, &mp_req, MLX4_MP_REQ_VERBS_CMD_FD);
292         ret = rte_mp_request_sync(&mp_req, &mp_rep, &ts);
293         if (ret) {
294                 ERROR("port %u request to primary process failed",
295                       dev->data->port_id);
296                 return -rte_errno;
297         }
298         assert(mp_rep.nb_received == 1);
299         mp_res = &mp_rep.msgs[0];
300         res = (struct mlx4_mp_param *)mp_res->param;
301         if (res->result) {
302                 rte_errno = -res->result;
303                 ERROR("port %u failed to get command FD from primary process",
304                       dev->data->port_id);
305                 ret = -rte_errno;
306                 goto exit;
307         }
308         assert(mp_res->num_fds == 1);
309         ret = mp_res->fds[0];
310         DEBUG("port %u command FD from primary is %d",
311               dev->data->port_id, ret);
312 exit:
313         free(mp_rep.msgs);
314         return ret;
315 }
316
317 /**
318  * Initialize by primary process.
319  */
320 int
321 mlx4_mp_init_primary(void)
322 {
323         int ret;
324
325         assert(rte_eal_process_type() == RTE_PROC_PRIMARY);
326
327         /* primary is allowed to not support IPC */
328         ret = rte_mp_action_register(MLX4_MP_NAME, mp_primary_handle);
329         if (ret && rte_errno != ENOTSUP)
330                 return -1;
331         return 0;
332 }
333
334 /**
335  * Un-initialize by primary process.
336  */
337 void
338 mlx4_mp_uninit_primary(void)
339 {
340         assert(rte_eal_process_type() == RTE_PROC_PRIMARY);
341         rte_mp_action_unregister(MLX4_MP_NAME);
342 }
343
344 /**
345  * Initialize by secondary process.
346  */
347 int
348 mlx4_mp_init_secondary(void)
349 {
350         assert(rte_eal_process_type() == RTE_PROC_SECONDARY);
351         return rte_mp_action_register(MLX4_MP_NAME, mp_secondary_handle);
352 }
353
354 /**
355  * Un-initialize by secondary process.
356  */
357 void
358 mlx4_mp_uninit_secondary(void)
359 {
360         assert(rte_eal_process_type() == RTE_PROC_SECONDARY);
361         rte_mp_action_unregister(MLX4_MP_NAME);
362 }