PCI: shpchp: Handle push button event asynchronously
[firefly-linux-kernel-4.4.55.git] / drivers / pci / hotplug / shpchp_core.c
1 /*
2  * Standard Hot Plug Controller Driver
3  *
4  * Copyright (C) 1995,2001 Compaq Computer Corporation
5  * Copyright (C) 2001 Greg Kroah-Hartman (greg@kroah.com)
6  * Copyright (C) 2001 IBM Corp.
7  * Copyright (C) 2003-2004 Intel Corporation
8  *
9  * All rights reserved.
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or (at
14  * your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful, but
17  * WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
19  * NON INFRINGEMENT.  See the GNU General Public License for more
20  * details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25  *
26  * Send feedback to <greg@kroah.com>, <kristen.c.accardi@intel.com>
27  *
28  */
29
30 #include <linux/module.h>
31 #include <linux/moduleparam.h>
32 #include <linux/kernel.h>
33 #include <linux/types.h>
34 #include <linux/slab.h>
35 #include <linux/pci.h>
36 #include "shpchp.h"
37
38 /* Global variables */
39 bool shpchp_debug;
40 bool shpchp_poll_mode;
41 int shpchp_poll_time;
42 struct workqueue_struct *shpchp_wq;
43
44 #define DRIVER_VERSION  "0.4"
45 #define DRIVER_AUTHOR   "Dan Zink <dan.zink@compaq.com>, Greg Kroah-Hartman <greg@kroah.com>, Dely Sy <dely.l.sy@intel.com>"
46 #define DRIVER_DESC     "Standard Hot Plug PCI Controller Driver"
47
48 MODULE_AUTHOR(DRIVER_AUTHOR);
49 MODULE_DESCRIPTION(DRIVER_DESC);
50 MODULE_LICENSE("GPL");
51
52 module_param(shpchp_debug, bool, 0644);
53 module_param(shpchp_poll_mode, bool, 0644);
54 module_param(shpchp_poll_time, int, 0644);
55 MODULE_PARM_DESC(shpchp_debug, "Debugging mode enabled or not");
56 MODULE_PARM_DESC(shpchp_poll_mode, "Using polling mechanism for hot-plug events or not");
57 MODULE_PARM_DESC(shpchp_poll_time, "Polling mechanism frequency, in seconds");
58
59 #define SHPC_MODULE_NAME "shpchp"
60
61 static int set_attention_status (struct hotplug_slot *slot, u8 value);
62 static int enable_slot          (struct hotplug_slot *slot);
63 static int disable_slot         (struct hotplug_slot *slot);
64 static int get_power_status     (struct hotplug_slot *slot, u8 *value);
65 static int get_attention_status (struct hotplug_slot *slot, u8 *value);
66 static int get_latch_status     (struct hotplug_slot *slot, u8 *value);
67 static int get_adapter_status   (struct hotplug_slot *slot, u8 *value);
68
69 static struct hotplug_slot_ops shpchp_hotplug_slot_ops = {
70         .set_attention_status = set_attention_status,
71         .enable_slot =          enable_slot,
72         .disable_slot =         disable_slot,
73         .get_power_status =     get_power_status,
74         .get_attention_status = get_attention_status,
75         .get_latch_status =     get_latch_status,
76         .get_adapter_status =   get_adapter_status,
77 };
78
79 /**
80  * release_slot - free up the memory used by a slot
81  * @hotplug_slot: slot to free
82  */
83 static void release_slot(struct hotplug_slot *hotplug_slot)
84 {
85         struct slot *slot = hotplug_slot->private;
86
87         ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
88                  __func__, slot_name(slot));
89
90         kfree(slot->hotplug_slot->info);
91         kfree(slot->hotplug_slot);
92         kfree(slot);
93 }
94
95 static int init_slots(struct controller *ctrl)
96 {
97         struct slot *slot;
98         struct hotplug_slot *hotplug_slot;
99         struct hotplug_slot_info *info;
100         char name[SLOT_NAME_SIZE];
101         int retval;
102         int i;
103
104         for (i = 0; i < ctrl->num_slots; i++) {
105                 slot = kzalloc(sizeof(*slot), GFP_KERNEL);
106                 if (!slot) {
107                         retval = -ENOMEM;
108                         goto error;
109                 }
110
111                 hotplug_slot = kzalloc(sizeof(*hotplug_slot), GFP_KERNEL);
112                 if (!hotplug_slot) {
113                         retval = -ENOMEM;
114                         goto error_slot;
115                 }
116                 slot->hotplug_slot = hotplug_slot;
117
118                 info = kzalloc(sizeof(*info), GFP_KERNEL);
119                 if (!info) {
120                         retval = -ENOMEM;
121                         goto error_hpslot;
122                 }
123                 hotplug_slot->info = info;
124
125                 slot->hp_slot = i;
126                 slot->ctrl = ctrl;
127                 slot->bus = ctrl->pci_dev->subordinate->number;
128                 slot->device = ctrl->slot_device_offset + i;
129                 slot->hpc_ops = ctrl->hpc_ops;
130                 slot->number = ctrl->first_slot + (ctrl->slot_num_inc * i);
131                 mutex_init(&slot->lock);
132                 INIT_DELAYED_WORK(&slot->work, shpchp_queue_pushbutton_work);
133
134                 /* register this slot with the hotplug pci core */
135                 hotplug_slot->private = slot;
136                 hotplug_slot->release = &release_slot;
137                 snprintf(name, SLOT_NAME_SIZE, "%d", slot->number);
138                 hotplug_slot->ops = &shpchp_hotplug_slot_ops;
139
140                 ctrl_dbg(ctrl, "Registering domain:bus:dev=%04x:%02x:%02x "
141                          "hp_slot=%x sun=%x slot_device_offset=%x\n",
142                          pci_domain_nr(ctrl->pci_dev->subordinate),
143                          slot->bus, slot->device, slot->hp_slot, slot->number,
144                          ctrl->slot_device_offset);
145                 retval = pci_hp_register(slot->hotplug_slot,
146                                 ctrl->pci_dev->subordinate, slot->device, name);
147                 if (retval) {
148                         ctrl_err(ctrl, "pci_hp_register failed with error %d\n",
149                                  retval);
150                         goto error_info;
151                 }
152
153                 get_power_status(hotplug_slot, &info->power_status);
154                 get_attention_status(hotplug_slot, &info->attention_status);
155                 get_latch_status(hotplug_slot, &info->latch_status);
156                 get_adapter_status(hotplug_slot, &info->adapter_status);
157
158                 list_add(&slot->slot_list, &ctrl->slot_list);
159         }
160
161         return 0;
162 error_info:
163         kfree(info);
164 error_hpslot:
165         kfree(hotplug_slot);
166 error_slot:
167         kfree(slot);
168 error:
169         return retval;
170 }
171
172 void cleanup_slots(struct controller *ctrl)
173 {
174         struct list_head *tmp;
175         struct list_head *next;
176         struct slot *slot;
177
178         list_for_each_safe(tmp, next, &ctrl->slot_list) {
179                 slot = list_entry(tmp, struct slot, slot_list);
180                 list_del(&slot->slot_list);
181                 cancel_delayed_work(&slot->work);
182                 flush_workqueue(shpchp_wq);
183                 pci_hp_deregister(slot->hotplug_slot);
184         }
185 }
186
187 /*
188  * set_attention_status - Turns the Amber LED for a slot on, off or blink
189  */
190 static int set_attention_status (struct hotplug_slot *hotplug_slot, u8 status)
191 {
192         struct slot *slot = get_slot(hotplug_slot);
193
194         ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
195                  __func__, slot_name(slot));
196
197         hotplug_slot->info->attention_status = status;
198         slot->hpc_ops->set_attention_status(slot, status);
199
200         return 0;
201 }
202
203 static int enable_slot (struct hotplug_slot *hotplug_slot)
204 {
205         struct slot *slot = get_slot(hotplug_slot);
206
207         ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
208                  __func__, slot_name(slot));
209
210         return shpchp_sysfs_enable_slot(slot);
211 }
212
213 static int disable_slot (struct hotplug_slot *hotplug_slot)
214 {
215         struct slot *slot = get_slot(hotplug_slot);
216
217         ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
218                  __func__, slot_name(slot));
219
220         return shpchp_sysfs_disable_slot(slot);
221 }
222
223 static int get_power_status (struct hotplug_slot *hotplug_slot, u8 *value)
224 {
225         struct slot *slot = get_slot(hotplug_slot);
226         int retval;
227
228         ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
229                  __func__, slot_name(slot));
230
231         retval = slot->hpc_ops->get_power_status(slot, value);
232         if (retval < 0)
233                 *value = hotplug_slot->info->power_status;
234
235         return 0;
236 }
237
238 static int get_attention_status (struct hotplug_slot *hotplug_slot, u8 *value)
239 {
240         struct slot *slot = get_slot(hotplug_slot);
241         int retval;
242
243         ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
244                  __func__, slot_name(slot));
245
246         retval = slot->hpc_ops->get_attention_status(slot, value);
247         if (retval < 0)
248                 *value = hotplug_slot->info->attention_status;
249
250         return 0;
251 }
252
253 static int get_latch_status (struct hotplug_slot *hotplug_slot, u8 *value)
254 {
255         struct slot *slot = get_slot(hotplug_slot);
256         int retval;
257
258         ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
259                  __func__, slot_name(slot));
260
261         retval = slot->hpc_ops->get_latch_status(slot, value);
262         if (retval < 0)
263                 *value = hotplug_slot->info->latch_status;
264
265         return 0;
266 }
267
268 static int get_adapter_status (struct hotplug_slot *hotplug_slot, u8 *value)
269 {
270         struct slot *slot = get_slot(hotplug_slot);
271         int retval;
272
273         ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
274                  __func__, slot_name(slot));
275
276         retval = slot->hpc_ops->get_adapter_status(slot, value);
277         if (retval < 0)
278                 *value = hotplug_slot->info->adapter_status;
279
280         return 0;
281 }
282
283 static int is_shpc_capable(struct pci_dev *dev)
284 {
285         if (dev->vendor == PCI_VENDOR_ID_AMD &&
286             dev->device == PCI_DEVICE_ID_AMD_GOLAM_7450)
287                 return 1;
288         if (!pci_find_capability(dev, PCI_CAP_ID_SHPC))
289                 return 0;
290         if (get_hp_hw_control_from_firmware(dev))
291                 return 0;
292         return 1;
293 }
294
295 static int shpc_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
296 {
297         int rc;
298         struct controller *ctrl;
299
300         if (!is_shpc_capable(pdev))
301                 return -ENODEV;
302
303         ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
304         if (!ctrl) {
305                 dev_err(&pdev->dev, "%s: Out of memory\n", __func__);
306                 goto err_out_none;
307         }
308         INIT_LIST_HEAD(&ctrl->slot_list);
309
310         rc = shpc_init(ctrl, pdev);
311         if (rc) {
312                 ctrl_dbg(ctrl, "Controller initialization failed\n");
313                 goto err_out_free_ctrl;
314         }
315
316         pci_set_drvdata(pdev, ctrl);
317
318         /* Setup the slot information structures */
319         rc = init_slots(ctrl);
320         if (rc) {
321                 ctrl_err(ctrl, "Slot initialization failed\n");
322                 goto err_out_release_ctlr;
323         }
324
325         rc = shpchp_create_ctrl_files(ctrl);
326         if (rc)
327                 goto err_cleanup_slots;
328
329         return 0;
330
331 err_cleanup_slots:
332         cleanup_slots(ctrl);
333 err_out_release_ctlr:
334         ctrl->hpc_ops->release_ctlr(ctrl);
335 err_out_free_ctrl:
336         kfree(ctrl);
337 err_out_none:
338         return -ENODEV;
339 }
340
341 static void shpc_remove(struct pci_dev *dev)
342 {
343         struct controller *ctrl = pci_get_drvdata(dev);
344
345         shpchp_remove_ctrl_files(ctrl);
346         ctrl->hpc_ops->release_ctlr(ctrl);
347         kfree(ctrl);
348 }
349
350 static struct pci_device_id shpcd_pci_tbl[] = {
351         {PCI_DEVICE_CLASS(((PCI_CLASS_BRIDGE_PCI << 8) | 0x00), ~0)},
352         { /* end: all zeroes */ }
353 };
354 MODULE_DEVICE_TABLE(pci, shpcd_pci_tbl);
355
356 static struct pci_driver shpc_driver = {
357         .name =         SHPC_MODULE_NAME,
358         .id_table =     shpcd_pci_tbl,
359         .probe =        shpc_probe,
360         .remove =       shpc_remove,
361 };
362
363 static int __init shpcd_init(void)
364 {
365         int retval = 0;
366
367         shpchp_wq = alloc_workqueue("shpchp", 0, 0);
368         if (!shpchp_wq)
369                 return -ENOMEM;
370
371         retval = pci_register_driver(&shpc_driver);
372         dbg("%s: pci_register_driver = %d\n", __func__, retval);
373         info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
374         if (retval) {
375                 destroy_workqueue(shpchp_wq);
376         }
377         return retval;
378 }
379
380 static void __exit shpcd_cleanup(void)
381 {
382         dbg("unload_shpchpd()\n");
383         pci_unregister_driver(&shpc_driver);
384         destroy_workqueue(shpchp_wq);
385         info(DRIVER_DESC " version: " DRIVER_VERSION " unloaded\n");
386 }
387
388 module_init(shpcd_init);
389 module_exit(shpcd_cleanup);