s390_pci_hpc.c 3.7 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 29 30 31 32 33 34
#include <asm/sclp.h>

#define SLOT_NAME_SIZE	10
static LIST_HEAD(s390_hotplug_slot_list);

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

/*
 * struct slot - slot information for each *physical* slot
 */
struct slot {
	struct list_head slot_list;
L
Lukas Wunner 已提交
35
	struct hotplug_slot hotplug_slot;
36 37 38
	struct zpci_dev *zdev;
};

L
Lukas Wunner 已提交
39 40 41 42 43
static inline struct slot *to_slot(struct hotplug_slot *hotplug_slot)
{
	return container_of(hotplug_slot, struct slot, hotplug_slot);
}

S
Sebastian Ott 已提交
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
static inline int slot_configure(struct slot *slot)
{
	int ret = sclp_pci_configure(slot->zdev->fid);

	zpci_dbg(3, "conf fid:%x, rc:%d\n", slot->zdev->fid, ret);
	if (!ret)
		slot->zdev->state = ZPCI_FN_STATE_CONFIGURED;

	return ret;
}

static inline int slot_deconfigure(struct slot *slot)
{
	int ret = sclp_pci_deconfigure(slot->zdev->fid);

	zpci_dbg(3, "deconf fid:%x, rc:%d\n", slot->zdev->fid, ret);
	if (!ret)
		slot->zdev->state = ZPCI_FN_STATE_STANDBY;

	return ret;
}

66 67
static int enable_slot(struct hotplug_slot *hotplug_slot)
{
L
Lukas Wunner 已提交
68
	struct slot *slot = to_slot(hotplug_slot);
69 70 71 72 73
	int rc;

	if (slot->zdev->state != ZPCI_FN_STATE_STANDBY)
		return -EIO;

S
Sebastian Ott 已提交
74 75 76 77 78 79 80 81 82
	rc = slot_configure(slot);
	if (rc)
		return rc;

	rc = zpci_enable_device(slot->zdev);
	if (rc)
		goto out_deconfigure;

	pci_scan_slot(slot->zdev->bus, ZPCI_DEVFN);
83
	pci_lock_rescan_remove();
S
Sebastian Ott 已提交
84
	pci_bus_add_devices(slot->zdev->bus);
85
	pci_unlock_rescan_remove();
S
Sebastian Ott 已提交
86 87 88 89 90

	return rc;

out_deconfigure:
	slot_deconfigure(slot);
91 92 93 94 95
	return rc;
}

static int disable_slot(struct hotplug_slot *hotplug_slot)
{
L
Lukas Wunner 已提交
96
	struct slot *slot = to_slot(hotplug_slot);
97
	struct pci_dev *pdev;
98 99 100 101 102
	int rc;

	if (!zpci_fn_configured(slot->zdev->state))
		return -EIO;

103 104 105 106 107
	pdev = pci_get_slot(slot->zdev->bus, ZPCI_DEVFN);
	if (pdev) {
		pci_stop_and_remove_bus_device_locked(pdev);
		pci_dev_put(pdev);
	}
108

109 110 111
	rc = zpci_disable_device(slot->zdev);
	if (rc)
		return rc;
112

S
Sebastian Ott 已提交
113
	return slot_deconfigure(slot);
114 115 116 117
}

static int get_power_status(struct hotplug_slot *hotplug_slot, u8 *value)
{
L
Lukas Wunner 已提交
118
	struct slot *slot = to_slot(hotplug_slot);
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137

	switch (slot->zdev->state) {
	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;
}

138
static const struct hotplug_slot_ops s390_hotplug_slot_ops = {
139 140 141 142 143 144
	.enable_slot =		enable_slot,
	.disable_slot =		disable_slot,
	.get_power_status =	get_power_status,
	.get_adapter_status =	get_adapter_status,
};

145
int zpci_init_slot(struct zpci_dev *zdev)
146 147 148 149 150 151 152 153 154 155 156 157 158
{
	char name[SLOT_NAME_SIZE];
	struct slot *slot;
	int rc;

	if (!zdev)
		return 0;

	slot = kzalloc(sizeof(*slot), GFP_KERNEL);
	if (!slot)
		goto error;

	slot->zdev = zdev;
L
Lukas Wunner 已提交
159
	slot->hotplug_slot.ops = &s390_hotplug_slot_ops;
160 161

	snprintf(name, SLOT_NAME_SIZE, "%08x", zdev->fid);
L
Lukas Wunner 已提交
162
	rc = pci_hp_register(&slot->hotplug_slot, zdev->bus,
163
			     ZPCI_DEVFN, name);
S
Sebastian Ott 已提交
164
	if (rc)
165
		goto error_reg;
S
Sebastian Ott 已提交
166

167 168 169 170 171 172 173 174 175
	list_add(&slot->slot_list, &s390_hotplug_slot_list);
	return 0;

error_reg:
	kfree(slot);
error:
	return -ENOMEM;
}

176
void zpci_exit_slot(struct zpci_dev *zdev)
177
{
178
	struct slot *slot, *next;
179

180 181
	list_for_each_entry_safe(slot, next, &s390_hotplug_slot_list,
				 slot_list) {
182 183 184
		if (slot->zdev != zdev)
			continue;
		list_del(&slot->slot_list);
L
Lukas Wunner 已提交
185
		pci_hp_deregister(&slot->hotplug_slot);
186
		kfree(slot);
187 188
	}
}