162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * KUnit test for the FPGA Bridge 462306a36Sopenharmony_ci * 562306a36Sopenharmony_ci * Copyright (C) 2023 Red Hat, Inc. 662306a36Sopenharmony_ci * 762306a36Sopenharmony_ci * Author: Marco Pagani <marpagan@redhat.com> 862306a36Sopenharmony_ci */ 962306a36Sopenharmony_ci 1062306a36Sopenharmony_ci#include <kunit/test.h> 1162306a36Sopenharmony_ci#include <linux/device.h> 1262306a36Sopenharmony_ci#include <linux/fpga/fpga-bridge.h> 1362306a36Sopenharmony_ci#include <linux/module.h> 1462306a36Sopenharmony_ci#include <linux/types.h> 1562306a36Sopenharmony_ci 1662306a36Sopenharmony_cistruct bridge_stats { 1762306a36Sopenharmony_ci bool enable; 1862306a36Sopenharmony_ci}; 1962306a36Sopenharmony_ci 2062306a36Sopenharmony_cistruct bridge_ctx { 2162306a36Sopenharmony_ci struct fpga_bridge *bridge; 2262306a36Sopenharmony_ci struct platform_device *pdev; 2362306a36Sopenharmony_ci struct bridge_stats stats; 2462306a36Sopenharmony_ci}; 2562306a36Sopenharmony_ci 2662306a36Sopenharmony_cistatic int op_enable_set(struct fpga_bridge *bridge, bool enable) 2762306a36Sopenharmony_ci{ 2862306a36Sopenharmony_ci struct bridge_stats *stats = bridge->priv; 2962306a36Sopenharmony_ci 3062306a36Sopenharmony_ci stats->enable = enable; 3162306a36Sopenharmony_ci 3262306a36Sopenharmony_ci return 0; 3362306a36Sopenharmony_ci} 3462306a36Sopenharmony_ci 3562306a36Sopenharmony_ci/* 3662306a36Sopenharmony_ci * Fake FPGA bridge that implements only the enable_set op to track 3762306a36Sopenharmony_ci * the state. 3862306a36Sopenharmony_ci */ 3962306a36Sopenharmony_cistatic const struct fpga_bridge_ops fake_bridge_ops = { 4062306a36Sopenharmony_ci .enable_set = op_enable_set, 4162306a36Sopenharmony_ci}; 4262306a36Sopenharmony_ci 4362306a36Sopenharmony_ci/** 4462306a36Sopenharmony_ci * register_test_bridge() - Register a fake FPGA bridge for testing. 4562306a36Sopenharmony_ci * @test: KUnit test context object. 4662306a36Sopenharmony_ci * 4762306a36Sopenharmony_ci * Return: Context of the newly registered FPGA bridge. 4862306a36Sopenharmony_ci */ 4962306a36Sopenharmony_cistatic struct bridge_ctx *register_test_bridge(struct kunit *test) 5062306a36Sopenharmony_ci{ 5162306a36Sopenharmony_ci struct bridge_ctx *ctx; 5262306a36Sopenharmony_ci 5362306a36Sopenharmony_ci ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL); 5462306a36Sopenharmony_ci KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx); 5562306a36Sopenharmony_ci 5662306a36Sopenharmony_ci ctx->pdev = platform_device_register_simple("bridge_pdev", PLATFORM_DEVID_AUTO, NULL, 0); 5762306a36Sopenharmony_ci KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx->pdev); 5862306a36Sopenharmony_ci 5962306a36Sopenharmony_ci ctx->bridge = fpga_bridge_register(&ctx->pdev->dev, "Fake FPGA bridge", &fake_bridge_ops, 6062306a36Sopenharmony_ci &ctx->stats); 6162306a36Sopenharmony_ci KUNIT_ASSERT_FALSE(test, IS_ERR_OR_NULL(ctx->bridge)); 6262306a36Sopenharmony_ci 6362306a36Sopenharmony_ci return ctx; 6462306a36Sopenharmony_ci} 6562306a36Sopenharmony_ci 6662306a36Sopenharmony_cistatic void unregister_test_bridge(struct bridge_ctx *ctx) 6762306a36Sopenharmony_ci{ 6862306a36Sopenharmony_ci fpga_bridge_unregister(ctx->bridge); 6962306a36Sopenharmony_ci platform_device_unregister(ctx->pdev); 7062306a36Sopenharmony_ci} 7162306a36Sopenharmony_ci 7262306a36Sopenharmony_cistatic void fpga_bridge_test_get(struct kunit *test) 7362306a36Sopenharmony_ci{ 7462306a36Sopenharmony_ci struct bridge_ctx *ctx = test->priv; 7562306a36Sopenharmony_ci struct fpga_bridge *bridge; 7662306a36Sopenharmony_ci 7762306a36Sopenharmony_ci bridge = fpga_bridge_get(&ctx->pdev->dev, NULL); 7862306a36Sopenharmony_ci KUNIT_EXPECT_PTR_EQ(test, bridge, ctx->bridge); 7962306a36Sopenharmony_ci 8062306a36Sopenharmony_ci bridge = fpga_bridge_get(&ctx->pdev->dev, NULL); 8162306a36Sopenharmony_ci KUNIT_EXPECT_EQ(test, PTR_ERR(bridge), -EBUSY); 8262306a36Sopenharmony_ci 8362306a36Sopenharmony_ci fpga_bridge_put(ctx->bridge); 8462306a36Sopenharmony_ci} 8562306a36Sopenharmony_ci 8662306a36Sopenharmony_cistatic void fpga_bridge_test_toggle(struct kunit *test) 8762306a36Sopenharmony_ci{ 8862306a36Sopenharmony_ci struct bridge_ctx *ctx = test->priv; 8962306a36Sopenharmony_ci int ret; 9062306a36Sopenharmony_ci 9162306a36Sopenharmony_ci ret = fpga_bridge_disable(ctx->bridge); 9262306a36Sopenharmony_ci KUNIT_EXPECT_EQ(test, ret, 0); 9362306a36Sopenharmony_ci KUNIT_EXPECT_FALSE(test, ctx->stats.enable); 9462306a36Sopenharmony_ci 9562306a36Sopenharmony_ci ret = fpga_bridge_enable(ctx->bridge); 9662306a36Sopenharmony_ci KUNIT_EXPECT_EQ(test, ret, 0); 9762306a36Sopenharmony_ci KUNIT_EXPECT_TRUE(test, ctx->stats.enable); 9862306a36Sopenharmony_ci} 9962306a36Sopenharmony_ci 10062306a36Sopenharmony_ci/* Test the functions for getting and controlling a list of bridges */ 10162306a36Sopenharmony_cistatic void fpga_bridge_test_get_put_list(struct kunit *test) 10262306a36Sopenharmony_ci{ 10362306a36Sopenharmony_ci struct list_head bridge_list; 10462306a36Sopenharmony_ci struct bridge_ctx *ctx_0, *ctx_1; 10562306a36Sopenharmony_ci int ret; 10662306a36Sopenharmony_ci 10762306a36Sopenharmony_ci ctx_0 = test->priv; 10862306a36Sopenharmony_ci ctx_1 = register_test_bridge(test); 10962306a36Sopenharmony_ci 11062306a36Sopenharmony_ci INIT_LIST_HEAD(&bridge_list); 11162306a36Sopenharmony_ci 11262306a36Sopenharmony_ci /* Get bridge 0 and add it to the list */ 11362306a36Sopenharmony_ci ret = fpga_bridge_get_to_list(&ctx_0->pdev->dev, NULL, &bridge_list); 11462306a36Sopenharmony_ci KUNIT_EXPECT_EQ(test, ret, 0); 11562306a36Sopenharmony_ci 11662306a36Sopenharmony_ci KUNIT_EXPECT_PTR_EQ(test, ctx_0->bridge, 11762306a36Sopenharmony_ci list_first_entry_or_null(&bridge_list, struct fpga_bridge, node)); 11862306a36Sopenharmony_ci 11962306a36Sopenharmony_ci /* Get bridge 1 and add it to the list */ 12062306a36Sopenharmony_ci ret = fpga_bridge_get_to_list(&ctx_1->pdev->dev, NULL, &bridge_list); 12162306a36Sopenharmony_ci KUNIT_EXPECT_EQ(test, ret, 0); 12262306a36Sopenharmony_ci 12362306a36Sopenharmony_ci KUNIT_EXPECT_PTR_EQ(test, ctx_1->bridge, 12462306a36Sopenharmony_ci list_first_entry_or_null(&bridge_list, struct fpga_bridge, node)); 12562306a36Sopenharmony_ci 12662306a36Sopenharmony_ci /* Disable an then enable both bridges from the list */ 12762306a36Sopenharmony_ci ret = fpga_bridges_disable(&bridge_list); 12862306a36Sopenharmony_ci KUNIT_EXPECT_EQ(test, ret, 0); 12962306a36Sopenharmony_ci 13062306a36Sopenharmony_ci KUNIT_EXPECT_FALSE(test, ctx_0->stats.enable); 13162306a36Sopenharmony_ci KUNIT_EXPECT_FALSE(test, ctx_1->stats.enable); 13262306a36Sopenharmony_ci 13362306a36Sopenharmony_ci ret = fpga_bridges_enable(&bridge_list); 13462306a36Sopenharmony_ci KUNIT_EXPECT_EQ(test, ret, 0); 13562306a36Sopenharmony_ci 13662306a36Sopenharmony_ci KUNIT_EXPECT_TRUE(test, ctx_0->stats.enable); 13762306a36Sopenharmony_ci KUNIT_EXPECT_TRUE(test, ctx_1->stats.enable); 13862306a36Sopenharmony_ci 13962306a36Sopenharmony_ci /* Put and remove both bridges from the list */ 14062306a36Sopenharmony_ci fpga_bridges_put(&bridge_list); 14162306a36Sopenharmony_ci 14262306a36Sopenharmony_ci KUNIT_EXPECT_TRUE(test, list_empty(&bridge_list)); 14362306a36Sopenharmony_ci 14462306a36Sopenharmony_ci unregister_test_bridge(ctx_1); 14562306a36Sopenharmony_ci} 14662306a36Sopenharmony_ci 14762306a36Sopenharmony_cistatic int fpga_bridge_test_init(struct kunit *test) 14862306a36Sopenharmony_ci{ 14962306a36Sopenharmony_ci test->priv = register_test_bridge(test); 15062306a36Sopenharmony_ci 15162306a36Sopenharmony_ci return 0; 15262306a36Sopenharmony_ci} 15362306a36Sopenharmony_ci 15462306a36Sopenharmony_cistatic void fpga_bridge_test_exit(struct kunit *test) 15562306a36Sopenharmony_ci{ 15662306a36Sopenharmony_ci unregister_test_bridge(test->priv); 15762306a36Sopenharmony_ci} 15862306a36Sopenharmony_ci 15962306a36Sopenharmony_cistatic struct kunit_case fpga_bridge_test_cases[] = { 16062306a36Sopenharmony_ci KUNIT_CASE(fpga_bridge_test_get), 16162306a36Sopenharmony_ci KUNIT_CASE(fpga_bridge_test_toggle), 16262306a36Sopenharmony_ci KUNIT_CASE(fpga_bridge_test_get_put_list), 16362306a36Sopenharmony_ci {} 16462306a36Sopenharmony_ci}; 16562306a36Sopenharmony_ci 16662306a36Sopenharmony_cistatic struct kunit_suite fpga_bridge_suite = { 16762306a36Sopenharmony_ci .name = "fpga_bridge", 16862306a36Sopenharmony_ci .init = fpga_bridge_test_init, 16962306a36Sopenharmony_ci .exit = fpga_bridge_test_exit, 17062306a36Sopenharmony_ci .test_cases = fpga_bridge_test_cases, 17162306a36Sopenharmony_ci}; 17262306a36Sopenharmony_ci 17362306a36Sopenharmony_cikunit_test_suite(fpga_bridge_suite); 17462306a36Sopenharmony_ci 17562306a36Sopenharmony_ciMODULE_LICENSE("GPL"); 176