Merge git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/usb-2.6
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / pci / hotplug / pciehp_core.c
CommitLineData
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>
33#include <linux/types.h>
1da177e4 34#include <linux/pci.h>
1da177e4 35#include "pciehp.h"
1da177e4 36#include <linux/interrupt.h>
34d03419 37#include <linux/time.h>
1da177e4
LT
38
39/* Global variables */
40int pciehp_debug;
41int pciehp_poll_mode;
42int pciehp_poll_time;
a3a45ec8 43int pciehp_force;
5d386e1a 44struct workqueue_struct *pciehp_wq;
1da177e4
LT
45
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"
49
50MODULE_AUTHOR(DRIVER_AUTHOR);
51MODULE_DESCRIPTION(DRIVER_DESC);
52MODULE_LICENSE("GPL");
53
54module_param(pciehp_debug, bool, 0644);
55module_param(pciehp_poll_mode, bool, 0644);
56module_param(pciehp_poll_time, int, 0644);
a3a45ec8 57module_param(pciehp_force, bool, 0644);
1da177e4
LT
58MODULE_PARM_DESC(pciehp_debug, "Debugging mode enabled or not");
59MODULE_PARM_DESC(pciehp_poll_mode, "Using polling mechanism for hot-plug events or not");
60MODULE_PARM_DESC(pciehp_poll_time, "Polling mechanism frequency, in seconds");
a3a45ec8 61MODULE_PARM_DESC(pciehp_force, "Force pciehp, even if _OSC and OSHP are missing");
1da177e4
LT
62
63#define PCIE_MODULE_NAME "pciehp"
64
1da177e4
LT
65static int set_attention_status (struct hotplug_slot *slot, u8 value);
66static int enable_slot (struct hotplug_slot *slot);
67static int disable_slot (struct hotplug_slot *slot);
68static int get_power_status (struct hotplug_slot *slot, u8 *value);
69static int get_attention_status (struct hotplug_slot *slot, u8 *value);
70static int get_latch_status (struct hotplug_slot *slot, u8 *value);
71static int get_adapter_status (struct hotplug_slot *slot, u8 *value);
72static int get_max_bus_speed (struct hotplug_slot *slot, enum pci_bus_speed *value);
73static int get_cur_bus_speed (struct hotplug_slot *slot, enum pci_bus_speed *value);
74
75static struct hotplug_slot_ops pciehp_hotplug_slot_ops = {
1da177e4
LT
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,
85};
86
b308240b
DS
87/**
88 * release_slot - free up the memory used by a slot
89 * @hotplug_slot: slot to free
90 */
91static void release_slot(struct hotplug_slot *hotplug_slot)
92{
7f2feec1
TI
93 struct slot *slot = hotplug_slot->private;
94
18b341b7 95 ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
e1acb24f 96 __func__, hotplug_slot_name(hotplug_slot));
b308240b 97
c4635eb0
KK
98 kfree(hotplug_slot->info);
99 kfree(hotplug_slot);
a0b17257
KK
100}
101
1da177e4
LT
102static int init_slots(struct controller *ctrl)
103{
121082e2 104 struct slot *slot;
121082e2 105 struct hotplug_slot *hotplug_slot;
a0b17257 106 struct hotplug_slot_info *info;
e1acb24f 107 char name[SLOT_NAME_SIZE];
a0b17257 108 int retval = -ENOMEM;
1da177e4 109
c4635eb0 110 list_for_each_entry(slot, &ctrl->slot_list, slot_list) {
a0b17257
KK
111 hotplug_slot = kzalloc(sizeof(*hotplug_slot), GFP_KERNEL);
112 if (!hotplug_slot)
c4635eb0 113 goto error;
1da177e4 114
a0b17257
KK
115 info = kzalloc(sizeof(*info), GFP_KERNEL);
116 if (!info)
1da177e4 117 goto error_hpslot;
1da177e4
LT
118
119 /* register this slot with the hotplug pci core */
c4635eb0 120 hotplug_slot->info = info;
121082e2
JJ
121 hotplug_slot->private = slot;
122 hotplug_slot->release = &release_slot;
121082e2 123 hotplug_slot->ops = &pciehp_hotplug_slot_ops;
c4635eb0 124 slot->hotplug_slot = hotplug_slot;
e1acb24f 125 snprintf(name, SLOT_NAME_SIZE, "%u", slot->number);
121082e2 126
18b341b7
TI
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);
f46753c5
AC
132 retval = pci_hp_register(hotplug_slot,
133 ctrl->pci_dev->subordinate,
1359f270 134 slot->device,
e1acb24f 135 name);
a0b17257 136 if (retval) {
7f2feec1
TI
137 ctrl_err(ctrl, "pci_hp_register failed with error %d\n",
138 retval);
a0b17257 139 goto error_info;
1da177e4 140 }
e1acb24f
AC
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);
1da177e4
LT
145 }
146
147 return 0;
1da177e4 148error_info:
a0b17257 149 kfree(info);
1da177e4 150error_hpslot:
121082e2 151 kfree(hotplug_slot);
1da177e4 152error:
a0b17257 153 return retval;
1da177e4
LT
154}
155
2410fa4e 156static void cleanup_slots(struct controller *ctrl)
1da177e4 157{
2410fa4e 158 struct slot *slot;
bd3d99c1 159 list_for_each_entry(slot, &ctrl->slot_list, slot_list)
2410fa4e 160 pci_hp_deregister(slot->hotplug_slot);
1da177e4
LT
161}
162
1da177e4
LT
163/*
164 * set_attention_status - Turns the Amber LED for a slot on, off or blink
165 */
166static int set_attention_status(struct hotplug_slot *hotplug_slot, u8 status)
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
LT
172
173 hotplug_slot->info->attention_status = status;
a073a826 174
ae416e6b 175 if (ATTN_LED(slot->ctrl))
1da177e4
LT
176 slot->hpc_ops->set_attention_status(slot, status);
177
178 return 0;
179}
180
181
182static int enable_slot(struct hotplug_slot *hotplug_slot)
183{
184 struct slot *slot = hotplug_slot->private;
185
18b341b7 186 ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
e1acb24f 187 __func__, slot_name(slot));
1da177e4 188
5d386e1a 189 return pciehp_sysfs_enable_slot(slot);
1da177e4
LT
190}
191
192
193static int disable_slot(struct hotplug_slot *hotplug_slot)
194{
195 struct slot *slot = hotplug_slot->private;
196
18b341b7 197 ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
e1acb24f 198 __func__, slot_name(slot));
1da177e4 199
5d386e1a 200 return pciehp_sysfs_disable_slot(slot);
1da177e4
LT
201}
202
203static int get_power_status(struct hotplug_slot *hotplug_slot, u8 *value)
204{
205 struct slot *slot = hotplug_slot->private;
206 int retval;
207
18b341b7 208 ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
e1acb24f 209 __func__, slot_name(slot));
1da177e4
LT
210
211 retval = slot->hpc_ops->get_power_status(slot, value);
212 if (retval < 0)
213 *value = hotplug_slot->info->power_status;
214
215 return 0;
216}
217
218static int get_attention_status(struct hotplug_slot *hotplug_slot, u8 *value)
219{
220 struct slot *slot = hotplug_slot->private;
221 int retval;
222
18b341b7 223 ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
e1acb24f 224 __func__, slot_name(slot));
1da177e4
LT
225
226 retval = slot->hpc_ops->get_attention_status(slot, value);
227 if (retval < 0)
228 *value = hotplug_slot->info->attention_status;
229
230 return 0;
231}
232
233static int get_latch_status(struct hotplug_slot *hotplug_slot, u8 *value)
234{
235 struct slot *slot = hotplug_slot->private;
236 int retval;
237
18b341b7 238 ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
e1acb24f 239 __func__, slot_name(slot));
1da177e4
LT
240
241 retval = slot->hpc_ops->get_latch_status(slot, value);
242 if (retval < 0)
243 *value = hotplug_slot->info->latch_status;
244
245 return 0;
246}
247
248static int get_adapter_status(struct hotplug_slot *hotplug_slot, u8 *value)
249{
250 struct slot *slot = hotplug_slot->private;
251 int retval;
252
18b341b7 253 ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
e1acb24f 254 __func__, slot_name(slot));
1da177e4
LT
255
256 retval = slot->hpc_ops->get_adapter_status(slot, value);
257 if (retval < 0)
258 *value = hotplug_slot->info->adapter_status;
259
260 return 0;
261}
262
f46753c5
AC
263static int get_max_bus_speed(struct hotplug_slot *hotplug_slot,
264 enum pci_bus_speed *value)
1da177e4
LT
265{
266 struct slot *slot = hotplug_slot->private;
267 int retval;
268
18b341b7 269 ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
e1acb24f 270 __func__, slot_name(slot));
a073a826 271
1da177e4
LT
272 retval = slot->hpc_ops->get_max_bus_speed(slot, value);
273 if (retval < 0)
274 *value = PCI_SPEED_UNKNOWN;
275
276 return 0;
277}
278
279static int get_cur_bus_speed(struct hotplug_slot *hotplug_slot, enum pci_bus_speed *value)
280{
281 struct slot *slot = hotplug_slot->private;
282 int retval;
283
18b341b7 284 ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
e1acb24f 285 __func__, slot_name(slot));
a073a826 286
1da177e4
LT
287 retval = slot->hpc_ops->get_cur_bus_speed(slot, value);
288 if (retval < 0)
289 *value = PCI_SPEED_UNKNOWN;
290
291 return 0;
292}
293
0516c8bc 294static int pciehp_probe(struct pcie_device *dev)
1da177e4
LT
295{
296 int rc;
297 struct controller *ctrl;
298 struct slot *t_slot;
1da177e4 299 u8 value;
125c39f7
KK
300 struct pci_dev *pdev = dev->port;
301
302 if (pciehp_force)
7f2feec1
TI
303 dev_info(&dev->device,
304 "Bypassing BIOS check for pciehp use on %s\n",
305 pci_name(pdev));
125c39f7
KK
306 else if (pciehp_get_hp_hw_control_from_firmware(pdev))
307 goto err_out_none;
a073a826 308
c4635eb0 309 ctrl = pcie_init(dev);
1da177e4 310 if (!ctrl) {
18b341b7 311 dev_err(&dev->device, "Controller initialization failed\n");
c4635eb0 312 goto err_out_none;
1da177e4 313 }
b9708940 314 set_service_data(dev, ctrl);
1da177e4 315
1da177e4
LT
316 /* Setup the slot information structures */
317 rc = init_slots(ctrl);
318 if (rc) {
f46753c5 319 if (rc == -EBUSY)
18b341b7 320 ctrl_warn(ctrl, "Slot already registered by another "
7f2feec1 321 "hotplug driver\n");
f46753c5 322 else
18b341b7 323 ctrl_err(ctrl, "Slot initialization failed\n");
a8c2b635 324 goto err_out_release_ctlr;
1da177e4
LT
325 }
326
dbc7e1e5
EB
327 /* Enable events after we have setup the data structures */
328 rc = pcie_init_notification(ctrl);
329 if (rc) {
330 ctrl_err(ctrl, "Notification initialization failed\n");
331 goto err_out_release_ctlr;
332 }
333
db9aaf0b 334 /* Check if slot is occupied */
48fe3915 335 t_slot = pciehp_find_slot(ctrl, ctrl->slot_device_offset);
db9aaf0b
KK
336 t_slot->hpc_ops->get_adapter_status(t_slot, &value);
337 if (value) {
338 if (pciehp_force)
339 pciehp_enable_slot(t_slot);
340 } else {
341 /* Power off slot if not occupied */
342 if (POWER_CTRL(ctrl)) {
343 rc = t_slot->hpc_ops->power_off_slot(t_slot);
344 if (rc)
345 goto err_out_free_ctrl_slot;
346 }
1da177e4
LT
347 }
348
1da177e4
LT
349 return 0;
350
351err_out_free_ctrl_slot:
352 cleanup_slots(ctrl);
a8c2b635 353err_out_release_ctlr:
1da177e4 354 ctrl->hpc_ops->release_ctlr(ctrl);
1da177e4
LT
355err_out_none:
356 return -ENODEV;
357}
358
92333526 359static void pciehp_remove (struct pcie_device *dev)
1da177e4 360{
b9708940 361 struct controller *ctrl = get_service_data(dev);
1da177e4 362
92333526
KK
363 cleanup_slots(ctrl);
364 ctrl->hpc_ops->release_ctlr(ctrl);
1da177e4
LT
365}
366
367#ifdef CONFIG_PM
3a3c244c 368static int pciehp_suspend (struct pcie_device *dev)
1da177e4 369{
7f2feec1 370 dev_info(&dev->device, "%s ENTRY\n", __func__);
1da177e4
LT
371 return 0;
372}
373
374static int pciehp_resume (struct pcie_device *dev)
375{
7f2feec1 376 dev_info(&dev->device, "%s ENTRY\n", __func__);
cd2fe83a 377 if (pciehp_force) {
b9708940 378 struct controller *ctrl = get_service_data(dev);
cd2fe83a
ML
379 struct slot *t_slot;
380 u8 status;
381
382 /* reinitialize the chipset's event detection logic */
c4635eb0 383 pcie_enable_notification(ctrl);
cd2fe83a
ML
384
385 t_slot = pciehp_find_slot(ctrl, ctrl->slot_device_offset);
386
387 /* Check if slot is occupied */
388 t_slot->hpc_ops->get_adapter_status(t_slot, &status);
389 if (status)
390 pciehp_enable_slot(t_slot);
391 else
392 pciehp_disable_slot(t_slot);
393 }
1da177e4
LT
394 return 0;
395}
3a3c244c 396#endif /* PM */
1da177e4 397
1da177e4 398static struct pcie_port_service_driver hpdriver_portdrv = {
83e9ad54 399 .name = PCIE_MODULE_NAME,
22106368
RW
400 .port_type = PCIE_ANY_PORT,
401 .service = PCIE_PORT_SERVICE_HP,
1da177e4
LT
402
403 .probe = pciehp_probe,
404 .remove = pciehp_remove,
405
406#ifdef CONFIG_PM
407 .suspend = pciehp_suspend,
408 .resume = pciehp_resume,
409#endif /* PM */
410};
411
412static int __init pcied_init(void)
413{
414 int retval = 0;
415
c9ffa5a5 416 pciehp_firmware_init();
a8a2be94
RS
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");
420 if (retval)
18b341b7 421 dbg("Failure to register service\n");
1da177e4
LT
422 return retval;
423}
424
425static void __exit pcied_cleanup(void)
426{
427 dbg("unload_pciehpd()\n");
1da177e4 428 pcie_port_service_unregister(&hpdriver_portdrv);
1da177e4
LT
429 info(DRIVER_DESC " version: " DRIVER_VERSION " unloaded\n");
430}
431
432module_init(pcied_init);
433module_exit(pcied_cleanup);