18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * VFIO PCI config space virtualization
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2012 Red Hat, Inc.  All rights reserved.
68c2ecf20Sopenharmony_ci *     Author: Alex Williamson <alex.williamson@redhat.com>
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * Derived from original vfio:
98c2ecf20Sopenharmony_ci * Copyright 2010 Cisco Systems, Inc.  All rights reserved.
108c2ecf20Sopenharmony_ci * Author: Tom Lyon, pugs@cisco.com
118c2ecf20Sopenharmony_ci */
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci/*
148c2ecf20Sopenharmony_ci * This code handles reading and writing of PCI configuration registers.
158c2ecf20Sopenharmony_ci * This is hairy because we want to allow a lot of flexibility to the
168c2ecf20Sopenharmony_ci * user driver, but cannot trust it with all of the config fields.
178c2ecf20Sopenharmony_ci * Tables determine which fields can be read and written, as well as
188c2ecf20Sopenharmony_ci * which fields are 'virtualized' - special actions and translations to
198c2ecf20Sopenharmony_ci * make it appear to the user that he has control, when in fact things
208c2ecf20Sopenharmony_ci * must be negotiated with the underlying OS.
218c2ecf20Sopenharmony_ci */
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci#include <linux/fs.h>
248c2ecf20Sopenharmony_ci#include <linux/pci.h>
258c2ecf20Sopenharmony_ci#include <linux/uaccess.h>
268c2ecf20Sopenharmony_ci#include <linux/vfio.h>
278c2ecf20Sopenharmony_ci#include <linux/slab.h>
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci#include "vfio_pci_private.h"
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci/* Fake capability ID for standard config space */
328c2ecf20Sopenharmony_ci#define PCI_CAP_ID_BASIC	0
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci#define is_bar(offset)	\
358c2ecf20Sopenharmony_ci	((offset >= PCI_BASE_ADDRESS_0 && offset < PCI_BASE_ADDRESS_5 + 4) || \
368c2ecf20Sopenharmony_ci	 (offset >= PCI_ROM_ADDRESS && offset < PCI_ROM_ADDRESS + 4))
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci/*
398c2ecf20Sopenharmony_ci * Lengths of PCI Config Capabilities
408c2ecf20Sopenharmony_ci *   0: Removed from the user visible capability list
418c2ecf20Sopenharmony_ci *   FF: Variable length
428c2ecf20Sopenharmony_ci */
438c2ecf20Sopenharmony_cistatic const u8 pci_cap_length[PCI_CAP_ID_MAX + 1] = {
448c2ecf20Sopenharmony_ci	[PCI_CAP_ID_BASIC]	= PCI_STD_HEADER_SIZEOF, /* pci config header */
458c2ecf20Sopenharmony_ci	[PCI_CAP_ID_PM]		= PCI_PM_SIZEOF,
468c2ecf20Sopenharmony_ci	[PCI_CAP_ID_AGP]	= PCI_AGP_SIZEOF,
478c2ecf20Sopenharmony_ci	[PCI_CAP_ID_VPD]	= PCI_CAP_VPD_SIZEOF,
488c2ecf20Sopenharmony_ci	[PCI_CAP_ID_SLOTID]	= 0,		/* bridge - don't care */
498c2ecf20Sopenharmony_ci	[PCI_CAP_ID_MSI]	= 0xFF,		/* 10, 14, 20, or 24 */
508c2ecf20Sopenharmony_ci	[PCI_CAP_ID_CHSWP]	= 0,		/* cpci - not yet */
518c2ecf20Sopenharmony_ci	[PCI_CAP_ID_PCIX]	= 0xFF,		/* 8 or 24 */
528c2ecf20Sopenharmony_ci	[PCI_CAP_ID_HT]		= 0xFF,		/* hypertransport */
538c2ecf20Sopenharmony_ci	[PCI_CAP_ID_VNDR]	= 0xFF,		/* variable */
548c2ecf20Sopenharmony_ci	[PCI_CAP_ID_DBG]	= 0,		/* debug - don't care */
558c2ecf20Sopenharmony_ci	[PCI_CAP_ID_CCRC]	= 0,		/* cpci - not yet */
568c2ecf20Sopenharmony_ci	[PCI_CAP_ID_SHPC]	= 0,		/* hotswap - not yet */
578c2ecf20Sopenharmony_ci	[PCI_CAP_ID_SSVID]	= 0,		/* bridge - don't care */
588c2ecf20Sopenharmony_ci	[PCI_CAP_ID_AGP3]	= 0,		/* AGP8x - not yet */
598c2ecf20Sopenharmony_ci	[PCI_CAP_ID_SECDEV]	= 0,		/* secure device not yet */
608c2ecf20Sopenharmony_ci	[PCI_CAP_ID_EXP]	= 0xFF,		/* 20 or 44 */
618c2ecf20Sopenharmony_ci	[PCI_CAP_ID_MSIX]	= PCI_CAP_MSIX_SIZEOF,
628c2ecf20Sopenharmony_ci	[PCI_CAP_ID_SATA]	= 0xFF,
638c2ecf20Sopenharmony_ci	[PCI_CAP_ID_AF]		= PCI_CAP_AF_SIZEOF,
648c2ecf20Sopenharmony_ci};
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci/*
678c2ecf20Sopenharmony_ci * Lengths of PCIe/PCI-X Extended Config Capabilities
688c2ecf20Sopenharmony_ci *   0: Removed or masked from the user visible capability list
698c2ecf20Sopenharmony_ci *   FF: Variable length
708c2ecf20Sopenharmony_ci */
718c2ecf20Sopenharmony_cistatic const u16 pci_ext_cap_length[PCI_EXT_CAP_ID_MAX + 1] = {
728c2ecf20Sopenharmony_ci	[PCI_EXT_CAP_ID_ERR]	=	PCI_ERR_ROOT_COMMAND,
738c2ecf20Sopenharmony_ci	[PCI_EXT_CAP_ID_VC]	=	0xFF,
748c2ecf20Sopenharmony_ci	[PCI_EXT_CAP_ID_DSN]	=	PCI_EXT_CAP_DSN_SIZEOF,
758c2ecf20Sopenharmony_ci	[PCI_EXT_CAP_ID_PWR]	=	PCI_EXT_CAP_PWR_SIZEOF,
768c2ecf20Sopenharmony_ci	[PCI_EXT_CAP_ID_RCLD]	=	0,	/* root only - don't care */
778c2ecf20Sopenharmony_ci	[PCI_EXT_CAP_ID_RCILC]	=	0,	/* root only - don't care */
788c2ecf20Sopenharmony_ci	[PCI_EXT_CAP_ID_RCEC]	=	0,	/* root only - don't care */
798c2ecf20Sopenharmony_ci	[PCI_EXT_CAP_ID_MFVC]	=	0xFF,
808c2ecf20Sopenharmony_ci	[PCI_EXT_CAP_ID_VC9]	=	0xFF,	/* same as CAP_ID_VC */
818c2ecf20Sopenharmony_ci	[PCI_EXT_CAP_ID_RCRB]	=	0,	/* root only - don't care */
828c2ecf20Sopenharmony_ci	[PCI_EXT_CAP_ID_VNDR]	=	0xFF,
838c2ecf20Sopenharmony_ci	[PCI_EXT_CAP_ID_CAC]	=	0,	/* obsolete */
848c2ecf20Sopenharmony_ci	[PCI_EXT_CAP_ID_ACS]	=	0xFF,
858c2ecf20Sopenharmony_ci	[PCI_EXT_CAP_ID_ARI]	=	PCI_EXT_CAP_ARI_SIZEOF,
868c2ecf20Sopenharmony_ci	[PCI_EXT_CAP_ID_ATS]	=	PCI_EXT_CAP_ATS_SIZEOF,
878c2ecf20Sopenharmony_ci	[PCI_EXT_CAP_ID_SRIOV]	=	PCI_EXT_CAP_SRIOV_SIZEOF,
888c2ecf20Sopenharmony_ci	[PCI_EXT_CAP_ID_MRIOV]	=	0,	/* not yet */
898c2ecf20Sopenharmony_ci	[PCI_EXT_CAP_ID_MCAST]	=	PCI_EXT_CAP_MCAST_ENDPOINT_SIZEOF,
908c2ecf20Sopenharmony_ci	[PCI_EXT_CAP_ID_PRI]	=	PCI_EXT_CAP_PRI_SIZEOF,
918c2ecf20Sopenharmony_ci	[PCI_EXT_CAP_ID_AMD_XXX] =	0,	/* not yet */
928c2ecf20Sopenharmony_ci	[PCI_EXT_CAP_ID_REBAR]	=	0xFF,
938c2ecf20Sopenharmony_ci	[PCI_EXT_CAP_ID_DPA]	=	0xFF,
948c2ecf20Sopenharmony_ci	[PCI_EXT_CAP_ID_TPH]	=	0xFF,
958c2ecf20Sopenharmony_ci	[PCI_EXT_CAP_ID_LTR]	=	PCI_EXT_CAP_LTR_SIZEOF,
968c2ecf20Sopenharmony_ci	[PCI_EXT_CAP_ID_SECPCI]	=	0,	/* not yet */
978c2ecf20Sopenharmony_ci	[PCI_EXT_CAP_ID_PMUX]	=	0,	/* not yet */
988c2ecf20Sopenharmony_ci	[PCI_EXT_CAP_ID_PASID]	=	0,	/* not yet */
998c2ecf20Sopenharmony_ci};
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci/*
1028c2ecf20Sopenharmony_ci * Read/Write Permission Bits - one bit for each bit in capability
1038c2ecf20Sopenharmony_ci * Any field can be read if it exists, but what is read depends on
1048c2ecf20Sopenharmony_ci * whether the field is 'virtualized', or just pass thru to the
1058c2ecf20Sopenharmony_ci * hardware.  Any virtualized field is also virtualized for writes.
1068c2ecf20Sopenharmony_ci * Writes are only permitted if they have a 1 bit here.
1078c2ecf20Sopenharmony_ci */
1088c2ecf20Sopenharmony_cistruct perm_bits {
1098c2ecf20Sopenharmony_ci	u8	*virt;		/* read/write virtual data, not hw */
1108c2ecf20Sopenharmony_ci	u8	*write;		/* writeable bits */
1118c2ecf20Sopenharmony_ci	int	(*readfn)(struct vfio_pci_device *vdev, int pos, int count,
1128c2ecf20Sopenharmony_ci			  struct perm_bits *perm, int offset, __le32 *val);
1138c2ecf20Sopenharmony_ci	int	(*writefn)(struct vfio_pci_device *vdev, int pos, int count,
1148c2ecf20Sopenharmony_ci			   struct perm_bits *perm, int offset, __le32 val);
1158c2ecf20Sopenharmony_ci};
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci#define	NO_VIRT		0
1188c2ecf20Sopenharmony_ci#define	ALL_VIRT	0xFFFFFFFFU
1198c2ecf20Sopenharmony_ci#define	NO_WRITE	0
1208c2ecf20Sopenharmony_ci#define	ALL_WRITE	0xFFFFFFFFU
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_cistatic int vfio_user_config_read(struct pci_dev *pdev, int offset,
1238c2ecf20Sopenharmony_ci				 __le32 *val, int count)
1248c2ecf20Sopenharmony_ci{
1258c2ecf20Sopenharmony_ci	int ret = -EINVAL;
1268c2ecf20Sopenharmony_ci	u32 tmp_val = 0;
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci	switch (count) {
1298c2ecf20Sopenharmony_ci	case 1:
1308c2ecf20Sopenharmony_ci	{
1318c2ecf20Sopenharmony_ci		u8 tmp;
1328c2ecf20Sopenharmony_ci		ret = pci_user_read_config_byte(pdev, offset, &tmp);
1338c2ecf20Sopenharmony_ci		tmp_val = tmp;
1348c2ecf20Sopenharmony_ci		break;
1358c2ecf20Sopenharmony_ci	}
1368c2ecf20Sopenharmony_ci	case 2:
1378c2ecf20Sopenharmony_ci	{
1388c2ecf20Sopenharmony_ci		u16 tmp;
1398c2ecf20Sopenharmony_ci		ret = pci_user_read_config_word(pdev, offset, &tmp);
1408c2ecf20Sopenharmony_ci		tmp_val = tmp;
1418c2ecf20Sopenharmony_ci		break;
1428c2ecf20Sopenharmony_ci	}
1438c2ecf20Sopenharmony_ci	case 4:
1448c2ecf20Sopenharmony_ci		ret = pci_user_read_config_dword(pdev, offset, &tmp_val);
1458c2ecf20Sopenharmony_ci		break;
1468c2ecf20Sopenharmony_ci	}
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci	*val = cpu_to_le32(tmp_val);
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ci	return ret;
1518c2ecf20Sopenharmony_ci}
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_cistatic int vfio_user_config_write(struct pci_dev *pdev, int offset,
1548c2ecf20Sopenharmony_ci				  __le32 val, int count)
1558c2ecf20Sopenharmony_ci{
1568c2ecf20Sopenharmony_ci	int ret = -EINVAL;
1578c2ecf20Sopenharmony_ci	u32 tmp_val = le32_to_cpu(val);
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci	switch (count) {
1608c2ecf20Sopenharmony_ci	case 1:
1618c2ecf20Sopenharmony_ci		ret = pci_user_write_config_byte(pdev, offset, tmp_val);
1628c2ecf20Sopenharmony_ci		break;
1638c2ecf20Sopenharmony_ci	case 2:
1648c2ecf20Sopenharmony_ci		ret = pci_user_write_config_word(pdev, offset, tmp_val);
1658c2ecf20Sopenharmony_ci		break;
1668c2ecf20Sopenharmony_ci	case 4:
1678c2ecf20Sopenharmony_ci		ret = pci_user_write_config_dword(pdev, offset, tmp_val);
1688c2ecf20Sopenharmony_ci		break;
1698c2ecf20Sopenharmony_ci	}
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_ci	return ret;
1728c2ecf20Sopenharmony_ci}
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_cistatic int vfio_default_config_read(struct vfio_pci_device *vdev, int pos,
1758c2ecf20Sopenharmony_ci				    int count, struct perm_bits *perm,
1768c2ecf20Sopenharmony_ci				    int offset, __le32 *val)
1778c2ecf20Sopenharmony_ci{
1788c2ecf20Sopenharmony_ci	__le32 virt = 0;
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_ci	memcpy(val, vdev->vconfig + pos, count);
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci	memcpy(&virt, perm->virt + offset, count);
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_ci	/* Any non-virtualized bits? */
1858c2ecf20Sopenharmony_ci	if (cpu_to_le32(~0U >> (32 - (count * 8))) != virt) {
1868c2ecf20Sopenharmony_ci		struct pci_dev *pdev = vdev->pdev;
1878c2ecf20Sopenharmony_ci		__le32 phys_val = 0;
1888c2ecf20Sopenharmony_ci		int ret;
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_ci		ret = vfio_user_config_read(pdev, pos, &phys_val, count);
1918c2ecf20Sopenharmony_ci		if (ret)
1928c2ecf20Sopenharmony_ci			return ret;
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ci		*val = (phys_val & ~virt) | (*val & virt);
1958c2ecf20Sopenharmony_ci	}
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ci	return count;
1988c2ecf20Sopenharmony_ci}
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_cistatic int vfio_default_config_write(struct vfio_pci_device *vdev, int pos,
2018c2ecf20Sopenharmony_ci				     int count, struct perm_bits *perm,
2028c2ecf20Sopenharmony_ci				     int offset, __le32 val)
2038c2ecf20Sopenharmony_ci{
2048c2ecf20Sopenharmony_ci	__le32 virt = 0, write = 0;
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_ci	memcpy(&write, perm->write + offset, count);
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_ci	if (!write)
2098c2ecf20Sopenharmony_ci		return count; /* drop, no writable bits */
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_ci	memcpy(&virt, perm->virt + offset, count);
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_ci	/* Virtualized and writable bits go to vconfig */
2148c2ecf20Sopenharmony_ci	if (write & virt) {
2158c2ecf20Sopenharmony_ci		__le32 virt_val = 0;
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_ci		memcpy(&virt_val, vdev->vconfig + pos, count);
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_ci		virt_val &= ~(write & virt);
2208c2ecf20Sopenharmony_ci		virt_val |= (val & (write & virt));
2218c2ecf20Sopenharmony_ci
2228c2ecf20Sopenharmony_ci		memcpy(vdev->vconfig + pos, &virt_val, count);
2238c2ecf20Sopenharmony_ci	}
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_ci	/* Non-virtualzed and writable bits go to hardware */
2268c2ecf20Sopenharmony_ci	if (write & ~virt) {
2278c2ecf20Sopenharmony_ci		struct pci_dev *pdev = vdev->pdev;
2288c2ecf20Sopenharmony_ci		__le32 phys_val = 0;
2298c2ecf20Sopenharmony_ci		int ret;
2308c2ecf20Sopenharmony_ci
2318c2ecf20Sopenharmony_ci		ret = vfio_user_config_read(pdev, pos, &phys_val, count);
2328c2ecf20Sopenharmony_ci		if (ret)
2338c2ecf20Sopenharmony_ci			return ret;
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_ci		phys_val &= ~(write & ~virt);
2368c2ecf20Sopenharmony_ci		phys_val |= (val & (write & ~virt));
2378c2ecf20Sopenharmony_ci
2388c2ecf20Sopenharmony_ci		ret = vfio_user_config_write(pdev, pos, phys_val, count);
2398c2ecf20Sopenharmony_ci		if (ret)
2408c2ecf20Sopenharmony_ci			return ret;
2418c2ecf20Sopenharmony_ci	}
2428c2ecf20Sopenharmony_ci
2438c2ecf20Sopenharmony_ci	return count;
2448c2ecf20Sopenharmony_ci}
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_ci/* Allow direct read from hardware, except for capability next pointer */
2478c2ecf20Sopenharmony_cistatic int vfio_direct_config_read(struct vfio_pci_device *vdev, int pos,
2488c2ecf20Sopenharmony_ci				   int count, struct perm_bits *perm,
2498c2ecf20Sopenharmony_ci				   int offset, __le32 *val)
2508c2ecf20Sopenharmony_ci{
2518c2ecf20Sopenharmony_ci	int ret;
2528c2ecf20Sopenharmony_ci
2538c2ecf20Sopenharmony_ci	ret = vfio_user_config_read(vdev->pdev, pos, val, count);
2548c2ecf20Sopenharmony_ci	if (ret)
2558c2ecf20Sopenharmony_ci		return ret;
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_ci	if (pos >= PCI_CFG_SPACE_SIZE) { /* Extended cap header mangling */
2588c2ecf20Sopenharmony_ci		if (offset < 4)
2598c2ecf20Sopenharmony_ci			memcpy(val, vdev->vconfig + pos, count);
2608c2ecf20Sopenharmony_ci	} else if (pos >= PCI_STD_HEADER_SIZEOF) { /* Std cap mangling */
2618c2ecf20Sopenharmony_ci		if (offset == PCI_CAP_LIST_ID && count > 1)
2628c2ecf20Sopenharmony_ci			memcpy(val, vdev->vconfig + pos,
2638c2ecf20Sopenharmony_ci			       min(PCI_CAP_FLAGS, count));
2648c2ecf20Sopenharmony_ci		else if (offset == PCI_CAP_LIST_NEXT)
2658c2ecf20Sopenharmony_ci			memcpy(val, vdev->vconfig + pos, 1);
2668c2ecf20Sopenharmony_ci	}
2678c2ecf20Sopenharmony_ci
2688c2ecf20Sopenharmony_ci	return count;
2698c2ecf20Sopenharmony_ci}
2708c2ecf20Sopenharmony_ci
2718c2ecf20Sopenharmony_ci/* Raw access skips any kind of virtualization */
2728c2ecf20Sopenharmony_cistatic int vfio_raw_config_write(struct vfio_pci_device *vdev, int pos,
2738c2ecf20Sopenharmony_ci				 int count, struct perm_bits *perm,
2748c2ecf20Sopenharmony_ci				 int offset, __le32 val)
2758c2ecf20Sopenharmony_ci{
2768c2ecf20Sopenharmony_ci	int ret;
2778c2ecf20Sopenharmony_ci
2788c2ecf20Sopenharmony_ci	ret = vfio_user_config_write(vdev->pdev, pos, val, count);
2798c2ecf20Sopenharmony_ci	if (ret)
2808c2ecf20Sopenharmony_ci		return ret;
2818c2ecf20Sopenharmony_ci
2828c2ecf20Sopenharmony_ci	return count;
2838c2ecf20Sopenharmony_ci}
2848c2ecf20Sopenharmony_ci
2858c2ecf20Sopenharmony_cistatic int vfio_raw_config_read(struct vfio_pci_device *vdev, int pos,
2868c2ecf20Sopenharmony_ci				int count, struct perm_bits *perm,
2878c2ecf20Sopenharmony_ci				int offset, __le32 *val)
2888c2ecf20Sopenharmony_ci{
2898c2ecf20Sopenharmony_ci	int ret;
2908c2ecf20Sopenharmony_ci
2918c2ecf20Sopenharmony_ci	ret = vfio_user_config_read(vdev->pdev, pos, val, count);
2928c2ecf20Sopenharmony_ci	if (ret)
2938c2ecf20Sopenharmony_ci		return ret;
2948c2ecf20Sopenharmony_ci
2958c2ecf20Sopenharmony_ci	return count;
2968c2ecf20Sopenharmony_ci}
2978c2ecf20Sopenharmony_ci
2988c2ecf20Sopenharmony_ci/* Virt access uses only virtualization */
2998c2ecf20Sopenharmony_cistatic int vfio_virt_config_write(struct vfio_pci_device *vdev, int pos,
3008c2ecf20Sopenharmony_ci				  int count, struct perm_bits *perm,
3018c2ecf20Sopenharmony_ci				  int offset, __le32 val)
3028c2ecf20Sopenharmony_ci{
3038c2ecf20Sopenharmony_ci	memcpy(vdev->vconfig + pos, &val, count);
3048c2ecf20Sopenharmony_ci	return count;
3058c2ecf20Sopenharmony_ci}
3068c2ecf20Sopenharmony_ci
3078c2ecf20Sopenharmony_cistatic int vfio_virt_config_read(struct vfio_pci_device *vdev, int pos,
3088c2ecf20Sopenharmony_ci				 int count, struct perm_bits *perm,
3098c2ecf20Sopenharmony_ci				 int offset, __le32 *val)
3108c2ecf20Sopenharmony_ci{
3118c2ecf20Sopenharmony_ci	memcpy(val, vdev->vconfig + pos, count);
3128c2ecf20Sopenharmony_ci	return count;
3138c2ecf20Sopenharmony_ci}
3148c2ecf20Sopenharmony_ci
3158c2ecf20Sopenharmony_ci/* Default capability regions to read-only, no-virtualization */
3168c2ecf20Sopenharmony_cistatic struct perm_bits cap_perms[PCI_CAP_ID_MAX + 1] = {
3178c2ecf20Sopenharmony_ci	[0 ... PCI_CAP_ID_MAX] = { .readfn = vfio_direct_config_read }
3188c2ecf20Sopenharmony_ci};
3198c2ecf20Sopenharmony_cistatic struct perm_bits ecap_perms[PCI_EXT_CAP_ID_MAX + 1] = {
3208c2ecf20Sopenharmony_ci	[0 ... PCI_EXT_CAP_ID_MAX] = { .readfn = vfio_direct_config_read }
3218c2ecf20Sopenharmony_ci};
3228c2ecf20Sopenharmony_ci/*
3238c2ecf20Sopenharmony_ci * Default unassigned regions to raw read-write access.  Some devices
3248c2ecf20Sopenharmony_ci * require this to function as they hide registers between the gaps in
3258c2ecf20Sopenharmony_ci * config space (be2net).  Like MMIO and I/O port registers, we have
3268c2ecf20Sopenharmony_ci * to trust the hardware isolation.
3278c2ecf20Sopenharmony_ci */
3288c2ecf20Sopenharmony_cistatic struct perm_bits unassigned_perms = {
3298c2ecf20Sopenharmony_ci	.readfn = vfio_raw_config_read,
3308c2ecf20Sopenharmony_ci	.writefn = vfio_raw_config_write
3318c2ecf20Sopenharmony_ci};
3328c2ecf20Sopenharmony_ci
3338c2ecf20Sopenharmony_cistatic struct perm_bits virt_perms = {
3348c2ecf20Sopenharmony_ci	.readfn = vfio_virt_config_read,
3358c2ecf20Sopenharmony_ci	.writefn = vfio_virt_config_write
3368c2ecf20Sopenharmony_ci};
3378c2ecf20Sopenharmony_ci
3388c2ecf20Sopenharmony_cistatic void free_perm_bits(struct perm_bits *perm)
3398c2ecf20Sopenharmony_ci{
3408c2ecf20Sopenharmony_ci	kfree(perm->virt);
3418c2ecf20Sopenharmony_ci	kfree(perm->write);
3428c2ecf20Sopenharmony_ci	perm->virt = NULL;
3438c2ecf20Sopenharmony_ci	perm->write = NULL;
3448c2ecf20Sopenharmony_ci}
3458c2ecf20Sopenharmony_ci
3468c2ecf20Sopenharmony_cistatic int alloc_perm_bits(struct perm_bits *perm, int size)
3478c2ecf20Sopenharmony_ci{
3488c2ecf20Sopenharmony_ci	/*
3498c2ecf20Sopenharmony_ci	 * Round up all permission bits to the next dword, this lets us
3508c2ecf20Sopenharmony_ci	 * ignore whether a read/write exceeds the defined capability
3518c2ecf20Sopenharmony_ci	 * structure.  We can do this because:
3528c2ecf20Sopenharmony_ci	 *  - Standard config space is already dword aligned
3538c2ecf20Sopenharmony_ci	 *  - Capabilities are all dword aligned (bits 0:1 of next reserved)
3548c2ecf20Sopenharmony_ci	 *  - Express capabilities defined as dword aligned
3558c2ecf20Sopenharmony_ci	 */
3568c2ecf20Sopenharmony_ci	size = round_up(size, 4);
3578c2ecf20Sopenharmony_ci
3588c2ecf20Sopenharmony_ci	/*
3598c2ecf20Sopenharmony_ci	 * Zero state is
3608c2ecf20Sopenharmony_ci	 * - All Readable, None Writeable, None Virtualized
3618c2ecf20Sopenharmony_ci	 */
3628c2ecf20Sopenharmony_ci	perm->virt = kzalloc(size, GFP_KERNEL);
3638c2ecf20Sopenharmony_ci	perm->write = kzalloc(size, GFP_KERNEL);
3648c2ecf20Sopenharmony_ci	if (!perm->virt || !perm->write) {
3658c2ecf20Sopenharmony_ci		free_perm_bits(perm);
3668c2ecf20Sopenharmony_ci		return -ENOMEM;
3678c2ecf20Sopenharmony_ci	}
3688c2ecf20Sopenharmony_ci
3698c2ecf20Sopenharmony_ci	perm->readfn = vfio_default_config_read;
3708c2ecf20Sopenharmony_ci	perm->writefn = vfio_default_config_write;
3718c2ecf20Sopenharmony_ci
3728c2ecf20Sopenharmony_ci	return 0;
3738c2ecf20Sopenharmony_ci}
3748c2ecf20Sopenharmony_ci
3758c2ecf20Sopenharmony_ci/*
3768c2ecf20Sopenharmony_ci * Helper functions for filling in permission tables
3778c2ecf20Sopenharmony_ci */
3788c2ecf20Sopenharmony_cistatic inline void p_setb(struct perm_bits *p, int off, u8 virt, u8 write)
3798c2ecf20Sopenharmony_ci{
3808c2ecf20Sopenharmony_ci	p->virt[off] = virt;
3818c2ecf20Sopenharmony_ci	p->write[off] = write;
3828c2ecf20Sopenharmony_ci}
3838c2ecf20Sopenharmony_ci
3848c2ecf20Sopenharmony_ci/* Handle endian-ness - pci and tables are little-endian */
3858c2ecf20Sopenharmony_cistatic inline void p_setw(struct perm_bits *p, int off, u16 virt, u16 write)
3868c2ecf20Sopenharmony_ci{
3878c2ecf20Sopenharmony_ci	*(__le16 *)(&p->virt[off]) = cpu_to_le16(virt);
3888c2ecf20Sopenharmony_ci	*(__le16 *)(&p->write[off]) = cpu_to_le16(write);
3898c2ecf20Sopenharmony_ci}
3908c2ecf20Sopenharmony_ci
3918c2ecf20Sopenharmony_ci/* Handle endian-ness - pci and tables are little-endian */
3928c2ecf20Sopenharmony_cistatic inline void p_setd(struct perm_bits *p, int off, u32 virt, u32 write)
3938c2ecf20Sopenharmony_ci{
3948c2ecf20Sopenharmony_ci	*(__le32 *)(&p->virt[off]) = cpu_to_le32(virt);
3958c2ecf20Sopenharmony_ci	*(__le32 *)(&p->write[off]) = cpu_to_le32(write);
3968c2ecf20Sopenharmony_ci}
3978c2ecf20Sopenharmony_ci
3988c2ecf20Sopenharmony_ci/* Caller should hold memory_lock semaphore */
3998c2ecf20Sopenharmony_cibool __vfio_pci_memory_enabled(struct vfio_pci_device *vdev)
4008c2ecf20Sopenharmony_ci{
4018c2ecf20Sopenharmony_ci	struct pci_dev *pdev = vdev->pdev;
4028c2ecf20Sopenharmony_ci	u16 cmd = le16_to_cpu(*(__le16 *)&vdev->vconfig[PCI_COMMAND]);
4038c2ecf20Sopenharmony_ci
4048c2ecf20Sopenharmony_ci	/*
4058c2ecf20Sopenharmony_ci	 * SR-IOV VF memory enable is handled by the MSE bit in the
4068c2ecf20Sopenharmony_ci	 * PF SR-IOV capability, there's therefore no need to trigger
4078c2ecf20Sopenharmony_ci	 * faults based on the virtual value.
4088c2ecf20Sopenharmony_ci	 */
4098c2ecf20Sopenharmony_ci	return pdev->no_command_memory || (cmd & PCI_COMMAND_MEMORY);
4108c2ecf20Sopenharmony_ci}
4118c2ecf20Sopenharmony_ci
4128c2ecf20Sopenharmony_ci/*
4138c2ecf20Sopenharmony_ci * Restore the *real* BARs after we detect a FLR or backdoor reset.
4148c2ecf20Sopenharmony_ci * (backdoor = some device specific technique that we didn't catch)
4158c2ecf20Sopenharmony_ci */
4168c2ecf20Sopenharmony_cistatic void vfio_bar_restore(struct vfio_pci_device *vdev)
4178c2ecf20Sopenharmony_ci{
4188c2ecf20Sopenharmony_ci	struct pci_dev *pdev = vdev->pdev;
4198c2ecf20Sopenharmony_ci	u32 *rbar = vdev->rbar;
4208c2ecf20Sopenharmony_ci	u16 cmd;
4218c2ecf20Sopenharmony_ci	int i;
4228c2ecf20Sopenharmony_ci
4238c2ecf20Sopenharmony_ci	if (pdev->is_virtfn)
4248c2ecf20Sopenharmony_ci		return;
4258c2ecf20Sopenharmony_ci
4268c2ecf20Sopenharmony_ci	pci_info(pdev, "%s: reset recovery - restoring BARs\n", __func__);
4278c2ecf20Sopenharmony_ci
4288c2ecf20Sopenharmony_ci	for (i = PCI_BASE_ADDRESS_0; i <= PCI_BASE_ADDRESS_5; i += 4, rbar++)
4298c2ecf20Sopenharmony_ci		pci_user_write_config_dword(pdev, i, *rbar);
4308c2ecf20Sopenharmony_ci
4318c2ecf20Sopenharmony_ci	pci_user_write_config_dword(pdev, PCI_ROM_ADDRESS, *rbar);
4328c2ecf20Sopenharmony_ci
4338c2ecf20Sopenharmony_ci	if (vdev->nointx) {
4348c2ecf20Sopenharmony_ci		pci_user_read_config_word(pdev, PCI_COMMAND, &cmd);
4358c2ecf20Sopenharmony_ci		cmd |= PCI_COMMAND_INTX_DISABLE;
4368c2ecf20Sopenharmony_ci		pci_user_write_config_word(pdev, PCI_COMMAND, cmd);
4378c2ecf20Sopenharmony_ci	}
4388c2ecf20Sopenharmony_ci}
4398c2ecf20Sopenharmony_ci
4408c2ecf20Sopenharmony_cistatic __le32 vfio_generate_bar_flags(struct pci_dev *pdev, int bar)
4418c2ecf20Sopenharmony_ci{
4428c2ecf20Sopenharmony_ci	unsigned long flags = pci_resource_flags(pdev, bar);
4438c2ecf20Sopenharmony_ci	u32 val;
4448c2ecf20Sopenharmony_ci
4458c2ecf20Sopenharmony_ci	if (flags & IORESOURCE_IO)
4468c2ecf20Sopenharmony_ci		return cpu_to_le32(PCI_BASE_ADDRESS_SPACE_IO);
4478c2ecf20Sopenharmony_ci
4488c2ecf20Sopenharmony_ci	val = PCI_BASE_ADDRESS_SPACE_MEMORY;
4498c2ecf20Sopenharmony_ci
4508c2ecf20Sopenharmony_ci	if (flags & IORESOURCE_PREFETCH)
4518c2ecf20Sopenharmony_ci		val |= PCI_BASE_ADDRESS_MEM_PREFETCH;
4528c2ecf20Sopenharmony_ci
4538c2ecf20Sopenharmony_ci	if (flags & IORESOURCE_MEM_64)
4548c2ecf20Sopenharmony_ci		val |= PCI_BASE_ADDRESS_MEM_TYPE_64;
4558c2ecf20Sopenharmony_ci
4568c2ecf20Sopenharmony_ci	return cpu_to_le32(val);
4578c2ecf20Sopenharmony_ci}
4588c2ecf20Sopenharmony_ci
4598c2ecf20Sopenharmony_ci/*
4608c2ecf20Sopenharmony_ci * Pretend we're hardware and tweak the values of the *virtual* PCI BARs
4618c2ecf20Sopenharmony_ci * to reflect the hardware capabilities.  This implements BAR sizing.
4628c2ecf20Sopenharmony_ci */
4638c2ecf20Sopenharmony_cistatic void vfio_bar_fixup(struct vfio_pci_device *vdev)
4648c2ecf20Sopenharmony_ci{
4658c2ecf20Sopenharmony_ci	struct pci_dev *pdev = vdev->pdev;
4668c2ecf20Sopenharmony_ci	int i;
4678c2ecf20Sopenharmony_ci	__le32 *vbar;
4688c2ecf20Sopenharmony_ci	u64 mask;
4698c2ecf20Sopenharmony_ci
4708c2ecf20Sopenharmony_ci	if (!vdev->bardirty)
4718c2ecf20Sopenharmony_ci		return;
4728c2ecf20Sopenharmony_ci
4738c2ecf20Sopenharmony_ci	vbar = (__le32 *)&vdev->vconfig[PCI_BASE_ADDRESS_0];
4748c2ecf20Sopenharmony_ci
4758c2ecf20Sopenharmony_ci	for (i = 0; i < PCI_STD_NUM_BARS; i++, vbar++) {
4768c2ecf20Sopenharmony_ci		int bar = i + PCI_STD_RESOURCES;
4778c2ecf20Sopenharmony_ci
4788c2ecf20Sopenharmony_ci		if (!pci_resource_start(pdev, bar)) {
4798c2ecf20Sopenharmony_ci			*vbar = 0; /* Unmapped by host = unimplemented to user */
4808c2ecf20Sopenharmony_ci			continue;
4818c2ecf20Sopenharmony_ci		}
4828c2ecf20Sopenharmony_ci
4838c2ecf20Sopenharmony_ci		mask = ~(pci_resource_len(pdev, bar) - 1);
4848c2ecf20Sopenharmony_ci
4858c2ecf20Sopenharmony_ci		*vbar &= cpu_to_le32((u32)mask);
4868c2ecf20Sopenharmony_ci		*vbar |= vfio_generate_bar_flags(pdev, bar);
4878c2ecf20Sopenharmony_ci
4888c2ecf20Sopenharmony_ci		if (*vbar & cpu_to_le32(PCI_BASE_ADDRESS_MEM_TYPE_64)) {
4898c2ecf20Sopenharmony_ci			vbar++;
4908c2ecf20Sopenharmony_ci			*vbar &= cpu_to_le32((u32)(mask >> 32));
4918c2ecf20Sopenharmony_ci			i++;
4928c2ecf20Sopenharmony_ci		}
4938c2ecf20Sopenharmony_ci	}
4948c2ecf20Sopenharmony_ci
4958c2ecf20Sopenharmony_ci	vbar = (__le32 *)&vdev->vconfig[PCI_ROM_ADDRESS];
4968c2ecf20Sopenharmony_ci
4978c2ecf20Sopenharmony_ci	/*
4988c2ecf20Sopenharmony_ci	 * NB. REGION_INFO will have reported zero size if we weren't able
4998c2ecf20Sopenharmony_ci	 * to read the ROM, but we still return the actual BAR size here if
5008c2ecf20Sopenharmony_ci	 * it exists (or the shadow ROM space).
5018c2ecf20Sopenharmony_ci	 */
5028c2ecf20Sopenharmony_ci	if (pci_resource_start(pdev, PCI_ROM_RESOURCE)) {
5038c2ecf20Sopenharmony_ci		mask = ~(pci_resource_len(pdev, PCI_ROM_RESOURCE) - 1);
5048c2ecf20Sopenharmony_ci		mask |= PCI_ROM_ADDRESS_ENABLE;
5058c2ecf20Sopenharmony_ci		*vbar &= cpu_to_le32((u32)mask);
5068c2ecf20Sopenharmony_ci	} else if (pdev->resource[PCI_ROM_RESOURCE].flags &
5078c2ecf20Sopenharmony_ci					IORESOURCE_ROM_SHADOW) {
5088c2ecf20Sopenharmony_ci		mask = ~(0x20000 - 1);
5098c2ecf20Sopenharmony_ci		mask |= PCI_ROM_ADDRESS_ENABLE;
5108c2ecf20Sopenharmony_ci		*vbar &= cpu_to_le32((u32)mask);
5118c2ecf20Sopenharmony_ci	} else
5128c2ecf20Sopenharmony_ci		*vbar = 0;
5138c2ecf20Sopenharmony_ci
5148c2ecf20Sopenharmony_ci	vdev->bardirty = false;
5158c2ecf20Sopenharmony_ci}
5168c2ecf20Sopenharmony_ci
5178c2ecf20Sopenharmony_cistatic int vfio_basic_config_read(struct vfio_pci_device *vdev, int pos,
5188c2ecf20Sopenharmony_ci				  int count, struct perm_bits *perm,
5198c2ecf20Sopenharmony_ci				  int offset, __le32 *val)
5208c2ecf20Sopenharmony_ci{
5218c2ecf20Sopenharmony_ci	if (is_bar(offset)) /* pos == offset for basic config */
5228c2ecf20Sopenharmony_ci		vfio_bar_fixup(vdev);
5238c2ecf20Sopenharmony_ci
5248c2ecf20Sopenharmony_ci	count = vfio_default_config_read(vdev, pos, count, perm, offset, val);
5258c2ecf20Sopenharmony_ci
5268c2ecf20Sopenharmony_ci	/* Mask in virtual memory enable */
5278c2ecf20Sopenharmony_ci	if (offset == PCI_COMMAND && vdev->pdev->no_command_memory) {
5288c2ecf20Sopenharmony_ci		u16 cmd = le16_to_cpu(*(__le16 *)&vdev->vconfig[PCI_COMMAND]);
5298c2ecf20Sopenharmony_ci		u32 tmp_val = le32_to_cpu(*val);
5308c2ecf20Sopenharmony_ci
5318c2ecf20Sopenharmony_ci		tmp_val |= cmd & PCI_COMMAND_MEMORY;
5328c2ecf20Sopenharmony_ci		*val = cpu_to_le32(tmp_val);
5338c2ecf20Sopenharmony_ci	}
5348c2ecf20Sopenharmony_ci
5358c2ecf20Sopenharmony_ci	return count;
5368c2ecf20Sopenharmony_ci}
5378c2ecf20Sopenharmony_ci
5388c2ecf20Sopenharmony_ci/* Test whether BARs match the value we think they should contain */
5398c2ecf20Sopenharmony_cistatic bool vfio_need_bar_restore(struct vfio_pci_device *vdev)
5408c2ecf20Sopenharmony_ci{
5418c2ecf20Sopenharmony_ci	int i = 0, pos = PCI_BASE_ADDRESS_0, ret;
5428c2ecf20Sopenharmony_ci	u32 bar;
5438c2ecf20Sopenharmony_ci
5448c2ecf20Sopenharmony_ci	for (; pos <= PCI_BASE_ADDRESS_5; i++, pos += 4) {
5458c2ecf20Sopenharmony_ci		if (vdev->rbar[i]) {
5468c2ecf20Sopenharmony_ci			ret = pci_user_read_config_dword(vdev->pdev, pos, &bar);
5478c2ecf20Sopenharmony_ci			if (ret || vdev->rbar[i] != bar)
5488c2ecf20Sopenharmony_ci				return true;
5498c2ecf20Sopenharmony_ci		}
5508c2ecf20Sopenharmony_ci	}
5518c2ecf20Sopenharmony_ci
5528c2ecf20Sopenharmony_ci	return false;
5538c2ecf20Sopenharmony_ci}
5548c2ecf20Sopenharmony_ci
5558c2ecf20Sopenharmony_cistatic int vfio_basic_config_write(struct vfio_pci_device *vdev, int pos,
5568c2ecf20Sopenharmony_ci				   int count, struct perm_bits *perm,
5578c2ecf20Sopenharmony_ci				   int offset, __le32 val)
5588c2ecf20Sopenharmony_ci{
5598c2ecf20Sopenharmony_ci	struct pci_dev *pdev = vdev->pdev;
5608c2ecf20Sopenharmony_ci	__le16 *virt_cmd;
5618c2ecf20Sopenharmony_ci	u16 new_cmd = 0;
5628c2ecf20Sopenharmony_ci	int ret;
5638c2ecf20Sopenharmony_ci
5648c2ecf20Sopenharmony_ci	virt_cmd = (__le16 *)&vdev->vconfig[PCI_COMMAND];
5658c2ecf20Sopenharmony_ci
5668c2ecf20Sopenharmony_ci	if (offset == PCI_COMMAND) {
5678c2ecf20Sopenharmony_ci		bool phys_mem, virt_mem, new_mem, phys_io, virt_io, new_io;
5688c2ecf20Sopenharmony_ci		u16 phys_cmd;
5698c2ecf20Sopenharmony_ci
5708c2ecf20Sopenharmony_ci		ret = pci_user_read_config_word(pdev, PCI_COMMAND, &phys_cmd);
5718c2ecf20Sopenharmony_ci		if (ret)
5728c2ecf20Sopenharmony_ci			return ret;
5738c2ecf20Sopenharmony_ci
5748c2ecf20Sopenharmony_ci		new_cmd = le32_to_cpu(val);
5758c2ecf20Sopenharmony_ci
5768c2ecf20Sopenharmony_ci		phys_io = !!(phys_cmd & PCI_COMMAND_IO);
5778c2ecf20Sopenharmony_ci		virt_io = !!(le16_to_cpu(*virt_cmd) & PCI_COMMAND_IO);
5788c2ecf20Sopenharmony_ci		new_io = !!(new_cmd & PCI_COMMAND_IO);
5798c2ecf20Sopenharmony_ci
5808c2ecf20Sopenharmony_ci		phys_mem = !!(phys_cmd & PCI_COMMAND_MEMORY);
5818c2ecf20Sopenharmony_ci		virt_mem = !!(le16_to_cpu(*virt_cmd) & PCI_COMMAND_MEMORY);
5828c2ecf20Sopenharmony_ci		new_mem = !!(new_cmd & PCI_COMMAND_MEMORY);
5838c2ecf20Sopenharmony_ci
5848c2ecf20Sopenharmony_ci		if (!new_mem)
5858c2ecf20Sopenharmony_ci			vfio_pci_zap_and_down_write_memory_lock(vdev);
5868c2ecf20Sopenharmony_ci		else
5878c2ecf20Sopenharmony_ci			down_write(&vdev->memory_lock);
5888c2ecf20Sopenharmony_ci
5898c2ecf20Sopenharmony_ci		/*
5908c2ecf20Sopenharmony_ci		 * If the user is writing mem/io enable (new_mem/io) and we
5918c2ecf20Sopenharmony_ci		 * think it's already enabled (virt_mem/io), but the hardware
5928c2ecf20Sopenharmony_ci		 * shows it disabled (phys_mem/io, then the device has
5938c2ecf20Sopenharmony_ci		 * undergone some kind of backdoor reset and needs to be
5948c2ecf20Sopenharmony_ci		 * restored before we allow it to enable the bars.
5958c2ecf20Sopenharmony_ci		 * SR-IOV devices will trigger this - for mem enable let's
5968c2ecf20Sopenharmony_ci		 * catch this now and for io enable it will be caught later
5978c2ecf20Sopenharmony_ci		 */
5988c2ecf20Sopenharmony_ci		if ((new_mem && virt_mem && !phys_mem &&
5998c2ecf20Sopenharmony_ci		     !pdev->no_command_memory) ||
6008c2ecf20Sopenharmony_ci		    (new_io && virt_io && !phys_io) ||
6018c2ecf20Sopenharmony_ci		    vfio_need_bar_restore(vdev))
6028c2ecf20Sopenharmony_ci			vfio_bar_restore(vdev);
6038c2ecf20Sopenharmony_ci	}
6048c2ecf20Sopenharmony_ci
6058c2ecf20Sopenharmony_ci	count = vfio_default_config_write(vdev, pos, count, perm, offset, val);
6068c2ecf20Sopenharmony_ci	if (count < 0) {
6078c2ecf20Sopenharmony_ci		if (offset == PCI_COMMAND)
6088c2ecf20Sopenharmony_ci			up_write(&vdev->memory_lock);
6098c2ecf20Sopenharmony_ci		return count;
6108c2ecf20Sopenharmony_ci	}
6118c2ecf20Sopenharmony_ci
6128c2ecf20Sopenharmony_ci	/*
6138c2ecf20Sopenharmony_ci	 * Save current memory/io enable bits in vconfig to allow for
6148c2ecf20Sopenharmony_ci	 * the test above next time.
6158c2ecf20Sopenharmony_ci	 */
6168c2ecf20Sopenharmony_ci	if (offset == PCI_COMMAND) {
6178c2ecf20Sopenharmony_ci		u16 mask = PCI_COMMAND_MEMORY | PCI_COMMAND_IO;
6188c2ecf20Sopenharmony_ci
6198c2ecf20Sopenharmony_ci		*virt_cmd &= cpu_to_le16(~mask);
6208c2ecf20Sopenharmony_ci		*virt_cmd |= cpu_to_le16(new_cmd & mask);
6218c2ecf20Sopenharmony_ci
6228c2ecf20Sopenharmony_ci		up_write(&vdev->memory_lock);
6238c2ecf20Sopenharmony_ci	}
6248c2ecf20Sopenharmony_ci
6258c2ecf20Sopenharmony_ci	/* Emulate INTx disable */
6268c2ecf20Sopenharmony_ci	if (offset >= PCI_COMMAND && offset <= PCI_COMMAND + 1) {
6278c2ecf20Sopenharmony_ci		bool virt_intx_disable;
6288c2ecf20Sopenharmony_ci
6298c2ecf20Sopenharmony_ci		virt_intx_disable = !!(le16_to_cpu(*virt_cmd) &
6308c2ecf20Sopenharmony_ci				       PCI_COMMAND_INTX_DISABLE);
6318c2ecf20Sopenharmony_ci
6328c2ecf20Sopenharmony_ci		if (virt_intx_disable && !vdev->virq_disabled) {
6338c2ecf20Sopenharmony_ci			vdev->virq_disabled = true;
6348c2ecf20Sopenharmony_ci			vfio_pci_intx_mask(vdev);
6358c2ecf20Sopenharmony_ci		} else if (!virt_intx_disable && vdev->virq_disabled) {
6368c2ecf20Sopenharmony_ci			vdev->virq_disabled = false;
6378c2ecf20Sopenharmony_ci			vfio_pci_intx_unmask(vdev);
6388c2ecf20Sopenharmony_ci		}
6398c2ecf20Sopenharmony_ci	}
6408c2ecf20Sopenharmony_ci
6418c2ecf20Sopenharmony_ci	if (is_bar(offset))
6428c2ecf20Sopenharmony_ci		vdev->bardirty = true;
6438c2ecf20Sopenharmony_ci
6448c2ecf20Sopenharmony_ci	return count;
6458c2ecf20Sopenharmony_ci}
6468c2ecf20Sopenharmony_ci
6478c2ecf20Sopenharmony_ci/* Permissions for the Basic PCI Header */
6488c2ecf20Sopenharmony_cistatic int __init init_pci_cap_basic_perm(struct perm_bits *perm)
6498c2ecf20Sopenharmony_ci{
6508c2ecf20Sopenharmony_ci	if (alloc_perm_bits(perm, PCI_STD_HEADER_SIZEOF))
6518c2ecf20Sopenharmony_ci		return -ENOMEM;
6528c2ecf20Sopenharmony_ci
6538c2ecf20Sopenharmony_ci	perm->readfn = vfio_basic_config_read;
6548c2ecf20Sopenharmony_ci	perm->writefn = vfio_basic_config_write;
6558c2ecf20Sopenharmony_ci
6568c2ecf20Sopenharmony_ci	/* Virtualized for SR-IOV functions, which just have FFFF */
6578c2ecf20Sopenharmony_ci	p_setw(perm, PCI_VENDOR_ID, (u16)ALL_VIRT, NO_WRITE);
6588c2ecf20Sopenharmony_ci	p_setw(perm, PCI_DEVICE_ID, (u16)ALL_VIRT, NO_WRITE);
6598c2ecf20Sopenharmony_ci
6608c2ecf20Sopenharmony_ci	/*
6618c2ecf20Sopenharmony_ci	 * Virtualize INTx disable, we use it internally for interrupt
6628c2ecf20Sopenharmony_ci	 * control and can emulate it for non-PCI 2.3 devices.
6638c2ecf20Sopenharmony_ci	 */
6648c2ecf20Sopenharmony_ci	p_setw(perm, PCI_COMMAND, PCI_COMMAND_INTX_DISABLE, (u16)ALL_WRITE);
6658c2ecf20Sopenharmony_ci
6668c2ecf20Sopenharmony_ci	/* Virtualize capability list, we might want to skip/disable */
6678c2ecf20Sopenharmony_ci	p_setw(perm, PCI_STATUS, PCI_STATUS_CAP_LIST, NO_WRITE);
6688c2ecf20Sopenharmony_ci
6698c2ecf20Sopenharmony_ci	/* No harm to write */
6708c2ecf20Sopenharmony_ci	p_setb(perm, PCI_CACHE_LINE_SIZE, NO_VIRT, (u8)ALL_WRITE);
6718c2ecf20Sopenharmony_ci	p_setb(perm, PCI_LATENCY_TIMER, NO_VIRT, (u8)ALL_WRITE);
6728c2ecf20Sopenharmony_ci	p_setb(perm, PCI_BIST, NO_VIRT, (u8)ALL_WRITE);
6738c2ecf20Sopenharmony_ci
6748c2ecf20Sopenharmony_ci	/* Virtualize all bars, can't touch the real ones */
6758c2ecf20Sopenharmony_ci	p_setd(perm, PCI_BASE_ADDRESS_0, ALL_VIRT, ALL_WRITE);
6768c2ecf20Sopenharmony_ci	p_setd(perm, PCI_BASE_ADDRESS_1, ALL_VIRT, ALL_WRITE);
6778c2ecf20Sopenharmony_ci	p_setd(perm, PCI_BASE_ADDRESS_2, ALL_VIRT, ALL_WRITE);
6788c2ecf20Sopenharmony_ci	p_setd(perm, PCI_BASE_ADDRESS_3, ALL_VIRT, ALL_WRITE);
6798c2ecf20Sopenharmony_ci	p_setd(perm, PCI_BASE_ADDRESS_4, ALL_VIRT, ALL_WRITE);
6808c2ecf20Sopenharmony_ci	p_setd(perm, PCI_BASE_ADDRESS_5, ALL_VIRT, ALL_WRITE);
6818c2ecf20Sopenharmony_ci	p_setd(perm, PCI_ROM_ADDRESS, ALL_VIRT, ALL_WRITE);
6828c2ecf20Sopenharmony_ci
6838c2ecf20Sopenharmony_ci	/* Allow us to adjust capability chain */
6848c2ecf20Sopenharmony_ci	p_setb(perm, PCI_CAPABILITY_LIST, (u8)ALL_VIRT, NO_WRITE);
6858c2ecf20Sopenharmony_ci
6868c2ecf20Sopenharmony_ci	/* Sometimes used by sw, just virtualize */
6878c2ecf20Sopenharmony_ci	p_setb(perm, PCI_INTERRUPT_LINE, (u8)ALL_VIRT, (u8)ALL_WRITE);
6888c2ecf20Sopenharmony_ci
6898c2ecf20Sopenharmony_ci	/* Virtualize interrupt pin to allow hiding INTx */
6908c2ecf20Sopenharmony_ci	p_setb(perm, PCI_INTERRUPT_PIN, (u8)ALL_VIRT, (u8)NO_WRITE);
6918c2ecf20Sopenharmony_ci
6928c2ecf20Sopenharmony_ci	return 0;
6938c2ecf20Sopenharmony_ci}
6948c2ecf20Sopenharmony_ci
6958c2ecf20Sopenharmony_cistatic int vfio_pm_config_write(struct vfio_pci_device *vdev, int pos,
6968c2ecf20Sopenharmony_ci				int count, struct perm_bits *perm,
6978c2ecf20Sopenharmony_ci				int offset, __le32 val)
6988c2ecf20Sopenharmony_ci{
6998c2ecf20Sopenharmony_ci	count = vfio_default_config_write(vdev, pos, count, perm, offset, val);
7008c2ecf20Sopenharmony_ci	if (count < 0)
7018c2ecf20Sopenharmony_ci		return count;
7028c2ecf20Sopenharmony_ci
7038c2ecf20Sopenharmony_ci	if (offset == PCI_PM_CTRL) {
7048c2ecf20Sopenharmony_ci		pci_power_t state;
7058c2ecf20Sopenharmony_ci
7068c2ecf20Sopenharmony_ci		switch (le32_to_cpu(val) & PCI_PM_CTRL_STATE_MASK) {
7078c2ecf20Sopenharmony_ci		case 0:
7088c2ecf20Sopenharmony_ci			state = PCI_D0;
7098c2ecf20Sopenharmony_ci			break;
7108c2ecf20Sopenharmony_ci		case 1:
7118c2ecf20Sopenharmony_ci			state = PCI_D1;
7128c2ecf20Sopenharmony_ci			break;
7138c2ecf20Sopenharmony_ci		case 2:
7148c2ecf20Sopenharmony_ci			state = PCI_D2;
7158c2ecf20Sopenharmony_ci			break;
7168c2ecf20Sopenharmony_ci		case 3:
7178c2ecf20Sopenharmony_ci			state = PCI_D3hot;
7188c2ecf20Sopenharmony_ci			break;
7198c2ecf20Sopenharmony_ci		}
7208c2ecf20Sopenharmony_ci
7218c2ecf20Sopenharmony_ci		vfio_pci_set_power_state(vdev, state);
7228c2ecf20Sopenharmony_ci	}
7238c2ecf20Sopenharmony_ci
7248c2ecf20Sopenharmony_ci	return count;
7258c2ecf20Sopenharmony_ci}
7268c2ecf20Sopenharmony_ci
7278c2ecf20Sopenharmony_ci/* Permissions for the Power Management capability */
7288c2ecf20Sopenharmony_cistatic int __init init_pci_cap_pm_perm(struct perm_bits *perm)
7298c2ecf20Sopenharmony_ci{
7308c2ecf20Sopenharmony_ci	if (alloc_perm_bits(perm, pci_cap_length[PCI_CAP_ID_PM]))
7318c2ecf20Sopenharmony_ci		return -ENOMEM;
7328c2ecf20Sopenharmony_ci
7338c2ecf20Sopenharmony_ci	perm->writefn = vfio_pm_config_write;
7348c2ecf20Sopenharmony_ci
7358c2ecf20Sopenharmony_ci	/*
7368c2ecf20Sopenharmony_ci	 * We always virtualize the next field so we can remove
7378c2ecf20Sopenharmony_ci	 * capabilities from the chain if we want to.
7388c2ecf20Sopenharmony_ci	 */
7398c2ecf20Sopenharmony_ci	p_setb(perm, PCI_CAP_LIST_NEXT, (u8)ALL_VIRT, NO_WRITE);
7408c2ecf20Sopenharmony_ci
7418c2ecf20Sopenharmony_ci	/*
7428c2ecf20Sopenharmony_ci	 * Power management is defined *per function*, so we can let
7438c2ecf20Sopenharmony_ci	 * the user change power state, but we trap and initiate the
7448c2ecf20Sopenharmony_ci	 * change ourselves, so the state bits are read-only.
7458c2ecf20Sopenharmony_ci	 */
7468c2ecf20Sopenharmony_ci	p_setd(perm, PCI_PM_CTRL, NO_VIRT, ~PCI_PM_CTRL_STATE_MASK);
7478c2ecf20Sopenharmony_ci	return 0;
7488c2ecf20Sopenharmony_ci}
7498c2ecf20Sopenharmony_ci
7508c2ecf20Sopenharmony_cistatic int vfio_vpd_config_write(struct vfio_pci_device *vdev, int pos,
7518c2ecf20Sopenharmony_ci				 int count, struct perm_bits *perm,
7528c2ecf20Sopenharmony_ci				 int offset, __le32 val)
7538c2ecf20Sopenharmony_ci{
7548c2ecf20Sopenharmony_ci	struct pci_dev *pdev = vdev->pdev;
7558c2ecf20Sopenharmony_ci	__le16 *paddr = (__le16 *)(vdev->vconfig + pos - offset + PCI_VPD_ADDR);
7568c2ecf20Sopenharmony_ci	__le32 *pdata = (__le32 *)(vdev->vconfig + pos - offset + PCI_VPD_DATA);
7578c2ecf20Sopenharmony_ci	u16 addr;
7588c2ecf20Sopenharmony_ci	u32 data;
7598c2ecf20Sopenharmony_ci
7608c2ecf20Sopenharmony_ci	/*
7618c2ecf20Sopenharmony_ci	 * Write through to emulation.  If the write includes the upper byte
7628c2ecf20Sopenharmony_ci	 * of PCI_VPD_ADDR, then the PCI_VPD_ADDR_F bit is written and we
7638c2ecf20Sopenharmony_ci	 * have work to do.
7648c2ecf20Sopenharmony_ci	 */
7658c2ecf20Sopenharmony_ci	count = vfio_default_config_write(vdev, pos, count, perm, offset, val);
7668c2ecf20Sopenharmony_ci	if (count < 0 || offset > PCI_VPD_ADDR + 1 ||
7678c2ecf20Sopenharmony_ci	    offset + count <= PCI_VPD_ADDR + 1)
7688c2ecf20Sopenharmony_ci		return count;
7698c2ecf20Sopenharmony_ci
7708c2ecf20Sopenharmony_ci	addr = le16_to_cpu(*paddr);
7718c2ecf20Sopenharmony_ci
7728c2ecf20Sopenharmony_ci	if (addr & PCI_VPD_ADDR_F) {
7738c2ecf20Sopenharmony_ci		data = le32_to_cpu(*pdata);
7748c2ecf20Sopenharmony_ci		if (pci_write_vpd(pdev, addr & ~PCI_VPD_ADDR_F, 4, &data) != 4)
7758c2ecf20Sopenharmony_ci			return count;
7768c2ecf20Sopenharmony_ci	} else {
7778c2ecf20Sopenharmony_ci		data = 0;
7788c2ecf20Sopenharmony_ci		if (pci_read_vpd(pdev, addr, 4, &data) < 0)
7798c2ecf20Sopenharmony_ci			return count;
7808c2ecf20Sopenharmony_ci		*pdata = cpu_to_le32(data);
7818c2ecf20Sopenharmony_ci	}
7828c2ecf20Sopenharmony_ci
7838c2ecf20Sopenharmony_ci	/*
7848c2ecf20Sopenharmony_ci	 * Toggle PCI_VPD_ADDR_F in the emulated PCI_VPD_ADDR register to
7858c2ecf20Sopenharmony_ci	 * signal completion.  If an error occurs above, we assume that not
7868c2ecf20Sopenharmony_ci	 * toggling this bit will induce a driver timeout.
7878c2ecf20Sopenharmony_ci	 */
7888c2ecf20Sopenharmony_ci	addr ^= PCI_VPD_ADDR_F;
7898c2ecf20Sopenharmony_ci	*paddr = cpu_to_le16(addr);
7908c2ecf20Sopenharmony_ci
7918c2ecf20Sopenharmony_ci	return count;
7928c2ecf20Sopenharmony_ci}
7938c2ecf20Sopenharmony_ci
7948c2ecf20Sopenharmony_ci/* Permissions for Vital Product Data capability */
7958c2ecf20Sopenharmony_cistatic int __init init_pci_cap_vpd_perm(struct perm_bits *perm)
7968c2ecf20Sopenharmony_ci{
7978c2ecf20Sopenharmony_ci	if (alloc_perm_bits(perm, pci_cap_length[PCI_CAP_ID_VPD]))
7988c2ecf20Sopenharmony_ci		return -ENOMEM;
7998c2ecf20Sopenharmony_ci
8008c2ecf20Sopenharmony_ci	perm->writefn = vfio_vpd_config_write;
8018c2ecf20Sopenharmony_ci
8028c2ecf20Sopenharmony_ci	/*
8038c2ecf20Sopenharmony_ci	 * We always virtualize the next field so we can remove
8048c2ecf20Sopenharmony_ci	 * capabilities from the chain if we want to.
8058c2ecf20Sopenharmony_ci	 */
8068c2ecf20Sopenharmony_ci	p_setb(perm, PCI_CAP_LIST_NEXT, (u8)ALL_VIRT, NO_WRITE);
8078c2ecf20Sopenharmony_ci
8088c2ecf20Sopenharmony_ci	/*
8098c2ecf20Sopenharmony_ci	 * Both the address and data registers are virtualized to
8108c2ecf20Sopenharmony_ci	 * enable access through the pci_vpd_read/write functions
8118c2ecf20Sopenharmony_ci	 */
8128c2ecf20Sopenharmony_ci	p_setw(perm, PCI_VPD_ADDR, (u16)ALL_VIRT, (u16)ALL_WRITE);
8138c2ecf20Sopenharmony_ci	p_setd(perm, PCI_VPD_DATA, ALL_VIRT, ALL_WRITE);
8148c2ecf20Sopenharmony_ci
8158c2ecf20Sopenharmony_ci	return 0;
8168c2ecf20Sopenharmony_ci}
8178c2ecf20Sopenharmony_ci
8188c2ecf20Sopenharmony_ci/* Permissions for PCI-X capability */
8198c2ecf20Sopenharmony_cistatic int __init init_pci_cap_pcix_perm(struct perm_bits *perm)
8208c2ecf20Sopenharmony_ci{
8218c2ecf20Sopenharmony_ci	/* Alloc 24, but only 8 are used in v0 */
8228c2ecf20Sopenharmony_ci	if (alloc_perm_bits(perm, PCI_CAP_PCIX_SIZEOF_V2))
8238c2ecf20Sopenharmony_ci		return -ENOMEM;
8248c2ecf20Sopenharmony_ci
8258c2ecf20Sopenharmony_ci	p_setb(perm, PCI_CAP_LIST_NEXT, (u8)ALL_VIRT, NO_WRITE);
8268c2ecf20Sopenharmony_ci
8278c2ecf20Sopenharmony_ci	p_setw(perm, PCI_X_CMD, NO_VIRT, (u16)ALL_WRITE);
8288c2ecf20Sopenharmony_ci	p_setd(perm, PCI_X_ECC_CSR, NO_VIRT, ALL_WRITE);
8298c2ecf20Sopenharmony_ci	return 0;
8308c2ecf20Sopenharmony_ci}
8318c2ecf20Sopenharmony_ci
8328c2ecf20Sopenharmony_cistatic int vfio_exp_config_write(struct vfio_pci_device *vdev, int pos,
8338c2ecf20Sopenharmony_ci				 int count, struct perm_bits *perm,
8348c2ecf20Sopenharmony_ci				 int offset, __le32 val)
8358c2ecf20Sopenharmony_ci{
8368c2ecf20Sopenharmony_ci	__le16 *ctrl = (__le16 *)(vdev->vconfig + pos -
8378c2ecf20Sopenharmony_ci				  offset + PCI_EXP_DEVCTL);
8388c2ecf20Sopenharmony_ci	int readrq = le16_to_cpu(*ctrl) & PCI_EXP_DEVCTL_READRQ;
8398c2ecf20Sopenharmony_ci
8408c2ecf20Sopenharmony_ci	count = vfio_default_config_write(vdev, pos, count, perm, offset, val);
8418c2ecf20Sopenharmony_ci	if (count < 0)
8428c2ecf20Sopenharmony_ci		return count;
8438c2ecf20Sopenharmony_ci
8448c2ecf20Sopenharmony_ci	/*
8458c2ecf20Sopenharmony_ci	 * The FLR bit is virtualized, if set and the device supports PCIe
8468c2ecf20Sopenharmony_ci	 * FLR, issue a reset_function.  Regardless, clear the bit, the spec
8478c2ecf20Sopenharmony_ci	 * requires it to be always read as zero.  NB, reset_function might
8488c2ecf20Sopenharmony_ci	 * not use a PCIe FLR, we don't have that level of granularity.
8498c2ecf20Sopenharmony_ci	 */
8508c2ecf20Sopenharmony_ci	if (*ctrl & cpu_to_le16(PCI_EXP_DEVCTL_BCR_FLR)) {
8518c2ecf20Sopenharmony_ci		u32 cap;
8528c2ecf20Sopenharmony_ci		int ret;
8538c2ecf20Sopenharmony_ci
8548c2ecf20Sopenharmony_ci		*ctrl &= ~cpu_to_le16(PCI_EXP_DEVCTL_BCR_FLR);
8558c2ecf20Sopenharmony_ci
8568c2ecf20Sopenharmony_ci		ret = pci_user_read_config_dword(vdev->pdev,
8578c2ecf20Sopenharmony_ci						 pos - offset + PCI_EXP_DEVCAP,
8588c2ecf20Sopenharmony_ci						 &cap);
8598c2ecf20Sopenharmony_ci
8608c2ecf20Sopenharmony_ci		if (!ret && (cap & PCI_EXP_DEVCAP_FLR)) {
8618c2ecf20Sopenharmony_ci			vfio_pci_zap_and_down_write_memory_lock(vdev);
8628c2ecf20Sopenharmony_ci			pci_try_reset_function(vdev->pdev);
8638c2ecf20Sopenharmony_ci			up_write(&vdev->memory_lock);
8648c2ecf20Sopenharmony_ci		}
8658c2ecf20Sopenharmony_ci	}
8668c2ecf20Sopenharmony_ci
8678c2ecf20Sopenharmony_ci	/*
8688c2ecf20Sopenharmony_ci	 * MPS is virtualized to the user, writes do not change the physical
8698c2ecf20Sopenharmony_ci	 * register since determining a proper MPS value requires a system wide
8708c2ecf20Sopenharmony_ci	 * device view.  The MRRS is largely independent of MPS, but since the
8718c2ecf20Sopenharmony_ci	 * user does not have that system-wide view, they might set a safe, but
8728c2ecf20Sopenharmony_ci	 * inefficiently low value.  Here we allow writes through to hardware,
8738c2ecf20Sopenharmony_ci	 * but we set the floor to the physical device MPS setting, so that
8748c2ecf20Sopenharmony_ci	 * we can at least use full TLPs, as defined by the MPS value.
8758c2ecf20Sopenharmony_ci	 *
8768c2ecf20Sopenharmony_ci	 * NB, if any devices actually depend on an artificially low MRRS
8778c2ecf20Sopenharmony_ci	 * setting, this will need to be revisited, perhaps with a quirk
8788c2ecf20Sopenharmony_ci	 * though pcie_set_readrq().
8798c2ecf20Sopenharmony_ci	 */
8808c2ecf20Sopenharmony_ci	if (readrq != (le16_to_cpu(*ctrl) & PCI_EXP_DEVCTL_READRQ)) {
8818c2ecf20Sopenharmony_ci		readrq = 128 <<
8828c2ecf20Sopenharmony_ci			((le16_to_cpu(*ctrl) & PCI_EXP_DEVCTL_READRQ) >> 12);
8838c2ecf20Sopenharmony_ci		readrq = max(readrq, pcie_get_mps(vdev->pdev));
8848c2ecf20Sopenharmony_ci
8858c2ecf20Sopenharmony_ci		pcie_set_readrq(vdev->pdev, readrq);
8868c2ecf20Sopenharmony_ci	}
8878c2ecf20Sopenharmony_ci
8888c2ecf20Sopenharmony_ci	return count;
8898c2ecf20Sopenharmony_ci}
8908c2ecf20Sopenharmony_ci
8918c2ecf20Sopenharmony_ci/* Permissions for PCI Express capability */
8928c2ecf20Sopenharmony_cistatic int __init init_pci_cap_exp_perm(struct perm_bits *perm)
8938c2ecf20Sopenharmony_ci{
8948c2ecf20Sopenharmony_ci	/* Alloc largest of possible sizes */
8958c2ecf20Sopenharmony_ci	if (alloc_perm_bits(perm, PCI_CAP_EXP_ENDPOINT_SIZEOF_V2))
8968c2ecf20Sopenharmony_ci		return -ENOMEM;
8978c2ecf20Sopenharmony_ci
8988c2ecf20Sopenharmony_ci	perm->writefn = vfio_exp_config_write;
8998c2ecf20Sopenharmony_ci
9008c2ecf20Sopenharmony_ci	p_setb(perm, PCI_CAP_LIST_NEXT, (u8)ALL_VIRT, NO_WRITE);
9018c2ecf20Sopenharmony_ci
9028c2ecf20Sopenharmony_ci	/*
9038c2ecf20Sopenharmony_ci	 * Allow writes to device control fields, except devctl_phantom,
9048c2ecf20Sopenharmony_ci	 * which could confuse IOMMU, MPS, which can break communication
9058c2ecf20Sopenharmony_ci	 * with other physical devices, and the ARI bit in devctl2, which
9068c2ecf20Sopenharmony_ci	 * is set at probe time.  FLR and MRRS get virtualized via our
9078c2ecf20Sopenharmony_ci	 * writefn.
9088c2ecf20Sopenharmony_ci	 */
9098c2ecf20Sopenharmony_ci	p_setw(perm, PCI_EXP_DEVCTL,
9108c2ecf20Sopenharmony_ci	       PCI_EXP_DEVCTL_BCR_FLR | PCI_EXP_DEVCTL_PAYLOAD |
9118c2ecf20Sopenharmony_ci	       PCI_EXP_DEVCTL_READRQ, ~PCI_EXP_DEVCTL_PHANTOM);
9128c2ecf20Sopenharmony_ci	p_setw(perm, PCI_EXP_DEVCTL2, NO_VIRT, ~PCI_EXP_DEVCTL2_ARI);
9138c2ecf20Sopenharmony_ci	return 0;
9148c2ecf20Sopenharmony_ci}
9158c2ecf20Sopenharmony_ci
9168c2ecf20Sopenharmony_cistatic int vfio_af_config_write(struct vfio_pci_device *vdev, int pos,
9178c2ecf20Sopenharmony_ci				int count, struct perm_bits *perm,
9188c2ecf20Sopenharmony_ci				int offset, __le32 val)
9198c2ecf20Sopenharmony_ci{
9208c2ecf20Sopenharmony_ci	u8 *ctrl = vdev->vconfig + pos - offset + PCI_AF_CTRL;
9218c2ecf20Sopenharmony_ci
9228c2ecf20Sopenharmony_ci	count = vfio_default_config_write(vdev, pos, count, perm, offset, val);
9238c2ecf20Sopenharmony_ci	if (count < 0)
9248c2ecf20Sopenharmony_ci		return count;
9258c2ecf20Sopenharmony_ci
9268c2ecf20Sopenharmony_ci	/*
9278c2ecf20Sopenharmony_ci	 * The FLR bit is virtualized, if set and the device supports AF
9288c2ecf20Sopenharmony_ci	 * FLR, issue a reset_function.  Regardless, clear the bit, the spec
9298c2ecf20Sopenharmony_ci	 * requires it to be always read as zero.  NB, reset_function might
9308c2ecf20Sopenharmony_ci	 * not use an AF FLR, we don't have that level of granularity.
9318c2ecf20Sopenharmony_ci	 */
9328c2ecf20Sopenharmony_ci	if (*ctrl & PCI_AF_CTRL_FLR) {
9338c2ecf20Sopenharmony_ci		u8 cap;
9348c2ecf20Sopenharmony_ci		int ret;
9358c2ecf20Sopenharmony_ci
9368c2ecf20Sopenharmony_ci		*ctrl &= ~PCI_AF_CTRL_FLR;
9378c2ecf20Sopenharmony_ci
9388c2ecf20Sopenharmony_ci		ret = pci_user_read_config_byte(vdev->pdev,
9398c2ecf20Sopenharmony_ci						pos - offset + PCI_AF_CAP,
9408c2ecf20Sopenharmony_ci						&cap);
9418c2ecf20Sopenharmony_ci
9428c2ecf20Sopenharmony_ci		if (!ret && (cap & PCI_AF_CAP_FLR) && (cap & PCI_AF_CAP_TP)) {
9438c2ecf20Sopenharmony_ci			vfio_pci_zap_and_down_write_memory_lock(vdev);
9448c2ecf20Sopenharmony_ci			pci_try_reset_function(vdev->pdev);
9458c2ecf20Sopenharmony_ci			up_write(&vdev->memory_lock);
9468c2ecf20Sopenharmony_ci		}
9478c2ecf20Sopenharmony_ci	}
9488c2ecf20Sopenharmony_ci
9498c2ecf20Sopenharmony_ci	return count;
9508c2ecf20Sopenharmony_ci}
9518c2ecf20Sopenharmony_ci
9528c2ecf20Sopenharmony_ci/* Permissions for Advanced Function capability */
9538c2ecf20Sopenharmony_cistatic int __init init_pci_cap_af_perm(struct perm_bits *perm)
9548c2ecf20Sopenharmony_ci{
9558c2ecf20Sopenharmony_ci	if (alloc_perm_bits(perm, pci_cap_length[PCI_CAP_ID_AF]))
9568c2ecf20Sopenharmony_ci		return -ENOMEM;
9578c2ecf20Sopenharmony_ci
9588c2ecf20Sopenharmony_ci	perm->writefn = vfio_af_config_write;
9598c2ecf20Sopenharmony_ci
9608c2ecf20Sopenharmony_ci	p_setb(perm, PCI_CAP_LIST_NEXT, (u8)ALL_VIRT, NO_WRITE);
9618c2ecf20Sopenharmony_ci	p_setb(perm, PCI_AF_CTRL, PCI_AF_CTRL_FLR, PCI_AF_CTRL_FLR);
9628c2ecf20Sopenharmony_ci	return 0;
9638c2ecf20Sopenharmony_ci}
9648c2ecf20Sopenharmony_ci
9658c2ecf20Sopenharmony_ci/* Permissions for Advanced Error Reporting extended capability */
9668c2ecf20Sopenharmony_cistatic int __init init_pci_ext_cap_err_perm(struct perm_bits *perm)
9678c2ecf20Sopenharmony_ci{
9688c2ecf20Sopenharmony_ci	u32 mask;
9698c2ecf20Sopenharmony_ci
9708c2ecf20Sopenharmony_ci	if (alloc_perm_bits(perm, pci_ext_cap_length[PCI_EXT_CAP_ID_ERR]))
9718c2ecf20Sopenharmony_ci		return -ENOMEM;
9728c2ecf20Sopenharmony_ci
9738c2ecf20Sopenharmony_ci	/*
9748c2ecf20Sopenharmony_ci	 * Virtualize the first dword of all express capabilities
9758c2ecf20Sopenharmony_ci	 * because it includes the next pointer.  This lets us later
9768c2ecf20Sopenharmony_ci	 * remove capabilities from the chain if we need to.
9778c2ecf20Sopenharmony_ci	 */
9788c2ecf20Sopenharmony_ci	p_setd(perm, 0, ALL_VIRT, NO_WRITE);
9798c2ecf20Sopenharmony_ci
9808c2ecf20Sopenharmony_ci	/* Writable bits mask */
9818c2ecf20Sopenharmony_ci	mask =	PCI_ERR_UNC_UND |		/* Undefined */
9828c2ecf20Sopenharmony_ci		PCI_ERR_UNC_DLP |		/* Data Link Protocol */
9838c2ecf20Sopenharmony_ci		PCI_ERR_UNC_SURPDN |		/* Surprise Down */
9848c2ecf20Sopenharmony_ci		PCI_ERR_UNC_POISON_TLP |	/* Poisoned TLP */
9858c2ecf20Sopenharmony_ci		PCI_ERR_UNC_FCP |		/* Flow Control Protocol */
9868c2ecf20Sopenharmony_ci		PCI_ERR_UNC_COMP_TIME |		/* Completion Timeout */
9878c2ecf20Sopenharmony_ci		PCI_ERR_UNC_COMP_ABORT |	/* Completer Abort */
9888c2ecf20Sopenharmony_ci		PCI_ERR_UNC_UNX_COMP |		/* Unexpected Completion */
9898c2ecf20Sopenharmony_ci		PCI_ERR_UNC_RX_OVER |		/* Receiver Overflow */
9908c2ecf20Sopenharmony_ci		PCI_ERR_UNC_MALF_TLP |		/* Malformed TLP */
9918c2ecf20Sopenharmony_ci		PCI_ERR_UNC_ECRC |		/* ECRC Error Status */
9928c2ecf20Sopenharmony_ci		PCI_ERR_UNC_UNSUP |		/* Unsupported Request */
9938c2ecf20Sopenharmony_ci		PCI_ERR_UNC_ACSV |		/* ACS Violation */
9948c2ecf20Sopenharmony_ci		PCI_ERR_UNC_INTN |		/* internal error */
9958c2ecf20Sopenharmony_ci		PCI_ERR_UNC_MCBTLP |		/* MC blocked TLP */
9968c2ecf20Sopenharmony_ci		PCI_ERR_UNC_ATOMEG |		/* Atomic egress blocked */
9978c2ecf20Sopenharmony_ci		PCI_ERR_UNC_TLPPRE;		/* TLP prefix blocked */
9988c2ecf20Sopenharmony_ci	p_setd(perm, PCI_ERR_UNCOR_STATUS, NO_VIRT, mask);
9998c2ecf20Sopenharmony_ci	p_setd(perm, PCI_ERR_UNCOR_MASK, NO_VIRT, mask);
10008c2ecf20Sopenharmony_ci	p_setd(perm, PCI_ERR_UNCOR_SEVER, NO_VIRT, mask);
10018c2ecf20Sopenharmony_ci
10028c2ecf20Sopenharmony_ci	mask =	PCI_ERR_COR_RCVR |		/* Receiver Error Status */
10038c2ecf20Sopenharmony_ci		PCI_ERR_COR_BAD_TLP |		/* Bad TLP Status */
10048c2ecf20Sopenharmony_ci		PCI_ERR_COR_BAD_DLLP |		/* Bad DLLP Status */
10058c2ecf20Sopenharmony_ci		PCI_ERR_COR_REP_ROLL |		/* REPLAY_NUM Rollover */
10068c2ecf20Sopenharmony_ci		PCI_ERR_COR_REP_TIMER |		/* Replay Timer Timeout */
10078c2ecf20Sopenharmony_ci		PCI_ERR_COR_ADV_NFAT |		/* Advisory Non-Fatal */
10088c2ecf20Sopenharmony_ci		PCI_ERR_COR_INTERNAL |		/* Corrected Internal */
10098c2ecf20Sopenharmony_ci		PCI_ERR_COR_LOG_OVER;		/* Header Log Overflow */
10108c2ecf20Sopenharmony_ci	p_setd(perm, PCI_ERR_COR_STATUS, NO_VIRT, mask);
10118c2ecf20Sopenharmony_ci	p_setd(perm, PCI_ERR_COR_MASK, NO_VIRT, mask);
10128c2ecf20Sopenharmony_ci
10138c2ecf20Sopenharmony_ci	mask =	PCI_ERR_CAP_ECRC_GENE |		/* ECRC Generation Enable */
10148c2ecf20Sopenharmony_ci		PCI_ERR_CAP_ECRC_CHKE;		/* ECRC Check Enable */
10158c2ecf20Sopenharmony_ci	p_setd(perm, PCI_ERR_CAP, NO_VIRT, mask);
10168c2ecf20Sopenharmony_ci	return 0;
10178c2ecf20Sopenharmony_ci}
10188c2ecf20Sopenharmony_ci
10198c2ecf20Sopenharmony_ci/* Permissions for Power Budgeting extended capability */
10208c2ecf20Sopenharmony_cistatic int __init init_pci_ext_cap_pwr_perm(struct perm_bits *perm)
10218c2ecf20Sopenharmony_ci{
10228c2ecf20Sopenharmony_ci	if (alloc_perm_bits(perm, pci_ext_cap_length[PCI_EXT_CAP_ID_PWR]))
10238c2ecf20Sopenharmony_ci		return -ENOMEM;
10248c2ecf20Sopenharmony_ci
10258c2ecf20Sopenharmony_ci	p_setd(perm, 0, ALL_VIRT, NO_WRITE);
10268c2ecf20Sopenharmony_ci
10278c2ecf20Sopenharmony_ci	/* Writing the data selector is OK, the info is still read-only */
10288c2ecf20Sopenharmony_ci	p_setb(perm, PCI_PWR_DATA, NO_VIRT, (u8)ALL_WRITE);
10298c2ecf20Sopenharmony_ci	return 0;
10308c2ecf20Sopenharmony_ci}
10318c2ecf20Sopenharmony_ci
10328c2ecf20Sopenharmony_ci/*
10338c2ecf20Sopenharmony_ci * Initialize the shared permission tables
10348c2ecf20Sopenharmony_ci */
10358c2ecf20Sopenharmony_civoid vfio_pci_uninit_perm_bits(void)
10368c2ecf20Sopenharmony_ci{
10378c2ecf20Sopenharmony_ci	free_perm_bits(&cap_perms[PCI_CAP_ID_BASIC]);
10388c2ecf20Sopenharmony_ci
10398c2ecf20Sopenharmony_ci	free_perm_bits(&cap_perms[PCI_CAP_ID_PM]);
10408c2ecf20Sopenharmony_ci	free_perm_bits(&cap_perms[PCI_CAP_ID_VPD]);
10418c2ecf20Sopenharmony_ci	free_perm_bits(&cap_perms[PCI_CAP_ID_PCIX]);
10428c2ecf20Sopenharmony_ci	free_perm_bits(&cap_perms[PCI_CAP_ID_EXP]);
10438c2ecf20Sopenharmony_ci	free_perm_bits(&cap_perms[PCI_CAP_ID_AF]);
10448c2ecf20Sopenharmony_ci
10458c2ecf20Sopenharmony_ci	free_perm_bits(&ecap_perms[PCI_EXT_CAP_ID_ERR]);
10468c2ecf20Sopenharmony_ci	free_perm_bits(&ecap_perms[PCI_EXT_CAP_ID_PWR]);
10478c2ecf20Sopenharmony_ci}
10488c2ecf20Sopenharmony_ci
10498c2ecf20Sopenharmony_ciint __init vfio_pci_init_perm_bits(void)
10508c2ecf20Sopenharmony_ci{
10518c2ecf20Sopenharmony_ci	int ret;
10528c2ecf20Sopenharmony_ci
10538c2ecf20Sopenharmony_ci	/* Basic config space */
10548c2ecf20Sopenharmony_ci	ret = init_pci_cap_basic_perm(&cap_perms[PCI_CAP_ID_BASIC]);
10558c2ecf20Sopenharmony_ci
10568c2ecf20Sopenharmony_ci	/* Capabilities */
10578c2ecf20Sopenharmony_ci	ret |= init_pci_cap_pm_perm(&cap_perms[PCI_CAP_ID_PM]);
10588c2ecf20Sopenharmony_ci	ret |= init_pci_cap_vpd_perm(&cap_perms[PCI_CAP_ID_VPD]);
10598c2ecf20Sopenharmony_ci	ret |= init_pci_cap_pcix_perm(&cap_perms[PCI_CAP_ID_PCIX]);
10608c2ecf20Sopenharmony_ci	cap_perms[PCI_CAP_ID_VNDR].writefn = vfio_raw_config_write;
10618c2ecf20Sopenharmony_ci	ret |= init_pci_cap_exp_perm(&cap_perms[PCI_CAP_ID_EXP]);
10628c2ecf20Sopenharmony_ci	ret |= init_pci_cap_af_perm(&cap_perms[PCI_CAP_ID_AF]);
10638c2ecf20Sopenharmony_ci
10648c2ecf20Sopenharmony_ci	/* Extended capabilities */
10658c2ecf20Sopenharmony_ci	ret |= init_pci_ext_cap_err_perm(&ecap_perms[PCI_EXT_CAP_ID_ERR]);
10668c2ecf20Sopenharmony_ci	ret |= init_pci_ext_cap_pwr_perm(&ecap_perms[PCI_EXT_CAP_ID_PWR]);
10678c2ecf20Sopenharmony_ci	ecap_perms[PCI_EXT_CAP_ID_VNDR].writefn = vfio_raw_config_write;
10688c2ecf20Sopenharmony_ci
10698c2ecf20Sopenharmony_ci	if (ret)
10708c2ecf20Sopenharmony_ci		vfio_pci_uninit_perm_bits();
10718c2ecf20Sopenharmony_ci
10728c2ecf20Sopenharmony_ci	return ret;
10738c2ecf20Sopenharmony_ci}
10748c2ecf20Sopenharmony_ci
10758c2ecf20Sopenharmony_cistatic int vfio_find_cap_start(struct vfio_pci_device *vdev, int pos)
10768c2ecf20Sopenharmony_ci{
10778c2ecf20Sopenharmony_ci	u8 cap;
10788c2ecf20Sopenharmony_ci	int base = (pos >= PCI_CFG_SPACE_SIZE) ? PCI_CFG_SPACE_SIZE :
10798c2ecf20Sopenharmony_ci						 PCI_STD_HEADER_SIZEOF;
10808c2ecf20Sopenharmony_ci	cap = vdev->pci_config_map[pos];
10818c2ecf20Sopenharmony_ci
10828c2ecf20Sopenharmony_ci	if (cap == PCI_CAP_ID_BASIC)
10838c2ecf20Sopenharmony_ci		return 0;
10848c2ecf20Sopenharmony_ci
10858c2ecf20Sopenharmony_ci	/* XXX Can we have to abutting capabilities of the same type? */
10868c2ecf20Sopenharmony_ci	while (pos - 1 >= base && vdev->pci_config_map[pos - 1] == cap)
10878c2ecf20Sopenharmony_ci		pos--;
10888c2ecf20Sopenharmony_ci
10898c2ecf20Sopenharmony_ci	return pos;
10908c2ecf20Sopenharmony_ci}
10918c2ecf20Sopenharmony_ci
10928c2ecf20Sopenharmony_cistatic int vfio_msi_config_read(struct vfio_pci_device *vdev, int pos,
10938c2ecf20Sopenharmony_ci				int count, struct perm_bits *perm,
10948c2ecf20Sopenharmony_ci				int offset, __le32 *val)
10958c2ecf20Sopenharmony_ci{
10968c2ecf20Sopenharmony_ci	/* Update max available queue size from msi_qmax */
10978c2ecf20Sopenharmony_ci	if (offset <= PCI_MSI_FLAGS && offset + count >= PCI_MSI_FLAGS) {
10988c2ecf20Sopenharmony_ci		__le16 *flags;
10998c2ecf20Sopenharmony_ci		int start;
11008c2ecf20Sopenharmony_ci
11018c2ecf20Sopenharmony_ci		start = vfio_find_cap_start(vdev, pos);
11028c2ecf20Sopenharmony_ci
11038c2ecf20Sopenharmony_ci		flags = (__le16 *)&vdev->vconfig[start];
11048c2ecf20Sopenharmony_ci
11058c2ecf20Sopenharmony_ci		*flags &= cpu_to_le16(~PCI_MSI_FLAGS_QMASK);
11068c2ecf20Sopenharmony_ci		*flags |= cpu_to_le16(vdev->msi_qmax << 1);
11078c2ecf20Sopenharmony_ci	}
11088c2ecf20Sopenharmony_ci
11098c2ecf20Sopenharmony_ci	return vfio_default_config_read(vdev, pos, count, perm, offset, val);
11108c2ecf20Sopenharmony_ci}
11118c2ecf20Sopenharmony_ci
11128c2ecf20Sopenharmony_cistatic int vfio_msi_config_write(struct vfio_pci_device *vdev, int pos,
11138c2ecf20Sopenharmony_ci				 int count, struct perm_bits *perm,
11148c2ecf20Sopenharmony_ci				 int offset, __le32 val)
11158c2ecf20Sopenharmony_ci{
11168c2ecf20Sopenharmony_ci	count = vfio_default_config_write(vdev, pos, count, perm, offset, val);
11178c2ecf20Sopenharmony_ci	if (count < 0)
11188c2ecf20Sopenharmony_ci		return count;
11198c2ecf20Sopenharmony_ci
11208c2ecf20Sopenharmony_ci	/* Fixup and write configured queue size and enable to hardware */
11218c2ecf20Sopenharmony_ci	if (offset <= PCI_MSI_FLAGS && offset + count >= PCI_MSI_FLAGS) {
11228c2ecf20Sopenharmony_ci		__le16 *pflags;
11238c2ecf20Sopenharmony_ci		u16 flags;
11248c2ecf20Sopenharmony_ci		int start, ret;
11258c2ecf20Sopenharmony_ci
11268c2ecf20Sopenharmony_ci		start = vfio_find_cap_start(vdev, pos);
11278c2ecf20Sopenharmony_ci
11288c2ecf20Sopenharmony_ci		pflags = (__le16 *)&vdev->vconfig[start + PCI_MSI_FLAGS];
11298c2ecf20Sopenharmony_ci
11308c2ecf20Sopenharmony_ci		flags = le16_to_cpu(*pflags);
11318c2ecf20Sopenharmony_ci
11328c2ecf20Sopenharmony_ci		/* MSI is enabled via ioctl */
11338c2ecf20Sopenharmony_ci		if  (!is_msi(vdev))
11348c2ecf20Sopenharmony_ci			flags &= ~PCI_MSI_FLAGS_ENABLE;
11358c2ecf20Sopenharmony_ci
11368c2ecf20Sopenharmony_ci		/* Check queue size */
11378c2ecf20Sopenharmony_ci		if ((flags & PCI_MSI_FLAGS_QSIZE) >> 4 > vdev->msi_qmax) {
11388c2ecf20Sopenharmony_ci			flags &= ~PCI_MSI_FLAGS_QSIZE;
11398c2ecf20Sopenharmony_ci			flags |= vdev->msi_qmax << 4;
11408c2ecf20Sopenharmony_ci		}
11418c2ecf20Sopenharmony_ci
11428c2ecf20Sopenharmony_ci		/* Write back to virt and to hardware */
11438c2ecf20Sopenharmony_ci		*pflags = cpu_to_le16(flags);
11448c2ecf20Sopenharmony_ci		ret = pci_user_write_config_word(vdev->pdev,
11458c2ecf20Sopenharmony_ci						 start + PCI_MSI_FLAGS,
11468c2ecf20Sopenharmony_ci						 flags);
11478c2ecf20Sopenharmony_ci		if (ret)
11488c2ecf20Sopenharmony_ci			return ret;
11498c2ecf20Sopenharmony_ci	}
11508c2ecf20Sopenharmony_ci
11518c2ecf20Sopenharmony_ci	return count;
11528c2ecf20Sopenharmony_ci}
11538c2ecf20Sopenharmony_ci
11548c2ecf20Sopenharmony_ci/*
11558c2ecf20Sopenharmony_ci * MSI determination is per-device, so this routine gets used beyond
11568c2ecf20Sopenharmony_ci * initialization time. Don't add __init
11578c2ecf20Sopenharmony_ci */
11588c2ecf20Sopenharmony_cistatic int init_pci_cap_msi_perm(struct perm_bits *perm, int len, u16 flags)
11598c2ecf20Sopenharmony_ci{
11608c2ecf20Sopenharmony_ci	if (alloc_perm_bits(perm, len))
11618c2ecf20Sopenharmony_ci		return -ENOMEM;
11628c2ecf20Sopenharmony_ci
11638c2ecf20Sopenharmony_ci	perm->readfn = vfio_msi_config_read;
11648c2ecf20Sopenharmony_ci	perm->writefn = vfio_msi_config_write;
11658c2ecf20Sopenharmony_ci
11668c2ecf20Sopenharmony_ci	p_setb(perm, PCI_CAP_LIST_NEXT, (u8)ALL_VIRT, NO_WRITE);
11678c2ecf20Sopenharmony_ci
11688c2ecf20Sopenharmony_ci	/*
11698c2ecf20Sopenharmony_ci	 * The upper byte of the control register is reserved,
11708c2ecf20Sopenharmony_ci	 * just setup the lower byte.
11718c2ecf20Sopenharmony_ci	 */
11728c2ecf20Sopenharmony_ci	p_setb(perm, PCI_MSI_FLAGS, (u8)ALL_VIRT, (u8)ALL_WRITE);
11738c2ecf20Sopenharmony_ci	p_setd(perm, PCI_MSI_ADDRESS_LO, ALL_VIRT, ALL_WRITE);
11748c2ecf20Sopenharmony_ci	if (flags & PCI_MSI_FLAGS_64BIT) {
11758c2ecf20Sopenharmony_ci		p_setd(perm, PCI_MSI_ADDRESS_HI, ALL_VIRT, ALL_WRITE);
11768c2ecf20Sopenharmony_ci		p_setw(perm, PCI_MSI_DATA_64, (u16)ALL_VIRT, (u16)ALL_WRITE);
11778c2ecf20Sopenharmony_ci		if (flags & PCI_MSI_FLAGS_MASKBIT) {
11788c2ecf20Sopenharmony_ci			p_setd(perm, PCI_MSI_MASK_64, NO_VIRT, ALL_WRITE);
11798c2ecf20Sopenharmony_ci			p_setd(perm, PCI_MSI_PENDING_64, NO_VIRT, ALL_WRITE);
11808c2ecf20Sopenharmony_ci		}
11818c2ecf20Sopenharmony_ci	} else {
11828c2ecf20Sopenharmony_ci		p_setw(perm, PCI_MSI_DATA_32, (u16)ALL_VIRT, (u16)ALL_WRITE);
11838c2ecf20Sopenharmony_ci		if (flags & PCI_MSI_FLAGS_MASKBIT) {
11848c2ecf20Sopenharmony_ci			p_setd(perm, PCI_MSI_MASK_32, NO_VIRT, ALL_WRITE);
11858c2ecf20Sopenharmony_ci			p_setd(perm, PCI_MSI_PENDING_32, NO_VIRT, ALL_WRITE);
11868c2ecf20Sopenharmony_ci		}
11878c2ecf20Sopenharmony_ci	}
11888c2ecf20Sopenharmony_ci	return 0;
11898c2ecf20Sopenharmony_ci}
11908c2ecf20Sopenharmony_ci
11918c2ecf20Sopenharmony_ci/* Determine MSI CAP field length; initialize msi_perms on 1st call per vdev */
11928c2ecf20Sopenharmony_cistatic int vfio_msi_cap_len(struct vfio_pci_device *vdev, u8 pos)
11938c2ecf20Sopenharmony_ci{
11948c2ecf20Sopenharmony_ci	struct pci_dev *pdev = vdev->pdev;
11958c2ecf20Sopenharmony_ci	int len, ret;
11968c2ecf20Sopenharmony_ci	u16 flags;
11978c2ecf20Sopenharmony_ci
11988c2ecf20Sopenharmony_ci	ret = pci_read_config_word(pdev, pos + PCI_MSI_FLAGS, &flags);
11998c2ecf20Sopenharmony_ci	if (ret)
12008c2ecf20Sopenharmony_ci		return pcibios_err_to_errno(ret);
12018c2ecf20Sopenharmony_ci
12028c2ecf20Sopenharmony_ci	len = 10; /* Minimum size */
12038c2ecf20Sopenharmony_ci	if (flags & PCI_MSI_FLAGS_64BIT)
12048c2ecf20Sopenharmony_ci		len += 4;
12058c2ecf20Sopenharmony_ci	if (flags & PCI_MSI_FLAGS_MASKBIT)
12068c2ecf20Sopenharmony_ci		len += 10;
12078c2ecf20Sopenharmony_ci
12088c2ecf20Sopenharmony_ci	if (vdev->msi_perm)
12098c2ecf20Sopenharmony_ci		return len;
12108c2ecf20Sopenharmony_ci
12118c2ecf20Sopenharmony_ci	vdev->msi_perm = kmalloc(sizeof(struct perm_bits), GFP_KERNEL);
12128c2ecf20Sopenharmony_ci	if (!vdev->msi_perm)
12138c2ecf20Sopenharmony_ci		return -ENOMEM;
12148c2ecf20Sopenharmony_ci
12158c2ecf20Sopenharmony_ci	ret = init_pci_cap_msi_perm(vdev->msi_perm, len, flags);
12168c2ecf20Sopenharmony_ci	if (ret) {
12178c2ecf20Sopenharmony_ci		kfree(vdev->msi_perm);
12188c2ecf20Sopenharmony_ci		return ret;
12198c2ecf20Sopenharmony_ci	}
12208c2ecf20Sopenharmony_ci
12218c2ecf20Sopenharmony_ci	return len;
12228c2ecf20Sopenharmony_ci}
12238c2ecf20Sopenharmony_ci
12248c2ecf20Sopenharmony_ci/* Determine extended capability length for VC (2 & 9) and MFVC */
12258c2ecf20Sopenharmony_cistatic int vfio_vc_cap_len(struct vfio_pci_device *vdev, u16 pos)
12268c2ecf20Sopenharmony_ci{
12278c2ecf20Sopenharmony_ci	struct pci_dev *pdev = vdev->pdev;
12288c2ecf20Sopenharmony_ci	u32 tmp;
12298c2ecf20Sopenharmony_ci	int ret, evcc, phases, vc_arb;
12308c2ecf20Sopenharmony_ci	int len = PCI_CAP_VC_BASE_SIZEOF;
12318c2ecf20Sopenharmony_ci
12328c2ecf20Sopenharmony_ci	ret = pci_read_config_dword(pdev, pos + PCI_VC_PORT_CAP1, &tmp);
12338c2ecf20Sopenharmony_ci	if (ret)
12348c2ecf20Sopenharmony_ci		return pcibios_err_to_errno(ret);
12358c2ecf20Sopenharmony_ci
12368c2ecf20Sopenharmony_ci	evcc = tmp & PCI_VC_CAP1_EVCC; /* extended vc count */
12378c2ecf20Sopenharmony_ci	ret = pci_read_config_dword(pdev, pos + PCI_VC_PORT_CAP2, &tmp);
12388c2ecf20Sopenharmony_ci	if (ret)
12398c2ecf20Sopenharmony_ci		return pcibios_err_to_errno(ret);
12408c2ecf20Sopenharmony_ci
12418c2ecf20Sopenharmony_ci	if (tmp & PCI_VC_CAP2_128_PHASE)
12428c2ecf20Sopenharmony_ci		phases = 128;
12438c2ecf20Sopenharmony_ci	else if (tmp & PCI_VC_CAP2_64_PHASE)
12448c2ecf20Sopenharmony_ci		phases = 64;
12458c2ecf20Sopenharmony_ci	else if (tmp & PCI_VC_CAP2_32_PHASE)
12468c2ecf20Sopenharmony_ci		phases = 32;
12478c2ecf20Sopenharmony_ci	else
12488c2ecf20Sopenharmony_ci		phases = 0;
12498c2ecf20Sopenharmony_ci
12508c2ecf20Sopenharmony_ci	vc_arb = phases * 4;
12518c2ecf20Sopenharmony_ci
12528c2ecf20Sopenharmony_ci	/*
12538c2ecf20Sopenharmony_ci	 * Port arbitration tables are root & switch only;
12548c2ecf20Sopenharmony_ci	 * function arbitration tables are function 0 only.
12558c2ecf20Sopenharmony_ci	 * In either case, we'll never let user write them so
12568c2ecf20Sopenharmony_ci	 * we don't care how big they are
12578c2ecf20Sopenharmony_ci	 */
12588c2ecf20Sopenharmony_ci	len += (1 + evcc) * PCI_CAP_VC_PER_VC_SIZEOF;
12598c2ecf20Sopenharmony_ci	if (vc_arb) {
12608c2ecf20Sopenharmony_ci		len = round_up(len, 16);
12618c2ecf20Sopenharmony_ci		len += vc_arb / 8;
12628c2ecf20Sopenharmony_ci	}
12638c2ecf20Sopenharmony_ci	return len;
12648c2ecf20Sopenharmony_ci}
12658c2ecf20Sopenharmony_ci
12668c2ecf20Sopenharmony_cistatic int vfio_cap_len(struct vfio_pci_device *vdev, u8 cap, u8 pos)
12678c2ecf20Sopenharmony_ci{
12688c2ecf20Sopenharmony_ci	struct pci_dev *pdev = vdev->pdev;
12698c2ecf20Sopenharmony_ci	u32 dword;
12708c2ecf20Sopenharmony_ci	u16 word;
12718c2ecf20Sopenharmony_ci	u8 byte;
12728c2ecf20Sopenharmony_ci	int ret;
12738c2ecf20Sopenharmony_ci
12748c2ecf20Sopenharmony_ci	switch (cap) {
12758c2ecf20Sopenharmony_ci	case PCI_CAP_ID_MSI:
12768c2ecf20Sopenharmony_ci		return vfio_msi_cap_len(vdev, pos);
12778c2ecf20Sopenharmony_ci	case PCI_CAP_ID_PCIX:
12788c2ecf20Sopenharmony_ci		ret = pci_read_config_word(pdev, pos + PCI_X_CMD, &word);
12798c2ecf20Sopenharmony_ci		if (ret)
12808c2ecf20Sopenharmony_ci			return pcibios_err_to_errno(ret);
12818c2ecf20Sopenharmony_ci
12828c2ecf20Sopenharmony_ci		if (PCI_X_CMD_VERSION(word)) {
12838c2ecf20Sopenharmony_ci			if (pdev->cfg_size > PCI_CFG_SPACE_SIZE) {
12848c2ecf20Sopenharmony_ci				/* Test for extended capabilities */
12858c2ecf20Sopenharmony_ci				pci_read_config_dword(pdev, PCI_CFG_SPACE_SIZE,
12868c2ecf20Sopenharmony_ci						      &dword);
12878c2ecf20Sopenharmony_ci				vdev->extended_caps = (dword != 0);
12888c2ecf20Sopenharmony_ci			}
12898c2ecf20Sopenharmony_ci			return PCI_CAP_PCIX_SIZEOF_V2;
12908c2ecf20Sopenharmony_ci		} else
12918c2ecf20Sopenharmony_ci			return PCI_CAP_PCIX_SIZEOF_V0;
12928c2ecf20Sopenharmony_ci	case PCI_CAP_ID_VNDR:
12938c2ecf20Sopenharmony_ci		/* length follows next field */
12948c2ecf20Sopenharmony_ci		ret = pci_read_config_byte(pdev, pos + PCI_CAP_FLAGS, &byte);
12958c2ecf20Sopenharmony_ci		if (ret)
12968c2ecf20Sopenharmony_ci			return pcibios_err_to_errno(ret);
12978c2ecf20Sopenharmony_ci
12988c2ecf20Sopenharmony_ci		return byte;
12998c2ecf20Sopenharmony_ci	case PCI_CAP_ID_EXP:
13008c2ecf20Sopenharmony_ci		if (pdev->cfg_size > PCI_CFG_SPACE_SIZE) {
13018c2ecf20Sopenharmony_ci			/* Test for extended capabilities */
13028c2ecf20Sopenharmony_ci			pci_read_config_dword(pdev, PCI_CFG_SPACE_SIZE, &dword);
13038c2ecf20Sopenharmony_ci			vdev->extended_caps = (dword != 0);
13048c2ecf20Sopenharmony_ci		}
13058c2ecf20Sopenharmony_ci
13068c2ecf20Sopenharmony_ci		/* length based on version and type */
13078c2ecf20Sopenharmony_ci		if ((pcie_caps_reg(pdev) & PCI_EXP_FLAGS_VERS) == 1) {
13088c2ecf20Sopenharmony_ci			if (pci_pcie_type(pdev) == PCI_EXP_TYPE_RC_END)
13098c2ecf20Sopenharmony_ci				return 0xc; /* "All Devices" only, no link */
13108c2ecf20Sopenharmony_ci			return PCI_CAP_EXP_ENDPOINT_SIZEOF_V1;
13118c2ecf20Sopenharmony_ci		} else {
13128c2ecf20Sopenharmony_ci			if (pci_pcie_type(pdev) == PCI_EXP_TYPE_RC_END)
13138c2ecf20Sopenharmony_ci				return 0x2c; /* No link */
13148c2ecf20Sopenharmony_ci			return PCI_CAP_EXP_ENDPOINT_SIZEOF_V2;
13158c2ecf20Sopenharmony_ci		}
13168c2ecf20Sopenharmony_ci	case PCI_CAP_ID_HT:
13178c2ecf20Sopenharmony_ci		ret = pci_read_config_byte(pdev, pos + 3, &byte);
13188c2ecf20Sopenharmony_ci		if (ret)
13198c2ecf20Sopenharmony_ci			return pcibios_err_to_errno(ret);
13208c2ecf20Sopenharmony_ci
13218c2ecf20Sopenharmony_ci		return (byte & HT_3BIT_CAP_MASK) ?
13228c2ecf20Sopenharmony_ci			HT_CAP_SIZEOF_SHORT : HT_CAP_SIZEOF_LONG;
13238c2ecf20Sopenharmony_ci	case PCI_CAP_ID_SATA:
13248c2ecf20Sopenharmony_ci		ret = pci_read_config_byte(pdev, pos + PCI_SATA_REGS, &byte);
13258c2ecf20Sopenharmony_ci		if (ret)
13268c2ecf20Sopenharmony_ci			return pcibios_err_to_errno(ret);
13278c2ecf20Sopenharmony_ci
13288c2ecf20Sopenharmony_ci		byte &= PCI_SATA_REGS_MASK;
13298c2ecf20Sopenharmony_ci		if (byte == PCI_SATA_REGS_INLINE)
13308c2ecf20Sopenharmony_ci			return PCI_SATA_SIZEOF_LONG;
13318c2ecf20Sopenharmony_ci		else
13328c2ecf20Sopenharmony_ci			return PCI_SATA_SIZEOF_SHORT;
13338c2ecf20Sopenharmony_ci	default:
13348c2ecf20Sopenharmony_ci		pci_warn(pdev, "%s: unknown length for PCI cap %#x@%#x\n",
13358c2ecf20Sopenharmony_ci			 __func__, cap, pos);
13368c2ecf20Sopenharmony_ci	}
13378c2ecf20Sopenharmony_ci
13388c2ecf20Sopenharmony_ci	return 0;
13398c2ecf20Sopenharmony_ci}
13408c2ecf20Sopenharmony_ci
13418c2ecf20Sopenharmony_cistatic int vfio_ext_cap_len(struct vfio_pci_device *vdev, u16 ecap, u16 epos)
13428c2ecf20Sopenharmony_ci{
13438c2ecf20Sopenharmony_ci	struct pci_dev *pdev = vdev->pdev;
13448c2ecf20Sopenharmony_ci	u8 byte;
13458c2ecf20Sopenharmony_ci	u32 dword;
13468c2ecf20Sopenharmony_ci	int ret;
13478c2ecf20Sopenharmony_ci
13488c2ecf20Sopenharmony_ci	switch (ecap) {
13498c2ecf20Sopenharmony_ci	case PCI_EXT_CAP_ID_VNDR:
13508c2ecf20Sopenharmony_ci		ret = pci_read_config_dword(pdev, epos + PCI_VSEC_HDR, &dword);
13518c2ecf20Sopenharmony_ci		if (ret)
13528c2ecf20Sopenharmony_ci			return pcibios_err_to_errno(ret);
13538c2ecf20Sopenharmony_ci
13548c2ecf20Sopenharmony_ci		return dword >> PCI_VSEC_HDR_LEN_SHIFT;
13558c2ecf20Sopenharmony_ci	case PCI_EXT_CAP_ID_VC:
13568c2ecf20Sopenharmony_ci	case PCI_EXT_CAP_ID_VC9:
13578c2ecf20Sopenharmony_ci	case PCI_EXT_CAP_ID_MFVC:
13588c2ecf20Sopenharmony_ci		return vfio_vc_cap_len(vdev, epos);
13598c2ecf20Sopenharmony_ci	case PCI_EXT_CAP_ID_ACS:
13608c2ecf20Sopenharmony_ci		ret = pci_read_config_byte(pdev, epos + PCI_ACS_CAP, &byte);
13618c2ecf20Sopenharmony_ci		if (ret)
13628c2ecf20Sopenharmony_ci			return pcibios_err_to_errno(ret);
13638c2ecf20Sopenharmony_ci
13648c2ecf20Sopenharmony_ci		if (byte & PCI_ACS_EC) {
13658c2ecf20Sopenharmony_ci			int bits;
13668c2ecf20Sopenharmony_ci
13678c2ecf20Sopenharmony_ci			ret = pci_read_config_byte(pdev,
13688c2ecf20Sopenharmony_ci						   epos + PCI_ACS_EGRESS_BITS,
13698c2ecf20Sopenharmony_ci						   &byte);
13708c2ecf20Sopenharmony_ci			if (ret)
13718c2ecf20Sopenharmony_ci				return pcibios_err_to_errno(ret);
13728c2ecf20Sopenharmony_ci
13738c2ecf20Sopenharmony_ci			bits = byte ? round_up(byte, 32) : 256;
13748c2ecf20Sopenharmony_ci			return 8 + (bits / 8);
13758c2ecf20Sopenharmony_ci		}
13768c2ecf20Sopenharmony_ci		return 8;
13778c2ecf20Sopenharmony_ci
13788c2ecf20Sopenharmony_ci	case PCI_EXT_CAP_ID_REBAR:
13798c2ecf20Sopenharmony_ci		ret = pci_read_config_byte(pdev, epos + PCI_REBAR_CTRL, &byte);
13808c2ecf20Sopenharmony_ci		if (ret)
13818c2ecf20Sopenharmony_ci			return pcibios_err_to_errno(ret);
13828c2ecf20Sopenharmony_ci
13838c2ecf20Sopenharmony_ci		byte &= PCI_REBAR_CTRL_NBAR_MASK;
13848c2ecf20Sopenharmony_ci		byte >>= PCI_REBAR_CTRL_NBAR_SHIFT;
13858c2ecf20Sopenharmony_ci
13868c2ecf20Sopenharmony_ci		return 4 + (byte * 8);
13878c2ecf20Sopenharmony_ci	case PCI_EXT_CAP_ID_DPA:
13888c2ecf20Sopenharmony_ci		ret = pci_read_config_byte(pdev, epos + PCI_DPA_CAP, &byte);
13898c2ecf20Sopenharmony_ci		if (ret)
13908c2ecf20Sopenharmony_ci			return pcibios_err_to_errno(ret);
13918c2ecf20Sopenharmony_ci
13928c2ecf20Sopenharmony_ci		byte &= PCI_DPA_CAP_SUBSTATE_MASK;
13938c2ecf20Sopenharmony_ci		return PCI_DPA_BASE_SIZEOF + byte + 1;
13948c2ecf20Sopenharmony_ci	case PCI_EXT_CAP_ID_TPH:
13958c2ecf20Sopenharmony_ci		ret = pci_read_config_dword(pdev, epos + PCI_TPH_CAP, &dword);
13968c2ecf20Sopenharmony_ci		if (ret)
13978c2ecf20Sopenharmony_ci			return pcibios_err_to_errno(ret);
13988c2ecf20Sopenharmony_ci
13998c2ecf20Sopenharmony_ci		if ((dword & PCI_TPH_CAP_LOC_MASK) == PCI_TPH_LOC_CAP) {
14008c2ecf20Sopenharmony_ci			int sts;
14018c2ecf20Sopenharmony_ci
14028c2ecf20Sopenharmony_ci			sts = dword & PCI_TPH_CAP_ST_MASK;
14038c2ecf20Sopenharmony_ci			sts >>= PCI_TPH_CAP_ST_SHIFT;
14048c2ecf20Sopenharmony_ci			return PCI_TPH_BASE_SIZEOF + (sts * 2) + 2;
14058c2ecf20Sopenharmony_ci		}
14068c2ecf20Sopenharmony_ci		return PCI_TPH_BASE_SIZEOF;
14078c2ecf20Sopenharmony_ci	default:
14088c2ecf20Sopenharmony_ci		pci_warn(pdev, "%s: unknown length for PCI ecap %#x@%#x\n",
14098c2ecf20Sopenharmony_ci			 __func__, ecap, epos);
14108c2ecf20Sopenharmony_ci	}
14118c2ecf20Sopenharmony_ci
14128c2ecf20Sopenharmony_ci	return 0;
14138c2ecf20Sopenharmony_ci}
14148c2ecf20Sopenharmony_ci
14158c2ecf20Sopenharmony_cistatic int vfio_fill_vconfig_bytes(struct vfio_pci_device *vdev,
14168c2ecf20Sopenharmony_ci				   int offset, int size)
14178c2ecf20Sopenharmony_ci{
14188c2ecf20Sopenharmony_ci	struct pci_dev *pdev = vdev->pdev;
14198c2ecf20Sopenharmony_ci	int ret = 0;
14208c2ecf20Sopenharmony_ci
14218c2ecf20Sopenharmony_ci	/*
14228c2ecf20Sopenharmony_ci	 * We try to read physical config space in the largest chunks
14238c2ecf20Sopenharmony_ci	 * we can, assuming that all of the fields support dword access.
14248c2ecf20Sopenharmony_ci	 * pci_save_state() makes this same assumption and seems to do ok.
14258c2ecf20Sopenharmony_ci	 */
14268c2ecf20Sopenharmony_ci	while (size) {
14278c2ecf20Sopenharmony_ci		int filled;
14288c2ecf20Sopenharmony_ci
14298c2ecf20Sopenharmony_ci		if (size >= 4 && !(offset % 4)) {
14308c2ecf20Sopenharmony_ci			__le32 *dwordp = (__le32 *)&vdev->vconfig[offset];
14318c2ecf20Sopenharmony_ci			u32 dword;
14328c2ecf20Sopenharmony_ci
14338c2ecf20Sopenharmony_ci			ret = pci_read_config_dword(pdev, offset, &dword);
14348c2ecf20Sopenharmony_ci			if (ret)
14358c2ecf20Sopenharmony_ci				return ret;
14368c2ecf20Sopenharmony_ci			*dwordp = cpu_to_le32(dword);
14378c2ecf20Sopenharmony_ci			filled = 4;
14388c2ecf20Sopenharmony_ci		} else if (size >= 2 && !(offset % 2)) {
14398c2ecf20Sopenharmony_ci			__le16 *wordp = (__le16 *)&vdev->vconfig[offset];
14408c2ecf20Sopenharmony_ci			u16 word;
14418c2ecf20Sopenharmony_ci
14428c2ecf20Sopenharmony_ci			ret = pci_read_config_word(pdev, offset, &word);
14438c2ecf20Sopenharmony_ci			if (ret)
14448c2ecf20Sopenharmony_ci				return ret;
14458c2ecf20Sopenharmony_ci			*wordp = cpu_to_le16(word);
14468c2ecf20Sopenharmony_ci			filled = 2;
14478c2ecf20Sopenharmony_ci		} else {
14488c2ecf20Sopenharmony_ci			u8 *byte = &vdev->vconfig[offset];
14498c2ecf20Sopenharmony_ci			ret = pci_read_config_byte(pdev, offset, byte);
14508c2ecf20Sopenharmony_ci			if (ret)
14518c2ecf20Sopenharmony_ci				return ret;
14528c2ecf20Sopenharmony_ci			filled = 1;
14538c2ecf20Sopenharmony_ci		}
14548c2ecf20Sopenharmony_ci
14558c2ecf20Sopenharmony_ci		offset += filled;
14568c2ecf20Sopenharmony_ci		size -= filled;
14578c2ecf20Sopenharmony_ci	}
14588c2ecf20Sopenharmony_ci
14598c2ecf20Sopenharmony_ci	return ret;
14608c2ecf20Sopenharmony_ci}
14618c2ecf20Sopenharmony_ci
14628c2ecf20Sopenharmony_cistatic int vfio_cap_init(struct vfio_pci_device *vdev)
14638c2ecf20Sopenharmony_ci{
14648c2ecf20Sopenharmony_ci	struct pci_dev *pdev = vdev->pdev;
14658c2ecf20Sopenharmony_ci	u8 *map = vdev->pci_config_map;
14668c2ecf20Sopenharmony_ci	u16 status;
14678c2ecf20Sopenharmony_ci	u8 pos, *prev, cap;
14688c2ecf20Sopenharmony_ci	int loops, ret, caps = 0;
14698c2ecf20Sopenharmony_ci
14708c2ecf20Sopenharmony_ci	/* Any capabilities? */
14718c2ecf20Sopenharmony_ci	ret = pci_read_config_word(pdev, PCI_STATUS, &status);
14728c2ecf20Sopenharmony_ci	if (ret)
14738c2ecf20Sopenharmony_ci		return ret;
14748c2ecf20Sopenharmony_ci
14758c2ecf20Sopenharmony_ci	if (!(status & PCI_STATUS_CAP_LIST))
14768c2ecf20Sopenharmony_ci		return 0; /* Done */
14778c2ecf20Sopenharmony_ci
14788c2ecf20Sopenharmony_ci	ret = pci_read_config_byte(pdev, PCI_CAPABILITY_LIST, &pos);
14798c2ecf20Sopenharmony_ci	if (ret)
14808c2ecf20Sopenharmony_ci		return ret;
14818c2ecf20Sopenharmony_ci
14828c2ecf20Sopenharmony_ci	/* Mark the previous position in case we want to skip a capability */
14838c2ecf20Sopenharmony_ci	prev = &vdev->vconfig[PCI_CAPABILITY_LIST];
14848c2ecf20Sopenharmony_ci
14858c2ecf20Sopenharmony_ci	/* We can bound our loop, capabilities are dword aligned */
14868c2ecf20Sopenharmony_ci	loops = (PCI_CFG_SPACE_SIZE - PCI_STD_HEADER_SIZEOF) / PCI_CAP_SIZEOF;
14878c2ecf20Sopenharmony_ci	while (pos && loops--) {
14888c2ecf20Sopenharmony_ci		u8 next;
14898c2ecf20Sopenharmony_ci		int i, len = 0;
14908c2ecf20Sopenharmony_ci
14918c2ecf20Sopenharmony_ci		ret = pci_read_config_byte(pdev, pos, &cap);
14928c2ecf20Sopenharmony_ci		if (ret)
14938c2ecf20Sopenharmony_ci			return ret;
14948c2ecf20Sopenharmony_ci
14958c2ecf20Sopenharmony_ci		ret = pci_read_config_byte(pdev,
14968c2ecf20Sopenharmony_ci					   pos + PCI_CAP_LIST_NEXT, &next);
14978c2ecf20Sopenharmony_ci		if (ret)
14988c2ecf20Sopenharmony_ci			return ret;
14998c2ecf20Sopenharmony_ci
15008c2ecf20Sopenharmony_ci		/*
15018c2ecf20Sopenharmony_ci		 * ID 0 is a NULL capability, conflicting with our fake
15028c2ecf20Sopenharmony_ci		 * PCI_CAP_ID_BASIC.  As it has no content, consider it
15038c2ecf20Sopenharmony_ci		 * hidden for now.
15048c2ecf20Sopenharmony_ci		 */
15058c2ecf20Sopenharmony_ci		if (cap && cap <= PCI_CAP_ID_MAX) {
15068c2ecf20Sopenharmony_ci			len = pci_cap_length[cap];
15078c2ecf20Sopenharmony_ci			if (len == 0xFF) { /* Variable length */
15088c2ecf20Sopenharmony_ci				len = vfio_cap_len(vdev, cap, pos);
15098c2ecf20Sopenharmony_ci				if (len < 0)
15108c2ecf20Sopenharmony_ci					return len;
15118c2ecf20Sopenharmony_ci			}
15128c2ecf20Sopenharmony_ci		}
15138c2ecf20Sopenharmony_ci
15148c2ecf20Sopenharmony_ci		if (!len) {
15158c2ecf20Sopenharmony_ci			pci_info(pdev, "%s: hiding cap %#x@%#x\n", __func__,
15168c2ecf20Sopenharmony_ci				 cap, pos);
15178c2ecf20Sopenharmony_ci			*prev = next;
15188c2ecf20Sopenharmony_ci			pos = next;
15198c2ecf20Sopenharmony_ci			continue;
15208c2ecf20Sopenharmony_ci		}
15218c2ecf20Sopenharmony_ci
15228c2ecf20Sopenharmony_ci		/* Sanity check, do we overlap other capabilities? */
15238c2ecf20Sopenharmony_ci		for (i = 0; i < len; i++) {
15248c2ecf20Sopenharmony_ci			if (likely(map[pos + i] == PCI_CAP_ID_INVALID))
15258c2ecf20Sopenharmony_ci				continue;
15268c2ecf20Sopenharmony_ci
15278c2ecf20Sopenharmony_ci			pci_warn(pdev, "%s: PCI config conflict @%#x, was cap %#x now cap %#x\n",
15288c2ecf20Sopenharmony_ci				 __func__, pos + i, map[pos + i], cap);
15298c2ecf20Sopenharmony_ci		}
15308c2ecf20Sopenharmony_ci
15318c2ecf20Sopenharmony_ci		BUILD_BUG_ON(PCI_CAP_ID_MAX >= PCI_CAP_ID_INVALID_VIRT);
15328c2ecf20Sopenharmony_ci
15338c2ecf20Sopenharmony_ci		memset(map + pos, cap, len);
15348c2ecf20Sopenharmony_ci		ret = vfio_fill_vconfig_bytes(vdev, pos, len);
15358c2ecf20Sopenharmony_ci		if (ret)
15368c2ecf20Sopenharmony_ci			return ret;
15378c2ecf20Sopenharmony_ci
15388c2ecf20Sopenharmony_ci		prev = &vdev->vconfig[pos + PCI_CAP_LIST_NEXT];
15398c2ecf20Sopenharmony_ci		pos = next;
15408c2ecf20Sopenharmony_ci		caps++;
15418c2ecf20Sopenharmony_ci	}
15428c2ecf20Sopenharmony_ci
15438c2ecf20Sopenharmony_ci	/* If we didn't fill any capabilities, clear the status flag */
15448c2ecf20Sopenharmony_ci	if (!caps) {
15458c2ecf20Sopenharmony_ci		__le16 *vstatus = (__le16 *)&vdev->vconfig[PCI_STATUS];
15468c2ecf20Sopenharmony_ci		*vstatus &= ~cpu_to_le16(PCI_STATUS_CAP_LIST);
15478c2ecf20Sopenharmony_ci	}
15488c2ecf20Sopenharmony_ci
15498c2ecf20Sopenharmony_ci	return 0;
15508c2ecf20Sopenharmony_ci}
15518c2ecf20Sopenharmony_ci
15528c2ecf20Sopenharmony_cistatic int vfio_ecap_init(struct vfio_pci_device *vdev)
15538c2ecf20Sopenharmony_ci{
15548c2ecf20Sopenharmony_ci	struct pci_dev *pdev = vdev->pdev;
15558c2ecf20Sopenharmony_ci	u8 *map = vdev->pci_config_map;
15568c2ecf20Sopenharmony_ci	u16 epos;
15578c2ecf20Sopenharmony_ci	__le32 *prev = NULL;
15588c2ecf20Sopenharmony_ci	int loops, ret, ecaps = 0;
15598c2ecf20Sopenharmony_ci
15608c2ecf20Sopenharmony_ci	if (!vdev->extended_caps)
15618c2ecf20Sopenharmony_ci		return 0;
15628c2ecf20Sopenharmony_ci
15638c2ecf20Sopenharmony_ci	epos = PCI_CFG_SPACE_SIZE;
15648c2ecf20Sopenharmony_ci
15658c2ecf20Sopenharmony_ci	loops = (pdev->cfg_size - PCI_CFG_SPACE_SIZE) / PCI_CAP_SIZEOF;
15668c2ecf20Sopenharmony_ci
15678c2ecf20Sopenharmony_ci	while (loops-- && epos >= PCI_CFG_SPACE_SIZE) {
15688c2ecf20Sopenharmony_ci		u32 header;
15698c2ecf20Sopenharmony_ci		u16 ecap;
15708c2ecf20Sopenharmony_ci		int i, len = 0;
15718c2ecf20Sopenharmony_ci		bool hidden = false;
15728c2ecf20Sopenharmony_ci
15738c2ecf20Sopenharmony_ci		ret = pci_read_config_dword(pdev, epos, &header);
15748c2ecf20Sopenharmony_ci		if (ret)
15758c2ecf20Sopenharmony_ci			return ret;
15768c2ecf20Sopenharmony_ci
15778c2ecf20Sopenharmony_ci		ecap = PCI_EXT_CAP_ID(header);
15788c2ecf20Sopenharmony_ci
15798c2ecf20Sopenharmony_ci		if (ecap <= PCI_EXT_CAP_ID_MAX) {
15808c2ecf20Sopenharmony_ci			len = pci_ext_cap_length[ecap];
15818c2ecf20Sopenharmony_ci			if (len == 0xFF) {
15828c2ecf20Sopenharmony_ci				len = vfio_ext_cap_len(vdev, ecap, epos);
15838c2ecf20Sopenharmony_ci				if (len < 0)
15848c2ecf20Sopenharmony_ci					return len;
15858c2ecf20Sopenharmony_ci			}
15868c2ecf20Sopenharmony_ci		}
15878c2ecf20Sopenharmony_ci
15888c2ecf20Sopenharmony_ci		if (!len) {
15898c2ecf20Sopenharmony_ci			pci_info(pdev, "%s: hiding ecap %#x@%#x\n",
15908c2ecf20Sopenharmony_ci				 __func__, ecap, epos);
15918c2ecf20Sopenharmony_ci
15928c2ecf20Sopenharmony_ci			/* If not the first in the chain, we can skip over it */
15938c2ecf20Sopenharmony_ci			if (prev) {
15948c2ecf20Sopenharmony_ci				u32 val = epos = PCI_EXT_CAP_NEXT(header);
15958c2ecf20Sopenharmony_ci				*prev &= cpu_to_le32(~(0xffcU << 20));
15968c2ecf20Sopenharmony_ci				*prev |= cpu_to_le32(val << 20);
15978c2ecf20Sopenharmony_ci				continue;
15988c2ecf20Sopenharmony_ci			}
15998c2ecf20Sopenharmony_ci
16008c2ecf20Sopenharmony_ci			/*
16018c2ecf20Sopenharmony_ci			 * Otherwise, fill in a placeholder, the direct
16028c2ecf20Sopenharmony_ci			 * readfn will virtualize this automatically
16038c2ecf20Sopenharmony_ci			 */
16048c2ecf20Sopenharmony_ci			len = PCI_CAP_SIZEOF;
16058c2ecf20Sopenharmony_ci			hidden = true;
16068c2ecf20Sopenharmony_ci		}
16078c2ecf20Sopenharmony_ci
16088c2ecf20Sopenharmony_ci		for (i = 0; i < len; i++) {
16098c2ecf20Sopenharmony_ci			if (likely(map[epos + i] == PCI_CAP_ID_INVALID))
16108c2ecf20Sopenharmony_ci				continue;
16118c2ecf20Sopenharmony_ci
16128c2ecf20Sopenharmony_ci			pci_warn(pdev, "%s: PCI config conflict @%#x, was ecap %#x now ecap %#x\n",
16138c2ecf20Sopenharmony_ci				 __func__, epos + i, map[epos + i], ecap);
16148c2ecf20Sopenharmony_ci		}
16158c2ecf20Sopenharmony_ci
16168c2ecf20Sopenharmony_ci		/*
16178c2ecf20Sopenharmony_ci		 * Even though ecap is 2 bytes, we're currently a long way
16188c2ecf20Sopenharmony_ci		 * from exceeding 1 byte capabilities.  If we ever make it
16198c2ecf20Sopenharmony_ci		 * up to 0xFE we'll need to up this to a two-byte, byte map.
16208c2ecf20Sopenharmony_ci		 */
16218c2ecf20Sopenharmony_ci		BUILD_BUG_ON(PCI_EXT_CAP_ID_MAX >= PCI_CAP_ID_INVALID_VIRT);
16228c2ecf20Sopenharmony_ci
16238c2ecf20Sopenharmony_ci		memset(map + epos, ecap, len);
16248c2ecf20Sopenharmony_ci		ret = vfio_fill_vconfig_bytes(vdev, epos, len);
16258c2ecf20Sopenharmony_ci		if (ret)
16268c2ecf20Sopenharmony_ci			return ret;
16278c2ecf20Sopenharmony_ci
16288c2ecf20Sopenharmony_ci		/*
16298c2ecf20Sopenharmony_ci		 * If we're just using this capability to anchor the list,
16308c2ecf20Sopenharmony_ci		 * hide the real ID.  Only count real ecaps.  XXX PCI spec
16318c2ecf20Sopenharmony_ci		 * indicates to use cap id = 0, version = 0, next = 0 if
16328c2ecf20Sopenharmony_ci		 * ecaps are absent, hope users check all the way to next.
16338c2ecf20Sopenharmony_ci		 */
16348c2ecf20Sopenharmony_ci		if (hidden)
16358c2ecf20Sopenharmony_ci			*(__le32 *)&vdev->vconfig[epos] &=
16368c2ecf20Sopenharmony_ci				cpu_to_le32((0xffcU << 20));
16378c2ecf20Sopenharmony_ci		else
16388c2ecf20Sopenharmony_ci			ecaps++;
16398c2ecf20Sopenharmony_ci
16408c2ecf20Sopenharmony_ci		prev = (__le32 *)&vdev->vconfig[epos];
16418c2ecf20Sopenharmony_ci		epos = PCI_EXT_CAP_NEXT(header);
16428c2ecf20Sopenharmony_ci	}
16438c2ecf20Sopenharmony_ci
16448c2ecf20Sopenharmony_ci	if (!ecaps)
16458c2ecf20Sopenharmony_ci		*(u32 *)&vdev->vconfig[PCI_CFG_SPACE_SIZE] = 0;
16468c2ecf20Sopenharmony_ci
16478c2ecf20Sopenharmony_ci	return 0;
16488c2ecf20Sopenharmony_ci}
16498c2ecf20Sopenharmony_ci
16508c2ecf20Sopenharmony_ci/*
16518c2ecf20Sopenharmony_ci * Nag about hardware bugs, hopefully to have vendors fix them, but at least
16528c2ecf20Sopenharmony_ci * to collect a list of dependencies for the VF INTx pin quirk below.
16538c2ecf20Sopenharmony_ci */
16548c2ecf20Sopenharmony_cistatic const struct pci_device_id known_bogus_vf_intx_pin[] = {
16558c2ecf20Sopenharmony_ci	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x270c) },
16568c2ecf20Sopenharmony_ci	{}
16578c2ecf20Sopenharmony_ci};
16588c2ecf20Sopenharmony_ci
16598c2ecf20Sopenharmony_ci/*
16608c2ecf20Sopenharmony_ci * For each device we allocate a pci_config_map that indicates the
16618c2ecf20Sopenharmony_ci * capability occupying each dword and thus the struct perm_bits we
16628c2ecf20Sopenharmony_ci * use for read and write.  We also allocate a virtualized config
16638c2ecf20Sopenharmony_ci * space which tracks reads and writes to bits that we emulate for
16648c2ecf20Sopenharmony_ci * the user.  Initial values filled from device.
16658c2ecf20Sopenharmony_ci *
16668c2ecf20Sopenharmony_ci * Using shared struct perm_bits between all vfio-pci devices saves
16678c2ecf20Sopenharmony_ci * us from allocating cfg_size buffers for virt and write for every
16688c2ecf20Sopenharmony_ci * device.  We could remove vconfig and allocate individual buffers
16698c2ecf20Sopenharmony_ci * for each area requiring emulated bits, but the array of pointers
16708c2ecf20Sopenharmony_ci * would be comparable in size (at least for standard config space).
16718c2ecf20Sopenharmony_ci */
16728c2ecf20Sopenharmony_ciint vfio_config_init(struct vfio_pci_device *vdev)
16738c2ecf20Sopenharmony_ci{
16748c2ecf20Sopenharmony_ci	struct pci_dev *pdev = vdev->pdev;
16758c2ecf20Sopenharmony_ci	u8 *map, *vconfig;
16768c2ecf20Sopenharmony_ci	int ret;
16778c2ecf20Sopenharmony_ci
16788c2ecf20Sopenharmony_ci	/*
16798c2ecf20Sopenharmony_ci	 * Config space, caps and ecaps are all dword aligned, so we could
16808c2ecf20Sopenharmony_ci	 * use one byte per dword to record the type.  However, there are
16818c2ecf20Sopenharmony_ci	 * no requiremenst on the length of a capability, so the gap between
16828c2ecf20Sopenharmony_ci	 * capabilities needs byte granularity.
16838c2ecf20Sopenharmony_ci	 */
16848c2ecf20Sopenharmony_ci	map = kmalloc(pdev->cfg_size, GFP_KERNEL);
16858c2ecf20Sopenharmony_ci	if (!map)
16868c2ecf20Sopenharmony_ci		return -ENOMEM;
16878c2ecf20Sopenharmony_ci
16888c2ecf20Sopenharmony_ci	vconfig = kmalloc(pdev->cfg_size, GFP_KERNEL);
16898c2ecf20Sopenharmony_ci	if (!vconfig) {
16908c2ecf20Sopenharmony_ci		kfree(map);
16918c2ecf20Sopenharmony_ci		return -ENOMEM;
16928c2ecf20Sopenharmony_ci	}
16938c2ecf20Sopenharmony_ci
16948c2ecf20Sopenharmony_ci	vdev->pci_config_map = map;
16958c2ecf20Sopenharmony_ci	vdev->vconfig = vconfig;
16968c2ecf20Sopenharmony_ci
16978c2ecf20Sopenharmony_ci	memset(map, PCI_CAP_ID_BASIC, PCI_STD_HEADER_SIZEOF);
16988c2ecf20Sopenharmony_ci	memset(map + PCI_STD_HEADER_SIZEOF, PCI_CAP_ID_INVALID,
16998c2ecf20Sopenharmony_ci	       pdev->cfg_size - PCI_STD_HEADER_SIZEOF);
17008c2ecf20Sopenharmony_ci
17018c2ecf20Sopenharmony_ci	ret = vfio_fill_vconfig_bytes(vdev, 0, PCI_STD_HEADER_SIZEOF);
17028c2ecf20Sopenharmony_ci	if (ret)
17038c2ecf20Sopenharmony_ci		goto out;
17048c2ecf20Sopenharmony_ci
17058c2ecf20Sopenharmony_ci	vdev->bardirty = true;
17068c2ecf20Sopenharmony_ci
17078c2ecf20Sopenharmony_ci	/*
17088c2ecf20Sopenharmony_ci	 * XXX can we just pci_load_saved_state/pci_restore_state?
17098c2ecf20Sopenharmony_ci	 * may need to rebuild vconfig after that
17108c2ecf20Sopenharmony_ci	 */
17118c2ecf20Sopenharmony_ci
17128c2ecf20Sopenharmony_ci	/* For restore after reset */
17138c2ecf20Sopenharmony_ci	vdev->rbar[0] = le32_to_cpu(*(__le32 *)&vconfig[PCI_BASE_ADDRESS_0]);
17148c2ecf20Sopenharmony_ci	vdev->rbar[1] = le32_to_cpu(*(__le32 *)&vconfig[PCI_BASE_ADDRESS_1]);
17158c2ecf20Sopenharmony_ci	vdev->rbar[2] = le32_to_cpu(*(__le32 *)&vconfig[PCI_BASE_ADDRESS_2]);
17168c2ecf20Sopenharmony_ci	vdev->rbar[3] = le32_to_cpu(*(__le32 *)&vconfig[PCI_BASE_ADDRESS_3]);
17178c2ecf20Sopenharmony_ci	vdev->rbar[4] = le32_to_cpu(*(__le32 *)&vconfig[PCI_BASE_ADDRESS_4]);
17188c2ecf20Sopenharmony_ci	vdev->rbar[5] = le32_to_cpu(*(__le32 *)&vconfig[PCI_BASE_ADDRESS_5]);
17198c2ecf20Sopenharmony_ci	vdev->rbar[6] = le32_to_cpu(*(__le32 *)&vconfig[PCI_ROM_ADDRESS]);
17208c2ecf20Sopenharmony_ci
17218c2ecf20Sopenharmony_ci	if (pdev->is_virtfn) {
17228c2ecf20Sopenharmony_ci		*(__le16 *)&vconfig[PCI_VENDOR_ID] = cpu_to_le16(pdev->vendor);
17238c2ecf20Sopenharmony_ci		*(__le16 *)&vconfig[PCI_DEVICE_ID] = cpu_to_le16(pdev->device);
17248c2ecf20Sopenharmony_ci
17258c2ecf20Sopenharmony_ci		/*
17268c2ecf20Sopenharmony_ci		 * Per SR-IOV spec rev 1.1, 3.4.1.18 the interrupt pin register
17278c2ecf20Sopenharmony_ci		 * does not apply to VFs and VFs must implement this register
17288c2ecf20Sopenharmony_ci		 * as read-only with value zero.  Userspace is not readily able
17298c2ecf20Sopenharmony_ci		 * to identify whether a device is a VF and thus that the pin
17308c2ecf20Sopenharmony_ci		 * definition on the device is bogus should it violate this
17318c2ecf20Sopenharmony_ci		 * requirement.  We already virtualize the pin register for
17328c2ecf20Sopenharmony_ci		 * other purposes, so we simply need to replace the bogus value
17338c2ecf20Sopenharmony_ci		 * and consider VFs when we determine INTx IRQ count.
17348c2ecf20Sopenharmony_ci		 */
17358c2ecf20Sopenharmony_ci		if (vconfig[PCI_INTERRUPT_PIN] &&
17368c2ecf20Sopenharmony_ci		    !pci_match_id(known_bogus_vf_intx_pin, pdev))
17378c2ecf20Sopenharmony_ci			pci_warn(pdev,
17388c2ecf20Sopenharmony_ci				 "Hardware bug: VF reports bogus INTx pin %d\n",
17398c2ecf20Sopenharmony_ci				 vconfig[PCI_INTERRUPT_PIN]);
17408c2ecf20Sopenharmony_ci
17418c2ecf20Sopenharmony_ci		vconfig[PCI_INTERRUPT_PIN] = 0; /* Gratuitous for good VFs */
17428c2ecf20Sopenharmony_ci	}
17438c2ecf20Sopenharmony_ci	if (pdev->no_command_memory) {
17448c2ecf20Sopenharmony_ci		/*
17458c2ecf20Sopenharmony_ci		 * VFs and devices that set pdev->no_command_memory do not
17468c2ecf20Sopenharmony_ci		 * implement the memory enable bit of the COMMAND register
17478c2ecf20Sopenharmony_ci		 * therefore we'll not have it set in our initial copy of
17488c2ecf20Sopenharmony_ci		 * config space after pci_enable_device().  For consistency
17498c2ecf20Sopenharmony_ci		 * with PFs, set the virtual enable bit here.
17508c2ecf20Sopenharmony_ci		 */
17518c2ecf20Sopenharmony_ci		*(__le16 *)&vconfig[PCI_COMMAND] |=
17528c2ecf20Sopenharmony_ci					cpu_to_le16(PCI_COMMAND_MEMORY);
17538c2ecf20Sopenharmony_ci	}
17548c2ecf20Sopenharmony_ci
17558c2ecf20Sopenharmony_ci	if (!IS_ENABLED(CONFIG_VFIO_PCI_INTX) || vdev->nointx)
17568c2ecf20Sopenharmony_ci		vconfig[PCI_INTERRUPT_PIN] = 0;
17578c2ecf20Sopenharmony_ci
17588c2ecf20Sopenharmony_ci	ret = vfio_cap_init(vdev);
17598c2ecf20Sopenharmony_ci	if (ret)
17608c2ecf20Sopenharmony_ci		goto out;
17618c2ecf20Sopenharmony_ci
17628c2ecf20Sopenharmony_ci	ret = vfio_ecap_init(vdev);
17638c2ecf20Sopenharmony_ci	if (ret)
17648c2ecf20Sopenharmony_ci		goto out;
17658c2ecf20Sopenharmony_ci
17668c2ecf20Sopenharmony_ci	return 0;
17678c2ecf20Sopenharmony_ci
17688c2ecf20Sopenharmony_ciout:
17698c2ecf20Sopenharmony_ci	kfree(map);
17708c2ecf20Sopenharmony_ci	vdev->pci_config_map = NULL;
17718c2ecf20Sopenharmony_ci	kfree(vconfig);
17728c2ecf20Sopenharmony_ci	vdev->vconfig = NULL;
17738c2ecf20Sopenharmony_ci	return pcibios_err_to_errno(ret);
17748c2ecf20Sopenharmony_ci}
17758c2ecf20Sopenharmony_ci
17768c2ecf20Sopenharmony_civoid vfio_config_free(struct vfio_pci_device *vdev)
17778c2ecf20Sopenharmony_ci{
17788c2ecf20Sopenharmony_ci	kfree(vdev->vconfig);
17798c2ecf20Sopenharmony_ci	vdev->vconfig = NULL;
17808c2ecf20Sopenharmony_ci	kfree(vdev->pci_config_map);
17818c2ecf20Sopenharmony_ci	vdev->pci_config_map = NULL;
17828c2ecf20Sopenharmony_ci	if (vdev->msi_perm) {
17838c2ecf20Sopenharmony_ci		free_perm_bits(vdev->msi_perm);
17848c2ecf20Sopenharmony_ci		kfree(vdev->msi_perm);
17858c2ecf20Sopenharmony_ci		vdev->msi_perm = NULL;
17868c2ecf20Sopenharmony_ci	}
17878c2ecf20Sopenharmony_ci}
17888c2ecf20Sopenharmony_ci
17898c2ecf20Sopenharmony_ci/*
17908c2ecf20Sopenharmony_ci * Find the remaining number of bytes in a dword that match the given
17918c2ecf20Sopenharmony_ci * position.  Stop at either the end of the capability or the dword boundary.
17928c2ecf20Sopenharmony_ci */
17938c2ecf20Sopenharmony_cistatic size_t vfio_pci_cap_remaining_dword(struct vfio_pci_device *vdev,
17948c2ecf20Sopenharmony_ci					   loff_t pos)
17958c2ecf20Sopenharmony_ci{
17968c2ecf20Sopenharmony_ci	u8 cap = vdev->pci_config_map[pos];
17978c2ecf20Sopenharmony_ci	size_t i;
17988c2ecf20Sopenharmony_ci
17998c2ecf20Sopenharmony_ci	for (i = 1; (pos + i) % 4 && vdev->pci_config_map[pos + i] == cap; i++)
18008c2ecf20Sopenharmony_ci		/* nop */;
18018c2ecf20Sopenharmony_ci
18028c2ecf20Sopenharmony_ci	return i;
18038c2ecf20Sopenharmony_ci}
18048c2ecf20Sopenharmony_ci
18058c2ecf20Sopenharmony_cistatic ssize_t vfio_config_do_rw(struct vfio_pci_device *vdev, char __user *buf,
18068c2ecf20Sopenharmony_ci				 size_t count, loff_t *ppos, bool iswrite)
18078c2ecf20Sopenharmony_ci{
18088c2ecf20Sopenharmony_ci	struct pci_dev *pdev = vdev->pdev;
18098c2ecf20Sopenharmony_ci	struct perm_bits *perm;
18108c2ecf20Sopenharmony_ci	__le32 val = 0;
18118c2ecf20Sopenharmony_ci	int cap_start = 0, offset;
18128c2ecf20Sopenharmony_ci	u8 cap_id;
18138c2ecf20Sopenharmony_ci	ssize_t ret;
18148c2ecf20Sopenharmony_ci
18158c2ecf20Sopenharmony_ci	if (*ppos < 0 || *ppos >= pdev->cfg_size ||
18168c2ecf20Sopenharmony_ci	    *ppos + count > pdev->cfg_size)
18178c2ecf20Sopenharmony_ci		return -EFAULT;
18188c2ecf20Sopenharmony_ci
18198c2ecf20Sopenharmony_ci	/*
18208c2ecf20Sopenharmony_ci	 * Chop accesses into aligned chunks containing no more than a
18218c2ecf20Sopenharmony_ci	 * single capability.  Caller increments to the next chunk.
18228c2ecf20Sopenharmony_ci	 */
18238c2ecf20Sopenharmony_ci	count = min(count, vfio_pci_cap_remaining_dword(vdev, *ppos));
18248c2ecf20Sopenharmony_ci	if (count >= 4 && !(*ppos % 4))
18258c2ecf20Sopenharmony_ci		count = 4;
18268c2ecf20Sopenharmony_ci	else if (count >= 2 && !(*ppos % 2))
18278c2ecf20Sopenharmony_ci		count = 2;
18288c2ecf20Sopenharmony_ci	else
18298c2ecf20Sopenharmony_ci		count = 1;
18308c2ecf20Sopenharmony_ci
18318c2ecf20Sopenharmony_ci	ret = count;
18328c2ecf20Sopenharmony_ci
18338c2ecf20Sopenharmony_ci	cap_id = vdev->pci_config_map[*ppos];
18348c2ecf20Sopenharmony_ci
18358c2ecf20Sopenharmony_ci	if (cap_id == PCI_CAP_ID_INVALID) {
18368c2ecf20Sopenharmony_ci		perm = &unassigned_perms;
18378c2ecf20Sopenharmony_ci		cap_start = *ppos;
18388c2ecf20Sopenharmony_ci	} else if (cap_id == PCI_CAP_ID_INVALID_VIRT) {
18398c2ecf20Sopenharmony_ci		perm = &virt_perms;
18408c2ecf20Sopenharmony_ci		cap_start = *ppos;
18418c2ecf20Sopenharmony_ci	} else {
18428c2ecf20Sopenharmony_ci		if (*ppos >= PCI_CFG_SPACE_SIZE) {
18438c2ecf20Sopenharmony_ci			WARN_ON(cap_id > PCI_EXT_CAP_ID_MAX);
18448c2ecf20Sopenharmony_ci
18458c2ecf20Sopenharmony_ci			perm = &ecap_perms[cap_id];
18468c2ecf20Sopenharmony_ci			cap_start = vfio_find_cap_start(vdev, *ppos);
18478c2ecf20Sopenharmony_ci		} else {
18488c2ecf20Sopenharmony_ci			WARN_ON(cap_id > PCI_CAP_ID_MAX);
18498c2ecf20Sopenharmony_ci
18508c2ecf20Sopenharmony_ci			perm = &cap_perms[cap_id];
18518c2ecf20Sopenharmony_ci
18528c2ecf20Sopenharmony_ci			if (cap_id == PCI_CAP_ID_MSI)
18538c2ecf20Sopenharmony_ci				perm = vdev->msi_perm;
18548c2ecf20Sopenharmony_ci
18558c2ecf20Sopenharmony_ci			if (cap_id > PCI_CAP_ID_BASIC)
18568c2ecf20Sopenharmony_ci				cap_start = vfio_find_cap_start(vdev, *ppos);
18578c2ecf20Sopenharmony_ci		}
18588c2ecf20Sopenharmony_ci	}
18598c2ecf20Sopenharmony_ci
18608c2ecf20Sopenharmony_ci	WARN_ON(!cap_start && cap_id != PCI_CAP_ID_BASIC);
18618c2ecf20Sopenharmony_ci	WARN_ON(cap_start > *ppos);
18628c2ecf20Sopenharmony_ci
18638c2ecf20Sopenharmony_ci	offset = *ppos - cap_start;
18648c2ecf20Sopenharmony_ci
18658c2ecf20Sopenharmony_ci	if (iswrite) {
18668c2ecf20Sopenharmony_ci		if (!perm->writefn)
18678c2ecf20Sopenharmony_ci			return ret;
18688c2ecf20Sopenharmony_ci
18698c2ecf20Sopenharmony_ci		if (copy_from_user(&val, buf, count))
18708c2ecf20Sopenharmony_ci			return -EFAULT;
18718c2ecf20Sopenharmony_ci
18728c2ecf20Sopenharmony_ci		ret = perm->writefn(vdev, *ppos, count, perm, offset, val);
18738c2ecf20Sopenharmony_ci	} else {
18748c2ecf20Sopenharmony_ci		if (perm->readfn) {
18758c2ecf20Sopenharmony_ci			ret = perm->readfn(vdev, *ppos, count,
18768c2ecf20Sopenharmony_ci					   perm, offset, &val);
18778c2ecf20Sopenharmony_ci			if (ret < 0)
18788c2ecf20Sopenharmony_ci				return ret;
18798c2ecf20Sopenharmony_ci		}
18808c2ecf20Sopenharmony_ci
18818c2ecf20Sopenharmony_ci		if (copy_to_user(buf, &val, count))
18828c2ecf20Sopenharmony_ci			return -EFAULT;
18838c2ecf20Sopenharmony_ci	}
18848c2ecf20Sopenharmony_ci
18858c2ecf20Sopenharmony_ci	return ret;
18868c2ecf20Sopenharmony_ci}
18878c2ecf20Sopenharmony_ci
18888c2ecf20Sopenharmony_cissize_t vfio_pci_config_rw(struct vfio_pci_device *vdev, char __user *buf,
18898c2ecf20Sopenharmony_ci			   size_t count, loff_t *ppos, bool iswrite)
18908c2ecf20Sopenharmony_ci{
18918c2ecf20Sopenharmony_ci	size_t done = 0;
18928c2ecf20Sopenharmony_ci	int ret = 0;
18938c2ecf20Sopenharmony_ci	loff_t pos = *ppos;
18948c2ecf20Sopenharmony_ci
18958c2ecf20Sopenharmony_ci	pos &= VFIO_PCI_OFFSET_MASK;
18968c2ecf20Sopenharmony_ci
18978c2ecf20Sopenharmony_ci	while (count) {
18988c2ecf20Sopenharmony_ci		ret = vfio_config_do_rw(vdev, buf, count, &pos, iswrite);
18998c2ecf20Sopenharmony_ci		if (ret < 0)
19008c2ecf20Sopenharmony_ci			return ret;
19018c2ecf20Sopenharmony_ci
19028c2ecf20Sopenharmony_ci		count -= ret;
19038c2ecf20Sopenharmony_ci		done += ret;
19048c2ecf20Sopenharmony_ci		buf += ret;
19058c2ecf20Sopenharmony_ci		pos += ret;
19068c2ecf20Sopenharmony_ci	}
19078c2ecf20Sopenharmony_ci
19088c2ecf20Sopenharmony_ci	*ppos += done;
19098c2ecf20Sopenharmony_ci
19108c2ecf20Sopenharmony_ci	return done;
19118c2ecf20Sopenharmony_ci}
1912