]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | /*- |
2 | * BSD LICENSE | |
3 | * | |
4 | * Copyright(c) 2010-2014 Intel Corporation. All rights reserved. | |
5 | * Copyright(c) 2014 6WIND S.A. | |
6 | * All rights reserved. | |
7 | * | |
8 | * Redistribution and use in source and binary forms, with or without | |
9 | * modification, are permitted provided that the following conditions | |
10 | * are met: | |
11 | * | |
12 | * * Redistributions of source code must retain the above copyright | |
13 | * notice, this list of conditions and the following disclaimer. | |
14 | * * Redistributions in binary form must reproduce the above copyright | |
15 | * notice, this list of conditions and the following disclaimer in | |
16 | * the documentation and/or other materials provided with the | |
17 | * distribution. | |
18 | * * Neither the name of Intel Corporation nor the names of its | |
19 | * contributors may be used to endorse or promote products derived | |
20 | * from this software without specific prior written permission. | |
21 | * | |
22 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
23 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
24 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
25 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
26 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
27 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
28 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
30 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
31 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
32 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
33 | */ | |
34 | ||
35 | #include <stdio.h> | |
36 | #include <string.h> | |
37 | #include <stdint.h> | |
38 | #include <sys/queue.h> | |
39 | ||
40 | #include <rte_interrupts.h> | |
41 | #include <rte_pci.h> | |
42 | #include <rte_ethdev.h> | |
43 | #include <rte_devargs.h> | |
44 | ||
45 | #include "test.h" | |
46 | #include "resource.h" | |
47 | ||
48 | /* Generic maximum number of drivers to have room to allocate all drivers */ | |
49 | #define NUM_MAX_DRIVERS 256 | |
50 | ||
51 | /* | |
52 | * PCI test | |
53 | * ======== | |
54 | * | |
55 | * - Register a driver with a ``probe()`` function. | |
56 | * | |
57 | * - Dump all PCI devices. | |
58 | * | |
59 | * - Check that the ``probe()`` function is called at least once. | |
60 | */ | |
61 | ||
62 | int test_pci_run = 0; /* value checked by the multiprocess test */ | |
63 | static unsigned pci_dev_count; | |
64 | ||
65 | static int my_driver_init(struct rte_pci_driver *dr, | |
66 | struct rte_pci_device *dev); | |
67 | ||
68 | /* IXGBE NICS */ | |
69 | struct rte_pci_id my_driver_id[] = { | |
70 | {RTE_PCI_DEVICE(0x0001, 0x1234)}, | |
71 | { .vendor_id = 0, /* sentinel */ }, | |
72 | }; | |
73 | ||
74 | struct rte_pci_id my_driver_id2[] = { | |
75 | {RTE_PCI_DEVICE(0x0001, 0x4444)}, | |
76 | {RTE_PCI_DEVICE(0x0002, 0xabcd)}, | |
77 | { .vendor_id = 0, /* sentinel */ }, | |
78 | }; | |
79 | ||
80 | struct rte_pci_driver my_driver = { | |
81 | .driver = { | |
82 | .name = "test_driver" | |
83 | }, | |
84 | .probe = my_driver_init, | |
85 | .id_table = my_driver_id, | |
86 | .drv_flags = 0, | |
87 | }; | |
88 | ||
89 | struct rte_pci_driver my_driver2 = { | |
90 | .driver = { | |
91 | .name = "test_driver2" | |
92 | }, | |
93 | .probe = my_driver_init, | |
94 | .id_table = my_driver_id2, | |
95 | .drv_flags = 0, | |
96 | }; | |
97 | ||
98 | static int | |
99 | my_driver_init(__attribute__((unused)) struct rte_pci_driver *dr, | |
100 | struct rte_pci_device *dev) | |
101 | { | |
102 | printf("My driver init called in %s\n", dr->driver.name); | |
103 | printf("%x:%x:%x.%d", dev->addr.domain, dev->addr.bus, | |
104 | dev->addr.devid, dev->addr.function); | |
105 | printf(" - vendor:%x device:%x\n", dev->id.vendor_id, dev->id.device_id); | |
106 | ||
107 | pci_dev_count ++; | |
108 | return 0; | |
109 | } | |
110 | ||
111 | static void | |
112 | blacklist_all_devices(void) | |
113 | { | |
114 | struct rte_pci_device *dev = NULL; | |
115 | unsigned i = 0; | |
116 | char pci_addr_str[16]; | |
117 | ||
118 | TAILQ_FOREACH(dev, &pci_device_list, next) { | |
119 | snprintf(pci_addr_str, sizeof(pci_addr_str), PCI_PRI_FMT, | |
120 | dev->addr.domain, dev->addr.bus, dev->addr.devid, | |
121 | dev->addr.function); | |
122 | if (rte_eal_devargs_add(RTE_DEVTYPE_BLACKLISTED_PCI, | |
123 | pci_addr_str) < 0) { | |
124 | printf("Error: cannot blacklist <%s>", pci_addr_str); | |
125 | break; | |
126 | } | |
127 | i++; | |
128 | } | |
129 | printf("%u devices blacklisted\n", i); | |
130 | } | |
131 | ||
132 | /* clear devargs list that was modified by the test */ | |
133 | static void free_devargs_list(void) | |
134 | { | |
135 | struct rte_devargs *devargs; | |
136 | ||
137 | while (!TAILQ_EMPTY(&devargs_list)) { | |
138 | devargs = TAILQ_FIRST(&devargs_list); | |
139 | TAILQ_REMOVE(&devargs_list, devargs, next); | |
140 | free(devargs->args); | |
141 | free(devargs); | |
142 | } | |
143 | } | |
144 | ||
145 | /* backup real devices & drivers (not used for testing) */ | |
146 | struct pci_driver_list real_pci_driver_list = | |
147 | TAILQ_HEAD_INITIALIZER(real_pci_driver_list); | |
148 | struct pci_device_list real_pci_device_list = | |
149 | TAILQ_HEAD_INITIALIZER(real_pci_device_list); | |
150 | ||
151 | REGISTER_LINKED_RESOURCE(test_pci_sysfs); | |
152 | ||
153 | static int | |
154 | test_pci_setup(void) | |
155 | { | |
156 | struct rte_pci_device *dev; | |
157 | struct rte_pci_driver *dr; | |
158 | const struct resource *r; | |
159 | int ret; | |
160 | ||
161 | r = resource_find("test_pci_sysfs"); | |
162 | TEST_ASSERT_NOT_NULL(r, "missing resource test_pci_sysfs"); | |
163 | ||
164 | ret = resource_untar(r); | |
165 | TEST_ASSERT_SUCCESS(ret, "failed to untar %s", r->name); | |
166 | ||
167 | ret = setenv("SYSFS_PCI_DEVICES", "test_pci_sysfs/bus/pci/devices", 1); | |
168 | TEST_ASSERT_SUCCESS(ret, "failed to setenv"); | |
169 | ||
170 | /* Unregister original devices & drivers lists */ | |
171 | while (!TAILQ_EMPTY(&pci_driver_list)) { | |
172 | dr = TAILQ_FIRST(&pci_driver_list); | |
173 | rte_eal_pci_unregister(dr); | |
174 | TAILQ_INSERT_TAIL(&real_pci_driver_list, dr, next); | |
175 | } | |
176 | ||
177 | while (!TAILQ_EMPTY(&pci_device_list)) { | |
178 | dev = TAILQ_FIRST(&pci_device_list); | |
179 | TAILQ_REMOVE(&pci_device_list, dev, next); | |
180 | TAILQ_INSERT_TAIL(&real_pci_device_list, dev, next); | |
181 | } | |
182 | ||
183 | ret = rte_eal_pci_scan(); | |
184 | TEST_ASSERT_SUCCESS(ret, "failed to scan PCI bus"); | |
185 | rte_eal_pci_dump(stdout); | |
186 | ||
187 | return 0; | |
188 | } | |
189 | ||
190 | static int | |
191 | test_pci_cleanup(void) | |
192 | { | |
193 | struct rte_pci_device *dev; | |
194 | struct rte_pci_driver *dr; | |
195 | const struct resource *r; | |
196 | int ret; | |
197 | ||
198 | unsetenv("SYSFS_PCI_DEVICES"); | |
199 | ||
200 | r = resource_find("test_pci_sysfs"); | |
201 | TEST_ASSERT_NOT_NULL(r, "missing resource test_pci_sysfs"); | |
202 | ||
203 | ret = resource_rm_by_tar(r); | |
204 | TEST_ASSERT_SUCCESS(ret, "Failed to delete resource %s", r->name); | |
205 | ||
206 | /* | |
207 | * FIXME: there is no API in DPDK to free a rte_pci_device so we | |
208 | * cannot free the devices in the right way. Let's assume that we | |
209 | * don't care for tests. | |
210 | */ | |
211 | while (!TAILQ_EMPTY(&pci_device_list)) { | |
212 | dev = TAILQ_FIRST(&pci_device_list); | |
213 | TAILQ_REMOVE(&pci_device_list, dev, next); | |
214 | } | |
215 | ||
216 | /* Restore original devices & drivers lists */ | |
217 | while (!TAILQ_EMPTY(&real_pci_driver_list)) { | |
218 | dr = TAILQ_FIRST(&real_pci_driver_list); | |
219 | TAILQ_REMOVE(&real_pci_driver_list, dr, next); | |
220 | rte_eal_pci_register(dr); | |
221 | } | |
222 | ||
223 | while (!TAILQ_EMPTY(&real_pci_device_list)) { | |
224 | dev = TAILQ_FIRST(&real_pci_device_list); | |
225 | TAILQ_REMOVE(&real_pci_device_list, dev, next); | |
226 | TAILQ_INSERT_TAIL(&pci_device_list, dev, next); | |
227 | } | |
228 | ||
229 | return 0; | |
230 | } | |
231 | ||
232 | static int | |
233 | test_pci_blacklist(void) | |
234 | { | |
235 | struct rte_devargs_list save_devargs_list; | |
236 | ||
237 | printf("Dump all devices\n"); | |
238 | TEST_ASSERT(TAILQ_EMPTY(&pci_driver_list), | |
239 | "pci_driver_list not empty"); | |
240 | ||
241 | rte_eal_pci_register(&my_driver); | |
242 | rte_eal_pci_register(&my_driver2); | |
243 | ||
244 | pci_dev_count = 0; | |
245 | printf("Scan bus\n"); | |
246 | rte_eal_pci_probe(); | |
247 | ||
248 | if (pci_dev_count == 0) { | |
249 | printf("no device detected\n"); | |
250 | return -1; | |
251 | } | |
252 | ||
253 | /* save the real devargs_list */ | |
254 | save_devargs_list = devargs_list; | |
255 | TAILQ_INIT(&devargs_list); | |
256 | ||
257 | blacklist_all_devices(); | |
258 | ||
259 | pci_dev_count = 0; | |
260 | printf("Scan bus with all devices blacklisted\n"); | |
261 | rte_eal_pci_probe(); | |
262 | ||
263 | free_devargs_list(); | |
264 | devargs_list = save_devargs_list; | |
265 | ||
266 | if (pci_dev_count != 0) { | |
267 | printf("not all devices are blacklisted\n"); | |
268 | return -1; | |
269 | } | |
270 | ||
271 | test_pci_run = 1; | |
272 | ||
273 | rte_eal_pci_unregister(&my_driver); | |
274 | rte_eal_pci_unregister(&my_driver2); | |
275 | ||
276 | return 0; | |
277 | } | |
278 | ||
279 | static int test_pci_sysfs(void) | |
280 | { | |
281 | const char *orig; | |
282 | const char *newpath; | |
283 | int ret; | |
284 | ||
285 | orig = pci_get_sysfs_path(); | |
286 | ret = setenv("SYSFS_PCI_DEVICES", "My Documents", 1); | |
287 | TEST_ASSERT_SUCCESS(ret, "Failed setenv to My Documents"); | |
288 | ||
289 | newpath = pci_get_sysfs_path(); | |
290 | TEST_ASSERT(!strcmp(newpath, "My Documents"), | |
291 | "pci_get_sysfs_path() should return 'My Documents' " | |
292 | "but gives %s", newpath); | |
293 | ||
294 | ret = setenv("SYSFS_PCI_DEVICES", orig, 1); | |
295 | TEST_ASSERT_SUCCESS(ret, "Failed setenv back to '%s'", orig); | |
296 | ||
297 | newpath = pci_get_sysfs_path(); | |
298 | TEST_ASSERT(!strcmp(orig, newpath), | |
299 | "pci_get_sysfs_path returned unexpected path: " | |
300 | "%s (expected: %s)", newpath, orig); | |
301 | return 0; | |
302 | } | |
303 | ||
304 | int | |
305 | test_pci(void) | |
306 | { | |
307 | if (test_pci_sysfs()) | |
308 | return -1; | |
309 | ||
310 | if (test_pci_setup()) | |
311 | return -1; | |
312 | ||
313 | if (test_pci_blacklist()) | |
314 | return -1; | |
315 | ||
316 | if (test_pci_cleanup()) | |
317 | return -1; | |
318 | ||
319 | return 0; | |
320 | } | |
321 | ||
322 | REGISTER_TEST_COMMAND(pci_autotest, test_pci); |