Linux Audio

Check our new training course

Loading...
v5.4
   1// SPDX-License-Identifier: GPL-2.0+
   2/*
   3 * IBM Hot Plug Controller Driver
   4 *
   5 * Written By: Irene Zubarev, IBM Corporation
   6 *
   7 * Copyright (C) 2001 Greg Kroah-Hartman (greg@kroah.com)
   8 * Copyright (C) 2001,2002 IBM Corp.
   9 *
  10 * All rights reserved.
  11 *
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  12 * Send feedback to <gregkh@us.ibm.com>
  13 *
  14 */
  15
  16#include <linux/module.h>
  17#include <linux/slab.h>
  18#include <linux/pci.h>
  19#include <linux/list.h>
  20#include "ibmphp.h"
  21
  22
  23static int configure_device(struct pci_func *);
  24static int configure_bridge(struct pci_func **, u8);
  25static struct res_needed *scan_behind_bridge(struct pci_func *, u8);
  26static int add_new_bus(struct bus_node *, struct resource_node *, struct resource_node *, struct resource_node *, u8);
  27static u8 find_sec_number(u8 primary_busno, u8 slotno);
  28
  29/*
  30 * NOTE..... If BIOS doesn't provide default routing, we assign:
  31 * 9 for SCSI, 10 for LAN adapters, and 11 for everything else.
  32 * If adapter is bridged, then we assign 11 to it and devices behind it.
  33 * We also assign the same irq numbers for multi function devices.
  34 * These are PIC mode, so shouldn't matter n.e.ways (hopefully)
  35 */
  36static void assign_alt_irq(struct pci_func *cur_func, u8 class_code)
  37{
  38	int j;
  39	for (j = 0; j < 4; j++) {
  40		if (cur_func->irq[j] == 0xff) {
  41			switch (class_code) {
  42				case PCI_BASE_CLASS_STORAGE:
  43					cur_func->irq[j] = SCSI_IRQ;
  44					break;
  45				case PCI_BASE_CLASS_NETWORK:
  46					cur_func->irq[j] = LAN_IRQ;
  47					break;
  48				default:
  49					cur_func->irq[j] = OTHER_IRQ;
  50					break;
  51			}
  52		}
  53	}
  54}
  55
  56/*
  57 * Configures the device to be added (will allocate needed resources if it
  58 * can), the device can be a bridge or a regular pci device, can also be
  59 * multi-functional
  60 *
  61 * Input: function to be added
  62 *
  63 * TO DO:  The error case with Multifunction device or multi function bridge,
  64 * if there is an error, will need to go through all previous functions and
  65 * unconfigure....or can add some code into unconfigure_card....
  66 */
  67int ibmphp_configure_card(struct pci_func *func, u8 slotno)
  68{
  69	u16 vendor_id;
  70	u32 class;
  71	u8 class_code;
  72	u8 hdr_type, device, sec_number;
  73	u8 function;
  74	struct pci_func *newfunc;	/* for multi devices */
  75	struct pci_func *cur_func, *prev_func;
  76	int rc, i, j;
  77	int cleanup_count;
  78	u8 flag;
  79	u8 valid_device = 0x00; /* to see if we are able to read from card any device info at all */
  80
  81	debug("inside configure_card, func->busno = %x\n", func->busno);
  82
  83	device = func->device;
  84	cur_func = func;
  85
  86	/* We only get bus and device from IRQ routing table.  So at this point,
  87	 * func->busno is correct, and func->device contains only device (at the 5
  88	 * highest bits)
  89	 */
  90
  91	/* For every function on the card */
  92	for (function = 0x00; function < 0x08; function++) {
  93		unsigned int devfn = PCI_DEVFN(device, function);
  94		ibmphp_pci_bus->number = cur_func->busno;
  95
  96		cur_func->function = function;
  97
  98		debug("inside the loop, cur_func->busno = %x, cur_func->device = %x, cur_func->function = %x\n",
  99			cur_func->busno, cur_func->device, cur_func->function);
 100
 101		pci_bus_read_config_word(ibmphp_pci_bus, devfn, PCI_VENDOR_ID, &vendor_id);
 102
 103		debug("vendor_id is %x\n", vendor_id);
 104		if (vendor_id != PCI_VENDOR_ID_NOTVALID) {
 105			/* found correct device!!! */
 106			debug("found valid device, vendor_id = %x\n", vendor_id);
 107
 108			++valid_device;
 109
 110			/* header: x x x x x x x x
 111			 *         | |___________|=> 1=PPB bridge, 0=normal device, 2=CardBus Bridge
 112			 *         |_=> 0 = single function device, 1 = multi-function device
 113			 */
 114
 115			pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_HEADER_TYPE, &hdr_type);
 116			pci_bus_read_config_dword(ibmphp_pci_bus, devfn, PCI_CLASS_REVISION, &class);
 117
 118			class_code = class >> 24;
 119			debug("hrd_type = %x, class = %x, class_code %x\n", hdr_type, class, class_code);
 120			class >>= 8;	/* to take revision out, class = class.subclass.prog i/f */
 121			if (class == PCI_CLASS_NOT_DEFINED_VGA) {
 122				err("The device %x is VGA compatible and as is not supported for hot plugging. "
 123				     "Please choose another device.\n", cur_func->device);
 124				return -ENODEV;
 125			} else if (class == PCI_CLASS_DISPLAY_VGA) {
 126				err("The device %x is not supported for hot plugging. Please choose another device.\n",
 127				     cur_func->device);
 128				return -ENODEV;
 129			}
 130			switch (hdr_type) {
 131				case PCI_HEADER_TYPE_NORMAL:
 132					debug("single device case.... vendor id = %x, hdr_type = %x, class = %x\n", vendor_id, hdr_type, class);
 133					assign_alt_irq(cur_func, class_code);
 134					rc = configure_device(cur_func);
 135					if (rc < 0) {
 136						/* We need to do this in case some other BARs were properly inserted */
 137						err("was not able to configure devfunc %x on bus %x.\n",
 138						     cur_func->device, cur_func->busno);
 139						cleanup_count = 6;
 140						goto error;
 141					}
 142					cur_func->next = NULL;
 143					function = 0x8;
 144					break;
 145				case PCI_HEADER_TYPE_MULTIDEVICE:
 146					assign_alt_irq(cur_func, class_code);
 147					rc = configure_device(cur_func);
 148					if (rc < 0) {
 149						/* We need to do this in case some other BARs were properly inserted */
 150						err("was not able to configure devfunc %x on bus %x...bailing out\n",
 151						     cur_func->device, cur_func->busno);
 152						cleanup_count = 6;
 153						goto error;
 154					}
 155					newfunc = kzalloc(sizeof(*newfunc), GFP_KERNEL);
 156					if (!newfunc)
 
 157						return -ENOMEM;
 158
 159					newfunc->busno = cur_func->busno;
 160					newfunc->device = device;
 161					cur_func->next = newfunc;
 162					cur_func = newfunc;
 163					for (j = 0; j < 4; j++)
 164						newfunc->irq[j] = cur_func->irq[j];
 165					break;
 166				case PCI_HEADER_TYPE_MULTIBRIDGE:
 167					class >>= 8;
 168					if (class != PCI_CLASS_BRIDGE_PCI) {
 169						err("This %x is not PCI-to-PCI bridge, and as is not supported for hot-plugging.  Please insert another card.\n",
 170						     cur_func->device);
 171						return -ENODEV;
 172					}
 173					assign_alt_irq(cur_func, class_code);
 174					rc = configure_bridge(&cur_func, slotno);
 175					if (rc == -ENODEV) {
 176						err("You chose to insert Single Bridge, or nested bridges, this is not supported...\n");
 177						err("Bus %x, devfunc %x\n", cur_func->busno, cur_func->device);
 178						return rc;
 179					}
 180					if (rc) {
 181						/* We need to do this in case some other BARs were properly inserted */
 182						err("was not able to hot-add PPB properly.\n");
 183						func->bus = 1; /* To indicate to the unconfigure function that this is a PPB */
 184						cleanup_count = 2;
 185						goto error;
 186					}
 187
 188					pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_SECONDARY_BUS, &sec_number);
 189					flag = 0;
 190					for (i = 0; i < 32; i++) {
 191						if (func->devices[i]) {
 192							newfunc = kzalloc(sizeof(*newfunc), GFP_KERNEL);
 193							if (!newfunc)
 
 194								return -ENOMEM;
 195
 196							newfunc->busno = sec_number;
 197							newfunc->device = (u8) i;
 198							for (j = 0; j < 4; j++)
 199								newfunc->irq[j] = cur_func->irq[j];
 200
 201							if (flag) {
 202								for (prev_func = cur_func; prev_func->next; prev_func = prev_func->next) ;
 203								prev_func->next = newfunc;
 204							} else
 205								cur_func->next = newfunc;
 206
 207							rc = ibmphp_configure_card(newfunc, slotno);
 208							/* This could only happen if kmalloc failed */
 209							if (rc) {
 210								/* We need to do this in case bridge itself got configured properly, but devices behind it failed */
 211								func->bus = 1; /* To indicate to the unconfigure function that this is a PPB */
 212								cleanup_count = 2;
 213								goto error;
 214							}
 215							flag = 1;
 216						}
 217					}
 218
 219					newfunc = kzalloc(sizeof(*newfunc), GFP_KERNEL);
 220					if (!newfunc)
 
 221						return -ENOMEM;
 222
 223					newfunc->busno = cur_func->busno;
 224					newfunc->device = device;
 225					for (j = 0; j < 4; j++)
 226						newfunc->irq[j] = cur_func->irq[j];
 227					for (prev_func = cur_func; prev_func->next; prev_func = prev_func->next);
 228					prev_func->next = newfunc;
 229					cur_func = newfunc;
 230					break;
 231				case PCI_HEADER_TYPE_BRIDGE:
 232					class >>= 8;
 233					debug("class now is %x\n", class);
 234					if (class != PCI_CLASS_BRIDGE_PCI) {
 235						err("This %x is not PCI-to-PCI bridge, and as is not supported for hot-plugging.  Please insert another card.\n",
 236						     cur_func->device);
 237						return -ENODEV;
 238					}
 239
 240					assign_alt_irq(cur_func, class_code);
 241
 242					debug("cur_func->busno b4 configure_bridge is %x\n", cur_func->busno);
 243					rc = configure_bridge(&cur_func, slotno);
 244					if (rc == -ENODEV) {
 245						err("You chose to insert Single Bridge, or nested bridges, this is not supported...\n");
 246						err("Bus %x, devfunc %x\n", cur_func->busno, cur_func->device);
 247						return rc;
 248					}
 249					if (rc) {
 250						/* We need to do this in case some other BARs were properly inserted */
 251						func->bus = 1; /* To indicate to the unconfigure function that this is a PPB */
 252						err("was not able to hot-add PPB properly.\n");
 253						cleanup_count = 2;
 254						goto error;
 255					}
 256					debug("cur_func->busno = %x, device = %x, function = %x\n",
 257						cur_func->busno, device, function);
 258					pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_SECONDARY_BUS, &sec_number);
 259					debug("after configuring bridge..., sec_number = %x\n", sec_number);
 260					flag = 0;
 261					for (i = 0; i < 32; i++) {
 262						if (func->devices[i]) {
 263							debug("inside for loop, device is %x\n", i);
 264							newfunc = kzalloc(sizeof(*newfunc), GFP_KERNEL);
 265							if (!newfunc)
 
 266								return -ENOMEM;
 267
 268							newfunc->busno = sec_number;
 269							newfunc->device = (u8) i;
 270							for (j = 0; j < 4; j++)
 271								newfunc->irq[j] = cur_func->irq[j];
 272
 273							if (flag) {
 274								for (prev_func = cur_func; prev_func->next; prev_func = prev_func->next);
 275								prev_func->next = newfunc;
 276							} else
 277								cur_func->next = newfunc;
 278
 279							rc = ibmphp_configure_card(newfunc, slotno);
 280
 281							/* Again, this case should not happen... For complete paranoia, will need to call remove_bus */
 282							if (rc) {
 283								/* We need to do this in case some other BARs were properly inserted */
 284								func->bus = 1; /* To indicate to the unconfigure function that this is a PPB */
 285								cleanup_count = 2;
 286								goto error;
 287							}
 288							flag = 1;
 289						}
 290					}
 291
 292					function = 0x8;
 293					break;
 294				default:
 295					err("MAJOR PROBLEM!!!!, header type not supported? %x\n", hdr_type);
 296					return -ENXIO;
 297					break;
 298			}	/* end of switch */
 299		}	/* end of valid device */
 300	}	/* end of for */
 301
 302	if (!valid_device) {
 303		err("Cannot find any valid devices on the card.  Or unable to read from card.\n");
 304		return -ENODEV;
 305	}
 306
 307	return 0;
 308
 309error:
 310	for (i = 0; i < cleanup_count; i++) {
 311		if (cur_func->io[i]) {
 312			ibmphp_remove_resource(cur_func->io[i]);
 313			cur_func->io[i] = NULL;
 314		} else if (cur_func->pfmem[i]) {
 315			ibmphp_remove_resource(cur_func->pfmem[i]);
 316			cur_func->pfmem[i] = NULL;
 317		} else if (cur_func->mem[i]) {
 318			ibmphp_remove_resource(cur_func->mem[i]);
 319			cur_func->mem[i] = NULL;
 320		}
 321	}
 322	return rc;
 323}
 324
 325/*
 326 * This function configures the pci BARs of a single device.
 327 * Input: pointer to the pci_func
 328 * Output: configured PCI, 0, or error
 329 */
 330static int configure_device(struct pci_func *func)
 331{
 332	u32 bar[6];
 333	u32 address[] = {
 334		PCI_BASE_ADDRESS_0,
 335		PCI_BASE_ADDRESS_1,
 336		PCI_BASE_ADDRESS_2,
 337		PCI_BASE_ADDRESS_3,
 338		PCI_BASE_ADDRESS_4,
 339		PCI_BASE_ADDRESS_5,
 340		0
 341	};
 342	u8 irq;
 343	int count;
 344	int len[6];
 345	struct resource_node *io[6];
 346	struct resource_node *mem[6];
 347	struct resource_node *mem_tmp;
 348	struct resource_node *pfmem[6];
 349	unsigned int devfn;
 350
 351	debug("%s - inside\n", __func__);
 352
 353	devfn = PCI_DEVFN(func->device, func->function);
 354	ibmphp_pci_bus->number = func->busno;
 355
 356	for (count = 0; address[count]; count++) {	/* for 6 BARs */
 357
 358		/* not sure if i need this.  per scott, said maybe need * something like this
 359		   if devices don't adhere 100% to the spec, so don't want to write
 360		   to the reserved bits
 361
 362		pcibios_read_config_byte(cur_func->busno, cur_func->device,
 363		PCI_BASE_ADDRESS_0 + 4 * count, &tmp);
 364		if (tmp & 0x01) // IO
 365			pcibios_write_config_dword(cur_func->busno, cur_func->device,
 366			PCI_BASE_ADDRESS_0 + 4 * count, 0xFFFFFFFD);
 367		else  // Memory
 368			pcibios_write_config_dword(cur_func->busno, cur_func->device,
 369			PCI_BASE_ADDRESS_0 + 4 * count, 0xFFFFFFFF);
 370		 */
 371		pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], 0xFFFFFFFF);
 372		pci_bus_read_config_dword(ibmphp_pci_bus, devfn, address[count], &bar[count]);
 373
 374		if (!bar[count])	/* This BAR is not implemented */
 375			continue;
 376
 377		debug("Device %x BAR %d wants %x\n", func->device, count, bar[count]);
 378
 379		if (bar[count] & PCI_BASE_ADDRESS_SPACE_IO) {
 380			/* This is IO */
 381			debug("inside IO SPACE\n");
 382
 383			len[count] = bar[count] & 0xFFFFFFFC;
 384			len[count] = ~len[count] + 1;
 385
 386			debug("len[count] in IO %x, count %d\n", len[count], count);
 387
 388			io[count] = kzalloc(sizeof(struct resource_node), GFP_KERNEL);
 389
 390			if (!io[count])
 
 391				return -ENOMEM;
 392
 393			io[count]->type = IO;
 394			io[count]->busno = func->busno;
 395			io[count]->devfunc = PCI_DEVFN(func->device, func->function);
 396			io[count]->len = len[count];
 397			if (ibmphp_check_resource(io[count], 0) == 0) {
 398				ibmphp_add_resource(io[count]);
 399				func->io[count] = io[count];
 400			} else {
 401				err("cannot allocate requested io for bus %x device %x function %x len %x\n",
 402				     func->busno, func->device, func->function, len[count]);
 403				kfree(io[count]);
 404				return -EIO;
 405			}
 406			pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], func->io[count]->start);
 407
 408			/* _______________This is for debugging purposes only_____________________ */
 409			debug("b4 writing, the IO address is %x\n", func->io[count]->start);
 410			pci_bus_read_config_dword(ibmphp_pci_bus, devfn, address[count], &bar[count]);
 411			debug("after writing.... the start address is %x\n", bar[count]);
 412			/* _________________________________________________________________________*/
 413
 414		} else {
 415			/* This is Memory */
 416			if (bar[count] & PCI_BASE_ADDRESS_MEM_PREFETCH) {
 417				/* pfmem */
 418				debug("PFMEM SPACE\n");
 419
 420				len[count] = bar[count] & 0xFFFFFFF0;
 421				len[count] = ~len[count] + 1;
 422
 423				debug("len[count] in PFMEM %x, count %d\n", len[count], count);
 424
 425				pfmem[count] = kzalloc(sizeof(struct resource_node), GFP_KERNEL);
 426				if (!pfmem[count])
 
 427					return -ENOMEM;
 428
 429				pfmem[count]->type = PFMEM;
 430				pfmem[count]->busno = func->busno;
 431				pfmem[count]->devfunc = PCI_DEVFN(func->device,
 432							func->function);
 433				pfmem[count]->len = len[count];
 434				pfmem[count]->fromMem = 0;
 435				if (ibmphp_check_resource(pfmem[count], 0) == 0) {
 436					ibmphp_add_resource(pfmem[count]);
 437					func->pfmem[count] = pfmem[count];
 438				} else {
 439					mem_tmp = kzalloc(sizeof(*mem_tmp), GFP_KERNEL);
 440					if (!mem_tmp) {
 
 441						kfree(pfmem[count]);
 442						return -ENOMEM;
 443					}
 444					mem_tmp->type = MEM;
 445					mem_tmp->busno = pfmem[count]->busno;
 446					mem_tmp->devfunc = pfmem[count]->devfunc;
 447					mem_tmp->len = pfmem[count]->len;
 448					debug("there's no pfmem... going into mem.\n");
 449					if (ibmphp_check_resource(mem_tmp, 0) == 0) {
 450						ibmphp_add_resource(mem_tmp);
 451						pfmem[count]->fromMem = 1;
 452						pfmem[count]->rangeno = mem_tmp->rangeno;
 453						pfmem[count]->start = mem_tmp->start;
 454						pfmem[count]->end = mem_tmp->end;
 455						ibmphp_add_pfmem_from_mem(pfmem[count]);
 456						func->pfmem[count] = pfmem[count];
 457					} else {
 458						err("cannot allocate requested pfmem for bus %x, device %x, len %x\n",
 459						     func->busno, func->device, len[count]);
 460						kfree(mem_tmp);
 461						kfree(pfmem[count]);
 462						return -EIO;
 463					}
 464				}
 465
 466				pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], func->pfmem[count]->start);
 467
 468				/*_______________This is for debugging purposes only______________________________*/
 469				debug("b4 writing, start address is %x\n", func->pfmem[count]->start);
 470				pci_bus_read_config_dword(ibmphp_pci_bus, devfn, address[count], &bar[count]);
 471				debug("after writing, start address is %x\n", bar[count]);
 472				/*_________________________________________________________________________________*/
 473
 474				if (bar[count] & PCI_BASE_ADDRESS_MEM_TYPE_64) {	/* takes up another dword */
 475					debug("inside the mem 64 case, count %d\n", count);
 476					count += 1;
 477					/* on the 2nd dword, write all 0s, since we can't handle them n.e.ways */
 478					pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], 0x00000000);
 479				}
 480			} else {
 481				/* regular memory */
 482				debug("REGULAR MEM SPACE\n");
 483
 484				len[count] = bar[count] & 0xFFFFFFF0;
 485				len[count] = ~len[count] + 1;
 486
 487				debug("len[count] in Mem %x, count %d\n", len[count], count);
 488
 489				mem[count] = kzalloc(sizeof(struct resource_node), GFP_KERNEL);
 490				if (!mem[count])
 
 491					return -ENOMEM;
 492
 493				mem[count]->type = MEM;
 494				mem[count]->busno = func->busno;
 495				mem[count]->devfunc = PCI_DEVFN(func->device,
 496							func->function);
 497				mem[count]->len = len[count];
 498				if (ibmphp_check_resource(mem[count], 0) == 0) {
 499					ibmphp_add_resource(mem[count]);
 500					func->mem[count] = mem[count];
 501				} else {
 502					err("cannot allocate requested mem for bus %x, device %x, len %x\n",
 503					     func->busno, func->device, len[count]);
 504					kfree(mem[count]);
 505					return -EIO;
 506				}
 507				pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], func->mem[count]->start);
 508				/* _______________________This is for debugging purposes only _______________________*/
 509				debug("b4 writing, start address is %x\n", func->mem[count]->start);
 510				pci_bus_read_config_dword(ibmphp_pci_bus, devfn, address[count], &bar[count]);
 511				debug("after writing, the address is %x\n", bar[count]);
 512				/* __________________________________________________________________________________*/
 513
 514				if (bar[count] & PCI_BASE_ADDRESS_MEM_TYPE_64) {
 515					/* takes up another dword */
 516					debug("inside mem 64 case, reg. mem, count %d\n", count);
 517					count += 1;
 518					/* on the 2nd dword, write all 0s, since we can't handle them n.e.ways */
 519					pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], 0x00000000);
 520				}
 521			}
 522		}		/* end of mem */
 523	}			/* end of for */
 524
 525	func->bus = 0;		/* To indicate that this is not a PPB */
 526	pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_INTERRUPT_PIN, &irq);
 527	if ((irq > 0x00) && (irq < 0x05))
 528		pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_INTERRUPT_LINE, func->irq[irq - 1]);
 529
 530	pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_CACHE_LINE_SIZE, CACHE);
 531	pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_LATENCY_TIMER, LATENCY);
 532
 533	pci_bus_write_config_dword(ibmphp_pci_bus, devfn, PCI_ROM_ADDRESS, 0x00L);
 534	pci_bus_write_config_word(ibmphp_pci_bus, devfn, PCI_COMMAND, DEVICEENABLE);
 535
 536	return 0;
 537}
 538
 539/******************************************************************************
 540 * This routine configures a PCI-2-PCI bridge and the functions behind it
 541 * Parameters: pci_func
 542 * Returns:
 543 ******************************************************************************/
 544static int configure_bridge(struct pci_func **func_passed, u8 slotno)
 545{
 546	int count;
 547	int i;
 548	int rc;
 549	u8 sec_number;
 550	u8 io_base;
 551	u16 pfmem_base;
 552	u32 bar[2];
 553	u32 len[2];
 554	u8 flag_io = 0;
 555	u8 flag_mem = 0;
 556	u8 flag_pfmem = 0;
 557	u8 need_io_upper = 0;
 558	u8 need_pfmem_upper = 0;
 559	struct res_needed *amount_needed = NULL;
 560	struct resource_node *io = NULL;
 561	struct resource_node *bus_io[2] = {NULL, NULL};
 562	struct resource_node *mem = NULL;
 563	struct resource_node *bus_mem[2] = {NULL, NULL};
 564	struct resource_node *mem_tmp = NULL;
 565	struct resource_node *pfmem = NULL;
 566	struct resource_node *bus_pfmem[2] = {NULL, NULL};
 567	struct bus_node *bus;
 568	u32 address[] = {
 569		PCI_BASE_ADDRESS_0,
 570		PCI_BASE_ADDRESS_1,
 571		0
 572	};
 573	struct pci_func *func = *func_passed;
 574	unsigned int devfn;
 575	u8 irq;
 576	int retval;
 577
 578	debug("%s - enter\n", __func__);
 579
 580	devfn = PCI_DEVFN(func->function, func->device);
 581	ibmphp_pci_bus->number = func->busno;
 582
 583	/* Configuring necessary info for the bridge so that we could see the devices
 584	 * behind it
 585	 */
 586
 587	pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_PRIMARY_BUS, func->busno);
 588
 589	/* _____________________For debugging purposes only __________________________
 590	pci_bus_config_byte(ibmphp_pci_bus, devfn, PCI_PRIMARY_BUS, &pri_number);
 591	debug("primary # written into the bridge is %x\n", pri_number);
 592	 ___________________________________________________________________________*/
 593
 594	/* in EBDA, only get allocated 1 additional bus # per slot */
 595	sec_number = find_sec_number(func->busno, slotno);
 596	if (sec_number == 0xff) {
 597		err("cannot allocate secondary bus number for the bridged device\n");
 598		return -EINVAL;
 599	}
 600
 601	debug("after find_sec_number, the number we got is %x\n", sec_number);
 602	debug("AFTER FIND_SEC_NUMBER, func->busno IS %x\n", func->busno);
 603
 604	pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_SECONDARY_BUS, sec_number);
 605
 606	/* __________________For debugging purposes only __________________________________
 607	pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_SECONDARY_BUS, &sec_number);
 608	debug("sec_number after write/read is %x\n", sec_number);
 609	 ________________________________________________________________________________*/
 610
 611	pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_SUBORDINATE_BUS, sec_number);
 612
 613	/* __________________For debugging purposes only ____________________________________
 614	pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_SUBORDINATE_BUS, &sec_number);
 615	debug("subordinate number after write/read is %x\n", sec_number);
 616	 __________________________________________________________________________________*/
 617
 618	pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_CACHE_LINE_SIZE, CACHE);
 619	pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_LATENCY_TIMER, LATENCY);
 620	pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_SEC_LATENCY_TIMER, LATENCY);
 621
 622	debug("func->busno is %x\n", func->busno);
 623	debug("sec_number after writing is %x\n", sec_number);
 624
 625
 626	/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
 627	   !!!!!!!!!!!!!!!NEED TO ADD!!!  FAST BACK-TO-BACK ENABLE!!!!!!!!!!!!!!!!!!!!
 628	   !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
 629
 630
 631	/* First we need to allocate mem/io for the bridge itself in case it needs it */
 632	for (count = 0; address[count]; count++) {	/* for 2 BARs */
 633		pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], 0xFFFFFFFF);
 634		pci_bus_read_config_dword(ibmphp_pci_bus, devfn, address[count], &bar[count]);
 635
 636		if (!bar[count]) {
 637			/* This BAR is not implemented */
 638			debug("so we come here then, eh?, count = %d\n", count);
 639			continue;
 640		}
 641		//  tmp_bar = bar[count];
 642
 643		debug("Bar %d wants %x\n", count, bar[count]);
 644
 645		if (bar[count] & PCI_BASE_ADDRESS_SPACE_IO) {
 646			/* This is IO */
 647			len[count] = bar[count] & 0xFFFFFFFC;
 648			len[count] = ~len[count] + 1;
 649
 650			debug("len[count] in IO = %x\n", len[count]);
 651
 652			bus_io[count] = kzalloc(sizeof(struct resource_node), GFP_KERNEL);
 653
 654			if (!bus_io[count]) {
 
 655				retval = -ENOMEM;
 656				goto error;
 657			}
 658			bus_io[count]->type = IO;
 659			bus_io[count]->busno = func->busno;
 660			bus_io[count]->devfunc = PCI_DEVFN(func->device,
 661							func->function);
 662			bus_io[count]->len = len[count];
 663			if (ibmphp_check_resource(bus_io[count], 0) == 0) {
 664				ibmphp_add_resource(bus_io[count]);
 665				func->io[count] = bus_io[count];
 666			} else {
 667				err("cannot allocate requested io for bus %x, device %x, len %x\n",
 668				     func->busno, func->device, len[count]);
 669				kfree(bus_io[count]);
 670				return -EIO;
 671			}
 672
 673			pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], func->io[count]->start);
 674
 675		} else {
 676			/* This is Memory */
 677			if (bar[count] & PCI_BASE_ADDRESS_MEM_PREFETCH) {
 678				/* pfmem */
 679				len[count] = bar[count] & 0xFFFFFFF0;
 680				len[count] = ~len[count] + 1;
 681
 682				debug("len[count] in PFMEM = %x\n", len[count]);
 683
 684				bus_pfmem[count] = kzalloc(sizeof(struct resource_node), GFP_KERNEL);
 685				if (!bus_pfmem[count]) {
 
 686					retval = -ENOMEM;
 687					goto error;
 688				}
 689				bus_pfmem[count]->type = PFMEM;
 690				bus_pfmem[count]->busno = func->busno;
 691				bus_pfmem[count]->devfunc = PCI_DEVFN(func->device,
 692							func->function);
 693				bus_pfmem[count]->len = len[count];
 694				bus_pfmem[count]->fromMem = 0;
 695				if (ibmphp_check_resource(bus_pfmem[count], 0) == 0) {
 696					ibmphp_add_resource(bus_pfmem[count]);
 697					func->pfmem[count] = bus_pfmem[count];
 698				} else {
 699					mem_tmp = kzalloc(sizeof(*mem_tmp), GFP_KERNEL);
 700					if (!mem_tmp) {
 
 701						retval = -ENOMEM;
 702						goto error;
 703					}
 704					mem_tmp->type = MEM;
 705					mem_tmp->busno = bus_pfmem[count]->busno;
 706					mem_tmp->devfunc = bus_pfmem[count]->devfunc;
 707					mem_tmp->len = bus_pfmem[count]->len;
 708					if (ibmphp_check_resource(mem_tmp, 0) == 0) {
 709						ibmphp_add_resource(mem_tmp);
 710						bus_pfmem[count]->fromMem = 1;
 711						bus_pfmem[count]->rangeno = mem_tmp->rangeno;
 712						ibmphp_add_pfmem_from_mem(bus_pfmem[count]);
 713						func->pfmem[count] = bus_pfmem[count];
 714					} else {
 715						err("cannot allocate requested pfmem for bus %x, device %x, len %x\n",
 716						     func->busno, func->device, len[count]);
 717						kfree(mem_tmp);
 718						kfree(bus_pfmem[count]);
 719						return -EIO;
 720					}
 721				}
 722
 723				pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], func->pfmem[count]->start);
 724
 725				if (bar[count] & PCI_BASE_ADDRESS_MEM_TYPE_64) {
 726					/* takes up another dword */
 727					count += 1;
 728					/* on the 2nd dword, write all 0s, since we can't handle them n.e.ways */
 729					pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], 0x00000000);
 730
 731				}
 732			} else {
 733				/* regular memory */
 734				len[count] = bar[count] & 0xFFFFFFF0;
 735				len[count] = ~len[count] + 1;
 736
 737				debug("len[count] in Memory is %x\n", len[count]);
 738
 739				bus_mem[count] = kzalloc(sizeof(struct resource_node), GFP_KERNEL);
 740				if (!bus_mem[count]) {
 
 741					retval = -ENOMEM;
 742					goto error;
 743				}
 744				bus_mem[count]->type = MEM;
 745				bus_mem[count]->busno = func->busno;
 746				bus_mem[count]->devfunc = PCI_DEVFN(func->device,
 747							func->function);
 748				bus_mem[count]->len = len[count];
 749				if (ibmphp_check_resource(bus_mem[count], 0) == 0) {
 750					ibmphp_add_resource(bus_mem[count]);
 751					func->mem[count] = bus_mem[count];
 752				} else {
 753					err("cannot allocate requested mem for bus %x, device %x, len %x\n",
 754					     func->busno, func->device, len[count]);
 755					kfree(bus_mem[count]);
 756					return -EIO;
 757				}
 758
 759				pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], func->mem[count]->start);
 760
 761				if (bar[count] & PCI_BASE_ADDRESS_MEM_TYPE_64) {
 762					/* takes up another dword */
 763					count += 1;
 764					/* on the 2nd dword, write all 0s, since we can't handle them n.e.ways */
 765					pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], 0x00000000);
 766
 767				}
 768			}
 769		}		/* end of mem */
 770	}			/* end of for  */
 771
 772	/* Now need to see how much space the devices behind the bridge needed */
 773	amount_needed = scan_behind_bridge(func, sec_number);
 774	if (amount_needed == NULL)
 775		return -ENOMEM;
 776
 777	ibmphp_pci_bus->number = func->busno;
 778	debug("after coming back from scan_behind_bridge\n");
 779	debug("amount_needed->not_correct = %x\n", amount_needed->not_correct);
 780	debug("amount_needed->io = %x\n", amount_needed->io);
 781	debug("amount_needed->mem = %x\n", amount_needed->mem);
 782	debug("amount_needed->pfmem =  %x\n", amount_needed->pfmem);
 783
 784	if (amount_needed->not_correct) {
 785		debug("amount_needed is not correct\n");
 786		for (count = 0; address[count]; count++) {
 787			/* for 2 BARs */
 788			if (bus_io[count]) {
 789				ibmphp_remove_resource(bus_io[count]);
 790				func->io[count] = NULL;
 791			} else if (bus_pfmem[count]) {
 792				ibmphp_remove_resource(bus_pfmem[count]);
 793				func->pfmem[count] = NULL;
 794			} else if (bus_mem[count]) {
 795				ibmphp_remove_resource(bus_mem[count]);
 796				func->mem[count] = NULL;
 797			}
 798		}
 799		kfree(amount_needed);
 800		return -ENODEV;
 801	}
 802
 803	if (!amount_needed->io) {
 804		debug("it doesn't want IO?\n");
 805		flag_io = 1;
 806	} else {
 807		debug("it wants %x IO behind the bridge\n", amount_needed->io);
 808		io = kzalloc(sizeof(*io), GFP_KERNEL);
 809
 810		if (!io) {
 
 811			retval = -ENOMEM;
 812			goto error;
 813		}
 814		io->type = IO;
 815		io->busno = func->busno;
 816		io->devfunc = PCI_DEVFN(func->device, func->function);
 817		io->len = amount_needed->io;
 818		if (ibmphp_check_resource(io, 1) == 0) {
 819			debug("were we able to add io\n");
 820			ibmphp_add_resource(io);
 821			flag_io = 1;
 822		}
 823	}
 824
 825	if (!amount_needed->mem) {
 826		debug("it doesn't want n.e.memory?\n");
 827		flag_mem = 1;
 828	} else {
 829		debug("it wants %x memory behind the bridge\n", amount_needed->mem);
 830		mem = kzalloc(sizeof(*mem), GFP_KERNEL);
 831		if (!mem) {
 
 832			retval = -ENOMEM;
 833			goto error;
 834		}
 835		mem->type = MEM;
 836		mem->busno = func->busno;
 837		mem->devfunc = PCI_DEVFN(func->device, func->function);
 838		mem->len = amount_needed->mem;
 839		if (ibmphp_check_resource(mem, 1) == 0) {
 840			ibmphp_add_resource(mem);
 841			flag_mem = 1;
 842			debug("were we able to add mem\n");
 843		}
 844	}
 845
 846	if (!amount_needed->pfmem) {
 847		debug("it doesn't want n.e.pfmem mem?\n");
 848		flag_pfmem = 1;
 849	} else {
 850		debug("it wants %x pfmemory behind the bridge\n", amount_needed->pfmem);
 851		pfmem = kzalloc(sizeof(*pfmem), GFP_KERNEL);
 852		if (!pfmem) {
 
 853			retval = -ENOMEM;
 854			goto error;
 855		}
 856		pfmem->type = PFMEM;
 857		pfmem->busno = func->busno;
 858		pfmem->devfunc = PCI_DEVFN(func->device, func->function);
 859		pfmem->len = amount_needed->pfmem;
 860		pfmem->fromMem = 0;
 861		if (ibmphp_check_resource(pfmem, 1) == 0) {
 862			ibmphp_add_resource(pfmem);
 863			flag_pfmem = 1;
 864		} else {
 865			mem_tmp = kzalloc(sizeof(*mem_tmp), GFP_KERNEL);
 866			if (!mem_tmp) {
 
 867				retval = -ENOMEM;
 868				goto error;
 869			}
 870			mem_tmp->type = MEM;
 871			mem_tmp->busno = pfmem->busno;
 872			mem_tmp->devfunc = pfmem->devfunc;
 873			mem_tmp->len = pfmem->len;
 874			if (ibmphp_check_resource(mem_tmp, 1) == 0) {
 875				ibmphp_add_resource(mem_tmp);
 876				pfmem->fromMem = 1;
 877				pfmem->rangeno = mem_tmp->rangeno;
 878				ibmphp_add_pfmem_from_mem(pfmem);
 879				flag_pfmem = 1;
 880			}
 881		}
 882	}
 883
 884	debug("b4 if (flag_io && flag_mem && flag_pfmem)\n");
 885	debug("flag_io = %x, flag_mem = %x, flag_pfmem = %x\n", flag_io, flag_mem, flag_pfmem);
 886
 887	if (flag_io && flag_mem && flag_pfmem) {
 888		/* If on bootup, there was a bridged card in this slot,
 889		 * then card was removed and ibmphp got unloaded and loaded
 890		 * back again, there's no way for us to remove the bus
 891		 * struct, so no need to kmalloc, can use existing node
 892		 */
 893		bus = ibmphp_find_res_bus(sec_number);
 894		if (!bus) {
 895			bus = kzalloc(sizeof(*bus), GFP_KERNEL);
 896			if (!bus) {
 
 897				retval = -ENOMEM;
 898				goto error;
 899			}
 900			bus->busno = sec_number;
 901			debug("b4 adding new bus\n");
 902			rc = add_new_bus(bus, io, mem, pfmem, func->busno);
 903		} else if (!(bus->rangeIO) && !(bus->rangeMem) && !(bus->rangePFMem))
 904			rc = add_new_bus(bus, io, mem, pfmem, 0xFF);
 905		else {
 906			err("expected bus structure not empty?\n");
 907			retval = -EIO;
 908			goto error;
 909		}
 910		if (rc) {
 911			if (rc == -ENOMEM) {
 912				ibmphp_remove_bus(bus, func->busno);
 913				kfree(amount_needed);
 914				return rc;
 915			}
 916			retval = rc;
 917			goto error;
 918		}
 919		pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_IO_BASE, &io_base);
 920		pci_bus_read_config_word(ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_BASE, &pfmem_base);
 921
 922		if ((io_base & PCI_IO_RANGE_TYPE_MASK) == PCI_IO_RANGE_TYPE_32) {
 923			debug("io 32\n");
 924			need_io_upper = 1;
 925		}
 926		if ((pfmem_base & PCI_PREF_RANGE_TYPE_MASK) == PCI_PREF_RANGE_TYPE_64) {
 927			debug("pfmem 64\n");
 928			need_pfmem_upper = 1;
 929		}
 930
 931		if (bus->noIORanges) {
 932			pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_IO_BASE, 0x00 | bus->rangeIO->start >> 8);
 933			pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_IO_LIMIT, 0x00 | bus->rangeIO->end >> 8);
 934
 935			/* _______________This is for debugging purposes only ____________________
 936			pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_IO_BASE, &temp);
 937			debug("io_base = %x\n", (temp & PCI_IO_RANGE_TYPE_MASK) << 8);
 938			pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_IO_LIMIT, &temp);
 939			debug("io_limit = %x\n", (temp & PCI_IO_RANGE_TYPE_MASK) << 8);
 940			 ________________________________________________________________________*/
 941
 942			if (need_io_upper) {	/* since can't support n.e.ways */
 943				pci_bus_write_config_word(ibmphp_pci_bus, devfn, PCI_IO_BASE_UPPER16, 0x0000);
 944				pci_bus_write_config_word(ibmphp_pci_bus, devfn, PCI_IO_LIMIT_UPPER16, 0x0000);
 945			}
 946		} else {
 947			pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_IO_BASE, 0x00);
 948			pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_IO_LIMIT, 0x00);
 949		}
 950
 951		if (bus->noMemRanges) {
 952			pci_bus_write_config_word(ibmphp_pci_bus, devfn, PCI_MEMORY_BASE, 0x0000 | bus->rangeMem->start >> 16);
 953			pci_bus_write_config_word(ibmphp_pci_bus, devfn, PCI_MEMORY_LIMIT, 0x0000 | bus->rangeMem->end >> 16);
 954
 955			/* ____________________This is for debugging purposes only ________________________
 956			pci_bus_read_config_word(ibmphp_pci_bus, devfn, PCI_MEMORY_BASE, &temp);
 957			debug("mem_base = %x\n", (temp & PCI_MEMORY_RANGE_TYPE_MASK) << 16);
 958			pci_bus_read_config_word(ibmphp_pci_bus, devfn, PCI_MEMORY_LIMIT, &temp);
 959			debug("mem_limit = %x\n", (temp & PCI_MEMORY_RANGE_TYPE_MASK) << 16);
 960			 __________________________________________________________________________________*/
 961
 962		} else {
 963			pci_bus_write_config_word(ibmphp_pci_bus, devfn, PCI_MEMORY_BASE, 0xffff);
 964			pci_bus_write_config_word(ibmphp_pci_bus, devfn, PCI_MEMORY_LIMIT, 0x0000);
 965		}
 966		if (bus->noPFMemRanges) {
 967			pci_bus_write_config_word(ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_BASE, 0x0000 | bus->rangePFMem->start >> 16);
 968			pci_bus_write_config_word(ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, 0x0000 | bus->rangePFMem->end >> 16);
 969
 970			/* __________________________This is for debugging purposes only _______________________
 971			pci_bus_read_config_word(ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_BASE, &temp);
 972			debug("pfmem_base = %x", (temp & PCI_MEMORY_RANGE_TYPE_MASK) << 16);
 973			pci_bus_read_config_word(ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, &temp);
 974			debug("pfmem_limit = %x\n", (temp & PCI_MEMORY_RANGE_TYPE_MASK) << 16);
 975			 ______________________________________________________________________________________*/
 976
 977			if (need_pfmem_upper) {	/* since can't support n.e.ways */
 978				pci_bus_write_config_dword(ibmphp_pci_bus, devfn, PCI_PREF_BASE_UPPER32, 0x00000000);
 979				pci_bus_write_config_dword(ibmphp_pci_bus, devfn, PCI_PREF_LIMIT_UPPER32, 0x00000000);
 980			}
 981		} else {
 982			pci_bus_write_config_word(ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_BASE, 0xffff);
 983			pci_bus_write_config_word(ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, 0x0000);
 984		}
 985
 986		debug("b4 writing control information\n");
 987
 988		pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_INTERRUPT_PIN, &irq);
 989		if ((irq > 0x00) && (irq < 0x05))
 990			pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_INTERRUPT_LINE, func->irq[irq - 1]);
 991		/*
 992		pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_BRIDGE_CONTROL, ctrl);
 993		pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_BRIDGE_CONTROL, PCI_BRIDGE_CTL_PARITY);
 994		pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_BRIDGE_CONTROL, PCI_BRIDGE_CTL_SERR);
 995		 */
 996
 997		pci_bus_write_config_word(ibmphp_pci_bus, devfn, PCI_COMMAND, DEVICEENABLE);
 998		pci_bus_write_config_word(ibmphp_pci_bus, devfn, PCI_BRIDGE_CONTROL, 0x07);
 999		for (i = 0; i < 32; i++) {
1000			if (amount_needed->devices[i]) {
1001				debug("device where devices[i] is 1 = %x\n", i);
1002				func->devices[i] = 1;
1003			}
1004		}
1005		func->bus = 1;	/* For unconfiguring, to indicate it's PPB */
1006		func_passed = &func;
1007		debug("func->busno b4 returning is %x\n", func->busno);
1008		debug("func->busno b4 returning in the other structure is %x\n", (*func_passed)->busno);
1009		kfree(amount_needed);
1010		return 0;
1011	} else {
1012		err("Configuring bridge was unsuccessful...\n");
1013		mem_tmp = NULL;
1014		retval = -EIO;
1015		goto error;
1016	}
1017
1018error:
1019	kfree(amount_needed);
1020	if (pfmem)
1021		ibmphp_remove_resource(pfmem);
1022	if (io)
1023		ibmphp_remove_resource(io);
1024	if (mem)
1025		ibmphp_remove_resource(mem);
1026	for (i = 0; i < 2; i++) {	/* for 2 BARs */
1027		if (bus_io[i]) {
1028			ibmphp_remove_resource(bus_io[i]);
1029			func->io[i] = NULL;
1030		} else if (bus_pfmem[i]) {
1031			ibmphp_remove_resource(bus_pfmem[i]);
1032			func->pfmem[i] = NULL;
1033		} else if (bus_mem[i]) {
1034			ibmphp_remove_resource(bus_mem[i]);
1035			func->mem[i] = NULL;
1036		}
1037	}
1038	return retval;
1039}
1040
1041/*****************************************************************************
1042 * This function adds up the amount of resources needed behind the PPB bridge
1043 * and passes it to the configure_bridge function
1044 * Input: bridge function
1045 * Output: amount of resources needed
1046 *****************************************************************************/
1047static struct res_needed *scan_behind_bridge(struct pci_func *func, u8 busno)
1048{
1049	int count, len[6];
1050	u16 vendor_id;
1051	u8 hdr_type;
1052	u8 device, function;
1053	unsigned int devfn;
1054	int howmany = 0;	/*this is to see if there are any devices behind the bridge */
1055
1056	u32 bar[6], class;
1057	u32 address[] = {
1058		PCI_BASE_ADDRESS_0,
1059		PCI_BASE_ADDRESS_1,
1060		PCI_BASE_ADDRESS_2,
1061		PCI_BASE_ADDRESS_3,
1062		PCI_BASE_ADDRESS_4,
1063		PCI_BASE_ADDRESS_5,
1064		0
1065	};
1066	struct res_needed *amount;
1067
1068	amount = kzalloc(sizeof(*amount), GFP_KERNEL);
1069	if (amount == NULL)
1070		return NULL;
1071
1072	ibmphp_pci_bus->number = busno;
1073
1074	debug("the bus_no behind the bridge is %x\n", busno);
1075	debug("scanning devices behind the bridge...\n");
1076	for (device = 0; device < 32; device++) {
1077		amount->devices[device] = 0;
1078		for (function = 0; function < 8; function++) {
1079			devfn = PCI_DEVFN(device, function);
1080
1081			pci_bus_read_config_word(ibmphp_pci_bus, devfn, PCI_VENDOR_ID, &vendor_id);
1082
1083			if (vendor_id != PCI_VENDOR_ID_NOTVALID) {
1084				/* found correct device!!! */
1085				howmany++;
1086
1087				pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_HEADER_TYPE, &hdr_type);
1088				pci_bus_read_config_dword(ibmphp_pci_bus, devfn, PCI_CLASS_REVISION, &class);
1089
1090				debug("hdr_type behind the bridge is %x\n", hdr_type);
1091				if ((hdr_type & 0x7f) == PCI_HEADER_TYPE_BRIDGE) {
1092					err("embedded bridges not supported for hot-plugging.\n");
1093					amount->not_correct = 1;
1094					return amount;
1095				}
1096
1097				class >>= 8;	/* to take revision out, class = class.subclass.prog i/f */
1098				if (class == PCI_CLASS_NOT_DEFINED_VGA) {
1099					err("The device %x is VGA compatible and as is not supported for hot plugging.  Please choose another device.\n", device);
1100					amount->not_correct = 1;
1101					return amount;
1102				} else if (class == PCI_CLASS_DISPLAY_VGA) {
1103					err("The device %x is not supported for hot plugging.  Please choose another device.\n", device);
1104					amount->not_correct = 1;
1105					return amount;
1106				}
1107
1108				amount->devices[device] = 1;
1109
1110				for (count = 0; address[count]; count++) {
1111					/* for 6 BARs */
1112					/*
1113					pci_bus_read_config_byte(ibmphp_pci_bus, devfn, address[count], &tmp);
1114					if (tmp & 0x01) // IO
1115						pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], 0xFFFFFFFD);
1116					else // MEMORY
1117						pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], 0xFFFFFFFF);
1118					*/
1119					pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], 0xFFFFFFFF);
1120					pci_bus_read_config_dword(ibmphp_pci_bus, devfn, address[count], &bar[count]);
1121
1122					debug("what is bar[count]? %x, count = %d\n", bar[count], count);
1123
1124					if (!bar[count])	/* This BAR is not implemented */
1125						continue;
1126
1127					//tmp_bar = bar[count];
1128
1129					debug("count %d device %x function %x wants %x resources\n", count, device, function, bar[count]);
1130
1131					if (bar[count] & PCI_BASE_ADDRESS_SPACE_IO) {
1132						/* This is IO */
1133						len[count] = bar[count] & 0xFFFFFFFC;
1134						len[count] = ~len[count] + 1;
1135						amount->io += len[count];
1136					} else {
1137						/* This is Memory */
1138						if (bar[count] & PCI_BASE_ADDRESS_MEM_PREFETCH) {
1139							/* pfmem */
1140							len[count] = bar[count] & 0xFFFFFFF0;
1141							len[count] = ~len[count] + 1;
1142							amount->pfmem += len[count];
1143							if (bar[count] & PCI_BASE_ADDRESS_MEM_TYPE_64)
1144								/* takes up another dword */
1145								count += 1;
1146
1147						} else {
1148							/* regular memory */
1149							len[count] = bar[count] & 0xFFFFFFF0;
1150							len[count] = ~len[count] + 1;
1151							amount->mem += len[count];
1152							if (bar[count] & PCI_BASE_ADDRESS_MEM_TYPE_64) {
1153								/* takes up another dword */
1154								count += 1;
1155							}
1156						}
1157					}
1158				}	/* end for */
1159			}	/* end if (valid) */
1160		}	/* end for */
1161	}	/* end for */
1162
1163	if (!howmany)
1164		amount->not_correct = 1;
1165	else
1166		amount->not_correct = 0;
1167	if ((amount->io) && (amount->io < IOBRIDGE))
1168		amount->io = IOBRIDGE;
1169	if ((amount->mem) && (amount->mem < MEMBRIDGE))
1170		amount->mem = MEMBRIDGE;
1171	if ((amount->pfmem) && (amount->pfmem < MEMBRIDGE))
1172		amount->pfmem = MEMBRIDGE;
1173	return amount;
1174}
1175
1176/* The following 3 unconfigure_boot_ routines deal with the case when we had the card
1177 * upon bootup in the system, since we don't allocate func to such case, we need to read
1178 * the start addresses from pci config space and then find the corresponding entries in
1179 * our resource lists.  The functions return either 0, -ENODEV, or -1 (general failure)
1180 * Change: we also call these functions even if we configured the card ourselves (i.e., not
1181 * the bootup case), since it should work same way
1182 */
1183static int unconfigure_boot_device(u8 busno, u8 device, u8 function)
1184{
1185	u32 start_address;
1186	u32 address[] = {
1187		PCI_BASE_ADDRESS_0,
1188		PCI_BASE_ADDRESS_1,
1189		PCI_BASE_ADDRESS_2,
1190		PCI_BASE_ADDRESS_3,
1191		PCI_BASE_ADDRESS_4,
1192		PCI_BASE_ADDRESS_5,
1193		0
1194	};
1195	int count;
1196	struct resource_node *io;
1197	struct resource_node *mem;
1198	struct resource_node *pfmem;
1199	struct bus_node *bus;
1200	u32 end_address;
1201	u32 temp_end;
1202	u32 size;
1203	u32 tmp_address;
1204	unsigned int devfn;
1205
1206	debug("%s - enter\n", __func__);
1207
1208	bus = ibmphp_find_res_bus(busno);
1209	if (!bus) {
1210		debug("cannot find corresponding bus.\n");
1211		return -EINVAL;
1212	}
1213
1214	devfn = PCI_DEVFN(device, function);
1215	ibmphp_pci_bus->number = busno;
1216	for (count = 0; address[count]; count++) {	/* for 6 BARs */
1217		pci_bus_read_config_dword(ibmphp_pci_bus, devfn, address[count], &start_address);
1218
1219		/* We can do this here, b/c by that time the device driver of the card has been stopped */
1220
1221		pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], 0xFFFFFFFF);
1222		pci_bus_read_config_dword(ibmphp_pci_bus, devfn, address[count], &size);
1223		pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], start_address);
1224
1225		debug("start_address is %x\n", start_address);
1226		debug("busno, device, function %x %x %x\n", busno, device, function);
1227		if (!size) {
1228			/* This BAR is not implemented */
1229			debug("is this bar no implemented?, count = %d\n", count);
1230			continue;
1231		}
1232		tmp_address = start_address;
1233		if (start_address & PCI_BASE_ADDRESS_SPACE_IO) {
1234			/* This is IO */
1235			start_address &= PCI_BASE_ADDRESS_IO_MASK;
1236			size = size & 0xFFFFFFFC;
1237			size = ~size + 1;
1238			end_address = start_address + size - 1;
1239			if (ibmphp_find_resource(bus, start_address, &io, IO))
1240				goto report_search_failure;
1241
 
1242			debug("io->start = %x\n", io->start);
1243			temp_end = io->end;
1244			start_address = io->end + 1;
1245			ibmphp_remove_resource(io);
1246			/* This is needed b/c of the old I/O restrictions in the BIOS */
1247			while (temp_end < end_address) {
1248				if (ibmphp_find_resource(bus, start_address,
1249							 &io, IO))
1250					goto report_search_failure;
1251
1252				debug("io->start = %x\n", io->start);
1253				temp_end = io->end;
1254				start_address = io->end + 1;
1255				ibmphp_remove_resource(io);
1256			}
1257
1258			/* ????????? DO WE NEED TO WRITE ANYTHING INTO THE PCI CONFIG SPACE BACK ?????????? */
1259		} else {
1260			/* This is Memory */
1261			if (start_address & PCI_BASE_ADDRESS_MEM_PREFETCH) {
1262				/* pfmem */
1263				debug("start address of pfmem is %x\n", start_address);
1264				start_address &= PCI_BASE_ADDRESS_MEM_MASK;
1265
1266				if (ibmphp_find_resource(bus, start_address, &pfmem, PFMEM) < 0) {
1267					err("cannot find corresponding PFMEM resource to remove\n");
1268					return -EIO;
1269				}
1270				if (pfmem) {
1271					debug("pfmem->start = %x\n", pfmem->start);
1272
1273					ibmphp_remove_resource(pfmem);
1274				}
1275			} else {
1276				/* regular memory */
1277				debug("start address of mem is %x\n", start_address);
1278				start_address &= PCI_BASE_ADDRESS_MEM_MASK;
1279
1280				if (ibmphp_find_resource(bus, start_address, &mem, MEM) < 0) {
1281					err("cannot find corresponding MEM resource to remove\n");
1282					return -EIO;
1283				}
1284				if (mem) {
1285					debug("mem->start = %x\n", mem->start);
1286
1287					ibmphp_remove_resource(mem);
1288				}
1289			}
1290			if (tmp_address & PCI_BASE_ADDRESS_MEM_TYPE_64) {
1291				/* takes up another dword */
1292				count += 1;
1293			}
1294		}	/* end of mem */
1295	}	/* end of for */
1296
1297	return 0;
1298
1299report_search_failure:
1300	err("cannot find corresponding IO resource to remove\n");
1301	return -EIO;
1302}
1303
1304static int unconfigure_boot_bridge(u8 busno, u8 device, u8 function)
1305{
1306	int count;
1307	int bus_no, pri_no, sub_no, sec_no = 0;
1308	u32 start_address, tmp_address;
1309	u8 sec_number, sub_number, pri_number;
1310	struct resource_node *io = NULL;
1311	struct resource_node *mem = NULL;
1312	struct resource_node *pfmem = NULL;
1313	struct bus_node *bus;
1314	u32 address[] = {
1315		PCI_BASE_ADDRESS_0,
1316		PCI_BASE_ADDRESS_1,
1317		0
1318	};
1319	unsigned int devfn;
1320
1321	devfn = PCI_DEVFN(device, function);
1322	ibmphp_pci_bus->number = busno;
1323	bus_no = (int) busno;
1324	debug("busno is %x\n", busno);
1325	pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_PRIMARY_BUS, &pri_number);
1326	debug("%s - busno = %x, primary_number = %x\n", __func__, busno, pri_number);
1327
1328	pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_SECONDARY_BUS, &sec_number);
1329	debug("sec_number is %x\n", sec_number);
1330	sec_no = (int) sec_number;
1331	pri_no = (int) pri_number;
1332	if (pri_no != bus_no) {
1333		err("primary numbers in our structures and pci config space don't match.\n");
1334		return -EINVAL;
1335	}
1336
1337	pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_SUBORDINATE_BUS, &sub_number);
1338	sub_no = (int) sub_number;
1339	debug("sub_no is %d, sec_no is %d\n", sub_no, sec_no);
1340	if (sec_no != sub_number) {
1341		err("there're more buses behind this bridge.  Hot removal is not supported.  Please choose another card\n");
1342		return -ENODEV;
1343	}
1344
1345	bus = ibmphp_find_res_bus(sec_number);
1346	if (!bus) {
1347		err("cannot find Bus structure for the bridged device\n");
1348		return -EINVAL;
1349	}
1350	debug("bus->busno is %x\n", bus->busno);
1351	debug("sec_number is %x\n", sec_number);
1352
1353	ibmphp_remove_bus(bus, busno);
1354
1355	for (count = 0; address[count]; count++) {
1356		/* for 2 BARs */
1357		pci_bus_read_config_dword(ibmphp_pci_bus, devfn, address[count], &start_address);
1358
1359		if (!start_address) {
1360			/* This BAR is not implemented */
1361			continue;
1362		}
1363
1364		tmp_address = start_address;
1365
1366		if (start_address & PCI_BASE_ADDRESS_SPACE_IO) {
1367			/* This is IO */
1368			start_address &= PCI_BASE_ADDRESS_IO_MASK;
1369			if (ibmphp_find_resource(bus, start_address, &io, IO) < 0) {
1370				err("cannot find corresponding IO resource to remove\n");
1371				return -EIO;
1372			}
1373			if (io)
1374				debug("io->start = %x\n", io->start);
1375
1376			ibmphp_remove_resource(io);
1377
1378			/* ????????? DO WE NEED TO WRITE ANYTHING INTO THE PCI CONFIG SPACE BACK ?????????? */
1379		} else {
1380			/* This is Memory */
1381			if (start_address & PCI_BASE_ADDRESS_MEM_PREFETCH) {
1382				/* pfmem */
1383				start_address &= PCI_BASE_ADDRESS_MEM_MASK;
1384				if (ibmphp_find_resource(bus, start_address, &pfmem, PFMEM) < 0) {
1385					err("cannot find corresponding PFMEM resource to remove\n");
1386					return -EINVAL;
1387				}
1388				if (pfmem) {
1389					debug("pfmem->start = %x\n", pfmem->start);
1390
1391					ibmphp_remove_resource(pfmem);
1392				}
1393			} else {
1394				/* regular memory */
1395				start_address &= PCI_BASE_ADDRESS_MEM_MASK;
1396				if (ibmphp_find_resource(bus, start_address, &mem, MEM) < 0) {
1397					err("cannot find corresponding MEM resource to remove\n");
1398					return -EINVAL;
1399				}
1400				if (mem) {
1401					debug("mem->start = %x\n", mem->start);
1402
1403					ibmphp_remove_resource(mem);
1404				}
1405			}
1406			if (tmp_address & PCI_BASE_ADDRESS_MEM_TYPE_64) {
1407				/* takes up another dword */
1408				count += 1;
1409			}
1410		}	/* end of mem */
1411	}	/* end of for */
1412	debug("%s - exiting, returning success\n", __func__);
1413	return 0;
1414}
1415
1416static int unconfigure_boot_card(struct slot *slot_cur)
1417{
1418	u16 vendor_id;
1419	u32 class;
1420	u8 hdr_type;
1421	u8 device;
1422	u8 busno;
1423	u8 function;
1424	int rc;
1425	unsigned int devfn;
1426	u8 valid_device = 0x00; /* To see if we are ever able to find valid device and read it */
1427
1428	debug("%s - enter\n", __func__);
1429
1430	device = slot_cur->device;
1431	busno = slot_cur->bus;
1432
1433	debug("b4 for loop, device is %x\n", device);
1434	/* For every function on the card */
1435	for (function = 0x0; function < 0x08; function++) {
1436		devfn = PCI_DEVFN(device, function);
1437		ibmphp_pci_bus->number = busno;
1438
1439		pci_bus_read_config_word(ibmphp_pci_bus, devfn, PCI_VENDOR_ID, &vendor_id);
1440
1441		if (vendor_id != PCI_VENDOR_ID_NOTVALID) {
1442			/* found correct device!!! */
1443			++valid_device;
1444
1445			debug("%s - found correct device\n", __func__);
1446
1447			/* header: x x x x x x x x
1448			 *         | |___________|=> 1=PPB bridge, 0=normal device, 2=CardBus Bridge
1449			 *         |_=> 0 = single function device, 1 = multi-function device
1450			 */
1451
1452			pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_HEADER_TYPE, &hdr_type);
1453			pci_bus_read_config_dword(ibmphp_pci_bus, devfn, PCI_CLASS_REVISION, &class);
1454
1455			debug("hdr_type %x, class %x\n", hdr_type, class);
1456			class >>= 8;	/* to take revision out, class = class.subclass.prog i/f */
1457			if (class == PCI_CLASS_NOT_DEFINED_VGA) {
1458				err("The device %x function %x is VGA compatible and is not supported for hot removing.  Please choose another device.\n", device, function);
1459				return -ENODEV;
1460			} else if (class == PCI_CLASS_DISPLAY_VGA) {
1461				err("The device %x function %x is not supported for hot removing.  Please choose another device.\n", device, function);
1462				return -ENODEV;
1463			}
1464
1465			switch (hdr_type) {
1466				case PCI_HEADER_TYPE_NORMAL:
1467					rc = unconfigure_boot_device(busno, device, function);
1468					if (rc) {
1469						err("was not able to unconfigure device %x func %x on bus %x. bailing out...\n",
1470						     device, function, busno);
1471						return rc;
1472					}
1473					function = 0x8;
1474					break;
1475				case PCI_HEADER_TYPE_MULTIDEVICE:
1476					rc = unconfigure_boot_device(busno, device, function);
1477					if (rc) {
1478						err("was not able to unconfigure device %x func %x on bus %x. bailing out...\n",
1479						     device, function, busno);
1480						return rc;
1481					}
1482					break;
1483				case PCI_HEADER_TYPE_BRIDGE:
1484					class >>= 8;
1485					if (class != PCI_CLASS_BRIDGE_PCI) {
1486						err("This device %x function %x is not PCI-to-PCI bridge, and is not supported for hot-removing.  Please try another card.\n", device, function);
1487						return -ENODEV;
1488					}
1489					rc = unconfigure_boot_bridge(busno, device, function);
1490					if (rc != 0) {
1491						err("was not able to hot-remove PPB properly.\n");
1492						return rc;
1493					}
1494
1495					function = 0x8;
1496					break;
1497				case PCI_HEADER_TYPE_MULTIBRIDGE:
1498					class >>= 8;
1499					if (class != PCI_CLASS_BRIDGE_PCI) {
1500						err("This device %x function %x is not PCI-to-PCI bridge,  and is not supported for hot-removing.  Please try another card.\n", device, function);
1501						return -ENODEV;
1502					}
1503					rc = unconfigure_boot_bridge(busno, device, function);
1504					if (rc != 0) {
1505						err("was not able to hot-remove PPB properly.\n");
1506						return rc;
1507					}
1508					break;
1509				default:
1510					err("MAJOR PROBLEM!!!! Cannot read device's header\n");
1511					return -1;
1512					break;
1513			}	/* end of switch */
1514		}	/* end of valid device */
1515	}	/* end of for */
1516
1517	if (!valid_device) {
1518		err("Could not find device to unconfigure.  Or could not read the card.\n");
1519		return -1;
1520	}
1521	return 0;
1522}
1523
1524/*
1525 * free the resources of the card (multi, single, or bridged)
1526 * Parameters: slot, flag to say if this is for removing entire module or just
1527 * unconfiguring the device
1528 * TO DO:  will probably need to add some code in case there was some resource,
1529 * to remove it... this is from when we have errors in the configure_card...
1530 *			!!!!!!!!!!!!!!!!!!!!!!!!!FOR BUSES!!!!!!!!!!!!
1531 * Returns: 0, -1, -ENODEV
1532 */
1533int ibmphp_unconfigure_card(struct slot **slot_cur, int the_end)
1534{
1535	int i;
1536	int count;
1537	int rc;
1538	struct slot *sl = *slot_cur;
1539	struct pci_func *cur_func = NULL;
1540	struct pci_func *temp_func;
1541
1542	debug("%s - enter\n", __func__);
1543
1544	if (!the_end) {
1545		/* Need to unconfigure the card */
1546		rc = unconfigure_boot_card(sl);
1547		if ((rc == -ENODEV) || (rc == -EIO) || (rc == -EINVAL)) {
1548			/* In all other cases, will still need to get rid of func structure if it exists */
1549			return rc;
1550		}
1551	}
1552
1553	if (sl->func) {
1554		cur_func = sl->func;
1555		while (cur_func) {
1556			/* TO DO: WILL MOST LIKELY NEED TO GET RID OF THE BUS STRUCTURE FROM RESOURCES AS WELL */
1557			if (cur_func->bus) {
1558				/* in other words, it's a PPB */
1559				count = 2;
1560			} else {
1561				count = 6;
1562			}
1563
1564			for (i = 0; i < count; i++) {
1565				if (cur_func->io[i]) {
1566					debug("io[%d] exists\n", i);
1567					if (the_end > 0)
1568						ibmphp_remove_resource(cur_func->io[i]);
1569					cur_func->io[i] = NULL;
1570				}
1571				if (cur_func->mem[i]) {
1572					debug("mem[%d] exists\n", i);
1573					if (the_end > 0)
1574						ibmphp_remove_resource(cur_func->mem[i]);
1575					cur_func->mem[i] = NULL;
1576				}
1577				if (cur_func->pfmem[i]) {
1578					debug("pfmem[%d] exists\n", i);
1579					if (the_end > 0)
1580						ibmphp_remove_resource(cur_func->pfmem[i]);
1581					cur_func->pfmem[i] = NULL;
1582				}
1583			}
1584
1585			temp_func = cur_func->next;
1586			kfree(cur_func);
1587			cur_func = temp_func;
1588		}
1589	}
1590
1591	sl->func = NULL;
1592	*slot_cur = sl;
1593	debug("%s - exit\n", __func__);
1594	return 0;
1595}
1596
1597/*
1598 * add a new bus resulting from hot-plugging a PPB bridge with devices
1599 *
1600 * Input: bus and the amount of resources needed (we know we can assign those,
1601 *        since they've been checked already
1602 * Output: bus added to the correct spot
1603 *         0, -1, error
1604 */
1605static int add_new_bus(struct bus_node *bus, struct resource_node *io, struct resource_node *mem, struct resource_node *pfmem, u8 parent_busno)
1606{
1607	struct range_node *io_range = NULL;
1608	struct range_node *mem_range = NULL;
1609	struct range_node *pfmem_range = NULL;
1610	struct bus_node *cur_bus = NULL;
1611
1612	/* Trying to find the parent bus number */
1613	if (parent_busno != 0xFF) {
1614		cur_bus	= ibmphp_find_res_bus(parent_busno);
1615		if (!cur_bus) {
1616			err("strange, cannot find bus which is supposed to be at the system... something is terribly wrong...\n");
1617			return -ENODEV;
1618		}
1619
1620		list_add(&bus->bus_list, &cur_bus->bus_list);
1621	}
1622	if (io) {
1623		io_range = kzalloc(sizeof(*io_range), GFP_KERNEL);
1624		if (!io_range)
 
1625			return -ENOMEM;
1626
1627		io_range->start = io->start;
1628		io_range->end = io->end;
1629		io_range->rangeno = 1;
1630		bus->noIORanges = 1;
1631		bus->rangeIO = io_range;
1632	}
1633	if (mem) {
1634		mem_range = kzalloc(sizeof(*mem_range), GFP_KERNEL);
1635		if (!mem_range)
 
1636			return -ENOMEM;
1637
1638		mem_range->start = mem->start;
1639		mem_range->end = mem->end;
1640		mem_range->rangeno = 1;
1641		bus->noMemRanges = 1;
1642		bus->rangeMem = mem_range;
1643	}
1644	if (pfmem) {
1645		pfmem_range = kzalloc(sizeof(*pfmem_range), GFP_KERNEL);
1646		if (!pfmem_range)
 
1647			return -ENOMEM;
1648
1649		pfmem_range->start = pfmem->start;
1650		pfmem_range->end = pfmem->end;
1651		pfmem_range->rangeno = 1;
1652		bus->noPFMemRanges = 1;
1653		bus->rangePFMem = pfmem_range;
1654	}
1655	return 0;
1656}
1657
1658/*
1659 * find the 1st available bus number for PPB to set as its secondary bus
1660 * Parameters: bus_number of the primary bus
1661 * Returns: bus_number of the secondary bus or 0xff in case of failure
1662 */
1663static u8 find_sec_number(u8 primary_busno, u8 slotno)
1664{
1665	int min, max;
1666	u8 busno;
1667	struct bus_info *bus;
1668	struct bus_node *bus_cur;
1669
1670	bus = ibmphp_find_same_bus_num(primary_busno);
1671	if (!bus) {
1672		err("cannot get slot range of the bus from the BIOS\n");
1673		return 0xff;
1674	}
1675	max = bus->slot_max;
1676	min = bus->slot_min;
1677	if ((slotno > max) || (slotno < min)) {
1678		err("got the wrong range\n");
1679		return 0xff;
1680	}
1681	busno = (u8) (slotno - (u8) min);
1682	busno += primary_busno + 0x01;
1683	bus_cur = ibmphp_find_res_bus(busno);
1684	/* either there is no such bus number, or there are no ranges, which
1685	 * can only happen if we removed the bridged device in previous load
1686	 * of the driver, and now only have the skeleton bus struct
1687	 */
1688	if ((!bus_cur) || (!(bus_cur->rangeIO) && !(bus_cur->rangeMem) && !(bus_cur->rangePFMem)))
1689		return busno;
1690	return 0xff;
1691}
v4.10.11
 
   1/*
   2 * IBM Hot Plug Controller Driver
   3 *
   4 * Written By: Irene Zubarev, IBM Corporation
   5 *
   6 * Copyright (C) 2001 Greg Kroah-Hartman (greg@kroah.com)
   7 * Copyright (C) 2001,2002 IBM Corp.
   8 *
   9 * All rights reserved.
  10 *
  11 * This program is free software; you can redistribute it and/or modify
  12 * it under the terms of the GNU General Public License as published by
  13 * the Free Software Foundation; either version 2 of the License, or (at
  14 * your option) any later version.
  15 *
  16 * This program is distributed in the hope that it will be useful, but
  17 * WITHOUT ANY WARRANTY; without even the implied warranty of
  18 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  19 * NON INFRINGEMENT.  See the GNU General Public License for more
  20 * details.
  21 *
  22 * You should have received a copy of the GNU General Public License
  23 * along with this program; if not, write to the Free Software
  24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  25 *
  26 * Send feedback to <gregkh@us.ibm.com>
  27 *
  28 */
  29
  30#include <linux/module.h>
  31#include <linux/slab.h>
  32#include <linux/pci.h>
  33#include <linux/list.h>
  34#include "ibmphp.h"
  35
  36
  37static int configure_device(struct pci_func *);
  38static int configure_bridge(struct pci_func **, u8);
  39static struct res_needed *scan_behind_bridge(struct pci_func *, u8);
  40static int add_new_bus(struct bus_node *, struct resource_node *, struct resource_node *, struct resource_node *, u8);
  41static u8 find_sec_number(u8 primary_busno, u8 slotno);
  42
  43/*
  44 * NOTE..... If BIOS doesn't provide default routing, we assign:
  45 * 9 for SCSI, 10 for LAN adapters, and 11 for everything else.
  46 * If adapter is bridged, then we assign 11 to it and devices behind it.
  47 * We also assign the same irq numbers for multi function devices.
  48 * These are PIC mode, so shouldn't matter n.e.ways (hopefully)
  49 */
  50static void assign_alt_irq(struct pci_func *cur_func, u8 class_code)
  51{
  52	int j;
  53	for (j = 0; j < 4; j++) {
  54		if (cur_func->irq[j] == 0xff) {
  55			switch (class_code) {
  56				case PCI_BASE_CLASS_STORAGE:
  57					cur_func->irq[j] = SCSI_IRQ;
  58					break;
  59				case PCI_BASE_CLASS_NETWORK:
  60					cur_func->irq[j] = LAN_IRQ;
  61					break;
  62				default:
  63					cur_func->irq[j] = OTHER_IRQ;
  64					break;
  65			}
  66		}
  67	}
  68}
  69
  70/*
  71 * Configures the device to be added (will allocate needed resources if it
  72 * can), the device can be a bridge or a regular pci device, can also be
  73 * multi-functional
  74 *
  75 * Input: function to be added
  76 *
  77 * TO DO:  The error case with Multifunction device or multi function bridge,
  78 * if there is an error, will need to go through all previous functions and
  79 * unconfigure....or can add some code into unconfigure_card....
  80 */
  81int ibmphp_configure_card(struct pci_func *func, u8 slotno)
  82{
  83	u16 vendor_id;
  84	u32 class;
  85	u8 class_code;
  86	u8 hdr_type, device, sec_number;
  87	u8 function;
  88	struct pci_func *newfunc;	/* for multi devices */
  89	struct pci_func *cur_func, *prev_func;
  90	int rc, i, j;
  91	int cleanup_count;
  92	u8 flag;
  93	u8 valid_device = 0x00; /* to see if we are able to read from card any device info at all */
  94
  95	debug("inside configure_card, func->busno = %x\n", func->busno);
  96
  97	device = func->device;
  98	cur_func = func;
  99
 100	/* We only get bus and device from IRQ routing table.  So at this point,
 101	 * func->busno is correct, and func->device contains only device (at the 5
 102	 * highest bits)
 103	 */
 104
 105	/* For every function on the card */
 106	for (function = 0x00; function < 0x08; function++) {
 107		unsigned int devfn = PCI_DEVFN(device, function);
 108		ibmphp_pci_bus->number = cur_func->busno;
 109
 110		cur_func->function = function;
 111
 112		debug("inside the loop, cur_func->busno = %x, cur_func->device = %x, cur_func->function = %x\n",
 113			cur_func->busno, cur_func->device, cur_func->function);
 114
 115		pci_bus_read_config_word(ibmphp_pci_bus, devfn, PCI_VENDOR_ID, &vendor_id);
 116
 117		debug("vendor_id is %x\n", vendor_id);
 118		if (vendor_id != PCI_VENDOR_ID_NOTVALID) {
 119			/* found correct device!!! */
 120			debug("found valid device, vendor_id = %x\n", vendor_id);
 121
 122			++valid_device;
 123
 124			/* header: x x x x x x x x
 125			 *         | |___________|=> 1=PPB bridge, 0=normal device, 2=CardBus Bridge
 126			 *         |_=> 0 = single function device, 1 = multi-function device
 127			 */
 128
 129			pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_HEADER_TYPE, &hdr_type);
 130			pci_bus_read_config_dword(ibmphp_pci_bus, devfn, PCI_CLASS_REVISION, &class);
 131
 132			class_code = class >> 24;
 133			debug("hrd_type = %x, class = %x, class_code %x\n", hdr_type, class, class_code);
 134			class >>= 8;	/* to take revision out, class = class.subclass.prog i/f */
 135			if (class == PCI_CLASS_NOT_DEFINED_VGA) {
 136				err("The device %x is VGA compatible and as is not supported for hot plugging. "
 137				     "Please choose another device.\n", cur_func->device);
 138				return -ENODEV;
 139			} else if (class == PCI_CLASS_DISPLAY_VGA) {
 140				err("The device %x is not supported for hot plugging. Please choose another device.\n",
 141				     cur_func->device);
 142				return -ENODEV;
 143			}
 144			switch (hdr_type) {
 145				case PCI_HEADER_TYPE_NORMAL:
 146					debug("single device case.... vendor id = %x, hdr_type = %x, class = %x\n", vendor_id, hdr_type, class);
 147					assign_alt_irq(cur_func, class_code);
 148					rc = configure_device(cur_func);
 149					if (rc < 0) {
 150						/* We need to do this in case some other BARs were properly inserted */
 151						err("was not able to configure devfunc %x on bus %x.\n",
 152						     cur_func->device, cur_func->busno);
 153						cleanup_count = 6;
 154						goto error;
 155					}
 156					cur_func->next = NULL;
 157					function = 0x8;
 158					break;
 159				case PCI_HEADER_TYPE_MULTIDEVICE:
 160					assign_alt_irq(cur_func, class_code);
 161					rc = configure_device(cur_func);
 162					if (rc < 0) {
 163						/* We need to do this in case some other BARs were properly inserted */
 164						err("was not able to configure devfunc %x on bus %x...bailing out\n",
 165						     cur_func->device, cur_func->busno);
 166						cleanup_count = 6;
 167						goto error;
 168					}
 169					newfunc = kzalloc(sizeof(*newfunc), GFP_KERNEL);
 170					if (!newfunc) {
 171						err("out of system memory\n");
 172						return -ENOMEM;
 173					}
 174					newfunc->busno = cur_func->busno;
 175					newfunc->device = device;
 176					cur_func->next = newfunc;
 177					cur_func = newfunc;
 178					for (j = 0; j < 4; j++)
 179						newfunc->irq[j] = cur_func->irq[j];
 180					break;
 181				case PCI_HEADER_TYPE_MULTIBRIDGE:
 182					class >>= 8;
 183					if (class != PCI_CLASS_BRIDGE_PCI) {
 184						err("This %x is not PCI-to-PCI bridge, and as is not supported for hot-plugging.  Please insert another card.\n",
 185						     cur_func->device);
 186						return -ENODEV;
 187					}
 188					assign_alt_irq(cur_func, class_code);
 189					rc = configure_bridge(&cur_func, slotno);
 190					if (rc == -ENODEV) {
 191						err("You chose to insert Single Bridge, or nested bridges, this is not supported...\n");
 192						err("Bus %x, devfunc %x\n", cur_func->busno, cur_func->device);
 193						return rc;
 194					}
 195					if (rc) {
 196						/* We need to do this in case some other BARs were properly inserted */
 197						err("was not able to hot-add PPB properly.\n");
 198						func->bus = 1; /* To indicate to the unconfigure function that this is a PPB */
 199						cleanup_count = 2;
 200						goto error;
 201					}
 202
 203					pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_SECONDARY_BUS, &sec_number);
 204					flag = 0;
 205					for (i = 0; i < 32; i++) {
 206						if (func->devices[i]) {
 207							newfunc = kzalloc(sizeof(*newfunc), GFP_KERNEL);
 208							if (!newfunc) {
 209								err("out of system memory\n");
 210								return -ENOMEM;
 211							}
 212							newfunc->busno = sec_number;
 213							newfunc->device = (u8) i;
 214							for (j = 0; j < 4; j++)
 215								newfunc->irq[j] = cur_func->irq[j];
 216
 217							if (flag) {
 218								for (prev_func = cur_func; prev_func->next; prev_func = prev_func->next) ;
 219								prev_func->next = newfunc;
 220							} else
 221								cur_func->next = newfunc;
 222
 223							rc = ibmphp_configure_card(newfunc, slotno);
 224							/* This could only happen if kmalloc failed */
 225							if (rc) {
 226								/* We need to do this in case bridge itself got configured properly, but devices behind it failed */
 227								func->bus = 1; /* To indicate to the unconfigure function that this is a PPB */
 228								cleanup_count = 2;
 229								goto error;
 230							}
 231							flag = 1;
 232						}
 233					}
 234
 235					newfunc = kzalloc(sizeof(*newfunc), GFP_KERNEL);
 236					if (!newfunc) {
 237						err("out of system memory\n");
 238						return -ENOMEM;
 239					}
 240					newfunc->busno = cur_func->busno;
 241					newfunc->device = device;
 242					for (j = 0; j < 4; j++)
 243						newfunc->irq[j] = cur_func->irq[j];
 244					for (prev_func = cur_func; prev_func->next; prev_func = prev_func->next);
 245					prev_func->next = newfunc;
 246					cur_func = newfunc;
 247					break;
 248				case PCI_HEADER_TYPE_BRIDGE:
 249					class >>= 8;
 250					debug("class now is %x\n", class);
 251					if (class != PCI_CLASS_BRIDGE_PCI) {
 252						err("This %x is not PCI-to-PCI bridge, and as is not supported for hot-plugging.  Please insert another card.\n",
 253						     cur_func->device);
 254						return -ENODEV;
 255					}
 256
 257					assign_alt_irq(cur_func, class_code);
 258
 259					debug("cur_func->busno b4 configure_bridge is %x\n", cur_func->busno);
 260					rc = configure_bridge(&cur_func, slotno);
 261					if (rc == -ENODEV) {
 262						err("You chose to insert Single Bridge, or nested bridges, this is not supported...\n");
 263						err("Bus %x, devfunc %x\n", cur_func->busno, cur_func->device);
 264						return rc;
 265					}
 266					if (rc) {
 267						/* We need to do this in case some other BARs were properly inserted */
 268						func->bus = 1; /* To indicate to the unconfigure function that this is a PPB */
 269						err("was not able to hot-add PPB properly.\n");
 270						cleanup_count = 2;
 271						goto error;
 272					}
 273					debug("cur_func->busno = %x, device = %x, function = %x\n",
 274						cur_func->busno, device, function);
 275					pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_SECONDARY_BUS, &sec_number);
 276					debug("after configuring bridge..., sec_number = %x\n", sec_number);
 277					flag = 0;
 278					for (i = 0; i < 32; i++) {
 279						if (func->devices[i]) {
 280							debug("inside for loop, device is %x\n", i);
 281							newfunc = kzalloc(sizeof(*newfunc), GFP_KERNEL);
 282							if (!newfunc) {
 283								err(" out of system memory\n");
 284								return -ENOMEM;
 285							}
 286							newfunc->busno = sec_number;
 287							newfunc->device = (u8) i;
 288							for (j = 0; j < 4; j++)
 289								newfunc->irq[j] = cur_func->irq[j];
 290
 291							if (flag) {
 292								for (prev_func = cur_func; prev_func->next; prev_func = prev_func->next);
 293								prev_func->next = newfunc;
 294							} else
 295								cur_func->next = newfunc;
 296
 297							rc = ibmphp_configure_card(newfunc, slotno);
 298
 299							/* Again, this case should not happen... For complete paranoia, will need to call remove_bus */
 300							if (rc) {
 301								/* We need to do this in case some other BARs were properly inserted */
 302								func->bus = 1; /* To indicate to the unconfigure function that this is a PPB */
 303								cleanup_count = 2;
 304								goto error;
 305							}
 306							flag = 1;
 307						}
 308					}
 309
 310					function = 0x8;
 311					break;
 312				default:
 313					err("MAJOR PROBLEM!!!!, header type not supported? %x\n", hdr_type);
 314					return -ENXIO;
 315					break;
 316			}	/* end of switch */
 317		}	/* end of valid device */
 318	}	/* end of for */
 319
 320	if (!valid_device) {
 321		err("Cannot find any valid devices on the card.  Or unable to read from card.\n");
 322		return -ENODEV;
 323	}
 324
 325	return 0;
 326
 327error:
 328	for (i = 0; i < cleanup_count; i++) {
 329		if (cur_func->io[i]) {
 330			ibmphp_remove_resource(cur_func->io[i]);
 331			cur_func->io[i] = NULL;
 332		} else if (cur_func->pfmem[i]) {
 333			ibmphp_remove_resource(cur_func->pfmem[i]);
 334			cur_func->pfmem[i] = NULL;
 335		} else if (cur_func->mem[i]) {
 336			ibmphp_remove_resource(cur_func->mem[i]);
 337			cur_func->mem[i] = NULL;
 338		}
 339	}
 340	return rc;
 341}
 342
 343/*
 344 * This function configures the pci BARs of a single device.
 345 * Input: pointer to the pci_func
 346 * Output: configured PCI, 0, or error
 347 */
 348static int configure_device(struct pci_func *func)
 349{
 350	u32 bar[6];
 351	u32 address[] = {
 352		PCI_BASE_ADDRESS_0,
 353		PCI_BASE_ADDRESS_1,
 354		PCI_BASE_ADDRESS_2,
 355		PCI_BASE_ADDRESS_3,
 356		PCI_BASE_ADDRESS_4,
 357		PCI_BASE_ADDRESS_5,
 358		0
 359	};
 360	u8 irq;
 361	int count;
 362	int len[6];
 363	struct resource_node *io[6];
 364	struct resource_node *mem[6];
 365	struct resource_node *mem_tmp;
 366	struct resource_node *pfmem[6];
 367	unsigned int devfn;
 368
 369	debug("%s - inside\n", __func__);
 370
 371	devfn = PCI_DEVFN(func->device, func->function);
 372	ibmphp_pci_bus->number = func->busno;
 373
 374	for (count = 0; address[count]; count++) {	/* for 6 BARs */
 375
 376		/* not sure if i need this.  per scott, said maybe need * something like this
 377		   if devices don't adhere 100% to the spec, so don't want to write
 378		   to the reserved bits
 379
 380		pcibios_read_config_byte(cur_func->busno, cur_func->device,
 381		PCI_BASE_ADDRESS_0 + 4 * count, &tmp);
 382		if (tmp & 0x01) // IO
 383			pcibios_write_config_dword(cur_func->busno, cur_func->device,
 384			PCI_BASE_ADDRESS_0 + 4 * count, 0xFFFFFFFD);
 385		else  // Memory
 386			pcibios_write_config_dword(cur_func->busno, cur_func->device,
 387			PCI_BASE_ADDRESS_0 + 4 * count, 0xFFFFFFFF);
 388		 */
 389		pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], 0xFFFFFFFF);
 390		pci_bus_read_config_dword(ibmphp_pci_bus, devfn, address[count], &bar[count]);
 391
 392		if (!bar[count])	/* This BAR is not implemented */
 393			continue;
 394
 395		debug("Device %x BAR %d wants %x\n", func->device, count, bar[count]);
 396
 397		if (bar[count] & PCI_BASE_ADDRESS_SPACE_IO) {
 398			/* This is IO */
 399			debug("inside IO SPACE\n");
 400
 401			len[count] = bar[count] & 0xFFFFFFFC;
 402			len[count] = ~len[count] + 1;
 403
 404			debug("len[count] in IO %x, count %d\n", len[count], count);
 405
 406			io[count] = kzalloc(sizeof(struct resource_node), GFP_KERNEL);
 407
 408			if (!io[count]) {
 409				err("out of system memory\n");
 410				return -ENOMEM;
 411			}
 412			io[count]->type = IO;
 413			io[count]->busno = func->busno;
 414			io[count]->devfunc = PCI_DEVFN(func->device, func->function);
 415			io[count]->len = len[count];
 416			if (ibmphp_check_resource(io[count], 0) == 0) {
 417				ibmphp_add_resource(io[count]);
 418				func->io[count] = io[count];
 419			} else {
 420				err("cannot allocate requested io for bus %x device %x function %x len %x\n",
 421				     func->busno, func->device, func->function, len[count]);
 422				kfree(io[count]);
 423				return -EIO;
 424			}
 425			pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], func->io[count]->start);
 426
 427			/* _______________This is for debugging purposes only_____________________ */
 428			debug("b4 writing, the IO address is %x\n", func->io[count]->start);
 429			pci_bus_read_config_dword(ibmphp_pci_bus, devfn, address[count], &bar[count]);
 430			debug("after writing.... the start address is %x\n", bar[count]);
 431			/* _________________________________________________________________________*/
 432
 433		} else {
 434			/* This is Memory */
 435			if (bar[count] & PCI_BASE_ADDRESS_MEM_PREFETCH) {
 436				/* pfmem */
 437				debug("PFMEM SPACE\n");
 438
 439				len[count] = bar[count] & 0xFFFFFFF0;
 440				len[count] = ~len[count] + 1;
 441
 442				debug("len[count] in PFMEM %x, count %d\n", len[count], count);
 443
 444				pfmem[count] = kzalloc(sizeof(struct resource_node), GFP_KERNEL);
 445				if (!pfmem[count]) {
 446					err("out of system memory\n");
 447					return -ENOMEM;
 448				}
 449				pfmem[count]->type = PFMEM;
 450				pfmem[count]->busno = func->busno;
 451				pfmem[count]->devfunc = PCI_DEVFN(func->device,
 452							func->function);
 453				pfmem[count]->len = len[count];
 454				pfmem[count]->fromMem = 0;
 455				if (ibmphp_check_resource(pfmem[count], 0) == 0) {
 456					ibmphp_add_resource(pfmem[count]);
 457					func->pfmem[count] = pfmem[count];
 458				} else {
 459					mem_tmp = kzalloc(sizeof(*mem_tmp), GFP_KERNEL);
 460					if (!mem_tmp) {
 461						err("out of system memory\n");
 462						kfree(pfmem[count]);
 463						return -ENOMEM;
 464					}
 465					mem_tmp->type = MEM;
 466					mem_tmp->busno = pfmem[count]->busno;
 467					mem_tmp->devfunc = pfmem[count]->devfunc;
 468					mem_tmp->len = pfmem[count]->len;
 469					debug("there's no pfmem... going into mem.\n");
 470					if (ibmphp_check_resource(mem_tmp, 0) == 0) {
 471						ibmphp_add_resource(mem_tmp);
 472						pfmem[count]->fromMem = 1;
 473						pfmem[count]->rangeno = mem_tmp->rangeno;
 474						pfmem[count]->start = mem_tmp->start;
 475						pfmem[count]->end = mem_tmp->end;
 476						ibmphp_add_pfmem_from_mem(pfmem[count]);
 477						func->pfmem[count] = pfmem[count];
 478					} else {
 479						err("cannot allocate requested pfmem for bus %x, device %x, len %x\n",
 480						     func->busno, func->device, len[count]);
 481						kfree(mem_tmp);
 482						kfree(pfmem[count]);
 483						return -EIO;
 484					}
 485				}
 486
 487				pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], func->pfmem[count]->start);
 488
 489				/*_______________This is for debugging purposes only______________________________*/
 490				debug("b4 writing, start address is %x\n", func->pfmem[count]->start);
 491				pci_bus_read_config_dword(ibmphp_pci_bus, devfn, address[count], &bar[count]);
 492				debug("after writing, start address is %x\n", bar[count]);
 493				/*_________________________________________________________________________________*/
 494
 495				if (bar[count] & PCI_BASE_ADDRESS_MEM_TYPE_64) {	/* takes up another dword */
 496					debug("inside the mem 64 case, count %d\n", count);
 497					count += 1;
 498					/* on the 2nd dword, write all 0s, since we can't handle them n.e.ways */
 499					pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], 0x00000000);
 500				}
 501			} else {
 502				/* regular memory */
 503				debug("REGULAR MEM SPACE\n");
 504
 505				len[count] = bar[count] & 0xFFFFFFF0;
 506				len[count] = ~len[count] + 1;
 507
 508				debug("len[count] in Mem %x, count %d\n", len[count], count);
 509
 510				mem[count] = kzalloc(sizeof(struct resource_node), GFP_KERNEL);
 511				if (!mem[count]) {
 512					err("out of system memory\n");
 513					return -ENOMEM;
 514				}
 515				mem[count]->type = MEM;
 516				mem[count]->busno = func->busno;
 517				mem[count]->devfunc = PCI_DEVFN(func->device,
 518							func->function);
 519				mem[count]->len = len[count];
 520				if (ibmphp_check_resource(mem[count], 0) == 0) {
 521					ibmphp_add_resource(mem[count]);
 522					func->mem[count] = mem[count];
 523				} else {
 524					err("cannot allocate requested mem for bus %x, device %x, len %x\n",
 525					     func->busno, func->device, len[count]);
 526					kfree(mem[count]);
 527					return -EIO;
 528				}
 529				pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], func->mem[count]->start);
 530				/* _______________________This is for debugging purposes only _______________________*/
 531				debug("b4 writing, start address is %x\n", func->mem[count]->start);
 532				pci_bus_read_config_dword(ibmphp_pci_bus, devfn, address[count], &bar[count]);
 533				debug("after writing, the address is %x\n", bar[count]);
 534				/* __________________________________________________________________________________*/
 535
 536				if (bar[count] & PCI_BASE_ADDRESS_MEM_TYPE_64) {
 537					/* takes up another dword */
 538					debug("inside mem 64 case, reg. mem, count %d\n", count);
 539					count += 1;
 540					/* on the 2nd dword, write all 0s, since we can't handle them n.e.ways */
 541					pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], 0x00000000);
 542				}
 543			}
 544		}		/* end of mem */
 545	}			/* end of for */
 546
 547	func->bus = 0;		/* To indicate that this is not a PPB */
 548	pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_INTERRUPT_PIN, &irq);
 549	if ((irq > 0x00) && (irq < 0x05))
 550		pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_INTERRUPT_LINE, func->irq[irq - 1]);
 551
 552	pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_CACHE_LINE_SIZE, CACHE);
 553	pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_LATENCY_TIMER, LATENCY);
 554
 555	pci_bus_write_config_dword(ibmphp_pci_bus, devfn, PCI_ROM_ADDRESS, 0x00L);
 556	pci_bus_write_config_word(ibmphp_pci_bus, devfn, PCI_COMMAND, DEVICEENABLE);
 557
 558	return 0;
 559}
 560
 561/******************************************************************************
 562 * This routine configures a PCI-2-PCI bridge and the functions behind it
 563 * Parameters: pci_func
 564 * Returns:
 565 ******************************************************************************/
 566static int configure_bridge(struct pci_func **func_passed, u8 slotno)
 567{
 568	int count;
 569	int i;
 570	int rc;
 571	u8 sec_number;
 572	u8 io_base;
 573	u16 pfmem_base;
 574	u32 bar[2];
 575	u32 len[2];
 576	u8 flag_io = 0;
 577	u8 flag_mem = 0;
 578	u8 flag_pfmem = 0;
 579	u8 need_io_upper = 0;
 580	u8 need_pfmem_upper = 0;
 581	struct res_needed *amount_needed = NULL;
 582	struct resource_node *io = NULL;
 583	struct resource_node *bus_io[2] = {NULL, NULL};
 584	struct resource_node *mem = NULL;
 585	struct resource_node *bus_mem[2] = {NULL, NULL};
 586	struct resource_node *mem_tmp = NULL;
 587	struct resource_node *pfmem = NULL;
 588	struct resource_node *bus_pfmem[2] = {NULL, NULL};
 589	struct bus_node *bus;
 590	u32 address[] = {
 591		PCI_BASE_ADDRESS_0,
 592		PCI_BASE_ADDRESS_1,
 593		0
 594	};
 595	struct pci_func *func = *func_passed;
 596	unsigned int devfn;
 597	u8 irq;
 598	int retval;
 599
 600	debug("%s - enter\n", __func__);
 601
 602	devfn = PCI_DEVFN(func->function, func->device);
 603	ibmphp_pci_bus->number = func->busno;
 604
 605	/* Configuring necessary info for the bridge so that we could see the devices
 606	 * behind it
 607	 */
 608
 609	pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_PRIMARY_BUS, func->busno);
 610
 611	/* _____________________For debugging purposes only __________________________
 612	pci_bus_config_byte(ibmphp_pci_bus, devfn, PCI_PRIMARY_BUS, &pri_number);
 613	debug("primary # written into the bridge is %x\n", pri_number);
 614	 ___________________________________________________________________________*/
 615
 616	/* in EBDA, only get allocated 1 additional bus # per slot */
 617	sec_number = find_sec_number(func->busno, slotno);
 618	if (sec_number == 0xff) {
 619		err("cannot allocate secondary bus number for the bridged device\n");
 620		return -EINVAL;
 621	}
 622
 623	debug("after find_sec_number, the number we got is %x\n", sec_number);
 624	debug("AFTER FIND_SEC_NUMBER, func->busno IS %x\n", func->busno);
 625
 626	pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_SECONDARY_BUS, sec_number);
 627
 628	/* __________________For debugging purposes only __________________________________
 629	pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_SECONDARY_BUS, &sec_number);
 630	debug("sec_number after write/read is %x\n", sec_number);
 631	 ________________________________________________________________________________*/
 632
 633	pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_SUBORDINATE_BUS, sec_number);
 634
 635	/* __________________For debugging purposes only ____________________________________
 636	pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_SUBORDINATE_BUS, &sec_number);
 637	debug("subordinate number after write/read is %x\n", sec_number);
 638	 __________________________________________________________________________________*/
 639
 640	pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_CACHE_LINE_SIZE, CACHE);
 641	pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_LATENCY_TIMER, LATENCY);
 642	pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_SEC_LATENCY_TIMER, LATENCY);
 643
 644	debug("func->busno is %x\n", func->busno);
 645	debug("sec_number after writing is %x\n", sec_number);
 646
 647
 648	/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
 649	   !!!!!!!!!!!!!!!NEED TO ADD!!!  FAST BACK-TO-BACK ENABLE!!!!!!!!!!!!!!!!!!!!
 650	   !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
 651
 652
 653	/* First we need to allocate mem/io for the bridge itself in case it needs it */
 654	for (count = 0; address[count]; count++) {	/* for 2 BARs */
 655		pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], 0xFFFFFFFF);
 656		pci_bus_read_config_dword(ibmphp_pci_bus, devfn, address[count], &bar[count]);
 657
 658		if (!bar[count]) {
 659			/* This BAR is not implemented */
 660			debug("so we come here then, eh?, count = %d\n", count);
 661			continue;
 662		}
 663		//  tmp_bar = bar[count];
 664
 665		debug("Bar %d wants %x\n", count, bar[count]);
 666
 667		if (bar[count] & PCI_BASE_ADDRESS_SPACE_IO) {
 668			/* This is IO */
 669			len[count] = bar[count] & 0xFFFFFFFC;
 670			len[count] = ~len[count] + 1;
 671
 672			debug("len[count] in IO = %x\n", len[count]);
 673
 674			bus_io[count] = kzalloc(sizeof(struct resource_node), GFP_KERNEL);
 675
 676			if (!bus_io[count]) {
 677				err("out of system memory\n");
 678				retval = -ENOMEM;
 679				goto error;
 680			}
 681			bus_io[count]->type = IO;
 682			bus_io[count]->busno = func->busno;
 683			bus_io[count]->devfunc = PCI_DEVFN(func->device,
 684							func->function);
 685			bus_io[count]->len = len[count];
 686			if (ibmphp_check_resource(bus_io[count], 0) == 0) {
 687				ibmphp_add_resource(bus_io[count]);
 688				func->io[count] = bus_io[count];
 689			} else {
 690				err("cannot allocate requested io for bus %x, device %x, len %x\n",
 691				     func->busno, func->device, len[count]);
 692				kfree(bus_io[count]);
 693				return -EIO;
 694			}
 695
 696			pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], func->io[count]->start);
 697
 698		} else {
 699			/* This is Memory */
 700			if (bar[count] & PCI_BASE_ADDRESS_MEM_PREFETCH) {
 701				/* pfmem */
 702				len[count] = bar[count] & 0xFFFFFFF0;
 703				len[count] = ~len[count] + 1;
 704
 705				debug("len[count] in PFMEM = %x\n", len[count]);
 706
 707				bus_pfmem[count] = kzalloc(sizeof(struct resource_node), GFP_KERNEL);
 708				if (!bus_pfmem[count]) {
 709					err("out of system memory\n");
 710					retval = -ENOMEM;
 711					goto error;
 712				}
 713				bus_pfmem[count]->type = PFMEM;
 714				bus_pfmem[count]->busno = func->busno;
 715				bus_pfmem[count]->devfunc = PCI_DEVFN(func->device,
 716							func->function);
 717				bus_pfmem[count]->len = len[count];
 718				bus_pfmem[count]->fromMem = 0;
 719				if (ibmphp_check_resource(bus_pfmem[count], 0) == 0) {
 720					ibmphp_add_resource(bus_pfmem[count]);
 721					func->pfmem[count] = bus_pfmem[count];
 722				} else {
 723					mem_tmp = kzalloc(sizeof(*mem_tmp), GFP_KERNEL);
 724					if (!mem_tmp) {
 725						err("out of system memory\n");
 726						retval = -ENOMEM;
 727						goto error;
 728					}
 729					mem_tmp->type = MEM;
 730					mem_tmp->busno = bus_pfmem[count]->busno;
 731					mem_tmp->devfunc = bus_pfmem[count]->devfunc;
 732					mem_tmp->len = bus_pfmem[count]->len;
 733					if (ibmphp_check_resource(mem_tmp, 0) == 0) {
 734						ibmphp_add_resource(mem_tmp);
 735						bus_pfmem[count]->fromMem = 1;
 736						bus_pfmem[count]->rangeno = mem_tmp->rangeno;
 737						ibmphp_add_pfmem_from_mem(bus_pfmem[count]);
 738						func->pfmem[count] = bus_pfmem[count];
 739					} else {
 740						err("cannot allocate requested pfmem for bus %x, device %x, len %x\n",
 741						     func->busno, func->device, len[count]);
 742						kfree(mem_tmp);
 743						kfree(bus_pfmem[count]);
 744						return -EIO;
 745					}
 746				}
 747
 748				pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], func->pfmem[count]->start);
 749
 750				if (bar[count] & PCI_BASE_ADDRESS_MEM_TYPE_64) {
 751					/* takes up another dword */
 752					count += 1;
 753					/* on the 2nd dword, write all 0s, since we can't handle them n.e.ways */
 754					pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], 0x00000000);
 755
 756				}
 757			} else {
 758				/* regular memory */
 759				len[count] = bar[count] & 0xFFFFFFF0;
 760				len[count] = ~len[count] + 1;
 761
 762				debug("len[count] in Memory is %x\n", len[count]);
 763
 764				bus_mem[count] = kzalloc(sizeof(struct resource_node), GFP_KERNEL);
 765				if (!bus_mem[count]) {
 766					err("out of system memory\n");
 767					retval = -ENOMEM;
 768					goto error;
 769				}
 770				bus_mem[count]->type = MEM;
 771				bus_mem[count]->busno = func->busno;
 772				bus_mem[count]->devfunc = PCI_DEVFN(func->device,
 773							func->function);
 774				bus_mem[count]->len = len[count];
 775				if (ibmphp_check_resource(bus_mem[count], 0) == 0) {
 776					ibmphp_add_resource(bus_mem[count]);
 777					func->mem[count] = bus_mem[count];
 778				} else {
 779					err("cannot allocate requested mem for bus %x, device %x, len %x\n",
 780					     func->busno, func->device, len[count]);
 781					kfree(bus_mem[count]);
 782					return -EIO;
 783				}
 784
 785				pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], func->mem[count]->start);
 786
 787				if (bar[count] & PCI_BASE_ADDRESS_MEM_TYPE_64) {
 788					/* takes up another dword */
 789					count += 1;
 790					/* on the 2nd dword, write all 0s, since we can't handle them n.e.ways */
 791					pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], 0x00000000);
 792
 793				}
 794			}
 795		}		/* end of mem */
 796	}			/* end of for  */
 797
 798	/* Now need to see how much space the devices behind the bridge needed */
 799	amount_needed = scan_behind_bridge(func, sec_number);
 800	if (amount_needed == NULL)
 801		return -ENOMEM;
 802
 803	ibmphp_pci_bus->number = func->busno;
 804	debug("after coming back from scan_behind_bridge\n");
 805	debug("amount_needed->not_correct = %x\n", amount_needed->not_correct);
 806	debug("amount_needed->io = %x\n", amount_needed->io);
 807	debug("amount_needed->mem = %x\n", amount_needed->mem);
 808	debug("amount_needed->pfmem =  %x\n", amount_needed->pfmem);
 809
 810	if (amount_needed->not_correct) {
 811		debug("amount_needed is not correct\n");
 812		for (count = 0; address[count]; count++) {
 813			/* for 2 BARs */
 814			if (bus_io[count]) {
 815				ibmphp_remove_resource(bus_io[count]);
 816				func->io[count] = NULL;
 817			} else if (bus_pfmem[count]) {
 818				ibmphp_remove_resource(bus_pfmem[count]);
 819				func->pfmem[count] = NULL;
 820			} else if (bus_mem[count]) {
 821				ibmphp_remove_resource(bus_mem[count]);
 822				func->mem[count] = NULL;
 823			}
 824		}
 825		kfree(amount_needed);
 826		return -ENODEV;
 827	}
 828
 829	if (!amount_needed->io) {
 830		debug("it doesn't want IO?\n");
 831		flag_io = 1;
 832	} else {
 833		debug("it wants %x IO behind the bridge\n", amount_needed->io);
 834		io = kzalloc(sizeof(*io), GFP_KERNEL);
 835
 836		if (!io) {
 837			err("out of system memory\n");
 838			retval = -ENOMEM;
 839			goto error;
 840		}
 841		io->type = IO;
 842		io->busno = func->busno;
 843		io->devfunc = PCI_DEVFN(func->device, func->function);
 844		io->len = amount_needed->io;
 845		if (ibmphp_check_resource(io, 1) == 0) {
 846			debug("were we able to add io\n");
 847			ibmphp_add_resource(io);
 848			flag_io = 1;
 849		}
 850	}
 851
 852	if (!amount_needed->mem) {
 853		debug("it doesn't want n.e.memory?\n");
 854		flag_mem = 1;
 855	} else {
 856		debug("it wants %x memory behind the bridge\n", amount_needed->mem);
 857		mem = kzalloc(sizeof(*mem), GFP_KERNEL);
 858		if (!mem) {
 859			err("out of system memory\n");
 860			retval = -ENOMEM;
 861			goto error;
 862		}
 863		mem->type = MEM;
 864		mem->busno = func->busno;
 865		mem->devfunc = PCI_DEVFN(func->device, func->function);
 866		mem->len = amount_needed->mem;
 867		if (ibmphp_check_resource(mem, 1) == 0) {
 868			ibmphp_add_resource(mem);
 869			flag_mem = 1;
 870			debug("were we able to add mem\n");
 871		}
 872	}
 873
 874	if (!amount_needed->pfmem) {
 875		debug("it doesn't want n.e.pfmem mem?\n");
 876		flag_pfmem = 1;
 877	} else {
 878		debug("it wants %x pfmemory behind the bridge\n", amount_needed->pfmem);
 879		pfmem = kzalloc(sizeof(*pfmem), GFP_KERNEL);
 880		if (!pfmem) {
 881			err("out of system memory\n");
 882			retval = -ENOMEM;
 883			goto error;
 884		}
 885		pfmem->type = PFMEM;
 886		pfmem->busno = func->busno;
 887		pfmem->devfunc = PCI_DEVFN(func->device, func->function);
 888		pfmem->len = amount_needed->pfmem;
 889		pfmem->fromMem = 0;
 890		if (ibmphp_check_resource(pfmem, 1) == 0) {
 891			ibmphp_add_resource(pfmem);
 892			flag_pfmem = 1;
 893		} else {
 894			mem_tmp = kzalloc(sizeof(*mem_tmp), GFP_KERNEL);
 895			if (!mem_tmp) {
 896				err("out of system memory\n");
 897				retval = -ENOMEM;
 898				goto error;
 899			}
 900			mem_tmp->type = MEM;
 901			mem_tmp->busno = pfmem->busno;
 902			mem_tmp->devfunc = pfmem->devfunc;
 903			mem_tmp->len = pfmem->len;
 904			if (ibmphp_check_resource(mem_tmp, 1) == 0) {
 905				ibmphp_add_resource(mem_tmp);
 906				pfmem->fromMem = 1;
 907				pfmem->rangeno = mem_tmp->rangeno;
 908				ibmphp_add_pfmem_from_mem(pfmem);
 909				flag_pfmem = 1;
 910			}
 911		}
 912	}
 913
 914	debug("b4 if (flag_io && flag_mem && flag_pfmem)\n");
 915	debug("flag_io = %x, flag_mem = %x, flag_pfmem = %x\n", flag_io, flag_mem, flag_pfmem);
 916
 917	if (flag_io && flag_mem && flag_pfmem) {
 918		/* If on bootup, there was a bridged card in this slot,
 919		 * then card was removed and ibmphp got unloaded and loaded
 920		 * back again, there's no way for us to remove the bus
 921		 * struct, so no need to kmalloc, can use existing node
 922		 */
 923		bus = ibmphp_find_res_bus(sec_number);
 924		if (!bus) {
 925			bus = kzalloc(sizeof(*bus), GFP_KERNEL);
 926			if (!bus) {
 927				err("out of system memory\n");
 928				retval = -ENOMEM;
 929				goto error;
 930			}
 931			bus->busno = sec_number;
 932			debug("b4 adding new bus\n");
 933			rc = add_new_bus(bus, io, mem, pfmem, func->busno);
 934		} else if (!(bus->rangeIO) && !(bus->rangeMem) && !(bus->rangePFMem))
 935			rc = add_new_bus(bus, io, mem, pfmem, 0xFF);
 936		else {
 937			err("expected bus structure not empty?\n");
 938			retval = -EIO;
 939			goto error;
 940		}
 941		if (rc) {
 942			if (rc == -ENOMEM) {
 943				ibmphp_remove_bus(bus, func->busno);
 944				kfree(amount_needed);
 945				return rc;
 946			}
 947			retval = rc;
 948			goto error;
 949		}
 950		pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_IO_BASE, &io_base);
 951		pci_bus_read_config_word(ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_BASE, &pfmem_base);
 952
 953		if ((io_base & PCI_IO_RANGE_TYPE_MASK) == PCI_IO_RANGE_TYPE_32) {
 954			debug("io 32\n");
 955			need_io_upper = 1;
 956		}
 957		if ((pfmem_base & PCI_PREF_RANGE_TYPE_MASK) == PCI_PREF_RANGE_TYPE_64) {
 958			debug("pfmem 64\n");
 959			need_pfmem_upper = 1;
 960		}
 961
 962		if (bus->noIORanges) {
 963			pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_IO_BASE, 0x00 | bus->rangeIO->start >> 8);
 964			pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_IO_LIMIT, 0x00 | bus->rangeIO->end >> 8);
 965
 966			/* _______________This is for debugging purposes only ____________________
 967			pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_IO_BASE, &temp);
 968			debug("io_base = %x\n", (temp & PCI_IO_RANGE_TYPE_MASK) << 8);
 969			pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_IO_LIMIT, &temp);
 970			debug("io_limit = %x\n", (temp & PCI_IO_RANGE_TYPE_MASK) << 8);
 971			 ________________________________________________________________________*/
 972
 973			if (need_io_upper) {	/* since can't support n.e.ways */
 974				pci_bus_write_config_word(ibmphp_pci_bus, devfn, PCI_IO_BASE_UPPER16, 0x0000);
 975				pci_bus_write_config_word(ibmphp_pci_bus, devfn, PCI_IO_LIMIT_UPPER16, 0x0000);
 976			}
 977		} else {
 978			pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_IO_BASE, 0x00);
 979			pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_IO_LIMIT, 0x00);
 980		}
 981
 982		if (bus->noMemRanges) {
 983			pci_bus_write_config_word(ibmphp_pci_bus, devfn, PCI_MEMORY_BASE, 0x0000 | bus->rangeMem->start >> 16);
 984			pci_bus_write_config_word(ibmphp_pci_bus, devfn, PCI_MEMORY_LIMIT, 0x0000 | bus->rangeMem->end >> 16);
 985
 986			/* ____________________This is for debugging purposes only ________________________
 987			pci_bus_read_config_word(ibmphp_pci_bus, devfn, PCI_MEMORY_BASE, &temp);
 988			debug("mem_base = %x\n", (temp & PCI_MEMORY_RANGE_TYPE_MASK) << 16);
 989			pci_bus_read_config_word(ibmphp_pci_bus, devfn, PCI_MEMORY_LIMIT, &temp);
 990			debug("mem_limit = %x\n", (temp & PCI_MEMORY_RANGE_TYPE_MASK) << 16);
 991			 __________________________________________________________________________________*/
 992
 993		} else {
 994			pci_bus_write_config_word(ibmphp_pci_bus, devfn, PCI_MEMORY_BASE, 0xffff);
 995			pci_bus_write_config_word(ibmphp_pci_bus, devfn, PCI_MEMORY_LIMIT, 0x0000);
 996		}
 997		if (bus->noPFMemRanges) {
 998			pci_bus_write_config_word(ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_BASE, 0x0000 | bus->rangePFMem->start >> 16);
 999			pci_bus_write_config_word(ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, 0x0000 | bus->rangePFMem->end >> 16);
1000
1001			/* __________________________This is for debugging purposes only _______________________
1002			pci_bus_read_config_word(ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_BASE, &temp);
1003			debug("pfmem_base = %x", (temp & PCI_MEMORY_RANGE_TYPE_MASK) << 16);
1004			pci_bus_read_config_word(ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, &temp);
1005			debug("pfmem_limit = %x\n", (temp & PCI_MEMORY_RANGE_TYPE_MASK) << 16);
1006			 ______________________________________________________________________________________*/
1007
1008			if (need_pfmem_upper) {	/* since can't support n.e.ways */
1009				pci_bus_write_config_dword(ibmphp_pci_bus, devfn, PCI_PREF_BASE_UPPER32, 0x00000000);
1010				pci_bus_write_config_dword(ibmphp_pci_bus, devfn, PCI_PREF_LIMIT_UPPER32, 0x00000000);
1011			}
1012		} else {
1013			pci_bus_write_config_word(ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_BASE, 0xffff);
1014			pci_bus_write_config_word(ibmphp_pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, 0x0000);
1015		}
1016
1017		debug("b4 writing control information\n");
1018
1019		pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_INTERRUPT_PIN, &irq);
1020		if ((irq > 0x00) && (irq < 0x05))
1021			pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_INTERRUPT_LINE, func->irq[irq - 1]);
1022		/*
1023		pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_BRIDGE_CONTROL, ctrl);
1024		pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_BRIDGE_CONTROL, PCI_BRIDGE_CTL_PARITY);
1025		pci_bus_write_config_byte(ibmphp_pci_bus, devfn, PCI_BRIDGE_CONTROL, PCI_BRIDGE_CTL_SERR);
1026		 */
1027
1028		pci_bus_write_config_word(ibmphp_pci_bus, devfn, PCI_COMMAND, DEVICEENABLE);
1029		pci_bus_write_config_word(ibmphp_pci_bus, devfn, PCI_BRIDGE_CONTROL, 0x07);
1030		for (i = 0; i < 32; i++) {
1031			if (amount_needed->devices[i]) {
1032				debug("device where devices[i] is 1 = %x\n", i);
1033				func->devices[i] = 1;
1034			}
1035		}
1036		func->bus = 1;	/* For unconfiguring, to indicate it's PPB */
1037		func_passed = &func;
1038		debug("func->busno b4 returning is %x\n", func->busno);
1039		debug("func->busno b4 returning in the other structure is %x\n", (*func_passed)->busno);
1040		kfree(amount_needed);
1041		return 0;
1042	} else {
1043		err("Configuring bridge was unsuccessful...\n");
1044		mem_tmp = NULL;
1045		retval = -EIO;
1046		goto error;
1047	}
1048
1049error:
1050	kfree(amount_needed);
1051	if (pfmem)
1052		ibmphp_remove_resource(pfmem);
1053	if (io)
1054		ibmphp_remove_resource(io);
1055	if (mem)
1056		ibmphp_remove_resource(mem);
1057	for (i = 0; i < 2; i++) {	/* for 2 BARs */
1058		if (bus_io[i]) {
1059			ibmphp_remove_resource(bus_io[i]);
1060			func->io[i] = NULL;
1061		} else if (bus_pfmem[i]) {
1062			ibmphp_remove_resource(bus_pfmem[i]);
1063			func->pfmem[i] = NULL;
1064		} else if (bus_mem[i]) {
1065			ibmphp_remove_resource(bus_mem[i]);
1066			func->mem[i] = NULL;
1067		}
1068	}
1069	return retval;
1070}
1071
1072/*****************************************************************************
1073 * This function adds up the amount of resources needed behind the PPB bridge
1074 * and passes it to the configure_bridge function
1075 * Input: bridge function
1076 * Output: amount of resources needed
1077 *****************************************************************************/
1078static struct res_needed *scan_behind_bridge(struct pci_func *func, u8 busno)
1079{
1080	int count, len[6];
1081	u16 vendor_id;
1082	u8 hdr_type;
1083	u8 device, function;
1084	unsigned int devfn;
1085	int howmany = 0;	/*this is to see if there are any devices behind the bridge */
1086
1087	u32 bar[6], class;
1088	u32 address[] = {
1089		PCI_BASE_ADDRESS_0,
1090		PCI_BASE_ADDRESS_1,
1091		PCI_BASE_ADDRESS_2,
1092		PCI_BASE_ADDRESS_3,
1093		PCI_BASE_ADDRESS_4,
1094		PCI_BASE_ADDRESS_5,
1095		0
1096	};
1097	struct res_needed *amount;
1098
1099	amount = kzalloc(sizeof(*amount), GFP_KERNEL);
1100	if (amount == NULL)
1101		return NULL;
1102
1103	ibmphp_pci_bus->number = busno;
1104
1105	debug("the bus_no behind the bridge is %x\n", busno);
1106	debug("scanning devices behind the bridge...\n");
1107	for (device = 0; device < 32; device++) {
1108		amount->devices[device] = 0;
1109		for (function = 0; function < 8; function++) {
1110			devfn = PCI_DEVFN(device, function);
1111
1112			pci_bus_read_config_word(ibmphp_pci_bus, devfn, PCI_VENDOR_ID, &vendor_id);
1113
1114			if (vendor_id != PCI_VENDOR_ID_NOTVALID) {
1115				/* found correct device!!! */
1116				howmany++;
1117
1118				pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_HEADER_TYPE, &hdr_type);
1119				pci_bus_read_config_dword(ibmphp_pci_bus, devfn, PCI_CLASS_REVISION, &class);
1120
1121				debug("hdr_type behind the bridge is %x\n", hdr_type);
1122				if ((hdr_type & 0x7f) == PCI_HEADER_TYPE_BRIDGE) {
1123					err("embedded bridges not supported for hot-plugging.\n");
1124					amount->not_correct = 1;
1125					return amount;
1126				}
1127
1128				class >>= 8;	/* to take revision out, class = class.subclass.prog i/f */
1129				if (class == PCI_CLASS_NOT_DEFINED_VGA) {
1130					err("The device %x is VGA compatible and as is not supported for hot plugging.  Please choose another device.\n", device);
1131					amount->not_correct = 1;
1132					return amount;
1133				} else if (class == PCI_CLASS_DISPLAY_VGA) {
1134					err("The device %x is not supported for hot plugging.  Please choose another device.\n", device);
1135					amount->not_correct = 1;
1136					return amount;
1137				}
1138
1139				amount->devices[device] = 1;
1140
1141				for (count = 0; address[count]; count++) {
1142					/* for 6 BARs */
1143					/*
1144					pci_bus_read_config_byte(ibmphp_pci_bus, devfn, address[count], &tmp);
1145					if (tmp & 0x01) // IO
1146						pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], 0xFFFFFFFD);
1147					else // MEMORY
1148						pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], 0xFFFFFFFF);
1149					*/
1150					pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], 0xFFFFFFFF);
1151					pci_bus_read_config_dword(ibmphp_pci_bus, devfn, address[count], &bar[count]);
1152
1153					debug("what is bar[count]? %x, count = %d\n", bar[count], count);
1154
1155					if (!bar[count])	/* This BAR is not implemented */
1156						continue;
1157
1158					//tmp_bar = bar[count];
1159
1160					debug("count %d device %x function %x wants %x resources\n", count, device, function, bar[count]);
1161
1162					if (bar[count] & PCI_BASE_ADDRESS_SPACE_IO) {
1163						/* This is IO */
1164						len[count] = bar[count] & 0xFFFFFFFC;
1165						len[count] = ~len[count] + 1;
1166						amount->io += len[count];
1167					} else {
1168						/* This is Memory */
1169						if (bar[count] & PCI_BASE_ADDRESS_MEM_PREFETCH) {
1170							/* pfmem */
1171							len[count] = bar[count] & 0xFFFFFFF0;
1172							len[count] = ~len[count] + 1;
1173							amount->pfmem += len[count];
1174							if (bar[count] & PCI_BASE_ADDRESS_MEM_TYPE_64)
1175								/* takes up another dword */
1176								count += 1;
1177
1178						} else {
1179							/* regular memory */
1180							len[count] = bar[count] & 0xFFFFFFF0;
1181							len[count] = ~len[count] + 1;
1182							amount->mem += len[count];
1183							if (bar[count] & PCI_BASE_ADDRESS_MEM_TYPE_64) {
1184								/* takes up another dword */
1185								count += 1;
1186							}
1187						}
1188					}
1189				}	/* end for */
1190			}	/* end if (valid) */
1191		}	/* end for */
1192	}	/* end for */
1193
1194	if (!howmany)
1195		amount->not_correct = 1;
1196	else
1197		amount->not_correct = 0;
1198	if ((amount->io) && (amount->io < IOBRIDGE))
1199		amount->io = IOBRIDGE;
1200	if ((amount->mem) && (amount->mem < MEMBRIDGE))
1201		amount->mem = MEMBRIDGE;
1202	if ((amount->pfmem) && (amount->pfmem < MEMBRIDGE))
1203		amount->pfmem = MEMBRIDGE;
1204	return amount;
1205}
1206
1207/* The following 3 unconfigure_boot_ routines deal with the case when we had the card
1208 * upon bootup in the system, since we don't allocate func to such case, we need to read
1209 * the start addresses from pci config space and then find the corresponding entries in
1210 * our resource lists.  The functions return either 0, -ENODEV, or -1 (general failure)
1211 * Change: we also call these functions even if we configured the card ourselves (i.e., not
1212 * the bootup case), since it should work same way
1213 */
1214static int unconfigure_boot_device(u8 busno, u8 device, u8 function)
1215{
1216	u32 start_address;
1217	u32 address[] = {
1218		PCI_BASE_ADDRESS_0,
1219		PCI_BASE_ADDRESS_1,
1220		PCI_BASE_ADDRESS_2,
1221		PCI_BASE_ADDRESS_3,
1222		PCI_BASE_ADDRESS_4,
1223		PCI_BASE_ADDRESS_5,
1224		0
1225	};
1226	int count;
1227	struct resource_node *io;
1228	struct resource_node *mem;
1229	struct resource_node *pfmem;
1230	struct bus_node *bus;
1231	u32 end_address;
1232	u32 temp_end;
1233	u32 size;
1234	u32 tmp_address;
1235	unsigned int devfn;
1236
1237	debug("%s - enter\n", __func__);
1238
1239	bus = ibmphp_find_res_bus(busno);
1240	if (!bus) {
1241		debug("cannot find corresponding bus.\n");
1242		return -EINVAL;
1243	}
1244
1245	devfn = PCI_DEVFN(device, function);
1246	ibmphp_pci_bus->number = busno;
1247	for (count = 0; address[count]; count++) {	/* for 6 BARs */
1248		pci_bus_read_config_dword(ibmphp_pci_bus, devfn, address[count], &start_address);
1249
1250		/* We can do this here, b/c by that time the device driver of the card has been stopped */
1251
1252		pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], 0xFFFFFFFF);
1253		pci_bus_read_config_dword(ibmphp_pci_bus, devfn, address[count], &size);
1254		pci_bus_write_config_dword(ibmphp_pci_bus, devfn, address[count], start_address);
1255
1256		debug("start_address is %x\n", start_address);
1257		debug("busno, device, function %x %x %x\n", busno, device, function);
1258		if (!size) {
1259			/* This BAR is not implemented */
1260			debug("is this bar no implemented?, count = %d\n", count);
1261			continue;
1262		}
1263		tmp_address = start_address;
1264		if (start_address & PCI_BASE_ADDRESS_SPACE_IO) {
1265			/* This is IO */
1266			start_address &= PCI_BASE_ADDRESS_IO_MASK;
1267			size = size & 0xFFFFFFFC;
1268			size = ~size + 1;
1269			end_address = start_address + size - 1;
1270			if (ibmphp_find_resource(bus, start_address, &io, IO) < 0) {
1271				err("cannot find corresponding IO resource to remove\n");
1272				return -EIO;
1273			}
1274			debug("io->start = %x\n", io->start);
1275			temp_end = io->end;
1276			start_address = io->end + 1;
1277			ibmphp_remove_resource(io);
1278			/* This is needed b/c of the old I/O restrictions in the BIOS */
1279			while (temp_end < end_address) {
1280				if (ibmphp_find_resource(bus, start_address, &io, IO) < 0) {
1281					err("cannot find corresponding IO resource to remove\n");
1282					return -EIO;
1283				}
1284				debug("io->start = %x\n", io->start);
1285				temp_end = io->end;
1286				start_address = io->end + 1;
1287				ibmphp_remove_resource(io);
1288			}
1289
1290			/* ????????? DO WE NEED TO WRITE ANYTHING INTO THE PCI CONFIG SPACE BACK ?????????? */
1291		} else {
1292			/* This is Memory */
1293			if (start_address & PCI_BASE_ADDRESS_MEM_PREFETCH) {
1294				/* pfmem */
1295				debug("start address of pfmem is %x\n", start_address);
1296				start_address &= PCI_BASE_ADDRESS_MEM_MASK;
1297
1298				if (ibmphp_find_resource(bus, start_address, &pfmem, PFMEM) < 0) {
1299					err("cannot find corresponding PFMEM resource to remove\n");
1300					return -EIO;
1301				}
1302				if (pfmem) {
1303					debug("pfmem->start = %x\n", pfmem->start);
1304
1305					ibmphp_remove_resource(pfmem);
1306				}
1307			} else {
1308				/* regular memory */
1309				debug("start address of mem is %x\n", start_address);
1310				start_address &= PCI_BASE_ADDRESS_MEM_MASK;
1311
1312				if (ibmphp_find_resource(bus, start_address, &mem, MEM) < 0) {
1313					err("cannot find corresponding MEM resource to remove\n");
1314					return -EIO;
1315				}
1316				if (mem) {
1317					debug("mem->start = %x\n", mem->start);
1318
1319					ibmphp_remove_resource(mem);
1320				}
1321			}
1322			if (tmp_address & PCI_BASE_ADDRESS_MEM_TYPE_64) {
1323				/* takes up another dword */
1324				count += 1;
1325			}
1326		}	/* end of mem */
1327	}	/* end of for */
1328
1329	return 0;
 
 
 
 
1330}
1331
1332static int unconfigure_boot_bridge(u8 busno, u8 device, u8 function)
1333{
1334	int count;
1335	int bus_no, pri_no, sub_no, sec_no = 0;
1336	u32 start_address, tmp_address;
1337	u8 sec_number, sub_number, pri_number;
1338	struct resource_node *io = NULL;
1339	struct resource_node *mem = NULL;
1340	struct resource_node *pfmem = NULL;
1341	struct bus_node *bus;
1342	u32 address[] = {
1343		PCI_BASE_ADDRESS_0,
1344		PCI_BASE_ADDRESS_1,
1345		0
1346	};
1347	unsigned int devfn;
1348
1349	devfn = PCI_DEVFN(device, function);
1350	ibmphp_pci_bus->number = busno;
1351	bus_no = (int) busno;
1352	debug("busno is %x\n", busno);
1353	pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_PRIMARY_BUS, &pri_number);
1354	debug("%s - busno = %x, primary_number = %x\n", __func__, busno, pri_number);
1355
1356	pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_SECONDARY_BUS, &sec_number);
1357	debug("sec_number is %x\n", sec_number);
1358	sec_no = (int) sec_number;
1359	pri_no = (int) pri_number;
1360	if (pri_no != bus_no) {
1361		err("primary numbers in our structures and pci config space don't match.\n");
1362		return -EINVAL;
1363	}
1364
1365	pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_SUBORDINATE_BUS, &sub_number);
1366	sub_no = (int) sub_number;
1367	debug("sub_no is %d, sec_no is %d\n", sub_no, sec_no);
1368	if (sec_no != sub_number) {
1369		err("there're more buses behind this bridge.  Hot removal is not supported.  Please choose another card\n");
1370		return -ENODEV;
1371	}
1372
1373	bus = ibmphp_find_res_bus(sec_number);
1374	if (!bus) {
1375		err("cannot find Bus structure for the bridged device\n");
1376		return -EINVAL;
1377	}
1378	debug("bus->busno is %x\n", bus->busno);
1379	debug("sec_number is %x\n", sec_number);
1380
1381	ibmphp_remove_bus(bus, busno);
1382
1383	for (count = 0; address[count]; count++) {
1384		/* for 2 BARs */
1385		pci_bus_read_config_dword(ibmphp_pci_bus, devfn, address[count], &start_address);
1386
1387		if (!start_address) {
1388			/* This BAR is not implemented */
1389			continue;
1390		}
1391
1392		tmp_address = start_address;
1393
1394		if (start_address & PCI_BASE_ADDRESS_SPACE_IO) {
1395			/* This is IO */
1396			start_address &= PCI_BASE_ADDRESS_IO_MASK;
1397			if (ibmphp_find_resource(bus, start_address, &io, IO) < 0) {
1398				err("cannot find corresponding IO resource to remove\n");
1399				return -EIO;
1400			}
1401			if (io)
1402				debug("io->start = %x\n", io->start);
1403
1404			ibmphp_remove_resource(io);
1405
1406			/* ????????? DO WE NEED TO WRITE ANYTHING INTO THE PCI CONFIG SPACE BACK ?????????? */
1407		} else {
1408			/* This is Memory */
1409			if (start_address & PCI_BASE_ADDRESS_MEM_PREFETCH) {
1410				/* pfmem */
1411				start_address &= PCI_BASE_ADDRESS_MEM_MASK;
1412				if (ibmphp_find_resource(bus, start_address, &pfmem, PFMEM) < 0) {
1413					err("cannot find corresponding PFMEM resource to remove\n");
1414					return -EINVAL;
1415				}
1416				if (pfmem) {
1417					debug("pfmem->start = %x\n", pfmem->start);
1418
1419					ibmphp_remove_resource(pfmem);
1420				}
1421			} else {
1422				/* regular memory */
1423				start_address &= PCI_BASE_ADDRESS_MEM_MASK;
1424				if (ibmphp_find_resource(bus, start_address, &mem, MEM) < 0) {
1425					err("cannot find corresponding MEM resource to remove\n");
1426					return -EINVAL;
1427				}
1428				if (mem) {
1429					debug("mem->start = %x\n", mem->start);
1430
1431					ibmphp_remove_resource(mem);
1432				}
1433			}
1434			if (tmp_address & PCI_BASE_ADDRESS_MEM_TYPE_64) {
1435				/* takes up another dword */
1436				count += 1;
1437			}
1438		}	/* end of mem */
1439	}	/* end of for */
1440	debug("%s - exiting, returning success\n", __func__);
1441	return 0;
1442}
1443
1444static int unconfigure_boot_card(struct slot *slot_cur)
1445{
1446	u16 vendor_id;
1447	u32 class;
1448	u8 hdr_type;
1449	u8 device;
1450	u8 busno;
1451	u8 function;
1452	int rc;
1453	unsigned int devfn;
1454	u8 valid_device = 0x00; /* To see if we are ever able to find valid device and read it */
1455
1456	debug("%s - enter\n", __func__);
1457
1458	device = slot_cur->device;
1459	busno = slot_cur->bus;
1460
1461	debug("b4 for loop, device is %x\n", device);
1462	/* For every function on the card */
1463	for (function = 0x0; function < 0x08; function++) {
1464		devfn = PCI_DEVFN(device, function);
1465		ibmphp_pci_bus->number = busno;
1466
1467		pci_bus_read_config_word(ibmphp_pci_bus, devfn, PCI_VENDOR_ID, &vendor_id);
1468
1469		if (vendor_id != PCI_VENDOR_ID_NOTVALID) {
1470			/* found correct device!!! */
1471			++valid_device;
1472
1473			debug("%s - found correct device\n", __func__);
1474
1475			/* header: x x x x x x x x
1476			 *         | |___________|=> 1=PPB bridge, 0=normal device, 2=CardBus Bridge
1477			 *         |_=> 0 = single function device, 1 = multi-function device
1478			 */
1479
1480			pci_bus_read_config_byte(ibmphp_pci_bus, devfn, PCI_HEADER_TYPE, &hdr_type);
1481			pci_bus_read_config_dword(ibmphp_pci_bus, devfn, PCI_CLASS_REVISION, &class);
1482
1483			debug("hdr_type %x, class %x\n", hdr_type, class);
1484			class >>= 8;	/* to take revision out, class = class.subclass.prog i/f */
1485			if (class == PCI_CLASS_NOT_DEFINED_VGA) {
1486				err("The device %x function %x is VGA compatible and is not supported for hot removing.  Please choose another device.\n", device, function);
1487				return -ENODEV;
1488			} else if (class == PCI_CLASS_DISPLAY_VGA) {
1489				err("The device %x function %x is not supported for hot removing.  Please choose another device.\n", device, function);
1490				return -ENODEV;
1491			}
1492
1493			switch (hdr_type) {
1494				case PCI_HEADER_TYPE_NORMAL:
1495					rc = unconfigure_boot_device(busno, device, function);
1496					if (rc) {
1497						err("was not able to unconfigure device %x func %x on bus %x. bailing out...\n",
1498						     device, function, busno);
1499						return rc;
1500					}
1501					function = 0x8;
1502					break;
1503				case PCI_HEADER_TYPE_MULTIDEVICE:
1504					rc = unconfigure_boot_device(busno, device, function);
1505					if (rc) {
1506						err("was not able to unconfigure device %x func %x on bus %x. bailing out...\n",
1507						     device, function, busno);
1508						return rc;
1509					}
1510					break;
1511				case PCI_HEADER_TYPE_BRIDGE:
1512					class >>= 8;
1513					if (class != PCI_CLASS_BRIDGE_PCI) {
1514						err("This device %x function %x is not PCI-to-PCI bridge, and is not supported for hot-removing.  Please try another card.\n", device, function);
1515						return -ENODEV;
1516					}
1517					rc = unconfigure_boot_bridge(busno, device, function);
1518					if (rc != 0) {
1519						err("was not able to hot-remove PPB properly.\n");
1520						return rc;
1521					}
1522
1523					function = 0x8;
1524					break;
1525				case PCI_HEADER_TYPE_MULTIBRIDGE:
1526					class >>= 8;
1527					if (class != PCI_CLASS_BRIDGE_PCI) {
1528						err("This device %x function %x is not PCI-to-PCI bridge,  and is not supported for hot-removing.  Please try another card.\n", device, function);
1529						return -ENODEV;
1530					}
1531					rc = unconfigure_boot_bridge(busno, device, function);
1532					if (rc != 0) {
1533						err("was not able to hot-remove PPB properly.\n");
1534						return rc;
1535					}
1536					break;
1537				default:
1538					err("MAJOR PROBLEM!!!! Cannot read device's header\n");
1539					return -1;
1540					break;
1541			}	/* end of switch */
1542		}	/* end of valid device */
1543	}	/* end of for */
1544
1545	if (!valid_device) {
1546		err("Could not find device to unconfigure.  Or could not read the card.\n");
1547		return -1;
1548	}
1549	return 0;
1550}
1551
1552/*
1553 * free the resources of the card (multi, single, or bridged)
1554 * Parameters: slot, flag to say if this is for removing entire module or just
1555 * unconfiguring the device
1556 * TO DO:  will probably need to add some code in case there was some resource,
1557 * to remove it... this is from when we have errors in the configure_card...
1558 *			!!!!!!!!!!!!!!!!!!!!!!!!!FOR BUSES!!!!!!!!!!!!
1559 * Returns: 0, -1, -ENODEV
1560 */
1561int ibmphp_unconfigure_card(struct slot **slot_cur, int the_end)
1562{
1563	int i;
1564	int count;
1565	int rc;
1566	struct slot *sl = *slot_cur;
1567	struct pci_func *cur_func = NULL;
1568	struct pci_func *temp_func;
1569
1570	debug("%s - enter\n", __func__);
1571
1572	if (!the_end) {
1573		/* Need to unconfigure the card */
1574		rc = unconfigure_boot_card(sl);
1575		if ((rc == -ENODEV) || (rc == -EIO) || (rc == -EINVAL)) {
1576			/* In all other cases, will still need to get rid of func structure if it exists */
1577			return rc;
1578		}
1579	}
1580
1581	if (sl->func) {
1582		cur_func = sl->func;
1583		while (cur_func) {
1584			/* TO DO: WILL MOST LIKELY NEED TO GET RID OF THE BUS STRUCTURE FROM RESOURCES AS WELL */
1585			if (cur_func->bus) {
1586				/* in other words, it's a PPB */
1587				count = 2;
1588			} else {
1589				count = 6;
1590			}
1591
1592			for (i = 0; i < count; i++) {
1593				if (cur_func->io[i]) {
1594					debug("io[%d] exists\n", i);
1595					if (the_end > 0)
1596						ibmphp_remove_resource(cur_func->io[i]);
1597					cur_func->io[i] = NULL;
1598				}
1599				if (cur_func->mem[i]) {
1600					debug("mem[%d] exists\n", i);
1601					if (the_end > 0)
1602						ibmphp_remove_resource(cur_func->mem[i]);
1603					cur_func->mem[i] = NULL;
1604				}
1605				if (cur_func->pfmem[i]) {
1606					debug("pfmem[%d] exists\n", i);
1607					if (the_end > 0)
1608						ibmphp_remove_resource(cur_func->pfmem[i]);
1609					cur_func->pfmem[i] = NULL;
1610				}
1611			}
1612
1613			temp_func = cur_func->next;
1614			kfree(cur_func);
1615			cur_func = temp_func;
1616		}
1617	}
1618
1619	sl->func = NULL;
1620	*slot_cur = sl;
1621	debug("%s - exit\n", __func__);
1622	return 0;
1623}
1624
1625/*
1626 * add a new bus resulting from hot-plugging a PPB bridge with devices
1627 *
1628 * Input: bus and the amount of resources needed (we know we can assign those,
1629 *        since they've been checked already
1630 * Output: bus added to the correct spot
1631 *         0, -1, error
1632 */
1633static int add_new_bus(struct bus_node *bus, struct resource_node *io, struct resource_node *mem, struct resource_node *pfmem, u8 parent_busno)
1634{
1635	struct range_node *io_range = NULL;
1636	struct range_node *mem_range = NULL;
1637	struct range_node *pfmem_range = NULL;
1638	struct bus_node *cur_bus = NULL;
1639
1640	/* Trying to find the parent bus number */
1641	if (parent_busno != 0xFF) {
1642		cur_bus	= ibmphp_find_res_bus(parent_busno);
1643		if (!cur_bus) {
1644			err("strange, cannot find bus which is supposed to be at the system... something is terribly wrong...\n");
1645			return -ENODEV;
1646		}
1647
1648		list_add(&bus->bus_list, &cur_bus->bus_list);
1649	}
1650	if (io) {
1651		io_range = kzalloc(sizeof(*io_range), GFP_KERNEL);
1652		if (!io_range) {
1653			err("out of system memory\n");
1654			return -ENOMEM;
1655		}
1656		io_range->start = io->start;
1657		io_range->end = io->end;
1658		io_range->rangeno = 1;
1659		bus->noIORanges = 1;
1660		bus->rangeIO = io_range;
1661	}
1662	if (mem) {
1663		mem_range = kzalloc(sizeof(*mem_range), GFP_KERNEL);
1664		if (!mem_range) {
1665			err("out of system memory\n");
1666			return -ENOMEM;
1667		}
1668		mem_range->start = mem->start;
1669		mem_range->end = mem->end;
1670		mem_range->rangeno = 1;
1671		bus->noMemRanges = 1;
1672		bus->rangeMem = mem_range;
1673	}
1674	if (pfmem) {
1675		pfmem_range = kzalloc(sizeof(*pfmem_range), GFP_KERNEL);
1676		if (!pfmem_range) {
1677			err("out of system memory\n");
1678			return -ENOMEM;
1679		}
1680		pfmem_range->start = pfmem->start;
1681		pfmem_range->end = pfmem->end;
1682		pfmem_range->rangeno = 1;
1683		bus->noPFMemRanges = 1;
1684		bus->rangePFMem = pfmem_range;
1685	}
1686	return 0;
1687}
1688
1689/*
1690 * find the 1st available bus number for PPB to set as its secondary bus
1691 * Parameters: bus_number of the primary bus
1692 * Returns: bus_number of the secondary bus or 0xff in case of failure
1693 */
1694static u8 find_sec_number(u8 primary_busno, u8 slotno)
1695{
1696	int min, max;
1697	u8 busno;
1698	struct bus_info *bus;
1699	struct bus_node *bus_cur;
1700
1701	bus = ibmphp_find_same_bus_num(primary_busno);
1702	if (!bus) {
1703		err("cannot get slot range of the bus from the BIOS\n");
1704		return 0xff;
1705	}
1706	max = bus->slot_max;
1707	min = bus->slot_min;
1708	if ((slotno > max) || (slotno < min)) {
1709		err("got the wrong range\n");
1710		return 0xff;
1711	}
1712	busno = (u8) (slotno - (u8) min);
1713	busno += primary_busno + 0x01;
1714	bus_cur = ibmphp_find_res_bus(busno);
1715	/* either there is no such bus number, or there are no ranges, which
1716	 * can only happen if we removed the bridged device in previous load
1717	 * of the driver, and now only have the skeleton bus struct
1718	 */
1719	if ((!bus_cur) || (!(bus_cur->rangeIO) && !(bus_cur->rangeMem) && !(bus_cur->rangePFMem)))
1720		return busno;
1721	return 0xff;
1722}