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/pci.h> 26 #include <asm/pci-bridge.h> 27 #include <asm/rtas.h> 28 #include <asm/machdep.h> 29 #include "../pci.h" /* for pci_add_new_bus */ 30 31 #include "rpaphp.h" 32 33 static struct pci_bus *find_bus_among_children(struct pci_bus *bus, 34 struct device_node *dn) 35 { 36 struct pci_bus *child = NULL; 37 struct list_head *tmp; 38 struct device_node *busdn; 39 40 busdn = pci_bus_to_OF_node(bus); 41 if (busdn == dn) 42 return bus; 43 44 list_for_each(tmp, &bus->children) { 45 child = find_bus_among_children(pci_bus_b(tmp), dn); 46 if (child) 47 break; 48 } 49 return child; 50 } 51 52 static struct pci_bus *rpaphp_find_pci_bus(struct device_node *dn) 53 { 54 if (!dn->phb || !dn->phb->bus) 55 return NULL; 56 57 return find_bus_among_children(dn->phb->bus, dn); 58 } 59 60 int rpaphp_claim_resource(struct pci_dev *dev, int resource) 61 { 62 struct resource *res = &dev->resource[resource]; 63 struct resource *root = pci_find_parent_resource(dev, res); 64 char *dtype = resource < PCI_BRIDGE_RESOURCES ? "device" : "bridge"; 65 int err = -EINVAL; 66 67 if (root != NULL) { 68 err = request_resource(root, res); 69 } 70 71 if (err) { 72 err("PCI: %s region %d of %s %s [%lx:%lx]\n", 73 root ? "Address space collision on" : 74 "No parent found for", 75 resource, dtype, pci_name(dev), res->start, res->end); 76 } 77 return err; 78 } 79 80 EXPORT_SYMBOL_GPL(rpaphp_claim_resource); 81 82 static int rpaphp_get_sensor_state(struct slot *slot, int *state) 83 { 84 int rc; 85 int setlevel; 86 87 rc = rtas_get_sensor(DR_ENTITY_SENSE, slot->index, state); 88 89 if (rc < 0) { 90 if (rc == -EFAULT || rc == -EEXIST) { 91 dbg("%s: slot must be power up to get sensor-state\n", 92 __FUNCTION__); 93 94 /* some slots have to be powered up 95 * before get-sensor will succeed. 96 */ 97 rc = rtas_set_power_level(slot->power_domain, POWER_ON, 98 &setlevel); 99 if (rc < 0) { 100 dbg("%s: power on slot[%s] failed rc=%d.\n", 101 __FUNCTION__, slot->name, rc); 102 } else { 103 rc = rtas_get_sensor(DR_ENTITY_SENSE, 104 slot->index, state); 105 } 106 } else if (rc == -ENODEV) 107 info("%s: slot is unusable\n", __FUNCTION__); 108 else 109 err("%s failed to get sensor state\n", __FUNCTION__); 110 } 111 return rc; 112 } 113 114 /** 115 * get_pci_adapter_status - get the status of a slot 116 * 117 * 0-- slot is empty 118 * 1-- adapter is configured 119 * 2-- adapter is not configured 120 * 3-- not valid 121 */ 122 int rpaphp_get_pci_adapter_status(struct slot *slot, int is_init, u8 * value) 123 { 124 struct pci_bus *bus; 125 int state, rc; 126 127 *value = NOT_VALID; 128 rc = rpaphp_get_sensor_state(slot, &state); 129 if (rc) 130 goto exit; 131 132 if ((state == EMPTY) || (slot->type == PHB)) { 133 dbg("slot is empty\n"); 134 *value = EMPTY; 135 } 136 else if (state == PRESENT) { 137 if (!is_init) { 138 /* at run-time slot->state can be changed by */ 139 /* config/unconfig adapter */ 140 *value = slot->state; 141 } else { 142 bus = rpaphp_find_pci_bus(slot->dn); 143 if (bus && !list_empty(&bus->devices)) 144 *value = CONFIGURED; 145 else 146 *value = NOT_CONFIGURED; 147 } 148 } 149 exit: 150 return rc; 151 } 152 153 /* Must be called before pci_bus_add_devices */ 154 static void 155 rpaphp_fixup_new_pci_devices(struct pci_bus *bus, int fix_bus) 156 { 157 struct pci_dev *dev; 158 159 list_for_each_entry(dev, &bus->devices, bus_list) { 160 /* 161 * Skip already-present devices (which are on the 162 * global device list.) 163 */ 164 if (list_empty(&dev->global_list)) { 165 int i; 166 167 /* Need to setup IOMMU tables */ 168 ppc_md.iommu_dev_setup(dev); 169 170 if(fix_bus) 171 pcibios_fixup_device_resources(dev, bus); 172 pci_read_irq_line(dev); 173 for (i = 0; i < PCI_NUM_RESOURCES; i++) { 174 struct resource *r = &dev->resource[i]; 175 176 if (r->parent || !r->start || !r->flags) 177 continue; 178 rpaphp_claim_resource(dev, i); 179 } 180 } 181 } 182 } 183 184 static int rpaphp_pci_config_bridge(struct pci_dev *dev) 185 { 186 u8 sec_busno; 187 struct pci_bus *child_bus; 188 struct pci_dev *child_dev; 189 190 dbg("Enter %s: BRIDGE dev=%s\n", __FUNCTION__, pci_name(dev)); 191 192 /* get busno of downstream bus */ 193 pci_read_config_byte(dev, PCI_SECONDARY_BUS, &sec_busno); 194 195 /* add to children of PCI bridge dev->bus */ 196 child_bus = pci_add_new_bus(dev->bus, dev, sec_busno); 197 if (!child_bus) { 198 err("%s: could not add second bus\n", __FUNCTION__); 199 return -EIO; 200 } 201 sprintf(child_bus->name, "PCI Bus #%02x", child_bus->number); 202 /* do pci_scan_child_bus */ 203 pci_scan_child_bus(child_bus); 204 205 list_for_each_entry(child_dev, &child_bus->devices, bus_list) { 206 eeh_add_device_late(child_dev); 207 } 208 209 /* fixup new pci devices without touching bus struct */ 210 rpaphp_fixup_new_pci_devices(child_bus, 0); 211 212 /* Make the discovered devices available */ 213 pci_bus_add_devices(child_bus); 214 return 0; 215 } 216 217 /***************************************************************************** 218 rpaphp_pci_config_slot() will configure all devices under the 219 given slot->dn and return the the first pci_dev. 220 *****************************************************************************/ 221 static struct pci_dev * 222 rpaphp_pci_config_slot(struct pci_bus *bus) 223 { 224 struct device_node *dn = pci_bus_to_OF_node(bus); 225 struct pci_dev *dev = NULL; 226 int slotno; 227 int num; 228 229 dbg("Enter %s: dn=%s bus=%s\n", __FUNCTION__, dn->full_name, bus->name); 230 if (!dn || !dn->child) 231 return NULL; 232 233 slotno = PCI_SLOT(dn->child->devfn); 234 235 /* pci_scan_slot should find all children */ 236 num = pci_scan_slot(bus, PCI_DEVFN(slotno, 0)); 237 if (num) { 238 rpaphp_fixup_new_pci_devices(bus, 1); 239 pci_bus_add_devices(bus); 240 } 241 if (list_empty(&bus->devices)) { 242 err("%s: No new device found\n", __FUNCTION__); 243 return NULL; 244 } 245 list_for_each_entry(dev, &bus->devices, bus_list) { 246 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) 247 rpaphp_pci_config_bridge(dev); 248 } 249 250 return dev; 251 } 252 253 static void enable_eeh(struct device_node *dn) 254 { 255 struct device_node *sib; 256 257 for (sib = dn->child; sib; sib = sib->sibling) 258 enable_eeh(sib); 259 eeh_add_device_early(dn); 260 return; 261 262 } 263 264 static void print_slot_pci_funcs(struct pci_bus *bus) 265 { 266 struct device_node *dn; 267 struct pci_dev *dev; 268 269 dn = pci_bus_to_OF_node(bus); 270 if (!dn) 271 return; 272 273 dbg("%s: pci_devs of slot[%s]\n", __FUNCTION__, dn->full_name); 274 list_for_each_entry (dev, &bus->devices, bus_list) 275 dbg("\t%s\n", pci_name(dev)); 276 return; 277 } 278 279 int rpaphp_config_pci_adapter(struct pci_bus *bus) 280 { 281 struct device_node *dn = pci_bus_to_OF_node(bus); 282 struct pci_dev *dev; 283 int rc = -ENODEV; 284 285 dbg("Entry %s: slot[%s]\n", __FUNCTION__, dn->full_name); 286 if (!dn) 287 goto exit; 288 289 enable_eeh(dn); 290 dev = rpaphp_pci_config_slot(bus); 291 if (!dev) { 292 err("%s: can't find any devices.\n", __FUNCTION__); 293 goto exit; 294 } 295 print_slot_pci_funcs(bus); 296 rc = 0; 297 exit: 298 dbg("Exit %s: rc=%d\n", __FUNCTION__, rc); 299 return rc; 300 } 301 EXPORT_SYMBOL_GPL(rpaphp_config_pci_adapter); 302 303 static void rpaphp_eeh_remove_bus_device(struct pci_dev *dev) 304 { 305 eeh_remove_device(dev); 306 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) { 307 struct pci_bus *bus = dev->subordinate; 308 struct list_head *ln; 309 if (!bus) 310 return; 311 for (ln = bus->devices.next; ln != &bus->devices; ln = ln->next) { 312 struct pci_dev *pdev = pci_dev_b(ln); 313 if (pdev) 314 rpaphp_eeh_remove_bus_device(pdev); 315 } 316 317 } 318 return; 319 } 320 321 int rpaphp_unconfig_pci_adapter(struct slot *slot) 322 { 323 struct pci_dev *dev, *tmp; 324 int retval = 0; 325 326 list_for_each_entry_safe(dev, tmp, slot->pci_devs, bus_list) { 327 rpaphp_eeh_remove_bus_device(dev); 328 pci_remove_bus_device(dev); 329 } 330 331 slot->state = NOT_CONFIGURED; 332 info("%s: devices in slot[%s] unconfigured.\n", __FUNCTION__, 333 slot->name); 334 return retval; 335 } 336 337 static int setup_pci_hotplug_slot_info(struct slot *slot) 338 { 339 dbg("%s Initilize the PCI slot's hotplug->info structure ...\n", 340 __FUNCTION__); 341 rpaphp_get_power_status(slot, &slot->hotplug_slot->info->power_status); 342 rpaphp_get_pci_adapter_status(slot, 1, 343 &slot->hotplug_slot->info-> 344 adapter_status); 345 if (slot->hotplug_slot->info->adapter_status == NOT_VALID) { 346 err("%s: NOT_VALID: skip dn->full_name=%s\n", 347 __FUNCTION__, slot->dn->full_name); 348 return -EINVAL; 349 } 350 return 0; 351 } 352 353 static void set_slot_name(struct slot *slot) 354 { 355 struct pci_bus *bus = slot->bus; 356 struct pci_dev *bridge; 357 358 bridge = bus->self; 359 if (bridge) 360 strcpy(slot->name, pci_name(bridge)); 361 else 362 sprintf(slot->name, "%04x:%02x:00.0", pci_domain_nr(bus), 363 bus->number); 364 } 365 366 static int setup_pci_slot(struct slot *slot) 367 { 368 struct device_node *dn = slot->dn; 369 struct pci_bus *bus; 370 371 BUG_ON(!dn); 372 bus = rpaphp_find_pci_bus(dn); 373 if (!bus) { 374 err("%s: no pci_bus for dn %s\n", __FUNCTION__, dn->full_name); 375 goto exit_rc; 376 } 377 378 slot->bus = bus; 379 slot->pci_devs = &bus->devices; 380 set_slot_name(slot); 381 382 /* find slot's pci_dev if it's not empty */ 383 if (slot->hotplug_slot->info->adapter_status == EMPTY) { 384 slot->state = EMPTY; /* slot is empty */ 385 } else { 386 /* slot is occupied */ 387 if (!dn->child) { 388 /* non-empty slot has to have child */ 389 err("%s: slot[%s]'s device_node doesn't have child for adapter\n", 390 __FUNCTION__, slot->name); 391 goto exit_rc; 392 } 393 394 if (slot->hotplug_slot->info->adapter_status == NOT_CONFIGURED) { 395 dbg("%s CONFIGURING pci adapter in slot[%s]\n", 396 __FUNCTION__, slot->name); 397 if (rpaphp_config_pci_adapter(slot->bus)) { 398 err("%s: CONFIG pci adapter failed\n", __FUNCTION__); 399 goto exit_rc; 400 } 401 402 } else if (slot->hotplug_slot->info->adapter_status != CONFIGURED) { 403 err("%s: slot[%s]'s adapter_status is NOT_VALID.\n", 404 __FUNCTION__, slot->name); 405 goto exit_rc; 406 } 407 print_slot_pci_funcs(slot->bus); 408 if (!list_empty(slot->pci_devs)) { 409 slot->state = CONFIGURED; 410 } else { 411 /* DLPAR add as opposed to 412 * boot time */ 413 slot->state = NOT_CONFIGURED; 414 } 415 } 416 return 0; 417 exit_rc: 418 dealloc_slot_struct(slot); 419 return -EINVAL; 420 } 421 422 int register_pci_slot(struct slot *slot) 423 { 424 int rc = -EINVAL; 425 426 if ((slot->type == EMBEDDED) || (slot->type == PHB)) 427 slot->removable = 0; 428 else 429 slot->removable = 1; 430 if (setup_pci_hotplug_slot_info(slot)) 431 goto exit_rc; 432 if (setup_pci_slot(slot)) 433 goto exit_rc; 434 rc = register_slot(slot); 435 exit_rc: 436 return rc; 437 } 438 439 int rpaphp_enable_pci_slot(struct slot *slot) 440 { 441 int retval = 0, state; 442 443 retval = rpaphp_get_sensor_state(slot, &state); 444 if (retval) 445 goto exit; 446 dbg("%s: sensor state[%d]\n", __FUNCTION__, state); 447 /* if slot is not empty, enable the adapter */ 448 if (state == PRESENT) { 449 dbg("%s : slot[%s] is occupied.\n", __FUNCTION__, slot->name); 450 retval = rpaphp_config_pci_adapter(slot->bus); 451 if (!retval) { 452 slot->state = CONFIGURED; 453 dbg("%s: PCI devices in slot[%s] has been configured\n", 454 __FUNCTION__, slot->name); 455 } else { 456 slot->state = NOT_CONFIGURED; 457 dbg("%s: no pci_dev struct for adapter in slot[%s]\n", 458 __FUNCTION__, slot->name); 459 } 460 } else if (state == EMPTY) { 461 dbg("%s : slot[%s] is empty\n", __FUNCTION__, slot->name); 462 slot->state = EMPTY; 463 } else { 464 err("%s: slot[%s] is in invalid state\n", __FUNCTION__, 465 slot->name); 466 slot->state = NOT_VALID; 467 retval = -EINVAL; 468 } 469 exit: 470 dbg("%s - Exit: rc[%d]\n", __FUNCTION__, retval); 471 return retval; 472 } 473