18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright IBM Corp. 2012
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Author(s):
68c2ecf20Sopenharmony_ci *   Jan Glauber <jang@linux.vnet.ibm.com>
78c2ecf20Sopenharmony_ci */
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#define KMSG_COMPONENT "zpci"
108c2ecf20Sopenharmony_ci#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci#include <linux/kernel.h>
138c2ecf20Sopenharmony_ci#include <linux/stat.h>
148c2ecf20Sopenharmony_ci#include <linux/pci.h>
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci#include "../../../drivers/pci/pci.h"
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci#include <asm/sclp.h>
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci#define zpci_attr(name, fmt, member)					\
218c2ecf20Sopenharmony_cistatic ssize_t name##_show(struct device *dev,				\
228c2ecf20Sopenharmony_ci			   struct device_attribute *attr, char *buf)	\
238c2ecf20Sopenharmony_ci{									\
248c2ecf20Sopenharmony_ci	struct zpci_dev *zdev = to_zpci(to_pci_dev(dev));		\
258c2ecf20Sopenharmony_ci									\
268c2ecf20Sopenharmony_ci	return sprintf(buf, fmt, zdev->member);				\
278c2ecf20Sopenharmony_ci}									\
288c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RO(name)
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_cizpci_attr(function_id, "0x%08x\n", fid);
318c2ecf20Sopenharmony_cizpci_attr(function_handle, "0x%08x\n", fh);
328c2ecf20Sopenharmony_cizpci_attr(pchid, "0x%04x\n", pchid);
338c2ecf20Sopenharmony_cizpci_attr(pfgid, "0x%02x\n", pfgid);
348c2ecf20Sopenharmony_cizpci_attr(vfn, "0x%04x\n", vfn);
358c2ecf20Sopenharmony_cizpci_attr(pft, "0x%02x\n", pft);
368c2ecf20Sopenharmony_cizpci_attr(port, "%d\n", port);
378c2ecf20Sopenharmony_cizpci_attr(uid, "0x%x\n", uid);
388c2ecf20Sopenharmony_cizpci_attr(segment0, "0x%02x\n", pfip[0]);
398c2ecf20Sopenharmony_cizpci_attr(segment1, "0x%02x\n", pfip[1]);
408c2ecf20Sopenharmony_cizpci_attr(segment2, "0x%02x\n", pfip[2]);
418c2ecf20Sopenharmony_cizpci_attr(segment3, "0x%02x\n", pfip[3]);
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_cistatic ssize_t mio_enabled_show(struct device *dev,
448c2ecf20Sopenharmony_ci				struct device_attribute *attr, char *buf)
458c2ecf20Sopenharmony_ci{
468c2ecf20Sopenharmony_ci	struct zpci_dev *zdev = to_zpci(to_pci_dev(dev));
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci	return sprintf(buf, zpci_use_mio(zdev) ? "1\n" : "0\n");
498c2ecf20Sopenharmony_ci}
508c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RO(mio_enabled);
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_cistatic ssize_t recover_store(struct device *dev, struct device_attribute *attr,
538c2ecf20Sopenharmony_ci			     const char *buf, size_t count)
548c2ecf20Sopenharmony_ci{
558c2ecf20Sopenharmony_ci	struct kernfs_node *kn;
568c2ecf20Sopenharmony_ci	struct pci_dev *pdev = to_pci_dev(dev);
578c2ecf20Sopenharmony_ci	struct zpci_dev *zdev = to_zpci(pdev);
588c2ecf20Sopenharmony_ci	int ret = 0;
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci	/* Can't use device_remove_self() here as that would lead us to lock
618c2ecf20Sopenharmony_ci	 * the pci_rescan_remove_lock while holding the device' kernfs lock.
628c2ecf20Sopenharmony_ci	 * This would create a possible deadlock with disable_slot() which is
638c2ecf20Sopenharmony_ci	 * not directly protected by the device' kernfs lock but takes it
648c2ecf20Sopenharmony_ci	 * during the device removal which happens under
658c2ecf20Sopenharmony_ci	 * pci_rescan_remove_lock.
668c2ecf20Sopenharmony_ci	 *
678c2ecf20Sopenharmony_ci	 * This is analogous to sdev_store_delete() in
688c2ecf20Sopenharmony_ci	 * drivers/scsi/scsi_sysfs.c
698c2ecf20Sopenharmony_ci	 */
708c2ecf20Sopenharmony_ci	kn = sysfs_break_active_protection(&dev->kobj, &attr->attr);
718c2ecf20Sopenharmony_ci	WARN_ON_ONCE(!kn);
728c2ecf20Sopenharmony_ci	/* device_remove_file() serializes concurrent calls ignoring all but
738c2ecf20Sopenharmony_ci	 * the first
748c2ecf20Sopenharmony_ci	 */
758c2ecf20Sopenharmony_ci	device_remove_file(dev, attr);
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci	/* A concurrent call to recover_store() may slip between
788c2ecf20Sopenharmony_ci	 * sysfs_break_active_protection() and the sysfs file removal.
798c2ecf20Sopenharmony_ci	 * Once it unblocks from pci_lock_rescan_remove() the original pdev
808c2ecf20Sopenharmony_ci	 * will already be removed.
818c2ecf20Sopenharmony_ci	 */
828c2ecf20Sopenharmony_ci	pci_lock_rescan_remove();
838c2ecf20Sopenharmony_ci	if (pci_dev_is_added(pdev)) {
848c2ecf20Sopenharmony_ci		pci_stop_and_remove_bus_device(pdev);
858c2ecf20Sopenharmony_ci		ret = zpci_disable_device(zdev);
868c2ecf20Sopenharmony_ci		if (ret)
878c2ecf20Sopenharmony_ci			goto out;
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci		ret = zpci_enable_device(zdev);
908c2ecf20Sopenharmony_ci		if (ret)
918c2ecf20Sopenharmony_ci			goto out;
928c2ecf20Sopenharmony_ci		pci_rescan_bus(zdev->zbus->bus);
938c2ecf20Sopenharmony_ci	}
948c2ecf20Sopenharmony_ciout:
958c2ecf20Sopenharmony_ci	pci_unlock_rescan_remove();
968c2ecf20Sopenharmony_ci	if (kn)
978c2ecf20Sopenharmony_ci		sysfs_unbreak_active_protection(kn);
988c2ecf20Sopenharmony_ci	return ret ? ret : count;
998c2ecf20Sopenharmony_ci}
1008c2ecf20Sopenharmony_cistatic DEVICE_ATTR_WO(recover);
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_cistatic ssize_t util_string_read(struct file *filp, struct kobject *kobj,
1038c2ecf20Sopenharmony_ci				struct bin_attribute *attr, char *buf,
1048c2ecf20Sopenharmony_ci				loff_t off, size_t count)
1058c2ecf20Sopenharmony_ci{
1068c2ecf20Sopenharmony_ci	struct device *dev = kobj_to_dev(kobj);
1078c2ecf20Sopenharmony_ci	struct pci_dev *pdev = to_pci_dev(dev);
1088c2ecf20Sopenharmony_ci	struct zpci_dev *zdev = to_zpci(pdev);
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci	return memory_read_from_buffer(buf, count, &off, zdev->util_str,
1118c2ecf20Sopenharmony_ci				       sizeof(zdev->util_str));
1128c2ecf20Sopenharmony_ci}
1138c2ecf20Sopenharmony_cistatic BIN_ATTR_RO(util_string, CLP_UTIL_STR_LEN);
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_cistatic ssize_t report_error_write(struct file *filp, struct kobject *kobj,
1168c2ecf20Sopenharmony_ci				  struct bin_attribute *attr, char *buf,
1178c2ecf20Sopenharmony_ci				  loff_t off, size_t count)
1188c2ecf20Sopenharmony_ci{
1198c2ecf20Sopenharmony_ci	struct zpci_report_error_header *report = (void *) buf;
1208c2ecf20Sopenharmony_ci	struct device *dev = kobj_to_dev(kobj);
1218c2ecf20Sopenharmony_ci	struct pci_dev *pdev = to_pci_dev(dev);
1228c2ecf20Sopenharmony_ci	struct zpci_dev *zdev = to_zpci(pdev);
1238c2ecf20Sopenharmony_ci	int ret;
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci	if (off || (count < sizeof(*report)))
1268c2ecf20Sopenharmony_ci		return -EINVAL;
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci	ret = sclp_pci_report(report, zdev->fh, zdev->fid);
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ci	return ret ? ret : count;
1318c2ecf20Sopenharmony_ci}
1328c2ecf20Sopenharmony_cistatic BIN_ATTR(report_error, S_IWUSR, NULL, report_error_write, PAGE_SIZE);
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_cistatic struct bin_attribute *zpci_bin_attrs[] = {
1358c2ecf20Sopenharmony_ci	&bin_attr_util_string,
1368c2ecf20Sopenharmony_ci	&bin_attr_report_error,
1378c2ecf20Sopenharmony_ci	NULL,
1388c2ecf20Sopenharmony_ci};
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_cistatic struct attribute *zpci_dev_attrs[] = {
1418c2ecf20Sopenharmony_ci	&dev_attr_function_id.attr,
1428c2ecf20Sopenharmony_ci	&dev_attr_function_handle.attr,
1438c2ecf20Sopenharmony_ci	&dev_attr_pchid.attr,
1448c2ecf20Sopenharmony_ci	&dev_attr_pfgid.attr,
1458c2ecf20Sopenharmony_ci	&dev_attr_pft.attr,
1468c2ecf20Sopenharmony_ci	&dev_attr_port.attr,
1478c2ecf20Sopenharmony_ci	&dev_attr_vfn.attr,
1488c2ecf20Sopenharmony_ci	&dev_attr_uid.attr,
1498c2ecf20Sopenharmony_ci	&dev_attr_recover.attr,
1508c2ecf20Sopenharmony_ci	&dev_attr_mio_enabled.attr,
1518c2ecf20Sopenharmony_ci	NULL,
1528c2ecf20Sopenharmony_ci};
1538c2ecf20Sopenharmony_cistatic struct attribute_group zpci_attr_group = {
1548c2ecf20Sopenharmony_ci	.attrs = zpci_dev_attrs,
1558c2ecf20Sopenharmony_ci	.bin_attrs = zpci_bin_attrs,
1568c2ecf20Sopenharmony_ci};
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_cistatic struct attribute *pfip_attrs[] = {
1598c2ecf20Sopenharmony_ci	&dev_attr_segment0.attr,
1608c2ecf20Sopenharmony_ci	&dev_attr_segment1.attr,
1618c2ecf20Sopenharmony_ci	&dev_attr_segment2.attr,
1628c2ecf20Sopenharmony_ci	&dev_attr_segment3.attr,
1638c2ecf20Sopenharmony_ci	NULL,
1648c2ecf20Sopenharmony_ci};
1658c2ecf20Sopenharmony_cistatic struct attribute_group pfip_attr_group = {
1668c2ecf20Sopenharmony_ci	.name = "pfip",
1678c2ecf20Sopenharmony_ci	.attrs = pfip_attrs,
1688c2ecf20Sopenharmony_ci};
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_ciconst struct attribute_group *zpci_attr_groups[] = {
1718c2ecf20Sopenharmony_ci	&zpci_attr_group,
1728c2ecf20Sopenharmony_ci	&pfip_attr_group,
1738c2ecf20Sopenharmony_ci	NULL,
1748c2ecf20Sopenharmony_ci};
175