7f341938cf2800d9e2ca7180d87c3667aa317cc9
[dpdk.git] / drivers / vdpa / mlx5 / mlx5_vdpa.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2019 Mellanox Technologies, Ltd
3  */
4 #include <unistd.h>
5 #include <net/if.h>
6 #include <sys/socket.h>
7 #include <sys/ioctl.h>
8 #include <fcntl.h>
9 #include <netinet/in.h>
10
11 #include <rte_malloc.h>
12 #include <rte_log.h>
13 #include <rte_errno.h>
14 #include <rte_bus_pci.h>
15 #include <rte_pci.h>
16 #include <rte_string_fns.h>
17
18 #include <mlx5_glue.h>
19 #include <mlx5_common.h>
20 #include <mlx5_devx_cmds.h>
21 #include <mlx5_prm.h>
22 #include <mlx5_nl.h>
23
24 #include "mlx5_vdpa_utils.h"
25 #include "mlx5_vdpa.h"
26
27
28 #define MLX5_VDPA_DEFAULT_FEATURES ((1ULL << VHOST_USER_F_PROTOCOL_FEATURES) | \
29                             (1ULL << VIRTIO_F_ANY_LAYOUT) | \
30                             (1ULL << VIRTIO_NET_F_MQ) | \
31                             (1ULL << VIRTIO_NET_F_GUEST_ANNOUNCE) | \
32                             (1ULL << VIRTIO_F_ORDER_PLATFORM) | \
33                             (1ULL << VHOST_F_LOG_ALL) | \
34                             (1ULL << VIRTIO_NET_F_MTU))
35
36 #define MLX5_VDPA_PROTOCOL_FEATURES \
37                             ((1ULL << VHOST_USER_PROTOCOL_F_SLAVE_REQ) | \
38                              (1ULL << VHOST_USER_PROTOCOL_F_SLAVE_SEND_FD) | \
39                              (1ULL << VHOST_USER_PROTOCOL_F_HOST_NOTIFIER) | \
40                              (1ULL << VHOST_USER_PROTOCOL_F_LOG_SHMFD) | \
41                              (1ULL << VHOST_USER_PROTOCOL_F_MQ) | \
42                              (1ULL << VHOST_USER_PROTOCOL_F_NET_MTU))
43
44 #define MLX5_VDPA_MAX_RETRIES 20
45 #define MLX5_VDPA_USEC 1000
46 #define MLX5_VDPA_DEFAULT_NO_TRAFFIC_TIME_S 2LLU
47
48 TAILQ_HEAD(mlx5_vdpa_privs, mlx5_vdpa_priv) priv_list =
49                                               TAILQ_HEAD_INITIALIZER(priv_list);
50 static pthread_mutex_t priv_list_lock = PTHREAD_MUTEX_INITIALIZER;
51 int mlx5_vdpa_logtype;
52
53 static struct mlx5_vdpa_priv *
54 mlx5_vdpa_find_priv_resource_by_vdev(struct rte_vdpa_device *vdev)
55 {
56         struct mlx5_vdpa_priv *priv;
57         int found = 0;
58
59         pthread_mutex_lock(&priv_list_lock);
60         TAILQ_FOREACH(priv, &priv_list, next) {
61                 if (vdev == priv->vdev) {
62                         found = 1;
63                         break;
64                 }
65         }
66         pthread_mutex_unlock(&priv_list_lock);
67         if (!found) {
68                 DRV_LOG(ERR, "Invalid vDPA device: %s.", vdev->device->name);
69                 rte_errno = EINVAL;
70                 return NULL;
71         }
72         return priv;
73 }
74
75 static int
76 mlx5_vdpa_get_queue_num(struct rte_vdpa_device *vdev, uint32_t *queue_num)
77 {
78         struct mlx5_vdpa_priv *priv =
79                 mlx5_vdpa_find_priv_resource_by_vdev(vdev);
80
81         if (priv == NULL) {
82                 DRV_LOG(ERR, "Invalid vDPA device: %s.", vdev->device->name);
83                 return -1;
84         }
85         *queue_num = priv->caps.max_num_virtio_queues;
86         return 0;
87 }
88
89 static int
90 mlx5_vdpa_get_vdpa_features(struct rte_vdpa_device *vdev, uint64_t *features)
91 {
92         struct mlx5_vdpa_priv *priv =
93                 mlx5_vdpa_find_priv_resource_by_vdev(vdev);
94
95         if (priv == NULL) {
96                 DRV_LOG(ERR, "Invalid vDPA device: %s.", vdev->device->name);
97                 return -1;
98         }
99         *features = MLX5_VDPA_DEFAULT_FEATURES;
100         if (priv->caps.virtio_queue_type & (1 << MLX5_VIRTQ_TYPE_PACKED))
101                 *features |= (1ULL << VIRTIO_F_RING_PACKED);
102         if (priv->caps.tso_ipv4)
103                 *features |= (1ULL << VIRTIO_NET_F_HOST_TSO4);
104         if (priv->caps.tso_ipv6)
105                 *features |= (1ULL << VIRTIO_NET_F_HOST_TSO6);
106         if (priv->caps.tx_csum)
107                 *features |= (1ULL << VIRTIO_NET_F_CSUM);
108         if (priv->caps.rx_csum)
109                 *features |= (1ULL << VIRTIO_NET_F_GUEST_CSUM);
110         if (priv->caps.virtio_version_1_0)
111                 *features |= (1ULL << VIRTIO_F_VERSION_1);
112         return 0;
113 }
114
115 static int
116 mlx5_vdpa_get_protocol_features(struct rte_vdpa_device *vdev,
117                 uint64_t *features)
118 {
119         struct mlx5_vdpa_priv *priv =
120                 mlx5_vdpa_find_priv_resource_by_vdev(vdev);
121
122         if (priv == NULL) {
123                 DRV_LOG(ERR, "Invalid vDPA device: %s.", vdev->device->name);
124                 return -1;
125         }
126         *features = MLX5_VDPA_PROTOCOL_FEATURES;
127         return 0;
128 }
129
130 static int
131 mlx5_vdpa_set_vring_state(int vid, int vring, int state)
132 {
133         struct rte_vdpa_device *vdev = rte_vhost_get_vdpa_device(vid);
134         struct mlx5_vdpa_priv *priv =
135                 mlx5_vdpa_find_priv_resource_by_vdev(vdev);
136
137         if (priv == NULL) {
138                 DRV_LOG(ERR, "Invalid vDPA device: %s.", vdev->device->name);
139                 return -EINVAL;
140         }
141         if (vring >= (int)priv->caps.max_num_virtio_queues * 2) {
142                 DRV_LOG(ERR, "Too big vring id: %d.", vring);
143                 return -E2BIG;
144         }
145         return mlx5_vdpa_virtq_enable(priv, vring, state);
146 }
147
148 static int
149 mlx5_vdpa_direct_db_prepare(struct mlx5_vdpa_priv *priv)
150 {
151         int ret;
152
153         if (priv->direct_notifier) {
154                 ret = rte_vhost_host_notifier_ctrl(priv->vid, false);
155                 if (ret != 0) {
156                         DRV_LOG(INFO, "Direct HW notifier FD cannot be "
157                                 "destroyed for device %d: %d.", priv->vid, ret);
158                         return -1;
159                 }
160                 priv->direct_notifier = 0;
161         }
162         ret = rte_vhost_host_notifier_ctrl(priv->vid, true);
163         if (ret != 0)
164                 DRV_LOG(INFO, "Direct HW notifier FD cannot be configured for"
165                         " device %d: %d.", priv->vid, ret);
166         else
167                 priv->direct_notifier = 1;
168         return 0;
169 }
170
171 static int
172 mlx5_vdpa_features_set(int vid)
173 {
174         struct rte_vdpa_device *vdev = rte_vhost_get_vdpa_device(vid);
175         struct mlx5_vdpa_priv *priv =
176                 mlx5_vdpa_find_priv_resource_by_vdev(vdev);
177         uint64_t log_base, log_size;
178         uint64_t features;
179         int ret;
180
181         if (priv == NULL) {
182                 DRV_LOG(ERR, "Invalid vDPA device: %s.", vdev->device->name);
183                 return -EINVAL;
184         }
185         ret = rte_vhost_get_negotiated_features(vid, &features);
186         if (ret) {
187                 DRV_LOG(ERR, "Failed to get negotiated features.");
188                 return ret;
189         }
190         if (RTE_VHOST_NEED_LOG(features)) {
191                 ret = rte_vhost_get_log_base(vid, &log_base, &log_size);
192                 if (ret) {
193                         DRV_LOG(ERR, "Failed to get log base.");
194                         return ret;
195                 }
196                 ret = mlx5_vdpa_dirty_bitmap_set(priv, log_base, log_size);
197                 if (ret) {
198                         DRV_LOG(ERR, "Failed to set dirty bitmap.");
199                         return ret;
200                 }
201                 DRV_LOG(INFO, "mlx5 vdpa: enabling dirty logging...");
202                 ret = mlx5_vdpa_logging_enable(priv, 1);
203                 if (ret) {
204                         DRV_LOG(ERR, "Failed t enable dirty logging.");
205                         return ret;
206                 }
207         }
208         return 0;
209 }
210
211 static int
212 mlx5_vdpa_pd_create(struct mlx5_vdpa_priv *priv)
213 {
214 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
215         priv->pd = mlx5_glue->alloc_pd(priv->ctx);
216         if (priv->pd == NULL) {
217                 DRV_LOG(ERR, "Failed to allocate PD.");
218                 return errno ? -errno : -ENOMEM;
219         }
220         struct mlx5dv_obj obj;
221         struct mlx5dv_pd pd_info;
222         int ret = 0;
223
224         obj.pd.in = priv->pd;
225         obj.pd.out = &pd_info;
226         ret = mlx5_glue->dv_init_obj(&obj, MLX5DV_OBJ_PD);
227         if (ret) {
228                 DRV_LOG(ERR, "Fail to get PD object info.");
229                 mlx5_glue->dealloc_pd(priv->pd);
230                 priv->pd = NULL;
231                 return -errno;
232         }
233         priv->pdn = pd_info.pdn;
234         return 0;
235 #else
236         (void)priv;
237         DRV_LOG(ERR, "Cannot get pdn - no DV support.");
238         return -ENOTSUP;
239 #endif /* HAVE_IBV_FLOW_DV_SUPPORT */
240 }
241
242 static int
243 mlx5_vdpa_mtu_set(struct mlx5_vdpa_priv *priv)
244 {
245         struct ifreq request;
246         uint16_t vhost_mtu = 0;
247         uint16_t kern_mtu = 0;
248         int ret = rte_vhost_get_mtu(priv->vid, &vhost_mtu);
249         int sock;
250         int retries = MLX5_VDPA_MAX_RETRIES;
251
252         if (ret) {
253                 DRV_LOG(DEBUG, "Cannot get vhost MTU - %d.", ret);
254                 return ret;
255         }
256         if (!vhost_mtu) {
257                 DRV_LOG(DEBUG, "Vhost MTU is 0.");
258                 return ret;
259         }
260         ret = mlx5_get_ifname_sysfs(priv->ctx->device->ibdev_path,
261                                     request.ifr_name);
262         if (ret) {
263                 DRV_LOG(DEBUG, "Cannot get kernel IF name - %d.", ret);
264                 return ret;
265         }
266         sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
267         if (sock == -1) {
268                 DRV_LOG(DEBUG, "Cannot open IF socket.");
269                 return sock;
270         }
271         while (retries--) {
272                 ret = ioctl(sock, SIOCGIFMTU, &request);
273                 if (ret == -1)
274                         break;
275                 kern_mtu = request.ifr_mtu;
276                 DRV_LOG(DEBUG, "MTU: current %d requested %d.", (int)kern_mtu,
277                         (int)vhost_mtu);
278                 if (kern_mtu == vhost_mtu)
279                         break;
280                 request.ifr_mtu = vhost_mtu;
281                 ret = ioctl(sock, SIOCSIFMTU, &request);
282                 if (ret == -1)
283                         break;
284                 request.ifr_mtu = 0;
285                 usleep(MLX5_VDPA_USEC);
286         }
287         close(sock);
288         return kern_mtu == vhost_mtu ? 0 : -1;
289 }
290
291 static int
292 mlx5_vdpa_dev_close(int vid)
293 {
294         struct rte_vdpa_device *vdev = rte_vhost_get_vdpa_device(vid);
295         struct mlx5_vdpa_priv *priv =
296                 mlx5_vdpa_find_priv_resource_by_vdev(vdev);
297         int ret = 0;
298
299         if (priv == NULL) {
300                 DRV_LOG(ERR, "Invalid vDPA device: %s.", vdev->device->name);
301                 return -1;
302         }
303         if (priv->configured)
304                 ret |= mlx5_vdpa_lm_log(priv);
305         mlx5_vdpa_cqe_event_unset(priv);
306         mlx5_vdpa_steer_unset(priv);
307         mlx5_vdpa_virtqs_release(priv);
308         mlx5_vdpa_event_qp_global_release(priv);
309         mlx5_vdpa_mem_dereg(priv);
310         if (priv->pd) {
311                 claim_zero(mlx5_glue->dealloc_pd(priv->pd));
312                 priv->pd = NULL;
313         }
314         priv->configured = 0;
315         priv->vid = 0;
316         DRV_LOG(INFO, "vDPA device %d was closed.", vid);
317         return ret;
318 }
319
320 static int
321 mlx5_vdpa_dev_config(int vid)
322 {
323         struct rte_vdpa_device *vdev = rte_vhost_get_vdpa_device(vid);
324         struct mlx5_vdpa_priv *priv =
325                 mlx5_vdpa_find_priv_resource_by_vdev(vdev);
326
327         if (priv == NULL) {
328                 DRV_LOG(ERR, "Invalid vDPA device: %s.", vdev->device->name);
329                 return -EINVAL;
330         }
331         if (priv->configured && mlx5_vdpa_dev_close(vid)) {
332                 DRV_LOG(ERR, "Failed to reconfigure vid %d.", vid);
333                 return -1;
334         }
335         priv->vid = vid;
336         if (mlx5_vdpa_mtu_set(priv))
337                 DRV_LOG(WARNING, "MTU cannot be set on device %s.",
338                                 vdev->device->name);
339         if (mlx5_vdpa_pd_create(priv) || mlx5_vdpa_mem_register(priv) ||
340             mlx5_vdpa_direct_db_prepare(priv) ||
341             mlx5_vdpa_virtqs_prepare(priv) || mlx5_vdpa_steer_setup(priv) ||
342             mlx5_vdpa_cqe_event_setup(priv)) {
343                 mlx5_vdpa_dev_close(vid);
344                 return -1;
345         }
346         priv->configured = 1;
347         DRV_LOG(INFO, "vDPA device %d was configured.", vid);
348         return 0;
349 }
350
351 static int
352 mlx5_vdpa_get_device_fd(int vid)
353 {
354         struct rte_vdpa_device *vdev = rte_vhost_get_vdpa_device(vid);
355         struct mlx5_vdpa_priv *priv =
356                 mlx5_vdpa_find_priv_resource_by_vdev(vdev);
357
358         if (priv == NULL) {
359                 DRV_LOG(ERR, "Invalid vDPA device: %s.", vdev->device->name);
360                 return -EINVAL;
361         }
362         return priv->ctx->cmd_fd;
363 }
364
365 static int
366 mlx5_vdpa_get_notify_area(int vid, int qid, uint64_t *offset, uint64_t *size)
367 {
368         struct rte_vdpa_device *vdev = rte_vhost_get_vdpa_device(vid);
369         struct mlx5_vdpa_priv *priv =
370                 mlx5_vdpa_find_priv_resource_by_vdev(vdev);
371
372         RTE_SET_USED(qid);
373         if (priv == NULL) {
374                 DRV_LOG(ERR, "Invalid vDPA device: %s.", vdev->device->name);
375                 return -EINVAL;
376         }
377         if (!priv->var) {
378                 DRV_LOG(ERR, "VAR was not created for device %s, is the device"
379                         " configured?.", vdev->device->name);
380                 return -EINVAL;
381         }
382         *offset = priv->var->mmap_off;
383         *size = priv->var->length;
384         return 0;
385 }
386
387 static int
388 mlx5_vdpa_get_stats_names(struct rte_vdpa_device *vdev,
389                 struct rte_vdpa_stat_name *stats_names,
390                 unsigned int size)
391 {
392         static const char *mlx5_vdpa_stats_names[MLX5_VDPA_STATS_MAX] = {
393                 "received_descriptors",
394                 "completed_descriptors",
395                 "bad descriptor errors",
396                 "exceed max chain",
397                 "invalid buffer",
398                 "completion errors",
399         };
400         struct mlx5_vdpa_priv *priv =
401                 mlx5_vdpa_find_priv_resource_by_vdev(vdev);
402         unsigned int i;
403
404         if (priv == NULL) {
405                 DRV_LOG(ERR, "Invalid device: %s.", vdev->device->name);
406                 return -ENODEV;
407         }
408         if (!stats_names)
409                 return MLX5_VDPA_STATS_MAX;
410         size = RTE_MIN(size, (unsigned int)MLX5_VDPA_STATS_MAX);
411         for (i = 0; i < size; ++i)
412                 strlcpy(stats_names[i].name, mlx5_vdpa_stats_names[i],
413                         RTE_VDPA_STATS_NAME_SIZE);
414         return size;
415 }
416
417 static int
418 mlx5_vdpa_get_stats(struct rte_vdpa_device *vdev, int qid,
419                 struct rte_vdpa_stat *stats, unsigned int n)
420 {
421         struct mlx5_vdpa_priv *priv =
422                 mlx5_vdpa_find_priv_resource_by_vdev(vdev);
423
424         if (priv == NULL) {
425                 DRV_LOG(ERR, "Invalid device: %s.", vdev->device->name);
426                 return -ENODEV;
427         }
428         if (!priv->configured) {
429                 DRV_LOG(ERR, "Device %s was not configured.",
430                                 vdev->device->name);
431                 return -ENODATA;
432         }
433         if (qid >= (int)priv->nr_virtqs) {
434                 DRV_LOG(ERR, "Too big vring id: %d for device %s.", qid,
435                                 vdev->device->name);
436                 return -E2BIG;
437         }
438         if (!priv->caps.queue_counters_valid) {
439                 DRV_LOG(ERR, "Virtq statistics is not supported for device %s.",
440                         vdev->device->name);
441                 return -ENOTSUP;
442         }
443         return mlx5_vdpa_virtq_stats_get(priv, qid, stats, n);
444 }
445
446 static int
447 mlx5_vdpa_reset_stats(struct rte_vdpa_device *vdev, int qid)
448 {
449         struct mlx5_vdpa_priv *priv =
450                 mlx5_vdpa_find_priv_resource_by_vdev(vdev);
451
452         if (priv == NULL) {
453                 DRV_LOG(ERR, "Invalid device: %s.", vdev->device->name);
454                 return -ENODEV;
455         }
456         if (!priv->configured) {
457                 DRV_LOG(ERR, "Device %s was not configured.",
458                                 vdev->device->name);
459                 return -ENODATA;
460         }
461         if (qid >= (int)priv->nr_virtqs) {
462                 DRV_LOG(ERR, "Too big vring id: %d for device %s.", qid,
463                                 vdev->device->name);
464                 return -E2BIG;
465         }
466         if (!priv->caps.queue_counters_valid) {
467                 DRV_LOG(ERR, "Virtq statistics is not supported for device %s.",
468                         vdev->device->name);
469                 return -ENOTSUP;
470         }
471         return mlx5_vdpa_virtq_stats_reset(priv, qid);
472 }
473
474 static struct rte_vdpa_dev_ops mlx5_vdpa_ops = {
475         .get_queue_num = mlx5_vdpa_get_queue_num,
476         .get_features = mlx5_vdpa_get_vdpa_features,
477         .get_protocol_features = mlx5_vdpa_get_protocol_features,
478         .dev_conf = mlx5_vdpa_dev_config,
479         .dev_close = mlx5_vdpa_dev_close,
480         .set_vring_state = mlx5_vdpa_set_vring_state,
481         .set_features = mlx5_vdpa_features_set,
482         .migration_done = NULL,
483         .get_vfio_group_fd = NULL,
484         .get_vfio_device_fd = mlx5_vdpa_get_device_fd,
485         .get_notify_area = mlx5_vdpa_get_notify_area,
486         .get_stats_names = mlx5_vdpa_get_stats_names,
487         .get_stats = mlx5_vdpa_get_stats,
488         .reset_stats = mlx5_vdpa_reset_stats,
489 };
490
491 static struct ibv_device *
492 mlx5_vdpa_get_ib_device_match(struct rte_pci_addr *addr)
493 {
494         int n;
495         struct ibv_device **ibv_list = mlx5_glue->get_device_list(&n);
496         struct ibv_device *ibv_match = NULL;
497
498         if (!ibv_list) {
499                 rte_errno = ENOSYS;
500                 return NULL;
501         }
502         while (n-- > 0) {
503                 struct rte_pci_addr pci_addr;
504
505                 DRV_LOG(DEBUG, "Checking device \"%s\"..", ibv_list[n]->name);
506                 if (mlx5_dev_to_pci_addr(ibv_list[n]->ibdev_path, &pci_addr))
507                         continue;
508                 if (rte_pci_addr_cmp(addr, &pci_addr))
509                         continue;
510                 ibv_match = ibv_list[n];
511                 break;
512         }
513         if (!ibv_match)
514                 rte_errno = ENOENT;
515         mlx5_glue->free_device_list(ibv_list);
516         return ibv_match;
517 }
518
519 /* Try to disable ROCE by Netlink\Devlink. */
520 static int
521 mlx5_vdpa_nl_roce_disable(const char *addr)
522 {
523         int nlsk_fd = mlx5_nl_init(NETLINK_GENERIC);
524         int devlink_id;
525         int enable;
526         int ret;
527
528         if (nlsk_fd < 0)
529                 return nlsk_fd;
530         devlink_id = mlx5_nl_devlink_family_id_get(nlsk_fd);
531         if (devlink_id < 0) {
532                 ret = devlink_id;
533                 DRV_LOG(DEBUG, "Failed to get devlink id for ROCE operations by"
534                         " Netlink.");
535                 goto close;
536         }
537         ret = mlx5_nl_enable_roce_get(nlsk_fd, devlink_id, addr, &enable);
538         if (ret) {
539                 DRV_LOG(DEBUG, "Failed to get ROCE enable by Netlink: %d.",
540                         ret);
541                 goto close;
542         } else if (!enable) {
543                 DRV_LOG(INFO, "ROCE has already disabled(Netlink).");
544                 goto close;
545         }
546         ret = mlx5_nl_enable_roce_set(nlsk_fd, devlink_id, addr, 0);
547         if (ret)
548                 DRV_LOG(DEBUG, "Failed to disable ROCE by Netlink: %d.", ret);
549         else
550                 DRV_LOG(INFO, "ROCE is disabled by Netlink successfully.");
551 close:
552         close(nlsk_fd);
553         return ret;
554 }
555
556 /* Try to disable ROCE by sysfs. */
557 static int
558 mlx5_vdpa_sys_roce_disable(const char *addr)
559 {
560         FILE *file_o;
561         int enable;
562         int ret;
563
564         MKSTR(file_p, "/sys/bus/pci/devices/%s/roce_enable", addr);
565         file_o = fopen(file_p, "rb");
566         if (!file_o) {
567                 rte_errno = ENOTSUP;
568                 return -ENOTSUP;
569         }
570         ret = fscanf(file_o, "%d", &enable);
571         if (ret != 1) {
572                 rte_errno = EINVAL;
573                 ret = EINVAL;
574                 goto close;
575         } else if (!enable) {
576                 ret = 0;
577                 DRV_LOG(INFO, "ROCE has already disabled(sysfs).");
578                 goto close;
579         }
580         fclose(file_o);
581         file_o = fopen(file_p, "wb");
582         if (!file_o) {
583                 rte_errno = ENOTSUP;
584                 return -ENOTSUP;
585         }
586         fprintf(file_o, "0\n");
587         ret = 0;
588 close:
589         if (ret)
590                 DRV_LOG(DEBUG, "Failed to disable ROCE by sysfs: %d.", ret);
591         else
592                 DRV_LOG(INFO, "ROCE is disabled by sysfs successfully.");
593         fclose(file_o);
594         return ret;
595 }
596
597 static int
598 mlx5_vdpa_roce_disable(struct rte_pci_addr *addr, struct ibv_device **ibv)
599 {
600         char addr_name[64] = {0};
601
602         rte_pci_device_name(addr, addr_name, sizeof(addr_name));
603         /* Firstly try to disable ROCE by Netlink and fallback to sysfs. */
604         if (mlx5_vdpa_nl_roce_disable(addr_name) == 0 ||
605             mlx5_vdpa_sys_roce_disable(addr_name) == 0) {
606                 /*
607                  * Succeed to disable ROCE, wait for the IB device to appear
608                  * again after reload.
609                  */
610                 int r;
611                 struct ibv_device *ibv_new;
612
613                 for (r = MLX5_VDPA_MAX_RETRIES; r; r--) {
614                         ibv_new = mlx5_vdpa_get_ib_device_match(addr);
615                         if (ibv_new) {
616                                 *ibv = ibv_new;
617                                 return 0;
618                         }
619                         usleep(MLX5_VDPA_USEC);
620                 }
621                 DRV_LOG(ERR, "Cannot much device %s after ROCE disable, "
622                         "retries exceed %d", addr_name, MLX5_VDPA_MAX_RETRIES);
623                 rte_errno = EAGAIN;
624         }
625         return -rte_errno;
626 }
627
628 static int
629 mlx5_vdpa_args_check_handler(const char *key, const char *val, void *opaque)
630 {
631         struct mlx5_vdpa_priv *priv = opaque;
632         unsigned long tmp;
633
634         if (strcmp(key, "class") == 0)
635                 return 0;
636         errno = 0;
637         tmp = strtoul(val, NULL, 0);
638         if (errno) {
639                 DRV_LOG(WARNING, "%s: \"%s\" is an invalid integer.", key, val);
640                 return -errno;
641         }
642         if (strcmp(key, "event_mode") == 0) {
643                 if (tmp <= MLX5_VDPA_EVENT_MODE_ONLY_INTERRUPT)
644                         priv->event_mode = (int)tmp;
645                 else
646                         DRV_LOG(WARNING, "Invalid event_mode %s.", val);
647         } else if (strcmp(key, "event_us") == 0) {
648                 priv->event_us = (uint32_t)tmp;
649         } else if (strcmp(key, "no_traffic_time") == 0) {
650                 priv->no_traffic_time_s = (uint32_t)tmp;
651         } else {
652                 DRV_LOG(WARNING, "Invalid key %s.", key);
653         }
654         return 0;
655 }
656
657 static void
658 mlx5_vdpa_config_get(struct rte_devargs *devargs, struct mlx5_vdpa_priv *priv)
659 {
660         struct rte_kvargs *kvlist;
661
662         priv->event_mode = MLX5_VDPA_EVENT_MODE_DYNAMIC_TIMER;
663         priv->event_us = 0;
664         priv->no_traffic_time_s = MLX5_VDPA_DEFAULT_NO_TRAFFIC_TIME_S;
665         if (devargs == NULL)
666                 return;
667         kvlist = rte_kvargs_parse(devargs->args, NULL);
668         if (kvlist == NULL)
669                 return;
670         rte_kvargs_process(kvlist, NULL, mlx5_vdpa_args_check_handler, priv);
671         rte_kvargs_free(kvlist);
672         if (!priv->event_us) {
673                 if (priv->event_mode == MLX5_VDPA_EVENT_MODE_DYNAMIC_TIMER)
674                         priv->event_us = MLX5_VDPA_DEFAULT_TIMER_STEP_US;
675                 else if (priv->event_mode == MLX5_VDPA_EVENT_MODE_FIXED_TIMER)
676                         priv->event_us = MLX5_VDPA_DEFAULT_TIMER_DELAY_US;
677         }
678         DRV_LOG(DEBUG, "event mode is %d.", priv->event_mode);
679         DRV_LOG(DEBUG, "event_us is %u us.", priv->event_us);
680         DRV_LOG(DEBUG, "no traffic time is %u s.", priv->no_traffic_time_s);
681 }
682
683 /**
684  * DPDK callback to register a PCI device.
685  *
686  * This function spawns vdpa device out of a given PCI device.
687  *
688  * @param[in] pci_drv
689  *   PCI driver structure (mlx5_vpda_driver).
690  * @param[in] pci_dev
691  *   PCI device information.
692  *
693  * @return
694  *   0 on success, 1 to skip this driver, a negative errno value otherwise
695  *   and rte_errno is set.
696  */
697 static int
698 mlx5_vdpa_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
699                     struct rte_pci_device *pci_dev __rte_unused)
700 {
701         struct ibv_device *ibv;
702         struct mlx5_vdpa_priv *priv = NULL;
703         struct ibv_context *ctx = NULL;
704         struct mlx5_hca_attr attr;
705         int ret;
706
707         if (mlx5_class_get(pci_dev->device.devargs) != MLX5_CLASS_VDPA) {
708                 DRV_LOG(DEBUG, "Skip probing - should be probed by other mlx5"
709                         " driver.");
710                 return 1;
711         }
712         ibv = mlx5_vdpa_get_ib_device_match(&pci_dev->addr);
713         if (!ibv) {
714                 DRV_LOG(ERR, "No matching IB device for PCI slot "
715                         PCI_PRI_FMT ".", pci_dev->addr.domain,
716                         pci_dev->addr.bus, pci_dev->addr.devid,
717                         pci_dev->addr.function);
718                 return -rte_errno;
719         } else {
720                 DRV_LOG(INFO, "PCI information matches for device \"%s\".",
721                         ibv->name);
722         }
723         if (mlx5_vdpa_roce_disable(&pci_dev->addr, &ibv) != 0) {
724                 DRV_LOG(WARNING, "Failed to disable ROCE for \"%s\".",
725                         ibv->name);
726                 return -rte_errno;
727         }
728         ctx = mlx5_glue->dv_open_device(ibv);
729         if (!ctx) {
730                 DRV_LOG(ERR, "Failed to open IB device \"%s\".", ibv->name);
731                 rte_errno = ENODEV;
732                 return -rte_errno;
733         }
734         ret = mlx5_devx_cmd_query_hca_attr(ctx, &attr);
735         if (ret) {
736                 DRV_LOG(ERR, "Unable to read HCA capabilities.");
737                 rte_errno = ENOTSUP;
738                 goto error;
739         } else if (!attr.vdpa.valid || !attr.vdpa.max_num_virtio_queues) {
740                 DRV_LOG(ERR, "Not enough capabilities to support vdpa, maybe "
741                         "old FW/OFED version?");
742                 rte_errno = ENOTSUP;
743                 goto error;
744         }
745         if (!attr.vdpa.queue_counters_valid)
746                 DRV_LOG(DEBUG, "No capability to support virtq statistics.");
747         priv = rte_zmalloc("mlx5 vDPA device private", sizeof(*priv) +
748                            sizeof(struct mlx5_vdpa_virtq) *
749                            attr.vdpa.max_num_virtio_queues * 2,
750                            RTE_CACHE_LINE_SIZE);
751         if (!priv) {
752                 DRV_LOG(ERR, "Failed to allocate private memory.");
753                 rte_errno = ENOMEM;
754                 goto error;
755         }
756         priv->caps = attr.vdpa;
757         priv->log_max_rqt_size = attr.log_max_rqt_size;
758         priv->ctx = ctx;
759         priv->pci_dev = pci_dev;
760         priv->var = mlx5_glue->dv_alloc_var(ctx, 0);
761         if (!priv->var) {
762                 DRV_LOG(ERR, "Failed to allocate VAR %u.\n", errno);
763                 goto error;
764         }
765         priv->vdev = rte_vdpa_register_device(&pci_dev->device,
766                         &mlx5_vdpa_ops);
767         if (priv->vdev == NULL) {
768                 DRV_LOG(ERR, "Failed to register vDPA device.");
769                 rte_errno = rte_errno ? rte_errno : EINVAL;
770                 goto error;
771         }
772         mlx5_vdpa_config_get(pci_dev->device.devargs, priv);
773         SLIST_INIT(&priv->mr_list);
774         pthread_mutex_lock(&priv_list_lock);
775         TAILQ_INSERT_TAIL(&priv_list, priv, next);
776         pthread_mutex_unlock(&priv_list_lock);
777         return 0;
778
779 error:
780         if (priv) {
781                 if (priv->var)
782                         mlx5_glue->dv_free_var(priv->var);
783                 rte_free(priv);
784         }
785         if (ctx)
786                 mlx5_glue->close_device(ctx);
787         return -rte_errno;
788 }
789
790 /**
791  * DPDK callback to remove a PCI device.
792  *
793  * This function removes all vDPA devices belong to a given PCI device.
794  *
795  * @param[in] pci_dev
796  *   Pointer to the PCI device.
797  *
798  * @return
799  *   0 on success, the function cannot fail.
800  */
801 static int
802 mlx5_vdpa_pci_remove(struct rte_pci_device *pci_dev)
803 {
804         struct mlx5_vdpa_priv *priv = NULL;
805         int found = 0;
806
807         pthread_mutex_lock(&priv_list_lock);
808         TAILQ_FOREACH(priv, &priv_list, next) {
809                 if (!rte_pci_addr_cmp(&priv->pci_dev->addr, &pci_dev->addr)) {
810                         found = 1;
811                         break;
812                 }
813         }
814         if (found)
815                 TAILQ_REMOVE(&priv_list, priv, next);
816         pthread_mutex_unlock(&priv_list_lock);
817         if (found) {
818                 if (priv->configured)
819                         mlx5_vdpa_dev_close(priv->vid);
820                 if (priv->var) {
821                         mlx5_glue->dv_free_var(priv->var);
822                         priv->var = NULL;
823                 }
824                 mlx5_glue->close_device(priv->ctx);
825                 rte_free(priv);
826         }
827         return 0;
828 }
829
830 static const struct rte_pci_id mlx5_vdpa_pci_id_map[] = {
831         {
832                 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
833                                 PCI_DEVICE_ID_MELLANOX_CONNECTX6)
834         },
835         {
836                 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
837                                 PCI_DEVICE_ID_MELLANOX_CONNECTX6VF)
838         },
839         {
840                 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
841                                 PCI_DEVICE_ID_MELLANOX_CONNECTX6DX)
842         },
843         {
844                 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
845                                 PCI_DEVICE_ID_MELLANOX_CONNECTX6DXVF)
846         },
847         {
848                 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
849                                 PCI_DEVICE_ID_MELLANOX_CONNECTX6DXBF)
850         },
851         {
852                 .vendor_id = 0
853         }
854 };
855
856 static struct rte_pci_driver mlx5_vdpa_driver = {
857         .driver = {
858                 .name = "mlx5_vdpa",
859         },
860         .id_table = mlx5_vdpa_pci_id_map,
861         .probe = mlx5_vdpa_pci_probe,
862         .remove = mlx5_vdpa_pci_remove,
863         .drv_flags = 0,
864 };
865
866 /**
867  * Driver initialization routine.
868  */
869 RTE_INIT(rte_mlx5_vdpa_init)
870 {
871         /* Initialize common log type. */
872         mlx5_vdpa_logtype = rte_log_register("pmd.vdpa.mlx5");
873         if (mlx5_vdpa_logtype >= 0)
874                 rte_log_set_level(mlx5_vdpa_logtype, RTE_LOG_NOTICE);
875         if (mlx5_glue)
876                 rte_pci_register(&mlx5_vdpa_driver);
877 }
878
879 RTE_PMD_EXPORT_NAME(net_mlx5_vdpa, __COUNTER__);
880 RTE_PMD_REGISTER_PCI_TABLE(net_mlx5_vdpa, mlx5_vdpa_pci_id_map);
881 RTE_PMD_REGISTER_KMOD_DEP(net_mlx5_vdpa, "* ib_uverbs & mlx5_core & mlx5_ib");