a3ac0a66c5d031af17e4dc5cb75ff9f19aa16b89
[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 vfio_dmamap_mem_region(uint64_t vaddr,
190                            uint64_t iova,
191                            uint64_t size)
192 {
193         struct fslmc_vfio_group *group;
194         struct vfio_iommu_type1_dma_map dma_map = {
195                 .argsz = sizeof(dma_map),
196                 .flags = VFIO_DMA_MAP_FLAG_READ | VFIO_DMA_MAP_FLAG_WRITE,
197         };
198
199         dma_map.vaddr = vaddr;
200         dma_map.size = size;
201         dma_map.iova = iova;
202
203         /* SET DMA MAP for IOMMU */
204         group = &vfio_groups[0];
205         if (ioctl(group->container->fd, VFIO_IOMMU_MAP_DMA, &dma_map)) {
206                 FSLMC_VFIO_LOG(ERR, "VFIO_IOMMU_MAP_DMA (errno = %d)", errno);
207                 return -1;
208         }
209         return 0;
210 }
211
212 int rte_fslmc_vfio_dmamap(void)
213 {
214         int ret;
215         struct fslmc_vfio_group *group;
216         struct vfio_iommu_type1_dma_map dma_map = {
217                 .argsz = sizeof(struct vfio_iommu_type1_dma_map),
218                 .flags = VFIO_DMA_MAP_FLAG_READ | VFIO_DMA_MAP_FLAG_WRITE,
219         };
220
221         int i;
222         const struct rte_memseg *memseg;
223
224         if (is_dma_done)
225                 return 0;
226
227         memseg = rte_eal_get_physmem_layout();
228         if (memseg == NULL) {
229                 FSLMC_VFIO_LOG(ERR, "Cannot get physical layout.");
230                 return -ENODEV;
231         }
232
233         for (i = 0; i < RTE_MAX_MEMSEG; i++) {
234                 if (memseg[i].addr == NULL && memseg[i].len == 0) {
235                         FSLMC_VFIO_LOG(DEBUG, "Total %d segments found.", i);
236                         break;
237                 }
238
239                 dma_map.size = memseg[i].len;
240                 dma_map.vaddr = memseg[i].addr_64;
241 #ifdef RTE_LIBRTE_DPAA2_USE_PHYS_IOVA
242                 dma_map.iova = memseg[i].phys_addr;
243 #else
244                 dma_map.iova = dma_map.vaddr;
245 #endif
246
247                 /* SET DMA MAP for IOMMU */
248                 group = &vfio_groups[0];
249
250                 if (!group->container) {
251                         FSLMC_VFIO_LOG(ERR, "Container is not connected ");
252                         return -1;
253                 }
254
255                 FSLMC_VFIO_LOG(DEBUG, "-->Initial SHM Virtual ADDR %llX",
256                              dma_map.vaddr);
257                 FSLMC_VFIO_LOG(DEBUG, "-----> DMA size 0x%llX", dma_map.size);
258                 ret = ioctl(group->container->fd, VFIO_IOMMU_MAP_DMA,
259                             &dma_map);
260                 if (ret) {
261                         FSLMC_VFIO_LOG(ERR, "VFIO_IOMMU_MAP_DMA API(errno = %d)",
262                                        errno);
263                         return ret;
264                 }
265         }
266
267         /* Verifying that at least single segment is available */
268         if (i <= 0) {
269                 FSLMC_VFIO_LOG(ERR, "No Segments found for VFIO Mapping");
270                 return -1;
271         }
272
273         /* TODO - This is a W.A. as VFIO currently does not add the mapping of
274          * the interrupt region to SMMU. This should be removed once the
275          * support is added in the Kernel.
276          */
277         vfio_map_irq_region(group);
278
279         is_dma_done = 1;
280
281         return 0;
282 }
283
284 static int64_t vfio_map_mcp_obj(struct fslmc_vfio_group *group, char *mcp_obj)
285 {
286         int64_t v_addr = (int64_t)MAP_FAILED;
287         int32_t ret, mc_fd;
288
289         struct vfio_device_info d_info = { .argsz = sizeof(d_info) };
290         struct vfio_region_info reg_info = { .argsz = sizeof(reg_info) };
291
292         /* getting the mcp object's fd*/
293         mc_fd = ioctl(group->fd, VFIO_GROUP_GET_DEVICE_FD, mcp_obj);
294         if (mc_fd < 0) {
295                 FSLMC_VFIO_LOG(ERR, "error in VFIO get dev %s fd from group %d",
296                                mcp_obj, group->fd);
297                 return v_addr;
298         }
299
300         /* getting device info*/
301         ret = ioctl(mc_fd, VFIO_DEVICE_GET_INFO, &d_info);
302         if (ret < 0) {
303                 FSLMC_VFIO_LOG(ERR, "error in VFIO getting DEVICE_INFO");
304                 goto MC_FAILURE;
305         }
306
307         /* getting device region info*/
308         ret = ioctl(mc_fd, VFIO_DEVICE_GET_REGION_INFO, &reg_info);
309         if (ret < 0) {
310                 FSLMC_VFIO_LOG(ERR, "error in VFIO getting REGION_INFO");
311                 goto MC_FAILURE;
312         }
313
314         FSLMC_VFIO_LOG(DEBUG, "region offset = %llx  , region size = %llx",
315                        reg_info.offset, reg_info.size);
316
317         v_addr = (uint64_t)mmap(NULL, reg_info.size,
318                 PROT_WRITE | PROT_READ, MAP_SHARED,
319                 mc_fd, reg_info.offset);
320
321 MC_FAILURE:
322         close(mc_fd);
323
324         return v_addr;
325 }
326
327 static inline int
328 dpaa2_compare_dpaa2_dev(const struct rte_dpaa2_device *dev,
329                          const struct rte_dpaa2_device *dev2)
330 {
331         /*not the same family device */
332         if (dev->dev_type != DPAA2_MC_DPNI_DEVID ||
333                         dev->dev_type != DPAA2_MC_DPSECI_DEVID)
334                 return -1;
335
336         if (dev->object_id == dev2->object_id)
337                 return 0;
338         else
339                 return 1;
340 }
341
342 static void
343 fslmc_bus_add_device(struct rte_dpaa2_device *dev)
344 {
345         struct rte_fslmc_device_list *dev_l;
346
347         dev_l = &rte_fslmc_bus.device_list;
348
349         /* device is valid, add in list (sorted) */
350         if (TAILQ_EMPTY(dev_l)) {
351                 TAILQ_INSERT_TAIL(dev_l, dev, next);
352         } else {
353                 struct rte_dpaa2_device *dev2;
354                 int ret;
355
356                 TAILQ_FOREACH(dev2, dev_l, next) {
357                         ret = dpaa2_compare_dpaa2_dev(dev, dev2);
358                         if (ret <= 0)
359                                 continue;
360
361                         TAILQ_INSERT_BEFORE(dev2, dev, next);
362                         return;
363                 }
364
365                 TAILQ_INSERT_TAIL(dev_l, dev, next);
366         }
367 }
368
369 #define IRQ_SET_BUF_LEN  (sizeof(struct vfio_irq_set) + sizeof(int))
370
371 int rte_dpaa2_intr_enable(struct rte_intr_handle *intr_handle,
372                           uint32_t index)
373 {
374         struct vfio_irq_set *irq_set;
375         char irq_set_buf[IRQ_SET_BUF_LEN];
376         int *fd_ptr, fd, ret;
377
378         /* Prepare vfio_irq_set structure and SET the IRQ in VFIO */
379         /* Give the eventfd to VFIO */
380         fd = eventfd(0, 0);
381         irq_set = (struct vfio_irq_set *)irq_set_buf;
382         irq_set->argsz = sizeof(irq_set_buf);
383         irq_set->count = 1;
384         irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD |
385                          VFIO_IRQ_SET_ACTION_TRIGGER;
386         irq_set->index = index;
387         irq_set->start = 0;
388         fd_ptr = (int *)&irq_set->data;
389         *fd_ptr = fd;
390
391         ret = ioctl(intr_handle->vfio_dev_fd, VFIO_DEVICE_SET_IRQS, irq_set);
392         if (ret < 0) {
393                 FSLMC_VFIO_LOG(ERR, "Unable to set IRQ in VFIO, ret: %d\n",
394                                ret);
395                 return -1;
396         }
397
398         /* Set the FD and update the flags */
399         intr_handle->fd = fd;
400         return 0;
401 }
402
403 /* Following function shall fetch total available list of MC devices
404  * from VFIO container & populate private list of devices and other
405  * data structures
406  */
407 int fslmc_vfio_process_group(void)
408 {
409         struct fslmc_vfio_device *vdev;
410         struct vfio_device_info device_info = { .argsz = sizeof(device_info) };
411         char *temp_obj, *object_type, *mcp_obj, *dev_name;
412         int32_t object_id, i, dev_fd, ret;
413         DIR *d;
414         struct dirent *dir;
415         char path[PATH_MAX];
416         int64_t v_addr;
417         int ndev_count;
418         struct fslmc_vfio_group *group = &vfio_groups[0];
419         static int process_once;
420
421         /* if already done once */
422         if (process_once) {
423                 FSLMC_VFIO_LOG(DEBUG,
424                                "Already scanned once - re-scan not supported");
425                 return 0;
426         }
427         process_once = 0;
428
429         sprintf(path, "/sys/kernel/iommu_groups/%d/devices", group->groupid);
430
431         d = opendir(path);
432         if (!d) {
433                 FSLMC_VFIO_LOG(ERR, "Unable to open directory %s", path);
434                 return -1;
435         }
436
437         /*Counting the number of devices in a group and getting the mcp ID*/
438         ndev_count = 0;
439         mcp_obj = NULL;
440         while ((dir = readdir(d)) != NULL) {
441                 if (dir->d_type == DT_LNK) {
442                         ndev_count++;
443                         if (!strncmp("dpmcp", dir->d_name, 5)) {
444                                 if (mcp_obj)
445                                         free(mcp_obj);
446                                 mcp_obj = malloc(sizeof(dir->d_name));
447                                 if (!mcp_obj) {
448                                         FSLMC_VFIO_LOG(ERR,
449                                                        "mcp obj:alloc failed");
450                                         closedir(d);
451                                         return -ENOMEM;
452                                 }
453                                 strcpy(mcp_obj, dir->d_name);
454                                 temp_obj = strtok(dir->d_name, ".");
455                                 temp_obj = strtok(NULL, ".");
456                                 sscanf(temp_obj, "%d", &mcp_id);
457                         }
458                 }
459         }
460         closedir(d);
461         d = NULL;
462         if (!mcp_obj) {
463                 FSLMC_VFIO_LOG(ERR, "DPAA2 MCP Object not Found");
464                 return -ENODEV;
465         }
466         RTE_LOG(INFO, EAL, "fslmc: DPRC contains = %d devices\n", ndev_count);
467
468         /* Allocate the memory depends upon number of objects in a group*/
469         group->vfio_device = (struct fslmc_vfio_device *)malloc(ndev_count *
470                              sizeof(struct fslmc_vfio_device));
471         if (!(group->vfio_device)) {
472                 FSLMC_VFIO_LOG(ERR, "vfio device: Unable to allocate memory\n");
473                 free(mcp_obj);
474                 return -ENOMEM;
475         }
476
477         /* Allocate memory for MC Portal list */
478         rte_mcp_ptr_list = malloc(sizeof(void *) * 1);
479         if (!rte_mcp_ptr_list) {
480                 FSLMC_VFIO_LOG(ERR, "portal list: Unable to allocate memory!");
481                 free(mcp_obj);
482                 goto FAILURE;
483         }
484
485         v_addr = vfio_map_mcp_obj(group, mcp_obj);
486         free(mcp_obj);
487         if (v_addr == (int64_t)MAP_FAILED) {
488                 FSLMC_VFIO_LOG(ERR, "Error mapping region (errno = %d)", errno);
489                 goto FAILURE;
490         }
491
492         rte_mcp_ptr_list[0] = (void *)v_addr;
493
494         d = opendir(path);
495         if (!d) {
496                 FSLMC_VFIO_LOG(ERR, "Unable to open %s Directory", path);
497                 goto FAILURE;
498         }
499
500         i = 0;
501         /* Parsing each object and initiating them*/
502         while ((dir = readdir(d)) != NULL) {
503                 if (dir->d_type != DT_LNK)
504                         continue;
505                 if (!strncmp("dprc", dir->d_name, 4) ||
506                     !strncmp("dpmcp", dir->d_name, 5))
507                         continue;
508                 dev_name = malloc(sizeof(dir->d_name));
509                 if (!dev_name) {
510                         FSLMC_VFIO_LOG(ERR, "name: Unable to allocate memory");
511                         goto FAILURE;
512                 }
513                 strcpy(dev_name, dir->d_name);
514                 object_type = strtok(dir->d_name, ".");
515                 temp_obj = strtok(NULL, ".");
516                 sscanf(temp_obj, "%d", &object_id);
517
518                 /* getting the device fd*/
519                 dev_fd = ioctl(group->fd, VFIO_GROUP_GET_DEVICE_FD, dev_name);
520                 if (dev_fd < 0) {
521                         FSLMC_VFIO_LOG(ERR,
522                                        "GET_DEVICE_FD error fd: %s, Group: %d",
523                                        dev_name, group->fd);
524                         free(dev_name);
525                         goto FAILURE;
526                 }
527
528                 free(dev_name);
529                 vdev = &group->vfio_device[group->object_index++];
530                 vdev->fd = dev_fd;
531                 vdev->index = i;
532                 i++;
533                 /* Get Device inofrmation */
534                 if (ioctl(vdev->fd, VFIO_DEVICE_GET_INFO, &device_info)) {
535                         FSLMC_VFIO_LOG(ERR, "DPAA2 VFIO_DEVICE_GET_INFO fail");
536                         goto FAILURE;
537                 }
538                 if (!strcmp(object_type, "dpni") ||
539                     !strcmp(object_type, "dpseci")) {
540                         struct rte_dpaa2_device *dev;
541
542                         dev = malloc(sizeof(struct rte_dpaa2_device));
543                         if (dev == NULL)
544                                 return -1;
545
546                         memset(dev, 0, sizeof(*dev));
547                         /* store hw_id of dpni/dpseci device */
548                         dev->object_id = object_id;
549                         dev->dev_type = (strcmp(object_type, "dpseci")) ?
550                                 DPAA2_MC_DPNI_DEVID : DPAA2_MC_DPSECI_DEVID;
551
552                         sprintf(dev->name, "%s.%d", object_type, object_id);
553                         dev->device.name = dev->name;
554
555                         fslmc_bus_add_device(dev);
556                         FSLMC_VFIO_LOG(DEBUG, "DPAA2: Added %s", dev->name);
557                 } else {
558                         /* Parse all other objects */
559                         struct rte_dpaa2_object *object;
560
561                         TAILQ_FOREACH(object, &fslmc_obj_list, next) {
562                                 if (!strcmp(object_type, object->name))
563                                         object->create(vdev, &device_info,
564                                                        object_id);
565                                 else
566                                         continue;
567                         }
568                 }
569         }
570         closedir(d);
571
572         ret = dpaa2_affine_qbman_swp();
573         if (ret)
574                 FSLMC_VFIO_LOG(DEBUG, "Error in affining qbman swp %d", ret);
575
576         return 0;
577
578 FAILURE:
579         if (d)
580                 closedir(d);
581         if (rte_mcp_ptr_list) {
582                 free(rte_mcp_ptr_list);
583                 rte_mcp_ptr_list = NULL;
584         }
585
586         free(group->vfio_device);
587         group->vfio_device = NULL;
588         return -1;
589 }
590
591 int fslmc_vfio_setup_group(void)
592 {
593         struct fslmc_vfio_group *group = NULL;
594         int groupid;
595         int ret, i;
596         char *container;
597         struct vfio_group_status status = { .argsz = sizeof(status) };
598
599         /* if already done once */
600         if (container_device_fd)
601                 return 0;
602
603         container = getenv("DPRC");
604
605         if (container == NULL) {
606                 FSLMC_VFIO_LOG(ERR, "VFIO container not set in env DPRC");
607                 return -EOPNOTSUPP;
608         }
609
610         /* get group number */
611         ret = vfio_get_group_no(SYSFS_FSL_MC_DEVICES, container, &groupid);
612         if (ret == 0) {
613                 RTE_LOG(WARNING, EAL, "%s not managed by VFIO, skipping\n",
614                         container);
615                 return -EOPNOTSUPP;
616         }
617
618         /* if negative, something failed */
619         if (ret < 0)
620                 return ret;
621
622         FSLMC_VFIO_LOG(DEBUG, "VFIO iommu group id = %d", groupid);
623
624         /* Check if group already exists */
625         for (i = 0; i < VFIO_MAX_GRP; i++) {
626                 group = &vfio_groups[i];
627                 if (group->groupid == groupid) {
628                         FSLMC_VFIO_LOG(ERR, "groupid already exists %d",
629                                        groupid);
630                         return 0;
631                 }
632         }
633
634         /* get the actual group fd */
635         ret = vfio_get_group_fd(groupid);
636         if (ret < 0)
637                 return ret;
638         group->fd = ret;
639
640         /*
641          * at this point, we know that this group is viable (meaning,
642          * all devices are either bound to VFIO or not bound to anything)
643          */
644
645         ret = ioctl(group->fd, VFIO_GROUP_GET_STATUS, &status);
646         if (ret) {
647                 FSLMC_VFIO_LOG(ERR, " VFIO error getting group status");
648                 close(group->fd);
649                 return ret;
650         }
651
652         if (!(status.flags & VFIO_GROUP_FLAGS_VIABLE)) {
653                 FSLMC_VFIO_LOG(ERR, "VFIO group not viable");
654                 close(group->fd);
655                 return -EPERM;
656         }
657         /* Since Group is VIABLE, Store the groupid */
658         group->groupid = groupid;
659
660         /* check if group does not have a container yet */
661         if (!(status.flags & VFIO_GROUP_FLAGS_CONTAINER_SET)) {
662                 /* Now connect this IOMMU group to given container */
663                 ret = vfio_connect_container(group);
664                 if (ret) {
665                         FSLMC_VFIO_LOG(ERR, "VFIO error connecting container"
666                                        " with groupid %d", groupid);
667                         close(group->fd);
668                         return ret;
669                 }
670         }
671
672         /* Get Device information */
673         ret = ioctl(group->fd, VFIO_GROUP_GET_DEVICE_FD, container);
674         if (ret < 0) {
675                 FSLMC_VFIO_LOG(ERR, "VFIO error getting device %s fd from"
676                                " group  %d", container, group->groupid);
677                 return ret;
678         }
679         container_device_fd = ret;
680         FSLMC_VFIO_LOG(DEBUG, "VFIO Container FD is [0x%X]",
681                      container_device_fd);
682
683         return 0;
684 }