blob: 0aac33e15dab2de21e68b487bc907a3d0251d241 [file] [log] [blame]
Bjorn Helgaas736759e2018-01-26 14:22:04 -06001// SPDX-License-Identifier: GPL-2.0+
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
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 Torvalds1da177e2005-04-16 15:20:36 -07008 * Send feedback to <lxie@us.ibm.com>
9 *
10 */
11#include <linux/pci.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080012#include <linux/string.h>
13
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <asm/pci-bridge.h>
15#include <asm/rtas.h>
16#include <asm/machdep.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
Tim Schmielau4e57b682005-10-30 15:03:48 -080018#include "../pci.h" /* for pci_add_new_bus */
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include "rpaphp.h"
20
linas@austin.ibm.come06b80b2006-01-12 18:28:22 -060021int rpaphp_get_sensor_state(struct slot *slot, int *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -070022{
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 Harrison66bef8c2008-03-03 19:09:46 -080031 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Bjorn Helgaasf7625982013-11-14 11:28:18 -070033 /* some slots have to be powered up
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 * 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 Harrison66bef8c2008-03-03 19:09:46 -080040 __func__, slot->name, rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 } else {
42 rc = rtas_get_sensor(DR_ENTITY_SENSE,
43 slot->index, state);
44 }
45 } else if (rc == -ENODEV)
Harvey Harrison66bef8c2008-03-03 19:09:46 -080046 info("%s: slot is unusable\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 else
Harvey Harrison66bef8c2008-03-03 19:09:46 -080048 err("%s failed to get sensor state\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 }
50 return rc;
51}
52
Linas Vepstasfea54b82007-04-13 15:34:20 -070053/**
54 * rpaphp_enable_slot - record slot state, config pci device
Randy Dunlap26e6c662007-11-28 09:04:30 -080055 * @slot: target &slot
Linas Vepstasfea54b82007-04-13 15:34:20 -070056 *
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 */
62int rpaphp_enable_slot(struct slot *slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063{
Linas Vepstasbf0af512007-04-13 15:34:14 -070064 int rc, level, state;
65 struct pci_bus *bus;
Linas Vepstas517d5a02007-04-13 15:34:12 -070066 struct hotplug_slot_info *info = slot->hotplug_slot->info;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Linas Vepstas03a66752007-04-13 15:34:16 -070068 info->adapter_status = NOT_VALID;
69 slot->state = EMPTY;
70
Linas Vepstasbf0af512007-04-13 15:34:14 -070071 /* Find out if the power is turned on for the slot */
Linas Vepstas427310f2007-04-13 15:34:13 -070072 rc = rtas_get_power_level(slot->power_domain, &level);
73 if (rc)
74 return rc;
75 info->power_status = level;
76
Linas Vepstasbf0af512007-04-13 15:34:14 -070077 /* Figure out if there is an adapter in the slot */
Linas Vepstasbf0af512007-04-13 15:34:14 -070078 rc = rpaphp_get_sensor_state(slot, &state);
79 if (rc)
80 return rc;
Linas Vepstas517d5a02007-04-13 15:34:12 -070081
Gavin Shan3773dd22016-05-03 15:41:38 +100082 bus = pci_find_bus_by_node(slot->dn);
Linas Vepstas03a66752007-04-13 15:34:16 -070083 if (!bus) {
Rob Herringb63773a2017-07-18 16:43:21 -050084 err("%s: no pci_bus for dn %pOF\n", __func__, slot->dn);
Linas Vepstas03a66752007-04-13 15:34:16 -070085 return -EINVAL;
Linas Vepstas517d5a02007-04-13 15:34:12 -070086 }
87
Linas Vepstas03a66752007-04-13 15:34:16 -070088 info->adapter_status = EMPTY;
89 slot->bus = bus;
90 slot->pci_devs = &bus->devices;
Linas Vepstas03a66752007-04-13 15:34:16 -070091
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 Harrison66bef8c2008-03-03 19:09:46 -0800100 __func__, slot->name);
Linas Vepstas03a66752007-04-13 15:34:16 -0700101 return -EINVAL;
102 }
103
104 if (list_empty(&bus->devices))
Gavin Shanbd251b82016-05-03 15:41:37 +1000105 pci_hp_add_devices(bus);
Linas Vepstas03a66752007-04-13 15:34:16 -0700106
Linas Vepstas03a66752007-04-13 15:34:16 -0700107 if (!list_empty(&bus->devices)) {
108 info->adapter_status = CONFIGURED;
109 slot->state = CONFIGURED;
110 }
Linas Vepstas307ff122007-04-13 15:34:17 -0700111
Kristen Carlson Accardi5d9bc1f2008-10-13 09:59:12 -0700112 if (rpaphp_debug) {
Linas Vepstas307ff122007-04-13 15:34:17 -0700113 struct pci_dev *dev;
Rob Herringb63773a2017-07-18 16:43:21 -0500114 dbg("%s: pci_devs of slot[%pOF]\n", __func__, slot->dn);
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800115 list_for_each_entry(dev, &bus->devices, bus_list)
Linas Vepstas307ff122007-04-13 15:34:17 -0700116 dbg("\t%s\n", pci_name(dev));
117 }
Linas Vepstas03a66752007-04-13 15:34:16 -0700118 }
Linas Vepstas517d5a02007-04-13 15:34:12 -0700119
Linas Vepstas6f79eb72007-04-13 15:34:19 -0700120 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121}