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/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 */
42 static struct semaphore rpaphp_sem;
43 LIST_HEAD(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(debug, bool, 0644);
58 * set_attention_status - set attention LED
59 * echo 0 > attention -- set LED OFF
60 * echo 1 > attention -- set LED ON
61 * echo 2 > attention -- set LED ID(identify, light is blinking)
64 static int set_attention_status(struct hotplug_slot *hotplug_slot, u8 value)
67 struct slot *slot = (struct slot *)hotplug_slot->private;
72 retval = rpaphp_set_attention_status(slot, LED_OFF);
73 hotplug_slot->info->attention_status = 0;
77 retval = rpaphp_set_attention_status(slot, LED_ON);
78 hotplug_slot->info->attention_status = 1;
81 retval = rpaphp_set_attention_status(slot, LED_ID);
82 hotplug_slot->info->attention_status = 2;
90 * get_power_status - get power status of a slot
91 * @hotplug_slot: slot to get status
92 * @value: pointer to store status
94 static int get_power_status(struct hotplug_slot *hotplug_slot, u8 * value)
97 struct slot *slot = (struct slot *)hotplug_slot->private;
100 retval = rtas_get_power_level (slot->power_domain, &level);
108 * get_attention_status - get attention LED status
110 static int get_attention_status(struct hotplug_slot *hotplug_slot, u8 * value)
112 struct slot *slot = (struct slot *)hotplug_slot->private;
113 *value = slot->hotplug_slot->info->attention_status;
117 static int get_adapter_status(struct hotplug_slot *hotplug_slot, u8 * value)
119 struct slot *slot = (struct slot *)hotplug_slot->private;
123 rc = rpaphp_get_sensor_state(slot, &state);
132 else if (state == PRESENT)
133 *value = slot->state;
138 static int get_max_bus_speed(struct hotplug_slot *hotplug_slot, enum pci_bus_speed *value)
140 struct slot *slot = (struct slot *)hotplug_slot->private;
143 switch (slot->type) {
150 *value = PCI_SPEED_33MHz; /* speed for case 1-6 */
154 *value = PCI_SPEED_66MHz;
158 *value = PCI_SPEED_66MHz_PCIX;
162 *value = PCI_SPEED_100MHz_PCIX;
166 *value = PCI_SPEED_133MHz_PCIX;
169 *value = PCI_SPEED_UNKNOWN;
177 static int get_children_props(struct device_node *dn, const int **drc_indexes,
178 const int **drc_names, const int **drc_types,
179 const int **drc_power_domains)
181 const int *indexes, *names, *types, *domains;
183 indexes = get_property(dn, "ibm,drc-indexes", NULL);
184 names = get_property(dn, "ibm,drc-names", NULL);
185 types = get_property(dn, "ibm,drc-types", NULL);
186 domains = get_property(dn, "ibm,drc-power-domains", NULL);
188 if (!indexes || !names || !types || !domains) {
189 /* Slot does not have dynamically-removable children */
193 *drc_indexes = indexes;
195 /* &drc_names[1] contains NULL terminated slot names */
198 /* &drc_types[1] contains NULL terminated slot types */
200 if (drc_power_domains)
201 *drc_power_domains = domains;
206 /* To get the DRC props describing the current node, first obtain it's
207 * my-drc-index property. Next obtain the DRC list from it's parent. Use
208 * the my-drc-index for correlation, and obtain the requested properties.
210 int rpaphp_get_drc_props(struct device_node *dn, int *drc_index,
211 char **drc_name, char **drc_type, int *drc_power_domain)
213 const int *indexes, *names;
214 const int *types, *domains;
215 const unsigned int *my_index;
216 char *name_tmp, *type_tmp;
219 my_index = get_property(dn, "ibm,my-drc-index", NULL);
221 /* Node isn't DLPAR/hotplug capable */
225 rc = get_children_props(dn->parent, &indexes, &names, &types, &domains);
230 name_tmp = (char *) &names[1];
231 type_tmp = (char *) &types[1];
233 /* Iterate through parent properties, looking for my-drc-index */
234 for (i = 0; i < indexes[0]; i++) {
235 if ((unsigned int) indexes[i + 1] == *my_index) {
237 *drc_name = name_tmp;
239 *drc_type = type_tmp;
241 *drc_index = *my_index;
242 if (drc_power_domain)
243 *drc_power_domain = domains[i+1];
246 name_tmp += (strlen(name_tmp) + 1);
247 type_tmp += (strlen(type_tmp) + 1);
253 static int is_php_type(char *drc_type)
258 /* PCI Hotplug nodes have an integer for drc_type */
259 value = simple_strtoul(drc_type, &endptr, 10);
260 if (endptr == drc_type)
266 static int is_php_dn(struct device_node *dn, const int **indexes,
267 const int **names, const int **types, const int **power_domains)
269 const int *drc_types;
272 rc = get_children_props(dn, indexes, names, &drc_types, power_domains);
274 if (is_php_type((char *) &drc_types[1])) {
284 * rpaphp_add_slot -- add hotplug or dlpar slot
286 * rpaphp not only registers PCI hotplug slots(HOTPLUG),
287 * but also logical DR slots(EMBEDDED).
288 * HOTPLUG slot: An adapter can be physically added/removed.
289 * EMBEDDED slot: An adapter can be logically removed/added
290 * from/to a partition with the slot.
292 int rpaphp_add_slot(struct device_node *dn)
297 const int *indexes, *names, *types, *power_domains;
300 if (!dn->name || strcmp(dn->name, "pci"))
303 if (!is_php_dn(dn, &indexes, &names, &types, &power_domains))
306 dbg("Entry %s: dn->full_name=%s\n", __FUNCTION__, dn->full_name);
308 /* register PCI devices */
309 name = (char *) &names[1];
310 type = (char *) &types[1];
311 for (i = 0; i < indexes[0]; i++) {
313 slot = alloc_slot_struct(dn, indexes[i + 1], name, power_domains[i + 1]);
317 slot->type = simple_strtoul(type, NULL, 10);
319 dbg("Found drc-index:0x%x drc-name:%s drc-type:%s\n",
320 indexes[i + 1], name, type);
322 retval = rpaphp_register_pci_slot(slot);
324 dealloc_slot_struct(slot);
326 name += strlen(name) + 1;
327 type += strlen(type) + 1;
329 dbg("%s - Exit: rc[%d]\n", __FUNCTION__, retval);
331 /* XXX FIXME: reports a failure only if last entry in loop failed */
335 static void __exit cleanup_slots(void)
337 struct list_head *tmp, *n;
341 * Unregister all of our slots with the pci_hotplug subsystem,
342 * and free up all memory that we had allocated.
343 * memory will be freed in release_slot callback.
346 list_for_each_safe(tmp, n, &rpaphp_slot_head) {
347 slot = list_entry(tmp, struct slot, rpaphp_slot_list);
348 list_del(&slot->rpaphp_slot_list);
349 pci_hp_deregister(slot->hotplug_slot);
354 static int __init rpaphp_init(void)
356 struct device_node *dn = NULL;
358 info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
359 init_MUTEX(&rpaphp_sem);
361 while ((dn = of_find_node_by_name(dn, "pci")))
367 static void __exit rpaphp_exit(void)
372 static int __enable_slot(struct slot *slot)
377 if (slot->state == CONFIGURED)
380 retval = rpaphp_get_sensor_state(slot, &state);
384 if (state == PRESENT) {
385 pcibios_add_pci_devices(slot->bus);
386 slot->state = CONFIGURED;
387 } else if (state == EMPTY) {
390 err("%s: slot[%s] is in invalid state\n", __FUNCTION__, slot->name);
391 slot->state = NOT_VALID;
397 static int enable_slot(struct hotplug_slot *hotplug_slot)
400 struct slot *slot = (struct slot *)hotplug_slot->private;
403 retval = __enable_slot(slot);
409 static int __disable_slot(struct slot *slot)
411 struct pci_dev *dev, *tmp;
413 if (slot->state == NOT_CONFIGURED)
416 list_for_each_entry_safe(dev, tmp, &slot->bus->devices, bus_list) {
417 eeh_remove_bus_device(dev);
418 pci_remove_bus_device(dev);
421 slot->state = NOT_CONFIGURED;
425 static int disable_slot(struct hotplug_slot *hotplug_slot)
427 struct slot *slot = (struct slot *)hotplug_slot->private;
431 retval = __disable_slot (slot);
437 struct hotplug_slot_ops rpaphp_hotplug_slot_ops = {
438 .owner = THIS_MODULE,
439 .enable_slot = enable_slot,
440 .disable_slot = disable_slot,
441 .set_attention_status = set_attention_status,
442 .get_power_status = get_power_status,
443 .get_attention_status = get_attention_status,
444 .get_adapter_status = get_adapter_status,
445 .get_max_bus_speed = get_max_bus_speed,
448 module_init(rpaphp_init);
449 module_exit(rpaphp_exit);
451 EXPORT_SYMBOL_GPL(rpaphp_add_slot);
452 EXPORT_SYMBOL_GPL(rpaphp_slot_head);
453 EXPORT_SYMBOL_GPL(rpaphp_get_drc_props);