rpaphp_pci.c revision bf0af511fcc856649a2a39c627828695b580d124
11da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
21da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * PCI Hot Plug Controller Driver for RPA-compliant PPC64 platform.
31da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Copyright (C) 2003 Linda Xie <lxie@us.ibm.com>
41da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
51da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * All rights reserved.
61da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
71da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * This program is free software; you can redistribute it and/or modify
81da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * it under the terms of the GNU General Public License as published by
91da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * the Free Software Foundation; either version 2 of the License, or (at
101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * your option) any later version.
111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * This program is distributed in the hope that it will be useful, but
131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * WITHOUT ANY WARRANTY; without even the implied warranty of
141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * NON INFRINGEMENT.  See the GNU General Public License for more
161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * details.
171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * You should have received a copy of the GNU General Public License
191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * along with this program; if not, write to the Free Software
201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Send feedback to <lxie@us.ibm.com>
231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/pci.h>
264e57b6817880946a3a78d5d8cad1ace363f7e449Tim Schmielau#include <linux/string.h>
274e57b6817880946a3a78d5d8cad1ace363f7e449Tim Schmielau
281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <asm/pci-bridge.h>
291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <asm/rtas.h>
301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <asm/machdep.h>
311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
324e57b6817880946a3a78d5d8cad1ace363f7e449Tim Schmielau#include "../pci.h"		/* for pci_add_new_bus */
331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include "rpaphp.h"
341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
35e06b80b78db96ca272db4ec0b26ce1092a1a9704Linas Vepstasint rpaphp_get_sensor_state(struct slot *slot, int *state)
361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int rc;
381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int setlevel;
391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	rc = rtas_get_sensor(DR_ENTITY_SENSE, slot->index, state);
411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (rc < 0) {
431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (rc == -EFAULT || rc == -EEXIST) {
441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			dbg("%s: slot must be power up to get sensor-state\n",
451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			    __FUNCTION__);
461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			/* some slots have to be powered up
481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			 * before get-sensor will succeed.
491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			 */
501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			rc = rtas_set_power_level(slot->power_domain, POWER_ON,
511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds						  &setlevel);
521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			if (rc < 0) {
531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				dbg("%s: power on slot[%s] failed rc=%d.\n",
541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				    __FUNCTION__, slot->name, rc);
551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			} else {
561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				rc = rtas_get_sensor(DR_ENTITY_SENSE,
571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds						     slot->index, state);
581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			}
591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		} else if (rc == -ENODEV)
601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			info("%s: slot is unusable\n", __FUNCTION__);
611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		else
621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			err("%s failed to get sensor state\n", __FUNCTION__);
631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return rc;
651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
67940903c5a5a906c622a79b3101586deb1a1b3480John Rosestatic void print_slot_pci_funcs(struct pci_bus *bus)
681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
69940903c5a5a906c622a79b3101586deb1a1b3480John Rose	struct device_node *dn;
701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct pci_dev *dev;
711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
72940903c5a5a906c622a79b3101586deb1a1b3480John Rose	dn = pci_bus_to_OF_node(bus);
73940903c5a5a906c622a79b3101586deb1a1b3480John Rose	if (!dn)
74940903c5a5a906c622a79b3101586deb1a1b3480John Rose		return;
75940903c5a5a906c622a79b3101586deb1a1b3480John Rose
76940903c5a5a906c622a79b3101586deb1a1b3480John Rose	dbg("%s: pci_devs of slot[%s]\n", __FUNCTION__, dn->full_name);
77940903c5a5a906c622a79b3101586deb1a1b3480John Rose	list_for_each_entry (dev, &bus->devices, bus_list)
785eeb8c63a38ff20285f3bbe7bcfe5e7c33c8ba14John Rose		dbg("\t%s\n", pci_name(dev));
791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return;
801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
829c209c919df95f83aa042b3352c43841ad15a02bJohn Rosestatic void set_slot_name(struct slot *slot)
831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
849c209c919df95f83aa042b3352c43841ad15a02bJohn Rose	struct pci_bus *bus = slot->bus;
859c209c919df95f83aa042b3352c43841ad15a02bJohn Rose	struct pci_dev *bridge;
869c209c919df95f83aa042b3352c43841ad15a02bJohn Rose
879c209c919df95f83aa042b3352c43841ad15a02bJohn Rose	bridge = bus->self;
889c209c919df95f83aa042b3352c43841ad15a02bJohn Rose	if (bridge)
899c209c919df95f83aa042b3352c43841ad15a02bJohn Rose		strcpy(slot->name, pci_name(bridge));
909c209c919df95f83aa042b3352c43841ad15a02bJohn Rose	else
919c209c919df95f83aa042b3352c43841ad15a02bJohn Rose		sprintf(slot->name, "%04x:%02x:00.0", pci_domain_nr(bus),
929c209c919df95f83aa042b3352c43841ad15a02bJohn Rose			bus->number);
931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int setup_pci_slot(struct slot *slot)
961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
979c209c919df95f83aa042b3352c43841ad15a02bJohn Rose	struct device_node *dn = slot->dn;
981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct pci_bus *bus;
991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1009c209c919df95f83aa042b3352c43841ad15a02bJohn Rose	BUG_ON(!dn);
10101657868be1c21b2b8b0e683ea24bdcc2331d522Linas Vepstas	bus = pcibios_find_pci_bus(dn);
1029c209c919df95f83aa042b3352c43841ad15a02bJohn Rose	if (!bus) {
1039c209c919df95f83aa042b3352c43841ad15a02bJohn Rose		err("%s: no pci_bus for dn %s\n", __FUNCTION__, dn->full_name);
1049c209c919df95f83aa042b3352c43841ad15a02bJohn Rose		goto exit_rc;
1051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
1061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1079c209c919df95f83aa042b3352c43841ad15a02bJohn Rose	slot->bus = bus;
1089c209c919df95f83aa042b3352c43841ad15a02bJohn Rose	slot->pci_devs = &bus->devices;
1099c209c919df95f83aa042b3352c43841ad15a02bJohn Rose	set_slot_name(slot);
1109c209c919df95f83aa042b3352c43841ad15a02bJohn Rose
1111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* find slot's pci_dev if it's not empty */
1121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (slot->hotplug_slot->info->adapter_status == EMPTY) {
1131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		slot->state = EMPTY;	/* slot is empty */
1141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	} else {
1151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* slot is occupied */
1169c209c919df95f83aa042b3352c43841ad15a02bJohn Rose		if (!dn->child) {
1171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			/* non-empty slot has to have child */
1181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			err("%s: slot[%s]'s device_node doesn't have child for adapter\n",
1191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				__FUNCTION__, slot->name);
1201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			goto exit_rc;
1211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
1221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (slot->hotplug_slot->info->adapter_status == NOT_CONFIGURED) {
1241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			dbg("%s CONFIGURING pci adapter in slot[%s]\n",
1251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				__FUNCTION__, slot->name);
1267fec77e4793f307b30846a3d4015d329ffc0b685Linas Vepstas			pcibios_add_pci_devices(slot->bus);
1271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		} else if (slot->hotplug_slot->info->adapter_status != CONFIGURED) {
1291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			err("%s: slot[%s]'s adapter_status is NOT_VALID.\n",
1301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				__FUNCTION__, slot->name);
1311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			goto exit_rc;
1321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
133940903c5a5a906c622a79b3101586deb1a1b3480John Rose		print_slot_pci_funcs(slot->bus);
1345eeb8c63a38ff20285f3bbe7bcfe5e7c33c8ba14John Rose		if (!list_empty(slot->pci_devs)) {
1351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			slot->state = CONFIGURED;
1361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		} else {
1371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			/* DLPAR add as opposed to
1381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 	 * boot time */
1391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			slot->state = NOT_CONFIGURED;
1401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
1411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
1421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return 0;
1431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsexit_rc:
1441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return -EINVAL;
1451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
1461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
147f6afbad82c6b7bab0198442592b110341fb419baLinas Vepstasint rpaphp_register_pci_slot(struct slot *slot)
1481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
149bf0af511fcc856649a2a39c627828695b580d124Linas Vepstas	int rc, level, state;
150bf0af511fcc856649a2a39c627828695b580d124Linas Vepstas	struct pci_bus *bus;
151517d5a0417e19101eaa769039d1921d626ee546cLinas Vepstas	struct hotplug_slot_info *info = slot->hotplug_slot->info;
152517d5a0417e19101eaa769039d1921d626ee546cLinas Vepstas
153bf0af511fcc856649a2a39c627828695b580d124Linas Vepstas	/* Find out if the power is turned on for the slot */
154427310ff02e80cc80826407c0121cec3694c9e7dLinas Vepstas	rc = rtas_get_power_level(slot->power_domain, &level);
155427310ff02e80cc80826407c0121cec3694c9e7dLinas Vepstas	if (rc)
156427310ff02e80cc80826407c0121cec3694c9e7dLinas Vepstas		return rc;
157427310ff02e80cc80826407c0121cec3694c9e7dLinas Vepstas	info->power_status = level;
158427310ff02e80cc80826407c0121cec3694c9e7dLinas Vepstas
159bf0af511fcc856649a2a39c627828695b580d124Linas Vepstas	/* Figure out if there is an adapter in the slot */
160bf0af511fcc856649a2a39c627828695b580d124Linas Vepstas	info->adapter_status = NOT_VALID;
161bf0af511fcc856649a2a39c627828695b580d124Linas Vepstas	rc = rpaphp_get_sensor_state(slot, &state);
162bf0af511fcc856649a2a39c627828695b580d124Linas Vepstas	if (rc)
163bf0af511fcc856649a2a39c627828695b580d124Linas Vepstas		return rc;
164517d5a0417e19101eaa769039d1921d626ee546cLinas Vepstas
165bf0af511fcc856649a2a39c627828695b580d124Linas Vepstas	if (state == EMPTY)
166bf0af511fcc856649a2a39c627828695b580d124Linas Vepstas		info->adapter_status = EMPTY;
167bf0af511fcc856649a2a39c627828695b580d124Linas Vepstas	else if (state == PRESENT) {
168bf0af511fcc856649a2a39c627828695b580d124Linas Vepstas		bus = pcibios_find_pci_bus(slot->dn);
169bf0af511fcc856649a2a39c627828695b580d124Linas Vepstas		if (bus && !list_empty(&bus->devices))
170bf0af511fcc856649a2a39c627828695b580d124Linas Vepstas			info->adapter_status = CONFIGURED;
171bf0af511fcc856649a2a39c627828695b580d124Linas Vepstas		else
172bf0af511fcc856649a2a39c627828695b580d124Linas Vepstas			info->adapter_status = NOT_CONFIGURED;
173517d5a0417e19101eaa769039d1921d626ee546cLinas Vepstas	}
1741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (setup_pci_slot(slot))
176517d5a0417e19101eaa769039d1921d626ee546cLinas Vepstas		return -EINVAL;
177517d5a0417e19101eaa769039d1921d626ee546cLinas Vepstas
178517d5a0417e19101eaa769039d1921d626ee546cLinas Vepstas	return rpaphp_register_slot(slot);
1791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
1801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
181