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
= {
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
,
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
);
102 static int init_slots(struct controller
*ctrl
)
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
);
115 info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
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
,
137 ctrl_err(ctrl
, "pci_hp_register failed with error %d\n",
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
);
156 static void cleanup_slots(struct controller
*ctrl
)
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
);
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;
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
);
213 *value
= hotplug_slot
->info
->power_status
;
218 static int get_attention_status(struct hotplug_slot
*hotplug_slot
, u8
*value
)
220 struct slot
*slot
= hotplug_slot
->private;
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
);
228 *value
= hotplug_slot
->info
->attention_status
;
233 static int get_latch_status(struct hotplug_slot
*hotplug_slot
, u8
*value
)
235 struct slot
*slot
= hotplug_slot
->private;
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
);
243 *value
= hotplug_slot
->info
->latch_status
;
248 static int get_adapter_status(struct hotplug_slot
*hotplug_slot
, u8
*value
)
250 struct slot
*slot
= hotplug_slot
->private;
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
);
258 *value
= hotplug_slot
->info
->adapter_status
;
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;
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
);
274 *value
= PCI_SPEED_UNKNOWN
;
279 static int get_cur_bus_speed(struct hotplug_slot
*hotplug_slot
, enum pci_bus_speed
*value
)
281 struct slot
*slot
= hotplug_slot
->private;
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
);
289 *value
= PCI_SPEED_UNKNOWN
;
294 static int pciehp_probe(struct pcie_device
*dev
)
297 struct controller
*ctrl
;
300 struct pci_dev
*pdev
= dev
->port
;
303 dev_info(&dev
->device
,
304 "Bypassing BIOS check for pciehp use on %s\n",
306 else if (pciehp_get_hp_hw_control_from_firmware(pdev
))
309 ctrl
= pcie_init(dev
);
311 dev_err(&dev
->device
, "Controller initialization failed\n");
314 set_service_data(dev
, ctrl
);
316 /* Setup the slot information structures */
317 rc
= init_slots(ctrl
);
320 ctrl_warn(ctrl
, "Slot already registered by another "
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
);
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
);
339 pciehp_enable_slot(t_slot
);
341 /* Power off slot if not occupied */
342 if (POWER_CTRL(ctrl
)) {
343 rc
= t_slot
->hpc_ops
->power_off_slot(t_slot
);
345 goto err_out_free_ctrl_slot
;
351 err_out_free_ctrl_slot
:
353 err_out_release_ctlr
:
354 ctrl
->hpc_ops
->release_ctlr(ctrl
);
359 static void pciehp_remove (struct pcie_device
*dev
)
361 struct controller
*ctrl
= get_service_data(dev
);
364 ctrl
->hpc_ops
->release_ctlr(ctrl
);
368 static int pciehp_suspend (struct pcie_device
*dev
)
370 dev_info(&dev
->device
, "%s ENTRY\n", __func__
);
374 static int pciehp_resume (struct pcie_device
*dev
)
376 dev_info(&dev
->device
, "%s ENTRY\n", __func__
);
378 struct controller
*ctrl
= get_service_data(dev
);
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
);
390 pciehp_enable_slot(t_slot
);
392 pciehp_disable_slot(t_slot
);
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
,
407 .suspend
= pciehp_suspend
,
408 .resume
= pciehp_resume
,
412 static int __init
pcied_init(void)
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");
421 dbg("Failure to register service\n");
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
);