blob: 6053e7221b51f55cad6bde046686fed1ae7c2f02 [file] [log] [blame]
Jan Glauber7441b062012-11-29 14:35:47 +01001/*
2 * PCI Hot Plug Controller Driver for System z
3 *
4 * Copyright 2012 IBM Corp.
5 *
6 * Author(s):
7 * Jan Glauber <jang@linux.vnet.ibm.com>
8 */
9
10#define COMPONENT "zPCI hpc"
11#define pr_fmt(fmt) COMPONENT ": " fmt
12
13#include <linux/module.h>
14#include <linux/kernel.h>
15#include <linux/slab.h>
16#include <linux/pci.h>
17#include <linux/pci_hotplug.h>
18#include <linux/init.h>
Sebastian Otta2ab8332013-04-16 14:11:14 +020019#include <asm/pci_debug.h>
Jan Glauber7441b062012-11-29 14:35:47 +010020#include <asm/sclp.h>
21
22#define SLOT_NAME_SIZE 10
23static LIST_HEAD(s390_hotplug_slot_list);
24
25MODULE_AUTHOR("Jan Glauber <jang@linux.vnet.ibm.com");
26MODULE_DESCRIPTION("Hot Plug PCI Controller for System z");
27MODULE_LICENSE("GPL");
28
29static int zpci_fn_configured(enum zpci_state state)
30{
31 return state == ZPCI_FN_STATE_CONFIGURED ||
32 state == ZPCI_FN_STATE_ONLINE;
33}
34
35/*
36 * struct slot - slot information for each *physical* slot
37 */
38struct slot {
39 struct list_head slot_list;
40 struct hotplug_slot *hotplug_slot;
41 struct zpci_dev *zdev;
42};
43
44static int enable_slot(struct hotplug_slot *hotplug_slot)
45{
46 struct slot *slot = hotplug_slot->private;
47 int rc;
48
49 if (slot->zdev->state != ZPCI_FN_STATE_STANDBY)
50 return -EIO;
51
52 rc = sclp_pci_configure(slot->zdev->fid);
Sebastian Otta2ab8332013-04-16 14:11:14 +020053 zpci_dbg(3, "conf fid:%x, rc:%d\n", slot->zdev->fid, rc);
Jan Glauber7441b062012-11-29 14:35:47 +010054 if (!rc) {
55 slot->zdev->state = ZPCI_FN_STATE_CONFIGURED;
56 /* automatically scan the device after is was configured */
57 zpci_enable_device(slot->zdev);
58 zpci_scan_device(slot->zdev);
59 }
60 return rc;
61}
62
63static int disable_slot(struct hotplug_slot *hotplug_slot)
64{
65 struct slot *slot = hotplug_slot->private;
66 int rc;
67
68 if (!zpci_fn_configured(slot->zdev->state))
69 return -EIO;
70
71 /* TODO: we rely on the user to unbind/remove the device, is that plausible
72 * or do we need to trigger that here?
73 */
74 rc = sclp_pci_deconfigure(slot->zdev->fid);
Sebastian Otta2ab8332013-04-16 14:11:14 +020075 zpci_dbg(3, "deconf fid:%x, rc:%d\n", slot->zdev->fid, rc);
Jan Glauber7441b062012-11-29 14:35:47 +010076 if (!rc) {
77 /* Fixme: better call List-PCI to find the disabled FH
78 for the FID since the FH should be opaque... */
79 slot->zdev->fh &= 0x7fffffff;
80 slot->zdev->state = ZPCI_FN_STATE_STANDBY;
81 }
82 return rc;
83}
84
85static int get_power_status(struct hotplug_slot *hotplug_slot, u8 *value)
86{
87 struct slot *slot = hotplug_slot->private;
88
89 switch (slot->zdev->state) {
90 case ZPCI_FN_STATE_STANDBY:
91 *value = 0;
92 break;
93 default:
94 *value = 1;
95 break;
96 }
97 return 0;
98}
99
100static int get_adapter_status(struct hotplug_slot *hotplug_slot, u8 *value)
101{
102 /* if the slot exits it always contains a function */
103 *value = 1;
104 return 0;
105}
106
107static void release_slot(struct hotplug_slot *hotplug_slot)
108{
109 struct slot *slot = hotplug_slot->private;
110
111 pr_debug("%s - physical_slot = %s\n", __func__, hotplug_slot_name(hotplug_slot));
112 kfree(slot->hotplug_slot->info);
113 kfree(slot->hotplug_slot);
114 kfree(slot);
115}
116
117static struct hotplug_slot_ops s390_hotplug_slot_ops = {
118 .enable_slot = enable_slot,
119 .disable_slot = disable_slot,
120 .get_power_status = get_power_status,
121 .get_adapter_status = get_adapter_status,
122};
123
124static int init_pci_slot(struct zpci_dev *zdev)
125{
126 struct hotplug_slot *hotplug_slot;
127 struct hotplug_slot_info *info;
128 char name[SLOT_NAME_SIZE];
129 struct slot *slot;
130 int rc;
131
132 if (!zdev)
133 return 0;
134
135 slot = kzalloc(sizeof(*slot), GFP_KERNEL);
136 if (!slot)
137 goto error;
138
139 hotplug_slot = kzalloc(sizeof(*hotplug_slot), GFP_KERNEL);
140 if (!hotplug_slot)
141 goto error_hp;
142 hotplug_slot->private = slot;
143
144 slot->hotplug_slot = hotplug_slot;
145 slot->zdev = zdev;
146
147 info = kzalloc(sizeof(*info), GFP_KERNEL);
148 if (!info)
149 goto error_info;
150 hotplug_slot->info = info;
151
152 hotplug_slot->ops = &s390_hotplug_slot_ops;
153 hotplug_slot->release = &release_slot;
154
155 get_power_status(hotplug_slot, &info->power_status);
156 get_adapter_status(hotplug_slot, &info->adapter_status);
157
158 snprintf(name, SLOT_NAME_SIZE, "%08x", zdev->fid);
159 rc = pci_hp_register(slot->hotplug_slot, zdev->bus,
160 ZPCI_DEVFN, name);
161 if (rc) {
162 pr_err("pci_hp_register failed with error %d\n", rc);
163 goto error_reg;
164 }
165 list_add(&slot->slot_list, &s390_hotplug_slot_list);
166 return 0;
167
168error_reg:
169 kfree(info);
170error_info:
171 kfree(hotplug_slot);
172error_hp:
173 kfree(slot);
174error:
175 return -ENOMEM;
176}
177
Jan Glauber7441b062012-11-29 14:35:47 +0100178static void exit_pci_slot(struct zpci_dev *zdev)
179{
180 struct list_head *tmp, *n;
181 struct slot *slot;
182
183 list_for_each_safe(tmp, n, &s390_hotplug_slot_list) {
184 slot = list_entry(tmp, struct slot, slot_list);
185 if (slot->zdev != zdev)
186 continue;
187 list_del(&slot->slot_list);
188 pci_hp_deregister(slot->hotplug_slot);
189 }
190}
191
Sebastian Ott53923352013-01-31 19:55:17 +0100192static struct pci_hp_callback_ops hp_ops = {
193 .create_slot = init_pci_slot,
194 .remove_slot = exit_pci_slot,
195};
196
197static void __init init_pci_slots(void)
198{
199 struct zpci_dev *zdev;
200
201 /*
202 * Create a structure for each slot, and register that slot
203 * with the pci_hotplug subsystem.
204 */
205 mutex_lock(&zpci_list_lock);
206 list_for_each_entry(zdev, &zpci_list, entry) {
207 init_pci_slot(zdev);
208 }
209 mutex_unlock(&zpci_list_lock);
210}
211
Jan Glauber7441b062012-11-29 14:35:47 +0100212static void __exit exit_pci_slots(void)
213{
214 struct list_head *tmp, *n;
215 struct slot *slot;
216
217 /*
218 * Unregister all of our slots with the pci_hotplug subsystem.
219 * Memory will be freed in release_slot() callback after slot's
220 * lifespan is finished.
221 */
222 list_for_each_safe(tmp, n, &s390_hotplug_slot_list) {
223 slot = list_entry(tmp, struct slot, slot_list);
224 list_del(&slot->slot_list);
225 pci_hp_deregister(slot->hotplug_slot);
226 }
227}
228
229static int __init pci_hotplug_s390_init(void)
230{
Heiko Carstens1e5635d2013-01-30 15:52:16 +0100231 if (!s390_pci_probe)
Jan Glauber7441b062012-11-29 14:35:47 +0100232 return -EOPNOTSUPP;
233
Sebastian Ott53923352013-01-31 19:55:17 +0100234 zpci_register_hp_ops(&hp_ops);
235 init_pci_slots();
236
237 return 0;
Jan Glauber7441b062012-11-29 14:35:47 +0100238}
239
240static void __exit pci_hotplug_s390_exit(void)
241{
242 exit_pci_slots();
Sebastian Ott53923352013-01-31 19:55:17 +0100243 zpci_deregister_hp_ops();
Jan Glauber7441b062012-11-29 14:35:47 +0100244}
245
246module_init(pci_hotplug_s390_init);
247module_exit(pci_hotplug_s390_exit);