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