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