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> 30cd248341SJan Glauber 319a4da8a5SJan Glauber #include <asm/isc.h> 329a4da8a5SJan Glauber #include <asm/airq.h> 33cd248341SJan Glauber #include <asm/facility.h> 34cd248341SJan Glauber #include <asm/pci_insn.h> 35a755a45dSJan Glauber #include <asm/pci_clp.h> 36828b35f6SJan Glauber #include <asm/pci_dma.h> 37cd248341SJan Glauber 38cd248341SJan Glauber /* list of all detected zpci devices */ 3967f43f38SSebastian Ott static LIST_HEAD(zpci_list); 4057b5918cSSebastian Ott static DEFINE_SPINLOCK(zpci_list_lock); 411f44a225SMartin Schwidefsky 42cd248341SJan Glauber static DECLARE_BITMAP(zpci_domain, ZPCI_NR_DEVICES); 43cd248341SJan Glauber static DEFINE_SPINLOCK(zpci_domain_lock); 44cd248341SJan Glauber 45c506fff3SSebastian Ott #define ZPCI_IOMAP_ENTRIES \ 46b0c8ce89SSebastian Ott min(((unsigned long) ZPCI_NR_DEVICES * PCI_BAR_COUNT / 2), \ 47c506fff3SSebastian Ott ZPCI_IOMAP_MAX_ENTRIES) 48c506fff3SSebastian Ott 49cd248341SJan Glauber static DEFINE_SPINLOCK(zpci_iomap_lock); 50c506fff3SSebastian Ott static unsigned long *zpci_iomap_bitmap; 51cd248341SJan Glauber struct zpci_iomap_entry *zpci_iomap_start; 52cd248341SJan Glauber EXPORT_SYMBOL_GPL(zpci_iomap_start); 53cd248341SJan Glauber 5471ba41c9SSebastian Ott DEFINE_STATIC_KEY_FALSE(have_mio); 5571ba41c9SSebastian Ott 56d0b08853SJan Glauber static struct kmem_cache *zdev_fmb_cache; 57d0b08853SJan Glauber 58cd248341SJan Glauber struct zpci_dev *get_zdev_by_fid(u32 fid) 59cd248341SJan Glauber { 60cd248341SJan Glauber struct zpci_dev *tmp, *zdev = NULL; 61cd248341SJan Glauber 6257b5918cSSebastian Ott spin_lock(&zpci_list_lock); 63cd248341SJan Glauber list_for_each_entry(tmp, &zpci_list, entry) { 64cd248341SJan Glauber if (tmp->fid == fid) { 65cd248341SJan Glauber zdev = tmp; 66cd248341SJan Glauber break; 67cd248341SJan Glauber } 68cd248341SJan Glauber } 6957b5918cSSebastian Ott spin_unlock(&zpci_list_lock); 70cd248341SJan Glauber return zdev; 71cd248341SJan Glauber } 72cd248341SJan Glauber 7301553d9aSSebastian Ott void zpci_remove_reserved_devices(void) 7401553d9aSSebastian Ott { 7501553d9aSSebastian Ott struct zpci_dev *tmp, *zdev; 7601553d9aSSebastian Ott enum zpci_state state; 7701553d9aSSebastian Ott LIST_HEAD(remove); 7801553d9aSSebastian Ott 7901553d9aSSebastian Ott spin_lock(&zpci_list_lock); 8001553d9aSSebastian Ott list_for_each_entry_safe(zdev, tmp, &zpci_list, entry) { 8101553d9aSSebastian Ott if (zdev->state == ZPCI_FN_STATE_STANDBY && 8201553d9aSSebastian Ott !clp_get_state(zdev->fid, &state) && 8301553d9aSSebastian Ott state == ZPCI_FN_STATE_RESERVED) 8401553d9aSSebastian Ott list_move_tail(&zdev->entry, &remove); 8501553d9aSSebastian Ott } 8601553d9aSSebastian Ott spin_unlock(&zpci_list_lock); 8701553d9aSSebastian Ott 8801553d9aSSebastian Ott list_for_each_entry_safe(zdev, tmp, &remove, entry) 8901553d9aSSebastian Ott zpci_remove_device(zdev); 9001553d9aSSebastian Ott } 9101553d9aSSebastian Ott 92cd248341SJan Glauber static struct zpci_dev *get_zdev_by_bus(struct pci_bus *bus) 93cd248341SJan Glauber { 94cd248341SJan Glauber return (bus && bus->sysdata) ? (struct zpci_dev *) bus->sysdata : NULL; 95cd248341SJan Glauber } 96cd248341SJan Glauber 97cd248341SJan Glauber int pci_domain_nr(struct pci_bus *bus) 98cd248341SJan Glauber { 99cd248341SJan Glauber return ((struct zpci_dev *) bus->sysdata)->domain; 100cd248341SJan Glauber } 101cd248341SJan Glauber EXPORT_SYMBOL_GPL(pci_domain_nr); 102cd248341SJan Glauber 103cd248341SJan Glauber int pci_proc_domain(struct pci_bus *bus) 104cd248341SJan Glauber { 105cd248341SJan Glauber return pci_domain_nr(bus); 106cd248341SJan Glauber } 107cd248341SJan Glauber EXPORT_SYMBOL_GPL(pci_proc_domain); 108cd248341SJan Glauber 109828b35f6SJan Glauber /* Modify PCI: Register I/O address translation parameters */ 110828b35f6SJan Glauber int zpci_register_ioat(struct zpci_dev *zdev, u8 dmaas, 111828b35f6SJan Glauber u64 base, u64 limit, u64 iota) 112828b35f6SJan Glauber { 11372570834SSebastian Ott u64 req = ZPCI_CREATE_REQ(zdev->fh, dmaas, ZPCI_MOD_FC_REG_IOAT); 11472570834SSebastian Ott struct zpci_fib fib = {0}; 11572570834SSebastian Ott u8 status; 116828b35f6SJan Glauber 117828b35f6SJan Glauber WARN_ON_ONCE(iota & 0x3fff); 11872570834SSebastian Ott fib.pba = base; 11972570834SSebastian Ott fib.pal = limit; 12072570834SSebastian Ott fib.iota = iota | ZPCI_IOTA_RTTO_FLAG; 12172570834SSebastian Ott return zpci_mod_fc(req, &fib, &status) ? -EIO : 0; 122828b35f6SJan Glauber } 123828b35f6SJan Glauber 124828b35f6SJan Glauber /* Modify PCI: Unregister I/O address translation parameters */ 125828b35f6SJan Glauber int zpci_unregister_ioat(struct zpci_dev *zdev, u8 dmaas) 126828b35f6SJan Glauber { 12772570834SSebastian Ott u64 req = ZPCI_CREATE_REQ(zdev->fh, dmaas, ZPCI_MOD_FC_DEREG_IOAT); 12872570834SSebastian Ott struct zpci_fib fib = {0}; 12972570834SSebastian Ott u8 cc, status; 130828b35f6SJan Glauber 13172570834SSebastian Ott cc = zpci_mod_fc(req, &fib, &status); 13272570834SSebastian Ott if (cc == 3) /* Function already gone. */ 13372570834SSebastian Ott cc = 0; 13472570834SSebastian Ott return cc ? -EIO : 0; 135828b35f6SJan Glauber } 136828b35f6SJan Glauber 137d0b08853SJan Glauber /* Modify PCI: Set PCI function measurement parameters */ 138d0b08853SJan Glauber int zpci_fmb_enable_device(struct zpci_dev *zdev) 139d0b08853SJan Glauber { 1404e5bd780SSebastian Ott u64 req = ZPCI_CREATE_REQ(zdev->fh, 0, ZPCI_MOD_FC_SET_MEASURE); 1414e5bd780SSebastian Ott struct zpci_fib fib = {0}; 1424e5bd780SSebastian Ott u8 cc, status; 143d0b08853SJan Glauber 1440b7589ecSSebastian Ott if (zdev->fmb || sizeof(*zdev->fmb) < zdev->fmb_length) 145d0b08853SJan Glauber return -EINVAL; 146d0b08853SJan Glauber 14708b42124SWei Yongjun zdev->fmb = kmem_cache_zalloc(zdev_fmb_cache, GFP_KERNEL); 148d0b08853SJan Glauber if (!zdev->fmb) 149d0b08853SJan Glauber return -ENOMEM; 150d0b08853SJan Glauber WARN_ON((u64) zdev->fmb & 0xf); 151d0b08853SJan Glauber 1526001018aSSebastian Ott /* reset software counters */ 1536001018aSSebastian Ott atomic64_set(&zdev->allocated_pages, 0); 1546001018aSSebastian Ott atomic64_set(&zdev->mapped_pages, 0); 1556001018aSSebastian Ott atomic64_set(&zdev->unmapped_pages, 0); 1566001018aSSebastian Ott 1574e5bd780SSebastian Ott fib.fmb_addr = virt_to_phys(zdev->fmb); 1584e5bd780SSebastian Ott cc = zpci_mod_fc(req, &fib, &status); 1594e5bd780SSebastian Ott if (cc) { 1604e5bd780SSebastian Ott kmem_cache_free(zdev_fmb_cache, zdev->fmb); 1614e5bd780SSebastian Ott zdev->fmb = NULL; 1624e5bd780SSebastian Ott } 1634e5bd780SSebastian Ott return cc ? -EIO : 0; 164d0b08853SJan Glauber } 165d0b08853SJan Glauber 166d0b08853SJan Glauber /* Modify PCI: Disable PCI function measurement */ 167d0b08853SJan Glauber int zpci_fmb_disable_device(struct zpci_dev *zdev) 168d0b08853SJan Glauber { 1694e5bd780SSebastian Ott u64 req = ZPCI_CREATE_REQ(zdev->fh, 0, ZPCI_MOD_FC_SET_MEASURE); 1704e5bd780SSebastian Ott struct zpci_fib fib = {0}; 1714e5bd780SSebastian Ott u8 cc, status; 172d0b08853SJan Glauber 173d0b08853SJan Glauber if (!zdev->fmb) 174d0b08853SJan Glauber return -EINVAL; 175d0b08853SJan Glauber 176d0b08853SJan Glauber /* Function measurement is disabled if fmb address is zero */ 1774e5bd780SSebastian Ott cc = zpci_mod_fc(req, &fib, &status); 1784e5bd780SSebastian Ott if (cc == 3) /* Function already gone. */ 1794e5bd780SSebastian Ott cc = 0; 180d0b08853SJan Glauber 1814e5bd780SSebastian Ott if (!cc) { 182d0b08853SJan Glauber kmem_cache_free(zdev_fmb_cache, zdev->fmb); 183d0b08853SJan Glauber zdev->fmb = NULL; 1844e5bd780SSebastian Ott } 1854e5bd780SSebastian Ott return cc ? -EIO : 0; 186d0b08853SJan Glauber } 187d0b08853SJan Glauber 188cd248341SJan Glauber static int zpci_cfg_load(struct zpci_dev *zdev, int offset, u32 *val, u8 len) 189cd248341SJan Glauber { 190cd248341SJan Glauber u64 req = ZPCI_CREATE_REQ(zdev->fh, ZPCI_PCIAS_CFGSPC, len); 191cd248341SJan Glauber u64 data; 192cd248341SJan Glauber int rc; 193cd248341SJan Glauber 19481deca12SSebastian Ott rc = __zpci_load(&data, req, offset); 195b170bad4SSebastian Ott if (!rc) { 1965064cd35SSebastian Ott data = le64_to_cpu((__force __le64) data); 1975064cd35SSebastian Ott data >>= (8 - len) * 8; 198cd248341SJan Glauber *val = (u32) data; 199b170bad4SSebastian Ott } else 200cd248341SJan Glauber *val = 0xffffffff; 201cd248341SJan Glauber return rc; 202cd248341SJan Glauber } 203cd248341SJan Glauber 204cd248341SJan Glauber static int zpci_cfg_store(struct zpci_dev *zdev, int offset, u32 val, u8 len) 205cd248341SJan Glauber { 206cd248341SJan Glauber u64 req = ZPCI_CREATE_REQ(zdev->fh, ZPCI_PCIAS_CFGSPC, len); 207cd248341SJan Glauber u64 data = val; 208cd248341SJan Glauber int rc; 209cd248341SJan Glauber 2105064cd35SSebastian Ott data <<= (8 - len) * 8; 2115064cd35SSebastian Ott data = (__force u64) cpu_to_le64(data); 21281deca12SSebastian Ott rc = __zpci_store(data, req, offset); 213cd248341SJan Glauber return rc; 214cd248341SJan Glauber } 215cd248341SJan Glauber 216cd248341SJan Glauber resource_size_t pcibios_align_resource(void *data, const struct resource *res, 217cd248341SJan Glauber resource_size_t size, 218cd248341SJan Glauber resource_size_t align) 219cd248341SJan Glauber { 220cd248341SJan Glauber return 0; 221cd248341SJan Glauber } 222cd248341SJan Glauber 22387bc359bSJan Glauber /* combine single writes by using store-block insn */ 22487bc359bSJan Glauber void __iowrite64_copy(void __iomem *to, const void *from, size_t count) 22587bc359bSJan Glauber { 22687bc359bSJan Glauber zpci_memcpy_toio(to, from, count); 22787bc359bSJan Glauber } 22887bc359bSJan Glauber 22971ba41c9SSebastian Ott void __iomem *ioremap(unsigned long ioaddr, unsigned long size) 23071ba41c9SSebastian Ott { 23171ba41c9SSebastian Ott struct vm_struct *area; 23271ba41c9SSebastian Ott unsigned long offset; 23371ba41c9SSebastian Ott 23471ba41c9SSebastian Ott if (!size) 23571ba41c9SSebastian Ott return NULL; 23671ba41c9SSebastian Ott 23771ba41c9SSebastian Ott if (!static_branch_unlikely(&have_mio)) 23871ba41c9SSebastian Ott return (void __iomem *) ioaddr; 23971ba41c9SSebastian Ott 24071ba41c9SSebastian Ott offset = ioaddr & ~PAGE_MASK; 24171ba41c9SSebastian Ott ioaddr &= PAGE_MASK; 24271ba41c9SSebastian Ott size = PAGE_ALIGN(size + offset); 24371ba41c9SSebastian Ott area = get_vm_area(size, VM_IOREMAP); 24471ba41c9SSebastian Ott if (!area) 24571ba41c9SSebastian Ott return NULL; 24671ba41c9SSebastian Ott 24771ba41c9SSebastian Ott if (ioremap_page_range((unsigned long) area->addr, 24871ba41c9SSebastian Ott (unsigned long) area->addr + size, 24971ba41c9SSebastian Ott ioaddr, PAGE_KERNEL)) { 25071ba41c9SSebastian Ott vunmap(area->addr); 25171ba41c9SSebastian Ott return NULL; 25271ba41c9SSebastian Ott } 25371ba41c9SSebastian Ott return (void __iomem *) ((unsigned long) area->addr + offset); 25471ba41c9SSebastian Ott } 25571ba41c9SSebastian Ott EXPORT_SYMBOL(ioremap); 25671ba41c9SSebastian Ott 25771ba41c9SSebastian Ott void iounmap(volatile void __iomem *addr) 25871ba41c9SSebastian Ott { 25971ba41c9SSebastian Ott if (static_branch_likely(&have_mio)) 26071ba41c9SSebastian Ott vunmap((__force void *) ((unsigned long) addr & PAGE_MASK)); 26171ba41c9SSebastian Ott } 26271ba41c9SSebastian Ott EXPORT_SYMBOL(iounmap); 26371ba41c9SSebastian Ott 264cd248341SJan Glauber /* Create a virtual mapping cookie for a PCI BAR */ 26571ba41c9SSebastian Ott static void __iomem *pci_iomap_range_fh(struct pci_dev *pdev, int bar, 26671ba41c9SSebastian Ott unsigned long offset, unsigned long max) 267cd248341SJan Glauber { 268198a5278SSebastian Ott struct zpci_dev *zdev = to_zpci(pdev); 269cd248341SJan Glauber int idx; 270cd248341SJan Glauber 271cd248341SJan Glauber idx = zdev->bars[bar].map_idx; 272cd248341SJan Glauber spin_lock(&zpci_iomap_lock); 273f5e44f82SSebastian Ott /* Detect overrun */ 274f5e44f82SSebastian Ott WARN_ON(!++zpci_iomap_start[idx].count); 275cd248341SJan Glauber zpci_iomap_start[idx].fh = zdev->fh; 276cd248341SJan Glauber zpci_iomap_start[idx].bar = bar; 277cd248341SJan Glauber spin_unlock(&zpci_iomap_lock); 278cd248341SJan Glauber 2799e00caaeSSebastian Ott return (void __iomem *) ZPCI_ADDR(idx) + offset; 280cd248341SJan Glauber } 28171ba41c9SSebastian Ott 28271ba41c9SSebastian Ott static void __iomem *pci_iomap_range_mio(struct pci_dev *pdev, int bar, 28371ba41c9SSebastian Ott unsigned long offset, 28471ba41c9SSebastian Ott unsigned long max) 28571ba41c9SSebastian Ott { 28671ba41c9SSebastian Ott unsigned long barsize = pci_resource_len(pdev, bar); 28771ba41c9SSebastian Ott struct zpci_dev *zdev = to_zpci(pdev); 28871ba41c9SSebastian Ott void __iomem *iova; 28971ba41c9SSebastian Ott 29071ba41c9SSebastian Ott iova = ioremap((unsigned long) zdev->bars[bar].mio_wt, barsize); 29171ba41c9SSebastian Ott return iova ? iova + offset : iova; 29271ba41c9SSebastian Ott } 29371ba41c9SSebastian Ott 29471ba41c9SSebastian Ott void __iomem *pci_iomap_range(struct pci_dev *pdev, int bar, 29571ba41c9SSebastian Ott unsigned long offset, unsigned long max) 29671ba41c9SSebastian Ott { 29771ba41c9SSebastian Ott if (!pci_resource_len(pdev, bar) || bar >= PCI_BAR_COUNT) 29871ba41c9SSebastian Ott return NULL; 29971ba41c9SSebastian Ott 30071ba41c9SSebastian Ott if (static_branch_likely(&have_mio)) 30171ba41c9SSebastian Ott return pci_iomap_range_mio(pdev, bar, offset, max); 30271ba41c9SSebastian Ott else 30371ba41c9SSebastian Ott return pci_iomap_range_fh(pdev, bar, offset, max); 30471ba41c9SSebastian Ott } 305d9426083SSebastian Ott EXPORT_SYMBOL(pci_iomap_range); 3068cfc99b5SMichael S. Tsirkin 3078cfc99b5SMichael S. Tsirkin void __iomem *pci_iomap(struct pci_dev *dev, int bar, unsigned long maxlen) 3088cfc99b5SMichael S. Tsirkin { 3098cfc99b5SMichael S. Tsirkin return pci_iomap_range(dev, bar, 0, maxlen); 3108cfc99b5SMichael S. Tsirkin } 3118cfc99b5SMichael S. Tsirkin EXPORT_SYMBOL(pci_iomap); 312cd248341SJan Glauber 31371ba41c9SSebastian Ott static void __iomem *pci_iomap_wc_range_mio(struct pci_dev *pdev, int bar, 31471ba41c9SSebastian Ott unsigned long offset, unsigned long max) 31571ba41c9SSebastian Ott { 31671ba41c9SSebastian Ott unsigned long barsize = pci_resource_len(pdev, bar); 31771ba41c9SSebastian Ott struct zpci_dev *zdev = to_zpci(pdev); 31871ba41c9SSebastian Ott void __iomem *iova; 31971ba41c9SSebastian Ott 32071ba41c9SSebastian Ott iova = ioremap((unsigned long) zdev->bars[bar].mio_wb, barsize); 32171ba41c9SSebastian Ott return iova ? iova + offset : iova; 32271ba41c9SSebastian Ott } 32371ba41c9SSebastian Ott 32471ba41c9SSebastian Ott void __iomem *pci_iomap_wc_range(struct pci_dev *pdev, int bar, 32571ba41c9SSebastian Ott unsigned long offset, unsigned long max) 32671ba41c9SSebastian Ott { 32771ba41c9SSebastian Ott if (!pci_resource_len(pdev, bar) || bar >= PCI_BAR_COUNT) 32871ba41c9SSebastian Ott return NULL; 32971ba41c9SSebastian Ott 33071ba41c9SSebastian Ott if (static_branch_likely(&have_mio)) 33171ba41c9SSebastian Ott return pci_iomap_wc_range_mio(pdev, bar, offset, max); 33271ba41c9SSebastian Ott else 33371ba41c9SSebastian Ott return pci_iomap_range_fh(pdev, bar, offset, max); 33471ba41c9SSebastian Ott } 33571ba41c9SSebastian Ott EXPORT_SYMBOL(pci_iomap_wc_range); 33671ba41c9SSebastian Ott 33771ba41c9SSebastian Ott void __iomem *pci_iomap_wc(struct pci_dev *dev, int bar, unsigned long maxlen) 33871ba41c9SSebastian Ott { 33971ba41c9SSebastian Ott return pci_iomap_wc_range(dev, bar, 0, maxlen); 34071ba41c9SSebastian Ott } 34171ba41c9SSebastian Ott EXPORT_SYMBOL(pci_iomap_wc); 34271ba41c9SSebastian Ott 34371ba41c9SSebastian Ott static void pci_iounmap_fh(struct pci_dev *pdev, void __iomem *addr) 344cd248341SJan Glauber { 3459e00caaeSSebastian Ott unsigned int idx = ZPCI_IDX(addr); 346cd248341SJan Glauber 347cd248341SJan Glauber spin_lock(&zpci_iomap_lock); 3488cfc99b5SMichael S. Tsirkin /* Detect underrun */ 349f5e44f82SSebastian Ott WARN_ON(!zpci_iomap_start[idx].count); 3508cfc99b5SMichael S. Tsirkin if (!--zpci_iomap_start[idx].count) { 351cd248341SJan Glauber zpci_iomap_start[idx].fh = 0; 352cd248341SJan Glauber zpci_iomap_start[idx].bar = 0; 3538cfc99b5SMichael S. Tsirkin } 354cd248341SJan Glauber spin_unlock(&zpci_iomap_lock); 355cd248341SJan Glauber } 35671ba41c9SSebastian Ott 35771ba41c9SSebastian Ott static void pci_iounmap_mio(struct pci_dev *pdev, void __iomem *addr) 35871ba41c9SSebastian Ott { 35971ba41c9SSebastian Ott iounmap(addr); 36071ba41c9SSebastian Ott } 36171ba41c9SSebastian Ott 36271ba41c9SSebastian Ott void pci_iounmap(struct pci_dev *pdev, void __iomem *addr) 36371ba41c9SSebastian Ott { 36471ba41c9SSebastian Ott if (static_branch_likely(&have_mio)) 36571ba41c9SSebastian Ott pci_iounmap_mio(pdev, addr); 36671ba41c9SSebastian Ott else 36771ba41c9SSebastian Ott pci_iounmap_fh(pdev, addr); 36871ba41c9SSebastian Ott } 369d9426083SSebastian Ott EXPORT_SYMBOL(pci_iounmap); 370cd248341SJan Glauber 371cd248341SJan Glauber static int pci_read(struct pci_bus *bus, unsigned int devfn, int where, 372cd248341SJan Glauber int size, u32 *val) 373cd248341SJan Glauber { 374cd248341SJan Glauber struct zpci_dev *zdev = get_zdev_by_bus(bus); 3752c3700bbSSebastian Ott int ret; 376cd248341SJan Glauber 377cd248341SJan Glauber if (!zdev || devfn != ZPCI_DEVFN) 3782c3700bbSSebastian Ott ret = -ENODEV; 3792c3700bbSSebastian Ott else 3802c3700bbSSebastian Ott ret = zpci_cfg_load(zdev, where, val, size); 3812c3700bbSSebastian Ott 3822c3700bbSSebastian Ott return ret; 383cd248341SJan Glauber } 384cd248341SJan Glauber 385cd248341SJan Glauber static int pci_write(struct pci_bus *bus, unsigned int devfn, int where, 386cd248341SJan Glauber int size, u32 val) 387cd248341SJan Glauber { 388cd248341SJan Glauber struct zpci_dev *zdev = get_zdev_by_bus(bus); 3892c3700bbSSebastian Ott int ret; 390cd248341SJan Glauber 391cd248341SJan Glauber if (!zdev || devfn != ZPCI_DEVFN) 3922c3700bbSSebastian Ott ret = -ENODEV; 3932c3700bbSSebastian Ott else 3942c3700bbSSebastian Ott ret = zpci_cfg_store(zdev, where, val, size); 3952c3700bbSSebastian Ott 3962c3700bbSSebastian Ott return ret; 397cd248341SJan Glauber } 398cd248341SJan Glauber 399cd248341SJan Glauber static struct pci_ops pci_root_ops = { 400cd248341SJan Glauber .read = pci_read, 401cd248341SJan Glauber .write = pci_write, 402cd248341SJan Glauber }; 403cd248341SJan Glauber 404cfbb4a7aSSebastian Ott #ifdef CONFIG_PCI_IOV 405cfbb4a7aSSebastian Ott static struct resource iov_res = { 406cfbb4a7aSSebastian Ott .name = "PCI IOV res", 407cfbb4a7aSSebastian Ott .start = 0, 408cfbb4a7aSSebastian Ott .end = -1, 409cfbb4a7aSSebastian Ott .flags = IORESOURCE_MEM, 410cfbb4a7aSSebastian Ott }; 411cfbb4a7aSSebastian Ott #endif 412cfbb4a7aSSebastian Ott 4131803ba2dSSebastian Ott static void zpci_map_resources(struct pci_dev *pdev) 414cd248341SJan Glauber { 41571ba41c9SSebastian Ott struct zpci_dev *zdev = to_zpci(pdev); 416cd248341SJan Glauber resource_size_t len; 417cd248341SJan Glauber int i; 418cd248341SJan Glauber 419cd248341SJan Glauber for (i = 0; i < PCI_BAR_COUNT; i++) { 420cd248341SJan Glauber len = pci_resource_len(pdev, i); 421cd248341SJan Glauber if (!len) 422cd248341SJan Glauber continue; 42371ba41c9SSebastian Ott 42471ba41c9SSebastian Ott if (static_branch_likely(&have_mio)) 42571ba41c9SSebastian Ott pdev->resource[i].start = 42671ba41c9SSebastian Ott (resource_size_t __force) zdev->bars[i].mio_wb; 42771ba41c9SSebastian Ott else 4285b9f2081SMartin Schwidefsky pdev->resource[i].start = 4295b9f2081SMartin Schwidefsky (resource_size_t __force) pci_iomap(pdev, i, 0); 430cd248341SJan Glauber pdev->resource[i].end = pdev->resource[i].start + len - 1; 431cd248341SJan Glauber } 432cfbb4a7aSSebastian Ott 433cfbb4a7aSSebastian Ott #ifdef CONFIG_PCI_IOV 434cfbb4a7aSSebastian Ott i = PCI_IOV_RESOURCES; 435cfbb4a7aSSebastian Ott 436cfbb4a7aSSebastian Ott for (; i < PCI_SRIOV_NUM_BARS + PCI_IOV_RESOURCES; i++) { 437cfbb4a7aSSebastian Ott len = pci_resource_len(pdev, i); 438cfbb4a7aSSebastian Ott if (!len) 439cfbb4a7aSSebastian Ott continue; 440cfbb4a7aSSebastian Ott pdev->resource[i].parent = &iov_res; 441cfbb4a7aSSebastian Ott } 442cfbb4a7aSSebastian Ott #endif 443944239c5SSebastian Ott } 444944239c5SSebastian Ott 4451803ba2dSSebastian Ott static void zpci_unmap_resources(struct pci_dev *pdev) 446944239c5SSebastian Ott { 447944239c5SSebastian Ott resource_size_t len; 448944239c5SSebastian Ott int i; 449944239c5SSebastian Ott 45071ba41c9SSebastian Ott if (static_branch_likely(&have_mio)) 45171ba41c9SSebastian Ott return; 45271ba41c9SSebastian Ott 453944239c5SSebastian Ott for (i = 0; i < PCI_BAR_COUNT; i++) { 454944239c5SSebastian Ott len = pci_resource_len(pdev, i); 455944239c5SSebastian Ott if (!len) 456944239c5SSebastian Ott continue; 4575b9f2081SMartin Schwidefsky pci_iounmap(pdev, (void __iomem __force *) 4585b9f2081SMartin Schwidefsky pdev->resource[i].start); 459944239c5SSebastian Ott } 460944239c5SSebastian Ott } 461cd248341SJan Glauber 462cd248341SJan Glauber static int zpci_alloc_iomap(struct zpci_dev *zdev) 463cd248341SJan Glauber { 464bf19c94dSSebastian Ott unsigned long entry; 465cd248341SJan Glauber 466cd248341SJan Glauber spin_lock(&zpci_iomap_lock); 467c506fff3SSebastian Ott entry = find_first_zero_bit(zpci_iomap_bitmap, ZPCI_IOMAP_ENTRIES); 468c506fff3SSebastian Ott if (entry == ZPCI_IOMAP_ENTRIES) { 469cd248341SJan Glauber spin_unlock(&zpci_iomap_lock); 470cd248341SJan Glauber return -ENOSPC; 471cd248341SJan Glauber } 472c506fff3SSebastian Ott set_bit(entry, zpci_iomap_bitmap); 473cd248341SJan Glauber spin_unlock(&zpci_iomap_lock); 474cd248341SJan Glauber return entry; 475cd248341SJan Glauber } 476cd248341SJan Glauber 477cd248341SJan Glauber static void zpci_free_iomap(struct zpci_dev *zdev, int entry) 478cd248341SJan Glauber { 479cd248341SJan Glauber spin_lock(&zpci_iomap_lock); 480cd248341SJan Glauber memset(&zpci_iomap_start[entry], 0, sizeof(struct zpci_iomap_entry)); 481c506fff3SSebastian Ott clear_bit(entry, zpci_iomap_bitmap); 482cd248341SJan Glauber spin_unlock(&zpci_iomap_lock); 483cd248341SJan Glauber } 484cd248341SJan Glauber 4857a572a3aSSebastian Ott static struct resource *__alloc_res(struct zpci_dev *zdev, unsigned long start, 4867a572a3aSSebastian Ott unsigned long size, unsigned long flags) 4877a572a3aSSebastian Ott { 4887a572a3aSSebastian Ott struct resource *r; 4897a572a3aSSebastian Ott 4907a572a3aSSebastian Ott r = kzalloc(sizeof(*r), GFP_KERNEL); 4917a572a3aSSebastian Ott if (!r) 4927a572a3aSSebastian Ott return NULL; 4937a572a3aSSebastian Ott 4947a572a3aSSebastian Ott r->start = start; 4957a572a3aSSebastian Ott r->end = r->start + size - 1; 4967a572a3aSSebastian Ott r->flags = flags; 4977a572a3aSSebastian Ott r->name = zdev->res_name; 4987a572a3aSSebastian Ott 4997a572a3aSSebastian Ott if (request_resource(&iomem_resource, r)) { 5007a572a3aSSebastian Ott kfree(r); 5017a572a3aSSebastian Ott return NULL; 5027a572a3aSSebastian Ott } 5037a572a3aSSebastian Ott return r; 5047a572a3aSSebastian Ott } 5057a572a3aSSebastian Ott 5067a572a3aSSebastian Ott static int zpci_setup_bus_resources(struct zpci_dev *zdev, 5077a572a3aSSebastian Ott struct list_head *resources) 5087a572a3aSSebastian Ott { 5097a572a3aSSebastian Ott unsigned long addr, size, flags; 5107a572a3aSSebastian Ott struct resource *res; 5117a572a3aSSebastian Ott int i, entry; 5127a572a3aSSebastian Ott 5137a572a3aSSebastian Ott snprintf(zdev->res_name, sizeof(zdev->res_name), 5147a572a3aSSebastian Ott "PCI Bus %04x:%02x", zdev->domain, ZPCI_BUS_NR); 5157a572a3aSSebastian Ott 5167a572a3aSSebastian Ott for (i = 0; i < PCI_BAR_COUNT; i++) { 5177a572a3aSSebastian Ott if (!zdev->bars[i].size) 5187a572a3aSSebastian Ott continue; 5197a572a3aSSebastian Ott entry = zpci_alloc_iomap(zdev); 5207a572a3aSSebastian Ott if (entry < 0) 5217a572a3aSSebastian Ott return entry; 5227a572a3aSSebastian Ott zdev->bars[i].map_idx = entry; 5237a572a3aSSebastian Ott 5247a572a3aSSebastian Ott /* only MMIO is supported */ 5257a572a3aSSebastian Ott flags = IORESOURCE_MEM; 5267a572a3aSSebastian Ott if (zdev->bars[i].val & 8) 5277a572a3aSSebastian Ott flags |= IORESOURCE_PREFETCH; 5287a572a3aSSebastian Ott if (zdev->bars[i].val & 4) 5297a572a3aSSebastian Ott flags |= IORESOURCE_MEM_64; 5307a572a3aSSebastian Ott 531*dcd33b23SSebastian Ott if (static_branch_likely(&have_mio)) 532*dcd33b23SSebastian Ott addr = (unsigned long) zdev->bars[i].mio_wb; 533*dcd33b23SSebastian Ott else 5349e00caaeSSebastian Ott addr = ZPCI_ADDR(entry); 5357a572a3aSSebastian Ott size = 1UL << zdev->bars[i].size; 5367a572a3aSSebastian Ott 5377a572a3aSSebastian Ott res = __alloc_res(zdev, addr, size, flags); 5387a572a3aSSebastian Ott if (!res) { 5397a572a3aSSebastian Ott zpci_free_iomap(zdev, entry); 5407a572a3aSSebastian Ott return -ENOMEM; 5417a572a3aSSebastian Ott } 5427a572a3aSSebastian Ott zdev->bars[i].res = res; 5437a572a3aSSebastian Ott pci_add_resource(resources, res); 5447a572a3aSSebastian Ott } 5457a572a3aSSebastian Ott 5467a572a3aSSebastian Ott return 0; 5477a572a3aSSebastian Ott } 5487a572a3aSSebastian Ott 5497a572a3aSSebastian Ott static void zpci_cleanup_bus_resources(struct zpci_dev *zdev) 5507a572a3aSSebastian Ott { 5517a572a3aSSebastian Ott int i; 5527a572a3aSSebastian Ott 5537a572a3aSSebastian Ott for (i = 0; i < PCI_BAR_COUNT; i++) { 5542b1df724SSebastian Ott if (!zdev->bars[i].size || !zdev->bars[i].res) 5557a572a3aSSebastian Ott continue; 5567a572a3aSSebastian Ott 5577a572a3aSSebastian Ott zpci_free_iomap(zdev, zdev->bars[i].map_idx); 5587a572a3aSSebastian Ott release_resource(zdev->bars[i].res); 5597a572a3aSSebastian Ott kfree(zdev->bars[i].res); 5607a572a3aSSebastian Ott } 5617a572a3aSSebastian Ott } 5627a572a3aSSebastian Ott 563af0a8a84SSebastian Ott int pcibios_add_device(struct pci_dev *pdev) 564af0a8a84SSebastian Ott { 565cb809182SSebastian Ott struct resource *res; 566cb809182SSebastian Ott int i; 567cb809182SSebastian Ott 5687dc20ab1SSebastian Ott if (pdev->is_physfn) 5697dc20ab1SSebastian Ott pdev->no_vf_scan = 1; 5707dc20ab1SSebastian Ott 571ef4858c6SSebastian Ott pdev->dev.groups = zpci_attr_groups; 5725657933dSBart Van Assche pdev->dev.dma_ops = &s390_pci_dma_ops; 5731803ba2dSSebastian Ott zpci_map_resources(pdev); 574cb809182SSebastian Ott 575cb809182SSebastian Ott for (i = 0; i < PCI_BAR_COUNT; i++) { 576cb809182SSebastian Ott res = &pdev->resource[i]; 577cb809182SSebastian Ott if (res->parent || !res->flags) 578cb809182SSebastian Ott continue; 579cb809182SSebastian Ott pci_claim_resource(pdev, i); 580cb809182SSebastian Ott } 581cb809182SSebastian Ott 582cb809182SSebastian Ott return 0; 583cb809182SSebastian Ott } 584cb809182SSebastian Ott 5851803ba2dSSebastian Ott void pcibios_release_device(struct pci_dev *pdev) 5861803ba2dSSebastian Ott { 5871803ba2dSSebastian Ott zpci_unmap_resources(pdev); 5881803ba2dSSebastian Ott } 5891803ba2dSSebastian Ott 590cb809182SSebastian Ott int pcibios_enable_device(struct pci_dev *pdev, int mask) 591cb809182SSebastian Ott { 592198a5278SSebastian Ott struct zpci_dev *zdev = to_zpci(pdev); 593af0a8a84SSebastian Ott 5949a99649fSSebastian Ott zpci_debug_init_device(zdev, dev_name(&pdev->dev)); 595af0a8a84SSebastian Ott zpci_fmb_enable_device(zdev); 596af0a8a84SSebastian Ott 597d7533232SBjorn Helgaas return pci_enable_resources(pdev, mask); 598af0a8a84SSebastian Ott } 599af0a8a84SSebastian Ott 600cb809182SSebastian Ott void pcibios_disable_device(struct pci_dev *pdev) 601944239c5SSebastian Ott { 602198a5278SSebastian Ott struct zpci_dev *zdev = to_zpci(pdev); 603944239c5SSebastian Ott 604944239c5SSebastian Ott zpci_fmb_disable_device(zdev); 605944239c5SSebastian Ott zpci_debug_exit_device(zdev); 606944239c5SSebastian Ott } 607944239c5SSebastian Ott 60869db3b5eSSebastian Ott #ifdef CONFIG_HIBERNATE_CALLBACKS 60969db3b5eSSebastian Ott static int zpci_restore(struct device *dev) 61069db3b5eSSebastian Ott { 6111803ba2dSSebastian Ott struct pci_dev *pdev = to_pci_dev(dev); 612198a5278SSebastian Ott struct zpci_dev *zdev = to_zpci(pdev); 61369db3b5eSSebastian Ott int ret = 0; 61469db3b5eSSebastian Ott 61569db3b5eSSebastian Ott if (zdev->state != ZPCI_FN_STATE_ONLINE) 61669db3b5eSSebastian Ott goto out; 61769db3b5eSSebastian Ott 61869db3b5eSSebastian Ott ret = clp_enable_fh(zdev, ZPCI_NR_DMA_SPACES); 61969db3b5eSSebastian Ott if (ret) 62069db3b5eSSebastian Ott goto out; 62169db3b5eSSebastian Ott 6221803ba2dSSebastian Ott zpci_map_resources(pdev); 62369eea95cSGerald Schaefer zpci_register_ioat(zdev, 0, zdev->start_dma, zdev->end_dma, 62469db3b5eSSebastian Ott (u64) zdev->dma_table); 62569db3b5eSSebastian Ott 62669db3b5eSSebastian Ott out: 62769db3b5eSSebastian Ott return ret; 62869db3b5eSSebastian Ott } 62969db3b5eSSebastian Ott 63069db3b5eSSebastian Ott static int zpci_freeze(struct device *dev) 63169db3b5eSSebastian Ott { 6321803ba2dSSebastian Ott struct pci_dev *pdev = to_pci_dev(dev); 633198a5278SSebastian Ott struct zpci_dev *zdev = to_zpci(pdev); 63469db3b5eSSebastian Ott 63569db3b5eSSebastian Ott if (zdev->state != ZPCI_FN_STATE_ONLINE) 63669db3b5eSSebastian Ott return 0; 63769db3b5eSSebastian Ott 63869db3b5eSSebastian Ott zpci_unregister_ioat(zdev, 0); 6391803ba2dSSebastian Ott zpci_unmap_resources(pdev); 64069db3b5eSSebastian Ott return clp_disable_fh(zdev); 64169db3b5eSSebastian Ott } 64269db3b5eSSebastian Ott 64369db3b5eSSebastian Ott struct dev_pm_ops pcibios_pm_ops = { 64469db3b5eSSebastian Ott .thaw_noirq = zpci_restore, 64569db3b5eSSebastian Ott .freeze_noirq = zpci_freeze, 64669db3b5eSSebastian Ott .restore_noirq = zpci_restore, 64769db3b5eSSebastian Ott .poweroff_noirq = zpci_freeze, 64869db3b5eSSebastian Ott }; 64969db3b5eSSebastian Ott #endif /* CONFIG_HIBERNATE_CALLBACKS */ 65069db3b5eSSebastian Ott 651cd248341SJan Glauber static int zpci_alloc_domain(struct zpci_dev *zdev) 652cd248341SJan Glauber { 6535c5afd02SSebastian Ott if (zpci_unique_uid) { 6545c5afd02SSebastian Ott zdev->domain = (u16) zdev->uid; 655312e8462SSebastian Ott if (zdev->domain >= ZPCI_NR_DEVICES) 656312e8462SSebastian Ott return 0; 657312e8462SSebastian Ott 658312e8462SSebastian Ott spin_lock(&zpci_domain_lock); 659312e8462SSebastian Ott if (test_bit(zdev->domain, zpci_domain)) { 660312e8462SSebastian Ott spin_unlock(&zpci_domain_lock); 661312e8462SSebastian Ott return -EEXIST; 662312e8462SSebastian Ott } 663312e8462SSebastian Ott set_bit(zdev->domain, zpci_domain); 664312e8462SSebastian Ott spin_unlock(&zpci_domain_lock); 6655c5afd02SSebastian Ott return 0; 6665c5afd02SSebastian Ott } 6675c5afd02SSebastian Ott 668cd248341SJan Glauber spin_lock(&zpci_domain_lock); 669cd248341SJan Glauber zdev->domain = find_first_zero_bit(zpci_domain, ZPCI_NR_DEVICES); 670cd248341SJan Glauber if (zdev->domain == ZPCI_NR_DEVICES) { 671cd248341SJan Glauber spin_unlock(&zpci_domain_lock); 672cd248341SJan Glauber return -ENOSPC; 673cd248341SJan Glauber } 674cd248341SJan Glauber set_bit(zdev->domain, zpci_domain); 675cd248341SJan Glauber spin_unlock(&zpci_domain_lock); 676cd248341SJan Glauber return 0; 677cd248341SJan Glauber } 678cd248341SJan Glauber 679cd248341SJan Glauber static void zpci_free_domain(struct zpci_dev *zdev) 680cd248341SJan Glauber { 681312e8462SSebastian Ott if (zdev->domain >= ZPCI_NR_DEVICES) 6825c5afd02SSebastian Ott return; 6835c5afd02SSebastian Ott 684cd248341SJan Glauber spin_lock(&zpci_domain_lock); 685cd248341SJan Glauber clear_bit(zdev->domain, zpci_domain); 686cd248341SJan Glauber spin_unlock(&zpci_domain_lock); 687cd248341SJan Glauber } 688cd248341SJan Glauber 6897d594322SSebastian Ott void pcibios_remove_bus(struct pci_bus *bus) 6907d594322SSebastian Ott { 6917d594322SSebastian Ott struct zpci_dev *zdev = get_zdev_by_bus(bus); 6927d594322SSebastian Ott 6937d594322SSebastian Ott zpci_exit_slot(zdev); 6947d594322SSebastian Ott zpci_cleanup_bus_resources(zdev); 695f42c2235SJoerg Roedel zpci_destroy_iommu(zdev); 6967d594322SSebastian Ott zpci_free_domain(zdev); 6977d594322SSebastian Ott 6987d594322SSebastian Ott spin_lock(&zpci_list_lock); 6997d594322SSebastian Ott list_del(&zdev->entry); 7007d594322SSebastian Ott spin_unlock(&zpci_list_lock); 7017d594322SSebastian Ott 702be2c3676SSebastian Ott zpci_dbg(3, "rem fid:%x\n", zdev->fid); 7037d594322SSebastian Ott kfree(zdev); 7047d594322SSebastian Ott } 7057d594322SSebastian Ott 7067d594322SSebastian Ott static int zpci_scan_bus(struct zpci_dev *zdev) 7077d594322SSebastian Ott { 7087d594322SSebastian Ott LIST_HEAD(resources); 7097d594322SSebastian Ott int ret; 7107d594322SSebastian Ott 7117d594322SSebastian Ott ret = zpci_setup_bus_resources(zdev, &resources); 7127d594322SSebastian Ott if (ret) 7132b1df724SSebastian Ott goto error; 7147d594322SSebastian Ott 7157d594322SSebastian Ott zdev->bus = pci_scan_root_bus(NULL, ZPCI_BUS_NR, &pci_root_ops, 7167d594322SSebastian Ott zdev, &resources); 7177d594322SSebastian Ott if (!zdev->bus) { 7182b1df724SSebastian Ott ret = -EIO; 7192b1df724SSebastian Ott goto error; 7207d594322SSebastian Ott } 7217d594322SSebastian Ott zdev->bus->max_bus_speed = zdev->max_bus_speed; 722b97ea289SYijing Wang pci_bus_add_devices(zdev->bus); 7237d594322SSebastian Ott return 0; 7242b1df724SSebastian Ott 7252b1df724SSebastian Ott error: 7262b1df724SSebastian Ott zpci_cleanup_bus_resources(zdev); 7272b1df724SSebastian Ott pci_free_resource_list(&resources); 7282b1df724SSebastian Ott return ret; 7297d594322SSebastian Ott } 7307d594322SSebastian Ott 731a755a45dSJan Glauber int zpci_enable_device(struct zpci_dev *zdev) 732a755a45dSJan Glauber { 733a755a45dSJan Glauber int rc; 734a755a45dSJan Glauber 735a755a45dSJan Glauber rc = clp_enable_fh(zdev, ZPCI_NR_DMA_SPACES); 736a755a45dSJan Glauber if (rc) 737a755a45dSJan Glauber goto out; 738828b35f6SJan Glauber 739828b35f6SJan Glauber rc = zpci_dma_init_device(zdev); 740828b35f6SJan Glauber if (rc) 741828b35f6SJan Glauber goto out_dma; 7420ff70ec8SSebastian Ott 7430ff70ec8SSebastian Ott zdev->state = ZPCI_FN_STATE_ONLINE; 744a755a45dSJan Glauber return 0; 745828b35f6SJan Glauber 746828b35f6SJan Glauber out_dma: 747828b35f6SJan Glauber clp_disable_fh(zdev); 748a755a45dSJan Glauber out: 749a755a45dSJan Glauber return rc; 750a755a45dSJan Glauber } 751a755a45dSJan Glauber EXPORT_SYMBOL_GPL(zpci_enable_device); 752a755a45dSJan Glauber 753cb65a669SSebastian Ott int zpci_disable_device(struct zpci_dev *zdev) 754cb65a669SSebastian Ott { 755cb65a669SSebastian Ott zpci_dma_exit_device(zdev); 756cb65a669SSebastian Ott return clp_disable_fh(zdev); 757cb65a669SSebastian Ott } 758cb65a669SSebastian Ott EXPORT_SYMBOL_GPL(zpci_disable_device); 759cb65a669SSebastian Ott 760cd248341SJan Glauber int zpci_create_device(struct zpci_dev *zdev) 761cd248341SJan Glauber { 762cd248341SJan Glauber int rc; 763cd248341SJan Glauber 764cd248341SJan Glauber rc = zpci_alloc_domain(zdev); 765cd248341SJan Glauber if (rc) 766cd248341SJan Glauber goto out; 767cd248341SJan Glauber 768f42c2235SJoerg Roedel rc = zpci_init_iommu(zdev); 769f42c2235SJoerg Roedel if (rc) 770f42c2235SJoerg Roedel goto out_free; 771f42c2235SJoerg Roedel 77280ed156aSSebastian Ott mutex_init(&zdev->lock); 7731c21351bSSebastian Ott if (zdev->state == ZPCI_FN_STATE_CONFIGURED) { 7741c21351bSSebastian Ott rc = zpci_enable_device(zdev); 775cd248341SJan Glauber if (rc) 776f42c2235SJoerg Roedel goto out_destroy_iommu; 7771c21351bSSebastian Ott } 7781c21351bSSebastian Ott rc = zpci_scan_bus(zdev); 7791c21351bSSebastian Ott if (rc) 7801c21351bSSebastian Ott goto out_disable; 781cd248341SJan Glauber 78257b5918cSSebastian Ott spin_lock(&zpci_list_lock); 783cd248341SJan Glauber list_add_tail(&zdev->entry, &zpci_list); 78457b5918cSSebastian Ott spin_unlock(&zpci_list_lock); 785cd248341SJan Glauber 78667f43f38SSebastian Ott zpci_init_slot(zdev); 78767f43f38SSebastian Ott 788cd248341SJan Glauber return 0; 789cd248341SJan Glauber 7901c21351bSSebastian Ott out_disable: 7911c21351bSSebastian Ott if (zdev->state == ZPCI_FN_STATE_ONLINE) 7921c21351bSSebastian Ott zpci_disable_device(zdev); 793f42c2235SJoerg Roedel out_destroy_iommu: 794f42c2235SJoerg Roedel zpci_destroy_iommu(zdev); 7951c21351bSSebastian Ott out_free: 796cd248341SJan Glauber zpci_free_domain(zdev); 797cd248341SJan Glauber out: 798cd248341SJan Glauber return rc; 799cd248341SJan Glauber } 800cd248341SJan Glauber 801623bd44dSSebastian Ott void zpci_remove_device(struct zpci_dev *zdev) 802623bd44dSSebastian Ott { 803623bd44dSSebastian Ott if (!zdev->bus) 804623bd44dSSebastian Ott return; 805623bd44dSSebastian Ott 806623bd44dSSebastian Ott pci_stop_root_bus(zdev->bus); 807623bd44dSSebastian Ott pci_remove_root_bus(zdev->bus); 808623bd44dSSebastian Ott } 809623bd44dSSebastian Ott 810bd3a1725SMartin Schwidefsky int zpci_report_error(struct pci_dev *pdev, 811bd3a1725SMartin Schwidefsky struct zpci_report_error_header *report) 812bd3a1725SMartin Schwidefsky { 813bd3a1725SMartin Schwidefsky struct zpci_dev *zdev = to_zpci(pdev); 814bd3a1725SMartin Schwidefsky 815bd3a1725SMartin Schwidefsky return sclp_pci_report(report, zdev->fh, zdev->fid); 816bd3a1725SMartin Schwidefsky } 817bd3a1725SMartin Schwidefsky EXPORT_SYMBOL(zpci_report_error); 818bd3a1725SMartin Schwidefsky 819cd248341SJan Glauber static int zpci_mem_init(void) 820cd248341SJan Glauber { 82180c544deSSebastian Ott BUILD_BUG_ON(!is_power_of_2(__alignof__(struct zpci_fmb)) || 82280c544deSSebastian Ott __alignof__(struct zpci_fmb) < sizeof(struct zpci_fmb)); 82380c544deSSebastian Ott 824d0b08853SJan Glauber zdev_fmb_cache = kmem_cache_create("PCI_FMB_cache", sizeof(struct zpci_fmb), 82580c544deSSebastian Ott __alignof__(struct zpci_fmb), 0, NULL); 826d0b08853SJan Glauber if (!zdev_fmb_cache) 827c506fff3SSebastian Ott goto error_fmb; 828d0b08853SJan Glauber 829c506fff3SSebastian Ott zpci_iomap_start = kcalloc(ZPCI_IOMAP_ENTRIES, 830c506fff3SSebastian Ott sizeof(*zpci_iomap_start), GFP_KERNEL); 831cd248341SJan Glauber if (!zpci_iomap_start) 8329a4da8a5SJan Glauber goto error_iomap; 833cd248341SJan Glauber 834c506fff3SSebastian Ott zpci_iomap_bitmap = kcalloc(BITS_TO_LONGS(ZPCI_IOMAP_ENTRIES), 835c506fff3SSebastian Ott sizeof(*zpci_iomap_bitmap), GFP_KERNEL); 836c506fff3SSebastian Ott if (!zpci_iomap_bitmap) 837c506fff3SSebastian Ott goto error_iomap_bitmap; 838c506fff3SSebastian Ott 839c506fff3SSebastian Ott return 0; 840c506fff3SSebastian Ott error_iomap_bitmap: 841c506fff3SSebastian Ott kfree(zpci_iomap_start); 8429a4da8a5SJan Glauber error_iomap: 843d0b08853SJan Glauber kmem_cache_destroy(zdev_fmb_cache); 844c506fff3SSebastian Ott error_fmb: 845cd248341SJan Glauber return -ENOMEM; 846cd248341SJan Glauber } 847cd248341SJan Glauber 848cd248341SJan Glauber static void zpci_mem_exit(void) 849cd248341SJan Glauber { 850c506fff3SSebastian Ott kfree(zpci_iomap_bitmap); 851cd248341SJan Glauber kfree(zpci_iomap_start); 852d0b08853SJan Glauber kmem_cache_destroy(zdev_fmb_cache); 853cd248341SJan Glauber } 854cd248341SJan Glauber 8556324b4deSSebastian Ott static unsigned int s390_pci_probe __initdata = 1; 85656271303SSebastian Ott static unsigned int s390_pci_no_mio __initdata; 857fbfe07d4SSebastian Ott unsigned int s390_pci_force_floating __initdata; 858aa3b7c29SSebastian Ott static unsigned int s390_pci_initialized; 859cd248341SJan Glauber 860cd248341SJan Glauber char * __init pcibios_setup(char *str) 861cd248341SJan Glauber { 862257608fbSSebastian Ott if (!strcmp(str, "off")) { 863257608fbSSebastian Ott s390_pci_probe = 0; 864cd248341SJan Glauber return NULL; 865cd248341SJan Glauber } 86656271303SSebastian Ott if (!strcmp(str, "nomio")) { 86756271303SSebastian Ott s390_pci_no_mio = 1; 86856271303SSebastian Ott return NULL; 86956271303SSebastian Ott } 870fbfe07d4SSebastian Ott if (!strcmp(str, "force_floating")) { 871fbfe07d4SSebastian Ott s390_pci_force_floating = 1; 872fbfe07d4SSebastian Ott return NULL; 873fbfe07d4SSebastian Ott } 874cd248341SJan Glauber return str; 875cd248341SJan Glauber } 876cd248341SJan Glauber 877aa3b7c29SSebastian Ott bool zpci_is_enabled(void) 878aa3b7c29SSebastian Ott { 879aa3b7c29SSebastian Ott return s390_pci_initialized; 880aa3b7c29SSebastian Ott } 881aa3b7c29SSebastian Ott 882cd248341SJan Glauber static int __init pci_base_init(void) 883cd248341SJan Glauber { 884cd248341SJan Glauber int rc; 885cd248341SJan Glauber 8861e5635d1SHeiko Carstens if (!s390_pci_probe) 887cd248341SJan Glauber return 0; 888cd248341SJan Glauber 88948070c73SChristian Borntraeger if (!test_facility(69) || !test_facility(71)) 890cd248341SJan Glauber return 0; 891cd248341SJan Glauber 89256271303SSebastian Ott if (test_facility(153) && !s390_pci_no_mio) 89371ba41c9SSebastian Ott static_branch_enable(&have_mio); 89471ba41c9SSebastian Ott 895d0b08853SJan Glauber rc = zpci_debug_init(); 896d0b08853SJan Glauber if (rc) 8971f44a225SMartin Schwidefsky goto out; 898d0b08853SJan Glauber 899cd248341SJan Glauber rc = zpci_mem_init(); 900cd248341SJan Glauber if (rc) 901cd248341SJan Glauber goto out_mem; 902cd248341SJan Glauber 9039a4da8a5SJan Glauber rc = zpci_irq_init(); 9049a4da8a5SJan Glauber if (rc) 9059a4da8a5SJan Glauber goto out_irq; 9069a4da8a5SJan Glauber 907828b35f6SJan Glauber rc = zpci_dma_init(); 908828b35f6SJan Glauber if (rc) 909828b35f6SJan Glauber goto out_dma; 910828b35f6SJan Glauber 9111d578966SSebastian Ott rc = clp_scan_pci_devices(); 912a755a45dSJan Glauber if (rc) 913a755a45dSJan Glauber goto out_find; 914a755a45dSJan Glauber 915aa3b7c29SSebastian Ott s390_pci_initialized = 1; 916cd248341SJan Glauber return 0; 917cd248341SJan Glauber 918a755a45dSJan Glauber out_find: 919828b35f6SJan Glauber zpci_dma_exit(); 920828b35f6SJan Glauber out_dma: 9219a4da8a5SJan Glauber zpci_irq_exit(); 9229a4da8a5SJan Glauber out_irq: 923cd248341SJan Glauber zpci_mem_exit(); 924cd248341SJan Glauber out_mem: 925d0b08853SJan Glauber zpci_debug_exit(); 9261f44a225SMartin Schwidefsky out: 927cd248341SJan Glauber return rc; 928cd248341SJan Glauber } 92967f43f38SSebastian Ott subsys_initcall_sync(pci_base_init); 93057b5918cSSebastian Ott 93157b5918cSSebastian Ott void zpci_rescan(void) 93257b5918cSSebastian Ott { 933aa3b7c29SSebastian Ott if (zpci_is_enabled()) 93457b5918cSSebastian Ott clp_rescan_pci_devices_simple(); 93557b5918cSSebastian Ott } 936