s390_pci_hpc.c 3.1 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0+
2 3 4 5 6 7 8 9 10
/*
 * PCI Hot Plug Controller Driver for System z
 *
 * Copyright 2012 IBM Corp.
 *
 * Author(s):
 *   Jan Glauber <jang@linux.vnet.ibm.com>
 */

G
Gerald Schaefer 已提交
11 12
#define KMSG_COMPONENT "zpci"
#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
13 14 15 16 17

#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/pci.h>
#include <linux/pci_hotplug.h>
S
Sebastian Ott 已提交
18
#include <asm/pci_debug.h>
19 20 21 22 23 24 25 26 27 28
#include <asm/sclp.h>

#define SLOT_NAME_SIZE	10

static int zpci_fn_configured(enum zpci_state state)
{
	return state == ZPCI_FN_STATE_CONFIGURED ||
	       state == ZPCI_FN_STATE_ONLINE;
}

29
static inline int zdev_configure(struct zpci_dev *zdev)
L
Lukas Wunner 已提交
30
{
31
	int ret = sclp_pci_configure(zdev->fid);
S
Sebastian Ott 已提交
32

33
	zpci_dbg(3, "conf fid:%x, rc:%d\n", zdev->fid, ret);
S
Sebastian Ott 已提交
34
	if (!ret)
35
		zdev->state = ZPCI_FN_STATE_CONFIGURED;
S
Sebastian Ott 已提交
36 37 38 39

	return ret;
}

40
static inline int zdev_deconfigure(struct zpci_dev *zdev)
S
Sebastian Ott 已提交
41
{
42
	int ret = sclp_pci_deconfigure(zdev->fid);
S
Sebastian Ott 已提交
43

44
	zpci_dbg(3, "deconf fid:%x, rc:%d\n", zdev->fid, ret);
S
Sebastian Ott 已提交
45
	if (!ret)
46
		zdev->state = ZPCI_FN_STATE_STANDBY;
S
Sebastian Ott 已提交
47 48 49 50

	return ret;
}

51 52
static int enable_slot(struct hotplug_slot *hotplug_slot)
{
53 54
	struct zpci_dev *zdev = container_of(hotplug_slot, struct zpci_dev,
					     hotplug_slot);
P
Pierre Morel 已提交
55
	struct zpci_bus *zbus = zdev->zbus;
56 57
	int rc;

58
	if (zdev->state != ZPCI_FN_STATE_STANDBY)
59 60
		return -EIO;

61
	rc = zdev_configure(zdev);
S
Sebastian Ott 已提交
62 63 64
	if (rc)
		return rc;

65
	rc = zpci_enable_device(zdev);
S
Sebastian Ott 已提交
66 67 68
	if (rc)
		goto out_deconfigure;

P
Pierre Morel 已提交
69
	pci_scan_slot(zbus->bus, zdev->devfn);
70
	pci_lock_rescan_remove();
P
Pierre Morel 已提交
71
	pci_bus_add_devices(zbus->bus);
72
	pci_unlock_rescan_remove();
S
Sebastian Ott 已提交
73 74 75 76

	return rc;

out_deconfigure:
77
	zdev_deconfigure(zdev);
78 79 80 81 82
	return rc;
}

static int disable_slot(struct hotplug_slot *hotplug_slot)
{
83 84
	struct zpci_dev *zdev = container_of(hotplug_slot, struct zpci_dev,
					     hotplug_slot);
85
	struct pci_dev *pdev;
86 87
	int rc;

88
	if (!zpci_fn_configured(zdev->state))
89 90
		return -EIO;

91 92
	pdev = pci_get_slot(zdev->zbus->bus, zdev->devfn);
	if (pdev && pci_num_vf(pdev)) {
93
		pci_dev_put(pdev);
94
		return -EBUSY;
95
	}
96

97 98
	zpci_remove_device(zdev);

99
	rc = zpci_disable_device(zdev);
100 101
	if (rc)
		return rc;
102

103
	return zdev_deconfigure(zdev);
104 105 106 107
}

static int get_power_status(struct hotplug_slot *hotplug_slot, u8 *value)
{
108 109
	struct zpci_dev *zdev = container_of(hotplug_slot, struct zpci_dev,
					     hotplug_slot);
110

111
	switch (zdev->state) {
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
	case ZPCI_FN_STATE_STANDBY:
		*value = 0;
		break;
	default:
		*value = 1;
		break;
	}
	return 0;
}

static int get_adapter_status(struct hotplug_slot *hotplug_slot, u8 *value)
{
	/* if the slot exits it always contains a function */
	*value = 1;
	return 0;
}

129
static const struct hotplug_slot_ops s390_hotplug_slot_ops = {
130 131 132 133 134 135
	.enable_slot =		enable_slot,
	.disable_slot =		disable_slot,
	.get_power_status =	get_power_status,
	.get_adapter_status =	get_adapter_status,
};

136
int zpci_init_slot(struct zpci_dev *zdev)
137 138
{
	char name[SLOT_NAME_SIZE];
P
Pierre Morel 已提交
139
	struct zpci_bus *zbus = zdev->zbus;
140

141
	zdev->hotplug_slot.ops = &s390_hotplug_slot_ops;
142 143

	snprintf(name, SLOT_NAME_SIZE, "%08x", zdev->fid);
P
Pierre Morel 已提交
144
	return pci_hp_register(&zdev->hotplug_slot, zbus->bus,
P
Pierre Morel 已提交
145
			       zdev->devfn, name);
146 147
}

148
void zpci_exit_slot(struct zpci_dev *zdev)
149
{
150
	pci_hp_deregister(&zdev->hotplug_slot);
151
}