iSeries_pci.c 24 KB

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