rpaphp_pci.c 11.5 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
/*
 * PCI Hot Plug Controller Driver for RPA-compliant PPC64 platform.
 * Copyright (C) 2003 Linda Xie <lxie@us.ibm.com>
 *
 * All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
 * NON INFRINGEMENT.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * Send feedback to <lxie@us.ibm.com>
 *
 */
#include <linux/pci.h>
#include <asm/pci-bridge.h>
#include <asm/rtas.h>
#include <asm/machdep.h>
#include "../pci.h"		/* for pci_add_new_bus */

#include "rpaphp.h"

33
static struct pci_bus *find_bus_among_children(struct pci_bus *bus,
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
					struct device_node *dn)
{
	struct pci_bus *child = NULL;
	struct list_head *tmp;
	struct device_node *busdn;

	busdn = pci_bus_to_OF_node(bus);
	if (busdn == dn)
		return bus;

	list_for_each(tmp, &bus->children) {
		child = find_bus_among_children(pci_bus_b(tmp), dn);
		if (child)
			break;
	}
	return child;
}

52
static struct pci_bus *rpaphp_find_pci_bus(struct device_node *dn)
53
{
54 55
	if (!dn->phb || !dn->phb->bus)
		return NULL;
56 57 58 59

	return find_bus_among_children(dn->phb->bus, dn);
}

Linus Torvalds's avatar
Linus Torvalds committed
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
int rpaphp_claim_resource(struct pci_dev *dev, int resource)
{
	struct resource *res = &dev->resource[resource];
	struct resource *root = pci_find_parent_resource(dev, res);
	char *dtype = resource < PCI_BRIDGE_RESOURCES ? "device" : "bridge";
	int err = -EINVAL;

	if (root != NULL) {
		err = request_resource(root, res);
	}

	if (err) {
		err("PCI: %s region %d of %s %s [%lx:%lx]\n",
		    root ? "Address space collision on" :
		    "No parent found for",
		    resource, dtype, pci_name(dev), res->start, res->end);
	}
	return err;
}

EXPORT_SYMBOL_GPL(rpaphp_claim_resource);

static int rpaphp_get_sensor_state(struct slot *slot, int *state)
{
	int rc;
	int setlevel;

	rc = rtas_get_sensor(DR_ENTITY_SENSE, slot->index, state);

	if (rc < 0) {
		if (rc == -EFAULT || rc == -EEXIST) {
			dbg("%s: slot must be power up to get sensor-state\n",
			    __FUNCTION__);

			/* some slots have to be powered up 
			 * before get-sensor will succeed.
			 */
			rc = rtas_set_power_level(slot->power_domain, POWER_ON,
						  &setlevel);
			if (rc < 0) {
				dbg("%s: power on slot[%s] failed rc=%d.\n",
				    __FUNCTION__, slot->name, rc);
			} else {
				rc = rtas_get_sensor(DR_ENTITY_SENSE,
						     slot->index, state);
			}
		} else if (rc == -ENODEV)
			info("%s: slot is unusable\n", __FUNCTION__);
		else
			err("%s failed to get sensor state\n", __FUNCTION__);
	}
	return rc;
}

/**
 * get_pci_adapter_status - get the status of a slot
 * 
 * 0-- slot is empty
 * 1-- adapter is configured
 * 2-- adapter is not configured
 * 3-- not valid
 */
int rpaphp_get_pci_adapter_status(struct slot *slot, int is_init, u8 * value)
{
124
	struct pci_bus *bus;
Linus Torvalds's avatar
Linus Torvalds committed
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
	int state, rc;

	*value = NOT_VALID;
	rc = rpaphp_get_sensor_state(slot, &state);
	if (rc)
		goto exit;

 	if ((state == EMPTY) || (slot->type == PHB)) {
 		dbg("slot is empty\n");
 		*value = EMPTY;
 	}
 	else if (state == PRESENT) {
		if (!is_init) {
			/* at run-time slot->state can be changed by */
			/* config/unconfig adapter */
			*value = slot->state;
		} else {
142 143 144 145
			bus = rpaphp_find_pci_bus(slot->dn);
			if (bus && !list_empty(&bus->devices))
				*value = CONFIGURED;
			else
Linus Torvalds's avatar
Linus Torvalds committed
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
				*value = NOT_CONFIGURED;
		}
	}
exit:
	return rc;
}

/* Must be called before pci_bus_add_devices */
static void 
rpaphp_fixup_new_pci_devices(struct pci_bus *bus, int fix_bus)
{
	struct pci_dev *dev;

	list_for_each_entry(dev, &bus->devices, bus_list) {
		/*
		 * Skip already-present devices (which are on the
		 * global device list.)
		 */
		if (list_empty(&dev->global_list)) {
			int i;
			
			/* Need to setup IOMMU tables */
			ppc_md.iommu_dev_setup(dev);

			if(fix_bus)
				pcibios_fixup_device_resources(dev, bus);
			pci_read_irq_line(dev);
			for (i = 0; i < PCI_NUM_RESOURCES; i++) {
				struct resource *r = &dev->resource[i];

				if (r->parent || !r->start || !r->flags)
					continue;
				rpaphp_claim_resource(dev, i);
			}
		}
	}
}

static int rpaphp_pci_config_bridge(struct pci_dev *dev)
{
	u8 sec_busno;
	struct pci_bus *child_bus;
	struct pci_dev *child_dev;

	dbg("Enter %s:  BRIDGE dev=%s\n", __FUNCTION__, pci_name(dev));

	/* get busno of downstream bus */
	pci_read_config_byte(dev, PCI_SECONDARY_BUS, &sec_busno);
		
	/* add to children of PCI bridge dev->bus */
	child_bus = pci_add_new_bus(dev->bus, dev, sec_busno);
	if (!child_bus) {
		err("%s: could not add second bus\n", __FUNCTION__);
		return -EIO;
	}
	sprintf(child_bus->name, "PCI Bus #%02x", child_bus->number);
	/* do pci_scan_child_bus */
	pci_scan_child_bus(child_bus);

	list_for_each_entry(child_dev, &child_bus->devices, bus_list) {
		eeh_add_device_late(child_dev);
	}

	 /* fixup new pci devices without touching bus struct */
	rpaphp_fixup_new_pci_devices(child_bus, 0);

	/* Make the discovered devices available */
	pci_bus_add_devices(child_bus);
	return 0;
}

217 218 219 220 221
/*****************************************************************************
 rpaphp_pci_config_slot() will  configure all devices under the
 given slot->dn and return the the first pci_dev.
 *****************************************************************************/
static struct pci_dev *
222
rpaphp_pci_config_slot(struct pci_bus *bus)
223
{
224
	struct device_node *dn = pci_bus_to_OF_node(bus);
225
	struct pci_dev *dev = NULL;
226
	int slotno;
227 228 229
	int num;

	dbg("Enter %s: dn=%s bus=%s\n", __FUNCTION__, dn->full_name, bus->name);
230
	if (!dn || !dn->child)
231
		return NULL;
232

233
	slotno = PCI_SLOT(dn->child->devfn);
234

235 236 237 238 239 240 241 242 243 244 245
	/* pci_scan_slot should find all children */
	num = pci_scan_slot(bus, PCI_DEVFN(slotno, 0));
	if (num) {
		rpaphp_fixup_new_pci_devices(bus, 1);
		pci_bus_add_devices(bus);
	}
	if (list_empty(&bus->devices)) {
		err("%s: No new device found\n", __FUNCTION__);
		return NULL;
	}
	list_for_each_entry(dev, &bus->devices, bus_list) {
246 247 248
		if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE)
			rpaphp_pci_config_bridge(dev);
	}
249

250 251 252
	return dev;
}

Linus Torvalds's avatar
Linus Torvalds committed
253 254 255 256 257 258 259 260 261 262 263
static void enable_eeh(struct device_node *dn)
{
	struct device_node *sib;

	for (sib = dn->child; sib; sib = sib->sibling) 
		enable_eeh(sib);
	eeh_add_device_early(dn);
	return;
	
}

264
static void print_slot_pci_funcs(struct pci_bus *bus)
Linus Torvalds's avatar
Linus Torvalds committed
265
{
266
	struct device_node *dn;
Linus Torvalds's avatar
Linus Torvalds committed
267 268
	struct pci_dev *dev;

269 270 271 272 273 274
	dn = pci_bus_to_OF_node(bus);
	if (!dn)
		return;

	dbg("%s: pci_devs of slot[%s]\n", __FUNCTION__, dn->full_name);
	list_for_each_entry (dev, &bus->devices, bus_list)
275
		dbg("\t%s\n", pci_name(dev));
Linus Torvalds's avatar
Linus Torvalds committed
276 277 278
	return;
}

279
int rpaphp_config_pci_adapter(struct pci_bus *bus)
Linus Torvalds's avatar
Linus Torvalds committed
280
{
281
	struct device_node *dn = pci_bus_to_OF_node(bus);
Linus Torvalds's avatar
Linus Torvalds committed
282 283 284
	struct pci_dev *dev;
	int rc = -ENODEV;

285 286 287
	dbg("Entry %s: slot[%s]\n", __FUNCTION__, dn->full_name);
	if (!dn)
		goto exit;
Linus Torvalds's avatar
Linus Torvalds committed
288

289 290
	enable_eeh(dn);
	dev = rpaphp_pci_config_slot(bus);
291 292 293
	if (!dev) {
		err("%s: can't find any devices.\n", __FUNCTION__);
		goto exit;
Linus Torvalds's avatar
Linus Torvalds committed
294
	}
295
	print_slot_pci_funcs(bus);
296
	rc = 0;
Linus Torvalds's avatar
Linus Torvalds committed
297 298 299 300
exit:
	dbg("Exit %s:  rc=%d\n", __FUNCTION__, rc);
	return rc;
}
301
EXPORT_SYMBOL_GPL(rpaphp_config_pci_adapter);
Linus Torvalds's avatar
Linus Torvalds committed
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322

static void rpaphp_eeh_remove_bus_device(struct pci_dev *dev)
{
	eeh_remove_device(dev);
	if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
		struct pci_bus *bus = dev->subordinate;
		struct list_head *ln;
		if (!bus)
			return; 
		for (ln = bus->devices.next; ln != &bus->devices; ln = ln->next) {
			struct pci_dev *pdev = pci_dev_b(ln);
			if (pdev)
				rpaphp_eeh_remove_bus_device(pdev);
		}

	}
	return;
}

int rpaphp_unconfig_pci_adapter(struct slot *slot)
{
323
	struct pci_dev *dev, *tmp;
Linus Torvalds's avatar
Linus Torvalds committed
324 325
	int retval = 0;

326
	list_for_each_entry_safe(dev, tmp, slot->pci_devs, bus_list) {
Linus Torvalds's avatar
Linus Torvalds committed
327
		rpaphp_eeh_remove_bus_device(dev);
328 329
		pci_remove_bus_device(dev);
	}
Linus Torvalds's avatar
Linus Torvalds committed
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352

	slot->state = NOT_CONFIGURED;
	info("%s: devices in slot[%s] unconfigured.\n", __FUNCTION__,
	     slot->name);
	return retval;
}

static int setup_pci_hotplug_slot_info(struct slot *slot)
{
	dbg("%s Initilize the PCI slot's hotplug->info structure ...\n",
	    __FUNCTION__);
	rpaphp_get_power_status(slot, &slot->hotplug_slot->info->power_status);
	rpaphp_get_pci_adapter_status(slot, 1,
				      &slot->hotplug_slot->info->
				      adapter_status);
	if (slot->hotplug_slot->info->adapter_status == NOT_VALID) {
		err("%s: NOT_VALID: skip dn->full_name=%s\n",
		    __FUNCTION__, slot->dn->full_name);
		return -EINVAL;
	}
	return 0;
}

353
static void set_slot_name(struct slot *slot)
Linus Torvalds's avatar
Linus Torvalds committed
354
{
355 356 357 358 359 360 361 362 363
	struct pci_bus *bus = slot->bus;
	struct pci_dev *bridge;

	bridge = bus->self;
	if (bridge)
		strcpy(slot->name, pci_name(bridge));
	else
		sprintf(slot->name, "%04x:%02x:00.0", pci_domain_nr(bus),
			bus->number);
Linus Torvalds's avatar
Linus Torvalds committed
364 365 366 367
}

static int setup_pci_slot(struct slot *slot)
{
368
	struct device_node *dn = slot->dn;
Linus Torvalds's avatar
Linus Torvalds committed
369 370
	struct pci_bus *bus;

371 372 373 374 375
	BUG_ON(!dn);
	bus = rpaphp_find_pci_bus(dn);
	if (!bus) {
		err("%s: no pci_bus for dn %s\n", __FUNCTION__, dn->full_name);
		goto exit_rc;
Linus Torvalds's avatar
Linus Torvalds committed
376 377
	}

378 379 380 381
	slot->bus = bus;
	slot->pci_devs = &bus->devices;
	set_slot_name(slot);

Linus Torvalds's avatar
Linus Torvalds committed
382 383 384 385 386
	/* find slot's pci_dev if it's not empty */
	if (slot->hotplug_slot->info->adapter_status == EMPTY) {
		slot->state = EMPTY;	/* slot is empty */
	} else {
		/* slot is occupied */
387
		if (!dn->child) {
Linus Torvalds's avatar
Linus Torvalds committed
388 389 390 391 392 393 394 395 396
			/* non-empty slot has to have child */
			err("%s: slot[%s]'s device_node doesn't have child for adapter\n", 
				__FUNCTION__, slot->name);
			goto exit_rc;
		}

		if (slot->hotplug_slot->info->adapter_status == NOT_CONFIGURED) {
			dbg("%s CONFIGURING pci adapter in slot[%s]\n",  
				__FUNCTION__, slot->name);
397
			if (rpaphp_config_pci_adapter(slot->bus)) {
Linus Torvalds's avatar
Linus Torvalds committed
398 399 400 401 402 403 404 405 406
				err("%s: CONFIG pci adapter failed\n", __FUNCTION__);
				goto exit_rc;		
			}

		} else if (slot->hotplug_slot->info->adapter_status != CONFIGURED) {
			err("%s: slot[%s]'s adapter_status is NOT_VALID.\n",
				__FUNCTION__, slot->name);
			goto exit_rc;
		}
407
		print_slot_pci_funcs(slot->bus);
408
		if (!list_empty(slot->pci_devs)) {
Linus Torvalds's avatar
Linus Torvalds committed
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
			slot->state = CONFIGURED;
		} else {
			/* DLPAR add as opposed to 
		 	 * boot time */
			slot->state = NOT_CONFIGURED;
		}
	}
	return 0;
exit_rc:
	dealloc_slot_struct(slot);
	return -EINVAL;
}

int register_pci_slot(struct slot *slot)
{
	int rc = -EINVAL;

	if ((slot->type == EMBEDDED) || (slot->type == PHB))
		slot->removable = 0;
	else
		slot->removable = 1;
	if (setup_pci_hotplug_slot_info(slot))
		goto exit_rc;
	if (setup_pci_slot(slot))
		goto exit_rc;
	rc = register_slot(slot);
exit_rc:
	return rc;
}

int rpaphp_enable_pci_slot(struct slot *slot)
{
	int retval = 0, state;

	retval = rpaphp_get_sensor_state(slot, &state);
	if (retval)
		goto exit;
	dbg("%s: sensor state[%d]\n", __FUNCTION__, state);
	/* if slot is not empty, enable the adapter */
	if (state == PRESENT) {
		dbg("%s : slot[%s] is occupied.\n", __FUNCTION__, slot->name);
450
		retval = rpaphp_config_pci_adapter(slot->bus);
Linus Torvalds's avatar
Linus Torvalds committed
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
		if (!retval) {
			slot->state = CONFIGURED;
			dbg("%s: PCI devices in slot[%s] has been configured\n", 
				__FUNCTION__, slot->name);
		} else {
			slot->state = NOT_CONFIGURED;
			dbg("%s: no pci_dev struct for adapter in slot[%s]\n",
			    __FUNCTION__, slot->name);
		}
	} else if (state == EMPTY) {
		dbg("%s : slot[%s] is empty\n", __FUNCTION__, slot->name);
		slot->state = EMPTY;
	} else {
		err("%s: slot[%s] is in invalid state\n", __FUNCTION__,
		    slot->name);
		slot->state = NOT_VALID;
		retval = -EINVAL;
	}
exit:
	dbg("%s - Exit: rc[%d]\n", __FUNCTION__, retval);
	return retval;
}