pciehp: remove unused pci_bus from struct controller
[linux-2.6/x86.git] / drivers / pci / hotplug / pciehp_core.c
blobb617613381d60ef9567a0266aaccb15ef4459871
1 /*
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
9 * All rights reserved.
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
20 * details.
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>
35 #include "pciehp.h"
36 #include <linux/interrupt.h>
38 /* Global variables */
39 int pciehp_debug;
40 int pciehp_poll_mode;
41 int pciehp_poll_time;
42 int pciehp_force;
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_address (struct hotplug_slot *slot, u32 *value);
73 static int get_max_bus_speed (struct hotplug_slot *slot, enum pci_bus_speed *value);
74 static int get_cur_bus_speed (struct hotplug_slot *slot, enum pci_bus_speed *value);
76 static 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,
85 .get_address = get_address,
86 .get_max_bus_speed = get_max_bus_speed,
87 .get_cur_bus_speed = get_cur_bus_speed,
90 /**
91 * release_slot - free up the memory used by a slot
92 * @hotplug_slot: slot to free
94 static void release_slot(struct hotplug_slot *hotplug_slot)
96 struct slot *slot = hotplug_slot->private;
98 dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
100 kfree(slot->hotplug_slot->info);
101 kfree(slot->hotplug_slot);
102 kfree(slot);
105 static void make_slot_name(struct slot *slot)
107 snprintf(slot->hotplug_slot->name, SLOT_NAME_SIZE, "%04d_%04d",
108 slot->bus, slot->number);
111 static int init_slots(struct controller *ctrl)
113 struct slot *slot;
114 struct hotplug_slot *hotplug_slot;
115 struct hotplug_slot_info *info;
116 int retval = -ENOMEM;
117 int i;
119 for (i = 0; i < ctrl->num_slots; i++) {
120 slot = kzalloc(sizeof(*slot), GFP_KERNEL);
121 if (!slot)
122 goto error;
124 hotplug_slot = kzalloc(sizeof(*hotplug_slot), GFP_KERNEL);
125 if (!hotplug_slot)
126 goto error_slot;
127 slot->hotplug_slot = hotplug_slot;
129 info = kzalloc(sizeof(*info), GFP_KERNEL);
130 if (!info)
131 goto error_hpslot;
132 hotplug_slot->info = info;
134 hotplug_slot->name = slot->name;
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;
141 slot->number = ctrl->first_slot;
143 /* register this slot with the hotplug pci core */
144 hotplug_slot->private = slot;
145 hotplug_slot->release = &release_slot;
146 make_slot_name(slot);
147 hotplug_slot->ops = &pciehp_hotplug_slot_ops;
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);
154 dbg("Registering bus=%x dev=%x hp_slot=%x sun=%x "
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;
163 list_add(&slot->slot_list, &ctrl->slot_list);
166 return 0;
167 error_info:
168 kfree(info);
169 error_hpslot:
170 kfree(hotplug_slot);
171 error_slot:
172 kfree(slot);
173 error:
174 return retval;
177 static void cleanup_slots(struct controller *ctrl)
179 struct list_head *tmp;
180 struct list_head *next;
181 struct slot *slot;
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);
191 * set_attention_status - Turns the Amber LED for a slot on, off or blink
193 static int set_attention_status(struct hotplug_slot *hotplug_slot, u8 status)
195 struct slot *slot = hotplug_slot->private;
197 dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
199 hotplug_slot->info->attention_status = status;
201 if (ATTN_LED(slot->ctrl->ctrlcap))
202 slot->hpc_ops->set_attention_status(slot, status);
204 return 0;
208 static int enable_slot(struct hotplug_slot *hotplug_slot)
210 struct slot *slot = hotplug_slot->private;
212 dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
214 return pciehp_enable_slot(slot);
218 static int disable_slot(struct hotplug_slot *hotplug_slot)
220 struct slot *slot = hotplug_slot->private;
222 dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
224 return pciehp_disable_slot(slot);
227 static int get_power_status(struct hotplug_slot *hotplug_slot, u8 *value)
229 struct slot *slot = hotplug_slot->private;
230 int retval;
232 dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
234 retval = slot->hpc_ops->get_power_status(slot, value);
235 if (retval < 0)
236 *value = hotplug_slot->info->power_status;
238 return 0;
241 static int get_attention_status(struct hotplug_slot *hotplug_slot, u8 *value)
243 struct slot *slot = hotplug_slot->private;
244 int retval;
246 dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
248 retval = slot->hpc_ops->get_attention_status(slot, value);
249 if (retval < 0)
250 *value = hotplug_slot->info->attention_status;
252 return 0;
255 static int get_latch_status(struct hotplug_slot *hotplug_slot, u8 *value)
257 struct slot *slot = hotplug_slot->private;
258 int retval;
260 dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
262 retval = slot->hpc_ops->get_latch_status(slot, value);
263 if (retval < 0)
264 *value = hotplug_slot->info->latch_status;
266 return 0;
269 static int get_adapter_status(struct hotplug_slot *hotplug_slot, u8 *value)
271 struct slot *slot = hotplug_slot->private;
272 int retval;
274 dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
276 retval = slot->hpc_ops->get_adapter_status(slot, value);
277 if (retval < 0)
278 *value = hotplug_slot->info->adapter_status;
280 return 0;
283 static int get_address(struct hotplug_slot *hotplug_slot, u32 *value)
285 struct slot *slot = hotplug_slot->private;
286 struct pci_bus *bus = slot->ctrl->pci_dev->subordinate;
288 dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
290 *value = (pci_domain_nr(bus) << 16) | (slot->bus << 8) | slot->device;
292 return 0;
295 static int get_max_bus_speed(struct hotplug_slot *hotplug_slot, enum pci_bus_speed *value)
297 struct slot *slot = hotplug_slot->private;
298 int retval;
300 dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
302 retval = slot->hpc_ops->get_max_bus_speed(slot, value);
303 if (retval < 0)
304 *value = PCI_SPEED_UNKNOWN;
306 return 0;
309 static int get_cur_bus_speed(struct hotplug_slot *hotplug_slot, enum pci_bus_speed *value)
311 struct slot *slot = hotplug_slot->private;
312 int retval;
314 dbg("%s - physical_slot = %s\n", __FUNCTION__, hotplug_slot->name);
316 retval = slot->hpc_ops->get_cur_bus_speed(slot, value);
317 if (retval < 0)
318 *value = PCI_SPEED_UNKNOWN;
320 return 0;
323 static int pciehp_probe(struct pcie_device *dev, const struct pcie_port_service_id *id)
325 int rc;
326 struct controller *ctrl;
327 struct slot *t_slot;
328 u8 value;
329 struct pci_dev *pdev;
331 ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
332 if (!ctrl) {
333 err("%s : out of memory\n", __FUNCTION__);
334 goto err_out_none;
336 INIT_LIST_HEAD(&ctrl->slot_list);
338 pdev = dev->port;
339 ctrl->pci_dev = pdev;
341 rc = pcie_init(ctrl, dev);
342 if (rc) {
343 dbg("%s: controller initialization failed\n", PCIE_MODULE_NAME);
344 goto err_out_free_ctrl;
347 pci_set_drvdata(pdev, ctrl);
349 ctrl->bus = pdev->bus->number; /* ctrl bus */
350 ctrl->slot_bus = pdev->subordinate->number; /* bus controlled by this HPC */
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);
357 /* Setup the slot information structures */
358 rc = init_slots(ctrl);
359 if (rc) {
360 err(msg_initialization_err, 6);
361 goto err_out_release_ctlr;
364 t_slot = pciehp_find_slot(ctrl, ctrl->slot_device_offset);
366 /* Finish setting up the hot plug ctrl device */
367 ctrl->next_event = 0;
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;
377 /* Wait for exclusive access to hardware */
378 mutex_lock(&ctrl->ctrl_lock);
380 t_slot->hpc_ops->get_adapter_status(t_slot, &value); /* Check if slot is occupied */
382 if ((POWER_CTRL(ctrl->ctrlcap)) && !value) {
383 rc = t_slot->hpc_ops->power_off_slot(t_slot); /* Power off slot if not occupied*/
384 if (rc) {
385 /* Done with exclusive hardware access */
386 mutex_unlock(&ctrl->ctrl_lock);
387 goto err_out_free_ctrl_slot;
388 } else
389 /* Wait for the command to complete */
390 wait_for_ctrl_irq (ctrl);
393 /* Done with exclusive hardware access */
394 mutex_unlock(&ctrl->ctrl_lock);
396 return 0;
398 err_out_free_ctrl_slot:
399 cleanup_slots(ctrl);
400 err_out_release_ctlr:
401 ctrl->hpc_ops->release_ctlr(ctrl);
402 err_out_free_ctrl:
403 kfree(ctrl);
404 err_out_none:
405 return -ENODEV;
409 static int pcie_start_thread(void)
411 int retval = 0;
413 dbg("Initialize + Start the notification/polling mechanism \n");
415 retval = pciehp_event_start_thread();
416 if (retval) {
417 dbg("pciehp_event_start_thread() failed\n");
418 return retval;
421 return retval;
424 static void __exit unload_pciehpd(void)
426 struct controller *ctrl;
427 struct controller *tctrl;
429 ctrl = pciehp_ctrl_list;
431 while (ctrl) {
432 cleanup_slots(ctrl);
434 ctrl->hpc_ops->release_ctlr(ctrl);
436 tctrl = ctrl;
437 ctrl = ctrl->next;
439 kfree(tctrl);
442 /* Stop the notification mechanism */
443 pciehp_event_stop_thread();
447 static void pciehp_remove (struct pcie_device *device)
449 /* XXX - Needs to be adapted to device driver model */
452 #ifdef CONFIG_PM
453 static int pciehp_suspend (struct pcie_device *dev, pm_message_t state)
455 printk("%s ENTRY\n", __FUNCTION__);
456 return 0;
459 static int pciehp_resume (struct pcie_device *dev)
461 printk("%s ENTRY\n", __FUNCTION__);
462 return 0;
464 #endif
466 static struct pcie_port_service_id port_pci_ids[] = { {
467 .vendor = PCI_ANY_ID,
468 .device = PCI_ANY_ID,
469 .port_type = PCIE_ANY_PORT,
470 .service_type = PCIE_PORT_SERVICE_HP,
471 .driver_data = 0,
472 }, { /* end: all zeroes */ }
474 static const char device_name[] = "hpdriver";
476 static struct pcie_port_service_driver hpdriver_portdrv = {
477 .name = (char *)device_name,
478 .id_table = &port_pci_ids[0],
480 .probe = pciehp_probe,
481 .remove = pciehp_remove,
483 #ifdef CONFIG_PM
484 .suspend = pciehp_suspend,
485 .resume = pciehp_resume,
486 #endif /* PM */
489 static int __init pcied_init(void)
491 int retval = 0;
493 #ifdef CONFIG_HOTPLUG_PCI_PCIE_POLL_EVENT_MODE
494 pciehp_poll_mode = 1;
495 #endif
497 retval = pcie_start_thread();
498 if (retval)
499 goto error_hpc_init;
501 retval = pcie_port_service_register(&hpdriver_portdrv);
502 dbg("pcie_port_service_register = %d\n", retval);
503 info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
504 if (retval)
505 dbg("%s: Failure to register service\n", __FUNCTION__);
507 error_hpc_init:
508 if (retval) {
509 pciehp_event_stop_thread();
512 return retval;
515 static void __exit pcied_cleanup(void)
517 dbg("unload_pciehpd()\n");
518 unload_pciehpd();
520 pcie_port_service_unregister(&hpdriver_portdrv);
522 info(DRIVER_DESC " version: " DRIVER_VERSION " unloaded\n");
525 module_init(pcied_init);
526 module_exit(pcied_cleanup);