5d4ac67ce0dda69fe9272912b5031426fb9b9741
[deb_dpdk.git] / drivers / bus / fslmc / fslmc_vfio.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright (c) 2015-2016 Freescale Semiconductor, Inc. All rights reserved.
5  *   Copyright (c) 2016 NXP. All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Freescale Semiconductor, Inc nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <unistd.h>
35 #include <stdio.h>
36 #include <sys/types.h>
37 #include <string.h>
38 #include <stdlib.h>
39 #include <fcntl.h>
40 #include <errno.h>
41 #include <sys/ioctl.h>
42 #include <sys/stat.h>
43 #include <sys/types.h>
44 #include <sys/mman.h>
45 #include <sys/vfs.h>
46 #include <libgen.h>
47 #include <dirent.h>
48 #include <sys/eventfd.h>
49
50 #include <rte_mbuf.h>
51 #include <rte_ethdev.h>
52 #include <rte_malloc.h>
53 #include <rte_memcpy.h>
54 #include <rte_string_fns.h>
55 #include <rte_cycles.h>
56 #include <rte_kvargs.h>
57 #include <rte_dev.h>
58 #include <rte_ethdev.h>
59 #include <rte_bus.h>
60
61 #include "rte_fslmc.h"
62 #include "fslmc_vfio.h"
63
64 #include "portal/dpaa2_hw_pvt.h"
65 #include "portal/dpaa2_hw_dpio.h"
66
67 #define VFIO_MAX_CONTAINERS     1
68
69 #define FSLMC_VFIO_LOG(level, fmt, args...) \
70         RTE_LOG(level, EAL, "%s(): " fmt "\n", __func__, ##args)
71
72 /** Pathname of FSL-MC devices directory. */
73 #define SYSFS_FSL_MC_DEVICES "/sys/bus/fsl-mc/devices"
74
75 /* Number of VFIO containers & groups with in */
76 static struct fslmc_vfio_group vfio_groups[VFIO_MAX_GRP];
77 static struct fslmc_vfio_container vfio_containers[VFIO_MAX_CONTAINERS];
78 static int container_device_fd;
79 static uint32_t *msi_intr_vaddr;
80 void *(*rte_mcp_ptr_list);
81 static uint32_t mcp_id;
82 static int is_dma_done;
83
84 static int vfio_connect_container(struct fslmc_vfio_group *vfio_group)
85 {
86         struct fslmc_vfio_container *container;
87         int i, fd, ret;
88
89         /* Try connecting to vfio container if already created */
90         for (i = 0; i < VFIO_MAX_CONTAINERS; i++) {
91                 container = &vfio_containers[i];
92                 if (!ioctl(vfio_group->fd, VFIO_GROUP_SET_CONTAINER,
93                            &container->fd)) {
94                         FSLMC_VFIO_LOG(INFO, "Container pre-exists with"
95                                     " FD[0x%x] for this group",
96                                     container->fd);
97                         vfio_group->container = container;
98                         return 0;
99                 }
100         }
101
102         /* Opens main vfio file descriptor which represents the "container" */
103         fd = vfio_get_container_fd();
104         if (fd < 0) {
105                 FSLMC_VFIO_LOG(ERR, "Failed to open VFIO container");
106                 return -errno;
107         }
108
109         /* Check whether support for SMMU type IOMMU present or not */
110         if (ioctl(fd, VFIO_CHECK_EXTENSION, VFIO_TYPE1_IOMMU)) {
111                 /* Connect group to container */
112                 ret = ioctl(vfio_group->fd, VFIO_GROUP_SET_CONTAINER, &fd);
113                 if (ret) {
114                         FSLMC_VFIO_LOG(ERR, "Failed to setup group container");
115                         close(fd);
116                         return -errno;
117                 }
118
119                 ret = ioctl(fd, VFIO_SET_IOMMU, VFIO_TYPE1_IOMMU);
120                 if (ret) {
121                         FSLMC_VFIO_LOG(ERR, "Failed to setup VFIO iommu");
122                         close(fd);
123                         return -errno;
124                 }
125         } else {
126                 FSLMC_VFIO_LOG(ERR, "No supported IOMMU available");
127                 close(fd);
128                 return -EINVAL;
129         }
130
131         container = NULL;
132         for (i = 0; i < VFIO_MAX_CONTAINERS; i++) {
133                 if (vfio_containers[i].used)
134                         continue;
135                 FSLMC_VFIO_LOG(DEBUG, "Unused container at index %d", i);
136                 container = &vfio_containers[i];
137         }
138         if (!container) {
139                 FSLMC_VFIO_LOG(ERR, "No free container found");
140                 close(fd);
141                 return -ENOMEM;
142         }
143
144         container->used = 1;
145         container->fd = fd;
146         container->group_list[container->index] = vfio_group;
147         vfio_group->container = container;
148         container->index++;
149         return 0;
150 }
151
152 static int vfio_map_irq_region(struct fslmc_vfio_group *group)
153 {
154         int ret;
155         unsigned long *vaddr = NULL;
156         struct vfio_iommu_type1_dma_map map = {
157                 .argsz = sizeof(map),
158                 .flags = VFIO_DMA_MAP_FLAG_READ | VFIO_DMA_MAP_FLAG_WRITE,
159                 .vaddr = 0x6030000,
160                 .iova = 0x6030000,
161                 .size = 0x1000,
162         };
163
164         vaddr = (unsigned long *)mmap(NULL, 0x1000, PROT_WRITE |
165                 PROT_READ, MAP_SHARED, container_device_fd, 0x6030000);
166         if (vaddr == MAP_FAILED) {
167                 FSLMC_VFIO_LOG(ERR, "Unable to map region (errno = %d)", errno);
168                 return -errno;
169         }
170
171         msi_intr_vaddr = (uint32_t *)((char *)(vaddr) + 64);
172         map.vaddr = (unsigned long)vaddr;
173         ret = ioctl(group->container->fd, VFIO_IOMMU_MAP_DMA, &map);
174         if (ret == 0)
175                 return 0;
176
177         FSLMC_VFIO_LOG(ERR, "VFIO_IOMMU_MAP_DMA fails (errno = %d)", errno);
178         return -errno;
179 }
180
181 int vfio_dmamap_mem_region(uint64_t vaddr,
182                            uint64_t iova,
183                            uint64_t size)
184 {
185         struct fslmc_vfio_group *group;
186         struct vfio_iommu_type1_dma_map dma_map = {
187                 .argsz = sizeof(dma_map),
188                 .flags = VFIO_DMA_MAP_FLAG_READ | VFIO_DMA_MAP_FLAG_WRITE,
189         };
190
191         dma_map.vaddr = vaddr;
192         dma_map.size = size;
193         dma_map.iova = iova;
194
195         /* SET DMA MAP for IOMMU */
196         group = &vfio_groups[0];
197         if (ioctl(group->container->fd, VFIO_IOMMU_MAP_DMA, &dma_map)) {
198                 FSLMC_VFIO_LOG(ERR, "VFIO_IOMMU_MAP_DMA (errno = %d)", errno);
199                 return -1;
200         }
201         return 0;
202 }
203
204 int rte_fslmc_vfio_dmamap(void)
205 {
206         int ret;
207         struct fslmc_vfio_group *group;
208         struct vfio_iommu_type1_dma_map dma_map = {
209                 .argsz = sizeof(struct vfio_iommu_type1_dma_map),
210                 .flags = VFIO_DMA_MAP_FLAG_READ | VFIO_DMA_MAP_FLAG_WRITE,
211         };
212
213         int i;
214         const struct rte_memseg *memseg;
215
216         if (is_dma_done)
217                 return 0;
218         is_dma_done = 1;
219
220         for (i = 0; i < RTE_MAX_MEMSEG; i++) {
221                 memseg = rte_eal_get_physmem_layout();
222                 if (memseg == NULL) {
223                         FSLMC_VFIO_LOG(ERR, "Cannot get physical layout.");
224                         return -ENODEV;
225                 }
226
227                 if (memseg[i].addr == NULL && memseg[i].len == 0)
228                         break;
229
230                 dma_map.size = memseg[i].len;
231                 dma_map.vaddr = memseg[i].addr_64;
232 #ifdef RTE_LIBRTE_DPAA2_USE_PHYS_IOVA
233                 dma_map.iova = memseg[i].phys_addr;
234 #else
235                 dma_map.iova = dma_map.vaddr;
236 #endif
237
238                 /* SET DMA MAP for IOMMU */
239                 group = &vfio_groups[0];
240
241                 if (!group->container) {
242                         FSLMC_VFIO_LOG(ERR, "Container is not connected ");
243                         return -1;
244                 }
245
246                 FSLMC_VFIO_LOG(DEBUG, "-->Initial SHM Virtual ADDR %llX",
247                              dma_map.vaddr);
248                 FSLMC_VFIO_LOG(DEBUG, "-----> DMA size 0x%llX\n", dma_map.size);
249                 ret = ioctl(group->container->fd, VFIO_IOMMU_MAP_DMA,
250                             &dma_map);
251                 if (ret) {
252                         FSLMC_VFIO_LOG(ERR, "VFIO_IOMMU_MAP_DMA API"
253                                        "(errno = %d)", errno);
254                         return ret;
255                 }
256                 FSLMC_VFIO_LOG(DEBUG, "-----> dma_map.vaddr = 0x%llX",
257                              dma_map.vaddr);
258         }
259
260         /* TODO - This is a W.A. as VFIO currently does not add the mapping of
261          * the interrupt region to SMMU. This should be removed once the
262          * support is added in the Kernel.
263          */
264         vfio_map_irq_region(group);
265
266         return 0;
267 }
268
269 static int64_t vfio_map_mcp_obj(struct fslmc_vfio_group *group, char *mcp_obj)
270 {
271         int64_t v_addr = (int64_t)MAP_FAILED;
272         int32_t ret, mc_fd;
273
274         struct vfio_device_info d_info = { .argsz = sizeof(d_info) };
275         struct vfio_region_info reg_info = { .argsz = sizeof(reg_info) };
276
277         /* getting the mcp object's fd*/
278         mc_fd = ioctl(group->fd, VFIO_GROUP_GET_DEVICE_FD, mcp_obj);
279         if (mc_fd < 0) {
280                 FSLMC_VFIO_LOG(ERR, "error in VFIO get device %s fd from group"
281                             " %d", mcp_obj, group->fd);
282                 return v_addr;
283         }
284
285         /* getting device info*/
286         ret = ioctl(mc_fd, VFIO_DEVICE_GET_INFO, &d_info);
287         if (ret < 0) {
288                 FSLMC_VFIO_LOG(ERR, "error in VFIO getting DEVICE_INFO");
289                 goto MC_FAILURE;
290         }
291
292         /* getting device region info*/
293         ret = ioctl(mc_fd, VFIO_DEVICE_GET_REGION_INFO, &reg_info);
294         if (ret < 0) {
295                 FSLMC_VFIO_LOG(ERR, "error in VFIO getting REGION_INFO");
296                 goto MC_FAILURE;
297         }
298
299         FSLMC_VFIO_LOG(DEBUG, "region offset = %llx  , region size = %llx",
300                      reg_info.offset, reg_info.size);
301
302         v_addr = (uint64_t)mmap(NULL, reg_info.size,
303                 PROT_WRITE | PROT_READ, MAP_SHARED,
304                 mc_fd, reg_info.offset);
305
306 MC_FAILURE:
307         close(mc_fd);
308
309         return v_addr;
310 }
311
312 static inline int
313 dpaa2_compare_dpaa2_dev(const struct rte_dpaa2_device *dev,
314                          const struct rte_dpaa2_device *dev2)
315 {
316         /*not the same family device */
317         if (dev->dev_type != DPAA2_MC_DPNI_DEVID ||
318                         dev->dev_type != DPAA2_MC_DPSECI_DEVID)
319                 return -1;
320
321         if (dev->object_id == dev2->object_id)
322                 return 0;
323         else
324                 return 1;
325 }
326
327 static void
328 fslmc_bus_add_device(struct rte_dpaa2_device *dev)
329 {
330         struct rte_fslmc_device_list *dev_l;
331
332         dev_l = &rte_fslmc_bus.device_list;
333
334         /* device is valid, add in list (sorted) */
335         if (TAILQ_EMPTY(dev_l)) {
336                 TAILQ_INSERT_TAIL(dev_l, dev, next);
337         } else {
338                 struct rte_dpaa2_device *dev2;
339                 int ret;
340
341                 TAILQ_FOREACH(dev2, dev_l, next) {
342                         ret = dpaa2_compare_dpaa2_dev(dev, dev2);
343                         if (ret <= 0)
344                                 continue;
345
346                         TAILQ_INSERT_BEFORE(dev2, dev, next);
347                         return;
348                 }
349
350                 TAILQ_INSERT_TAIL(dev_l, dev, next);
351         }
352 }
353
354 /* Following function shall fetch total available list of MC devices
355  * from VFIO container & populate private list of devices and other
356  * data structures
357  */
358 int fslmc_vfio_process_group(void)
359 {
360         struct fslmc_vfio_device *vdev;
361         struct vfio_device_info device_info = { .argsz = sizeof(device_info) };
362         char *temp_obj, *object_type, *mcp_obj, *dev_name;
363         int32_t object_id, i, dev_fd, ret;
364         DIR *d;
365         struct dirent *dir;
366         char path[PATH_MAX];
367         int64_t v_addr;
368         int ndev_count;
369         int dpio_count = 0, dpbp_count = 0;
370         struct fslmc_vfio_group *group = &vfio_groups[0];
371         static int process_once;
372
373         /* if already done once */
374         if (process_once) {
375                 FSLMC_VFIO_LOG(DEBUG, "Already scanned once - re-scan "
376                             "not supported");
377                 return 0;
378         }
379         process_once = 0;
380
381         sprintf(path, "/sys/kernel/iommu_groups/%d/devices", group->groupid);
382
383         d = opendir(path);
384         if (!d) {
385                 FSLMC_VFIO_LOG(ERR, "Unable to open directory %s", path);
386                 return -1;
387         }
388
389         /*Counting the number of devices in a group and getting the mcp ID*/
390         ndev_count = 0;
391         mcp_obj = NULL;
392         while ((dir = readdir(d)) != NULL) {
393                 if (dir->d_type == DT_LNK) {
394                         ndev_count++;
395                         if (!strncmp("dpmcp", dir->d_name, 5)) {
396                                 if (mcp_obj)
397                                         free(mcp_obj);
398                                 mcp_obj = malloc(sizeof(dir->d_name));
399                                 if (!mcp_obj) {
400                                         FSLMC_VFIO_LOG(ERR, "mcp obj:Unable to"
401                                                     " allocate memory");
402                                         closedir(d);
403                                         return -ENOMEM;
404                                 }
405                                 strcpy(mcp_obj, dir->d_name);
406                                 temp_obj = strtok(dir->d_name, ".");
407                                 temp_obj = strtok(NULL, ".");
408                                 sscanf(temp_obj, "%d", &mcp_id);
409                         }
410                 }
411         }
412         closedir(d);
413         d = NULL;
414         if (!mcp_obj) {
415                 FSLMC_VFIO_LOG(ERR, "DPAA2 MCP Object not Found");
416                 return -ENODEV;
417         }
418         RTE_LOG(INFO, EAL, "fslmc: DPRC contains = %d devices\n", ndev_count);
419
420         /* Allocate the memory depends upon number of objects in a group*/
421         group->vfio_device = (struct fslmc_vfio_device *)malloc(ndev_count *
422                              sizeof(struct fslmc_vfio_device));
423         if (!(group->vfio_device)) {
424                 FSLMC_VFIO_LOG(ERR, "vfio device: Unable to allocate memory\n");
425                 free(mcp_obj);
426                 return -ENOMEM;
427         }
428
429         /* Allocate memory for MC Portal list */
430         rte_mcp_ptr_list = malloc(sizeof(void *) * 1);
431         if (!rte_mcp_ptr_list) {
432                 FSLMC_VFIO_LOG(ERR, "portal list: Unable to allocate memory!");
433                 free(mcp_obj);
434                 goto FAILURE;
435         }
436
437         v_addr = vfio_map_mcp_obj(group, mcp_obj);
438         free(mcp_obj);
439         if (v_addr == (int64_t)MAP_FAILED) {
440                 FSLMC_VFIO_LOG(ERR, "Error mapping region (errno = %d)", errno);
441                 goto FAILURE;
442         }
443
444         FSLMC_VFIO_LOG(DEBUG, "DPAA2 MC has VIR_ADD = %ld", v_addr);
445
446         rte_mcp_ptr_list[0] = (void *)v_addr;
447
448         d = opendir(path);
449         if (!d) {
450                 FSLMC_VFIO_LOG(ERR, "Unable to open %s Directory", path);
451                 goto FAILURE;
452         }
453
454         i = 0;
455         FSLMC_VFIO_LOG(DEBUG, "DPAA2 - Parsing devices:");
456         /* Parsing each object and initiating them*/
457         while ((dir = readdir(d)) != NULL) {
458                 if (dir->d_type != DT_LNK)
459                         continue;
460                 if (!strncmp("dprc", dir->d_name, 4) ||
461                     !strncmp("dpmcp", dir->d_name, 5))
462                         continue;
463                 dev_name = malloc(sizeof(dir->d_name));
464                 if (!dev_name) {
465                         FSLMC_VFIO_LOG(ERR, "name: Unable to allocate memory");
466                         goto FAILURE;
467                 }
468                 strcpy(dev_name, dir->d_name);
469                 object_type = strtok(dir->d_name, ".");
470                 temp_obj = strtok(NULL, ".");
471                 sscanf(temp_obj, "%d", &object_id);
472                 FSLMC_VFIO_LOG(DEBUG, " - %s ", dev_name);
473
474                 /* getting the device fd*/
475                 dev_fd = ioctl(group->fd, VFIO_GROUP_GET_DEVICE_FD, dev_name);
476                 if (dev_fd < 0) {
477                         FSLMC_VFIO_LOG(ERR, "VFIO_GROUP_GET_DEVICE_FD error"
478                                     " Device fd: %s, Group: %d",
479                                     dev_name, group->fd);
480                         free(dev_name);
481                         goto FAILURE;
482                 }
483
484                 free(dev_name);
485                 vdev = &group->vfio_device[group->object_index++];
486                 vdev->fd = dev_fd;
487                 vdev->index = i;
488                 i++;
489                 /* Get Device inofrmation */
490                 if (ioctl(vdev->fd, VFIO_DEVICE_GET_INFO, &device_info)) {
491                         FSLMC_VFIO_LOG(ERR, "DPAA2 VFIO_DEVICE_GET_INFO fail");
492                         goto FAILURE;
493                 }
494                 if (!strcmp(object_type, "dpni") ||
495                     !strcmp(object_type, "dpseci")) {
496                         struct rte_dpaa2_device *dev;
497
498                         dev = malloc(sizeof(struct rte_dpaa2_device));
499                         if (dev == NULL)
500                                 return -1;
501
502                         memset(dev, 0, sizeof(*dev));
503                         /* store hw_id of dpni/dpseci device */
504                         dev->object_id = object_id;
505                         dev->dev_type = (strcmp(object_type, "dpseci")) ?
506                                 DPAA2_MC_DPNI_DEVID : DPAA2_MC_DPSECI_DEVID;
507
508                         FSLMC_VFIO_LOG(DEBUG, "DPAA2: Added [%s-%d]\n",
509                                       object_type, object_id);
510
511                         fslmc_bus_add_device(dev);
512                 }
513                 if (!strcmp(object_type, "dpio")) {
514                         ret = dpaa2_create_dpio_device(vdev,
515                                                        &device_info,
516                                                        object_id);
517                         if (!ret)
518                                 dpio_count++;
519                 }
520                 if (!strcmp(object_type, "dpbp")) {
521                         ret = dpaa2_create_dpbp_device(object_id);
522                         if (!ret)
523                                 dpbp_count++;
524                 }
525         }
526         closedir(d);
527
528         ret = dpaa2_affine_qbman_swp();
529         if (ret)
530                 FSLMC_VFIO_LOG(DEBUG, "Error in affining qbman swp %d", ret);
531
532         FSLMC_VFIO_LOG(DEBUG, "DPAA2: Added dpbp_count = %d dpio_count=%d\n",
533                       dpbp_count, dpio_count);
534         return 0;
535
536 FAILURE:
537         if (d)
538                 closedir(d);
539         if (rte_mcp_ptr_list) {
540                 free(rte_mcp_ptr_list);
541                 rte_mcp_ptr_list = NULL;
542         }
543
544         free(group->vfio_device);
545         group->vfio_device = NULL;
546         return -1;
547 }
548
549 int fslmc_vfio_setup_group(void)
550 {
551         struct fslmc_vfio_group *group = NULL;
552         int groupid;
553         int ret, i;
554         char *container;
555         struct vfio_group_status status = { .argsz = sizeof(status) };
556
557         /* if already done once */
558         if (container_device_fd)
559                 return 0;
560
561         container = getenv("DPRC");
562
563         if (container == NULL) {
564                 FSLMC_VFIO_LOG(ERR, "VFIO container not set in env DPRC");
565                 return -EOPNOTSUPP;
566         }
567
568         /* get group number */
569         ret = vfio_get_group_no(SYSFS_FSL_MC_DEVICES, container, &groupid);
570         if (ret == 0) {
571                 RTE_LOG(WARNING, EAL, "%s not managed by VFIO, skipping\n",
572                         container);
573                 return -EOPNOTSUPP;
574         }
575
576         /* if negative, something failed */
577         if (ret < 0)
578                 return ret;
579
580         FSLMC_VFIO_LOG(DEBUG, "VFIO iommu group id = %d", groupid);
581
582         /* Check if group already exists */
583         for (i = 0; i < VFIO_MAX_GRP; i++) {
584                 group = &vfio_groups[i];
585                 if (group->groupid == groupid) {
586                         FSLMC_VFIO_LOG(ERR, "groupid already exists %d",
587                                        groupid);
588                         return 0;
589                 }
590         }
591
592         /* get the actual group fd */
593         ret = vfio_get_group_fd(groupid);
594         if (ret < 0)
595                 return ret;
596         group->fd = ret;
597
598         /*
599          * at this point, we know that this group is viable (meaning,
600          * all devices are either bound to VFIO or not bound to anything)
601          */
602
603         ret = ioctl(group->fd, VFIO_GROUP_GET_STATUS, &status);
604         if (ret) {
605                 FSLMC_VFIO_LOG(ERR, " VFIO error getting group status");
606                 close(group->fd);
607                 return ret;
608         }
609
610         if (!(status.flags & VFIO_GROUP_FLAGS_VIABLE)) {
611                 FSLMC_VFIO_LOG(ERR, "VFIO group not viable");
612                 close(group->fd);
613                 return -EPERM;
614         }
615         /* Since Group is VIABLE, Store the groupid */
616         group->groupid = groupid;
617
618         /* check if group does not have a container yet */
619         if (!(status.flags & VFIO_GROUP_FLAGS_CONTAINER_SET)) {
620                 /* Now connect this IOMMU group to given container */
621                 ret = vfio_connect_container(group);
622                 if (ret) {
623                         FSLMC_VFIO_LOG(ERR, "VFIO error connecting container"
624                                        " with groupid %d", groupid);
625                         close(group->fd);
626                         return ret;
627                 }
628         }
629
630         /* Get Device information */
631         ret = ioctl(group->fd, VFIO_GROUP_GET_DEVICE_FD, container);
632         if (ret < 0) {
633                 FSLMC_VFIO_LOG(ERR, "VFIO error getting device %s fd from"
634                                " group  %d", container, group->groupid);
635                 return ret;
636         }
637         container_device_fd = ret;
638         FSLMC_VFIO_LOG(DEBUG, "VFIO Container FD is [0x%X]",
639                      container_device_fd);
640
641         return 0;
642 }