3aace0838a790d1c3d603e63d82037a5ab941a0f
[dpdk.git] / lib / librte_eal / common / eal_common_devargs.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright 2014 6WIND S.A.
5  *
6  *   Redistribution and use in source and binary forms, with or without
7  *   modification, are permitted provided that the following conditions
8  *   are met:
9  *
10  *     * Redistributions of source code must retain the above copyright
11  *       notice, this list of conditions and the following disclaimer.
12  *     * Redistributions in binary form must reproduce the above copyright
13  *       notice, this list of conditions and the following disclaimer in
14  *       the documentation and/or other materials provided with the
15  *       distribution.
16  *     * Neither the name of 6WIND S.A nor the names of its contributors
17  *       may be used to endorse or promote products derived from this
18  *       software without specific prior written permission.
19  *
20  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 /* This file manages the list of devices and their arguments, as given
34  * by the user at startup */
35
36 #include <string.h>
37
38 #include <rte_log.h>
39 #include <rte_pci.h>
40 #include <rte_devargs.h>
41 #include "eal_private.h"
42
43 /** Global list of user devices */
44 struct rte_devargs_list devargs_list =
45         TAILQ_HEAD_INITIALIZER(devargs_list);
46
47 /* store a whitelist parameter for later parsing */
48 int
49 rte_eal_devargs_add(enum rte_devtype devtype, const char *devargs_str)
50 {
51         struct rte_devargs *devargs = NULL;
52         char *buf = NULL;
53         char *sep;
54         int ret;
55
56         /* use malloc instead of rte_malloc as it's called early at init */
57         devargs = malloc(sizeof(*devargs));
58         if (devargs == NULL) {
59                 RTE_LOG(ERR, EAL, "cannot allocate devargs\n");
60                 goto fail;
61         }
62         memset(devargs, 0, sizeof(*devargs));
63         devargs->type = devtype;
64
65         buf = strdup(devargs_str);
66         if (buf == NULL) {
67                 RTE_LOG(ERR, EAL, "cannot allocate temp memory for devargs\n");
68                 goto fail;
69         }
70
71         /* set the first ',' to '\0' to split name and arguments */
72         sep = strchr(buf, ',');
73         if (sep != NULL) {
74                 sep[0] = '\0';
75                 devargs->args = strdup(sep + 1);
76                 if (devargs->args == NULL) {
77                         RTE_LOG(ERR, EAL, "cannot allocate for devargs args\n");
78                         goto fail;
79                 }
80         }
81
82         switch (devargs->type) {
83         case RTE_DEVTYPE_WHITELISTED_PCI:
84         case RTE_DEVTYPE_BLACKLISTED_PCI:
85                 /* try to parse pci identifier */
86                 if (eal_parse_pci_BDF(buf, &devargs->pci.addr) != 0 &&
87                     eal_parse_pci_DomBDF(buf, &devargs->pci.addr) != 0) {
88                         RTE_LOG(ERR, EAL, "invalid PCI identifier <%s>\n", buf);
89                         goto fail;
90                 }
91                 break;
92         case RTE_DEVTYPE_VIRTUAL:
93                 /* save driver name */
94                 ret = snprintf(devargs->virtual.drv_name,
95                                sizeof(devargs->virtual.drv_name), "%s", buf);
96                 if (ret < 0 || ret >= (int)sizeof(devargs->virtual.drv_name)) {
97                         RTE_LOG(ERR, EAL, "driver name too large: <%s>\n", buf);
98                         goto fail;
99                 }
100                 break;
101         }
102
103         free(buf);
104         TAILQ_INSERT_TAIL(&devargs_list, devargs, next);
105         return 0;
106
107 fail:
108         if (devargs->args)
109                 free(devargs->args);
110         if (buf)
111                 free(buf);
112         if (devargs)
113                 free(devargs);
114         return -1;
115 }
116
117 /* count the number of devices of a specified type */
118 unsigned int
119 rte_eal_devargs_type_count(enum rte_devtype devtype)
120 {
121         struct rte_devargs *devargs;
122         unsigned int count = 0;
123
124         TAILQ_FOREACH(devargs, &devargs_list, next) {
125                 if (devargs->type != devtype)
126                         continue;
127                 count++;
128         }
129         return count;
130 }
131
132 /* dump the user devices on the console */
133 void
134 rte_eal_devargs_dump(FILE *f)
135 {
136         struct rte_devargs *devargs;
137
138         fprintf(f, "User device white list:\n");
139         TAILQ_FOREACH(devargs, &devargs_list, next) {
140                 if (devargs->type == RTE_DEVTYPE_WHITELISTED_PCI)
141                         fprintf(f, "  PCI whitelist " PCI_PRI_FMT " %s\n",
142                                devargs->pci.addr.domain,
143                                devargs->pci.addr.bus,
144                                devargs->pci.addr.devid,
145                                devargs->pci.addr.function,
146                                devargs->args);
147                 else if (devargs->type == RTE_DEVTYPE_BLACKLISTED_PCI)
148                         fprintf(f, "  PCI blacklist " PCI_PRI_FMT " %s\n",
149                                devargs->pci.addr.domain,
150                                devargs->pci.addr.bus,
151                                devargs->pci.addr.devid,
152                                devargs->pci.addr.function,
153                                devargs->args);
154                 else if (devargs->type == RTE_DEVTYPE_VIRTUAL)
155                         fprintf(f, "  VIRTUAL %s %s\n",
156                                devargs->virtual.drv_name,
157                                devargs->args);
158                 else
159                         fprintf(f, "  UNKNOWN %s\n", devargs->args);
160         }
161 }