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