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/kernel.h>
26 #include <linux/module.h>
27 #include <linux/moduleparam.h>
28 #include <linux/pci.h>
29 #include <linux/pci_hotplug.h>
30 #include <linux/smp.h>
31 #include <linux/init.h>
32 #include <linux/vmalloc.h>
33 #include <asm/firmware.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 */
42 LIST_HEAD(rpaphp_slot_head);
43 EXPORT_SYMBOL_GPL(rpaphp_slot_head);
45 #define DRIVER_VERSION "0.1"
46 #define DRIVER_AUTHOR "Linda Xie <lxie@us.ibm.com>"
47 #define DRIVER_DESC "RPA HOT Plug PCI Controller Driver"
49 #define MAX_LOC_CODE 128
51 MODULE_AUTHOR(DRIVER_AUTHOR);
52 MODULE_DESCRIPTION(DRIVER_DESC);
53 MODULE_LICENSE("GPL");
55 module_param_named(debug, rpaphp_debug, bool, 0644);
58 * set_attention_status - set attention LED
59 * @hotplug_slot: target &hotplug_slot
60 * @value: LED control value
62 * echo 0 > attention -- set LED OFF
63 * echo 1 > attention -- set LED ON
64 * echo 2 > attention -- set LED ID(identify, light is blinking)
66 static int set_attention_status(struct hotplug_slot *hotplug_slot, u8 value)
69 struct slot *slot = (struct slot *)hotplug_slot->private;
81 rc = rtas_set_indicator(DR_INDICATOR, slot->index, value);
83 hotplug_slot->info->attention_status = value;
89 * get_power_status - get power status of a slot
90 * @hotplug_slot: slot to get status
91 * @value: pointer to store status
93 static int get_power_status(struct hotplug_slot *hotplug_slot, u8 *value)
96 struct slot *slot = (struct slot *)hotplug_slot->private;
98 retval = rtas_get_power_level(slot->power_domain, &level);
105 * get_attention_status - get attention LED status
106 * @hotplug_slot: slot to get status
107 * @value: pointer to store status
109 static int get_attention_status(struct hotplug_slot *hotplug_slot, u8 *value)
111 struct slot *slot = (struct slot *)hotplug_slot->private;
112 *value = slot->hotplug_slot->info->attention_status;
116 static int get_adapter_status(struct hotplug_slot *hotplug_slot, u8 *value)
118 struct slot *slot = (struct slot *)hotplug_slot->private;
121 rc = rpaphp_get_sensor_state(slot, &state);
129 else if (state == PRESENT)
130 *value = slot->state;
135 static enum pci_bus_speed get_max_bus_speed(struct slot *slot)
137 enum pci_bus_speed speed;
138 switch (slot->type) {
145 speed = PCI_SPEED_33MHz; /* speed for case 1-6 */
149 speed = PCI_SPEED_66MHz;
153 speed = PCI_SPEED_66MHz_PCIX;
157 speed = PCI_SPEED_100MHz_PCIX;
161 speed = PCI_SPEED_133MHz_PCIX;
164 speed = PCI_SPEED_UNKNOWN;
171 static int get_children_props(struct device_node *dn, const int **drc_indexes,
172 const int **drc_names, const int **drc_types,
173 const int **drc_power_domains)
175 const int *indexes, *names, *types, *domains;
177 indexes = of_get_property(dn, "ibm,drc-indexes", NULL);
178 names = of_get_property(dn, "ibm,drc-names", NULL);
179 types = of_get_property(dn, "ibm,drc-types", NULL);
180 domains = of_get_property(dn, "ibm,drc-power-domains", NULL);
182 if (!indexes || !names || !types || !domains) {
183 /* Slot does not have dynamically-removable children */
187 *drc_indexes = indexes;
189 /* &drc_names[1] contains NULL terminated slot names */
192 /* &drc_types[1] contains NULL terminated slot types */
194 if (drc_power_domains)
195 *drc_power_domains = domains;
201 /* Verify the existence of 'drc_name' and/or 'drc_type' within the
202 * current node. First obtain it's my-drc-index property. Next,
203 * obtain the DRC info from it's parent. Use the my-drc-index for
204 * correlation, and obtain/validate the requested properties.
207 static int rpaphp_check_drc_props_v1(struct device_node *dn, char *drc_name,
208 char *drc_type, unsigned int my_index)
210 char *name_tmp, *type_tmp;
211 const int *indexes, *names;
212 const int *types, *domains;
215 rc = get_children_props(dn->parent, &indexes, &names, &types, &domains);
220 name_tmp = (char *) &names[1];
221 type_tmp = (char *) &types[1];
223 /* Iterate through parent properties, looking for my-drc-index */
224 for (i = 0; i < be32_to_cpu(indexes[0]); i++) {
225 if ((unsigned int) indexes[i + 1] == my_index)
228 name_tmp += (strlen(name_tmp) + 1);
229 type_tmp += (strlen(type_tmp) + 1);
232 if (((drc_name == NULL) || (drc_name && !strcmp(drc_name, name_tmp))) &&
233 ((drc_type == NULL) || (drc_type && !strcmp(drc_type, type_tmp))))
239 static int rpaphp_check_drc_props_v2(struct device_node *dn, char *drc_name,
240 char *drc_type, unsigned int my_index)
242 struct property *info;
243 unsigned int entries;
244 struct of_drc_info drc;
246 char cell_drc_name[MAX_DRC_NAME_LEN];
249 info = of_find_property(dn->parent, "ibm,drc-info", NULL);
253 value = of_prop_next_u32(info, NULL, &entries);
257 for (j = 0; j < entries; j++) {
258 of_read_drc_info_cell(&info, &value, &drc);
260 /* Should now know end of current entry */
262 if (my_index > drc.last_drc_index)
271 sprintf(cell_drc_name, "%s%d", drc.drc_name_prefix,
274 if (((drc_name == NULL) ||
275 (drc_name && !strcmp(drc_name, cell_drc_name))) &&
276 ((drc_type == NULL) ||
277 (drc_type && !strcmp(drc_type, drc.drc_type))))
283 int rpaphp_check_drc_props(struct device_node *dn, char *drc_name,
286 const unsigned int *my_index;
288 my_index = of_get_property(dn, "ibm,my-drc-index", NULL);
290 /* Node isn't DLPAR/hotplug capable */
294 if (firmware_has_feature(FW_FEATURE_DRC_INFO))
295 return rpaphp_check_drc_props_v2(dn, drc_name, drc_type,
298 return rpaphp_check_drc_props_v1(dn, drc_name, drc_type,
301 EXPORT_SYMBOL_GPL(rpaphp_check_drc_props);
304 static int is_php_type(char *drc_type)
309 /* PCI Hotplug nodes have an integer for drc_type */
310 value = simple_strtoul(drc_type, &endptr, 10);
311 if (endptr == drc_type)
318 * is_php_dn() - return 1 if this is a hotpluggable pci slot, else 0
319 * @dn: target &device_node
320 * @indexes: passed to get_children_props()
321 * @names: passed to get_children_props()
322 * @types: returned from get_children_props()
325 * This routine will return true only if the device node is
326 * a hotpluggable slot. This routine will return false
327 * for built-in pci slots (even when the built-in slots are
330 static int is_php_dn(struct device_node *dn, const int **indexes,
331 const int **names, const int **types, const int **power_domains)
333 const int *drc_types;
336 rc = get_children_props(dn, indexes, names, &drc_types, power_domains);
340 if (!is_php_type((char *) &drc_types[1]))
348 * rpaphp_add_slot -- declare a hotplug slot to the hotplug subsystem.
349 * @dn: device node of slot
351 * This subroutine will register a hotpluggable slot with the
352 * PCI hotplug infrastructure. This routine is typically called
353 * during boot time, if the hotplug slots are present at boot time,
354 * or is called later, by the dlpar add code, if the slot is
355 * being dynamically added during runtime.
357 * If the device node points at an embedded (built-in) slot, this
358 * routine will just return without doing anything, since embedded
359 * slots cannot be hotplugged.
361 * To remove a slot, it suffices to call rpaphp_deregister_slot().
363 int rpaphp_add_slot(struct device_node *dn)
368 const int *indexes, *names, *types, *power_domains;
371 if (!dn->name || strcmp(dn->name, "pci"))
374 /* If this is not a hotplug slot, return without doing anything. */
375 if (!is_php_dn(dn, &indexes, &names, &types, &power_domains))
378 dbg("Entry %s: dn=%pOF\n", __func__, dn);
380 /* register PCI devices */
381 name = (char *) &names[1];
382 type = (char *) &types[1];
383 for (i = 0; i < be32_to_cpu(indexes[0]); i++) {
386 index = be32_to_cpu(indexes[i + 1]);
387 slot = alloc_slot_struct(dn, index, name,
388 be32_to_cpu(power_domains[i + 1]));
392 slot->type = simple_strtoul(type, NULL, 10);
394 dbg("Found drc-index:0x%x drc-name:%s drc-type:%s\n",
397 retval = rpaphp_enable_slot(slot);
399 retval = rpaphp_register_slot(slot);
402 dealloc_slot_struct(slot);
404 name += strlen(name) + 1;
405 type += strlen(type) + 1;
407 dbg("%s - Exit: rc[%d]\n", __func__, retval);
409 /* XXX FIXME: reports a failure only if last entry in loop failed */
412 EXPORT_SYMBOL_GPL(rpaphp_add_slot);
414 static void __exit cleanup_slots(void)
416 struct slot *slot, *next;
419 * Unregister all of our slots with the pci_hotplug subsystem,
420 * and free up all memory that we had allocated.
421 * memory will be freed in release_slot callback.
424 list_for_each_entry_safe(slot, next, &rpaphp_slot_head,
426 list_del(&slot->rpaphp_slot_list);
427 pci_hp_deregister(slot->hotplug_slot);
432 static int __init rpaphp_init(void)
434 struct device_node *dn;
436 info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
438 for_each_node_by_name(dn, "pci")
444 static void __exit rpaphp_exit(void)
449 static int enable_slot(struct hotplug_slot *hotplug_slot)
451 struct slot *slot = (struct slot *)hotplug_slot->private;
455 if (slot->state == CONFIGURED)
458 retval = rpaphp_get_sensor_state(slot, &state);
462 if (state == PRESENT) {
463 pci_lock_rescan_remove();
464 pci_hp_add_devices(slot->bus);
465 pci_unlock_rescan_remove();
466 slot->state = CONFIGURED;
467 } else if (state == EMPTY) {
470 err("%s: slot[%s] is in invalid state\n", __func__, slot->name);
471 slot->state = NOT_VALID;
475 slot->bus->max_bus_speed = get_max_bus_speed(slot);
479 static int disable_slot(struct hotplug_slot *hotplug_slot)
481 struct slot *slot = (struct slot *)hotplug_slot->private;
482 if (slot->state == NOT_CONFIGURED)
485 pci_lock_rescan_remove();
486 pci_hp_remove_devices(slot->bus);
487 pci_unlock_rescan_remove();
490 slot->state = NOT_CONFIGURED;
494 struct hotplug_slot_ops rpaphp_hotplug_slot_ops = {
495 .enable_slot = enable_slot,
496 .disable_slot = disable_slot,
497 .set_attention_status = set_attention_status,
498 .get_power_status = get_power_status,
499 .get_attention_status = get_attention_status,
500 .get_adapter_status = get_adapter_status,
503 module_init(rpaphp_init);
504 module_exit(rpaphp_exit);