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