iSeries_pci.c 23.7 KB
Newer Older
1
#define PCIFR(...)
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/*
 * iSeries_pci.c
 *
 * Copyright (C) 2001 Allan Trautman, IBM Corporation
 *
 * iSeries specific routines for PCI.
 * 
 * Based on code from pci.c and iSeries_pci.c 32bit
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 */
#include <linux/kernel.h>
#include <linux/list.h> 
#include <linux/string.h>
#include <linux/init.h>
29
#include <linux/module.h>
30 31 32 33 34 35 36 37 38
#include <linux/ide.h>
#include <linux/pci.h>

#include <asm/io.h>
#include <asm/irq.h>
#include <asm/prom.h>
#include <asm/machdep.h>
#include <asm/pci-bridge.h>
#include <asm/ppcdebug.h>
39
#include <asm/naca.h>
40
#include <asm/iommu.h>
41 42 43 44 45 46 47 48 49 50 51 52

#include <asm/iSeries/HvCallPci.h>
#include <asm/iSeries/HvCallSm.h>
#include <asm/iSeries/HvCallXm.h>
#include <asm/iSeries/LparData.h>
#include <asm/iSeries/iSeries_irq.h>
#include <asm/iSeries/iSeries_pci.h>
#include <asm/iSeries/mf.h>

#include "iSeries_IoMmTable.h"
#include "pci.h"

53
extern int panic_timeout;
54 55 56

extern unsigned long iSeries_Base_Io_Memory;    

57
extern struct iommu_table *tceTables[256];
58 59 60

extern void iSeries_MmIoTest(void);

61
/*
62
 * Forward declares of prototypes. 
63
 */
64
static struct iSeries_Device_Node *find_Device_Node(int bus, int devfn);
65 66 67 68 69
static void iSeries_Scan_PHBs_Slots(struct pci_controller *Phb);
static void iSeries_Scan_EADs_Bridge(HvBusNumber Bus, HvSubBusNumber SubBus,
		int IdSel);
static int iSeries_Scan_Bridge_Slot(HvBusNumber Bus,
		struct HvCallPci_BridgeInfo *Info);
70

Anton Blanchard's avatar
Anton Blanchard committed
71
LIST_HEAD(iSeries_Global_Device_List);
72

73
static int DeviceCount;
Anton Blanchard's avatar
Anton Blanchard committed
74 75

/* Counters and control flags. */
76 77 78 79 80 81 82 83 84 85
static long Pci_Io_Read_Count;
static long Pci_Io_Write_Count;
#if 0
static long Pci_Cfg_Read_Count;
static long Pci_Cfg_Write_Count;
#endif
static long Pci_Error_Count;

static int Pci_Retry_Max = 3;	/* Only retry 3 times  */	
static int Pci_Error_Flag = 1;	/* Set Retry Error on. */
86 87

static struct pci_ops iSeries_pci_ops;
Anton Blanchard's avatar
Anton Blanchard committed
88

89
/*
90 91 92 93 94
 * Log Error infor in Flight Recorder to system Console.
 * Filter out the device not there errors.
 * PCI: EADs Connect Failed 0x18.58.10 Rc: 0x00xx
 * PCI: Read Vendor Failed 0x18.58.10 Rc: 0x00xx
 * PCI: Connect Bus Unit Failed 0x18.58.10 Rc: 0x00xx
95 96 97
 */
static void pci_Log_Error(char *Error_Text, int Bus, int SubBus,
		int AgentId, int HvRc)
98
{
99 100
	if (HvRc == 0x0302)
		return;
101

102 103
	printk(KERN_ERR "PCI: %s Failed: 0x%02X.%02X.%02X Rc: 0x%04X",
	       Error_Text, Bus, SubBus, AgentId, HvRc);
104 105
}

106 107
#if 0
/*
108
 * Dump the iSeries Temp Device Node 
109 110 111 112 113 114 115 116 117 118 119 120 121
 * <4>buswalk [swapper : - DeviceNode: 0xC000000000634300
 * <4>00. Device Node   = 0xC000000000634300
 * <4>    - PciDev      = 0x0000000000000000
 * <4>    - tDevice     = 0x  17:01.00  0x1022 00
 * <4>  4. Device Node = 0xC000000000634480
 * <4>     - PciDev    = 0x0000000000000000
 * <4>     - Device    = 0x  18:38.16 Irq:0xA7 Vendor:0x1014  Flags:0x00
 * <4>     - Devfn     = 0xB0: 22.18
 */
void dumpDevice_Node(struct iSeries_Device_Node *DevNode)
{
	udbg_printf("Device Node      = 0x%p\n", DevNode);
	udbg_printf("     - PciDev    = 0x%p\n", DevNode->PciDev);
122
	udbg_printf("     - Device    = 0x%4X:%02X.%02X (0x%02X)\n",
123 124 125 126 127
			ISERIES_BUS(DevNode), ISERIES_SUBBUS(DevNode),
			DevNode->AgentId, DevNode->DevFn);
	udbg_printf("     - LSlot     = 0x%02X\n", DevNode->LogicalSlot);
	udbg_printf("     - TceTable  = 0x%p\n  ", DevNode->DevTceTable);
	udbg_printf("     - DSA       = 0x%04X\n", ISERIES_DSA(DevNode) >> 32);
128
	udbg_printf("                 = Irq:0x%02X Vendor:0x%04X  Flags:0x%02X\n",
129 130
			DevNode->Irq, DevNode->Vendor, DevNode->Flags);
	udbg_printf("     - Location  = %s\n", DevNode->CardLocation);
131
}
132 133

/*
134
 * Walk down the device node chain 
135 136
 */
static void list_device_nodes(void)
137
{
138 139 140 141
	struct list_head *Device_Node_Ptr = iSeries_Global_Device_List.next;

	while (Device_Node_Ptr != &iSeries_Global_Device_List) {
		dumpDevice_Node((struct iSeries_Device_Node*)Device_Node_Ptr);
142 143 144
		Device_Node_Ptr = Device_Node_Ptr->next;
	}
}
145
#endif
146

147
/*
148
 * build_device_node(u16 Bus, int SubBus, u8 DevFn)
149 150 151
 */
static struct iSeries_Device_Node *build_device_node(HvBusNumber Bus,
		HvSubBusNumber SubBus, int AgentId, int Function)
152
{
153
	struct iSeries_Device_Node *node;
154

155 156 157
	PPCDBG(PPCDBG_BUSWALK,
			"-build_device_node 0x%02X.%02X.%02X Function: %02X\n",
			Bus, SubBus, AgentId, Function);
158

159 160
	node = kmalloc(sizeof(struct iSeries_Device_Node), GFP_KERNEL);
	if (node == NULL)
161
		return NULL;
162

163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
	memset(node, 0, sizeof(struct iSeries_Device_Node));
	list_add_tail(&node->Device_List, &iSeries_Global_Device_List);
#if 0
	node->DsaAddr = ((u64)Bus << 48) + ((u64)SubBus << 40) + ((u64)0x10 << 32);
#endif
	node->DsaAddr.DsaAddr = 0;
	node->DsaAddr.Dsa.busNumber = Bus;
	node->DsaAddr.Dsa.subBusNumber = SubBus;
	node->DsaAddr.Dsa.deviceId = 0x10;
	node->AgentId = AgentId;
	node->DevFn = PCI_DEVFN(ISERIES_ENCODE_DEVICE(AgentId), Function);
	node->IoRetry = 0;
	iSeries_Get_Location_Code(node);
	PCIFR("Device 0x%02X.%2X, Node:0x%p ", ISERIES_BUS(node),
			ISERIES_DEVFUN(node), node);
	return node;
179 180
}

181
/*
182
 * unsigned long __init find_and_init_phbs(void)
183 184 185 186
 *
 * Description:
 *   This function checks for all possible system PCI host bridges that connect
 *   PCI buses.  The system hypervisor is queried as to the guest partition
187
 *   ownership status.  A pci_controller is built for any bus which is partially
188
 *   owned or fully owned by this guest partition.
189
 */
190 191
unsigned long __init find_and_init_phbs(void)
{
192
	struct pci_controller *phb;
193
	HvBusNumber bus;
194

195
	PPCDBG(PPCDBG_BUSWALK, "find_and_init_phbs Entry\n");
196 197

	/* Check all possible buses. */
198 199 200 201 202
	for (bus = 0; bus < 256; bus++) {
		int ret = HvCallXm_testBus(bus);
		if (ret == 0) {
			printk("bus %d appears to exist\n", bus);
			phb = pci_alloc_pci_controller(phb_type_hypervisor);
203 204
			if (phb == NULL) {
				PCIFR("Allocate pci_controller failed.");
205 206
				return -1;
			}
207 208 209
			phb->pci_mem_offset = phb->local_number = bus;
			phb->first_busno = bus;
			phb->last_busno = bus;
210
			phb->ops = &iSeries_pci_ops;
211

212
			PPCDBG(PPCDBG_BUSWALK, "PCI:Create iSeries pci_controller(%p), Bus: %04X\n",
213 214
					phb, bus);
			PCIFR("Create iSeries PHB controller: %04X", bus);
215

216
			/* Find and connect the devices. */
217 218
			iSeries_Scan_PHBs_Slots(phb);
		}
219 220 221 222
		/*
		 * Check for Unexpected Return code, a clue that something
		 * has gone wrong.
		 */
223 224 225
		else if (ret != 0x0301)
			printk(KERN_ERR "Unexpected Return on Probe(0x%04X): 0x%04X",
			       bus, ret);
226 227 228
	}
	return 0;
}
229 230

/*
231
 * iSeries_pcibios_init
232 233
 *  
 * Chance to initialize and structures or variable before PCI Bus walk.
234
 */
235 236
void iSeries_pcibios_init(void)
{
237
	PPCDBG(PPCDBG_BUSWALK, "iSeries_pcibios_init Entry.\n"); 
238 239
	iSeries_IoMmTable_Initialize();
	find_and_init_phbs();
240 241
	/* pci_assign_all_busses = 0;		SFRXXX*/
	PPCDBG(PPCDBG_BUSWALK, "iSeries_pcibios_init Exit.\n"); 
242
}
243 244

/*
245
 * iSeries_pci_final_fixup(void)  
246
 */
247
void __init iSeries_pci_final_fixup(void)
248
{
249 250
	struct pci_dev *pdev = NULL;
	struct iSeries_Device_Node *node;
251 252 253 254
	char Buffer[256];
    	int DeviceCount = 0;

	PPCDBG(PPCDBG_BUSWALK, "iSeries_pcibios_fixup Entry.\n"); 
255 256

	/* Fix up at the device node and pci_dev relationship */
257 258
	mf_displaySrc(0xC9000100);

259 260
	printk("pcibios_final_fixup\n");
	while ((pdev = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, pdev))
261
			!= NULL) {
262 263 264 265 266
		node = find_Device_Node(pdev->bus->number, pdev->devfn);
		printk("pci dev %p (%x.%x), node %p\n", pdev,
		       pdev->bus->number, pdev->devfn, node);

		if (node != NULL) {
267
			++DeviceCount;
268 269
			pdev->sysdata = (void *)node;
			node->PciDev = pdev;
270
			PPCDBG(PPCDBG_BUSWALK,
271 272 273 274
					"pdev 0x%p <==> DevNode 0x%p\n",
					pdev, node);
			iSeries_allocateDeviceBars(pdev);
			iSeries_Device_Information(pdev, Buffer,
275 276
					sizeof(Buffer));
			printk("%d. %s\n", DeviceCount, Buffer);
277
			iommu_devnode_init(node);
278 279
		} else
			printk("PCI: Device Tree not found for 0x%016lX\n",
280
					(unsigned long)pdev);
281
		pdev->irq = node->Irq;
282 283 284
	}
	iSeries_IoMmTable_Status();
	iSeries_activate_IRQs();
285 286 287
	mf_displaySrc(0xC9000200);
}

288
void pcibios_fixup_bus(struct pci_bus *PciBus)
289
{
290 291
	PPCDBG(PPCDBG_BUSWALK, "iSeries_pcibios_fixup_bus(0x%04X) Entry.\n",
			PciBus->number); 
292 293
}

294
void pcibios_fixup_resources(struct pci_dev *pdev)
295
{
296
	PPCDBG(PPCDBG_BUSWALK, "fixup_resources pdev %p\n", pdev);
297 298
}   

299 300 301 302
/*
 * Loop through each node function to find usable EADs bridges.  
 */
static void iSeries_Scan_PHBs_Slots(struct pci_controller *Phb)
303
{
304
	struct HvCallPci_DeviceInfo *DevInfo;
305 306
	HvBusNumber bus = Phb->local_number;	/* System Bus */	
	const HvSubBusNumber SubBus = 0;	/* EADs is always 0. */
307
	int HvRc = 0;
308 309
	int IdSel;	
	const int MaxAgents = 8;
310

311 312 313 314
	DevInfo = (struct HvCallPci_DeviceInfo*)
		kmalloc(sizeof(struct HvCallPci_DeviceInfo), GFP_KERNEL);
	if (DevInfo == NULL)
		return;
315

316
	/*
317
	 * Probe for EADs Bridges      
318
	 */
319 320
	for (IdSel = 1; IdSel < MaxAgents; ++IdSel) {
    		HvRc = HvCallPci_getDeviceInfo(bus, SubBus, IdSel,
321 322
				REALADDR(DevInfo),
				sizeof(struct HvCallPci_DeviceInfo));
323
		if (HvRc == 0) {
324
			if (DevInfo->deviceType == HvCallPci_NodeDevice)
325
				iSeries_Scan_EADs_Bridge(bus, SubBus, IdSel);
326
			else
327 328 329
				printk("PCI: Invalid System Configuration(0x%02X)"
				       " for bus 0x%02x id 0x%02x.\n",
				       DevInfo->deviceType, bus, IdSel);
330
		}
331
		else
332
			pci_Log_Error("getDeviceInfo", bus, SubBus, IdSel, HvRc);
333 334 335 336
	}
	kfree(DevInfo);
}

337
static void iSeries_Scan_EADs_Bridge(HvBusNumber bus, HvSubBusNumber SubBus,
338
		int IdSel)
339
{
340 341 342 343
	struct HvCallPci_BridgeInfo *BridgeInfo;
	HvAgentId AgentId;
	int Function;
	int HvRc;
344

345 346 347 348
	BridgeInfo = (struct HvCallPci_BridgeInfo *)
		kmalloc(sizeof(struct HvCallPci_BridgeInfo), GFP_KERNEL);
	if (BridgeInfo == NULL)
		return;
349

350 351
	/* Note: hvSubBus and irq is always be 0 at this level! */
	for (Function = 0; Function < 8; ++Function) {
352
	  	AgentId = ISERIES_PCI_AGENTID(IdSel, Function);
353
		HvRc = HvCallXm_connectBusUnit(bus, SubBus, AgentId, 0);
354
 		if (HvRc == 0) {
355 356
			printk("found device at bus %d idsel %d func %d (AgentId %x)\n",
			       bus, IdSel, Function, AgentId);
357
  			/*  Connect EADs: 0x18.00.12 = 0x00 */
358 359
			PPCDBG(PPCDBG_BUSWALK,
					"PCI:Connect EADs: 0x%02X.%02X.%02X\n",
360 361
					bus, SubBus, AgentId);
	    		HvRc = HvCallPci_getBusUnitInfo(bus, SubBus, AgentId,
362 363
					REALADDR(BridgeInfo),
					sizeof(struct HvCallPci_BridgeInfo));
364
	 		if (HvRc == 0) {
365
				printk("bridge info: type %x subbus %x maxAgents %x maxsubbus %x logslot %x\n",
366 367 368 369 370
					BridgeInfo->busUnitInfo.deviceType,
					BridgeInfo->subBusNumber,
					BridgeInfo->maxAgents,
					BridgeInfo->maxSubBusNumber,
					BridgeInfo->logicalSlotNumber);
371 372
				PPCDBG(PPCDBG_BUSWALK,
					"PCI: BridgeInfo, Type:0x%02X, SubBus:0x%02X, MaxAgents:0x%02X, MaxSubBus: 0x%02X, LSlot: 0x%02X\n",
373 374 375 376 377 378 379 380
					BridgeInfo->busUnitInfo.deviceType,
					BridgeInfo->subBusNumber,
					BridgeInfo->maxAgents,
					BridgeInfo->maxSubBusNumber,
					BridgeInfo->logicalSlotNumber);

				if (BridgeInfo->busUnitInfo.deviceType ==
						HvCallPci_BridgeDevice)  {
381
					/* Scan_Bridge_Slot...: 0x18.00.12 */
382
					iSeries_Scan_Bridge_Slot(bus, BridgeInfo);
383 384 385
				} else
					printk("PCI: Invalid Bridge Configuration(0x%02X)",
						BridgeInfo->busUnitInfo.deviceType);
386
			}
387
    		} else if (HvRc != 0x000B)
388
			pci_Log_Error("EADs Connect",
389
					bus, SubBus, AgentId, HvRc);
390 391 392 393
	}
	kfree(BridgeInfo);
}

394 395 396 397 398
/*
 * This assumes that the node slot is always on the primary bus!
 */
static int iSeries_Scan_Bridge_Slot(HvBusNumber Bus,
		struct HvCallPci_BridgeInfo *BridgeInfo)
399
{
400
	struct iSeries_Device_Node *node;
401
	HvSubBusNumber SubBus = BridgeInfo->subBusNumber;
402 403 404 405 406 407 408 409
	u16 VendorId = 0;
	int HvRc = 0;
	u8 Irq = 0;
	int IdSel = ISERIES_GET_DEVICE_FROM_SUBBUS(SubBus);
	int Function = ISERIES_GET_FUNCTION_FROM_SUBBUS(SubBus);
	HvAgentId EADsIdSel = ISERIES_PCI_AGENTID(IdSel, Function);

	/* iSeries_allocate_IRQ.: 0x18.00.12(0xA3) */
410
  	Irq = iSeries_allocate_IRQ(Bus, 0, EADsIdSel);
411 412
	PPCDBG(PPCDBG_BUSWALK,
		"PCI:- allocate and assign IRQ 0x%02X.%02X.%02X = 0x%02X\n",
413
		Bus, 0, EADsIdSel, Irq);
414

415
	/*
416
	 * Connect all functions of any device found.  
417
	 */
418
  	for (IdSel = 1; IdSel <= BridgeInfo->maxAgents; ++IdSel) {
419
    		for (Function = 0; Function < 8; ++Function) {
420
			HvAgentId AgentId = ISERIES_PCI_AGENTID(IdSel, Function);
421 422
			HvRc = HvCallXm_connectBusUnit(Bus, SubBus,
					AgentId, Irq);
423
			if (HvRc != 0) {
424
				pci_Log_Error("Connect Bus Unit",
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
					      Bus, SubBus, AgentId, HvRc);
				continue;
			}

			HvRc = HvCallPci_configLoad16(Bus, SubBus, AgentId,
						      PCI_VENDOR_ID, &VendorId);
			if (HvRc != 0) {
				pci_Log_Error("Read Vendor",
					      Bus, SubBus, AgentId, HvRc);
				continue;
			}
			printk("read vendor ID: %x\n", VendorId);

			/* FoundDevice: 0x18.28.10 = 0x12AE */
			PPCDBG(PPCDBG_BUSWALK,
440 441
			       "PCI:- FoundDevice: 0x%02X.%02X.%02X = 0x%04X, irq %d\n",
			       Bus, SubBus, AgentId, VendorId, Irq);
442 443 444 445 446 447 448 449 450 451 452 453
			HvRc = HvCallPci_configStore8(Bus, SubBus, AgentId,
						      PCI_INTERRUPT_LINE, Irq);  
			if (HvRc != 0)
				pci_Log_Error("PciCfgStore Irq Failed!",
					      Bus, SubBus, AgentId, HvRc);

			++DeviceCount;
			node = build_device_node(Bus, SubBus, EADsIdSel, Function);
			node->Vendor = VendorId;
			node->Irq = Irq;
			node->LogicalSlot = BridgeInfo->logicalSlotNumber;

454 455 456 457
		} /* for (Function = 0; Function < 8; ++Function) */
	} /* for (IdSel = 1; IdSel <= MaxAgents; ++IdSel) */
	return HvRc;
}
458 459 460 461 462 463 464 465 466 467 468 469 470

/*
 * I/0 Memory copy MUST use mmio commands on iSeries
 * To do; For performance, include the hv call directly
 */
void *iSeries_memset_io(void *dest, char c, size_t Count)
{
	u8 ByteValue = c;
	long NumberOfBytes = Count;
	char *IoBuffer = dest;

	while (NumberOfBytes > 0) {
		iSeries_Write_Byte(ByteValue, (void *)IoBuffer);
471 472 473 474
		++IoBuffer;
		-- NumberOfBytes;
	}
	return dest;
475
}
476
EXPORT_SYMBOL(iSeries_memset_io);
477 478 479 480 481 482 483 484 485

void *iSeries_memcpy_toio(void *dest, void *source, size_t count)
{
	char *dst = dest;
	char *src = source;
	long NumberOfBytes = count;

	while (NumberOfBytes > 0) {
		iSeries_Write_Byte(*src++, (void *)dst++);
486 487 488 489
		-- NumberOfBytes;
	}
	return dest;
}
490
EXPORT_SYMBOL(iSeries_memcpy_toio);
491 492

void *iSeries_memcpy_fromio(void *dest, void *source, size_t count)
493 494 495
{
	char *dst = dest;
	char *src = source;
496 497 498 499
	long NumberOfBytes = count;

	while (NumberOfBytes > 0) {
		*dst++ = iSeries_Read_Byte((void *)src++);
500 501 502 503
		-- NumberOfBytes;
	}
	return dest;
}
504
EXPORT_SYMBOL(iSeries_memcpy_fromio);
505 506

/*
507
 * Look down the chain to find the matching Device Device
508
 */
509
static struct iSeries_Device_Node *find_Device_Node(int bus, int devfn)
510
{
511
	struct list_head *pos;
512

513 514 515
	list_for_each(pos, &iSeries_Global_Device_List) {
		struct iSeries_Device_Node *node =
			list_entry(pos, struct iSeries_Device_Node, Device_List);
516

517 518
		if ((bus == ISERIES_BUS(node)) && (devfn == node->DevFn))
			return node;
519 520 521
	}
	return NULL;
}
522 523 524 525 526 527 528

#if 0
/*
 * Returns the device node for the passed pci_dev
 * Sanity Check Node PciDev to passed pci_dev
 * If none is found, returns a NULL which the client must handle.
 */
529
static struct iSeries_Device_Node *get_Device_Node(struct pci_dev *pdev)
530
{
531 532 533 534 535 536
	struct iSeries_Device_Node *node;

	node = pdev->sysdata;
	if (node == NULL || node->PciDev != pdev)
		node = find_Device_Node(pdev->bus->number, pdev->devfn);
	return node;
537
}
538 539 540
#endif

/*
541 542 543 544
 * Config space read and write functions.
 * For now at least, we look for the device node for the bus and devfn
 * that we are asked to access.  It may be possible to translate the devfn
 * to a subbus and deviceid more directly.
545
 */
546 547 548 549
static u64 hv_cfg_read_func[4]  = {
	HvCallPciConfigLoad8, HvCallPciConfigLoad16,
	HvCallPciConfigLoad32, HvCallPciConfigLoad32
};
550

551 552 553 554
static u64 hv_cfg_write_func[4] = {
	HvCallPciConfigStore8, HvCallPciConfigStore16,
	HvCallPciConfigStore32, HvCallPciConfigStore32
};
555

556 557 558
/*
 * Read PCI config space
 */
559 560 561
static int iSeries_pci_read_config(struct pci_bus *bus, unsigned int devfn,
		int offset, int size, u32 *val)
{
562 563 564
	struct iSeries_Device_Node *node = find_Device_Node(bus->number, devfn);
	u64 fn;
	struct HvCallPci_LoadReturn ret;
565

566 567 568 569 570 571 572 573 574
	if (node == NULL)
		return PCIBIOS_DEVICE_NOT_FOUND;

	fn = hv_cfg_read_func[(size - 1) & 3];
	HvCall3Ret16(fn, &ret, node->DsaAddr.DsaAddr, offset, 0);

	if (ret.rc != 0) {
		*val = ~0;
		return PCIBIOS_DEVICE_NOT_FOUND;	/* or something */
575
	}
576

577 578
	*val = ret.value;
	return 0;
579
}
580 581 582 583

/*
 * Write PCI config space
 */
584 585 586

static int iSeries_pci_write_config(struct pci_bus *bus, unsigned int devfn,
		int offset, int size, u32 val)
587
{
588 589 590 591 592 593 594 595 596 597 598 599 600 601
	struct iSeries_Device_Node *node = find_Device_Node(bus->number, devfn);
	u64 fn;
	u64 ret;

	if (node == NULL)
		return PCIBIOS_DEVICE_NOT_FOUND;

	fn = hv_cfg_write_func[(size - 1) & 3];
	ret = HvCall4(fn, node->DsaAddr.DsaAddr, offset, val, 0);

	if (ret != 0)
		return PCIBIOS_DEVICE_NOT_FOUND;

	return 0;
602 603
}

604
static struct pci_ops iSeries_pci_ops = {
605 606
	.read = iSeries_pci_read_config,
	.write = iSeries_pci_write_config
607 608
};

609
/*
610 611 612 613
 * Check Return Code
 * -> On Failure, print and log information.
 *    Increment Retry Count, if exceeds max, panic partition.
 * -> If in retry, print and log success 
614
 *
615 616 617
 * PCI: Device 23.90 ReadL I/O Error( 0): 0x1234
 * PCI: Device 23.90 ReadL Retry( 1)
 * PCI: Device 23.90 ReadL Retry Successful(1)
618
 */
619 620
static int CheckReturnCode(char *TextHdr, struct iSeries_Device_Node *DevNode,
		u64 ret)
621
{
622
	if (ret != 0)  {
623 624 625
		++Pci_Error_Count;
		++DevNode->IoRetry;
		printk("PCI: %s: Device 0x%04X:%02X  I/O Error(%2d): 0x%04X\n",
626 627
				TextHdr, DevNode->DsaAddr.Dsa.busNumber, DevNode->DevFn,
				DevNode->IoRetry, (int)ret);
628 629 630 631 632 633
		/*
		 * Bump the retry and check for retry count exceeded.
		 * If, Exceeded, panic the system.
		 */
		if ((DevNode->IoRetry > Pci_Retry_Max) &&
				(Pci_Error_Flag > 0)) {
634 635
			mf_displaySrc(0xB6000103);
			panic_timeout = 0; 
636 637
			panic("PCI: Hardware I/O Error, SRC B6000103, "
					"Automatic Reboot Disabled.\n");
638 639 640
		}
		return -1;	/* Retry Try */
	}
641 642
	/* If retry was in progress, log success and rest retry count */
	if (DevNode->IoRetry > 0) {
643
		PCIFR("%s: Device 0x%04X:%02X Retry Successful(%2d).",
644
				TextHdr, DevNode->DsaAddr.Dsa.busNumber, DevNode->DevFn,
645
				DevNode->IoRetry);
646 647 648 649
		DevNode->IoRetry = 0;
	}
	return 0; 
}
650 651 652 653 654 655 656

/*
 * Translate the I/O Address into a device node, bar, and bar offset.
 * Note: Make sure the passed variable end up on the stack to avoid
 * the exposure of being device global.
 */
static inline struct iSeries_Device_Node *xlateIoMmAddress(void *IoAddress,
657
		 u64 *dsaptr, u64 *BarOffsetPtr)
658
{
659 660 661 662 663 664 665 666 667 668
	unsigned long BaseIoAddr;
	unsigned long TableIndex;
	struct iSeries_Device_Node *DevNode;

	if (((unsigned long)IoAddress < iSeries_Base_Io_Memory) ||
			((unsigned long)IoAddress >= iSeries_Max_Io_Memory))
		return NULL;
	BaseIoAddr = (unsigned long)IoAddress - iSeries_Base_Io_Memory;
	TableIndex = BaseIoAddr / iSeries_IoMmTable_Entry_Size;
	DevNode = iSeries_IoMmTable[TableIndex];
669 670

	if (DevNode != NULL) {
671 672
		int barnum = iSeries_IoBarTable[TableIndex];
		*dsaptr = DevNode->DsaAddr.DsaAddr | (barnum << 24);
673 674
		*BarOffsetPtr = BaseIoAddr % iSeries_IoMmTable_Entry_Size;
	} else
675 676 677 678
		panic("PCI: Invalid PCI IoAddress detected!\n");
	return DevNode;
}

679 680 681 682 683 684 685 686 687 688
/*
 * Read MM I/O Instructions for the iSeries
 * On MM I/O error, all ones are returned and iSeries_pci_IoError is cal
 * else, data is returned in big Endian format.
 *
 * iSeries_Read_Byte = Read Byte  ( 8 bit)
 * iSeries_Read_Word = Read Word  (16 bit)
 * iSeries_Read_Long = Read Long  (32 bit)
 */
u8 iSeries_Read_Byte(void *IoAddress)
689
{
690
	u64 BarOffset;
691 692
	u64 dsa;
	struct HvCallPci_LoadReturn ret;
693
	struct iSeries_Device_Node *DevNode =
694
		xlateIoMmAddress(IoAddress, &dsa, &BarOffset);
695

696 697 698 699 700 701 702 703 704 705 706 707
	if (DevNode == NULL) {
		static unsigned long last_jiffies;
		static int num_printed;

		if ((jiffies - last_jiffies) > 60 * HZ) {
			last_jiffies = jiffies;
			num_printed = 0;
		}
		if (num_printed++ < 10)
			printk(KERN_ERR "iSeries_Read_Byte: invalid access at IO address %p\n", IoAddress);
		return 0xff;
	}
708 709
	do {
		++Pci_Io_Read_Count;
710 711
		HvCall3Ret16(HvCallPciBarLoad8, &ret, dsa, BarOffset, 0);
	} while (CheckReturnCode("RDB", DevNode, ret.rc) != 0);
712

713
	return (u8)ret.value;
714
}
715
EXPORT_SYMBOL(iSeries_Read_Byte);
716 717

u16 iSeries_Read_Word(void *IoAddress)
718
{
719
	u64 BarOffset;
720 721
	u64 dsa;
	struct HvCallPci_LoadReturn ret;
722
	struct iSeries_Device_Node *DevNode =
723
		xlateIoMmAddress(IoAddress, &dsa, &BarOffset);
724

725 726 727 728 729 730 731 732 733 734 735 736
	if (DevNode == NULL) {
		static unsigned long last_jiffies;
		static int num_printed;

		if ((jiffies - last_jiffies) > 60 * HZ) {
			last_jiffies = jiffies;
			num_printed = 0;
		}
		if (num_printed++ < 10)
			printk(KERN_ERR "iSeries_Read_Word: invalid access at IO address %p\n", IoAddress);
		return 0xffff;
	}
737 738
	do {
		++Pci_Io_Read_Count;
739
		HvCall3Ret16(HvCallPciBarLoad16, &ret, dsa,
740
				BarOffset, 0);
741
	} while (CheckReturnCode("RDW", DevNode, ret.rc) != 0);
742

743
	return swab16((u16)ret.value);
744
}
745
EXPORT_SYMBOL(iSeries_Read_Word);
746 747

u32 iSeries_Read_Long(void *IoAddress)
748
{
749
	u64 BarOffset;
750 751
	u64 dsa;
	struct HvCallPci_LoadReturn ret;
752
	struct iSeries_Device_Node *DevNode =
753
		xlateIoMmAddress(IoAddress, &dsa, &BarOffset);
754

755 756 757 758 759 760 761 762 763 764 765 766
	if (DevNode == NULL) {
		static unsigned long last_jiffies;
		static int num_printed;

		if ((jiffies - last_jiffies) > 60 * HZ) {
			last_jiffies = jiffies;
			num_printed = 0;
		}
		if (num_printed++ < 10)
			printk(KERN_ERR "iSeries_Read_Long: invalid access at IO address %p\n", IoAddress);
		return 0xffffffff;
	}
767 768
	do {
		++Pci_Io_Read_Count;
769
		HvCall3Ret16(HvCallPciBarLoad32, &ret, dsa,
770
				BarOffset, 0);
771
	} while (CheckReturnCode("RDL", DevNode, ret.rc) != 0);
772

773
	return swab32((u32)ret.value);
774
}
775
EXPORT_SYMBOL(iSeries_Read_Long);
776 777 778 779 780 781 782 783

/*
 * Write MM I/O Instructions for the iSeries
 *
 * iSeries_Write_Byte = Write Byte (8 bit)
 * iSeries_Write_Word = Write Word(16 bit)
 * iSeries_Write_Long = Write Long(32 bit)
 */
784
void iSeries_Write_Byte(u8 data, void *IoAddress)
785 786
{
	u64 BarOffset;
787 788
	u64 dsa;
	u64 rc;
789
	struct iSeries_Device_Node *DevNode =
790
		xlateIoMmAddress(IoAddress, &dsa, &BarOffset);
791

792 793 794 795 796 797 798 799 800 801 802 803
	if (DevNode == NULL) {
		static unsigned long last_jiffies;
		static int num_printed;

		if ((jiffies - last_jiffies) > 60 * HZ) {
			last_jiffies = jiffies;
			num_printed = 0;
		}
		if (num_printed++ < 10)
			printk(KERN_ERR "iSeries_Write_Byte: invalid access at IO address %p\n", IoAddress);
		return;
	}
804 805
	do {
		++Pci_Io_Write_Count;
806 807
		rc = HvCall4(HvCallPciBarStore8, dsa, BarOffset, data, 0);
	} while (CheckReturnCode("WWB", DevNode, rc) != 0);
808
}
809
EXPORT_SYMBOL(iSeries_Write_Byte);
810

811
void iSeries_Write_Word(u16 data, void *IoAddress)
812
{
813
	u64 BarOffset;
814 815
	u64 dsa;
	u64 rc;
816
	struct iSeries_Device_Node *DevNode =
817
		xlateIoMmAddress(IoAddress, &dsa, &BarOffset);
818

819 820 821 822 823 824 825 826 827 828 829 830
	if (DevNode == NULL) {
		static unsigned long last_jiffies;
		static int num_printed;

		if ((jiffies - last_jiffies) > 60 * HZ) {
			last_jiffies = jiffies;
			num_printed = 0;
		}
		if (num_printed++ < 10)
			printk(KERN_ERR "iSeries_Write_Word: invalid access at IO address %p\n", IoAddress);
		return;
	}
831 832
	do {
		++Pci_Io_Write_Count;
833 834
		rc = HvCall4(HvCallPciBarStore16, dsa, BarOffset, swab16(data), 0);
	} while (CheckReturnCode("WWW", DevNode, rc) != 0);
835
}
836
EXPORT_SYMBOL(iSeries_Write_Word);
837

838
void iSeries_Write_Long(u32 data, void *IoAddress)
839
{
840
	u64 BarOffset;
841 842
	u64 dsa;
	u64 rc;
843
	struct iSeries_Device_Node *DevNode =
844
		xlateIoMmAddress(IoAddress, &dsa, &BarOffset);
845

846 847 848 849 850 851 852 853 854 855 856 857
	if (DevNode == NULL) {
		static unsigned long last_jiffies;
		static int num_printed;

		if ((jiffies - last_jiffies) > 60 * HZ) {
			last_jiffies = jiffies;
			num_printed = 0;
		}
		if (num_printed++ < 10)
			printk(KERN_ERR "iSeries_Write_Long: invalid access at IO address %p\n", IoAddress);
		return;
	}
858 859
	do {
		++Pci_Io_Write_Count;
860 861
		rc = HvCall4(HvCallPciBarStore32, dsa, BarOffset, swab32(data), 0);
	} while (CheckReturnCode("WWL", DevNode, rc) != 0);
862
}
863
EXPORT_SYMBOL(iSeries_Write_Long);
864 865 866

void pcibios_name_device(struct pci_dev *dev)
{
867
}