xref: /linux/drivers/pci/hotplug/s390_pci_hpc.c (revision 3a39d672e7f48b8d6b91a09afa4b55352773b4b5)
136b07274SBjorn Helgaas // SPDX-License-Identifier: GPL-2.0+
27441b062SJan Glauber /*
37441b062SJan Glauber  * PCI Hot Plug Controller Driver for System z
47441b062SJan Glauber  *
57441b062SJan Glauber  * Copyright 2012 IBM Corp.
67441b062SJan Glauber  *
77441b062SJan Glauber  * Author(s):
87441b062SJan Glauber  *   Jan Glauber <jang@linux.vnet.ibm.com>
97441b062SJan Glauber  */
107441b062SJan Glauber 
11896cb7e6SGerald Schaefer #define KMSG_COMPONENT "zpci"
12896cb7e6SGerald Schaefer #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
137441b062SJan Glauber 
147441b062SJan Glauber #include <linux/kernel.h>
157441b062SJan Glauber #include <linux/slab.h>
167441b062SJan Glauber #include <linux/pci.h>
177441b062SJan Glauber #include <linux/pci_hotplug.h>
18a2ab8333SSebastian Ott #include <asm/pci_debug.h>
197441b062SJan Glauber #include <asm/sclp.h>
207441b062SJan Glauber 
217441b062SJan Glauber #define SLOT_NAME_SIZE	10
227441b062SJan Glauber 
enable_slot(struct hotplug_slot * hotplug_slot)237441b062SJan Glauber static int enable_slot(struct hotplug_slot *hotplug_slot)
247441b062SJan Glauber {
25035f212fSPierre Morel 	struct zpci_dev *zdev = container_of(hotplug_slot, struct zpci_dev,
26035f212fSPierre Morel 					     hotplug_slot);
2761311e32SNiklas Schnelle 	int rc;
287441b062SJan Glauber 
29bcb5d6c7SGerd Bayer 	mutex_lock(&zdev->state_lock);
30bcb5d6c7SGerd Bayer 	if (zdev->state != ZPCI_FN_STATE_STANDBY) {
31bcb5d6c7SGerd Bayer 		rc = -EIO;
32bcb5d6c7SGerd Bayer 		goto out;
33bcb5d6c7SGerd Bayer 	}
347441b062SJan Glauber 
3561311e32SNiklas Schnelle 	rc = sclp_pci_configure(zdev->fid);
3661311e32SNiklas Schnelle 	zpci_dbg(3, "conf fid:%x, rc:%d\n", zdev->fid, rc);
3761311e32SNiklas Schnelle 	if (rc)
38bcb5d6c7SGerd Bayer 		goto out;
3961311e32SNiklas Schnelle 	zdev->state = ZPCI_FN_STATE_CONFIGURED;
4061311e32SNiklas Schnelle 
41bcb5d6c7SGerd Bayer 	rc = zpci_scan_configured_device(zdev, zdev->fh);
42bcb5d6c7SGerd Bayer out:
43bcb5d6c7SGerd Bayer 	mutex_unlock(&zdev->state_lock);
44bcb5d6c7SGerd Bayer 	return rc;
457441b062SJan Glauber }
467441b062SJan Glauber 
disable_slot(struct hotplug_slot * hotplug_slot)477441b062SJan Glauber static int disable_slot(struct hotplug_slot *hotplug_slot)
487441b062SJan Glauber {
49035f212fSPierre Morel 	struct zpci_dev *zdev = container_of(hotplug_slot, struct zpci_dev,
50035f212fSPierre Morel 					     hotplug_slot);
51bcb5d6c7SGerd Bayer 	struct pci_dev *pdev = NULL;
52bcb5d6c7SGerd Bayer 	int rc;
537441b062SJan Glauber 
54bcb5d6c7SGerd Bayer 	mutex_lock(&zdev->state_lock);
55bcb5d6c7SGerd Bayer 	if (zdev->state != ZPCI_FN_STATE_CONFIGURED) {
56bcb5d6c7SGerd Bayer 		rc = -EIO;
57bcb5d6c7SGerd Bayer 		goto out;
58bcb5d6c7SGerd Bayer 	}
597441b062SJan Glauber 
602f0230b2SNiklas Schnelle 	pdev = pci_get_slot(zdev->zbus->bus, zdev->devfn);
612f0230b2SNiklas Schnelle 	if (pdev && pci_num_vf(pdev)) {
629a99649fSSebastian Ott 		pci_dev_put(pdev);
63bcb5d6c7SGerd Bayer 		rc = -EBUSY;
64bcb5d6c7SGerd Bayer 		goto out;
659a99649fSSebastian Ott 	}
668b2a7e60SSebastian Ott 
67bcb5d6c7SGerd Bayer 	rc = zpci_deconfigure_device(zdev);
68bcb5d6c7SGerd Bayer out:
69bcb5d6c7SGerd Bayer 	mutex_unlock(&zdev->state_lock);
70bcb5d6c7SGerd Bayer 	if (pdev)
71bcb5d6c7SGerd Bayer 		pci_dev_put(pdev);
72bcb5d6c7SGerd Bayer 	return rc;
737441b062SJan Glauber }
747441b062SJan Glauber 
reset_slot(struct hotplug_slot * hotplug_slot,bool probe)75da995d53SNiklas Schnelle static int reset_slot(struct hotplug_slot *hotplug_slot, bool probe)
76da995d53SNiklas Schnelle {
77da995d53SNiklas Schnelle 	struct zpci_dev *zdev = container_of(hotplug_slot, struct zpci_dev,
78da995d53SNiklas Schnelle 					     hotplug_slot);
79bcb5d6c7SGerd Bayer 	int rc = -EIO;
80da995d53SNiklas Schnelle 
81da995d53SNiklas Schnelle 	/*
82bcb5d6c7SGerd Bayer 	 * If we can't get the zdev->state_lock the device state is
83bcb5d6c7SGerd Bayer 	 * currently undergoing a transition and we bail out - just
84bcb5d6c7SGerd Bayer 	 * the same as if the device's state is not configured at all.
85da995d53SNiklas Schnelle 	 */
86bcb5d6c7SGerd Bayer 	if (!mutex_trylock(&zdev->state_lock))
87bcb5d6c7SGerd Bayer 		return rc;
88da995d53SNiklas Schnelle 
89bcb5d6c7SGerd Bayer 	/* We can reset only if the function is configured */
90bcb5d6c7SGerd Bayer 	if (zdev->state != ZPCI_FN_STATE_CONFIGURED)
91bcb5d6c7SGerd Bayer 		goto out;
92da995d53SNiklas Schnelle 
93bcb5d6c7SGerd Bayer 	if (probe) {
94bcb5d6c7SGerd Bayer 		rc = 0;
95bcb5d6c7SGerd Bayer 		goto out;
96bcb5d6c7SGerd Bayer 	}
97bcb5d6c7SGerd Bayer 
98bcb5d6c7SGerd Bayer 	rc = zpci_hot_reset_device(zdev);
99bcb5d6c7SGerd Bayer out:
100bcb5d6c7SGerd Bayer 	mutex_unlock(&zdev->state_lock);
101bcb5d6c7SGerd Bayer 	return rc;
102da995d53SNiklas Schnelle }
103da995d53SNiklas Schnelle 
get_power_status(struct hotplug_slot * hotplug_slot,u8 * value)1047441b062SJan Glauber static int get_power_status(struct hotplug_slot *hotplug_slot, u8 *value)
1057441b062SJan Glauber {
106035f212fSPierre Morel 	struct zpci_dev *zdev = container_of(hotplug_slot, struct zpci_dev,
107035f212fSPierre Morel 					     hotplug_slot);
1087441b062SJan Glauber 
109a46044a9SNiklas Schnelle 	*value = zpci_is_device_configured(zdev) ? 1 : 0;
1107441b062SJan Glauber 	return 0;
1117441b062SJan Glauber }
1127441b062SJan Glauber 
get_adapter_status(struct hotplug_slot * hotplug_slot,u8 * value)1137441b062SJan Glauber static int get_adapter_status(struct hotplug_slot *hotplug_slot, u8 *value)
1147441b062SJan Glauber {
115*5c7bdac7SBjorn Helgaas 	/* if the slot exists it always contains a function */
1167441b062SJan Glauber 	*value = 1;
1177441b062SJan Glauber 	return 0;
1187441b062SJan Glauber }
1197441b062SJan Glauber 
12081c4b5bfSLukas Wunner static const struct hotplug_slot_ops s390_hotplug_slot_ops = {
1217441b062SJan Glauber 	.enable_slot =		enable_slot,
1227441b062SJan Glauber 	.disable_slot =		disable_slot,
123da995d53SNiklas Schnelle 	.reset_slot =		reset_slot,
1247441b062SJan Glauber 	.get_power_status =	get_power_status,
1257441b062SJan Glauber 	.get_adapter_status =	get_adapter_status,
1267441b062SJan Glauber };
1277441b062SJan Glauber 
zpci_init_slot(struct zpci_dev * zdev)12867f43f38SSebastian Ott int zpci_init_slot(struct zpci_dev *zdev)
1297441b062SJan Glauber {
1307441b062SJan Glauber 	char name[SLOT_NAME_SIZE];
13105bc1be6SPierre Morel 	struct zpci_bus *zbus = zdev->zbus;
1327441b062SJan Glauber 
133035f212fSPierre Morel 	zdev->hotplug_slot.ops = &s390_hotplug_slot_ops;
1347441b062SJan Glauber 
1357441b062SJan Glauber 	snprintf(name, SLOT_NAME_SIZE, "%08x", zdev->fid);
13605bc1be6SPierre Morel 	return pci_hp_register(&zdev->hotplug_slot, zbus->bus,
13744510d6fSPierre Morel 			       zdev->devfn, name);
1387441b062SJan Glauber }
1397441b062SJan Glauber 
zpci_exit_slot(struct zpci_dev * zdev)14067f43f38SSebastian Ott void zpci_exit_slot(struct zpci_dev *zdev)
1417441b062SJan Glauber {
142035f212fSPierre Morel 	pci_hp_deregister(&zdev->hotplug_slot);
1437441b062SJan Glauber }
144