From patchwork Mon Oct 15 09:20:57 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bin Meng X-Patchwork-Id: 983992 X-Patchwork-Delegate: sjg@chromium.org Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=lists.denx.de (client-ip=81.169.180.215; helo=lists.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="iIO7LLMB"; dkim-atps=neutral Received: from lists.denx.de (dione.denx.de [81.169.180.215]) by ozlabs.org (Postfix) with ESMTP id 42YXrv6QFsz9s7W for ; Mon, 15 Oct 2018 20:17:19 +1100 (AEDT) Received: by lists.denx.de (Postfix, from userid 105) id 48C0AC21F02; Mon, 15 Oct 2018 09:17:17 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on lists.denx.de X-Spam-Level: X-Spam-Status: No, score=-0.0 required=5.0 tests=FREEMAIL_FROM, RCVD_IN_DNSWL_BLOCKED, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, T_DKIM_INVALID autolearn=unavailable autolearn_force=no version=3.4.0 Received: from lists.denx.de (localhost [IPv6:::1]) by lists.denx.de (Postfix) with ESMTP id 5CC86C21DC1; Mon, 15 Oct 2018 09:16:37 +0000 (UTC) Received: by lists.denx.de (Postfix, from userid 105) id DE0EEC21BE5; Mon, 15 Oct 2018 09:16:34 +0000 (UTC) Received: from mail-pg1-f196.google.com (mail-pg1-f196.google.com [209.85.215.196]) by lists.denx.de (Postfix) with ESMTPS id 4B8CCC21C2C for ; Mon, 15 Oct 2018 09:16:34 +0000 (UTC) Received: by mail-pg1-f196.google.com with SMTP id f18-v6so8883597pgv.3 for ; Mon, 15 Oct 2018 02:16:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=nIeIB1a4jNG39Eo8IH2bgmLuT7lfURDWJrtkgWFktaM=; b=iIO7LLMBdM8kShImk8a4PsYqkdREhR9TtbnjS1xCfXFlVrNGTj0EcRwqBF45Z+PG1v yDfMnQdFrJJ7hT8K5dWFxM60hLxLd2iWY6yP6+UNVOi640fef7yDWWnNFfs/sPBjHTOM BAi0dnpjiqHJXPgHWq9S/BRaEtbBtEYIcTjjUxnnk0pmefTVWhL6SDnv9OHOCszCuN5K 25V4g/Pi6SjV64P6ZTME1Sl5R3wvzH9OUp/EJQpOe7eRoTkClsJ+p+LfdYwbXr4D8uHk LsNQf7+wJtkyxPC8bhFC6CNVkZBk8X6EVKbYEHU/6fkTyxZV1g0CbFKjxE5I6T1dGetV 1Sog== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=nIeIB1a4jNG39Eo8IH2bgmLuT7lfURDWJrtkgWFktaM=; b=avi7l+LOzwjog8ZZlGnMWCyMGIhnOz+mlVxtcsikNPrXKX6ZXV/AhBRkAlNqfYdrHD kJveaGqYsvnEDfWG17Few36zqfHjtgyGnqw/fbtC1qr7BGZvRc53OwyxwelqNUWh0TlU JIsSRk0JKo/ZgCVDJXc4OYEefTS2KogfOASvpjr1SCNS8LfRQlDiPL1WpCof9vkUmmKo fslUl/YfsTIS0nicEa6yBnwf0LiBtlZHCNbnY9kpZsMBIe3+UEl+CCDzdXdfJ6HgGZdX CqbZZWb5DzGROAJ1mOu6175w4Sagt25im9qOmU47fzaPAQ10+YhJLFhRdVonce6Gj79j 1KUg== X-Gm-Message-State: ABuFfohoduPWRg5lnTpmFQjqkT0hcKaIYhZJJo88/6eAG0PuREKhKamL pA7u/qP5RARpqujgDnD+zvc= X-Google-Smtp-Source: ACcGV61X6WQlFjSNR2bKFZz2FaRz1vmWd+M2sICXqDDyzuTJIwsYFod+qwgz/NsVGE8iC68HjEdAoA== X-Received: by 2002:a63:f848:: with SMTP id v8-v6mr15133036pgj.82.1539594992928; Mon, 15 Oct 2018 02:16:32 -0700 (PDT) Received: from ala-d2121-lx1.wrs.com (unknown-156-139.windriver.com. [147.11.156.139]) by smtp.gmail.com with ESMTPSA id a11-v6sm10798256pgw.54.2018.10.15.02.16.31 (version=TLS1_1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 15 Oct 2018 02:16:32 -0700 (PDT) From: Bin Meng To: Simon Glass , U-Boot Mailing List Date: Mon, 15 Oct 2018 02:20:57 -0700 Message-Id: <1539595287-31378-2-git-send-email-bmeng.cn@gmail.com> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: <1539595287-31378-1-git-send-email-bmeng.cn@gmail.com> References: <1539595287-31378-1-git-send-email-bmeng.cn@gmail.com> Subject: [U-Boot] [PATCH v3 01/31] dm: core: Allow uclass to set up a device's child after it is probed X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.18 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" Some buses need to set up their child devices after they are probed. Support a common child_post_probe() method for the uclass. With this change, the two APIs uclass_pre_probe_device() and uclass_post_probe_device() become symmetric. Signed-off-by: Bin Meng Reviewed-by: Simon Glass Reviewed-by: Simon Glass --- Changes in v3: None Changes in v2: None drivers/core/uclass.c | 13 ++++++++++++- include/dm/uclass.h | 4 +++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/drivers/core/uclass.c b/drivers/core/uclass.c index 3113d6a..3c7b9cf 100644 --- a/drivers/core/uclass.c +++ b/drivers/core/uclass.c @@ -687,8 +687,19 @@ int uclass_pre_probe_device(struct udevice *dev) int uclass_post_probe_device(struct udevice *dev) { - struct uclass_driver *uc_drv = dev->uclass->uc_drv; + struct uclass_driver *uc_drv; + int ret; + + if (dev->parent) { + uc_drv = dev->parent->uclass->uc_drv; + if (uc_drv->child_post_probe) { + ret = uc_drv->child_post_probe(dev); + if (ret) + return ret; + } + } + uc_drv = dev->uclass->uc_drv; if (uc_drv->post_probe) return uc_drv->post_probe(dev); diff --git a/include/dm/uclass.h b/include/dm/uclass.h index eebf2d5..4ef0d0f 100644 --- a/include/dm/uclass.h +++ b/include/dm/uclass.h @@ -61,7 +61,8 @@ struct udevice; * @post_probe: Called after a new device is probed * @pre_remove: Called before a device is removed * @child_post_bind: Called after a child is bound to a device in this uclass - * @child_pre_probe: Called before a child is probed in this uclass + * @child_pre_probe: Called before a child in this uclass is probed + * @child_post_probe: Called after a child in this uclass is probed * @init: Called to set up the uclass * @destroy: Called to destroy the uclass * @priv_auto_alloc_size: If non-zero this is the size of the private data @@ -94,6 +95,7 @@ struct uclass_driver { int (*pre_remove)(struct udevice *dev); int (*child_post_bind)(struct udevice *dev); int (*child_pre_probe)(struct udevice *dev); + int (*child_post_probe)(struct udevice *dev); int (*init)(struct uclass *class); int (*destroy)(struct uclass *class); int priv_auto_alloc_size; From patchwork Mon Oct 15 09:20:58 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bin Meng X-Patchwork-Id: 983994 X-Patchwork-Delegate: sjg@chromium.org Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=lists.denx.de (client-ip=81.169.180.215; helo=lists.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="SuTPOREC"; dkim-atps=neutral Received: from lists.denx.de (dione.denx.de [81.169.180.215]) by ozlabs.org (Postfix) with ESMTP id 42YXtl4yksz9s7W for ; Mon, 15 Oct 2018 20:18:55 +1100 (AEDT) Received: by lists.denx.de (Postfix, from userid 105) id 43D87C21E7D; Mon, 15 Oct 2018 09:17:40 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on lists.denx.de X-Spam-Level: X-Spam-Status: No, score=-0.0 required=5.0 tests=FREEMAIL_FROM, RCVD_IN_DNSWL_BLOCKED, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, T_DKIM_INVALID autolearn=unavailable autolearn_force=no version=3.4.0 Received: from lists.denx.de (localhost [IPv6:::1]) by lists.denx.de (Postfix) with ESMTP id 363EEC21DB5; Mon, 15 Oct 2018 09:16:39 +0000 (UTC) Received: by lists.denx.de (Postfix, from userid 105) id 60637C21D4A; Mon, 15 Oct 2018 09:16:36 +0000 (UTC) Received: from mail-pf1-f196.google.com (mail-pf1-f196.google.com [209.85.210.196]) by lists.denx.de (Postfix) with ESMTPS id B47DCC21BE5 for ; Mon, 15 Oct 2018 09:16:35 +0000 (UTC) Received: by mail-pf1-f196.google.com with SMTP id f29-v6so9368976pff.5 for ; Mon, 15 Oct 2018 02:16:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=l4UxZ1qIhAOU9htJXWOUDTdrht3Jal5rJghl5PRttng=; b=SuTPORECFyBJ0+mXRbkqsqqotigKwUVhi06kBU3+rG7ggn4i4p0S9ks3bxQeOBO6r9 HbUsF2+9shRA/M402p898Amy78aKIkgLmMFdOhzSbND32GMtP6zUeogDMSUM+bBBSUnt Tt/L1LPnvHbQIDoh//sybA5FLYMkzNRezUvP96+Zy7mtoX4EKlQcFRXDYiRSJl8vdSXT 6rD2O6vER9fDJQfr8bXgY/zJBZyOjDQrkfnMgio5IWf7up5g8NKT50Mj4YDaQVsSf+vh 3mpuRLeclwobZ6Ra0y1rc5aUT/5GrtpfAthX6hFau2fMU1uqC++688ozdBRRzbXib6y9 s3CA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=l4UxZ1qIhAOU9htJXWOUDTdrht3Jal5rJghl5PRttng=; b=YKw8WH+swswAYXFNYPpncLblXaG9J/fbz7THX9Fzy41CSiPiv90uUglNFSSFP0uBYD 81pgIE/gNcywrJnLeb6OU+gGfMXHts18aMWmy9Sy4Wy9rKbe8YS1XV9VeXFDr0pAuAnS 9V6t4xwpdLj2bKxMi1IOwsuRei78SqjN0595lhRjHQ1R5W9mlV+gxRN9fX5xjOoyV4pp TXm9RJ1FaNXMqBqiwbVe1m3BqmrQ1iljhCu4OefZB21LqLu4DSa0ijOjbQdMgFg3SHKl I3YeWnh5zWde9CHQ5bw4Kry1z60isKlPBTxa2fm1E5eESIwaKE+QOcO8FhrqcfJ0eUZ3 wWJg== X-Gm-Message-State: ABuFfogd90DUpY3/EPDoI+WtZ/rAkV2Ptw1BYeLLKB29A052qKgTqdFE PRmQu6MPnOzikQkI2ONsx3cmCRh7 X-Google-Smtp-Source: ACcGV61zy5Yp7TjHM5ts0DCnAHr50p4Mya7Njr4TtXWWM1TbU2unFhVU70HTSbgRzJgaO6kVgdyvNQ== X-Received: by 2002:a63:214d:: with SMTP id s13-v6mr15434323pgm.148.1539594994157; Mon, 15 Oct 2018 02:16:34 -0700 (PDT) Received: from ala-d2121-lx1.wrs.com (unknown-156-139.windriver.com. [147.11.156.139]) by smtp.gmail.com with ESMTPSA id a11-v6sm10798256pgw.54.2018.10.15.02.16.32 (version=TLS1_1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 15 Oct 2018 02:16:33 -0700 (PDT) From: Bin Meng To: Simon Glass , U-Boot Mailing List Date: Mon, 15 Oct 2018 02:20:58 -0700 Message-Id: <1539595287-31378-3-git-send-email-bmeng.cn@gmail.com> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: <1539595287-31378-1-git-send-email-bmeng.cn@gmail.com> References: <1539595287-31378-1-git-send-email-bmeng.cn@gmail.com> Subject: [U-Boot] [PATCH v3 02/31] test: dm: core: Add test case for uclass driver's child_post_probe() X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.18 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" Add test case to cover uclass driver's child_post_probe() method. Signed-off-by: Bin Meng Reviewed-by: Simon Glass Reviewed-by: Simon Glass --- Changes in v3: None Changes in v2: - new patch to add test case for uclass driver's child_post_probe() include/dm/test.h | 1 + test/dm/bus.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ test/dm/test-fdt.c | 7 +++++-- 3 files changed, 51 insertions(+), 2 deletions(-) diff --git a/include/dm/test.h b/include/dm/test.h index 83418eb..07385cd 100644 --- a/include/dm/test.h +++ b/include/dm/test.h @@ -69,6 +69,7 @@ struct dm_test_priv { int op_count[DM_TEST_OP_COUNT]; int uclass_flag; int uclass_total; + int uclass_postp; }; /** diff --git a/test/dm/bus.c b/test/dm/bus.c index d0cd5a0..93f3acd 100644 --- a/test/dm/bus.c +++ b/test/dm/bus.c @@ -63,6 +63,15 @@ static int testbus_child_pre_probe_uclass(struct udevice *dev) return 0; } +static int testbus_child_post_probe_uclass(struct udevice *dev) +{ + struct dm_test_priv *priv = dev_get_priv(dev); + + priv->uclass_postp++; + + return 0; +} + static int testbus_child_post_remove(struct udevice *dev) { struct dm_test_parent_data *parent_data = dev_get_parent_priv(dev); @@ -102,6 +111,7 @@ UCLASS_DRIVER(testbus) = { .id = UCLASS_TEST_BUS, .flags = DM_UC_FLAG_SEQ_ALIAS, .child_pre_probe = testbus_child_pre_probe_uclass, + .child_post_probe = testbus_child_post_probe_uclass, }; /* Test that we can probe for children */ @@ -547,3 +557,38 @@ static int dm_test_bus_child_pre_probe_uclass(struct unit_test_state *uts) } DM_TEST(dm_test_bus_child_pre_probe_uclass, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); + +/* + * Test that the bus' uclass' child_post_probe() is called after the + * device's probe() method + */ +static int dm_test_bus_child_post_probe_uclass(struct unit_test_state *uts) +{ + struct udevice *bus, *dev; + int child_count; + + /* + * See testfdt_drv_probe() which effectively initializes that + * the uclass postp flag is set to a value + */ + ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus)); + for (device_find_first_child(bus, &dev), child_count = 0; + dev; + device_find_next_child(&dev)) { + struct dm_test_priv *priv = dev_get_priv(dev); + + /* Check that things happened in the right order */ + ut_asserteq_ptr(NULL, priv); + ut_assertok(device_probe(dev)); + + priv = dev_get_priv(dev); + ut_assert(priv != NULL); + ut_asserteq(0, priv->uclass_postp); + child_count++; + } + ut_asserteq(3, child_count); + + return 0; +} +DM_TEST(dm_test_bus_child_post_probe_uclass, + DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); diff --git a/test/dm/test-fdt.c b/test/dm/test-fdt.c index 3da384f..e43acb2 100644 --- a/test/dm/test-fdt.c +++ b/test/dm/test-fdt.c @@ -55,10 +55,13 @@ static int testfdt_drv_probe(struct udevice *dev) /* * If this device is on a bus, the uclass_flag will be set before - * calling this function. This is used by - * dm_test_bus_child_pre_probe_uclass(). + * calling this function. In the meantime the uclass_postp is + * initlized to a value -1. These are used respectively by + * dm_test_bus_child_pre_probe_uclass() and + * dm_test_bus_child_post_probe_uclass(). */ priv->uclass_total += priv->uclass_flag; + priv->uclass_postp = -1; return 0; } From patchwork Mon Oct 15 09:20:59 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bin Meng X-Patchwork-Id: 983995 X-Patchwork-Delegate: sjg@chromium.org Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=lists.denx.de (client-ip=81.169.180.215; helo=lists.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="sn5wdCuR"; dkim-atps=neutral Received: from lists.denx.de (dione.denx.de [81.169.180.215]) by ozlabs.org (Postfix) with ESMTP id 42YXtx5WDLz9s7W for ; Mon, 15 Oct 2018 20:19:05 +1100 (AEDT) Received: by lists.denx.de (Postfix, from userid 105) id 7F532C21E57; Mon, 15 Oct 2018 09:18:08 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on lists.denx.de X-Spam-Level: X-Spam-Status: No, score=-0.0 required=5.0 tests=FREEMAIL_FROM, RCVD_IN_DNSWL_BLOCKED, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, T_DKIM_INVALID autolearn=unavailable autolearn_force=no version=3.4.0 Received: from lists.denx.de (localhost [IPv6:::1]) by lists.denx.de (Postfix) with ESMTP id 03633C21E3B; Mon, 15 Oct 2018 09:16:44 +0000 (UTC) Received: by lists.denx.de (Postfix, from userid 105) id DC3E6C21DA1; Mon, 15 Oct 2018 09:16:41 +0000 (UTC) Received: from mail-pg1-f196.google.com (mail-pg1-f196.google.com [209.85.215.196]) by lists.denx.de (Postfix) with ESMTPS id A6540C21D83 for ; Mon, 15 Oct 2018 09:16:36 +0000 (UTC) Received: by mail-pg1-f196.google.com with SMTP id f18-v6so8883640pgv.3 for ; Mon, 15 Oct 2018 02:16:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=EfwDRIslbEKKIGqb50z6OU8kxjjfRJ7YJ67jbE+1TpA=; b=sn5wdCuRzYhAFBSb0bCm5PEUQo/93GO1VqK7jP5pdcy6sxlPe6vWVeZjLNrzeH3nlT 9AHblNb3dUTPKyruLAbT/cfqEtVi7ZFJL0+efP3Vh0G6OFgaL+fEk8APv6TDsaXLSJEu VhlrWuOGG9ONyHMIUeGGJFEfyx+MWYYuRri3ZbK3WPMQ4KQlkwqThlOZzsohvOb6XT7f tss62REeUH2B34KedjabuJ4BLJYqXuKrtJeWpUfI9/UlUzTjsUF3ozPQdnw8LiuRBFO7 YruKE08mKRSJj0XcpO5xH0uq3FRlVGoV6URJY5USKCoIS9s50F7wJoiXYl69MrCXNYC4 zLIA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=EfwDRIslbEKKIGqb50z6OU8kxjjfRJ7YJ67jbE+1TpA=; b=BKSxs6iHfIRtjVz+9otdyjI7zFYujjzl5RgM4uGyV0YK2HKUiiWhVCrBBmzcwepfTe sEYMRNIcCNCQuawanD4sDE1hGZLKtTdalcVJWKxhSP0LDrgtUm/+0CPka6IbE9NY6W4L L4T4c/yfDiE5B5BI0swzOZ6OSXoQH30lh7nvxB7bP0rglhaHKr3w1+cGiFVsPM5SaYdh LEmyLy0CnEGGpuSQ4xzd1QLIj+gsEyA+wa6xwPJnmCt7+Y8/g72c+ogBmwnjJDisVq5z rxzDBYiB4r4qAQXbM7D4TNELQidD6Y9rzE87oNLR/+5VYWivTwCbLAe7E2g1q5F1ucRo F7Fw== X-Gm-Message-State: ABuFfojJirxFbrkkZhh9pO0HUvIzcCju09RwxAjeXJnMqqHqrgrcSz7T rnA6Pffn9c8I2IjEGEqFBkI= X-Google-Smtp-Source: ACcGV63thbtAcb8vMkT28RLqWQFU/3/zal4FeZSTEi5wpVZlAn5N88cfag4iE8/J51DwlI/1Yy4PLw== X-Received: by 2002:a63:7156:: with SMTP id b22-v6mr15166913pgn.342.1539594995297; Mon, 15 Oct 2018 02:16:35 -0700 (PDT) Received: from ala-d2121-lx1.wrs.com (unknown-156-139.windriver.com. [147.11.156.139]) by smtp.gmail.com with ESMTPSA id a11-v6sm10798256pgw.54.2018.10.15.02.16.34 (version=TLS1_1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 15 Oct 2018 02:16:34 -0700 (PDT) From: Bin Meng To: Simon Glass , U-Boot Mailing List Date: Mon, 15 Oct 2018 02:20:59 -0700 Message-Id: <1539595287-31378-4-git-send-email-bmeng.cn@gmail.com> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: <1539595287-31378-1-git-send-email-bmeng.cn@gmail.com> References: <1539595287-31378-1-git-send-email-bmeng.cn@gmail.com> Subject: [U-Boot] [PATCH v3 03/31] riscv: bootm: Add dm_remove_devices_flags() call to do_bootm_linux() X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.18 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" This adds a call to dm_remove_devices_flags() to do_bootm_linux() so that drivers that have one of the removal flags set (e.g. DM_FLAG_ACTIVE_DMA_REMOVE) in their driver struct, may do some last-stage cleanup before the OS is started. arm and x86 already did such, and we should do the same for riscv. Signed-off-by: Bin Meng Reviewed-by: Simon Glass Reviewed-by: Simon Glass --- Changes in v3: None Changes in v2: - new patch to add dm_remove_devices_flags() call to do_bootm_linux() for riscv bootm arch/riscv/lib/bootm.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/arch/riscv/lib/bootm.c b/arch/riscv/lib/bootm.c index a7a9fb9..2b5ccce 100644 --- a/arch/riscv/lib/bootm.c +++ b/arch/riscv/lib/bootm.c @@ -9,9 +9,11 @@ #include #include #include -#include #include #include +#include +#include +#include DECLARE_GLOBAL_DATA_PTR; @@ -57,6 +59,13 @@ int do_bootm_linux(int flag, int argc, char *argv[], bootm_headers_t *images) /* we assume that the kernel is in place */ printf("\nStarting kernel ...\n\n"); + /* + * Call remove function of all devices with a removal flag set. + * This may be useful for last-stage operations, like cancelling + * of DMA operation or releasing device internal buffers. + */ + dm_remove_devices_flags(DM_REMOVE_ACTIVE_ALL); + cleanup_before_linux(); if (IMAGE_ENABLE_OF_LIBFDT && images->ft_len) From patchwork Mon Oct 15 09:21:00 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Bin Meng X-Patchwork-Id: 984004 X-Patchwork-Delegate: sjg@chromium.org Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=lists.denx.de (client-ip=81.169.180.215; helo=lists.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="hcUP3U7Z"; dkim-atps=neutral Received: from lists.denx.de (dione.denx.de [81.169.180.215]) by ozlabs.org (Postfix) with ESMTP id 42YXzF0GK2z9s3Z for ; Mon, 15 Oct 2018 20:22:48 +1100 (AEDT) Received: by lists.denx.de (Postfix, from userid 105) id 73AC3C21E2C; Mon, 15 Oct 2018 09:21:26 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on lists.denx.de X-Spam-Level: X-Spam-Status: No, score=-0.0 required=5.0 tests=FREEMAIL_FROM, RCVD_IN_DNSWL_BLOCKED, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, T_DKIM_INVALID autolearn=unavailable autolearn_force=no version=3.4.0 Received: from lists.denx.de (localhost [IPv6:::1]) by lists.denx.de (Postfix) with ESMTP id 40DFCC21DCA; Mon, 15 Oct 2018 09:16:58 +0000 (UTC) Received: by lists.denx.de (Postfix, from userid 105) id E173DC21E62; Mon, 15 Oct 2018 09:16:46 +0000 (UTC) Received: from mail-pl1-f196.google.com (mail-pl1-f196.google.com [209.85.214.196]) by lists.denx.de (Postfix) with ESMTPS id 7CE83C21DF9 for ; Mon, 15 Oct 2018 09:16:38 +0000 (UTC) Received: by mail-pl1-f196.google.com with SMTP id q19-v6so5919110pll.5 for ; Mon, 15 Oct 2018 02:16:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=OELAsiqcTxkHj9awsKPHuZL9X4pzSlot4RPv6Vd/KOM=; b=hcUP3U7ZjnHHoLSoASKX+kBfQa8Nf+XAydgyju7xsXgS7lhejeiwEgw1tOvRFprspV 0bqrbQc62mH4+D3kPggO//BSWvP7UI1bn6SHWktt2MUADn8DMFGUHynBSKZLDKI29A+2 UhCWkpiXyaR+q0o5wcO5EbVE2+tO+WRUXMqj3RGK9+1GE3/dIcEMFraWmfsSPIsNWEA4 b4R0xC5zP68KSTU59Cqfw3jBpRg7V/jvHpXbCQFOwSUyPXCGOhWOQqG5/ZE3Q/Qwmg1O YYVHFLFl4QYqgktkHV8KP3GAt+Kj4yS0+2xr0b3rqXu2991YLSCE2sWjz/oWkIU+j7kB 8U5g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=OELAsiqcTxkHj9awsKPHuZL9X4pzSlot4RPv6Vd/KOM=; b=kyco4/slGNHBsYp1SFDCZg3RH3jREpW1UI5+tM9fM+RL8YyGWqrOxdcY7elXQPV691 pzMLVPSWY9iby5HTrzMVTCWS5/HdNAy5qesvX82+iK8Sf9ivEjsoJrFX033rsHs8d+PY MgAqpFW+255eixFFL5fa2OOKyCpTNGiEG4DU3DM+GIFCZOVrEbxNX8//xP000q1BmCe+ Q/gBnsYLQJLw5KI6stgT8tX+GKJ9kK8kmw5+K8fpUNhGAvuprZALihi2uvOfe6vXlMPG EbVyEeidcZ4Jww5kIHtbdzKu4ZS4uw66x2LoaDGA9NMDLmSfncqKx5DDRkU7V30ji08m TEEg== X-Gm-Message-State: ABuFfojrISZpLQavpuieZ6u9xatxiASYGPPmrAT/W3oIeYR83po7LMQg DJQIkajVYLh8Eqqenb25XjUbTTvg X-Google-Smtp-Source: ACcGV60ccFQ+QKUj8KM0sMVf6aSvv+vdv7bW83kX0NI4bACzKeL2PrasJ8viBG6e9NNmPsCGa+HIAg== X-Received: by 2002:a17:902:3a5:: with SMTP id d34-v6mr16135281pld.231.1539594996568; Mon, 15 Oct 2018 02:16:36 -0700 (PDT) Received: from ala-d2121-lx1.wrs.com (unknown-156-139.windriver.com. [147.11.156.139]) by smtp.gmail.com with ESMTPSA id a11-v6sm10798256pgw.54.2018.10.15.02.16.35 (version=TLS1_1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 15 Oct 2018 02:16:35 -0700 (PDT) From: Bin Meng To: Simon Glass , U-Boot Mailing List Date: Mon, 15 Oct 2018 02:21:00 -0700 Message-Id: <1539595287-31378-5-git-send-email-bmeng.cn@gmail.com> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: <1539595287-31378-1-git-send-email-bmeng.cn@gmail.com> References: <1539595287-31378-1-git-send-email-bmeng.cn@gmail.com> MIME-Version: 1.0 Subject: [U-Boot] [PATCH v3 04/31] dm: Add a new uclass driver for VirtIO transport devices X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.18 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" This adds a new virtio uclass driver for “virtio” [1] family of devices that are are found in virtual environments like QEMU, yet by design they look like physical devices to the guest. The uclass driver provides child_pre_probe() and child_post_probe() methods to do some common operations for virtio device drivers like device and driver supported feature negotiation, etc. [1] http://docs.oasis-open.org/virtio/virtio/v1.0/virtio-v1.0.pdf Signed-off-by: Tuomas Tynkkynen Signed-off-by: Bin Meng Reviewed-by: Simon Glass Reviewed-by: Simon Glass --- Changes in v3: None Changes in v2: - Added virtio spec link and a short statement of what virtio is in the header and source files - Changed to use 'transport' over 'bus' in drivers/virtio/Kconfig - Added UCLASS_VIRTIO ID in alphabetical order - Moved the header file inclusion from virtio.h to C file - Fixed code styling issue (blank line before return) in virtio_xxx() APIs - Changed virtio_init() to return int if there is an error - Added virtio_uclass_pre_probe() to check virtio transport driver ops here so that we don't need check these ops each time when the virtio_xxx APIs are called - Implemented child_post_remove() method to reset the virtio device - Changed virtio_driver_features_init() parameter 'feature' and 'feature_legacy' to const, and adjust member of 'struct virtio_dev_priv' accordingly - Initialize uc_priv->vqs in virtio_uclass_post_probe() drivers/Kconfig | 2 + drivers/Makefile | 1 + drivers/virtio/Kconfig | 25 ++ drivers/virtio/Makefile | 6 + drivers/virtio/virtio-uclass.c | 369 +++++++++++++++++++++ include/dm/uclass-id.h | 1 + include/virtio.h | 707 +++++++++++++++++++++++++++++++++++++++++ include/virtio_types.h | 24 ++ 8 files changed, 1135 insertions(+) create mode 100644 drivers/virtio/Kconfig create mode 100644 drivers/virtio/Makefile create mode 100644 drivers/virtio/virtio-uclass.c create mode 100644 include/virtio.h create mode 100644 include/virtio_types.h diff --git a/drivers/Kconfig b/drivers/Kconfig index 927a2b8..4ac823d 100644 --- a/drivers/Kconfig +++ b/drivers/Kconfig @@ -112,6 +112,8 @@ source "drivers/usb/Kconfig" source "drivers/video/Kconfig" +source "drivers/virtio/Kconfig" + source "drivers/w1/Kconfig" source "drivers/w1-eeprom/Kconfig" diff --git a/drivers/Makefile b/drivers/Makefile index fb38b67..4453c62 100644 --- a/drivers/Makefile +++ b/drivers/Makefile @@ -14,6 +14,7 @@ obj-$(CONFIG_$(SPL_TPL_)SERIAL_SUPPORT) += serial/ obj-$(CONFIG_$(SPL_TPL_)SPI_FLASH_SUPPORT) += mtd/spi/ obj-$(CONFIG_$(SPL_TPL_)SPI_SUPPORT) += spi/ obj-$(CONFIG_$(SPL_TPL_)TIMER) += timer/ +obj-$(CONFIG_$(SPL_TPL_)VIRTIO) += virtio/ obj-$(CONFIG_$(SPL_)DM_MAILBOX) += mailbox/ obj-$(CONFIG_$(SPL_)REMOTEPROC) += remoteproc/ diff --git a/drivers/virtio/Kconfig b/drivers/virtio/Kconfig new file mode 100644 index 0000000..82fc536 --- /dev/null +++ b/drivers/virtio/Kconfig @@ -0,0 +1,25 @@ +# SPDX-License-Identifier: GPL-2.0+ +# +# Copyright (C) 2018, Tuomas Tynkkynen +# Copyright (C) 2018, Bin Meng +# +# VirtIO is a virtualization standard for network and disk device drivers +# where just the guest's device driver "knows" it is running in a virtual +# environment, and cooperates with the hypervisor. This enables guests to +# get high performance network and disk operations, and gives most of the +# performance benefits of paravirtualization. In the U-Boot case, the guest +# is U-Boot itself, while the virtual environment are normally QEMU targets +# like ARM, RISC-V and x86. +# +# See http://docs.oasis-open.org/virtio/virtio/v1.0/virtio-v1.0.pdf for +# the VirtIO specification v1.0. + +menu "VirtIO Drivers" + +config VIRTIO + bool + help + This option is selected by any driver which implements the virtio + transport, such as CONFIG_VIRTIO_MMIO or CONFIG_VIRTIO_PCI. + +endmenu diff --git a/drivers/virtio/Makefile b/drivers/virtio/Makefile new file mode 100644 index 0000000..23e7be7 --- /dev/null +++ b/drivers/virtio/Makefile @@ -0,0 +1,6 @@ +# SPDX-License-Identifier: GPL-2.0+ +# +# Copyright (C) 2018, Tuomas Tynkkynen +# Copyright (C) 2018, Bin Meng + +obj-y += virtio-uclass.o diff --git a/drivers/virtio/virtio-uclass.c b/drivers/virtio/virtio-uclass.c new file mode 100644 index 0000000..34397d7 --- /dev/null +++ b/drivers/virtio/virtio-uclass.c @@ -0,0 +1,369 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * Copyright (C) 2018, Tuomas Tynkkynen + * Copyright (C) 2018, Bin Meng + * + * VirtIO is a virtualization standard for network and disk device drivers + * where just the guest's device driver "knows" it is running in a virtual + * environment, and cooperates with the hypervisor. This enables guests to + * get high performance network and disk operations, and gives most of the + * performance benefits of paravirtualization. In the U-Boot case, the guest + * is U-Boot itself, while the virtual environment are normally QEMU targets + * like ARM, RISC-V and x86. + * + * See http://docs.oasis-open.org/virtio/virtio/v1.0/virtio-v1.0.pdf for + * the VirtIO specification v1.0. + */ + +#include +#include +#include +#include +#include + +static const char *const virtio_drv_name[VIRTIO_ID_MAX_NUM] = { + [VIRTIO_ID_NET] = VIRTIO_NET_DRV_NAME, + [VIRTIO_ID_BLOCK] = VIRTIO_BLK_DRV_NAME, +}; + +int virtio_get_config(struct udevice *vdev, unsigned int offset, + void *buf, unsigned int len) +{ + struct dm_virtio_ops *ops; + + ops = virtio_get_ops(vdev->parent); + + return ops->get_config(vdev->parent, offset, buf, len); +} + +int virtio_set_config(struct udevice *vdev, unsigned int offset, + void *buf, unsigned int len) +{ + struct dm_virtio_ops *ops; + + ops = virtio_get_ops(vdev->parent); + + return ops->set_config(vdev->parent, offset, buf, len); +} + +int virtio_generation(struct udevice *vdev, u32 *counter) +{ + struct dm_virtio_ops *ops; + + ops = virtio_get_ops(vdev->parent); + if (!ops->generation) + return -ENOSYS; + + return ops->generation(vdev->parent, counter); +} + +int virtio_get_status(struct udevice *vdev, u8 *status) +{ + struct dm_virtio_ops *ops; + + ops = virtio_get_ops(vdev->parent); + + return ops->get_status(vdev->parent, status); +} + +int virtio_set_status(struct udevice *vdev, u8 status) +{ + struct dm_virtio_ops *ops; + + ops = virtio_get_ops(vdev->parent); + + return ops->set_status(vdev->parent, status); +} + +int virtio_reset(struct udevice *vdev) +{ + struct dm_virtio_ops *ops; + + ops = virtio_get_ops(vdev->parent); + + return ops->reset(vdev->parent); +} + +int virtio_get_features(struct udevice *vdev, u64 *features) +{ + struct dm_virtio_ops *ops; + + ops = virtio_get_ops(vdev->parent); + + return ops->get_features(vdev->parent, features); +} + +int virtio_set_features(struct udevice *vdev) +{ + struct dm_virtio_ops *ops; + + ops = virtio_get_ops(vdev->parent); + + return ops->set_features(vdev->parent); +} + +int virtio_find_vqs(struct udevice *vdev, unsigned int nvqs, + struct virtqueue *vqs[]) +{ + struct dm_virtio_ops *ops; + + ops = virtio_get_ops(vdev->parent); + + return ops->find_vqs(vdev->parent, nvqs, vqs); +} + +int virtio_del_vqs(struct udevice *vdev) +{ + struct dm_virtio_ops *ops; + + ops = virtio_get_ops(vdev->parent); + + return ops->del_vqs(vdev->parent); +} + +int virtio_notify(struct udevice *vdev, struct virtqueue *vq) +{ + struct dm_virtio_ops *ops; + + ops = virtio_get_ops(vdev->parent); + + return ops->notify(vdev->parent, vq); +} + +void virtio_add_status(struct udevice *vdev, u8 status) +{ + u8 old; + + if (!virtio_get_status(vdev, &old)) + virtio_set_status(vdev, old | status); +} + +int virtio_finalize_features(struct udevice *vdev) +{ + struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(vdev->parent); + u8 status; + int ret; + + ret = virtio_set_features(vdev); + if (ret) + return ret; + + if (uc_priv->legacy) + return 0; + + virtio_add_status(vdev, VIRTIO_CONFIG_S_FEATURES_OK); + ret = virtio_get_status(vdev, &status); + if (ret) + return ret; + if (!(status & VIRTIO_CONFIG_S_FEATURES_OK)) { + debug("(%s): device refuses features %x\n", vdev->name, status); + return -ENODEV; + } + + return 0; +} + +void virtio_driver_features_init(struct virtio_dev_priv *priv, + const u32 *feature, + u32 feature_size, + const u32 *feature_legacy, + u32 feature_legacy_size) +{ + priv->feature_table = feature; + priv->feature_table_size = feature_size; + priv->feature_table_legacy = feature_legacy; + priv->feature_table_size_legacy = feature_legacy_size; +} + +int virtio_init(void) +{ + struct udevice *bus; + int ret; + + /* Enumerate all known virtio devices */ + ret = uclass_first_device(UCLASS_VIRTIO, &bus); + if (ret) + return ret; + + while (bus) { + ret = uclass_next_device(&bus); + if (ret) + break; + } + + return ret; +} + +static int virtio_uclass_pre_probe(struct udevice *udev) +{ + struct dm_virtio_ops *ops; + + ops = (struct dm_virtio_ops *)(udev->driver->ops); + + /* + * Check virtio transport driver ops here so that we don't need + * check these ops each time when the virtio_xxx APIs are called. + * + * Only generation op is optional. All other ops are must-have. + */ + if (!ops->get_config || !ops->set_config || + !ops->get_status || !ops->set_status || + !ops->get_features || !ops->set_features || + !ops->find_vqs || !ops->del_vqs || + !ops->reset || !ops->notify) + return -ENOENT; + + return 0; +} + +static int virtio_uclass_post_probe(struct udevice *udev) +{ + struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(udev); + char dev_name[30], *str; + struct udevice *vdev; + int ret; + + if (uc_priv->device > VIRTIO_ID_MAX_NUM) { + debug("(%s): virtio device ID %d exceeds maximum num\n", + udev->name, uc_priv->device); + return 0; + } + + if (!virtio_drv_name[uc_priv->device]) { + debug("(%s): underlying virtio device driver unavailable\n", + udev->name); + return 0; + } + + snprintf(dev_name, sizeof(dev_name), "%s#%d", + virtio_drv_name[uc_priv->device], udev->seq); + str = strdup(dev_name); + if (!str) + return -ENOMEM; + + ret = device_bind_driver(udev, virtio_drv_name[uc_priv->device], + str, &vdev); + if (ret == -ENOENT) { + debug("(%s): no driver configured\n", udev->name); + return 0; + } + if (ret) { + free(str); + return ret; + } + device_set_name_alloced(vdev); + + INIT_LIST_HEAD(&uc_priv->vqs); + + return 0; +} + +static int virtio_uclass_child_post_bind(struct udevice *vdev) +{ + /* Acknowledge that we've seen the device */ + virtio_add_status(vdev, VIRTIO_CONFIG_S_ACKNOWLEDGE); + + return 0; +} + +static int virtio_uclass_child_pre_probe(struct udevice *vdev) +{ + struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(vdev->parent); + u64 device_features; + u64 driver_features; + u64 driver_features_legacy; + int i; + int ret; + + /* + * Save the real virtio device (eg: virtio-net, virtio-blk) to + * the transport (parent) device's uclass priv for future use. + */ + uc_priv->vdev = vdev; + + /* + * We always start by resetting the device, in case a previous driver + * messed it up. This also tests that code path a little. + */ + ret = virtio_reset(vdev); + if (ret) + goto err; + + /* We have a driver! */ + virtio_add_status(vdev, VIRTIO_CONFIG_S_DRIVER); + + /* Figure out what features the device supports */ + virtio_get_features(vdev, &device_features); + debug("(%s) plain device features supported %016llx\n", + vdev->name, device_features); + if (!(device_features & (1ULL << VIRTIO_F_VERSION_1))) + uc_priv->legacy = true; + + /* Figure out what features the driver supports */ + driver_features = 0; + for (i = 0; i < uc_priv->feature_table_size; i++) { + unsigned int f = uc_priv->feature_table[i]; + + WARN_ON(f >= 64); + driver_features |= (1ULL << f); + } + + /* Some drivers have a separate feature table for virtio v1.0 */ + if (uc_priv->feature_table_legacy) { + driver_features_legacy = 0; + for (i = 0; i < uc_priv->feature_table_size_legacy; i++) { + unsigned int f = uc_priv->feature_table_legacy[i]; + + WARN_ON(f >= 64); + driver_features_legacy |= (1ULL << f); + } + } else { + driver_features_legacy = driver_features; + } + + if (uc_priv->legacy) { + debug("(%s): legacy virtio device\n", vdev->name); + uc_priv->features = driver_features_legacy & device_features; + } else { + debug("(%s): v1.0 complaint virtio device\n", vdev->name); + uc_priv->features = driver_features & device_features; + } + + /* Transport features always preserved to pass to finalize_features */ + for (i = VIRTIO_TRANSPORT_F_START; i < VIRTIO_TRANSPORT_F_END; i++) + if ((device_features & (1ULL << i)) && + (i == VIRTIO_F_VERSION_1)) + __virtio_set_bit(vdev->parent, i); + + debug("(%s) final negotiated features supported %016llx\n", + vdev->name, uc_priv->features); + ret = virtio_finalize_features(vdev); + if (ret) + goto err; + + return 0; + +err: + virtio_add_status(vdev, VIRTIO_CONFIG_S_FAILED); + return ret; +} + +static int virtio_uclass_child_post_probe(struct udevice *vdev) +{ + /* Indicates that the driver is set up and ready to drive the device */ + virtio_add_status(vdev, VIRTIO_CONFIG_S_DRIVER_OK); + + return 0; +} + +UCLASS_DRIVER(virtio) = { + .name = "virtio", + .id = UCLASS_VIRTIO, + .flags = DM_UC_FLAG_SEQ_ALIAS, + .pre_probe = virtio_uclass_pre_probe, + .post_probe = virtio_uclass_post_probe, + .child_post_bind = virtio_uclass_child_post_bind, + .child_pre_probe = virtio_uclass_child_pre_probe, + .child_post_probe = virtio_uclass_child_post_probe, + .per_device_auto_alloc_size = sizeof(struct virtio_dev_priv), +}; diff --git a/include/dm/uclass-id.h b/include/dm/uclass-id.h index 269a2c6..c91dca1 100644 --- a/include/dm/uclass-id.h +++ b/include/dm/uclass-id.h @@ -96,6 +96,7 @@ enum uclass_id { UCLASS_VIDEO_BRIDGE, /* Video bridge, e.g. DisplayPort to LVDS */ UCLASS_VIDEO_CONSOLE, /* Text console driver for video device */ UCLASS_VIDEO_OSD, /* On-screen display */ + UCLASS_VIRTIO, /* VirtIO transport device */ UCLASS_W1, /* Dallas 1-Wire bus */ UCLASS_W1_EEPROM, /* one-wire EEPROMs */ UCLASS_WDT, /* Watchdot Timer driver */ diff --git a/include/virtio.h b/include/virtio.h new file mode 100644 index 0000000..654fdf1 --- /dev/null +++ b/include/virtio.h @@ -0,0 +1,707 @@ +/* SPDX-License-Identifier: GPL-2.0+ */ +/* + * Copyright (C) 2018, Tuomas Tynkkynen + * Copyright (C) 2018, Bin Meng + * + * VirtIO is a virtualization standard for network and disk device drivers + * where just the guest's device driver "knows" it is running in a virtual + * environment, and cooperates with the hypervisor. This enables guests to + * get high performance network and disk operations, and gives most of the + * performance benefits of paravirtualization. In the U-Boot case, the guest + * is U-Boot itself, while the virtual environment are normally QEMU targets + * like ARM, RISC-V and x86. + * + * See http://docs.oasis-open.org/virtio/virtio/v1.0/virtio-v1.0.pdf for + * the VirtIO specification v1.0. + * + * This file is largely based on Linux kernel virtio_*.h files + */ + +#ifndef __VIRTIO_H__ +#define __VIRTIO_H__ + +#define VIRTIO_ID_NET 1 /* virtio net */ +#define VIRTIO_ID_BLOCK 2 /* virtio block */ +#define VIRTIO_ID_MAX_NUM 3 + +#define VIRTIO_NET_DRV_NAME "virtio-net" +#define VIRTIO_BLK_DRV_NAME "virtio-blk" + +/* Status byte for guest to report progress, and synchronize features */ + +/* We have seen device and processed generic fields (VIRTIO_CONFIG_F_VIRTIO) */ +#define VIRTIO_CONFIG_S_ACKNOWLEDGE 1 +/* We have found a driver for the device */ +#define VIRTIO_CONFIG_S_DRIVER 2 +/* Driver has used its parts of the config, and is happy */ +#define VIRTIO_CONFIG_S_DRIVER_OK 4 +/* Driver has finished configuring features */ +#define VIRTIO_CONFIG_S_FEATURES_OK 8 +/* Device entered invalid state, driver must reset it */ +#define VIRTIO_CONFIG_S_NEEDS_RESET 0x40 +/* We've given up on this device */ +#define VIRTIO_CONFIG_S_FAILED 0x80 + +/* + * Virtio feature bits VIRTIO_TRANSPORT_F_START through VIRTIO_TRANSPORT_F_END + * are reserved for the transport being used (eg: virtio_ring, virtio_pci etc.), + * the rest are per-device feature bits. + */ +#define VIRTIO_TRANSPORT_F_START 28 +#define VIRTIO_TRANSPORT_F_END 38 + +#ifndef VIRTIO_CONFIG_NO_LEGACY +/* + * Do we get callbacks when the ring is completely used, + * even if we've suppressed them? + */ +#define VIRTIO_F_NOTIFY_ON_EMPTY 24 + +/* Can the device handle any descriptor layout? */ +#define VIRTIO_F_ANY_LAYOUT 27 +#endif /* VIRTIO_CONFIG_NO_LEGACY */ + +/* v1.0 compliant */ +#define VIRTIO_F_VERSION_1 32 + +/* + * If clear - device has the IOMMU bypass quirk feature. + * If set - use platform tools to detect the IOMMU. + * + * Note the reverse polarity (compared to most other features), + * this is for compatibility with legacy systems. + */ +#define VIRTIO_F_IOMMU_PLATFORM 33 + +/* Does the device support Single Root I/O Virtualization? */ +#define VIRTIO_F_SR_IOV 37 + +/** + * virtio scatter-gather struct + * + * @addr: sg buffer address + * @lengh: sg buffer length + */ +struct virtio_sg { + void *addr; + size_t length; +}; + +struct virtqueue; + +/* virtio bus operations */ +struct dm_virtio_ops { + /** + * get_config() - read the value of a configuration field + * + * @vdev: the real virtio device + * @offset: the offset of the configuration field + * @buf: the buffer to write the field value into + * @len: the length of the buffer + * @return 0 if OK, -ve on error + */ + int (*get_config)(struct udevice *vdev, unsigned int offset, + void *buf, unsigned int len); + /** + * set_config() - write the value of a configuration field + * + * @vdev: the real virtio device + * @offset: the offset of the configuration field + * @buf: the buffer to read the field value from + * @len: the length of the buffer + * @return 0 if OK, -ve on error + */ + int (*set_config)(struct udevice *vdev, unsigned int offset, + const void *buf, unsigned int len); + /** + * generation() - config generation counter + * + * @vdev: the real virtio device + * @counter: the returned config generation counter + * @return 0 if OK, -ve on error + */ + int (*generation)(struct udevice *vdev, u32 *counter); + /** + * get_status() - read the status byte + * + * @vdev: the real virtio device + * @status: the returned status byte + * @return 0 if OK, -ve on error + */ + int (*get_status)(struct udevice *vdev, u8 *status); + /** + * set_status() - write the status byte + * + * @vdev: the real virtio device + * @status: the new status byte + * @return 0 if OK, -ve on error + */ + int (*set_status)(struct udevice *vdev, u8 status); + /** + * reset() - reset the device + * + * @vdev: the real virtio device + * @return 0 if OK, -ve on error + */ + int (*reset)(struct udevice *vdev); + /** + * get_features() - get the array of feature bits for this device + * + * @vdev: the real virtio device + * @features: the first 32 feature bits (all we currently need) + * @return 0 if OK, -ve on error + */ + int (*get_features)(struct udevice *vdev, u64 *features); + /** + * set_features() - confirm what device features we'll be using + * + * @vdev: the real virtio device + * @return 0 if OK, -ve on error + */ + int (*set_features)(struct udevice *vdev); + /** + * find_vqs() - find virtqueues and instantiate them + * + * @vdev: the real virtio device + * @nvqs: the number of virtqueues to find + * @vqs: on success, includes new virtqueues + * @return 0 if OK, -ve on error + */ + int (*find_vqs)(struct udevice *vdev, unsigned int nvqs, + struct virtqueue *vqs[]); + /** + * del_vqs() - free virtqueues found by find_vqs() + * + * @vdev: the real virtio device + * @return 0 if OK, -ve on error + */ + int (*del_vqs)(struct udevice *vdev); + /** + * notify() - notify the device to process the queue + * + * @vdev: the real virtio device + * @vq: virtqueue to process + * @return 0 if OK, -ve on error + */ + int (*notify)(struct udevice *vdev, struct virtqueue *vq); +}; + +/* Get access to a virtio bus' operations */ +#define virtio_get_ops(dev) ((struct dm_virtio_ops *)(dev)->driver->ops) + +/** + * virtio uclass per device private data + * + * @vqs: virtualqueue for the virtio device + * @vdev: the real virtio device underneath + * @legacy: is it a legacy device? + * @device: virtio device ID + * @vendor: virtio vendor ID + * @features: negotiated supported features + * @feature_table: an array of feature supported by the driver + * @feature_table_size: number of entries in the feature table array + * @feature_table_legacy: same as feature_table but working in legacy mode + * @feature_table_size_legacy: number of entries in feature table legacy array + */ +struct virtio_dev_priv { + struct list_head vqs; + struct udevice *vdev; + bool legacy; + u32 device; + u32 vendor; + u64 features; + const u32 *feature_table; + u32 feature_table_size; + const u32 *feature_table_legacy; + u32 feature_table_size_legacy; +}; + +/** + * virtio_get_config() - read the value of a configuration field + * + * @vdev: the real virtio device + * @offset: the offset of the configuration field + * @buf: the buffer to write the field value into + * @len: the length of the buffer + * @return 0 if OK, -ve on error + */ +int virtio_get_config(struct udevice *vdev, unsigned int offset, + void *buf, unsigned int len); + +/** + * virtio_set_config() - write the value of a configuration field + * + * @vdev: the real virtio device + * @offset: the offset of the configuration field + * @buf: the buffer to read the field value from + * @len: the length of the buffer + * @return 0 if OK, -ve on error + */ +int virtio_set_config(struct udevice *vdev, unsigned int offset, + void *buf, unsigned int len); + +/** + * virtio_generation() - config generation counter + * + * @vdev: the real virtio device + * @counter: the returned config generation counter + * @return 0 if OK, -ve on error + */ +int virtio_generation(struct udevice *vdev, u32 *counter); + +/** + * virtio_get_status() - read the status byte + * + * @vdev: the real virtio device + * @status: the returned status byte + * @return 0 if OK, -ve on error + */ +int virtio_get_status(struct udevice *vdev, u8 *status); + +/** + * virtio_set_status() - write the status byte + * + * @vdev: the real virtio device + * @status: the new status byte + * @return 0 if OK, -ve on error + */ +int virtio_set_status(struct udevice *vdev, u8 status); + +/** + * virtio_reset() - reset the device + * + * @vdev: the real virtio device + * @return 0 if OK, -ve on error + */ +int virtio_reset(struct udevice *vdev); + +/** + * virtio_get_features() - get the array of feature bits for this device + * + * @vdev: the real virtio device + * @features: the first 32 feature bits (all we currently need) + * @return 0 if OK, -ve on error + */ +int virtio_get_features(struct udevice *vdev, u64 *features); + +/** + * virtio_set_features() - confirm what device features we'll be using + * + * @vdev: the real virtio device + * @return 0 if OK, -ve on error + */ +int virtio_set_features(struct udevice *vdev); + +/** + * virtio_find_vqs() - find virtqueues and instantiate them + * + * @vdev: the real virtio device + * @nvqs: the number of virtqueues to find + * @vqs: on success, includes new virtqueues + * @return 0 if OK, -ve on error + */ +int virtio_find_vqs(struct udevice *vdev, unsigned int nvqs, + struct virtqueue *vqs[]); + +/** + * virtio_del_vqs() - free virtqueues found by find_vqs() + * + * @vdev: the real virtio device + * @return 0 if OK, -ve on error + */ +int virtio_del_vqs(struct udevice *vdev); + +/** + * virtio_notify() - notify the device to process the queue + * + * @vdev: the real virtio device + * @vq: virtqueue to process + * @return 0 if OK, -ve on error + */ +int virtio_notify(struct udevice *vdev, struct virtqueue *vq); + +/** + * virtio_add_status() - helper to set a new status code to the device + * + * @vdev: the real virtio device + * @status: new status code to be added + */ +void virtio_add_status(struct udevice *vdev, u8 status); + +/** + * virtio_finalize_features() - helper to finalize features + * + * @vdev: the real virtio device + * @return 0 if OK, -ve on error + */ +int virtio_finalize_features(struct udevice *vdev); + +/** + * virtio_driver_features_init() - initialize driver supported features + * + * This fills in the virtio device parent per child private data with the given + * information, which contains driver supported features and legacy features. + * + * This API should be called in the virtio device driver's bind method, so that + * later virtio transport uclass driver can utilize the driver supplied features + * to negotiate with the device on the final supported features. + * + * @priv: virtio uclass per device private data + * @feature: an array of feature supported by the driver + * @feature_size: number of entries in the feature table array + * @feature_legacy: same as feature_table but working in legacy mode + * @feature_legacy_size:number of entries in feature table legacy array + */ +void virtio_driver_features_init(struct virtio_dev_priv *priv, + const u32 *feature, + u32 feature_size, + const u32 *feature_legacy, + u32 feature_legacy_size); + +/** + * virtio_init() - helper to enumerate all known virtio devices + * + * @return 0 if OK, -ve on error + */ +int virtio_init(void); + +static inline u16 __virtio16_to_cpu(bool little_endian, __virtio16 val) +{ + if (little_endian) + return le16_to_cpu((__force __le16)val); + else + return be16_to_cpu((__force __be16)val); +} + +static inline __virtio16 __cpu_to_virtio16(bool little_endian, u16 val) +{ + if (little_endian) + return (__force __virtio16)cpu_to_le16(val); + else + return (__force __virtio16)cpu_to_be16(val); +} + +static inline u32 __virtio32_to_cpu(bool little_endian, __virtio32 val) +{ + if (little_endian) + return le32_to_cpu((__force __le32)val); + else + return be32_to_cpu((__force __be32)val); +} + +static inline __virtio32 __cpu_to_virtio32(bool little_endian, u32 val) +{ + if (little_endian) + return (__force __virtio32)cpu_to_le32(val); + else + return (__force __virtio32)cpu_to_be32(val); +} + +static inline u64 __virtio64_to_cpu(bool little_endian, __virtio64 val) +{ + if (little_endian) + return le64_to_cpu((__force __le64)val); + else + return be64_to_cpu((__force __be64)val); +} + +static inline __virtio64 __cpu_to_virtio64(bool little_endian, u64 val) +{ + if (little_endian) + return (__force __virtio64)cpu_to_le64(val); + else + return (__force __virtio64)cpu_to_be64(val); +} + +/** + * __virtio_test_bit - helper to test feature bits + * + * For use by transports. Devices should normally use virtio_has_feature, + * which includes more checks. + * + * @udev: the transport device + * @fbit: the feature bit + */ +static inline bool __virtio_test_bit(struct udevice *udev, unsigned int fbit) +{ + struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(udev); + + /* Did you forget to fix assumptions on max features? */ + if (__builtin_constant_p(fbit)) + BUILD_BUG_ON(fbit >= 64); + else + WARN_ON(fbit >= 64); + + return uc_priv->features & BIT_ULL(fbit); +} + +/** + * __virtio_set_bit - helper to set feature bits + * + * For use by transports. + * + * @udev: the transport device + * @fbit: the feature bit + */ +static inline void __virtio_set_bit(struct udevice *udev, unsigned int fbit) +{ + struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(udev); + + /* Did you forget to fix assumptions on max features? */ + if (__builtin_constant_p(fbit)) + BUILD_BUG_ON(fbit >= 64); + else + WARN_ON(fbit >= 64); + + uc_priv->features |= BIT_ULL(fbit); +} + +/** + * __virtio_clear_bit - helper to clear feature bits + * + * For use by transports. + * + * @vdev: the transport device + * @fbit: the feature bit + */ +static inline void __virtio_clear_bit(struct udevice *udev, unsigned int fbit) +{ + struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(udev); + + /* Did you forget to fix assumptions on max features? */ + if (__builtin_constant_p(fbit)) + BUILD_BUG_ON(fbit >= 64); + else + WARN_ON(fbit >= 64); + + uc_priv->features &= ~BIT_ULL(fbit); +} + +/** + * virtio_has_feature - helper to determine if this device has this feature + * + * Note this API is only usable after the virtio device driver's bind phase, + * as the feature has been negotiated between the device and the driver. + * + * @vdev: the virtio device + * @fbit: the feature bit + */ +static inline bool virtio_has_feature(struct udevice *vdev, unsigned int fbit) +{ + if (!(vdev->flags & DM_FLAG_BOUND)) + WARN_ON(true); + + return __virtio_test_bit(vdev->parent, fbit); +} + +static inline bool virtio_legacy_is_little_endian(void) +{ +#ifdef __LITTLE_ENDIAN + return true; +#else + return false; +#endif +} + +static inline bool virtio_is_little_endian(struct udevice *vdev) +{ + struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(vdev->parent); + + return !uc_priv->legacy || virtio_legacy_is_little_endian(); +} + +/* Memory accessors */ +static inline u16 virtio16_to_cpu(struct udevice *vdev, __virtio16 val) +{ + return __virtio16_to_cpu(virtio_is_little_endian(vdev), val); +} + +static inline __virtio16 cpu_to_virtio16(struct udevice *vdev, u16 val) +{ + return __cpu_to_virtio16(virtio_is_little_endian(vdev), val); +} + +static inline u32 virtio32_to_cpu(struct udevice *vdev, __virtio32 val) +{ + return __virtio32_to_cpu(virtio_is_little_endian(vdev), val); +} + +static inline __virtio32 cpu_to_virtio32(struct udevice *vdev, u32 val) +{ + return __cpu_to_virtio32(virtio_is_little_endian(vdev), val); +} + +static inline u64 virtio64_to_cpu(struct udevice *vdev, __virtio64 val) +{ + return __virtio64_to_cpu(virtio_is_little_endian(vdev), val); +} + +static inline __virtio64 cpu_to_virtio64(struct udevice *vdev, u64 val) +{ + return __cpu_to_virtio64(virtio_is_little_endian(vdev), val); +} + +/* Read @count fields, @bytes each */ +static inline void __virtio_cread_many(struct udevice *vdev, + unsigned int offset, + void *buf, size_t count, size_t bytes) +{ + u32 old, gen; + int i; + + /* no need to check return value as generation can be optional */ + virtio_generation(vdev, &gen); + do { + old = gen; + + for (i = 0; i < count; i++) + virtio_get_config(vdev, offset + bytes * i, + buf + i * bytes, bytes); + + virtio_generation(vdev, &gen); + } while (gen != old); +} + +static inline void virtio_cread_bytes(struct udevice *vdev, + unsigned int offset, + void *buf, size_t len) +{ + __virtio_cread_many(vdev, offset, buf, len, 1); +} + +static inline u8 virtio_cread8(struct udevice *vdev, unsigned int offset) +{ + u8 ret; + + virtio_get_config(vdev, offset, &ret, sizeof(ret)); + return ret; +} + +static inline void virtio_cwrite8(struct udevice *vdev, + unsigned int offset, u8 val) +{ + virtio_set_config(vdev, offset, &val, sizeof(val)); +} + +static inline u16 virtio_cread16(struct udevice *vdev, + unsigned int offset) +{ + u16 ret; + + virtio_get_config(vdev, offset, &ret, sizeof(ret)); + return virtio16_to_cpu(vdev, (__force __virtio16)ret); +} + +static inline void virtio_cwrite16(struct udevice *vdev, + unsigned int offset, u16 val) +{ + val = (__force u16)cpu_to_virtio16(vdev, val); + virtio_set_config(vdev, offset, &val, sizeof(val)); +} + +static inline u32 virtio_cread32(struct udevice *vdev, + unsigned int offset) +{ + u32 ret; + + virtio_get_config(vdev, offset, &ret, sizeof(ret)); + return virtio32_to_cpu(vdev, (__force __virtio32)ret); +} + +static inline void virtio_cwrite32(struct udevice *vdev, + unsigned int offset, u32 val) +{ + val = (__force u32)cpu_to_virtio32(vdev, val); + virtio_set_config(vdev, offset, &val, sizeof(val)); +} + +static inline u64 virtio_cread64(struct udevice *vdev, + unsigned int offset) +{ + u64 ret; + + __virtio_cread_many(vdev, offset, &ret, 1, sizeof(ret)); + return virtio64_to_cpu(vdev, (__force __virtio64)ret); +} + +static inline void virtio_cwrite64(struct udevice *vdev, + unsigned int offset, u64 val) +{ + val = (__force u64)cpu_to_virtio64(vdev, val); + virtio_set_config(vdev, offset, &val, sizeof(val)); +} + +/* Config space read accessor */ +#define virtio_cread(vdev, structname, member, ptr) \ + do { \ + /* Must match the member's type, and be integer */ \ + if (!typecheck(typeof((((structname *)0)->member)), *(ptr))) \ + (*ptr) = 1; \ + \ + switch (sizeof(*ptr)) { \ + case 1: \ + *(ptr) = virtio_cread8(vdev, \ + offsetof(structname, member)); \ + break; \ + case 2: \ + *(ptr) = virtio_cread16(vdev, \ + offsetof(structname, member)); \ + break; \ + case 4: \ + *(ptr) = virtio_cread32(vdev, \ + offsetof(structname, member)); \ + break; \ + case 8: \ + *(ptr) = virtio_cread64(vdev, \ + offsetof(structname, member)); \ + break; \ + default: \ + WARN_ON(true); \ + } \ + } while (0) + +/* Config space write accessor */ +#define virtio_cwrite(vdev, structname, member, ptr) \ + do { \ + /* Must match the member's type, and be integer */ \ + if (!typecheck(typeof((((structname *)0)->member)), *(ptr))) \ + WARN_ON((*ptr) == 1); \ + \ + switch (sizeof(*ptr)) { \ + case 1: \ + virtio_cwrite8(vdev, \ + offsetof(structname, member), \ + *(ptr)); \ + break; \ + case 2: \ + virtio_cwrite16(vdev, \ + offsetof(structname, member), \ + *(ptr)); \ + break; \ + case 4: \ + virtio_cwrite32(vdev, \ + offsetof(structname, member), \ + *(ptr)); \ + break; \ + case 8: \ + virtio_cwrite64(vdev, \ + offsetof(structname, member), \ + *(ptr)); \ + break; \ + default: \ + WARN_ON(true); \ + } \ + } while (0) + +/* Conditional config space accessors */ +#define virtio_cread_feature(vdev, fbit, structname, member, ptr) \ + ({ \ + int _r = 0; \ + if (!virtio_has_feature(vdev, fbit)) \ + _r = -ENOENT; \ + else \ + virtio_cread(vdev, structname, member, ptr); \ + _r; \ + }) + +#endif /* __VIRTIO_H__ */ diff --git a/include/virtio_types.h b/include/virtio_types.h new file mode 100644 index 0000000..d700d19 --- /dev/null +++ b/include/virtio_types.h @@ -0,0 +1,24 @@ +/* SPDX-License-Identifier: BSD-3-Clause */ +/* + * Copyright (C) 2018, Tuomas Tynkkynen + * Copyright (C) 2018, Bin Meng + * + * From Linux kernel include/uapi/linux/virtio_types.h + */ + +#ifndef _LINUX_VIRTIO_TYPES_H +#define _LINUX_VIRTIO_TYPES_H + +#include + +/* + * __virtio{16,32,64} have the following meaning: + * - __u{16,32,64} for virtio devices in legacy mode, accessed in native endian + * - __le{16,32,64} for standard-compliant virtio devices + */ + +typedef __u16 __bitwise __virtio16; +typedef __u32 __bitwise __virtio32; +typedef __u64 __bitwise __virtio64; + +#endif /* _LINUX_VIRTIO_TYPES_H */ From patchwork Mon Oct 15 09:21:01 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bin Meng X-Patchwork-Id: 984008 X-Patchwork-Delegate: sjg@chromium.org Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=lists.denx.de (client-ip=81.169.180.215; helo=lists.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="pAnVbjib"; dkim-atps=neutral Received: from lists.denx.de (dione.denx.de [81.169.180.215]) by ozlabs.org (Postfix) with ESMTP id 42YY0n4fZ5z9s5c for ; Mon, 15 Oct 2018 20:24:09 +1100 (AEDT) Received: by lists.denx.de (Postfix, from userid 105) id 48BA4C21E53; Mon, 15 Oct 2018 09:22:33 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on lists.denx.de X-Spam-Level: X-Spam-Status: No, score=-0.0 required=5.0 tests=FREEMAIL_FROM, RCVD_IN_DNSWL_BLOCKED, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, T_DKIM_INVALID autolearn=unavailable autolearn_force=no version=3.4.0 Received: from lists.denx.de (localhost [IPv6:::1]) by lists.denx.de (Postfix) with ESMTP id 3CFA4C21EA1; Mon, 15 Oct 2018 09:17:01 +0000 (UTC) Received: by lists.denx.de (Postfix, from userid 105) id 75B52C21E50; Mon, 15 Oct 2018 09:16:46 +0000 (UTC) Received: from mail-pg1-f193.google.com (mail-pg1-f193.google.com [209.85.215.193]) by lists.denx.de (Postfix) with ESMTPS id 78B1AC21E1D for ; Mon, 15 Oct 2018 09:16:39 +0000 (UTC) Received: by mail-pg1-f193.google.com with SMTP id n31-v6so8868444pgm.7 for ; Mon, 15 Oct 2018 02:16:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=iK82or2JpsVUWvqCSPazdYyw+gb7EqaPemMO72WfQ74=; b=pAnVbjib/0b3YLzP921Wb3mpkQ/PNqQizuZgWJM0LuN3qh15tpNMCc+Vx2q+BDUcTu dbmjJ/obcNSXhgdbPx07C00ItxZZMxatOQkBx5+Mh9gmsawzoTIICd2JgLpuODT13r0y 3++ecr7QisYNdvRVQjMnjoqVi3Ka2mcW1yrIb369HSYKPqBk1Z1AuoPb4fsaZZMJOgxO O8JndMtj0C91ke+mL0yLt95Nh1G142hl2YqHm/FGscRYi968Q7WWEGYubvob7a/TgNb3 3YdHq8Bu0Gb8HimIAqt6gNYBPmpzNcxNkDRNj0xkLAFKNtLHx09dto5e3clDRnsxFEBP AY6Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=iK82or2JpsVUWvqCSPazdYyw+gb7EqaPemMO72WfQ74=; b=fa0ENvIffpDdKPD4QLLuafy/+Xulcta9ey99hY0RruiA1ooN9WH6zg31gHU61UCS0a mwMHuSq0SIy7v6AbXqLSuu4xmkMVYZ63SDzbUnTCafAnCUURVi7fIM5GxjJxJYf1sdE5 FoJ9xqfHVP5Hb4290TqI1irU5S5BLFbIIW1oh4GymBvEaHM7+pCSG/iDeDBn/nUFsdDu VBG1YxDfgH1lVGBuqNJiNXbSN/D8AwVt1liBiGCev7Fi5ykMMc9psL2roUhg7g0dgXTY nbM1Sqkc9FjRfTnSZX4aiJR9zLvFM/MRMMelIRtVh+h+y8XRWeEGRM35FRFqowE5IZmw vsoA== X-Gm-Message-State: ABuFfojR+alZsO2j8MFuGWXGpBG7CiDg6rawNaWTWhzeGkCE0OQrZ7IN PJOywA1mhTU08bQvbmybjUXSlzIP X-Google-Smtp-Source: ACcGV63sm8ONrBZLLKEJqh4hP64Cy+yAMYSWXgAgCznHdftAkmRoRLx4KkiuNsHsoQgL97GbbetS1Q== X-Received: by 2002:a62:fc13:: with SMTP id e19-v6mr16936348pfh.101.1539594997749; Mon, 15 Oct 2018 02:16:37 -0700 (PDT) Received: from ala-d2121-lx1.wrs.com (unknown-156-139.windriver.com. [147.11.156.139]) by smtp.gmail.com with ESMTPSA id a11-v6sm10798256pgw.54.2018.10.15.02.16.36 (version=TLS1_1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 15 Oct 2018 02:16:36 -0700 (PDT) From: Bin Meng To: Simon Glass , U-Boot Mailing List Date: Mon, 15 Oct 2018 02:21:01 -0700 Message-Id: <1539595287-31378-6-git-send-email-bmeng.cn@gmail.com> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: <1539595287-31378-1-git-send-email-bmeng.cn@gmail.com> References: <1539595287-31378-1-git-send-email-bmeng.cn@gmail.com> Subject: [U-Boot] [PATCH v3 05/31] virtio: Add codes for virtual queue/ring management X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.18 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" From: Tuomas Tynkkynen This adds support for managing virtual queue/ring, the channel for high performance I/O between host and guest. Signed-off-by: Tuomas Tynkkynen Signed-off-by: Bin Meng Reviewed-by: Simon Glass Signed-off-by: Tuomas Tynkkynen Signed-off-by: Bin Meng Reviewed-by: Simon Glass --- Changes in v3: None Changes in v2: None drivers/virtio/Makefile | 2 +- drivers/virtio/virtio_ring.c | 358 +++++++++++++++++++++++++++++++++++++++++++ include/virtio_ring.h | 320 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 679 insertions(+), 1 deletion(-) create mode 100644 drivers/virtio/virtio_ring.c create mode 100644 include/virtio_ring.h diff --git a/drivers/virtio/Makefile b/drivers/virtio/Makefile index 23e7be7..17d264a 100644 --- a/drivers/virtio/Makefile +++ b/drivers/virtio/Makefile @@ -3,4 +3,4 @@ # Copyright (C) 2018, Tuomas Tynkkynen # Copyright (C) 2018, Bin Meng -obj-y += virtio-uclass.o +obj-y += virtio-uclass.o virtio_ring.o diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c new file mode 100644 index 0000000..0eeb350 --- /dev/null +++ b/drivers/virtio/virtio_ring.c @@ -0,0 +1,358 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * Copyright (C) 2018, Tuomas Tynkkynen + * Copyright (C) 2018, Bin Meng + * + * virtio ring implementation + */ + +#include +#include +#include +#include +#include +#include + +int virtqueue_add(struct virtqueue *vq, struct virtio_sg *sgs[], + unsigned int out_sgs, unsigned int in_sgs) +{ + struct vring_desc *desc; + unsigned int total_sg = out_sgs + in_sgs; + unsigned int i, n, avail, descs_used, uninitialized_var(prev); + int head; + + WARN_ON(total_sg == 0); + + head = vq->free_head; + + desc = vq->vring.desc; + i = head; + descs_used = total_sg; + + if (vq->num_free < descs_used) { + debug("Can't add buf len %i - avail = %i\n", + descs_used, vq->num_free); + /* + * FIXME: for historical reasons, we force a notify here if + * there are outgoing parts to the buffer. Presumably the + * host should service the ring ASAP. + */ + if (out_sgs) + virtio_notify(vq->vdev, vq); + return -ENOSPC; + } + + for (n = 0; n < out_sgs; n++) { + struct virtio_sg *sg = sgs[n]; + + desc[i].flags = cpu_to_virtio16(vq->vdev, VRING_DESC_F_NEXT); + desc[i].addr = cpu_to_virtio64(vq->vdev, (u64)(size_t)sg->addr); + desc[i].len = cpu_to_virtio32(vq->vdev, sg->length); + + prev = i; + i = virtio16_to_cpu(vq->vdev, desc[i].next); + } + for (; n < (out_sgs + in_sgs); n++) { + struct virtio_sg *sg = sgs[n]; + + desc[i].flags = cpu_to_virtio16(vq->vdev, VRING_DESC_F_NEXT | + VRING_DESC_F_WRITE); + desc[i].addr = cpu_to_virtio64(vq->vdev, + (u64)(uintptr_t)sg->addr); + desc[i].len = cpu_to_virtio32(vq->vdev, sg->length); + + prev = i; + i = virtio16_to_cpu(vq->vdev, desc[i].next); + } + /* Last one doesn't continue */ + desc[prev].flags &= cpu_to_virtio16(vq->vdev, ~VRING_DESC_F_NEXT); + + /* We're using some buffers from the free list. */ + vq->num_free -= descs_used; + + /* Update free pointer */ + vq->free_head = i; + + /* + * Put entry in available array (but don't update avail->idx + * until they do sync). + */ + avail = vq->avail_idx_shadow & (vq->vring.num - 1); + vq->vring.avail->ring[avail] = cpu_to_virtio16(vq->vdev, head); + + /* + * Descriptors and available array need to be set before we expose the + * new available array entries. + */ + virtio_wmb(); + vq->avail_idx_shadow++; + vq->vring.avail->idx = cpu_to_virtio16(vq->vdev, vq->avail_idx_shadow); + vq->num_added++; + + /* + * This is very unlikely, but theoretically possible. + * Kick just in case. + */ + if (unlikely(vq->num_added == (1 << 16) - 1)) + virtqueue_kick(vq); + + return 0; +} + +static bool virtqueue_kick_prepare(struct virtqueue *vq) +{ + u16 new, old; + bool needs_kick; + + /* + * We need to expose available array entries before checking + * avail event. + */ + virtio_mb(); + + old = vq->avail_idx_shadow - vq->num_added; + new = vq->avail_idx_shadow; + vq->num_added = 0; + + if (vq->event) { + needs_kick = vring_need_event(virtio16_to_cpu(vq->vdev, + vring_avail_event(&vq->vring)), new, old); + } else { + needs_kick = !(vq->vring.used->flags & cpu_to_virtio16(vq->vdev, + VRING_USED_F_NO_NOTIFY)); + } + + return needs_kick; +} + +void virtqueue_kick(struct virtqueue *vq) +{ + if (virtqueue_kick_prepare(vq)) + virtio_notify(vq->vdev, vq); +} + +static void detach_buf(struct virtqueue *vq, unsigned int head) +{ + unsigned int i; + __virtio16 nextflag = cpu_to_virtio16(vq->vdev, VRING_DESC_F_NEXT); + + /* Put back on free list: unmap first-level descriptors and find end */ + i = head; + + while (vq->vring.desc[i].flags & nextflag) { + i = virtio16_to_cpu(vq->vdev, vq->vring.desc[i].next); + vq->num_free++; + } + + vq->vring.desc[i].next = cpu_to_virtio16(vq->vdev, vq->free_head); + vq->free_head = head; + + /* Plus final descriptor */ + vq->num_free++; +} + +static inline bool more_used(const struct virtqueue *vq) +{ + return vq->last_used_idx != virtio16_to_cpu(vq->vdev, + vq->vring.used->idx); +} + +void *virtqueue_get_buf(struct virtqueue *vq, unsigned int *len) +{ + unsigned int i; + u16 last_used; + + if (!more_used(vq)) { + debug("(%s.%d): No more buffers in queue\n", + vq->vdev->name, vq->index); + return NULL; + } + + /* Only get used array entries after they have been exposed by host */ + virtio_rmb(); + + last_used = (vq->last_used_idx & (vq->vring.num - 1)); + i = virtio32_to_cpu(vq->vdev, vq->vring.used->ring[last_used].id); + if (len) { + *len = virtio32_to_cpu(vq->vdev, + vq->vring.used->ring[last_used].len); + debug("(%s.%d): last used idx %u with len %u\n", + vq->vdev->name, vq->index, i, *len); + } + + if (unlikely(i >= vq->vring.num)) { + printf("(%s.%d): id %u out of range\n", + vq->vdev->name, vq->index, i); + return NULL; + } + + detach_buf(vq, i); + vq->last_used_idx++; + /* + * If we expect an interrupt for the next entry, tell host + * by writing event index and flush out the write before + * the read in the next get_buf call. + */ + if (!(vq->avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT)) + virtio_store_mb(&vring_used_event(&vq->vring), + cpu_to_virtio16(vq->vdev, vq->last_used_idx)); + + return (void *)(uintptr_t)virtio64_to_cpu(vq->vdev, + vq->vring.desc[i].addr); +} + +static struct virtqueue *__vring_new_virtqueue(unsigned int index, + struct vring vring, + struct udevice *udev) +{ + unsigned int i; + struct virtqueue *vq; + struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(udev); + struct udevice *vdev = uc_priv->vdev; + + vq = malloc(sizeof(*vq)); + if (!vq) + return NULL; + + vq->vdev = vdev; + vq->index = index; + vq->num_free = vring.num; + vq->vring = vring; + vq->last_used_idx = 0; + vq->avail_flags_shadow = 0; + vq->avail_idx_shadow = 0; + vq->num_added = 0; + list_add_tail(&vq->list, &uc_priv->vqs); + + vq->event = virtio_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX); + + /* Tell other side not to bother us */ + vq->avail_flags_shadow |= VRING_AVAIL_F_NO_INTERRUPT; + if (!vq->event) + vq->vring.avail->flags = cpu_to_virtio16(vdev, + vq->avail_flags_shadow); + + /* Put everything in free lists */ + vq->free_head = 0; + for (i = 0; i < vring.num - 1; i++) + vq->vring.desc[i].next = cpu_to_virtio16(vdev, i + 1); + + return vq; +} + +struct virtqueue *vring_create_virtqueue(unsigned int index, unsigned int num, + unsigned int vring_align, + struct udevice *udev) +{ + struct virtqueue *vq; + void *queue = NULL; + struct vring vring; + + /* We assume num is a power of 2 */ + if (num & (num - 1)) { + printf("Bad virtqueue length %u\n", num); + return NULL; + } + + /* TODO: allocate each queue chunk individually */ + for (; num && vring_size(num, vring_align) > PAGE_SIZE; num /= 2) { + queue = memalign(PAGE_SIZE, vring_size(num, vring_align)); + if (queue) + break; + } + + if (!num) + return NULL; + + if (!queue) { + /* Try to get a single page. You are my only hope! */ + queue = memalign(PAGE_SIZE, vring_size(num, vring_align)); + } + if (!queue) + return NULL; + + memset(queue, 0, vring_size(num, vring_align)); + vring_init(&vring, num, queue, vring_align); + + vq = __vring_new_virtqueue(index, vring, udev); + if (!vq) { + free(queue); + return NULL; + } + debug("(%s): created vring @ %p for vq @ %p with num %u\n", udev->name, + queue, vq, num); + + return vq; +} + +void vring_del_virtqueue(struct virtqueue *vq) +{ + free(vq->vring.desc); + list_del(&vq->list); + free(vq); +} + +unsigned int virtqueue_get_vring_size(struct virtqueue *vq) +{ + return vq->vring.num; +} + +ulong virtqueue_get_desc_addr(struct virtqueue *vq) +{ + return (ulong)vq->vring.desc; +} + +ulong virtqueue_get_avail_addr(struct virtqueue *vq) +{ + return (ulong)vq->vring.desc + + ((char *)vq->vring.avail - (char *)vq->vring.desc); +} + +ulong virtqueue_get_used_addr(struct virtqueue *vq) +{ + return (ulong)vq->vring.desc + + ((char *)vq->vring.used - (char *)vq->vring.desc); +} + +bool virtqueue_poll(struct virtqueue *vq, u16 last_used_idx) +{ + virtio_mb(); + + return last_used_idx != virtio16_to_cpu(vq->vdev, vq->vring.used->idx); +} + +void virtqueue_dump(struct virtqueue *vq) +{ + unsigned int i; + + printf("virtqueue %p for dev %s:\n", vq, vq->vdev->name); + printf("\tindex %u, phys addr %p num %u\n", + vq->index, vq->vring.desc, vq->vring.num); + printf("\tfree_head %u, num_added %u, num_free %u\n", + vq->free_head, vq->num_added, vq->num_free); + printf("\tlast_used_idx %u, avail_flags_shadow %u, avail_idx_shadow %u\n", + vq->last_used_idx, vq->avail_flags_shadow, vq->avail_idx_shadow); + + printf("Descriptor dump:\n"); + for (i = 0; i < vq->vring.num; i++) { + printf("\tdesc[%u] = { 0x%llx, len %u, flags %u, next %u }\n", + i, vq->vring.desc[i].addr, vq->vring.desc[i].len, + vq->vring.desc[i].flags, vq->vring.desc[i].next); + } + + printf("Avail ring dump:\n"); + printf("\tflags %u, idx %u\n", + vq->vring.avail->flags, vq->vring.avail->idx); + for (i = 0; i < vq->vring.num; i++) { + printf("\tavail[%u] = %u\n", + i, vq->vring.avail->ring[i]); + } + + printf("Used ring dump:\n"); + printf("\tflags %u, idx %u\n", + vq->vring.used->flags, vq->vring.used->idx); + for (i = 0; i < vq->vring.num; i++) { + printf("\tused[%u] = { %u, %u }\n", i, + vq->vring.used->ring[i].id, vq->vring.used->ring[i].len); + } +} diff --git a/include/virtio_ring.h b/include/virtio_ring.h new file mode 100644 index 0000000..6fc0593 --- /dev/null +++ b/include/virtio_ring.h @@ -0,0 +1,320 @@ +/* SPDX-License-Identifier: BSD-3-Clause */ +/* + * Copyright (C) 2018, Tuomas Tynkkynen + * Copyright (C) 2018, Bin Meng + * + * From Linux kernel include/uapi/linux/virtio_ring.h + */ + +#ifndef _LINUX_VIRTIO_RING_H +#define _LINUX_VIRTIO_RING_H + +#include + +/* This marks a buffer as continuing via the next field */ +#define VRING_DESC_F_NEXT 1 +/* This marks a buffer as write-only (otherwise read-only) */ +#define VRING_DESC_F_WRITE 2 +/* This means the buffer contains a list of buffer descriptors */ +#define VRING_DESC_F_INDIRECT 4 + +/* + * The Host uses this in used->flags to advise the Guest: don't kick me when + * you add a buffer. It's unreliable, so it's simply an optimization. Guest + * will still kick if it's out of buffers. + */ +#define VRING_USED_F_NO_NOTIFY 1 + +/* + * The Guest uses this in avail->flags to advise the Host: don't interrupt me + * when you consume a buffer. It's unreliable, so it's simply an optimization. + */ +#define VRING_AVAIL_F_NO_INTERRUPT 1 + +/* We support indirect buffer descriptors */ +#define VIRTIO_RING_F_INDIRECT_DESC 28 + +/* + * The Guest publishes the used index for which it expects an interrupt + * at the end of the avail ring. Host should ignore the avail->flags field. + * + * The Host publishes the avail index for which it expects a kick + * at the end of the used ring. Guest should ignore the used->flags field. + */ +#define VIRTIO_RING_F_EVENT_IDX 29 + +/* Virtio ring descriptors: 16 bytes. These can chain together via "next". */ +struct vring_desc { + /* Address (guest-physical) */ + __virtio64 addr; + /* Length */ + __virtio32 len; + /* The flags as indicated above */ + __virtio16 flags; + /* We chain unused descriptors via this, too */ + __virtio16 next; +}; + +struct vring_avail { + __virtio16 flags; + __virtio16 idx; + __virtio16 ring[]; +}; + +struct vring_used_elem { + /* Index of start of used descriptor chain */ + __virtio32 id; + /* Total length of the descriptor chain which was used (written to) */ + __virtio32 len; +}; + +struct vring_used { + __virtio16 flags; + __virtio16 idx; + struct vring_used_elem ring[]; +}; + +struct vring { + unsigned int num; + struct vring_desc *desc; + struct vring_avail *avail; + struct vring_used *used; +}; + +/** + * virtqueue - a queue to register buffers for sending or receiving. + * + * @list: the chain of virtqueues for this device + * @vdev: the virtio device this queue was created for + * @index: the zero-based ordinal number for this queue + * @num_free: number of elements we expect to be able to fit + * @vring: actual memory layout for this queue + * @event: host publishes avail event idx + * @free_head: head of free buffer list + * @num_added: number we've added since last sync + * @last_used_idx: last used index we've seen + * @avail_flags_shadow: last written value to avail->flags + * @avail_idx_shadow: last written value to avail->idx in guest byte order + */ +struct virtqueue { + struct list_head list; + struct udevice *vdev; + unsigned int index; + unsigned int num_free; + struct vring vring; + bool event; + unsigned int free_head; + unsigned int num_added; + u16 last_used_idx; + u16 avail_flags_shadow; + u16 avail_idx_shadow; +}; + +/* + * Alignment requirements for vring elements. + * When using pre-virtio 1.0 layout, these fall out naturally. + */ +#define VRING_AVAIL_ALIGN_SIZE 2 +#define VRING_USED_ALIGN_SIZE 4 +#define VRING_DESC_ALIGN_SIZE 16 + +/* + * We publish the used event index at the end of the available ring, + * and vice versa. They are at the end for backwards compatibility. + */ +#define vring_used_event(vr) ((vr)->avail->ring[(vr)->num]) +#define vring_avail_event(vr) (*(__virtio16 *)&(vr)->used->ring[(vr)->num]) + +static inline void vring_init(struct vring *vr, unsigned int num, void *p, + unsigned long align) +{ + vr->num = num; + vr->desc = p; + vr->avail = p + num * sizeof(struct vring_desc); + vr->used = (void *)(((uintptr_t)&vr->avail->ring[num] + + sizeof(__virtio16) + align - 1) & ~(align - 1)); +} + +static inline unsigned int vring_size(unsigned int num, unsigned long align) +{ + return ((sizeof(struct vring_desc) * num + + sizeof(__virtio16) * (3 + num) + align - 1) & ~(align - 1)) + + sizeof(__virtio16) * 3 + sizeof(struct vring_used_elem) * num; +} + +/* + * The following is used with USED_EVENT_IDX and AVAIL_EVENT_IDX. + * Assuming a given event_idx value from the other side, if we have just + * incremented index from old to new_idx, should we trigger an event? + */ +static inline int vring_need_event(__u16 event_idx, __u16 new_idx, __u16 old) +{ + /* + * Note: Xen has similar logic for notification hold-off + * in include/xen/interface/io/ring.h with req_event and req_prod + * corresponding to event_idx + 1 and new_idx respectively. + * Note also that req_event and req_prod in Xen start at 1, + * event indexes in virtio start at 0. + */ + return (__u16)(new_idx - event_idx - 1) < (__u16)(new_idx - old); +} + +struct virtio_sg; + +/** + * virtqueue_add - expose buffers to other end + * + * @vq: the struct virtqueue we're talking about + * @sgs: array of terminated scatterlists + * @out_sgs: the number of scatterlists readable by other side + * @in_sgs: the number of scatterlists which are writable + * (after readable ones) + * + * Caller must ensure we don't call this with other virtqueue operations + * at the same time (except where noted). + * + * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO). + */ +int virtqueue_add(struct virtqueue *vq, struct virtio_sg *sgs[], + unsigned int out_sgs, unsigned int in_sgs); + +/** + * virtqueue_kick - update after add_buf + * + * @vq: the struct virtqueue + * + * After one or more virtqueue_add() calls, invoke this to kick + * the other side. + * + * Caller must ensure we don't call this with other virtqueue + * operations at the same time (except where noted). + */ +void virtqueue_kick(struct virtqueue *vq); + +/** + * virtqueue_get_buf - get the next used buffer + * + * @vq: the struct virtqueue we're talking about + * @len: the length written into the buffer + * + * If the device wrote data into the buffer, @len will be set to the + * amount written. This means you don't need to clear the buffer + * beforehand to ensure there's no data leakage in the case of short + * writes. + * + * Caller must ensure we don't call this with other virtqueue + * operations at the same time (except where noted). + * + * Returns NULL if there are no used buffers, or the memory buffer + * handed to virtqueue_add_*(). + */ +void *virtqueue_get_buf(struct virtqueue *vq, unsigned int *len); + +/** + * vring_create_virtqueue - create a virtqueue for a virtio device + * + * @index: the index of the queue + * @num: number of elements of the queue + * @vring_align:the alignment requirement of the descriptor ring + * @udev: the virtio transport udevice + * @return: the virtqueue pointer or NULL if failed + * + * This creates a virtqueue and allocates the descriptor ring for a virtio + * device. The caller should query virtqueue_get_ring_size() to learn the + * actual size of the ring. + * + * This API is supposed to be called by the virtio transport driver in the + * virtio find_vqs() uclass method. + */ +struct virtqueue *vring_create_virtqueue(unsigned int index, unsigned int num, + unsigned int vring_align, + struct udevice *udev); + +/** + * vring_del_virtqueue - destroy a virtqueue + * + * @vq: the struct virtqueue we're talking about + * + * This destroys a virtqueue. If created with vring_create_virtqueue(), + * this also frees the descriptor ring. + * + * This API is supposed to be called by the virtio transport driver in the + * virtio del_vqs() uclass method. + */ +void vring_del_virtqueue(struct virtqueue *vq); + +/** + * virtqueue_get_vring_size - get the size of the virtqueue's vring + * + * @vq: the struct virtqueue containing the vring of interest + * @return: the size of the vring in a virtqueue. + */ +unsigned int virtqueue_get_vring_size(struct virtqueue *vq); + +/** + * virtqueue_get_desc_addr - get the vring descriptor table address + * + * @vq: the struct virtqueue containing the vring of interest + * @return: the descriptor table address of the vring in a virtqueue. + */ +ulong virtqueue_get_desc_addr(struct virtqueue *vq); + +/** + * virtqueue_get_avail_addr - get the vring available ring address + * + * @vq: the struct virtqueue containing the vring of interest + * @return: the available ring address of the vring in a virtqueue. + */ +ulong virtqueue_get_avail_addr(struct virtqueue *vq); + +/** + * virtqueue_get_used_addr - get the vring used ring address + * + * @vq: the struct virtqueue containing the vring of interest + * @return: the used ring address of the vring in a virtqueue. + */ +ulong virtqueue_get_used_addr(struct virtqueue *vq); + +/** + * virtqueue_poll - query pending used buffers + * + * @vq: the struct virtqueue we're talking about + * @last_used_idx: virtqueue last used index + * + * Returns "true" if there are pending used buffers in the queue. + */ +bool virtqueue_poll(struct virtqueue *vq, u16 last_used_idx); + +/** + * virtqueue_dump - dump the virtqueue for debugging + * + * @vq: the struct virtqueue we're talking about + * + * Caller must ensure we don't call this with other virtqueue operations + * at the same time (except where noted). + */ +void virtqueue_dump(struct virtqueue *vq); + +/* + * Barriers in virtio are tricky. Since we are not in a hyperviosr/guest + * scenario, having these as nops is enough to work as expected. + */ + +static inline void virtio_mb(void) +{ +} + +static inline void virtio_rmb(void) +{ +} + +static inline void virtio_wmb(void) +{ +} + +static inline void virtio_store_mb(__virtio16 *p, __virtio16 v) +{ + WRITE_ONCE(*p, v); +} + +#endif /* _LINUX_VIRTIO_RING_H */ From patchwork Mon Oct 15 09:21:02 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Bin Meng X-Patchwork-Id: 984003 X-Patchwork-Delegate: sjg@chromium.org Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=lists.denx.de (client-ip=81.169.180.215; helo=lists.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="Gu5dPkyZ"; dkim-atps=neutral Received: from lists.denx.de (dione.denx.de [81.169.180.215]) by ozlabs.org (Postfix) with ESMTP id 42YXy15jGYz9sBq for ; Mon, 15 Oct 2018 20:21:45 +1100 (AEDT) Received: by lists.denx.de (Postfix, from userid 105) id 708A7C21E4E; Mon, 15 Oct 2018 09:20:41 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on lists.denx.de X-Spam-Level: X-Spam-Status: No, score=-0.0 required=5.0 tests=FREEMAIL_FROM, RCVD_IN_DNSWL_BLOCKED, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, T_DKIM_INVALID autolearn=unavailable autolearn_force=no version=3.4.0 Received: from lists.denx.de (localhost [IPv6:::1]) by lists.denx.de (Postfix) with ESMTP id 5E668C21E1E; Mon, 15 Oct 2018 09:16:57 +0000 (UTC) Received: by lists.denx.de (Postfix, from userid 105) id 595E4C21E75; Mon, 15 Oct 2018 09:16:48 +0000 (UTC) Received: from mail-pf1-f193.google.com (mail-pf1-f193.google.com [209.85.210.193]) by lists.denx.de (Postfix) with ESMTPS id A33B5C21E2B for ; Mon, 15 Oct 2018 09:16:40 +0000 (UTC) Received: by mail-pf1-f193.google.com with SMTP id j23-v6so9360180pfi.4 for ; Mon, 15 Oct 2018 02:16:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=egY3gHICrmAuFMYylGvNKrk/WIgWYrjT8JFf7cewhoY=; b=Gu5dPkyZ9zzKiT+ZzBXD5lir79i85Hm6B7Sdc3j17I6yWxTa9KN3mjUVBBBFmqc8Dl yafeWSU6HIWOdyTlzC23+l27cIkwWFLWGMK5HyXlR40RSIN9aw6qiE8pgqn9I3tP4TwY OLPqyiCJLxmp4kLJleyfD/ZRWkuYtaaqNAFY1X3IyQoZHsHMSAVosc9QOoDfv8MKiXqQ 6Yy1ZViQZgnlhXwzKu5nfc9jS5Mn+dWyc0nh/0VMZKMZkTVdyGpcHOMkmM1eCIg7A5Bc j7zScMMu98OPIq33V6FKIY9DVpGvv4rAnQZKDB7VNXcVakzlsnXorSp6Sdm/paPJfvhd 7tLA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=egY3gHICrmAuFMYylGvNKrk/WIgWYrjT8JFf7cewhoY=; b=MeAs6Qd6DNU8cg/fDCC/70u+saKnOJ32XGQN80gcDMaiAH1CUtakwnk9/Ggp09glLJ lSkPX6qzRiVdz6Xe3g+sB80S7MxFBl5YxKQFOPFNpDR6VTLb90UKhLMp3DuRk9K4Ms72 T0RmK3PYUY1wMZparoll1/y2iPRkQzmgYJM5b4fga2Oyjv8hQ9J024ZOYjRzxDnLUTkP YAQby0RbKozJtb+Mug6AXakRZM/3m6mtj5qeXkM2x3COxcD9JGmlI1z6uzoR1cewoCgJ yLsGxgugmwpyj66SaeA0hbpRtVkis2d2cKwmMsfhMJX4LPY1smeneTQ8vEDL8WckVJkF zuFA== X-Gm-Message-State: ABuFfojEasRhQYv1Htd8IrkoHQD/pjI5laMxe8SWc1rZU3uaDORN9WB5 Fk8RllSIyCWHwV8J35tKkFk= X-Google-Smtp-Source: ACcGV63TZQ1XTLvNeN/LwoDUfH5sJKvlNeFRp8/oZFMSNJiQMkh5QULbkZBqss1EUpVTtlYBsLtUfQ== X-Received: by 2002:a62:7107:: with SMTP id m7-v6mr9115742pfc.56.1539594998938; Mon, 15 Oct 2018 02:16:38 -0700 (PDT) Received: from ala-d2121-lx1.wrs.com (unknown-156-139.windriver.com. [147.11.156.139]) by smtp.gmail.com with ESMTPSA id a11-v6sm10798256pgw.54.2018.10.15.02.16.37 (version=TLS1_1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 15 Oct 2018 02:16:38 -0700 (PDT) From: Bin Meng To: Simon Glass , U-Boot Mailing List Date: Mon, 15 Oct 2018 02:21:02 -0700 Message-Id: <1539595287-31378-7-git-send-email-bmeng.cn@gmail.com> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: <1539595287-31378-1-git-send-email-bmeng.cn@gmail.com> References: <1539595287-31378-1-git-send-email-bmeng.cn@gmail.com> MIME-Version: 1.0 Subject: [U-Boot] [PATCH v3 06/31] virtio: Add virtio over mmio transport driver X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.18 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" VirtIO can use various different buses and virtio devices are commonly implemented as PCI devices. But virtual environments without PCI support (a common situation in embedded devices models) might use simple memory mapped device (“virtio-mmio”) instead of the PCI device. This adds a transport driver that implements UCLASS_VIRTIO for virtio over mmio. Signed-off-by: Tuomas Tynkkynen Signed-off-by: Bin Meng Reviewed-by: Simon Glass Signed-off-by: Tuomas Tynkkynen Signed-off-by: Bin Meng Reviewed-by: Simon Glass --- Changes in v3: None Changes in v2: None drivers/virtio/Kconfig | 7 + drivers/virtio/Makefile | 1 + drivers/virtio/virtio_mmio.c | 413 +++++++++++++++++++++++++++++++++++++++++++ drivers/virtio/virtio_mmio.h | 129 ++++++++++++++ 4 files changed, 550 insertions(+) create mode 100644 drivers/virtio/virtio_mmio.c create mode 100644 drivers/virtio/virtio_mmio.h diff --git a/drivers/virtio/Kconfig b/drivers/virtio/Kconfig index 82fc536..4f9a11b 100644 --- a/drivers/virtio/Kconfig +++ b/drivers/virtio/Kconfig @@ -22,4 +22,11 @@ config VIRTIO This option is selected by any driver which implements the virtio transport, such as CONFIG_VIRTIO_MMIO or CONFIG_VIRTIO_PCI. +config VIRTIO_MMIO + bool "Platform bus driver for memory mapped virtio devices" + select VIRTIO + help + This driver provides support for memory mapped virtio + platform device driver. + endmenu diff --git a/drivers/virtio/Makefile b/drivers/virtio/Makefile index 17d264a..2e48785 100644 --- a/drivers/virtio/Makefile +++ b/drivers/virtio/Makefile @@ -4,3 +4,4 @@ # Copyright (C) 2018, Bin Meng obj-y += virtio-uclass.o virtio_ring.o +obj-$(CONFIG_VIRTIO_MMIO) += virtio_mmio.o diff --git a/drivers/virtio/virtio_mmio.c b/drivers/virtio/virtio_mmio.c new file mode 100644 index 0000000..7b73870 --- /dev/null +++ b/drivers/virtio/virtio_mmio.c @@ -0,0 +1,413 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * Copyright (C) 2018, Tuomas Tynkkynen + * Copyright (C) 2018, Bin Meng + * + * VirtIO memory-maped I/O transport driver + * Ported from Linux drivers/virtio/virtio_mmio.c + */ + +#include +#include +#include +#include +#include +#include +#include +#include "virtio_mmio.h" + +static int virtio_mmio_get_config(struct udevice *udev, unsigned int offset, + void *buf, unsigned int len) +{ + struct virtio_mmio_priv *priv = dev_get_priv(udev); + void __iomem *base = priv->base + VIRTIO_MMIO_CONFIG; + u8 b; + __le16 w; + __le32 l; + + if (priv->version == 1) { + u8 *ptr = buf; + int i; + + for (i = 0; i < len; i++) + ptr[i] = readb(base + offset + i); + + return 0; + } + + switch (len) { + case 1: + b = readb(base + offset); + memcpy(buf, &b, sizeof(b)); + break; + case 2: + w = cpu_to_le16(readw(base + offset)); + memcpy(buf, &w, sizeof(w)); + break; + case 4: + l = cpu_to_le32(readl(base + offset)); + memcpy(buf, &l, sizeof(l)); + break; + case 8: + l = cpu_to_le32(readl(base + offset)); + memcpy(buf, &l, sizeof(l)); + l = cpu_to_le32(readl(base + offset + sizeof(l))); + memcpy(buf + sizeof(l), &l, sizeof(l)); + break; + default: + WARN_ON(true); + } + + return 0; +} + +static int virtio_mmio_set_config(struct udevice *udev, unsigned int offset, + const void *buf, unsigned int len) +{ + struct virtio_mmio_priv *priv = dev_get_priv(udev); + void __iomem *base = priv->base + VIRTIO_MMIO_CONFIG; + u8 b; + __le16 w; + __le32 l; + + if (priv->version == 1) { + const u8 *ptr = buf; + int i; + + for (i = 0; i < len; i++) + writeb(ptr[i], base + offset + i); + + return 0; + } + + switch (len) { + case 1: + memcpy(&b, buf, sizeof(b)); + writeb(b, base + offset); + break; + case 2: + memcpy(&w, buf, sizeof(w)); + writew(le16_to_cpu(w), base + offset); + break; + case 4: + memcpy(&l, buf, sizeof(l)); + writel(le32_to_cpu(l), base + offset); + break; + case 8: + memcpy(&l, buf, sizeof(l)); + writel(le32_to_cpu(l), base + offset); + memcpy(&l, buf + sizeof(l), sizeof(l)); + writel(le32_to_cpu(l), base + offset + sizeof(l)); + break; + default: + WARN_ON(true); + } + + return 0; +} + +static int virtio_mmio_generation(struct udevice *udev, u32 *counter) +{ + struct virtio_mmio_priv *priv = dev_get_priv(udev); + + if (priv->version == 1) + *counter = 0; + else + *counter = readl(priv->base + VIRTIO_MMIO_CONFIG_GENERATION); + + return 0; +} + +static int virtio_mmio_get_status(struct udevice *udev, u8 *status) +{ + struct virtio_mmio_priv *priv = dev_get_priv(udev); + + *status = readl(priv->base + VIRTIO_MMIO_STATUS) & 0xff; + + return 0; +} + +static int virtio_mmio_set_status(struct udevice *udev, u8 status) +{ + struct virtio_mmio_priv *priv = dev_get_priv(udev); + + /* We should never be setting status to 0 */ + WARN_ON(status == 0); + + writel(status, priv->base + VIRTIO_MMIO_STATUS); + + return 0; +} + +static int virtio_mmio_reset(struct udevice *udev) +{ + struct virtio_mmio_priv *priv = dev_get_priv(udev); + + /* 0 status means a reset */ + writel(0, priv->base + VIRTIO_MMIO_STATUS); + + return 0; +} + +static int virtio_mmio_get_features(struct udevice *udev, u64 *features) +{ + struct virtio_mmio_priv *priv = dev_get_priv(udev); + + writel(1, priv->base + VIRTIO_MMIO_DEVICE_FEATURES_SEL); + *features = readl(priv->base + VIRTIO_MMIO_DEVICE_FEATURES); + *features <<= 32; + + writel(0, priv->base + VIRTIO_MMIO_DEVICE_FEATURES_SEL); + *features |= readl(priv->base + VIRTIO_MMIO_DEVICE_FEATURES); + + return 0; +} + +static int virtio_mmio_set_features(struct udevice *udev) +{ + struct virtio_mmio_priv *priv = dev_get_priv(udev); + struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(udev); + + /* Make sure there is are no mixed devices */ + if (priv->version == 2 && uc_priv->legacy) { + debug("New virtio-mmio devices (version 2) must provide VIRTIO_F_VERSION_1 feature!\n"); + return -EINVAL; + } + + writel(1, priv->base + VIRTIO_MMIO_DRIVER_FEATURES_SEL); + writel((u32)(uc_priv->features >> 32), + priv->base + VIRTIO_MMIO_DRIVER_FEATURES); + + writel(0, priv->base + VIRTIO_MMIO_DRIVER_FEATURES_SEL); + writel((u32)uc_priv->features, + priv->base + VIRTIO_MMIO_DRIVER_FEATURES); + + return 0; +} + +static struct virtqueue *virtio_mmio_setup_vq(struct udevice *udev, + unsigned int index) +{ + struct virtio_mmio_priv *priv = dev_get_priv(udev); + struct virtqueue *vq; + unsigned int num; + int err; + + /* Select the queue we're interested in */ + writel(index, priv->base + VIRTIO_MMIO_QUEUE_SEL); + + /* Queue shouldn't already be set up */ + if (readl(priv->base + (priv->version == 1 ? + VIRTIO_MMIO_QUEUE_PFN : VIRTIO_MMIO_QUEUE_READY))) { + err = -ENOENT; + goto error_available; + } + + num = readl(priv->base + VIRTIO_MMIO_QUEUE_NUM_MAX); + if (num == 0) { + err = -ENOENT; + goto error_new_virtqueue; + } + + /* Create the vring */ + vq = vring_create_virtqueue(index, num, VIRTIO_MMIO_VRING_ALIGN, udev); + if (!vq) { + err = -ENOMEM; + goto error_new_virtqueue; + } + + /* Activate the queue */ + writel(virtqueue_get_vring_size(vq), + priv->base + VIRTIO_MMIO_QUEUE_NUM); + if (priv->version == 1) { + u64 q_pfn = virtqueue_get_desc_addr(vq) >> PAGE_SHIFT; + + /* + * virtio-mmio v1 uses a 32bit QUEUE PFN. If we have something + * that doesn't fit in 32bit, fail the setup rather than + * pretending to be successful. + */ + if (q_pfn >> 32) { + debug("platform bug: legacy virtio-mmio must not be used with RAM above 0x%llxGB\n", + 0x1ULL << (32 + PAGE_SHIFT - 30)); + err = -E2BIG; + goto error_bad_pfn; + } + + writel(PAGE_SIZE, priv->base + VIRTIO_MMIO_QUEUE_ALIGN); + writel(q_pfn, priv->base + VIRTIO_MMIO_QUEUE_PFN); + } else { + u64 addr; + + addr = virtqueue_get_desc_addr(vq); + writel((u32)addr, priv->base + VIRTIO_MMIO_QUEUE_DESC_LOW); + writel((u32)(addr >> 32), + priv->base + VIRTIO_MMIO_QUEUE_DESC_HIGH); + + addr = virtqueue_get_avail_addr(vq); + writel((u32)addr, priv->base + VIRTIO_MMIO_QUEUE_AVAIL_LOW); + writel((u32)(addr >> 32), + priv->base + VIRTIO_MMIO_QUEUE_AVAIL_HIGH); + + addr = virtqueue_get_used_addr(vq); + writel((u32)addr, priv->base + VIRTIO_MMIO_QUEUE_USED_LOW); + writel((u32)(addr >> 32), + priv->base + VIRTIO_MMIO_QUEUE_USED_HIGH); + + writel(1, priv->base + VIRTIO_MMIO_QUEUE_READY); + } + + return vq; + +error_bad_pfn: + vring_del_virtqueue(vq); + +error_new_virtqueue: + if (priv->version == 1) { + writel(0, priv->base + VIRTIO_MMIO_QUEUE_PFN); + } else { + writel(0, priv->base + VIRTIO_MMIO_QUEUE_READY); + WARN_ON(readl(priv->base + VIRTIO_MMIO_QUEUE_READY)); + } + +error_available: + return ERR_PTR(err); +} + +static void virtio_mmio_del_vq(struct virtqueue *vq) +{ + struct virtio_mmio_priv *priv = dev_get_priv(vq->vdev); + unsigned int index = vq->index; + + /* Select and deactivate the queue */ + writel(index, priv->base + VIRTIO_MMIO_QUEUE_SEL); + if (priv->version == 1) { + writel(0, priv->base + VIRTIO_MMIO_QUEUE_PFN); + } else { + writel(0, priv->base + VIRTIO_MMIO_QUEUE_READY); + WARN_ON(readl(priv->base + VIRTIO_MMIO_QUEUE_READY)); + } + + vring_del_virtqueue(vq); +} + +static int virtio_mmio_del_vqs(struct udevice *udev) +{ + struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(udev); + struct virtqueue *vq, *n; + + list_for_each_entry_safe(vq, n, &uc_priv->vqs, list) + virtio_mmio_del_vq(vq); + + return 0; +} + +static int virtio_mmio_find_vqs(struct udevice *udev, unsigned int nvqs, + struct virtqueue *vqs[]) +{ + int i; + + for (i = 0; i < nvqs; ++i) { + vqs[i] = virtio_mmio_setup_vq(udev, i); + if (IS_ERR(vqs[i])) { + virtio_mmio_del_vqs(udev); + return PTR_ERR(vqs[i]); + } + } + + return 0; +} + +static int virtio_mmio_notify(struct udevice *udev, struct virtqueue *vq) +{ + struct virtio_mmio_priv *priv = dev_get_priv(udev); + + /* + * We write the queue's selector into the notification register + * to signal the other end + */ + writel(vq->index, priv->base + VIRTIO_MMIO_QUEUE_NOTIFY); + + return 0; +} + +static int virtio_mmio_ofdata_to_platdata(struct udevice *udev) +{ + struct virtio_mmio_priv *priv = dev_get_priv(udev); + + priv->base = (void __iomem *)(ulong)dev_read_addr(udev); + if (priv->base == (void __iomem *)FDT_ADDR_T_NONE) + return -EINVAL; + + return 0; +} + +static int virtio_mmio_probe(struct udevice *udev) +{ + struct virtio_mmio_priv *priv = dev_get_priv(udev); + struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(udev); + u32 magic; + + /* Check magic value */ + magic = readl(priv->base + VIRTIO_MMIO_MAGIC_VALUE); + if (magic != ('v' | 'i' << 8 | 'r' << 16 | 't' << 24)) { + debug("(%s): wrong magic value 0x%08x!\n", udev->name, magic); + return 0; + } + + /* Check device version */ + priv->version = readl(priv->base + VIRTIO_MMIO_VERSION); + if (priv->version < 1 || priv->version > 2) { + debug("(%s): version %d not supported!\n", + udev->name, priv->version); + return 0; + } + + /* Check devicd ID */ + uc_priv->device = readl(priv->base + VIRTIO_MMIO_DEVICE_ID); + if (uc_priv->device == 0) { + /* + * virtio-mmio device with an ID 0 is a (dummy) placeholder + * with no function. End probing now with no error reported. + */ + return 0; + } + uc_priv->vendor = readl(priv->base + VIRTIO_MMIO_VENDOR_ID); + + if (priv->version == 1) + writel(PAGE_SIZE, priv->base + VIRTIO_MMIO_GUEST_PAGE_SIZE); + + debug("(%s): device (%d) vendor (%08x) version (%d)\n", udev->name, + uc_priv->device, uc_priv->vendor, priv->version); + + return 0; +} + +static const struct dm_virtio_ops virtio_mmio_ops = { + .get_config = virtio_mmio_get_config, + .set_config = virtio_mmio_set_config, + .generation = virtio_mmio_generation, + .get_status = virtio_mmio_get_status, + .set_status = virtio_mmio_set_status, + .reset = virtio_mmio_reset, + .get_features = virtio_mmio_get_features, + .set_features = virtio_mmio_set_features, + .find_vqs = virtio_mmio_find_vqs, + .del_vqs = virtio_mmio_del_vqs, + .notify = virtio_mmio_notify, +}; + +static const struct udevice_id virtio_mmio_ids[] = { + { .compatible = "virtio,mmio" }, + { } +}; + +U_BOOT_DRIVER(virtio_mmio) = { + .name = "virtio-mmio", + .id = UCLASS_VIRTIO, + .of_match = virtio_mmio_ids, + .ops = &virtio_mmio_ops, + .probe = virtio_mmio_probe, + .ofdata_to_platdata = virtio_mmio_ofdata_to_platdata, + .priv_auto_alloc_size = sizeof(struct virtio_mmio_priv), +}; diff --git a/drivers/virtio/virtio_mmio.h b/drivers/virtio/virtio_mmio.h new file mode 100644 index 0000000..b340882 --- /dev/null +++ b/drivers/virtio/virtio_mmio.h @@ -0,0 +1,129 @@ +/* SPDX-License-Identifier: GPL-2.0+ */ +/* + * Copyright (C) 2018, Tuomas Tynkkynen + * Copyright (C) 2018, Bin Meng + * + * From Linux kernel include/uapi/linux/virtio_mmio.h + */ + +#ifndef _LINUX_VIRTIO_MMIO_H +#define _LINUX_VIRTIO_MMIO_H + +/* Control registers */ + +/* Magic value ("virt" string) - Read Only */ +#define VIRTIO_MMIO_MAGIC_VALUE 0x000 + +/* Virtio device version - Read Only */ +#define VIRTIO_MMIO_VERSION 0x004 + +/* Virtio device ID - Read Only */ +#define VIRTIO_MMIO_DEVICE_ID 0x008 + +/* Virtio vendor ID - Read Only */ +#define VIRTIO_MMIO_VENDOR_ID 0x00c + +/* + * Bitmask of the features supported by the device (host) + * (32 bits per set) - Read Only + */ +#define VIRTIO_MMIO_DEVICE_FEATURES 0x010 + +/* Device (host) features set selector - Write Only */ +#define VIRTIO_MMIO_DEVICE_FEATURES_SEL 0x014 + +/* + * Bitmask of features activated by the driver (guest) + * (32 bits per set) - Write Only + */ +#define VIRTIO_MMIO_DRIVER_FEATURES 0x020 + +/* Activated features set selector - Write Only */ +#define VIRTIO_MMIO_DRIVER_FEATURES_SEL 0x024 + +#ifndef VIRTIO_MMIO_NO_LEGACY /* LEGACY DEVICES ONLY! */ + +/* Guest's memory page size in bytes - Write Only */ +#define VIRTIO_MMIO_GUEST_PAGE_SIZE 0x028 + +#endif + +/* Queue selector - Write Only */ +#define VIRTIO_MMIO_QUEUE_SEL 0x030 + +/* Maximum size of the currently selected queue - Read Only */ +#define VIRTIO_MMIO_QUEUE_NUM_MAX 0x034 + +/* Queue size for the currently selected queue - Write Only */ +#define VIRTIO_MMIO_QUEUE_NUM 0x038 + +#ifndef VIRTIO_MMIO_NO_LEGACY /* LEGACY DEVICES ONLY! */ + +/* Used Ring alignment for the currently selected queue - Write Only */ +#define VIRTIO_MMIO_QUEUE_ALIGN 0x03c + +/* Guest's PFN for the currently selected queue - Read Write */ +#define VIRTIO_MMIO_QUEUE_PFN 0x040 + +#endif + +/* Ready bit for the currently selected queue - Read Write */ +#define VIRTIO_MMIO_QUEUE_READY 0x044 + +/* Queue notifier - Write Only */ +#define VIRTIO_MMIO_QUEUE_NOTIFY 0x050 + +/* Interrupt status - Read Only */ +#define VIRTIO_MMIO_INTERRUPT_STATUS 0x060 + +/* Interrupt acknowledge - Write Only */ +#define VIRTIO_MMIO_INTERRUPT_ACK 0x064 + +/* Device status register - Read Write */ +#define VIRTIO_MMIO_STATUS 0x070 + +/* Selected queue's Descriptor Table address, 64 bits in two halves */ +#define VIRTIO_MMIO_QUEUE_DESC_LOW 0x080 +#define VIRTIO_MMIO_QUEUE_DESC_HIGH 0x084 + +/* Selected queue's Available Ring address, 64 bits in two halves */ +#define VIRTIO_MMIO_QUEUE_AVAIL_LOW 0x090 +#define VIRTIO_MMIO_QUEUE_AVAIL_HIGH 0x094 + +/* Selected queue's Used Ring address, 64 bits in two halves */ +#define VIRTIO_MMIO_QUEUE_USED_LOW 0x0a0 +#define VIRTIO_MMIO_QUEUE_USED_HIGH 0x0a4 + +/* Configuration atomicity value */ +#define VIRTIO_MMIO_CONFIG_GENERATION 0x0fc + +/* + * The config space is defined by each driver as + * the per-driver configuration space - Read Write + */ +#define VIRTIO_MMIO_CONFIG 0x100 + +/* Interrupt flags (re: interrupt status & acknowledge registers) */ + +#define VIRTIO_MMIO_INT_VRING BIT(0) +#define VIRTIO_MMIO_INT_CONFIG BIT(1) + +/* + * The alignment to use between consumer and producer parts of vring. + * Currently hardcoded to the page size. + */ +#define PAGE_SHIFT 12 +#define VIRTIO_MMIO_VRING_ALIGN PAGE_SIZE + +/** + * virtio mmio transport driver private data + * + * @base: mmio transport device register base + * @version: mmio transport device version + */ +struct virtio_mmio_priv { + void __iomem *base; + u32 version; +}; + +#endif /* _LINUX_VIRTIO_MMIO_H */ From patchwork Mon Oct 15 09:21:03 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bin Meng X-Patchwork-Id: 984001 X-Patchwork-Delegate: sjg@chromium.org Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=lists.denx.de (client-ip=81.169.180.215; helo=lists.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="lh33us5s"; dkim-atps=neutral Received: from lists.denx.de (dione.denx.de [81.169.180.215]) by ozlabs.org (Postfix) with ESMTP id 42YXxF039wz9sBj for ; Mon, 15 Oct 2018 20:21:04 +1100 (AEDT) Received: by lists.denx.de (Postfix, from userid 105) id B5BDAC21DC1; Mon, 15 Oct 2018 09:19:36 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on lists.denx.de X-Spam-Level: X-Spam-Status: No, score=-0.0 required=5.0 tests=FREEMAIL_FROM, RCVD_IN_DNSWL_BLOCKED, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, T_DKIM_INVALID autolearn=unavailable autolearn_force=no version=3.4.0 Received: from lists.denx.de (localhost [IPv6:::1]) by lists.denx.de (Postfix) with ESMTP id E5EB2C21E63; Mon, 15 Oct 2018 09:16:53 +0000 (UTC) Received: by lists.denx.de (Postfix, from userid 105) id 5C2EDC21D9A; Mon, 15 Oct 2018 09:16:48 +0000 (UTC) Received: from mail-pl1-f193.google.com (mail-pl1-f193.google.com [209.85.214.193]) by lists.denx.de (Postfix) with ESMTPS id CFF3DC21D83 for ; Mon, 15 Oct 2018 09:16:41 +0000 (UTC) Received: by mail-pl1-f193.google.com with SMTP id f18-v6so9004068plr.1 for ; Mon, 15 Oct 2018 02:16:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=O/FPKGu+RFzDAP+uXpR3JNPWmtsaM+sCQSWFIeoXaCI=; b=lh33us5stIDtn2YcRn/cvKJQKKgh2hobY3pndz/1+OeMrLBOJycE/cQdW/t0wAzaYZ w1Rb7uaD3LH+k2SPD02/8uYAtuKmg3YSUFv2R3FUSVRSSD5X3PPYgagOPQPMGWDHd2i9 ZAOn4hPzMcvgnR9pm1Jv/8yobWmdna3JyE//P/M9z8EllxxD+eOQtlt3R8X98MG/SyU/ WNW7hUIM1KB2yyRbQfur6Za2WszfXqukyDWtBLMKZLLhzmZbuCshlK7RwtTax7zLuQ2l I5DU6qZeEvxxacU4IA7CWqL7OUfxP4GNTcnFF4xYirgf6r5lNgdp/Gcv74hRvDT1Jy2y h7XQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=O/FPKGu+RFzDAP+uXpR3JNPWmtsaM+sCQSWFIeoXaCI=; b=Xi2Nc7kVWhcBofFShDCz7VSoNEyxmFo2B/oE6CS8UUNggN++EGsxxoXXqFql/xmLR4 eW8noypJsGwBTd/6FEZpOgMTcPj1WGoQGKcfXqtAygeo0Q3Vg8U+5LwrFAU2Xrnvwwpq bzI83NiWA9qm04e6SIu7brBWb7EWVJHtGD1yFkNyUH/vUo+Hdrq9MVxBBDdtu1e6xGzg npg9ymQqVSG6ASq5NniDloS8AVC26Fgt3SiBGFQ2+kA07ViCv58BTptaj0mq6NpfSUT+ C6TyVz0In0UwGe9eZbUsShWVZxcaCPayod3psaF4jVZp4HsOchYwQO3JwYpOVbmWygQc 5zbA== X-Gm-Message-State: ABuFfoheSAau6QchtqMxE8Byu4Hv8rR5t7o+8hUiqA+lfc9fGipAy6C6 jOTN8bEaqNX1/TpvUj6aWmg= X-Google-Smtp-Source: ACcGV622P2UQTVa4Y5BmVGJtL/RWBnSbR2x3yf17T+wUM4n+5dQXFqeLkXwKbpgcmBhKT1EMQBHRNQ== X-Received: by 2002:a17:902:f08c:: with SMTP id go12mr16412860plb.263.1539595000186; Mon, 15 Oct 2018 02:16:40 -0700 (PDT) Received: from ala-d2121-lx1.wrs.com (unknown-156-139.windriver.com. [147.11.156.139]) by smtp.gmail.com with ESMTPSA id a11-v6sm10798256pgw.54.2018.10.15.02.16.38 (version=TLS1_1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 15 Oct 2018 02:16:39 -0700 (PDT) From: Bin Meng To: Simon Glass , U-Boot Mailing List Date: Mon, 15 Oct 2018 02:21:03 -0700 Message-Id: <1539595287-31378-8-git-send-email-bmeng.cn@gmail.com> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: <1539595287-31378-1-git-send-email-bmeng.cn@gmail.com> References: <1539595287-31378-1-git-send-email-bmeng.cn@gmail.com> Subject: [U-Boot] [PATCH v3 07/31] virtio: Add net driver support X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.18 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" From: Tuomas Tynkkynen This adds virtio net device driver support. Signed-off-by: Tuomas Tynkkynen Signed-off-by: Bin Meng Reviewed-by: Simon Glass Signed-off-by: Tuomas Tynkkynen Signed-off-by: Bin Meng Reviewed-by: Simon Glass --- Changes in v3: None Changes in v2: - Changed virtio net driver feature table to const drivers/virtio/Kconfig | 7 ++ drivers/virtio/Makefile | 1 + drivers/virtio/virtio_net.c | 218 +++++++++++++++++++++++++++++++++++ drivers/virtio/virtio_net.h | 268 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 494 insertions(+) create mode 100644 drivers/virtio/virtio_net.c create mode 100644 drivers/virtio/virtio_net.h diff --git a/drivers/virtio/Kconfig b/drivers/virtio/Kconfig index 4f9a11b..e20dd69 100644 --- a/drivers/virtio/Kconfig +++ b/drivers/virtio/Kconfig @@ -29,4 +29,11 @@ config VIRTIO_MMIO This driver provides support for memory mapped virtio platform device driver. +config VIRTIO_NET + bool "virtio net driver" + depends on VIRTIO + help + This is the virtual net driver for virtio. It can be used with + QEMU based targets. + endmenu diff --git a/drivers/virtio/Makefile b/drivers/virtio/Makefile index 2e48785..b7764f1 100644 --- a/drivers/virtio/Makefile +++ b/drivers/virtio/Makefile @@ -5,3 +5,4 @@ obj-y += virtio-uclass.o virtio_ring.o obj-$(CONFIG_VIRTIO_MMIO) += virtio_mmio.o +obj-$(CONFIG_VIRTIO_NET) += virtio_net.o diff --git a/drivers/virtio/virtio_net.c b/drivers/virtio/virtio_net.c new file mode 100644 index 0000000..5bb6a9f --- /dev/null +++ b/drivers/virtio/virtio_net.c @@ -0,0 +1,218 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * Copyright (C) 2018, Tuomas Tynkkynen + * Copyright (C) 2018, Bin Meng + */ + +#include +#include +#include +#include +#include +#include +#include "virtio_net.h" + +/* Amount of buffers to keep in the RX virtqueue */ +#define VIRTIO_NET_NUM_RX_BUFS 32 + +/* + * This value comes from the VirtIO spec: 1500 for maximum packet size, + * 14 for the Ethernet header, 12 for virtio_net_hdr. In total 1526 bytes. + */ +#define VIRTIO_NET_RX_BUF_SIZE 1526 + +struct virtio_net_priv { + union { + struct virtqueue *vqs[2]; + struct { + struct virtqueue *rx_vq; + struct virtqueue *tx_vq; + }; + }; + + char rx_buff[VIRTIO_NET_NUM_RX_BUFS][VIRTIO_NET_RX_BUF_SIZE]; + bool rx_running; +}; + +/* + * For simplicity, the driver only negotiates the VIRTIO_NET_F_MAC feature. + * For the VIRTIO_NET_F_STATUS feature, we don't negotiate it, hence per spec + * we should assume the link is always active. + */ +static const u32 feature[] = { + VIRTIO_NET_F_MAC +}; + +static const u32 feature_legacy[] = { + VIRTIO_NET_F_MAC +}; + +static int virtio_net_start(struct udevice *dev) +{ + struct virtio_net_priv *priv = dev_get_priv(dev); + struct virtio_sg sg; + struct virtio_sg *sgs[] = { &sg }; + int i; + + if (!priv->rx_running) { + /* receive buffer length is always 1526 */ + sg.length = VIRTIO_NET_RX_BUF_SIZE; + + /* setup the receive buffer address */ + for (i = 0; i < VIRTIO_NET_NUM_RX_BUFS; i++) { + sg.addr = priv->rx_buff[i]; + virtqueue_add(priv->rx_vq, sgs, 0, 1); + } + + virtqueue_kick(priv->rx_vq); + + /* setup the receive queue only once */ + priv->rx_running = true; + } + + return 0; +} + +static int virtio_net_send(struct udevice *dev, void *packet, int length) +{ + struct virtio_net_priv *priv = dev_get_priv(dev); + struct virtio_net_hdr hdr; + struct virtio_sg hdr_sg = { &hdr, sizeof(hdr) }; + struct virtio_sg data_sg = { packet, length }; + struct virtio_sg *sgs[] = { &hdr_sg, &data_sg }; + int ret; + + memset(&hdr, 0, sizeof(struct virtio_net_hdr)); + + ret = virtqueue_add(priv->tx_vq, sgs, 2, 0); + if (ret) + return ret; + + virtqueue_kick(priv->tx_vq); + + while (1) { + if (virtqueue_get_buf(priv->tx_vq, NULL)) + break; + } + + return 0; +} + +static int virtio_net_recv(struct udevice *dev, int flags, uchar **packetp) +{ + struct virtio_net_priv *priv = dev_get_priv(dev); + unsigned int len; + void *buf; + + buf = virtqueue_get_buf(priv->rx_vq, &len); + if (!buf) + return -EAGAIN; + + *packetp = buf + sizeof(struct virtio_net_hdr); + return len - sizeof(struct virtio_net_hdr); +} + +static int virtio_net_free_pkt(struct udevice *dev, uchar *packet, int length) +{ + struct virtio_net_priv *priv = dev_get_priv(dev); + void *buf = packet - sizeof(struct virtio_net_hdr); + struct virtio_sg sg = { buf, VIRTIO_NET_RX_BUF_SIZE }; + struct virtio_sg *sgs[] = { &sg }; + + /* Put the buffer back to the rx ring */ + virtqueue_add(priv->rx_vq, sgs, 0, 1); + + return 0; +} + +static void virtio_net_stop(struct udevice *dev) +{ + /* + * There is no way to stop the queue from running, unless we issue + * a reset to the virtio device, and re-do the queue initialization + * from the beginning. + */ +} + +static int virtio_net_write_hwaddr(struct udevice *dev) +{ + struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(dev->parent); + struct eth_pdata *pdata = dev_get_platdata(dev); + int i; + + /* + * v1.0 compliant device's MAC address is set through control channel, + * which we don't support for now. + */ + if (!uc_priv->legacy) + return -ENOSYS; + + for (i = 0; i < sizeof(pdata->enetaddr); i++) { + virtio_cwrite8(dev, + offsetof(struct virtio_net_config, mac) + i, + pdata->enetaddr[i]); + } + + return 0; +} + +static int virtio_net_read_rom_hwaddr(struct udevice *dev) +{ + struct eth_pdata *pdata = dev_get_platdata(dev); + + if (!pdata) + return -ENOSYS; + + if (virtio_has_feature(dev, VIRTIO_NET_F_MAC)) { + virtio_cread_bytes(dev, + offsetof(struct virtio_net_config, mac), + pdata->enetaddr, sizeof(pdata->enetaddr)); + } + + return 0; +} + +static int virtio_net_bind(struct udevice *dev) +{ + struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(dev->parent); + + /* Indicate what driver features we support */ + virtio_driver_features_init(uc_priv, feature, ARRAY_SIZE(feature), + feature_legacy, ARRAY_SIZE(feature_legacy)); + + return 0; +} + +static int virtio_net_probe(struct udevice *dev) +{ + struct virtio_net_priv *priv = dev_get_priv(dev); + int ret; + + ret = virtio_find_vqs(dev, 2, priv->vqs); + if (ret < 0) + return ret; + + return 0; +} + +static const struct eth_ops virtio_net_ops = { + .start = virtio_net_start, + .send = virtio_net_send, + .recv = virtio_net_recv, + .free_pkt = virtio_net_free_pkt, + .stop = virtio_net_stop, + .write_hwaddr = virtio_net_write_hwaddr, + .read_rom_hwaddr = virtio_net_read_rom_hwaddr, +}; + +U_BOOT_DRIVER(virtio_net) = { + .name = VIRTIO_NET_DRV_NAME, + .id = UCLASS_ETH, + .bind = virtio_net_bind, + .probe = virtio_net_probe, + .remove = virtio_reset, + .ops = &virtio_net_ops, + .priv_auto_alloc_size = sizeof(struct virtio_net_priv), + .platdata_auto_alloc_size = sizeof(struct eth_pdata), + .flags = DM_FLAG_ACTIVE_DMA, +}; diff --git a/drivers/virtio/virtio_net.h b/drivers/virtio/virtio_net.h new file mode 100644 index 0000000..c92bae5 --- /dev/null +++ b/drivers/virtio/virtio_net.h @@ -0,0 +1,268 @@ +/* SPDX-License-Identifier: BSD-3-Clause */ +/* + * Copyright (C) 2018, Tuomas Tynkkynen + * Copyright (C) 2018, Bin Meng + * + * From Linux kernel include/uapi/linux/virtio_net.h + */ + +#ifndef _LINUX_VIRTIO_NET_H +#define _LINUX_VIRTIO_NET_H + +/* TODO: needs to be removed! */ +#define ETH_ALEN 6 + +/* The feature bitmap for virtio net */ + +/* Host handles pkts w/ partial csum */ +#define VIRTIO_NET_F_CSUM 0 +/* Guest handles pkts w/ partial csum */ +#define VIRTIO_NET_F_GUEST_CSUM 1 +/* Dynamic offload configuration */ +#define VIRTIO_NET_F_CTRL_GUEST_OFFLOADS 2 +/* Initial MTU advice */ +#define VIRTIO_NET_F_MTU 3 +/* Host has given MAC address */ +#define VIRTIO_NET_F_MAC 5 +/* Guest can handle TSOv4 in */ +#define VIRTIO_NET_F_GUEST_TSO4 7 +/* Guest can handle TSOv6 in */ +#define VIRTIO_NET_F_GUEST_TSO6 8 +/* Guest can handle TSO[6] w/ ECN in */ +#define VIRTIO_NET_F_GUEST_ECN 9 +/* Guest can handle UFO in */ +#define VIRTIO_NET_F_GUEST_UFO 10 +/* Host can handle TSOv4 in */ +#define VIRTIO_NET_F_HOST_TSO4 11 +/* Host can handle TSOv6 in */ +#define VIRTIO_NET_F_HOST_TSO6 12 +/* Host can handle TSO[6] w/ ECN in */ +#define VIRTIO_NET_F_HOST_ECN 13 +/* Host can handle UFO in */ +#define VIRTIO_NET_F_HOST_UFO 14 +/* Host can merge receive buffers */ +#define VIRTIO_NET_F_MRG_RXBUF 15 +/* virtio_net_config.status available */ +#define VIRTIO_NET_F_STATUS 16 +/* Control channel available */ +#define VIRTIO_NET_F_CTRL_VQ 17 +/* Control channel RX mode support */ +#define VIRTIO_NET_F_CTRL_RX 18 +/* Control channel VLAN filtering */ +#define VIRTIO_NET_F_CTRL_VLAN 19 +/* Extra RX mode control support */ +#define VIRTIO_NET_F_CTRL_RX_EXTRA 20 +/* Guest can announce device on the network */ +#define VIRTIO_NET_F_GUEST_ANNOUNCE 21 +/* Device supports receive flow steering */ +#define VIRTIO_NET_F_MQ 22 +/* Set MAC address */ +#define VIRTIO_NET_F_CTRL_MAC_ADDR 23 +/* Device set linkspeed and duplex */ +#define VIRTIO_NET_F_SPEED_DUPLEX 63 + +#ifndef VIRTIO_NET_NO_LEGACY +/* Host handles pkts w/ any GSO type */ +#define VIRTIO_NET_F_GSO 6 +#endif /* VIRTIO_NET_NO_LEGACY */ + +#define VIRTIO_NET_S_LINK_UP 1 /* Link is up */ +#define VIRTIO_NET_S_ANNOUNCE 2 /* Announcement is needed */ + +struct __packed virtio_net_config { + /* The config defining mac address (if VIRTIO_NET_F_MAC) */ + __u8 mac[ETH_ALEN]; + /* See VIRTIO_NET_F_STATUS and VIRTIO_NET_S_* above */ + __u16 status; + /* + * Maximum number of each of transmit and receive queues; + * see VIRTIO_NET_F_MQ and VIRTIO_NET_CTRL_MQ. + * Legal values are between 1 and 0x8000 + */ + __u16 max_virtqueue_pairs; + /* Default maximum transmit unit advice */ + __u16 mtu; + /* + * speed, in units of 1Mb. All values 0 to INT_MAX are legal. + * Any other value stands for unknown. + */ + __u32 speed; + /* + * 0x00 - half duplex + * 0x01 - full duplex + * Any other value stands for unknown. + */ + __u8 duplex; +}; + +/* + * This header comes first in the scatter-gather list. If you don't + * specify GSO or CSUM features, you can simply ignore the header. + * + * This is bitwise-equivalent to the legacy struct virtio_net_hdr_mrg_rxbuf, + * only flattened. + */ +struct virtio_net_hdr_v1 { +#define VIRTIO_NET_HDR_F_NEEDS_CSUM 0x01 /* Use csum_start, csum_offset */ +#define VIRTIO_NET_HDR_F_DATA_VALID 0x02 /* Csum is valid */ + __u8 flags; +#define VIRTIO_NET_HDR_GSO_NONE 0x00 /* Not a GSO frame */ +#define VIRTIO_NET_HDR_GSO_TCPV4 0x01 /* GSO frame, IPv4 TCP (TSO) */ +#define VIRTIO_NET_HDR_GSO_UDP 0x03 /* GSO frame, IPv4 UDP (UFO) */ +#define VIRTIO_NET_HDR_GSO_TCPV6 0x04 /* GSO frame, IPv6 TCP */ +#define VIRTIO_NET_HDR_GSO_ECN 0x80 /* TCP has ECN set */ + __u8 gso_type; + __virtio16 hdr_len; /* Ethernet + IP + tcp/udp hdrs */ + __virtio16 gso_size; /* Bytes to append to hdr_len per frame */ + __virtio16 csum_start; /* Position to start checksumming from */ + __virtio16 csum_offset; /* Offset after that to place checksum */ + __virtio16 num_buffers; /* Number of merged rx buffers */ +}; + +#ifndef VIRTIO_NET_NO_LEGACY +/* + * This header comes first in the scatter-gather list. + * + * For legacy virtio, if VIRTIO_F_ANY_LAYOUT is not negotiated, it must + * be the first element of the scatter-gather list. If you don't + * specify GSO or CSUM features, you can simply ignore the header. + */ +struct virtio_net_hdr { + /* See VIRTIO_NET_HDR_F_* */ + __u8 flags; + /* See VIRTIO_NET_HDR_GSO_* */ + __u8 gso_type; + __virtio16 hdr_len; /* Ethernet + IP + tcp/udp hdrs */ + __virtio16 gso_size; /* Bytes to append to hdr_len per frame */ + __virtio16 csum_start; /* Position to start checksumming from */ + __virtio16 csum_offset; /* Offset after that to place checksum */ +}; + +/* + * This is the version of the header to use when the MRG_RXBUF + * feature has been negotiated. + */ +struct virtio_net_hdr_mrg_rxbuf { + struct virtio_net_hdr hdr; + __virtio16 num_buffers; /* Number of merged rx buffers */ +}; +#endif /* ...VIRTIO_NET_NO_LEGACY */ + +/* + * Control virtqueue data structures + * + * The control virtqueue expects a header in the first sg entry + * and an ack/status response in the last entry. Data for the + * command goes in between. + */ +struct __packed virtio_net_ctrl_hdr { + __u8 class; + __u8 cmd; +}; + +typedef __u8 virtio_net_ctrl_ack; + +#define VIRTIO_NET_OK 0 +#define VIRTIO_NET_ERR 1 + +/* + * Control the RX mode, ie. promisucous, allmulti, etc... + * + * All commands require an "out" sg entry containing a 1 byte state value, + * zero = disable, non-zero = enable. + * + * Commands 0 and 1 are supported with the VIRTIO_NET_F_CTRL_RX feature. + * Commands 2-5 are added with VIRTIO_NET_F_CTRL_RX_EXTRA. + */ +#define VIRTIO_NET_CTRL_RX 0 +#define VIRTIO_NET_CTRL_RX_PROMISC 0 +#define VIRTIO_NET_CTRL_RX_ALLMULTI 1 +#define VIRTIO_NET_CTRL_RX_ALLUNI 2 +#define VIRTIO_NET_CTRL_RX_NOMULTI 3 +#define VIRTIO_NET_CTRL_RX_NOUNI 4 +#define VIRTIO_NET_CTRL_RX_NOBCAST 5 + +/* + * Control the MAC + * + * The MAC filter table is managed by the hypervisor, the guest should assume + * the size is infinite. Filtering should be considered non-perfect, ie. based + * on hypervisor resources, the guest may received packets from sources not + * specified in the filter list. + * + * In addition to the class/cmd header, the TABLE_SET command requires two + * out scatterlists. Each contains a 4 byte count of entries followed by a + * concatenated byte stream of the ETH_ALEN MAC addresses. The first sg list + * contains unicast addresses, the second is for multicast. This functionality + * is present if the VIRTIO_NET_F_CTRL_RX feature is available. + * + * The ADDR_SET command requests one out scatterlist, it contains a 6 bytes MAC + * address. This functionality is present if the VIRTIO_NET_F_CTRL_MAC_ADDR + * feature is available. + */ +struct __packed virtio_net_ctrl_mac { + __virtio32 entries; + __u8 macs[][ETH_ALEN]; +}; + +#define VIRTIO_NET_CTRL_MAC 1 +#define VIRTIO_NET_CTRL_MAC_TABLE_SET 0 +#define VIRTIO_NET_CTRL_MAC_ADDR_SET 1 + +/* + * Control VLAN filtering + * + * The VLAN filter table is controlled via a simple ADD/DEL interface. VLAN IDs + * not added may be filterd by the hypervisor. Del is the opposite of add. Both + * commands expect an out entry containing a 2 byte VLAN ID. VLAN filterting is + * available with the VIRTIO_NET_F_CTRL_VLAN feature bit. + */ +#define VIRTIO_NET_CTRL_VLAN 2 +#define VIRTIO_NET_CTRL_VLAN_ADD 0 +#define VIRTIO_NET_CTRL_VLAN_DEL 1 + +/* + * Control link announce acknowledgment + * + * The command VIRTIO_NET_CTRL_ANNOUNCE_ACK is used to indicate that driver has + * recevied the notification; device would clear the VIRTIO_NET_S_ANNOUNCE bit + * in the status field after it receives this command. + */ +#define VIRTIO_NET_CTRL_ANNOUNCE 3 +#define VIRTIO_NET_CTRL_ANNOUNCE_ACK 0 + +/* + * Control receive flow steering + * + * The command VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET enables receive flow steering, + * specifying the number of the transmit and receive queues that will be used. + * After the command is consumed and acked by the device, the device will not + * steer new packets on receive virtqueues other than specified nor read from + * transmit virtqueues other than specified. Accordingly, driver should not + * transmit new packets on virtqueues other than specified. + */ +struct virtio_net_ctrl_mq { + __virtio16 virtqueue_pairs; +}; + +#define VIRTIO_NET_CTRL_MQ 4 +#define VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET 0 +#define VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN 1 +#define VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX 0x8000 + +/* + * Control network offloads + * + * Reconfigures the network offloads that guest can handle. + * + * Available with the VIRTIO_NET_F_CTRL_GUEST_OFFLOADS feature bit. + * + * Command data format matches the feature bit mask exactly. + * + * See VIRTIO_NET_F_GUEST_* for the list of offloads + * that can be enabled/disabled. + */ +#define VIRTIO_NET_CTRL_GUEST_OFFLOADS 5 +#define VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET 0 + +#endif /* _LINUX_VIRTIO_NET_H */ From patchwork Mon Oct 15 09:21:04 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bin Meng X-Patchwork-Id: 983999 X-Patchwork-Delegate: sjg@chromium.org Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=lists.denx.de (client-ip=81.169.180.215; helo=lists.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="Upbna4hU"; dkim-atps=neutral Received: from lists.denx.de (dione.denx.de [81.169.180.215]) by ozlabs.org (Postfix) with ESMTP id 42YXvW0Hx8z9sBj for ; Mon, 15 Oct 2018 20:19:34 +1100 (AEDT) Received: by lists.denx.de (Postfix, from userid 105) id AE46EC21DA2; Mon, 15 Oct 2018 09:18:48 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on lists.denx.de X-Spam-Level: X-Spam-Status: No, score=-0.0 required=5.0 tests=FREEMAIL_FROM, RCVD_IN_DNSWL_BLOCKED, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, T_DKIM_INVALID autolearn=unavailable autolearn_force=no version=3.4.0 Received: from lists.denx.de (localhost [IPv6:::1]) by lists.denx.de (Postfix) with ESMTP id 3F062C21E52; Mon, 15 Oct 2018 09:16:52 +0000 (UTC) Received: by lists.denx.de (Postfix, from userid 105) id D6B9CC21E35; Mon, 15 Oct 2018 09:16:48 +0000 (UTC) Received: from mail-pl1-f194.google.com (mail-pl1-f194.google.com [209.85.214.194]) by lists.denx.de (Postfix) with ESMTPS id AD1A3C21DB6 for ; Mon, 15 Oct 2018 09:16:42 +0000 (UTC) Received: by mail-pl1-f194.google.com with SMTP id f18-v6so9004087plr.1 for ; Mon, 15 Oct 2018 02:16:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=htpB1SAuHg5qXppoSRzOx8+D0yJRhLq1X3d8TuAxsug=; b=Upbna4hU2AgtnjPb9JpayXdH/dmD3vYsh8soVYVdmzAM9hvMSbujofn5xdZflzROlV q4YVUl/UWO/fpu6jNZR64853ryGX+dsZrChF4twKUUsvXXWowsk0Ocl155CH59Qjclr5 jLad8SCddmqovfzX6mZayi0vzyhXyXkwzYAQDLrxwiUTmpZanZgNJXz3gzzG4RaIVBsg qAReoOlPphVO7773V7TsPYrXP3nRWqn+IyHmBcWf1Qwv1fe5fmxntXtQEBjsKCyIzH9g PX1+GvMFuE5cdFdsYuRHonxrSxyHIap9SoPbP2BhQXQO9rgTDhl6gh5CkKSb4pG+rtqG ge9Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=htpB1SAuHg5qXppoSRzOx8+D0yJRhLq1X3d8TuAxsug=; b=P4xo59u37+aujplaAjpN/e+V/W3ojFqOVVusJzk0/4WSFGELKoLBRsUhKmj8Hn5Q8o yVhyb4wmPG8bzbIWtbHHKCDezPlmQFc6Ms/+Zq1rrPdI+BB5mjveSSS1IHUPZ/0bvDf2 v8PNn7gt1b8XBHGt4cpI0ycj/CeKVeURa3tq3OsHnzV8hiPQNfUcfh/uZwXNjOI9O1qc tMHhqNYxXtxqJf3acg07iBWLDFE/v0O9MDvrMm7FniceWOMaetd54kegzRzatNZIIQV7 ri6F0VEea5jPchQC+zp/aCcALuAnA51KGvx3+BE+SUE+IfJzpTD+ch/TN1yK3fRGh2nz mYKg== X-Gm-Message-State: ABuFfoh8XKtKqm4CdBbXMR8/AoqpnIT5dqt96OPuyAB3/8xvMwPKmNqc Gau6G9ocjXfTd4WIOr64NyeVir8f X-Google-Smtp-Source: ACcGV63zHlTtcJGSeDnPnZP5PWbdN2cwGmtvHv9VUWLj0yV/jQZXss7PxaX0W20Gv/qkgk0UfwgHvQ== X-Received: by 2002:a17:902:3fe4:: with SMTP id a91-v6mr16244348pld.187.1539595001333; Mon, 15 Oct 2018 02:16:41 -0700 (PDT) Received: from ala-d2121-lx1.wrs.com (unknown-156-139.windriver.com. [147.11.156.139]) by smtp.gmail.com with ESMTPSA id a11-v6sm10798256pgw.54.2018.10.15.02.16.40 (version=TLS1_1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 15 Oct 2018 02:16:40 -0700 (PDT) From: Bin Meng To: Simon Glass , U-Boot Mailing List Date: Mon, 15 Oct 2018 02:21:04 -0700 Message-Id: <1539595287-31378-9-git-send-email-bmeng.cn@gmail.com> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: <1539595287-31378-1-git-send-email-bmeng.cn@gmail.com> References: <1539595287-31378-1-git-send-email-bmeng.cn@gmail.com> Subject: [U-Boot] [PATCH v3 08/31] test: dm: blk: Correct blk_base test case X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.18 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" The blk_base test case creates a USB mass storage block device with the Sandbox host block device as its parent. This does not make any sense and causes potential issue, for example if the test case tries to read/write anything on the USB mass storage block device it will definitely fail as its parent is not on USB bus at all. Correct the test case by creating another Sandbox host block device instead of the USB mass storage one and adjust the case accordingly. Signed-off-by: Bin Meng Reviewed-by: Simon Glass Signed-off-by: Bin Meng Reviewed-by: Simon Glass --- Changes in v3: None Changes in v2: None test/dm/blk.c | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/test/dm/blk.c b/test/dm/blk.c index 4de477b..9c71adc 100644 --- a/test/dm/blk.c +++ b/test/dm/blk.c @@ -15,34 +15,29 @@ DECLARE_GLOBAL_DATA_PTR; /* Test that block devices can be created */ static int dm_test_blk_base(struct unit_test_state *uts) { - struct udevice *blk, *usb_blk, *dev; + struct udevice *blk1, *blk3, *dev; /* Make sure there are no block devices */ - ut_asserteq(-ENODEV, uclass_get_device_by_seq(UCLASS_BLK, 0, &blk)); + ut_asserteq(-ENODEV, uclass_get_device_by_seq(UCLASS_BLK, 0, &dev)); /* Create two, one the parent of the other */ ut_assertok(blk_create_device(gd->dm_root, "sandbox_host_blk", "test", - IF_TYPE_HOST, 1, 512, 2, &blk)); - ut_assertok(blk_create_device(blk, "usb_storage_blk", "test", - IF_TYPE_USB, 3, 512, 2, &usb_blk)); + IF_TYPE_HOST, 1, 512, 2, &blk1)); + ut_assertok(blk_create_device(blk1, "sandbox_host_blk", "test", + IF_TYPE_HOST, 3, 512, 2, &blk3)); /* Check we can find them */ ut_asserteq(-ENODEV, blk_get_device(IF_TYPE_HOST, 0, &dev)); ut_assertok(blk_get_device(IF_TYPE_HOST, 1, &dev)); - ut_asserteq_ptr(blk, dev); - - ut_asserteq(-ENODEV, blk_get_device(IF_TYPE_USB, 0, &dev)); - ut_assertok(blk_get_device(IF_TYPE_USB, 3, &dev)); - ut_asserteq_ptr(usb_blk, dev); + ut_asserteq_ptr(blk1, dev); + ut_assertok(blk_get_device(IF_TYPE_HOST, 3, &dev)); + ut_asserteq_ptr(blk3, dev); /* Check we can iterate */ ut_assertok(blk_first_device(IF_TYPE_HOST, &dev)); - ut_asserteq_ptr(blk, dev); - ut_asserteq(-ENODEV, blk_next_device(&dev)); - - ut_assertok(blk_first_device(IF_TYPE_USB, &dev)); - ut_asserteq_ptr(usb_blk, dev); - ut_asserteq(-ENODEV, blk_next_device(&dev)); + ut_asserteq_ptr(blk1, dev); + ut_assertok(blk_next_device(&dev)); + ut_asserteq_ptr(blk3, dev); return 0; } From patchwork Mon Oct 15 09:21:05 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bin Meng X-Patchwork-Id: 984002 X-Patchwork-Delegate: sjg@chromium.org Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=lists.denx.de (client-ip=81.169.180.215; helo=lists.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="qYoSRyyJ"; dkim-atps=neutral Received: from lists.denx.de (dione.denx.de [81.169.180.215]) by ozlabs.org (Postfix) with ESMTP id 42YXxb0C1Zz9sBj for ; Mon, 15 Oct 2018 20:21:22 +1100 (AEDT) Received: by lists.denx.de (Postfix, from userid 105) id A7CC6C21DE8; Mon, 15 Oct 2018 09:20:19 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on lists.denx.de X-Spam-Level: X-Spam-Status: No, score=-0.0 required=5.0 tests=FREEMAIL_FROM, RCVD_IN_DNSWL_BLOCKED, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, T_DKIM_INVALID autolearn=unavailable autolearn_force=no version=3.4.0 Received: from lists.denx.de (localhost [IPv6:::1]) by lists.denx.de (Postfix) with ESMTP id AE3FDC21E79; Mon, 15 Oct 2018 09:16:56 +0000 (UTC) Received: by lists.denx.de (Postfix, from userid 105) id 22A6AC21E07; Mon, 15 Oct 2018 09:16:49 +0000 (UTC) Received: from mail-pf1-f196.google.com (mail-pf1-f196.google.com [209.85.210.196]) by lists.denx.de (Postfix) with ESMTPS id CE671C21E15 for ; Mon, 15 Oct 2018 09:16:43 +0000 (UTC) Received: by mail-pf1-f196.google.com with SMTP id r9-v6so9354061pff.11 for ; Mon, 15 Oct 2018 02:16:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=vopmd97pVg4iwaNBdVF365Y/Qj0YQ9g2KXggtDynQ74=; b=qYoSRyyJ4UA5KhviiOyxpyrFWjYoQ1uhTCfGIqG3CAu237S4ieDPjh1t98jhz0J044 BCTzjKPjbd3+nHRdOwJONWM9kpmp2KFdkMZ+pneqWuEZZ/aTkAdentJZ/xvc0fet2152 q3bFGToTCTuMnQ+A8Ns/5rlvC8loQ2Dc8ipWJblwOnMikgf6996Uz9GKvvFVi7UWRUih NjDN7x9gwRjOIw0id83c4PRoPXjZnTEsYNWt360GaFok8YFAPG2Pjh5f7KijOticEcvF WFWydrAk5sOIe2YQFjJWbPIn7rmWPE1+P1j1/7NMbPCrQt2nL3c5rJvMcNQHFqOZs8x0 gObg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=vopmd97pVg4iwaNBdVF365Y/Qj0YQ9g2KXggtDynQ74=; b=ipN3Q6vCzg7DQJMmNB7TtAMTO4PNiZG6U1GHTx7fC0oUJo9zP0pvDjm6TolJftmBZt cQ9WKzxroPQa4z7ddVeowX7FOOQZzms9Po6Y41FtbxJwn2dTeyHq8zJ0ekngJ3PrLGy1 QyErnCiJJRmPe83b2OhjbdNd+GX3D9oL4Za7j9xEhQQQ8XC057LxldUKGVR41slmWizT p94gtC93dL/Rp8X2xton1yJOEz3mxCfYJIBUdqmNlhFHPMNNA1wapUdEJ1Ws1HOEhVAV sWw3utKVgRUbaY/POi7NDKSO0koJae7ycl2C/U19/qHg/TsINDzWsKzxoC+hU7Ct8QPf Se4g== X-Gm-Message-State: ABuFfohAuXRtL0ugOCE90ipOw0R3AV1tyQ4hMadPMy28Mup/v2FuKZap rkbzHXvwVcjGFyz2veXq2Dg= X-Google-Smtp-Source: ACcGV605FNZ+ZKBo1u8CV+7ckF7J4aw7D5audhkdFR2c14/Wh0Vavv3GfyAGSd1SH153T7kdu8DRHA== X-Received: by 2002:a63:4658:: with SMTP id v24-v6mr15580421pgk.425.1539595002471; Mon, 15 Oct 2018 02:16:42 -0700 (PDT) Received: from ala-d2121-lx1.wrs.com (unknown-156-139.windriver.com. [147.11.156.139]) by smtp.gmail.com with ESMTPSA id a11-v6sm10798256pgw.54.2018.10.15.02.16.41 (version=TLS1_1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 15 Oct 2018 02:16:41 -0700 (PDT) From: Bin Meng To: Simon Glass , U-Boot Mailing List Date: Mon, 15 Oct 2018 02:21:05 -0700 Message-Id: <1539595287-31378-10-git-send-email-bmeng.cn@gmail.com> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: <1539595287-31378-1-git-send-email-bmeng.cn@gmail.com> References: <1539595287-31378-1-git-send-email-bmeng.cn@gmail.com> Subject: [U-Boot] [PATCH v3 09/31] sandbox: blk: Switch to use platdata_auto_alloc_size for the driver data X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.18 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" Currently the sandbox block driver uses priv_auto_alloc_size for the driver data, however that's only available after the device probe phase. In order to make it accessible in an earlier phase, switch to use platdata_auto_alloc_size instead. This patch is the prerequisite for the follow up patch of DM BLK driver changes to work with Sandbox. Signed-off-by: Bin Meng Reviewed-by: Simon Glass Signed-off-by: Bin Meng Reviewed-by: Simon Glass --- Changes in v3: None Changes in v2: None drivers/block/sandbox.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/drivers/block/sandbox.c b/drivers/block/sandbox.c index 0392437..576d049 100644 --- a/drivers/block/sandbox.c +++ b/drivers/block/sandbox.c @@ -33,7 +33,7 @@ static unsigned long host_block_read(struct udevice *dev, unsigned long start, lbaint_t blkcnt, void *buffer) { - struct host_block_dev *host_dev = dev_get_priv(dev); + struct host_block_dev *host_dev = dev_get_platdata(dev); struct blk_desc *block_dev = dev_get_uclass_platdata(dev); #else @@ -64,7 +64,7 @@ static unsigned long host_block_write(struct udevice *dev, unsigned long start, lbaint_t blkcnt, const void *buffer) { - struct host_block_dev *host_dev = dev_get_priv(dev); + struct host_block_dev *host_dev = dev_get_platdata(dev); struct blk_desc *block_dev = dev_get_uclass_platdata(dev); #else static unsigned long host_block_write(struct blk_desc *block_dev, @@ -131,16 +131,17 @@ int host_dev_bind(int devnum, char *filename) os_lseek(fd, 0, OS_SEEK_END) / 512, &dev); if (ret) goto err_file; + + host_dev = dev_get_platdata(dev); + host_dev->fd = fd; + host_dev->filename = fname; + ret = device_probe(dev); if (ret) { device_unbind(dev); goto err_file; } - host_dev = dev_get_priv(dev); - host_dev->fd = fd; - host_dev->filename = fname; - return blk_prepare_device(dev); err_file: os_close(fd); @@ -226,7 +227,7 @@ U_BOOT_DRIVER(sandbox_host_blk) = { .name = "sandbox_host_blk", .id = UCLASS_BLK, .ops = &sandbox_host_blk_ops, - .priv_auto_alloc_size = sizeof(struct host_block_dev), + .platdata_auto_alloc_size = sizeof(struct host_block_dev), }; #else U_BOOT_LEGACY_BLK(sandbox_host) = { From patchwork Mon Oct 15 09:21:06 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bin Meng X-Patchwork-Id: 984015 X-Patchwork-Delegate: sjg@chromium.org Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=lists.denx.de (client-ip=81.169.180.215; helo=lists.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="iMnwu986"; dkim-atps=neutral Received: from lists.denx.de (dione.denx.de [81.169.180.215]) by ozlabs.org (Postfix) with ESMTP id 42YY2M1v4qz9s5c for ; Mon, 15 Oct 2018 20:25:30 +1100 (AEDT) Received: by lists.denx.de (Postfix, from userid 105) id 779ECC21E13; Mon, 15 Oct 2018 09:23:39 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on lists.denx.de X-Spam-Level: X-Spam-Status: No, score=-0.0 required=5.0 tests=FREEMAIL_FROM, RCVD_IN_DNSWL_BLOCKED, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, T_DKIM_INVALID autolearn=unavailable autolearn_force=no version=3.4.0 Received: from lists.denx.de (localhost [IPv6:::1]) by lists.denx.de (Postfix) with ESMTP id D7901C21EA8; Mon, 15 Oct 2018 09:17:02 +0000 (UTC) Received: by lists.denx.de (Postfix, from userid 105) id 65F3DC21E08; Mon, 15 Oct 2018 09:16:53 +0000 (UTC) Received: from mail-pl1-f196.google.com (mail-pl1-f196.google.com [209.85.214.196]) by lists.denx.de (Postfix) with ESMTPS id EA3EAC21E13 for ; Mon, 15 Oct 2018 09:16:44 +0000 (UTC) Received: by mail-pl1-f196.google.com with SMTP id q19-v6so5919245pll.5 for ; Mon, 15 Oct 2018 02:16:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=3pqmRYuXxSIu79nnrQEc13XwzqJtYk+sbKpIeP+sfpE=; b=iMnwu986MoVnNWDGueElON9syZkZSLbg/FBeCpVBxksT6CMKXm+Mz18swnGizTBZjD Ph/Wxk7vM2lW/CKkTi9L7xHXGcNoWoGellqyNYhlF6q6oHPB12EYjvZoC/2N65LG8Br/ c/NzwkeY+u4AUpmququy7pZagVJGDRsspx66QrPjiC+NtzaMAVBEmGGTp/Gi8CFwiVLc OaV9/3WEl6f8+DqGXMJvXIb1geeL28MFXB7jIeKe/VGHwmxq3uWV4iISYkrIia4F2oqH 7iCIENa/p0MjSIoh8Riw79cKT8KEkl2Rgw7sycG/JowAP0K+wwwgg1IuPUTDpai0OS8Z dtrA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=3pqmRYuXxSIu79nnrQEc13XwzqJtYk+sbKpIeP+sfpE=; b=VO7nG9JpoRaugKlNcpPXF9QY3TITnZAVOC2PeCEjWI+1f88IeoLgY7EUZEwflkbRwV dqphQNJF/M22RCPqvTK6dvGkxVN2mRc/Oa14fGJG0wbfFmK3KdukKGjBUyYWQk7FXNgx EuFDJealL3CYMExUVd6URAQ4c0doGJcCN1ut2M7R0KhoWX7AOejxvjsL09nW3mGxeRA4 3cd1jz9a8/3UP47GBrcyEdAVBaFqMeS3NN1i5XUHE03caoY7UxvODtpvPMcjC+rXtG7w +PeOP+lGsrElxa6U1dyhfGJB1J16JZ+ktLiGekrI9Yn8YVrxsUUYkqfR9d27ijEKiss5 xkbQ== X-Gm-Message-State: ABuFfohPdzEZEHIB+7O0PfIPjkaQRkbPRI0QKXNQQPNwizl3FyqoUHcC gF3VhvBmDT0TD271ED5NSoI= X-Google-Smtp-Source: ACcGV63Bd5dN5Oouytoh+GwEUZ/fPs91VCIA2KSR3IOfoN60v8wYSOuqKZTuLzVyDJjJDf/VKo0pWg== X-Received: by 2002:a17:902:d917:: with SMTP id c23-v6mr16268162plz.206.1539595003594; Mon, 15 Oct 2018 02:16:43 -0700 (PDT) Received: from ala-d2121-lx1.wrs.com (unknown-156-139.windriver.com. [147.11.156.139]) by smtp.gmail.com with ESMTPSA id a11-v6sm10798256pgw.54.2018.10.15.02.16.42 (version=TLS1_1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 15 Oct 2018 02:16:42 -0700 (PDT) From: Bin Meng To: Simon Glass , U-Boot Mailing List Date: Mon, 15 Oct 2018 02:21:06 -0700 Message-Id: <1539595287-31378-11-git-send-email-bmeng.cn@gmail.com> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: <1539595287-31378-1-git-send-email-bmeng.cn@gmail.com> References: <1539595287-31378-1-git-send-email-bmeng.cn@gmail.com> Subject: [U-Boot] [PATCH v3 10/31] efi_driver: blk: Switch to use platdata_auto_alloc_size for the driver data X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.18 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" Currently the efi block driver uses priv_auto_alloc_size for the driver data, however that's only available after the device probe phase. In order to make it accessible in an earlier phase, switch to use platdata_auto_alloc_size instead. This patch is the prerequisite for the follow up patch of DM BLK driver changes to work with EFI loader. Signed-off-by: Bin Meng Reviewed-by: Simon Glass Signed-off-by: Bin Meng Reviewed-by: Simon Glass --- Changes in v3: None Changes in v2: None lib/efi_driver/efi_block_device.c | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/lib/efi_driver/efi_block_device.c b/lib/efi_driver/efi_block_device.c index 5b9c139..7b71b4d 100644 --- a/lib/efi_driver/efi_block_device.c +++ b/lib/efi_driver/efi_block_device.c @@ -38,7 +38,7 @@ * handle handle of the controller on which this driver is installed * io block io protocol proxied by this driver */ -struct efi_blk_priv { +struct efi_blk_platdata { efi_handle_t handle; struct efi_block_io *io; }; @@ -55,8 +55,8 @@ struct efi_blk_priv { static ulong efi_bl_read(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt, void *buffer) { - struct efi_blk_priv *priv = dev->priv; - struct efi_block_io *io = priv->io; + struct efi_blk_platdata *platdata = dev_get_platdata(dev); + struct efi_block_io *io = platdata->io; efi_status_t ret; EFI_PRINT("%s: read '%s', from block " LBAFU ", " LBAFU " blocks\n", @@ -84,8 +84,8 @@ static ulong efi_bl_read(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt, static ulong efi_bl_write(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt, const void *buffer) { - struct efi_blk_priv *priv = dev->priv; - struct efi_block_io *io = priv->io; + struct efi_blk_platdata *platdata = dev_get_platdata(dev); + struct efi_block_io *io = platdata->io; efi_status_t ret; EFI_PRINT("%s: write '%s', from block " LBAFU ", " LBAFU " blocks\n", @@ -135,7 +135,7 @@ static int efi_bl_bind(efi_handle_t handle, void *interface) struct efi_object *obj = efi_search_obj(handle); struct efi_block_io *io = interface; int disks; - struct efi_blk_priv *priv; + struct efi_blk_platdata *platdata; EFI_PRINT("%s: handle %p, interface %p\n", __func__, handle, io); @@ -163,16 +163,16 @@ static int efi_bl_bind(efi_handle_t handle, void *interface) return -ENOENT; /* Set the DM_FLAG_NAME_ALLOCED flag to avoid a memory leak */ device_set_name_alloced(bdev); - /* Allocate priv */ + + platdata = dev_get_platdata(bdev); + platdata->handle = handle; + platdata->io = interface; + ret = device_probe(bdev); if (ret) return ret; EFI_PRINT("%s: block device '%s' created\n", __func__, bdev->name); - priv = bdev->priv; - priv->handle = handle; - priv->io = interface; - ret = blk_prepare_device(bdev); /* Create handles for the partions of the block device */ @@ -193,7 +193,7 @@ U_BOOT_DRIVER(efi_blk) = { .name = "efi_blk", .id = UCLASS_BLK, .ops = &efi_blk_ops, - .priv_auto_alloc_size = sizeof(struct efi_blk_priv), + .platdata_auto_alloc_size = sizeof(struct efi_blk_platdata), }; /* EFI driver operators */ From patchwork Mon Oct 15 09:21:07 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bin Meng X-Patchwork-Id: 984007 X-Patchwork-Delegate: sjg@chromium.org Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=lists.denx.de (client-ip=81.169.180.215; helo=lists.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="BF/NRjTO"; dkim-atps=neutral Received: from lists.denx.de (dione.denx.de [81.169.180.215]) by ozlabs.org (Postfix) with ESMTP id 42YY0c3bcwz9sBq for ; Mon, 15 Oct 2018 20:24:00 +1100 (AEDT) Received: by lists.denx.de (Postfix, from userid 105) id C173FC21DD7; Mon, 15 Oct 2018 09:23:17 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on lists.denx.de X-Spam-Level: X-Spam-Status: No, score=-0.0 required=5.0 tests=FREEMAIL_FROM, RCVD_IN_DNSWL_BLOCKED, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, T_DKIM_INVALID autolearn=unavailable autolearn_force=no version=3.4.0 Received: from lists.denx.de (localhost [IPv6:::1]) by lists.denx.de (Postfix) with ESMTP id F3EB4C21E68; Mon, 15 Oct 2018 09:17:01 +0000 (UTC) Received: by lists.denx.de (Postfix, from userid 105) id E012CC21E73; Mon, 15 Oct 2018 09:16:55 +0000 (UTC) Received: from mail-pl1-f194.google.com (mail-pl1-f194.google.com [209.85.214.194]) by lists.denx.de (Postfix) with ESMTPS id 41F44C21DFD for ; Mon, 15 Oct 2018 09:16:46 +0000 (UTC) Received: by mail-pl1-f194.google.com with SMTP id s4-v6so9019223plp.0 for ; Mon, 15 Oct 2018 02:16:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=XO9HQxOGsVJyqSdwLRqxsaL7uW3gOEJwrRZKDW7EMYc=; b=BF/NRjTOcXPbQtfZb5FIk651p2WERnnO+YG8pO6HX6HQTYfj+g97njFnntzrD86xkN 59WoqiTS1Ei2tXvt0zD7Mn0zl6RTi4N4Xd1sJiTKpwlpWH75xQCZicG1HHd7cpKpaQSS +H8MHQyTpwkuPs94ZGZDdzMCWirNHnpyggqiK33a05tSE/C+tUHJYzqNKUPqadTX3cQB rg0L2VJcuUAl3jSKFhAvr8bzc1MCOh0UjIJq8YZZC1H4Y3fbpX6h4aVXlRHqHyRJLubA gLhmq1OE+DT8AXBm6FR826Qg12W1Lq2gdCE9GYmJmk664EfgjWus/I4uD8Y6FFU6DmZK skjw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=XO9HQxOGsVJyqSdwLRqxsaL7uW3gOEJwrRZKDW7EMYc=; b=NZd3mLN16u3Bcp6FbqP35XNtvQ5UtVc7mTS49tsYsFpNVrCtw7cIDpBmDaeG9tPhvI pbCiSABoVtricY+0QTg4xd+joq9ptVwkuZDTsQtivhimDikVlrv6Axrfmcg12nFoNe2y aQGBwHT7efsC0CBJee1pFElSiuObLEH0j5HawyOB3UYnCoo57+HMKGIGjl+3BnaylTuS sfGuYRmLblGAftFrsLnce9+3QiITve4Rn1bFjndT5XtxY1Q5LZNdN4+9VeXw6Le4LKiB UavrEgCmTPc2Xed2EMHhFB71z+4PLwGMIkVx2PHVrhV8g2rJNUaYSi98/Lg3DgZ8qbqW 0zMA== X-Gm-Message-State: ABuFfojXXNwfcHBTC+Z1LTFOxP99tcmcg0YfKdKw+tR9DsCgTGSoPmyg jG0FO3VZVLhhEF7OPmZgCiU= X-Google-Smtp-Source: ACcGV63rOQugpEXhZfZ0B9i31z6lhKtA+gP8NGTyWS7TrZ7/dSvaQ1kVGIgT3zllCg6vmN4wpjWz2A== X-Received: by 2002:a17:902:7798:: with SMTP id o24-v6mr15911126pll.299.1539595004854; Mon, 15 Oct 2018 02:16:44 -0700 (PDT) Received: from ala-d2121-lx1.wrs.com (unknown-156-139.windriver.com. [147.11.156.139]) by smtp.gmail.com with ESMTPSA id a11-v6sm10798256pgw.54.2018.10.15.02.16.43 (version=TLS1_1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 15 Oct 2018 02:16:44 -0700 (PDT) From: Bin Meng To: Simon Glass , U-Boot Mailing List Date: Mon, 15 Oct 2018 02:21:07 -0700 Message-Id: <1539595287-31378-12-git-send-email-bmeng.cn@gmail.com> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: <1539595287-31378-1-git-send-email-bmeng.cn@gmail.com> References: <1539595287-31378-1-git-send-email-bmeng.cn@gmail.com> Subject: [U-Boot] [PATCH v3 11/31] blk: Call part_init() in the post_probe() method X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.18 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" part_init() is currently called in every DM BLK driver, either in its bind() or probe() method. However we can use the BLK uclass driver's post_probe() method to do it automatically. Update all DM BLK drivers to adopt this change. Signed-off-by: Bin Meng Reviewed-by: Simon Glass Signed-off-by: Bin Meng Reviewed-by: Simon Glass --- Changes in v3: None Changes in v2: None cmd/sata.c | 9 --------- common/usb_storage.c | 4 +--- drivers/block/blk-uclass.c | 12 ++++++++++++ drivers/block/ide.c | 2 -- drivers/block/sandbox.c | 2 +- drivers/mmc/mmc.c | 3 --- drivers/nvme/nvme.c | 1 - drivers/scsi/scsi.c | 1 - lib/efi_driver/efi_block_device.c | 2 -- 9 files changed, 14 insertions(+), 22 deletions(-) diff --git a/cmd/sata.c b/cmd/sata.c index 4f0c6e0..6d62ba8 100644 --- a/cmd/sata.c +++ b/cmd/sata.c @@ -51,7 +51,6 @@ int sata_probe(int devnum) { #ifdef CONFIG_AHCI struct udevice *dev; - struct udevice *blk; int rc; rc = uclass_get_device(UCLASS_AHCI, devnum, &dev); @@ -67,14 +66,6 @@ int sata_probe(int devnum) return CMD_RET_FAILURE; } - rc = blk_get_from_parent(dev, &blk); - if (!rc) { - struct blk_desc *desc = dev_get_uclass_platdata(blk); - - if (desc->lba > 0 && desc->blksz > 0) - part_init(desc); - } - return 0; #else return sata_initialize() < 0 ? CMD_RET_FAILURE : CMD_RET_SUCCESS; diff --git a/common/usb_storage.c b/common/usb_storage.c index d92ebb6..560d605 100644 --- a/common/usb_storage.c +++ b/common/usb_storage.c @@ -226,9 +226,7 @@ static int usb_stor_probe_device(struct usb_device *udev) blkdev->lun = lun; ret = usb_stor_get_info(udev, data, blkdev); - if (ret == 1) - ret = blk_prepare_device(dev); - if (!ret) { + if (ret == 1) { usb_max_devs++; debug("%s: Found device %p\n", __func__, udev); } else { diff --git a/drivers/block/blk-uclass.c b/drivers/block/blk-uclass.c index facf527..95e7b54 100644 --- a/drivers/block/blk-uclass.c +++ b/drivers/block/blk-uclass.c @@ -644,8 +644,20 @@ int blk_unbind_all(int if_type) return 0; } +static int blk_post_probe(struct udevice *dev) +{ +#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBDISK_SUPPORT) + struct blk_desc *desc = dev_get_uclass_platdata(dev); + + part_init(desc); +#endif + + return 0; +} + UCLASS_DRIVER(blk) = { .id = UCLASS_BLK, .name = "blk", + .post_probe = blk_post_probe, .per_device_platdata_auto_alloc_size = sizeof(struct blk_desc), }; diff --git a/drivers/block/ide.c b/drivers/block/ide.c index 38adb6a..4b8a4ea 100644 --- a/drivers/block/ide.c +++ b/drivers/block/ide.c @@ -1169,8 +1169,6 @@ static int ide_blk_probe(struct udevice *udev) BLK_REV_SIZE); desc->revision[BLK_REV_SIZE] = '\0'; - part_init(desc); - return 0; } diff --git a/drivers/block/sandbox.c b/drivers/block/sandbox.c index 576d049..d3b1aaa 100644 --- a/drivers/block/sandbox.c +++ b/drivers/block/sandbox.c @@ -142,7 +142,7 @@ int host_dev_bind(int devnum, char *filename) goto err_file; } - return blk_prepare_device(dev); + return 0; err_file: os_close(fd); err: diff --git a/drivers/mmc/mmc.c b/drivers/mmc/mmc.c index 585951c..d6b9cdc 100644 --- a/drivers/mmc/mmc.c +++ b/drivers/mmc/mmc.c @@ -2444,9 +2444,6 @@ static int mmc_startup(struct mmc *mmc) bdesc->product[0] = 0; bdesc->revision[0] = 0; #endif -#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBDISK_SUPPORT) - part_init(bdesc); -#endif return 0; } diff --git a/drivers/nvme/nvme.c b/drivers/nvme/nvme.c index eb6fded..1ee0a0a 100644 --- a/drivers/nvme/nvme.c +++ b/drivers/nvme/nvme.c @@ -664,7 +664,6 @@ static int nvme_blk_probe(struct udevice *udev) sprintf(desc->vendor, "0x%.4x", pplat->vendor); memcpy(desc->product, ndev->serial, sizeof(ndev->serial)); memcpy(desc->revision, ndev->firmware_rev, sizeof(ndev->firmware_rev)); - part_init(desc); return 0; } diff --git a/drivers/scsi/scsi.c b/drivers/scsi/scsi.c index bc6ac8c..df47e2f 100644 --- a/drivers/scsi/scsi.c +++ b/drivers/scsi/scsi.c @@ -592,7 +592,6 @@ static int do_scsi_scan_one(struct udevice *dev, int id, int lun, bool verbose) memcpy(&bdesc->vendor, &bd.vendor, sizeof(bd.vendor)); memcpy(&bdesc->product, &bd.product, sizeof(bd.product)); memcpy(&bdesc->revision, &bd.revision, sizeof(bd.revision)); - part_init(bdesc); if (verbose) { printf(" Device %d: ", 0); diff --git a/lib/efi_driver/efi_block_device.c b/lib/efi_driver/efi_block_device.c index 7b71b4d..3f147cf 100644 --- a/lib/efi_driver/efi_block_device.c +++ b/lib/efi_driver/efi_block_device.c @@ -173,8 +173,6 @@ static int efi_bl_bind(efi_handle_t handle, void *interface) return ret; EFI_PRINT("%s: block device '%s' created\n", __func__, bdev->name); - ret = blk_prepare_device(bdev); - /* Create handles for the partions of the block device */ disks = efi_bl_bind_partitions(handle, bdev); EFI_PRINT("Found %d partitions\n", disks); From patchwork Mon Oct 15 09:21:08 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bin Meng X-Patchwork-Id: 984018 X-Patchwork-Delegate: sjg@chromium.org Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=lists.denx.de (client-ip=81.169.180.215; helo=lists.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="q2/Hglvb"; dkim-atps=neutral Received: from lists.denx.de (dione.denx.de [81.169.180.215]) by ozlabs.org (Postfix) with ESMTP id 42YY4q37RVz9s3C for ; Mon, 15 Oct 2018 20:27:39 +1100 (AEDT) Received: by lists.denx.de (Postfix, from userid 105) id D3586C21E2C; Mon, 15 Oct 2018 09:24:13 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on lists.denx.de X-Spam-Level: X-Spam-Status: No, score=-0.0 required=5.0 tests=FREEMAIL_FROM, RCVD_IN_DNSWL_BLOCKED, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, T_DKIM_INVALID autolearn=unavailable autolearn_force=no version=3.4.0 Received: from lists.denx.de (localhost [IPv6:::1]) by lists.denx.de (Postfix) with ESMTP id 75413C21ED6; Mon, 15 Oct 2018 09:17:05 +0000 (UTC) Received: by lists.denx.de (Postfix, from userid 105) id AC582C21ECA; Mon, 15 Oct 2018 09:16:57 +0000 (UTC) Received: from mail-pl1-f196.google.com (mail-pl1-f196.google.com [209.85.214.196]) by lists.denx.de (Postfix) with ESMTPS id 549CDC21E49 for ; Mon, 15 Oct 2018 09:16:47 +0000 (UTC) Received: by mail-pl1-f196.google.com with SMTP id e67-v6so11255plb.6 for ; Mon, 15 Oct 2018 02:16:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=+xEsnO10htHslSOt7vmxH2dNYWLKepVxNo3le/722Cc=; b=q2/Hglvbl4Dr0r0MiXt7Ivg7dJBLCPbkoGxm6HYDdUQykxIdhK8QaKhllqfu+gAtAU L00XgZblcxAmJD/YYGHn/PIfF0uzISXzjFl+T68ZjBZdJ37D4qU5pqZJ9XEMRIZRaCjO vCPfqhjLUUNITT1kyG2IYC/aeCA3kJmQRirZoX8afoPNVgDbbBZNoA1z1ZY2syoElqE3 3ZhmDf1ncNcWVfQ0ZkUx7ffnZjstIT3CuX7/RO/GJbsYm4yblZvIDErGU9Lj9AESMUCQ uw5jseX74GpDjJO1olTwwBcFw8r1+HkZavLC9AQtuadOSY0n8R9d34UdIaaHpf5ZXInw rUdg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=+xEsnO10htHslSOt7vmxH2dNYWLKepVxNo3le/722Cc=; b=VHSa+jY3wnKE4766l+liLyk7FHPkyBBt7FdTm/+i8bdDI/ybbYcBqQz9fA+d14DbpT w+4JgecsutuuRc9diK+nPeuZpa20PLobM5ZqAFSCiy2mAvMgJf6RXcIhiTaO1Q24O3gS +yWb1ITDpkT42chqVxkpUdLwki8gug803KxkkIefum7yE9aNOiqcHwJvNMk6eiHj/jpO odocsf3hi8lauaQ5hXXiiG1DL/MuxHfKyp79jpYmG6U8E7smKvDlz+TE3ZICiTUT87KG JdEZNL7bS/TS3fMwBEaSo7z/HAHmvj8SQKtWMj8dKzk3riW/vrxg1qNSqeHjSg89vyyG vOHw== X-Gm-Message-State: ABuFfohVK30sXDaovIaliakAOvNS49qBVb4skgOICsU/AtH4pnpbM+X5 e1y7ARBO8jMsmdDHpt58T1E= X-Google-Smtp-Source: ACcGV63Uxm7IcFzVEvRpE3DCiBhZUvVl8j1rAkIscdJIpUOJMsi2ZnLrTlst7ixn3u4OgrEF+TWgqQ== X-Received: by 2002:a17:902:2:: with SMTP id 2-v6mr16646500pla.178.1539595005997; Mon, 15 Oct 2018 02:16:45 -0700 (PDT) Received: from ala-d2121-lx1.wrs.com (unknown-156-139.windriver.com. [147.11.156.139]) by smtp.gmail.com with ESMTPSA id a11-v6sm10798256pgw.54.2018.10.15.02.16.44 (version=TLS1_1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 15 Oct 2018 02:16:45 -0700 (PDT) From: Bin Meng To: Simon Glass , U-Boot Mailing List Date: Mon, 15 Oct 2018 02:21:08 -0700 Message-Id: <1539595287-31378-13-git-send-email-bmeng.cn@gmail.com> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: <1539595287-31378-1-git-send-email-bmeng.cn@gmail.com> References: <1539595287-31378-1-git-send-email-bmeng.cn@gmail.com> Subject: [U-Boot] [PATCH v3 12/31] blk: Drop blk_prepare_device() X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.18 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" With the post_probe() changes, this API is no longer needed. Signed-off-by: Bin Meng Reviewed-by: Simon Glass Signed-off-by: Bin Meng Reviewed-by: Simon Glass --- Changes in v3: None Changes in v2: None drivers/block/blk-uclass.c | 9 --------- include/blk.h | 10 ---------- 2 files changed, 19 deletions(-) diff --git a/drivers/block/blk-uclass.c b/drivers/block/blk-uclass.c index 95e7b54..3f00ff8 100644 --- a/drivers/block/blk-uclass.c +++ b/drivers/block/blk-uclass.c @@ -471,15 +471,6 @@ unsigned long blk_derase(struct blk_desc *block_dev, lbaint_t start, return ops->erase(dev, start, blkcnt); } -int blk_prepare_device(struct udevice *dev) -{ - struct blk_desc *desc = dev_get_uclass_platdata(dev); - - part_init(desc); - - return 0; -} - int blk_get_from_parent(struct udevice *parent, struct udevice **devp) { struct udevice *dev; diff --git a/include/blk.h b/include/blk.h index 6af2196..c243179 100644 --- a/include/blk.h +++ b/include/blk.h @@ -357,16 +357,6 @@ int blk_create_devicef(struct udevice *parent, const char *drv_name, lbaint_t lba, struct udevice **devp); /** - * blk_prepare_device() - Prepare a block device for use - * - * This reads partition information from the device if supported. - * - * @dev: Device to prepare - * @return 0 if ok, -ve on error - */ -int blk_prepare_device(struct udevice *dev); - -/** * blk_unbind_all() - Unbind all device of the given interface type * * The devices are removed and then unbound. From patchwork Mon Oct 15 09:21:09 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bin Meng X-Patchwork-Id: 984030 X-Patchwork-Delegate: sjg@chromium.org Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=lists.denx.de (client-ip=81.169.180.215; helo=lists.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="TNu9+wsL"; dkim-atps=neutral Received: from lists.denx.de (dione.denx.de [81.169.180.215]) by ozlabs.org (Postfix) with ESMTP id 42YY9r5QQpz9sBq for ; Mon, 15 Oct 2018 20:31:56 +1100 (AEDT) Received: by lists.denx.de (Postfix, from userid 105) id 23332C21E52; Mon, 15 Oct 2018 09:23:56 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on lists.denx.de X-Spam-Level: X-Spam-Status: No, score=-0.0 required=5.0 tests=FREEMAIL_FROM, RCVD_IN_DNSWL_BLOCKED, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, T_DKIM_INVALID autolearn=unavailable autolearn_force=no version=3.4.0 Received: from lists.denx.de (localhost [IPv6:::1]) by lists.denx.de (Postfix) with ESMTP id 9A2F7C21EE0; Mon, 15 Oct 2018 09:17:04 +0000 (UTC) Received: by lists.denx.de (Postfix, from userid 105) id 2692AC21E39; Mon, 15 Oct 2018 09:17:00 +0000 (UTC) Received: from mail-pg1-f196.google.com (mail-pg1-f196.google.com [209.85.215.196]) by lists.denx.de (Postfix) with ESMTPS id 780CAC21BE5 for ; Mon, 15 Oct 2018 09:16:48 +0000 (UTC) Received: by mail-pg1-f196.google.com with SMTP id r9-v6so8873090pgv.6 for ; Mon, 15 Oct 2018 02:16:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=//shaBgbydnVbMZvmqw+AGTfgnBdXCtV5eIFhPHL85o=; b=TNu9+wsLvqHSYGUZYSuVaCglqUZEyyOv+ZAcGis514H9P0bF4r9mOQRblj52bxxtO1 CbiofLqjC0KkG4ZccPW9pX1xR0Vzy9MGz1Ldn3qa4x8BefDDhGPG7t4OE2BDj84mcvHY iZG/7Pq7ShrvV5ymfmR6HP4AFtoYitARBI1Wsmd77zM6BMhHFsVIwjUXfyW4pj4fk1lk lLjZGd7SvNJytZRd/w6N69Zs2sGHpvDnfT9g2rkflJYgXDWT0AvB8xZbAnY/qiB0OTRE aWVkrD4gjteb3oxFpBPkQIUi8Abld1PhaECem9jh+2pyj/Oae/Nf5zr3ZFZeqeEel4bo GB5w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=//shaBgbydnVbMZvmqw+AGTfgnBdXCtV5eIFhPHL85o=; b=SopeG1oFJv8mfeurQ59WGQFk38sg9FFvCJwHxB362TEgktWFHMuyvbRdRUssxf37f7 IhF1rRprRoP+S8cRzgXw0Zwli2Oa57bpYsvgHkHfXWE/9aZg3wJl3ZdqVmrzLqcGwEXp 8XWlUWKRJrDy8QJHPlfXWG1xg9UGY2vv/uzWJNVLeG/cJLnddiekBfZ+vYta1Zz2znHR N6RVxWMb6pf9bvQD74faVITxvcy6+rJI0YanctDHfOTwR0gXFNRsr6Cg7/JzaDO8IheC M6brAna1vSklkCqqmpCqazb9ohYpl0MSnPhgQy1vsifGuh6BMwfqEloSt/vrgZzXjd1H wQbw== X-Gm-Message-State: ABuFfoioIMfKSCJVMbr2OD8cF+fxvd+wBR6PW0M9gI9G/Cn+iPSylcNl /1pp4+8208udKcgrSnkvrao= X-Google-Smtp-Source: ACcGV609HoUjTFGt7pvk03XfaGbRV6mbr8NkNKZFd4IcEmwaF1yUypcsftQFe73U0ai2m5egOH3Fxg== X-Received: by 2002:a62:a0e:: with SMTP id s14-v6mr16670343pfi.153.1539595007138; Mon, 15 Oct 2018 02:16:47 -0700 (PDT) Received: from ala-d2121-lx1.wrs.com (unknown-156-139.windriver.com. [147.11.156.139]) by smtp.gmail.com with ESMTPSA id a11-v6sm10798256pgw.54.2018.10.15.02.16.46 (version=TLS1_1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 15 Oct 2018 02:16:46 -0700 (PDT) From: Bin Meng To: Simon Glass , U-Boot Mailing List Date: Mon, 15 Oct 2018 02:21:09 -0700 Message-Id: <1539595287-31378-14-git-send-email-bmeng.cn@gmail.com> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: <1539595287-31378-1-git-send-email-bmeng.cn@gmail.com> References: <1539595287-31378-1-git-send-email-bmeng.cn@gmail.com> Subject: [U-Boot] [PATCH v3 13/31] blk: Make blk_next_free_devnum() public X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.18 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" blk_next_free_devnum() can be helpful in some cases. Make it a public API. Signed-off-by: Bin Meng Reviewed-by: Simon Glass Signed-off-by: Bin Meng Reviewed-by: Simon Glass --- Changes in v3: None Changes in v2: None drivers/block/blk-uclass.c | 2 +- include/blk.h | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/drivers/block/blk-uclass.c b/drivers/block/blk-uclass.c index 3f00ff8..76ee3db 100644 --- a/drivers/block/blk-uclass.c +++ b/drivers/block/blk-uclass.c @@ -517,7 +517,7 @@ int blk_find_max_devnum(enum if_type if_type) return max_devnum; } -static int blk_next_free_devnum(enum if_type if_type) +int blk_next_free_devnum(enum if_type if_type) { int ret; diff --git a/include/blk.h b/include/blk.h index c243179..5e94f0e 100644 --- a/include/blk.h +++ b/include/blk.h @@ -379,6 +379,17 @@ int blk_unbind_all(int if_type); int blk_find_max_devnum(enum if_type if_type); /** + * blk_next_free_devnum() - get the next device number for an interface type + * + * Finds the next number that is safe to use for a newly allocated device for + * an interface type @if_type. + * + * @if_type: Interface type to scan + * @return next device number safe to use, or -ve on error + */ +int blk_next_free_devnum(enum if_type if_type); + +/** * blk_select_hwpart() - select a hardware partition * * Select a hardware partition if the device supports it (typically MMC does) From patchwork Mon Oct 15 09:21:10 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bin Meng X-Patchwork-Id: 984029 X-Patchwork-Delegate: sjg@chromium.org Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=lists.denx.de (client-ip=81.169.180.215; helo=lists.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="TOUtHtRr"; dkim-atps=neutral Received: from lists.denx.de (dione.denx.de [81.169.180.215]) by ozlabs.org (Postfix) with ESMTP id 42YY9r54Qpz9s5c for ; Mon, 15 Oct 2018 20:31:56 +1100 (AEDT) Received: by lists.denx.de (Postfix, from userid 105) id F05F8C21DA2; Mon, 15 Oct 2018 09:24:30 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on lists.denx.de X-Spam-Level: X-Spam-Status: No, score=-0.0 required=5.0 tests=FREEMAIL_FROM, RCVD_IN_DNSWL_BLOCKED, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, T_DKIM_INVALID autolearn=unavailable autolearn_force=no version=3.4.0 Received: from lists.denx.de (localhost [IPv6:::1]) by lists.denx.de (Postfix) with ESMTP id 48B81C21E39; Mon, 15 Oct 2018 09:17:08 +0000 (UTC) Received: by lists.denx.de (Postfix, from userid 105) id 8AE1BC21E38; Mon, 15 Oct 2018 09:17:03 +0000 (UTC) Received: from mail-pf1-f193.google.com (mail-pf1-f193.google.com [209.85.210.193]) by lists.denx.de (Postfix) with ESMTPS id A5F53C21E3E for ; Mon, 15 Oct 2018 09:16:49 +0000 (UTC) Received: by mail-pf1-f193.google.com with SMTP id f26-v6so9353172pfn.9 for ; Mon, 15 Oct 2018 02:16:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=gnwAIU7VkB9Jfkf8yCJTxHmmFxw7mw6EUVY8bxc2AZY=; b=TOUtHtRrOAg+kmkHxGAWqQBHJOrXygXesU/E8xyuSq+qxLeUOtx4eA8mjPejYRFlSM FRkeurKci7lns4oHzi0m3kUPCWxAdbmHKYNvBTiLispInrFt3uG4BHZvSnPfASuRf1fu 4D9tQY4+5SdZRpyPuObon3orfuhYbTLy/OVAmHvJMi2jvNZV67ThPCLNAYvcurvXwHmt 0zjwKNOYheyB1yCZCmDp9zn91UgbHAceMIz7LLgAz3tIw5Xg3mQLMofugWxOiFqHBd53 jWWyQU1U5/VR0J83/Bny4lItpmguuojttKAeUbXohKzE8qULEGuMBvLcGgmuNLq0VBS+ PpcQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=gnwAIU7VkB9Jfkf8yCJTxHmmFxw7mw6EUVY8bxc2AZY=; b=TsbBkqe8Ihc5wL4oilZI0zbNBu19GyR4x1yM1WUg9cEIrmwVus4SrTeRU4232Q0piE Fp4Ddw5ojccZ6spc4p+6oVwz0U0PtXbaID9XAUjeqq+tJnZkX4pk3HfbU1wf2Udj9Xdi g2x3/uxXHYzMSar6Q8TflpYdOnxCt3401M5JV7ALl31kajbFEtLQ5EaKcAgpU+RAiPKJ YFbFYmwQGorf/2rYZxJEZ+IutIY7e+arbk8+WgGfxBqEKFO00jOBcivriLfcsl5i7YvD 0wh3EOpvSqzySq0uNoZ+Pm43cxphd+bCtgFzFsZd8ks6ZKteYG9KqRx4e2BxTRqsOLb1 Ferw== X-Gm-Message-State: ABuFfogoEuBoa1G+YWf84uCgVZXdnn2bZxCbGVxbYdtzeJ2Mz0+/jpq+ dXPlgaeFZi2sPOgPnPymfgU= X-Google-Smtp-Source: ACcGV61vMSCYO5kOByBKKjIAS/kxykM1FGOM0BzjhAbeRn0ZzVEvka/4UNJ0vETeoJL8a+ZK6Ut7Xw== X-Received: by 2002:a62:e110:: with SMTP id q16-v6mr16440593pfh.75.1539595008295; Mon, 15 Oct 2018 02:16:48 -0700 (PDT) Received: from ala-d2121-lx1.wrs.com (unknown-156-139.windriver.com. [147.11.156.139]) by smtp.gmail.com with ESMTPSA id a11-v6sm10798256pgw.54.2018.10.15.02.16.47 (version=TLS1_1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 15 Oct 2018 02:16:47 -0700 (PDT) From: Bin Meng To: Simon Glass , U-Boot Mailing List Date: Mon, 15 Oct 2018 02:21:10 -0700 Message-Id: <1539595287-31378-15-git-send-email-bmeng.cn@gmail.com> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: <1539595287-31378-1-git-send-email-bmeng.cn@gmail.com> References: <1539595287-31378-1-git-send-email-bmeng.cn@gmail.com> Subject: [U-Boot] [PATCH v3 14/31] blk: Introduce IF_TYPE_VIRTIO X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.18 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" From: Tuomas Tynkkynen This adds a new block interface type for VirtIO block devices. Signed-off-by: Tuomas Tynkkynen Signed-off-by: Bin Meng Reviewed-by: Simon Glass Signed-off-by: Tuomas Tynkkynen Signed-off-by: Bin Meng Reviewed-by: Simon Glass --- Changes in v3: None Changes in v2: None disk/part.c | 6 ++++++ drivers/block/blk-uclass.c | 2 ++ include/blk.h | 1 + 3 files changed, 9 insertions(+) diff --git a/disk/part.c b/disk/part.c index 9e457a6..f30f9e9 100644 --- a/disk/part.c +++ b/disk/part.c @@ -150,6 +150,9 @@ void dev_print (struct blk_desc *dev_desc) dev_desc->revision, dev_desc->product); break; + case IF_TYPE_VIRTIO: + printf("%s VirtIO Block Device\n", dev_desc->vendor); + break; case IF_TYPE_DOC: puts("device type DOC\n"); return; @@ -281,6 +284,9 @@ static void print_part_header(const char *type, struct blk_desc *dev_desc) case IF_TYPE_NVME: puts ("NVMe"); break; + case IF_TYPE_VIRTIO: + puts("VirtIO"); + break; default: puts ("UNKNOWN"); break; diff --git a/drivers/block/blk-uclass.c b/drivers/block/blk-uclass.c index 76ee3db..65a766e 100644 --- a/drivers/block/blk-uclass.c +++ b/drivers/block/blk-uclass.c @@ -23,6 +23,7 @@ static const char *if_typename_str[IF_TYPE_COUNT] = { [IF_TYPE_HOST] = "host", [IF_TYPE_NVME] = "nvme", [IF_TYPE_EFI] = "efi", + [IF_TYPE_VIRTIO] = "virtio", }; static enum uclass_id if_type_uclass_id[IF_TYPE_COUNT] = { @@ -37,6 +38,7 @@ static enum uclass_id if_type_uclass_id[IF_TYPE_COUNT] = { [IF_TYPE_HOST] = UCLASS_ROOT, [IF_TYPE_NVME] = UCLASS_NVME, [IF_TYPE_EFI] = UCLASS_EFI, + [IF_TYPE_VIRTIO] = UCLASS_VIRTIO, }; static enum if_type if_typename_to_iftype(const char *if_typename) diff --git a/include/blk.h b/include/blk.h index 5e94f0e..d0c033a 100644 --- a/include/blk.h +++ b/include/blk.h @@ -33,6 +33,7 @@ enum if_type { IF_TYPE_HOST, IF_TYPE_NVME, IF_TYPE_EFI, + IF_TYPE_VIRTIO, IF_TYPE_COUNT, /* Number of interface types */ }; From patchwork Mon Oct 15 09:21:11 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bin Meng X-Patchwork-Id: 984043 X-Patchwork-Delegate: sjg@chromium.org Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=lists.denx.de (client-ip=81.169.180.215; helo=lists.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="ZA6oQoCX"; dkim-atps=neutral Received: from lists.denx.de (dione.denx.de [81.169.180.215]) by ozlabs.org (Postfix) with ESMTP id 42YYFj0P8tz9sBq for ; Mon, 15 Oct 2018 20:35:20 +1100 (AEDT) Received: by lists.denx.de (Postfix, from userid 105) id 9C59BC21C50; Mon, 15 Oct 2018 09:25:28 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on lists.denx.de X-Spam-Level: X-Spam-Status: No, score=-0.0 required=5.0 tests=FREEMAIL_FROM, RCVD_IN_DNSWL_BLOCKED, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, T_DKIM_INVALID autolearn=unavailable autolearn_force=no version=3.4.0 Received: from lists.denx.de (localhost [IPv6:::1]) by lists.denx.de (Postfix) with ESMTP id 1BC45C21EFC; Mon, 15 Oct 2018 09:17:11 +0000 (UTC) Received: by lists.denx.de (Postfix, from userid 105) id 46FA4C21BE5; Mon, 15 Oct 2018 09:17:04 +0000 (UTC) Received: from mail-pl1-f196.google.com (mail-pl1-f196.google.com [209.85.214.196]) by lists.denx.de (Postfix) with ESMTPS id 14BFFC21E2C for ; Mon, 15 Oct 2018 09:16:51 +0000 (UTC) Received: by mail-pl1-f196.google.com with SMTP id s4-v6so9019335plp.0 for ; Mon, 15 Oct 2018 02:16:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=HqzNC8g578HrTuHHIQ1UBF7Gy95fH1iAkDcAcpNVNpA=; b=ZA6oQoCXEHH8kEC/ppEuysCnK0/SBo9P0nk8WsfPE48Om2xOl6lrq0pyqnRFw43H+V rFA/roknZiJ9CcqYEM9J+F7v8ovMPzLWtAAoF8JGr3pFWct1n9LStsqWyFmfq1yglZtw qcrWo3vwX4wAtledbceMX4vzCeZLeg+yuakWMowDWSMlx1NEwp3YcorTgLoFrqMOHBd4 m+/RbFa4aWKXNbiaAC25zFfPgRvABIMMSfnCx48VE5K6FEPlWqLgIxJYCY0czIj5z+w4 W+hnKw9R9J+OtLM+Htjlps1uYXDpgQr8szzoUXD51m1CQaMlrUwQCn+eDIsA1bZWkXHC AXcA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=HqzNC8g578HrTuHHIQ1UBF7Gy95fH1iAkDcAcpNVNpA=; b=kOFTMjJBoP+u68XOYo3AQIco0qFJICPOL9mCHOgGLhwxyBAkgY9iY/UkLN+yV46D10 oEnHSgv2t1OETEQZahuFbD9MyxXv0Woia5icQVYuO7VpldPzL3XFtap6KFJMoUHdGmlH LPfUiTyp2EXvCEkf2zKiRuG7w+J6aZHTr0e1WbzGdxhWZSUA5Li9G+dn4LiatNXD7iSD hNtpd2KlpDY5fH3L1Oor3sYGRkrrBeIgteaN9ALi5hXZGWV3uvEVDYrYhAbRWeSKEVcB SU1VZyUEToLo1jZGQrCNcp96QozOeZWjh+bQLxAbI+Xk9Gw0L/tR/ZgU0m8MlHOG8wTy LVWA== X-Gm-Message-State: ABuFfogEKQlANJiPWl/XrsW/OY+n74RxnqyOyCZ77q788dy4CG5LScZl 1J+xSZ2Bm5ifLLF2NX7JQPU= X-Google-Smtp-Source: ACcGV60rRrSfxUPj4Y10Cat6VdiMYyngLsCnyHgHyV7Zwd6uPT5mn79QkOQU3g+1Jax0h7gndz1K2g== X-Received: by 2002:a17:902:bc42:: with SMTP id t2-v6mr9730040plz.168.1539595009568; Mon, 15 Oct 2018 02:16:49 -0700 (PDT) Received: from ala-d2121-lx1.wrs.com (unknown-156-139.windriver.com. [147.11.156.139]) by smtp.gmail.com with ESMTPSA id a11-v6sm10798256pgw.54.2018.10.15.02.16.48 (version=TLS1_1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 15 Oct 2018 02:16:48 -0700 (PDT) From: Bin Meng To: Simon Glass , U-Boot Mailing List Date: Mon, 15 Oct 2018 02:21:11 -0700 Message-Id: <1539595287-31378-16-git-send-email-bmeng.cn@gmail.com> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: <1539595287-31378-1-git-send-email-bmeng.cn@gmail.com> References: <1539595287-31378-1-git-send-email-bmeng.cn@gmail.com> Subject: [U-Boot] [PATCH v3 15/31] virtio: Add block driver support X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.18 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" From: Tuomas Tynkkynen This adds virtio block device driver support. Signed-off-by: Tuomas Tynkkynen Signed-off-by: Bin Meng Reviewed-by: Simon Glass Signed-off-by: Tuomas Tynkkynen Signed-off-by: Bin Meng Reviewed-by: Simon Glass --- Changes in v3: None Changes in v2: - Correct desc->vendor for PCI transport drivers/virtio/Kconfig | 7 +++ drivers/virtio/Makefile | 1 + drivers/virtio/virtio_blk.c | 137 ++++++++++++++++++++++++++++++++++++++++++++ drivers/virtio/virtio_blk.h | 129 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 274 insertions(+) create mode 100644 drivers/virtio/virtio_blk.c create mode 100644 drivers/virtio/virtio_blk.h diff --git a/drivers/virtio/Kconfig b/drivers/virtio/Kconfig index e20dd69..b72477a 100644 --- a/drivers/virtio/Kconfig +++ b/drivers/virtio/Kconfig @@ -36,4 +36,11 @@ config VIRTIO_NET This is the virtual net driver for virtio. It can be used with QEMU based targets. +config VIRTIO_BLK + bool "virtio block driver" + depends on VIRTIO + help + This is the virtual block driver for virtio. It can be used with + QEMU based targets. + endmenu diff --git a/drivers/virtio/Makefile b/drivers/virtio/Makefile index b7764f1..5fe7428 100644 --- a/drivers/virtio/Makefile +++ b/drivers/virtio/Makefile @@ -6,3 +6,4 @@ obj-y += virtio-uclass.o virtio_ring.o obj-$(CONFIG_VIRTIO_MMIO) += virtio_mmio.o obj-$(CONFIG_VIRTIO_NET) += virtio_net.o +obj-$(CONFIG_VIRTIO_BLK) += virtio_blk.o diff --git a/drivers/virtio/virtio_blk.c b/drivers/virtio/virtio_blk.c new file mode 100644 index 0000000..e793e34 --- /dev/null +++ b/drivers/virtio/virtio_blk.c @@ -0,0 +1,137 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * Copyright (C) 2018, Tuomas Tynkkynen + * Copyright (C) 2018, Bin Meng + */ + +#include +#include +#include +#include +#include +#include +#include "virtio_blk.h" + +struct virtio_blk_priv { + struct virtqueue *vq; +}; + +static ulong virtio_blk_do_req(struct udevice *dev, u64 sector, + lbaint_t blkcnt, void *buffer, u32 type) +{ + struct virtio_blk_priv *priv = dev_get_priv(dev); + unsigned int num_out = 0, num_in = 0; + struct virtio_sg *sgs[3]; + u8 status; + int ret; + + struct virtio_blk_outhdr out_hdr = { + .type = cpu_to_virtio32(dev, type), + .sector = cpu_to_virtio64(dev, sector), + }; + struct virtio_sg hdr_sg = { &out_hdr, sizeof(out_hdr) }; + struct virtio_sg data_sg = { buffer, blkcnt * 512 }; + struct virtio_sg status_sg = { &status, sizeof(status) }; + + sgs[num_out++] = &hdr_sg; + + if (type & VIRTIO_BLK_T_OUT) + sgs[num_out++] = &data_sg; + else + sgs[num_out + num_in++] = &data_sg; + + sgs[num_out + num_in++] = &status_sg; + + ret = virtqueue_add(priv->vq, sgs, num_out, num_in); + if (ret) + return ret; + + virtqueue_kick(priv->vq); + + while (!virtqueue_get_buf(priv->vq, NULL)) + ; + + return status == VIRTIO_BLK_S_OK ? blkcnt : -EIO; +} + +static ulong virtio_blk_read(struct udevice *dev, lbaint_t start, + lbaint_t blkcnt, void *buffer) +{ + return virtio_blk_do_req(dev, start, blkcnt, buffer, + VIRTIO_BLK_T_IN); +} + +static ulong virtio_blk_write(struct udevice *dev, lbaint_t start, + lbaint_t blkcnt, const void *buffer) +{ + return virtio_blk_do_req(dev, start, blkcnt, (void *)buffer, + VIRTIO_BLK_T_OUT); +} + +static int virtio_blk_bind(struct udevice *dev) +{ + struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(dev->parent); + struct blk_desc *desc = dev_get_uclass_platdata(dev); + int devnum; + + desc->if_type = IF_TYPE_VIRTIO; + /* + * Initialize the devnum to -ENODEV. This is to make sure that + * blk_next_free_devnum() works as expected, since the default + * value 0 is a valid devnum. + */ + desc->devnum = -ENODEV; + devnum = blk_next_free_devnum(IF_TYPE_VIRTIO); + if (devnum < 0) + return devnum; + desc->devnum = devnum; + desc->part_type = PART_TYPE_UNKNOWN; + /* + * virtio mmio transport supplies string identification for us, + * while pci trnasport uses a 2-byte subvendor value. + */ + if (uc_priv->vendor >> 16) + sprintf(desc->vendor, "%s", (char *)&uc_priv->vendor); + else + sprintf(desc->vendor, "%04x", uc_priv->vendor); + desc->bdev = dev; + + /* Indicate what driver features we support */ + virtio_driver_features_init(uc_priv, NULL, 0, NULL, 0); + + return 0; +} + +static int virtio_blk_probe(struct udevice *dev) +{ + struct virtio_blk_priv *priv = dev_get_priv(dev); + struct blk_desc *desc = dev_get_uclass_platdata(dev); + u64 cap; + int ret; + + ret = virtio_find_vqs(dev, 1, &priv->vq); + if (ret) + return ret; + + desc->blksz = 512; + virtio_cread(dev, struct virtio_blk_config, capacity, &cap); + desc->lba = cap; + + return 0; +} + +static const struct blk_ops virtio_blk_ops = { + .read = virtio_blk_read, + .write = virtio_blk_write, +}; + +U_BOOT_DRIVER(virtio_blk) = { + .name = VIRTIO_BLK_DRV_NAME, + .id = UCLASS_BLK, + .ops = &virtio_blk_ops, + .bind = virtio_blk_bind, + .probe = virtio_blk_probe, + .remove = virtio_reset, + .priv_auto_alloc_size = sizeof(struct virtio_blk_priv), + .flags = DM_FLAG_ACTIVE_DMA, +}; diff --git a/drivers/virtio/virtio_blk.h b/drivers/virtio/virtio_blk.h new file mode 100644 index 0000000..8d8e02f --- /dev/null +++ b/drivers/virtio/virtio_blk.h @@ -0,0 +1,129 @@ +/* SPDX-License-Identifier: BSD-3-Clause */ +/* + * Copyright (C) 2018, Tuomas Tynkkynen + * Copyright (C) 2018, Bin Meng + * + * From Linux kernel include/uapi/linux/virtio_blk.h + */ + +#ifndef _LINUX_VIRTIO_BLK_H +#define _LINUX_VIRTIO_BLK_H + +/* Feature bits */ +#define VIRTIO_BLK_F_SIZE_MAX 1 /* Indicates maximum segment size */ +#define VIRTIO_BLK_F_SEG_MAX 2 /* Indicates maximum # of segments */ +#define VIRTIO_BLK_F_GEOMETRY 4 /* Legacy geometry available */ +#define VIRTIO_BLK_F_RO 5 /* Disk is read-only */ +#define VIRTIO_BLK_F_BLK_SIZE 6 /* Block size of disk is available */ +#define VIRTIO_BLK_F_TOPOLOGY 10 /* Topology information is available */ +#define VIRTIO_BLK_F_MQ 12 /* Support more than one vq */ + +/* Legacy feature bits */ +#ifndef VIRTIO_BLK_NO_LEGACY +#define VIRTIO_BLK_F_BARRIER 0 /* Does host support barriers? */ +#define VIRTIO_BLK_F_SCSI 7 /* Supports scsi command passthru */ +#define VIRTIO_BLK_F_FLUSH 9 /* Flush command supported */ +#define VIRTIO_BLK_F_CONFIG_WCE 11 /* Writeback mode available in config */ +#ifndef __KERNEL__ +/* Old (deprecated) name for VIRTIO_BLK_F_FLUSH */ +#define VIRTIO_BLK_F_WCE VIRTIO_BLK_F_FLUSH +#endif +#endif /* !VIRTIO_BLK_NO_LEGACY */ + +#define VIRTIO_BLK_ID_BYTES 20 /* ID string length */ + +struct __packed virtio_blk_config { + /* The capacity (in 512-byte sectors) */ + __u64 capacity; + /* The maximum segment size (if VIRTIO_BLK_F_SIZE_MAX) */ + __u32 size_max; + /* The maximum number of segments (if VIRTIO_BLK_F_SEG_MAX) */ + __u32 seg_max; + /* geometry of the device (if VIRTIO_BLK_F_GEOMETRY) */ + struct virtio_blk_geometry { + __u16 cylinders; + __u8 heads; + __u8 sectors; + } geometry; + + /* block size of device (if VIRTIO_BLK_F_BLK_SIZE) */ + __u32 blk_size; + + /* the next 4 entries are guarded by VIRTIO_BLK_F_TOPOLOGY */ + /* exponent for physical block per logical block */ + __u8 physical_block_exp; + /* alignment offset in logical blocks */ + __u8 alignment_offset; + /* minimum I/O size without performance penalty in logical blocks */ + __u16 min_io_size; + /* optimal sustained I/O size in logical blocks */ + __u32 opt_io_size; + + /* writeback mode (if VIRTIO_BLK_F_CONFIG_WCE) */ + __u8 wce; + __u8 unused; + + /* number of vqs, only available when VIRTIO_BLK_F_MQ is set */ + __u16 num_queues; +}; + +/* + * Command types + * + * Usage is a bit tricky as some bits are used as flags and some are not. + * + * Rules: + * VIRTIO_BLK_T_OUT may be combined with VIRTIO_BLK_T_SCSI_CMD or + * VIRTIO_BLK_T_BARRIER. VIRTIO_BLK_T_FLUSH is a command of its own + * and may not be combined with any of the other flags. + */ + +/* These two define direction */ +#define VIRTIO_BLK_T_IN 0 +#define VIRTIO_BLK_T_OUT 1 + +#ifndef VIRTIO_BLK_NO_LEGACY +/* This bit says it's a scsi command, not an actual read or write */ +#define VIRTIO_BLK_T_SCSI_CMD 2 +#endif /* VIRTIO_BLK_NO_LEGACY */ + +/* Cache flush command */ +#define VIRTIO_BLK_T_FLUSH 4 + +/* Get device ID command */ +#define VIRTIO_BLK_T_GET_ID 8 + +#ifndef VIRTIO_BLK_NO_LEGACY +/* Barrier before this op */ +#define VIRTIO_BLK_T_BARRIER 0x80000000 +#endif /* !VIRTIO_BLK_NO_LEGACY */ + +/* + * This comes first in the read scatter-gather list. + * For legacy virtio, if VIRTIO_F_ANY_LAYOUT is not negotiated, + * this is the first element of the read scatter-gather list. + */ +struct virtio_blk_outhdr { + /* VIRTIO_BLK_T* */ + __virtio32 type; + /* io priority */ + __virtio32 ioprio; + /* Sector (ie. 512 byte offset) */ + __virtio64 sector; +}; + +#ifndef VIRTIO_BLK_NO_LEGACY +struct virtio_scsi_inhdr { + __virtio32 errors; + __virtio32 data_len; + __virtio32 sense_len; + __virtio32 residual; +}; +#endif /* !VIRTIO_BLK_NO_LEGACY */ + +/* And this is the final byte of the write scatter-gather list */ +#define VIRTIO_BLK_S_OK 0 +#define VIRTIO_BLK_S_IOERR 1 +#define VIRTIO_BLK_S_UNSUPP 2 + +#endif /* _LINUX_VIRTIO_BLK_H */ From patchwork Mon Oct 15 09:21:12 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bin Meng X-Patchwork-Id: 984028 X-Patchwork-Delegate: sjg@chromium.org Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=lists.denx.de (client-ip=81.169.180.215; helo=lists.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="ayyjQRV8"; dkim-atps=neutral Received: from lists.denx.de (dione.denx.de [81.169.180.215]) by ozlabs.org (Postfix) with ESMTP id 42YY9F3tXXz9s5c for ; Mon, 15 Oct 2018 20:31:29 +1100 (AEDT) Received: by lists.denx.de (Postfix, from userid 105) id 427B9C21DFB; Mon, 15 Oct 2018 09:24:51 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on lists.denx.de X-Spam-Level: X-Spam-Status: No, score=-0.0 required=5.0 tests=FREEMAIL_FROM, RCVD_IN_DNSWL_BLOCKED, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, T_DKIM_INVALID autolearn=unavailable autolearn_force=no version=3.4.0 Received: from lists.denx.de (localhost [IPv6:::1]) by lists.denx.de (Postfix) with ESMTP id 33388C21EC8; Mon, 15 Oct 2018 09:17:09 +0000 (UTC) Received: by lists.denx.de (Postfix, from userid 105) id 05D6BC21D83; Mon, 15 Oct 2018 09:17:05 +0000 (UTC) Received: from mail-pf1-f193.google.com (mail-pf1-f193.google.com [209.85.210.193]) by lists.denx.de (Postfix) with ESMTPS id 36958C21E2B for ; Mon, 15 Oct 2018 09:16:52 +0000 (UTC) Received: by mail-pf1-f193.google.com with SMTP id j23-v6so9360407pfi.4 for ; Mon, 15 Oct 2018 02:16:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=/r4YVe5ENWxOwP1Wmbi6VkJcDSMvEq5aIwJgi+JvckY=; b=ayyjQRV8EkqEaA+FYk9DoiH3IDo2TZ8DipS70NFj3BZZFg2bwV7QLWmKAGMwOux3tY 8BkUlswSIa5SVp6lpm7ygEgZWkhGHi66H9eeA+4rgUWkxUEga/yg+6W6BcByMrCaC5Uj Nt5kVHlmiRXcBMrhm00kPlxS35fn4skyMeAk8qPWb0++1LJZkk/fF1GFV8WPUTwzYX0I 0KeDrJvVR0Q6dOSrMym4tsuoVP8kG63vq4anouPtkYokNPeKiScj8Yb3gykvrGeOavgE iaiWYzyu8xx/x75GzgGqzQoBvJ0Kg6oNV4CVl3Jg/6Q8UEOxp3zMoO9N0ZuhD6hGODkl u3vg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=/r4YVe5ENWxOwP1Wmbi6VkJcDSMvEq5aIwJgi+JvckY=; b=LFt1duxQZ4U9kFc4w73j9bGVMtlBtj636uEOreZeGR3fd0KGElAK+8FplU9+Mia9+9 wUmCcoMlhIBJp+b+7s6Sw+YvSFl94eKI6AJ31Aqt5ubKDyhHYCt17TYwQ0x1YL0sxhzu MPkQtjQ7LZNtw+pXNicr2N1G1KnxTAjn5RAYVEn8J1EZVeJZfAqGutr/FKlqhxYNCgGd 8/iSGkcQMAhBfiemE4agxuHFAXLQy6GxRmW8rHG49kTQAEFLN8vwrTszmQlbQdi7M4lU dOxWPjvG+PuhV4ICt9byN/vMKPDqYrCOIcgwRLGwRMOI4bpiDMMHFraRbOXG23ti8ZF9 SoiA== X-Gm-Message-State: ABuFfoh5W+ps/UiHwea86LMf5YmaqOeXLCte2YErH1ebNBtwilE5SnUN t1wZwqLNzeMvCcwgoz3INKCuhth/ X-Google-Smtp-Source: ACcGV62XvwGEWgSwO+vrpIIO3mnLKAHA2DqR/PxvfKb33HYZCksoLFN0MJFAyePlrAo3dKUIV1QvCA== X-Received: by 2002:a63:6342:: with SMTP id x63-v6mr14417018pgb.83.1539595010784; Mon, 15 Oct 2018 02:16:50 -0700 (PDT) Received: from ala-d2121-lx1.wrs.com (unknown-156-139.windriver.com. [147.11.156.139]) by smtp.gmail.com with ESMTPSA id a11-v6sm10798256pgw.54.2018.10.15.02.16.49 (version=TLS1_1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 15 Oct 2018 02:16:49 -0700 (PDT) From: Bin Meng To: Simon Glass , U-Boot Mailing List Date: Mon, 15 Oct 2018 02:21:12 -0700 Message-Id: <1539595287-31378-17-git-send-email-bmeng.cn@gmail.com> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: <1539595287-31378-1-git-send-email-bmeng.cn@gmail.com> References: <1539595287-31378-1-git-send-email-bmeng.cn@gmail.com> Subject: [U-Boot] [PATCH v3 16/31] virtio: cmd: Add virtio command for virtio devices X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.18 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" From: Tuomas Tynkkynen Add 'virtio' command in U-Boot command line. Signed-off-by: Tuomas Tynkkynen Signed-off-by: Bin Meng Reviewed-by: Simon Glass Signed-off-by: Tuomas Tynkkynen Signed-off-by: Bin Meng Reviewed-by: Simon Glass --- Changes in v3: None Changes in v2: None cmd/Kconfig | 7 +++++++ cmd/Makefile | 1 + cmd/virtio.c | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+) create mode 100644 cmd/virtio.c diff --git a/cmd/Kconfig b/cmd/Kconfig index 7ed3c9c..2fbdb17 100644 --- a/cmd/Kconfig +++ b/cmd/Kconfig @@ -1065,6 +1065,13 @@ config CMD_USB_MASS_STORAGE help USB mass storage support +config CMD_VIRTIO + bool "virtio" + depends on VIRTIO + default y if VIRTIO + help + VirtIO block device support + config CMD_AXI bool "axi" depends on AXI diff --git a/cmd/Makefile b/cmd/Makefile index d9cdaf6..2e27514 100644 --- a/cmd/Makefile +++ b/cmd/Makefile @@ -136,6 +136,7 @@ obj-$(CONFIG_CMD_UBI) += ubi.o obj-$(CONFIG_CMD_UBIFS) += ubifs.o obj-$(CONFIG_CMD_UNIVERSE) += universe.o obj-$(CONFIG_CMD_UNZIP) += unzip.o +obj-$(CONFIG_CMD_VIRTIO) += virtio.o obj-$(CONFIG_CMD_LZMADEC) += lzmadec.o obj-$(CONFIG_CMD_USB) += usb.o disk.o diff --git a/cmd/virtio.c b/cmd/virtio.c new file mode 100644 index 0000000..b7082bc --- /dev/null +++ b/cmd/virtio.c @@ -0,0 +1,38 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * Copyright (C) 2018, Tuomas Tynkkynen + * Copyright (C) 2018, Bin Meng + */ + +#include +#include +#include +#include +#include + +static int virtio_curr_dev; + +static int do_virtio(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) +{ + if (argc == 2 && !strcmp(argv[1], "scan")) { + /* make sure all virtio devices are enumerated */ + virtio_init(); + + return CMD_RET_SUCCESS; + } + + return blk_common_cmd(argc, argv, IF_TYPE_VIRTIO, &virtio_curr_dev); +} + +U_BOOT_CMD( + virtio, 8, 1, do_virtio, + "virtio block devices sub-system", + "scan - initialize virtio bus\n" + "virtio info - show all available virtio block devices\n" + "virtio device [dev] - show or set current virtio block device\n" + "virtio part [dev] - print partition table of one or all virtio block devices\n" + "virtio read addr blk# cnt - read `cnt' blocks starting at block\n" + " `blk#' to memory address `addr'\n" + "virtio write addr blk# cnt - write `cnt' blocks starting at block\n" + " `blk#' from memory address `addr'" +); From patchwork Mon Oct 15 09:21:13 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bin Meng X-Patchwork-Id: 984045 X-Patchwork-Delegate: sjg@chromium.org Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=lists.denx.de (client-ip=81.169.180.215; helo=lists.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="MEatngSY"; dkim-atps=neutral Received: from lists.denx.de (dione.denx.de [81.169.180.215]) by ozlabs.org (Postfix) with ESMTP id 42YYGb6sSWz9s5c for ; Mon, 15 Oct 2018 20:36:07 +1100 (AEDT) Received: by lists.denx.de (Postfix, from userid 105) id AE26DC21E75; Mon, 15 Oct 2018 09:25:53 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on lists.denx.de X-Spam-Level: X-Spam-Status: No, score=-0.0 required=5.0 tests=FREEMAIL_FROM, RCVD_IN_DNSWL_BLOCKED, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, T_DKIM_INVALID autolearn=unavailable autolearn_force=no version=3.4.0 Received: from lists.denx.de (localhost [IPv6:::1]) by lists.denx.de (Postfix) with ESMTP id EA97DC21E73; Mon, 15 Oct 2018 09:17:11 +0000 (UTC) Received: by lists.denx.de (Postfix, from userid 105) id 73758C21E15; Mon, 15 Oct 2018 09:17:05 +0000 (UTC) Received: from mail-pl1-f196.google.com (mail-pl1-f196.google.com [209.85.214.196]) by lists.denx.de (Postfix) with ESMTPS id 70B6AC21E38 for ; Mon, 15 Oct 2018 09:16:53 +0000 (UTC) Received: by mail-pl1-f196.google.com with SMTP id y15-v6so8989737plr.12 for ; Mon, 15 Oct 2018 02:16:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=JG91yn3S2wgRqzGPVB5uG4KUqNM+XTgrRDAqeotsRXs=; b=MEatngSYddhtLuM/J+kEXT3E2GvPuLnte4GwXx0VTp/iCJpB6VghG0gNlH5J/7v+4R 3xct0dzgPCdDNCFikaJRrJOZtUeXL+Fv00Aetm1FNqRsu0Ra8H/ISTcPeyQAtbsksRD5 xmRH8IijsDO6gKriGb6XYOXMcYtWzd/kGl7i8CHMCiE4sGvnvHMjmkNac3X5X1bPQU/e opM1XHrSlAbORyLqgo9HDPuHtdObVsjcr7j1Krw+Y3P8d02eJ2VqR3oR0Bghf38OVq1R N9gs2tjA/g0e3mN8J+hYAGuUe5MAWpK2wSR4x+jIHFblHxKwYKKEuvksmDAKqpW+0yUn EjvA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=JG91yn3S2wgRqzGPVB5uG4KUqNM+XTgrRDAqeotsRXs=; b=QfQEB+F/bnuVzh6n2dWNxRYCReakzeOR7JIWijlXwSnbwnNEfrshABcLreSTQuQJsj maCBBwUCZzTPaO0uY+WU1HPeWSIX4oLn8WX0hXw7WXAmz0RTr4ahQpRzLcpPC0AmkAsA gBRktzYqh+vfO1DdHz/q9hIK7xJdh7JBqsDCh9C920Ww+S3Ov9FpVsNqy+VsgTmTNLEh CvZitV3nTf+5ASh1FpP7jREVVOCmlB28x/XzeTcJaqrqpb3NG4wo5/HtwM9WV/hBwFwP EsVqvTS9TFRFGV8yS2fD9X8NahPL++FxrdZinUnLsfHq755588V7fIomEqPs/DA0ceGq Dvtg== X-Gm-Message-State: ABuFfogDV0fzU0zsvs66EHeiWeDmWEHCO5ZpAdNtAA/El52DxwI0Uryn YtXB+nRWH5+VJ+lrsT9enV4= X-Google-Smtp-Source: ACcGV60o1wq0QFN6SgnoFoEcHOjuMEayFrX/v35NxN/nuzvpcceegMg8C7Q0X1NvJpn5wNQnpcJG+A== X-Received: by 2002:a17:902:b287:: with SMTP id u7-v6mr16115099plr.123.1539595012114; Mon, 15 Oct 2018 02:16:52 -0700 (PDT) Received: from ala-d2121-lx1.wrs.com (unknown-156-139.windriver.com. [147.11.156.139]) by smtp.gmail.com with ESMTPSA id a11-v6sm10798256pgw.54.2018.10.15.02.16.50 (version=TLS1_1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 15 Oct 2018 02:16:51 -0700 (PDT) From: Bin Meng To: Simon Glass , U-Boot Mailing List Date: Mon, 15 Oct 2018 02:21:13 -0700 Message-Id: <1539595287-31378-18-git-send-email-bmeng.cn@gmail.com> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: <1539595287-31378-1-git-send-email-bmeng.cn@gmail.com> References: <1539595287-31378-1-git-send-email-bmeng.cn@gmail.com> Subject: [U-Boot] [PATCH v3 17/31] riscv: qemu: Enumerate virtio bus during early boot X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.18 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" Currently devices on the virtio bus is not automatically enumerated, which means peripherals on the virtio bus are not discovered by their drivers. This uses board_init() to do the virtio enumeration. Signed-off-by: Bin Meng Reviewed-by: Simon Glass Signed-off-by: Bin Meng Reviewed-by: Simon Glass --- Changes in v3: None Changes in v2: None board/emulation/qemu-riscv/Kconfig | 3 +++ board/emulation/qemu-riscv/qemu-riscv.c | 9 +++++++++ 2 files changed, 12 insertions(+) diff --git a/board/emulation/qemu-riscv/Kconfig b/board/emulation/qemu-riscv/Kconfig index af23363..5ae56da 100644 --- a/board/emulation/qemu-riscv/Kconfig +++ b/board/emulation/qemu-riscv/Kconfig @@ -18,5 +18,8 @@ config SYS_TEXT_BASE config BOARD_SPECIFIC_OPTIONS # dummy def_bool y imply SYS_NS16550 + imply VIRTIO_MMIO + imply VIRTIO_NET + imply VIRTIO_BLK endif diff --git a/board/emulation/qemu-riscv/qemu-riscv.c b/board/emulation/qemu-riscv/qemu-riscv.c index 041e716..2730a28 100644 --- a/board/emulation/qemu-riscv/qemu-riscv.c +++ b/board/emulation/qemu-riscv/qemu-riscv.c @@ -4,12 +4,21 @@ */ #include +#include #include +#include +#include #define MROM_FDT_ADDR 0x1020 int board_init(void) { + /* + * Make sure virtio bus is enumerated so that peripherals + * on the virtio bus can be discovered by their drivers + */ + virtio_init(); + return 0; } From patchwork Mon Oct 15 09:21:14 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bin Meng X-Patchwork-Id: 984046 X-Patchwork-Delegate: sjg@chromium.org Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=lists.denx.de (client-ip=81.169.180.215; helo=lists.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="Xo2JhxRj"; dkim-atps=neutral Received: from lists.denx.de (dione.denx.de [81.169.180.215]) by ozlabs.org (Postfix) with ESMTP id 42YYGg15KYz9s5c for ; Mon, 15 Oct 2018 20:36:11 +1100 (AEDT) Received: by lists.denx.de (Postfix, from userid 105) id D3D19C21DE8; Mon, 15 Oct 2018 09:26:10 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on lists.denx.de X-Spam-Level: X-Spam-Status: No, score=-0.0 required=5.0 tests=FREEMAIL_FROM, RCVD_IN_DNSWL_BLOCKED, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, T_DKIM_INVALID autolearn=unavailable autolearn_force=no version=3.4.0 Received: from lists.denx.de (localhost [IPv6:::1]) by lists.denx.de (Postfix) with ESMTP id DDE73C21E85; Mon, 15 Oct 2018 09:17:12 +0000 (UTC) Received: by lists.denx.de (Postfix, from userid 105) id CEAE5C21D56; Mon, 15 Oct 2018 09:17:06 +0000 (UTC) Received: from mail-pf1-f196.google.com (mail-pf1-f196.google.com [209.85.210.196]) by lists.denx.de (Postfix) with ESMTPS id C24C3C21E6A for ; Mon, 15 Oct 2018 09:16:54 +0000 (UTC) Received: by mail-pf1-f196.google.com with SMTP id c25-v6so9363330pfe.6 for ; Mon, 15 Oct 2018 02:16:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=wlZPj4mZfAGjoBPIJNHbUE6CMlOeUDqA9WJi2jjcdxU=; b=Xo2JhxRjEuKtiENEs50YELdP7LQuv5p8kWAokdT5h5QDAFcC39/1mFejwmMunnwHA6 UiL7NMccFuIE6bKbq2WWmqT0VmgKDCmouyXO5QYr9GjqkLOKxZB71EIIYFWqfFHUNbiF MFE+jFClVgfchAJunfTrMvX9Bqz/s9tGR4lpVuU6x8CsDQ8qgIk6KzGPXSq5HcDiXytx nb1Pz/yy9awpwLWzvrwnXD4N00KM/EwquP78syR4OaJ/RXlcxmemrNT7v1c2qcWebHCs /2rTxqkXwN749HFX2H7iOMoWP9UMCLhzwrwzG5VorWGhMTK/TWTAUVLzOg8gwO+70fsp de4w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=wlZPj4mZfAGjoBPIJNHbUE6CMlOeUDqA9WJi2jjcdxU=; b=ERza/3yOpS/Zx9gQHE5EdufIyjvEWcmbdX1l8SXQ68XFCbZ2e3OTPXhY1D7hViSZB6 FW/bpLzBhO+zOa+k2r1r33i5qHj046v32P4XJgJ53p98E308oTTnLLzEV+QrnfxhPzgn aYd9gTL72fBDqeHZQEP1/WWAQngnDlDk/mDqROtZ/527aU99cBq5pj6t9AM3YEUKbThx aPRQoblNbj9cUYChMGREJ5rXqyN5LbJQ+oKNNVr3HKwSas3pfNu3u/sXJQgWp7uXKjDs oUDdJNNZWEA2Bu4yKdiYIS5OXjTtgrkuzc7cYjWcJsUtevhp+SJrjg6aQqcUks48bAAr ONHg== X-Gm-Message-State: ABuFfoim/+4Bi/DpPCfWWz1LGQEwxw/IHj5XDDLP5dQXGr83w+IQ2WNt B3FijLnosIYWymlhte/5UFpeROFh X-Google-Smtp-Source: ACcGV60HbG2YDuGXiKPiBUdydQk8PbisYyglVFb4QvaU4EN91HIM+L+VJo+7Ok/241FBg50o2xna+A== X-Received: by 2002:a63:f501:: with SMTP id w1-v6mr14618913pgh.336.1539595013256; Mon, 15 Oct 2018 02:16:53 -0700 (PDT) Received: from ala-d2121-lx1.wrs.com (unknown-156-139.windriver.com. [147.11.156.139]) by smtp.gmail.com with ESMTPSA id a11-v6sm10798256pgw.54.2018.10.15.02.16.52 (version=TLS1_1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 15 Oct 2018 02:16:52 -0700 (PDT) From: Bin Meng To: Simon Glass , U-Boot Mailing List Date: Mon, 15 Oct 2018 02:21:14 -0700 Message-Id: <1539595287-31378-19-git-send-email-bmeng.cn@gmail.com> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: <1539595287-31378-1-git-send-email-bmeng.cn@gmail.com> References: <1539595287-31378-1-git-send-email-bmeng.cn@gmail.com> Subject: [U-Boot] [PATCH v3 18/31] riscv: qemu: Include some useful commands X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.18 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" With the virtio net and blk drivers, we can do more stuff with some useful commands. Imply those in the board Kconfig. Signed-off-by: Bin Meng Reviewed-by: Simon Glass Signed-off-by: Bin Meng Reviewed-by: Simon Glass --- Changes in v3: None Changes in v2: None board/emulation/qemu-riscv/Kconfig | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/board/emulation/qemu-riscv/Kconfig b/board/emulation/qemu-riscv/Kconfig index 5ae56da..37a80db 100644 --- a/board/emulation/qemu-riscv/Kconfig +++ b/board/emulation/qemu-riscv/Kconfig @@ -21,5 +21,13 @@ config BOARD_SPECIFIC_OPTIONS # dummy imply VIRTIO_MMIO imply VIRTIO_NET imply VIRTIO_BLK + imply CMD_PING + imply CMD_FS_GENERIC + imply DOS_PARTITION + imply EFI_PARTITION + imply ISO_PARTITION + imply CMD_EXT2 + imply CMD_EXT4 + imply CMD_FAT endif From patchwork Mon Oct 15 09:21:15 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bin Meng X-Patchwork-Id: 984033 X-Patchwork-Delegate: sjg@chromium.org Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=lists.denx.de (client-ip=81.169.180.215; helo=lists.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="K4lTHnuB"; dkim-atps=neutral Received: from lists.denx.de (dione.denx.de [81.169.180.215]) by ozlabs.org (Postfix) with ESMTP id 42YYBc2Wbvz9s5c for ; Mon, 15 Oct 2018 20:32:40 +1100 (AEDT) Received: by lists.denx.de (Postfix, from userid 105) id 10BFBC21E36; Mon, 15 Oct 2018 09:25:09 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on lists.denx.de X-Spam-Level: X-Spam-Status: No, score=-0.0 required=5.0 tests=FREEMAIL_FROM, RCVD_IN_DNSWL_BLOCKED, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, T_DKIM_INVALID autolearn=unavailable autolearn_force=no version=3.4.0 Received: from lists.denx.de (localhost [IPv6:::1]) by lists.denx.de (Postfix) with ESMTP id 0F2E7C21E08; Mon, 15 Oct 2018 09:17:10 +0000 (UTC) Received: by lists.denx.de (Postfix, from userid 105) id B12E8C21D83; Mon, 15 Oct 2018 09:17:06 +0000 (UTC) Received: from mail-pf1-f196.google.com (mail-pf1-f196.google.com [209.85.210.196]) by lists.denx.de (Postfix) with ESMTPS id E12CDC21DFD for ; Mon, 15 Oct 2018 09:16:55 +0000 (UTC) Received: by mail-pf1-f196.google.com with SMTP id u12-v6so9343885pfn.12 for ; Mon, 15 Oct 2018 02:16:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=PkarnzC5u1jxAJutLqmTDkPMI4X/dqJk8AWwAXxyPh4=; b=K4lTHnuBpF2IIS0Sbal28mcn4w85iuPgz9m4+wqnC4rU3IvcpSuNWi1gmj1FddhB3n zZ7qTE5ADbIDDnqdzVy65pGwFkzvsaXIHkY1AwXD2/BBAXpY2ZpFVGNaENJW2Udk0LBg NiIYs6W1gFmwZQVmWXebMBBeYqNxQD32dcFCb+TkCDkeARwSox09kVWm3QdP/KXFCN13 58OtmMzlLykK8XUy3h+e0k7KoAin4LiyNUK8abIlDNPR9uLd9rsgrmEym8s8P5mZnPkr jrtYzwFgWc9QK+U/NLQs9qhRfxqiCSNCdIBnrx5/itBTymCUhrYYlUQYt4CHtC0FOWiw HK6w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=PkarnzC5u1jxAJutLqmTDkPMI4X/dqJk8AWwAXxyPh4=; b=YN0+PAC3oEMZVTEqTmaa3JU6r9H6jal5VrkkfCbRk0jGpD/hHmWAnOy7ZvridpRwrU IsCfG7s1XpUxqtR9lNRu+Uw0bCZrzv+UsLI52rH37kptvCsBpK6cfpZkak1MdYfzLJvl soqsWQR+3aDSPB6UbL4itxYo3S+w9cAnzONdIaDPRU2cRB6bCMo2E9FLRFmCMRxpuVky dKgoLqpj0VpdZ32sV4Tip3OF4RzXssOZcEuT/S+CkT+GZtgcCDMPGcnSX5xjVQVCUloX QEh/8M5JPokCtiXpAmZam4YjUH5EC2CZX+8dVKDY7Ox1q4BXFj4vnsv6+JPMV04bldF4 8SHA== X-Gm-Message-State: ABuFfog1DF9FNVgRSAe7qNIAUk4OG8JvwdIUvReNzO3bLH7nsCctK3Ys WOs2YNkNIHXfAg4per/O3lc= X-Google-Smtp-Source: ACcGV6374VZNTEcF0g/EEL71YrBSw5iCFfviVbWkuHcVUAbMXK0NYh47w3mk/F/i9+ZpEDaveUZeUA== X-Received: by 2002:a63:9507:: with SMTP id p7-v6mr15393658pgd.449.1539595014547; Mon, 15 Oct 2018 02:16:54 -0700 (PDT) Received: from ala-d2121-lx1.wrs.com (unknown-156-139.windriver.com. [147.11.156.139]) by smtp.gmail.com with ESMTPSA id a11-v6sm10798256pgw.54.2018.10.15.02.16.53 (version=TLS1_1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 15 Oct 2018 02:16:53 -0700 (PDT) From: Bin Meng To: Simon Glass , U-Boot Mailing List Date: Mon, 15 Oct 2018 02:21:15 -0700 Message-Id: <1539595287-31378-20-git-send-email-bmeng.cn@gmail.com> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: <1539595287-31378-1-git-send-email-bmeng.cn@gmail.com> References: <1539595287-31378-1-git-send-email-bmeng.cn@gmail.com> Subject: [U-Boot] [PATCH v3 19/31] kconfig: Introduce HAVE_ARCH_IOMAP X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.18 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" Introduce a new Kconfig option for architecture codes to control whether it provides io{read,write}{8,16,32} I/O accessor functions. Signed-off-by: Bin Meng Reviewed-by: Simon Glass Signed-off-by: Bin Meng Reviewed-by: Simon Glass --- Changes in v3: None Changes in v2: None include/linux/io.h | 4 ++++ lib/Kconfig | 6 ++++++ 2 files changed, 10 insertions(+) diff --git a/include/linux/io.h b/include/linux/io.h index d1b3efe..9badab4 100644 --- a/include/linux/io.h +++ b/include/linux/io.h @@ -7,6 +7,7 @@ #include #include +#ifndef CONFIG_HAVE_ARCH_IOMAP static inline u8 ioread8(const volatile void __iomem *addr) { return readb(addr); @@ -21,6 +22,7 @@ static inline u32 ioread32(const volatile void __iomem *addr) { return readl(addr); } +#endif /* !CONFIG_HAVE_ARCH_IOMAP */ #ifdef CONFIG_64BIT static inline u64 ioread64(const volatile void __iomem *addr) @@ -29,6 +31,7 @@ static inline u64 ioread64(const volatile void __iomem *addr) } #endif /* CONFIG_64BIT */ +#ifndef CONFIG_HAVE_ARCH_IOMAP static inline void iowrite8(u8 value, volatile void __iomem *addr) { writeb(value, addr); @@ -43,6 +46,7 @@ static inline void iowrite32(u32 value, volatile void __iomem *addr) { writel(value, addr); } +#endif /* !CONFIG_HAVE_ARCH_IOMAP */ #ifdef CONFIG_64BIT static inline void iowrite64(u64 value, volatile void __iomem *addr) diff --git a/lib/Kconfig b/lib/Kconfig index ccab426..847e797 100644 --- a/lib/Kconfig +++ b/lib/Kconfig @@ -21,6 +21,12 @@ config DYNAMIC_CRC_TABLE Enable this option to calculate entries for CRC tables at runtime. This can be helpful when reducing the size of the build image +config HAVE_ARCH_IOMAP + bool + help + Enable this option if architecture provides io{read,write}{8,16,32} + I/O accessor functions. + config HAVE_PRIVATE_LIBGCC bool From patchwork Mon Oct 15 09:21:16 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bin Meng X-Patchwork-Id: 984042 X-Patchwork-Delegate: sjg@chromium.org Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=lists.denx.de (client-ip=81.169.180.215; helo=lists.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="XpXTIgWt"; dkim-atps=neutral Received: from lists.denx.de (dione.denx.de [81.169.180.215]) by ozlabs.org (Postfix) with ESMTP id 42YYFX5KHHz9s5c for ; Mon, 15 Oct 2018 20:35:12 +1100 (AEDT) Received: by lists.denx.de (Postfix, from userid 105) id 61857C21E73; Mon, 15 Oct 2018 09:28:40 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on lists.denx.de X-Spam-Level: X-Spam-Status: No, score=-0.0 required=5.0 tests=FREEMAIL_FROM, RCVD_IN_DNSWL_BLOCKED, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, T_DKIM_INVALID autolearn=unavailable autolearn_force=no version=3.4.0 Received: from lists.denx.de (localhost [IPv6:::1]) by lists.denx.de (Postfix) with ESMTP id 5C474C21ED5; Mon, 15 Oct 2018 09:17:24 +0000 (UTC) Received: by lists.denx.de (Postfix, from userid 105) id B2C8AC21E89; Mon, 15 Oct 2018 09:17:06 +0000 (UTC) Received: from mail-pl1-f196.google.com (mail-pl1-f196.google.com [209.85.214.196]) by lists.denx.de (Postfix) with ESMTPS id 2DF66C21E47 for ; Mon, 15 Oct 2018 09:16:57 +0000 (UTC) Received: by mail-pl1-f196.google.com with SMTP id u6-v6so6255608plz.4 for ; Mon, 15 Oct 2018 02:16:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=jvZTS68QSCKrBDjeto4zUM0+Xoigoj1LczFlixRG2eU=; b=XpXTIgWtASLvOkCCrvt0wk9Se/FFqS0k1DBDgHFcjjHk/a9ogfxgSuGdRZSrkcevTy ++9m2lx0rl1ZkolKwKK4HEpRxN9CDQOnQVfa/QZgisnwfKsiiTHUrDn8HoDxtlR5XYDi bNGegV6KTNJDYfVj69R0hjNdZ6I12simO176I2nJ/pjb8hF+kwuo3WUatR394Dpyu29B XhTpZaCM4l7tuJLRFty8SrVYgwOTE7jthQ6ZfSrnkYeNPC1WTcHI5e96FFuiPpK8YLW7 U4wOAs46LCDZb046jpaIlRS917/DIQrZRMnBMQm5NfyBtarooU12Rn2aMaJTDmofiXmM df6g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=jvZTS68QSCKrBDjeto4zUM0+Xoigoj1LczFlixRG2eU=; b=cueIA2r+2xdOLqVz4cxWJ6e2CtdUFWocOUGOQYhOkVDnYDx9NbGv23GcP4JNaoTf2b 4T6v6QkipLlF4FgKNsmGiVA6c+q9bIJnmQWw078p62Dc9H1SsHcGD6lqL+SGbAn84bJN EBdqMQUgkkw7OQKRqBlCRgzSGxPlz894kJdqy3GMROGmDSnXrjYJLfRc+yVNBPIgENj8 zfy/NuM3Mv0vUoZyZBL5htUvzmfX2+AB2yf9VOrS3OAcf7I/ARHsIM92wAoc/+Xi0OiF sMUWBUG5jrybro/0bfeEqsgQaSSqSQwLqzHbKApn3U/Aj9EtSwDDk3Mg3Xbs6z1hkl5W 9LWg== X-Gm-Message-State: ABuFfohqbUAEY7rOgzE3sX+bt+9U0ZeqhDEakZVPfBvTu2O0HMtvmHJ0 ealkj9CEUXI+fCHm4hksNlL57Vv5 X-Google-Smtp-Source: ACcGV63tegOz67SMtOfpbAuaycqIhI3QLQOOhIQqO8+eALXo2O2DDdAIflLezLFkozEo3aaVUyIvrg== X-Received: by 2002:a17:902:b104:: with SMTP id q4-v6mr16301190plr.238.1539595015796; Mon, 15 Oct 2018 02:16:55 -0700 (PDT) Received: from ala-d2121-lx1.wrs.com (unknown-156-139.windriver.com. [147.11.156.139]) by smtp.gmail.com with ESMTPSA id a11-v6sm10798256pgw.54.2018.10.15.02.16.54 (version=TLS1_1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 15 Oct 2018 02:16:54 -0700 (PDT) From: Bin Meng To: Simon Glass , U-Boot Mailing List Date: Mon, 15 Oct 2018 02:21:16 -0700 Message-Id: <1539595287-31378-21-git-send-email-bmeng.cn@gmail.com> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: <1539595287-31378-1-git-send-email-bmeng.cn@gmail.com> References: <1539595287-31378-1-git-send-email-bmeng.cn@gmail.com> Subject: [U-Boot] [PATCH v3 20/31] x86: Implement arch-specific io accessor routines X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.18 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" At present the generic io{read,write}{8,16,32} routines only support MMIO access. With architecture like x86 that has a separate IO space, these routines cannot be used to access I/O ports. Implement x86-specific version to support both PIO and MMIO access, so that drivers for multiple architectures can use these accessors without the need to know whether it's MMIO or PIO. These are ported from Linux kernel lib/iomap.c, with slight changes. Signed-off-by: Bin Meng Reviewed-by: Simon Glass Signed-off-by: Bin Meng Reviewed-by: Simon Glass --- Changes in v3: None Changes in v2: None arch/Kconfig | 1 + arch/x86/include/asm/io.h | 66 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) diff --git a/arch/Kconfig b/arch/Kconfig index 1f2f407..e822a0b 100644 --- a/arch/Kconfig +++ b/arch/Kconfig @@ -120,6 +120,7 @@ config X86 select CREATE_ARCH_SYMLINK select DM select DM_PCI + select HAVE_ARCH_IOMAP select HAVE_PRIVATE_LIBGCC select OF_CONTROL select PCI diff --git a/arch/x86/include/asm/io.h b/arch/x86/include/asm/io.h index c05c6bf..81def0a 100644 --- a/arch/x86/include/asm/io.h +++ b/arch/x86/include/asm/io.h @@ -241,6 +241,72 @@ static inline void sync(void) #define __iormb() dmb() #define __iowmb() dmb() +/* + * Read/write from/to an (offsettable) iomem cookie. It might be a PIO + * access or a MMIO access, these functions don't care. The info is + * encoded in the hardware mapping set up by the mapping functions + * (or the cookie itself, depending on implementation and hw). + * + * The generic routines don't assume any hardware mappings, and just + * encode the PIO/MMIO as part of the cookie. They coldly assume that + * the MMIO IO mappings are not in the low address range. + * + * Architectures for which this is not true can't use this generic + * implementation and should do their own copy. + */ + +/* + * We assume that all the low physical PIO addresses (0-0xffff) always + * PIO. That means we can do some sanity checks on the low bits, and + * don't need to just take things for granted. + */ +#define PIO_RESERVED 0x10000UL + +/* + * Ugly macros are a way of life. + */ +#define IO_COND(addr, is_pio, is_mmio) do { \ + unsigned long port = (unsigned long __force)addr; \ + if (port >= PIO_RESERVED) { \ + is_mmio; \ + } else { \ + is_pio; \ + } \ +} while (0) + +static inline u8 ioread8(const volatile void __iomem *addr) +{ + IO_COND(addr, return inb(port), return readb(addr)); + return 0xff; +} + +static inline u16 ioread16(const volatile void __iomem *addr) +{ + IO_COND(addr, return inw(port), return readw(addr)); + return 0xffff; +} + +static inline u32 ioread32(const volatile void __iomem *addr) +{ + IO_COND(addr, return inl(port), return readl(addr)); + return 0xffffffff; +} + +static inline void iowrite8(u8 value, volatile void __iomem *addr) +{ + IO_COND(addr, outb(value, port), writeb(value, addr)); +} + +static inline void iowrite16(u16 value, volatile void __iomem *addr) +{ + IO_COND(addr, outw(value, port), writew(value, addr)); +} + +static inline void iowrite32(u32 value, volatile void __iomem *addr) +{ + IO_COND(addr, outl(value, port), writel(value, addr)); +} + #include #endif /* _ASM_IO_H */ From patchwork Mon Oct 15 09:21:17 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bin Meng X-Patchwork-Id: 984041 X-Patchwork-Delegate: sjg@chromium.org Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=lists.denx.de (client-ip=81.169.180.215; helo=lists.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="SbPxYkQl"; dkim-atps=neutral Received: from lists.denx.de (dione.denx.de [81.169.180.215]) by ozlabs.org (Postfix) with ESMTP id 42YYDw3cVXz9s5c for ; Mon, 15 Oct 2018 20:34:40 +1100 (AEDT) Received: by lists.denx.de (Postfix, from userid 105) id 38AB4C21E26; Mon, 15 Oct 2018 09:27:06 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on lists.denx.de X-Spam-Level: X-Spam-Status: No, score=-0.0 required=5.0 tests=FREEMAIL_FROM, RCVD_IN_DNSWL_BLOCKED, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, T_DKIM_INVALID autolearn=unavailable autolearn_force=no version=3.4.0 Received: from lists.denx.de (localhost [IPv6:::1]) by lists.denx.de (Postfix) with ESMTP id 80654C21F17; Mon, 15 Oct 2018 09:17:20 +0000 (UTC) Received: by lists.denx.de (Postfix, from userid 105) id 7F00BC21ECF; Mon, 15 Oct 2018 09:17:07 +0000 (UTC) Received: from mail-pl1-f194.google.com (mail-pl1-f194.google.com [209.85.214.194]) by lists.denx.de (Postfix) with ESMTPS id B954AC21E16 for ; Mon, 15 Oct 2018 09:16:58 +0000 (UTC) Received: by mail-pl1-f194.google.com with SMTP id y11-v6so8992842plt.3 for ; Mon, 15 Oct 2018 02:16:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=Whdurlvc32UxPYH4sSuYhWrlnEszu/had4Qq/Jms7xA=; b=SbPxYkQlrW1ies26G5aOvKdCJywO0cq4kx54jKgGyIr7Ak0loojCaeY2rIb85t19ym 6AEhfN+sPSXjcnfVH+WxyUKxT4Gkgbe42gUOz8v9ItJbxT2IvPIa0xNXlqb0DHM34SL7 n3mOM1kf1T5+7Y0NyIEJMv5zXjPZECNMI0o+dnSPSvALnE2oq6hgbg7Ke4XPmJEeYKIM 9nMJhnWonPdUqxmQUVaXZ2x5L9bYvLjM7QQOACl4o9cYQag4djEPhkNpMJKEXnZ31lx7 aK2jREELlSrZhRkaA4yQTJv3MEqxczKfbkAdo3bRQRXw8mD/Pv9SsaKJOZfz5z47NpSe heCw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=Whdurlvc32UxPYH4sSuYhWrlnEszu/had4Qq/Jms7xA=; b=rHf1u7ZBEOISvBp07m3VWyjG/oNUdQEXl/2BuLQvNuv7rfhM7Ynf4MzXILMQgGvQFX 2RD5qapAZHSVBkiUpRLS/BIspXjnNgQJz/DP30Ifpy4VFHrDl0HcvfbRjixI60aj0U4G AdJfjXy1Eo69EaB50O+bJECuw9zwoGjcS9lMPR7lXNha71RmhuQQIimwrP4EPP3Zb028 HEe+w4aF1urS26up0ShIEw10mifD/lLNpv8hzmgFgF/XTCthpFUSukP9oLi4djJTuWZb oRdG2sosTtbm4WgCsL6KuIF3+C9jhlQIOHepoLfI+Ag6E21G0OPbthInVDzjay7xY/w2 Q9pA== X-Gm-Message-State: ABuFfoiEAh3m+iK0raa+M6NRQRAxwVpObEmx2X+b5kugni3DXg9/INj3 g1IJj9opTtKGpu6Y5DnGpKE= X-Google-Smtp-Source: ACcGV637cZO6tSDzfHSBP0hLfG1G99uYx+xDm2Qqj9j4e9laeEu7i8lPyHxm+Bqc0y2GS/sGVgfUEA== X-Received: by 2002:a17:902:d208:: with SMTP id t8-v6mr16513115ply.22.1539595017039; Mon, 15 Oct 2018 02:16:57 -0700 (PDT) Received: from ala-d2121-lx1.wrs.com (unknown-156-139.windriver.com. [147.11.156.139]) by smtp.gmail.com with ESMTPSA id a11-v6sm10798256pgw.54.2018.10.15.02.16.55 (version=TLS1_1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 15 Oct 2018 02:16:56 -0700 (PDT) From: Bin Meng To: Simon Glass , U-Boot Mailing List Date: Mon, 15 Oct 2018 02:21:17 -0700 Message-Id: <1539595287-31378-22-git-send-email-bmeng.cn@gmail.com> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: <1539595287-31378-1-git-send-email-bmeng.cn@gmail.com> References: <1539595287-31378-1-git-send-email-bmeng.cn@gmail.com> Subject: [U-Boot] [PATCH v3 21/31] virtio: Add virtio over pci transport driver X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.18 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" This adds a transport driver that implements UCLASS_VIRTIO for virtio over pci, which is commonly used on x86. It only supports the legacy interface of the pci transport, which is the default device that QEMU emulates. Signed-off-by: Bin Meng Reviewed-by: Simon Glass Signed-off-by: Bin Meng Reviewed-by: Simon Glass --- Changes in v3: None Changes in v2: None drivers/virtio/Kconfig | 8 + drivers/virtio/Makefile | 1 + drivers/virtio/virtio_pci.c | 421 ++++++++++++++++++++++++++++++++++++++++++++ drivers/virtio/virtio_pci.h | 173 ++++++++++++++++++ 4 files changed, 603 insertions(+) create mode 100644 drivers/virtio/virtio_pci.c create mode 100644 drivers/virtio/virtio_pci.h diff --git a/drivers/virtio/Kconfig b/drivers/virtio/Kconfig index b72477a..753912f 100644 --- a/drivers/virtio/Kconfig +++ b/drivers/virtio/Kconfig @@ -29,6 +29,14 @@ config VIRTIO_MMIO This driver provides support for memory mapped virtio platform device driver. +config VIRTIO_PCI + bool "PCI driver for virtio devices" + depends on DM_PCI + select VIRTIO + help + This driver provides support for virtio based paravirtual device + drivers over PCI. + config VIRTIO_NET bool "virtio net driver" depends on VIRTIO diff --git a/drivers/virtio/Makefile b/drivers/virtio/Makefile index 5fe7428..5ee6183 100644 --- a/drivers/virtio/Makefile +++ b/drivers/virtio/Makefile @@ -5,5 +5,6 @@ obj-y += virtio-uclass.o virtio_ring.o obj-$(CONFIG_VIRTIO_MMIO) += virtio_mmio.o +obj-$(CONFIG_VIRTIO_PCI) += virtio_pci.o obj-$(CONFIG_VIRTIO_NET) += virtio_net.o obj-$(CONFIG_VIRTIO_BLK) += virtio_blk.o diff --git a/drivers/virtio/virtio_pci.c b/drivers/virtio/virtio_pci.c new file mode 100644 index 0000000..a3f1083 --- /dev/null +++ b/drivers/virtio/virtio_pci.c @@ -0,0 +1,421 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * Copyright (C) 2018, Bin Meng + * + * VirtIO PCI bus transport driver + * Ported from Linux drivers/virtio/virtio_pci*.c + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include "virtio_pci.h" + +#define VIRTIO_PCI_DRV_NAME "virtio-pci" + +/* PCI device ID in the range 0x1000 to 0x103f */ +#define VIRTIO_PCI_VENDOR_ID 0x1af4 +#define VIRTIO_PCI_DEVICE_ID00 0x1000 +#define VIRTIO_PCI_DEVICE_ID01 0x1001 +#define VIRTIO_PCI_DEVICE_ID02 0x1002 +#define VIRTIO_PCI_DEVICE_ID03 0x1003 +#define VIRTIO_PCI_DEVICE_ID04 0x1004 +#define VIRTIO_PCI_DEVICE_ID05 0x1005 +#define VIRTIO_PCI_DEVICE_ID06 0x1006 +#define VIRTIO_PCI_DEVICE_ID07 0x1007 +#define VIRTIO_PCI_DEVICE_ID08 0x1008 +#define VIRTIO_PCI_DEVICE_ID09 0x1009 +#define VIRTIO_PCI_DEVICE_ID0A 0x100a +#define VIRTIO_PCI_DEVICE_ID0B 0x100b +#define VIRTIO_PCI_DEVICE_ID0C 0x100c +#define VIRTIO_PCI_DEVICE_ID0D 0x100d +#define VIRTIO_PCI_DEVICE_ID0E 0x100e +#define VIRTIO_PCI_DEVICE_ID0F 0x100f +#define VIRTIO_PCI_DEVICE_ID10 0x1010 +#define VIRTIO_PCI_DEVICE_ID11 0x1011 +#define VIRTIO_PCI_DEVICE_ID12 0x1012 +#define VIRTIO_PCI_DEVICE_ID13 0x1013 +#define VIRTIO_PCI_DEVICE_ID14 0x1014 +#define VIRTIO_PCI_DEVICE_ID15 0x1015 +#define VIRTIO_PCI_DEVICE_ID16 0x1016 +#define VIRTIO_PCI_DEVICE_ID17 0x1017 +#define VIRTIO_PCI_DEVICE_ID18 0x1018 +#define VIRTIO_PCI_DEVICE_ID19 0x1019 +#define VIRTIO_PCI_DEVICE_ID1A 0x101a +#define VIRTIO_PCI_DEVICE_ID1B 0x101b +#define VIRTIO_PCI_DEVICE_ID1C 0x101c +#define VIRTIO_PCI_DEVICE_ID1D 0x101d +#define VIRTIO_PCI_DEVICE_ID1E 0x101e +#define VIRTIO_PCI_DEVICE_ID1F 0x101f +#define VIRTIO_PCI_DEVICE_ID20 0x1020 +#define VIRTIO_PCI_DEVICE_ID21 0x1021 +#define VIRTIO_PCI_DEVICE_ID22 0x1022 +#define VIRTIO_PCI_DEVICE_ID23 0x1023 +#define VIRTIO_PCI_DEVICE_ID24 0x1024 +#define VIRTIO_PCI_DEVICE_ID25 0x1025 +#define VIRTIO_PCI_DEVICE_ID26 0x1026 +#define VIRTIO_PCI_DEVICE_ID27 0x1027 +#define VIRTIO_PCI_DEVICE_ID28 0x1028 +#define VIRTIO_PCI_DEVICE_ID29 0x1029 +#define VIRTIO_PCI_DEVICE_ID2A 0x102a +#define VIRTIO_PCI_DEVICE_ID2B 0x102b +#define VIRTIO_PCI_DEVICE_ID2C 0x102c +#define VIRTIO_PCI_DEVICE_ID2D 0x102d +#define VIRTIO_PCI_DEVICE_ID2E 0x102e +#define VIRTIO_PCI_DEVICE_ID2F 0x102f +#define VIRTIO_PCI_DEVICE_ID30 0x1030 +#define VIRTIO_PCI_DEVICE_ID31 0x1031 +#define VIRTIO_PCI_DEVICE_ID32 0x1032 +#define VIRTIO_PCI_DEVICE_ID33 0x1033 +#define VIRTIO_PCI_DEVICE_ID34 0x1034 +#define VIRTIO_PCI_DEVICE_ID35 0x1035 +#define VIRTIO_PCI_DEVICE_ID36 0x1036 +#define VIRTIO_PCI_DEVICE_ID37 0x1037 +#define VIRTIO_PCI_DEVICE_ID38 0x1038 +#define VIRTIO_PCI_DEVICE_ID39 0x1039 +#define VIRTIO_PCI_DEVICE_ID3A 0x103a +#define VIRTIO_PCI_DEVICE_ID3B 0x103b +#define VIRTIO_PCI_DEVICE_ID3C 0x103c +#define VIRTIO_PCI_DEVICE_ID3D 0x103d +#define VIRTIO_PCI_DEVICE_ID3E 0x103e +#define VIRTIO_PCI_DEVICE_ID3F 0x103f + +/** + * virtio pci transport driver private data + * + * @ioaddr: pci transport device register base + * @version: pci transport device version + */ +struct virtio_pci_priv { + void __iomem *ioaddr; +}; + +static int virtio_pci_get_config(struct udevice *udev, unsigned int offset, + void *buf, unsigned int len) +{ + struct virtio_pci_priv *priv = dev_get_priv(udev); + void __iomem *ioaddr = priv->ioaddr + VIRTIO_PCI_CONFIG_OFF(false); + u8 *ptr = buf; + int i; + + for (i = 0; i < len; i++) + ptr[i] = ioread8(ioaddr + i); + + return 0; +} + +static int virtio_pci_set_config(struct udevice *udev, unsigned int offset, + const void *buf, unsigned int len) +{ + struct virtio_pci_priv *priv = dev_get_priv(udev); + void __iomem *ioaddr = priv->ioaddr + VIRTIO_PCI_CONFIG_OFF(false); + const u8 *ptr = buf; + int i; + + for (i = 0; i < len; i++) + iowrite8(ptr[i], ioaddr + i); + + return 0; +} + +static int virtio_pci_get_status(struct udevice *udev, u8 *status) +{ + struct virtio_pci_priv *priv = dev_get_priv(udev); + + *status = ioread8(priv->ioaddr + VIRTIO_PCI_STATUS); + + return 0; +} + +static int virtio_pci_set_status(struct udevice *udev, u8 status) +{ + struct virtio_pci_priv *priv = dev_get_priv(udev); + + /* We should never be setting status to 0 */ + WARN_ON(status == 0); + + iowrite8(status, priv->ioaddr + VIRTIO_PCI_STATUS); + + return 0; +} + +static int virtio_pci_reset(struct udevice *udev) +{ + struct virtio_pci_priv *priv = dev_get_priv(udev); + + /* 0 status means a reset */ + iowrite8(0, priv->ioaddr + VIRTIO_PCI_STATUS); + + /* + * Flush out the status write, and flush in device writes, + * including MSI-X interrupts, if any. + */ + ioread8(priv->ioaddr + VIRTIO_PCI_STATUS); + + return 0; +} + +static int virtio_pci_get_features(struct udevice *udev, u64 *features) +{ + struct virtio_pci_priv *priv = dev_get_priv(udev); + + /* + * When someone needs more than 32 feature bits, we'll need to + * steal a bit to indicate that the rest are somewhere else. + */ + *features = ioread32(priv->ioaddr + VIRTIO_PCI_HOST_FEATURES); + + return 0; +} + +static int virtio_pci_set_features(struct udevice *udev) +{ + struct virtio_pci_priv *priv = dev_get_priv(udev); + struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(udev); + + /* Make sure we don't have any features > 32 bits! */ + WARN_ON((u32)uc_priv->features != uc_priv->features); + + /* We only support 32 feature bits */ + iowrite32(uc_priv->features, priv->ioaddr + VIRTIO_PCI_GUEST_FEATURES); + + return 0; +} + +static struct virtqueue *virtio_pci_setup_vq(struct udevice *udev, + unsigned int index) +{ + struct virtio_pci_priv *priv = dev_get_priv(udev); + struct virtqueue *vq; + unsigned int num; + int err; + + /* Select the queue we're interested in */ + iowrite16(index, priv->ioaddr + VIRTIO_PCI_QUEUE_SEL); + + /* Check if queue is either not available or already active */ + num = ioread16(priv->ioaddr + VIRTIO_PCI_QUEUE_NUM); + if (!num || ioread32(priv->ioaddr + VIRTIO_PCI_QUEUE_PFN)) { + err = -ENOENT; + goto error_available; + } + + /* Create the vring */ + vq = vring_create_virtqueue(index, num, VIRTIO_PCI_VRING_ALIGN, udev); + if (!vq) { + err = -ENOMEM; + goto error_available; + } + + /* Activate the queue */ + iowrite32(virtqueue_get_desc_addr(vq) >> VIRTIO_PCI_QUEUE_ADDR_SHIFT, + priv->ioaddr + VIRTIO_PCI_QUEUE_PFN); + + return vq; + +error_available: + return ERR_PTR(err); +} + +static void virtio_pci_del_vq(struct virtqueue *vq) +{ + struct virtio_pci_priv *priv = dev_get_priv(vq->vdev); + unsigned int index = vq->index; + + iowrite16(index, priv->ioaddr + VIRTIO_PCI_QUEUE_SEL); + + /* Select and deactivate the queue */ + iowrite32(0, priv->ioaddr + VIRTIO_PCI_QUEUE_PFN); + + vring_del_virtqueue(vq); +} + +static int virtio_pci_del_vqs(struct udevice *udev) +{ + struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(udev); + struct virtqueue *vq, *n; + + list_for_each_entry_safe(vq, n, &uc_priv->vqs, list) + virtio_pci_del_vq(vq); + + return 0; +} + +static int virtio_pci_find_vqs(struct udevice *udev, unsigned int nvqs, + struct virtqueue *vqs[]) +{ + int i; + + for (i = 0; i < nvqs; ++i) { + vqs[i] = virtio_pci_setup_vq(udev, i); + if (IS_ERR(vqs[i])) { + virtio_pci_del_vqs(udev); + return PTR_ERR(vqs[i]); + } + } + + return 0; +} + +static int virtio_pci_notify(struct udevice *udev, struct virtqueue *vq) +{ + struct virtio_pci_priv *priv = dev_get_priv(udev); + + /* + * We write the queue's selector into the notification register + * to signal the other end + */ + iowrite16(vq->index, priv->ioaddr + VIRTIO_PCI_QUEUE_NOTIFY); + + return 0; +} + +static int virtio_pci_bind(struct udevice *udev) +{ + static int num_devs; + char name[20]; + + /* Create a unique device name for PCI type devices */ + sprintf(name, "%s#%u", VIRTIO_PCI_DRV_NAME, num_devs++); + device_set_name(udev, name); + + return 0; +} + +static int virtio_pci_probe(struct udevice *udev) +{ + struct pci_child_platdata *pplat = dev_get_parent_platdata(udev); + struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(udev); + struct virtio_pci_priv *priv = dev_get_priv(udev); + u16 subvendor, subdevice; + u8 revision; + + /* We only own devices >= 0x1000 and <= 0x103f: leave the rest. */ + if (pplat->device < 0x1000 || pplat->device > 0x103f) + return -ENODEV; + + /* Transitional devices must have a PCI revision ID of 0 */ + dm_pci_read_config8(udev, PCI_REVISION_ID, &revision); + if (revision != VIRTIO_PCI_ABI_VERSION) { + printf("(%s): virtio_pci expected ABI version %d, got %d\n", + udev->name, VIRTIO_PCI_ABI_VERSION, revision); + return -ENODEV; + } + + /* + * Transitional devices must have the PCI subsystem device ID matching + * the virtio device ID + */ + dm_pci_read_config16(udev, PCI_SUBSYSTEM_ID, &subdevice); + dm_pci_read_config16(udev, PCI_SUBSYSTEM_VENDOR_ID, &subvendor); + uc_priv->device = subdevice; + uc_priv->vendor = subvendor; + + priv->ioaddr = dm_pci_map_bar(udev, PCI_BASE_ADDRESS_0, PCI_REGION_IO); + if (!priv->ioaddr) + return -ENXIO; + debug("(%s): virtio legacy device reg base %04lx\n", + udev->name, (ulong)priv->ioaddr); + + debug("(%s): device (%d) vendor (%08x) version (%d)\n", udev->name, + uc_priv->device, uc_priv->vendor, revision); + + return 0; +} + +static const struct dm_virtio_ops virtio_pci_ops = { + .get_config = virtio_pci_get_config, + .set_config = virtio_pci_set_config, + .get_status = virtio_pci_get_status, + .set_status = virtio_pci_set_status, + .reset = virtio_pci_reset, + .get_features = virtio_pci_get_features, + .set_features = virtio_pci_set_features, + .find_vqs = virtio_pci_find_vqs, + .del_vqs = virtio_pci_del_vqs, + .notify = virtio_pci_notify, +}; + +U_BOOT_DRIVER(virtio_pci) = { + .name = VIRTIO_PCI_DRV_NAME, + .id = UCLASS_VIRTIO, + .ops = &virtio_pci_ops, + .bind = virtio_pci_bind, + .probe = virtio_pci_probe, + .priv_auto_alloc_size = sizeof(struct virtio_pci_priv), +}; + +static struct pci_device_id virtio_pci_supported[] = { + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID00) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID01) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID02) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID03) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID04) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID05) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID06) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID07) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID08) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID09) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID0A) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID0B) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID0C) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID0D) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID0E) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID0F) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID10) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID11) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID12) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID13) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID14) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID15) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID16) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID17) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID18) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID19) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID1A) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID1B) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID1C) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID1D) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID1E) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID1F) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID20) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID21) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID22) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID23) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID24) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID25) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID26) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID27) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID28) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID29) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID2A) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID2B) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID2C) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID2D) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID2E) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID2F) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID30) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID31) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID32) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID33) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID34) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID35) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID36) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID37) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID38) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID39) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID3A) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID3B) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID3C) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID3D) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID3E) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID3F) }, + {}, +}; + +U_BOOT_PCI_DEVICE(virtio_pci, virtio_pci_supported); diff --git a/drivers/virtio/virtio_pci.h b/drivers/virtio/virtio_pci.h new file mode 100644 index 0000000..cc753ed --- /dev/null +++ b/drivers/virtio/virtio_pci.h @@ -0,0 +1,173 @@ +/* SPDX-License-Identifier: BSD-3-Clause */ +/* + * Copyright (C) 2018, Bin Meng + * + * From Linux kernel include/uapi/linux/virtio_pci.h + */ + +#ifndef _LINUX_VIRTIO_PCI_H +#define _LINUX_VIRTIO_PCI_H + +#ifndef VIRTIO_PCI_NO_LEGACY + +/* A 32-bit r/o bitmask of the features supported by the host */ +#define VIRTIO_PCI_HOST_FEATURES 0 + +/* A 32-bit r/w bitmask of features activated by the guest */ +#define VIRTIO_PCI_GUEST_FEATURES 4 + +/* A 32-bit r/w PFN for the currently selected queue */ +#define VIRTIO_PCI_QUEUE_PFN 8 + +/* A 16-bit r/o queue size for the currently selected queue */ +#define VIRTIO_PCI_QUEUE_NUM 12 + +/* A 16-bit r/w queue selector */ +#define VIRTIO_PCI_QUEUE_SEL 14 + +/* A 16-bit r/w queue notifier */ +#define VIRTIO_PCI_QUEUE_NOTIFY 16 + +/* An 8-bit device status register */ +#define VIRTIO_PCI_STATUS 18 + +/* + * An 8-bit r/o interrupt status register. Reading the value will return the + * current contents of the ISR and will also clear it. This is effectively + * a read-and-acknowledge. + */ +#define VIRTIO_PCI_ISR 19 + +/* MSI-X registers: only enabled if MSI-X is enabled */ + +/* A 16-bit vector for configuration changes */ +#define VIRTIO_MSI_CONFIG_VECTOR 20 +/* A 16-bit vector for selected queue notifications */ +#define VIRTIO_MSI_QUEUE_VECTOR 22 + +/* + * The remaining space is defined by each driver as the per-driver + * configuration space + */ +#define VIRTIO_PCI_CONFIG_OFF(msix) ((msix) ? 24 : 20) + +/* Virtio ABI version, this must match exactly */ +#define VIRTIO_PCI_ABI_VERSION 0 + +/* + * How many bits to shift physical queue address written to QUEUE_PFN. + * 12 is historical, and due to x86 page size. + */ +#define VIRTIO_PCI_QUEUE_ADDR_SHIFT 12 + +/* + * The alignment to use between consumer and producer parts of vring. + * x86 pagesize again. + */ +#define VIRTIO_PCI_VRING_ALIGN 4096 + +#endif /* VIRTIO_PCI_NO_LEGACY */ + +/* The bit of the ISR which indicates a device configuration change */ +#define VIRTIO_PCI_ISR_CONFIG 0x2 +/* Vector value used to disable MSI for queue */ +#define VIRTIO_MSI_NO_VECTOR 0xffff + +#ifndef VIRTIO_PCI_NO_MODERN + +/* IDs for different capabilities. Must all exist. */ + +/* Common configuration */ +#define VIRTIO_PCI_CAP_COMMON_CFG 1 +/* Notifications */ +#define VIRTIO_PCI_CAP_NOTIFY_CFG 2 +/* ISR access */ +#define VIRTIO_PCI_CAP_ISR_CFG 3 +/* Device specific configuration */ +#define VIRTIO_PCI_CAP_DEVICE_CFG 4 +/* PCI configuration access */ +#define VIRTIO_PCI_CAP_PCI_CFG 5 + +/* This is the PCI capability header: */ +struct virtio_pci_cap { + __u8 cap_vndr; /* Generic PCI field: PCI_CAP_ID_VNDR */ + __u8 cap_next; /* Generic PCI field: next ptr */ + __u8 cap_len; /* Generic PCI field: capability length */ + __u8 cfg_type; /* Identifies the structure */ + __u8 bar; /* Where to find it */ + __u8 padding[3]; /* Pad to full dword */ + __le32 offset; /* Offset within bar */ + __le32 length; /* Length of the structure, in bytes */ +}; + +struct virtio_pci_notify_cap { + struct virtio_pci_cap cap; + __le32 notify_off_multiplier; /* Multiplier for queue_notify_off */ +}; + +/* Fields in VIRTIO_PCI_CAP_COMMON_CFG: */ +struct virtio_pci_common_cfg { + /* About the whole device */ + __le32 device_feature_select; /* read-write */ + __le32 device_feature; /* read-only */ + __le32 guest_feature_select; /* read-write */ + __le32 guest_feature; /* read-write */ + __le16 msix_config; /* read-write */ + __le16 num_queues; /* read-only */ + __u8 device_status; /* read-write */ + __u8 config_generation; /* read-only */ + + /* About a specific virtqueue */ + __le16 queue_select; /* read-write */ + __le16 queue_size; /* read-write, power of 2 */ + __le16 queue_msix_vector; /* read-write */ + __le16 queue_enable; /* read-write */ + __le16 queue_notify_off; /* read-only */ + __le32 queue_desc_lo; /* read-write */ + __le32 queue_desc_hi; /* read-write */ + __le32 queue_avail_lo; /* read-write */ + __le32 queue_avail_hi; /* read-write */ + __le32 queue_used_lo; /* read-write */ + __le32 queue_used_hi; /* read-write */ +}; + +/* Fields in VIRTIO_PCI_CAP_PCI_CFG: */ +struct virtio_pci_cfg_cap { + struct virtio_pci_cap cap; + __u8 pci_cfg_data[4]; /* Data for BAR access */ +}; + +/* Macro versions of offsets for the Old Timers! */ +#define VIRTIO_PCI_CAP_VNDR 0 +#define VIRTIO_PCI_CAP_NEXT 1 +#define VIRTIO_PCI_CAP_LEN 2 +#define VIRTIO_PCI_CAP_CFG_TYPE 3 +#define VIRTIO_PCI_CAP_BAR 4 +#define VIRTIO_PCI_CAP_OFFSET 8 +#define VIRTIO_PCI_CAP_LENGTH 12 + +#define VIRTIO_PCI_NOTIFY_CAP_MULT 16 + +#define VIRTIO_PCI_COMMON_DFSELECT 0 +#define VIRTIO_PCI_COMMON_DF 4 +#define VIRTIO_PCI_COMMON_GFSELECT 8 +#define VIRTIO_PCI_COMMON_GF 12 +#define VIRTIO_PCI_COMMON_MSIX 16 +#define VIRTIO_PCI_COMMON_NUMQ 18 +#define VIRTIO_PCI_COMMON_STATUS 20 +#define VIRTIO_PCI_COMMON_CFGGENERATION 21 +#define VIRTIO_PCI_COMMON_Q_SELECT 22 +#define VIRTIO_PCI_COMMON_Q_SIZE 24 +#define VIRTIO_PCI_COMMON_Q_MSIX 26 +#define VIRTIO_PCI_COMMON_Q_ENABLE 28 +#define VIRTIO_PCI_COMMON_Q_NOFF 30 +#define VIRTIO_PCI_COMMON_Q_DESCLO 32 +#define VIRTIO_PCI_COMMON_Q_DESCHI 36 +#define VIRTIO_PCI_COMMON_Q_AVAILLO 40 +#define VIRTIO_PCI_COMMON_Q_AVAILHI 44 +#define VIRTIO_PCI_COMMON_Q_USEDLO 48 +#define VIRTIO_PCI_COMMON_Q_USEDHI 52 + +#endif /* VIRTIO_PCI_NO_MODERN */ + +#endif /* _LINUX_VIRTIO_PCI_H */ From patchwork Mon Oct 15 09:21:18 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bin Meng X-Patchwork-Id: 984021 X-Patchwork-Delegate: sjg@chromium.org Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=lists.denx.de (client-ip=81.169.180.215; helo=lists.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="G7jYowPS"; dkim-atps=neutral Received: from lists.denx.de (dione.denx.de [81.169.180.215]) by ozlabs.org (Postfix) with ESMTP id 42YY5m09Ncz9s5c for ; Mon, 15 Oct 2018 20:28:27 +1100 (AEDT) Received: by lists.denx.de (Postfix, from userid 105) id 63918C21E18; Mon, 15 Oct 2018 09:26:28 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on lists.denx.de X-Spam-Level: X-Spam-Status: No, score=-0.0 required=5.0 tests=FREEMAIL_FROM, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, T_DKIM_INVALID autolearn=unavailable autolearn_force=no version=3.4.0 Received: from lists.denx.de (localhost [IPv6:::1]) by lists.denx.de (Postfix) with ESMTP id BDF56C21F1B; Mon, 15 Oct 2018 09:17:18 +0000 (UTC) Received: by lists.denx.de (Postfix, from userid 105) id F1A81C21EEB; Mon, 15 Oct 2018 09:17:07 +0000 (UTC) Received: from mail-pl1-f195.google.com (mail-pl1-f195.google.com [209.85.214.195]) by lists.denx.de (Postfix) with ESMTPS id 7BDDFC21E75 for ; Mon, 15 Oct 2018 09:16:59 +0000 (UTC) Received: by mail-pl1-f195.google.com with SMTP id f8-v6so9011742plb.2 for ; Mon, 15 Oct 2018 02:16:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=UzC+EV2jLQpAjxpD4VdvY5XfThv1hxKUrN2OacAdldw=; b=G7jYowPSMjKNreFjIkoij1EizPuA35LEfHfOo0VWhs3zS6ACeGb11XFcl5EuAWeR+S YWmz3d36IIElDeajWfy/RkR6aWoxS2xxAw7bTDGARf3flbIjTcY9O1VEgfKDo74/l/4z ibrcBdfIIdpimgow8B6WdL6ryDWTWlgwJlZqwUl4wlkG0I8MLsV33LUYqCO8c4svg4RG VFgOWVBHpLvcKGYIy8thgfTgnqv6MJZDZdWQzRz367cDh4EE9TqYcI3fvJowmuYgYkGd nNpUe3ADS2lRbEW1Cq3QIkfAzMiSAunIf28niCirHau5DEkeJ5+8DISIDoHcQ3CC7qlz eO3g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=UzC+EV2jLQpAjxpD4VdvY5XfThv1hxKUrN2OacAdldw=; b=msUFNFLQvivrtaAP1k8IbYvGskTbGBGTx339bkQqPJgabNybxsUJFyFUhnaM6STgfP zMfd54s+7lYSSb38uRdREp1/E+QruwL7pJppcrU1y8vj2nkS4JTPMUyD9VW3IGgrqZ1I m2S1fVHjsAMqXCuvUNodC/BGH01dRzeMdWIk9u8NqLjvxoqt8NbLf8jmmm6RJ6OHpvW3 jcimFMSNC960zY0/2QEKsW+iEzj7Z0ZY2T3CVSLSj1uEEy+y54ZCMM9LqgqIFTnAkyw8 Qctm3RWJdQiqIbDbBcwQYUDT4qBTrXdIRvThbXCWr1WMmqWgNVQfSgFb/KN7JWuy5Zw7 U0Lg== X-Gm-Message-State: ABuFfoimn+TWSIJgZo5AeyZUYcXzAMMI1UVU/JVsyjFYMRelvAaTzWrc AvQTojtcO4o3UfSOMN/obNNUIVay X-Google-Smtp-Source: ACcGV60bUy1FtEUylNAcf48RTlFt6F0Ntqf00uUJn85aUQqhjty8laMv04M5yK2Sa5mGgtdZl+Chrg== X-Received: by 2002:a17:902:d694:: with SMTP id v20-v6mr16177642ply.261.1539595018216; Mon, 15 Oct 2018 02:16:58 -0700 (PDT) Received: from ala-d2121-lx1.wrs.com (unknown-156-139.windriver.com. [147.11.156.139]) by smtp.gmail.com with ESMTPSA id a11-v6sm10798256pgw.54.2018.10.15.02.16.57 (version=TLS1_1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 15 Oct 2018 02:16:57 -0700 (PDT) From: Bin Meng To: Simon Glass , U-Boot Mailing List Date: Mon, 15 Oct 2018 02:21:18 -0700 Message-Id: <1539595287-31378-23-git-send-email-bmeng.cn@gmail.com> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: <1539595287-31378-1-git-send-email-bmeng.cn@gmail.com> References: <1539595287-31378-1-git-send-email-bmeng.cn@gmail.com> Subject: [U-Boot] [PATCH v3 22/31] arm: qemu: Add a Kconfig in the board directory X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.18 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" This adds a Kconfig file in the board directory, so that some board-specific options can be specified there. Signed-off-by: Bin Meng Reviewed-by: Simon Glass Signed-off-by: Bin Meng Reviewed-by: Simon Glass --- Changes in v3: None Changes in v2: None arch/arm/Kconfig | 1 + board/emulation/qemu-arm/Kconfig | 9 +++++++++ configs/qemu_arm64_defconfig | 1 - configs/qemu_arm_defconfig | 1 - 4 files changed, 10 insertions(+), 2 deletions(-) create mode 100644 board/emulation/qemu-arm/Kconfig diff --git a/arch/arm/Kconfig b/arch/arm/Kconfig index ccf2a84..a3e6784 100644 --- a/arch/arm/Kconfig +++ b/arch/arm/Kconfig @@ -1474,6 +1474,7 @@ source "board/broadcom/bcmns2/Kconfig" source "board/cavium/thunderx/Kconfig" source "board/cirrus/edb93xx/Kconfig" source "board/eets/pdu001/Kconfig" +source "board/emulation/qemu-arm/Kconfig" source "board/freescale/ls2080a/Kconfig" source "board/freescale/ls2080aqds/Kconfig" source "board/freescale/ls2080ardb/Kconfig" diff --git a/board/emulation/qemu-arm/Kconfig b/board/emulation/qemu-arm/Kconfig new file mode 100644 index 0000000..d1c08c2 --- /dev/null +++ b/board/emulation/qemu-arm/Kconfig @@ -0,0 +1,9 @@ +if TARGET_QEMU_ARM_32BIT || TARGET_QEMU_ARM_64BIT + +config SYS_TEXT_BASE + default 0x00000000 + +config BOARD_SPECIFIC_OPTIONS # dummy + def_bool y + +endif diff --git a/configs/qemu_arm64_defconfig b/configs/qemu_arm64_defconfig index 7fd726f..bdb1c12 100644 --- a/configs/qemu_arm64_defconfig +++ b/configs/qemu_arm64_defconfig @@ -1,7 +1,6 @@ CONFIG_ARM=y CONFIG_ARM_SMCCC=y CONFIG_ARCH_QEMU=y -CONFIG_SYS_TEXT_BASE=0x00000000 CONFIG_TARGET_QEMU_ARM_64BIT=y CONFIG_AHCI=y CONFIG_DISTRO_DEFAULTS=y diff --git a/configs/qemu_arm_defconfig b/configs/qemu_arm_defconfig index fbceaf3..10959b2 100644 --- a/configs/qemu_arm_defconfig +++ b/configs/qemu_arm_defconfig @@ -1,7 +1,6 @@ CONFIG_ARM=y CONFIG_ARM_SMCCC=y CONFIG_ARCH_QEMU=y -CONFIG_SYS_TEXT_BASE=0x00000000 CONFIG_TARGET_QEMU_ARM_32BIT=y CONFIG_AHCI=y CONFIG_DISTRO_DEFAULTS=y From patchwork Mon Oct 15 09:21:19 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bin Meng X-Patchwork-Id: 984026 X-Patchwork-Delegate: sjg@chromium.org Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=lists.denx.de (client-ip=81.169.180.215; helo=lists.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="Mfr8KQzI"; dkim-atps=neutral Received: from lists.denx.de (dione.denx.de [81.169.180.215]) by ozlabs.org (Postfix) with ESMTP id 42YY8d1BPMz9sCw for ; Mon, 15 Oct 2018 20:30:57 +1100 (AEDT) Received: by lists.denx.de (Postfix, from userid 105) id D8B4EC21E44; Mon, 15 Oct 2018 09:28:06 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on lists.denx.de X-Spam-Level: X-Spam-Status: No, score=-0.0 required=5.0 tests=FREEMAIL_FROM, RCVD_IN_DNSWL_BLOCKED, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, T_DKIM_INVALID autolearn=unavailable autolearn_force=no version=3.4.0 Received: from lists.denx.de (localhost [IPv6:::1]) by lists.denx.de (Postfix) with ESMTP id 20856C21F32; Mon, 15 Oct 2018 09:17:22 +0000 (UTC) Received: by lists.denx.de (Postfix, from userid 105) id 019E5C21E8A; Mon, 15 Oct 2018 09:17:09 +0000 (UTC) Received: from mail-pl1-f196.google.com (mail-pl1-f196.google.com [209.85.214.196]) by lists.denx.de (Postfix) with ESMTPS id C069DC21E08 for ; Mon, 15 Oct 2018 09:17:00 +0000 (UTC) Received: by mail-pl1-f196.google.com with SMTP id q19-v6so5919563pll.5 for ; Mon, 15 Oct 2018 02:17:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=mN/UkQDpiNfXYWzwSkzNSYaqS7k7BCHFR5oFepiMKok=; b=Mfr8KQzIab4hWpCah2YuGttDVCk+JuRGhrg8RhbaP/xPMdrr0b6GIPLXxl/IkBxVwI okWBzE0RoHkLLEo2DdX3vKGiZFcWp5Gi1EXtSj+M1W/fNmZtj7oitPawpLudsmzSFT99 EvtIRxjavKfSFzFl3n7VesDTmrq+sXGO19Tq7XKNPaPm3kYFo4XneTzIqfEIzlMzsSfx VHqqTOKe2n0HZvDKRi2nPJPiJn2AkS2Ux+JtuIC64F0t2lq0KRos/3FhS5IKZ0N5m0M/ L+EvZvLtyMAzpI/4HYshNjNc8eTdwFZtnsoNpQVWEmt34H7wAz62RL5ABXWrORRyhJY2 JolQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=mN/UkQDpiNfXYWzwSkzNSYaqS7k7BCHFR5oFepiMKok=; b=NIzIuW12G89ZLCU9ZlvOCQwvmKm1Awj+YDjelu1ECyO2KgWKaIxXp9Vg2/3qzbui3h EOS8zzGGfjxa+hHFpztmxfZinLEF552jtS0PmK6rle7mcKh7BrsOC3TBHf5+cT2D75xE DbanzG6XAeHTzVVJHv/K0XH7yD42oW65xDywep2b8kOtdSgQEA3JltC9YThp1CAbjR22 Zu1pciII+ESMy2+EIwgeKhK3+y5yO1PEbvST0fphmdRKGuzNzAoqpH07ecn+TTOdwz99 OtDh3YBI8j4t4vEGjzyaj6yZQ1gE9KL/64I8qhyNbN3ed5XojfLv59J0ZfMsXTC8f4Ls RNhA== X-Gm-Message-State: ABuFfojlcMgQzldo+Hgbjg2T3AltJ5inc4ISQWLUDhuyFXCaYTiGVVQW 5cWudkKEXRR+TIT1F5nRYSw= X-Google-Smtp-Source: ACcGV63kTcCUtsX3+cfmDtOdBopv9P7iNtflzB+ZQdZG9sZnKkXkIhNVVxzbrR26d0KywhEgAeeCIw== X-Received: by 2002:a17:902:187:: with SMTP id b7-v6mr16336724plb.150.1539595019489; Mon, 15 Oct 2018 02:16:59 -0700 (PDT) Received: from ala-d2121-lx1.wrs.com (unknown-156-139.windriver.com. [147.11.156.139]) by smtp.gmail.com with ESMTPSA id a11-v6sm10798256pgw.54.2018.10.15.02.16.58 (version=TLS1_1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 15 Oct 2018 02:16:58 -0700 (PDT) From: Bin Meng To: Simon Glass , U-Boot Mailing List Date: Mon, 15 Oct 2018 02:21:19 -0700 Message-Id: <1539595287-31378-24-git-send-email-bmeng.cn@gmail.com> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: <1539595287-31378-1-git-send-email-bmeng.cn@gmail.com> References: <1539595287-31378-1-git-send-email-bmeng.cn@gmail.com> Subject: [U-Boot] [PATCH v3 23/31] arm: qemu: Enumerate virtio bus during early boot X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.18 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" Currently devices on the virtio bus is not automatically enumerated, which means peripherals on the virtio bus are not discovered by their drivers. This uses board_init() to do the virtio enumeration. Signed-off-by: Bin Meng Reviewed-by: Simon Glass Signed-off-by: Bin Meng Reviewed-by: Simon Glass --- Changes in v3: None Changes in v2: - imply VIRTIO_PCI for qemu-arm too board/emulation/qemu-arm/Kconfig | 4 ++++ board/emulation/qemu-arm/qemu-arm.c | 10 ++++++++++ 2 files changed, 14 insertions(+) diff --git a/board/emulation/qemu-arm/Kconfig b/board/emulation/qemu-arm/Kconfig index d1c08c2..02ae4d9 100644 --- a/board/emulation/qemu-arm/Kconfig +++ b/board/emulation/qemu-arm/Kconfig @@ -5,5 +5,9 @@ config SYS_TEXT_BASE config BOARD_SPECIFIC_OPTIONS # dummy def_bool y + imply VIRTIO_MMIO + imply VIRTIO_PCI + imply VIRTIO_NET + imply VIRTIO_BLK endif diff --git a/board/emulation/qemu-arm/qemu-arm.c b/board/emulation/qemu-arm/qemu-arm.c index 812c906..e1f4709 100644 --- a/board/emulation/qemu-arm/qemu-arm.c +++ b/board/emulation/qemu-arm/qemu-arm.c @@ -2,8 +2,12 @@ /* * Copyright (c) 2017 Tuomas Tynkkynen */ + #include +#include #include +#include +#include #ifdef CONFIG_ARM64 #include @@ -58,6 +62,12 @@ struct mm_region *mem_map = qemu_arm64_mem_map; int board_init(void) { + /* + * Make sure virtio bus is enumerated so that peripherals + * on the virtio bus can be discovered by their drivers + */ + virtio_init(); + return 0; } From patchwork Mon Oct 15 09:21:20 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bin Meng X-Patchwork-Id: 984044 X-Patchwork-Delegate: sjg@chromium.org Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=lists.denx.de (client-ip=81.169.180.215; helo=lists.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="ZQyPczFa"; dkim-atps=neutral Received: from lists.denx.de (dione.denx.de [81.169.180.215]) by ozlabs.org (Postfix) with ESMTP id 42YYGY6ktvz9s5c for ; Mon, 15 Oct 2018 20:36:05 +1100 (AEDT) Received: by lists.denx.de (Postfix, from userid 105) id 97C5CC21DED; Mon, 15 Oct 2018 09:26:45 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on lists.denx.de X-Spam-Level: X-Spam-Status: No, score=-0.0 required=5.0 tests=FREEMAIL_FROM, RCVD_IN_DNSWL_BLOCKED, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, T_DKIM_INVALID autolearn=unavailable autolearn_force=no version=3.4.0 Received: from lists.denx.de (localhost [IPv6:::1]) by lists.denx.de (Postfix) with ESMTP id A8C84C21F20; Mon, 15 Oct 2018 09:17:19 +0000 (UTC) Received: by lists.denx.de (Postfix, from userid 105) id 2D682C21ECF; Mon, 15 Oct 2018 09:17:10 +0000 (UTC) Received: from mail-pg1-f196.google.com (mail-pg1-f196.google.com [209.85.215.196]) by lists.denx.de (Postfix) with ESMTPS id 0D89AC21E7D for ; Mon, 15 Oct 2018 09:17:02 +0000 (UTC) Received: by mail-pg1-f196.google.com with SMTP id f18-v6so8884130pgv.3 for ; Mon, 15 Oct 2018 02:17:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=FMqQNjAkfuAxmHk86edF/8pnPDO6YXUdsWfz4YDuN4A=; b=ZQyPczFauI6vlZ7VG4sZRq5VMKYaf4tZALU2497+mgKkaTCio0iQVpjnEZioS4iCzK KPIa1eT7ZtipytuSz+9jeF6l5sbeNmgplDjKv2oj3IC2Td03Q1ameyrqGbZHsHQ32oQe H5qLS+BGYotEgDt5ifCO3AvTzWV6gjWXCK0ZSs+DT3pdDCzZ1/2QXMif7V6s9Jb+dEYc wKit715KaOBqCHtX+Fssp+w+iOeKFOXj5o4GXjuqJ9A3sln5b3/i7clQ2yEEVmdkfll2 47vIoBlqptb+SNdkNxOWpJGQzzGEUacw5ELTjH9wB/Hs8FBkBhGp6+1kBDcAAf90F+vt nsng== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=FMqQNjAkfuAxmHk86edF/8pnPDO6YXUdsWfz4YDuN4A=; b=jrc8y3U2SMRenjsbm1i65e7Qtkfa6eT8zZ991oVDvTCyemqLqm1VSw2WJTE49wMhrB xQG4ZOB3NEURqGr4mgCP57SUapcDabp0WzBUlyPEsiZwFcRLi0vZIOiM9NYvgX88nIcM LqXCb7JqX9/uwBXSQ67z1J/CIJMQkqLZkmceMOKFDUW6OmAAu8lAMJ0iUnhYiXZ0S386 yhgyq0zFSx+KbG1F4CiF5ttjiKsMVCCQymYcnRy1KhDHrMZ1SK95y8HsxrcnMnxneeJg rMd6J/fIgoHnj4FAgG5jgipPWs4j/1sqxUJYCJBVcmpjXGYKTwS+qXiW14rcUDeKogg4 s9fg== X-Gm-Message-State: ABuFfohzuo0hwzx0Ap3Un2xBKWrYasAgDlEuBEE0tqkqvX2NdJdOAyZW cNxI6JGdJdJwEZw8uGUpSMI= X-Google-Smtp-Source: ACcGV60jdl/oJB4D/w0+KrcGco2LLxLBsmVAZPmYw5ymSwU3RyPgI9Tb1MSiBDfrFKNCXzAQHcjhvw== X-Received: by 2002:a63:88c1:: with SMTP id l184-v6mr15021340pgd.431.1539595020690; Mon, 15 Oct 2018 02:17:00 -0700 (PDT) Received: from ala-d2121-lx1.wrs.com (unknown-156-139.windriver.com. [147.11.156.139]) by smtp.gmail.com with ESMTPSA id a11-v6sm10798256pgw.54.2018.10.15.02.16.59 (version=TLS1_1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 15 Oct 2018 02:16:59 -0700 (PDT) From: Bin Meng To: Simon Glass , U-Boot Mailing List Date: Mon, 15 Oct 2018 02:21:20 -0700 Message-Id: <1539595287-31378-25-git-send-email-bmeng.cn@gmail.com> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: <1539595287-31378-1-git-send-email-bmeng.cn@gmail.com> References: <1539595287-31378-1-git-send-email-bmeng.cn@gmail.com> Subject: [U-Boot] [PATCH v3 24/31] x86: qemu: Imply virtio PCI transport and device drivers X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.18 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" Add virtio drivers for QEMU x86 targets. Signed-off-by: Bin Meng Reviewed-by: Simon Glass Signed-off-by: Bin Meng Reviewed-by: Simon Glass --- Changes in v3: None Changes in v2: None board/emulation/qemu-x86/Kconfig | 3 +++ 1 file changed, 3 insertions(+) diff --git a/board/emulation/qemu-x86/Kconfig b/board/emulation/qemu-x86/Kconfig index 41a27dd..6d19299 100644 --- a/board/emulation/qemu-x86/Kconfig +++ b/board/emulation/qemu-x86/Kconfig @@ -21,5 +21,8 @@ config BOARD_SPECIFIC_OPTIONS # dummy select X86_RESET_VECTOR select QEMU select BOARD_ROMSIZE_KB_1024 + imply VIRTIO_PCI + imply VIRTIO_NET + imply VIRTIO_BLK endif From patchwork Mon Oct 15 09:21:21 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bin Meng X-Patchwork-Id: 984048 X-Patchwork-Delegate: sjg@chromium.org Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=lists.denx.de (client-ip=81.169.180.215; helo=lists.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="ZbxKWfyI"; dkim-atps=neutral Received: from lists.denx.de (dione.denx.de [81.169.180.215]) by ozlabs.org (Postfix) with ESMTP id 42YYHJ1KZzz9s5c for ; Mon, 15 Oct 2018 20:36:44 +1100 (AEDT) Received: by lists.denx.de (Postfix, from userid 105) id DB008C21E53; Mon, 15 Oct 2018 09:27:46 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on lists.denx.de X-Spam-Level: X-Spam-Status: No, score=-0.0 required=5.0 tests=FREEMAIL_FROM, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, T_DKIM_INVALID autolearn=unavailable autolearn_force=no version=3.4.0 Received: from lists.denx.de (localhost [IPv6:::1]) by lists.denx.de (Postfix) with ESMTP id 38498C21F21; Mon, 15 Oct 2018 09:17:21 +0000 (UTC) Received: by lists.denx.de (Postfix, from userid 105) id 90F9AC21F13; Mon, 15 Oct 2018 09:17:10 +0000 (UTC) Received: from mail-pl1-f195.google.com (mail-pl1-f195.google.com [209.85.214.195]) by lists.denx.de (Postfix) with ESMTPS id 3C058C21EA6 for ; Mon, 15 Oct 2018 09:17:03 +0000 (UTC) Received: by mail-pl1-f195.google.com with SMTP id v5-v6so8984771plz.13 for ; Mon, 15 Oct 2018 02:17:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=n0zdNnNOmLwL6iISpK1XdElYBMAMLo17zbXz0g6uCFM=; b=ZbxKWfyI7tvr/ey/NgFvJbmlMeZE3OMd+KxTmTg7UNhMB6sJDqCN4VCXhNMyu33CQS x+deOYaA09oE+KgNd06K1dGzFqbU6xOffmbDxVmrqpyy8o7IihqrQmsCZKkinMfwHRs8 B1RKkpLzMI27k/FAIvkScUwd38jjXrUHbs1L2ZPsqSTXQdetsqBcte8CxMJFQj9vF44Q 8d5131OthTwlTgQjbTrbdIvg6PIzNxcUrx7QTORN+OGV0d9WhPhMKOI2zncRTF9nYeuw 5nXGjdlKqCGpJs+LQGdZEoEKjdAAPa8TtBu1WsIUc6QEEkEJ1igckuMkg/9kgOGDBZUL FYJA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=n0zdNnNOmLwL6iISpK1XdElYBMAMLo17zbXz0g6uCFM=; b=c/W5WXwo0Cv6LPR9kD+hB1WjaehigXE+mT6C76ifrYt6nsKpqmHrvGtVVDSF7WuAX4 EQad6XjD8RmEaBFsODArrs+akeutaaS6oKSrKqWg2/nukZVs5astjAkSuzSOX2BdzYDC L/SAIc2kTknZ6xXgi+sK2u+b9yLtw3rScIJdNaKVeAYgkEDJsgKtj93kYU4pDUbHkvfY 57X7tlD9p1yxJsyYmzWtqFJ6b5JlYbR5M29XnN1QFO2yk0Y2yOQFH4Pg1ni2OE1f9p4b M1439B7O8qVwkO8DBgHVOY4jVA7cfr74I/f0ImJyuENTnVSQbsC4rrv72NTx8BSEEOHJ qCyQ== X-Gm-Message-State: ABuFfoj/642H9+NYKHVVVpeVeJ8yjI252k2NaaMBKmef0WAYuujBIMzD Ig6Ycm3fSamAUC6BRur646weR4/E X-Google-Smtp-Source: ACcGV61a7vgiockY7kexfHwgeziLu1D2kbjnwrAeHsl4RNvJ08YqSHlAQUwhImqR6FuqQYISPGtsGQ== X-Received: by 2002:a17:902:7486:: with SMTP id h6-v6mr15813680pll.29.1539595021830; Mon, 15 Oct 2018 02:17:01 -0700 (PDT) Received: from ala-d2121-lx1.wrs.com (unknown-156-139.windriver.com. [147.11.156.139]) by smtp.gmail.com with ESMTPSA id a11-v6sm10798256pgw.54.2018.10.15.02.17.00 (version=TLS1_1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 15 Oct 2018 02:17:01 -0700 (PDT) From: Bin Meng To: Simon Glass , U-Boot Mailing List Date: Mon, 15 Oct 2018 02:21:21 -0700 Message-Id: <1539595287-31378-26-git-send-email-bmeng.cn@gmail.com> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: <1539595287-31378-1-git-send-email-bmeng.cn@gmail.com> References: <1539595287-31378-1-git-send-email-bmeng.cn@gmail.com> Subject: [U-Boot] [PATCH v3 25/31] dm: pci: Add APIs to find next capability and extended capability X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.18 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" This introduces two new APIs dm_pci_find_next_capability() and dm_pci_find_next_ext_capability() to get PCI capability address and PCI express extended capability address for a given PCI device starting from a given offset. Signed-off-by: Bin Meng Reviewed-by: Simon Glass Signed-off-by: Bin Meng Reviewed-by: Simon Glass --- Changes in v3: None Changes in v2: - use an internal function _dm_pci_find_next_capability() for dm_pci_find_capability() and dm_pci_find_next_capability() drivers/pci/pci-uclass.c | 51 ++++++++++++++++++++++++++++++++++-------------- include/pci.h | 48 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+), 15 deletions(-) diff --git a/drivers/pci/pci-uclass.c b/drivers/pci/pci-uclass.c index eb118f3..0455c72 100644 --- a/drivers/pci/pci-uclass.c +++ b/drivers/pci/pci-uclass.c @@ -1318,26 +1318,14 @@ void *dm_pci_map_bar(struct udevice *dev, int bar, int flags) return dm_pci_bus_to_virt(dev, pci_bus_addr, flags, 0, MAP_NOCACHE); } -int dm_pci_find_capability(struct udevice *dev, int cap) +static int _dm_pci_find_next_capability(struct udevice *dev, u8 pos, int cap) { - u16 status; - u8 header_type; int ttl = PCI_FIND_CAP_TTL; u8 id; u16 ent; - u8 pos; - - dm_pci_read_config16(dev, PCI_STATUS, &status); - if (!(status & PCI_STATUS_CAP_LIST)) - return 0; - - dm_pci_read_config8(dev, PCI_HEADER_TYPE, &header_type); - if ((header_type & 0x7f) == PCI_HEADER_TYPE_CARDBUS) - pos = PCI_CB_CAPABILITY_LIST; - else - pos = PCI_CAPABILITY_LIST; dm_pci_read_config8(dev, pos, &pos); + while (ttl--) { if (pos < PCI_STD_HEADER_SIZEOF) break; @@ -1355,7 +1343,32 @@ int dm_pci_find_capability(struct udevice *dev, int cap) return 0; } -int dm_pci_find_ext_capability(struct udevice *dev, int cap) +int dm_pci_find_next_capability(struct udevice *dev, u8 start, int cap) +{ + return _dm_pci_find_next_capability(dev, start + PCI_CAP_LIST_NEXT, + cap); +} + +int dm_pci_find_capability(struct udevice *dev, int cap) +{ + u16 status; + u8 header_type; + u8 pos; + + dm_pci_read_config16(dev, PCI_STATUS, &status); + if (!(status & PCI_STATUS_CAP_LIST)) + return 0; + + dm_pci_read_config8(dev, PCI_HEADER_TYPE, &header_type); + if ((header_type & 0x7f) == PCI_HEADER_TYPE_CARDBUS) + pos = PCI_CB_CAPABILITY_LIST; + else + pos = PCI_CAPABILITY_LIST; + + return _dm_pci_find_next_capability(dev, pos, cap); +} + +int dm_pci_find_next_ext_capability(struct udevice *dev, int start, int cap) { u32 header; int ttl; @@ -1364,6 +1377,9 @@ int dm_pci_find_ext_capability(struct udevice *dev, int cap) /* minimum 8 bytes per capability */ ttl = (PCI_CFG_SPACE_EXP_SIZE - PCI_CFG_SPACE_SIZE) / 8; + if (start) + pos = start; + dm_pci_read_config32(dev, pos, &header); /* * If we have no capabilities, this is indicated by cap ID, @@ -1386,6 +1402,11 @@ int dm_pci_find_ext_capability(struct udevice *dev, int cap) return 0; } +int dm_pci_find_ext_capability(struct udevice *dev, int cap) +{ + return dm_pci_find_next_ext_capability(dev, 0, cap); +} + UCLASS_DRIVER(pci) = { .id = UCLASS_PCI, .name = "pci", diff --git a/include/pci.h b/include/pci.h index 938a839..785d7d2 100644 --- a/include/pci.h +++ b/include/pci.h @@ -1313,6 +1313,29 @@ pci_addr_t dm_pci_phys_to_bus(struct udevice *dev, phys_addr_t addr, void *dm_pci_map_bar(struct udevice *dev, int bar, int flags); /** + * dm_pci_find_next_capability() - find a capability starting from an offset + * + * Tell if a device supports a given PCI capability. Returns the + * address of the requested capability structure within the device's + * PCI configuration space or 0 in case the device does not support it. + * + * Possible values for @cap: + * + * %PCI_CAP_ID_MSI Message Signalled Interrupts + * %PCI_CAP_ID_PCIX PCI-X + * %PCI_CAP_ID_EXP PCI Express + * %PCI_CAP_ID_MSIX MSI-X + * + * See PCI_CAP_ID_xxx for the complete capability ID codes. + * + * @dev: PCI device to query + * @start: offset to start from + * @cap: capability code + * @return: capability address or 0 if not supported + */ +int dm_pci_find_next_capability(struct udevice *dev, u8 start, int cap); + +/** * dm_pci_find_capability() - find a capability * * Tell if a device supports a given PCI capability. Returns the @@ -1335,6 +1358,31 @@ void *dm_pci_map_bar(struct udevice *dev, int bar, int flags); int dm_pci_find_capability(struct udevice *dev, int cap); /** + * dm_pci_find_next_ext_capability() - find an extended capability + * starting from an offset + * + * Tell if a device supports a given PCI express extended capability. + * Returns the address of the requested extended capability structure + * within the device's PCI configuration space or 0 in case the device + * does not support it. + * + * Possible values for @cap: + * + * %PCI_EXT_CAP_ID_ERR Advanced Error Reporting + * %PCI_EXT_CAP_ID_VC Virtual Channel + * %PCI_EXT_CAP_ID_DSN Device Serial Number + * %PCI_EXT_CAP_ID_PWR Power Budgeting + * + * See PCI_EXT_CAP_ID_xxx for the complete extended capability ID codes. + * + * @dev: PCI device to query + * @start: offset to start from + * @cap: extended capability code + * @return: extended capability address or 0 if not supported + */ +int dm_pci_find_next_ext_capability(struct udevice *dev, int start, int cap); + +/** * dm_pci_find_ext_capability() - find an extended capability * * Tell if a device supports a given PCI express extended capability. From patchwork Mon Oct 15 09:21:22 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bin Meng X-Patchwork-Id: 984047 X-Patchwork-Delegate: sjg@chromium.org Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=lists.denx.de (client-ip=81.169.180.215; helo=lists.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="Ipf9ft5L"; dkim-atps=neutral Received: from lists.denx.de (dione.denx.de [81.169.180.215]) by ozlabs.org (Postfix) with ESMTP id 42YYGz2nL3z9sBq for ; Mon, 15 Oct 2018 20:36:27 +1100 (AEDT) Received: by lists.denx.de (Postfix, from userid 105) id 0C781C21E62; Mon, 15 Oct 2018 09:28:23 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on lists.denx.de X-Spam-Level: X-Spam-Status: No, score=-0.0 required=5.0 tests=FREEMAIL_FROM, RCVD_IN_DNSWL_BLOCKED, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, T_DKIM_INVALID autolearn=unavailable autolearn_force=no version=3.4.0 Received: from lists.denx.de (localhost [IPv6:::1]) by lists.denx.de (Postfix) with ESMTP id 2985AC21D8A; Mon, 15 Oct 2018 09:17:23 +0000 (UTC) Received: by lists.denx.de (Postfix, from userid 105) id 7F70CC21E89; Mon, 15 Oct 2018 09:17:10 +0000 (UTC) Received: from mail-pl1-f196.google.com (mail-pl1-f196.google.com [209.85.214.196]) by lists.denx.de (Postfix) with ESMTPS id 46C31C21ECF for ; Mon, 15 Oct 2018 09:17:04 +0000 (UTC) Received: by mail-pl1-f196.google.com with SMTP id y11-v6so8992972plt.3 for ; Mon, 15 Oct 2018 02:17:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=s6yUqDPw9ZQmVcHzP0ae2M89ACxoqtUQD3aRs3bgMCc=; b=Ipf9ft5LNbfkhhH7KW1qIjy2c5sjg7JI72rfB/rnm3raPVfS5B8OQJ3yY4EXge3d9K 9wqzuVpCBajvvYOZofd3Ff8iwKh++JVG36w91V46fJcHQsib0rl5Hbz8LoD58ns/BdnY ZIw7NnTnotWxnxvUzyCkbL8MNAlxRShmnYv5seZa3uAmj2Qg9f26bnipJj2EsA1vDgAh zr07+fW2w5RSwUaJaC18jc0IltiKZgHIz8GhNfz1O0Tnz+HV4nul4Pgd7qCIrB+z78XW VzKhC0jLvQsRtzmWHWf/WY0nFMMiv84Hk8kCx6AttL2NMHISiTGFGGclppD5XSNJYkbb wXBQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=s6yUqDPw9ZQmVcHzP0ae2M89ACxoqtUQD3aRs3bgMCc=; b=Hz7kLwyb+yhUvWsWQ2nc/bDgEM09/2gneEXOQbHRexCv69cyCVRZV90dcofclBICN0 dj+e+XGYD8j+PrFOv0OiiPc8LAbp4qtuD/YfROWjFjAbOz7L1uTxC+eA/CYv5AC2szKk hllAeIF8H10C5aFyijJuJTdGUDUaRAkijkv/ab8+YOhFCcqFOsa/1cdHNyoYN/4NLWKi 0Pb1zRd65RojGGUjLQegEem91fkvyKkCPNxwNODiAqAgwK4GyQMmJOxH3x/2KEJAYPOI 5t0z1kgagR7MHnSiQtQf8vkRIVpXhpSUQ0hzXho8Y68NR9FW/AdxPjA29nHT/V7u7bWx a6Ug== X-Gm-Message-State: ABuFfogara9D5ry6RKs7jq97ICjGsxCTPva30dzcJwfLKMPT35Nw6huF K6d7GgBFawc34b01qpyvkSSwzOIx X-Google-Smtp-Source: ACcGV60Icb/WbGCr78rG7oYYblbfLHZiarttNCbtytX3okPuicJmX17WEmfsElYwX7R0Ka3E/XBSVA== X-Received: by 2002:a17:902:b198:: with SMTP id s24-v6mr15626402plr.70.1539595022980; Mon, 15 Oct 2018 02:17:02 -0700 (PDT) Received: from ala-d2121-lx1.wrs.com (unknown-156-139.windriver.com. [147.11.156.139]) by smtp.gmail.com with ESMTPSA id a11-v6sm10798256pgw.54.2018.10.15.02.17.01 (version=TLS1_1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 15 Oct 2018 02:17:02 -0700 (PDT) From: Bin Meng To: Simon Glass , U-Boot Mailing List Date: Mon, 15 Oct 2018 02:21:22 -0700 Message-Id: <1539595287-31378-27-git-send-email-bmeng.cn@gmail.com> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: <1539595287-31378-1-git-send-email-bmeng.cn@gmail.com> References: <1539595287-31378-1-git-send-email-bmeng.cn@gmail.com> Subject: [U-Boot] [PATCH v3 26/31] test: dm: pci: Add cases for finding next PCI capability APIs X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.18 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" Add test cases to cover the two newly added PCI APIs: dm_pci_find_next_capability() & dm_pci_find_next_ext_capability(). Signed-off-by: Bin Meng Reviewed-by: Simon Glass Signed-off-by: Bin Meng Reviewed-by: Simon Glass --- Changes in v3: None Changes in v2: - adjust swap_case driver to handle request from _dm_pci_find_next_capability() drivers/misc/swap_case.c | 9 +++++++++ test/dm/pci.c | 20 ++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/drivers/misc/swap_case.c b/drivers/misc/swap_case.c index bffb809..fa608ce 100644 --- a/drivers/misc/swap_case.c +++ b/drivers/misc/swap_case.c @@ -124,12 +124,21 @@ static int sandbox_swap_case_read_config(struct udevice *emul, uint offset, case PCI_CAP_ID_PM_OFFSET: *valuep = (PCI_CAP_ID_EXP_OFFSET << 8) | PCI_CAP_ID_PM; break; + case PCI_CAP_ID_PM_OFFSET + PCI_CAP_LIST_NEXT: + *valuep = PCI_CAP_ID_EXP_OFFSET; + break; case PCI_CAP_ID_EXP_OFFSET: *valuep = (PCI_CAP_ID_MSIX_OFFSET << 8) | PCI_CAP_ID_EXP; break; + case PCI_CAP_ID_EXP_OFFSET + PCI_CAP_LIST_NEXT: + *valuep = PCI_CAP_ID_MSIX_OFFSET; + break; case PCI_CAP_ID_MSIX_OFFSET: *valuep = PCI_CAP_ID_MSIX; break; + case PCI_CAP_ID_MSIX_OFFSET + PCI_CAP_LIST_NEXT: + *valuep = 0; + break; case PCI_EXT_CAP_ID_ERR_OFFSET: *valuep = (PCI_EXT_CAP_ID_VC_OFFSET << 20) | PCI_EXT_CAP_ID_ERR; break; diff --git a/test/dm/pci.c b/test/dm/pci.c index 8699700..f645b8b 100644 --- a/test/dm/pci.c +++ b/test/dm/pci.c @@ -206,6 +206,16 @@ static int dm_test_pci_cap(struct unit_test_state *uts) cap = dm_pci_find_capability(swap, PCI_CAP_ID_PCIX); ut_asserteq(0, cap); + /* look up PCI_CAP_ID_MSIX starting from PCI_CAP_ID_PM_OFFSET */ + cap = dm_pci_find_next_capability(swap, PCI_CAP_ID_PM_OFFSET, + PCI_CAP_ID_MSIX); + ut_asserteq(PCI_CAP_ID_MSIX_OFFSET, cap); + + /* look up PCI_CAP_ID_VNDR starting from PCI_CAP_ID_EXP_OFFSET */ + cap = dm_pci_find_next_capability(swap, PCI_CAP_ID_EXP_OFFSET, + PCI_CAP_ID_VNDR); + ut_asserteq(0, cap); + ut_assertok(uclass_get_device_by_seq(UCLASS_PCI, 1, &bus)); ut_assertok(dm_pci_bus_find_bdf(PCI_BDF(1, 0x08, 0), &swap)); @@ -217,6 +227,16 @@ static int dm_test_pci_cap(struct unit_test_state *uts) cap = dm_pci_find_ext_capability(swap, PCI_EXT_CAP_ID_SRIOV); ut_asserteq(0, cap); + /* look up PCI_EXT_CAP_ID_DSN starting from PCI_EXT_CAP_ID_ERR_OFFSET */ + cap = dm_pci_find_next_ext_capability(swap, PCI_EXT_CAP_ID_ERR_OFFSET, + PCI_EXT_CAP_ID_DSN); + ut_asserteq(PCI_EXT_CAP_ID_DSN_OFFSET, cap); + + /* look up PCI_EXT_CAP_ID_RCRB starting from PCI_EXT_CAP_ID_VC_OFFSET */ + cap = dm_pci_find_next_ext_capability(swap, PCI_EXT_CAP_ID_VC_OFFSET, + PCI_EXT_CAP_ID_RCRB); + ut_asserteq(0, cap); + return 0; } DM_TEST(dm_test_pci_cap, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); From patchwork Mon Oct 15 09:21:23 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bin Meng X-Patchwork-Id: 984052 X-Patchwork-Delegate: sjg@chromium.org Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=lists.denx.de (client-ip=81.169.180.215; helo=lists.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="kGPce7Z8"; dkim-atps=neutral Received: from lists.denx.de (dione.denx.de [81.169.180.215]) by ozlabs.org (Postfix) with ESMTP id 42YYKb3jSmz9s5c for ; Mon, 15 Oct 2018 20:38:43 +1100 (AEDT) Received: by lists.denx.de (Postfix, from userid 105) id 37C80C21E4E; Mon, 15 Oct 2018 09:33:40 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on lists.denx.de X-Spam-Level: X-Spam-Status: No, score=-0.0 required=5.0 tests=FREEMAIL_FROM, RCVD_IN_DNSWL_BLOCKED, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, T_DKIM_INVALID autolearn=unavailable autolearn_force=no version=3.4.0 Received: from lists.denx.de (localhost [IPv6:::1]) by lists.denx.de (Postfix) with ESMTP id 0D265C21F13; Mon, 15 Oct 2018 09:17:35 +0000 (UTC) Received: by lists.denx.de (Postfix, from userid 105) id 9815FC21E76; Mon, 15 Oct 2018 09:17:14 +0000 (UTC) Received: from mail-pl1-f193.google.com (mail-pl1-f193.google.com [209.85.214.193]) by lists.denx.de (Postfix) with ESMTPS id D690FC21E8A for ; Mon, 15 Oct 2018 09:17:05 +0000 (UTC) Received: by mail-pl1-f193.google.com with SMTP id f18-v6so9004585plr.1 for ; Mon, 15 Oct 2018 02:17:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=YMAum6iTBoh+gs8w61WAas3Oovve+k3ATHC3be745rA=; b=kGPce7Z8kNBwAvGGeN7Hjt7aSgVBU+VTVs5YReHtXOEPYy5lVY4nXz47dxZ9x5wYiQ npaXKiPjarlKDXwtWE5F4jBMrhQevhyyQcgeWf52M/geyKxNyW1SrFxC0GoQMoxDCJJF wK32QYpEhvAU0MzRlzNeAlYZjtb+cx3TNdGEAKRpIq2GNuFscnRLl4fRFqVo2A3gK40f mos0JbBOfUU0LI48fHzVO12UwOrZ4TRKYIYklwgIQKux7egUV8kYo77jovSvCLgCkZm8 lFKjdC39sTGdSfe0fhp8CYxVuzZnd4jeypuI6Tw8O3bDng0D4/l1J5+p/0EhGjIoaEHI lhJw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=YMAum6iTBoh+gs8w61WAas3Oovve+k3ATHC3be745rA=; b=UowlzLOhAFGHTICkXXiEzeW1qWNxDmcJOpiliPrzOVJfghJj3Q9bGOZs7F9V74KPr6 mTAK3JLI3DO89tInmJN8ccRG+XRp7aGk5xZZBBK7EPV+i2o3tLyKNiQxEbtFTuFE5nQT QFSYSFYXezhYDAMLrwATrebjuoeQ13nuTApMdP5k2IUTsE/XhzHicDyaf71ynQEfzSmp GQfAONaJtag3XGBR3F/NSs+JFN5VYdcvV0tx3Bg7A5jVq/I7xYiy0YjXt5EdZCN6cPnF 9zgrt3QnJgFwsot5T6j2kAmKF8Yu/aXsE/ROxmm3SWSIb/Dd8EtUijkWq98JOBGwZrEL 3ZhA== X-Gm-Message-State: ABuFfohNKDLq+g3NOUBTahp1J3SZ9TkpafOJ3hkTovKtFe+T5umPti4G nyKLkDaazYkMIz/nFAqwT4M= X-Google-Smtp-Source: ACcGV61pkByTict9mB7JVyhrmGlxUNaRhemGT6QuoZJFtinUfLMfeHNCpSwOaebQksT4Zy/BOHY85w== X-Received: by 2002:a17:902:421:: with SMTP id 30-v6mr16629688ple.184.1539595024224; Mon, 15 Oct 2018 02:17:04 -0700 (PDT) Received: from ala-d2121-lx1.wrs.com (unknown-156-139.windriver.com. [147.11.156.139]) by smtp.gmail.com with ESMTPSA id a11-v6sm10798256pgw.54.2018.10.15.02.17.03 (version=TLS1_1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 15 Oct 2018 02:17:03 -0700 (PDT) From: Bin Meng To: Simon Glass , U-Boot Mailing List Date: Mon, 15 Oct 2018 02:21:23 -0700 Message-Id: <1539595287-31378-28-git-send-email-bmeng.cn@gmail.com> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: <1539595287-31378-1-git-send-email-bmeng.cn@gmail.com> References: <1539595287-31378-1-git-send-email-bmeng.cn@gmail.com> Subject: [U-Boot] [PATCH v3 27/31] virtio: pci: Support non-legacy PCI transport device X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.18 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" By default QEMU creates legacy PCI transport devices, but we can ask QEMU to create non-legacy one if we pass additional device property/value pairs in the command line: -device virtio-blk-pci,disable-legacy=true,disable-modern=false This adds a new driver driver to support non-legacy (modern) device mode. Previous driver/file name is changed accordingly. Signed-off-by: Bin Meng Reviewed-by: Simon Glass Signed-off-by: Bin Meng Reviewed-by: Simon Glass --- Changes in v3: None Changes in v2: - fix compiler warnings in 64-bit build - adjust virtio_pci_find_capability() to avoid walking through capability list to find next one by ourselves drivers/virtio/Makefile | 2 +- .../virtio/{virtio_pci.c => virtio_pci_legacy.c} | 6 +- drivers/virtio/virtio_pci_modern.c | 609 +++++++++++++++++++++ 3 files changed, 613 insertions(+), 4 deletions(-) rename drivers/virtio/{virtio_pci.c => virtio_pci_legacy.c} (98%) create mode 100644 drivers/virtio/virtio_pci_modern.c diff --git a/drivers/virtio/Makefile b/drivers/virtio/Makefile index 5ee6183..072fb56 100644 --- a/drivers/virtio/Makefile +++ b/drivers/virtio/Makefile @@ -5,6 +5,6 @@ obj-y += virtio-uclass.o virtio_ring.o obj-$(CONFIG_VIRTIO_MMIO) += virtio_mmio.o -obj-$(CONFIG_VIRTIO_PCI) += virtio_pci.o +obj-$(CONFIG_VIRTIO_PCI) += virtio_pci_legacy.o virtio_pci_modern.o obj-$(CONFIG_VIRTIO_NET) += virtio_net.o obj-$(CONFIG_VIRTIO_BLK) += virtio_blk.o diff --git a/drivers/virtio/virtio_pci.c b/drivers/virtio/virtio_pci_legacy.c similarity index 98% rename from drivers/virtio/virtio_pci.c rename to drivers/virtio/virtio_pci_legacy.c index a3f1083..08764ee 100644 --- a/drivers/virtio/virtio_pci.c +++ b/drivers/virtio/virtio_pci_legacy.c @@ -16,7 +16,7 @@ #include #include "virtio_pci.h" -#define VIRTIO_PCI_DRV_NAME "virtio-pci" +#define VIRTIO_PCI_DRV_NAME "virtio-pci.l" /* PCI device ID in the range 0x1000 to 0x103f */ #define VIRTIO_PCI_VENDOR_ID 0x1af4 @@ -341,7 +341,7 @@ static const struct dm_virtio_ops virtio_pci_ops = { .notify = virtio_pci_notify, }; -U_BOOT_DRIVER(virtio_pci) = { +U_BOOT_DRIVER(virtio_pci_legacy) = { .name = VIRTIO_PCI_DRV_NAME, .id = UCLASS_VIRTIO, .ops = &virtio_pci_ops, @@ -418,4 +418,4 @@ static struct pci_device_id virtio_pci_supported[] = { {}, }; -U_BOOT_PCI_DEVICE(virtio_pci, virtio_pci_supported); +U_BOOT_PCI_DEVICE(virtio_pci_legacy, virtio_pci_supported); diff --git a/drivers/virtio/virtio_pci_modern.c b/drivers/virtio/virtio_pci_modern.c new file mode 100644 index 0000000..da76aea --- /dev/null +++ b/drivers/virtio/virtio_pci_modern.c @@ -0,0 +1,609 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * Copyright (C) 2018, Bin Meng + * + * VirtIO PCI bus transport driver + * Ported from Linux drivers/virtio/virtio_pci*.c + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include "virtio_pci.h" + +#define VIRTIO_PCI_DRV_NAME "virtio-pci.m" + +/* PCI device ID in the range 0x1040 to 0x107f */ +#define VIRTIO_PCI_VENDOR_ID 0x1af4 +#define VIRTIO_PCI_DEVICE_ID00 0x1040 +#define VIRTIO_PCI_DEVICE_ID01 0x1041 +#define VIRTIO_PCI_DEVICE_ID02 0x1042 +#define VIRTIO_PCI_DEVICE_ID03 0x1043 +#define VIRTIO_PCI_DEVICE_ID04 0x1044 +#define VIRTIO_PCI_DEVICE_ID05 0x1045 +#define VIRTIO_PCI_DEVICE_ID06 0x1046 +#define VIRTIO_PCI_DEVICE_ID07 0x1047 +#define VIRTIO_PCI_DEVICE_ID08 0x1048 +#define VIRTIO_PCI_DEVICE_ID09 0x1049 +#define VIRTIO_PCI_DEVICE_ID0A 0x104a +#define VIRTIO_PCI_DEVICE_ID0B 0x104b +#define VIRTIO_PCI_DEVICE_ID0C 0x104c +#define VIRTIO_PCI_DEVICE_ID0D 0x104d +#define VIRTIO_PCI_DEVICE_ID0E 0x104e +#define VIRTIO_PCI_DEVICE_ID0F 0x104f +#define VIRTIO_PCI_DEVICE_ID10 0x1050 +#define VIRTIO_PCI_DEVICE_ID11 0x1051 +#define VIRTIO_PCI_DEVICE_ID12 0x1052 +#define VIRTIO_PCI_DEVICE_ID13 0x1053 +#define VIRTIO_PCI_DEVICE_ID14 0x1054 +#define VIRTIO_PCI_DEVICE_ID15 0x1055 +#define VIRTIO_PCI_DEVICE_ID16 0x1056 +#define VIRTIO_PCI_DEVICE_ID17 0x1057 +#define VIRTIO_PCI_DEVICE_ID18 0x1058 +#define VIRTIO_PCI_DEVICE_ID19 0x1059 +#define VIRTIO_PCI_DEVICE_ID1A 0x105a +#define VIRTIO_PCI_DEVICE_ID1B 0x105b +#define VIRTIO_PCI_DEVICE_ID1C 0x105c +#define VIRTIO_PCI_DEVICE_ID1D 0x105d +#define VIRTIO_PCI_DEVICE_ID1E 0x105e +#define VIRTIO_PCI_DEVICE_ID1F 0x105f +#define VIRTIO_PCI_DEVICE_ID20 0x1060 +#define VIRTIO_PCI_DEVICE_ID21 0x1061 +#define VIRTIO_PCI_DEVICE_ID22 0x1062 +#define VIRTIO_PCI_DEVICE_ID23 0x1063 +#define VIRTIO_PCI_DEVICE_ID24 0x1064 +#define VIRTIO_PCI_DEVICE_ID25 0x1065 +#define VIRTIO_PCI_DEVICE_ID26 0x1066 +#define VIRTIO_PCI_DEVICE_ID27 0x1067 +#define VIRTIO_PCI_DEVICE_ID28 0x1068 +#define VIRTIO_PCI_DEVICE_ID29 0x1069 +#define VIRTIO_PCI_DEVICE_ID2A 0x106a +#define VIRTIO_PCI_DEVICE_ID2B 0x106b +#define VIRTIO_PCI_DEVICE_ID2C 0x106c +#define VIRTIO_PCI_DEVICE_ID2D 0x106d +#define VIRTIO_PCI_DEVICE_ID2E 0x106e +#define VIRTIO_PCI_DEVICE_ID2F 0x106f +#define VIRTIO_PCI_DEVICE_ID30 0x1070 +#define VIRTIO_PCI_DEVICE_ID31 0x1071 +#define VIRTIO_PCI_DEVICE_ID32 0x1072 +#define VIRTIO_PCI_DEVICE_ID33 0x1073 +#define VIRTIO_PCI_DEVICE_ID34 0x1074 +#define VIRTIO_PCI_DEVICE_ID35 0x1075 +#define VIRTIO_PCI_DEVICE_ID36 0x1076 +#define VIRTIO_PCI_DEVICE_ID37 0x1077 +#define VIRTIO_PCI_DEVICE_ID38 0x1078 +#define VIRTIO_PCI_DEVICE_ID39 0x1079 +#define VIRTIO_PCI_DEVICE_ID3A 0x107a +#define VIRTIO_PCI_DEVICE_ID3B 0x107b +#define VIRTIO_PCI_DEVICE_ID3C 0x107c +#define VIRTIO_PCI_DEVICE_ID3D 0x107d +#define VIRTIO_PCI_DEVICE_ID3E 0x107e +#define VIRTIO_PCI_DEVICE_ID3F 0x107f + +/** + * virtio pci transport driver private data + * + * @common: pci transport device common register block base + * @notify_base: pci transport device notify register block base + * @device: pci transport device device-specific register block base + * @device_len: pci transport device device-specific register block length + * @notify_offset_multiplier: multiply queue_notify_off by this value + */ +struct virtio_pci_priv { + struct virtio_pci_common_cfg __iomem *common; + void __iomem *notify_base; + void __iomem *device; + u32 device_len; + u32 notify_offset_multiplier; +}; + +static int virtio_pci_get_config(struct udevice *udev, unsigned int offset, + void *buf, unsigned int len) +{ + struct virtio_pci_priv *priv = dev_get_priv(udev); + u8 b; + __le16 w; + __le32 l; + + WARN_ON(offset + len > priv->device_len); + + switch (len) { + case 1: + b = ioread8(priv->device + offset); + memcpy(buf, &b, sizeof(b)); + break; + case 2: + w = cpu_to_le16(ioread16(priv->device + offset)); + memcpy(buf, &w, sizeof(w)); + break; + case 4: + l = cpu_to_le32(ioread32(priv->device + offset)); + memcpy(buf, &l, sizeof(l)); + break; + case 8: + l = cpu_to_le32(ioread32(priv->device + offset)); + memcpy(buf, &l, sizeof(l)); + l = cpu_to_le32(ioread32(priv->device + offset + sizeof(l))); + memcpy(buf + sizeof(l), &l, sizeof(l)); + break; + default: + WARN_ON(true); + } + + return 0; +} + +static int virtio_pci_set_config(struct udevice *udev, unsigned int offset, + const void *buf, unsigned int len) +{ + struct virtio_pci_priv *priv = dev_get_priv(udev); + u8 b; + __le16 w; + __le32 l; + + WARN_ON(offset + len > priv->device_len); + + switch (len) { + case 1: + memcpy(&b, buf, sizeof(b)); + iowrite8(b, priv->device + offset); + break; + case 2: + memcpy(&w, buf, sizeof(w)); + iowrite16(le16_to_cpu(w), priv->device + offset); + break; + case 4: + memcpy(&l, buf, sizeof(l)); + iowrite32(le32_to_cpu(l), priv->device + offset); + break; + case 8: + memcpy(&l, buf, sizeof(l)); + iowrite32(le32_to_cpu(l), priv->device + offset); + memcpy(&l, buf + sizeof(l), sizeof(l)); + iowrite32(le32_to_cpu(l), priv->device + offset + sizeof(l)); + break; + default: + WARN_ON(true); + } + + return 0; +} + +static int virtio_pci_generation(struct udevice *udev, u32 *counter) +{ + struct virtio_pci_priv *priv = dev_get_priv(udev); + + *counter = ioread8(&priv->common->config_generation); + + return 0; +} + +static int virtio_pci_get_status(struct udevice *udev, u8 *status) +{ + struct virtio_pci_priv *priv = dev_get_priv(udev); + + *status = ioread8(&priv->common->device_status); + + return 0; +} + +static int virtio_pci_set_status(struct udevice *udev, u8 status) +{ + struct virtio_pci_priv *priv = dev_get_priv(udev); + + /* We should never be setting status to 0 */ + WARN_ON(status == 0); + + iowrite8(status, &priv->common->device_status); + + return 0; +} + +static int virtio_pci_reset(struct udevice *udev) +{ + struct virtio_pci_priv *priv = dev_get_priv(udev); + + /* 0 status means a reset */ + iowrite8(0, &priv->common->device_status); + + /* + * After writing 0 to device_status, the driver MUST wait for a read + * of device_status to return 0 before reinitializing the device. + * This will flush out the status write, and flush in device writes, + * including MSI-X interrupts, if any. + */ + while (ioread8(&priv->common->device_status)) + udelay(1000); + + return 0; +} + +static int virtio_pci_get_features(struct udevice *udev, u64 *features) +{ + struct virtio_pci_priv *priv = dev_get_priv(udev); + + iowrite32(0, &priv->common->device_feature_select); + *features = ioread32(&priv->common->device_feature); + iowrite32(1, &priv->common->device_feature_select); + *features |= ((u64)ioread32(&priv->common->device_feature) << 32); + + return 0; +} + +static int virtio_pci_set_features(struct udevice *udev) +{ + struct virtio_pci_priv *priv = dev_get_priv(udev); + struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(udev); + + if (!__virtio_test_bit(udev, VIRTIO_F_VERSION_1)) { + debug("virtio: device uses modern interface but does not have VIRTIO_F_VERSION_1\n"); + return -EINVAL; + } + + iowrite32(0, &priv->common->guest_feature_select); + iowrite32((u32)uc_priv->features, &priv->common->guest_feature); + iowrite32(1, &priv->common->guest_feature_select); + iowrite32(uc_priv->features >> 32, &priv->common->guest_feature); + + return 0; +} + +static struct virtqueue *virtio_pci_setup_vq(struct udevice *udev, + unsigned int index) +{ + struct virtio_pci_priv *priv = dev_get_priv(udev); + struct virtio_pci_common_cfg __iomem *cfg = priv->common; + struct virtqueue *vq; + u16 num; + u64 addr; + int err; + + if (index >= ioread16(&cfg->num_queues)) + return ERR_PTR(-ENOENT); + + /* Select the queue we're interested in */ + iowrite16(index, &cfg->queue_select); + + /* Check if queue is either not available or already active */ + num = ioread16(&cfg->queue_size); + if (!num || ioread16(&cfg->queue_enable)) + return ERR_PTR(-ENOENT); + + if (num & (num - 1)) { + printf("(%s): bad queue size %u", udev->name, num); + return ERR_PTR(-EINVAL); + } + + /* Create the vring */ + vq = vring_create_virtqueue(index, num, VIRTIO_PCI_VRING_ALIGN, udev); + if (!vq) { + err = -ENOMEM; + goto error_available; + } + + /* Activate the queue */ + iowrite16(virtqueue_get_vring_size(vq), &cfg->queue_size); + + addr = virtqueue_get_desc_addr(vq); + iowrite32((u32)addr, &cfg->queue_desc_lo); + iowrite32(addr >> 32, &cfg->queue_desc_hi); + + addr = virtqueue_get_avail_addr(vq); + iowrite32((u32)addr, &cfg->queue_avail_lo); + iowrite32(addr >> 32, &cfg->queue_avail_hi); + + addr = virtqueue_get_used_addr(vq); + iowrite32((u32)addr, &cfg->queue_used_lo); + iowrite32(addr >> 32, &cfg->queue_used_hi); + + iowrite16(1, &cfg->queue_enable); + + return vq; + +error_available: + return ERR_PTR(err); +} + +static void virtio_pci_del_vq(struct virtqueue *vq) +{ + struct virtio_pci_priv *priv = dev_get_priv(vq->vdev); + unsigned int index = vq->index; + + iowrite16(index, &priv->common->queue_select); + + /* Select and deactivate the queue */ + iowrite16(0, &priv->common->queue_enable); + + vring_del_virtqueue(vq); +} + +static int virtio_pci_del_vqs(struct udevice *udev) +{ + struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(udev); + struct virtqueue *vq, *n; + + list_for_each_entry_safe(vq, n, &uc_priv->vqs, list) + virtio_pci_del_vq(vq); + + return 0; +} + +static int virtio_pci_find_vqs(struct udevice *udev, unsigned int nvqs, + struct virtqueue *vqs[]) +{ + int i; + + for (i = 0; i < nvqs; ++i) { + vqs[i] = virtio_pci_setup_vq(udev, i); + if (IS_ERR(vqs[i])) { + virtio_pci_del_vqs(udev); + return PTR_ERR(vqs[i]); + } + } + + return 0; +} + +static int virtio_pci_notify(struct udevice *udev, struct virtqueue *vq) +{ + struct virtio_pci_priv *priv = dev_get_priv(udev); + u16 off; + + /* Select the queue we're interested in */ + iowrite16(vq->index, &priv->common->queue_select); + + /* get offset of notification word for this vq */ + off = ioread16(&priv->common->queue_notify_off); + + /* + * We write the queue's selector into the notification register + * to signal the other end + */ + iowrite16(vq->index, + priv->notify_base + off * priv->notify_offset_multiplier); + + return 0; +} + +/** + * virtio_pci_find_capability - walk capabilities to find device info + * + * @udev: the transport device + * @cfg_type: the VIRTIO_PCI_CAP_* value we seek + * + * @return offset of the configuration structure + */ +static int virtio_pci_find_capability(struct udevice *udev, u8 cfg_type) +{ + int pos; + int offset; + u8 type, bar; + + for (pos = dm_pci_find_capability(udev, PCI_CAP_ID_VNDR); + pos > 0; + pos = dm_pci_find_next_capability(udev, pos, PCI_CAP_ID_VNDR)) { + offset = pos + offsetof(struct virtio_pci_cap, cfg_type); + dm_pci_read_config8(udev, offset, &type); + offset = pos + offsetof(struct virtio_pci_cap, bar); + dm_pci_read_config8(udev, offset, &bar); + + /* Ignore structures with reserved BAR values */ + if (bar > 0x5) + continue; + + if (type == cfg_type) + return pos; + } + + return 0; +} + +/** + * virtio_pci_map_capability - map base address of the capability + * + * @udev: the transport device + * @off: offset of the configuration structure + * + * @return base address of the capability + */ +static void __iomem *virtio_pci_map_capability(struct udevice *udev, int off) +{ + u8 bar; + u32 offset; + ulong base; + void __iomem *p; + + if (!off) + return NULL; + + offset = off + offsetof(struct virtio_pci_cap, bar); + dm_pci_read_config8(udev, offset, &bar); + offset = off + offsetof(struct virtio_pci_cap, offset); + dm_pci_read_config32(udev, offset, &offset); + + /* + * TODO: adding 64-bit BAR support + * + * Per spec, the BAR is permitted to be either 32-bit or 64-bit. + * For simplicity, only read the BAR address as 32-bit. + */ + base = dm_pci_read_bar32(udev, bar); + p = (void __iomem *)base + offset; + + return p; +} + +static int virtio_pci_bind(struct udevice *udev) +{ + static int num_devs; + char name[20]; + + /* Create a unique device name */ + sprintf(name, "%s#%u", VIRTIO_PCI_DRV_NAME, num_devs++); + device_set_name(udev, name); + + return 0; +} + +static int virtio_pci_probe(struct udevice *udev) +{ + struct pci_child_platdata *pplat = dev_get_parent_platdata(udev); + struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(udev); + struct virtio_pci_priv *priv = dev_get_priv(udev); + u16 subvendor; + u8 revision; + int common, notify, device; + int offset; + + /* We only own devices >= 0x1040 and <= 0x107f: leave the rest. */ + if (pplat->device < 0x1040 || pplat->device > 0x107f) + return -ENODEV; + + /* Transitional devices must not have a PCI revision ID of 0 */ + dm_pci_read_config8(udev, PCI_REVISION_ID, &revision); + + /* Modern devices: simply use PCI device id, but start from 0x1040. */ + uc_priv->device = pplat->device - 0x1040; + dm_pci_read_config16(udev, PCI_SUBSYSTEM_VENDOR_ID, &subvendor); + uc_priv->vendor = subvendor; + + /* Check for a common config: if not, use legacy mode (bar 0) */ + common = virtio_pci_find_capability(udev, VIRTIO_PCI_CAP_COMMON_CFG); + if (!common) { + printf("(%s): leaving for legacy driver\n", udev->name); + return -ENODEV; + } + + /* If common is there, notify should be too */ + notify = virtio_pci_find_capability(udev, VIRTIO_PCI_CAP_NOTIFY_CFG); + if (!notify) { + printf("(%s): missing capabilities %i/%i\n", udev->name, + common, notify); + return -EINVAL; + } + + /* + * Device capability is only mandatory for devices that have + * device-specific configuration. + */ + device = virtio_pci_find_capability(udev, VIRTIO_PCI_CAP_DEVICE_CFG); + if (device) { + offset = notify + offsetof(struct virtio_pci_cap, length); + dm_pci_read_config32(udev, offset, &priv->device_len); + } + + /* Map configuration structures */ + priv->common = virtio_pci_map_capability(udev, common); + priv->notify_base = virtio_pci_map_capability(udev, notify); + priv->device = virtio_pci_map_capability(udev, device); + debug("(%p): common @ %p, notify base @ %p, device @ %p\n", + udev, priv->common, priv->notify_base, priv->device); + + /* Read notify_off_multiplier from config space */ + offset = notify + offsetof(struct virtio_pci_notify_cap, + notify_off_multiplier); + dm_pci_read_config32(udev, offset, &priv->notify_offset_multiplier); + + debug("(%s): device (%d) vendor (%08x) version (%d)\n", udev->name, + uc_priv->device, uc_priv->vendor, revision); + + return 0; +} + +static const struct dm_virtio_ops virtio_pci_ops = { + .get_config = virtio_pci_get_config, + .set_config = virtio_pci_set_config, + .generation = virtio_pci_generation, + .get_status = virtio_pci_get_status, + .set_status = virtio_pci_set_status, + .reset = virtio_pci_reset, + .get_features = virtio_pci_get_features, + .set_features = virtio_pci_set_features, + .find_vqs = virtio_pci_find_vqs, + .del_vqs = virtio_pci_del_vqs, + .notify = virtio_pci_notify, +}; + +U_BOOT_DRIVER(virtio_pci_modern) = { + .name = VIRTIO_PCI_DRV_NAME, + .id = UCLASS_VIRTIO, + .ops = &virtio_pci_ops, + .bind = virtio_pci_bind, + .probe = virtio_pci_probe, + .priv_auto_alloc_size = sizeof(struct virtio_pci_priv), +}; + +static struct pci_device_id virtio_pci_supported[] = { + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID00) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID01) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID02) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID03) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID04) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID05) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID06) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID07) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID08) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID09) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID0A) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID0B) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID0C) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID0D) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID0E) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID0F) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID10) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID11) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID12) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID13) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID14) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID15) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID16) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID17) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID18) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID19) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID1A) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID1B) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID1C) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID1D) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID1E) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID1F) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID20) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID21) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID22) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID23) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID24) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID25) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID26) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID27) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID28) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID29) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID2A) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID2B) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID2C) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID2D) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID2E) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID2F) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID30) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID31) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID32) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID33) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID34) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID35) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID36) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID37) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID38) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID39) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID3A) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID3B) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID3C) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID3D) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID3E) }, + { PCI_DEVICE(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_DEVICE_ID3F) }, + {}, +}; + +U_BOOT_PCI_DEVICE(virtio_pci_modern, virtio_pci_supported); From patchwork Mon Oct 15 09:21:24 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bin Meng X-Patchwork-Id: 984025 X-Patchwork-Delegate: sjg@chromium.org Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=lists.denx.de (client-ip=81.169.180.215; helo=lists.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="Y+U+KyrP"; dkim-atps=neutral Received: from lists.denx.de (dione.denx.de [81.169.180.215]) by ozlabs.org (Postfix) with ESMTP id 42YY8W5nNwz9sBq for ; Mon, 15 Oct 2018 20:30:51 +1100 (AEDT) Received: by lists.denx.de (Postfix, from userid 105) id 4B0FEC21E35; Mon, 15 Oct 2018 09:29:27 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on lists.denx.de X-Spam-Level: X-Spam-Status: No, score=-0.0 required=5.0 tests=FREEMAIL_FROM, RCVD_IN_DNSWL_BLOCKED, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, T_DKIM_INVALID autolearn=unavailable autolearn_force=no version=3.4.0 Received: from lists.denx.de (localhost [IPv6:::1]) by lists.denx.de (Postfix) with ESMTP id 5D65EC21F34; Mon, 15 Oct 2018 09:17:29 +0000 (UTC) Received: by lists.denx.de (Postfix, from userid 105) id 0C41AC21F35; Mon, 15 Oct 2018 09:17:14 +0000 (UTC) Received: from mail-pl1-f196.google.com (mail-pl1-f196.google.com [209.85.214.196]) by lists.denx.de (Postfix) with ESMTPS id BD4BCC21DFD for ; Mon, 15 Oct 2018 09:17:06 +0000 (UTC) Received: by mail-pl1-f196.google.com with SMTP id u6-v6so6255832plz.4 for ; Mon, 15 Oct 2018 02:17:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=6U6gR9P7OBxSTLr/yUvOv3RGWvgvUvp43m1hkJL2psY=; b=Y+U+KyrPT2AFHtMQAQtt9G+BfRzUN0NQjVLgfpGgOlFLykhCb11+xPEu5UC2Rv1nYZ 4wMVowa9j6JGvaTS2JgV8gnwgPxDnJaoQw8SEfrGAT9NvIgTXC0WvFLa6fvRO5LS9+ea RV9MbC7aXW4EvSRAKyLZ8bJKvFs+d/3XdgZ05PU6tRbKqyy/MxtbJ2esQjFPexiU8y2Y Vbf8nA9fTIqo/ga0SCNVLwvxBdCvjC2pluNlr+M7x6Lx+b9NpRrZFhKJcmh3nF1W16sj 8hOImLss9D3+BSe8SnX302Kw7pEf7YE6SkTVIG+N4O3b+If1wcS0voisxkpzMFEDd6JV i9WA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=6U6gR9P7OBxSTLr/yUvOv3RGWvgvUvp43m1hkJL2psY=; b=GyJAm7rs1Rm6fi+BbydrNUhqlxWYTtzCaPgNmOz1ApzYmgaGp1+GOeLDLxmaMboa3X bigkDFnP01vZIH2YbFanwYSrefAiICaL4TNGzbfgcgaBbFrQvB+Xd3kgVqO6YzlYMBkO OWWRplt1Zl0yRJtYewRdk60Zfc4OBgot/v0Y72CBWztP8sIeZ3ZBUL3xWjp3Ionyd18V HNVMeRUm81BkzBYeixNsu7Ee9P7C9wCvLpMxW8TlcE7YTwP/g0fx15tVUwdFzaXEMrCx jBkAv5d7RwL7AU5vJtiBG5FeyaLtRQCISfZOKGH6YdFHiV0cfDP6KDHokHg5xhG6XAWv mdLQ== X-Gm-Message-State: ABuFfojVnTtSvRA+Hg/MiihAOpdulKeOwVZ0mR/j6GMKVta/Vov7nuX4 hENGkl90ghIQaKaokepTGKoZKbep X-Google-Smtp-Source: ACcGV62RcYJQ/htySh41LPuWylHI3+RBYAss0/hajdo3lZP746+x2MNQXk5Qt29yj3W80P8YFZ1IuA== X-Received: by 2002:a17:902:3fe4:: with SMTP id a91-v6mr16245536pld.187.1539595025352; Mon, 15 Oct 2018 02:17:05 -0700 (PDT) Received: from ala-d2121-lx1.wrs.com (unknown-156-139.windriver.com. [147.11.156.139]) by smtp.gmail.com with ESMTPSA id a11-v6sm10798256pgw.54.2018.10.15.02.17.04 (version=TLS1_1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 15 Oct 2018 02:17:04 -0700 (PDT) From: Bin Meng To: Simon Glass , U-Boot Mailing List Date: Mon, 15 Oct 2018 02:21:24 -0700 Message-Id: <1539595287-31378-29-git-send-email-bmeng.cn@gmail.com> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: <1539595287-31378-1-git-send-email-bmeng.cn@gmail.com> References: <1539595287-31378-1-git-send-email-bmeng.cn@gmail.com> Subject: [U-Boot] [PATCH v3 28/31] virtio: net: Support non-legacy device X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.18 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" For v1.0 compliant device, it always assumes the member 'num_buffers' exists in the struct virtio_net_hdr while the legacy driver only presented 'num_buffers' when VIRTIO_NET_F_MRG_RXBUF was negotiated. Without that feature the structure was 2 bytes shorter. Update the driver to support the non-legacy device. Signed-off-by: Bin Meng Reviewed-by: Simon Glass Signed-off-by: Bin Meng Reviewed-by: Simon Glass --- Changes in v3: None Changes in v2: None drivers/virtio/virtio_net.c | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/drivers/virtio/virtio_net.c b/drivers/virtio/virtio_net.c index 5bb6a9f..0dbbd78 100644 --- a/drivers/virtio/virtio_net.c +++ b/drivers/virtio/virtio_net.c @@ -32,6 +32,7 @@ struct virtio_net_priv { char rx_buff[VIRTIO_NET_NUM_RX_BUFS][VIRTIO_NET_RX_BUF_SIZE]; bool rx_running; + int net_hdr_len; }; /* @@ -77,12 +78,19 @@ static int virtio_net_send(struct udevice *dev, void *packet, int length) { struct virtio_net_priv *priv = dev_get_priv(dev); struct virtio_net_hdr hdr; - struct virtio_sg hdr_sg = { &hdr, sizeof(hdr) }; + struct virtio_net_hdr_v1 hdr_v1; + struct virtio_sg hdr_sg; struct virtio_sg data_sg = { packet, length }; struct virtio_sg *sgs[] = { &hdr_sg, &data_sg }; int ret; - memset(&hdr, 0, sizeof(struct virtio_net_hdr)); + if (priv->net_hdr_len == sizeof(struct virtio_net_hdr)) + hdr_sg.addr = &hdr; + else + hdr_sg.addr = &hdr_v1; + hdr_sg.length = priv->net_hdr_len; + + memset(hdr_sg.addr, 0, priv->net_hdr_len); ret = virtqueue_add(priv->tx_vq, sgs, 2, 0); if (ret) @@ -108,14 +116,14 @@ static int virtio_net_recv(struct udevice *dev, int flags, uchar **packetp) if (!buf) return -EAGAIN; - *packetp = buf + sizeof(struct virtio_net_hdr); - return len - sizeof(struct virtio_net_hdr); + *packetp = buf + priv->net_hdr_len; + return len - priv->net_hdr_len; } static int virtio_net_free_pkt(struct udevice *dev, uchar *packet, int length) { struct virtio_net_priv *priv = dev_get_priv(dev); - void *buf = packet - sizeof(struct virtio_net_hdr); + void *buf = packet - priv->net_hdr_len; struct virtio_sg sg = { buf, VIRTIO_NET_RX_BUF_SIZE }; struct virtio_sg *sgs[] = { &sg }; @@ -186,12 +194,25 @@ static int virtio_net_bind(struct udevice *dev) static int virtio_net_probe(struct udevice *dev) { struct virtio_net_priv *priv = dev_get_priv(dev); + struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(dev->parent); int ret; ret = virtio_find_vqs(dev, 2, priv->vqs); if (ret < 0) return ret; + /* + * For v1.0 compliant device, it always assumes the member + * 'num_buffers' exists in the struct virtio_net_hdr while + * the legacy driver only presented 'num_buffers' when + * VIRTIO_NET_F_MRG_RXBUF was negotiated. Without that feature + * the structure was 2 bytes shorter. + */ + if (uc_priv->legacy) + priv->net_hdr_len = sizeof(struct virtio_net_hdr); + else + priv->net_hdr_len = sizeof(struct virtio_net_hdr_v1); + return 0; } From patchwork Mon Oct 15 09:21:25 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bin Meng X-Patchwork-Id: 984032 X-Patchwork-Delegate: sjg@chromium.org Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=lists.denx.de (client-ip=81.169.180.215; helo=lists.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="s/gvCnUM"; dkim-atps=neutral Received: from lists.denx.de (dione.denx.de [81.169.180.215]) by ozlabs.org (Postfix) with ESMTP id 42YYB52fGKz9s5c for ; Mon, 15 Oct 2018 20:32:13 +1100 (AEDT) Received: by lists.denx.de (Postfix, from userid 105) id F2D91C21DEC; Mon, 15 Oct 2018 09:29:45 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on lists.denx.de X-Spam-Level: X-Spam-Status: No, score=-0.0 required=5.0 tests=FREEMAIL_FROM, RCVD_IN_DNSWL_BLOCKED, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, T_DKIM_INVALID autolearn=unavailable autolearn_force=no version=3.4.0 Received: from lists.denx.de (localhost [IPv6:::1]) by lists.denx.de (Postfix) with ESMTP id 687A7C21F53; Mon, 15 Oct 2018 09:17:30 +0000 (UTC) Received: by lists.denx.de (Postfix, from userid 105) id 4F97DC21F35; Mon, 15 Oct 2018 09:17:14 +0000 (UTC) Received: from mail-pf1-f193.google.com (mail-pf1-f193.google.com [209.85.210.193]) by lists.denx.de (Postfix) with ESMTPS id 02D60C21D8A for ; Mon, 15 Oct 2018 09:17:08 +0000 (UTC) Received: by mail-pf1-f193.google.com with SMTP id c25-v6so9363627pfe.6 for ; Mon, 15 Oct 2018 02:17:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=jSNszZtgCWP++gcyevvwjayR20uBol0VjSoVs+SnKWs=; b=s/gvCnUMmHgDNWY5E6+7csP2suH4fHfAvnxYFEeF3HRZr1lpW3owQOqjs+XTL5eqTS 1op3a59MPgzyTgUCPJYv//VraTUcvuFSg+7f/Y0jzVjsGnCDnE2LCl+cf4n+lr9Tn50v lpdUq3z2LM368wLr/qHBOvRDvG7NkjZ+OC8oRgs9ZWKUWS22tOLjX2/FHV2l2K+PanQJ WeBaqSlt/zYrYi+lNJX1dqQuwiXHNwYDFl0a05tp+owO1drIzaIMsw3VMexmWYxy8Id3 FxHZ/Tsnd8PGi2xvfWpC9vcYBdkFYrR5RKIrUC9O6XmpWgVv+uqoX+Mp69W/MDpih48Q PykA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=jSNszZtgCWP++gcyevvwjayR20uBol0VjSoVs+SnKWs=; b=b6t03SiYs+49jK9h7Do/X9hzrw25pPJ4O0Qa7CQ5+a8TpQ/m9SD6HcbnGTZHwXBlyq 29rw6Q54JHzjTXFyX1O1A9h7Dht30KUfWaVpj7lWT6HqiejZZ9cyUV7tBnNmb+wuSiAs Hd3jfKMS4avDbctilUSpOw7fO323MjI00JfRinw+3966gKv/Xb1nfvde3PqqfT+Qbbm2 80+5D2Z+eIlpvZ03VRlYdfTad5IvAZZ9Vxb6fxFAmYUy7oZiP4WpI58AwI9j3lKgklqG fqDoSQ7xTheLizoWYBX2m61MKZPvoP5bxJyg7bj8c6Agz5Hn4sHT8Fq8kNO371VdvekG GNJA== X-Gm-Message-State: ABuFfohSOaNGK5PgFOidt+EnvQg8VVXD3SORyOrZywwDhn4+qPy9UYqK 2ZYX/WGNkYiLFmj8T5jLY2+9n4Y/ X-Google-Smtp-Source: ACcGV610mUOg6z0gypUv0zz+u8NgpdwFwgxgrnJoPlTxm5l/IkAfyVDJwbPhdpzGi/dRV0WOGlGC/g== X-Received: by 2002:a63:c00b:: with SMTP id h11-v6mr15479648pgg.159.1539595026504; Mon, 15 Oct 2018 02:17:06 -0700 (PDT) Received: from ala-d2121-lx1.wrs.com (unknown-156-139.windriver.com. [147.11.156.139]) by smtp.gmail.com with ESMTPSA id a11-v6sm10798256pgw.54.2018.10.15.02.17.05 (version=TLS1_1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 15 Oct 2018 02:17:05 -0700 (PDT) From: Bin Meng To: Simon Glass , U-Boot Mailing List Date: Mon, 15 Oct 2018 02:21:25 -0700 Message-Id: <1539595287-31378-30-git-send-email-bmeng.cn@gmail.com> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: <1539595287-31378-1-git-send-email-bmeng.cn@gmail.com> References: <1539595287-31378-1-git-send-email-bmeng.cn@gmail.com> Subject: [U-Boot] [PATCH v3 29/31] virtio: Add a Sandbox transport driver X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.18 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" This driver provides support for Sandbox implementation of virtio transport driver which is used for testing purpose only. Two drivers are provided. The 2nd one is a driver that lacks the 'notify' op. Signed-off-by: Bin Meng Reviewed-by: Simon Glass Reviewed-by: Simon Glass --- Changes in v3: None Changes in v2: - new patch to add a Sandbox transport driver drivers/virtio/Kconfig | 8 ++ drivers/virtio/Makefile | 1 + drivers/virtio/virtio_sandbox.c | 233 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 242 insertions(+) create mode 100644 drivers/virtio/virtio_sandbox.c diff --git a/drivers/virtio/Kconfig b/drivers/virtio/Kconfig index 753912f..a9d5fd0 100644 --- a/drivers/virtio/Kconfig +++ b/drivers/virtio/Kconfig @@ -37,6 +37,14 @@ config VIRTIO_PCI This driver provides support for virtio based paravirtual device drivers over PCI. +config VIRTIO_SANDBOX + bool "Sandbox driver for virtio devices" + depends on SANDBOX + select VIRTIO + help + This driver provides support for Sandbox implementation of virtio + transport driver which is used for testing purpose only. + config VIRTIO_NET bool "virtio net driver" depends on VIRTIO diff --git a/drivers/virtio/Makefile b/drivers/virtio/Makefile index 072fb56..4579044 100644 --- a/drivers/virtio/Makefile +++ b/drivers/virtio/Makefile @@ -6,5 +6,6 @@ obj-y += virtio-uclass.o virtio_ring.o obj-$(CONFIG_VIRTIO_MMIO) += virtio_mmio.o obj-$(CONFIG_VIRTIO_PCI) += virtio_pci_legacy.o virtio_pci_modern.o +obj-$(CONFIG_VIRTIO_SANDBOX) += virtio_sandbox.o obj-$(CONFIG_VIRTIO_NET) += virtio_net.o obj-$(CONFIG_VIRTIO_BLK) += virtio_blk.o diff --git a/drivers/virtio/virtio_sandbox.c b/drivers/virtio/virtio_sandbox.c new file mode 100644 index 0000000..2addb1e --- /dev/null +++ b/drivers/virtio/virtio_sandbox.c @@ -0,0 +1,233 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * Copyright (C) 2018, Bin Meng + * + * VirtIO Sandbox transport driver, for testing purpose only + */ + +#include +#include +#include +#include +#include +#include +#include + +struct virtio_sandbox_priv { + u8 id; + u8 status; + u64 device_features; + u64 driver_features; + ulong queue_desc; + ulong queue_available; + ulong queue_used; +}; + +static int virtio_sandbox_get_config(struct udevice *udev, unsigned int offset, + void *buf, unsigned int len) +{ + return 0; +} + +static int virtio_sandbox_set_config(struct udevice *udev, unsigned int offset, + const void *buf, unsigned int len) +{ + return 0; +} + +static int virtio_sandbox_get_status(struct udevice *udev, u8 *status) +{ + struct virtio_sandbox_priv *priv = dev_get_priv(udev); + + *status = priv->status; + + return 0; +} + +static int virtio_sandbox_set_status(struct udevice *udev, u8 status) +{ + struct virtio_sandbox_priv *priv = dev_get_priv(udev); + + /* We should never be setting status to 0 */ + WARN_ON(status == 0); + + priv->status = status; + + return 0; +} + +static int virtio_sandbox_reset(struct udevice *udev) +{ + struct virtio_sandbox_priv *priv = dev_get_priv(udev); + + /* 0 status means a reset */ + priv->status = 0; + + return 0; +} + +static int virtio_sandbox_get_features(struct udevice *udev, u64 *features) +{ + struct virtio_sandbox_priv *priv = dev_get_priv(udev); + + *features = priv->device_features; + + return 0; +} + +static int virtio_sandbox_set_features(struct udevice *udev) +{ + struct virtio_sandbox_priv *priv = dev_get_priv(udev); + struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(udev); + + priv->driver_features = uc_priv->features; + + return 0; +} + +static struct virtqueue *virtio_sandbox_setup_vq(struct udevice *udev, + unsigned int index) +{ + struct virtio_sandbox_priv *priv = dev_get_priv(udev); + struct virtqueue *vq; + ulong addr; + int err; + + /* Create the vring */ + vq = vring_create_virtqueue(index, 4, 4096, udev); + if (!vq) { + err = -ENOMEM; + goto error_new_virtqueue; + } + + addr = virtqueue_get_desc_addr(vq); + priv->queue_desc = addr; + + addr = virtqueue_get_avail_addr(vq); + priv->queue_available = addr; + + addr = virtqueue_get_used_addr(vq); + priv->queue_used = addr; + + return vq; + +error_new_virtqueue: + return ERR_PTR(err); +} + +static void virtio_sandbox_del_vq(struct virtqueue *vq) +{ + vring_del_virtqueue(vq); +} + +static int virtio_sandbox_del_vqs(struct udevice *udev) +{ + struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(udev); + struct virtqueue *vq, *n; + + list_for_each_entry_safe(vq, n, &uc_priv->vqs, list) + virtio_sandbox_del_vq(vq); + + return 0; +} + +static int virtio_sandbox_find_vqs(struct udevice *udev, unsigned int nvqs, + struct virtqueue *vqs[]) +{ + int i; + + for (i = 0; i < nvqs; ++i) { + vqs[i] = virtio_sandbox_setup_vq(udev, i); + if (IS_ERR(vqs[i])) { + virtio_sandbox_del_vqs(udev); + return PTR_ERR(vqs[i]); + } + } + + return 0; +} + +static int virtio_sandbox_notify(struct udevice *udev, struct virtqueue *vq) +{ + return 0; +} + +static int virtio_sandbox_probe(struct udevice *udev) +{ + struct virtio_sandbox_priv *priv = dev_get_priv(udev); + struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(udev); + + /* fake some information for testing */ + priv->device_features = VIRTIO_F_VERSION_1; + uc_priv->device = VIRTIO_ID_BLOCK; + uc_priv->vendor = ('u' << 24) | ('b' << 16) | ('o' << 8) | 't'; + + return 0; +} + +/* check virtio device driver's remove routine was called to reset the device */ +static int virtio_sandbox_child_post_remove(struct udevice *vdev) +{ + u8 status; + + virtio_get_status(vdev, &status); + if (status) + panic("virtio device was not reset\n"); + + return 0; +} + +static const struct dm_virtio_ops virtio_sandbox1_ops = { + .get_config = virtio_sandbox_get_config, + .set_config = virtio_sandbox_set_config, + .get_status = virtio_sandbox_get_status, + .set_status = virtio_sandbox_set_status, + .reset = virtio_sandbox_reset, + .get_features = virtio_sandbox_get_features, + .set_features = virtio_sandbox_set_features, + .find_vqs = virtio_sandbox_find_vqs, + .del_vqs = virtio_sandbox_del_vqs, + .notify = virtio_sandbox_notify, +}; + +static const struct udevice_id virtio_sandbox1_ids[] = { + { .compatible = "sandbox,virtio1" }, + { } +}; + +U_BOOT_DRIVER(virtio_sandbox1) = { + .name = "virtio-sandbox1", + .id = UCLASS_VIRTIO, + .of_match = virtio_sandbox1_ids, + .ops = &virtio_sandbox1_ops, + .probe = virtio_sandbox_probe, + .child_post_remove = virtio_sandbox_child_post_remove, + .priv_auto_alloc_size = sizeof(struct virtio_sandbox_priv), +}; + +/* this one without notify op */ +static const struct dm_virtio_ops virtio_sandbox2_ops = { + .get_config = virtio_sandbox_get_config, + .set_config = virtio_sandbox_set_config, + .get_status = virtio_sandbox_get_status, + .set_status = virtio_sandbox_set_status, + .reset = virtio_sandbox_reset, + .get_features = virtio_sandbox_get_features, + .set_features = virtio_sandbox_set_features, + .find_vqs = virtio_sandbox_find_vqs, + .del_vqs = virtio_sandbox_del_vqs, +}; + +static const struct udevice_id virtio_sandbox2_ids[] = { + { .compatible = "sandbox,virtio2" }, + { } +}; + +U_BOOT_DRIVER(virtio_sandbox2) = { + .name = "virtio-sandbox2", + .id = UCLASS_VIRTIO, + .of_match = virtio_sandbox2_ids, + .ops = &virtio_sandbox2_ops, + .probe = virtio_sandbox_probe, + .priv_auto_alloc_size = sizeof(struct virtio_sandbox_priv), +}; From patchwork Mon Oct 15 09:21:26 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bin Meng X-Patchwork-Id: 984051 X-Patchwork-Delegate: sjg@chromium.org Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=lists.denx.de (client-ip=81.169.180.215; helo=lists.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="Q1pI1awp"; dkim-atps=neutral Received: from lists.denx.de (dione.denx.de [81.169.180.215]) by ozlabs.org (Postfix) with ESMTP id 42YYKC526gz9s5c for ; Mon, 15 Oct 2018 20:38:23 +1100 (AEDT) Received: by lists.denx.de (Postfix, from userid 105) id 5AFD8C21CB1; Mon, 15 Oct 2018 09:34:24 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on lists.denx.de X-Spam-Level: X-Spam-Status: No, score=-0.0 required=5.0 tests=FREEMAIL_FROM, RCVD_IN_DNSWL_BLOCKED, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, T_DKIM_INVALID autolearn=unavailable autolearn_force=no version=3.4.0 Received: from lists.denx.de (localhost [IPv6:::1]) by lists.denx.de (Postfix) with ESMTP id 5ED0AC21F6A; Mon, 15 Oct 2018 09:17:36 +0000 (UTC) Received: by lists.denx.de (Postfix, from userid 105) id E9E84C21E75; Mon, 15 Oct 2018 09:17:15 +0000 (UTC) Received: from mail-pg1-f194.google.com (mail-pg1-f194.google.com [209.85.215.194]) by lists.denx.de (Postfix) with ESMTPS id 1DA5EC21ECE for ; Mon, 15 Oct 2018 09:17:09 +0000 (UTC) Received: by mail-pg1-f194.google.com with SMTP id c10-v6so8877464pgq.4 for ; Mon, 15 Oct 2018 02:17:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=+XtTlQsYCnYpliLMruBXk/7t5Wb/AHsASl2HUnaXTvc=; b=Q1pI1awp3pBEKjWv+niMruiG3C0PnT0k/4kQihNVRVdwrf0T9xCDU32Bmf9vVHu0Bg J1xfVnSJRn1lJksD449tzw82zz79uRSVGjmeXfp6GvtBmqr1/IJ/B9ybAOeUO9IjYGkO ZlDtByDwiFcWvz+9Br/90Dh7wm+X/FiBBoWx6eX7hVFMsYA46/n4H0XYeSU8IQGdY7HL CmA5dsqm5bKoqfftolLWHuMSvR1dSvVPa3u3jcP/NmIHG2gGIuF29VS0zKK+0p+HeCHc 6x7/XX02WZ883yELOnzKFSTeuAnD633BODBWRVShWFOE9/vdJmCX2lqmAhWcWct8uTg8 1O0Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=+XtTlQsYCnYpliLMruBXk/7t5Wb/AHsASl2HUnaXTvc=; b=tYs52pV+PY/LiSzpuQh0rO7BacC1lX/4/tVIZGEwNPc97F600H9+MzjbejIpuKXr5E nSF7p4Ns+/tsh/trxZe3Pf8MeIHmYfrIa0c21fnioBHq78RYoWL593L/M0N+N7x/oJFC gt972uvhCnOJNTpuqBarwMGugKMlYE2VL3jEbGgIHcBbGnf68TLBtc4in2+dT/bikeT2 TzLJ0WYrgUNsCiiIYGBmzxSV+KRswPmZXMOKlUbp8jmronQ+66UALYuTHYsLZS5dQn2s BsduIcEcMZAgKBW1ehHbZ8/RdZ1JR/hSe3xrf0sz1Kp3sqGLMGQeC14kB6wSvbjArVyR eaag== X-Gm-Message-State: ABuFfojaQxxEIkdupCcRKAMOfRj4VvyEZK8qiDwah6yH+f7qas77WD/Q tOVq6Y9tJrjHuQhTXEr+PIg= X-Google-Smtp-Source: ACcGV60dHMW7HK4vUYQaOP/4vJJxPdjsmemwg/Sf3VqKGMMQTkTFxms6PkeOEeMAX8/LHfKxQaifwA== X-Received: by 2002:a63:7b09:: with SMTP id w9-v6mr15396314pgc.385.1539595027704; Mon, 15 Oct 2018 02:17:07 -0700 (PDT) Received: from ala-d2121-lx1.wrs.com (unknown-156-139.windriver.com. [147.11.156.139]) by smtp.gmail.com with ESMTPSA id a11-v6sm10798256pgw.54.2018.10.15.02.17.06 (version=TLS1_1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 15 Oct 2018 02:17:06 -0700 (PDT) From: Bin Meng To: Simon Glass , U-Boot Mailing List Date: Mon, 15 Oct 2018 02:21:26 -0700 Message-Id: <1539595287-31378-31-git-send-email-bmeng.cn@gmail.com> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: <1539595287-31378-1-git-send-email-bmeng.cn@gmail.com> References: <1539595287-31378-1-git-send-email-bmeng.cn@gmail.com> Subject: [U-Boot] [PATCH v3 30/31] test: dm: virtio: Add test cases for virtio uclass X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.18 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" Now that we have a sandbox virtio transport driver, add some test cases to test virtio uclass driver. Signed-off-by: Bin Meng Reviewed-by: Simon Glass Reviewed-by: Simon Glass --- Changes in v3: - Exclude VIRTIO_BLK in sandbox_noblk_defconfig build Changes in v2: - new patch to add test cases for virtio uclass arch/Kconfig | 5 ++ arch/sandbox/dts/test.dts | 8 +++ configs/sandbox_noblk_defconfig | 1 + test/dm/Makefile | 1 + test/dm/virtio.c | 122 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 137 insertions(+) create mode 100644 test/dm/virtio.c diff --git a/arch/Kconfig b/arch/Kconfig index e822a0b..9fdd2f7 100644 --- a/arch/Kconfig +++ b/arch/Kconfig @@ -110,6 +110,11 @@ config SANDBOX imply LIBAVB imply CMD_AVB imply UDP_FUNCTION_FASTBOOT + imply VIRTIO_MMIO + imply VIRTIO_PCI + imply VIRTIO_SANDBOX + imply VIRTIO_BLK + imply VIRTIO_NET config SH bool "SuperH architecture" diff --git a/arch/sandbox/dts/test.dts b/arch/sandbox/dts/test.dts index c52f5a9..47e0646 100644 --- a/arch/sandbox/dts/test.dts +++ b/arch/sandbox/dts/test.dts @@ -715,6 +715,14 @@ sandbox_tee { compatible = "sandbox,tee"; }; + + sandbox_virtio1 { + compatible = "sandbox,virtio1"; + }; + + sandbox_virtio2 { + compatible = "sandbox,virtio2"; + }; }; #include "sandbox_pmic.dtsi" diff --git a/configs/sandbox_noblk_defconfig b/configs/sandbox_noblk_defconfig index 93b2240..e71e2d3 100644 --- a/configs/sandbox_noblk_defconfig +++ b/configs/sandbox_noblk_defconfig @@ -171,6 +171,7 @@ CONFIG_CONSOLE_TRUETYPE_CANTORAONE=y CONFIG_VIDEO_SANDBOX_SDL=y CONFIG_OSD=y CONFIG_SANDBOX_OSD=y +# CONFIG_VIRTIO_BLK is not set CONFIG_FS_CBFS=y CONFIG_FS_CRAMFS=y CONFIG_CMD_DHRYSTONE=y diff --git a/test/dm/Makefile b/test/dm/Makefile index b490cf2..213e0fd 100644 --- a/test/dm/Makefile +++ b/test/dm/Makefile @@ -53,4 +53,5 @@ obj-$(CONFIG_MISC) += misc.o obj-$(CONFIG_DM_SERIAL) += serial.o obj-$(CONFIG_CPU) += cpu.o obj-$(CONFIG_TEE) += tee.o +obj-$(CONFIG_VIRTIO_SANDBOX) += virtio.o endif diff --git a/test/dm/virtio.c b/test/dm/virtio.c new file mode 100644 index 0000000..4b317d2 --- /dev/null +++ b/test/dm/virtio.c @@ -0,0 +1,122 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * Copyright (C) 2018, Bin Meng + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* Basic test of the virtio uclass */ +static int dm_test_virtio_base(struct unit_test_state *uts) +{ + struct udevice *bus, *dev; + u8 status; + + /* check probe success */ + ut_assertok(uclass_first_device(UCLASS_VIRTIO, &bus)); + + /* check the child virtio-blk device is bound */ + ut_assertok(device_find_first_child(bus, &dev)); + ut_assertok(strcmp(dev->name, "virtio-blk#0")); + + /* check driver status */ + ut_assertok(virtio_get_status(dev, &status)); + ut_asserteq(VIRTIO_CONFIG_S_ACKNOWLEDGE, status); + + return 0; +} +DM_TEST(dm_test_virtio_base, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); + +/* Test all of the virtio uclass ops */ +static int dm_test_virtio_all_ops(struct unit_test_state *uts) +{ + struct udevice *bus, *dev; + struct virtio_dev_priv *uc_priv; + uint offset = 0, len = 0, nvqs = 1; + void *buffer = NULL; + u8 status; + u32 counter; + u64 features; + struct virtqueue *vqs[2]; + + /* check probe success */ + ut_assertok(uclass_first_device(UCLASS_VIRTIO, &bus)); + + /* check the child virtio-blk device is bound */ + ut_assertok(device_find_first_child(bus, &dev)); + + /* + * fake the virtio device probe by filling in uc_priv->vdev + * which is used by virtio_find_vqs/virtio_del_vqs. + */ + uc_priv = dev_get_uclass_priv(bus); + uc_priv->vdev = dev; + + /* test virtio_xxx APIs */ + ut_assertok(virtio_get_config(dev, offset, buffer, len)); + ut_assertok(virtio_set_config(dev, offset, buffer, len)); + ut_asserteq(-ENOSYS, virtio_generation(dev, &counter)); + ut_assertok(virtio_set_status(dev, VIRTIO_CONFIG_S_DRIVER_OK)); + ut_assertok(virtio_get_status(dev, &status)); + ut_asserteq(VIRTIO_CONFIG_S_DRIVER_OK, status); + ut_assertok(virtio_reset(dev)); + ut_assertok(virtio_get_status(dev, &status)); + ut_asserteq(0, status); + ut_assertok(virtio_get_features(dev, &features)); + ut_asserteq(VIRTIO_F_VERSION_1, features); + ut_assertok(virtio_set_features(dev)); + ut_assertok(virtio_find_vqs(dev, nvqs, vqs)); + ut_assertok(virtio_del_vqs(dev)); + ut_assertok(virtio_notify(dev, vqs[0])); + + return 0; +} +DM_TEST(dm_test_virtio_all_ops, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); + +/* Test of the virtio driver that does not have required driver ops */ +static int dm_test_virtio_missing_ops(struct unit_test_state *uts) +{ + struct udevice *bus; + + /* find the virtio device */ + ut_assertok(uclass_find_device(UCLASS_VIRTIO, 1, &bus)); + + /* + * Probe the device should fail with error -ENOENT. + * See ops check in virtio_uclass_pre_probe(). + */ + ut_asserteq(-ENOENT, device_probe(bus)); + + return 0; +} +DM_TEST(dm_test_virtio_missing_ops, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); + +/* Test removal of virtio device driver */ +static int dm_test_virtio_remove(struct unit_test_state *uts) +{ + struct udevice *bus, *dev; + + /* check probe success */ + ut_assertok(uclass_first_device(UCLASS_VIRTIO, &bus)); + + /* check the child virtio-blk device is bound */ + ut_assertok(device_find_first_child(bus, &dev)); + + /* set driver status to VIRTIO_CONFIG_S_DRIVER_OK */ + ut_assertok(virtio_set_status(dev, VIRTIO_CONFIG_S_DRIVER_OK)); + + /* check the device can be successfully removed */ + dev->flags |= DM_FLAG_ACTIVATED; + ut_assertok(device_remove(bus, DM_REMOVE_ACTIVE_ALL)); + + return 0; +} +DM_TEST(dm_test_virtio_remove, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); From patchwork Mon Oct 15 09:21:27 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bin Meng X-Patchwork-Id: 984049 X-Patchwork-Delegate: sjg@chromium.org Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=lists.denx.de (client-ip=81.169.180.215; helo=lists.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="Xt+NP5aP"; dkim-atps=neutral Received: from lists.denx.de (dione.denx.de [81.169.180.215]) by ozlabs.org (Postfix) with ESMTP id 42YYHr6g1mz9s5c for ; Mon, 15 Oct 2018 20:37:12 +1100 (AEDT) Received: by lists.denx.de (Postfix, from userid 105) id 9E761C21E3E; Mon, 15 Oct 2018 09:28:59 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on lists.denx.de X-Spam-Level: X-Spam-Status: No, score=-0.0 required=5.0 tests=FREEMAIL_FROM, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, T_DKIM_INVALID autolearn=unavailable autolearn_force=no version=3.4.0 Received: from lists.denx.de (localhost [IPv6:::1]) by lists.denx.de (Postfix) with ESMTP id 713ACC21F19; Mon, 15 Oct 2018 09:17:28 +0000 (UTC) Received: by lists.denx.de (Postfix, from userid 105) id D452CC21F32; Mon, 15 Oct 2018 09:17:17 +0000 (UTC) Received: from mail-pf1-f173.google.com (mail-pf1-f173.google.com [209.85.210.173]) by lists.denx.de (Postfix) with ESMTPS id 71F6DC21DDC for ; Mon, 15 Oct 2018 09:17:10 +0000 (UTC) Received: by mail-pf1-f173.google.com with SMTP id f29-v6so9369746pff.5 for ; Mon, 15 Oct 2018 02:17:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=VebdyNgD42tLyyjZFJ4Ja+5RiYpeJ8ZlvrUA82CvoHQ=; b=Xt+NP5aPE5zg03oIKLTAylBimJ85cgWIlQ8xwgbACGZ5QLHSBh1Tc4BEiWwLVhOrif gjSmWwderClzi1FAtgmwOw8FtteW8RXbWHmk0W2VIxnMiyqAsIlvCNKLQvkjvE4MnJP0 jHOrNOZ1Qh7TTY7AWam+24VleWkpjo1wgqrZyp/7xNNCODuV9NEv0utdZfmxyYP1zsEK 6t9Z4XgdmArbm6SYa/s5zSqKC27ZSziH9lBXmxNakELHmDEkkcIXvUuieTycxxj5POpf G2GgEP46wcgmHjC55PJd6sdHM3rNKihViMRCc+CbL+h6kPwaXzA5HqMvLI1q54Cu1lWP 8qjg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=VebdyNgD42tLyyjZFJ4Ja+5RiYpeJ8ZlvrUA82CvoHQ=; b=kANe9BwQgzxeglK/RGYihu0PYxUoyzLGtbujgI9kGLfcL8ZvJnvRzMv5hP6WRacMQ0 r8FsHRFk4/VQbOrxJFsVJJxd7Vc//LqREDmFNp1FtxwGWyupsCD1Ttdl2mYFEPnW8Ow9 3vkTZaesQYUfmDepCvLet6Ah/aqx4GouzZ4dnNt7AUWBroAMubAq2xbKhTuxAjYidlY/ bmAL/kpOLiVdlsHCH/b87267i6U1f6P+x4UnFirr3/WYU5r7wOT01VlBuw1ZCWrFW8R0 7bk0MJ1Jbn4Qn8OJWmj0VYnsykKxuevoZUFYr+/e54Qfrv72Gqy5GaXnm2AzOLMgH22L /QzQ== X-Gm-Message-State: ABuFfogaV/idSKknwAmdiyCYegEzCV9YYYYUA7SquwQi9q6h4OAmA5NL UUwPWhY/IFztJkPx6FaGjQU= X-Google-Smtp-Source: ACcGV60ok4MzB98kdLdjGknA+Y2s1p1GjR/2D3rYwD6AudpFKRMUYpZSkvGO6Wz3aSOWpbrtZv+HTA== X-Received: by 2002:a63:f848:: with SMTP id v8-v6mr15134615pgj.82.1539595028930; Mon, 15 Oct 2018 02:17:08 -0700 (PDT) Received: from ala-d2121-lx1.wrs.com (unknown-156-139.windriver.com. [147.11.156.139]) by smtp.gmail.com with ESMTPSA id a11-v6sm10798256pgw.54.2018.10.15.02.17.07 (version=TLS1_1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 15 Oct 2018 02:17:08 -0700 (PDT) From: Bin Meng To: Simon Glass , U-Boot Mailing List Date: Mon, 15 Oct 2018 02:21:27 -0700 Message-Id: <1539595287-31378-32-git-send-email-bmeng.cn@gmail.com> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: <1539595287-31378-1-git-send-email-bmeng.cn@gmail.com> References: <1539595287-31378-1-git-send-email-bmeng.cn@gmail.com> Subject: [U-Boot] [PATCH v3 31/31] doc: Document virtio support X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.18 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" Add REAME.virtio to describe the information about U-Boot support for VirtIO devices, including supported boards, build instructions, driver details etc. Signed-off-by: Bin Meng Reviewed-by: Simon Glass Signed-off-by: Bin Meng Reviewed-by: Simon Glass --- Changes in v3: None Changes in v2: - Added driver remove and flags description in the documentation doc/README.virtio | 253 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 253 insertions(+) create mode 100644 doc/README.virtio diff --git a/doc/README.virtio b/doc/README.virtio new file mode 100644 index 0000000..d3652f2 --- /dev/null +++ b/doc/README.virtio @@ -0,0 +1,253 @@ +# SPDX-License-Identifier: GPL-2.0+ +# +# Copyright (C) 2018, Bin Meng + +VirtIO Support +============== + +This document describes the information about U-Boot support for VirtIO [1] +devices, including supported boards, build instructions, driver details etc. + +What's VirtIO? +-------------- +VirtIO is a virtualization standard for network and disk device drivers where +just the guest's device driver "knows" it is running in a virtual environment, +and cooperates with the hypervisor. This enables guests to get high performance +network and disk operations, and gives most of the performance benefits of +paravirtualization. In the U-Boot case, the guest is U-Boot itself, while the +virtual environment are normally QEMU [2] targets like ARM, RISC-V and x86. + +Status +------ +VirtIO can use various different buses, aka transports as described in the +spec. While VirtIO devices are commonly implemented as PCI devices on x86, +embedded devices models like ARM/RISC-V, which does not normally come with +PCI support might use simple memory mapped device (MMIO) instead of the PCI +device. The memory mapped virtio device behaviour is based on the PCI device +specification. Therefore most operations including device initialization, +queues configuration and buffer transfers are nearly identical. Both MMIO +and PCI transport options are supported in U-Boot. + +The VirtIO spec defines a lots of VirtIO device types, however at present only +network and block device, the most two commonly used devices, are supported. + +The following QEMU targets are supported. + + - qemu_arm_defconfig + - qemu_arm64_defconfig + - qemu-riscv32_defconfig + - qemu-riscv64_defconfig + - qemu-x86_defconfig + - qemu-x86_64_defconfig + +Note ARM and RISC-V targets are configured with VirtIO MMIO transport driver, +and on x86 it's the PCI transport driver. + +Build Instructions +------------------ +Building U-Boot for pre-configured QEMU targets is no different from others. +For example, we can do the following with the CROSS_COMPILE environment +variable being properly set to a working toolchain for ARM: + + $ make qemu_arm_defconfig + $ make + +You can even create a QEMU ARM target with VirtIO devices showing up on both +MMIO and PCI buses. In this case, you can enable the PCI transport driver +from 'make menuconfig': + +Device Drivers ---> + ... + VirtIO Drivers ---> + ... + [*] PCI driver for virtio devices + +Other drivers are at the same location and can be tuned to suit the needs. + +Requirements +------------ +It is required that QEMU v2.5.0+ should be used to test U-Boot VirtIO support +on QEMU ARM and x86, and v2.12.0+ on QEMU RISC-V. + +Testing +------- +The following QEMU command line is used to get U-Boot up and running with +VirtIO net and block devices on ARM. + + $ qemu-system-arm -nographic -machine virt -bios u-boot.bin \ + -netdev tap,ifname=tap0,id=net0 \ + -device virtio-net-device,netdev=net0 \ + -drive if=none,file=test.img,format=raw,id=hd0 \ + -device virtio-blk-device,drive=hd0 + +On x86, command is slightly different to create PCI VirtIO devices. + + $ qemu-system-i386 -nographic -bios u-boot.rom \ + -netdev tap,ifname=tap0,id=net0 \ + -device virtio-net-pci,netdev=net0 \ + -drive if=none,file=test.img,format=raw,id=hd0 \ + -device virtio-blk-pci,drive=hd0 + +Additional net and block devices can be created by appending more '-device' +parameters. It is also possible to specify both MMIO and PCI VirtIO devices. +For example, the following commnad creates 3 VirtIO devices, with 1 on MMIO +and 2 on PCI bus. + + $ qemu-system-arm -nographic -machine virt -bios u-boot.bin \ + -netdev tap,ifname=tap0,id=net0 \ + -device virtio-net-pci,netdev=net0 \ + -drive if=none,file=test0.img,format=raw,id=hd0 \ + -device virtio-blk-device,drive=hd0 \ + -drive if=none,file=test1.img,format=raw,id=hd1 \ + -device virtio-blk-pci,drive=hd1 + +By default QEMU creates VirtIO legacy devices by default. To create non-legacy +(aka modern) devices, pass additional device property/value pairs like below: + + $ qemu-system-i386 -nographic -bios u-boot.rom \ + -netdev tap,ifname=tap0,id=net0 \ + -device virtio-net-pci,netdev=net0,disable-legacy=true,disable-modern=false \ + -drive if=none,file=test.img,format=raw,id=hd0 \ + -device virtio-blk-pci,drive=hd0,disable-legacy=true,disable-modern=false + +A 'virtio' command is provided in U-Boot shell. + + => virtio + virtio - virtio block devices sub-system + + Usage: + virtio scan - initialize virtio bus + virtio info - show all available virtio block devices + virtio device [dev] - show or set current virtio block device + virtio part [dev] - print partition table of one or all virtio block devices + virtio read addr blk# cnt - read `cnt' blocks starting at block + `blk#' to memory address `addr' + virtio write addr blk# cnt - write `cnt' blocks starting at block + `blk#' from memory address `addr' + +To probe all the VirtIO devices, type: + + => virtio scan + +Then we can show the connected block device details by: + + => virtio info + Device 0: QEMU VirtIO Block Device + Type: Hard Disk + Capacity: 4096.0 MB = 4.0 GB (8388608 x 512) + +And list the directories and files on the disk by: + + => ls virtio 0 / + 4096 . + 4096 .. + 16384 lost+found + 4096 dev + 4096 proc + 4096 sys + 4096 var + 4096 etc + 4096 usr + 7 bin + 8 sbin + 7 lib + 9 lib64 + 4096 run + 4096 boot + 4096 home + 4096 media + 4096 mnt + 4096 opt + 4096 root + 4096 srv + 4096 tmp + 0 .autorelabel + +Driver Internals +---------------- +There are 3 level of drivers in the VirtIO driver family. + + +---------------------------------------+ + | virtio device drivers | + | +-------------+ +------------+ | + | | virtio-net | | virtio-blk | | + | +-------------+ +------------+ | + +---------------------------------------+ + +---------------------------------------+ + | virtio transport drivers | + | +-------------+ +------------+ | + | | virtio-mmio | | virtio-pci | | + | +-------------+ +------------+ | + +---------------------------------------+ + +----------------------+ + | virtio uclass driver | + +----------------------+ + +The root one is the virtio uclass driver (virtio-uclass.c), which does lots of +common stuff for the transport drivers (virtio_mmio.c, virtio_pci.c). The real +virtio device is discovered in the transport driver's probe() method, and its +device ID is saved in the virtio uclass's private data of the transport device. +Then in the virtio uclass's post_probe() method, the real virtio device driver +(virtio_net.c, virtio_blk.c) is bound if there is a match on the device ID. + +The child_post_bind(), child_pre_probe() and child_post_probe() methods of the +virtio uclass driver help bring the virtio device driver online. They do things +like acknowledging device, feature negotiation, etc, which are really common +for all virtio devices. + +The transport drivers provide a set of ops (struct dm_virtio_ops) for the real +virtio device driver to call. These ops APIs's parameter is designed to remind +the caller to pass the correct 'struct udevice' id of the virtio device, eg: + +int virtio_get_status(struct udevice *vdev, u8 *status) + +So the parameter 'vdev' indicates the device should be the real virtio device. +But we also have an API like: + +struct virtqueue *vring_create_virtqueue(unsigned int index, unsigned int num, + unsigned int vring_align, + struct udevice *udev) + +Here the parameter 'udev' indicates the device should be the transport device. +Similar naming is applied in other functions that are even not APIs, eg: + +static int virtio_uclass_post_probe(struct udevice *udev) +static int virtio_uclass_child_pre_probe(struct udevice *vdev) + +So it's easy to tell which device these functions are operating on. + +Development Flow +---------------- +At present only VirtIO network card (device ID 1) and block device (device +ID 2) are supported. If you want to develop new driver for new devices, +please follow the guideline below. + +1. add new device ID in virtio.h +#define VIRTIO_ID_XXX X + +2. update VIRTIO_ID_MAX_NUM to be the largest device ID plus 1 + +3. add new driver name string in virtio.h +#define VIRTIO_XXX_DRV_NAME "virtio-xxx" + +4. create a new driver with name set to the name string above +U_BOOT_DRIVER(virtio_xxx) = { + .name = VIRTIO_XXX_DRV_NAME, + ... + .remove = virtio_reset, + .flags = DM_FLAG_ACTIVE_DMA, +} + +Note the driver needs to provide the remove method and normally this can be +hooked to virtio_reset(). The driver flags should contain DM_FLAG_ACTIVE_DMA +for the remove method to be called before jumping to OS. + +5. provide bind() method in the driver, where virtio_driver_features_init() + should be called for driver to negotiate feature support with the device. + +6. do funny stuff with the driver + +References +---------- +[1] http://docs.oasis-open.org/virtio/virtio/v1.0/virtio-v1.0.pdf +[2] https://www.qemu.org