2 * PCI Hot Plug Controller Driver for RPA-compliant PPC64 platform.
3 * Copyright (C) 2003 Linda Xie <lxie@us.ibm.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or (at
10 * your option) any later version.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
15 * NON INFRINGEMENT. See the GNU General Public License for more
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 * Send feedback to <lxie@us.ibm.com>
25 #include <linux/config.h>
26 #include <linux/kernel.h>
27 #include <linux/module.h>
28 #include <linux/moduleparam.h>
29 #include <linux/pci.h>
30 #include <linux/slab.h>
31 #include <linux/smp.h>
32 #include <linux/smp_lock.h>
33 #include <linux/init.h>
34 #include <asm/eeh.h> /* for eeh_add_device() */
35 #include <asm/rtas.h> /* rtas_call */
36 #include <asm/pci-bridge.h> /* for pci_controller */
37 #include "../pci.h" /* for pci_add_new_bus */
38 /* and pci_do_scan_bus */
40 #include "pci_hotplug.h"
43 static struct semaphore rpaphp_sem;
44 LIST_HEAD(rpaphp_slot_head);
47 #define DRIVER_VERSION "0.1"
48 #define DRIVER_AUTHOR "Linda Xie <lxie@us.ibm.com>"
49 #define DRIVER_DESC "RPA HOT Plug PCI Controller Driver"
51 #define MAX_LOC_CODE 128
53 MODULE_AUTHOR(DRIVER_AUTHOR);
54 MODULE_DESCRIPTION(DRIVER_DESC);
55 MODULE_LICENSE("GPL");
57 void eeh_register_disable_func(int (*)(struct pci_dev *));
59 module_param(debug, bool, 0644);
61 static int enable_slot(struct hotplug_slot *slot);
62 static int disable_slot(struct hotplug_slot *slot);
63 static int set_attention_status(struct hotplug_slot *slot, u8 value);
64 static int get_power_status(struct hotplug_slot *slot, u8 * value);
65 static int get_attention_status(struct hotplug_slot *slot, u8 * value);
66 static int get_adapter_status(struct hotplug_slot *slot, u8 * value);
67 static int get_max_bus_speed(struct hotplug_slot *hotplug_slot, enum pci_bus_speed *value);
68 static int rpaphp_disable_slot(struct pci_dev *dev);
70 struct hotplug_slot_ops rpaphp_hotplug_slot_ops = {
72 .enable_slot = enable_slot,
73 .disable_slot = disable_slot,
74 .set_attention_status = set_attention_status,
75 .get_power_status = get_power_status,
76 .get_attention_status = get_attention_status,
77 .get_adapter_status = get_adapter_status,
78 .get_max_bus_speed = get_max_bus_speed,
81 static int rpaphp_get_attention_status(struct slot *slot)
83 return slot->hotplug_slot->info->attention_status;
87 * set_attention_status - set attention LED
88 * echo 0 > attention -- set LED OFF
89 * echo 1 > attention -- set LED ON
90 * echo 2 > attention -- set LED ID(identify, light is blinking)
93 static int set_attention_status(struct hotplug_slot *hotplug_slot, u8 value)
96 struct slot *slot = (struct slot *)hotplug_slot->private;
101 retval = rpaphp_set_attention_status(slot, LED_OFF);
102 hotplug_slot->info->attention_status = 0;
106 retval = rpaphp_set_attention_status(slot, LED_ON);
107 hotplug_slot->info->attention_status = 1;
110 retval = rpaphp_set_attention_status(slot, LED_ID);
111 hotplug_slot->info->attention_status = 2;
119 * get_power_status - get power status of a slot
120 * @hotplug_slot: slot to get status
121 * @value: pointer to store status
125 static int get_power_status(struct hotplug_slot *hotplug_slot, u8 * value)
128 struct slot *slot = (struct slot *)hotplug_slot->private;
131 retval = rpaphp_get_power_status(slot, value);
137 * get_attention_status - get attention LED status
141 static int get_attention_status(struct hotplug_slot *hotplug_slot, u8 * value)
144 struct slot *slot = (struct slot *)hotplug_slot->private;
147 *value = rpaphp_get_attention_status(slot);
152 static int get_adapter_status(struct hotplug_slot *hotplug_slot, u8 * value)
154 struct slot *slot = (struct slot *)hotplug_slot->private;
158 /* have to go through this */
159 switch (slot->dev_type) {
161 retval = rpaphp_get_pci_adapter_status(slot, 0, value);
164 retval = rpaphp_get_vio_adapter_status(slot, 0, value);
173 static int get_max_bus_speed(struct hotplug_slot *hotplug_slot, enum pci_bus_speed *value)
175 struct slot *slot = (struct slot *)hotplug_slot->private;
178 switch (slot->type) {
185 *value = PCI_SPEED_33MHz; /* speed for case 1-6 */
189 *value = PCI_SPEED_66MHz;
193 *value = PCI_SPEED_66MHz_PCIX;
197 *value = PCI_SPEED_100MHz_PCIX;
201 *value = PCI_SPEED_133MHz_PCIX;
204 *value = PCI_SPEED_UNKNOWN;
212 int rpaphp_remove_slot(struct slot *slot)
214 return deregister_slot(slot);
217 static int get_dn_properties(struct device_node *dn, int **indexes, int **names,
218 int **types, int **power_domains)
220 *indexes = (int *) get_property(dn, "ibm,drc-indexes", NULL);
222 /* &names[1] contains NULL terminated slot names */
223 *names = (int *) get_property(dn, "ibm,drc-names", NULL);
225 /* &types[1] contains NULL terminated slot types */
226 *types = (int *) get_property(dn, "ibm,drc-types", NULL);
228 /* power_domains[1...n] are the slot power domains */
229 *power_domains = (int *) get_property(dn, "ibm,drc-power-domains", NULL);
231 if (*indexes && *names && *types && *power_domains)
237 static int is_php_dn(struct device_node *dn, int **indexes, int **names, int **types,
240 if (!is_hotplug_capable(dn))
242 if (!get_dn_properties(dn, indexes, names, types, power_domains))
247 static int is_dr_dn(struct device_node *dn, int **indexes, int **names, int **types,
248 int **power_domains, int **my_drc_index)
250 if (!is_hotplug_capable(dn))
253 *my_drc_index = (int *) get_property(dn, "ibm,my-drc-index", NULL);
260 return get_dn_properties(dn->parent, indexes, names, types, power_domains);
263 static inline int is_vdevice_root(struct device_node *dn)
265 return !strcmp(dn->name, "vdevice");
268 char *rpaphp_get_drc_name(struct device_node *dn)
270 char *name, *ptr = NULL;
271 int *drc_names, *drc_indexes, i;
272 struct device_node *parent = dn->parent;
278 my_drc_index = (u32 *) get_property(dn, "ibm,my-drc-index", NULL);
282 drc_names = (int *) get_property(parent, "ibm,drc-names", NULL);
283 drc_indexes = (int *) get_property(parent, "ibm,drc-indexes", NULL);
284 if (!drc_names || !drc_indexes)
287 name = (char *) &drc_names[1];
288 for (i = 0; i < drc_indexes[0]; i++, name += (strlen(name) + 1)) {
289 if (drc_indexes[i + 1] == *my_drc_index) {
298 /****************************************************************
299 * rpaphp not only registers PCI hotplug slots(HOTPLUG),
300 * but also logical DR slots(EMBEDDED).
301 * HOTPLUG slot: An adapter can be physically added/removed.
302 * EMBEDDED slot: An adapter can be logically removed/added
303 * from/to a partition with the slot.
304 ***************************************************************/
305 int rpaphp_add_slot(struct device_node *dn)
309 int i, *my_drc_index, slot_type;
310 int *indexes, *names, *types, *power_domains;
313 dbg("Entry %s: dn->full_name=%s\n", __FUNCTION__, dn->full_name);
315 if (dn->parent && is_vdevice_root(dn->parent)) {
316 /* register a VIO device */
317 retval = register_vio_slot(dn);
321 /* register PCI devices */
322 if (dn->name != 0 && strcmp(dn->name, "pci") == 0) {
323 if (is_php_dn(dn, &indexes, &names, &types, &power_domains))
325 else if (is_dr_dn(dn, &indexes, &names, &types, &power_domains, &my_drc_index))
326 slot_type = EMBEDDED;
329 name = (char *) &names[1];
330 type = (char *) &types[1];
331 for (i = 0; i < indexes[0]; i++,
332 name += (strlen(name) + 1), type += (strlen(type) + 1)) {
334 if ( slot_type == HOTPLUG ||
335 (slot_type == EMBEDDED && indexes[i + 1] == my_drc_index[0])) {
337 if (!(slot = alloc_slot_struct(dn, indexes[i + 1], name,
338 power_domains[i + 1]))) {
342 if (slot_type == EMBEDDED)
343 slot->type = EMBEDDED;
345 slot->type = simple_strtoul(type, NULL, 10);
347 dbg(" Found drc-index:0x%x drc-name:%s drc-type:%s\n",
348 indexes[i + 1], name, type);
350 retval = register_pci_slot(slot);
351 if (slot_type == EMBEDDED)
357 dbg("%s - Exit: num_slots=%d rc[%d]\n",
358 __FUNCTION__, num_slots, retval);
363 * init_slots - initialize 'struct slot' structures for each slot
366 static void init_slots(void)
368 struct device_node *dn;
370 for (dn = find_all_nodes(); dn; dn = dn->next)
374 static int __init init_rpa(void)
377 init_MUTEX(&rpaphp_sem);
379 /* initialize internal data structure etc. */
387 static void __exit cleanup_slots(void)
389 struct list_head *tmp, *n;
393 * Unregister all of our slots with the pci_hotplug subsystem,
394 * and free up all memory that we had allocated.
395 * memory will be freed in release_slot callback.
398 list_for_each_safe(tmp, n, &rpaphp_slot_head) {
399 slot = list_entry(tmp, struct slot, rpaphp_slot_list);
400 list_del(&slot->rpaphp_slot_list);
401 pci_hp_deregister(slot->hotplug_slot);
406 static int __init rpaphp_init(void)
408 info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
410 /* let EEH know they can use hotplug */
411 eeh_register_disable_func(&rpaphp_disable_slot);
413 /* read all the PRA info from the system */
417 static void __exit rpaphp_exit(void)
419 /* let EEH know we are going away */
420 eeh_register_disable_func(NULL);
425 static int enable_slot(struct hotplug_slot *hotplug_slot)
428 struct slot *slot = (struct slot *)hotplug_slot->private;
430 if (slot->state == CONFIGURED) {
431 dbg("%s: %s is already enabled\n", __FUNCTION__, slot->name);
435 dbg("ENABLING SLOT %s\n", slot->name);
437 switch (slot->dev_type) {
439 retval = rpaphp_enable_pci_slot(slot);
442 retval = rpaphp_enable_vio_slot(slot);
449 dbg("%s - Exit: rc[%d]\n", __FUNCTION__, retval);
453 static int rpaphp_disable_slot(struct pci_dev *dev)
455 return disable_slot(rpaphp_find_hotplug_slot(dev));
458 static int disable_slot(struct hotplug_slot *hotplug_slot)
461 struct slot *slot = (struct slot *)hotplug_slot->private;
463 dbg("%s - Entry: slot[%s]\n", __FUNCTION__, slot->name);
465 if (slot->state == NOT_CONFIGURED) {
466 dbg("%s: %s is already disabled\n", __FUNCTION__, slot->name);
470 dbg("DISABLING SLOT %s\n", slot->name);
472 switch (slot->dev_type) {
474 retval = rpaphp_unconfig_pci_adapter(slot);
477 retval = rpaphp_unconfig_vio_adapter(slot);
484 dbg("%s - Exit: rc[%d]\n", __FUNCTION__, retval);
488 module_init(rpaphp_init);
489 module_exit(rpaphp_exit);
491 EXPORT_SYMBOL_GPL(rpaphp_add_slot);
492 EXPORT_SYMBOL_GPL(rpaphp_remove_slot);
493 EXPORT_SYMBOL_GPL(rpaphp_slot_head);
494 EXPORT_SYMBOL_GPL(rpaphp_get_drc_name);