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>, <dely.l.sy@intel.com>
30 #include <linux/config.h>
31 #include <linux/module.h>
32 #include <linux/kernel.h>
33 #include <linux/types.h>
34 #include <linux/proc_fs.h>
35 #include <linux/miscdevice.h>
36 #include <linux/slab.h>
37 #include <linux/workqueue.h>
38 #include <linux/pci.h>
39 #include <linux/init.h>
40 #include <asm/uaccess.h>
44 /* Global variables */
48 struct controller *pciehp_ctrl_list; /* = NULL */
49 struct pci_func *pciehp_slot_list[256];
51 #define DRIVER_VERSION "0.4"
52 #define DRIVER_AUTHOR "Dan Zink <dan.zink@compaq.com>, Greg Kroah-Hartman <greg@kroah.com>, Dely Sy <dely.l.sy@intel.com>"
53 #define DRIVER_DESC "PCI Express Hot Plug Controller Driver"
55 MODULE_AUTHOR(DRIVER_AUTHOR);
56 MODULE_DESCRIPTION(DRIVER_DESC);
57 MODULE_LICENSE("GPL");
59 MODULE_PARM(pciehp_debug, "i");
60 MODULE_PARM(pciehp_poll_mode, "i");
61 MODULE_PARM(pciehp_poll_time, "i");
62 MODULE_PARM_DESC(pciehp_debug, "Debugging mode enabled or not");
63 MODULE_PARM_DESC(pciehp_poll_mode, "Using polling mechanism for hot-plug events or not");
64 MODULE_PARM_DESC(pciehp_poll_time, "Polling mechanism frequency, in seconds");
66 #define PCIE_MODULE_NAME "pciehp"
68 static int pcie_start_thread (void);
69 static int set_attention_status (struct hotplug_slot *slot, u8 value);
70 static int enable_slot (struct hotplug_slot *slot);
71 static int disable_slot (struct hotplug_slot *slot);
72 static int hardware_test (struct hotplug_slot *slot, u32 value);
73 static int get_power_status (struct hotplug_slot *slot, u8 *value);
74 static int get_attention_status (struct hotplug_slot *slot, u8 *value);
75 static int get_latch_status (struct hotplug_slot *slot, u8 *value);
76 static int get_adapter_status (struct hotplug_slot *slot, u8 *value);
77 static int get_max_bus_speed (struct hotplug_slot *slot, enum pci_bus_speed *value);
78 static int get_cur_bus_speed (struct hotplug_slot *slot, enum pci_bus_speed *value);
80 static struct hotplug_slot_ops pciehp_hotplug_slot_ops = {
82 .set_attention_status = set_attention_status,
83 .enable_slot = enable_slot,
84 .disable_slot = disable_slot,
85 .hardware_test = hardware_test,
86 .get_power_status = get_power_status,
87 .get_attention_status = get_attention_status,
88 .get_latch_status = get_latch_status,
89 .get_adapter_status = get_adapter_status,
90 .get_max_bus_speed = get_max_bus_speed,
91 .get_cur_bus_speed = get_cur_bus_speed,
94 static int init_slots(struct controller *ctrl)
96 struct slot *new_slot;
102 dbg("%s\n",__FUNCTION__);
104 number_of_slots = ctrl->num_slots;
105 slot_device = ctrl->slot_device_offset;
106 slot_number = ctrl->first_slot;
108 while (number_of_slots) {
109 new_slot = (struct slot *) kmalloc(sizeof(struct slot), GFP_KERNEL);
113 memset(new_slot, 0, sizeof(struct slot));
114 new_slot->hotplug_slot = kmalloc (sizeof (struct hotplug_slot), GFP_KERNEL);
115 if (!new_slot->hotplug_slot) {
119 memset(new_slot->hotplug_slot, 0, sizeof (struct hotplug_slot));
121 new_slot->hotplug_slot->info = kmalloc (sizeof (struct hotplug_slot_info), GFP_KERNEL);
122 if (!new_slot->hotplug_slot->info) {
123 kfree (new_slot->hotplug_slot);
127 memset(new_slot->hotplug_slot->info, 0, sizeof (struct hotplug_slot_info));
128 new_slot->hotplug_slot->name = kmalloc (SLOT_NAME_SIZE, GFP_KERNEL);
129 if (!new_slot->hotplug_slot->name) {
130 kfree (new_slot->hotplug_slot->info);
131 kfree (new_slot->hotplug_slot);
136 new_slot->magic = SLOT_MAGIC;
137 new_slot->ctrl = ctrl;
138 new_slot->bus = ctrl->slot_bus;
139 new_slot->device = slot_device;
140 new_slot->hpc_ops = ctrl->hpc_ops;
142 new_slot->number = ctrl->first_slot;
143 new_slot->hp_slot = slot_device - ctrl->slot_device_offset;
145 /* register this slot with the hotplug pci core */
146 new_slot->hotplug_slot->private = new_slot;
147 make_slot_name (new_slot->hotplug_slot->name, SLOT_NAME_SIZE, new_slot);
148 new_slot->hotplug_slot->ops = &pciehp_hotplug_slot_ops;
150 new_slot->hpc_ops->get_power_status(new_slot, &(new_slot->hotplug_slot->info->power_status));
151 new_slot->hpc_ops->get_attention_status(new_slot, &(new_slot->hotplug_slot->info->attention_status));
152 new_slot->hpc_ops->get_latch_status(new_slot, &(new_slot->hotplug_slot->info->latch_status));
153 new_slot->hpc_ops->get_adapter_status(new_slot, &(new_slot->hotplug_slot->info->adapter_status));
155 dbg("Registering bus=%x dev=%x hp_slot=%x sun=%x slot_device_offset=%x\n",
156 new_slot->bus, new_slot->device, new_slot->hp_slot, new_slot->number, ctrl->slot_device_offset);
157 result = pci_hp_register (new_slot->hotplug_slot);
159 err ("pci_hp_register failed with error %d\n", result);
160 kfree (new_slot->hotplug_slot->info);
161 kfree (new_slot->hotplug_slot->name);
162 kfree (new_slot->hotplug_slot);
167 new_slot->next = ctrl->slot;
168 ctrl->slot = new_slot;
172 slot_number += ctrl->slot_num_inc;
179 static int cleanup_slots (struct controller * ctrl)
181 struct slot *old_slot, *next_slot;
183 old_slot = ctrl->slot;
187 next_slot = old_slot->next;
188 pci_hp_deregister (old_slot->hotplug_slot);
189 kfree(old_slot->hotplug_slot->info);
190 kfree(old_slot->hotplug_slot->name);
191 kfree(old_slot->hotplug_slot);
193 old_slot = next_slot;
200 static int get_ctlr_slot_config(struct controller *ctrl)
202 int num_ctlr_slots; /* Not needed; PCI Express has 1 slot per port*/
203 int first_device_num; /* Not needed */
204 int physical_slot_num;
205 int updown; /* Not needed */
207 int flags; /* Not needed */
209 rc = pcie_get_ctlr_slot_config(ctrl, &num_ctlr_slots, &first_device_num, &physical_slot_num, &updown, &flags);
211 err("%s: get_ctlr_slot_config fail for b:d (%x:%x)\n", __FUNCTION__, ctrl->bus, ctrl->device);
215 ctrl->num_slots = num_ctlr_slots; /* PCI Express has 1 slot per port */
216 ctrl->slot_device_offset = first_device_num;
217 ctrl->first_slot = physical_slot_num;
218 ctrl->slot_num_inc = updown; /* Not needed */ /* either -1 or 1 */
220 dbg("%s: bus(0x%x) num_slot(0x%x) 1st_dev(0x%x) psn(0x%x) updown(%d) for b:d (%x:%x)\n",
221 __FUNCTION__, ctrl->slot_bus, num_ctlr_slots, first_device_num, physical_slot_num, updown,
222 ctrl->bus, ctrl->device);
229 * set_attention_status - Turns the Amber LED for a slot on, off or blink
231 static int set_attention_status (struct hotplug_slot *hotplug_slot, u8 status)
233 struct slot *slot = get_slot (hotplug_slot, __FUNCTION__);
238 dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
240 hotplug_slot->info->attention_status = status;
241 slot->hpc_ops->set_attention_status(slot, status);
247 static int enable_slot (struct hotplug_slot *hotplug_slot)
249 struct slot *slot = get_slot (hotplug_slot, __FUNCTION__);
254 dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
256 return pciehp_enable_slot(slot);
260 static int disable_slot (struct hotplug_slot *hotplug_slot)
262 struct slot *slot = get_slot (hotplug_slot, __FUNCTION__);
267 dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
269 return pciehp_disable_slot(slot);
273 static int hardware_test (struct hotplug_slot *hotplug_slot, u32 value)
279 static int get_power_status (struct hotplug_slot *hotplug_slot, u8 *value)
281 struct slot *slot = get_slot (hotplug_slot, __FUNCTION__);
287 dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
289 retval = slot->hpc_ops->get_power_status(slot, value);
291 *value = hotplug_slot->info->power_status;
296 static int get_attention_status (struct hotplug_slot *hotplug_slot, u8 *value)
298 struct slot *slot = get_slot (hotplug_slot, __FUNCTION__);
304 dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
306 retval = slot->hpc_ops->get_attention_status(slot, value);
308 *value = hotplug_slot->info->attention_status;
313 static int get_latch_status (struct hotplug_slot *hotplug_slot, u8 *value)
315 struct slot *slot = get_slot (hotplug_slot, __FUNCTION__);
321 dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
323 retval = slot->hpc_ops->get_latch_status(slot, value);
325 *value = hotplug_slot->info->latch_status;
330 static int get_adapter_status (struct hotplug_slot *hotplug_slot, u8 *value)
332 struct slot *slot = get_slot (hotplug_slot, __FUNCTION__);
338 dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
340 retval = slot->hpc_ops->get_adapter_status(slot, value);
343 *value = hotplug_slot->info->adapter_status;
348 static int get_max_bus_speed (struct hotplug_slot *hotplug_slot, enum pci_bus_speed *value)
350 struct slot *slot = get_slot (hotplug_slot, __FUNCTION__);
356 dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
358 retval = slot->hpc_ops->get_max_bus_speed(slot, value);
360 *value = PCI_SPEED_UNKNOWN;
365 static int get_cur_bus_speed (struct hotplug_slot *hotplug_slot, enum pci_bus_speed *value)
367 struct slot *slot = get_slot (hotplug_slot, __FUNCTION__);
373 dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
375 retval = slot->hpc_ops->get_cur_bus_speed(slot, value);
377 *value = PCI_SPEED_UNKNOWN;
382 static int pcie_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
385 struct controller *ctrl;
387 int first_device_num = 0 ; /* first PCI device number supported by this PCIE */
388 int num_ctlr_slots; /* number of slots supported by this HPC */
391 ctrl = (struct controller *) kmalloc(sizeof(struct controller), GFP_KERNEL);
393 err("%s : out of memory\n", __FUNCTION__);
396 memset(ctrl, 0, sizeof(struct controller));
398 dbg("%s: DRV_thread pid = %d\n", __FUNCTION__, current->pid);
400 rc = pcie_init(ctrl, pdev,
401 (php_intr_callback_t) pciehp_handle_attention_button,
402 (php_intr_callback_t) pciehp_handle_switch_change,
403 (php_intr_callback_t) pciehp_handle_presence_change,
404 (php_intr_callback_t) pciehp_handle_power_fault);
406 dbg("%s: controller initialization failed\n", PCIE_MODULE_NAME);
407 goto err_out_free_ctrl;
410 ctrl->pci_dev = pdev;
412 pci_set_drvdata(pdev, ctrl);
414 ctrl->pci_bus = kmalloc (sizeof (*ctrl->pci_bus), GFP_KERNEL);
415 if (!ctrl->pci_bus) {
416 err("%s: out of memory\n", __FUNCTION__);
418 goto err_out_unmap_mmio_region;
420 dbg("%s: ctrl->pci_bus %p\n", __FUNCTION__, ctrl->pci_bus);
421 memcpy (ctrl->pci_bus, pdev->bus, sizeof (*ctrl->pci_bus));
422 ctrl->bus = pdev->bus->number; /* ctrl bus */
423 ctrl->slot_bus = pdev->subordinate->number; /* bus controlled by this HPC */
425 ctrl->device = PCI_SLOT(pdev->devfn);
426 ctrl->function = PCI_FUNC(pdev->devfn);
427 dbg("%s: ctrl bus=0x%x, device=%x, function=%x, irq=%x\n", __FUNCTION__,
428 ctrl->bus, ctrl->device, ctrl->function, pdev->irq);
431 * Save configuration headers for this and subordinate PCI buses
434 rc = get_ctlr_slot_config(ctrl);
436 err(msg_initialization_err, rc);
437 goto err_out_free_ctrl_bus;
439 first_device_num = ctrl->slot_device_offset;
440 num_ctlr_slots = ctrl->num_slots;
442 /* Store PCI Config Space for all devices on this bus */
443 dbg("%s: Before calling pciehp_save_config, ctrl->bus %x,ctrl->slot_bus %x\n",
444 __FUNCTION__,ctrl->bus, ctrl->slot_bus);
445 rc = pciehp_save_config(ctrl, ctrl->slot_bus, num_ctlr_slots, first_device_num);
447 err("%s: unable to save PCI configuration data, error %d\n", __FUNCTION__, rc);
448 goto err_out_free_ctrl_bus;
451 /* Get IO, memory, and IRQ resources for new devices */
452 rc = pciehprm_find_available_resources(ctrl);
453 ctrl->add_support = !rc;
456 dbg("pciehprm_find_available_resources = %#x\n", rc);
457 err("unable to locate PCI configuration resources for hot plug add.\n");
458 goto err_out_free_ctrl_bus;
461 /* Setup the slot information structures */
462 rc = init_slots(ctrl);
464 err(msg_initialization_err, 6);
465 goto err_out_free_ctrl_slot;
468 t_slot = pciehp_find_slot(ctrl, first_device_num);
469 dbg("%s: t_slot %p\n", __FUNCTION__, t_slot);
471 /* Finish setting up the hot plug ctrl device */
472 ctrl->next_event = 0;
474 if (!pciehp_ctrl_list) {
475 pciehp_ctrl_list = ctrl;
478 ctrl->next = pciehp_ctrl_list;
479 pciehp_ctrl_list = ctrl;
482 /* Wait for exclusive access to hardware */
483 down(&ctrl->crit_sect);
485 t_slot->hpc_ops->get_adapter_status(t_slot, &value); /* Check if slot is occupied */
486 dbg("%s: adpater value %x\n", __FUNCTION__, value);
488 rc = t_slot->hpc_ops->power_off_slot(t_slot); /* Power off slot if not occupied*/
490 /* Done with exclusive hardware access */
491 up(&ctrl->crit_sect);
492 goto err_out_free_ctrl_slot;
494 /* Wait for the command to complete */
495 wait_for_ctrl_irq (ctrl);
498 /* Done with exclusive hardware access */
499 up(&ctrl->crit_sect);
503 err_out_free_ctrl_slot:
505 err_out_free_ctrl_bus:
506 kfree(ctrl->pci_bus);
507 err_out_unmap_mmio_region:
508 ctrl->hpc_ops->release_ctlr(ctrl);
516 static int pcie_start_thread(void)
521 dbg("Initialize + Start the notification/polling mechanism \n");
523 retval = pciehp_event_start_thread();
525 dbg("pciehp_event_start_thread() failed\n");
529 dbg("Initialize slot lists\n");
530 /* One slot list for each bus in the system */
531 for (loop = 0; loop < 256; loop++) {
532 pciehp_slot_list[loop] = NULL;
539 static void unload_pciehpd(void)
541 struct pci_func *next;
542 struct pci_func *TempSlot;
544 struct controller *ctrl;
545 struct controller *tctrl;
546 struct pci_resource *res;
547 struct pci_resource *tres;
549 ctrl = pciehp_ctrl_list;
561 res = ctrl->mem_head;
568 res = ctrl->p_mem_head;
575 res = ctrl->bus_head;
582 kfree (ctrl->pci_bus);
584 ctrl->hpc_ops->release_ctlr(ctrl);
592 for (loop = 0; loop < 256; loop++) {
593 next = pciehp_slot_list[loop];
594 while (next != NULL) {
602 res = next->mem_head;
609 res = next->p_mem_head;
616 res = next->bus_head;
629 /* Stop the notification mechanism */
630 pciehp_event_stop_thread();
635 static struct pci_device_id pcied_pci_tbl[] = {
637 .class = ((PCI_CLASS_BRIDGE_PCI << 8) | 0x00),
639 .vendor = PCI_ANY_ID,
640 .device = PCI_ANY_ID,
641 .subvendor = PCI_ANY_ID,
642 .subdevice = PCI_ANY_ID,
645 { /* end: all zeroes */ }
648 MODULE_DEVICE_TABLE(pci, pcied_pci_tbl);
652 static struct pci_driver pcie_driver = {
653 .name = PCIE_MODULE_NAME,
654 .id_table = pcied_pci_tbl,
656 /* remove: pcie_remove_one, */
661 static int __init pcied_init(void)
665 #ifdef CONFIG_HOTPLUG_PCI_PCIE_POLL_EVENT_MODE
666 pciehp_poll_mode = 1;
669 retval = pcie_start_thread();
673 retval = pciehprm_init(PCI);
675 retval = pci_module_init(&pcie_driver);
676 dbg("pci_module_init = %d\n", retval);
677 info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
683 pciehp_event_stop_thread();
685 pciehprm_print_pirt();
690 static void __exit pcied_cleanup(void)
692 dbg("unload_pciehpd()\n");
697 dbg("pci_unregister_driver\n");
698 pci_unregister_driver(&pcie_driver);
700 info(DRIVER_DESC " version: " DRIVER_VERSION " unloaded\n");
704 module_init(pcied_init);
705 module_exit(pcied_cleanup);