From patchwork Fri Apr 29 12:11:55 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dong Jia Shi X-Patchwork-Id: 8982501 Return-Path: X-Original-To: patchwork-qemu-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id B3A1E9F46D for ; Fri, 29 Apr 2016 13:42:29 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 9E08120212 for ; Fri, 29 Apr 2016 13:42:28 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 89F83201CE for ; Fri, 29 Apr 2016 13:42:27 +0000 (UTC) Received: from localhost ([::1]:54535 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aw8gZ-0003vJ-H0 for patchwork-qemu-devel@patchwork.kernel.org; Fri, 29 Apr 2016 09:42:23 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:39994) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aw7IL-0001vg-MC for qemu-devel@nongnu.org; Fri, 29 Apr 2016 08:13:24 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1aw7I8-00037S-R4 for qemu-devel@nongnu.org; Fri, 29 Apr 2016 08:13:12 -0400 Received: from e17.ny.us.ibm.com ([129.33.205.207]:36238) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aw7I8-0002xG-GE for qemu-devel@nongnu.org; Fri, 29 Apr 2016 08:13:04 -0400 Received: from localhost by e17.ny.us.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Fri, 29 Apr 2016 08:12:27 -0400 Received: from d01dlp02.pok.ibm.com (9.56.250.167) by e17.ny.us.ibm.com (146.89.104.204) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Fri, 29 Apr 2016 08:12:25 -0400 X-IBM-Helo: d01dlp02.pok.ibm.com X-IBM-MailFrom: bjsdjshi@linux.vnet.ibm.com X-IBM-RcptTo: qemu-devel@nongnu.org; alex.williamson@redhat.com; agraf@suse.com; kvm@vger.kernel.org; linux-s390@vger.kernel.org Received: from b01cxnp22036.gho.pok.ibm.com (b01cxnp22036.gho.pok.ibm.com [9.57.198.26]) by d01dlp02.pok.ibm.com (Postfix) with ESMTP id 993966E8040; Fri, 29 Apr 2016 08:12:09 -0400 (EDT) Received: from b01ledav001.gho.pok.ibm.com (b01ledav001.gho.pok.ibm.com [9.57.199.106]) by b01cxnp22036.gho.pok.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id u3TCCOo745285568; Fri, 29 Apr 2016 12:12:24 GMT Received: from b01ledav001.gho.pok.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id F31FE2803F; Fri, 29 Apr 2016 08:12:21 -0400 (EDT) Received: from tuxmaker.boeblingen.de.ibm.com (unknown [9.152.85.9]) by b01ledav001.gho.pok.ibm.com (Postfix) with ESMTP id E33D62803E; Fri, 29 Apr 2016 08:12:20 -0400 (EDT) From: Dong Jia Shi To: kvm@vger.kernel.org, linux-s390@vger.kernel.org, qemu-devel@nongnu.org Date: Fri, 29 Apr 2016 14:11:55 +0200 Message-Id: <1461931915-22397-9-git-send-email-bjsdjshi@linux.vnet.ibm.com> X-Mailer: git-send-email 2.6.6 In-Reply-To: <1461931915-22397-1-git-send-email-bjsdjshi@linux.vnet.ibm.com> References: <1461931915-22397-1-git-send-email-bjsdjshi@linux.vnet.ibm.com> X-TM-AS-GCONF: 00 X-Content-Scanned: Fidelis XPS MAILER x-cbid: 16042912-0041-0000-0000-000004049769 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 129.33.205.207 X-Mailman-Approved-At: Fri, 29 Apr 2016 09:31:35 -0400 Subject: [Qemu-devel] [PATCH RFC 8/8] vfio: ccw: realize VFIO_DEVICE_CCW_CMD_REQUEST ioctl X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: borntraeger@de.ibm.com, alex.williamson@redhat.com, renxiaof@linux.vnet.ibm.com, cornelia.huck@de.ibm.com, bjsdjshi@linux.vnet.ibm.com, agraf@suse.com Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Introduce VFIO_DEVICE_CCW_CMD_REQUEST ioctl for vfio-ccw to handle the translated ccw commands. We implement the basic ccw command handling infrastructure here: 1. Issue the translated ccw commands to the device. 2. Once we get the execution result, update the guest SCSW with it. Signed-off-by: Dong Jia Shi Reviewed-by: Pierre Morel --- drivers/vfio/ccw/vfio_ccw.c | 190 +++++++++++++++++++++++++++++++++++++++++++- include/uapi/linux/vfio.h | 23 ++++++ 2 files changed, 212 insertions(+), 1 deletion(-) diff --git a/drivers/vfio/ccw/vfio_ccw.c b/drivers/vfio/ccw/vfio_ccw.c index 9700448..3979544 100644 --- a/drivers/vfio/ccw/vfio_ccw.c +++ b/drivers/vfio/ccw/vfio_ccw.c @@ -17,15 +17,30 @@ #include #include #include +#include +#include "ccwchain.h" /** * struct vfio_ccw_device * @cdev: ccw device + * @curr_intparm: record current interrupt parameter, + * used for wait interrupt. + * @wait_q: wait for interrupt + * @ccwchain_cmd: address map for current ccwchain. + * @irb: irb info received from interrupt + * @orb: orb for the currently processed ssch request + * @scsw: scsw info * @going_away: if an offline procedure was already ongoing * @hot_reset: if hot-reset is ongoing */ struct vfio_ccw_device { struct ccw_device *cdev; + u32 curr_intparm; + wait_queue_head_t wait_q; + struct ccwchain_cmd ccwchain_cmd; + struct irb irb; + union orb orb; + union scsw scsw; bool going_away; bool hot_reset; }; @@ -42,6 +57,118 @@ struct ccw_device_id vfio_ccw_ids[] = { MODULE_DEVICE_TABLE(ccw, vfio_ccw_ids); /* + * LATER: + * This is good for Linux guests; but we may need an interface to + * deal with further bits in the orb. + */ +static unsigned long flags_from_orb(union orb *orb) +{ + unsigned long flags = 0; + + flags |= orb->cmd.pfch ? 0 : DOIO_DENY_PREFETCH; + flags |= orb->cmd.spnd ? DOIO_ALLOW_SUSPEND : 0; + flags |= orb->cmd.ssic ? (DOIO_SUPPRESS_INTER | DOIO_ALLOW_SUSPEND) : 0; + + return flags; +} + +/* Check if the current intparm has been set. */ +static int doing_io(struct vfio_ccw_device *vcdev, u32 intparm) +{ + unsigned long flags; + int ret; + + spin_lock_irqsave(get_ccwdev_lock(vcdev->cdev), flags); + ret = (vcdev->curr_intparm == intparm); + spin_unlock_irqrestore(get_ccwdev_lock(vcdev->cdev), flags); + return ret; +} + +int vfio_ccw_io_helper(struct vfio_ccw_device *vcdev) +{ + struct ccwchain_cmd *ccwchain_cmd; + struct ccw1 *cpa; + u32 intparm; + unsigned long io_flags, lock_flags; + int ret; + + ccwchain_cmd = &vcdev->ccwchain_cmd; + cpa = ccwchain_get_cpa(ccwchain_cmd); + intparm = (u32)(u64)ccwchain_cmd->k_ccwchain; + io_flags = flags_from_orb(&vcdev->orb); + + spin_lock_irqsave(get_ccwdev_lock(vcdev->cdev), lock_flags); + ret = ccw_device_start(vcdev->cdev, cpa, intparm, + vcdev->orb.cmd.lpm, io_flags); + if (!ret) + vcdev->curr_intparm = 0; + spin_unlock_irqrestore(get_ccwdev_lock(vcdev->cdev), lock_flags); + + if (!ret) + wait_event(vcdev->wait_q, + doing_io(vcdev, intparm)); + + ccwchain_update_scsw(ccwchain_cmd, &(vcdev->irb.scsw)); + + return ret; +} + +/* Deal with the ccw command request from the userspace. */ +int vfio_ccw_cmd_request(struct vfio_ccw_device *vcdev, + struct vfio_ccw_cmd *ccw_cmd) +{ + union orb *orb = &vcdev->orb; + union scsw *scsw = &vcdev->scsw; + struct irb *irb = &vcdev->irb; + int ret; + + memcpy(orb, ccw_cmd->orb_area, sizeof(*orb)); + memcpy(scsw, ccw_cmd->scsw_area, sizeof(*scsw)); + vcdev->ccwchain_cmd.u_ccwchain = (void *)ccw_cmd->ccwchain_buf; + vcdev->ccwchain_cmd.k_ccwchain = NULL; + vcdev->ccwchain_cmd.nr = ccw_cmd->ccwchain_nr; + + if (scsw->cmd.fctl & SCSW_FCTL_START_FUNC) { + /* + * XXX: + * Only support prefetch enable mode now. + * Only support 64bit addressing idal. + */ + if (!orb->cmd.pfch || !orb->cmd.c64) + return -EOPNOTSUPP; + + ret = ccwchain_alloc(&vcdev->ccwchain_cmd); + if (ret) + return ret; + + ret = ccwchain_prefetch(&vcdev->ccwchain_cmd); + if (ret) { + ccwchain_free(&vcdev->ccwchain_cmd); + return ret; + } + + /* Start channel program and wait for I/O interrupt. */ + ret = vfio_ccw_io_helper(vcdev); + if (!ret) { + /* Get irb info and copy it to irb_area. */ + memcpy(ccw_cmd->irb_area, irb, sizeof(*irb)); + } + + ccwchain_free(&vcdev->ccwchain_cmd); + } else if (scsw->cmd.fctl & SCSW_FCTL_HALT_FUNC) { + /* XXX: Handle halt. */ + ret = -EOPNOTSUPP; + } else if (scsw->cmd.fctl & SCSW_FCTL_CLEAR_FUNC) { + /* XXX: Handle clear. */ + ret = -EOPNOTSUPP; + } else { + ret = -EOPNOTSUPP; + } + + return ret; +} + +/* * vfio callbacks */ static int vfio_ccw_open(void *device_data) @@ -107,6 +234,24 @@ static long vfio_ccw_ioctl(void *device_data, unsigned int cmd, vcdev->hot_reset = false; spin_unlock_irqrestore(get_ccwdev_lock(vcdev->cdev), flags); return ret; + + } else if (cmd == VFIO_DEVICE_CCW_CMD_REQUEST) { + struct vfio_ccw_cmd ccw_cmd; + int ret; + + minsz = offsetofend(struct vfio_ccw_cmd, ccwchain_buf); + + if (copy_from_user(&ccw_cmd, (void __user *)arg, minsz)) + return -EFAULT; + + if (ccw_cmd.argsz < minsz) + return -EINVAL; + + ret = vfio_ccw_cmd_request(vcdev, &ccw_cmd); + if (ret) + return ret; + + return copy_to_user((void __user *)arg, &ccw_cmd, minsz); } return -ENOTTY; @@ -119,6 +264,25 @@ static const struct vfio_device_ops vfio_ccw_ops = { .ioctl = vfio_ccw_ioctl, }; +static void vfio_ccw_int_handler(struct ccw_device *cdev, + unsigned long intparm, + struct irb *irb) +{ + struct vfio_device *device = dev_get_drvdata(&cdev->dev); + struct vfio_ccw_device *vdev; + + if (!device) + return; + + vdev = vfio_device_data(device); + if (!vdev) + return; + + vdev->curr_intparm = intparm; + memcpy(&vdev->irb, irb, sizeof(*irb)); + wake_up(&vdev->wait_q); +} + static int vfio_ccw_probe(struct ccw_device *cdev) { struct iommu_group *group = vfio_iommu_group_get(&cdev->dev); @@ -126,6 +290,8 @@ static int vfio_ccw_probe(struct ccw_device *cdev) if (!group) return -EINVAL; + cdev->handler = vfio_ccw_int_handler; + return 0; } @@ -142,6 +308,9 @@ static int vfio_ccw_set_offline(struct ccw_device *cdev) if (!vdev || vdev->hot_reset || vdev->going_away) return 0; + /* Put the vfio_device reference we got during the online process. */ + vfio_device_put(device); + vdev->going_away = true; vfio_del_group_dev(&cdev->dev); kfree(vdev); @@ -155,6 +324,8 @@ void vfio_ccw_remove(struct ccw_device *cdev) vfio_ccw_set_offline(cdev); vfio_iommu_group_put(cdev->dev.iommu_group, &cdev->dev); + + cdev->handler = NULL; } static int vfio_ccw_set_online(struct ccw_device *cdev) @@ -186,8 +357,25 @@ create_device: vdev->cdev = cdev; ret = vfio_add_group_dev(&cdev->dev, &vfio_ccw_ops, vdev); - if (ret) + if (ret) { + kfree(vdev); + return ret; + } + + /* + * Get a reference to the vfio_device for this device, and don't put + * it until device offline. Thus we don't need to get/put a reference + * every time we run into the int_handler. And we will get rid of a + * wrong usage of mutex in int_handler. + */ + device = vfio_device_get_from_dev(&cdev->dev); + if (!device) { + vfio_del_group_dev(&cdev->dev); kfree(vdev); + return -ENODEV; + } + + init_waitqueue_head(&vdev->wait_q); return ret; } diff --git a/include/uapi/linux/vfio.h b/include/uapi/linux/vfio.h index 889a316..5e8a58e 100644 --- a/include/uapi/linux/vfio.h +++ b/include/uapi/linux/vfio.h @@ -695,6 +695,29 @@ struct vfio_iommu_spapr_tce_remove { */ #define VFIO_DEVICE_CCW_HOT_RESET _IO(VFIO_TYPE, VFIO_BASE + 21) +/** + * VFIO_DEVICE_CCW_CMD_REQUEST - _IOWR(VFIO_TYPE, VFIO_BASE + 22, + * struct vfio_ccw_cmd) + * + * Issue a user-space ccw program for translation and performing channel I/O + * operations. + */ +struct vfio_ccw_cmd { + __u32 argsz; + __u8 cssid; + __u8 ssid; + __u16 devno; +#define ORB_AREA_SIZE 12 + __u8 orb_area[ORB_AREA_SIZE]; +#define SCSW_AREA_SIZE 12 + __u8 scsw_area[SCSW_AREA_SIZE]; +#define IRB_AREA_SIZE 96 + __u8 irb_area[IRB_AREA_SIZE]; + __u32 ccwchain_nr; + __u64 ccwchain_buf; +} __attribute__((packed)); +#define VFIO_DEVICE_CCW_CMD_REQUEST _IO(VFIO_TYPE, VFIO_BASE + 22) + /* ***************************************************************** */ #endif /* _UAPIVFIO_H */