Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * PCI Express Hot Plug Controller Driver | |
3 | * | |
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 | |
8 | * | |
9 | * All rights reserved. | |
10 | * | |
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. | |
15 | * | |
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 | |
20 | * details. | |
21 | * | |
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. | |
25 | * | |
8cf4c195 | 26 | * Send feedback to <greg@kroah.com>, <kristen.c.accardi@intel.com> |
1da177e4 LT |
27 | * |
28 | */ | |
29 | ||
1da177e4 LT |
30 | #include <linux/module.h> |
31 | #include <linux/moduleparam.h> | |
32 | #include <linux/kernel.h> | |
5a0e3ad6 | 33 | #include <linux/slab.h> |
1da177e4 | 34 | #include <linux/types.h> |
1da177e4 | 35 | #include <linux/pci.h> |
1da177e4 | 36 | #include "pciehp.h" |
1da177e4 | 37 | #include <linux/interrupt.h> |
34d03419 | 38 | #include <linux/time.h> |
1da177e4 LT |
39 | |
40 | /* Global variables */ | |
90ab5ee9 RR |
41 | bool pciehp_debug; |
42 | bool pciehp_poll_mode; | |
1da177e4 | 43 | int pciehp_poll_time; |
90ab5ee9 | 44 | bool pciehp_force; |
5d386e1a | 45 | struct workqueue_struct *pciehp_wq; |
1da177e4 LT |
46 | |
47 | #define DRIVER_VERSION "0.4" | |
48 | #define DRIVER_AUTHOR "Dan Zink <dan.zink@compaq.com>, Greg Kroah-Hartman <greg@kroah.com>, Dely Sy <dely.l.sy@intel.com>" | |
49 | #define DRIVER_DESC "PCI Express Hot Plug Controller Driver" | |
50 | ||
51 | MODULE_AUTHOR(DRIVER_AUTHOR); | |
52 | MODULE_DESCRIPTION(DRIVER_DESC); | |
53 | MODULE_LICENSE("GPL"); | |
54 | ||
55 | module_param(pciehp_debug, bool, 0644); | |
56 | module_param(pciehp_poll_mode, bool, 0644); | |
57 | module_param(pciehp_poll_time, int, 0644); | |
a3a45ec8 | 58 | module_param(pciehp_force, bool, 0644); |
1da177e4 LT |
59 | MODULE_PARM_DESC(pciehp_debug, "Debugging mode enabled or not"); |
60 | MODULE_PARM_DESC(pciehp_poll_mode, "Using polling mechanism for hot-plug events or not"); | |
61 | MODULE_PARM_DESC(pciehp_poll_time, "Polling mechanism frequency, in seconds"); | |
28eb5f27 | 62 | MODULE_PARM_DESC(pciehp_force, "Force pciehp, even if OSHP is missing"); |
1da177e4 LT |
63 | |
64 | #define PCIE_MODULE_NAME "pciehp" | |
65 | ||
1da177e4 LT |
66 | static int set_attention_status (struct hotplug_slot *slot, u8 value); |
67 | static int enable_slot (struct hotplug_slot *slot); | |
68 | static int disable_slot (struct hotplug_slot *slot); | |
69 | static int get_power_status (struct hotplug_slot *slot, u8 *value); | |
70 | static int get_attention_status (struct hotplug_slot *slot, u8 *value); | |
71 | static int get_latch_status (struct hotplug_slot *slot, u8 *value); | |
72 | static int get_adapter_status (struct hotplug_slot *slot, u8 *value); | |
1da177e4 | 73 | |
b308240b DS |
74 | /** |
75 | * release_slot - free up the memory used by a slot | |
76 | * @hotplug_slot: slot to free | |
77 | */ | |
78 | static void release_slot(struct hotplug_slot *hotplug_slot) | |
79 | { | |
7f2feec1 TI |
80 | struct slot *slot = hotplug_slot->private; |
81 | ||
18b341b7 | 82 | ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n", |
e1acb24f | 83 | __func__, hotplug_slot_name(hotplug_slot)); |
b308240b | 84 | |
586f1d66 | 85 | kfree(hotplug_slot->ops); |
c4635eb0 KK |
86 | kfree(hotplug_slot->info); |
87 | kfree(hotplug_slot); | |
a0b17257 KK |
88 | } |
89 | ||
8720d27d | 90 | static int init_slot(struct controller *ctrl) |
1da177e4 | 91 | { |
8720d27d KK |
92 | struct slot *slot = ctrl->slot; |
93 | struct hotplug_slot *hotplug = NULL; | |
94 | struct hotplug_slot_info *info = NULL; | |
586f1d66 | 95 | struct hotplug_slot_ops *ops = NULL; |
e1acb24f | 96 | char name[SLOT_NAME_SIZE]; |
a0b17257 | 97 | int retval = -ENOMEM; |
1da177e4 | 98 | |
8720d27d KK |
99 | hotplug = kzalloc(sizeof(*hotplug), GFP_KERNEL); |
100 | if (!hotplug) | |
101 | goto out; | |
102 | ||
103 | info = kzalloc(sizeof(*info), GFP_KERNEL); | |
104 | if (!info) | |
105 | goto out; | |
106 | ||
586f1d66 KK |
107 | /* Setup hotplug slot ops */ |
108 | ops = kzalloc(sizeof(*ops), GFP_KERNEL); | |
109 | if (!ops) | |
110 | goto out; | |
111 | ops->enable_slot = enable_slot; | |
112 | ops->disable_slot = disable_slot; | |
113 | ops->get_power_status = get_power_status; | |
114 | ops->get_adapter_status = get_adapter_status; | |
586f1d66 KK |
115 | if (MRL_SENS(ctrl)) |
116 | ops->get_latch_status = get_latch_status; | |
117 | if (ATTN_LED(ctrl)) { | |
118 | ops->get_attention_status = get_attention_status; | |
119 | ops->set_attention_status = set_attention_status; | |
120 | } | |
121 | ||
8720d27d KK |
122 | /* register this slot with the hotplug pci core */ |
123 | hotplug->info = info; | |
124 | hotplug->private = slot; | |
125 | hotplug->release = &release_slot; | |
586f1d66 | 126 | hotplug->ops = ops; |
8720d27d | 127 | slot->hotplug_slot = hotplug; |
07a09694 | 128 | snprintf(name, SLOT_NAME_SIZE, "%u", PSN(ctrl)); |
8720d27d | 129 | |
a2359a33 | 130 | ctrl_dbg(ctrl, "Registering domain:bus:dev=%04x:%02x:00 sun=%x\n", |
385e2491 | 131 | pci_domain_nr(ctrl->pcie->port->subordinate), |
07a09694 | 132 | ctrl->pcie->port->subordinate->number, PSN(ctrl)); |
8720d27d | 133 | retval = pci_hp_register(hotplug, |
385e2491 | 134 | ctrl->pcie->port->subordinate, 0, name); |
445f7985 | 135 | if (retval) |
8720d27d KK |
136 | ctrl_err(ctrl, |
137 | "pci_hp_register failed with error %d\n", retval); | |
8720d27d KK |
138 | out: |
139 | if (retval) { | |
586f1d66 | 140 | kfree(ops); |
8720d27d KK |
141 | kfree(info); |
142 | kfree(hotplug); | |
1da177e4 | 143 | } |
a0b17257 | 144 | return retval; |
1da177e4 LT |
145 | } |
146 | ||
8720d27d | 147 | static void cleanup_slot(struct controller *ctrl) |
1da177e4 | 148 | { |
8720d27d | 149 | pci_hp_deregister(ctrl->slot->hotplug_slot); |
1da177e4 LT |
150 | } |
151 | ||
1da177e4 LT |
152 | /* |
153 | * set_attention_status - Turns the Amber LED for a slot on, off or blink | |
154 | */ | |
155 | static int set_attention_status(struct hotplug_slot *hotplug_slot, u8 status) | |
156 | { | |
157 | struct slot *slot = hotplug_slot->private; | |
158 | ||
18b341b7 | 159 | ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n", |
e1acb24f | 160 | __func__, slot_name(slot)); |
1da177e4 | 161 | |
445f7985 | 162 | return pciehp_set_attention_status(slot, status); |
1da177e4 LT |
163 | } |
164 | ||
165 | ||
166 | static int enable_slot(struct hotplug_slot *hotplug_slot) | |
167 | { | |
168 | struct slot *slot = hotplug_slot->private; | |
169 | ||
18b341b7 | 170 | ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n", |
e1acb24f | 171 | __func__, slot_name(slot)); |
1da177e4 | 172 | |
5d386e1a | 173 | return pciehp_sysfs_enable_slot(slot); |
1da177e4 LT |
174 | } |
175 | ||
176 | ||
177 | static int disable_slot(struct hotplug_slot *hotplug_slot) | |
178 | { | |
179 | struct slot *slot = hotplug_slot->private; | |
180 | ||
18b341b7 | 181 | ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n", |
e1acb24f | 182 | __func__, slot_name(slot)); |
1da177e4 | 183 | |
5d386e1a | 184 | return pciehp_sysfs_disable_slot(slot); |
1da177e4 LT |
185 | } |
186 | ||
187 | static int get_power_status(struct hotplug_slot *hotplug_slot, u8 *value) | |
188 | { | |
189 | struct slot *slot = hotplug_slot->private; | |
1da177e4 | 190 | |
18b341b7 | 191 | ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n", |
e1acb24f | 192 | __func__, slot_name(slot)); |
1da177e4 | 193 | |
445f7985 | 194 | return pciehp_get_power_status(slot, value); |
1da177e4 LT |
195 | } |
196 | ||
197 | static int get_attention_status(struct hotplug_slot *hotplug_slot, u8 *value) | |
198 | { | |
199 | struct slot *slot = hotplug_slot->private; | |
1da177e4 | 200 | |
18b341b7 | 201 | ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n", |
e1acb24f | 202 | __func__, slot_name(slot)); |
1da177e4 | 203 | |
445f7985 | 204 | return pciehp_get_attention_status(slot, value); |
1da177e4 LT |
205 | } |
206 | ||
207 | static int get_latch_status(struct hotplug_slot *hotplug_slot, u8 *value) | |
208 | { | |
209 | struct slot *slot = hotplug_slot->private; | |
1da177e4 | 210 | |
18b341b7 | 211 | ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n", |
e1acb24f | 212 | __func__, slot_name(slot)); |
1da177e4 | 213 | |
445f7985 | 214 | return pciehp_get_latch_status(slot, value); |
1da177e4 LT |
215 | } |
216 | ||
217 | static int get_adapter_status(struct hotplug_slot *hotplug_slot, u8 *value) | |
218 | { | |
219 | struct slot *slot = hotplug_slot->private; | |
1da177e4 | 220 | |
18b341b7 | 221 | ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n", |
e1acb24f | 222 | __func__, slot_name(slot)); |
1da177e4 | 223 | |
445f7985 | 224 | return pciehp_get_adapter_status(slot, value); |
1da177e4 LT |
225 | } |
226 | ||
0516c8bc | 227 | static int pciehp_probe(struct pcie_device *dev) |
1da177e4 LT |
228 | { |
229 | int rc; | |
230 | struct controller *ctrl; | |
8720d27d | 231 | struct slot *slot; |
8792e11f | 232 | u8 occupied, poweron; |
125c39f7 KK |
233 | |
234 | if (pciehp_force) | |
7f2feec1 TI |
235 | dev_info(&dev->device, |
236 | "Bypassing BIOS check for pciehp use on %s\n", | |
8792e11f | 237 | pci_name(dev->port)); |
28eb5f27 | 238 | else if (pciehp_acpi_slot_detection_check(dev->port)) |
125c39f7 | 239 | goto err_out_none; |
a073a826 | 240 | |
c4635eb0 | 241 | ctrl = pcie_init(dev); |
1da177e4 | 242 | if (!ctrl) { |
18b341b7 | 243 | dev_err(&dev->device, "Controller initialization failed\n"); |
c4635eb0 | 244 | goto err_out_none; |
1da177e4 | 245 | } |
b9708940 | 246 | set_service_data(dev, ctrl); |
1da177e4 | 247 | |
1da177e4 | 248 | /* Setup the slot information structures */ |
8720d27d | 249 | rc = init_slot(ctrl); |
1da177e4 | 250 | if (rc) { |
f46753c5 | 251 | if (rc == -EBUSY) |
18b341b7 | 252 | ctrl_warn(ctrl, "Slot already registered by another " |
7f2feec1 | 253 | "hotplug driver\n"); |
f46753c5 | 254 | else |
18b341b7 | 255 | ctrl_err(ctrl, "Slot initialization failed\n"); |
a8c2b635 | 256 | goto err_out_release_ctlr; |
1da177e4 LT |
257 | } |
258 | ||
dbc7e1e5 EB |
259 | /* Enable events after we have setup the data structures */ |
260 | rc = pcie_init_notification(ctrl); | |
261 | if (rc) { | |
262 | ctrl_err(ctrl, "Notification initialization failed\n"); | |
65b947bc | 263 | goto err_out_free_ctrl_slot; |
dbc7e1e5 EB |
264 | } |
265 | ||
db9aaf0b | 266 | /* Check if slot is occupied */ |
8720d27d | 267 | slot = ctrl->slot; |
8792e11f KK |
268 | pciehp_get_adapter_status(slot, &occupied); |
269 | pciehp_get_power_status(slot, &poweron); | |
270 | if (occupied && pciehp_force) | |
271 | pciehp_enable_slot(slot); | |
272 | /* If empty slot's power status is on, turn power off */ | |
273 | if (!occupied && poweron && POWER_CTRL(ctrl)) | |
274 | pciehp_power_off_slot(slot); | |
1da177e4 | 275 | |
1da177e4 LT |
276 | return 0; |
277 | ||
278 | err_out_free_ctrl_slot: | |
8720d27d | 279 | cleanup_slot(ctrl); |
a8c2b635 | 280 | err_out_release_ctlr: |
82a9e79e | 281 | pciehp_release_ctrl(ctrl); |
1da177e4 LT |
282 | err_out_none: |
283 | return -ENODEV; | |
284 | } | |
285 | ||
82a9e79e | 286 | static void pciehp_remove(struct pcie_device *dev) |
1da177e4 | 287 | { |
b9708940 | 288 | struct controller *ctrl = get_service_data(dev); |
1da177e4 | 289 | |
8720d27d | 290 | cleanup_slot(ctrl); |
82a9e79e | 291 | pciehp_release_ctrl(ctrl); |
1da177e4 LT |
292 | } |
293 | ||
294 | #ifdef CONFIG_PM | |
3a3c244c | 295 | static int pciehp_suspend (struct pcie_device *dev) |
1da177e4 | 296 | { |
7f2feec1 | 297 | dev_info(&dev->device, "%s ENTRY\n", __func__); |
1da177e4 LT |
298 | return 0; |
299 | } | |
300 | ||
301 | static int pciehp_resume (struct pcie_device *dev) | |
302 | { | |
7f2feec1 | 303 | dev_info(&dev->device, "%s ENTRY\n", __func__); |
cd2fe83a | 304 | if (pciehp_force) { |
b9708940 | 305 | struct controller *ctrl = get_service_data(dev); |
8720d27d | 306 | struct slot *slot; |
cd2fe83a ML |
307 | u8 status; |
308 | ||
309 | /* reinitialize the chipset's event detection logic */ | |
c4635eb0 | 310 | pcie_enable_notification(ctrl); |
cd2fe83a | 311 | |
8720d27d | 312 | slot = ctrl->slot; |
cd2fe83a ML |
313 | |
314 | /* Check if slot is occupied */ | |
82a9e79e | 315 | pciehp_get_adapter_status(slot, &status); |
cd2fe83a | 316 | if (status) |
8720d27d | 317 | pciehp_enable_slot(slot); |
cd2fe83a | 318 | else |
8720d27d | 319 | pciehp_disable_slot(slot); |
cd2fe83a | 320 | } |
1da177e4 LT |
321 | return 0; |
322 | } | |
3a3c244c | 323 | #endif /* PM */ |
1da177e4 | 324 | |
1da177e4 | 325 | static struct pcie_port_service_driver hpdriver_portdrv = { |
83e9ad54 | 326 | .name = PCIE_MODULE_NAME, |
22106368 RW |
327 | .port_type = PCIE_ANY_PORT, |
328 | .service = PCIE_PORT_SERVICE_HP, | |
1da177e4 LT |
329 | |
330 | .probe = pciehp_probe, | |
331 | .remove = pciehp_remove, | |
332 | ||
333 | #ifdef CONFIG_PM | |
334 | .suspend = pciehp_suspend, | |
335 | .resume = pciehp_resume, | |
336 | #endif /* PM */ | |
337 | }; | |
338 | ||
339 | static int __init pcied_init(void) | |
340 | { | |
341 | int retval = 0; | |
342 | ||
a827ea30 TH |
343 | pciehp_wq = alloc_workqueue("pciehp", 0, 0); |
344 | if (!pciehp_wq) | |
345 | return -ENOMEM; | |
346 | ||
c9ffa5a5 | 347 | pciehp_firmware_init(); |
a8a2be94 | 348 | retval = pcie_port_service_register(&hpdriver_portdrv); |
349 | dbg("pcie_port_service_register = %d\n", retval); | |
350 | info(DRIVER_DESC " version: " DRIVER_VERSION "\n"); | |
a827ea30 | 351 | if (retval) { |
a827ea30 | 352 | destroy_workqueue(pciehp_wq); |
18b341b7 | 353 | dbg("Failure to register service\n"); |
a827ea30 | 354 | } |
1da177e4 LT |
355 | return retval; |
356 | } | |
357 | ||
358 | static void __exit pcied_cleanup(void) | |
359 | { | |
360 | dbg("unload_pciehpd()\n"); | |
027e8d52 | 361 | pcie_port_service_unregister(&hpdriver_portdrv); |
a827ea30 | 362 | destroy_workqueue(pciehp_wq); |
1da177e4 LT |
363 | info(DRIVER_DESC " version: " DRIVER_VERSION " unloaded\n"); |
364 | } | |
365 | ||
366 | module_init(pcied_init); | |
367 | module_exit(pcied_cleanup); |