1 // SPDX-License-Identifier: GPL-2.0
3 * KUnit test for the FPGA Manager
5 * Copyright (C) 2023 Red Hat, Inc.
7 * Author: Marco Pagani <marpagan@redhat.com>
10 #include <kunit/device.h>
11 #include <kunit/test.h>
12 #include <linux/fpga/fpga-mgr.h>
13 #include <linux/module.h>
14 #include <linux/scatterlist.h>
15 #include <linux/types.h>
17 #define HEADER_FILL 'H'
18 #define IMAGE_FILL 'P'
19 #define IMAGE_BLOCK 1024
21 #define HEADER_SIZE IMAGE_BLOCK
22 #define IMAGE_SIZE (IMAGE_BLOCK * 4)
28 u32 op_parse_header_seq
;
29 u32 op_write_init_seq
;
32 u32 op_write_complete_seq
;
33 enum fpga_mgr_states op_parse_header_state
;
34 enum fpga_mgr_states op_write_init_state
;
35 enum fpga_mgr_states op_write_state
;
36 enum fpga_mgr_states op_write_sg_state
;
37 enum fpga_mgr_states op_write_complete_state
;
41 struct fpga_image_info
*img_info
;
42 struct fpga_manager
*mgr
;
44 struct mgr_stats stats
;
48 * Wrappers to avoid cast warnings when passing action functions directly
49 * to kunit_add_action().
51 KUNIT_DEFINE_ACTION_WRAPPER(sg_free_table_wrapper
, sg_free_table
,
54 KUNIT_DEFINE_ACTION_WRAPPER(fpga_image_info_free_wrapper
, fpga_image_info_free
,
55 struct fpga_image_info
*);
58 * init_test_buffer() - Allocate and initialize a test image in a buffer.
59 * @test: KUnit test context object.
60 * @count: image size in bytes.
62 * Return: pointer to the newly allocated image.
64 static char *init_test_buffer(struct kunit
*test
, size_t count
)
68 KUNIT_ASSERT_GE(test
, count
, HEADER_SIZE
);
70 buf
= kunit_kzalloc(test
, count
, GFP_KERNEL
);
71 KUNIT_ASSERT_NOT_ERR_OR_NULL(test
, buf
);
73 memset(buf
, HEADER_FILL
, HEADER_SIZE
);
74 memset(buf
+ HEADER_SIZE
, IMAGE_FILL
, count
- HEADER_SIZE
);
80 * Check the image header. Do not return an error code if the image check fails
81 * since, in this case, it is a failure of the FPGA manager itself, not this
84 static int op_parse_header(struct fpga_manager
*mgr
, struct fpga_image_info
*info
,
85 const char *buf
, size_t count
)
87 struct mgr_stats
*stats
= mgr
->priv
;
90 stats
->op_parse_header_state
= mgr
->state
;
91 stats
->op_parse_header_seq
= stats
->seq_num
++;
93 /* Set header_size and data_size for later */
94 info
->header_size
= HEADER_SIZE
;
95 info
->data_size
= info
->count
- HEADER_SIZE
;
97 stats
->header_match
= true;
98 for (i
= 0; i
< info
->header_size
; i
++) {
99 if (buf
[i
] != HEADER_FILL
) {
100 stats
->header_match
= false;
108 static int op_write_init(struct fpga_manager
*mgr
, struct fpga_image_info
*info
,
109 const char *buf
, size_t count
)
111 struct mgr_stats
*stats
= mgr
->priv
;
113 stats
->op_write_init_state
= mgr
->state
;
114 stats
->op_write_init_seq
= stats
->seq_num
++;
120 * Check the image data. As with op_parse_header, do not return an error code
121 * if the image check fails.
123 static int op_write(struct fpga_manager
*mgr
, const char *buf
, size_t count
)
125 struct mgr_stats
*stats
= mgr
->priv
;
128 stats
->op_write_state
= mgr
->state
;
129 stats
->op_write_seq
= stats
->seq_num
++;
131 stats
->image_match
= true;
132 for (i
= 0; i
< count
; i
++) {
133 if (buf
[i
] != IMAGE_FILL
) {
134 stats
->image_match
= false;
143 * Check the image data, but first skip the header since write_sg will get
144 * the whole image in sg_table. As with op_parse_header, do not return an
145 * error code if the image check fails.
147 static int op_write_sg(struct fpga_manager
*mgr
, struct sg_table
*sgt
)
149 struct mgr_stats
*stats
= mgr
->priv
;
150 struct sg_mapping_iter miter
;
154 stats
->op_write_sg_state
= mgr
->state
;
155 stats
->op_write_sg_seq
= stats
->seq_num
++;
157 stats
->image_match
= true;
158 sg_miter_start(&miter
, sgt
->sgl
, sgt
->nents
, SG_MITER_FROM_SG
);
160 if (!sg_miter_skip(&miter
, HEADER_SIZE
)) {
161 stats
->image_match
= false;
165 while (sg_miter_next(&miter
)) {
167 for (i
= 0; i
< miter
.length
; i
++) {
168 if (img
[i
] != IMAGE_FILL
) {
169 stats
->image_match
= false;
175 sg_miter_stop(&miter
);
179 static int op_write_complete(struct fpga_manager
*mgr
, struct fpga_image_info
*info
)
181 struct mgr_stats
*stats
= mgr
->priv
;
183 stats
->op_write_complete_state
= mgr
->state
;
184 stats
->op_write_complete_seq
= stats
->seq_num
++;
190 * Fake FPGA manager that implements all ops required to check the programming
191 * sequence using a single contiguous buffer and a scatter gather table.
193 static const struct fpga_manager_ops fake_mgr_ops
= {
195 .parse_header
= op_parse_header
,
196 .write_init
= op_write_init
,
198 .write_sg
= op_write_sg
,
199 .write_complete
= op_write_complete
,
202 static void fpga_mgr_test_get(struct kunit
*test
)
204 struct mgr_ctx
*ctx
= test
->priv
;
205 struct fpga_manager
*mgr
;
207 mgr
= fpga_mgr_get(ctx
->dev
);
208 KUNIT_EXPECT_PTR_EQ(test
, mgr
, ctx
->mgr
);
210 fpga_mgr_put(ctx
->mgr
);
213 static void fpga_mgr_test_lock(struct kunit
*test
)
215 struct mgr_ctx
*ctx
= test
->priv
;
218 ret
= fpga_mgr_lock(ctx
->mgr
);
219 KUNIT_EXPECT_EQ(test
, ret
, 0);
221 ret
= fpga_mgr_lock(ctx
->mgr
);
222 KUNIT_EXPECT_EQ(test
, ret
, -EBUSY
);
224 fpga_mgr_unlock(ctx
->mgr
);
227 /* Check the programming sequence using an image in a buffer */
228 static void fpga_mgr_test_img_load_buf(struct kunit
*test
)
230 struct mgr_ctx
*ctx
= test
->priv
;
234 img_buf
= init_test_buffer(test
, IMAGE_SIZE
);
236 ctx
->img_info
->count
= IMAGE_SIZE
;
237 ctx
->img_info
->buf
= img_buf
;
239 ret
= fpga_mgr_load(ctx
->mgr
, ctx
->img_info
);
240 KUNIT_EXPECT_EQ(test
, ret
, 0);
242 KUNIT_EXPECT_TRUE(test
, ctx
->stats
.header_match
);
243 KUNIT_EXPECT_TRUE(test
, ctx
->stats
.image_match
);
245 KUNIT_EXPECT_EQ(test
, ctx
->stats
.op_parse_header_state
, FPGA_MGR_STATE_PARSE_HEADER
);
246 KUNIT_EXPECT_EQ(test
, ctx
->stats
.op_write_init_state
, FPGA_MGR_STATE_WRITE_INIT
);
247 KUNIT_EXPECT_EQ(test
, ctx
->stats
.op_write_state
, FPGA_MGR_STATE_WRITE
);
248 KUNIT_EXPECT_EQ(test
, ctx
->stats
.op_write_complete_state
, FPGA_MGR_STATE_WRITE_COMPLETE
);
250 KUNIT_EXPECT_EQ(test
, ctx
->stats
.op_write_init_seq
, ctx
->stats
.op_parse_header_seq
+ 1);
251 KUNIT_EXPECT_EQ(test
, ctx
->stats
.op_write_seq
, ctx
->stats
.op_parse_header_seq
+ 2);
252 KUNIT_EXPECT_EQ(test
, ctx
->stats
.op_write_complete_seq
, ctx
->stats
.op_parse_header_seq
+ 3);
255 /* Check the programming sequence using an image in a scatter gather table */
256 static void fpga_mgr_test_img_load_sgt(struct kunit
*test
)
258 struct mgr_ctx
*ctx
= test
->priv
;
259 struct sg_table
*sgt
;
263 img_buf
= init_test_buffer(test
, IMAGE_SIZE
);
265 sgt
= kunit_kzalloc(test
, sizeof(*sgt
), GFP_KERNEL
);
266 ret
= sg_alloc_table(sgt
, 1, GFP_KERNEL
);
267 KUNIT_ASSERT_EQ(test
, ret
, 0);
268 sg_init_one(sgt
->sgl
, img_buf
, IMAGE_SIZE
);
270 ret
= kunit_add_action_or_reset(test
, sg_free_table_wrapper
, sgt
);
271 KUNIT_ASSERT_EQ(test
, ret
, 0);
273 ctx
->img_info
->sgt
= sgt
;
275 ret
= fpga_mgr_load(ctx
->mgr
, ctx
->img_info
);
276 KUNIT_EXPECT_EQ(test
, ret
, 0);
278 KUNIT_EXPECT_TRUE(test
, ctx
->stats
.header_match
);
279 KUNIT_EXPECT_TRUE(test
, ctx
->stats
.image_match
);
281 KUNIT_EXPECT_EQ(test
, ctx
->stats
.op_parse_header_state
, FPGA_MGR_STATE_PARSE_HEADER
);
282 KUNIT_EXPECT_EQ(test
, ctx
->stats
.op_write_init_state
, FPGA_MGR_STATE_WRITE_INIT
);
283 KUNIT_EXPECT_EQ(test
, ctx
->stats
.op_write_sg_state
, FPGA_MGR_STATE_WRITE
);
284 KUNIT_EXPECT_EQ(test
, ctx
->stats
.op_write_complete_state
, FPGA_MGR_STATE_WRITE_COMPLETE
);
286 KUNIT_EXPECT_EQ(test
, ctx
->stats
.op_write_init_seq
, ctx
->stats
.op_parse_header_seq
+ 1);
287 KUNIT_EXPECT_EQ(test
, ctx
->stats
.op_write_sg_seq
, ctx
->stats
.op_parse_header_seq
+ 2);
288 KUNIT_EXPECT_EQ(test
, ctx
->stats
.op_write_complete_seq
, ctx
->stats
.op_parse_header_seq
+ 3);
291 static int fpga_mgr_test_init(struct kunit
*test
)
296 ctx
= kunit_kzalloc(test
, sizeof(*ctx
), GFP_KERNEL
);
297 KUNIT_ASSERT_NOT_ERR_OR_NULL(test
, ctx
);
299 ctx
->dev
= kunit_device_register(test
, "fpga-manager-test-dev");
300 KUNIT_ASSERT_NOT_ERR_OR_NULL(test
, ctx
->dev
);
302 ctx
->mgr
= devm_fpga_mgr_register(ctx
->dev
, "Fake FPGA Manager", &fake_mgr_ops
,
304 KUNIT_ASSERT_FALSE(test
, IS_ERR_OR_NULL(ctx
->mgr
));
306 ctx
->img_info
= fpga_image_info_alloc(ctx
->dev
);
307 KUNIT_ASSERT_NOT_ERR_OR_NULL(test
, ctx
->img_info
);
309 ret
= kunit_add_action_or_reset(test
, fpga_image_info_free_wrapper
, ctx
->img_info
);
310 KUNIT_ASSERT_EQ(test
, ret
, 0);
317 static struct kunit_case fpga_mgr_test_cases
[] = {
318 KUNIT_CASE(fpga_mgr_test_get
),
319 KUNIT_CASE(fpga_mgr_test_lock
),
320 KUNIT_CASE(fpga_mgr_test_img_load_buf
),
321 KUNIT_CASE(fpga_mgr_test_img_load_sgt
),
325 static struct kunit_suite fpga_mgr_suite
= {
327 .init
= fpga_mgr_test_init
,
328 .test_cases
= fpga_mgr_test_cases
,
331 kunit_test_suite(fpga_mgr_suite
);
333 MODULE_LICENSE("GPL");