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