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_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 dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
98 kfree(slot->hotplug_slot->info);
99 kfree(slot->hotplug_slot->name);
100 kfree(slot->hotplug_slot);
104 static int init_slots(struct controller *ctrl)
107 struct hpc_ops *hpc_ops;
108 struct hotplug_slot *hotplug_slot;
109 struct hotplug_slot_info *hotplug_slot_info;
113 int result = -ENOMEM;
115 number_of_slots = ctrl->num_slots;
116 slot_device = ctrl->slot_device_offset;
117 slot_number = ctrl->first_slot;
119 while (number_of_slots) {
120 slot = kmalloc(sizeof(*slot), GFP_KERNEL);
124 memset(slot, 0, sizeof(struct slot));
126 kmalloc(sizeof(*(slot->hotplug_slot)),
128 if (!slot->hotplug_slot)
130 hotplug_slot = slot->hotplug_slot;
131 memset(hotplug_slot, 0, sizeof(struct hotplug_slot));
134 kmalloc(sizeof(*(hotplug_slot->info)),
136 if (!hotplug_slot->info)
138 hotplug_slot_info = hotplug_slot->info;
139 memset(hotplug_slot_info, 0,
140 sizeof(struct hotplug_slot_info));
141 hotplug_slot->name = kmalloc(SLOT_NAME_SIZE, GFP_KERNEL);
142 if (!hotplug_slot->name)
146 slot->bus = ctrl->slot_bus;
147 slot->device = slot_device;
148 slot->hpc_ops = hpc_ops = ctrl->hpc_ops;
150 slot->number = ctrl->first_slot;
151 slot->hp_slot = slot_device - ctrl->slot_device_offset;
153 /* register this slot with the hotplug pci core */
154 hotplug_slot->private = slot;
155 hotplug_slot->release = &release_slot;
156 make_slot_name(hotplug_slot->name, SLOT_NAME_SIZE, slot);
157 hotplug_slot->ops = &pciehp_hotplug_slot_ops;
159 hpc_ops->get_power_status(slot,
160 &(hotplug_slot_info->power_status));
161 hpc_ops->get_attention_status(slot,
162 &(hotplug_slot_info->attention_status));
163 hpc_ops->get_latch_status(slot,
164 &(hotplug_slot_info->latch_status));
165 hpc_ops->get_adapter_status(slot,
166 &(hotplug_slot_info->adapter_status));
168 dbg("Registering bus=%x dev=%x hp_slot=%x sun=%x "
169 "slot_device_offset=%x\n",
170 slot->bus, slot->device, slot->hp_slot, slot->number,
171 ctrl->slot_device_offset);
172 result = pci_hp_register(hotplug_slot);
174 err ("pci_hp_register failed with error %d\n", result);
178 slot->next = ctrl->slot;
183 slot_number += ctrl->slot_num_inc;
189 kfree(hotplug_slot->name);
191 kfree(hotplug_slot_info);
201 static int cleanup_slots (struct controller * ctrl)
203 struct slot *old_slot, *next_slot;
205 old_slot = ctrl->slot;
209 next_slot = old_slot->next;
210 pci_hp_deregister (old_slot->hotplug_slot);
211 old_slot = next_slot;
218 static int get_ctlr_slot_config(struct controller *ctrl)
220 int num_ctlr_slots; /* Not needed; PCI Express has 1 slot per port*/
221 int first_device_num; /* Not needed */
222 int physical_slot_num;
226 rc = pcie_get_ctlr_slot_config(ctrl, &num_ctlr_slots, &first_device_num, &physical_slot_num, &ctrlcap);
228 err("%s: get_ctlr_slot_config fail for b:d (%x:%x)\n", __FUNCTION__, ctrl->bus, ctrl->device);
232 ctrl->num_slots = num_ctlr_slots; /* PCI Express has 1 slot per port */
233 ctrl->slot_device_offset = first_device_num;
234 ctrl->first_slot = physical_slot_num;
235 ctrl->ctrlcap = ctrlcap;
237 dbg("%s: bus(0x%x) num_slot(0x%x) 1st_dev(0x%x) psn(0x%x) ctrlcap(%x) for b:d (%x:%x)\n",
238 __FUNCTION__, ctrl->slot_bus, num_ctlr_slots, first_device_num, physical_slot_num, ctrlcap,
239 ctrl->bus, ctrl->device);
246 * set_attention_status - Turns the Amber LED for a slot on, off or blink
248 static int set_attention_status(struct hotplug_slot *hotplug_slot, u8 status)
250 struct slot *slot = hotplug_slot->private;
252 dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
254 hotplug_slot->info->attention_status = status;
256 if (ATTN_LED(slot->ctrl->ctrlcap))
257 slot->hpc_ops->set_attention_status(slot, status);
263 static int enable_slot(struct hotplug_slot *hotplug_slot)
265 struct slot *slot = hotplug_slot->private;
267 dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
269 return pciehp_enable_slot(slot);
273 static int disable_slot(struct hotplug_slot *hotplug_slot)
275 struct slot *slot = hotplug_slot->private;
277 dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
279 return pciehp_disable_slot(slot);
282 static int get_power_status(struct hotplug_slot *hotplug_slot, u8 *value)
284 struct slot *slot = hotplug_slot->private;
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 = hotplug_slot->private;
301 dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
303 retval = slot->hpc_ops->get_attention_status(slot, value);
305 *value = hotplug_slot->info->attention_status;
310 static int get_latch_status(struct hotplug_slot *hotplug_slot, u8 *value)
312 struct slot *slot = hotplug_slot->private;
315 dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
317 retval = slot->hpc_ops->get_latch_status(slot, value);
319 *value = hotplug_slot->info->latch_status;
324 static int get_adapter_status(struct hotplug_slot *hotplug_slot, u8 *value)
326 struct slot *slot = hotplug_slot->private;
329 dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
331 retval = slot->hpc_ops->get_adapter_status(slot, value);
333 *value = hotplug_slot->info->adapter_status;
338 static int get_max_bus_speed(struct hotplug_slot *hotplug_slot, enum pci_bus_speed *value)
340 struct slot *slot = hotplug_slot->private;
343 dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
345 retval = slot->hpc_ops->get_max_bus_speed(slot, value);
347 *value = PCI_SPEED_UNKNOWN;
352 static int get_cur_bus_speed(struct hotplug_slot *hotplug_slot, enum pci_bus_speed *value)
354 struct slot *slot = hotplug_slot->private;
357 dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
359 retval = slot->hpc_ops->get_cur_bus_speed(slot, value);
361 *value = PCI_SPEED_UNKNOWN;
366 static int pciehp_probe(struct pcie_device *dev, const struct pcie_port_service_id *id)
369 struct controller *ctrl;
371 int first_device_num = 0 ; /* first PCI device number supported by this PCIE */
372 int num_ctlr_slots; /* number of slots supported by this HPC */
374 struct pci_dev *pdev;
376 ctrl = kmalloc(sizeof(*ctrl), GFP_KERNEL);
378 err("%s : out of memory\n", __FUNCTION__);
381 memset(ctrl, 0, sizeof(struct controller));
384 ctrl->pci_dev = pdev;
386 rc = pcie_init(ctrl, dev);
388 dbg("%s: controller initialization failed\n", PCIE_MODULE_NAME);
389 goto err_out_free_ctrl;
392 pci_set_drvdata(pdev, ctrl);
394 ctrl->pci_bus = kmalloc(sizeof(*ctrl->pci_bus), GFP_KERNEL);
395 if (!ctrl->pci_bus) {
396 err("%s: out of memory\n", __FUNCTION__);
398 goto err_out_unmap_mmio_region;
400 memcpy (ctrl->pci_bus, pdev->bus, sizeof (*ctrl->pci_bus));
401 ctrl->bus = pdev->bus->number; /* ctrl bus */
402 ctrl->slot_bus = pdev->subordinate->number; /* bus controlled by this HPC */
404 ctrl->device = PCI_SLOT(pdev->devfn);
405 ctrl->function = PCI_FUNC(pdev->devfn);
406 dbg("%s: ctrl bus=0x%x, device=%x, function=%x, irq=%x\n", __FUNCTION__,
407 ctrl->bus, ctrl->device, ctrl->function, pdev->irq);
410 * Save configuration headers for this and subordinate PCI buses
413 rc = get_ctlr_slot_config(ctrl);
415 err(msg_initialization_err, rc);
416 goto err_out_free_ctrl_bus;
418 first_device_num = ctrl->slot_device_offset;
419 num_ctlr_slots = ctrl->num_slots;
421 /* Setup the slot information structures */
422 rc = init_slots(ctrl);
424 err(msg_initialization_err, 6);
425 goto err_out_free_ctrl_slot;
428 t_slot = pciehp_find_slot(ctrl, first_device_num);
430 /* Finish setting up the hot plug ctrl device */
431 ctrl->next_event = 0;
433 if (!pciehp_ctrl_list) {
434 pciehp_ctrl_list = ctrl;
437 ctrl->next = pciehp_ctrl_list;
438 pciehp_ctrl_list = ctrl;
441 /* Wait for exclusive access to hardware */
442 down(&ctrl->crit_sect);
444 t_slot->hpc_ops->get_adapter_status(t_slot, &value); /* Check if slot is occupied */
446 if ((POWER_CTRL(ctrl->ctrlcap)) && !value) {
447 rc = t_slot->hpc_ops->power_off_slot(t_slot); /* Power off slot if not occupied*/
449 /* Done with exclusive hardware access */
450 up(&ctrl->crit_sect);
451 goto err_out_free_ctrl_slot;
453 /* Wait for the command to complete */
454 wait_for_ctrl_irq (ctrl);
457 /* Done with exclusive hardware access */
458 up(&ctrl->crit_sect);
462 err_out_free_ctrl_slot:
464 err_out_free_ctrl_bus:
465 kfree(ctrl->pci_bus);
466 err_out_unmap_mmio_region:
467 ctrl->hpc_ops->release_ctlr(ctrl);
475 static int pcie_start_thread(void)
479 dbg("Initialize + Start the notification/polling mechanism \n");
481 retval = pciehp_event_start_thread();
483 dbg("pciehp_event_start_thread() failed\n");
490 static void __exit unload_pciehpd(void)
492 struct controller *ctrl;
493 struct controller *tctrl;
495 ctrl = pciehp_ctrl_list;
500 kfree (ctrl->pci_bus);
502 ctrl->hpc_ops->release_ctlr(ctrl);
510 /* Stop the notification mechanism */
511 pciehp_event_stop_thread();
515 static int hpdriver_context = 0;
517 static void pciehp_remove (struct pcie_device *device)
519 printk("%s ENTRY\n", __FUNCTION__);
520 printk("%s -> Call free_irq for irq = %d\n",
521 __FUNCTION__, device->irq);
522 free_irq(device->irq, &hpdriver_context);
526 static int pciehp_suspend (struct pcie_device *dev, pm_message_t state)
528 printk("%s ENTRY\n", __FUNCTION__);
532 static int pciehp_resume (struct pcie_device *dev)
534 printk("%s ENTRY\n", __FUNCTION__);
539 static struct pcie_port_service_id port_pci_ids[] = { {
540 .vendor = PCI_ANY_ID,
541 .device = PCI_ANY_ID,
542 .port_type = PCIE_ANY_PORT,
543 .service_type = PCIE_PORT_SERVICE_HP,
545 }, { /* end: all zeroes */ }
547 static const char device_name[] = "hpdriver";
549 static struct pcie_port_service_driver hpdriver_portdrv = {
550 .name = (char *)device_name,
551 .id_table = &port_pci_ids[0],
553 .probe = pciehp_probe,
554 .remove = pciehp_remove,
557 .suspend = pciehp_suspend,
558 .resume = pciehp_resume,
562 static int __init pcied_init(void)
566 #ifdef CONFIG_HOTPLUG_PCI_PCIE_POLL_EVENT_MODE
567 pciehp_poll_mode = 1;
570 retval = pcie_start_thread();
574 retval = pcie_port_service_register(&hpdriver_portdrv);
575 dbg("pcie_port_service_register = %d\n", retval);
576 info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
578 dbg("%s: Failure to register service\n", __FUNCTION__);
582 pciehp_event_stop_thread();
588 static void __exit pcied_cleanup(void)
590 dbg("unload_pciehpd()\n");
593 pcie_port_service_unregister(&hpdriver_portdrv);
595 info(DRIVER_DESC " version: " DRIVER_VERSION " unloaded\n");
598 module_init(pcied_init);
599 module_exit(pcied_cleanup);