1 /*
2 * Copyright (c) 2013 Oracle and/or its affiliates. All Rights Reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of
7 * the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 *
18 * Author: Alexey Kodanev <alexey.kodanev@oracle.com>
19 *
20 */
21
22 #define _GNU_SOURCE
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <string.h>
27 #include <errno.h>
28
29 #include "test.h"
30 #include "safe_macros.h"
31 #include "old_module.h"
32
33 #include "../tpci_kernel/tpci.h"
34
35 char *TCID = "test_pci";
36 int TST_TOTAL = PCI_TCASES_NUM;
37
38 static const char module_name[] = PCI_DEVICE_NAME ".ko";
39 static const char dev_result[] = "/sys/devices/" PCI_DEVICE_NAME "/result";
40 static const char dev_tcase[] = "/sys/devices/" PCI_DEVICE_NAME "/tcase";
41 static const char dev_busslot[] = "/sys/devices/" PCI_DEVICE_NAME "/bus_slot";
42 static int module_loaded;
43
cleanup(void)44 static void cleanup(void)
45 {
46 if (module_loaded)
47 tst_module_unload(NULL, module_name);
48 }
49
setup(void)50 void setup(void)
51 {
52 tst_require_root();
53
54 if (tst_kvercmp(2, 6, 0) < 0) {
55 tst_brkm(TCONF, NULL,
56 "Test must be run with kernel 2.6 or newer");
57 }
58
59 tst_sig(FORK, DEF_HANDLER, cleanup);
60 }
61
run_pci_testcases(int bus,int slot)62 static void run_pci_testcases(int bus, int slot)
63 {
64 int i, res;
65 for (i = 0; i < TST_TOTAL; ++i) {
66 /* skip pci disable test-case, it is manual */
67 if (i == PCI_DISABLE)
68 continue;
69
70 SAFE_FILE_PRINTF(cleanup, dev_tcase, "%d", i);
71 SAFE_FILE_SCANF(cleanup, dev_result, "%d", &res);
72
73 tst_resm(res, "PCI bus %02x slot %02x : Test-case '%d'",
74 bus, slot, i);
75 }
76 }
77
test_run(void)78 static void test_run(void)
79 {
80 tst_module_load(cleanup, module_name, NULL);
81 module_loaded = 1;
82
83 char buf[6];
84 int i, j, fd, count;
85
86 for (i = 0; i < MAX_BUS; ++i) {
87 for (j = 0; j < MAX_DEVFN; ++j) {
88 /* set pci device for the test */
89 fd = SAFE_OPEN(cleanup, dev_busslot, O_WRONLY);
90 count = snprintf(buf, 6, "%u", i << 8 | j);
91 errno = 0;
92 if (write(fd, buf, count) < 0) {
93 if (errno == ENODEV) {
94 SAFE_CLOSE(cleanup, fd);
95 continue;
96 }
97 tst_brkm(TBROK | TERRNO, cleanup,
98 "write to '%s' failed", dev_busslot);
99 }
100 SAFE_CLOSE(cleanup, fd);
101
102 run_pci_testcases(i, j);
103
104 }
105 }
106 }
107
main(void)108 int main(void)
109 {
110 setup();
111
112 test_run();
113
114 cleanup();
115
116 tst_exit();
117 }
118