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", |
Harvey Harrison | 66bef8c | 2008-03-03 19:09:46 -0800 | [diff] [blame] | 45 | __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | |
Bjorn Helgaas | f762598 | 2013-11-14 11:28:18 -0700 | [diff] [blame] | 47 | /* some slots have to be powered up |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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", |
Harvey Harrison | 66bef8c | 2008-03-03 19:09:46 -0800 | [diff] [blame] | 54 | __func__, slot->name, rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | } else { |
| 56 | rc = rtas_get_sensor(DR_ENTITY_SENSE, |
| 57 | slot->index, state); |
| 58 | } |
| 59 | } else if (rc == -ENODEV) |
Harvey Harrison | 66bef8c | 2008-03-03 19:09:46 -0800 | [diff] [blame] | 60 | info("%s: slot is unusable\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | else |
Harvey Harrison | 66bef8c | 2008-03-03 19:09:46 -0800 | [diff] [blame] | 62 | err("%s failed to get sensor state\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | } |
| 64 | return rc; |
| 65 | } |
| 66 | |
Linas Vepstas | fea54b8 | 2007-04-13 15:34:20 -0700 | [diff] [blame] | 67 | /** |
| 68 | * rpaphp_enable_slot - record slot state, config pci device |
Randy Dunlap | 26e6c66 | 2007-11-28 09:04:30 -0800 | [diff] [blame] | 69 | * @slot: target &slot |
Linas Vepstas | fea54b8 | 2007-04-13 15:34:20 -0700 | [diff] [blame] | 70 | * |
| 71 | * Initialize values in the slot, and the hotplug_slot info |
| 72 | * structures to indicate if there is a pci card plugged into |
| 73 | * the slot. If the slot is not empty, run the pcibios routine |
| 74 | * to get pcibios stuff correctly set up. |
| 75 | */ |
| 76 | int rpaphp_enable_slot(struct slot *slot) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | { |
Linas Vepstas | bf0af51 | 2007-04-13 15:34:14 -0700 | [diff] [blame] | 78 | int rc, level, state; |
| 79 | struct pci_bus *bus; |
Linas Vepstas | 517d5a0 | 2007-04-13 15:34:12 -0700 | [diff] [blame] | 80 | struct hotplug_slot_info *info = slot->hotplug_slot->info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | |
Linas Vepstas | 03a6675 | 2007-04-13 15:34:16 -0700 | [diff] [blame] | 82 | info->adapter_status = NOT_VALID; |
| 83 | slot->state = EMPTY; |
| 84 | |
Linas Vepstas | bf0af51 | 2007-04-13 15:34:14 -0700 | [diff] [blame] | 85 | /* Find out if the power is turned on for the slot */ |
Linas Vepstas | 427310f | 2007-04-13 15:34:13 -0700 | [diff] [blame] | 86 | rc = rtas_get_power_level(slot->power_domain, &level); |
| 87 | if (rc) |
| 88 | return rc; |
| 89 | info->power_status = level; |
| 90 | |
Linas Vepstas | bf0af51 | 2007-04-13 15:34:14 -0700 | [diff] [blame] | 91 | /* Figure out if there is an adapter in the slot */ |
Linas Vepstas | bf0af51 | 2007-04-13 15:34:14 -0700 | [diff] [blame] | 92 | rc = rpaphp_get_sensor_state(slot, &state); |
| 93 | if (rc) |
| 94 | return rc; |
Linas Vepstas | 517d5a0 | 2007-04-13 15:34:12 -0700 | [diff] [blame] | 95 | |
Linas Vepstas | 03a6675 | 2007-04-13 15:34:16 -0700 | [diff] [blame] | 96 | bus = pcibios_find_pci_bus(slot->dn); |
| 97 | if (!bus) { |
Harvey Harrison | 66bef8c | 2008-03-03 19:09:46 -0800 | [diff] [blame] | 98 | err("%s: no pci_bus for dn %s\n", __func__, slot->dn->full_name); |
Linas Vepstas | 03a6675 | 2007-04-13 15:34:16 -0700 | [diff] [blame] | 99 | return -EINVAL; |
Linas Vepstas | 517d5a0 | 2007-04-13 15:34:12 -0700 | [diff] [blame] | 100 | } |
| 101 | |
Linas Vepstas | 03a6675 | 2007-04-13 15:34:16 -0700 | [diff] [blame] | 102 | info->adapter_status = EMPTY; |
| 103 | slot->bus = bus; |
| 104 | slot->pci_devs = &bus->devices; |
Linas Vepstas | 03a6675 | 2007-04-13 15:34:16 -0700 | [diff] [blame] | 105 | |
| 106 | /* if there's an adapter in the slot, go add the pci devices */ |
| 107 | if (state == PRESENT) { |
| 108 | info->adapter_status = NOT_CONFIGURED; |
| 109 | slot->state = NOT_CONFIGURED; |
| 110 | |
| 111 | /* non-empty slot has to have child */ |
| 112 | if (!slot->dn->child) { |
| 113 | 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] | 114 | __func__, slot->name); |
Linas Vepstas | 03a6675 | 2007-04-13 15:34:16 -0700 | [diff] [blame] | 115 | return -EINVAL; |
| 116 | } |
| 117 | |
| 118 | if (list_empty(&bus->devices)) |
| 119 | pcibios_add_pci_devices(bus); |
| 120 | |
Linas Vepstas | 03a6675 | 2007-04-13 15:34:16 -0700 | [diff] [blame] | 121 | if (!list_empty(&bus->devices)) { |
| 122 | info->adapter_status = CONFIGURED; |
| 123 | slot->state = CONFIGURED; |
| 124 | } |
Linas Vepstas | 307ff12 | 2007-04-13 15:34:17 -0700 | [diff] [blame] | 125 | |
Kristen Carlson Accardi | 5d9bc1f | 2008-10-13 09:59:12 -0700 | [diff] [blame] | 126 | if (rpaphp_debug) { |
Linas Vepstas | 307ff12 | 2007-04-13 15:34:17 -0700 | [diff] [blame] | 127 | struct pci_dev *dev; |
Harvey Harrison | 66bef8c | 2008-03-03 19:09:46 -0800 | [diff] [blame] | 128 | dbg("%s: pci_devs of slot[%s]\n", __func__, slot->dn->full_name); |
Bogicevic Sasa | ff3ce48 | 2015-12-27 13:21:11 -0800 | [diff] [blame] | 129 | list_for_each_entry(dev, &bus->devices, bus_list) |
Linas Vepstas | 307ff12 | 2007-04-13 15:34:17 -0700 | [diff] [blame] | 130 | dbg("\t%s\n", pci_name(dev)); |
| 131 | } |
Linas Vepstas | 03a6675 | 2007-04-13 15:34:16 -0700 | [diff] [blame] | 132 | } |
Linas Vepstas | 517d5a0 | 2007-04-13 15:34:12 -0700 | [diff] [blame] | 133 | |
Linas Vepstas | 6f79eb7 | 2007-04-13 15:34:19 -0700 | [diff] [blame] | 134 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | } |