From patchwork Thu Apr 25 16:56:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gerd Bayer X-Patchwork-Id: 13643551 Received: from mx0b-001b2d01.pphosted.com (mx0b-001b2d01.pphosted.com [148.163.158.5]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EB84E14A4F3; Thu, 25 Apr 2024 16:56:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=148.163.158.5 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714064179; cv=none; b=Mn7DoZz2hjXuZdSkz2WOALT4uFB0PwSjlNwQ47SaqzGDklE4nwHXbqAjIh9v1IBiAW+TllIYjtdCMrElIsjsAhIlECcSl4CkmBbB8WCFHWDbr7a3yoOIXrXqHfWe1buB1Ym0i3cW7VadvJkLlaiPqZwGIS3xuwMnG/bII23Mvak= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714064179; c=relaxed/simple; bh=SisP7t0aOU0K9XoM4CIRfkH+FFXw15YnuMHUkrOIY6Y=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=aCg71q1wX/am9P6RWujUE0cQ+zPScKU0ZHe1zu6todAMTzDDJk5VFbmH8uIfIdG+iI6gWhYhVwCbfgVvjw7vR2hHLcKuvo66sECwAOYy3kaBKLFMpvcmvu0bhOFvz7l3ioRyCmU4k2aOOQZ90uyAWFV/VwJ7ivuUDzmJMEw6RWM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.ibm.com; spf=pass smtp.mailfrom=linux.ibm.com; dkim=pass (2048-bit key) header.d=ibm.com header.i=@ibm.com header.b=stKRNOrS; arc=none smtp.client-ip=148.163.158.5 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.ibm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.ibm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=ibm.com header.i=@ibm.com header.b="stKRNOrS" Received: from pps.filterd (m0356516.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 43PGlsiI009934; Thu, 25 Apr 2024 16:56:14 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding; s=pp1; bh=FfGTrmG9OKf99H7XrOnirk2kRH2mP5iiuwqpCjlOEp0=; b=stKRNOrS7lBoL7CShoNEVNJpyW707e7efdK2UuAc7eZPmSYovozsgd6YVi+kPoZpiPWS T50t2SdzIMOjbsLPM6DMtGfvVbGDwadjIbVhWgNeCfNSLjyGXgMmpFyjchktkeqUc5/R r5b7QiOUKMLqNmUpCm2f51m218loiQUTY346MEQtbuvTdON+cVXQ3Msuv9WUjZdXApZG F0vcZ2CwLI8xToD0uQvoLqXX8/wfWFq7ojhTcQYbrB2IBK5Lme/RAjM2h6ooaeRCvu+P TEOgyo5WWOoOQ8iL5TiO7Ag/SfNxGy6Y1DY1IXA+f+UyfTJAQAiSPKhb3aJAPlx8y1zG +A== Received: from pps.reinject (localhost [127.0.0.1]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3xqu0rr0pg-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 25 Apr 2024 16:56:14 +0000 Received: from m0356516.ppops.net (m0356516.ppops.net [127.0.0.1]) by pps.reinject (8.17.1.5/8.17.1.5) with ESMTP id 43PGuDP8021280; Thu, 25 Apr 2024 16:56:13 GMT Received: from ppma23.wdc07v.mail.ibm.com (5d.69.3da9.ip4.static.sl-reverse.com [169.61.105.93]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3xqu0rr0pd-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 25 Apr 2024 16:56:13 +0000 Received: from pps.filterd (ppma23.wdc07v.mail.ibm.com [127.0.0.1]) by ppma23.wdc07v.mail.ibm.com (8.17.1.19/8.17.1.19) with ESMTP id 43PFCnED015308; Thu, 25 Apr 2024 16:56:13 GMT Received: from smtprelay05.fra02v.mail.ibm.com ([9.218.2.225]) by ppma23.wdc07v.mail.ibm.com (PPS) with ESMTPS id 3xmshmjq6m-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 25 Apr 2024 16:56:12 +0000 Received: from smtpav01.fra02v.mail.ibm.com (smtpav01.fra02v.mail.ibm.com [10.20.54.100]) by smtprelay05.fra02v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 43PGu7rv50135410 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 25 Apr 2024 16:56:09 GMT Received: from smtpav01.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id F27D720043; Thu, 25 Apr 2024 16:56:06 +0000 (GMT) Received: from smtpav01.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id CCC482005A; Thu, 25 Apr 2024 16:56:06 +0000 (GMT) Received: from dilbert5.boeblingen.de.ibm.com (unknown [9.152.212.201]) by smtpav01.fra02v.mail.ibm.com (Postfix) with ESMTP; Thu, 25 Apr 2024 16:56:06 +0000 (GMT) From: Gerd Bayer To: Alex Williamson , Jason Gunthorpe , Niklas Schnelle Cc: kvm@vger.kernel.org, linux-s390@vger.kernel.org, Ankit Agrawal , Yishai Hadas , Halil Pasic , Julian Ruess , Gerd Bayer Subject: [PATCH v3 1/3] vfio/pci: Extract duplicated code into macro Date: Thu, 25 Apr 2024 18:56:02 +0200 Message-ID: <20240425165604.899447-2-gbayer@linux.ibm.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240425165604.899447-1-gbayer@linux.ibm.com> References: <20240425165604.899447-1-gbayer@linux.ibm.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-TM-AS-GCONF: 00 X-Proofpoint-GUID: rQeIgPrSbeT94AHVD8fcAwxrlYVY-fF0 X-Proofpoint-ORIG-GUID: MoMioSCHYzmBwx3j_IxlitInPaDGIYaE X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1011,Hydra:6.0.650,FMLib:17.11.176.26 definitions=2024-04-25_16,2024-04-25_01,2023-05-22_02 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 suspectscore=0 malwarescore=0 phishscore=0 bulkscore=0 adultscore=0 mlxscore=0 clxscore=1015 impostorscore=0 priorityscore=1501 mlxlogscore=999 spamscore=0 lowpriorityscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2404010000 definitions=main-2404250123 vfio_pci_core_do_io_rw() repeats the same code for multiple access widths. Factor this out into a macro Suggested-by: Alex Williamson Signed-off-by: Gerd Bayer --- drivers/vfio/pci/vfio_pci_rdwr.c | 106 ++++++++++++++----------------- 1 file changed, 46 insertions(+), 60 deletions(-) diff --git a/drivers/vfio/pci/vfio_pci_rdwr.c b/drivers/vfio/pci/vfio_pci_rdwr.c index 03b8f7ada1ac..3335f1b868b1 100644 --- a/drivers/vfio/pci/vfio_pci_rdwr.c +++ b/drivers/vfio/pci/vfio_pci_rdwr.c @@ -90,6 +90,40 @@ VFIO_IOREAD(8) VFIO_IOREAD(16) VFIO_IOREAD(32) +#define VFIO_IORDWR(size) \ +static int vfio_pci_core_iordwr##size(struct vfio_pci_core_device *vdev,\ + bool iswrite, bool test_mem, \ + void __iomem *io, char __user *buf, \ + loff_t off, size_t *filled) \ +{ \ + u##size val; \ + int ret; \ + \ + if (iswrite) { \ + if (copy_from_user(&val, buf, sizeof(val))) \ + return -EFAULT; \ + \ + ret = vfio_pci_core_iowrite##size(vdev, test_mem, \ + val, io + off); \ + if (ret) \ + return ret; \ + } else { \ + ret = vfio_pci_core_ioread##size(vdev, test_mem, \ + &val, io + off); \ + if (ret) \ + return ret; \ + \ + if (copy_to_user(buf, &val, sizeof(val))) \ + return -EFAULT; \ + } \ + \ + *filled = sizeof(val); \ + return 0; \ +} \ + +VFIO_IORDWR(8) +VFIO_IORDWR(16) +VFIO_IORDWR(32) /* * Read or write from an __iomem region (MMIO or I/O port) with an excluded * range which is inaccessible. The excluded range drops writes and fills @@ -115,71 +149,23 @@ ssize_t vfio_pci_core_do_io_rw(struct vfio_pci_core_device *vdev, bool test_mem, fillable = 0; if (fillable >= 4 && !(off % 4)) { - u32 val; - - if (iswrite) { - if (copy_from_user(&val, buf, 4)) - return -EFAULT; - - ret = vfio_pci_core_iowrite32(vdev, test_mem, - val, io + off); - if (ret) - return ret; - } else { - ret = vfio_pci_core_ioread32(vdev, test_mem, - &val, io + off); - if (ret) - return ret; - - if (copy_to_user(buf, &val, 4)) - return -EFAULT; - } + ret = vfio_pci_core_iordwr32(vdev, iswrite, test_mem, + io, buf, off, &filled); + if (ret) + return ret; - filled = 4; } else if (fillable >= 2 && !(off % 2)) { - u16 val; - - if (iswrite) { - if (copy_from_user(&val, buf, 2)) - return -EFAULT; - - ret = vfio_pci_core_iowrite16(vdev, test_mem, - val, io + off); - if (ret) - return ret; - } else { - ret = vfio_pci_core_ioread16(vdev, test_mem, - &val, io + off); - if (ret) - return ret; - - if (copy_to_user(buf, &val, 2)) - return -EFAULT; - } + ret = vfio_pci_core_iordwr16(vdev, iswrite, test_mem, + io, buf, off, &filled); + if (ret) + return ret; - filled = 2; } else if (fillable) { - u8 val; - - if (iswrite) { - if (copy_from_user(&val, buf, 1)) - return -EFAULT; - - ret = vfio_pci_core_iowrite8(vdev, test_mem, - val, io + off); - if (ret) - return ret; - } else { - ret = vfio_pci_core_ioread8(vdev, test_mem, - &val, io + off); - if (ret) - return ret; - - if (copy_to_user(buf, &val, 1)) - return -EFAULT; - } + ret = vfio_pci_core_iordwr8(vdev, iswrite, test_mem, + io, buf, off, &filled); + if (ret) + return ret; - filled = 1; } else { /* Fill reads with -1, drop writes */ filled = min(count, (size_t)(x_end - off)); From patchwork Thu Apr 25 16:56:03 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gerd Bayer X-Patchwork-Id: 13643552 Received: from mx0b-001b2d01.pphosted.com (mx0b-001b2d01.pphosted.com [148.163.158.5]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B54A4131746; Thu, 25 Apr 2024 16:56:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=148.163.158.5 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714064179; cv=none; b=qvweZ/xjIzYDVQ9WHg3QfBFnGYVigySKjjjIoAtU0AJltPdk3DVxB3YFwoJwr1gSG1D6jn+xoHADQCg5tjRc9UFfMPTQfOB9cuSNMQHyvWawmaghgf1L4NoBghzNWgAm6+tap+YNqsCLm8TRztO7S6ilyD7coARd9bmbC1+6SvY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714064179; c=relaxed/simple; bh=wYYRpd/cMkTAj5ejeAzN2MLPibc7ge3K5fVg9p+8EpY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=uVI5qVIZDR7YONnIEvXm26INKfxIDQi/yFB066rYsKWBLrxWq/NMr/V0/Vu4YUWF7pdcpeaDzdtFD7AQ2tskbmfjVYaupVoHTYy0qLAEtuMgPEId1CfXwMKWXpHLhstiNUySoxt5fSVGMRvFCn0FxMcNjz7d2KBkj/VhFIdqRBs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.ibm.com; spf=pass smtp.mailfrom=linux.ibm.com; dkim=pass (2048-bit key) header.d=ibm.com header.i=@ibm.com header.b=LsRklNZe; arc=none smtp.client-ip=148.163.158.5 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.ibm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.ibm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=ibm.com header.i=@ibm.com header.b="LsRklNZe" Received: from pps.filterd (m0353724.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 43PGgwC7030444; Thu, 25 Apr 2024 16:56:14 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding; s=pp1; bh=sluqFAkXX0/bNL/kTW0cN/G08T4ZJaLY2/JQj0PumKY=; b=LsRklNZeUbGUqDawfQbAqKRGp+qL9qrZrsxYp51G7WKXxSIm9+AFvu2fRoyJ/HdJ5J15 HCzQuqhrRJyQvfVIdd41X7bd2ePda2AmZILWQthQnLeTwHA4d4Rd9q3ImBdS7eow+/Hd b1udpqEpdTZXSTinz0Kk/Tsv1kOPlYjFBL3dMFn34LZshhadImGU5MGOBcEyzzxlWtGR ecKgGBCy9VhssmtX26T70E46boBlkgHl+fejkCcXFpmniI6k+zLHoipRBr/6AFtnBj84 JcJjMjtH6wBxY2uWQoqApM3CMgN0a+XQWXaQEuZM5N1LkveOJAphPEtlaIPodhzptyop Xg== Received: from pps.reinject (localhost [127.0.0.1]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3xqtxf01g5-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 25 Apr 2024 16:56:13 +0000 Received: from m0353724.ppops.net (m0353724.ppops.net [127.0.0.1]) by pps.reinject (8.17.1.5/8.17.1.5) with ESMTP id 43PGuD20021039; Thu, 25 Apr 2024 16:56:13 GMT Received: from ppma11.dal12v.mail.ibm.com (db.9e.1632.ip4.static.sl-reverse.com [50.22.158.219]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3xqtxf01g1-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 25 Apr 2024 16:56:13 +0000 Received: from pps.filterd (ppma11.dal12v.mail.ibm.com [127.0.0.1]) by ppma11.dal12v.mail.ibm.com (8.17.1.19/8.17.1.19) with ESMTP id 43PGWGL8028319; Thu, 25 Apr 2024 16:56:12 GMT Received: from smtprelay05.fra02v.mail.ibm.com ([9.218.2.225]) by ppma11.dal12v.mail.ibm.com (PPS) with ESMTPS id 3xmtr2tcud-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 25 Apr 2024 16:56:12 +0000 Received: from smtpav01.fra02v.mail.ibm.com (smtpav01.fra02v.mail.ibm.com [10.20.54.100]) by smtprelay05.fra02v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 43PGu7Dp48693532 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 25 Apr 2024 16:56:09 GMT Received: from smtpav01.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 2ABF02005A; Thu, 25 Apr 2024 16:56:07 +0000 (GMT) Received: from smtpav01.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 031F820063; Thu, 25 Apr 2024 16:56:07 +0000 (GMT) Received: from dilbert5.boeblingen.de.ibm.com (unknown [9.152.212.201]) by smtpav01.fra02v.mail.ibm.com (Postfix) with ESMTP; Thu, 25 Apr 2024 16:56:06 +0000 (GMT) From: Gerd Bayer To: Alex Williamson , Jason Gunthorpe , Niklas Schnelle Cc: kvm@vger.kernel.org, linux-s390@vger.kernel.org, Ankit Agrawal , Yishai Hadas , Halil Pasic , Julian Ruess , Ben Segal , Gerd Bayer Subject: [PATCH v3 2/3] vfio/pci: Support 8-byte PCI loads and stores Date: Thu, 25 Apr 2024 18:56:03 +0200 Message-ID: <20240425165604.899447-3-gbayer@linux.ibm.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240425165604.899447-1-gbayer@linux.ibm.com> References: <20240425165604.899447-1-gbayer@linux.ibm.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-TM-AS-GCONF: 00 X-Proofpoint-GUID: QPww_oYvfx4rOKFkd0Aqhe-xrffOeGvi X-Proofpoint-ORIG-GUID: ahoS4MJfzCzNwW1cGua9lvTr63t_G0dS X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1011,Hydra:6.0.650,FMLib:17.11.176.26 definitions=2024-04-25_16,2024-04-25_01,2023-05-22_02 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 lowpriorityscore=0 phishscore=0 adultscore=0 clxscore=1015 mlxscore=0 impostorscore=0 malwarescore=0 suspectscore=0 bulkscore=0 priorityscore=1501 mlxlogscore=748 spamscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2404010000 definitions=main-2404250123 From: Ben Segal Many PCI adapters can benefit or even require full 64bit read and write access to their registers. In order to enable work on user-space drivers for these devices add two new variations vfio_pci_core_io{read|write}64 of the existing access methods when the architecture supports 64-bit ioreads and iowrites. Signed-off-by: Ben Segal Co-developed-by: Gerd Bayer Signed-off-by: Gerd Bayer --- drivers/vfio/pci/vfio_pci_rdwr.c | 16 ++++++++++++++++ include/linux/vfio_pci_core.h | 3 +++ 2 files changed, 19 insertions(+) diff --git a/drivers/vfio/pci/vfio_pci_rdwr.c b/drivers/vfio/pci/vfio_pci_rdwr.c index 3335f1b868b1..8ed06edaee23 100644 --- a/drivers/vfio/pci/vfio_pci_rdwr.c +++ b/drivers/vfio/pci/vfio_pci_rdwr.c @@ -89,6 +89,9 @@ EXPORT_SYMBOL_GPL(vfio_pci_core_ioread##size); VFIO_IOREAD(8) VFIO_IOREAD(16) VFIO_IOREAD(32) +#ifdef ioread64 +VFIO_IOREAD(64) +#endif #define VFIO_IORDWR(size) \ static int vfio_pci_core_iordwr##size(struct vfio_pci_core_device *vdev,\ @@ -124,6 +127,10 @@ static int vfio_pci_core_iordwr##size(struct vfio_pci_core_device *vdev,\ VFIO_IORDWR(8) VFIO_IORDWR(16) VFIO_IORDWR(32) +#if defined(ioread64) && defined(iowrite64) +VFIO_IORDWR(64) +#endif + /* * Read or write from an __iomem region (MMIO or I/O port) with an excluded * range which is inaccessible. The excluded range drops writes and fills @@ -148,6 +155,15 @@ ssize_t vfio_pci_core_do_io_rw(struct vfio_pci_core_device *vdev, bool test_mem, else fillable = 0; +#if defined(ioread64) && defined(iowrite64) + if (fillable >= 8 && !(off % 8)) { + ret = vfio_pci_core_iordwr64(vdev, iswrite, test_mem, + io, buf, off, &filled); + if (ret) + return ret; + + } else +#endif /* defined(ioread64) && defined(iowrite64) */ if (fillable >= 4 && !(off % 4)) { ret = vfio_pci_core_iordwr32(vdev, iswrite, test_mem, io, buf, off, &filled); diff --git a/include/linux/vfio_pci_core.h b/include/linux/vfio_pci_core.h index a2c8b8bba711..f4cf5fd2350c 100644 --- a/include/linux/vfio_pci_core.h +++ b/include/linux/vfio_pci_core.h @@ -157,5 +157,8 @@ int vfio_pci_core_ioread##size(struct vfio_pci_core_device *vdev, \ VFIO_IOREAD_DECLATION(8) VFIO_IOREAD_DECLATION(16) VFIO_IOREAD_DECLATION(32) +#ifdef ioread64 +VFIO_IOREAD_DECLATION(64) +#endif #endif /* VFIO_PCI_CORE_H */ From patchwork Thu Apr 25 16:56:04 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gerd Bayer X-Patchwork-Id: 13643550 Received: from mx0b-001b2d01.pphosted.com (mx0b-001b2d01.pphosted.com [148.163.158.5]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2AE7714D439; Thu, 25 Apr 2024 16:56:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=148.163.158.5 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714064179; cv=none; b=bjBj3vtsbqZkycS2R6AiME5U9eYrWDGIt/Lg7DOFHxymxzinHLXi6SoOgt465LA6KbJ8yn4hd/ctb6WlDqse6SyYnN6/h2bK0WuL8w9nY8xqNxgXk35lbFi0o3dUMyJ4vsMoizqEv4/krqsvRZvTwXgp17NjU77c9eg8s3GuYWo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714064179; c=relaxed/simple; bh=aQopQx6OxXFwMj0TdW4oGFIjbPRA8+TCpMM18HBNXxw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=UW+kjVbUzwg5/uc1Lo3vwTxW0qf8oWWc1oKCPTz6bLR7lYpAsJtxOqBYiPhASOMYCqYwv7TBrV7arrw9hCC15wzewngSQ4r1FRVRAB0OB6rmgfJGB4yOsJA1ZAyb4hoONGNxftHeaDierDrfSa+fLw3+OrV4p0WV6GszzHQWEmo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.ibm.com; spf=pass smtp.mailfrom=linux.ibm.com; dkim=pass (2048-bit key) header.d=ibm.com header.i=@ibm.com header.b=FvekvzNY; arc=none smtp.client-ip=148.163.158.5 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.ibm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.ibm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=ibm.com header.i=@ibm.com header.b="FvekvzNY" Received: from pps.filterd (m0353724.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 43PGgvWm030332; Thu, 25 Apr 2024 16:56:14 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding; s=pp1; bh=Y8BJn3oGmuQTUDxwDR4DcRIKWQiKOWhEiZWZAO/4kmY=; b=FvekvzNYXxN+t3plpM6/z9COy07RNhLez6Rg7I5JJd7rlJNS+WOMBqKUvle+gpBg//CP SJQy/XUTlwljuAS20cGfYZCGBLNvh3SeWW5X3JeSCp46W+oQguoVC1zLgr991OoRs6f7 pdWwS6OvT0hv6FpdmMIqh8wPV4AfFkj55gK7Wd8fPlvNFJyTRnK580lZKXT+CEF8FO/j gpzhi1peGnYScZAqtD8my7w7ZAYwlDFxAX0cR3bEKK5MXQJiEaCCUU00rWLqTublz1dc im6pQg+U008r14CCQACO45hYDWxsZ3Rdrm4SShTF7/lxScD0NkLEy+P4phemBpc9xphD wg== Received: from pps.reinject (localhost [127.0.0.1]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3xqtxf01g8-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 25 Apr 2024 16:56:14 +0000 Received: from m0353724.ppops.net (m0353724.ppops.net [127.0.0.1]) by pps.reinject (8.17.1.5/8.17.1.5) with ESMTP id 43PGuDEa021044; Thu, 25 Apr 2024 16:56:13 GMT Received: from ppma23.wdc07v.mail.ibm.com (5d.69.3da9.ip4.static.sl-reverse.com [169.61.105.93]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3xqtxf01g3-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 25 Apr 2024 16:56:13 +0000 Received: from pps.filterd (ppma23.wdc07v.mail.ibm.com [127.0.0.1]) by ppma23.wdc07v.mail.ibm.com (8.17.1.19/8.17.1.19) with ESMTP id 43PGAcbf015302; Thu, 25 Apr 2024 16:56:13 GMT Received: from smtprelay05.fra02v.mail.ibm.com ([9.218.2.225]) by ppma23.wdc07v.mail.ibm.com (PPS) with ESMTPS id 3xmshmjq6p-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 25 Apr 2024 16:56:12 +0000 Received: from smtpav01.fra02v.mail.ibm.com (smtpav01.fra02v.mail.ibm.com [10.20.54.100]) by smtprelay05.fra02v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 43PGu7I450201004 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 25 Apr 2024 16:56:09 GMT Received: from smtpav01.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 536002004D; Thu, 25 Apr 2024 16:56:07 +0000 (GMT) Received: from smtpav01.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 2F5AC20065; Thu, 25 Apr 2024 16:56:07 +0000 (GMT) Received: from dilbert5.boeblingen.de.ibm.com (unknown [9.152.212.201]) by smtpav01.fra02v.mail.ibm.com (Postfix) with ESMTP; Thu, 25 Apr 2024 16:56:07 +0000 (GMT) From: Gerd Bayer To: Alex Williamson , Jason Gunthorpe , Niklas Schnelle Cc: kvm@vger.kernel.org, linux-s390@vger.kernel.org, Ankit Agrawal , Yishai Hadas , Halil Pasic , Julian Ruess , Gerd Bayer Subject: [PATCH v3 3/3] vfio/pci: Continue to refactor vfio_pci_core_do_io_rw Date: Thu, 25 Apr 2024 18:56:04 +0200 Message-ID: <20240425165604.899447-4-gbayer@linux.ibm.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240425165604.899447-1-gbayer@linux.ibm.com> References: <20240425165604.899447-1-gbayer@linux.ibm.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-TM-AS-GCONF: 00 X-Proofpoint-GUID: 4Z--wod7-PxpjBnLTQGw51X6rFLyhLvl X-Proofpoint-ORIG-GUID: s8Lq3pCM98slF7tCqobbZAcagwL264wl X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1011,Hydra:6.0.650,FMLib:17.11.176.26 definitions=2024-04-25_16,2024-04-25_01,2023-05-22_02 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 lowpriorityscore=0 phishscore=0 adultscore=0 clxscore=1015 mlxscore=0 impostorscore=0 malwarescore=0 suspectscore=0 bulkscore=0 priorityscore=1501 mlxlogscore=830 spamscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2404010000 definitions=main-2404250123 Convert if-elseif-chain into switch-case. Separate out and generalize the code from the if-clauses so the result can be used in the switch statement. Signed-off-by: Gerd Bayer --- drivers/vfio/pci/vfio_pci_rdwr.c | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/drivers/vfio/pci/vfio_pci_rdwr.c b/drivers/vfio/pci/vfio_pci_rdwr.c index 8ed06edaee23..634c00b03c71 100644 --- a/drivers/vfio/pci/vfio_pci_rdwr.c +++ b/drivers/vfio/pci/vfio_pci_rdwr.c @@ -131,6 +131,20 @@ VFIO_IORDWR(32) VFIO_IORDWR(64) #endif +static int fill_size(size_t fillable, loff_t off) +{ + unsigned int fill_size; +#if defined(ioread64) && defined(iowrite64) + for (fill_size = 8; fill_size >= 0; fill_size /= 2) { +#else + for (fill_size = 4; fill_size >= 0; fill_size /= 2) { +#endif /* defined(ioread64) && defined(iowrite64) */ + if (fillable >= fill_size && !(off % fill_size)) + return fill_size; + } + return -1; +} + /* * Read or write from an __iomem region (MMIO or I/O port) with an excluded * range which is inaccessible. The excluded range drops writes and fills @@ -155,34 +169,38 @@ ssize_t vfio_pci_core_do_io_rw(struct vfio_pci_core_device *vdev, bool test_mem, else fillable = 0; + switch (fill_size(fillable, off)) { #if defined(ioread64) && defined(iowrite64) - if (fillable >= 8 && !(off % 8)) { + case 8: ret = vfio_pci_core_iordwr64(vdev, iswrite, test_mem, io, buf, off, &filled); if (ret) return ret; + break; - } else #endif /* defined(ioread64) && defined(iowrite64) */ - if (fillable >= 4 && !(off % 4)) { + case 4: ret = vfio_pci_core_iordwr32(vdev, iswrite, test_mem, io, buf, off, &filled); if (ret) return ret; + break; - } else if (fillable >= 2 && !(off % 2)) { + case 2: ret = vfio_pci_core_iordwr16(vdev, iswrite, test_mem, io, buf, off, &filled); if (ret) return ret; + break; - } else if (fillable) { + case 1: ret = vfio_pci_core_iordwr8(vdev, iswrite, test_mem, io, buf, off, &filled); if (ret) return ret; + break; - } else { + default: /* Fill reads with -1, drop writes */ filled = min(count, (size_t)(x_end - off)); if (!iswrite) {