]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/pci/hotplug/pciehp_core.c
pciehp: fix wait command completion
[mirror_ubuntu-bionic-kernel.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
LT
36#include <linux/interrupt.h>
37
38/* Global variables */
39int pciehp_debug;
40int pciehp_poll_mode;
41int pciehp_poll_time;
a3a45ec8 42int pciehp_force;
1da177e4 43struct controller *pciehp_ctrl_list;
1da177e4
LT
44
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"
48
49MODULE_AUTHOR(DRIVER_AUTHOR);
50MODULE_DESCRIPTION(DRIVER_DESC);
51MODULE_LICENSE("GPL");
52
53module_param(pciehp_debug, bool, 0644);
54module_param(pciehp_poll_mode, bool, 0644);
55module_param(pciehp_poll_time, int, 0644);
a3a45ec8 56module_param(pciehp_force, bool, 0644);
1da177e4
LT
57MODULE_PARM_DESC(pciehp_debug, "Debugging mode enabled or not");
58MODULE_PARM_DESC(pciehp_poll_mode, "Using polling mechanism for hot-plug events or not");
59MODULE_PARM_DESC(pciehp_poll_time, "Polling mechanism frequency, in seconds");
a3a45ec8 60MODULE_PARM_DESC(pciehp_force, "Force pciehp, even if _OSC and OSHP are missing");
1da177e4
LT
61
62#define PCIE_MODULE_NAME "pciehp"
63
64static int pcie_start_thread (void);
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);
132066a9 72static int get_address (struct hotplug_slot *slot, u32 *value);
1da177e4
LT
73static int get_max_bus_speed (struct hotplug_slot *slot, enum pci_bus_speed *value);
74static int get_cur_bus_speed (struct hotplug_slot *slot, enum pci_bus_speed *value);
75
76static struct hotplug_slot_ops pciehp_hotplug_slot_ops = {
77 .owner = THIS_MODULE,
78 .set_attention_status = set_attention_status,
79 .enable_slot = enable_slot,
80 .disable_slot = disable_slot,
81 .get_power_status = get_power_status,
82 .get_attention_status = get_attention_status,
83 .get_latch_status = get_latch_status,
84 .get_adapter_status = get_adapter_status,
132066a9 85 .get_address = get_address,
1da177e4
LT
86 .get_max_bus_speed = get_max_bus_speed,
87 .get_cur_bus_speed = get_cur_bus_speed,
88};
89
b308240b
DS
90/**
91 * release_slot - free up the memory used by a slot
92 * @hotplug_slot: slot to free
93 */
94static void release_slot(struct hotplug_slot *hotplug_slot)
95{
96 struct slot *slot = hotplug_slot->private;
97
98 dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
99
100 kfree(slot->hotplug_slot->info);
b308240b
DS
101 kfree(slot->hotplug_slot);
102 kfree(slot);
103}
104
a0b17257
KK
105static void make_slot_name(struct slot *slot)
106{
107 snprintf(slot->hotplug_slot->name, SLOT_NAME_SIZE, "%04d_%04d",
108 slot->bus, slot->number);
109}
110
1da177e4
LT
111static int init_slots(struct controller *ctrl)
112{
121082e2 113 struct slot *slot;
121082e2 114 struct hotplug_slot *hotplug_slot;
a0b17257
KK
115 struct hotplug_slot_info *info;
116 int retval = -ENOMEM;
117 int i;
1da177e4 118
a0b17257 119 for (i = 0; i < ctrl->num_slots; i++) {
f5afe806 120 slot = kzalloc(sizeof(*slot), GFP_KERNEL);
121082e2 121 if (!slot)
1da177e4
LT
122 goto error;
123
a0b17257
KK
124 hotplug_slot = kzalloc(sizeof(*hotplug_slot), GFP_KERNEL);
125 if (!hotplug_slot)
1da177e4 126 goto error_slot;
a0b17257 127 slot->hotplug_slot = hotplug_slot;
1da177e4 128
a0b17257
KK
129 info = kzalloc(sizeof(*info), GFP_KERNEL);
130 if (!info)
1da177e4 131 goto error_hpslot;
a0b17257 132 hotplug_slot->info = info;
1da177e4 133
a0b17257 134 hotplug_slot->name = slot->name;
1da177e4 135
a0b17257
KK
136 slot->hp_slot = i;
137 slot->ctrl = ctrl;
138 slot->bus = ctrl->pci_dev->subordinate->number;
139 slot->device = ctrl->slot_device_offset + i;
140 slot->hpc_ops = ctrl->hpc_ops;
121082e2 141 slot->number = ctrl->first_slot;
1da177e4
LT
142
143 /* register this slot with the hotplug pci core */
121082e2
JJ
144 hotplug_slot->private = slot;
145 hotplug_slot->release = &release_slot;
a0b17257 146 make_slot_name(slot);
121082e2
JJ
147 hotplug_slot->ops = &pciehp_hotplug_slot_ops;
148
a0b17257
KK
149 get_power_status(hotplug_slot, &info->power_status);
150 get_attention_status(hotplug_slot, &info->attention_status);
151 get_latch_status(hotplug_slot, &info->latch_status);
152 get_adapter_status(hotplug_slot, &info->adapter_status);
121082e2
JJ
153
154 dbg("Registering bus=%x dev=%x hp_slot=%x sun=%x "
a0b17257
KK
155 "slot_device_offset=%x\n", slot->bus, slot->device,
156 slot->hp_slot, slot->number, ctrl->slot_device_offset);
157 retval = pci_hp_register(hotplug_slot);
158 if (retval) {
159 err ("pci_hp_register failed with error %d\n", retval);
160 goto error_info;
1da177e4
LT
161 }
162
2410fa4e 163 list_add(&slot->slot_list, &ctrl->slot_list);
1da177e4
LT
164 }
165
166 return 0;
1da177e4 167error_info:
a0b17257 168 kfree(info);
1da177e4 169error_hpslot:
121082e2 170 kfree(hotplug_slot);
1da177e4 171error_slot:
121082e2 172 kfree(slot);
1da177e4 173error:
a0b17257 174 return retval;
1da177e4
LT
175}
176
2410fa4e 177static void cleanup_slots(struct controller *ctrl)
1da177e4 178{
2410fa4e
KK
179 struct list_head *tmp;
180 struct list_head *next;
181 struct slot *slot;
1da177e4 182
2410fa4e
KK
183 list_for_each_safe(tmp, next, &ctrl->slot_list) {
184 slot = list_entry(tmp, struct slot, slot_list);
185 list_del(&slot->slot_list);
186 pci_hp_deregister(slot->hotplug_slot);
1da177e4 187 }
1da177e4
LT
188}
189
1da177e4
LT
190/*
191 * set_attention_status - Turns the Amber LED for a slot on, off or blink
192 */
193static int set_attention_status(struct hotplug_slot *hotplug_slot, u8 status)
194{
195 struct slot *slot = hotplug_slot->private;
196
197 dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
198
199 hotplug_slot->info->attention_status = status;
200
201 if (ATTN_LED(slot->ctrl->ctrlcap))
202 slot->hpc_ops->set_attention_status(slot, status);
203
204 return 0;
205}
206
207
208static int enable_slot(struct hotplug_slot *hotplug_slot)
209{
210 struct slot *slot = hotplug_slot->private;
211
212 dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
213
214 return pciehp_enable_slot(slot);
215}
216
217
218static int disable_slot(struct hotplug_slot *hotplug_slot)
219{
220 struct slot *slot = hotplug_slot->private;
221
222 dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
223
224 return pciehp_disable_slot(slot);
225}
226
227static int get_power_status(struct hotplug_slot *hotplug_slot, u8 *value)
228{
229 struct slot *slot = hotplug_slot->private;
230 int retval;
231
232 dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
233
234 retval = slot->hpc_ops->get_power_status(slot, value);
235 if (retval < 0)
236 *value = hotplug_slot->info->power_status;
237
238 return 0;
239}
240
241static int get_attention_status(struct hotplug_slot *hotplug_slot, u8 *value)
242{
243 struct slot *slot = hotplug_slot->private;
244 int retval;
245
246 dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
247
248 retval = slot->hpc_ops->get_attention_status(slot, value);
249 if (retval < 0)
250 *value = hotplug_slot->info->attention_status;
251
252 return 0;
253}
254
255static int get_latch_status(struct hotplug_slot *hotplug_slot, u8 *value)
256{
257 struct slot *slot = hotplug_slot->private;
258 int retval;
259
260 dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
261
262 retval = slot->hpc_ops->get_latch_status(slot, value);
263 if (retval < 0)
264 *value = hotplug_slot->info->latch_status;
265
266 return 0;
267}
268
269static int get_adapter_status(struct hotplug_slot *hotplug_slot, u8 *value)
270{
271 struct slot *slot = hotplug_slot->private;
272 int retval;
273
274 dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
275
276 retval = slot->hpc_ops->get_adapter_status(slot, value);
277 if (retval < 0)
278 *value = hotplug_slot->info->adapter_status;
279
280 return 0;
281}
282
132066a9
KK
283static int get_address(struct hotplug_slot *hotplug_slot, u32 *value)
284{
285 struct slot *slot = hotplug_slot->private;
286 struct pci_bus *bus = slot->ctrl->pci_dev->subordinate;
287
288 dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
289
290 *value = (pci_domain_nr(bus) << 16) | (slot->bus << 8) | slot->device;
291
292 return 0;
293}
294
1da177e4
LT
295static int get_max_bus_speed(struct hotplug_slot *hotplug_slot, enum pci_bus_speed *value)
296{
297 struct slot *slot = hotplug_slot->private;
298 int retval;
299
300 dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
301
302 retval = slot->hpc_ops->get_max_bus_speed(slot, value);
303 if (retval < 0)
304 *value = PCI_SPEED_UNKNOWN;
305
306 return 0;
307}
308
309static int get_cur_bus_speed(struct hotplug_slot *hotplug_slot, enum pci_bus_speed *value)
310{
311 struct slot *slot = hotplug_slot->private;
312 int retval;
313
314 dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
315
316 retval = slot->hpc_ops->get_cur_bus_speed(slot, value);
317 if (retval < 0)
318 *value = PCI_SPEED_UNKNOWN;
319
320 return 0;
321}
322
323static int pciehp_probe(struct pcie_device *dev, const struct pcie_port_service_id *id)
324{
325 int rc;
326 struct controller *ctrl;
327 struct slot *t_slot;
1da177e4
LT
328 u8 value;
329 struct pci_dev *pdev;
330
f5afe806 331 ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
1da177e4
LT
332 if (!ctrl) {
333 err("%s : out of memory\n", __FUNCTION__);
334 goto err_out_none;
335 }
2410fa4e 336 INIT_LIST_HEAD(&ctrl->slot_list);
1da177e4 337
1da177e4 338 pdev = dev->port;
a8a2be94 339 ctrl->pci_dev = pdev;
1da177e4 340
ed6cbcf2 341 rc = pcie_init(ctrl, dev);
1da177e4
LT
342 if (rc) {
343 dbg("%s: controller initialization failed\n", PCIE_MODULE_NAME);
344 goto err_out_free_ctrl;
345 }
346
1da177e4
LT
347 pci_set_drvdata(pdev, ctrl);
348
1da177e4
LT
349 ctrl->bus = pdev->bus->number; /* ctrl bus */
350 ctrl->slot_bus = pdev->subordinate->number; /* bus controlled by this HPC */
351
352 ctrl->device = PCI_SLOT(pdev->devfn);
353 ctrl->function = PCI_FUNC(pdev->devfn);
354 dbg("%s: ctrl bus=0x%x, device=%x, function=%x, irq=%x\n", __FUNCTION__,
355 ctrl->bus, ctrl->device, ctrl->function, pdev->irq);
356
1da177e4
LT
357 /* Setup the slot information structures */
358 rc = init_slots(ctrl);
359 if (rc) {
15232ece 360 err("%s: slot initialization failed\n", PCIE_MODULE_NAME);
a8c2b635 361 goto err_out_release_ctlr;
1da177e4
LT
362 }
363
48fe3915 364 t_slot = pciehp_find_slot(ctrl, ctrl->slot_device_offset);
1da177e4
LT
365
366 /* Finish setting up the hot plug ctrl device */
367 ctrl->next_event = 0;
368
369 if (!pciehp_ctrl_list) {
370 pciehp_ctrl_list = ctrl;
371 ctrl->next = NULL;
372 } else {
373 ctrl->next = pciehp_ctrl_list;
374 pciehp_ctrl_list = ctrl;
375 }
376
1da177e4 377 t_slot->hpc_ops->get_adapter_status(t_slot, &value); /* Check if slot is occupied */
1da177e4
LT
378 if ((POWER_CTRL(ctrl->ctrlcap)) && !value) {
379 rc = t_slot->hpc_ops->power_off_slot(t_slot); /* Power off slot if not occupied*/
44ef4cef 380 if (rc)
1da177e4 381 goto err_out_free_ctrl_slot;
1da177e4
LT
382 }
383
1da177e4
LT
384 return 0;
385
386err_out_free_ctrl_slot:
387 cleanup_slots(ctrl);
a8c2b635 388err_out_release_ctlr:
1da177e4
LT
389 ctrl->hpc_ops->release_ctlr(ctrl);
390err_out_free_ctrl:
391 kfree(ctrl);
392err_out_none:
393 return -ENODEV;
394}
395
396
397static int pcie_start_thread(void)
398{
1da177e4
LT
399 int retval = 0;
400
401 dbg("Initialize + Start the notification/polling mechanism \n");
402
403 retval = pciehp_event_start_thread();
404 if (retval) {
405 dbg("pciehp_event_start_thread() failed\n");
406 return retval;
407 }
408
1da177e4
LT
409 return retval;
410}
411
1da177e4
LT
412static void __exit unload_pciehpd(void)
413{
1da177e4
LT
414 struct controller *ctrl;
415 struct controller *tctrl;
416
417 ctrl = pciehp_ctrl_list;
418
419 while (ctrl) {
420 cleanup_slots(ctrl);
421
1da177e4
LT
422 ctrl->hpc_ops->release_ctlr(ctrl);
423
424 tctrl = ctrl;
425 ctrl = ctrl->next;
426
427 kfree(tctrl);
428 }
429
1da177e4
LT
430 /* Stop the notification mechanism */
431 pciehp_event_stop_thread();
432
433}
434
1da177e4
LT
435static void pciehp_remove (struct pcie_device *device)
436{
407f452b 437 /* XXX - Needs to be adapted to device driver model */
1da177e4
LT
438}
439
440#ifdef CONFIG_PM
7f4927c1 441static int pciehp_suspend (struct pcie_device *dev, pm_message_t state)
1da177e4
LT
442{
443 printk("%s ENTRY\n", __FUNCTION__);
444 return 0;
445}
446
447static int pciehp_resume (struct pcie_device *dev)
448{
449 printk("%s ENTRY\n", __FUNCTION__);
450 return 0;
451}
452#endif
453
454static struct pcie_port_service_id port_pci_ids[] = { {
455 .vendor = PCI_ANY_ID,
456 .device = PCI_ANY_ID,
8b245e45 457 .port_type = PCIE_ANY_PORT,
1da177e4
LT
458 .service_type = PCIE_PORT_SERVICE_HP,
459 .driver_data = 0,
460 }, { /* end: all zeroes */ }
461};
462static const char device_name[] = "hpdriver";
463
464static struct pcie_port_service_driver hpdriver_portdrv = {
465 .name = (char *)device_name,
466 .id_table = &port_pci_ids[0],
467
468 .probe = pciehp_probe,
469 .remove = pciehp_remove,
470
471#ifdef CONFIG_PM
472 .suspend = pciehp_suspend,
473 .resume = pciehp_resume,
474#endif /* PM */
475};
476
477static int __init pcied_init(void)
478{
479 int retval = 0;
480
481#ifdef CONFIG_HOTPLUG_PCI_PCIE_POLL_EVENT_MODE
482 pciehp_poll_mode = 1;
483#endif
484
485 retval = pcie_start_thread();
486 if (retval)
487 goto error_hpc_init;
488
a8a2be94
RS
489 retval = pcie_port_service_register(&hpdriver_portdrv);
490 dbg("pcie_port_service_register = %d\n", retval);
491 info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
492 if (retval)
493 dbg("%s: Failure to register service\n", __FUNCTION__);
1da177e4
LT
494
495error_hpc_init:
496 if (retval) {
1da177e4 497 pciehp_event_stop_thread();
71b720c0 498 };
1da177e4
LT
499
500 return retval;
501}
502
503static void __exit pcied_cleanup(void)
504{
505 dbg("unload_pciehpd()\n");
506 unload_pciehpd();
507
1da177e4
LT
508 pcie_port_service_unregister(&hpdriver_portdrv);
509
510 info(DRIVER_DESC " version: " DRIVER_VERSION " unloaded\n");
511}
512
513module_init(pcied_init);
514module_exit(pcied_cleanup);