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