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