Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * PCI Hot Plug Controller Driver for RPA-compliant PPC64 platform. |
| 3 | * Copyright (C) 2003 Linda Xie <lxie@us.ibm.com> |
| 4 | * |
| 5 | * All rights reserved. |
| 6 | * |
| 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. |
| 11 | * |
| 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 |
| 16 | * details. |
| 17 | * |
| 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. |
| 21 | * |
| 22 | * Send feedback to <lxie@us.ibm.com> |
| 23 | * |
| 24 | */ |
| 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 */ |
| 39 | #include "rpaphp.h" |
| 40 | #include "pci_hotplug.h" |
| 41 | |
| 42 | int debug; |
| 43 | static struct semaphore rpaphp_sem; |
| 44 | LIST_HEAD(rpaphp_slot_head); |
| 45 | int num_slots; |
| 46 | |
| 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" |
| 50 | |
| 51 | #define MAX_LOC_CODE 128 |
| 52 | |
| 53 | MODULE_AUTHOR(DRIVER_AUTHOR); |
| 54 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 55 | MODULE_LICENSE("GPL"); |
| 56 | |
| 57 | module_param(debug, bool, 0644); |
| 58 | |
| 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); |
| 66 | |
| 67 | struct hotplug_slot_ops rpaphp_hotplug_slot_ops = { |
| 68 | .owner = THIS_MODULE, |
| 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, |
| 76 | }; |
| 77 | |
| 78 | static int rpaphp_get_attention_status(struct slot *slot) |
| 79 | { |
| 80 | return slot->hotplug_slot->info->attention_status; |
| 81 | } |
| 82 | |
| 83 | /** |
| 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) |
| 88 | * |
| 89 | */ |
| 90 | static int set_attention_status(struct hotplug_slot *hotplug_slot, u8 value) |
| 91 | { |
| 92 | int retval = 0; |
| 93 | struct slot *slot = (struct slot *)hotplug_slot->private; |
| 94 | |
| 95 | down(&rpaphp_sem); |
| 96 | switch (value) { |
| 97 | case 0: |
| 98 | retval = rpaphp_set_attention_status(slot, LED_OFF); |
| 99 | hotplug_slot->info->attention_status = 0; |
| 100 | break; |
| 101 | case 1: |
| 102 | default: |
| 103 | retval = rpaphp_set_attention_status(slot, LED_ON); |
| 104 | hotplug_slot->info->attention_status = 1; |
| 105 | break; |
| 106 | case 2: |
| 107 | retval = rpaphp_set_attention_status(slot, LED_ID); |
| 108 | hotplug_slot->info->attention_status = 2; |
| 109 | break; |
| 110 | } |
| 111 | up(&rpaphp_sem); |
| 112 | return retval; |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * get_power_status - get power status of a slot |
| 117 | * @hotplug_slot: slot to get status |
| 118 | * @value: pointer to store status |
| 119 | * |
| 120 | * |
| 121 | */ |
| 122 | static int get_power_status(struct hotplug_slot *hotplug_slot, u8 * value) |
| 123 | { |
| 124 | int retval; |
| 125 | struct slot *slot = (struct slot *)hotplug_slot->private; |
| 126 | |
| 127 | down(&rpaphp_sem); |
| 128 | retval = rpaphp_get_power_status(slot, value); |
| 129 | up(&rpaphp_sem); |
| 130 | return retval; |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * get_attention_status - get attention LED status |
| 135 | * |
| 136 | * |
| 137 | */ |
| 138 | static int get_attention_status(struct hotplug_slot *hotplug_slot, u8 * value) |
| 139 | { |
| 140 | int retval = 0; |
| 141 | struct slot *slot = (struct slot *)hotplug_slot->private; |
| 142 | |
| 143 | down(&rpaphp_sem); |
| 144 | *value = rpaphp_get_attention_status(slot); |
| 145 | up(&rpaphp_sem); |
| 146 | return retval; |
| 147 | } |
| 148 | |
| 149 | static int get_adapter_status(struct hotplug_slot *hotplug_slot, u8 * value) |
| 150 | { |
| 151 | struct slot *slot = (struct slot *)hotplug_slot->private; |
| 152 | int retval = 0; |
| 153 | |
| 154 | down(&rpaphp_sem); |
John Rose | 5eeb8c6 | 2005-07-25 10:16:42 -0500 | [diff] [blame^] | 155 | retval = rpaphp_get_pci_adapter_status(slot, 0, value); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | up(&rpaphp_sem); |
| 157 | return retval; |
| 158 | } |
| 159 | |
| 160 | static int get_max_bus_speed(struct hotplug_slot *hotplug_slot, enum pci_bus_speed *value) |
| 161 | { |
| 162 | struct slot *slot = (struct slot *)hotplug_slot->private; |
| 163 | |
| 164 | down(&rpaphp_sem); |
| 165 | switch (slot->type) { |
| 166 | case 1: |
| 167 | case 2: |
| 168 | case 3: |
| 169 | case 4: |
| 170 | case 5: |
| 171 | case 6: |
| 172 | *value = PCI_SPEED_33MHz; /* speed for case 1-6 */ |
| 173 | break; |
| 174 | case 7: |
| 175 | case 8: |
| 176 | *value = PCI_SPEED_66MHz; |
| 177 | break; |
| 178 | case 11: |
| 179 | case 14: |
| 180 | *value = PCI_SPEED_66MHz_PCIX; |
| 181 | break; |
| 182 | case 12: |
| 183 | case 15: |
| 184 | *value = PCI_SPEED_100MHz_PCIX; |
| 185 | break; |
| 186 | case 13: |
| 187 | case 16: |
| 188 | *value = PCI_SPEED_133MHz_PCIX; |
| 189 | break; |
| 190 | default: |
| 191 | *value = PCI_SPEED_UNKNOWN; |
| 192 | break; |
| 193 | |
| 194 | } |
| 195 | up(&rpaphp_sem); |
| 196 | return 0; |
| 197 | } |
| 198 | |
| 199 | int rpaphp_remove_slot(struct slot *slot) |
| 200 | { |
| 201 | return deregister_slot(slot); |
| 202 | } |
| 203 | |
| 204 | static int get_children_props(struct device_node *dn, int **drc_indexes, |
| 205 | int **drc_names, int **drc_types, int **drc_power_domains) |
| 206 | { |
| 207 | int *indexes, *names; |
| 208 | int *types, *domains; |
| 209 | |
| 210 | indexes = (int *) get_property(dn, "ibm,drc-indexes", NULL); |
| 211 | names = (int *) get_property(dn, "ibm,drc-names", NULL); |
| 212 | types = (int *) get_property(dn, "ibm,drc-types", NULL); |
| 213 | domains = (int *) get_property(dn, "ibm,drc-power-domains", NULL); |
| 214 | |
| 215 | if (!indexes || !names || !types || !domains) { |
| 216 | /* Slot does not have dynamically-removable children */ |
| 217 | return -EINVAL; |
| 218 | } |
| 219 | if (drc_indexes) |
| 220 | *drc_indexes = indexes; |
| 221 | if (drc_names) |
| 222 | /* &drc_names[1] contains NULL terminated slot names */ |
| 223 | *drc_names = names; |
| 224 | if (drc_types) |
| 225 | /* &drc_types[1] contains NULL terminated slot types */ |
| 226 | *drc_types = types; |
| 227 | if (drc_power_domains) |
| 228 | *drc_power_domains = domains; |
| 229 | |
| 230 | return 0; |
| 231 | } |
| 232 | |
| 233 | /* To get the DRC props describing the current node, first obtain it's |
| 234 | * my-drc-index property. Next obtain the DRC list from it's parent. Use |
| 235 | * the my-drc-index for correlation, and obtain the requested properties. |
| 236 | */ |
| 237 | int rpaphp_get_drc_props(struct device_node *dn, int *drc_index, |
| 238 | char **drc_name, char **drc_type, int *drc_power_domain) |
| 239 | { |
| 240 | int *indexes, *names; |
| 241 | int *types, *domains; |
| 242 | unsigned int *my_index; |
| 243 | char *name_tmp, *type_tmp; |
| 244 | int i, rc; |
| 245 | |
| 246 | my_index = (int *) get_property(dn, "ibm,my-drc-index", NULL); |
| 247 | if (!my_index) { |
| 248 | /* Node isn't DLPAR/hotplug capable */ |
| 249 | return -EINVAL; |
| 250 | } |
| 251 | |
| 252 | rc = get_children_props(dn->parent, &indexes, &names, &types, &domains); |
| 253 | if (rc < 0) { |
| 254 | return -EINVAL; |
| 255 | } |
| 256 | |
| 257 | name_tmp = (char *) &names[1]; |
| 258 | type_tmp = (char *) &types[1]; |
| 259 | |
| 260 | /* Iterate through parent properties, looking for my-drc-index */ |
| 261 | for (i = 0; i < indexes[0]; i++) { |
| 262 | if ((unsigned int) indexes[i + 1] == *my_index) { |
| 263 | if (drc_name) |
| 264 | *drc_name = name_tmp; |
| 265 | if (drc_type) |
| 266 | *drc_type = type_tmp; |
| 267 | if (drc_index) |
| 268 | *drc_index = *my_index; |
| 269 | if (drc_power_domain) |
| 270 | *drc_power_domain = domains[i+1]; |
| 271 | return 0; |
| 272 | } |
| 273 | name_tmp += (strlen(name_tmp) + 1); |
| 274 | type_tmp += (strlen(type_tmp) + 1); |
| 275 | } |
| 276 | |
| 277 | return -EINVAL; |
| 278 | } |
| 279 | |
| 280 | static int is_php_type(char *drc_type) |
| 281 | { |
| 282 | unsigned long value; |
| 283 | char *endptr; |
| 284 | |
| 285 | /* PCI Hotplug nodes have an integer for drc_type */ |
| 286 | value = simple_strtoul(drc_type, &endptr, 10); |
| 287 | if (endptr == drc_type) |
| 288 | return 0; |
| 289 | |
| 290 | return 1; |
| 291 | } |
| 292 | |
| 293 | static int is_php_dn(struct device_node *dn, int **indexes, int **names, |
| 294 | int **types, int **power_domains) |
| 295 | { |
| 296 | int *drc_types; |
| 297 | int rc; |
| 298 | |
| 299 | rc = get_children_props(dn, indexes, names, &drc_types, power_domains); |
| 300 | if (rc >= 0) { |
| 301 | if (is_php_type((char *) &drc_types[1])) { |
| 302 | *types = drc_types; |
| 303 | return 1; |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | return 0; |
| 308 | } |
| 309 | |
| 310 | static int is_dr_dn(struct device_node *dn, int **indexes, int **names, |
| 311 | int **types, int **power_domains, int **my_drc_index) |
| 312 | { |
| 313 | int rc; |
| 314 | |
| 315 | *my_drc_index = (int *) get_property(dn, "ibm,my-drc-index", NULL); |
| 316 | if(!*my_drc_index) |
| 317 | return (0); |
| 318 | |
| 319 | if (!dn->parent) |
| 320 | return (0); |
| 321 | |
| 322 | rc = get_children_props(dn->parent, indexes, names, types, |
| 323 | power_domains); |
| 324 | return (rc >= 0); |
| 325 | } |
| 326 | |
| 327 | static inline int is_vdevice_root(struct device_node *dn) |
| 328 | { |
| 329 | return !strcmp(dn->name, "vdevice"); |
| 330 | } |
| 331 | |
| 332 | int is_dlpar_type(const char *type_str) |
| 333 | { |
| 334 | /* Only register DLPAR-capable nodes of drc-type PHB or SLOT */ |
| 335 | return (!strcmp(type_str, "PHB") || !strcmp(type_str, "SLOT")); |
| 336 | } |
| 337 | |
| 338 | /**************************************************************** |
| 339 | * rpaphp not only registers PCI hotplug slots(HOTPLUG), |
| 340 | * but also logical DR slots(EMBEDDED). |
| 341 | * HOTPLUG slot: An adapter can be physically added/removed. |
| 342 | * EMBEDDED slot: An adapter can be logically removed/added |
| 343 | * from/to a partition with the slot. |
| 344 | ***************************************************************/ |
| 345 | int rpaphp_add_slot(struct device_node *dn) |
| 346 | { |
| 347 | struct slot *slot; |
| 348 | int retval = 0; |
| 349 | int i, *my_drc_index, slot_type; |
| 350 | int *indexes, *names, *types, *power_domains; |
| 351 | char *name, *type; |
| 352 | |
| 353 | dbg("Entry %s: dn->full_name=%s\n", __FUNCTION__, dn->full_name); |
| 354 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | /* register PCI devices */ |
| 356 | if (dn->name != 0 && strcmp(dn->name, "pci") == 0) { |
| 357 | if (is_php_dn(dn, &indexes, &names, &types, &power_domains)) |
| 358 | slot_type = HOTPLUG; |
| 359 | else if (is_dr_dn(dn, &indexes, &names, &types, &power_domains, &my_drc_index)) |
| 360 | slot_type = EMBEDDED; |
| 361 | else goto exit; |
| 362 | |
| 363 | name = (char *) &names[1]; |
| 364 | type = (char *) &types[1]; |
| 365 | for (i = 0; i < indexes[0]; i++, |
| 366 | name += (strlen(name) + 1), type += (strlen(type) + 1)) { |
| 367 | |
| 368 | if (slot_type == HOTPLUG || |
| 369 | (slot_type == EMBEDDED && |
| 370 | indexes[i + 1] == my_drc_index[0] && |
| 371 | is_dlpar_type(type))) { |
| 372 | if (!(slot = alloc_slot_struct(dn, indexes[i + 1], name, |
| 373 | power_domains[i + 1]))) { |
| 374 | retval = -ENOMEM; |
| 375 | goto exit; |
| 376 | } |
| 377 | if (!strcmp(type, "PHB")) |
| 378 | slot->type = PHB; |
| 379 | else if (slot_type == EMBEDDED) |
| 380 | slot->type = EMBEDDED; |
| 381 | else |
| 382 | slot->type = simple_strtoul(type, NULL, 10); |
| 383 | |
| 384 | dbg(" Found drc-index:0x%x drc-name:%s drc-type:%s\n", |
| 385 | indexes[i + 1], name, type); |
| 386 | |
| 387 | retval = register_pci_slot(slot); |
| 388 | if (slot_type == EMBEDDED) |
| 389 | goto exit; |
| 390 | } |
| 391 | } |
| 392 | } |
| 393 | exit: |
| 394 | dbg("%s - Exit: num_slots=%d rc[%d]\n", |
| 395 | __FUNCTION__, num_slots, retval); |
| 396 | return retval; |
| 397 | } |
| 398 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | static void __exit cleanup_slots(void) |
| 400 | { |
| 401 | struct list_head *tmp, *n; |
| 402 | struct slot *slot; |
| 403 | |
| 404 | /* |
| 405 | * Unregister all of our slots with the pci_hotplug subsystem, |
| 406 | * and free up all memory that we had allocated. |
| 407 | * memory will be freed in release_slot callback. |
| 408 | */ |
| 409 | |
| 410 | list_for_each_safe(tmp, n, &rpaphp_slot_head) { |
| 411 | slot = list_entry(tmp, struct slot, rpaphp_slot_list); |
| 412 | list_del(&slot->rpaphp_slot_list); |
| 413 | pci_hp_deregister(slot->hotplug_slot); |
| 414 | } |
| 415 | return; |
| 416 | } |
| 417 | |
| 418 | static int __init rpaphp_init(void) |
| 419 | { |
John Rose | 5eeb8c6 | 2005-07-25 10:16:42 -0500 | [diff] [blame^] | 420 | struct device_node *dn = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | |
John Rose | 5eeb8c6 | 2005-07-25 10:16:42 -0500 | [diff] [blame^] | 422 | info(DRIVER_DESC " version: " DRIVER_VERSION "\n"); |
| 423 | init_MUTEX(&rpaphp_sem); |
| 424 | |
| 425 | while ((dn = of_find_node_by_type(dn, "pci"))) |
| 426 | rpaphp_add_slot(dn); |
| 427 | |
| 428 | if (!num_slots) |
| 429 | return -ENODEV; |
| 430 | |
| 431 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | static void __exit rpaphp_exit(void) |
| 435 | { |
| 436 | cleanup_slots(); |
| 437 | } |
| 438 | |
| 439 | static int enable_slot(struct hotplug_slot *hotplug_slot) |
| 440 | { |
| 441 | int retval = 0; |
| 442 | struct slot *slot = (struct slot *)hotplug_slot->private; |
| 443 | |
| 444 | if (slot->state == CONFIGURED) { |
| 445 | dbg("%s: %s is already enabled\n", __FUNCTION__, slot->name); |
| 446 | goto exit; |
| 447 | } |
| 448 | |
| 449 | dbg("ENABLING SLOT %s\n", slot->name); |
| 450 | down(&rpaphp_sem); |
John Rose | 5eeb8c6 | 2005-07-25 10:16:42 -0500 | [diff] [blame^] | 451 | retval = rpaphp_enable_pci_slot(slot); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | up(&rpaphp_sem); |
| 453 | exit: |
| 454 | dbg("%s - Exit: rc[%d]\n", __FUNCTION__, retval); |
| 455 | return retval; |
| 456 | } |
| 457 | |
| 458 | static int disable_slot(struct hotplug_slot *hotplug_slot) |
| 459 | { |
| 460 | int retval = -EINVAL; |
| 461 | struct slot *slot = (struct slot *)hotplug_slot->private; |
| 462 | |
| 463 | dbg("%s - Entry: slot[%s]\n", __FUNCTION__, slot->name); |
| 464 | |
| 465 | if (slot->state == NOT_CONFIGURED) { |
| 466 | dbg("%s: %s is already disabled\n", __FUNCTION__, slot->name); |
| 467 | goto exit; |
| 468 | } |
| 469 | |
| 470 | dbg("DISABLING SLOT %s\n", slot->name); |
| 471 | down(&rpaphp_sem); |
John Rose | 5eeb8c6 | 2005-07-25 10:16:42 -0500 | [diff] [blame^] | 472 | retval = rpaphp_unconfig_pci_adapter(slot); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | up(&rpaphp_sem); |
| 474 | exit: |
| 475 | dbg("%s - Exit: rc[%d]\n", __FUNCTION__, retval); |
| 476 | return retval; |
| 477 | } |
| 478 | |
| 479 | module_init(rpaphp_init); |
| 480 | module_exit(rpaphp_exit); |
| 481 | |
| 482 | EXPORT_SYMBOL_GPL(rpaphp_add_slot); |
| 483 | EXPORT_SYMBOL_GPL(rpaphp_remove_slot); |
| 484 | EXPORT_SYMBOL_GPL(rpaphp_slot_head); |
| 485 | EXPORT_SYMBOL_GPL(rpaphp_get_drc_props); |