1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * KUnit test for the FPGA Bridge
4  *
5  * Copyright (C) 2023 Red Hat, Inc.
6  *
7  * Author: Marco Pagani <marpagan@redhat.com>
8  */
9 
10 #include <kunit/test.h>
11 #include <linux/device.h>
12 #include <linux/fpga/fpga-bridge.h>
13 #include <linux/module.h>
14 #include <linux/types.h>
15 
16 struct bridge_stats {
17 	bool enable;
18 };
19 
20 struct bridge_ctx {
21 	struct fpga_bridge *bridge;
22 	struct platform_device *pdev;
23 	struct bridge_stats stats;
24 };
25 
26 static int op_enable_set(struct fpga_bridge *bridge, bool enable)
27 {
28 	struct bridge_stats *stats = bridge->priv;
29 
30 	stats->enable = enable;
31 
32 	return 0;
33 }
34 
35 /*
36  * Fake FPGA bridge that implements only the enable_set op to track
37  * the state.
38  */
39 static const struct fpga_bridge_ops fake_bridge_ops = {
40 	.enable_set = op_enable_set,
41 };
42 
43 /**
44  * register_test_bridge() - Register a fake FPGA bridge for testing.
45  * @test: KUnit test context object.
46  *
47  * Return: Context of the newly registered FPGA bridge.
48  */
49 static struct bridge_ctx *register_test_bridge(struct kunit *test)
50 {
51 	struct bridge_ctx *ctx;
52 
53 	ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
54 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx);
55 
56 	ctx->pdev = platform_device_register_simple("bridge_pdev", PLATFORM_DEVID_AUTO, NULL, 0);
57 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx->pdev);
58 
59 	ctx->bridge = fpga_bridge_register(&ctx->pdev->dev, "Fake FPGA bridge", &fake_bridge_ops,
60 					   &ctx->stats);
61 	KUNIT_ASSERT_FALSE(test, IS_ERR_OR_NULL(ctx->bridge));
62 
63 	return ctx;
64 }
65 
66 static void unregister_test_bridge(struct bridge_ctx *ctx)
67 {
68 	fpga_bridge_unregister(ctx->bridge);
69 	platform_device_unregister(ctx->pdev);
70 }
71 
72 static void fpga_bridge_test_get(struct kunit *test)
73 {
74 	struct bridge_ctx *ctx = test->priv;
75 	struct fpga_bridge *bridge;
76 
77 	bridge = fpga_bridge_get(&ctx->pdev->dev, NULL);
78 	KUNIT_EXPECT_PTR_EQ(test, bridge, ctx->bridge);
79 
80 	bridge = fpga_bridge_get(&ctx->pdev->dev, NULL);
81 	KUNIT_EXPECT_EQ(test, PTR_ERR(bridge), -EBUSY);
82 
83 	fpga_bridge_put(ctx->bridge);
84 }
85 
86 static void fpga_bridge_test_toggle(struct kunit *test)
87 {
88 	struct bridge_ctx *ctx = test->priv;
89 	int ret;
90 
91 	ret = fpga_bridge_disable(ctx->bridge);
92 	KUNIT_EXPECT_EQ(test, ret, 0);
93 	KUNIT_EXPECT_FALSE(test, ctx->stats.enable);
94 
95 	ret = fpga_bridge_enable(ctx->bridge);
96 	KUNIT_EXPECT_EQ(test, ret, 0);
97 	KUNIT_EXPECT_TRUE(test, ctx->stats.enable);
98 }
99 
100 /* Test the functions for getting and controlling a list of bridges */
101 static void fpga_bridge_test_get_put_list(struct kunit *test)
102 {
103 	struct list_head bridge_list;
104 	struct bridge_ctx *ctx_0, *ctx_1;
105 	int ret;
106 
107 	ctx_0 = test->priv;
108 	ctx_1 = register_test_bridge(test);
109 
110 	INIT_LIST_HEAD(&bridge_list);
111 
112 	/* Get bridge 0 and add it to the list */
113 	ret = fpga_bridge_get_to_list(&ctx_0->pdev->dev, NULL, &bridge_list);
114 	KUNIT_EXPECT_EQ(test, ret, 0);
115 
116 	KUNIT_EXPECT_PTR_EQ(test, ctx_0->bridge,
117 			    list_first_entry_or_null(&bridge_list, struct fpga_bridge, node));
118 
119 	/* Get bridge 1 and add it to the list */
120 	ret = fpga_bridge_get_to_list(&ctx_1->pdev->dev, NULL, &bridge_list);
121 	KUNIT_EXPECT_EQ(test, ret, 0);
122 
123 	KUNIT_EXPECT_PTR_EQ(test, ctx_1->bridge,
124 			    list_first_entry_or_null(&bridge_list, struct fpga_bridge, node));
125 
126 	/* Disable an then enable both bridges from the list */
127 	ret = fpga_bridges_disable(&bridge_list);
128 	KUNIT_EXPECT_EQ(test, ret, 0);
129 
130 	KUNIT_EXPECT_FALSE(test, ctx_0->stats.enable);
131 	KUNIT_EXPECT_FALSE(test, ctx_1->stats.enable);
132 
133 	ret = fpga_bridges_enable(&bridge_list);
134 	KUNIT_EXPECT_EQ(test, ret, 0);
135 
136 	KUNIT_EXPECT_TRUE(test, ctx_0->stats.enable);
137 	KUNIT_EXPECT_TRUE(test, ctx_1->stats.enable);
138 
139 	/* Put and remove both bridges from the list */
140 	fpga_bridges_put(&bridge_list);
141 
142 	KUNIT_EXPECT_TRUE(test, list_empty(&bridge_list));
143 
144 	unregister_test_bridge(ctx_1);
145 }
146 
147 static int fpga_bridge_test_init(struct kunit *test)
148 {
149 	test->priv = register_test_bridge(test);
150 
151 	return 0;
152 }
153 
154 static void fpga_bridge_test_exit(struct kunit *test)
155 {
156 	unregister_test_bridge(test->priv);
157 }
158 
159 static struct kunit_case fpga_bridge_test_cases[] = {
160 	KUNIT_CASE(fpga_bridge_test_get),
161 	KUNIT_CASE(fpga_bridge_test_toggle),
162 	KUNIT_CASE(fpga_bridge_test_get_put_list),
163 	{}
164 };
165 
166 static struct kunit_suite fpga_bridge_suite = {
167 	.name = "fpga_bridge",
168 	.init = fpga_bridge_test_init,
169 	.exit = fpga_bridge_test_exit,
170 	.test_cases = fpga_bridge_test_cases,
171 };
172 
173 kunit_test_suite(fpga_bridge_suite);
174 
175 MODULE_LICENSE("GPL");
176