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