New upstream version 17.11.1
[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 <eal_filesystem.h>
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_bus.h>
59
60 #include "rte_fslmc.h"
61 #include "fslmc_vfio.h"
62 #include <mc/fsl_dpmng.h>
63
64 #include "portal/dpaa2_hw_pvt.h"
65 #include "portal/dpaa2_hw_dpio.h"
66
67 #define FSLMC_VFIO_LOG(level, fmt, args...) \
68         RTE_LOG(level, EAL, fmt "\n", ##args)
69
70 /** Pathname of FSL-MC devices directory. */
71 #define SYSFS_FSL_MC_DEVICES "/sys/bus/fsl-mc/devices"
72
73 #define FSLMC_CONTAINER_MAX_LEN 8 /**< Of the format dprc.XX */
74
75 /* Number of VFIO containers & groups with in */
76 static struct fslmc_vfio_group vfio_group;
77 static struct fslmc_vfio_container vfio_container;
78 static int container_device_fd;
79 static char *g_container;
80 static uint32_t *msi_intr_vaddr;
81 void *(*rte_mcp_ptr_list);
82 static int is_dma_done;
83
84 static struct rte_dpaa2_object_list dpaa2_obj_list =
85         TAILQ_HEAD_INITIALIZER(dpaa2_obj_list);
86
87 /*register a fslmc bus based dpaa2 driver */
88 void
89 rte_fslmc_object_register(struct rte_dpaa2_object *object)
90 {
91         RTE_VERIFY(object);
92
93         TAILQ_INSERT_TAIL(&dpaa2_obj_list, object, next);
94 }
95
96 int
97 fslmc_get_container_group(int *groupid)
98 {
99         int ret;
100         char *container;
101
102         if (!g_container) {
103                 container = getenv("DPRC");
104                 if (container == NULL) {
105                         RTE_LOG(WARNING, EAL, "DPAA2: DPRC not available\n");
106                         return -EINVAL;
107                 }
108
109                 if (strlen(container) >= FSLMC_CONTAINER_MAX_LEN) {
110                         FSLMC_VFIO_LOG(ERR, "Invalid container name: %s\n",
111                                        container);
112                         return -1;
113                 }
114
115                 g_container = strdup(container);
116                 if (!g_container) {
117                         FSLMC_VFIO_LOG(ERR, "Out of memory.");
118                         return -ENOMEM;
119                 }
120         }
121
122         /* get group number */
123         ret = vfio_get_group_no(SYSFS_FSL_MC_DEVICES, g_container, groupid);
124         if (ret <= 0) {
125                 FSLMC_VFIO_LOG(ERR, "Unable to find %s IOMMU group",
126                                g_container);
127                 return -1;
128         }
129
130         FSLMC_VFIO_LOG(DEBUG, "Container: %s has VFIO iommu group id = %d",
131                        g_container, *groupid);
132
133         return 0;
134 }
135
136 static int
137 vfio_connect_container(void)
138 {
139         int fd, ret;
140
141         if (vfio_container.used) {
142                 FSLMC_VFIO_LOG(DEBUG, "No container available.");
143                 return -1;
144         }
145
146         /* Try connecting to vfio container if already created */
147         if (!ioctl(vfio_group.fd, VFIO_GROUP_SET_CONTAINER,
148                 &vfio_container.fd)) {
149                 FSLMC_VFIO_LOG(INFO,
150                     "Container pre-exists with FD[0x%x] for this group",
151                     vfio_container.fd);
152                 vfio_group.container = &vfio_container;
153                 return 0;
154         }
155
156         /* Opens main vfio file descriptor which represents the "container" */
157         fd = vfio_get_container_fd();
158         if (fd < 0) {
159                 FSLMC_VFIO_LOG(ERR, "Failed to open VFIO container");
160                 return -errno;
161         }
162
163         /* Check whether support for SMMU type IOMMU present or not */
164         if (ioctl(fd, VFIO_CHECK_EXTENSION, VFIO_TYPE1_IOMMU)) {
165                 /* Connect group to container */
166                 ret = ioctl(vfio_group.fd, VFIO_GROUP_SET_CONTAINER, &fd);
167                 if (ret) {
168                         FSLMC_VFIO_LOG(ERR, "Failed to setup group container");
169                         close(fd);
170                         return -errno;
171                 }
172
173                 ret = ioctl(fd, VFIO_SET_IOMMU, VFIO_TYPE1_IOMMU);
174                 if (ret) {
175                         FSLMC_VFIO_LOG(ERR, "Failed to setup VFIO iommu");
176                         close(fd);
177                         return -errno;
178                 }
179         } else {
180                 FSLMC_VFIO_LOG(ERR, "No supported IOMMU available");
181                 close(fd);
182                 return -EINVAL;
183         }
184
185         vfio_container.used = 1;
186         vfio_container.fd = fd;
187         vfio_container.group = &vfio_group;
188         vfio_group.container = &vfio_container;
189
190         return 0;
191 }
192
193 static int vfio_map_irq_region(struct fslmc_vfio_group *group)
194 {
195         int ret;
196         unsigned long *vaddr = NULL;
197         struct vfio_iommu_type1_dma_map map = {
198                 .argsz = sizeof(map),
199                 .flags = VFIO_DMA_MAP_FLAG_READ | VFIO_DMA_MAP_FLAG_WRITE,
200                 .vaddr = 0x6030000,
201                 .iova = 0x6030000,
202                 .size = 0x1000,
203         };
204
205         vaddr = (unsigned long *)mmap(NULL, 0x1000, PROT_WRITE |
206                 PROT_READ, MAP_SHARED, container_device_fd, 0x6030000);
207         if (vaddr == MAP_FAILED) {
208                 FSLMC_VFIO_LOG(ERR, "Unable to map region (errno = %d)", errno);
209                 return -errno;
210         }
211
212         msi_intr_vaddr = (uint32_t *)((char *)(vaddr) + 64);
213         map.vaddr = (unsigned long)vaddr;
214         ret = ioctl(group->container->fd, VFIO_IOMMU_MAP_DMA, &map);
215         if (ret == 0)
216                 return 0;
217
218         FSLMC_VFIO_LOG(ERR, "VFIO_IOMMU_MAP_DMA fails (errno = %d)", errno);
219         return -errno;
220 }
221
222 int rte_fslmc_vfio_dmamap(void)
223 {
224         int ret;
225         struct fslmc_vfio_group *group;
226         struct vfio_iommu_type1_dma_map dma_map = {
227                 .argsz = sizeof(struct vfio_iommu_type1_dma_map),
228                 .flags = VFIO_DMA_MAP_FLAG_READ | VFIO_DMA_MAP_FLAG_WRITE,
229         };
230
231         int i;
232         const struct rte_memseg *memseg;
233
234         if (is_dma_done)
235                 return 0;
236
237         memseg = rte_eal_get_physmem_layout();
238         if (memseg == NULL) {
239                 FSLMC_VFIO_LOG(ERR, "Cannot get physical layout.");
240                 return -ENODEV;
241         }
242
243         for (i = 0; i < RTE_MAX_MEMSEG; i++) {
244                 if (memseg[i].addr == NULL && memseg[i].len == 0) {
245                         FSLMC_VFIO_LOG(DEBUG, "Total %d segments found.", i);
246                         break;
247                 }
248
249                 dma_map.size = memseg[i].len;
250                 dma_map.vaddr = memseg[i].addr_64;
251 #ifdef RTE_LIBRTE_DPAA2_USE_PHYS_IOVA
252                 dma_map.iova = memseg[i].iova;
253 #else
254                 dma_map.iova = dma_map.vaddr;
255 #endif
256
257                 /* SET DMA MAP for IOMMU */
258                 group = &vfio_group;
259
260                 if (!group->container) {
261                         FSLMC_VFIO_LOG(ERR, "Container is not connected ");
262                         return -1;
263                 }
264
265                 FSLMC_VFIO_LOG(DEBUG, "-->Initial SHM Virtual ADDR %llX",
266                              dma_map.vaddr);
267                 FSLMC_VFIO_LOG(DEBUG, "-----> DMA size 0x%llX", dma_map.size);
268                 ret = ioctl(group->container->fd, VFIO_IOMMU_MAP_DMA,
269                             &dma_map);
270                 if (ret) {
271                         FSLMC_VFIO_LOG(ERR, "VFIO_IOMMU_MAP_DMA API(errno = %d)",
272                                        errno);
273                         return ret;
274                 }
275         }
276
277         /* Verifying that at least single segment is available */
278         if (i <= 0) {
279                 FSLMC_VFIO_LOG(ERR, "No Segments found for VFIO Mapping");
280                 return -1;
281         }
282
283         /* TODO - This is a W.A. as VFIO currently does not add the mapping of
284          * the interrupt region to SMMU. This should be removed once the
285          * support is added in the Kernel.
286          */
287         vfio_map_irq_region(group);
288
289         is_dma_done = 1;
290
291         return 0;
292 }
293
294 static int64_t vfio_map_mcp_obj(struct fslmc_vfio_group *group, char *mcp_obj)
295 {
296         int64_t v_addr = (int64_t)MAP_FAILED;
297         int32_t ret, mc_fd;
298
299         struct vfio_device_info d_info = { .argsz = sizeof(d_info) };
300         struct vfio_region_info reg_info = { .argsz = sizeof(reg_info) };
301
302         /* getting the mcp object's fd*/
303         mc_fd = ioctl(group->fd, VFIO_GROUP_GET_DEVICE_FD, mcp_obj);
304         if (mc_fd < 0) {
305                 FSLMC_VFIO_LOG(ERR, "error in VFIO get dev %s fd from group %d",
306                                mcp_obj, group->fd);
307                 return v_addr;
308         }
309
310         /* getting device info*/
311         ret = ioctl(mc_fd, VFIO_DEVICE_GET_INFO, &d_info);
312         if (ret < 0) {
313                 FSLMC_VFIO_LOG(ERR, "error in VFIO getting DEVICE_INFO");
314                 goto MC_FAILURE;
315         }
316
317         /* getting device region info*/
318         ret = ioctl(mc_fd, VFIO_DEVICE_GET_REGION_INFO, &reg_info);
319         if (ret < 0) {
320                 FSLMC_VFIO_LOG(ERR, "error in VFIO getting REGION_INFO");
321                 goto MC_FAILURE;
322         }
323
324         FSLMC_VFIO_LOG(DEBUG, "region offset = %llx  , region size = %llx",
325                        reg_info.offset, reg_info.size);
326
327         v_addr = (uint64_t)mmap(NULL, reg_info.size,
328                 PROT_WRITE | PROT_READ, MAP_SHARED,
329                 mc_fd, reg_info.offset);
330
331 MC_FAILURE:
332         close(mc_fd);
333
334         return v_addr;
335 }
336
337 #define IRQ_SET_BUF_LEN  (sizeof(struct vfio_irq_set) + sizeof(int))
338
339 int rte_dpaa2_intr_enable(struct rte_intr_handle *intr_handle, int index)
340 {
341         int len, ret;
342         char irq_set_buf[IRQ_SET_BUF_LEN];
343         struct vfio_irq_set *irq_set;
344         int *fd_ptr;
345
346         len = sizeof(irq_set_buf);
347
348         irq_set = (struct vfio_irq_set *)irq_set_buf;
349         irq_set->argsz = len;
350         irq_set->count = 1;
351         irq_set->flags =
352                 VFIO_IRQ_SET_DATA_EVENTFD | VFIO_IRQ_SET_ACTION_TRIGGER;
353         irq_set->index = index;
354         irq_set->start = 0;
355         fd_ptr = (int *)&irq_set->data;
356         *fd_ptr = intr_handle->fd;
357
358         ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
359         if (ret) {
360                 RTE_LOG(ERR, EAL, "Error:dpaa2 SET IRQs fd=%d, err = %d(%s)\n",
361                         intr_handle->fd, errno, strerror(errno));
362                 return ret;
363         }
364
365         return ret;
366 }
367
368 int rte_dpaa2_intr_disable(struct rte_intr_handle *intr_handle, int index)
369 {
370         struct vfio_irq_set *irq_set;
371         char irq_set_buf[IRQ_SET_BUF_LEN];
372         int len, ret;
373
374         len = sizeof(struct vfio_irq_set);
375
376         irq_set = (struct vfio_irq_set *)irq_set_buf;
377         irq_set->argsz = len;
378         irq_set->flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_TRIGGER;
379         irq_set->index = index;
380         irq_set->start = 0;
381         irq_set->count = 0;
382
383         ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
384         if (ret)
385                 RTE_LOG(ERR, EAL,
386                         "Error disabling dpaa2 interrupts for fd %d\n",
387                         intr_handle->fd);
388
389         return ret;
390 }
391
392 /* set up interrupt support (but not enable interrupts) */
393 int
394 rte_dpaa2_vfio_setup_intr(struct rte_intr_handle *intr_handle,
395                           int vfio_dev_fd,
396                           int num_irqs)
397 {
398         int i, ret;
399
400         /* start from MSI-X interrupt type */
401         for (i = 0; i < num_irqs; i++) {
402                 struct vfio_irq_info irq_info = { .argsz = sizeof(irq_info) };
403                 int fd = -1;
404
405                 irq_info.index = i;
406
407                 ret = ioctl(vfio_dev_fd, VFIO_DEVICE_GET_IRQ_INFO, &irq_info);
408                 if (ret < 0) {
409                         FSLMC_VFIO_LOG(ERR,
410                                        "cannot get IRQ(%d) info, error %i (%s)",
411                                        i, errno, strerror(errno));
412                         return -1;
413                 }
414
415                 /* if this vector cannot be used with eventfd,
416                  * fail if we explicitly
417                  * specified interrupt type, otherwise continue
418                  */
419                 if ((irq_info.flags & VFIO_IRQ_INFO_EVENTFD) == 0)
420                         continue;
421
422                 /* set up an eventfd for interrupts */
423                 fd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
424                 if (fd < 0) {
425                         FSLMC_VFIO_LOG(ERR,
426                                        "cannot set up eventfd, error %i (%s)\n",
427                                        errno, strerror(errno));
428                         return -1;
429                 }
430
431                 intr_handle->fd = fd;
432                 intr_handle->type = RTE_INTR_HANDLE_VFIO_MSI;
433                 intr_handle->vfio_dev_fd = vfio_dev_fd;
434
435                 return 0;
436         }
437
438         /* if we're here, we haven't found a suitable interrupt vector */
439         return -1;
440 }
441
442 /*
443  * fslmc_process_iodevices for processing only IO (ETH, CRYPTO, and possibly
444  * EVENT) devices.
445  */
446 static int
447 fslmc_process_iodevices(struct rte_dpaa2_device *dev)
448 {
449         int dev_fd;
450         struct vfio_device_info device_info = { .argsz = sizeof(device_info) };
451         struct rte_dpaa2_object *object = NULL;
452
453         dev_fd = ioctl(vfio_group.fd, VFIO_GROUP_GET_DEVICE_FD,
454                        dev->device.name);
455         if (dev_fd <= 0) {
456                 FSLMC_VFIO_LOG(ERR, "Unable to obtain device FD for device:%s",
457                                dev->device.name);
458                 return -1;
459         }
460
461         if (ioctl(dev_fd, VFIO_DEVICE_GET_INFO, &device_info)) {
462                 FSLMC_VFIO_LOG(ERR, "DPAA2 VFIO_DEVICE_GET_INFO fail");
463                 return -1;
464         }
465
466         switch (dev->dev_type) {
467         case DPAA2_ETH:
468                 rte_dpaa2_vfio_setup_intr(&dev->intr_handle, dev_fd,
469                                           device_info.num_irqs);
470                 break;
471         case DPAA2_CON:
472         case DPAA2_IO:
473         case DPAA2_CI:
474         case DPAA2_BPOOL:
475                 TAILQ_FOREACH(object, &dpaa2_obj_list, next) {
476                         if (dev->dev_type == object->dev_type)
477                                 object->create(dev_fd, &device_info,
478                                                dev->object_id);
479                         else
480                                 continue;
481                 }
482                 break;
483         default:
484                 break;
485         }
486
487         FSLMC_VFIO_LOG(DEBUG, "Device (%s) abstracted from VFIO",
488                        dev->device.name);
489         return 0;
490 }
491
492 static int
493 fslmc_process_mcp(struct rte_dpaa2_device *dev)
494 {
495         int64_t v_addr;
496         char *dev_name;
497         struct fsl_mc_io dpmng  = {0};
498         struct mc_version mc_ver_info = {0};
499
500         rte_mcp_ptr_list = malloc(sizeof(void *) * 1);
501         if (!rte_mcp_ptr_list) {
502                 FSLMC_VFIO_LOG(ERR, "Out of memory");
503                 return -ENOMEM;
504         }
505
506         dev_name = strdup(dev->device.name);
507         if (!dev_name) {
508                 FSLMC_VFIO_LOG(ERR, "Out of memory.");
509                 free(rte_mcp_ptr_list);
510                 rte_mcp_ptr_list = NULL;
511                 return -ENOMEM;
512         }
513
514         v_addr = vfio_map_mcp_obj(&vfio_group, dev_name);
515         if (v_addr == (int64_t)MAP_FAILED) {
516                 FSLMC_VFIO_LOG(ERR, "Error mapping region  (errno = %d)",
517                                errno);
518                 free(rte_mcp_ptr_list);
519                 rte_mcp_ptr_list = NULL;
520                 return -1;
521         }
522
523         /* check the MC version compatibility */
524         dpmng.regs = (void *)v_addr;
525         if (mc_get_version(&dpmng, CMD_PRI_LOW, &mc_ver_info))
526                 RTE_LOG(WARNING, PMD, "\tmc_get_version failed\n");
527
528         if ((mc_ver_info.major != MC_VER_MAJOR) ||
529             (mc_ver_info.minor < MC_VER_MINOR)) {
530                 RTE_LOG(ERR, PMD, "DPAA2 MC version not compatible!"
531                         " Expected %d.%d.x, Detected %d.%d.%d\n",
532                         MC_VER_MAJOR, MC_VER_MINOR,
533                         mc_ver_info.major, mc_ver_info.minor,
534                         mc_ver_info.revision);
535                 free(rte_mcp_ptr_list);
536                 rte_mcp_ptr_list = NULL;
537                 return -1;
538         }
539         rte_mcp_ptr_list[0] = (void *)v_addr;
540
541         return 0;
542 }
543
544 int
545 fslmc_vfio_process_group(void)
546 {
547         int ret;
548         int found_mportal = 0;
549         struct rte_dpaa2_device *dev, *dev_temp;
550
551         /* Search the MCP as that should be initialized first. */
552         TAILQ_FOREACH_SAFE(dev, &rte_fslmc_bus.device_list, next, dev_temp) {
553                 if (dev->dev_type == DPAA2_MPORTAL) {
554                         ret = fslmc_process_mcp(dev);
555                         if (ret) {
556                                 FSLMC_VFIO_LOG(DEBUG, "Unable to map Portal.");
557                                 return -1;
558                         }
559                         if (!found_mportal)
560                                 found_mportal = 1;
561
562                         TAILQ_REMOVE(&rte_fslmc_bus.device_list, dev, next);
563                         free(dev);
564                         dev = NULL;
565                         /* Ideally there is only a single dpmcp, but in case
566                          * multiple exists, looping on remaining devices.
567                          */
568                 }
569         }
570
571         /* Cannot continue if there is not even a single mportal */
572         if (!found_mportal) {
573                 FSLMC_VFIO_LOG(DEBUG,
574                                "No MC Portal device found. Not continuing.");
575                 return -1;
576         }
577
578         TAILQ_FOREACH_SAFE(dev, &rte_fslmc_bus.device_list, next, dev_temp) {
579                 if (!dev)
580                         break;
581
582                 switch (dev->dev_type) {
583                 case DPAA2_ETH:
584                 case DPAA2_CRYPTO:
585                         ret = fslmc_process_iodevices(dev);
586                         if (ret) {
587                                 FSLMC_VFIO_LOG(DEBUG,
588                                                "Dev (%s) init failed.",
589                                                dev->device.name);
590                                 return ret;
591                         }
592                         break;
593                 case DPAA2_CON:
594                 case DPAA2_IO:
595                 case DPAA2_CI:
596                 case DPAA2_BPOOL:
597                         /* Call the object creation routine and remove the
598                          * device entry from device list
599                          */
600                         ret = fslmc_process_iodevices(dev);
601                         if (ret) {
602                                 FSLMC_VFIO_LOG(DEBUG,
603                                                "Dev (%s) init failed.",
604                                                dev->device.name);
605                                 return -1;
606                         }
607
608                         /* This device is not required to be in the DPDK
609                          * exposed device list.
610                          */
611                         TAILQ_REMOVE(&rte_fslmc_bus.device_list, dev, next);
612                         free(dev);
613                         dev = NULL;
614                         break;
615                 case DPAA2_UNKNOWN:
616                 default:
617                         /* Unknown - ignore */
618                         FSLMC_VFIO_LOG(DEBUG, "Found unknown device (%s).",
619                                        dev->device.name);
620                         TAILQ_REMOVE(&rte_fslmc_bus.device_list, dev, next);
621                         free(dev);
622                         dev = NULL;
623                 }
624         }
625
626         return 0;
627 }
628
629 int
630 fslmc_vfio_setup_group(void)
631 {
632         int groupid;
633         int ret;
634         struct vfio_group_status status = { .argsz = sizeof(status) };
635
636         /* if already done once */
637         if (container_device_fd)
638                 return 0;
639
640         ret = fslmc_get_container_group(&groupid);
641         if (ret)
642                 return ret;
643
644         /* In case this group was already opened, continue without any
645          * processing.
646          */
647         if (vfio_group.groupid == groupid) {
648                 FSLMC_VFIO_LOG(ERR, "groupid already exists %d", groupid);
649                 return 0;
650         }
651
652         /* Get the actual group fd */
653         ret = vfio_get_group_fd(groupid);
654         if (ret < 0)
655                 return ret;
656         vfio_group.fd = ret;
657
658         /* Check group viability */
659         ret = ioctl(vfio_group.fd, VFIO_GROUP_GET_STATUS, &status);
660         if (ret) {
661                 FSLMC_VFIO_LOG(ERR, "VFIO error getting group status");
662                 close(vfio_group.fd);
663                 return ret;
664         }
665
666         if (!(status.flags & VFIO_GROUP_FLAGS_VIABLE)) {
667                 FSLMC_VFIO_LOG(ERR, "VFIO group not viable");
668                 close(vfio_group.fd);
669                 return -EPERM;
670         }
671         /* Since Group is VIABLE, Store the groupid */
672         vfio_group.groupid = groupid;
673
674         /* check if group does not have a container yet */
675         if (!(status.flags & VFIO_GROUP_FLAGS_CONTAINER_SET)) {
676                 /* Now connect this IOMMU group to given container */
677                 ret = vfio_connect_container();
678                 if (ret) {
679                         FSLMC_VFIO_LOG(ERR,
680                                 "Error connecting container with groupid %d",
681                                 groupid);
682                         close(vfio_group.fd);
683                         return ret;
684                 }
685         }
686
687         /* Get Device information */
688         ret = ioctl(vfio_group.fd, VFIO_GROUP_GET_DEVICE_FD, g_container);
689         if (ret < 0) {
690                 FSLMC_VFIO_LOG(ERR, "Error getting device %s fd from group %d",
691                                g_container, vfio_group.groupid);
692                 close(vfio_group.fd);
693                 return ret;
694         }
695         container_device_fd = ret;
696         FSLMC_VFIO_LOG(DEBUG, "VFIO Container FD is [0x%X]",
697                        container_device_fd);
698
699         return 0;
700 }