xref: /aosp_15_r20/external/ltp/testcases/kernel/device-drivers/pci/tpci_user/tpci.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
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 <[email protected]>
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 	tst_sig(FORK, DEF_HANDLER, cleanup);
55 }
56 
run_pci_testcases(int bus,int slot)57 static void run_pci_testcases(int bus, int slot)
58 {
59 	int i, res;
60 	for (i = 0; i < TST_TOTAL; ++i) {
61 		/* skip pci disable test-case, it is manual */
62 		if (i == PCI_DISABLE)
63 			continue;
64 
65 		SAFE_FILE_PRINTF(cleanup, dev_tcase, "%d", i);
66 		SAFE_FILE_SCANF(cleanup, dev_result, "%d", &res);
67 
68 		tst_resm(res, "PCI bus %02x slot %02x : Test-case '%d'",
69 			bus, slot, i);
70 	}
71 }
72 
test_run(void)73 static void test_run(void)
74 {
75 	tst_module_load(cleanup, module_name, NULL);
76 	module_loaded = 1;
77 
78 	char buf[6];
79 	int i, j, fd, count;
80 
81 	for (i = 0; i < MAX_BUS; ++i) {
82 		for (j = 0; j < MAX_DEVFN; ++j) {
83 			/* set pci device for the test */
84 			fd = SAFE_OPEN(cleanup, dev_busslot, O_WRONLY);
85 			count = snprintf(buf, 6, "%u", i << 8 | j);
86 			errno = 0;
87 			if (write(fd, buf, count) < 0) {
88 				if (errno == ENODEV) {
89 					SAFE_CLOSE(cleanup, fd);
90 					continue;
91 				}
92 				tst_brkm(TBROK | TERRNO, cleanup,
93 					"write to '%s' failed", dev_busslot);
94 			}
95 			SAFE_CLOSE(cleanup, fd);
96 
97 			run_pci_testcases(i, j);
98 
99 		}
100 	}
101 }
102 
main(void)103 int main(void)
104 {
105 	setup();
106 
107 	test_run();
108 
109 	cleanup();
110 
111 	tst_exit();
112 }
113