From patchwork Wed Oct 25 14:50:39 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Woodhouse X-Patchwork-Id: 13436347 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 68C52C0032E for ; Wed, 25 Oct 2023 14:51:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344472AbjJYOvm (ORCPT ); Wed, 25 Oct 2023 10:51:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58364 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344421AbjJYOvb (ORCPT ); Wed, 25 Oct 2023 10:51:31 -0400 Received: from casper.infradead.org (casper.infradead.org [IPv6:2001:8b0:10b:1236::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0195910D5 for ; Wed, 25 Oct 2023 07:51:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Sender:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description; bh=yQfwtPbREGreJZVtftdMEfHE+iyAsm6cKl0Oh4POK+Q=; b=Mip1iYJSsW63MhtMiW7lLRRtcN ur5UxOh/K0bhJvgyuwL3anhrKnfHaqrYx5ZJMdFB5jGn+RMD+HS7wtiHcoklHoFvdv2I43TTgm4TN kbQMmZY/q2QwvkEfSnvyMGAeZsQKkHKyqXNtvLtoA1o6dpir3gCSx6zkRP/hgW+wVh0OdOIXsuUYe +74CUSSgTmHxS5NFl3qIuMTHyLzhGKDdzUo9JKEtFq0vxXU9zdIfzsD8r79TPKaOSKhqd+hJpJhsQ 6ol8uqebpxD+CZwrgZBC26MmQdHCy/53gOLmG5HnyAqdEDJdxy72SrwmzynaKS5u9rW9ksT7Dyte/ F/41VQRA==; Received: from [2001:8b0:10b:1::ebe] (helo=i7.infradead.org) by casper.infradead.org with esmtpsa (Exim 4.94.2 #2 (Red Hat Linux)) id 1qvfDa-009Nms-1e; Wed, 25 Oct 2023 14:50:46 +0000 Received: from dwoodhou by i7.infradead.org with local (Exim 4.96 #2 (Red Hat Linux)) id 1qvfDZ-002dFg-23; Wed, 25 Oct 2023 15:50:45 +0100 From: David Woodhouse To: qemu-devel@nongnu.org Cc: Kevin Wolf , Hanna Reitz , Stefano Stabellini , Anthony Perard , Paul Durrant , =?utf-8?q?Marc-Andr=C3=A9_Lureau?= , Paolo Bonzini , "Michael S. Tsirkin" , Marcel Apfelbaum , Richard Henderson , Eduardo Habkost , Jason Wang , Marcelo Tosatti , qemu-block@nongnu.org, xen-devel@lists.xenproject.org, kvm@vger.kernel.org, Bernhard Beschow , Joel Upham Subject: [PATCH v3 25/28] hw/pci: add pci_init_nic_devices(), pci_init_nic_in_slot() Date: Wed, 25 Oct 2023 15:50:39 +0100 Message-Id: <20231025145042.627381-26-dwmw2@infradead.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20231025145042.627381-1-dwmw2@infradead.org> References: <20231025145042.627381-1-dwmw2@infradead.org> MIME-Version: 1.0 Sender: David Woodhouse X-SRS-Rewrite: SMTP reverse-path rewritten from by casper.infradead.org. See http://www.infradead.org/rpr.html Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: David Woodhouse The loop over nd_table[] to add PCI NICs is repeated in quite a few places. Add a helper function to do it. Some platforms also try to instantiate a specific model in a specific slot, to match the real hardware. Add pci_init_nic_in_slot() for that purpose. Signed-off-by: David Woodhouse Reviewed-by: Paul Durrant --- hw/pci/pci.c | 45 ++++++++++++++++++++++++++++++++++++++++++++ include/hw/pci/pci.h | 4 +++- 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/hw/pci/pci.c b/hw/pci/pci.c index 7d09e1a39d..51b0be3972 100644 --- a/hw/pci/pci.c +++ b/hw/pci/pci.c @@ -1925,6 +1925,51 @@ PCIDevice *pci_nic_init_nofail(NICInfo *nd, PCIBus *rootbus, return pci_dev; } +void pci_init_nic_devices(PCIBus *bus, const char *default_model) +{ + qemu_create_nic_bus_devices(&bus->qbus, TYPE_PCI_DEVICE, default_model, + "virtio", "virtio-net-pci"); +} + +bool pci_init_nic_in_slot(PCIBus *rootbus, const char *model, + const char *alias, const char *devaddr) +{ + NICInfo *nd = qemu_find_nic_info(model, true, alias); + int dom, busnr, devfn; + PCIDevice *pci_dev; + unsigned slot; + PCIBus *bus; + + if (!nd) { + return false; + } + + if (!devaddr || pci_parse_devaddr(devaddr, &dom, &busnr, &slot, NULL) < 0) { + error_report("Invalid PCI device address %s for device %s", + devaddr, model); + exit(1); + } + + if (dom != 0) { + error_report("No support for non-zero PCI domains"); + exit(1); + } + + devfn = PCI_DEVFN(slot, 0); + + bus = pci_find_bus_nr(rootbus, busnr); + if (!bus) { + error_report("Invalid PCI device address %s for device %s", + devaddr, model); + exit(1); + } + + pci_dev = pci_new(devfn, model); + qdev_set_nic_properties(&pci_dev->qdev, nd); + pci_realize_and_unref(pci_dev, bus, &error_fatal); + return true; +} + PCIDevice *pci_vga_init(PCIBus *bus) { vga_interface_created = true; diff --git a/include/hw/pci/pci.h b/include/hw/pci/pci.h index ea5aff118b..684d49bdcd 100644 --- a/include/hw/pci/pci.h +++ b/include/hw/pci/pci.h @@ -317,7 +317,9 @@ void pci_device_reset(PCIDevice *dev); PCIDevice *pci_nic_init_nofail(NICInfo *nd, PCIBus *rootbus, const char *default_model, const char *default_devaddr); - +void pci_init_nic_devices(PCIBus *bus, const char *default_model); +bool pci_init_nic_in_slot(PCIBus *rootbus, const char *default_model, + const char *alias, const char *devaddr); PCIDevice *pci_vga_init(PCIBus *bus); static inline PCIBus *pci_get_bus(const PCIDevice *dev)