Merge branch 'linux-next' of git://git.kernel.org/pub/scm/linux/kernel/git/jbarnes...
[linux-2.6] / drivers / pci / hotplug / pciehp_core.c
1 /*
2  * PCI Express 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/pci.h>
35 #include "pciehp.h"
36 #include <linux/interrupt.h>
37 #include <linux/time.h>
38
39 /* Global variables */
40 int pciehp_debug;
41 int pciehp_poll_mode;
42 int pciehp_poll_time;
43 int pciehp_force;
44 struct workqueue_struct *pciehp_wq;
45
46 #define DRIVER_VERSION  "0.4"
47 #define DRIVER_AUTHOR   "Dan Zink <dan.zink@compaq.com>, Greg Kroah-Hartman <greg@kroah.com>, Dely Sy <dely.l.sy@intel.com>"
48 #define DRIVER_DESC     "PCI Express Hot Plug Controller Driver"
49
50 MODULE_AUTHOR(DRIVER_AUTHOR);
51 MODULE_DESCRIPTION(DRIVER_DESC);
52 MODULE_LICENSE("GPL");
53
54 module_param(pciehp_debug, bool, 0644);
55 module_param(pciehp_poll_mode, bool, 0644);
56 module_param(pciehp_poll_time, int, 0644);
57 module_param(pciehp_force, bool, 0644);
58 MODULE_PARM_DESC(pciehp_debug, "Debugging mode enabled or not");
59 MODULE_PARM_DESC(pciehp_poll_mode, "Using polling mechanism for hot-plug events or not");
60 MODULE_PARM_DESC(pciehp_poll_time, "Polling mechanism frequency, in seconds");
61 MODULE_PARM_DESC(pciehp_force, "Force pciehp, even if _OSC and OSHP are missing");
62
63 #define PCIE_MODULE_NAME "pciehp"
64
65 static int set_attention_status (struct hotplug_slot *slot, u8 value);
66 static int enable_slot          (struct hotplug_slot *slot);
67 static int disable_slot         (struct hotplug_slot *slot);
68 static int get_power_status     (struct hotplug_slot *slot, u8 *value);
69 static int get_attention_status (struct hotplug_slot *slot, u8 *value);
70 static int get_latch_status     (struct hotplug_slot *slot, u8 *value);
71 static int get_adapter_status   (struct hotplug_slot *slot, u8 *value);
72 static int get_max_bus_speed    (struct hotplug_slot *slot, enum pci_bus_speed *value);
73 static int get_cur_bus_speed    (struct hotplug_slot *slot, enum pci_bus_speed *value);
74
75 static struct hotplug_slot_ops pciehp_hotplug_slot_ops = {
76         .set_attention_status = set_attention_status,
77         .enable_slot =          enable_slot,
78         .disable_slot =         disable_slot,
79         .get_power_status =     get_power_status,
80         .get_attention_status = get_attention_status,
81         .get_latch_status =     get_latch_status,
82         .get_adapter_status =   get_adapter_status,
83         .get_max_bus_speed =    get_max_bus_speed,
84         .get_cur_bus_speed =    get_cur_bus_speed,
85 };
86
87 /**
88  * release_slot - free up the memory used by a slot
89  * @hotplug_slot: slot to free
90  */
91 static void release_slot(struct hotplug_slot *hotplug_slot)
92 {
93         struct slot *slot = hotplug_slot->private;
94
95         ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
96                  __func__, hotplug_slot_name(hotplug_slot));
97
98         kfree(hotplug_slot->info);
99         kfree(hotplug_slot);
100 }
101
102 static int init_slots(struct controller *ctrl)
103 {
104         struct slot *slot;
105         struct hotplug_slot *hotplug_slot;
106         struct hotplug_slot_info *info;
107         char name[SLOT_NAME_SIZE];
108         int retval = -ENOMEM;
109
110         list_for_each_entry(slot, &ctrl->slot_list, slot_list) {
111                 hotplug_slot = kzalloc(sizeof(*hotplug_slot), GFP_KERNEL);
112                 if (!hotplug_slot)
113                         goto error;
114
115                 info = kzalloc(sizeof(*info), GFP_KERNEL);
116                 if (!info)
117                         goto error_hpslot;
118
119                 /* register this slot with the hotplug pci core */
120                 hotplug_slot->info = info;
121                 hotplug_slot->private = slot;
122                 hotplug_slot->release = &release_slot;
123                 hotplug_slot->ops = &pciehp_hotplug_slot_ops;
124                 slot->hotplug_slot = hotplug_slot;
125                 snprintf(name, SLOT_NAME_SIZE, "%u", slot->number);
126
127                 ctrl_dbg(ctrl, "Registering domain:bus:dev=%04x:%02x:%02x "
128                          "hp_slot=%x sun=%x slot_device_offset=%x\n",
129                          pci_domain_nr(ctrl->pci_dev->subordinate),
130                          slot->bus, slot->device, slot->hp_slot, slot->number,
131                          ctrl->slot_device_offset);
132                 retval = pci_hp_register(hotplug_slot,
133                                          ctrl->pci_dev->subordinate,
134                                          slot->device,
135                                          name);
136                 if (retval) {
137                         ctrl_err(ctrl, "pci_hp_register failed with error %d\n",
138                                  retval);
139                         goto error_info;
140                 }
141                 get_power_status(hotplug_slot, &info->power_status);
142                 get_attention_status(hotplug_slot, &info->attention_status);
143                 get_latch_status(hotplug_slot, &info->latch_status);
144                 get_adapter_status(hotplug_slot, &info->adapter_status);
145         }
146
147         return 0;
148 error_info:
149         kfree(info);
150 error_hpslot:
151         kfree(hotplug_slot);
152 error:
153         return retval;
154 }
155
156 static void cleanup_slots(struct controller *ctrl)
157 {
158         struct slot *slot;
159         list_for_each_entry(slot, &ctrl->slot_list, slot_list)
160                 pci_hp_deregister(slot->hotplug_slot);
161 }
162
163 /*
164  * set_attention_status - Turns the Amber LED for a slot on, off or blink
165  */
166 static int set_attention_status(struct hotplug_slot *hotplug_slot, u8 status)
167 {
168         struct slot *slot = hotplug_slot->private;
169
170         ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
171                   __func__, slot_name(slot));
172
173         hotplug_slot->info->attention_status = status;
174
175         if (ATTN_LED(slot->ctrl))
176                 slot->hpc_ops->set_attention_status(slot, status);
177
178         return 0;
179 }
180
181
182 static int enable_slot(struct hotplug_slot *hotplug_slot)
183 {
184         struct slot *slot = hotplug_slot->private;
185
186         ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
187                  __func__, slot_name(slot));
188
189         return pciehp_sysfs_enable_slot(slot);
190 }
191
192
193 static int disable_slot(struct hotplug_slot *hotplug_slot)
194 {
195         struct slot *slot = hotplug_slot->private;
196
197         ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
198                   __func__, slot_name(slot));
199
200         return pciehp_sysfs_disable_slot(slot);
201 }
202
203 static int get_power_status(struct hotplug_slot *hotplug_slot, u8 *value)
204 {
205         struct slot *slot = hotplug_slot->private;
206         int retval;
207
208         ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
209                   __func__, slot_name(slot));
210
211         retval = slot->hpc_ops->get_power_status(slot, value);
212         if (retval < 0)
213                 *value = hotplug_slot->info->power_status;
214
215         return 0;
216 }
217
218 static int get_attention_status(struct hotplug_slot *hotplug_slot, u8 *value)
219 {
220         struct slot *slot = hotplug_slot->private;
221         int retval;
222
223         ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
224                   __func__, slot_name(slot));
225
226         retval = slot->hpc_ops->get_attention_status(slot, value);
227         if (retval < 0)
228                 *value = hotplug_slot->info->attention_status;
229
230         return 0;
231 }
232
233 static int get_latch_status(struct hotplug_slot *hotplug_slot, u8 *value)
234 {
235         struct slot *slot = hotplug_slot->private;
236         int retval;
237
238         ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
239                  __func__, slot_name(slot));
240
241         retval = slot->hpc_ops->get_latch_status(slot, value);
242         if (retval < 0)
243                 *value = hotplug_slot->info->latch_status;
244
245         return 0;
246 }
247
248 static int get_adapter_status(struct hotplug_slot *hotplug_slot, u8 *value)
249 {
250         struct slot *slot = hotplug_slot->private;
251         int retval;
252
253         ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
254                  __func__, slot_name(slot));
255
256         retval = slot->hpc_ops->get_adapter_status(slot, value);
257         if (retval < 0)
258                 *value = hotplug_slot->info->adapter_status;
259
260         return 0;
261 }
262
263 static int get_max_bus_speed(struct hotplug_slot *hotplug_slot,
264                                 enum pci_bus_speed *value)
265 {
266         struct slot *slot = hotplug_slot->private;
267         int retval;
268
269         ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
270                  __func__, slot_name(slot));
271
272         retval = slot->hpc_ops->get_max_bus_speed(slot, value);
273         if (retval < 0)
274                 *value = PCI_SPEED_UNKNOWN;
275
276         return 0;
277 }
278
279 static int get_cur_bus_speed(struct hotplug_slot *hotplug_slot, enum pci_bus_speed *value)
280 {
281         struct slot *slot = hotplug_slot->private;
282         int retval;
283
284         ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
285                  __func__, slot_name(slot));
286
287         retval = slot->hpc_ops->get_cur_bus_speed(slot, value);
288         if (retval < 0)
289                 *value = PCI_SPEED_UNKNOWN;
290
291         return 0;
292 }
293
294 static int pciehp_probe(struct pcie_device *dev)
295 {
296         int rc;
297         struct controller *ctrl;
298         struct slot *t_slot;
299         u8 value;
300         struct pci_dev *pdev = dev->port;
301
302         if (pciehp_force)
303                 dev_info(&dev->device,
304                          "Bypassing BIOS check for pciehp use on %s\n",
305                          pci_name(pdev));
306         else if (pciehp_get_hp_hw_control_from_firmware(pdev))
307                 goto err_out_none;
308
309         ctrl = pcie_init(dev);
310         if (!ctrl) {
311                 dev_err(&dev->device, "Controller initialization failed\n");
312                 goto err_out_none;
313         }
314         set_service_data(dev, ctrl);
315
316         /* Setup the slot information structures */
317         rc = init_slots(ctrl);
318         if (rc) {
319                 if (rc == -EBUSY)
320                         ctrl_warn(ctrl, "Slot already registered by another "
321                                   "hotplug driver\n");
322                 else
323                         ctrl_err(ctrl, "Slot initialization failed\n");
324                 goto err_out_release_ctlr;
325         }
326
327         /* Enable events after we have setup the data structures */
328         rc = pcie_init_notification(ctrl);
329         if (rc) {
330                 ctrl_err(ctrl, "Notification initialization failed\n");
331                 goto err_out_release_ctlr;
332         }
333
334         /* Check if slot is occupied */
335         t_slot = pciehp_find_slot(ctrl, ctrl->slot_device_offset);
336         t_slot->hpc_ops->get_adapter_status(t_slot, &value);
337         if (value) {
338                 if (pciehp_force)
339                         pciehp_enable_slot(t_slot);
340         } else {
341                 /* Power off slot if not occupied */
342                 if (POWER_CTRL(ctrl)) {
343                         rc = t_slot->hpc_ops->power_off_slot(t_slot);
344                         if (rc)
345                                 goto err_out_free_ctrl_slot;
346                 }
347         }
348
349         return 0;
350
351 err_out_free_ctrl_slot:
352         cleanup_slots(ctrl);
353 err_out_release_ctlr:
354         ctrl->hpc_ops->release_ctlr(ctrl);
355 err_out_none:
356         return -ENODEV;
357 }
358
359 static void pciehp_remove (struct pcie_device *dev)
360 {
361         struct controller *ctrl = get_service_data(dev);
362
363         cleanup_slots(ctrl);
364         ctrl->hpc_ops->release_ctlr(ctrl);
365 }
366
367 #ifdef CONFIG_PM
368 static int pciehp_suspend (struct pcie_device *dev)
369 {
370         dev_info(&dev->device, "%s ENTRY\n", __func__);
371         return 0;
372 }
373
374 static int pciehp_resume (struct pcie_device *dev)
375 {
376         dev_info(&dev->device, "%s ENTRY\n", __func__);
377         if (pciehp_force) {
378                 struct controller *ctrl = get_service_data(dev);
379                 struct slot *t_slot;
380                 u8 status;
381
382                 /* reinitialize the chipset's event detection logic */
383                 pcie_enable_notification(ctrl);
384
385                 t_slot = pciehp_find_slot(ctrl, ctrl->slot_device_offset);
386
387                 /* Check if slot is occupied */
388                 t_slot->hpc_ops->get_adapter_status(t_slot, &status);
389                 if (status)
390                         pciehp_enable_slot(t_slot);
391                 else
392                         pciehp_disable_slot(t_slot);
393         }
394         return 0;
395 }
396 #endif /* PM */
397
398 static struct pcie_port_service_driver hpdriver_portdrv = {
399         .name           = PCIE_MODULE_NAME,
400         .port_type      = PCIE_ANY_PORT,
401         .service        = PCIE_PORT_SERVICE_HP,
402
403         .probe          = pciehp_probe,
404         .remove         = pciehp_remove,
405
406 #ifdef  CONFIG_PM
407         .suspend        = pciehp_suspend,
408         .resume         = pciehp_resume,
409 #endif  /* PM */
410 };
411
412 static int __init pcied_init(void)
413 {
414         int retval = 0;
415
416         pciehp_firmware_init();
417         retval = pcie_port_service_register(&hpdriver_portdrv);
418         dbg("pcie_port_service_register = %d\n", retval);
419         info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
420         if (retval)
421                 dbg("Failure to register service\n");
422         return retval;
423 }
424
425 static void __exit pcied_cleanup(void)
426 {
427         dbg("unload_pciehpd()\n");
428         pcie_port_service_unregister(&hpdriver_portdrv);
429         info(DRIVER_DESC " version: " DRIVER_VERSION " unloaded\n");
430 }
431
432 module_init(pcied_init);
433 module_exit(pcied_cleanup);