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/pci.h> |
Tim Schmielau | 4e57b68 | 2005-10-30 15:03:48 -0800 | [diff] [blame] | 26 | #include <linux/string.h> |
| 27 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include <asm/pci-bridge.h> |
| 29 | #include <asm/rtas.h> |
| 30 | #include <asm/machdep.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | |
Tim Schmielau | 4e57b68 | 2005-10-30 15:03:48 -0800 | [diff] [blame] | 32 | #include "../pci.h" /* for pci_add_new_bus */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #include "rpaphp.h" |
| 34 | |
linas@austin.ibm.com | e06b80b | 2006-01-12 18:28:22 -0600 | [diff] [blame] | 35 | int rpaphp_get_sensor_state(struct slot *slot, int *state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | { |
| 37 | int rc; |
| 38 | int setlevel; |
| 39 | |
| 40 | rc = rtas_get_sensor(DR_ENTITY_SENSE, slot->index, state); |
| 41 | |
| 42 | if (rc < 0) { |
| 43 | if (rc == -EFAULT || rc == -EEXIST) { |
| 44 | dbg("%s: slot must be power up to get sensor-state\n", |
| 45 | __FUNCTION__); |
| 46 | |
| 47 | /* some slots have to be powered up |
| 48 | * before get-sensor will succeed. |
| 49 | */ |
| 50 | rc = rtas_set_power_level(slot->power_domain, POWER_ON, |
| 51 | &setlevel); |
| 52 | if (rc < 0) { |
| 53 | dbg("%s: power on slot[%s] failed rc=%d.\n", |
| 54 | __FUNCTION__, slot->name, rc); |
| 55 | } else { |
| 56 | rc = rtas_get_sensor(DR_ENTITY_SENSE, |
| 57 | slot->index, state); |
| 58 | } |
| 59 | } else if (rc == -ENODEV) |
| 60 | info("%s: slot is unusable\n", __FUNCTION__); |
| 61 | else |
| 62 | err("%s failed to get sensor state\n", __FUNCTION__); |
| 63 | } |
| 64 | return rc; |
| 65 | } |
| 66 | |
John Rose | 940903c | 2005-07-25 10:16:58 -0500 | [diff] [blame] | 67 | static void print_slot_pci_funcs(struct pci_bus *bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | { |
John Rose | 940903c | 2005-07-25 10:16:58 -0500 | [diff] [blame] | 69 | struct device_node *dn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | struct pci_dev *dev; |
| 71 | |
John Rose | 940903c | 2005-07-25 10:16:58 -0500 | [diff] [blame] | 72 | dn = pci_bus_to_OF_node(bus); |
| 73 | if (!dn) |
| 74 | return; |
| 75 | |
| 76 | dbg("%s: pci_devs of slot[%s]\n", __FUNCTION__, dn->full_name); |
| 77 | list_for_each_entry (dev, &bus->devices, bus_list) |
John Rose | 5eeb8c6 | 2005-07-25 10:16:42 -0500 | [diff] [blame] | 78 | dbg("\t%s\n", pci_name(dev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | return; |
| 80 | } |
| 81 | |
John Rose | 9c209c9 | 2005-07-25 11:13:38 -0500 | [diff] [blame] | 82 | static void set_slot_name(struct slot *slot) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | { |
John Rose | 9c209c9 | 2005-07-25 11:13:38 -0500 | [diff] [blame] | 84 | struct pci_bus *bus = slot->bus; |
| 85 | struct pci_dev *bridge; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | |
John Rose | 9c209c9 | 2005-07-25 11:13:38 -0500 | [diff] [blame] | 87 | bridge = bus->self; |
| 88 | if (bridge) |
| 89 | strcpy(slot->name, pci_name(bridge)); |
| 90 | else |
| 91 | sprintf(slot->name, "%04x:%02x:00.0", pci_domain_nr(bus), |
| 92 | bus->number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | static int setup_pci_slot(struct slot *slot) |
| 96 | { |
John Rose | 9c209c9 | 2005-07-25 11:13:38 -0500 | [diff] [blame] | 97 | struct device_node *dn = slot->dn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | struct pci_bus *bus; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | |
John Rose | 9c209c9 | 2005-07-25 11:13:38 -0500 | [diff] [blame] | 100 | BUG_ON(!dn); |
linas@austin.ibm.com | 0165786 | 2006-01-12 18:18:26 -0600 | [diff] [blame] | 101 | bus = pcibios_find_pci_bus(dn); |
John Rose | 9c209c9 | 2005-07-25 11:13:38 -0500 | [diff] [blame] | 102 | if (!bus) { |
| 103 | err("%s: no pci_bus for dn %s\n", __FUNCTION__, dn->full_name); |
| 104 | goto exit_rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | } |
| 106 | |
John Rose | 9c209c9 | 2005-07-25 11:13:38 -0500 | [diff] [blame] | 107 | slot->bus = bus; |
| 108 | slot->pci_devs = &bus->devices; |
| 109 | set_slot_name(slot); |
| 110 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | /* find slot's pci_dev if it's not empty */ |
| 112 | if (slot->hotplug_slot->info->adapter_status == EMPTY) { |
| 113 | slot->state = EMPTY; /* slot is empty */ |
| 114 | } else { |
| 115 | /* slot is occupied */ |
John Rose | 9c209c9 | 2005-07-25 11:13:38 -0500 | [diff] [blame] | 116 | if (!dn->child) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | /* non-empty slot has to have child */ |
| 118 | err("%s: slot[%s]'s device_node doesn't have child for adapter\n", |
| 119 | __FUNCTION__, slot->name); |
| 120 | goto exit_rc; |
| 121 | } |
| 122 | |
| 123 | if (slot->hotplug_slot->info->adapter_status == NOT_CONFIGURED) { |
| 124 | dbg("%s CONFIGURING pci adapter in slot[%s]\n", |
| 125 | __FUNCTION__, slot->name); |
linas@austin.ibm.com | 7fec77e | 2006-01-12 18:22:07 -0600 | [diff] [blame] | 126 | pcibios_add_pci_devices(slot->bus); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | |
| 128 | } else if (slot->hotplug_slot->info->adapter_status != CONFIGURED) { |
| 129 | err("%s: slot[%s]'s adapter_status is NOT_VALID.\n", |
| 130 | __FUNCTION__, slot->name); |
| 131 | goto exit_rc; |
| 132 | } |
John Rose | 940903c | 2005-07-25 10:16:58 -0500 | [diff] [blame] | 133 | print_slot_pci_funcs(slot->bus); |
John Rose | 5eeb8c6 | 2005-07-25 10:16:42 -0500 | [diff] [blame] | 134 | if (!list_empty(slot->pci_devs)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | slot->state = CONFIGURED; |
| 136 | } else { |
| 137 | /* DLPAR add as opposed to |
| 138 | * boot time */ |
| 139 | slot->state = NOT_CONFIGURED; |
| 140 | } |
| 141 | } |
| 142 | return 0; |
| 143 | exit_rc: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | return -EINVAL; |
| 145 | } |
| 146 | |
linas@austin.ibm.com | f6afbad | 2006-01-12 18:31:01 -0600 | [diff] [blame] | 147 | int rpaphp_register_pci_slot(struct slot *slot) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | { |
Linas Vepstas | bf0af51 | 2007-04-13 15:34:14 -0700 | [diff] [blame^] | 149 | int rc, level, state; |
| 150 | struct pci_bus *bus; |
Linas Vepstas | 517d5a0 | 2007-04-13 15:34:12 -0700 | [diff] [blame] | 151 | struct hotplug_slot_info *info = slot->hotplug_slot->info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | |
Linas Vepstas | bf0af51 | 2007-04-13 15:34:14 -0700 | [diff] [blame^] | 153 | /* Find out if the power is turned on for the slot */ |
Linas Vepstas | 427310f | 2007-04-13 15:34:13 -0700 | [diff] [blame] | 154 | rc = rtas_get_power_level(slot->power_domain, &level); |
| 155 | if (rc) |
| 156 | return rc; |
| 157 | info->power_status = level; |
| 158 | |
Linas Vepstas | bf0af51 | 2007-04-13 15:34:14 -0700 | [diff] [blame^] | 159 | /* Figure out if there is an adapter in the slot */ |
| 160 | info->adapter_status = NOT_VALID; |
| 161 | rc = rpaphp_get_sensor_state(slot, &state); |
| 162 | if (rc) |
| 163 | return rc; |
Linas Vepstas | 517d5a0 | 2007-04-13 15:34:12 -0700 | [diff] [blame] | 164 | |
Linas Vepstas | bf0af51 | 2007-04-13 15:34:14 -0700 | [diff] [blame^] | 165 | if (state == EMPTY) |
| 166 | info->adapter_status = EMPTY; |
| 167 | else if (state == PRESENT) { |
| 168 | bus = pcibios_find_pci_bus(slot->dn); |
| 169 | if (bus && !list_empty(&bus->devices)) |
| 170 | info->adapter_status = CONFIGURED; |
| 171 | else |
| 172 | info->adapter_status = NOT_CONFIGURED; |
Linas Vepstas | 517d5a0 | 2007-04-13 15:34:12 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | if (setup_pci_slot(slot)) |
Linas Vepstas | 517d5a0 | 2007-04-13 15:34:12 -0700 | [diff] [blame] | 176 | return -EINVAL; |
| 177 | |
| 178 | return rpaphp_register_slot(slot); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | } |
| 180 | |