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 module_param(debug, bool, 0644);
59 static int enable_slot(struct hotplug_slot *slot);
60 static int disable_slot(struct hotplug_slot *slot);
61 static int set_attention_status(struct hotplug_slot *slot, u8 value);
62 static int get_power_status(struct hotplug_slot *slot, u8 * value);
63 static int get_attention_status(struct hotplug_slot *slot, u8 * value);
64 static int get_adapter_status(struct hotplug_slot *slot, u8 * value);
65 static int get_max_bus_speed(struct hotplug_slot *hotplug_slot, enum pci_bus_speed *value);
67 struct hotplug_slot_ops rpaphp_hotplug_slot_ops = {
69 .enable_slot = enable_slot,
70 .disable_slot = disable_slot,
71 .set_attention_status = set_attention_status,
72 .get_power_status = get_power_status,
73 .get_attention_status = get_attention_status,
74 .get_adapter_status = get_adapter_status,
75 .get_max_bus_speed = get_max_bus_speed,
78 static int rpaphp_get_attention_status(struct slot *slot)
80 return slot->hotplug_slot->info->attention_status;
84 * set_attention_status - set attention LED
85 * echo 0 > attention -- set LED OFF
86 * echo 1 > attention -- set LED ON
87 * echo 2 > attention -- set LED ID(identify, light is blinking)
90 static int set_attention_status(struct hotplug_slot *hotplug_slot, u8 value)
93 struct slot *slot = (struct slot *)hotplug_slot->private;
98 retval = rpaphp_set_attention_status(slot, LED_OFF);
99 hotplug_slot->info->attention_status = 0;
103 retval = rpaphp_set_attention_status(slot, LED_ON);
104 hotplug_slot->info->attention_status = 1;
107 retval = rpaphp_set_attention_status(slot, LED_ID);
108 hotplug_slot->info->attention_status = 2;
116 * get_power_status - get power status of a slot
117 * @hotplug_slot: slot to get status
118 * @value: pointer to store status
122 static int get_power_status(struct hotplug_slot *hotplug_slot, u8 * value)
125 struct slot *slot = (struct slot *)hotplug_slot->private;
128 retval = rpaphp_get_power_status(slot, value);
134 * get_attention_status - get attention LED status
138 static int get_attention_status(struct hotplug_slot *hotplug_slot, u8 * value)
141 struct slot *slot = (struct slot *)hotplug_slot->private;
144 *value = rpaphp_get_attention_status(slot);
149 static int get_adapter_status(struct hotplug_slot *hotplug_slot, u8 * value)
151 struct slot *slot = (struct slot *)hotplug_slot->private;
155 /* have to go through this */
156 switch (slot->dev_type) {
158 retval = rpaphp_get_pci_adapter_status(slot, 0, value);
161 retval = rpaphp_get_vio_adapter_status(slot, 0, value);
170 static int get_max_bus_speed(struct hotplug_slot *hotplug_slot, enum pci_bus_speed *value)
172 struct slot *slot = (struct slot *)hotplug_slot->private;
175 switch (slot->type) {
182 *value = PCI_SPEED_33MHz; /* speed for case 1-6 */
186 *value = PCI_SPEED_66MHz;
190 *value = PCI_SPEED_66MHz_PCIX;
194 *value = PCI_SPEED_100MHz_PCIX;
198 *value = PCI_SPEED_133MHz_PCIX;
201 *value = PCI_SPEED_UNKNOWN;
209 int rpaphp_remove_slot(struct slot *slot)
211 return deregister_slot(slot);
214 static int get_children_props(struct device_node *dn, int **drc_indexes,
215 int **drc_names, int **drc_types, int **drc_power_domains)
217 int *indexes, *names;
218 int *types, *domains;
220 indexes = (int *) get_property(dn, "ibm,drc-indexes", NULL);
221 names = (int *) get_property(dn, "ibm,drc-names", NULL);
222 types = (int *) get_property(dn, "ibm,drc-types", NULL);
223 domains = (int *) get_property(dn, "ibm,drc-power-domains", NULL);
225 if (!indexes || !names || !types || !domains) {
226 /* Slot does not have dynamically-removable children */
230 *drc_indexes = indexes;
232 /* &drc_names[1] contains NULL terminated slot names */
235 /* &drc_types[1] contains NULL terminated slot types */
237 if (drc_power_domains)
238 *drc_power_domains = domains;
243 /* To get the DRC props describing the current node, first obtain it's
244 * my-drc-index property. Next obtain the DRC list from it's parent. Use
245 * the my-drc-index for correlation, and obtain the requested properties.
247 int rpaphp_get_drc_props(struct device_node *dn, int *drc_index,
248 char **drc_name, char **drc_type, int *drc_power_domain)
250 int *indexes, *names;
251 int *types, *domains;
252 unsigned int *my_index;
253 char *name_tmp, *type_tmp;
256 my_index = (int *) get_property(dn, "ibm,my-drc-index", NULL);
258 /* Node isn't DLPAR/hotplug capable */
262 rc = get_children_props(dn->parent, &indexes, &names, &types, &domains);
267 name_tmp = (char *) &names[1];
268 type_tmp = (char *) &types[1];
270 /* Iterate through parent properties, looking for my-drc-index */
271 for (i = 0; i < indexes[0]; i++) {
272 if ((unsigned int) indexes[i + 1] == *my_index) {
274 *drc_name = name_tmp;
276 *drc_type = type_tmp;
278 *drc_index = *my_index;
279 if (drc_power_domain)
280 *drc_power_domain = domains[i+1];
283 name_tmp += (strlen(name_tmp) + 1);
284 type_tmp += (strlen(type_tmp) + 1);
290 static int is_php_type(char *drc_type)
295 /* PCI Hotplug nodes have an integer for drc_type */
296 value = simple_strtoul(drc_type, &endptr, 10);
297 if (endptr == drc_type)
303 static int is_php_dn(struct device_node *dn, int **indexes, int **names,
304 int **types, int **power_domains)
309 rc = get_children_props(dn, indexes, names, &drc_types, power_domains);
311 if (is_php_type((char *) &drc_types[1])) {
320 static int is_dr_dn(struct device_node *dn, int **indexes, int **names,
321 int **types, int **power_domains, int **my_drc_index)
325 *my_drc_index = (int *) get_property(dn, "ibm,my-drc-index", NULL);
332 rc = get_children_props(dn->parent, indexes, names, types,
337 static inline int is_vdevice_root(struct device_node *dn)
339 return !strcmp(dn->name, "vdevice");
342 int is_dlpar_type(const char *type_str)
344 /* Only register DLPAR-capable nodes of drc-type PHB or SLOT */
345 return (!strcmp(type_str, "PHB") || !strcmp(type_str, "SLOT"));
348 /****************************************************************
349 * rpaphp not only registers PCI hotplug slots(HOTPLUG),
350 * but also logical DR slots(EMBEDDED).
351 * HOTPLUG slot: An adapter can be physically added/removed.
352 * EMBEDDED slot: An adapter can be logically removed/added
353 * from/to a partition with the slot.
354 ***************************************************************/
355 int rpaphp_add_slot(struct device_node *dn)
359 int i, *my_drc_index, slot_type;
360 int *indexes, *names, *types, *power_domains;
363 dbg("Entry %s: dn->full_name=%s\n", __FUNCTION__, dn->full_name);
365 if (dn->parent && is_vdevice_root(dn->parent)) {
366 /* register a VIO device */
367 retval = register_vio_slot(dn);
371 /* register PCI devices */
372 if (dn->name != 0 && strcmp(dn->name, "pci") == 0) {
373 if (is_php_dn(dn, &indexes, &names, &types, &power_domains))
375 else if (is_dr_dn(dn, &indexes, &names, &types, &power_domains, &my_drc_index))
376 slot_type = EMBEDDED;
379 name = (char *) &names[1];
380 type = (char *) &types[1];
381 for (i = 0; i < indexes[0]; i++,
382 name += (strlen(name) + 1), type += (strlen(type) + 1)) {
384 if (slot_type == HOTPLUG ||
385 (slot_type == EMBEDDED &&
386 indexes[i + 1] == my_drc_index[0] &&
387 is_dlpar_type(type))) {
388 if (!(slot = alloc_slot_struct(dn, indexes[i + 1], name,
389 power_domains[i + 1]))) {
393 if (!strcmp(type, "PHB"))
395 else if (slot_type == EMBEDDED)
396 slot->type = EMBEDDED;
398 slot->type = simple_strtoul(type, NULL, 10);
400 dbg(" Found drc-index:0x%x drc-name:%s drc-type:%s\n",
401 indexes[i + 1], name, type);
403 retval = register_pci_slot(slot);
404 if (slot_type == EMBEDDED)
410 dbg("%s - Exit: num_slots=%d rc[%d]\n",
411 __FUNCTION__, num_slots, retval);
416 * init_slots - initialize 'struct slot' structures for each slot
419 static void init_slots(void)
421 struct device_node *dn;
423 for (dn = find_all_nodes(); dn; dn = dn->next)
427 static int __init init_rpa(void)
430 init_MUTEX(&rpaphp_sem);
432 /* initialize internal data structure etc. */
440 static void __exit cleanup_slots(void)
442 struct list_head *tmp, *n;
446 * Unregister all of our slots with the pci_hotplug subsystem,
447 * and free up all memory that we had allocated.
448 * memory will be freed in release_slot callback.
451 list_for_each_safe(tmp, n, &rpaphp_slot_head) {
452 slot = list_entry(tmp, struct slot, rpaphp_slot_list);
453 list_del(&slot->rpaphp_slot_list);
454 pci_hp_deregister(slot->hotplug_slot);
459 static int __init rpaphp_init(void)
461 info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
463 /* read all the PRA info from the system */
467 static void __exit rpaphp_exit(void)
472 static int enable_slot(struct hotplug_slot *hotplug_slot)
475 struct slot *slot = (struct slot *)hotplug_slot->private;
477 if (slot->state == CONFIGURED) {
478 dbg("%s: %s is already enabled\n", __FUNCTION__, slot->name);
482 dbg("ENABLING SLOT %s\n", slot->name);
484 switch (slot->dev_type) {
486 retval = rpaphp_enable_pci_slot(slot);
489 retval = rpaphp_enable_vio_slot(slot);
496 dbg("%s - Exit: rc[%d]\n", __FUNCTION__, retval);
500 static int disable_slot(struct hotplug_slot *hotplug_slot)
502 int retval = -EINVAL;
503 struct slot *slot = (struct slot *)hotplug_slot->private;
505 dbg("%s - Entry: slot[%s]\n", __FUNCTION__, slot->name);
507 if (slot->state == NOT_CONFIGURED) {
508 dbg("%s: %s is already disabled\n", __FUNCTION__, slot->name);
512 dbg("DISABLING SLOT %s\n", slot->name);
514 switch (slot->dev_type) {
516 retval = rpaphp_unconfig_pci_adapter(slot);
519 retval = rpaphp_unconfig_vio_adapter(slot);
526 dbg("%s - Exit: rc[%d]\n", __FUNCTION__, retval);
530 module_init(rpaphp_init);
531 module_exit(rpaphp_exit);
533 EXPORT_SYMBOL_GPL(rpaphp_add_slot);
534 EXPORT_SYMBOL_GPL(rpaphp_remove_slot);
535 EXPORT_SYMBOL_GPL(rpaphp_slot_head);
536 EXPORT_SYMBOL_GPL(rpaphp_get_drc_props);