Merge git://git.infradead.org/mtd-2.6
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / pci / hotplug / pciehp_core.c
blob2317557fdee637df956ebf957eaf0d09055f46e9
1 /*
2 * PCI Express Hot Plug Controller Driver
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
9 * All rights reserved.
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.
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.
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.
26 * Send feedback to <greg@kroah.com>, <kristen.c.accardi@intel.com>
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>
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;
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"
50 MODULE_AUTHOR(DRIVER_AUTHOR);
51 MODULE_DESCRIPTION(DRIVER_DESC);
52 MODULE_LICENSE("GPL");
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");
63 #define PCIE_MODULE_NAME "pciehp"
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);
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,
87 /**
88 * release_slot - free up the memory used by a slot
89 * @hotplug_slot: slot to free
91 static void release_slot(struct hotplug_slot *hotplug_slot)
93 struct slot *slot = hotplug_slot->private;
95 ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
96 __func__, hotplug_slot_name(hotplug_slot));
98 kfree(hotplug_slot->info);
99 kfree(hotplug_slot);
102 static int init_slots(struct controller *ctrl)
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;
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;
115 info = kzalloc(sizeof(*info), GFP_KERNEL);
116 if (!info)
117 goto error_hpslot;
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);
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;
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);
147 return 0;
148 error_info:
149 kfree(info);
150 error_hpslot:
151 kfree(hotplug_slot);
152 error:
153 return retval;
156 static void cleanup_slots(struct controller *ctrl)
158 struct slot *slot;
159 list_for_each_entry(slot, &ctrl->slot_list, slot_list)
160 pci_hp_deregister(slot->hotplug_slot);
164 * set_attention_status - Turns the Amber LED for a slot on, off or blink
166 static int set_attention_status(struct hotplug_slot *hotplug_slot, u8 status)
168 struct slot *slot = hotplug_slot->private;
170 ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
171 __func__, slot_name(slot));
173 hotplug_slot->info->attention_status = status;
175 if (ATTN_LED(slot->ctrl))
176 slot->hpc_ops->set_attention_status(slot, status);
178 return 0;
182 static int enable_slot(struct hotplug_slot *hotplug_slot)
184 struct slot *slot = hotplug_slot->private;
186 ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
187 __func__, slot_name(slot));
189 return pciehp_sysfs_enable_slot(slot);
193 static int disable_slot(struct hotplug_slot *hotplug_slot)
195 struct slot *slot = hotplug_slot->private;
197 ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
198 __func__, slot_name(slot));
200 return pciehp_sysfs_disable_slot(slot);
203 static int get_power_status(struct hotplug_slot *hotplug_slot, u8 *value)
205 struct slot *slot = hotplug_slot->private;
206 int retval;
208 ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
209 __func__, slot_name(slot));
211 retval = slot->hpc_ops->get_power_status(slot, value);
212 if (retval < 0)
213 *value = hotplug_slot->info->power_status;
215 return 0;
218 static int get_attention_status(struct hotplug_slot *hotplug_slot, u8 *value)
220 struct slot *slot = hotplug_slot->private;
221 int retval;
223 ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
224 __func__, slot_name(slot));
226 retval = slot->hpc_ops->get_attention_status(slot, value);
227 if (retval < 0)
228 *value = hotplug_slot->info->attention_status;
230 return 0;
233 static int get_latch_status(struct hotplug_slot *hotplug_slot, u8 *value)
235 struct slot *slot = hotplug_slot->private;
236 int retval;
238 ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
239 __func__, slot_name(slot));
241 retval = slot->hpc_ops->get_latch_status(slot, value);
242 if (retval < 0)
243 *value = hotplug_slot->info->latch_status;
245 return 0;
248 static int get_adapter_status(struct hotplug_slot *hotplug_slot, u8 *value)
250 struct slot *slot = hotplug_slot->private;
251 int retval;
253 ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
254 __func__, slot_name(slot));
256 retval = slot->hpc_ops->get_adapter_status(slot, value);
257 if (retval < 0)
258 *value = hotplug_slot->info->adapter_status;
260 return 0;
263 static int get_max_bus_speed(struct hotplug_slot *hotplug_slot,
264 enum pci_bus_speed *value)
266 struct slot *slot = hotplug_slot->private;
267 int retval;
269 ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
270 __func__, slot_name(slot));
272 retval = slot->hpc_ops->get_max_bus_speed(slot, value);
273 if (retval < 0)
274 *value = PCI_SPEED_UNKNOWN;
276 return 0;
279 static int get_cur_bus_speed(struct hotplug_slot *hotplug_slot, enum pci_bus_speed *value)
281 struct slot *slot = hotplug_slot->private;
282 int retval;
284 ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
285 __func__, slot_name(slot));
287 retval = slot->hpc_ops->get_cur_bus_speed(slot, value);
288 if (retval < 0)
289 *value = PCI_SPEED_UNKNOWN;
291 return 0;
294 static int pciehp_probe(struct pcie_device *dev)
296 int rc;
297 struct controller *ctrl;
298 struct slot *t_slot;
299 u8 value;
300 struct pci_dev *pdev = dev->port;
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;
309 ctrl = pcie_init(dev);
310 if (!ctrl) {
311 dev_err(&dev->device, "Controller initialization failed\n");
312 goto err_out_none;
314 set_service_data(dev, ctrl);
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;
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;
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;
349 return 0;
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;
359 static void pciehp_remove (struct pcie_device *dev)
361 struct controller *ctrl = get_service_data(dev);
363 cleanup_slots(ctrl);
364 ctrl->hpc_ops->release_ctlr(ctrl);
367 #ifdef CONFIG_PM
368 static int pciehp_suspend (struct pcie_device *dev)
370 dev_info(&dev->device, "%s ENTRY\n", __func__);
371 return 0;
374 static int pciehp_resume (struct pcie_device *dev)
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;
382 /* reinitialize the chipset's event detection logic */
383 pcie_enable_notification(ctrl);
385 t_slot = pciehp_find_slot(ctrl, ctrl->slot_device_offset);
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);
394 return 0;
396 #endif /* PM */
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,
403 .probe = pciehp_probe,
404 .remove = pciehp_remove,
406 #ifdef CONFIG_PM
407 .suspend = pciehp_suspend,
408 .resume = pciehp_resume,
409 #endif /* PM */
412 static int __init pcied_init(void)
414 int retval = 0;
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;
425 static void __exit pcied_cleanup(void)
427 dbg("unload_pciehpd()\n");
428 pcie_port_service_unregister(&hpdriver_portdrv);
429 info(DRIVER_DESC " version: " DRIVER_VERSION " unloaded\n");
432 module_init(pcied_init);
433 module_exit(pcied_cleanup);