From patchwork Fri Jun 30 15:25:05 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marco Pagani X-Patchwork-Id: 114775 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp10455503vqr; Fri, 30 Jun 2023 08:29:33 -0700 (PDT) X-Google-Smtp-Source: APBJJlFoQNMYVfxnxefAIjjvpbkkmRR+YXn9REY9LFy6i/tEvzzW2h3H6+pUTtWcEUZQ2zpi33DU X-Received: by 2002:a05:6a00:b90:b0:666:c1ab:d6e5 with SMTP id g16-20020a056a000b9000b00666c1abd6e5mr4834179pfj.16.1688138973447; Fri, 30 Jun 2023 08:29:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1688138973; cv=none; d=google.com; s=arc-20160816; b=i8SjffZkbV+RKMvs/qvXGjBTp8BFxXwi5prPe+uuwuSrp921PMz1AfAqzDE9JBOd4d R3k3jWATMZ9znoAO0m2x6JdcUMRxKxf2/TmIGfDMc1agDLflgvKtkAgW1CJhBI77nhWE fzUkrvIeXDGW6R+LnE870J1h/L17sENzXTxB+1eIwavK2Agr+d1xVE0w0aKzH1C6KgM1 NYPiJJ0XDEafOW9kKcHzQiPhgiaR3J23G3/1S63+XJ3hTk50pVumeDZHPZkbDl+tAVTb CTgTxuEaoHYM02z0ZnBM6yvbstel303vSzORLhdvvpkvCKun/TD/tmo3rRrTBkJfscyU qeHQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=YDJvffpbrPgNmPKLqywH3LAm8S873ODrHn5dEekELQE=; fh=vL6Moim7ujQ71QOBXYMaaPnG1N9WYgskJqrZSTruGY4=; b=g4wwHWnNodDwFjwte0sTb8K0aD67mz+4kD8R/a30wZOfa8YCfOGwdHh/xTNsSjGXIZ y2+lpBHfXSEZiPtKJsepnlPqQg929uO/epJXYZ//Uz0oe4qn8whisRgl7tWa1WJCFQCf vPCc8kk9zE13AuxFNE3jdfMDlmmBOoEeLD0bveRhT/ATTsewr+dm7e0c9IPt3PReHdw4 Tz/z5W9QqRdQ3+B/D9qVsUR0RaslIl1xMLbGpfdyrT1cv0B+5+BekLVSU5GNBmWqdL2N IL2alfOtM0cI9yzqU0Wb1zp3WMLcPGBSfHhP2CoslQvA+nZOmPDMnETFh2FUoZyQ99P3 FzoQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=an2KLj6G; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id b12-20020aa78ecc000000b006826f351306si38608pfr.222.2023.06.30.08.29.21; Fri, 30 Jun 2023 08:29:33 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=an2KLj6G; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232977AbjF3P0j (ORCPT + 99 others); Fri, 30 Jun 2023 11:26:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42882 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232809AbjF3P02 (ORCPT ); Fri, 30 Jun 2023 11:26:28 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 481BD3C10 for ; Fri, 30 Jun 2023 08:25:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1688138737; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=YDJvffpbrPgNmPKLqywH3LAm8S873ODrHn5dEekELQE=; b=an2KLj6Gnc9/MlRE7mlpFS6T7GryuLi0gIVwPN+8nTMLb2oH38Vg13wHknXiSnvxQfaWwX oAQiFy8bNkVxL/evs1VnTuMGO6OFcoeR+Hzc2z1IcTqdfdc6Lw+xESKBVfocgp2z0yeifM G1YLKK/AT9U++oKMwnseAZ0fVFQkoow= Received: from mail-lf1-f70.google.com (mail-lf1-f70.google.com [209.85.167.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-241-_scam52eM9erYnoM1SRttg-1; Fri, 30 Jun 2023 11:25:36 -0400 X-MC-Unique: _scam52eM9erYnoM1SRttg-1 Received: by mail-lf1-f70.google.com with SMTP id 2adb3069b0e04-4fb736a7746so2029981e87.3 for ; Fri, 30 Jun 2023 08:25:36 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1688138735; x=1690730735; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=YDJvffpbrPgNmPKLqywH3LAm8S873ODrHn5dEekELQE=; b=GR2BF9J13TTc5AfuP/+jsDDGIgr2GKJRH1fmGWyB+KVKmTvcRtN7GtvtwJqXx8AEC6 BRMAjCCJZsrkb5gKsRvhSaP7OKwIpUWj9DD69VmVQj0iZJvigMwxGqexEToyiMFIKM2b fxT+bWVOrJv6VxeTlq3cQZ3ACVsLVzLyBPone2J2xOBW+u0xNejnik5IpSG+SqM+lnFF 6HrLrJHcxL6SMSW1pblp9Cd0j3TqB+gSsSMsV6caVhBL5pQo3tqYQla9OKW7iD0/fIzq HhZy5C7UgJbi22Lh5nyHn2vphhZA+8Toa6a+vDXIdiI2WVxK4h3bRX2whjNr+a8uOVbR QI0w== X-Gm-Message-State: ABy/qLYxrKwE+gubQDQ1Mrtc6vTmniKTbgBSzgWt5FhXwskxYWTAfWda xjz6N6aGhe4S++xxLAuq5Yu0YNxEWmo84kvappgObBGTmAh/YDhy1cABFZne8EZEmdiALpeO/v5 mYN5OeSgqJ3KaFjxjljkOm7s= X-Received: by 2002:a05:6512:312a:b0:4f8:587e:cbe0 with SMTP id p10-20020a056512312a00b004f8587ecbe0mr2451977lfd.52.1688138734744; Fri, 30 Jun 2023 08:25:34 -0700 (PDT) X-Received: by 2002:a05:6512:312a:b0:4f8:587e:cbe0 with SMTP id p10-20020a056512312a00b004f8587ecbe0mr2451970lfd.52.1688138734545; Fri, 30 Jun 2023 08:25:34 -0700 (PDT) Received: from klayman.station (net-2-34-24-242.cust.vodafonedsl.it. [2.34.24.242]) by smtp.gmail.com with ESMTPSA id k24-20020aa7c398000000b0051def9be785sm813230edq.85.2023.06.30.08.25.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 30 Jun 2023 08:25:34 -0700 (PDT) From: Marco Pagani To: Moritz Fischer , Wu Hao , Xu Yilun , Tom Rix Cc: Marco Pagani , linux-kernel@vger.kernel.org, linux-fpga@vger.kernel.org Subject: [PATCH v8 2/4] fpga: add an initial KUnit suite for the FPGA Bridge Date: Fri, 30 Jun 2023 17:25:05 +0200 Message-ID: <20230630152507.30219-3-marpagan@redhat.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230630152507.30219-1-marpagan@redhat.com> References: <20230630152507.30219-1-marpagan@redhat.com> MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1770142012311589031?= X-GMAIL-MSGID: =?utf-8?q?1770142012311589031?= The suite tests the basic behaviors of the FPGA Bridge including the functions that operate on a list of bridges. Signed-off-by: Marco Pagani Acked-by: Xu Yilun --- drivers/fpga/tests/fpga-bridge-test.c | 175 ++++++++++++++++++++++++++ 1 file changed, 175 insertions(+) create mode 100644 drivers/fpga/tests/fpga-bridge-test.c diff --git a/drivers/fpga/tests/fpga-bridge-test.c b/drivers/fpga/tests/fpga-bridge-test.c new file mode 100644 index 000000000000..c69d451fee88 --- /dev/null +++ b/drivers/fpga/tests/fpga-bridge-test.c @@ -0,0 +1,175 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * KUnit test for the FPGA Bridge + * + * Copyright (C) 2023 Red Hat, Inc. + * + * Author: Marco Pagani + */ + +#include +#include +#include +#include +#include + +struct bridge_stats { + bool enable; +}; + +struct bridge_ctx { + struct fpga_bridge *bridge; + struct platform_device *pdev; + struct bridge_stats stats; +}; + +static int op_enable_set(struct fpga_bridge *bridge, bool enable) +{ + struct bridge_stats *stats = bridge->priv; + + stats->enable = enable; + + return 0; +} + +/* + * Fake Bridge that implements only the enable_set op to track + * the state. + */ +static const struct fpga_bridge_ops fake_bridge_ops = { + .enable_set = op_enable_set, +}; + +/** + * register_test_bridge() - Register a fake Bridge for testing. + * @test: KUnit test context object. + * + * Return: Context of the newly registered Bridge. + */ +static struct bridge_ctx *register_test_bridge(struct kunit *test) +{ + struct bridge_ctx *ctx; + + ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx); + + ctx->pdev = platform_device_register_simple("bridge_pdev", PLATFORM_DEVID_AUTO, NULL, 0); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx->pdev); + + ctx->bridge = fpga_bridge_register(&ctx->pdev->dev, "Fake FPGA Bridge", &fake_bridge_ops, + &ctx->stats); + KUNIT_ASSERT_FALSE(test, IS_ERR_OR_NULL(ctx->bridge)); + + return ctx; +} + +static void unregister_test_bridge(struct bridge_ctx *ctx) +{ + fpga_bridge_unregister(ctx->bridge); + platform_device_unregister(ctx->pdev); +} + +static void fpga_bridge_test_get(struct kunit *test) +{ + struct bridge_ctx *ctx = test->priv; + struct fpga_bridge *bridge; + + bridge = fpga_bridge_get(&ctx->pdev->dev, NULL); + KUNIT_EXPECT_PTR_EQ(test, bridge, ctx->bridge); + + bridge = fpga_bridge_get(&ctx->pdev->dev, NULL); + KUNIT_EXPECT_EQ(test, PTR_ERR(bridge), -EBUSY); + + fpga_bridge_put(ctx->bridge); +} + +static void fpga_bridge_test_toggle(struct kunit *test) +{ + struct bridge_ctx *ctx = test->priv; + int ret; + + ret = fpga_bridge_disable(ctx->bridge); + KUNIT_EXPECT_EQ(test, ret, 0); + KUNIT_EXPECT_FALSE(test, ctx->stats.enable); + + ret = fpga_bridge_enable(ctx->bridge); + KUNIT_EXPECT_EQ(test, ret, 0); + KUNIT_EXPECT_TRUE(test, ctx->stats.enable); +} + +/* Test the functions for getting and controlling a list of bridges */ +static void fpga_bridge_test_get_put_list(struct kunit *test) +{ + struct list_head bridge_list; + struct bridge_ctx *ctx_0, *ctx_1; + int ret; + + ctx_0 = test->priv; + ctx_1 = register_test_bridge(test); + + INIT_LIST_HEAD(&bridge_list); + + /* Get bridge 0 and add it to the list */ + ret = fpga_bridge_get_to_list(&ctx_0->pdev->dev, NULL, &bridge_list); + KUNIT_EXPECT_EQ(test, ret, 0); + + KUNIT_EXPECT_PTR_EQ(test, ctx_0->bridge, + list_first_entry_or_null(&bridge_list, struct fpga_bridge, node)); + + /* Get bridge 1 and add it to the list */ + ret = fpga_bridge_get_to_list(&ctx_1->pdev->dev, NULL, &bridge_list); + KUNIT_EXPECT_EQ(test, ret, 0); + + KUNIT_EXPECT_PTR_EQ(test, ctx_1->bridge, + list_first_entry_or_null(&bridge_list, struct fpga_bridge, node)); + + /* Disable an then enable both bridges from the list */ + ret = fpga_bridges_disable(&bridge_list); + KUNIT_EXPECT_EQ(test, ret, 0); + + KUNIT_EXPECT_FALSE(test, ctx_0->stats.enable); + KUNIT_EXPECT_FALSE(test, ctx_1->stats.enable); + + ret = fpga_bridges_enable(&bridge_list); + KUNIT_EXPECT_EQ(test, ret, 0); + + KUNIT_EXPECT_TRUE(test, ctx_0->stats.enable); + KUNIT_EXPECT_TRUE(test, ctx_1->stats.enable); + + /* Put and remove both bridges from the list */ + fpga_bridges_put(&bridge_list); + + KUNIT_EXPECT_TRUE(test, list_empty(&bridge_list)); + + unregister_test_bridge(ctx_1); +} + +static int fpga_bridge_test_init(struct kunit *test) +{ + test->priv = register_test_bridge(test); + + return 0; +} + +static void fpga_bridge_test_exit(struct kunit *test) +{ + unregister_test_bridge(test->priv); +} + +static struct kunit_case fpga_bridge_test_cases[] = { + KUNIT_CASE(fpga_bridge_test_get), + KUNIT_CASE(fpga_bridge_test_toggle), + KUNIT_CASE(fpga_bridge_test_get_put_list), + {} +}; + +static struct kunit_suite fpga_bridge_suite = { + .name = "fpga_bridge", + .init = fpga_bridge_test_init, + .exit = fpga_bridge_test_exit, + .test_cases = fpga_bridge_test_cases, +}; + +kunit_test_suite(fpga_bridge_suite); + +MODULE_LICENSE("GPL");