xref: /linux/drivers/pci/hotplug/s390_pci_hpc.c (revision 547c5775a742d9c83891b629b75d1d4c8e88d8c0)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * PCI Hot Plug Controller Driver for System z
4  *
5  * Copyright 2012 IBM Corp.
6  *
7  * Author(s):
8  *   Jan Glauber <jang@linux.vnet.ibm.com>
9  */
10 
11 #define KMSG_COMPONENT "zpci"
12 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
13 
14 #include <linux/kernel.h>
15 #include <linux/slab.h>
16 #include <linux/pci.h>
17 #include <linux/pci_hotplug.h>
18 #include <asm/pci_debug.h>
19 #include <asm/sclp.h>
20 
21 #define SLOT_NAME_SIZE	10
22 
enable_slot(struct hotplug_slot * hotplug_slot)23 static int enable_slot(struct hotplug_slot *hotplug_slot)
24 {
25 	struct zpci_dev *zdev = container_of(hotplug_slot, struct zpci_dev,
26 					     hotplug_slot);
27 	int rc;
28 
29 	mutex_lock(&zdev->state_lock);
30 	if (zdev->state != ZPCI_FN_STATE_STANDBY) {
31 		rc = -EIO;
32 		goto out;
33 	}
34 
35 	rc = sclp_pci_configure(zdev->fid);
36 	zpci_dbg(3, "conf fid:%x, rc:%d\n", zdev->fid, rc);
37 	if (rc)
38 		goto out;
39 	zdev->state = ZPCI_FN_STATE_CONFIGURED;
40 
41 	rc = zpci_scan_configured_device(zdev, zdev->fh);
42 out:
43 	mutex_unlock(&zdev->state_lock);
44 	return rc;
45 }
46 
disable_slot(struct hotplug_slot * hotplug_slot)47 static int disable_slot(struct hotplug_slot *hotplug_slot)
48 {
49 	struct zpci_dev *zdev = container_of(hotplug_slot, struct zpci_dev,
50 					     hotplug_slot);
51 	struct pci_dev *pdev = NULL;
52 	int rc;
53 
54 	mutex_lock(&zdev->state_lock);
55 	if (zdev->state != ZPCI_FN_STATE_CONFIGURED) {
56 		rc = -EIO;
57 		goto out;
58 	}
59 
60 	pdev = pci_get_slot(zdev->zbus->bus, zdev->devfn);
61 	if (pdev && pci_num_vf(pdev)) {
62 		rc = -EBUSY;
63 		goto out;
64 	}
65 
66 	rc = zpci_deconfigure_device(zdev);
67 out:
68 	if (pdev)
69 		pci_dev_put(pdev);
70 	mutex_unlock(&zdev->state_lock);
71 	return rc;
72 }
73 
reset_slot(struct hotplug_slot * hotplug_slot,bool probe)74 static int reset_slot(struct hotplug_slot *hotplug_slot, bool probe)
75 {
76 	struct zpci_dev *zdev = container_of(hotplug_slot, struct zpci_dev,
77 					     hotplug_slot);
78 	int rc = -EIO;
79 
80 	/*
81 	 * If we can't get the zdev->state_lock the device state is
82 	 * currently undergoing a transition and we bail out - just
83 	 * the same as if the device's state is not configured at all.
84 	 */
85 	if (!mutex_trylock(&zdev->state_lock))
86 		return rc;
87 
88 	/* We can reset only if the function is configured */
89 	if (zdev->state != ZPCI_FN_STATE_CONFIGURED)
90 		goto out;
91 
92 	if (probe) {
93 		rc = 0;
94 		goto out;
95 	}
96 
97 	rc = zpci_hot_reset_device(zdev);
98 out:
99 	mutex_unlock(&zdev->state_lock);
100 	return rc;
101 }
102 
get_power_status(struct hotplug_slot * hotplug_slot,u8 * value)103 static int get_power_status(struct hotplug_slot *hotplug_slot, u8 *value)
104 {
105 	struct zpci_dev *zdev = container_of(hotplug_slot, struct zpci_dev,
106 					     hotplug_slot);
107 
108 	*value = zpci_is_device_configured(zdev) ? 1 : 0;
109 	return 0;
110 }
111 
get_adapter_status(struct hotplug_slot * hotplug_slot,u8 * value)112 static int get_adapter_status(struct hotplug_slot *hotplug_slot, u8 *value)
113 {
114 	/* if the slot exists it always contains a function */
115 	*value = 1;
116 	return 0;
117 }
118 
119 static const struct hotplug_slot_ops s390_hotplug_slot_ops = {
120 	.enable_slot =		enable_slot,
121 	.disable_slot =		disable_slot,
122 	.reset_slot =		reset_slot,
123 	.get_power_status =	get_power_status,
124 	.get_adapter_status =	get_adapter_status,
125 };
126 
zpci_init_slot(struct zpci_dev * zdev)127 int zpci_init_slot(struct zpci_dev *zdev)
128 {
129 	char name[SLOT_NAME_SIZE];
130 	struct zpci_bus *zbus = zdev->zbus;
131 
132 	zdev->hotplug_slot.ops = &s390_hotplug_slot_ops;
133 
134 	snprintf(name, SLOT_NAME_SIZE, "%08x", zdev->fid);
135 	return pci_hp_register(&zdev->hotplug_slot, zbus->bus,
136 			       zdev->devfn, name);
137 }
138 
zpci_exit_slot(struct zpci_dev * zdev)139 void zpci_exit_slot(struct zpci_dev *zdev)
140 {
141 	pci_hp_deregister(&zdev->hotplug_slot);
142 }
143