From patchwork Sat Mar 9 07:52:15 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Pandarathil, Vijaymohan R" X-Patchwork-Id: 226316 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id DF09B2C0322 for ; Sat, 9 Mar 2013 18:54:06 +1100 (EST) Received: from localhost ([::1]:48305 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UEEbV-00023p-7O for incoming@patchwork.ozlabs.org; Sat, 09 Mar 2013 02:54:05 -0500 Received: from eggs.gnu.org ([208.118.235.92]:48244) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UEEa9-0007wS-Vk for qemu-devel@nongnu.org; Sat, 09 Mar 2013 02:52:46 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1UEEa3-0007dx-Gb for qemu-devel@nongnu.org; Sat, 09 Mar 2013 02:52:41 -0500 Received: from g4t0014.houston.hp.com ([15.201.24.17]:41648) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UEEa3-0007cf-9l for qemu-devel@nongnu.org; Sat, 09 Mar 2013 02:52:35 -0500 Received: from g4t0009.houston.hp.com (g4t0009.houston.hp.com [16.234.32.26]) by g4t0014.houston.hp.com (Postfix) with ESMTP id 0674924166; Sat, 9 Mar 2013 07:52:29 +0000 (UTC) Received: from cumin.cce.hp.com (cumin.cce.hp.com [15.250.252.195]) by g4t0009.houston.hp.com (Postfix) with ESMTP id C3FA6C0D5; Sat, 9 Mar 2013 07:52:28 +0000 (UTC) From: Vijay Mohan Pandarathil To: alex.williamson@redhat.com, gleb@redhat.com, bhelgaas@google.com, blauwirbel@gmail.com, lance.oritz@hp.com, kvm@vger.kernel.org, qemu-devel@nongnu.org, linux-pci@vger.kernel.org, linux-kernel@vger.kernel.org Date: Sat, 9 Mar 2013 01:52:15 -0600 Message-Id: <1362815537-27303-2-git-send-email-vijaymohan.pandarathil@hp.com> X-Mailer: git-send-email 1.7.11.3 In-Reply-To: <1362815537-27303-1-git-send-email-vijaymohan.pandarathil@hp.com> References: <1362815537-27303-1-git-send-email-vijaymohan.pandarathil@hp.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.4.x X-Received-From: 15.201.24.17 Cc: Vijay Mohan Pandarathil Subject: [Qemu-devel] [PATCH v7 1/3] VFIO: Wrapper for getting reference to vfio_device from device X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org - Added vfio_device_get_from_dev() as wrapper to get reference to vfio_device from struct device. - Added vfio_device_data() as a wrapper to get device_data from vfio_device. Signed-off-by: Vijay Mohan Pandarathil --- drivers/vfio/vfio.c | 30 +++++++++++++++++++++++++++++- include/linux/vfio.h | 3 +++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/drivers/vfio/vfio.c b/drivers/vfio/vfio.c index fcc12f3..21eddd9 100644 --- a/drivers/vfio/vfio.c +++ b/drivers/vfio/vfio.c @@ -392,12 +392,13 @@ static void vfio_device_release(struct kref *kref) } /* Device reference always implies a group reference */ -static void vfio_device_put(struct vfio_device *device) +void vfio_device_put(struct vfio_device *device) { struct vfio_group *group = device->group; kref_put_mutex(&device->kref, vfio_device_release, &group->device_lock); vfio_group_put(group); } +EXPORT_SYMBOL_GPL(vfio_device_put); static void vfio_device_get(struct vfio_device *device) { @@ -627,6 +628,33 @@ int vfio_add_group_dev(struct device *dev, } EXPORT_SYMBOL_GPL(vfio_add_group_dev); +/** + * Get a reference to the vfio_device for a device that is known to + * be bound to a vfio driver. The driver implicitly holds a + * vfio_device reference between vfio_add_group_dev and + * vfio_del_group_dev. We can therefore use drvdata to increment + * that reference from the struct device. This additional + * reference must be released by calling vfio_device_put. + */ +struct vfio_device *vfio_device_get_from_dev(struct device *dev) +{ + struct vfio_device *device = dev_get_drvdata(dev); + + vfio_device_get(device); + + return device; +} +EXPORT_SYMBOL_GPL(vfio_device_get_from_dev); + +/* + * Caller must hold a reference to the vfio_device + */ +void *vfio_device_data(struct vfio_device *device) +{ + return device->device_data; +} +EXPORT_SYMBOL_GPL(vfio_device_data); + /* Given a referenced group, check if it contains the device */ static bool vfio_dev_present(struct vfio_group *group, struct device *dev) { diff --git a/include/linux/vfio.h b/include/linux/vfio.h index ab9e862..ac8d488 100644 --- a/include/linux/vfio.h +++ b/include/linux/vfio.h @@ -45,6 +45,9 @@ extern int vfio_add_group_dev(struct device *dev, void *device_data); extern void *vfio_del_group_dev(struct device *dev); +extern struct vfio_device *vfio_device_get_from_dev(struct device *dev); +extern void vfio_device_put(struct vfio_device *device); +extern void *vfio_device_data(struct vfio_device *device); /** * struct vfio_iommu_driver_ops - VFIO IOMMU driver callbacks