Bjorn Helgaas | 736759e | 2018-01-26 14:22:04 -0600 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * PCI Hot Plug Controller Driver for RPA-compliant PPC64 platform. |
| 4 | * Copyright (C) 2003 Linda Xie <lxie@us.ibm.com> |
| 5 | * |
| 6 | * All rights reserved. |
| 7 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | * Send feedback to <lxie@us.ibm.com> |
| 9 | * |
| 10 | */ |
| 11 | #include <linux/pci.h> |
Tim Schmielau | 4e57b68 | 2005-10-30 15:03:48 -0800 | [diff] [blame] | 12 | #include <linux/string.h> |
| 13 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <asm/pci-bridge.h> |
| 15 | #include <asm/rtas.h> |
| 16 | #include <asm/machdep.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | |
Tim Schmielau | 4e57b68 | 2005-10-30 15:03:48 -0800 | [diff] [blame] | 18 | #include "../pci.h" /* for pci_add_new_bus */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include "rpaphp.h" |
| 20 | |
linas@austin.ibm.com | e06b80b | 2006-01-12 18:28:22 -0600 | [diff] [blame] | 21 | int rpaphp_get_sensor_state(struct slot *slot, int *state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | { |
| 23 | int rc; |
| 24 | int setlevel; |
| 25 | |
| 26 | rc = rtas_get_sensor(DR_ENTITY_SENSE, slot->index, state); |
| 27 | |
| 28 | if (rc < 0) { |
| 29 | if (rc == -EFAULT || rc == -EEXIST) { |
| 30 | dbg("%s: slot must be power up to get sensor-state\n", |
Harvey Harrison | 66bef8c | 2008-03-03 19:09:46 -0800 | [diff] [blame] | 31 | __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | |
Bjorn Helgaas | f762598 | 2013-11-14 11:28:18 -0700 | [diff] [blame] | 33 | /* some slots have to be powered up |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | * before get-sensor will succeed. |
| 35 | */ |
| 36 | rc = rtas_set_power_level(slot->power_domain, POWER_ON, |
| 37 | &setlevel); |
| 38 | if (rc < 0) { |
| 39 | dbg("%s: power on slot[%s] failed rc=%d.\n", |
Harvey Harrison | 66bef8c | 2008-03-03 19:09:46 -0800 | [diff] [blame] | 40 | __func__, slot->name, rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | } else { |
| 42 | rc = rtas_get_sensor(DR_ENTITY_SENSE, |
| 43 | slot->index, state); |
| 44 | } |
| 45 | } else if (rc == -ENODEV) |
Harvey Harrison | 66bef8c | 2008-03-03 19:09:46 -0800 | [diff] [blame] | 46 | info("%s: slot is unusable\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | else |
Harvey Harrison | 66bef8c | 2008-03-03 19:09:46 -0800 | [diff] [blame] | 48 | err("%s failed to get sensor state\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | } |
| 50 | return rc; |
| 51 | } |
| 52 | |
Linas Vepstas | fea54b8 | 2007-04-13 15:34:20 -0700 | [diff] [blame] | 53 | /** |
| 54 | * rpaphp_enable_slot - record slot state, config pci device |
Randy Dunlap | 26e6c66 | 2007-11-28 09:04:30 -0800 | [diff] [blame] | 55 | * @slot: target &slot |
Linas Vepstas | fea54b8 | 2007-04-13 15:34:20 -0700 | [diff] [blame] | 56 | * |
| 57 | * Initialize values in the slot, and the hotplug_slot info |
| 58 | * structures to indicate if there is a pci card plugged into |
| 59 | * the slot. If the slot is not empty, run the pcibios routine |
| 60 | * to get pcibios stuff correctly set up. |
| 61 | */ |
| 62 | int rpaphp_enable_slot(struct slot *slot) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | { |
Linas Vepstas | bf0af51 | 2007-04-13 15:34:14 -0700 | [diff] [blame] | 64 | int rc, level, state; |
| 65 | struct pci_bus *bus; |
Linas Vepstas | 517d5a0 | 2007-04-13 15:34:12 -0700 | [diff] [blame] | 66 | struct hotplug_slot_info *info = slot->hotplug_slot->info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | |
Linas Vepstas | 03a6675 | 2007-04-13 15:34:16 -0700 | [diff] [blame] | 68 | info->adapter_status = NOT_VALID; |
| 69 | slot->state = EMPTY; |
| 70 | |
Linas Vepstas | bf0af51 | 2007-04-13 15:34:14 -0700 | [diff] [blame] | 71 | /* Find out if the power is turned on for the slot */ |
Linas Vepstas | 427310f | 2007-04-13 15:34:13 -0700 | [diff] [blame] | 72 | rc = rtas_get_power_level(slot->power_domain, &level); |
| 73 | if (rc) |
| 74 | return rc; |
| 75 | info->power_status = level; |
| 76 | |
Linas Vepstas | bf0af51 | 2007-04-13 15:34:14 -0700 | [diff] [blame] | 77 | /* Figure out if there is an adapter in the slot */ |
Linas Vepstas | bf0af51 | 2007-04-13 15:34:14 -0700 | [diff] [blame] | 78 | rc = rpaphp_get_sensor_state(slot, &state); |
| 79 | if (rc) |
| 80 | return rc; |
Linas Vepstas | 517d5a0 | 2007-04-13 15:34:12 -0700 | [diff] [blame] | 81 | |
Gavin Shan | 3773dd2 | 2016-05-03 15:41:38 +1000 | [diff] [blame] | 82 | bus = pci_find_bus_by_node(slot->dn); |
Linas Vepstas | 03a6675 | 2007-04-13 15:34:16 -0700 | [diff] [blame] | 83 | if (!bus) { |
Rob Herring | b63773a | 2017-07-18 16:43:21 -0500 | [diff] [blame] | 84 | err("%s: no pci_bus for dn %pOF\n", __func__, slot->dn); |
Linas Vepstas | 03a6675 | 2007-04-13 15:34:16 -0700 | [diff] [blame] | 85 | return -EINVAL; |
Linas Vepstas | 517d5a0 | 2007-04-13 15:34:12 -0700 | [diff] [blame] | 86 | } |
| 87 | |
Linas Vepstas | 03a6675 | 2007-04-13 15:34:16 -0700 | [diff] [blame] | 88 | info->adapter_status = EMPTY; |
| 89 | slot->bus = bus; |
| 90 | slot->pci_devs = &bus->devices; |
Linas Vepstas | 03a6675 | 2007-04-13 15:34:16 -0700 | [diff] [blame] | 91 | |
| 92 | /* if there's an adapter in the slot, go add the pci devices */ |
| 93 | if (state == PRESENT) { |
| 94 | info->adapter_status = NOT_CONFIGURED; |
| 95 | slot->state = NOT_CONFIGURED; |
| 96 | |
| 97 | /* non-empty slot has to have child */ |
| 98 | if (!slot->dn->child) { |
| 99 | err("%s: slot[%s]'s device_node doesn't have child for adapter\n", |
Harvey Harrison | 66bef8c | 2008-03-03 19:09:46 -0800 | [diff] [blame] | 100 | __func__, slot->name); |
Linas Vepstas | 03a6675 | 2007-04-13 15:34:16 -0700 | [diff] [blame] | 101 | return -EINVAL; |
| 102 | } |
| 103 | |
| 104 | if (list_empty(&bus->devices)) |
Gavin Shan | bd251b8 | 2016-05-03 15:41:37 +1000 | [diff] [blame] | 105 | pci_hp_add_devices(bus); |
Linas Vepstas | 03a6675 | 2007-04-13 15:34:16 -0700 | [diff] [blame] | 106 | |
Linas Vepstas | 03a6675 | 2007-04-13 15:34:16 -0700 | [diff] [blame] | 107 | if (!list_empty(&bus->devices)) { |
| 108 | info->adapter_status = CONFIGURED; |
| 109 | slot->state = CONFIGURED; |
| 110 | } |
Linas Vepstas | 307ff12 | 2007-04-13 15:34:17 -0700 | [diff] [blame] | 111 | |
Kristen Carlson Accardi | 5d9bc1f | 2008-10-13 09:59:12 -0700 | [diff] [blame] | 112 | if (rpaphp_debug) { |
Linas Vepstas | 307ff12 | 2007-04-13 15:34:17 -0700 | [diff] [blame] | 113 | struct pci_dev *dev; |
Rob Herring | b63773a | 2017-07-18 16:43:21 -0500 | [diff] [blame] | 114 | dbg("%s: pci_devs of slot[%pOF]\n", __func__, slot->dn); |
Bogicevic Sasa | ff3ce48 | 2015-12-27 13:21:11 -0800 | [diff] [blame] | 115 | list_for_each_entry(dev, &bus->devices, bus_list) |
Linas Vepstas | 307ff12 | 2007-04-13 15:34:17 -0700 | [diff] [blame] | 116 | dbg("\t%s\n", pci_name(dev)); |
| 117 | } |
Linas Vepstas | 03a6675 | 2007-04-13 15:34:16 -0700 | [diff] [blame] | 118 | } |
Linas Vepstas | 517d5a0 | 2007-04-13 15:34:12 -0700 | [diff] [blame] | 119 | |
Linas Vepstas | 6f79eb7 | 2007-04-13 15:34:19 -0700 | [diff] [blame] | 120 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | } |