From patchwork Wed May 31 09:54:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marco Pagani X-Patchwork-Id: 101318 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp2768015vqr; Wed, 31 May 2023 03:18:31 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4o7jCrWa34G5OkiTtUpEHzeQte14vZMvIcnj9alMY+yr+YJPdCU4+rcS0uYJkFJSuLHnEh X-Received: by 2002:a92:d5c2:0:b0:335:1ed:359b with SMTP id d2-20020a92d5c2000000b0033501ed359bmr2010279ilq.15.1685528310852; Wed, 31 May 2023 03:18:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685528310; cv=none; d=google.com; s=arc-20160816; b=Z+CmF3DqcoOPIOr8A6NHwXgCEEY9JhQkxcj7fTBZxsV2X+txH1HcaUqbnXYLz0Ik7u PV3U3zqor/EkaC+1r6lWaaTvVvVV92WlpLfitapdgHtXwioWy9mg5r/F+3Fkoxyd0LNq Va9mkgISKkXXUxX8+d+FS5ODofDunwztP4s2togFdRQTzx6cgy8EkohBqc6fLEV43Mp1 AO+93aynR/2Hf3Rp76itI/LiFh+W4my/VcY7Hr5hQbBMk/G6JxOeVxB4KUq3iPtDvHAN eNJ9dbTw/oRwKOpUh5WQaiTvQQwYa3JvymwOGg7TbjoT0QQmK7xM8C62aUqThcutcaTf v42w== 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=N4+A4nutXompnLSFNP6hePxbga6fySP2AhvC5oSdcgc=; b=mNkLEE8Iu7LKxUyfT3qAfS3YiRULR4Q+68B79cmMwwBdJdjOaKMDSAJL4/uDWSI8ji 4ryf36aZke3/m9JSoNcwrI47qGuapgUFMwH133d79qxBDc9SdbQgebTG2bPXddwagMoH aMdaoHDFKgcM+XHa54cRjqvRxoAYPfcep4xRr/CSgdqzmlLOcTTz9y1mUmCZeaB0voS4 UuhxD9Wk1OBXoHyCFY+cpNu+6cMf1oGyUoi9JZZXyBQ5F+qV9MP9ne4rBYkrv58UFJJy b5TdOSoj1wH2bpSmkIQkbBli/w3iysnqoegW6fsh+LKwLZhH1aE+j0uLk7DiZfi2QH64 JI9A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Nw2AoPHu; 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 y20-20020a170902b49400b001ab13c7505asi651649plr.125.2023.05.31.03.18.17; Wed, 31 May 2023 03:18:30 -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=Nw2AoPHu; 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 S235669AbjEaJzJ (ORCPT + 99 others); Wed, 31 May 2023 05:55:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50616 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232671AbjEaJzG (ORCPT ); Wed, 31 May 2023 05:55:06 -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 9614810B for ; Wed, 31 May 2023 02:54:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1685526861; 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=N4+A4nutXompnLSFNP6hePxbga6fySP2AhvC5oSdcgc=; b=Nw2AoPHudCv8Ni5YkpFuEZqX7ocq1wmUTBS2qzCLIQ4F0yJ81ynz7fxZTSMk7xf4g7ue5i k45pgg8B/oVN06d+AQtMxDrnRiW5FdbesoemIrY1heLTauEXYgMoKycIQq+SNI6r5k/GZF MUrzgGkTKZJJikOSt3LBo3keZSgTPPk= 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-423-g-K-fWQ3OdS4d6Q7zSAdNg-1; Wed, 31 May 2023 05:54:20 -0400 X-MC-Unique: g-K-fWQ3OdS4d6Q7zSAdNg-1 Received: by mail-qk1-f200.google.com with SMTP id af79cd13be357-75b07b420a5so895238885a.3 for ; Wed, 31 May 2023 02:54:20 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685526860; x=1688118860; 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=N4+A4nutXompnLSFNP6hePxbga6fySP2AhvC5oSdcgc=; b=lRlTib3Rn/5TWUBDuAviojMGtZiaEEFLNv2oyftXcVYk62j4NTA65wqqkH5N6zW/FP J36Eq1pOGOMAj6guYXfWT2Gj5rJpL7bs0hwkf+kUk9jjMpd79H+5MH5FzbEg4iDeSrAA fJn+hlIAqhwz058aPHnaVM9DweyFcMxOZZaC4d/XG8vQCNVroioha63/gdXTBgKIMV87 bo/fpZiCBgoOjmwR4BBLNPQlSer41S6KG2KMcptQVYM45FA3Rsi2uKDTAtfu0KS1zYKJ tYEYOSvlP+HTQjt6sU2PyaD5Vx3MqR+q46jzscJ+ve7kdL/QiTmM9LZL+bIPgJYj1ote tnmw== X-Gm-Message-State: AC+VfDzEyNj3wuUdy1JY7DwBx0goNrZLXSPvo4AYGq9EMAoMujCc17eq OPNnnwhg41JhL01X37PoaYaQ1OvCWnIMbARBfWd+Lo4Xo3g9jfMaIsNwaumIpV9PLx3MPTFKnAH aD3lHK0GbNT76ECBbcB8xfek= X-Received: by 2002:a05:620a:3e87:b0:75b:23a1:3619 with SMTP id tv7-20020a05620a3e8700b0075b23a13619mr4538331qkn.42.1685526859952; Wed, 31 May 2023 02:54:19 -0700 (PDT) X-Received: by 2002:a05:620a:3e87:b0:75b:23a1:3619 with SMTP id tv7-20020a05620a3e8700b0075b23a13619mr4538319qkn.42.1685526859697; Wed, 31 May 2023 02:54:19 -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.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 31 May 2023 02:54:19 -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 1/4] fpga: add an initial KUnit suite for the FPGA Manager Date: Wed, 31 May 2023 11:54:02 +0200 Message-Id: <20230531095405.342080-2-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?1767404534517321132?= X-GMAIL-MSGID: =?utf-8?q?1767404534517321132?= The suite tests the basic behaviors of the FPGA Manager, and the programming using a single contiguous buffer and scatter gather table. Signed-off-by: Marco Pagani --- drivers/fpga/tests/fpga-mgr-test.c | 289 +++++++++++++++++++++++++++++ 1 file changed, 289 insertions(+) create mode 100644 drivers/fpga/tests/fpga-mgr-test.c diff --git a/drivers/fpga/tests/fpga-mgr-test.c b/drivers/fpga/tests/fpga-mgr-test.c new file mode 100644 index 000000000000..c771ae4dd05e --- /dev/null +++ b/drivers/fpga/tests/fpga-mgr-test.c @@ -0,0 +1,289 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * KUnit test for the FPGA Manager + * + * Copyright (C) 2023 Red Hat, Inc. + * + * Author: Marco Pagani + */ + +#include +#include +#include +#include +#include +#include + +#define HEADER_MAGIC 'H' +#define IMAGE_MAGIC 'P' +#define IMAGE_BLOCK 1024 + +#define HEADER_SIZE IMAGE_BLOCK +#define IMAGE_SIZE (IMAGE_BLOCK * 4) + +struct mgr_stats { + bool header_match; + bool image_match; + u32 seq_num; + u32 op_parse_header_seq; + u32 op_write_init_seq; + u32 op_write_seq; + u32 op_write_sg_seq; + u32 op_write_complete_seq; + enum fpga_mgr_states op_parse_header_state; + enum fpga_mgr_states op_write_init_state; + enum fpga_mgr_states op_write_state; + enum fpga_mgr_states op_write_sg_state; + enum fpga_mgr_states op_write_complete_state; +}; + +struct mgr_ctx { + struct fpga_image_info *img_info; + struct fpga_manager *mgr; + struct platform_device *pdev; + struct mgr_stats stats; +}; + +static char *init_test_buffer(struct kunit *test, size_t count) +{ + char *buf; + size_t i; + + buf = kunit_kzalloc(test, count, GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, buf); + + for (i = 0; i < count; i++) + buf[i] = i < HEADER_SIZE ? HEADER_MAGIC : IMAGE_MAGIC; + + return buf; +} + +static int op_parse_header(struct fpga_manager *mgr, struct fpga_image_info *info, + const char *buf, size_t count) +{ + struct mgr_stats *stats = mgr->priv; + size_t i; + + /* Set header_size and data_size for later */ + info->header_size = HEADER_SIZE; + info->data_size = info->count - HEADER_SIZE; + + stats->header_match = true; + + /* Check header */ + for (i = 0; i < info->header_size; i++) + if (buf[i] != HEADER_MAGIC) + stats->header_match = false; + + stats->op_parse_header_state = mgr->state; + stats->op_parse_header_seq = stats->seq_num++; + + return 0; +} + +static int op_write_init(struct fpga_manager *mgr, struct fpga_image_info *info, + const char *buf, size_t count) +{ + struct mgr_stats *stats = mgr->priv; + + stats->op_write_init_state = mgr->state; + stats->op_write_init_seq = stats->seq_num++; + + return 0; +} + +static int op_write(struct fpga_manager *mgr, const char *buf, size_t count) +{ + struct mgr_stats *stats = mgr->priv; + size_t i; + + /* Check image */ + stats->image_match = true; + for (i = 0; i < count; i++) + if (buf[i] != IMAGE_MAGIC) + stats->image_match = false; + + stats->op_write_state = mgr->state; + stats->op_write_seq = stats->seq_num++; + + return 0; +} + +static int op_write_sg(struct fpga_manager *mgr, struct sg_table *sgt) +{ + struct mgr_stats *stats = mgr->priv; + struct scatterlist *sg; + char *img; + unsigned int si, i, j = 0; + + stats->image_match = true; + + /* Check image (write_sg will still get whole image in sg_table) */ + for_each_sgtable_sg(sgt, sg, si) { + img = sg_virt(sg); + for (i = 0; i < sg->length; i++) { + if (i + j > HEADER_SIZE && img[i] != IMAGE_MAGIC) + stats->image_match = false; + } + j += i; + } + + stats->op_write_sg_state = mgr->state; + stats->op_write_sg_seq = stats->seq_num++; + + return 0; +} + +static int op_write_complete(struct fpga_manager *mgr, struct fpga_image_info *info) +{ + struct mgr_stats *stats = mgr->priv; + + stats->op_write_complete_state = mgr->state; + stats->op_write_complete_seq = stats->seq_num++; + + return 0; +} + +static const struct fpga_manager_ops fake_mgr_ops = { + .skip_header = true, + .parse_header = op_parse_header, + .write_init = op_write_init, + .write = op_write, + .write_sg = op_write_sg, + .write_complete = op_write_complete, +}; + +static void fpga_mgr_test_get(struct kunit *test) +{ + struct mgr_ctx *ctx = test->priv; + struct fpga_manager *mgr; + + mgr = fpga_mgr_get(&ctx->pdev->dev); + KUNIT_EXPECT_PTR_EQ(test, mgr, ctx->mgr); + + fpga_mgr_put(ctx->mgr); +} + +static void fpga_mgr_test_lock(struct kunit *test) +{ + struct mgr_ctx *ctx = test->priv; + int ret; + + ret = fpga_mgr_lock(ctx->mgr); + KUNIT_EXPECT_EQ(test, ret, 0); + + ret = fpga_mgr_lock(ctx->mgr); + KUNIT_EXPECT_EQ(test, ret, -EBUSY); + + fpga_mgr_unlock(ctx->mgr); +} + +static void fpga_mgr_test_img_load_buf(struct kunit *test) +{ + struct mgr_ctx *ctx = test->priv; + char *img_buf; + int ret; + + img_buf = init_test_buffer(test, IMAGE_SIZE); + + ctx->img_info->count = IMAGE_SIZE; + ctx->img_info->buf = img_buf; + + ret = fpga_mgr_load(ctx->mgr, ctx->img_info); + KUNIT_EXPECT_EQ(test, ret, 0); + + KUNIT_EXPECT_TRUE(test, ctx->stats.header_match); + KUNIT_EXPECT_TRUE(test, ctx->stats.image_match); + + KUNIT_EXPECT_EQ(test, ctx->stats.op_parse_header_state, FPGA_MGR_STATE_PARSE_HEADER); + KUNIT_EXPECT_EQ(test, ctx->stats.op_write_init_state, FPGA_MGR_STATE_WRITE_INIT); + KUNIT_EXPECT_EQ(test, ctx->stats.op_write_state, FPGA_MGR_STATE_WRITE); + KUNIT_EXPECT_EQ(test, ctx->stats.op_write_complete_state, FPGA_MGR_STATE_WRITE_COMPLETE); + + KUNIT_EXPECT_EQ(test, ctx->stats.op_write_init_seq, ctx->stats.op_parse_header_seq + 1); + KUNIT_EXPECT_EQ(test, ctx->stats.op_write_seq, ctx->stats.op_parse_header_seq + 2); + KUNIT_EXPECT_EQ(test, ctx->stats.op_write_complete_seq, ctx->stats.op_parse_header_seq + 3); +} + +static void fpga_mgr_test_img_load_sgt(struct kunit *test) +{ + struct mgr_ctx *ctx = test->priv; + struct sg_table *sgt; + char *img_buf; + int ret; + + img_buf = init_test_buffer(test, IMAGE_SIZE); + + sgt = kunit_kzalloc(test, sizeof(*sgt), GFP_KERNEL); + ret = sg_alloc_table(sgt, 1, GFP_KERNEL); + KUNIT_ASSERT_EQ(test, ret, 0); + sg_init_one(sgt->sgl, img_buf, IMAGE_SIZE); + + ctx->img_info->sgt = sgt; + + ret = fpga_mgr_load(ctx->mgr, ctx->img_info); + KUNIT_EXPECT_EQ(test, ret, 0); + + KUNIT_EXPECT_TRUE(test, ctx->stats.header_match); + KUNIT_EXPECT_TRUE(test, ctx->stats.image_match); + + KUNIT_EXPECT_EQ(test, ctx->stats.op_parse_header_state, FPGA_MGR_STATE_PARSE_HEADER); + KUNIT_EXPECT_EQ(test, ctx->stats.op_write_init_state, FPGA_MGR_STATE_WRITE_INIT); + KUNIT_EXPECT_EQ(test, ctx->stats.op_write_sg_state, FPGA_MGR_STATE_WRITE); + KUNIT_EXPECT_EQ(test, ctx->stats.op_write_complete_state, FPGA_MGR_STATE_WRITE_COMPLETE); + + KUNIT_EXPECT_EQ(test, ctx->stats.op_write_init_seq, ctx->stats.op_parse_header_seq + 1); + KUNIT_EXPECT_EQ(test, ctx->stats.op_write_sg_seq, ctx->stats.op_parse_header_seq + 2); + KUNIT_EXPECT_EQ(test, ctx->stats.op_write_complete_seq, ctx->stats.op_parse_header_seq + 3); + + sg_free_table(ctx->img_info->sgt); +} + +static int fpga_mgr_test_init(struct kunit *test) +{ + struct mgr_ctx *ctx; + + ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx); + + ctx->pdev = platform_device_register_simple("mgr_pdev", PLATFORM_DEVID_AUTO, NULL, 0); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx->pdev); + + ctx->mgr = devm_fpga_mgr_register(&ctx->pdev->dev, "Fake FPGA Manager", &fake_mgr_ops, + &ctx->stats); + KUNIT_ASSERT_FALSE(test, IS_ERR_OR_NULL(ctx->mgr)); + + ctx->img_info = fpga_image_info_alloc(&ctx->pdev->dev); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx->img_info); + + test->priv = ctx; + + return 0; +} + +static void fpga_mgr_test_exit(struct kunit *test) +{ + struct mgr_ctx *ctx = test->priv; + + fpga_image_info_free(ctx->img_info); + platform_device_unregister(ctx->pdev); +} + +static struct kunit_case fpga_mgr_test_cases[] = { + KUNIT_CASE(fpga_mgr_test_get), + KUNIT_CASE(fpga_mgr_test_lock), + KUNIT_CASE(fpga_mgr_test_img_load_buf), + KUNIT_CASE(fpga_mgr_test_img_load_sgt), + {} +}; + +static struct kunit_suite fpga_mgr_suite = { + .name = "fpga_mgr", + .init = fpga_mgr_test_init, + .exit = fpga_mgr_test_exit, + .test_cases = fpga_mgr_test_cases, +}; + +kunit_test_suite(fpga_mgr_suite); + +MODULE_LICENSE("GPL");