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
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
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>
36 #include <linux/interrupt.h>
37 #include <linux/time.h>
39 /* Global variables */
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 = {
77 .set_attention_status = set_attention_status,
78 .enable_slot = enable_slot,
79 .disable_slot = disable_slot,
80 .get_power_status = get_power_status,
81 .get_attention_status = get_attention_status,
82 .get_latch_status = get_latch_status,
83 .get_adapter_status = get_adapter_status,
84 .get_max_bus_speed = get_max_bus_speed,
85 .get_cur_bus_speed = get_cur_bus_speed,
89 * release_slot - free up the memory used by a slot
90 * @hotplug_slot: slot to free
92 static void release_slot(struct hotplug_slot *hotplug_slot)
94 struct slot *slot = hotplug_slot->private;
96 ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
97 __func__, hotplug_slot_name(hotplug_slot));
99 kfree(hotplug_slot->info);
103 static int init_slots(struct controller *ctrl)
106 struct hotplug_slot *hotplug_slot;
107 struct hotplug_slot_info *info;
108 char name[SLOT_NAME_SIZE];
109 int retval = -ENOMEM;
111 list_for_each_entry(slot, &ctrl->slot_list, slot_list) {
112 hotplug_slot = kzalloc(sizeof(*hotplug_slot), GFP_KERNEL);
116 info = kzalloc(sizeof(*info), GFP_KERNEL);
120 /* register this slot with the hotplug pci core */
121 hotplug_slot->info = info;
122 hotplug_slot->private = slot;
123 hotplug_slot->release = &release_slot;
124 hotplug_slot->ops = &pciehp_hotplug_slot_ops;
125 slot->hotplug_slot = hotplug_slot;
126 snprintf(name, SLOT_NAME_SIZE, "%u", slot->number);
128 ctrl_dbg(ctrl, "Registering domain:bus:dev=%04x:%02x:%02x "
129 "hp_slot=%x sun=%x slot_device_offset=%x\n",
130 pci_domain_nr(ctrl->pci_dev->subordinate),
131 slot->bus, slot->device, slot->hp_slot, slot->number,
132 ctrl->slot_device_offset);
133 retval = pci_hp_register(hotplug_slot,
134 ctrl->pci_dev->subordinate,
138 ctrl_err(ctrl, "pci_hp_register failed with error %d\n",
142 get_power_status(hotplug_slot, &info->power_status);
143 get_attention_status(hotplug_slot, &info->attention_status);
144 get_latch_status(hotplug_slot, &info->latch_status);
145 get_adapter_status(hotplug_slot, &info->adapter_status);
157 static void cleanup_slots(struct controller *ctrl)
160 list_for_each_entry(slot, &ctrl->slot_list, slot_list)
161 pci_hp_deregister(slot->hotplug_slot);
165 * set_attention_status - Turns the Amber LED for a slot on, off or blink
167 static int set_attention_status(struct hotplug_slot *hotplug_slot, u8 status)
169 struct slot *slot = hotplug_slot->private;
171 ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
172 __func__, slot_name(slot));
174 hotplug_slot->info->attention_status = status;
176 if (ATTN_LED(slot->ctrl))
177 slot->hpc_ops->set_attention_status(slot, status);
183 static int enable_slot(struct hotplug_slot *hotplug_slot)
185 struct slot *slot = hotplug_slot->private;
187 ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
188 __func__, slot_name(slot));
190 return pciehp_sysfs_enable_slot(slot);
194 static int disable_slot(struct hotplug_slot *hotplug_slot)
196 struct slot *slot = hotplug_slot->private;
198 ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
199 __func__, slot_name(slot));
201 return pciehp_sysfs_disable_slot(slot);
204 static int get_power_status(struct hotplug_slot *hotplug_slot, u8 *value)
206 struct slot *slot = hotplug_slot->private;
209 ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
210 __func__, slot_name(slot));
212 retval = slot->hpc_ops->get_power_status(slot, value);
214 *value = hotplug_slot->info->power_status;
219 static int get_attention_status(struct hotplug_slot *hotplug_slot, u8 *value)
221 struct slot *slot = hotplug_slot->private;
224 ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
225 __func__, slot_name(slot));
227 retval = slot->hpc_ops->get_attention_status(slot, value);
229 *value = hotplug_slot->info->attention_status;
234 static int get_latch_status(struct hotplug_slot *hotplug_slot, u8 *value)
236 struct slot *slot = hotplug_slot->private;
239 ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
240 __func__, slot_name(slot));
242 retval = slot->hpc_ops->get_latch_status(slot, value);
244 *value = hotplug_slot->info->latch_status;
249 static int get_adapter_status(struct hotplug_slot *hotplug_slot, u8 *value)
251 struct slot *slot = hotplug_slot->private;
254 ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
255 __func__, slot_name(slot));
257 retval = slot->hpc_ops->get_adapter_status(slot, value);
259 *value = hotplug_slot->info->adapter_status;
264 static int get_max_bus_speed(struct hotplug_slot *hotplug_slot,
265 enum pci_bus_speed *value)
267 struct slot *slot = hotplug_slot->private;
270 ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
271 __func__, slot_name(slot));
273 retval = slot->hpc_ops->get_max_bus_speed(slot, value);
275 *value = PCI_SPEED_UNKNOWN;
280 static int get_cur_bus_speed(struct hotplug_slot *hotplug_slot, enum pci_bus_speed *value)
282 struct slot *slot = hotplug_slot->private;
285 ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
286 __func__, slot_name(slot));
288 retval = slot->hpc_ops->get_cur_bus_speed(slot, value);
290 *value = PCI_SPEED_UNKNOWN;
295 static int pciehp_probe(struct pcie_device *dev)
298 struct controller *ctrl;
301 struct pci_dev *pdev = dev->port;
304 dev_info(&dev->device,
305 "Bypassing BIOS check for pciehp use on %s\n",
307 else if (pciehp_get_hp_hw_control_from_firmware(pdev))
310 ctrl = pcie_init(dev);
312 dev_err(&dev->device, "Controller initialization failed\n");
315 set_service_data(dev, ctrl);
317 /* Setup the slot information structures */
318 rc = init_slots(ctrl);
321 ctrl_warn(ctrl, "Slot already registered by another "
324 ctrl_err(ctrl, "Slot initialization failed\n");
325 goto err_out_release_ctlr;
328 /* Enable events after we have setup the data structures */
329 rc = pcie_init_notification(ctrl);
331 ctrl_err(ctrl, "Notification initialization failed\n");
332 goto err_out_release_ctlr;
335 /* Check if slot is occupied */
336 t_slot = pciehp_find_slot(ctrl, ctrl->slot_device_offset);
337 t_slot->hpc_ops->get_adapter_status(t_slot, &value);
340 pciehp_enable_slot(t_slot);
342 /* Power off slot if not occupied */
343 if (POWER_CTRL(ctrl)) {
344 rc = t_slot->hpc_ops->power_off_slot(t_slot);
346 goto err_out_free_ctrl_slot;
352 err_out_free_ctrl_slot:
354 err_out_release_ctlr:
355 ctrl->hpc_ops->release_ctlr(ctrl);
360 static void pciehp_remove (struct pcie_device *dev)
362 struct controller *ctrl = get_service_data(dev);
365 ctrl->hpc_ops->release_ctlr(ctrl);
369 static int pciehp_suspend (struct pcie_device *dev)
371 dev_info(&dev->device, "%s ENTRY\n", __func__);
375 static int pciehp_resume (struct pcie_device *dev)
377 dev_info(&dev->device, "%s ENTRY\n", __func__);
379 struct controller *ctrl = get_service_data(dev);
383 /* reinitialize the chipset's event detection logic */
384 pcie_enable_notification(ctrl);
386 t_slot = pciehp_find_slot(ctrl, ctrl->slot_device_offset);
388 /* Check if slot is occupied */
389 t_slot->hpc_ops->get_adapter_status(t_slot, &status);
391 pciehp_enable_slot(t_slot);
393 pciehp_disable_slot(t_slot);
399 static struct pcie_port_service_driver hpdriver_portdrv = {
400 .name = PCIE_MODULE_NAME,
401 .port_type = PCIE_ANY_PORT,
402 .service = PCIE_PORT_SERVICE_HP,
404 .probe = pciehp_probe,
405 .remove = pciehp_remove,
408 .suspend = pciehp_suspend,
409 .resume = pciehp_resume,
413 static int __init pcied_init(void)
417 pciehp_firmware_init();
418 retval = pcie_port_service_register(&hpdriver_portdrv);
419 dbg("pcie_port_service_register = %d\n", retval);
420 info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
422 dbg("Failure to register service\n");
426 static void __exit pcied_cleanup(void)
428 dbg("unload_pciehpd()\n");
429 pcie_port_service_unregister(&hpdriver_portdrv);
430 info(DRIVER_DESC " version: " DRIVER_VERSION " unloaded\n");
433 module_init(pcied_init);
434 module_exit(pcied_cleanup);