blob: 6271be8b155e31834696c4d668dde53a52dd8daf [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",
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 Rose9c209c92005-07-25 11:13:38 -050067static void set_slot_name(struct slot *slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068{
John Rose9c209c92005-07-25 11:13:38 -050069 struct pci_bus *bus = slot->bus;
70 struct pci_dev *bridge;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
John Rose9c209c92005-07-25 11:13:38 -050072 bridge = bus->self;
73 if (bridge)
74 strcpy(slot->name, pci_name(bridge));
75 else
76 sprintf(slot->name, "%04x:%02x:00.0", pci_domain_nr(bus),
77 bus->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078}
79
linas@austin.ibm.comf6afbad2006-01-12 18:31:01 -060080int rpaphp_register_pci_slot(struct slot *slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081{
Linas Vepstasbf0af512007-04-13 15:34:14 -070082 int rc, level, state;
83 struct pci_bus *bus;
Linas Vepstas517d5a02007-04-13 15:34:12 -070084 struct hotplug_slot_info *info = slot->hotplug_slot->info;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Linas Vepstas03a66752007-04-13 15:34:16 -070086 info->adapter_status = NOT_VALID;
87 slot->state = EMPTY;
88
Linas Vepstasbf0af512007-04-13 15:34:14 -070089 /* Find out if the power is turned on for the slot */
Linas Vepstas427310f2007-04-13 15:34:13 -070090 rc = rtas_get_power_level(slot->power_domain, &level);
91 if (rc)
92 return rc;
93 info->power_status = level;
94
Linas Vepstasbf0af512007-04-13 15:34:14 -070095 /* Figure out if there is an adapter in the slot */
Linas Vepstasbf0af512007-04-13 15:34:14 -070096 rc = rpaphp_get_sensor_state(slot, &state);
97 if (rc)
98 return rc;
Linas Vepstas517d5a02007-04-13 15:34:12 -070099
Linas Vepstas03a66752007-04-13 15:34:16 -0700100 bus = pcibios_find_pci_bus(slot->dn);
101 if (!bus) {
102 err("%s: no pci_bus for dn %s\n", __FUNCTION__, slot->dn->full_name);
103 return -EINVAL;
Linas Vepstas517d5a02007-04-13 15:34:12 -0700104 }
105
Linas Vepstas03a66752007-04-13 15:34:16 -0700106 info->adapter_status = EMPTY;
107 slot->bus = bus;
108 slot->pci_devs = &bus->devices;
109 set_slot_name(slot);
110
111 /* if there's an adapter in the slot, go add the pci devices */
112 if (state == PRESENT) {
113 info->adapter_status = NOT_CONFIGURED;
114 slot->state = NOT_CONFIGURED;
115
116 /* non-empty slot has to have child */
117 if (!slot->dn->child) {
118 err("%s: slot[%s]'s device_node doesn't have child for adapter\n",
119 __FUNCTION__, slot->name);
120 return -EINVAL;
121 }
122
123 if (list_empty(&bus->devices))
124 pcibios_add_pci_devices(bus);
125
Linas Vepstas03a66752007-04-13 15:34:16 -0700126 if (!list_empty(&bus->devices)) {
127 info->adapter_status = CONFIGURED;
128 slot->state = CONFIGURED;
129 }
Linas Vepstas307ff122007-04-13 15:34:17 -0700130
131 if (debug) {
132 struct pci_dev *dev;
133 dbg("%s: pci_devs of slot[%s]\n", __FUNCTION__, slot->dn->full_name);
134 list_for_each_entry (dev, &bus->devices, bus_list)
135 dbg("\t%s\n", pci_name(dev));
136 }
Linas Vepstas03a66752007-04-13 15:34:16 -0700137 }
Linas Vepstas517d5a02007-04-13 15:34:12 -0700138
139 return rpaphp_register_slot(slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140}
141