From nobody Fri Nov 7 10:26:59 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1548069147766169.96867982857304; Mon, 21 Jan 2019 03:12:27 -0800 (PST) Received: from localhost ([127.0.0.1]:51952 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1glXV8-0007eW-Rk for importer@patchew.org; Mon, 21 Jan 2019 06:12:22 -0500 Received: from eggs.gnu.org ([209.51.188.92]:45028) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1glXNn-0002ZJ-JB for qemu-devel@nongnu.org; Mon, 21 Jan 2019 06:04:51 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1glXNg-0003qd-Le for qemu-devel@nongnu.org; Mon, 21 Jan 2019 06:04:46 -0500 Received: from mx1.redhat.com ([209.132.183.28]:32972) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1glXNU-0003X1-UR; Mon, 21 Jan 2019 06:04:32 -0500 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id BFB9F432DA; Mon, 21 Jan 2019 11:04:18 +0000 (UTC) Received: from localhost (ovpn-116-178.ams2.redhat.com [10.36.116.178]) by smtp.corp.redhat.com (Postfix) with ESMTPS id ACC56104812F; Mon, 21 Jan 2019 11:04:14 +0000 (UTC) From: Cornelia Huck To: Halil Pasic , Eric Farman , Farhan Ali , Pierre Morel Date: Mon, 21 Jan 2019 12:03:50 +0100 Message-Id: <20190121110354.2247-2-cohuck@redhat.com> In-Reply-To: <20190121110354.2247-1-cohuck@redhat.com> References: <20190121110354.2247-1-cohuck@redhat.com> X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.30]); Mon, 21 Jan 2019 11:04:18 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v2 1/5] vfio-ccw: make it safe to access channel programs 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: linux-s390@vger.kernel.org, kvm@vger.kernel.org, Cornelia Huck , Alex Williamson , qemu-devel@nongnu.org, qemu-s390x@nongnu.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" When we get a solicited interrupt, the start function may have been cleared by a csch, but we still have a channel program structure allocated. Make it safe to call the cp accessors in any case, so we can call them unconditionally. Signed-off-by: Cornelia Huck --- drivers/s390/cio/vfio_ccw_cp.c | 3 +++ drivers/s390/cio/vfio_ccw_cp.h | 2 ++ 2 files changed, 5 insertions(+) diff --git a/drivers/s390/cio/vfio_ccw_cp.c b/drivers/s390/cio/vfio_ccw_cp.c index 70a006ba4d05..714987ceea9a 100644 --- a/drivers/s390/cio/vfio_ccw_cp.c +++ b/drivers/s390/cio/vfio_ccw_cp.c @@ -335,6 +335,7 @@ static void cp_unpin_free(struct channel_program *cp) struct ccwchain *chain, *temp; int i; =20 + cp->initialized =3D false; list_for_each_entry_safe(chain, temp, &cp->ccwchain_list, next) { for (i =3D 0; i < chain->ch_len; i++) { pfn_array_table_unpin_free(chain->ch_pat + i, @@ -701,6 +702,8 @@ int cp_init(struct channel_program *cp, struct device *= mdev, union orb *orb) */ cp->orb.cmd.c64 =3D 1; =20 + cp->initialized =3D true; + return ret; } =20 diff --git a/drivers/s390/cio/vfio_ccw_cp.h b/drivers/s390/cio/vfio_ccw_cp.h index a4b74fb1aa57..3c20cd208da5 100644 --- a/drivers/s390/cio/vfio_ccw_cp.h +++ b/drivers/s390/cio/vfio_ccw_cp.h @@ -21,6 +21,7 @@ * @ccwchain_list: list head of ccwchains * @orb: orb for the currently processed ssch request * @mdev: the mediated device to perform page pinning/unpinning + * @initialized: whether this instance is actually initialized * * @ccwchain_list is the head of a ccwchain list, that contents the * translated result of the guest channel program that pointed out by @@ -30,6 +31,7 @@ struct channel_program { struct list_head ccwchain_list; union orb orb; struct device *mdev; + bool initialized; }; =20 extern int cp_init(struct channel_program *cp, struct device *mdev, --=20 2.17.2 From nobody Fri Nov 7 10:26:59 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1548068782741723.9206983576512; Mon, 21 Jan 2019 03:06:22 -0800 (PST) Received: from localhost ([127.0.0.1]:51875 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1glXPG-0003Pu-Mf for importer@patchew.org; Mon, 21 Jan 2019 06:06:18 -0500 Received: from eggs.gnu.org ([209.51.188.92]:44970) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1glXNj-0002VL-0c for qemu-devel@nongnu.org; Mon, 21 Jan 2019 06:04:44 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1glXNc-0003n6-RY for qemu-devel@nongnu.org; Mon, 21 Jan 2019 06:04:42 -0500 Received: from mx1.redhat.com ([209.132.183.28]:49666) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1glXNQ-0003ZQ-Ve; Mon, 21 Jan 2019 06:04:28 -0500 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 6ED6514404F; Mon, 21 Jan 2019 11:04:21 +0000 (UTC) Received: from localhost (ovpn-116-178.ams2.redhat.com [10.36.116.178]) by smtp.corp.redhat.com (Postfix) with ESMTPS id E8AAF104812F; Mon, 21 Jan 2019 11:04:20 +0000 (UTC) From: Cornelia Huck To: Halil Pasic , Eric Farman , Farhan Ali , Pierre Morel Date: Mon, 21 Jan 2019 12:03:51 +0100 Message-Id: <20190121110354.2247-3-cohuck@redhat.com> In-Reply-To: <20190121110354.2247-1-cohuck@redhat.com> References: <20190121110354.2247-1-cohuck@redhat.com> X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.38]); Mon, 21 Jan 2019 11:04:21 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v2 2/5] vfio-ccw: concurrent I/O handling 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: linux-s390@vger.kernel.org, kvm@vger.kernel.org, Cornelia Huck , Alex Williamson , qemu-devel@nongnu.org, qemu-s390x@nongnu.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Rework handling of multiple I/O requests to return -EAGAIN if we are already processing an I/O request. Introduce a mutex to disallow concurrent writes to the I/O region. The expectation is that userspace simply retries the operation if it gets -EAGAIN. We currently don't allow multiple ssch requests at the same time, as we don't have support for keeping channel programs around for more than one request. Signed-off-by: Cornelia Huck --- drivers/s390/cio/vfio_ccw_drv.c | 1 + drivers/s390/cio/vfio_ccw_fsm.c | 8 +++----- drivers/s390/cio/vfio_ccw_ops.c | 31 +++++++++++++++++++---------- drivers/s390/cio/vfio_ccw_private.h | 2 ++ 4 files changed, 26 insertions(+), 16 deletions(-) diff --git a/drivers/s390/cio/vfio_ccw_drv.c b/drivers/s390/cio/vfio_ccw_dr= v.c index a10cec0e86eb..2ef189fe45ed 100644 --- a/drivers/s390/cio/vfio_ccw_drv.c +++ b/drivers/s390/cio/vfio_ccw_drv.c @@ -125,6 +125,7 @@ static int vfio_ccw_sch_probe(struct subchannel *sch) =20 private->sch =3D sch; dev_set_drvdata(&sch->dev, private); + mutex_init(&private->io_mutex); =20 spin_lock_irq(sch->lock); private->state =3D VFIO_CCW_STATE_NOT_OPER; diff --git a/drivers/s390/cio/vfio_ccw_fsm.c b/drivers/s390/cio/vfio_ccw_fs= m.c index cab17865aafe..f6ed934cc565 100644 --- a/drivers/s390/cio/vfio_ccw_fsm.c +++ b/drivers/s390/cio/vfio_ccw_fsm.c @@ -28,7 +28,6 @@ static int fsm_io_helper(struct vfio_ccw_private *private) sch =3D private->sch; =20 spin_lock_irqsave(sch->lock, flags); - private->state =3D VFIO_CCW_STATE_BUSY; =20 orb =3D cp_get_orb(&private->cp, (u32)(addr_t)sch, sch->lpm); =20 @@ -42,6 +41,8 @@ static int fsm_io_helper(struct vfio_ccw_private *private) */ sch->schib.scsw.cmd.actl |=3D SCSW_ACTL_START_PEND; ret =3D 0; + /* Don't allow another ssch for now */ + private->state =3D VFIO_CCW_STATE_BUSY; break; case 1: /* Status pending */ case 2: /* Busy */ @@ -99,7 +100,7 @@ static void fsm_io_error(struct vfio_ccw_private *privat= e, static void fsm_io_busy(struct vfio_ccw_private *private, enum vfio_ccw_event event) { - private->io_region->ret_code =3D -EBUSY; + private->io_region->ret_code =3D -EAGAIN; } =20 static void fsm_disabled_irq(struct vfio_ccw_private *private, @@ -130,8 +131,6 @@ static void fsm_io_request(struct vfio_ccw_private *pri= vate, struct mdev_device *mdev =3D private->mdev; char *errstr =3D "request"; =20 - private->state =3D VFIO_CCW_STATE_BUSY; - memcpy(scsw, io_region->scsw_area, sizeof(*scsw)); =20 if (scsw->cmd.fctl & SCSW_FCTL_START_FUNC) { @@ -176,7 +175,6 @@ static void fsm_io_request(struct vfio_ccw_private *pri= vate, } =20 err_out: - private->state =3D VFIO_CCW_STATE_IDLE; trace_vfio_ccw_io_fctl(scsw->cmd.fctl, get_schid(private), io_region->ret_code, errstr); } diff --git a/drivers/s390/cio/vfio_ccw_ops.c b/drivers/s390/cio/vfio_ccw_op= s.c index f673e106c041..3fa9fc570400 100644 --- a/drivers/s390/cio/vfio_ccw_ops.c +++ b/drivers/s390/cio/vfio_ccw_ops.c @@ -169,16 +169,20 @@ static ssize_t vfio_ccw_mdev_read(struct mdev_device = *mdev, { struct vfio_ccw_private *private; struct ccw_io_region *region; + int ret; =20 if (*ppos + count > sizeof(*region)) return -EINVAL; =20 private =3D dev_get_drvdata(mdev_parent_dev(mdev)); + mutex_lock(&private->io_mutex); region =3D private->io_region; if (copy_to_user(buf, (void *)region + *ppos, count)) - return -EFAULT; - - return count; + ret =3D -EFAULT; + else + ret =3D count; + mutex_unlock(&private->io_mutex); + return ret; } =20 static ssize_t vfio_ccw_mdev_write(struct mdev_device *mdev, @@ -188,25 +192,30 @@ static ssize_t vfio_ccw_mdev_write(struct mdev_device= *mdev, { struct vfio_ccw_private *private; struct ccw_io_region *region; + int ret; =20 if (*ppos + count > sizeof(*region)) return -EINVAL; =20 private =3D dev_get_drvdata(mdev_parent_dev(mdev)); - if (private->state !=3D VFIO_CCW_STATE_IDLE) + if (private->state =3D=3D VFIO_CCW_STATE_NOT_OPER || + private->state =3D=3D VFIO_CCW_STATE_STANDBY) return -EACCES; + if (!mutex_trylock(&private->io_mutex)) + return -EAGAIN; =20 region =3D private->io_region; - if (copy_from_user((void *)region + *ppos, buf, count)) - return -EFAULT; + if (copy_from_user((void *)region + *ppos, buf, count)) { + ret =3D -EFAULT; + goto out_unlock; + } =20 vfio_ccw_fsm_event(private, VFIO_CCW_EVENT_IO_REQ); - if (region->ret_code !=3D 0) { - private->state =3D VFIO_CCW_STATE_IDLE; - return region->ret_code; - } + ret =3D (region->ret_code !=3D 0) ? region->ret_code : count; =20 - return count; +out_unlock: + mutex_unlock(&private->io_mutex); + return ret; } =20 static int vfio_ccw_mdev_get_device_info(struct vfio_device_info *info) diff --git a/drivers/s390/cio/vfio_ccw_private.h b/drivers/s390/cio/vfio_cc= w_private.h index 08e9a7dc9176..e88237697f83 100644 --- a/drivers/s390/cio/vfio_ccw_private.h +++ b/drivers/s390/cio/vfio_ccw_private.h @@ -28,6 +28,7 @@ * @mdev: pointer to the mediated device * @nb: notifier for vfio events * @io_region: MMIO region to input/output I/O arguments/results + * @io_mutex: protect against concurrent update of I/O structures * @cp: channel program for the current I/O operation * @irb: irb info received from interrupt * @scsw: scsw info @@ -42,6 +43,7 @@ struct vfio_ccw_private { struct mdev_device *mdev; struct notifier_block nb; struct ccw_io_region *io_region; + struct mutex io_mutex; =20 struct channel_program cp; struct irb irb; --=20 2.17.2 From nobody Fri Nov 7 10:26:59 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1548069000364400.9446731575116; Mon, 21 Jan 2019 03:10:00 -0800 (PST) Received: from localhost ([127.0.0.1]:51905 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1glXSp-00067Q-4d for importer@patchew.org; Mon, 21 Jan 2019 06:09:59 -0500 Received: from eggs.gnu.org ([209.51.188.92]:45014) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1glXNm-0002Ye-PP for qemu-devel@nongnu.org; Mon, 21 Jan 2019 06:04:51 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1glXNi-0003sp-VG for qemu-devel@nongnu.org; Mon, 21 Jan 2019 06:04:46 -0500 Received: from mx1.redhat.com ([209.132.183.28]:55992) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1glXNi-0003fJ-9c; Mon, 21 Jan 2019 06:04:42 -0500 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id D4B018E3C6; Mon, 21 Jan 2019 11:04:27 +0000 (UTC) Received: from localhost (ovpn-116-178.ams2.redhat.com [10.36.116.178]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 6279A6090E; Mon, 21 Jan 2019 11:04:23 +0000 (UTC) From: Cornelia Huck To: Halil Pasic , Eric Farman , Farhan Ali , Pierre Morel Date: Mon, 21 Jan 2019 12:03:52 +0100 Message-Id: <20190121110354.2247-4-cohuck@redhat.com> In-Reply-To: <20190121110354.2247-1-cohuck@redhat.com> References: <20190121110354.2247-1-cohuck@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.25]); Mon, 21 Jan 2019 11:04:28 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v2 3/5] vfio-ccw: add capabilities chain 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: linux-s390@vger.kernel.org, kvm@vger.kernel.org, Cornelia Huck , Alex Williamson , qemu-devel@nongnu.org, qemu-s390x@nongnu.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Allow to extend the regions used by vfio-ccw. The first user will be handling of halt and clear subchannel. Signed-off-by: Cornelia Huck Reviewed-by: Eric Farman --- drivers/s390/cio/vfio_ccw_ops.c | 181 ++++++++++++++++++++++++---- drivers/s390/cio/vfio_ccw_private.h | 38 ++++++ include/uapi/linux/vfio.h | 2 + 3 files changed, 195 insertions(+), 26 deletions(-) diff --git a/drivers/s390/cio/vfio_ccw_ops.c b/drivers/s390/cio/vfio_ccw_op= s.c index 3fa9fc570400..5a89d09f9271 100644 --- a/drivers/s390/cio/vfio_ccw_ops.c +++ b/drivers/s390/cio/vfio_ccw_ops.c @@ -3,9 +3,11 @@ * Physical device callbacks for vfio_ccw * * Copyright IBM Corp. 2017 + * Copyright Red Hat, Inc. 2019 * * Author(s): Dong Jia Shi * Xiao Feng Ren + * Cornelia Huck */ =20 #include @@ -157,27 +159,33 @@ static void vfio_ccw_mdev_release(struct mdev_device = *mdev) { struct vfio_ccw_private *private =3D dev_get_drvdata(mdev_parent_dev(mdev)); + int i; =20 vfio_unregister_notifier(mdev_dev(mdev), VFIO_IOMMU_NOTIFY, &private->nb); + + for (i =3D 0; i < private->num_regions; i++) + private->region[i].ops->release(private, &private->region[i]); + + private->num_regions =3D 0; + kfree(private->region); + private->region =3D NULL; } =20 -static ssize_t vfio_ccw_mdev_read(struct mdev_device *mdev, - char __user *buf, - size_t count, - loff_t *ppos) +static ssize_t vfio_ccw_mdev_read_io_region(struct vfio_ccw_private *priva= te, + char __user *buf, size_t count, + loff_t *ppos) { - struct vfio_ccw_private *private; + loff_t pos =3D *ppos & VFIO_CCW_OFFSET_MASK; struct ccw_io_region *region; int ret; =20 - if (*ppos + count > sizeof(*region)) + if (pos + count > sizeof(*region)) return -EINVAL; =20 - private =3D dev_get_drvdata(mdev_parent_dev(mdev)); mutex_lock(&private->io_mutex); region =3D private->io_region; - if (copy_to_user(buf, (void *)region + *ppos, count)) + if (copy_to_user(buf, (void *)region + pos, count)) ret =3D -EFAULT; else ret =3D count; @@ -185,19 +193,42 @@ static ssize_t vfio_ccw_mdev_read(struct mdev_device = *mdev, return ret; } =20 -static ssize_t vfio_ccw_mdev_write(struct mdev_device *mdev, - const char __user *buf, - size_t count, - loff_t *ppos) +static ssize_t vfio_ccw_mdev_read(struct mdev_device *mdev, + char __user *buf, + size_t count, + loff_t *ppos) { + unsigned int index =3D VFIO_CCW_OFFSET_TO_INDEX(*ppos); struct vfio_ccw_private *private; + + private =3D dev_get_drvdata(mdev_parent_dev(mdev)); + + if (index >=3D VFIO_CCW_NUM_REGIONS + private->num_regions) + return -EINVAL; + + switch (index) { + case VFIO_CCW_CONFIG_REGION_INDEX: + return vfio_ccw_mdev_read_io_region(private, buf, count, ppos); + default: + index -=3D VFIO_CCW_NUM_REGIONS; + return private->region[index].ops->read(private, buf, count, + ppos); + } + + return -EINVAL; +} + +static ssize_t vfio_ccw_mdev_write_io_region(struct vfio_ccw_private *priv= ate, + const char __user *buf, + size_t count, loff_t *ppos) +{ + loff_t pos =3D *ppos & VFIO_CCW_OFFSET_MASK; struct ccw_io_region *region; int ret; =20 - if (*ppos + count > sizeof(*region)) + if (pos + count > sizeof(*region)) return -EINVAL; =20 - private =3D dev_get_drvdata(mdev_parent_dev(mdev)); if (private->state =3D=3D VFIO_CCW_STATE_NOT_OPER || private->state =3D=3D VFIO_CCW_STATE_STANDBY) return -EACCES; @@ -205,7 +236,7 @@ static ssize_t vfio_ccw_mdev_write(struct mdev_device *= mdev, return -EAGAIN; =20 region =3D private->io_region; - if (copy_from_user((void *)region + *ppos, buf, count)) { + if (copy_from_user((void *)region + pos, buf, count)) { ret =3D -EFAULT; goto out_unlock; } @@ -218,19 +249,52 @@ static ssize_t vfio_ccw_mdev_write(struct mdev_device= *mdev, return ret; } =20 -static int vfio_ccw_mdev_get_device_info(struct vfio_device_info *info) +static ssize_t vfio_ccw_mdev_write(struct mdev_device *mdev, + const char __user *buf, + size_t count, + loff_t *ppos) +{ + unsigned int index =3D VFIO_CCW_OFFSET_TO_INDEX(*ppos); + struct vfio_ccw_private *private; + + private =3D dev_get_drvdata(mdev_parent_dev(mdev)); + + if (index >=3D VFIO_CCW_NUM_REGIONS + private->num_regions) + return -EINVAL; + + switch (index) { + case VFIO_CCW_CONFIG_REGION_INDEX: + return vfio_ccw_mdev_write_io_region(private, buf, count, ppos); + default: + index -=3D VFIO_CCW_NUM_REGIONS; + return private->region[index].ops->write(private, buf, count, + ppos); + } + + return -EINVAL; +} + +static int vfio_ccw_mdev_get_device_info(struct vfio_device_info *info, + struct mdev_device *mdev) { + struct vfio_ccw_private *private; + + private =3D dev_get_drvdata(mdev_parent_dev(mdev)); info->flags =3D VFIO_DEVICE_FLAGS_CCW | VFIO_DEVICE_FLAGS_RESET; - info->num_regions =3D VFIO_CCW_NUM_REGIONS; + info->num_regions =3D VFIO_CCW_NUM_REGIONS + private->num_regions; info->num_irqs =3D VFIO_CCW_NUM_IRQS; =20 return 0; } =20 static int vfio_ccw_mdev_get_region_info(struct vfio_region_info *info, - u16 *cap_type_id, - void **cap_type) + struct mdev_device *mdev, + unsigned long arg) { + struct vfio_ccw_private *private; + int i; + + private =3D dev_get_drvdata(mdev_parent_dev(mdev)); switch (info->index) { case VFIO_CCW_CONFIG_REGION_INDEX: info->offset =3D 0; @@ -238,9 +302,51 @@ static int vfio_ccw_mdev_get_region_info(struct vfio_r= egion_info *info, info->flags =3D VFIO_REGION_INFO_FLAG_READ | VFIO_REGION_INFO_FLAG_WRITE; return 0; - default: - return -EINVAL; + default: /* all other regions are handled via capability chain */ + { + struct vfio_info_cap caps =3D { .buf =3D NULL, .size =3D 0 }; + struct vfio_region_info_cap_type cap_type =3D { + .header.id =3D VFIO_REGION_INFO_CAP_TYPE, + .header.version =3D 1 }; + int ret; + + if (info->index >=3D + VFIO_CCW_NUM_REGIONS + private->num_regions) + return -EINVAL; + + i =3D info->index - VFIO_CCW_NUM_REGIONS; + + info->offset =3D VFIO_CCW_INDEX_TO_OFFSET(info->index); + info->size =3D private->region[i].size; + info->flags =3D private->region[i].flags; + + cap_type.type =3D private->region[i].type; + cap_type.subtype =3D private->region[i].subtype; + + ret =3D vfio_info_add_capability(&caps, &cap_type.header, + sizeof(cap_type)); + if (ret) + return ret; + + info->flags |=3D VFIO_REGION_INFO_FLAG_CAPS; + if (info->argsz < sizeof(*info) + caps.size) { + info->argsz =3D sizeof(*info) + caps.size; + info->cap_offset =3D 0; + } else { + vfio_info_cap_shift(&caps, sizeof(*info)); + if (copy_to_user((void __user *)arg + sizeof(*info), + caps.buf, caps.size)) { + kfree(caps.buf); + return -EFAULT; + } + info->cap_offset =3D sizeof(*info); + } + + kfree(caps.buf); + + } } + return 0; } =20 static int vfio_ccw_mdev_get_irq_info(struct vfio_irq_info *info) @@ -317,6 +423,32 @@ static int vfio_ccw_mdev_set_irqs(struct mdev_device *= mdev, } } =20 +int vfio_ccw_register_dev_region(struct vfio_ccw_private *private, + unsigned int subtype, + const struct vfio_ccw_regops *ops, + size_t size, u32 flags, void *data) +{ + struct vfio_ccw_region *region; + + region =3D krealloc(private->region, + (private->num_regions + 1) * sizeof(*region), + GFP_KERNEL); + if (!region) + return -ENOMEM; + + private->region =3D region; + private->region[private->num_regions].type =3D VFIO_REGION_TYPE_CCW; + private->region[private->num_regions].subtype =3D subtype; + private->region[private->num_regions].ops =3D ops; + private->region[private->num_regions].size =3D size; + private->region[private->num_regions].flags =3D flags; + private->region[private->num_regions].data =3D data; + + private->num_regions++; + + return 0; +} + static ssize_t vfio_ccw_mdev_ioctl(struct mdev_device *mdev, unsigned int cmd, unsigned long arg) @@ -337,7 +469,7 @@ static ssize_t vfio_ccw_mdev_ioctl(struct mdev_device *= mdev, if (info.argsz < minsz) return -EINVAL; =20 - ret =3D vfio_ccw_mdev_get_device_info(&info); + ret =3D vfio_ccw_mdev_get_device_info(&info, mdev); if (ret) return ret; =20 @@ -346,8 +478,6 @@ static ssize_t vfio_ccw_mdev_ioctl(struct mdev_device *= mdev, case VFIO_DEVICE_GET_REGION_INFO: { struct vfio_region_info info; - u16 cap_type_id =3D 0; - void *cap_type =3D NULL; =20 minsz =3D offsetofend(struct vfio_region_info, offset); =20 @@ -357,8 +487,7 @@ static ssize_t vfio_ccw_mdev_ioctl(struct mdev_device *= mdev, if (info.argsz < minsz) return -EINVAL; =20 - ret =3D vfio_ccw_mdev_get_region_info(&info, &cap_type_id, - &cap_type); + ret =3D vfio_ccw_mdev_get_region_info(&info, mdev, arg); if (ret) return ret; =20 diff --git a/drivers/s390/cio/vfio_ccw_private.h b/drivers/s390/cio/vfio_cc= w_private.h index e88237697f83..20e75f4f3695 100644 --- a/drivers/s390/cio/vfio_ccw_private.h +++ b/drivers/s390/cio/vfio_ccw_private.h @@ -3,9 +3,11 @@ * Private stuff for vfio_ccw driver * * Copyright IBM Corp. 2017 + * Copyright Red Hat, Inc. 2019 * * Author(s): Dong Jia Shi * Xiao Feng Ren + * Cornelia Huck */ =20 #ifndef _VFIO_CCW_PRIVATE_H_ @@ -19,6 +21,38 @@ #include "css.h" #include "vfio_ccw_cp.h" =20 +#define VFIO_CCW_OFFSET_SHIFT 40 +#define VFIO_CCW_OFFSET_TO_INDEX(off) (off >> VFIO_CCW_OFFSET_SHIFT) +#define VFIO_CCW_INDEX_TO_OFFSET(index) ((u64)(index) << VFIO_CCW_OFFSET_S= HIFT) +#define VFIO_CCW_OFFSET_MASK (((u64)(1) << VFIO_CCW_OFFSET_SHIFT) - 1) + +/* capability chain handling similar to vfio-pci */ +struct vfio_ccw_private; +struct vfio_ccw_region; + +struct vfio_ccw_regops { + size_t (*read)(struct vfio_ccw_private *private, char __user *buf, + size_t count, loff_t *ppos); + size_t (*write)(struct vfio_ccw_private *private, + const char __user *buf, size_t count, loff_t *ppos); + void (*release)(struct vfio_ccw_private *private, + struct vfio_ccw_region *region); +}; + +struct vfio_ccw_region { + u32 type; + u32 subtype; + const struct vfio_ccw_regops *ops; + void *data; + size_t size; + u32 flags; +}; + +int vfio_ccw_register_dev_region(struct vfio_ccw_private *private, + unsigned int subtype, + const struct vfio_ccw_regops *ops, + size_t size, u32 flags, void *data); + /** * struct vfio_ccw_private * @sch: pointer to the subchannel @@ -29,6 +63,8 @@ * @nb: notifier for vfio events * @io_region: MMIO region to input/output I/O arguments/results * @io_mutex: protect against concurrent update of I/O structures + * @region: additional regions for other subchannel operations + * @num_regions: number of additional regions * @cp: channel program for the current I/O operation * @irb: irb info received from interrupt * @scsw: scsw info @@ -44,6 +80,8 @@ struct vfio_ccw_private { struct notifier_block nb; struct ccw_io_region *io_region; struct mutex io_mutex; + struct vfio_ccw_region *region; + int num_regions; =20 struct channel_program cp; struct irb irb; diff --git a/include/uapi/linux/vfio.h b/include/uapi/linux/vfio.h index 02bb7ad6e986..56e2413d3e00 100644 --- a/include/uapi/linux/vfio.h +++ b/include/uapi/linux/vfio.h @@ -353,6 +353,8 @@ struct vfio_region_gfx_edid { #define VFIO_DEVICE_GFX_LINK_STATE_DOWN 2 }; =20 +#define VFIO_REGION_TYPE_CCW (2) + /* * 10de vendor sub-type * --=20 2.17.2 From nobody Fri Nov 7 10:26:59 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1548068892758490.48595833472405; Mon, 21 Jan 2019 03:08:12 -0800 (PST) Received: from localhost ([127.0.0.1]:51890 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1glXR5-0004kF-Pp for importer@patchew.org; Mon, 21 Jan 2019 06:08:11 -0500 Received: from eggs.gnu.org ([209.51.188.92]:44973) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1glXNj-0002Va-93 for qemu-devel@nongnu.org; Mon, 21 Jan 2019 06:04:45 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1glXNi-0003sS-Bs for qemu-devel@nongnu.org; Mon, 21 Jan 2019 06:04:43 -0500 Received: from mx1.redhat.com ([209.132.183.28]:51604) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1glXNh-0003k3-OT; Mon, 21 Jan 2019 06:04:42 -0500 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 4D348753B1; Mon, 21 Jan 2019 11:04:33 +0000 (UTC) Received: from localhost (ovpn-116-178.ams2.redhat.com [10.36.116.178]) by smtp.corp.redhat.com (Postfix) with ESMTPS id B4CB960C74; Mon, 21 Jan 2019 11:04:29 +0000 (UTC) From: Cornelia Huck To: Halil Pasic , Eric Farman , Farhan Ali , Pierre Morel Date: Mon, 21 Jan 2019 12:03:53 +0100 Message-Id: <20190121110354.2247-5-cohuck@redhat.com> In-Reply-To: <20190121110354.2247-1-cohuck@redhat.com> References: <20190121110354.2247-1-cohuck@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.39]); Mon, 21 Jan 2019 11:04:33 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v2 4/5] s390/cio: export hsch to modules 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: linux-s390@vger.kernel.org, kvm@vger.kernel.org, Cornelia Huck , Alex Williamson , qemu-devel@nongnu.org, qemu-s390x@nongnu.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" The vfio-ccw code will need this, and it matches treatment of ssch and csch. Reviewed-by: Pierre Morel Signed-off-by: Cornelia Huck Reviewed-by: Halil Pasic --- drivers/s390/cio/ioasm.c | 1 + 1 file changed, 1 insertion(+) diff --git a/drivers/s390/cio/ioasm.c b/drivers/s390/cio/ioasm.c index 14d328338ce2..08eb10283b18 100644 --- a/drivers/s390/cio/ioasm.c +++ b/drivers/s390/cio/ioasm.c @@ -233,6 +233,7 @@ int hsch(struct subchannel_id schid) =20 return ccode; } +EXPORT_SYMBOL(hsch); =20 static inline int __xsch(struct subchannel_id schid) { --=20 2.17.2 From nobody Fri Nov 7 10:26:59 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 154806907557052.28685118987528; Mon, 21 Jan 2019 03:11:15 -0800 (PST) Received: from localhost ([127.0.0.1]:51946 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1glXU2-00070Z-D4 for importer@patchew.org; Mon, 21 Jan 2019 06:11:14 -0500 Received: from eggs.gnu.org ([209.51.188.92]:45026) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1glXNn-0002ZF-Im for qemu-devel@nongnu.org; Mon, 21 Jan 2019 06:04:51 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1glXNj-0003t9-1Z for qemu-devel@nongnu.org; Mon, 21 Jan 2019 06:04:46 -0500 Received: from mx1.redhat.com ([209.132.183.28]:20334) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1glXNi-0003q3-DE; Mon, 21 Jan 2019 06:04:42 -0500 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id A0BFF43A2E; Mon, 21 Jan 2019 11:04:39 +0000 (UTC) Received: from localhost (ovpn-116-178.ams2.redhat.com [10.36.116.178]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 42EB95D6AA; Mon, 21 Jan 2019 11:04:35 +0000 (UTC) From: Cornelia Huck To: Halil Pasic , Eric Farman , Farhan Ali , Pierre Morel Date: Mon, 21 Jan 2019 12:03:54 +0100 Message-Id: <20190121110354.2247-6-cohuck@redhat.com> In-Reply-To: <20190121110354.2247-1-cohuck@redhat.com> References: <20190121110354.2247-1-cohuck@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.30]); Mon, 21 Jan 2019 11:04:39 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v2 5/5] vfio-ccw: add handling for async channel instructions 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: linux-s390@vger.kernel.org, kvm@vger.kernel.org, Cornelia Huck , Alex Williamson , qemu-devel@nongnu.org, qemu-s390x@nongnu.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Add a region to the vfio-ccw device that can be used to submit asynchronous I/O instructions. ssch continues to be handled by the existing I/O region; the new region handles hsch and csch. Interrupt status continues to be reported through the same channels as for ssch. Signed-off-by: Cornelia Huck --- drivers/s390/cio/Makefile | 3 +- drivers/s390/cio/vfio_ccw_async.c | 91 ++++++++++++++++++++++ drivers/s390/cio/vfio_ccw_drv.c | 45 +++++++---- drivers/s390/cio/vfio_ccw_fsm.c | 114 +++++++++++++++++++++++++++- drivers/s390/cio/vfio_ccw_ops.c | 13 +++- drivers/s390/cio/vfio_ccw_private.h | 9 ++- include/uapi/linux/vfio.h | 2 + include/uapi/linux/vfio_ccw.h | 12 +++ 8 files changed, 269 insertions(+), 20 deletions(-) create mode 100644 drivers/s390/cio/vfio_ccw_async.c diff --git a/drivers/s390/cio/Makefile b/drivers/s390/cio/Makefile index f230516abb96..f6a8db04177c 100644 --- a/drivers/s390/cio/Makefile +++ b/drivers/s390/cio/Makefile @@ -20,5 +20,6 @@ obj-$(CONFIG_CCWGROUP) +=3D ccwgroup.o qdio-objs :=3D qdio_main.o qdio_thinint.o qdio_debug.o qdio_setup.o obj-$(CONFIG_QDIO) +=3D qdio.o =20 -vfio_ccw-objs +=3D vfio_ccw_drv.o vfio_ccw_cp.o vfio_ccw_ops.o vfio_ccw_fs= m.o +vfio_ccw-objs +=3D vfio_ccw_drv.o vfio_ccw_cp.o vfio_ccw_ops.o vfio_ccw_fs= m.o \ + vfio_ccw_async.o obj-$(CONFIG_VFIO_CCW) +=3D vfio_ccw.o diff --git a/drivers/s390/cio/vfio_ccw_async.c b/drivers/s390/cio/vfio_ccw_= async.c new file mode 100644 index 000000000000..604806c2970f --- /dev/null +++ b/drivers/s390/cio/vfio_ccw_async.c @@ -0,0 +1,91 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Async I/O region for vfio_ccw + * + * Copyright Red Hat, Inc. 2019 + * + * Author(s): Cornelia Huck + */ + +#include +#include + +#include "vfio_ccw_private.h" + +static ssize_t vfio_ccw_async_region_read(struct vfio_ccw_private *private, + char __user *buf, size_t count, + loff_t *ppos) +{ + unsigned int i =3D VFIO_CCW_OFFSET_TO_INDEX(*ppos) - VFIO_CCW_NUM_REGIONS; + loff_t pos =3D *ppos & VFIO_CCW_OFFSET_MASK; + struct ccw_cmd_region *region; + int ret; + + if (pos + count > sizeof(*region)) + return -EINVAL; + + mutex_lock(&private->io_mutex); + region =3D private->region[i].data; + if (copy_to_user(buf, (void *)region + pos, count)) + ret =3D -EFAULT; + else + ret =3D count; + mutex_unlock(&private->io_mutex); + return ret; +} + +static ssize_t vfio_ccw_async_region_write(struct vfio_ccw_private *privat= e, + const char __user *buf, size_t count, + loff_t *ppos) +{ + unsigned int i =3D VFIO_CCW_OFFSET_TO_INDEX(*ppos) - VFIO_CCW_NUM_REGIONS; + loff_t pos =3D *ppos & VFIO_CCW_OFFSET_MASK; + struct ccw_cmd_region *region; + int ret; + + if (pos + count > sizeof(*region)) + return -EINVAL; + + if (private->state =3D=3D VFIO_CCW_STATE_NOT_OPER || + private->state =3D=3D VFIO_CCW_STATE_STANDBY) + return -EACCES; + if (!mutex_trylock(&private->io_mutex)) + return -EAGAIN; + + region =3D private->region[i].data; + if (copy_from_user((void *)region + pos, buf, count)) { + ret =3D -EFAULT; + goto out_unlock; + } + + vfio_ccw_fsm_event(private, VFIO_CCW_EVENT_ASYNC_REQ); + + ret =3D region->ret_code ? region->ret_code : count; + +out_unlock: + mutex_unlock(&private->io_mutex); + return ret; +} + +static void vfio_ccw_async_region_release(struct vfio_ccw_private *private, + struct vfio_ccw_region *region) +{ + +} + +const struct vfio_ccw_regops vfio_ccw_async_region_ops =3D { + .read =3D vfio_ccw_async_region_read, + .write =3D vfio_ccw_async_region_write, + .release =3D vfio_ccw_async_region_release, +}; + +int vfio_ccw_register_async_dev_regions(struct vfio_ccw_private *private) +{ + return vfio_ccw_register_dev_region(private, + VFIO_REGION_SUBTYPE_CCW_ASYNC_CMD, + &vfio_ccw_async_region_ops, + sizeof(struct ccw_cmd_region), + VFIO_REGION_INFO_FLAG_READ | + VFIO_REGION_INFO_FLAG_WRITE, + private->cmd_region); +} diff --git a/drivers/s390/cio/vfio_ccw_drv.c b/drivers/s390/cio/vfio_ccw_dr= v.c index 2ef189fe45ed..d807911b8ed5 100644 --- a/drivers/s390/cio/vfio_ccw_drv.c +++ b/drivers/s390/cio/vfio_ccw_drv.c @@ -3,9 +3,11 @@ * VFIO based Physical Subchannel device driver * * Copyright IBM Corp. 2017 + * Copyright Red Hat, Inc. 2019 * * Author(s): Dong Jia Shi * Xiao Feng Ren + * Cornelia Huck */ =20 #include @@ -23,6 +25,7 @@ =20 struct workqueue_struct *vfio_ccw_work_q; static struct kmem_cache *vfio_ccw_io_region; +static struct kmem_cache *vfio_ccw_cmd_region; =20 /* * Helpers @@ -104,7 +107,7 @@ static int vfio_ccw_sch_probe(struct subchannel *sch) { struct pmcw *pmcw =3D &sch->schib.pmcw; struct vfio_ccw_private *private; - int ret; + int ret =3D -ENOMEM; =20 if (pmcw->qf) { dev_warn(&sch->dev, "vfio: ccw: does not support QDIO: %s\n", @@ -118,10 +121,13 @@ static int vfio_ccw_sch_probe(struct subchannel *sch) =20 private->io_region =3D kmem_cache_zalloc(vfio_ccw_io_region, GFP_KERNEL | GFP_DMA); - if (!private->io_region) { - kfree(private); - return -ENOMEM; - } + if (!private->io_region) + goto out_free; + + private->cmd_region =3D kmem_cache_zalloc(vfio_ccw_cmd_region, + GFP_KERNEL | GFP_DMA); + if (!private->cmd_region) + goto out_free; =20 private->sch =3D sch; dev_set_drvdata(&sch->dev, private); @@ -149,7 +155,10 @@ static int vfio_ccw_sch_probe(struct subchannel *sch) cio_disable_subchannel(sch); out_free: dev_set_drvdata(&sch->dev, NULL); - kmem_cache_free(vfio_ccw_io_region, private->io_region); + if (private->cmd_region) + kmem_cache_free(vfio_ccw_cmd_region, private->cmd_region); + if (private->io_region) + kmem_cache_free(vfio_ccw_io_region, private->io_region); kfree(private); return ret; } @@ -238,7 +247,7 @@ static struct css_driver vfio_ccw_sch_driver =3D { =20 static int __init vfio_ccw_sch_init(void) { - int ret; + int ret =3D -ENOMEM; =20 vfio_ccw_work_q =3D create_singlethread_workqueue("vfio-ccw"); if (!vfio_ccw_work_q) @@ -248,20 +257,30 @@ static int __init vfio_ccw_sch_init(void) sizeof(struct ccw_io_region), 0, SLAB_ACCOUNT, 0, sizeof(struct ccw_io_region), NULL); - if (!vfio_ccw_io_region) { - destroy_workqueue(vfio_ccw_work_q); - return -ENOMEM; - } + if (!vfio_ccw_io_region) + goto out_err; + + vfio_ccw_cmd_region =3D kmem_cache_create_usercopy("vfio_ccw_cmd_region", + sizeof(struct ccw_cmd_region), 0, + SLAB_ACCOUNT, 0, + sizeof(struct ccw_cmd_region), NULL); + if (!vfio_ccw_cmd_region) + goto out_err; =20 isc_register(VFIO_CCW_ISC); ret =3D css_driver_register(&vfio_ccw_sch_driver); if (ret) { isc_unregister(VFIO_CCW_ISC); - kmem_cache_destroy(vfio_ccw_io_region); - destroy_workqueue(vfio_ccw_work_q); + goto out_err; } =20 return ret; + +out_err: + kmem_cache_destroy(vfio_ccw_cmd_region); + kmem_cache_destroy(vfio_ccw_io_region); + destroy_workqueue(vfio_ccw_work_q); + return ret; } =20 static void __exit vfio_ccw_sch_exit(void) diff --git a/drivers/s390/cio/vfio_ccw_fsm.c b/drivers/s390/cio/vfio_ccw_fs= m.c index f6ed934cc565..72912d596181 100644 --- a/drivers/s390/cio/vfio_ccw_fsm.c +++ b/drivers/s390/cio/vfio_ccw_fsm.c @@ -3,8 +3,10 @@ * Finite state machine for vfio-ccw device handling * * Copyright IBM Corp. 2017 + * Copyright Red Hat, Inc. 2019 * * Author(s): Dong Jia Shi + * Cornelia Huck */ =20 #include @@ -69,6 +71,81 @@ static int fsm_io_helper(struct vfio_ccw_private *privat= e) return ret; } =20 +static int fsm_do_halt(struct vfio_ccw_private *private) +{ + struct subchannel *sch; + unsigned long flags; + int ccode; + int ret; + + sch =3D private->sch; + + spin_lock_irqsave(sch->lock, flags); + + /* Issue "Halt Subchannel" */ + ccode =3D hsch(sch->schid); + + switch (ccode) { + case 0: + /* + * Initialize device status information + */ + sch->schib.scsw.cmd.actl |=3D SCSW_ACTL_HALT_PEND; + ret =3D 0; + private->state =3D VFIO_CCW_STATE_BUSY; + break; + case 1: /* Status pending */ + case 2: /* Busy */ + ret =3D -EBUSY; + break; + case 3: /* Device not operational */ + { + ret =3D -ENODEV; + break; + } + default: + ret =3D ccode; + } + spin_unlock_irqrestore(sch->lock, flags); + return ret; +} + +static int fsm_do_clear(struct vfio_ccw_private *private) +{ + struct subchannel *sch; + unsigned long flags; + int ccode; + int ret; + + sch =3D private->sch; + + spin_lock_irqsave(sch->lock, flags); + + /* Issue "Clear Subchannel" */ + ccode =3D csch(sch->schid); + + switch (ccode) { + case 0: + /* + * Initialize device status information + */ + sch->schib.scsw.cmd.actl =3D SCSW_ACTL_CLEAR_PEND; + /* TODO: check what else we might need to clear */ + ret =3D 0; + private->state =3D VFIO_CCW_STATE_BUSY; + break; + case 3: /* Device not operational */ + { + ret =3D -ENODEV; + break; + } + default: + ret =3D ccode; + } + spin_unlock_irqrestore(sch->lock, flags); + return ret; +} + static void fsm_notoper(struct vfio_ccw_private *private, enum vfio_ccw_event event) { @@ -103,6 +180,14 @@ static void fsm_io_busy(struct vfio_ccw_private *priva= te, private->io_region->ret_code =3D -EAGAIN; } =20 +static void fsm_async_error(struct vfio_ccw_private *private, + enum vfio_ccw_event event) +{ + pr_err("vfio-ccw: FSM: halt/clear request from state:%d\n", + private->state); + private->cmd_region->ret_code =3D -EIO; +} + static void fsm_disabled_irq(struct vfio_ccw_private *private, enum vfio_ccw_event event) { @@ -165,11 +250,11 @@ static void fsm_io_request(struct vfio_ccw_private *p= rivate, } return; } else if (scsw->cmd.fctl & SCSW_FCTL_HALT_FUNC) { - /* XXX: Handle halt. */ + /* halt is handled via the async cmd region */ io_region->ret_code =3D -EOPNOTSUPP; goto err_out; } else if (scsw->cmd.fctl & SCSW_FCTL_CLEAR_FUNC) { - /* XXX: Handle clear. */ + /* clear is handled via the async cmd region */ io_region->ret_code =3D -EOPNOTSUPP; goto err_out; } @@ -179,6 +264,27 @@ static void fsm_io_request(struct vfio_ccw_private *pr= ivate, io_region->ret_code, errstr); } =20 +/* + * Deal with an async request from userspace. + */ +static void fsm_async_request(struct vfio_ccw_private *private, + enum vfio_ccw_event event) +{ + struct ccw_cmd_region *cmd_region =3D private->cmd_region; + + switch (cmd_region->command) { + case VFIO_CCW_ASYNC_CMD_HSCH: + cmd_region->ret_code =3D fsm_do_halt(private); + break; + case VFIO_CCW_ASYNC_CMD_CSCH: + cmd_region->ret_code =3D fsm_do_clear(private); + break; + default: + /* should not happen? */ + cmd_region->ret_code =3D -EINVAL; + } +} + /* * Got an interrupt for a normal io (state busy). */ @@ -202,21 +308,25 @@ fsm_func_t *vfio_ccw_jumptable[NR_VFIO_CCW_STATES][NR= _VFIO_CCW_EVENTS] =3D { [VFIO_CCW_STATE_NOT_OPER] =3D { [VFIO_CCW_EVENT_NOT_OPER] =3D fsm_nop, [VFIO_CCW_EVENT_IO_REQ] =3D fsm_io_error, + [VFIO_CCW_EVENT_ASYNC_REQ] =3D fsm_async_error, [VFIO_CCW_EVENT_INTERRUPT] =3D fsm_disabled_irq, }, [VFIO_CCW_STATE_STANDBY] =3D { [VFIO_CCW_EVENT_NOT_OPER] =3D fsm_notoper, [VFIO_CCW_EVENT_IO_REQ] =3D fsm_io_error, + [VFIO_CCW_EVENT_ASYNC_REQ] =3D fsm_async_error, [VFIO_CCW_EVENT_INTERRUPT] =3D fsm_irq, }, [VFIO_CCW_STATE_IDLE] =3D { [VFIO_CCW_EVENT_NOT_OPER] =3D fsm_notoper, [VFIO_CCW_EVENT_IO_REQ] =3D fsm_io_request, + [VFIO_CCW_EVENT_ASYNC_REQ] =3D fsm_async_request, [VFIO_CCW_EVENT_INTERRUPT] =3D fsm_irq, }, [VFIO_CCW_STATE_BUSY] =3D { [VFIO_CCW_EVENT_NOT_OPER] =3D fsm_notoper, [VFIO_CCW_EVENT_IO_REQ] =3D fsm_io_busy, + [VFIO_CCW_EVENT_ASYNC_REQ] =3D fsm_async_request, [VFIO_CCW_EVENT_INTERRUPT] =3D fsm_irq, }, }; diff --git a/drivers/s390/cio/vfio_ccw_ops.c b/drivers/s390/cio/vfio_ccw_op= s.c index 5a89d09f9271..755806cb8d53 100644 --- a/drivers/s390/cio/vfio_ccw_ops.c +++ b/drivers/s390/cio/vfio_ccw_ops.c @@ -148,11 +148,20 @@ static int vfio_ccw_mdev_open(struct mdev_device *mde= v) struct vfio_ccw_private *private =3D dev_get_drvdata(mdev_parent_dev(mdev)); unsigned long events =3D VFIO_IOMMU_NOTIFY_DMA_UNMAP; + int ret; =20 private->nb.notifier_call =3D vfio_ccw_mdev_notifier; =20 - return vfio_register_notifier(mdev_dev(mdev), VFIO_IOMMU_NOTIFY, - &events, &private->nb); + ret =3D vfio_register_notifier(mdev_dev(mdev), VFIO_IOMMU_NOTIFY, + &events, &private->nb); + if (ret) + return ret; + + ret =3D vfio_ccw_register_async_dev_regions(private); + if (ret) + vfio_unregister_notifier(mdev_dev(mdev), VFIO_IOMMU_NOTIFY, + &private->nb); + return ret; } =20 static void vfio_ccw_mdev_release(struct mdev_device *mdev) diff --git a/drivers/s390/cio/vfio_ccw_private.h b/drivers/s390/cio/vfio_cc= w_private.h index 20e75f4f3695..ed8b94ea2f08 100644 --- a/drivers/s390/cio/vfio_ccw_private.h +++ b/drivers/s390/cio/vfio_ccw_private.h @@ -31,9 +31,9 @@ struct vfio_ccw_private; struct vfio_ccw_region; =20 struct vfio_ccw_regops { - size_t (*read)(struct vfio_ccw_private *private, char __user *buf, + ssize_t (*read)(struct vfio_ccw_private *private, char __user *buf, size_t count, loff_t *ppos); - size_t (*write)(struct vfio_ccw_private *private, + ssize_t (*write)(struct vfio_ccw_private *private, const char __user *buf, size_t count, loff_t *ppos); void (*release)(struct vfio_ccw_private *private, struct vfio_ccw_region *region); @@ -53,6 +53,8 @@ int vfio_ccw_register_dev_region(struct vfio_ccw_private = *private, const struct vfio_ccw_regops *ops, size_t size, u32 flags, void *data); =20 +int vfio_ccw_register_async_dev_regions(struct vfio_ccw_private *private); + /** * struct vfio_ccw_private * @sch: pointer to the subchannel @@ -64,6 +66,7 @@ int vfio_ccw_register_dev_region(struct vfio_ccw_private = *private, * @io_region: MMIO region to input/output I/O arguments/results * @io_mutex: protect against concurrent update of I/O structures * @region: additional regions for other subchannel operations + * @cmd_region: MMIO region for asynchronous I/O commands other than START * @num_regions: number of additional regions * @cp: channel program for the current I/O operation * @irb: irb info received from interrupt @@ -81,6 +84,7 @@ struct vfio_ccw_private { struct ccw_io_region *io_region; struct mutex io_mutex; struct vfio_ccw_region *region; + struct ccw_cmd_region *cmd_region; int num_regions; =20 struct channel_program cp; @@ -115,6 +119,7 @@ enum vfio_ccw_event { VFIO_CCW_EVENT_NOT_OPER, VFIO_CCW_EVENT_IO_REQ, VFIO_CCW_EVENT_INTERRUPT, + VFIO_CCW_EVENT_ASYNC_REQ, /* last element! */ NR_VFIO_CCW_EVENTS }; diff --git a/include/uapi/linux/vfio.h b/include/uapi/linux/vfio.h index 56e2413d3e00..8f10748dac79 100644 --- a/include/uapi/linux/vfio.h +++ b/include/uapi/linux/vfio.h @@ -354,6 +354,8 @@ struct vfio_region_gfx_edid { }; =20 #define VFIO_REGION_TYPE_CCW (2) +/* ccw sub-types */ +#define VFIO_REGION_SUBTYPE_CCW_ASYNC_CMD (1) =20 /* * 10de vendor sub-type diff --git a/include/uapi/linux/vfio_ccw.h b/include/uapi/linux/vfio_ccw.h index 2ec5f367ff78..cbecbf0cd54f 100644 --- a/include/uapi/linux/vfio_ccw.h +++ b/include/uapi/linux/vfio_ccw.h @@ -12,6 +12,7 @@ =20 #include =20 +/* used for START SUBCHANNEL, always present */ struct ccw_io_region { #define ORB_AREA_SIZE 12 __u8 orb_area[ORB_AREA_SIZE]; @@ -22,4 +23,15 @@ struct ccw_io_region { __u32 ret_code; } __packed; =20 +/* + * used for processing commands that trigger asynchronous actions + * Note: this is controlled by a capability + */ +#define VFIO_CCW_ASYNC_CMD_HSCH (1 << 0) +#define VFIO_CCW_ASYNC_CMD_CSCH (1 << 1) +struct ccw_cmd_region { + __u32 command; + __u32 ret_code; +} __packed; + #endif --=20 2.17.2