upgrade to linux 2.6.10-1.12_FC2
[linux-2.6.git] / arch / ppc64 / kernel / iSeries_pci.c
1 /*
2  * iSeries_pci.c
3  *
4  * Copyright (C) 2001 Allan Trautman, IBM Corporation
5  *
6  * iSeries specific routines for PCI.
7  * 
8  * Based on code from pci.c and iSeries_pci.c 32bit
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  * 
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  * 
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
23  */
24 #include <linux/kernel.h>
25 #include <linux/list.h> 
26 #include <linux/string.h>
27 #include <linux/init.h>
28 #include <linux/module.h>
29 #include <linux/ide.h>
30 #include <linux/pci.h>
31
32 #include <asm/io.h>
33 #include <asm/irq.h>
34 #include <asm/prom.h>
35 #include <asm/machdep.h>
36 #include <asm/pci-bridge.h>
37 #include <asm/ppcdebug.h>
38 #include <asm/naca.h>
39 #include <asm/iommu.h>
40
41 #include <asm/iSeries/HvCallPci.h>
42 #include <asm/iSeries/HvCallSm.h>
43 #include <asm/iSeries/HvCallXm.h>
44 #include <asm/iSeries/LparData.h>
45 #include <asm/iSeries/iSeries_irq.h>
46 #include <asm/iSeries/iSeries_pci.h>
47 #include <asm/iSeries/mf.h>
48
49 #include "pci.h"
50
51 extern int panic_timeout;
52
53 extern unsigned long io_page_mask;
54
55 /*
56  * Forward declares of prototypes. 
57  */
58 static struct iSeries_Device_Node *find_Device_Node(int bus, int devfn);
59 static void scan_PHB_slots(struct pci_controller *Phb);
60 static void scan_EADS_bridge(HvBusNumber Bus, HvSubBusNumber SubBus, int IdSel);
61 static int scan_bridge_slot(HvBusNumber Bus, struct HvCallPci_BridgeInfo *Info);
62
63 LIST_HEAD(iSeries_Global_Device_List);
64
65 static int DeviceCount;
66
67 /* Counters and control flags. */
68 static long Pci_Io_Read_Count;
69 static long Pci_Io_Write_Count;
70 #if 0
71 static long Pci_Cfg_Read_Count;
72 static long Pci_Cfg_Write_Count;
73 #endif
74 static long Pci_Error_Count;
75
76 static int Pci_Retry_Max = 3;   /* Only retry 3 times  */       
77 static int Pci_Error_Flag = 1;  /* Set Retry Error on. */
78
79 static struct pci_ops iSeries_pci_ops;
80
81 /*
82  * Table defines
83  * Each Entry size is 4 MB * 1024 Entries = 4GB I/O address space.
84  */
85 #define IOMM_TABLE_MAX_ENTRIES  1024
86 #define IOMM_TABLE_ENTRY_SIZE   0x0000000000400000UL
87 #define BASE_IO_MEMORY          0xE000000000000000UL
88
89 static unsigned long max_io_memory = 0xE000000000000000UL;
90 static long current_iomm_table_entry;
91
92 /*
93  * Lookup Tables.
94  */
95 static struct iSeries_Device_Node **iomm_table;
96 static u8 *iobar_table;
97
98 /*
99  * Static and Global variables
100  */
101 static char *pci_io_text = "iSeries PCI I/O";
102 static spinlock_t iomm_table_lock = SPIN_LOCK_UNLOCKED;
103
104 /*
105  * iomm_table_initialize
106  *
107  * Allocates and initalizes the Address Translation Table and Bar
108  * Tables to get them ready for use.  Must be called before any
109  * I/O space is handed out to the device BARs.
110  */
111 static void iomm_table_initialize(void)
112 {
113         spin_lock(&iomm_table_lock);
114         iomm_table = kmalloc(sizeof(*iomm_table) * IOMM_TABLE_MAX_ENTRIES,
115                         GFP_KERNEL);
116         iobar_table = kmalloc(sizeof(*iobar_table) * IOMM_TABLE_MAX_ENTRIES,
117                         GFP_KERNEL);
118         spin_unlock(&iomm_table_lock);
119         if ((iomm_table == NULL) || (iobar_table == NULL))
120                 panic("PCI: I/O tables allocation failed.\n");
121 }
122
123 /*
124  * iomm_table_allocate_entry
125  *
126  * Adds pci_dev entry in address translation table
127  *
128  * - Allocates the number of entries required in table base on BAR
129  *   size.
130  * - Allocates starting at BASE_IO_MEMORY and increases.
131  * - The size is round up to be a multiple of entry size.
132  * - CurrentIndex is incremented to keep track of the last entry.
133  * - Builds the resource entry for allocated BARs.
134  */
135 static void iomm_table_allocate_entry(struct pci_dev *dev, int bar_num)
136 {
137         struct resource *bar_res = &dev->resource[bar_num];
138         long bar_size = pci_resource_len(dev, bar_num);
139
140         /*
141          * No space to allocate, quick exit, skip Allocation.
142          */
143         if (bar_size == 0)
144                 return;
145         /*
146          * Set Resource values.
147          */
148         spin_lock(&iomm_table_lock);
149         bar_res->name = pci_io_text;
150         bar_res->start =
151                 IOMM_TABLE_ENTRY_SIZE * current_iomm_table_entry;
152         bar_res->start += BASE_IO_MEMORY;
153         bar_res->end = bar_res->start + bar_size - 1;
154         /*
155          * Allocate the number of table entries needed for BAR.
156          */
157         while (bar_size > 0 ) {
158                 iomm_table[current_iomm_table_entry] = dev->sysdata;
159                 iobar_table[current_iomm_table_entry] = bar_num;
160                 bar_size -= IOMM_TABLE_ENTRY_SIZE;
161                 ++current_iomm_table_entry;
162         }
163         max_io_memory = BASE_IO_MEMORY +
164                 (IOMM_TABLE_ENTRY_SIZE * current_iomm_table_entry);
165         spin_unlock(&iomm_table_lock);
166 }
167
168 /*
169  * allocate_device_bars
170  *
171  * - Allocates ALL pci_dev BAR's and updates the resources with the
172  *   BAR value.  BARS with zero length will have the resources
173  *   The HvCallPci_getBarParms is used to get the size of the BAR
174  *   space.  It calls iomm_table_allocate_entry to allocate
175  *   each entry.
176  * - Loops through The Bar resources(0 - 5) including the ROM
177  *   is resource(6).
178  */
179 static void allocate_device_bars(struct pci_dev *dev)
180 {
181         struct resource *bar_res;
182         int bar_num;
183
184         for (bar_num = 0; bar_num <= PCI_ROM_RESOURCE; ++bar_num) {
185                 bar_res = &dev->resource[bar_num];
186                 iomm_table_allocate_entry(dev, bar_num);
187         }
188 }
189
190 /*
191  * Log error information to system console.
192  * Filter out the device not there errors.
193  * PCI: EADs Connect Failed 0x18.58.10 Rc: 0x00xx
194  * PCI: Read Vendor Failed 0x18.58.10 Rc: 0x00xx
195  * PCI: Connect Bus Unit Failed 0x18.58.10 Rc: 0x00xx
196  */
197 static void pci_Log_Error(char *Error_Text, int Bus, int SubBus,
198                 int AgentId, int HvRc)
199 {
200         if (HvRc == 0x0302)
201                 return;
202         printk(KERN_ERR "PCI: %s Failed: 0x%02X.%02X.%02X Rc: 0x%04X",
203                Error_Text, Bus, SubBus, AgentId, HvRc);
204 }
205
206 /*
207  * build_device_node(u16 Bus, int SubBus, u8 DevFn)
208  */
209 static struct iSeries_Device_Node *build_device_node(HvBusNumber Bus,
210                 HvSubBusNumber SubBus, int AgentId, int Function)
211 {
212         struct iSeries_Device_Node *node;
213
214         PPCDBG(PPCDBG_BUSWALK,
215                         "-build_device_node 0x%02X.%02X.%02X Function: %02X\n",
216                         Bus, SubBus, AgentId, Function);
217
218         node = kmalloc(sizeof(struct iSeries_Device_Node), GFP_KERNEL);
219         if (node == NULL)
220                 return NULL;
221
222         memset(node, 0, sizeof(struct iSeries_Device_Node));
223         list_add_tail(&node->Device_List, &iSeries_Global_Device_List);
224 #if 0
225         node->DsaAddr = ((u64)Bus << 48) + ((u64)SubBus << 40) + ((u64)0x10 << 32);
226 #endif
227         node->DsaAddr.DsaAddr = 0;
228         node->DsaAddr.Dsa.busNumber = Bus;
229         node->DsaAddr.Dsa.subBusNumber = SubBus;
230         node->DsaAddr.Dsa.deviceId = 0x10;
231         node->AgentId = AgentId;
232         node->DevFn = PCI_DEVFN(ISERIES_ENCODE_DEVICE(AgentId), Function);
233         node->IoRetry = 0;
234         iSeries_Get_Location_Code(node);
235         return node;
236 }
237
238 /*
239  * unsigned long __init find_and_init_phbs(void)
240  *
241  * Description:
242  *   This function checks for all possible system PCI host bridges that connect
243  *   PCI buses.  The system hypervisor is queried as to the guest partition
244  *   ownership status.  A pci_controller is built for any bus which is partially
245  *   owned or fully owned by this guest partition.
246  */
247 unsigned long __init find_and_init_phbs(void)
248 {
249         struct pci_controller *phb;
250         HvBusNumber bus;
251
252         PPCDBG(PPCDBG_BUSWALK, "find_and_init_phbs Entry\n");
253
254         /* Check all possible buses. */
255         for (bus = 0; bus < 256; bus++) {
256                 int ret = HvCallXm_testBus(bus);
257                 if (ret == 0) {
258                         printk("bus %d appears to exist\n", bus);
259
260                         phb = (struct pci_controller *)kmalloc(sizeof(struct pci_controller), GFP_KERNEL);
261                         if (phb == NULL)
262                                 return -ENOMEM;
263                         pci_setup_pci_controller(phb);
264
265                         phb->pci_mem_offset = phb->local_number = bus;
266                         phb->first_busno = bus;
267                         phb->last_busno = bus;
268                         phb->ops = &iSeries_pci_ops;
269
270                         PPCDBG(PPCDBG_BUSWALK, "PCI:Create iSeries pci_controller(%p), Bus: %04X\n",
271                                         phb, bus);
272
273                         /* Find and connect the devices. */
274                         scan_PHB_slots(phb);
275                 }
276                 /*
277                  * Check for Unexpected Return code, a clue that something
278                  * has gone wrong.
279                  */
280                 else if (ret != 0x0301)
281                         printk(KERN_ERR "Unexpected Return on Probe(0x%04X): 0x%04X",
282                                bus, ret);
283         }
284         return 0;
285 }
286
287 /*
288  * iSeries_pcibios_init
289  *  
290  * Chance to initialize and structures or variable before PCI Bus walk.
291  */
292 void iSeries_pcibios_init(void)
293 {
294         PPCDBG(PPCDBG_BUSWALK, "iSeries_pcibios_init Entry.\n"); 
295         iomm_table_initialize();
296         find_and_init_phbs();
297         io_page_mask = -1;
298         PPCDBG(PPCDBG_BUSWALK, "iSeries_pcibios_init Exit.\n"); 
299 }
300
301 /*
302  * iSeries_pci_final_fixup(void)  
303  */
304 void __init iSeries_pci_final_fixup(void)
305 {
306         struct pci_dev *pdev = NULL;
307         struct iSeries_Device_Node *node;
308         char Buffer[256];
309         int DeviceCount = 0;
310
311         PPCDBG(PPCDBG_BUSWALK, "iSeries_pcibios_fixup Entry.\n"); 
312
313         /* Fix up at the device node and pci_dev relationship */
314         mf_display_src(0xC9000100);
315
316         printk("pcibios_final_fixup\n");
317         for_each_pci_dev(pdev) {
318                 node = find_Device_Node(pdev->bus->number, pdev->devfn);
319                 printk("pci dev %p (%x.%x), node %p\n", pdev,
320                        pdev->bus->number, pdev->devfn, node);
321
322                 if (node != NULL) {
323                         ++DeviceCount;
324                         pdev->sysdata = (void *)node;
325                         node->PciDev = pdev;
326                         PPCDBG(PPCDBG_BUSWALK,
327                                         "pdev 0x%p <==> DevNode 0x%p\n",
328                                         pdev, node);
329                         allocate_device_bars(pdev);
330                         iSeries_Device_Information(pdev, Buffer,
331                                         sizeof(Buffer));
332                         printk("%d. %s\n", DeviceCount, Buffer);
333                         iommu_devnode_init_iSeries(node);
334                 } else
335                         printk("PCI: Device Tree not found for 0x%016lX\n",
336                                         (unsigned long)pdev);
337                 pdev->irq = node->Irq;
338         }
339         iSeries_activate_IRQs();
340         mf_display_src(0xC9000200);
341 }
342
343 void pcibios_fixup_bus(struct pci_bus *PciBus)
344 {
345         PPCDBG(PPCDBG_BUSWALK, "iSeries_pcibios_fixup_bus(0x%04X) Entry.\n",
346                         PciBus->number); 
347 }
348
349 void pcibios_fixup_resources(struct pci_dev *pdev)
350 {
351         PPCDBG(PPCDBG_BUSWALK, "fixup_resources pdev %p\n", pdev);
352 }   
353
354 /*
355  * Loop through each node function to find usable EADs bridges.  
356  */
357 static void scan_PHB_slots(struct pci_controller *Phb)
358 {
359         struct HvCallPci_DeviceInfo *DevInfo;
360         HvBusNumber bus = Phb->local_number;    /* System Bus */        
361         const HvSubBusNumber SubBus = 0;        /* EADs is always 0. */
362         int HvRc = 0;
363         int IdSel;      
364         const int MaxAgents = 8;
365
366         DevInfo = (struct HvCallPci_DeviceInfo*)
367                 kmalloc(sizeof(struct HvCallPci_DeviceInfo), GFP_KERNEL);
368         if (DevInfo == NULL)
369                 return;
370
371         /*
372          * Probe for EADs Bridges      
373          */
374         for (IdSel = 1; IdSel < MaxAgents; ++IdSel) {
375                 HvRc = HvCallPci_getDeviceInfo(bus, SubBus, IdSel,
376                                 ISERIES_HV_ADDR(DevInfo),
377                                 sizeof(struct HvCallPci_DeviceInfo));
378                 if (HvRc == 0) {
379                         if (DevInfo->deviceType == HvCallPci_NodeDevice)
380                                 scan_EADS_bridge(bus, SubBus, IdSel);
381                         else
382                                 printk("PCI: Invalid System Configuration(0x%02X)"
383                                        " for bus 0x%02x id 0x%02x.\n",
384                                        DevInfo->deviceType, bus, IdSel);
385                 }
386                 else
387                         pci_Log_Error("getDeviceInfo", bus, SubBus, IdSel, HvRc);
388         }
389         kfree(DevInfo);
390 }
391
392 static void scan_EADS_bridge(HvBusNumber bus, HvSubBusNumber SubBus,
393                 int IdSel)
394 {
395         struct HvCallPci_BridgeInfo *BridgeInfo;
396         HvAgentId AgentId;
397         int Function;
398         int HvRc;
399
400         BridgeInfo = (struct HvCallPci_BridgeInfo *)
401                 kmalloc(sizeof(struct HvCallPci_BridgeInfo), GFP_KERNEL);
402         if (BridgeInfo == NULL)
403                 return;
404
405         /* Note: hvSubBus and irq is always be 0 at this level! */
406         for (Function = 0; Function < 8; ++Function) {
407                 AgentId = ISERIES_PCI_AGENTID(IdSel, Function);
408                 HvRc = HvCallXm_connectBusUnit(bus, SubBus, AgentId, 0);
409                 if (HvRc == 0) {
410                         printk("found device at bus %d idsel %d func %d (AgentId %x)\n",
411                                bus, IdSel, Function, AgentId);
412                         /*  Connect EADs: 0x18.00.12 = 0x00 */
413                         PPCDBG(PPCDBG_BUSWALK,
414                                         "PCI:Connect EADs: 0x%02X.%02X.%02X\n",
415                                         bus, SubBus, AgentId);
416                         HvRc = HvCallPci_getBusUnitInfo(bus, SubBus, AgentId,
417                                         ISERIES_HV_ADDR(BridgeInfo),
418                                         sizeof(struct HvCallPci_BridgeInfo));
419                         if (HvRc == 0) {
420                                 printk("bridge info: type %x subbus %x maxAgents %x maxsubbus %x logslot %x\n",
421                                         BridgeInfo->busUnitInfo.deviceType,
422                                         BridgeInfo->subBusNumber,
423                                         BridgeInfo->maxAgents,
424                                         BridgeInfo->maxSubBusNumber,
425                                         BridgeInfo->logicalSlotNumber);
426                                 PPCDBG(PPCDBG_BUSWALK,
427                                         "PCI: BridgeInfo, Type:0x%02X, SubBus:0x%02X, MaxAgents:0x%02X, MaxSubBus: 0x%02X, LSlot: 0x%02X\n",
428                                         BridgeInfo->busUnitInfo.deviceType,
429                                         BridgeInfo->subBusNumber,
430                                         BridgeInfo->maxAgents,
431                                         BridgeInfo->maxSubBusNumber,
432                                         BridgeInfo->logicalSlotNumber);
433
434                                 if (BridgeInfo->busUnitInfo.deviceType ==
435                                                 HvCallPci_BridgeDevice)  {
436                                         /* Scan_Bridge_Slot...: 0x18.00.12 */
437                                         scan_bridge_slot(bus, BridgeInfo);
438                                 } else
439                                         printk("PCI: Invalid Bridge Configuration(0x%02X)",
440                                                 BridgeInfo->busUnitInfo.deviceType);
441                         }
442                 } else if (HvRc != 0x000B)
443                         pci_Log_Error("EADs Connect",
444                                         bus, SubBus, AgentId, HvRc);
445         }
446         kfree(BridgeInfo);
447 }
448
449 /*
450  * This assumes that the node slot is always on the primary bus!
451  */
452 static int scan_bridge_slot(HvBusNumber Bus,
453                 struct HvCallPci_BridgeInfo *BridgeInfo)
454 {
455         struct iSeries_Device_Node *node;
456         HvSubBusNumber SubBus = BridgeInfo->subBusNumber;
457         u16 VendorId = 0;
458         int HvRc = 0;
459         u8 Irq = 0;
460         int IdSel = ISERIES_GET_DEVICE_FROM_SUBBUS(SubBus);
461         int Function = ISERIES_GET_FUNCTION_FROM_SUBBUS(SubBus);
462         HvAgentId EADsIdSel = ISERIES_PCI_AGENTID(IdSel, Function);
463
464         /* iSeries_allocate_IRQ.: 0x18.00.12(0xA3) */
465         Irq = iSeries_allocate_IRQ(Bus, 0, EADsIdSel);
466         PPCDBG(PPCDBG_BUSWALK,
467                 "PCI:- allocate and assign IRQ 0x%02X.%02X.%02X = 0x%02X\n",
468                 Bus, 0, EADsIdSel, Irq);
469
470         /*
471          * Connect all functions of any device found.  
472          */
473         for (IdSel = 1; IdSel <= BridgeInfo->maxAgents; ++IdSel) {
474                 for (Function = 0; Function < 8; ++Function) {
475                         HvAgentId AgentId = ISERIES_PCI_AGENTID(IdSel, Function);
476                         HvRc = HvCallXm_connectBusUnit(Bus, SubBus,
477                                         AgentId, Irq);
478                         if (HvRc != 0) {
479                                 pci_Log_Error("Connect Bus Unit",
480                                               Bus, SubBus, AgentId, HvRc);
481                                 continue;
482                         }
483
484                         HvRc = HvCallPci_configLoad16(Bus, SubBus, AgentId,
485                                                       PCI_VENDOR_ID, &VendorId);
486                         if (HvRc != 0) {
487                                 pci_Log_Error("Read Vendor",
488                                               Bus, SubBus, AgentId, HvRc);
489                                 continue;
490                         }
491                         printk("read vendor ID: %x\n", VendorId);
492
493                         /* FoundDevice: 0x18.28.10 = 0x12AE */
494                         PPCDBG(PPCDBG_BUSWALK,
495                                "PCI:- FoundDevice: 0x%02X.%02X.%02X = 0x%04X, irq %d\n",
496                                Bus, SubBus, AgentId, VendorId, Irq);
497                         HvRc = HvCallPci_configStore8(Bus, SubBus, AgentId,
498                                                       PCI_INTERRUPT_LINE, Irq);  
499                         if (HvRc != 0)
500                                 pci_Log_Error("PciCfgStore Irq Failed!",
501                                               Bus, SubBus, AgentId, HvRc);
502
503                         ++DeviceCount;
504                         node = build_device_node(Bus, SubBus, EADsIdSel, Function);
505                         node->Vendor = VendorId;
506                         node->Irq = Irq;
507                         node->LogicalSlot = BridgeInfo->logicalSlotNumber;
508
509                 } /* for (Function = 0; Function < 8; ++Function) */
510         } /* for (IdSel = 1; IdSel <= MaxAgents; ++IdSel) */
511         return HvRc;
512 }
513
514 /*
515  * I/0 Memory copy MUST use mmio commands on iSeries
516  * To do; For performance, include the hv call directly
517  */
518 void iSeries_memset_io(volatile void __iomem *dest, char c, size_t Count)
519 {
520         u8 ByteValue = c;
521         long NumberOfBytes = Count;
522
523         while (NumberOfBytes > 0) {
524                 iSeries_Write_Byte(ByteValue, dest++);
525                 -- NumberOfBytes;
526         }
527 }
528 EXPORT_SYMBOL(iSeries_memset_io);
529
530 void iSeries_memcpy_toio(volatile void __iomem *dest, void *source, size_t count)
531 {
532         char *src = source;
533         long NumberOfBytes = count;
534
535         while (NumberOfBytes > 0) {
536                 iSeries_Write_Byte(*src++, dest++);
537                 -- NumberOfBytes;
538         }
539 }
540 EXPORT_SYMBOL(iSeries_memcpy_toio);
541
542 void iSeries_memcpy_fromio(void *dest, const volatile void __iomem *src, size_t count)
543 {
544         char *dst = dest;
545         long NumberOfBytes = count;
546
547         while (NumberOfBytes > 0) {
548                 *dst++ = iSeries_Read_Byte(src++);
549                 -- NumberOfBytes;
550         }
551 }
552 EXPORT_SYMBOL(iSeries_memcpy_fromio);
553
554 /*
555  * Look down the chain to find the matching Device Device
556  */
557 static struct iSeries_Device_Node *find_Device_Node(int bus, int devfn)
558 {
559         struct list_head *pos;
560
561         list_for_each(pos, &iSeries_Global_Device_List) {
562                 struct iSeries_Device_Node *node =
563                         list_entry(pos, struct iSeries_Device_Node, Device_List);
564
565                 if ((bus == ISERIES_BUS(node)) && (devfn == node->DevFn))
566                         return node;
567         }
568         return NULL;
569 }
570
571 #if 0
572 /*
573  * Returns the device node for the passed pci_dev
574  * Sanity Check Node PciDev to passed pci_dev
575  * If none is found, returns a NULL which the client must handle.
576  */
577 static struct iSeries_Device_Node *get_Device_Node(struct pci_dev *pdev)
578 {
579         struct iSeries_Device_Node *node;
580
581         node = pdev->sysdata;
582         if (node == NULL || node->PciDev != pdev)
583                 node = find_Device_Node(pdev->bus->number, pdev->devfn);
584         return node;
585 }
586 #endif
587
588 /*
589  * Config space read and write functions.
590  * For now at least, we look for the device node for the bus and devfn
591  * that we are asked to access.  It may be possible to translate the devfn
592  * to a subbus and deviceid more directly.
593  */
594 static u64 hv_cfg_read_func[4]  = {
595         HvCallPciConfigLoad8, HvCallPciConfigLoad16,
596         HvCallPciConfigLoad32, HvCallPciConfigLoad32
597 };
598
599 static u64 hv_cfg_write_func[4] = {
600         HvCallPciConfigStore8, HvCallPciConfigStore16,
601         HvCallPciConfigStore32, HvCallPciConfigStore32
602 };
603
604 /*
605  * Read PCI config space
606  */
607 static int iSeries_pci_read_config(struct pci_bus *bus, unsigned int devfn,
608                 int offset, int size, u32 *val)
609 {
610         struct iSeries_Device_Node *node = find_Device_Node(bus->number, devfn);
611         u64 fn;
612         struct HvCallPci_LoadReturn ret;
613
614         if (node == NULL)
615                 return PCIBIOS_DEVICE_NOT_FOUND;
616
617         fn = hv_cfg_read_func[(size - 1) & 3];
618         HvCall3Ret16(fn, &ret, node->DsaAddr.DsaAddr, offset, 0);
619
620         if (ret.rc != 0) {
621                 *val = ~0;
622                 return PCIBIOS_DEVICE_NOT_FOUND;        /* or something */
623         }
624
625         *val = ret.value;
626         return 0;
627 }
628
629 /*
630  * Write PCI config space
631  */
632
633 static int iSeries_pci_write_config(struct pci_bus *bus, unsigned int devfn,
634                 int offset, int size, u32 val)
635 {
636         struct iSeries_Device_Node *node = find_Device_Node(bus->number, devfn);
637         u64 fn;
638         u64 ret;
639
640         if (node == NULL)
641                 return PCIBIOS_DEVICE_NOT_FOUND;
642
643         fn = hv_cfg_write_func[(size - 1) & 3];
644         ret = HvCall4(fn, node->DsaAddr.DsaAddr, offset, val, 0);
645
646         if (ret != 0)
647                 return PCIBIOS_DEVICE_NOT_FOUND;
648
649         return 0;
650 }
651
652 static struct pci_ops iSeries_pci_ops = {
653         .read = iSeries_pci_read_config,
654         .write = iSeries_pci_write_config
655 };
656
657 /*
658  * Check Return Code
659  * -> On Failure, print and log information.
660  *    Increment Retry Count, if exceeds max, panic partition.
661  * -> If in retry, print and log success 
662  *
663  * PCI: Device 23.90 ReadL I/O Error( 0): 0x1234
664  * PCI: Device 23.90 ReadL Retry( 1)
665  * PCI: Device 23.90 ReadL Retry Successful(1)
666  */
667 static int CheckReturnCode(char *TextHdr, struct iSeries_Device_Node *DevNode,
668                 u64 ret)
669 {
670         if (ret != 0)  {
671                 ++Pci_Error_Count;
672                 ++DevNode->IoRetry;
673                 printk("PCI: %s: Device 0x%04X:%02X  I/O Error(%2d): 0x%04X\n",
674                                 TextHdr, DevNode->DsaAddr.Dsa.busNumber, DevNode->DevFn,
675                                 DevNode->IoRetry, (int)ret);
676                 /*
677                  * Bump the retry and check for retry count exceeded.
678                  * If, Exceeded, panic the system.
679                  */
680                 if ((DevNode->IoRetry > Pci_Retry_Max) &&
681                                 (Pci_Error_Flag > 0)) {
682                         mf_display_src(0xB6000103);
683                         panic_timeout = 0; 
684                         panic("PCI: Hardware I/O Error, SRC B6000103, "
685                                         "Automatic Reboot Disabled.\n");
686                 }
687                 return -1;      /* Retry Try */
688         }
689         /* If retry was in progress, log success and rest retry count */
690         if (DevNode->IoRetry > 0)
691                 DevNode->IoRetry = 0;
692         return 0; 
693 }
694
695 /*
696  * Translate the I/O Address into a device node, bar, and bar offset.
697  * Note: Make sure the passed variable end up on the stack to avoid
698  * the exposure of being device global.
699  */
700 static inline struct iSeries_Device_Node *xlate_iomm_address(
701                 const volatile void __iomem *IoAddress,
702                 u64 *dsaptr, u64 *BarOffsetPtr)
703 {
704         unsigned long OrigIoAddr;
705         unsigned long BaseIoAddr;
706         unsigned long TableIndex;
707         struct iSeries_Device_Node *DevNode;
708
709         OrigIoAddr = (unsigned long __force)IoAddress;
710         if ((OrigIoAddr < BASE_IO_MEMORY) || (OrigIoAddr >= max_io_memory))
711                 return NULL;
712         BaseIoAddr = OrigIoAddr - BASE_IO_MEMORY;
713         TableIndex = BaseIoAddr / IOMM_TABLE_ENTRY_SIZE;
714         DevNode = iomm_table[TableIndex];
715
716         if (DevNode != NULL) {
717                 int barnum = iobar_table[TableIndex];
718                 *dsaptr = DevNode->DsaAddr.DsaAddr | (barnum << 24);
719                 *BarOffsetPtr = BaseIoAddr % IOMM_TABLE_ENTRY_SIZE;
720         } else
721                 panic("PCI: Invalid PCI IoAddress detected!\n");
722         return DevNode;
723 }
724
725 /*
726  * Read MM I/O Instructions for the iSeries
727  * On MM I/O error, all ones are returned and iSeries_pci_IoError is cal
728  * else, data is returned in big Endian format.
729  *
730  * iSeries_Read_Byte = Read Byte  ( 8 bit)
731  * iSeries_Read_Word = Read Word  (16 bit)
732  * iSeries_Read_Long = Read Long  (32 bit)
733  */
734 u8 iSeries_Read_Byte(const volatile void __iomem *IoAddress)
735 {
736         u64 BarOffset;
737         u64 dsa;
738         struct HvCallPci_LoadReturn ret;
739         struct iSeries_Device_Node *DevNode =
740                 xlate_iomm_address(IoAddress, &dsa, &BarOffset);
741
742         if (DevNode == NULL) {
743                 static unsigned long last_jiffies;
744                 static int num_printed;
745
746                 if ((jiffies - last_jiffies) > 60 * HZ) {
747                         last_jiffies = jiffies;
748                         num_printed = 0;
749                 }
750                 if (num_printed++ < 10)
751                         printk(KERN_ERR "iSeries_Read_Byte: invalid access at IO address %p\n", IoAddress);
752                 return 0xff;
753         }
754         do {
755                 ++Pci_Io_Read_Count;
756                 HvCall3Ret16(HvCallPciBarLoad8, &ret, dsa, BarOffset, 0);
757         } while (CheckReturnCode("RDB", DevNode, ret.rc) != 0);
758
759         return (u8)ret.value;
760 }
761 EXPORT_SYMBOL(iSeries_Read_Byte);
762
763 u16 iSeries_Read_Word(const volatile void __iomem *IoAddress)
764 {
765         u64 BarOffset;
766         u64 dsa;
767         struct HvCallPci_LoadReturn ret;
768         struct iSeries_Device_Node *DevNode =
769                 xlate_iomm_address(IoAddress, &dsa, &BarOffset);
770
771         if (DevNode == NULL) {
772                 static unsigned long last_jiffies;
773                 static int num_printed;
774
775                 if ((jiffies - last_jiffies) > 60 * HZ) {
776                         last_jiffies = jiffies;
777                         num_printed = 0;
778                 }
779                 if (num_printed++ < 10)
780                         printk(KERN_ERR "iSeries_Read_Word: invalid access at IO address %p\n", IoAddress);
781                 return 0xffff;
782         }
783         do {
784                 ++Pci_Io_Read_Count;
785                 HvCall3Ret16(HvCallPciBarLoad16, &ret, dsa,
786                                 BarOffset, 0);
787         } while (CheckReturnCode("RDW", DevNode, ret.rc) != 0);
788
789         return swab16((u16)ret.value);
790 }
791 EXPORT_SYMBOL(iSeries_Read_Word);
792
793 u32 iSeries_Read_Long(const volatile void __iomem *IoAddress)
794 {
795         u64 BarOffset;
796         u64 dsa;
797         struct HvCallPci_LoadReturn ret;
798         struct iSeries_Device_Node *DevNode =
799                 xlate_iomm_address(IoAddress, &dsa, &BarOffset);
800
801         if (DevNode == NULL) {
802                 static unsigned long last_jiffies;
803                 static int num_printed;
804
805                 if ((jiffies - last_jiffies) > 60 * HZ) {
806                         last_jiffies = jiffies;
807                         num_printed = 0;
808                 }
809                 if (num_printed++ < 10)
810                         printk(KERN_ERR "iSeries_Read_Long: invalid access at IO address %p\n", IoAddress);
811                 return 0xffffffff;
812         }
813         do {
814                 ++Pci_Io_Read_Count;
815                 HvCall3Ret16(HvCallPciBarLoad32, &ret, dsa,
816                                 BarOffset, 0);
817         } while (CheckReturnCode("RDL", DevNode, ret.rc) != 0);
818
819         return swab32((u32)ret.value);
820 }
821 EXPORT_SYMBOL(iSeries_Read_Long);
822
823 /*
824  * Write MM I/O Instructions for the iSeries
825  *
826  * iSeries_Write_Byte = Write Byte (8 bit)
827  * iSeries_Write_Word = Write Word(16 bit)
828  * iSeries_Write_Long = Write Long(32 bit)
829  */
830 void iSeries_Write_Byte(u8 data, volatile void __iomem *IoAddress)
831 {
832         u64 BarOffset;
833         u64 dsa;
834         u64 rc;
835         struct iSeries_Device_Node *DevNode =
836                 xlate_iomm_address(IoAddress, &dsa, &BarOffset);
837
838         if (DevNode == NULL) {
839                 static unsigned long last_jiffies;
840                 static int num_printed;
841
842                 if ((jiffies - last_jiffies) > 60 * HZ) {
843                         last_jiffies = jiffies;
844                         num_printed = 0;
845                 }
846                 if (num_printed++ < 10)
847                         printk(KERN_ERR "iSeries_Write_Byte: invalid access at IO address %p\n", IoAddress);
848                 return;
849         }
850         do {
851                 ++Pci_Io_Write_Count;
852                 rc = HvCall4(HvCallPciBarStore8, dsa, BarOffset, data, 0);
853         } while (CheckReturnCode("WWB", DevNode, rc) != 0);
854 }
855 EXPORT_SYMBOL(iSeries_Write_Byte);
856
857 void iSeries_Write_Word(u16 data, volatile void __iomem *IoAddress)
858 {
859         u64 BarOffset;
860         u64 dsa;
861         u64 rc;
862         struct iSeries_Device_Node *DevNode =
863                 xlate_iomm_address(IoAddress, &dsa, &BarOffset);
864
865         if (DevNode == NULL) {
866                 static unsigned long last_jiffies;
867                 static int num_printed;
868
869                 if ((jiffies - last_jiffies) > 60 * HZ) {
870                         last_jiffies = jiffies;
871                         num_printed = 0;
872                 }
873                 if (num_printed++ < 10)
874                         printk(KERN_ERR "iSeries_Write_Word: invalid access at IO address %p\n", IoAddress);
875                 return;
876         }
877         do {
878                 ++Pci_Io_Write_Count;
879                 rc = HvCall4(HvCallPciBarStore16, dsa, BarOffset, swab16(data), 0);
880         } while (CheckReturnCode("WWW", DevNode, rc) != 0);
881 }
882 EXPORT_SYMBOL(iSeries_Write_Word);
883
884 void iSeries_Write_Long(u32 data, volatile void __iomem *IoAddress)
885 {
886         u64 BarOffset;
887         u64 dsa;
888         u64 rc;
889         struct iSeries_Device_Node *DevNode =
890                 xlate_iomm_address(IoAddress, &dsa, &BarOffset);
891
892         if (DevNode == NULL) {
893                 static unsigned long last_jiffies;
894                 static int num_printed;
895
896                 if ((jiffies - last_jiffies) > 60 * HZ) {
897                         last_jiffies = jiffies;
898                         num_printed = 0;
899                 }
900                 if (num_printed++ < 10)
901                         printk(KERN_ERR "iSeries_Write_Long: invalid access at IO address %p\n", IoAddress);
902                 return;
903         }
904         do {
905                 ++Pci_Io_Write_Count;
906                 rc = HvCall4(HvCallPciBarStore32, dsa, BarOffset, swab32(data), 0);
907         } while (CheckReturnCode("WWL", DevNode, rc) != 0);
908 }
909 EXPORT_SYMBOL(iSeries_Write_Long);