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>
38 /* Global variables */
43 struct controller *pciehp_ctrl_list;
45 #define DRIVER_VERSION "0.4"
46 #define DRIVER_AUTHOR "Dan Zink <dan.zink@compaq.com>, Greg Kroah-Hartman <greg@kroah.com>, Dely Sy <dely.l.sy@intel.com>"
47 #define DRIVER_DESC "PCI Express Hot Plug Controller Driver"
49 MODULE_AUTHOR(DRIVER_AUTHOR);
50 MODULE_DESCRIPTION(DRIVER_DESC);
51 MODULE_LICENSE("GPL");
53 module_param(pciehp_debug, bool, 0644);
54 module_param(pciehp_poll_mode, bool, 0644);
55 module_param(pciehp_poll_time, int, 0644);
56 module_param(pciehp_force, bool, 0644);
57 MODULE_PARM_DESC(pciehp_debug, "Debugging mode enabled or not");
58 MODULE_PARM_DESC(pciehp_poll_mode, "Using polling mechanism for hot-plug events or not");
59 MODULE_PARM_DESC(pciehp_poll_time, "Polling mechanism frequency, in seconds");
60 MODULE_PARM_DESC(pciehp_force, "Force pciehp, even if _OSC and OSHP are missing");
62 #define PCIE_MODULE_NAME "pciehp"
64 static int pcie_start_thread (void);
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_address (struct hotplug_slot *slot, u32 *value);
73 static int get_max_bus_speed (struct hotplug_slot *slot, enum pci_bus_speed *value);
74 static int get_cur_bus_speed (struct hotplug_slot *slot, enum pci_bus_speed *value);
76 static struct hotplug_slot_ops pciehp_hotplug_slot_ops = {
78 .set_attention_status = set_attention_status,
79 .enable_slot = enable_slot,
80 .disable_slot = disable_slot,
81 .get_power_status = get_power_status,
82 .get_attention_status = get_attention_status,
83 .get_latch_status = get_latch_status,
84 .get_adapter_status = get_adapter_status,
85 .get_address = get_address,
86 .get_max_bus_speed = get_max_bus_speed,
87 .get_cur_bus_speed = get_cur_bus_speed,
91 * release_slot - free up the memory used by a slot
92 * @hotplug_slot: slot to free
94 static void release_slot(struct hotplug_slot *hotplug_slot)
96 struct slot *slot = hotplug_slot->private;
98 dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
100 kfree(slot->hotplug_slot->info);
101 kfree(slot->hotplug_slot);
105 static void make_slot_name(struct slot *slot)
107 snprintf(slot->hotplug_slot->name, SLOT_NAME_SIZE, "%04d_%04d",
108 slot->bus, slot->number);
111 static int init_slots(struct controller *ctrl)
114 struct hotplug_slot *hotplug_slot;
115 struct hotplug_slot_info *info;
116 int retval = -ENOMEM;
119 for (i = 0; i < ctrl->num_slots; i++) {
120 slot = kzalloc(sizeof(*slot), GFP_KERNEL);
124 hotplug_slot = kzalloc(sizeof(*hotplug_slot), GFP_KERNEL);
127 slot->hotplug_slot = hotplug_slot;
129 info = kzalloc(sizeof(*info), GFP_KERNEL);
132 hotplug_slot->info = info;
134 hotplug_slot->name = slot->name;
138 slot->bus = ctrl->pci_dev->subordinate->number;
139 slot->device = ctrl->slot_device_offset + i;
140 slot->hpc_ops = ctrl->hpc_ops;
141 slot->number = ctrl->first_slot;
143 /* register this slot with the hotplug pci core */
144 hotplug_slot->private = slot;
145 hotplug_slot->release = &release_slot;
146 make_slot_name(slot);
147 hotplug_slot->ops = &pciehp_hotplug_slot_ops;
149 get_power_status(hotplug_slot, &info->power_status);
150 get_attention_status(hotplug_slot, &info->attention_status);
151 get_latch_status(hotplug_slot, &info->latch_status);
152 get_adapter_status(hotplug_slot, &info->adapter_status);
154 dbg("Registering bus=%x dev=%x hp_slot=%x sun=%x "
155 "slot_device_offset=%x\n", slot->bus, slot->device,
156 slot->hp_slot, slot->number, ctrl->slot_device_offset);
157 retval = pci_hp_register(hotplug_slot);
159 err ("pci_hp_register failed with error %d\n", retval);
163 list_add(&slot->slot_list, &ctrl->slot_list);
177 static void cleanup_slots(struct controller *ctrl)
179 struct list_head *tmp;
180 struct list_head *next;
183 list_for_each_safe(tmp, next, &ctrl->slot_list) {
184 slot = list_entry(tmp, struct slot, slot_list);
185 list_del(&slot->slot_list);
186 pci_hp_deregister(slot->hotplug_slot);
191 * set_attention_status - Turns the Amber LED for a slot on, off or blink
193 static int set_attention_status(struct hotplug_slot *hotplug_slot, u8 status)
195 struct slot *slot = hotplug_slot->private;
197 dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
199 hotplug_slot->info->attention_status = status;
201 if (ATTN_LED(slot->ctrl->ctrlcap))
202 slot->hpc_ops->set_attention_status(slot, status);
208 static int enable_slot(struct hotplug_slot *hotplug_slot)
210 struct slot *slot = hotplug_slot->private;
212 dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
214 return pciehp_enable_slot(slot);
218 static int disable_slot(struct hotplug_slot *hotplug_slot)
220 struct slot *slot = hotplug_slot->private;
222 dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
224 return pciehp_disable_slot(slot);
227 static int get_power_status(struct hotplug_slot *hotplug_slot, u8 *value)
229 struct slot *slot = hotplug_slot->private;
232 dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
234 retval = slot->hpc_ops->get_power_status(slot, value);
236 *value = hotplug_slot->info->power_status;
241 static int get_attention_status(struct hotplug_slot *hotplug_slot, u8 *value)
243 struct slot *slot = hotplug_slot->private;
246 dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
248 retval = slot->hpc_ops->get_attention_status(slot, value);
250 *value = hotplug_slot->info->attention_status;
255 static int get_latch_status(struct hotplug_slot *hotplug_slot, u8 *value)
257 struct slot *slot = hotplug_slot->private;
260 dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
262 retval = slot->hpc_ops->get_latch_status(slot, value);
264 *value = hotplug_slot->info->latch_status;
269 static int get_adapter_status(struct hotplug_slot *hotplug_slot, u8 *value)
271 struct slot *slot = hotplug_slot->private;
274 dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
276 retval = slot->hpc_ops->get_adapter_status(slot, value);
278 *value = hotplug_slot->info->adapter_status;
283 static int get_address(struct hotplug_slot *hotplug_slot, u32 *value)
285 struct slot *slot = hotplug_slot->private;
286 struct pci_bus *bus = slot->ctrl->pci_dev->subordinate;
288 dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
290 *value = (pci_domain_nr(bus) << 16) | (slot->bus << 8) | slot->device;
295 static int get_max_bus_speed(struct hotplug_slot *hotplug_slot, enum pci_bus_speed *value)
297 struct slot *slot = hotplug_slot->private;
300 dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
302 retval = slot->hpc_ops->get_max_bus_speed(slot, value);
304 *value = PCI_SPEED_UNKNOWN;
309 static int get_cur_bus_speed(struct hotplug_slot *hotplug_slot, enum pci_bus_speed *value)
311 struct slot *slot = hotplug_slot->private;
314 dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
316 retval = slot->hpc_ops->get_cur_bus_speed(slot, value);
318 *value = PCI_SPEED_UNKNOWN;
323 static int pciehp_probe(struct pcie_device *dev, const struct pcie_port_service_id *id)
326 struct controller *ctrl;
329 struct pci_dev *pdev;
331 ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
333 err("%s : out of memory\n", __FUNCTION__);
336 INIT_LIST_HEAD(&ctrl->slot_list);
339 ctrl->pci_dev = pdev;
341 rc = pcie_init(ctrl, dev);
343 dbg("%s: controller initialization failed\n", PCIE_MODULE_NAME);
344 goto err_out_free_ctrl;
347 pci_set_drvdata(pdev, ctrl);
349 ctrl->pci_bus = kmalloc(sizeof(*ctrl->pci_bus), GFP_KERNEL);
350 if (!ctrl->pci_bus) {
351 err("%s: out of memory\n", __FUNCTION__);
353 goto err_out_unmap_mmio_region;
355 memcpy (ctrl->pci_bus, pdev->bus, sizeof (*ctrl->pci_bus));
356 ctrl->bus = pdev->bus->number; /* ctrl bus */
357 ctrl->slot_bus = pdev->subordinate->number; /* bus controlled by this HPC */
359 ctrl->device = PCI_SLOT(pdev->devfn);
360 ctrl->function = PCI_FUNC(pdev->devfn);
361 dbg("%s: ctrl bus=0x%x, device=%x, function=%x, irq=%x\n", __FUNCTION__,
362 ctrl->bus, ctrl->device, ctrl->function, pdev->irq);
364 /* Setup the slot information structures */
365 rc = init_slots(ctrl);
367 err(msg_initialization_err, 6);
368 goto err_out_free_ctrl_slot;
371 t_slot = pciehp_find_slot(ctrl, ctrl->slot_device_offset);
373 /* Finish setting up the hot plug ctrl device */
374 ctrl->next_event = 0;
376 if (!pciehp_ctrl_list) {
377 pciehp_ctrl_list = ctrl;
380 ctrl->next = pciehp_ctrl_list;
381 pciehp_ctrl_list = ctrl;
384 /* Wait for exclusive access to hardware */
385 mutex_lock(&ctrl->ctrl_lock);
387 t_slot->hpc_ops->get_adapter_status(t_slot, &value); /* Check if slot is occupied */
389 if ((POWER_CTRL(ctrl->ctrlcap)) && !value) {
390 rc = t_slot->hpc_ops->power_off_slot(t_slot); /* Power off slot if not occupied*/
392 /* Done with exclusive hardware access */
393 mutex_unlock(&ctrl->ctrl_lock);
394 goto err_out_free_ctrl_slot;
396 /* Wait for the command to complete */
397 wait_for_ctrl_irq (ctrl);
400 /* Done with exclusive hardware access */
401 mutex_unlock(&ctrl->ctrl_lock);
405 err_out_free_ctrl_slot:
407 kfree(ctrl->pci_bus);
408 err_out_unmap_mmio_region:
409 ctrl->hpc_ops->release_ctlr(ctrl);
417 static int pcie_start_thread(void)
421 dbg("Initialize + Start the notification/polling mechanism \n");
423 retval = pciehp_event_start_thread();
425 dbg("pciehp_event_start_thread() failed\n");
432 static void __exit unload_pciehpd(void)
434 struct controller *ctrl;
435 struct controller *tctrl;
437 ctrl = pciehp_ctrl_list;
442 kfree (ctrl->pci_bus);
444 ctrl->hpc_ops->release_ctlr(ctrl);
452 /* Stop the notification mechanism */
453 pciehp_event_stop_thread();
457 static void pciehp_remove (struct pcie_device *device)
459 /* XXX - Needs to be adapted to device driver model */
463 static int pciehp_suspend (struct pcie_device *dev, pm_message_t state)
465 printk("%s ENTRY\n", __FUNCTION__);
469 static int pciehp_resume (struct pcie_device *dev)
471 printk("%s ENTRY\n", __FUNCTION__);
476 static struct pcie_port_service_id port_pci_ids[] = { {
477 .vendor = PCI_ANY_ID,
478 .device = PCI_ANY_ID,
479 .port_type = PCIE_ANY_PORT,
480 .service_type = PCIE_PORT_SERVICE_HP,
482 }, { /* end: all zeroes */ }
484 static const char device_name[] = "hpdriver";
486 static struct pcie_port_service_driver hpdriver_portdrv = {
487 .name = (char *)device_name,
488 .id_table = &port_pci_ids[0],
490 .probe = pciehp_probe,
491 .remove = pciehp_remove,
494 .suspend = pciehp_suspend,
495 .resume = pciehp_resume,
499 static int __init pcied_init(void)
503 #ifdef CONFIG_HOTPLUG_PCI_PCIE_POLL_EVENT_MODE
504 pciehp_poll_mode = 1;
507 retval = pcie_start_thread();
511 retval = pcie_port_service_register(&hpdriver_portdrv);
512 dbg("pcie_port_service_register = %d\n", retval);
513 info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
515 dbg("%s: Failure to register service\n", __FUNCTION__);
519 pciehp_event_stop_thread();
525 static void __exit pcied_cleanup(void)
527 dbg("unload_pciehpd()\n");
530 pcie_port_service_unregister(&hpdriver_portdrv);
532 info(DRIVER_DESC " version: " DRIVER_VERSION " unloaded\n");
535 module_init(pcied_init);
536 module_exit(pcied_cleanup);