From patchwork Wed May 31 09:54:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marco Pagani X-Patchwork-Id: 101309 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp2759241vqr; Wed, 31 May 2023 03:02:21 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4NFmjA2gcSUFORKbISpbpDf6puzMwIsyYipW3nON4hvfZYXBUY5tmqZucZ6VCxVxURVfy8 X-Received: by 2002:a17:902:988d:b0:1a6:45e5:a26a with SMTP id s13-20020a170902988d00b001a645e5a26amr3134371plp.27.1685527340627; Wed, 31 May 2023 03:02:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685527340; cv=none; d=google.com; s=arc-20160816; b=LKhW13AS+Tt8ST3+A5r716IHs/SXNKzmyo1P9cB6PkMgPLMIwJzGH1zeGOeTN8nxnb tiUy6PM+MDii5Ok0wC0YlWibCmuxi/teF+cUSJRqOcPnCaytFzf2/+Vg4t+VdHn8pYi7 xDR9uKMzOC/eShnaB196kfrG6GgEoX4Amhe6n5HTeb9cYkr0KGwvgIIMJRDMQDq38Qsj RI2t/M1gq7lcq6HatdFRGyCT6tVq5nh3PuablapKfQ/sVT3jF60x2q1p8O56I30PA5IZ nOj+AtCU/qC5+LMxg6vkkHYNDCEXDjYIiwTpV2Y2Pj6uyG32FPYNpd0IVvhd3qfwKwLO XgwA== 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=YtryeNC+vu63BT9xv/2HgRKYlLCkC92odnSUBOKH9oY=; b=i6G/ikXCwaedH3bSRidz1tPVSvMxIjRJSDUmFyxRddz7towTjIkCFgjk87bYbftKXy T72KPU5h/J+JbcfulYx8Cn0K149MFE397VXF9uvyJr4thSY1TXRGgANXZ0/39M9fHn21 we/ivCn79bIghbAcHGI3LezzslF6nuUn7yqcrzogZ1sUD4aejhd0MWDebn81UvMS+U5v bWEbTL9MhjAEWSDFHj2c6FX74icPsVD57SHmNweNJKLu6Fn75MXR2Y7MOnhPB3EEPzyQ XtiN+rAmSNqagf15c6n+JpUY1ySOsLBUf+ZPJ2t6nPl9D8ggjHrNgSaX4iIK7Tarb4zk 1IQw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=L1Nz+jbJ; 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 o15-20020a170902d4cf00b001a974fc86casi622048plg.592.2023.05.31.03.02.06; Wed, 31 May 2023 03:02:20 -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=L1Nz+jbJ; 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 S235685AbjEaJz0 (ORCPT + 99 others); Wed, 31 May 2023 05:55:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50662 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235664AbjEaJzR (ORCPT ); Wed, 31 May 2023 05:55:17 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C451711C for ; Wed, 31 May 2023 02:54:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1685526871; 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=YtryeNC+vu63BT9xv/2HgRKYlLCkC92odnSUBOKH9oY=; b=L1Nz+jbJG6i0Sl1jO42PkDkGI8lrj9539CBmmsDJQ4kmtQahzKX5keTq6uafXLkYEkEz2n HVxzimJSkjOrVyJ4S74GgqCBtVdzxuTu5GUjUP3ZLUJZzT2v2LwTQzEBY1jEeC9pEuyjKt +Zpv33zRVwCnbLO71EGNFqy/8HupU20= Received: from mail-qk1-f200.google.com (mail-qk1-f200.google.com [209.85.222.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-632-9UHZ_965Ms2OHvWYInGSxA-1; Wed, 31 May 2023 05:54:29 -0400 X-MC-Unique: 9UHZ_965Ms2OHvWYInGSxA-1 Received: by mail-qk1-f200.google.com with SMTP id af79cd13be357-75b06a31daaso870508185a.2 for ; Wed, 31 May 2023 02:54:29 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685526869; x=1688118869; 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=YtryeNC+vu63BT9xv/2HgRKYlLCkC92odnSUBOKH9oY=; b=W1r78DZKgu4K774S5nsKAFAAb6Yd8WEIIRlk7iIPdtlt4/AVCyWlNQnJBcp+szeJc5 3mg9eTVu1YN8dwCisMSvux19E9t8ELBY/3GHyuk6UFAx7jIj2LUT/ZrtbF+hf4h4QcLf M63ZGZtMjN51KUuN6eLLaHWc05c8++U3r4RWN9ndAR4ER/8RuqFyhw0iKHW18dg87wlQ g2jMpcUPskLZcXdFFgKiqw+1/LHdHxsT9MpZ/gy0vqsZrYBtZNSaiRl7boEqTRKciqaP dilHCwLJ6rYAsMl7utHA3rKdSk19amxKUR+CcH7vpqCPSG3QGotU08DfpE2TdElzQxNI mtrg== X-Gm-Message-State: AC+VfDw7JMhqz8fH+9uwNIFl3gVRYUhJQDyeixtUg72pW6F9nArFk4HV oLFOQMMgEq0mudO3I+4AAEJw/jbksdzZWfPXGrBMCx1Pdfz0qOfS+ihWEXdTvwVI+Os7aKHoM1h 2AM+iFsLedxyKtCnIMejabPQ= X-Received: by 2002:a05:620a:838f:b0:75b:23a0:deaf with SMTP id pb15-20020a05620a838f00b0075b23a0deafmr5146146qkn.45.1685526868873; Wed, 31 May 2023 02:54:28 -0700 (PDT) X-Received: by 2002:a05:620a:838f:b0:75b:23a0:deaf with SMTP id pb15-20020a05620a838f00b0075b23a0deafmr5146133qkn.45.1685526868593; Wed, 31 May 2023 02:54:28 -0700 (PDT) Received: from klayman.redhat.com (net-2-34-28-169.cust.vodafonedsl.it. [2.34.28.169]) by smtp.gmail.com with ESMTPSA id k25-20020a05620a139900b0074def53eca5sm4902404qki.53.2023.05.31.02.54.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 31 May 2023 02:54:28 -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: [RFC PATCH v6 2/4] fpga: add an initial KUnit suite for the FPGA Bridge Date: Wed, 31 May 2023 11:54:03 +0200 Message-Id: <20230531095405.342080-3-marpagan@redhat.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230531095405.342080-1-marpagan@redhat.com> References: <20230531095405.342080-1-marpagan@redhat.com> MIME-Version: 1.0 X-Spam-Status: No, score=-2.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, 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?1767403516907519446?= X-GMAIL-MSGID: =?utf-8?q?1767403516907519446?= The suite tests the basic behaviors of the FPGA Bridge and the functions that operate on a list of Bridges. Signed-off-by: Marco Pagani --- drivers/fpga/tests/fpga-bridge-test.c | 164 ++++++++++++++++++++++++++ 1 file changed, 164 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..52c82f76a92c --- /dev/null +++ b/drivers/fpga/tests/fpga-bridge-test.c @@ -0,0 +1,164 @@ +// 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; +} + +static const struct fpga_bridge_ops fake_bridge_ops = { + .enable_set = op_enable_set +}; + +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); +} + +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");