blob: 7836d6913e67df7053e4b31a1c1067b2e2f1c17a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Schmielau4e57b682005-10-30 15:03:48 -080026#include <linux/string.h>
27
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <asm/pci-bridge.h>
29#include <asm/rtas.h>
30#include <asm/machdep.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Tim Schmielau4e57b682005-10-30 15:03:48 -080032#include "../pci.h" /* for pci_add_new_bus */
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "rpaphp.h"
34
linas@austin.ibm.come06b80b2006-01-12 18:28:22 -060035int rpaphp_get_sensor_state(struct slot *slot, int *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -070036{
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 Harrison66bef8c2008-03-03 19:09:46 -080045 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Bjorn Helgaasf7625982013-11-14 11:28:18 -070047 /* some slots have to be powered up
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 * 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 Harrison66bef8c2008-03-03 19:09:46 -080054 __func__, slot->name, rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 } else {
56 rc = rtas_get_sensor(DR_ENTITY_SENSE,
57 slot->index, state);
58 }
59 } else if (rc == -ENODEV)
Harvey Harrison66bef8c2008-03-03 19:09:46 -080060 info("%s: slot is unusable\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 else
Harvey Harrison66bef8c2008-03-03 19:09:46 -080062 err("%s failed to get sensor state\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 }
64 return rc;
65}
66
Linas Vepstasfea54b82007-04-13 15:34:20 -070067/**
68 * rpaphp_enable_slot - record slot state, config pci device
Randy Dunlap26e6c662007-11-28 09:04:30 -080069 * @slot: target &slot
Linas Vepstasfea54b82007-04-13 15:34:20 -070070 *
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 */
76int rpaphp_enable_slot(struct slot *slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077{
Linas Vepstasbf0af512007-04-13 15:34:14 -070078 int rc, level, state;
79 struct pci_bus *bus;
Linas Vepstas517d5a02007-04-13 15:34:12 -070080 struct hotplug_slot_info *info = slot->hotplug_slot->info;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Linas Vepstas03a66752007-04-13 15:34:16 -070082 info->adapter_status = NOT_VALID;
83 slot->state = EMPTY;
84
Linas Vepstasbf0af512007-04-13 15:34:14 -070085 /* Find out if the power is turned on for the slot */
Linas Vepstas427310f2007-04-13 15:34:13 -070086 rc = rtas_get_power_level(slot->power_domain, &level);
87 if (rc)
88 return rc;
89 info->power_status = level;
90
Linas Vepstasbf0af512007-04-13 15:34:14 -070091 /* Figure out if there is an adapter in the slot */
Linas Vepstasbf0af512007-04-13 15:34:14 -070092 rc = rpaphp_get_sensor_state(slot, &state);
93 if (rc)
94 return rc;
Linas Vepstas517d5a02007-04-13 15:34:12 -070095
Linas Vepstas03a66752007-04-13 15:34:16 -070096 bus = pcibios_find_pci_bus(slot->dn);
97 if (!bus) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -080098 err("%s: no pci_bus for dn %s\n", __func__, slot->dn->full_name);
Linas Vepstas03a66752007-04-13 15:34:16 -070099 return -EINVAL;
Linas Vepstas517d5a02007-04-13 15:34:12 -0700100 }
101
Linas Vepstas03a66752007-04-13 15:34:16 -0700102 info->adapter_status = EMPTY;
103 slot->bus = bus;
104 slot->pci_devs = &bus->devices;
Linas Vepstas03a66752007-04-13 15:34:16 -0700105
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 Harrison66bef8c2008-03-03 19:09:46 -0800114 __func__, slot->name);
Linas Vepstas03a66752007-04-13 15:34:16 -0700115 return -EINVAL;
116 }
117
118 if (list_empty(&bus->devices))
119 pcibios_add_pci_devices(bus);
120
Linas Vepstas03a66752007-04-13 15:34:16 -0700121 if (!list_empty(&bus->devices)) {
122 info->adapter_status = CONFIGURED;
123 slot->state = CONFIGURED;
124 }
Linas Vepstas307ff122007-04-13 15:34:17 -0700125
Kristen Carlson Accardi5d9bc1f2008-10-13 09:59:12 -0700126 if (rpaphp_debug) {
Linas Vepstas307ff122007-04-13 15:34:17 -0700127 struct pci_dev *dev;
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800128 dbg("%s: pci_devs of slot[%s]\n", __func__, slot->dn->full_name);
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800129 list_for_each_entry(dev, &bus->devices, bus_list)
Linas Vepstas307ff122007-04-13 15:34:17 -0700130 dbg("\t%s\n", pci_name(dev));
131 }
Linas Vepstas03a66752007-04-13 15:34:16 -0700132 }
Linas Vepstas517d5a02007-04-13 15:34:12 -0700133
Linas Vepstas6f79eb72007-04-13 15:34:19 -0700134 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135}