From: muli@il.ibm.com Subject: [PATCH 6/6] device assignment: support for hot-plugging PCI devices Date: Wed, 29 Oct 2008 12:22:21 +0200 Message-ID: <1225275741-15285-7-git-send-email-muli@il.ibm.com> References: <1225275741-15285-1-git-send-email-muli@il.ibm.com> <1225275741-15285-2-git-send-email-muli@il.ibm.com> <1225275741-15285-3-git-send-email-muli@il.ibm.com> <1225275741-15285-4-git-send-email-muli@il.ibm.com> <1225275741-15285-5-git-send-email-muli@il.ibm.com> <1225275741-15285-6-git-send-email-muli@il.ibm.com> Cc: kvm@vger.kernel.org, anthony@codemonkey.ws, weidong.han@intel.com, benami@il.ibm.com, muli@il.ibm.com, amit.shah@redhat.com, allen.m.kay@intel.com To: avi@redhat.com Return-path: Received: from mtagate7.de.ibm.com ([195.212.29.156]:43652 "EHLO mtagate7.de.ibm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752779AbYJ2KXT (ORCPT ); Wed, 29 Oct 2008 06:23:19 -0400 Received: from d12nrmr1607.megacenter.de.ibm.com (d12nrmr1607.megacenter.de.ibm.com [9.149.167.49]) by mtagate7.de.ibm.com (8.13.8/8.13.8) with ESMTP id m9TAMPpO584760 for ; Wed, 29 Oct 2008 10:22:25 GMT Received: from d12av03.megacenter.de.ibm.com (d12av03.megacenter.de.ibm.com [9.149.165.213]) by d12nrmr1607.megacenter.de.ibm.com (8.13.8/8.13.8/NCO v9.1) with ESMTP id m9TAMPtX3838062 for ; Wed, 29 Oct 2008 11:22:25 +0100 Received: from d12av03.megacenter.de.ibm.com (loopback [127.0.0.1]) by d12av03.megacenter.de.ibm.com (8.12.11.20060308/8.13.3) with ESMTP id m9TAMOX7010539 for ; Wed, 29 Oct 2008 11:22:25 +0100 In-Reply-To: <1225275741-15285-6-git-send-email-muli@il.ibm.com> Sender: kvm-owner@vger.kernel.org List-ID: From: Amit Shah This patch adds support for hot-plugging host PCI devices into guests [muli: various small fixes for review comments] Signed-off-by: Amit Shah Signed-off-by: Muli Ben-Yehuda --- qemu/hw/device-hotplug.c | 37 +++++++++++++++++++++++++++++++++++++ qemu/monitor.c | 2 +- 2 files changed, 38 insertions(+), 1 deletions(-) diff --git a/qemu/hw/device-hotplug.c b/qemu/hw/device-hotplug.c index 8e2bc35..ba1b161 100644 --- a/qemu/hw/device-hotplug.c +++ b/qemu/hw/device-hotplug.c @@ -6,6 +6,8 @@ #include "pc.h" #include "console.h" #include "block_int.h" +#include "device-assignment.h" +#include "config.h" #define PCI_BASE_CLASS_STORAGE 0x01 #define PCI_BASE_CLASS_NETWORK 0x02 @@ -27,6 +29,37 @@ static PCIDevice *qemu_system_hot_add_nic(const char *opts, int bus_nr) return pci_nic_init (pci_bus, &nd_table[ret], -1); } +#ifdef USE_KVM_DEVICE_ASSIGNMENT +static PCIDevice *qemu_system_hot_assign_device(const char *opts, int bus_nr) +{ + PCIBus *pci_bus; + AssignedDevInfo *adev; + PCIDevice *ret; + + pci_bus = pci_find_bus(bus_nr); + if (!pci_bus) { + term_printf ("Can't find pci_bus %d\n", bus_nr); + return NULL; + } + adev = add_assigned_device(opts); + if (adev == NULL) { + term_printf ("Error adding device; check syntax\n"); + return NULL; + } + + ret = init_assigned_device(adev, pci_bus); + + term_printf("Registered host PCI device %02x:%02x.%1x " + "(\"%s\") as guest device %02x:%02x.%1x\n", + adev->bus, adev->dev, adev->func, adev->name, + pci_bus_num(pci_bus), (ret->devfn >> 3) & 0x1f, + adev->func); + + return ret; +} + +#endif /* USE_KVM_DEVICE_ASSIGNMENT */ + static int add_init_drive(const char *opts) { int drive_opt_idx, drive_idx; @@ -143,6 +176,10 @@ void device_hot_add(int pcibus, const char *type, const char *opts) dev = qemu_system_hot_add_nic(opts, pcibus); else if (strcmp(type, "storage") == 0) dev = qemu_system_hot_add_storage(opts, pcibus); +#ifdef USE_KVM_DEVICE_ASSIGNMENT + else if (strcmp(type, "host") == 0) + dev = qemu_system_hot_assign_device(opts, pcibus); +#endif /* USE_KVM_DEVICE_ASSIGNMENT */ else term_printf("invalid type: %s\n", type); diff --git a/qemu/monitor.c b/qemu/monitor.c index 79b6b4c..d1043b1 100644 --- a/qemu/monitor.c +++ b/qemu/monitor.c @@ -1529,7 +1529,7 @@ static const term_cmd_t term_cmds[] = { "[,cyls=c,heads=h,secs=s[,trans=t]]\n" "[snapshot=on|off][,cache=on|off]", "add drive to PCI storage controller" }, - { "pci_add", "iss", device_hot_add, "bus nic|storage [[vlan=n][,macaddr=addr][,model=type]] [file=file][,if=type][,bus=nr]...", "hot-add PCI device" }, + { "pci_add", "iss", device_hot_add, "bus nic|storage|host [[vlan=n][,macaddr=addr][,model=type]] [file=file][,if=type][,bus=nr]... [host=02:00.0[,name=string][,dma=none]" "hot-add PCI device" }, { "pci_del", "ii", device_hot_remove, "bus slot-number", "hot remove PCI device" }, #endif { "balloon", "i", do_balloon, -- 1.5.6.5