1adbb3901SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 2cd248341SJan Glauber /* 3cd248341SJan Glauber * Copyright IBM Corp. 2012 4cd248341SJan Glauber * 5cd248341SJan Glauber * Author(s): 6cd248341SJan Glauber * Jan Glauber <jang@linux.vnet.ibm.com> 7cd248341SJan Glauber * 8cd248341SJan Glauber * The System z PCI code is a rewrite from a prototype by 9cd248341SJan Glauber * the following people (Kudoz!): 10bedef755SJan Glauber * Alexander Schmidt 11bedef755SJan Glauber * Christoph Raisch 12bedef755SJan Glauber * Hannes Hering 13bedef755SJan Glauber * Hoang-Nam Nguyen 14bedef755SJan Glauber * Jan-Bernd Themann 15bedef755SJan Glauber * Stefan Roscher 16bedef755SJan Glauber * Thomas Klein 17cd248341SJan Glauber */ 18cd248341SJan Glauber 19896cb7e6SGerald Schaefer #define KMSG_COMPONENT "zpci" 20896cb7e6SGerald Schaefer #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt 21cd248341SJan Glauber 22cd248341SJan Glauber #include <linux/kernel.h> 23cd248341SJan Glauber #include <linux/slab.h> 24cd248341SJan Glauber #include <linux/err.h> 25cd248341SJan Glauber #include <linux/export.h> 26cd248341SJan Glauber #include <linux/delay.h> 27cd248341SJan Glauber #include <linux/seq_file.h> 2871ba41c9SSebastian Ott #include <linux/jump_label.h> 29cd248341SJan Glauber #include <linux/pci.h> 30*794b8846SNiklas Schnelle #include <linux/printk.h> 31cd248341SJan Glauber 329a4da8a5SJan Glauber #include <asm/isc.h> 339a4da8a5SJan Glauber #include <asm/airq.h> 34cd248341SJan Glauber #include <asm/facility.h> 35cd248341SJan Glauber #include <asm/pci_insn.h> 36a755a45dSJan Glauber #include <asm/pci_clp.h> 37828b35f6SJan Glauber #include <asm/pci_dma.h> 38cd248341SJan Glauber 39cd248341SJan Glauber /* list of all detected zpci devices */ 4067f43f38SSebastian Ott static LIST_HEAD(zpci_list); 4157b5918cSSebastian Ott static DEFINE_SPINLOCK(zpci_list_lock); 421f44a225SMartin Schwidefsky 43cd248341SJan Glauber static DECLARE_BITMAP(zpci_domain, ZPCI_NR_DEVICES); 44cd248341SJan Glauber static DEFINE_SPINLOCK(zpci_domain_lock); 45cd248341SJan Glauber 46c506fff3SSebastian Ott #define ZPCI_IOMAP_ENTRIES \ 47b0c8ce89SSebastian Ott min(((unsigned long) ZPCI_NR_DEVICES * PCI_BAR_COUNT / 2), \ 48c506fff3SSebastian Ott ZPCI_IOMAP_MAX_ENTRIES) 49c506fff3SSebastian Ott 50cd248341SJan Glauber static DEFINE_SPINLOCK(zpci_iomap_lock); 51c506fff3SSebastian Ott static unsigned long *zpci_iomap_bitmap; 52cd248341SJan Glauber struct zpci_iomap_entry *zpci_iomap_start; 53cd248341SJan Glauber EXPORT_SYMBOL_GPL(zpci_iomap_start); 54cd248341SJan Glauber 5571ba41c9SSebastian Ott DEFINE_STATIC_KEY_FALSE(have_mio); 5671ba41c9SSebastian Ott 57d0b08853SJan Glauber static struct kmem_cache *zdev_fmb_cache; 58d0b08853SJan Glauber 59cd248341SJan Glauber struct zpci_dev *get_zdev_by_fid(u32 fid) 60cd248341SJan Glauber { 61cd248341SJan Glauber struct zpci_dev *tmp, *zdev = NULL; 62cd248341SJan Glauber 6357b5918cSSebastian Ott spin_lock(&zpci_list_lock); 64cd248341SJan Glauber list_for_each_entry(tmp, &zpci_list, entry) { 65cd248341SJan Glauber if (tmp->fid == fid) { 66cd248341SJan Glauber zdev = tmp; 67cd248341SJan Glauber break; 68cd248341SJan Glauber } 69cd248341SJan Glauber } 7057b5918cSSebastian Ott spin_unlock(&zpci_list_lock); 71cd248341SJan Glauber return zdev; 72cd248341SJan Glauber } 73cd248341SJan Glauber 7401553d9aSSebastian Ott void zpci_remove_reserved_devices(void) 7501553d9aSSebastian Ott { 7601553d9aSSebastian Ott struct zpci_dev *tmp, *zdev; 7701553d9aSSebastian Ott enum zpci_state state; 7801553d9aSSebastian Ott LIST_HEAD(remove); 7901553d9aSSebastian Ott 8001553d9aSSebastian Ott spin_lock(&zpci_list_lock); 8101553d9aSSebastian Ott list_for_each_entry_safe(zdev, tmp, &zpci_list, entry) { 8201553d9aSSebastian Ott if (zdev->state == ZPCI_FN_STATE_STANDBY && 8301553d9aSSebastian Ott !clp_get_state(zdev->fid, &state) && 8401553d9aSSebastian Ott state == ZPCI_FN_STATE_RESERVED) 8501553d9aSSebastian Ott list_move_tail(&zdev->entry, &remove); 8601553d9aSSebastian Ott } 8701553d9aSSebastian Ott spin_unlock(&zpci_list_lock); 8801553d9aSSebastian Ott 8901553d9aSSebastian Ott list_for_each_entry_safe(zdev, tmp, &remove, entry) 9001553d9aSSebastian Ott zpci_remove_device(zdev); 9101553d9aSSebastian Ott } 9201553d9aSSebastian Ott 93cd248341SJan Glauber static struct zpci_dev *get_zdev_by_bus(struct pci_bus *bus) 94cd248341SJan Glauber { 95cd248341SJan Glauber return (bus && bus->sysdata) ? (struct zpci_dev *) bus->sysdata : NULL; 96cd248341SJan Glauber } 97cd248341SJan Glauber 98cd248341SJan Glauber int pci_domain_nr(struct pci_bus *bus) 99cd248341SJan Glauber { 100cd248341SJan Glauber return ((struct zpci_dev *) bus->sysdata)->domain; 101cd248341SJan Glauber } 102cd248341SJan Glauber EXPORT_SYMBOL_GPL(pci_domain_nr); 103cd248341SJan Glauber 104cd248341SJan Glauber int pci_proc_domain(struct pci_bus *bus) 105cd248341SJan Glauber { 106cd248341SJan Glauber return pci_domain_nr(bus); 107cd248341SJan Glauber } 108cd248341SJan Glauber EXPORT_SYMBOL_GPL(pci_proc_domain); 109cd248341SJan Glauber 110828b35f6SJan Glauber /* Modify PCI: Register I/O address translation parameters */ 111828b35f6SJan Glauber int zpci_register_ioat(struct zpci_dev *zdev, u8 dmaas, 112828b35f6SJan Glauber u64 base, u64 limit, u64 iota) 113828b35f6SJan Glauber { 11472570834SSebastian Ott u64 req = ZPCI_CREATE_REQ(zdev->fh, dmaas, ZPCI_MOD_FC_REG_IOAT); 11572570834SSebastian Ott struct zpci_fib fib = {0}; 11672570834SSebastian Ott u8 status; 117828b35f6SJan Glauber 118828b35f6SJan Glauber WARN_ON_ONCE(iota & 0x3fff); 11972570834SSebastian Ott fib.pba = base; 12072570834SSebastian Ott fib.pal = limit; 12172570834SSebastian Ott fib.iota = iota | ZPCI_IOTA_RTTO_FLAG; 12272570834SSebastian Ott return zpci_mod_fc(req, &fib, &status) ? -EIO : 0; 123828b35f6SJan Glauber } 124828b35f6SJan Glauber 125828b35f6SJan Glauber /* Modify PCI: Unregister I/O address translation parameters */ 126828b35f6SJan Glauber int zpci_unregister_ioat(struct zpci_dev *zdev, u8 dmaas) 127828b35f6SJan Glauber { 12872570834SSebastian Ott u64 req = ZPCI_CREATE_REQ(zdev->fh, dmaas, ZPCI_MOD_FC_DEREG_IOAT); 12972570834SSebastian Ott struct zpci_fib fib = {0}; 13072570834SSebastian Ott u8 cc, status; 131828b35f6SJan Glauber 13272570834SSebastian Ott cc = zpci_mod_fc(req, &fib, &status); 13372570834SSebastian Ott if (cc == 3) /* Function already gone. */ 13472570834SSebastian Ott cc = 0; 13572570834SSebastian Ott return cc ? -EIO : 0; 136828b35f6SJan Glauber } 137828b35f6SJan Glauber 138d0b08853SJan Glauber /* Modify PCI: Set PCI function measurement parameters */ 139d0b08853SJan Glauber int zpci_fmb_enable_device(struct zpci_dev *zdev) 140d0b08853SJan Glauber { 1414e5bd780SSebastian Ott u64 req = ZPCI_CREATE_REQ(zdev->fh, 0, ZPCI_MOD_FC_SET_MEASURE); 1424e5bd780SSebastian Ott struct zpci_fib fib = {0}; 1434e5bd780SSebastian Ott u8 cc, status; 144d0b08853SJan Glauber 1450b7589ecSSebastian Ott if (zdev->fmb || sizeof(*zdev->fmb) < zdev->fmb_length) 146d0b08853SJan Glauber return -EINVAL; 147d0b08853SJan Glauber 14808b42124SWei Yongjun zdev->fmb = kmem_cache_zalloc(zdev_fmb_cache, GFP_KERNEL); 149d0b08853SJan Glauber if (!zdev->fmb) 150d0b08853SJan Glauber return -ENOMEM; 151d0b08853SJan Glauber WARN_ON((u64) zdev->fmb & 0xf); 152d0b08853SJan Glauber 1536001018aSSebastian Ott /* reset software counters */ 1546001018aSSebastian Ott atomic64_set(&zdev->allocated_pages, 0); 1556001018aSSebastian Ott atomic64_set(&zdev->mapped_pages, 0); 1566001018aSSebastian Ott atomic64_set(&zdev->unmapped_pages, 0); 1576001018aSSebastian Ott 1584e5bd780SSebastian Ott fib.fmb_addr = virt_to_phys(zdev->fmb); 1594e5bd780SSebastian Ott cc = zpci_mod_fc(req, &fib, &status); 1604e5bd780SSebastian Ott if (cc) { 1614e5bd780SSebastian Ott kmem_cache_free(zdev_fmb_cache, zdev->fmb); 1624e5bd780SSebastian Ott zdev->fmb = NULL; 1634e5bd780SSebastian Ott } 1644e5bd780SSebastian Ott return cc ? -EIO : 0; 165d0b08853SJan Glauber } 166d0b08853SJan Glauber 167d0b08853SJan Glauber /* Modify PCI: Disable PCI function measurement */ 168d0b08853SJan Glauber int zpci_fmb_disable_device(struct zpci_dev *zdev) 169d0b08853SJan Glauber { 1704e5bd780SSebastian Ott u64 req = ZPCI_CREATE_REQ(zdev->fh, 0, ZPCI_MOD_FC_SET_MEASURE); 1714e5bd780SSebastian Ott struct zpci_fib fib = {0}; 1724e5bd780SSebastian Ott u8 cc, status; 173d0b08853SJan Glauber 174d0b08853SJan Glauber if (!zdev->fmb) 175d0b08853SJan Glauber return -EINVAL; 176d0b08853SJan Glauber 177d0b08853SJan Glauber /* Function measurement is disabled if fmb address is zero */ 1784e5bd780SSebastian Ott cc = zpci_mod_fc(req, &fib, &status); 1794e5bd780SSebastian Ott if (cc == 3) /* Function already gone. */ 1804e5bd780SSebastian Ott cc = 0; 181d0b08853SJan Glauber 1824e5bd780SSebastian Ott if (!cc) { 183d0b08853SJan Glauber kmem_cache_free(zdev_fmb_cache, zdev->fmb); 184d0b08853SJan Glauber zdev->fmb = NULL; 1854e5bd780SSebastian Ott } 1864e5bd780SSebastian Ott return cc ? -EIO : 0; 187d0b08853SJan Glauber } 188d0b08853SJan Glauber 189cd248341SJan Glauber static int zpci_cfg_load(struct zpci_dev *zdev, int offset, u32 *val, u8 len) 190cd248341SJan Glauber { 191cd248341SJan Glauber u64 req = ZPCI_CREATE_REQ(zdev->fh, ZPCI_PCIAS_CFGSPC, len); 192cd248341SJan Glauber u64 data; 193cd248341SJan Glauber int rc; 194cd248341SJan Glauber 19581deca12SSebastian Ott rc = __zpci_load(&data, req, offset); 196b170bad4SSebastian Ott if (!rc) { 1975064cd35SSebastian Ott data = le64_to_cpu((__force __le64) data); 1985064cd35SSebastian Ott data >>= (8 - len) * 8; 199cd248341SJan Glauber *val = (u32) data; 200b170bad4SSebastian Ott } else 201cd248341SJan Glauber *val = 0xffffffff; 202cd248341SJan Glauber return rc; 203cd248341SJan Glauber } 204cd248341SJan Glauber 205cd248341SJan Glauber static int zpci_cfg_store(struct zpci_dev *zdev, int offset, u32 val, u8 len) 206cd248341SJan Glauber { 207cd248341SJan Glauber u64 req = ZPCI_CREATE_REQ(zdev->fh, ZPCI_PCIAS_CFGSPC, len); 208cd248341SJan Glauber u64 data = val; 209cd248341SJan Glauber int rc; 210cd248341SJan Glauber 2115064cd35SSebastian Ott data <<= (8 - len) * 8; 2125064cd35SSebastian Ott data = (__force u64) cpu_to_le64(data); 21381deca12SSebastian Ott rc = __zpci_store(data, req, offset); 214cd248341SJan Glauber return rc; 215cd248341SJan Glauber } 216cd248341SJan Glauber 217cd248341SJan Glauber resource_size_t pcibios_align_resource(void *data, const struct resource *res, 218cd248341SJan Glauber resource_size_t size, 219cd248341SJan Glauber resource_size_t align) 220cd248341SJan Glauber { 221cd248341SJan Glauber return 0; 222cd248341SJan Glauber } 223cd248341SJan Glauber 22487bc359bSJan Glauber /* combine single writes by using store-block insn */ 22587bc359bSJan Glauber void __iowrite64_copy(void __iomem *to, const void *from, size_t count) 22687bc359bSJan Glauber { 22787bc359bSJan Glauber zpci_memcpy_toio(to, from, count); 22887bc359bSJan Glauber } 22987bc359bSJan Glauber 23071ba41c9SSebastian Ott void __iomem *ioremap(unsigned long ioaddr, unsigned long size) 23171ba41c9SSebastian Ott { 23271ba41c9SSebastian Ott struct vm_struct *area; 23371ba41c9SSebastian Ott unsigned long offset; 23471ba41c9SSebastian Ott 23571ba41c9SSebastian Ott if (!size) 23671ba41c9SSebastian Ott return NULL; 23771ba41c9SSebastian Ott 23871ba41c9SSebastian Ott if (!static_branch_unlikely(&have_mio)) 23971ba41c9SSebastian Ott return (void __iomem *) ioaddr; 24071ba41c9SSebastian Ott 24171ba41c9SSebastian Ott offset = ioaddr & ~PAGE_MASK; 24271ba41c9SSebastian Ott ioaddr &= PAGE_MASK; 24371ba41c9SSebastian Ott size = PAGE_ALIGN(size + offset); 24471ba41c9SSebastian Ott area = get_vm_area(size, VM_IOREMAP); 24571ba41c9SSebastian Ott if (!area) 24671ba41c9SSebastian Ott return NULL; 24771ba41c9SSebastian Ott 24871ba41c9SSebastian Ott if (ioremap_page_range((unsigned long) area->addr, 24971ba41c9SSebastian Ott (unsigned long) area->addr + size, 25071ba41c9SSebastian Ott ioaddr, PAGE_KERNEL)) { 25171ba41c9SSebastian Ott vunmap(area->addr); 25271ba41c9SSebastian Ott return NULL; 25371ba41c9SSebastian Ott } 25471ba41c9SSebastian Ott return (void __iomem *) ((unsigned long) area->addr + offset); 25571ba41c9SSebastian Ott } 25671ba41c9SSebastian Ott EXPORT_SYMBOL(ioremap); 25771ba41c9SSebastian Ott 25871ba41c9SSebastian Ott void iounmap(volatile void __iomem *addr) 25971ba41c9SSebastian Ott { 26071ba41c9SSebastian Ott if (static_branch_likely(&have_mio)) 26171ba41c9SSebastian Ott vunmap((__force void *) ((unsigned long) addr & PAGE_MASK)); 26271ba41c9SSebastian Ott } 26371ba41c9SSebastian Ott EXPORT_SYMBOL(iounmap); 26471ba41c9SSebastian Ott 265cd248341SJan Glauber /* Create a virtual mapping cookie for a PCI BAR */ 26671ba41c9SSebastian Ott static void __iomem *pci_iomap_range_fh(struct pci_dev *pdev, int bar, 26771ba41c9SSebastian Ott unsigned long offset, unsigned long max) 268cd248341SJan Glauber { 269198a5278SSebastian Ott struct zpci_dev *zdev = to_zpci(pdev); 270cd248341SJan Glauber int idx; 271cd248341SJan Glauber 272cd248341SJan Glauber idx = zdev->bars[bar].map_idx; 273cd248341SJan Glauber spin_lock(&zpci_iomap_lock); 274f5e44f82SSebastian Ott /* Detect overrun */ 275f5e44f82SSebastian Ott WARN_ON(!++zpci_iomap_start[idx].count); 276cd248341SJan Glauber zpci_iomap_start[idx].fh = zdev->fh; 277cd248341SJan Glauber zpci_iomap_start[idx].bar = bar; 278cd248341SJan Glauber spin_unlock(&zpci_iomap_lock); 279cd248341SJan Glauber 2809e00caaeSSebastian Ott return (void __iomem *) ZPCI_ADDR(idx) + offset; 281cd248341SJan Glauber } 28271ba41c9SSebastian Ott 28371ba41c9SSebastian Ott static void __iomem *pci_iomap_range_mio(struct pci_dev *pdev, int bar, 28471ba41c9SSebastian Ott unsigned long offset, 28571ba41c9SSebastian Ott unsigned long max) 28671ba41c9SSebastian Ott { 28771ba41c9SSebastian Ott unsigned long barsize = pci_resource_len(pdev, bar); 28871ba41c9SSebastian Ott struct zpci_dev *zdev = to_zpci(pdev); 28971ba41c9SSebastian Ott void __iomem *iova; 29071ba41c9SSebastian Ott 29171ba41c9SSebastian Ott iova = ioremap((unsigned long) zdev->bars[bar].mio_wt, barsize); 29271ba41c9SSebastian Ott return iova ? iova + offset : iova; 29371ba41c9SSebastian Ott } 29471ba41c9SSebastian Ott 29571ba41c9SSebastian Ott void __iomem *pci_iomap_range(struct pci_dev *pdev, int bar, 29671ba41c9SSebastian Ott unsigned long offset, unsigned long max) 29771ba41c9SSebastian Ott { 29871ba41c9SSebastian Ott if (!pci_resource_len(pdev, bar) || bar >= PCI_BAR_COUNT) 29971ba41c9SSebastian Ott return NULL; 30071ba41c9SSebastian Ott 30171ba41c9SSebastian Ott if (static_branch_likely(&have_mio)) 30271ba41c9SSebastian Ott return pci_iomap_range_mio(pdev, bar, offset, max); 30371ba41c9SSebastian Ott else 30471ba41c9SSebastian Ott return pci_iomap_range_fh(pdev, bar, offset, max); 30571ba41c9SSebastian Ott } 306d9426083SSebastian Ott EXPORT_SYMBOL(pci_iomap_range); 3078cfc99b5SMichael S. Tsirkin 3088cfc99b5SMichael S. Tsirkin void __iomem *pci_iomap(struct pci_dev *dev, int bar, unsigned long maxlen) 3098cfc99b5SMichael S. Tsirkin { 3108cfc99b5SMichael S. Tsirkin return pci_iomap_range(dev, bar, 0, maxlen); 3118cfc99b5SMichael S. Tsirkin } 3128cfc99b5SMichael S. Tsirkin EXPORT_SYMBOL(pci_iomap); 313cd248341SJan Glauber 31471ba41c9SSebastian Ott static void __iomem *pci_iomap_wc_range_mio(struct pci_dev *pdev, int bar, 31571ba41c9SSebastian Ott unsigned long offset, unsigned long max) 31671ba41c9SSebastian Ott { 31771ba41c9SSebastian Ott unsigned long barsize = pci_resource_len(pdev, bar); 31871ba41c9SSebastian Ott struct zpci_dev *zdev = to_zpci(pdev); 31971ba41c9SSebastian Ott void __iomem *iova; 32071ba41c9SSebastian Ott 32171ba41c9SSebastian Ott iova = ioremap((unsigned long) zdev->bars[bar].mio_wb, barsize); 32271ba41c9SSebastian Ott return iova ? iova + offset : iova; 32371ba41c9SSebastian Ott } 32471ba41c9SSebastian Ott 32571ba41c9SSebastian Ott void __iomem *pci_iomap_wc_range(struct pci_dev *pdev, int bar, 32671ba41c9SSebastian Ott unsigned long offset, unsigned long max) 32771ba41c9SSebastian Ott { 32871ba41c9SSebastian Ott if (!pci_resource_len(pdev, bar) || bar >= PCI_BAR_COUNT) 32971ba41c9SSebastian Ott return NULL; 33071ba41c9SSebastian Ott 33171ba41c9SSebastian Ott if (static_branch_likely(&have_mio)) 33271ba41c9SSebastian Ott return pci_iomap_wc_range_mio(pdev, bar, offset, max); 33371ba41c9SSebastian Ott else 33471ba41c9SSebastian Ott return pci_iomap_range_fh(pdev, bar, offset, max); 33571ba41c9SSebastian Ott } 33671ba41c9SSebastian Ott EXPORT_SYMBOL(pci_iomap_wc_range); 33771ba41c9SSebastian Ott 33871ba41c9SSebastian Ott void __iomem *pci_iomap_wc(struct pci_dev *dev, int bar, unsigned long maxlen) 33971ba41c9SSebastian Ott { 34071ba41c9SSebastian Ott return pci_iomap_wc_range(dev, bar, 0, maxlen); 34171ba41c9SSebastian Ott } 34271ba41c9SSebastian Ott EXPORT_SYMBOL(pci_iomap_wc); 34371ba41c9SSebastian Ott 34471ba41c9SSebastian Ott static void pci_iounmap_fh(struct pci_dev *pdev, void __iomem *addr) 345cd248341SJan Glauber { 3469e00caaeSSebastian Ott unsigned int idx = ZPCI_IDX(addr); 347cd248341SJan Glauber 348cd248341SJan Glauber spin_lock(&zpci_iomap_lock); 3498cfc99b5SMichael S. Tsirkin /* Detect underrun */ 350f5e44f82SSebastian Ott WARN_ON(!zpci_iomap_start[idx].count); 3518cfc99b5SMichael S. Tsirkin if (!--zpci_iomap_start[idx].count) { 352cd248341SJan Glauber zpci_iomap_start[idx].fh = 0; 353cd248341SJan Glauber zpci_iomap_start[idx].bar = 0; 3548cfc99b5SMichael S. Tsirkin } 355cd248341SJan Glauber spin_unlock(&zpci_iomap_lock); 356cd248341SJan Glauber } 35771ba41c9SSebastian Ott 35871ba41c9SSebastian Ott static void pci_iounmap_mio(struct pci_dev *pdev, void __iomem *addr) 35971ba41c9SSebastian Ott { 36071ba41c9SSebastian Ott iounmap(addr); 36171ba41c9SSebastian Ott } 36271ba41c9SSebastian Ott 36371ba41c9SSebastian Ott void pci_iounmap(struct pci_dev *pdev, void __iomem *addr) 36471ba41c9SSebastian Ott { 36571ba41c9SSebastian Ott if (static_branch_likely(&have_mio)) 36671ba41c9SSebastian Ott pci_iounmap_mio(pdev, addr); 36771ba41c9SSebastian Ott else 36871ba41c9SSebastian Ott pci_iounmap_fh(pdev, addr); 36971ba41c9SSebastian Ott } 370d9426083SSebastian Ott EXPORT_SYMBOL(pci_iounmap); 371cd248341SJan Glauber 372cd248341SJan Glauber static int pci_read(struct pci_bus *bus, unsigned int devfn, int where, 373cd248341SJan Glauber int size, u32 *val) 374cd248341SJan Glauber { 375cd248341SJan Glauber struct zpci_dev *zdev = get_zdev_by_bus(bus); 3762c3700bbSSebastian Ott int ret; 377cd248341SJan Glauber 378cd248341SJan Glauber if (!zdev || devfn != ZPCI_DEVFN) 3792c3700bbSSebastian Ott ret = -ENODEV; 3802c3700bbSSebastian Ott else 3812c3700bbSSebastian Ott ret = zpci_cfg_load(zdev, where, val, size); 3822c3700bbSSebastian Ott 3832c3700bbSSebastian Ott return ret; 384cd248341SJan Glauber } 385cd248341SJan Glauber 386cd248341SJan Glauber static int pci_write(struct pci_bus *bus, unsigned int devfn, int where, 387cd248341SJan Glauber int size, u32 val) 388cd248341SJan Glauber { 389cd248341SJan Glauber struct zpci_dev *zdev = get_zdev_by_bus(bus); 3902c3700bbSSebastian Ott int ret; 391cd248341SJan Glauber 392cd248341SJan Glauber if (!zdev || devfn != ZPCI_DEVFN) 3932c3700bbSSebastian Ott ret = -ENODEV; 3942c3700bbSSebastian Ott else 3952c3700bbSSebastian Ott ret = zpci_cfg_store(zdev, where, val, size); 3962c3700bbSSebastian Ott 3972c3700bbSSebastian Ott return ret; 398cd248341SJan Glauber } 399cd248341SJan Glauber 400cd248341SJan Glauber static struct pci_ops pci_root_ops = { 401cd248341SJan Glauber .read = pci_read, 402cd248341SJan Glauber .write = pci_write, 403cd248341SJan Glauber }; 404cd248341SJan Glauber 405cfbb4a7aSSebastian Ott #ifdef CONFIG_PCI_IOV 406cfbb4a7aSSebastian Ott static struct resource iov_res = { 407cfbb4a7aSSebastian Ott .name = "PCI IOV res", 408cfbb4a7aSSebastian Ott .start = 0, 409cfbb4a7aSSebastian Ott .end = -1, 410cfbb4a7aSSebastian Ott .flags = IORESOURCE_MEM, 411cfbb4a7aSSebastian Ott }; 412cfbb4a7aSSebastian Ott #endif 413cfbb4a7aSSebastian Ott 4141803ba2dSSebastian Ott static void zpci_map_resources(struct pci_dev *pdev) 415cd248341SJan Glauber { 41671ba41c9SSebastian Ott struct zpci_dev *zdev = to_zpci(pdev); 417cd248341SJan Glauber resource_size_t len; 418cd248341SJan Glauber int i; 419cd248341SJan Glauber 420cd248341SJan Glauber for (i = 0; i < PCI_BAR_COUNT; i++) { 421cd248341SJan Glauber len = pci_resource_len(pdev, i); 422cd248341SJan Glauber if (!len) 423cd248341SJan Glauber continue; 42471ba41c9SSebastian Ott 425c7ff0e91SSebastian Ott if (zpci_use_mio(zdev)) 42671ba41c9SSebastian Ott pdev->resource[i].start = 42771ba41c9SSebastian Ott (resource_size_t __force) zdev->bars[i].mio_wb; 42871ba41c9SSebastian Ott else 429c7ff0e91SSebastian Ott pdev->resource[i].start = (resource_size_t __force) 430c7ff0e91SSebastian Ott pci_iomap_range_fh(pdev, i, 0, 0); 431cd248341SJan Glauber pdev->resource[i].end = pdev->resource[i].start + len - 1; 432cd248341SJan Glauber } 433cfbb4a7aSSebastian Ott 434cfbb4a7aSSebastian Ott #ifdef CONFIG_PCI_IOV 435c4c37723SDenis Efremov for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) { 436c4c37723SDenis Efremov int bar = i + PCI_IOV_RESOURCES; 437cfbb4a7aSSebastian Ott 438c4c37723SDenis Efremov len = pci_resource_len(pdev, bar); 439cfbb4a7aSSebastian Ott if (!len) 440cfbb4a7aSSebastian Ott continue; 441c4c37723SDenis Efremov pdev->resource[bar].parent = &iov_res; 442cfbb4a7aSSebastian Ott } 443cfbb4a7aSSebastian Ott #endif 444944239c5SSebastian Ott } 445944239c5SSebastian Ott 4461803ba2dSSebastian Ott static void zpci_unmap_resources(struct pci_dev *pdev) 447944239c5SSebastian Ott { 448c7ff0e91SSebastian Ott struct zpci_dev *zdev = to_zpci(pdev); 449944239c5SSebastian Ott resource_size_t len; 450944239c5SSebastian Ott int i; 451944239c5SSebastian Ott 452c7ff0e91SSebastian Ott if (zpci_use_mio(zdev)) 45371ba41c9SSebastian Ott return; 45471ba41c9SSebastian Ott 455944239c5SSebastian Ott for (i = 0; i < PCI_BAR_COUNT; i++) { 456944239c5SSebastian Ott len = pci_resource_len(pdev, i); 457944239c5SSebastian Ott if (!len) 458944239c5SSebastian Ott continue; 459c7ff0e91SSebastian Ott pci_iounmap_fh(pdev, (void __iomem __force *) 4605b9f2081SMartin Schwidefsky pdev->resource[i].start); 461944239c5SSebastian Ott } 462944239c5SSebastian Ott } 463cd248341SJan Glauber 464cd248341SJan Glauber static int zpci_alloc_iomap(struct zpci_dev *zdev) 465cd248341SJan Glauber { 466bf19c94dSSebastian Ott unsigned long entry; 467cd248341SJan Glauber 468cd248341SJan Glauber spin_lock(&zpci_iomap_lock); 469c506fff3SSebastian Ott entry = find_first_zero_bit(zpci_iomap_bitmap, ZPCI_IOMAP_ENTRIES); 470c506fff3SSebastian Ott if (entry == ZPCI_IOMAP_ENTRIES) { 471cd248341SJan Glauber spin_unlock(&zpci_iomap_lock); 472cd248341SJan Glauber return -ENOSPC; 473cd248341SJan Glauber } 474c506fff3SSebastian Ott set_bit(entry, zpci_iomap_bitmap); 475cd248341SJan Glauber spin_unlock(&zpci_iomap_lock); 476cd248341SJan Glauber return entry; 477cd248341SJan Glauber } 478cd248341SJan Glauber 479cd248341SJan Glauber static void zpci_free_iomap(struct zpci_dev *zdev, int entry) 480cd248341SJan Glauber { 481cd248341SJan Glauber spin_lock(&zpci_iomap_lock); 482cd248341SJan Glauber memset(&zpci_iomap_start[entry], 0, sizeof(struct zpci_iomap_entry)); 483c506fff3SSebastian Ott clear_bit(entry, zpci_iomap_bitmap); 484cd248341SJan Glauber spin_unlock(&zpci_iomap_lock); 485cd248341SJan Glauber } 486cd248341SJan Glauber 4877a572a3aSSebastian Ott static struct resource *__alloc_res(struct zpci_dev *zdev, unsigned long start, 4887a572a3aSSebastian Ott unsigned long size, unsigned long flags) 4897a572a3aSSebastian Ott { 4907a572a3aSSebastian Ott struct resource *r; 4917a572a3aSSebastian Ott 4927a572a3aSSebastian Ott r = kzalloc(sizeof(*r), GFP_KERNEL); 4937a572a3aSSebastian Ott if (!r) 4947a572a3aSSebastian Ott return NULL; 4957a572a3aSSebastian Ott 4967a572a3aSSebastian Ott r->start = start; 4977a572a3aSSebastian Ott r->end = r->start + size - 1; 4987a572a3aSSebastian Ott r->flags = flags; 4997a572a3aSSebastian Ott r->name = zdev->res_name; 5007a572a3aSSebastian Ott 5017a572a3aSSebastian Ott if (request_resource(&iomem_resource, r)) { 5027a572a3aSSebastian Ott kfree(r); 5037a572a3aSSebastian Ott return NULL; 5047a572a3aSSebastian Ott } 5057a572a3aSSebastian Ott return r; 5067a572a3aSSebastian Ott } 5077a572a3aSSebastian Ott 5087a572a3aSSebastian Ott static int zpci_setup_bus_resources(struct zpci_dev *zdev, 5097a572a3aSSebastian Ott struct list_head *resources) 5107a572a3aSSebastian Ott { 5117a572a3aSSebastian Ott unsigned long addr, size, flags; 5127a572a3aSSebastian Ott struct resource *res; 5137a572a3aSSebastian Ott int i, entry; 5147a572a3aSSebastian Ott 5157a572a3aSSebastian Ott snprintf(zdev->res_name, sizeof(zdev->res_name), 5167a572a3aSSebastian Ott "PCI Bus %04x:%02x", zdev->domain, ZPCI_BUS_NR); 5177a572a3aSSebastian Ott 5187a572a3aSSebastian Ott for (i = 0; i < PCI_BAR_COUNT; i++) { 5197a572a3aSSebastian Ott if (!zdev->bars[i].size) 5207a572a3aSSebastian Ott continue; 5217a572a3aSSebastian Ott entry = zpci_alloc_iomap(zdev); 5227a572a3aSSebastian Ott if (entry < 0) 5237a572a3aSSebastian Ott return entry; 5247a572a3aSSebastian Ott zdev->bars[i].map_idx = entry; 5257a572a3aSSebastian Ott 5267a572a3aSSebastian Ott /* only MMIO is supported */ 5277a572a3aSSebastian Ott flags = IORESOURCE_MEM; 5287a572a3aSSebastian Ott if (zdev->bars[i].val & 8) 5297a572a3aSSebastian Ott flags |= IORESOURCE_PREFETCH; 5307a572a3aSSebastian Ott if (zdev->bars[i].val & 4) 5317a572a3aSSebastian Ott flags |= IORESOURCE_MEM_64; 5327a572a3aSSebastian Ott 533c7ff0e91SSebastian Ott if (zpci_use_mio(zdev)) 534dcd33b23SSebastian Ott addr = (unsigned long) zdev->bars[i].mio_wb; 535dcd33b23SSebastian Ott else 5369e00caaeSSebastian Ott addr = ZPCI_ADDR(entry); 5377a572a3aSSebastian Ott size = 1UL << zdev->bars[i].size; 5387a572a3aSSebastian Ott 5397a572a3aSSebastian Ott res = __alloc_res(zdev, addr, size, flags); 5407a572a3aSSebastian Ott if (!res) { 5417a572a3aSSebastian Ott zpci_free_iomap(zdev, entry); 5427a572a3aSSebastian Ott return -ENOMEM; 5437a572a3aSSebastian Ott } 5447a572a3aSSebastian Ott zdev->bars[i].res = res; 5457a572a3aSSebastian Ott pci_add_resource(resources, res); 5467a572a3aSSebastian Ott } 5477a572a3aSSebastian Ott 5487a572a3aSSebastian Ott return 0; 5497a572a3aSSebastian Ott } 5507a572a3aSSebastian Ott 5517a572a3aSSebastian Ott static void zpci_cleanup_bus_resources(struct zpci_dev *zdev) 5527a572a3aSSebastian Ott { 5537a572a3aSSebastian Ott int i; 5547a572a3aSSebastian Ott 5557a572a3aSSebastian Ott for (i = 0; i < PCI_BAR_COUNT; i++) { 5562b1df724SSebastian Ott if (!zdev->bars[i].size || !zdev->bars[i].res) 5577a572a3aSSebastian Ott continue; 5587a572a3aSSebastian Ott 5597a572a3aSSebastian Ott zpci_free_iomap(zdev, zdev->bars[i].map_idx); 5607a572a3aSSebastian Ott release_resource(zdev->bars[i].res); 5617a572a3aSSebastian Ott kfree(zdev->bars[i].res); 5627a572a3aSSebastian Ott } 5637a572a3aSSebastian Ott } 5647a572a3aSSebastian Ott 565af0a8a84SSebastian Ott int pcibios_add_device(struct pci_dev *pdev) 566af0a8a84SSebastian Ott { 567cb809182SSebastian Ott struct resource *res; 568cb809182SSebastian Ott int i; 569cb809182SSebastian Ott 5707dc20ab1SSebastian Ott if (pdev->is_physfn) 5717dc20ab1SSebastian Ott pdev->no_vf_scan = 1; 5727dc20ab1SSebastian Ott 573ef4858c6SSebastian Ott pdev->dev.groups = zpci_attr_groups; 5745657933dSBart Van Assche pdev->dev.dma_ops = &s390_pci_dma_ops; 5751803ba2dSSebastian Ott zpci_map_resources(pdev); 576cb809182SSebastian Ott 577cb809182SSebastian Ott for (i = 0; i < PCI_BAR_COUNT; i++) { 578cb809182SSebastian Ott res = &pdev->resource[i]; 579cb809182SSebastian Ott if (res->parent || !res->flags) 580cb809182SSebastian Ott continue; 581cb809182SSebastian Ott pci_claim_resource(pdev, i); 582cb809182SSebastian Ott } 583cb809182SSebastian Ott 584cb809182SSebastian Ott return 0; 585cb809182SSebastian Ott } 586cb809182SSebastian Ott 5871803ba2dSSebastian Ott void pcibios_release_device(struct pci_dev *pdev) 5881803ba2dSSebastian Ott { 5891803ba2dSSebastian Ott zpci_unmap_resources(pdev); 5901803ba2dSSebastian Ott } 5911803ba2dSSebastian Ott 592cb809182SSebastian Ott int pcibios_enable_device(struct pci_dev *pdev, int mask) 593cb809182SSebastian Ott { 594198a5278SSebastian Ott struct zpci_dev *zdev = to_zpci(pdev); 595af0a8a84SSebastian Ott 5969a99649fSSebastian Ott zpci_debug_init_device(zdev, dev_name(&pdev->dev)); 597af0a8a84SSebastian Ott zpci_fmb_enable_device(zdev); 598af0a8a84SSebastian Ott 599d7533232SBjorn Helgaas return pci_enable_resources(pdev, mask); 600af0a8a84SSebastian Ott } 601af0a8a84SSebastian Ott 602cb809182SSebastian Ott void pcibios_disable_device(struct pci_dev *pdev) 603944239c5SSebastian Ott { 604198a5278SSebastian Ott struct zpci_dev *zdev = to_zpci(pdev); 605944239c5SSebastian Ott 606944239c5SSebastian Ott zpci_fmb_disable_device(zdev); 607944239c5SSebastian Ott zpci_debug_exit_device(zdev); 608944239c5SSebastian Ott } 609944239c5SSebastian Ott 61069db3b5eSSebastian Ott #ifdef CONFIG_HIBERNATE_CALLBACKS 61169db3b5eSSebastian Ott static int zpci_restore(struct device *dev) 61269db3b5eSSebastian Ott { 6131803ba2dSSebastian Ott struct pci_dev *pdev = to_pci_dev(dev); 614198a5278SSebastian Ott struct zpci_dev *zdev = to_zpci(pdev); 61569db3b5eSSebastian Ott int ret = 0; 61669db3b5eSSebastian Ott 61769db3b5eSSebastian Ott if (zdev->state != ZPCI_FN_STATE_ONLINE) 61869db3b5eSSebastian Ott goto out; 61969db3b5eSSebastian Ott 62069db3b5eSSebastian Ott ret = clp_enable_fh(zdev, ZPCI_NR_DMA_SPACES); 62169db3b5eSSebastian Ott if (ret) 62269db3b5eSSebastian Ott goto out; 62369db3b5eSSebastian Ott 6241803ba2dSSebastian Ott zpci_map_resources(pdev); 62569eea95cSGerald Schaefer zpci_register_ioat(zdev, 0, zdev->start_dma, zdev->end_dma, 62669db3b5eSSebastian Ott (u64) zdev->dma_table); 62769db3b5eSSebastian Ott 62869db3b5eSSebastian Ott out: 62969db3b5eSSebastian Ott return ret; 63069db3b5eSSebastian Ott } 63169db3b5eSSebastian Ott 63269db3b5eSSebastian Ott static int zpci_freeze(struct device *dev) 63369db3b5eSSebastian Ott { 6341803ba2dSSebastian Ott struct pci_dev *pdev = to_pci_dev(dev); 635198a5278SSebastian Ott struct zpci_dev *zdev = to_zpci(pdev); 63669db3b5eSSebastian Ott 63769db3b5eSSebastian Ott if (zdev->state != ZPCI_FN_STATE_ONLINE) 63869db3b5eSSebastian Ott return 0; 63969db3b5eSSebastian Ott 64069db3b5eSSebastian Ott zpci_unregister_ioat(zdev, 0); 6411803ba2dSSebastian Ott zpci_unmap_resources(pdev); 64269db3b5eSSebastian Ott return clp_disable_fh(zdev); 64369db3b5eSSebastian Ott } 64469db3b5eSSebastian Ott 64569db3b5eSSebastian Ott struct dev_pm_ops pcibios_pm_ops = { 64669db3b5eSSebastian Ott .thaw_noirq = zpci_restore, 64769db3b5eSSebastian Ott .freeze_noirq = zpci_freeze, 64869db3b5eSSebastian Ott .restore_noirq = zpci_restore, 64969db3b5eSSebastian Ott .poweroff_noirq = zpci_freeze, 65069db3b5eSSebastian Ott }; 65169db3b5eSSebastian Ott #endif /* CONFIG_HIBERNATE_CALLBACKS */ 65269db3b5eSSebastian Ott 653cd248341SJan Glauber static int zpci_alloc_domain(struct zpci_dev *zdev) 654cd248341SJan Glauber { 6555c5afd02SSebastian Ott if (zpci_unique_uid) { 6565c5afd02SSebastian Ott zdev->domain = (u16) zdev->uid; 657312e8462SSebastian Ott if (zdev->domain >= ZPCI_NR_DEVICES) 658312e8462SSebastian Ott return 0; 659312e8462SSebastian Ott 660312e8462SSebastian Ott spin_lock(&zpci_domain_lock); 661312e8462SSebastian Ott if (test_bit(zdev->domain, zpci_domain)) { 662312e8462SSebastian Ott spin_unlock(&zpci_domain_lock); 663*794b8846SNiklas Schnelle pr_err("Adding PCI function %08x failed because domain %04x is already assigned\n", 664*794b8846SNiklas Schnelle zdev->fid, zdev->domain); 665312e8462SSebastian Ott return -EEXIST; 666312e8462SSebastian Ott } 667312e8462SSebastian Ott set_bit(zdev->domain, zpci_domain); 668312e8462SSebastian Ott spin_unlock(&zpci_domain_lock); 6695c5afd02SSebastian Ott return 0; 6705c5afd02SSebastian Ott } 6715c5afd02SSebastian Ott 672cd248341SJan Glauber spin_lock(&zpci_domain_lock); 673cd248341SJan Glauber zdev->domain = find_first_zero_bit(zpci_domain, ZPCI_NR_DEVICES); 674cd248341SJan Glauber if (zdev->domain == ZPCI_NR_DEVICES) { 675cd248341SJan Glauber spin_unlock(&zpci_domain_lock); 676cd248341SJan Glauber return -ENOSPC; 677cd248341SJan Glauber } 678cd248341SJan Glauber set_bit(zdev->domain, zpci_domain); 679cd248341SJan Glauber spin_unlock(&zpci_domain_lock); 680cd248341SJan Glauber return 0; 681cd248341SJan Glauber } 682cd248341SJan Glauber 683cd248341SJan Glauber static void zpci_free_domain(struct zpci_dev *zdev) 684cd248341SJan Glauber { 685312e8462SSebastian Ott if (zdev->domain >= ZPCI_NR_DEVICES) 6865c5afd02SSebastian Ott return; 6875c5afd02SSebastian Ott 688cd248341SJan Glauber spin_lock(&zpci_domain_lock); 689cd248341SJan Glauber clear_bit(zdev->domain, zpci_domain); 690cd248341SJan Glauber spin_unlock(&zpci_domain_lock); 691cd248341SJan Glauber } 692cd248341SJan Glauber 6937d594322SSebastian Ott void pcibios_remove_bus(struct pci_bus *bus) 6947d594322SSebastian Ott { 6957d594322SSebastian Ott struct zpci_dev *zdev = get_zdev_by_bus(bus); 6967d594322SSebastian Ott 6977d594322SSebastian Ott zpci_exit_slot(zdev); 6987d594322SSebastian Ott zpci_cleanup_bus_resources(zdev); 699f42c2235SJoerg Roedel zpci_destroy_iommu(zdev); 7007d594322SSebastian Ott zpci_free_domain(zdev); 7017d594322SSebastian Ott 7027d594322SSebastian Ott spin_lock(&zpci_list_lock); 7037d594322SSebastian Ott list_del(&zdev->entry); 7047d594322SSebastian Ott spin_unlock(&zpci_list_lock); 7057d594322SSebastian Ott 706be2c3676SSebastian Ott zpci_dbg(3, "rem fid:%x\n", zdev->fid); 7077d594322SSebastian Ott kfree(zdev); 7087d594322SSebastian Ott } 7097d594322SSebastian Ott 7107d594322SSebastian Ott static int zpci_scan_bus(struct zpci_dev *zdev) 7117d594322SSebastian Ott { 7127d594322SSebastian Ott LIST_HEAD(resources); 7137d594322SSebastian Ott int ret; 7147d594322SSebastian Ott 7157d594322SSebastian Ott ret = zpci_setup_bus_resources(zdev, &resources); 7167d594322SSebastian Ott if (ret) 7172b1df724SSebastian Ott goto error; 7187d594322SSebastian Ott 7197d594322SSebastian Ott zdev->bus = pci_scan_root_bus(NULL, ZPCI_BUS_NR, &pci_root_ops, 7207d594322SSebastian Ott zdev, &resources); 7217d594322SSebastian Ott if (!zdev->bus) { 7222b1df724SSebastian Ott ret = -EIO; 7232b1df724SSebastian Ott goto error; 7247d594322SSebastian Ott } 7257d594322SSebastian Ott zdev->bus->max_bus_speed = zdev->max_bus_speed; 726b97ea289SYijing Wang pci_bus_add_devices(zdev->bus); 7277d594322SSebastian Ott return 0; 7282b1df724SSebastian Ott 7292b1df724SSebastian Ott error: 7302b1df724SSebastian Ott zpci_cleanup_bus_resources(zdev); 7312b1df724SSebastian Ott pci_free_resource_list(&resources); 7322b1df724SSebastian Ott return ret; 7337d594322SSebastian Ott } 7347d594322SSebastian Ott 735a755a45dSJan Glauber int zpci_enable_device(struct zpci_dev *zdev) 736a755a45dSJan Glauber { 737a755a45dSJan Glauber int rc; 738a755a45dSJan Glauber 739a755a45dSJan Glauber rc = clp_enable_fh(zdev, ZPCI_NR_DMA_SPACES); 740a755a45dSJan Glauber if (rc) 741a755a45dSJan Glauber goto out; 742828b35f6SJan Glauber 743828b35f6SJan Glauber rc = zpci_dma_init_device(zdev); 744828b35f6SJan Glauber if (rc) 745828b35f6SJan Glauber goto out_dma; 7460ff70ec8SSebastian Ott 7470ff70ec8SSebastian Ott zdev->state = ZPCI_FN_STATE_ONLINE; 748a755a45dSJan Glauber return 0; 749828b35f6SJan Glauber 750828b35f6SJan Glauber out_dma: 751828b35f6SJan Glauber clp_disable_fh(zdev); 752a755a45dSJan Glauber out: 753a755a45dSJan Glauber return rc; 754a755a45dSJan Glauber } 755a755a45dSJan Glauber EXPORT_SYMBOL_GPL(zpci_enable_device); 756a755a45dSJan Glauber 757cb65a669SSebastian Ott int zpci_disable_device(struct zpci_dev *zdev) 758cb65a669SSebastian Ott { 759cb65a669SSebastian Ott zpci_dma_exit_device(zdev); 760cb65a669SSebastian Ott return clp_disable_fh(zdev); 761cb65a669SSebastian Ott } 762cb65a669SSebastian Ott EXPORT_SYMBOL_GPL(zpci_disable_device); 763cb65a669SSebastian Ott 764cd248341SJan Glauber int zpci_create_device(struct zpci_dev *zdev) 765cd248341SJan Glauber { 766cd248341SJan Glauber int rc; 767cd248341SJan Glauber 768cd248341SJan Glauber rc = zpci_alloc_domain(zdev); 769cd248341SJan Glauber if (rc) 770cd248341SJan Glauber goto out; 771cd248341SJan Glauber 772f42c2235SJoerg Roedel rc = zpci_init_iommu(zdev); 773f42c2235SJoerg Roedel if (rc) 774f42c2235SJoerg Roedel goto out_free; 775f42c2235SJoerg Roedel 77680ed156aSSebastian Ott mutex_init(&zdev->lock); 7771c21351bSSebastian Ott if (zdev->state == ZPCI_FN_STATE_CONFIGURED) { 7781c21351bSSebastian Ott rc = zpci_enable_device(zdev); 779cd248341SJan Glauber if (rc) 780f42c2235SJoerg Roedel goto out_destroy_iommu; 7811c21351bSSebastian Ott } 7821c21351bSSebastian Ott rc = zpci_scan_bus(zdev); 7831c21351bSSebastian Ott if (rc) 7841c21351bSSebastian Ott goto out_disable; 785cd248341SJan Glauber 78657b5918cSSebastian Ott spin_lock(&zpci_list_lock); 787cd248341SJan Glauber list_add_tail(&zdev->entry, &zpci_list); 78857b5918cSSebastian Ott spin_unlock(&zpci_list_lock); 789cd248341SJan Glauber 79067f43f38SSebastian Ott zpci_init_slot(zdev); 79167f43f38SSebastian Ott 792cd248341SJan Glauber return 0; 793cd248341SJan Glauber 7941c21351bSSebastian Ott out_disable: 7951c21351bSSebastian Ott if (zdev->state == ZPCI_FN_STATE_ONLINE) 7961c21351bSSebastian Ott zpci_disable_device(zdev); 797f42c2235SJoerg Roedel out_destroy_iommu: 798f42c2235SJoerg Roedel zpci_destroy_iommu(zdev); 7991c21351bSSebastian Ott out_free: 800cd248341SJan Glauber zpci_free_domain(zdev); 801cd248341SJan Glauber out: 802cd248341SJan Glauber return rc; 803cd248341SJan Glauber } 804cd248341SJan Glauber 805623bd44dSSebastian Ott void zpci_remove_device(struct zpci_dev *zdev) 806623bd44dSSebastian Ott { 807623bd44dSSebastian Ott if (!zdev->bus) 808623bd44dSSebastian Ott return; 809623bd44dSSebastian Ott 810623bd44dSSebastian Ott pci_stop_root_bus(zdev->bus); 811623bd44dSSebastian Ott pci_remove_root_bus(zdev->bus); 812623bd44dSSebastian Ott } 813623bd44dSSebastian Ott 814bd3a1725SMartin Schwidefsky int zpci_report_error(struct pci_dev *pdev, 815bd3a1725SMartin Schwidefsky struct zpci_report_error_header *report) 816bd3a1725SMartin Schwidefsky { 817bd3a1725SMartin Schwidefsky struct zpci_dev *zdev = to_zpci(pdev); 818bd3a1725SMartin Schwidefsky 819bd3a1725SMartin Schwidefsky return sclp_pci_report(report, zdev->fh, zdev->fid); 820bd3a1725SMartin Schwidefsky } 821bd3a1725SMartin Schwidefsky EXPORT_SYMBOL(zpci_report_error); 822bd3a1725SMartin Schwidefsky 823cd248341SJan Glauber static int zpci_mem_init(void) 824cd248341SJan Glauber { 82580c544deSSebastian Ott BUILD_BUG_ON(!is_power_of_2(__alignof__(struct zpci_fmb)) || 82680c544deSSebastian Ott __alignof__(struct zpci_fmb) < sizeof(struct zpci_fmb)); 82780c544deSSebastian Ott 828d0b08853SJan Glauber zdev_fmb_cache = kmem_cache_create("PCI_FMB_cache", sizeof(struct zpci_fmb), 82980c544deSSebastian Ott __alignof__(struct zpci_fmb), 0, NULL); 830d0b08853SJan Glauber if (!zdev_fmb_cache) 831c506fff3SSebastian Ott goto error_fmb; 832d0b08853SJan Glauber 833c506fff3SSebastian Ott zpci_iomap_start = kcalloc(ZPCI_IOMAP_ENTRIES, 834c506fff3SSebastian Ott sizeof(*zpci_iomap_start), GFP_KERNEL); 835cd248341SJan Glauber if (!zpci_iomap_start) 8369a4da8a5SJan Glauber goto error_iomap; 837cd248341SJan Glauber 838c506fff3SSebastian Ott zpci_iomap_bitmap = kcalloc(BITS_TO_LONGS(ZPCI_IOMAP_ENTRIES), 839c506fff3SSebastian Ott sizeof(*zpci_iomap_bitmap), GFP_KERNEL); 840c506fff3SSebastian Ott if (!zpci_iomap_bitmap) 841c506fff3SSebastian Ott goto error_iomap_bitmap; 842c506fff3SSebastian Ott 843c506fff3SSebastian Ott return 0; 844c506fff3SSebastian Ott error_iomap_bitmap: 845c506fff3SSebastian Ott kfree(zpci_iomap_start); 8469a4da8a5SJan Glauber error_iomap: 847d0b08853SJan Glauber kmem_cache_destroy(zdev_fmb_cache); 848c506fff3SSebastian Ott error_fmb: 849cd248341SJan Glauber return -ENOMEM; 850cd248341SJan Glauber } 851cd248341SJan Glauber 852cd248341SJan Glauber static void zpci_mem_exit(void) 853cd248341SJan Glauber { 854c506fff3SSebastian Ott kfree(zpci_iomap_bitmap); 855cd248341SJan Glauber kfree(zpci_iomap_start); 856d0b08853SJan Glauber kmem_cache_destroy(zdev_fmb_cache); 857cd248341SJan Glauber } 858cd248341SJan Glauber 8596324b4deSSebastian Ott static unsigned int s390_pci_probe __initdata = 1; 86056271303SSebastian Ott static unsigned int s390_pci_no_mio __initdata; 861fbfe07d4SSebastian Ott unsigned int s390_pci_force_floating __initdata; 862aa3b7c29SSebastian Ott static unsigned int s390_pci_initialized; 863cd248341SJan Glauber 864cd248341SJan Glauber char * __init pcibios_setup(char *str) 865cd248341SJan Glauber { 866257608fbSSebastian Ott if (!strcmp(str, "off")) { 867257608fbSSebastian Ott s390_pci_probe = 0; 868cd248341SJan Glauber return NULL; 869cd248341SJan Glauber } 87056271303SSebastian Ott if (!strcmp(str, "nomio")) { 87156271303SSebastian Ott s390_pci_no_mio = 1; 87256271303SSebastian Ott return NULL; 87356271303SSebastian Ott } 874fbfe07d4SSebastian Ott if (!strcmp(str, "force_floating")) { 875fbfe07d4SSebastian Ott s390_pci_force_floating = 1; 876fbfe07d4SSebastian Ott return NULL; 877fbfe07d4SSebastian Ott } 878cd248341SJan Glauber return str; 879cd248341SJan Glauber } 880cd248341SJan Glauber 881aa3b7c29SSebastian Ott bool zpci_is_enabled(void) 882aa3b7c29SSebastian Ott { 883aa3b7c29SSebastian Ott return s390_pci_initialized; 884aa3b7c29SSebastian Ott } 885aa3b7c29SSebastian Ott 886cd248341SJan Glauber static int __init pci_base_init(void) 887cd248341SJan Glauber { 888cd248341SJan Glauber int rc; 889cd248341SJan Glauber 8901e5635d1SHeiko Carstens if (!s390_pci_probe) 891cd248341SJan Glauber return 0; 892cd248341SJan Glauber 89348070c73SChristian Borntraeger if (!test_facility(69) || !test_facility(71)) 894cd248341SJan Glauber return 0; 895cd248341SJan Glauber 8969964f396SSebastian Ott if (test_facility(153) && !s390_pci_no_mio) { 89771ba41c9SSebastian Ott static_branch_enable(&have_mio); 8989964f396SSebastian Ott ctl_set_bit(2, 5); 8999964f396SSebastian Ott } 90071ba41c9SSebastian Ott 901d0b08853SJan Glauber rc = zpci_debug_init(); 902d0b08853SJan Glauber if (rc) 9031f44a225SMartin Schwidefsky goto out; 904d0b08853SJan Glauber 905cd248341SJan Glauber rc = zpci_mem_init(); 906cd248341SJan Glauber if (rc) 907cd248341SJan Glauber goto out_mem; 908cd248341SJan Glauber 9099a4da8a5SJan Glauber rc = zpci_irq_init(); 9109a4da8a5SJan Glauber if (rc) 9119a4da8a5SJan Glauber goto out_irq; 9129a4da8a5SJan Glauber 913828b35f6SJan Glauber rc = zpci_dma_init(); 914828b35f6SJan Glauber if (rc) 915828b35f6SJan Glauber goto out_dma; 916828b35f6SJan Glauber 9171d578966SSebastian Ott rc = clp_scan_pci_devices(); 918a755a45dSJan Glauber if (rc) 919a755a45dSJan Glauber goto out_find; 920a755a45dSJan Glauber 921aa3b7c29SSebastian Ott s390_pci_initialized = 1; 922cd248341SJan Glauber return 0; 923cd248341SJan Glauber 924a755a45dSJan Glauber out_find: 925828b35f6SJan Glauber zpci_dma_exit(); 926828b35f6SJan Glauber out_dma: 9279a4da8a5SJan Glauber zpci_irq_exit(); 9289a4da8a5SJan Glauber out_irq: 929cd248341SJan Glauber zpci_mem_exit(); 930cd248341SJan Glauber out_mem: 931d0b08853SJan Glauber zpci_debug_exit(); 9321f44a225SMartin Schwidefsky out: 933cd248341SJan Glauber return rc; 934cd248341SJan Glauber } 93567f43f38SSebastian Ott subsys_initcall_sync(pci_base_init); 93657b5918cSSebastian Ott 93757b5918cSSebastian Ott void zpci_rescan(void) 93857b5918cSSebastian Ott { 939aa3b7c29SSebastian Ott if (zpci_is_enabled()) 94057b5918cSSebastian Ott clp_rescan_pci_devices_simple(); 94157b5918cSSebastian Ott } 942