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> 30794b8846SNiklas 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 3905bc1be6SPierre Morel #include "pci_bus.h" 40abb95b75SNiklas Schnelle #include "pci_iov.h" 4105bc1be6SPierre Morel 42cd248341SJan Glauber /* list of all detected zpci devices */ 4367f43f38SSebastian Ott static LIST_HEAD(zpci_list); 4457b5918cSSebastian Ott static DEFINE_SPINLOCK(zpci_list_lock); 451f44a225SMartin Schwidefsky 46969ae01bSNiklas Schnelle static DECLARE_BITMAP(zpci_domain, ZPCI_DOMAIN_BITMAP_SIZE); 47cd248341SJan Glauber static DEFINE_SPINLOCK(zpci_domain_lock); 48cd248341SJan Glauber 49c506fff3SSebastian Ott #define ZPCI_IOMAP_ENTRIES \ 50c9c13ba4SDenis Efremov min(((unsigned long) ZPCI_NR_DEVICES * PCI_STD_NUM_BARS / 2), \ 51c506fff3SSebastian Ott ZPCI_IOMAP_MAX_ENTRIES) 52c506fff3SSebastian Ott 536cf17f9aSPierre Morel unsigned int s390_pci_no_rid; 546cf17f9aSPierre Morel 55cd248341SJan Glauber static DEFINE_SPINLOCK(zpci_iomap_lock); 56c506fff3SSebastian Ott static unsigned long *zpci_iomap_bitmap; 57cd248341SJan Glauber struct zpci_iomap_entry *zpci_iomap_start; 58cd248341SJan Glauber EXPORT_SYMBOL_GPL(zpci_iomap_start); 59cd248341SJan Glauber 6071ba41c9SSebastian Ott DEFINE_STATIC_KEY_FALSE(have_mio); 6171ba41c9SSebastian Ott 62d0b08853SJan Glauber static struct kmem_cache *zdev_fmb_cache; 63d0b08853SJan Glauber 64cd248341SJan Glauber struct zpci_dev *get_zdev_by_fid(u32 fid) 65cd248341SJan Glauber { 66cd248341SJan Glauber struct zpci_dev *tmp, *zdev = NULL; 67cd248341SJan Glauber 6857b5918cSSebastian Ott spin_lock(&zpci_list_lock); 69cd248341SJan Glauber list_for_each_entry(tmp, &zpci_list, entry) { 70cd248341SJan Glauber if (tmp->fid == fid) { 71cd248341SJan Glauber zdev = tmp; 72cd248341SJan Glauber break; 73cd248341SJan Glauber } 74cd248341SJan Glauber } 7557b5918cSSebastian Ott spin_unlock(&zpci_list_lock); 76cd248341SJan Glauber return zdev; 77cd248341SJan Glauber } 78cd248341SJan Glauber 7901553d9aSSebastian Ott void zpci_remove_reserved_devices(void) 8001553d9aSSebastian Ott { 8101553d9aSSebastian Ott struct zpci_dev *tmp, *zdev; 8201553d9aSSebastian Ott enum zpci_state state; 8301553d9aSSebastian Ott LIST_HEAD(remove); 8401553d9aSSebastian Ott 8501553d9aSSebastian Ott spin_lock(&zpci_list_lock); 8601553d9aSSebastian Ott list_for_each_entry_safe(zdev, tmp, &zpci_list, entry) { 8701553d9aSSebastian Ott if (zdev->state == ZPCI_FN_STATE_STANDBY && 8801553d9aSSebastian Ott !clp_get_state(zdev->fid, &state) && 8901553d9aSSebastian Ott state == ZPCI_FN_STATE_RESERVED) 9001553d9aSSebastian Ott list_move_tail(&zdev->entry, &remove); 9101553d9aSSebastian Ott } 9201553d9aSSebastian Ott spin_unlock(&zpci_list_lock); 9301553d9aSSebastian Ott 9401553d9aSSebastian Ott list_for_each_entry_safe(zdev, tmp, &remove, entry) 9505bc1be6SPierre Morel zpci_zdev_put(zdev); 96cd248341SJan Glauber } 97cd248341SJan Glauber 98cd248341SJan Glauber int pci_domain_nr(struct pci_bus *bus) 99cd248341SJan Glauber { 10005bc1be6SPierre Morel return ((struct zpci_bus *) bus->sysdata)->domain_nr; 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 230b02002ccSNiklas Schnelle static void __iomem *__ioremap(phys_addr_t addr, size_t size, pgprot_t prot) 23171ba41c9SSebastian Ott { 232a999eb96SNiklas Schnelle unsigned long offset, vaddr; 23371ba41c9SSebastian Ott struct vm_struct *area; 234a999eb96SNiklas Schnelle phys_addr_t last_addr; 23571ba41c9SSebastian Ott 236a999eb96SNiklas Schnelle last_addr = addr + size - 1; 237a999eb96SNiklas Schnelle if (!size || last_addr < addr) 23871ba41c9SSebastian Ott return NULL; 23971ba41c9SSebastian Ott 24071ba41c9SSebastian Ott if (!static_branch_unlikely(&have_mio)) 241a999eb96SNiklas Schnelle return (void __iomem *) addr; 24271ba41c9SSebastian Ott 243a999eb96SNiklas Schnelle offset = addr & ~PAGE_MASK; 244a999eb96SNiklas Schnelle addr &= PAGE_MASK; 24571ba41c9SSebastian Ott size = PAGE_ALIGN(size + offset); 24671ba41c9SSebastian Ott area = get_vm_area(size, VM_IOREMAP); 24771ba41c9SSebastian Ott if (!area) 24871ba41c9SSebastian Ott return NULL; 24971ba41c9SSebastian Ott 250a999eb96SNiklas Schnelle vaddr = (unsigned long) area->addr; 251b02002ccSNiklas Schnelle if (ioremap_page_range(vaddr, vaddr + size, addr, prot)) { 252a999eb96SNiklas Schnelle free_vm_area(area); 25371ba41c9SSebastian Ott return NULL; 25471ba41c9SSebastian Ott } 25571ba41c9SSebastian Ott return (void __iomem *) ((unsigned long) area->addr + offset); 25671ba41c9SSebastian Ott } 257b02002ccSNiklas Schnelle 258b02002ccSNiklas Schnelle void __iomem *ioremap_prot(phys_addr_t addr, size_t size, unsigned long prot) 259b02002ccSNiklas Schnelle { 260b02002ccSNiklas Schnelle return __ioremap(addr, size, __pgprot(prot)); 261b02002ccSNiklas Schnelle } 262b02002ccSNiklas Schnelle EXPORT_SYMBOL(ioremap_prot); 263b02002ccSNiklas Schnelle 264b02002ccSNiklas Schnelle void __iomem *ioremap(phys_addr_t addr, size_t size) 265b02002ccSNiklas Schnelle { 266b02002ccSNiklas Schnelle return __ioremap(addr, size, PAGE_KERNEL); 267b02002ccSNiklas Schnelle } 26871ba41c9SSebastian Ott EXPORT_SYMBOL(ioremap); 26971ba41c9SSebastian Ott 270b02002ccSNiklas Schnelle void __iomem *ioremap_wc(phys_addr_t addr, size_t size) 271b02002ccSNiklas Schnelle { 272b02002ccSNiklas Schnelle return __ioremap(addr, size, pgprot_writecombine(PAGE_KERNEL)); 273b02002ccSNiklas Schnelle } 274b02002ccSNiklas Schnelle EXPORT_SYMBOL(ioremap_wc); 275b02002ccSNiklas Schnelle 276b02002ccSNiklas Schnelle void __iomem *ioremap_wt(phys_addr_t addr, size_t size) 277b02002ccSNiklas Schnelle { 278b02002ccSNiklas Schnelle return __ioremap(addr, size, pgprot_writethrough(PAGE_KERNEL)); 279b02002ccSNiklas Schnelle } 280b02002ccSNiklas Schnelle EXPORT_SYMBOL(ioremap_wt); 281b02002ccSNiklas Schnelle 28271ba41c9SSebastian Ott void iounmap(volatile void __iomem *addr) 28371ba41c9SSebastian Ott { 28471ba41c9SSebastian Ott if (static_branch_likely(&have_mio)) 28571ba41c9SSebastian Ott vunmap((__force void *) ((unsigned long) addr & PAGE_MASK)); 28671ba41c9SSebastian Ott } 28771ba41c9SSebastian Ott EXPORT_SYMBOL(iounmap); 28871ba41c9SSebastian Ott 289cd248341SJan Glauber /* Create a virtual mapping cookie for a PCI BAR */ 29071ba41c9SSebastian Ott static void __iomem *pci_iomap_range_fh(struct pci_dev *pdev, int bar, 29171ba41c9SSebastian Ott unsigned long offset, unsigned long max) 292cd248341SJan Glauber { 293198a5278SSebastian Ott struct zpci_dev *zdev = to_zpci(pdev); 294cd248341SJan Glauber int idx; 295cd248341SJan Glauber 296cd248341SJan Glauber idx = zdev->bars[bar].map_idx; 297cd248341SJan Glauber spin_lock(&zpci_iomap_lock); 298f5e44f82SSebastian Ott /* Detect overrun */ 299f5e44f82SSebastian Ott WARN_ON(!++zpci_iomap_start[idx].count); 300cd248341SJan Glauber zpci_iomap_start[idx].fh = zdev->fh; 301cd248341SJan Glauber zpci_iomap_start[idx].bar = bar; 302cd248341SJan Glauber spin_unlock(&zpci_iomap_lock); 303cd248341SJan Glauber 3049e00caaeSSebastian Ott return (void __iomem *) ZPCI_ADDR(idx) + offset; 305cd248341SJan Glauber } 30671ba41c9SSebastian Ott 30771ba41c9SSebastian Ott static void __iomem *pci_iomap_range_mio(struct pci_dev *pdev, int bar, 30871ba41c9SSebastian Ott unsigned long offset, 30971ba41c9SSebastian Ott unsigned long max) 31071ba41c9SSebastian Ott { 31171ba41c9SSebastian Ott unsigned long barsize = pci_resource_len(pdev, bar); 31271ba41c9SSebastian Ott struct zpci_dev *zdev = to_zpci(pdev); 31371ba41c9SSebastian Ott void __iomem *iova; 31471ba41c9SSebastian Ott 31571ba41c9SSebastian Ott iova = ioremap((unsigned long) zdev->bars[bar].mio_wt, barsize); 31671ba41c9SSebastian Ott return iova ? iova + offset : iova; 31771ba41c9SSebastian Ott } 31871ba41c9SSebastian Ott 31971ba41c9SSebastian Ott void __iomem *pci_iomap_range(struct pci_dev *pdev, int bar, 32071ba41c9SSebastian Ott unsigned long offset, unsigned long max) 32171ba41c9SSebastian Ott { 322c9c13ba4SDenis Efremov if (bar >= PCI_STD_NUM_BARS || !pci_resource_len(pdev, bar)) 32371ba41c9SSebastian Ott return NULL; 32471ba41c9SSebastian Ott 32571ba41c9SSebastian Ott if (static_branch_likely(&have_mio)) 32671ba41c9SSebastian Ott return pci_iomap_range_mio(pdev, bar, offset, max); 32771ba41c9SSebastian Ott else 32871ba41c9SSebastian Ott return pci_iomap_range_fh(pdev, bar, offset, max); 32971ba41c9SSebastian Ott } 330d9426083SSebastian Ott EXPORT_SYMBOL(pci_iomap_range); 3318cfc99b5SMichael S. Tsirkin 3328cfc99b5SMichael S. Tsirkin void __iomem *pci_iomap(struct pci_dev *dev, int bar, unsigned long maxlen) 3338cfc99b5SMichael S. Tsirkin { 3348cfc99b5SMichael S. Tsirkin return pci_iomap_range(dev, bar, 0, maxlen); 3358cfc99b5SMichael S. Tsirkin } 3368cfc99b5SMichael S. Tsirkin EXPORT_SYMBOL(pci_iomap); 337cd248341SJan Glauber 33871ba41c9SSebastian Ott static void __iomem *pci_iomap_wc_range_mio(struct pci_dev *pdev, int bar, 33971ba41c9SSebastian Ott unsigned long offset, unsigned long max) 34071ba41c9SSebastian Ott { 34171ba41c9SSebastian Ott unsigned long barsize = pci_resource_len(pdev, bar); 34271ba41c9SSebastian Ott struct zpci_dev *zdev = to_zpci(pdev); 34371ba41c9SSebastian Ott void __iomem *iova; 34471ba41c9SSebastian Ott 34571ba41c9SSebastian Ott iova = ioremap((unsigned long) zdev->bars[bar].mio_wb, barsize); 34671ba41c9SSebastian Ott return iova ? iova + offset : iova; 34771ba41c9SSebastian Ott } 34871ba41c9SSebastian Ott 34971ba41c9SSebastian Ott void __iomem *pci_iomap_wc_range(struct pci_dev *pdev, int bar, 35071ba41c9SSebastian Ott unsigned long offset, unsigned long max) 35171ba41c9SSebastian Ott { 352c9c13ba4SDenis Efremov if (bar >= PCI_STD_NUM_BARS || !pci_resource_len(pdev, bar)) 35371ba41c9SSebastian Ott return NULL; 35471ba41c9SSebastian Ott 35571ba41c9SSebastian Ott if (static_branch_likely(&have_mio)) 35671ba41c9SSebastian Ott return pci_iomap_wc_range_mio(pdev, bar, offset, max); 35771ba41c9SSebastian Ott else 35871ba41c9SSebastian Ott return pci_iomap_range_fh(pdev, bar, offset, max); 35971ba41c9SSebastian Ott } 36071ba41c9SSebastian Ott EXPORT_SYMBOL(pci_iomap_wc_range); 36171ba41c9SSebastian Ott 36271ba41c9SSebastian Ott void __iomem *pci_iomap_wc(struct pci_dev *dev, int bar, unsigned long maxlen) 36371ba41c9SSebastian Ott { 36471ba41c9SSebastian Ott return pci_iomap_wc_range(dev, bar, 0, maxlen); 36571ba41c9SSebastian Ott } 36671ba41c9SSebastian Ott EXPORT_SYMBOL(pci_iomap_wc); 36771ba41c9SSebastian Ott 36871ba41c9SSebastian Ott static void pci_iounmap_fh(struct pci_dev *pdev, void __iomem *addr) 369cd248341SJan Glauber { 3709e00caaeSSebastian Ott unsigned int idx = ZPCI_IDX(addr); 371cd248341SJan Glauber 372cd248341SJan Glauber spin_lock(&zpci_iomap_lock); 3738cfc99b5SMichael S. Tsirkin /* Detect underrun */ 374f5e44f82SSebastian Ott WARN_ON(!zpci_iomap_start[idx].count); 3758cfc99b5SMichael S. Tsirkin if (!--zpci_iomap_start[idx].count) { 376cd248341SJan Glauber zpci_iomap_start[idx].fh = 0; 377cd248341SJan Glauber zpci_iomap_start[idx].bar = 0; 3788cfc99b5SMichael S. Tsirkin } 379cd248341SJan Glauber spin_unlock(&zpci_iomap_lock); 380cd248341SJan Glauber } 38171ba41c9SSebastian Ott 38271ba41c9SSebastian Ott static void pci_iounmap_mio(struct pci_dev *pdev, void __iomem *addr) 38371ba41c9SSebastian Ott { 38471ba41c9SSebastian Ott iounmap(addr); 38571ba41c9SSebastian Ott } 38671ba41c9SSebastian Ott 38771ba41c9SSebastian Ott void pci_iounmap(struct pci_dev *pdev, void __iomem *addr) 38871ba41c9SSebastian Ott { 38971ba41c9SSebastian Ott if (static_branch_likely(&have_mio)) 39071ba41c9SSebastian Ott pci_iounmap_mio(pdev, addr); 39171ba41c9SSebastian Ott else 39271ba41c9SSebastian Ott pci_iounmap_fh(pdev, addr); 39371ba41c9SSebastian Ott } 394d9426083SSebastian Ott EXPORT_SYMBOL(pci_iounmap); 395cd248341SJan Glauber 396cd248341SJan Glauber static int pci_read(struct pci_bus *bus, unsigned int devfn, int where, 397cd248341SJan Glauber int size, u32 *val) 398cd248341SJan Glauber { 39944510d6fSPierre Morel struct zpci_dev *zdev = get_zdev_by_bus(bus, devfn); 400cd248341SJan Glauber 40144510d6fSPierre Morel return (zdev) ? zpci_cfg_load(zdev, where, val, size) : -ENODEV; 402cd248341SJan Glauber } 403cd248341SJan Glauber 404cd248341SJan Glauber static int pci_write(struct pci_bus *bus, unsigned int devfn, int where, 405cd248341SJan Glauber int size, u32 val) 406cd248341SJan Glauber { 40744510d6fSPierre Morel struct zpci_dev *zdev = get_zdev_by_bus(bus, devfn); 408cd248341SJan Glauber 40944510d6fSPierre Morel return (zdev) ? zpci_cfg_store(zdev, where, val, size) : -ENODEV; 410cd248341SJan Glauber } 411cd248341SJan Glauber 412cd248341SJan Glauber static struct pci_ops pci_root_ops = { 413cd248341SJan Glauber .read = pci_read, 414cd248341SJan Glauber .write = pci_write, 415cd248341SJan Glauber }; 416cd248341SJan Glauber 4171803ba2dSSebastian Ott static void zpci_map_resources(struct pci_dev *pdev) 418cd248341SJan Glauber { 41971ba41c9SSebastian Ott struct zpci_dev *zdev = to_zpci(pdev); 420cd248341SJan Glauber resource_size_t len; 421cd248341SJan Glauber int i; 422cd248341SJan Glauber 423c9c13ba4SDenis Efremov for (i = 0; i < PCI_STD_NUM_BARS; i++) { 424cd248341SJan Glauber len = pci_resource_len(pdev, i); 425cd248341SJan Glauber if (!len) 426cd248341SJan Glauber continue; 42771ba41c9SSebastian Ott 428c7ff0e91SSebastian Ott if (zpci_use_mio(zdev)) 42971ba41c9SSebastian Ott pdev->resource[i].start = 430df057c91SNiklas Schnelle (resource_size_t __force) zdev->bars[i].mio_wt; 43171ba41c9SSebastian Ott else 432c7ff0e91SSebastian Ott pdev->resource[i].start = (resource_size_t __force) 433c7ff0e91SSebastian Ott pci_iomap_range_fh(pdev, i, 0, 0); 434cd248341SJan Glauber pdev->resource[i].end = pdev->resource[i].start + len - 1; 435cd248341SJan Glauber } 436cfbb4a7aSSebastian Ott 437abb95b75SNiklas Schnelle zpci_iov_map_resources(pdev); 438944239c5SSebastian Ott } 439944239c5SSebastian Ott 4401803ba2dSSebastian Ott static void zpci_unmap_resources(struct pci_dev *pdev) 441944239c5SSebastian Ott { 442c7ff0e91SSebastian Ott struct zpci_dev *zdev = to_zpci(pdev); 443944239c5SSebastian Ott resource_size_t len; 444944239c5SSebastian Ott int i; 445944239c5SSebastian Ott 446c7ff0e91SSebastian Ott if (zpci_use_mio(zdev)) 44771ba41c9SSebastian Ott return; 44871ba41c9SSebastian Ott 449c9c13ba4SDenis Efremov for (i = 0; i < PCI_STD_NUM_BARS; i++) { 450944239c5SSebastian Ott len = pci_resource_len(pdev, i); 451944239c5SSebastian Ott if (!len) 452944239c5SSebastian Ott continue; 453c7ff0e91SSebastian Ott pci_iounmap_fh(pdev, (void __iomem __force *) 4545b9f2081SMartin Schwidefsky pdev->resource[i].start); 455944239c5SSebastian Ott } 456944239c5SSebastian Ott } 457cd248341SJan Glauber 458cd248341SJan Glauber static int zpci_alloc_iomap(struct zpci_dev *zdev) 459cd248341SJan Glauber { 460bf19c94dSSebastian Ott unsigned long entry; 461cd248341SJan Glauber 462cd248341SJan Glauber spin_lock(&zpci_iomap_lock); 463c506fff3SSebastian Ott entry = find_first_zero_bit(zpci_iomap_bitmap, ZPCI_IOMAP_ENTRIES); 464c506fff3SSebastian Ott if (entry == ZPCI_IOMAP_ENTRIES) { 465cd248341SJan Glauber spin_unlock(&zpci_iomap_lock); 466cd248341SJan Glauber return -ENOSPC; 467cd248341SJan Glauber } 468c506fff3SSebastian Ott set_bit(entry, zpci_iomap_bitmap); 469cd248341SJan Glauber spin_unlock(&zpci_iomap_lock); 470cd248341SJan Glauber return entry; 471cd248341SJan Glauber } 472cd248341SJan Glauber 473cd248341SJan Glauber static void zpci_free_iomap(struct zpci_dev *zdev, int entry) 474cd248341SJan Glauber { 475cd248341SJan Glauber spin_lock(&zpci_iomap_lock); 476cd248341SJan Glauber memset(&zpci_iomap_start[entry], 0, sizeof(struct zpci_iomap_entry)); 477c506fff3SSebastian Ott clear_bit(entry, zpci_iomap_bitmap); 478cd248341SJan Glauber spin_unlock(&zpci_iomap_lock); 479cd248341SJan Glauber } 480cd248341SJan Glauber 4817a572a3aSSebastian Ott static struct resource *__alloc_res(struct zpci_dev *zdev, unsigned long start, 4827a572a3aSSebastian Ott unsigned long size, unsigned long flags) 4837a572a3aSSebastian Ott { 4847a572a3aSSebastian Ott struct resource *r; 4857a572a3aSSebastian Ott 4867a572a3aSSebastian Ott r = kzalloc(sizeof(*r), GFP_KERNEL); 4877a572a3aSSebastian Ott if (!r) 4887a572a3aSSebastian Ott return NULL; 4897a572a3aSSebastian Ott 4907a572a3aSSebastian Ott r->start = start; 4917a572a3aSSebastian Ott r->end = r->start + size - 1; 4927a572a3aSSebastian Ott r->flags = flags; 4937a572a3aSSebastian Ott r->name = zdev->res_name; 4947a572a3aSSebastian Ott 4957a572a3aSSebastian Ott if (request_resource(&iomem_resource, r)) { 4967a572a3aSSebastian Ott kfree(r); 4977a572a3aSSebastian Ott return NULL; 4987a572a3aSSebastian Ott } 4997a572a3aSSebastian Ott return r; 5007a572a3aSSebastian Ott } 5017a572a3aSSebastian Ott 50205bc1be6SPierre Morel int zpci_setup_bus_resources(struct zpci_dev *zdev, 5037a572a3aSSebastian Ott struct list_head *resources) 5047a572a3aSSebastian Ott { 5057a572a3aSSebastian Ott unsigned long addr, size, flags; 5067a572a3aSSebastian Ott struct resource *res; 5077a572a3aSSebastian Ott int i, entry; 5087a572a3aSSebastian Ott 5097a572a3aSSebastian Ott snprintf(zdev->res_name, sizeof(zdev->res_name), 51005bc1be6SPierre Morel "PCI Bus %04x:%02x", zdev->uid, ZPCI_BUS_NR); 5117a572a3aSSebastian Ott 512c9c13ba4SDenis Efremov for (i = 0; i < PCI_STD_NUM_BARS; i++) { 5137a572a3aSSebastian Ott if (!zdev->bars[i].size) 5147a572a3aSSebastian Ott continue; 5157a572a3aSSebastian Ott entry = zpci_alloc_iomap(zdev); 5167a572a3aSSebastian Ott if (entry < 0) 5177a572a3aSSebastian Ott return entry; 5187a572a3aSSebastian Ott zdev->bars[i].map_idx = entry; 5197a572a3aSSebastian Ott 5207a572a3aSSebastian Ott /* only MMIO is supported */ 5217a572a3aSSebastian Ott flags = IORESOURCE_MEM; 5227a572a3aSSebastian Ott if (zdev->bars[i].val & 8) 5237a572a3aSSebastian Ott flags |= IORESOURCE_PREFETCH; 5247a572a3aSSebastian Ott if (zdev->bars[i].val & 4) 5257a572a3aSSebastian Ott flags |= IORESOURCE_MEM_64; 5267a572a3aSSebastian Ott 527c7ff0e91SSebastian Ott if (zpci_use_mio(zdev)) 528df057c91SNiklas Schnelle addr = (unsigned long) zdev->bars[i].mio_wt; 529dcd33b23SSebastian Ott else 5309e00caaeSSebastian Ott addr = ZPCI_ADDR(entry); 5317a572a3aSSebastian Ott size = 1UL << zdev->bars[i].size; 5327a572a3aSSebastian Ott 5337a572a3aSSebastian Ott res = __alloc_res(zdev, addr, size, flags); 5347a572a3aSSebastian Ott if (!res) { 5357a572a3aSSebastian Ott zpci_free_iomap(zdev, entry); 5367a572a3aSSebastian Ott return -ENOMEM; 5377a572a3aSSebastian Ott } 5387a572a3aSSebastian Ott zdev->bars[i].res = res; 5397a572a3aSSebastian Ott pci_add_resource(resources, res); 5407a572a3aSSebastian Ott } 541*a50297cfSNiklas Schnelle zdev->has_resources = 1; 5427a572a3aSSebastian Ott 5437a572a3aSSebastian Ott return 0; 5447a572a3aSSebastian Ott } 5457a572a3aSSebastian Ott 5467a572a3aSSebastian Ott static void zpci_cleanup_bus_resources(struct zpci_dev *zdev) 5477a572a3aSSebastian Ott { 5487a572a3aSSebastian Ott int i; 5497a572a3aSSebastian Ott 550c9c13ba4SDenis Efremov for (i = 0; i < PCI_STD_NUM_BARS; i++) { 5512b1df724SSebastian Ott if (!zdev->bars[i].size || !zdev->bars[i].res) 5527a572a3aSSebastian Ott continue; 5537a572a3aSSebastian Ott 5547a572a3aSSebastian Ott zpci_free_iomap(zdev, zdev->bars[i].map_idx); 5557a572a3aSSebastian Ott release_resource(zdev->bars[i].res); 5567a572a3aSSebastian Ott kfree(zdev->bars[i].res); 5577a572a3aSSebastian Ott } 558*a50297cfSNiklas Schnelle zdev->has_resources = 0; 5597a572a3aSSebastian Ott } 5607a572a3aSSebastian Ott 561af0a8a84SSebastian Ott int pcibios_add_device(struct pci_dev *pdev) 562af0a8a84SSebastian Ott { 563cb809182SSebastian Ott struct resource *res; 564cb809182SSebastian Ott int i; 565cb809182SSebastian Ott 5667dc20ab1SSebastian Ott if (pdev->is_physfn) 5677dc20ab1SSebastian Ott pdev->no_vf_scan = 1; 5687dc20ab1SSebastian Ott 569ef4858c6SSebastian Ott pdev->dev.groups = zpci_attr_groups; 5705657933dSBart Van Assche pdev->dev.dma_ops = &s390_pci_dma_ops; 5711803ba2dSSebastian Ott zpci_map_resources(pdev); 572cb809182SSebastian Ott 573c9c13ba4SDenis Efremov for (i = 0; i < PCI_STD_NUM_BARS; i++) { 574cb809182SSebastian Ott res = &pdev->resource[i]; 575cb809182SSebastian Ott if (res->parent || !res->flags) 576cb809182SSebastian Ott continue; 577cb809182SSebastian Ott pci_claim_resource(pdev, i); 578cb809182SSebastian Ott } 579cb809182SSebastian Ott 580cb809182SSebastian Ott return 0; 581cb809182SSebastian Ott } 582cb809182SSebastian Ott 5831803ba2dSSebastian Ott void pcibios_release_device(struct pci_dev *pdev) 5841803ba2dSSebastian Ott { 5851803ba2dSSebastian Ott zpci_unmap_resources(pdev); 5861803ba2dSSebastian Ott } 5871803ba2dSSebastian Ott 588cb809182SSebastian Ott int pcibios_enable_device(struct pci_dev *pdev, int mask) 589cb809182SSebastian Ott { 590198a5278SSebastian Ott struct zpci_dev *zdev = to_zpci(pdev); 591af0a8a84SSebastian Ott 5929a99649fSSebastian Ott zpci_debug_init_device(zdev, dev_name(&pdev->dev)); 593af0a8a84SSebastian Ott zpci_fmb_enable_device(zdev); 594af0a8a84SSebastian Ott 595d7533232SBjorn Helgaas return pci_enable_resources(pdev, mask); 596af0a8a84SSebastian Ott } 597af0a8a84SSebastian Ott 598cb809182SSebastian Ott void pcibios_disable_device(struct pci_dev *pdev) 599944239c5SSebastian Ott { 600198a5278SSebastian Ott struct zpci_dev *zdev = to_zpci(pdev); 601944239c5SSebastian Ott 602944239c5SSebastian Ott zpci_fmb_disable_device(zdev); 603944239c5SSebastian Ott zpci_debug_exit_device(zdev); 604944239c5SSebastian Ott } 605944239c5SSebastian Ott 60605bc1be6SPierre Morel static int __zpci_register_domain(int domain) 607cd248341SJan Glauber { 608969ae01bSNiklas Schnelle spin_lock(&zpci_domain_lock); 60905bc1be6SPierre Morel if (test_bit(domain, zpci_domain)) { 610969ae01bSNiklas Schnelle spin_unlock(&zpci_domain_lock); 61105bc1be6SPierre Morel pr_err("Domain %04x is already assigned\n", domain); 612312e8462SSebastian Ott return -EEXIST; 613312e8462SSebastian Ott } 61405bc1be6SPierre Morel set_bit(domain, zpci_domain); 615312e8462SSebastian Ott spin_unlock(&zpci_domain_lock); 61605bc1be6SPierre Morel return domain; 6175c5afd02SSebastian Ott } 61805bc1be6SPierre Morel 61905bc1be6SPierre Morel static int __zpci_alloc_domain(void) 62005bc1be6SPierre Morel { 62105bc1be6SPierre Morel int domain; 62205bc1be6SPierre Morel 62305bc1be6SPierre Morel spin_lock(&zpci_domain_lock); 624969ae01bSNiklas Schnelle /* 625969ae01bSNiklas Schnelle * We can always auto allocate domains below ZPCI_NR_DEVICES. 626969ae01bSNiklas Schnelle * There is either a free domain or we have reached the maximum in 627969ae01bSNiklas Schnelle * which case we would have bailed earlier. 628969ae01bSNiklas Schnelle */ 62905bc1be6SPierre Morel domain = find_first_zero_bit(zpci_domain, ZPCI_NR_DEVICES); 63005bc1be6SPierre Morel set_bit(domain, zpci_domain); 631cd248341SJan Glauber spin_unlock(&zpci_domain_lock); 63205bc1be6SPierre Morel return domain; 633cd248341SJan Glauber } 634cd248341SJan Glauber 63505bc1be6SPierre Morel int zpci_alloc_domain(int domain) 63605bc1be6SPierre Morel { 63705bc1be6SPierre Morel if (zpci_unique_uid) { 63805bc1be6SPierre Morel if (domain) 63905bc1be6SPierre Morel return __zpci_register_domain(domain); 64005bc1be6SPierre Morel pr_warn("UID checking was active but no UID is provided: switching to automatic domain allocation\n"); 64105bc1be6SPierre Morel update_uid_checking(false); 64205bc1be6SPierre Morel } 64305bc1be6SPierre Morel return __zpci_alloc_domain(); 64405bc1be6SPierre Morel } 64505bc1be6SPierre Morel 64605bc1be6SPierre Morel void zpci_free_domain(int domain) 647cd248341SJan Glauber { 648cd248341SJan Glauber spin_lock(&zpci_domain_lock); 64905bc1be6SPierre Morel clear_bit(domain, zpci_domain); 650cd248341SJan Glauber spin_unlock(&zpci_domain_lock); 651cd248341SJan Glauber } 652cd248341SJan Glauber 6537d594322SSebastian Ott 654a755a45dSJan Glauber int zpci_enable_device(struct zpci_dev *zdev) 655a755a45dSJan Glauber { 656a755a45dSJan Glauber int rc; 657a755a45dSJan Glauber 658a755a45dSJan Glauber rc = clp_enable_fh(zdev, ZPCI_NR_DMA_SPACES); 659a755a45dSJan Glauber if (rc) 660a755a45dSJan Glauber goto out; 661828b35f6SJan Glauber 662828b35f6SJan Glauber rc = zpci_dma_init_device(zdev); 663828b35f6SJan Glauber if (rc) 664828b35f6SJan Glauber goto out_dma; 6650ff70ec8SSebastian Ott 666a755a45dSJan Glauber return 0; 667828b35f6SJan Glauber 668828b35f6SJan Glauber out_dma: 669828b35f6SJan Glauber clp_disable_fh(zdev); 670a755a45dSJan Glauber out: 671a755a45dSJan Glauber return rc; 672a755a45dSJan Glauber } 673a755a45dSJan Glauber 674cb65a669SSebastian Ott int zpci_disable_device(struct zpci_dev *zdev) 675cb65a669SSebastian Ott { 676cb65a669SSebastian Ott zpci_dma_exit_device(zdev); 677afdf9550SNiklas Schnelle /* 678afdf9550SNiklas Schnelle * The zPCI function may already be disabled by the platform, this is 679afdf9550SNiklas Schnelle * detected in clp_disable_fh() which becomes a no-op. 680afdf9550SNiklas Schnelle */ 681cb65a669SSebastian Ott return clp_disable_fh(zdev); 682cb65a669SSebastian Ott } 683cb65a669SSebastian Ott 684ba764dd7SNiklas Schnelle /** 685ba764dd7SNiklas Schnelle * zpci_create_device() - Create a new zpci_dev and add it to the zbus 686ba764dd7SNiklas Schnelle * @fid: Function ID of the device to be created 687ba764dd7SNiklas Schnelle * @fh: Current Function Handle of the device to be created 688ba764dd7SNiklas Schnelle * @state: Initial state after creation either Standby or Configured 689ba764dd7SNiklas Schnelle * 690ba764dd7SNiklas Schnelle * Creates a new zpci device and adds it to its, possibly newly created, zbus 691ba764dd7SNiklas Schnelle * as well as zpci_list. 692ba764dd7SNiklas Schnelle * 693ba764dd7SNiklas Schnelle * Returns: 0 on success, an error value otherwise 694ba764dd7SNiklas Schnelle */ 695ba764dd7SNiklas Schnelle int zpci_create_device(u32 fid, u32 fh, enum zpci_state state) 696cd248341SJan Glauber { 697ba764dd7SNiklas Schnelle struct zpci_dev *zdev; 698cd248341SJan Glauber int rc; 699cd248341SJan Glauber 700ba764dd7SNiklas Schnelle zpci_dbg(3, "add fid:%x, fh:%x, c:%d\n", fid, fh, state); 701ba764dd7SNiklas Schnelle zdev = kzalloc(sizeof(*zdev), GFP_KERNEL); 702ba764dd7SNiklas Schnelle if (!zdev) 703ba764dd7SNiklas Schnelle return -ENOMEM; 704ba764dd7SNiklas Schnelle 705ba764dd7SNiklas Schnelle /* FID and Function Handle are the static/dynamic identifiers */ 706ba764dd7SNiklas Schnelle zdev->fid = fid; 707ba764dd7SNiklas Schnelle zdev->fh = fh; 708ba764dd7SNiklas Schnelle 709ba764dd7SNiklas Schnelle /* Query function properties and update zdev */ 710ba764dd7SNiklas Schnelle rc = clp_query_pci_fn(zdev); 711ba764dd7SNiklas Schnelle if (rc) 712ba764dd7SNiklas Schnelle goto error; 713ba764dd7SNiklas Schnelle zdev->state = state; 714ba764dd7SNiklas Schnelle 71505bc1be6SPierre Morel kref_init(&zdev->kref); 716ba764dd7SNiklas Schnelle mutex_init(&zdev->lock); 717ba764dd7SNiklas Schnelle 718ba764dd7SNiklas Schnelle rc = zpci_init_iommu(zdev); 719ba764dd7SNiklas Schnelle if (rc) 720ba764dd7SNiklas Schnelle goto error; 721ba764dd7SNiklas Schnelle 722ba764dd7SNiklas Schnelle rc = zpci_bus_device_register(zdev, &pci_root_ops); 723ba764dd7SNiklas Schnelle if (rc) 724*a50297cfSNiklas Schnelle goto error_destroy_iommu; 72505bc1be6SPierre Morel 72605bc1be6SPierre Morel spin_lock(&zpci_list_lock); 72705bc1be6SPierre Morel list_add_tail(&zdev->entry, &zpci_list); 72805bc1be6SPierre Morel spin_unlock(&zpci_list_lock); 729cd248341SJan Glauber 730cd248341SJan Glauber return 0; 731cd248341SJan Glauber 732ba764dd7SNiklas Schnelle error_destroy_iommu: 733f42c2235SJoerg Roedel zpci_destroy_iommu(zdev); 734ba764dd7SNiklas Schnelle error: 735ba764dd7SNiklas Schnelle zpci_dbg(0, "add fid:%x, rc:%d\n", fid, rc); 736ba764dd7SNiklas Schnelle kfree(zdev); 737cd248341SJan Glauber return rc; 738cd248341SJan Glauber } 739cd248341SJan Glauber 7402631f6b6SNiklas Schnelle /** 7412631f6b6SNiklas Schnelle * zpci_configure_device() - Configure a zpci_dev 7422631f6b6SNiklas Schnelle * @zdev: The zpci_dev to be configured 7432631f6b6SNiklas Schnelle * @fh: The general function handle supplied by the platform 7442631f6b6SNiklas Schnelle * 7452631f6b6SNiklas Schnelle * Configuring a device includes the configuration itself, if not done by the 7462631f6b6SNiklas Schnelle * platform, enabling, scanning and adding it to the common code PCI subsystem. 747*a50297cfSNiklas Schnelle * If any failure occurs, the zpci_dev is left disabled either in Standby if 748*a50297cfSNiklas Schnelle * the configuration failed or Configured if enabling or scanning failed. 7492631f6b6SNiklas Schnelle * 7502631f6b6SNiklas Schnelle * Return: 0 on success, or an error code otherwise 7512631f6b6SNiklas Schnelle */ 7522631f6b6SNiklas Schnelle int zpci_configure_device(struct zpci_dev *zdev, u32 fh) 7532631f6b6SNiklas Schnelle { 7542631f6b6SNiklas Schnelle int rc; 7552631f6b6SNiklas Schnelle 7562631f6b6SNiklas Schnelle zdev->fh = fh; 7572631f6b6SNiklas Schnelle if (zdev->state != ZPCI_FN_STATE_CONFIGURED) { 7582631f6b6SNiklas Schnelle rc = sclp_pci_configure(zdev->fid); 7592631f6b6SNiklas Schnelle zpci_dbg(3, "conf fid:%x, rc:%d\n", zdev->fid, rc); 7602631f6b6SNiklas Schnelle if (rc) 7612631f6b6SNiklas Schnelle return rc; 7622631f6b6SNiklas Schnelle zdev->state = ZPCI_FN_STATE_CONFIGURED; 7632631f6b6SNiklas Schnelle } 7642631f6b6SNiklas Schnelle 7652631f6b6SNiklas Schnelle /* the PCI function will be scanned once function 0 appears */ 7662631f6b6SNiklas Schnelle if (!zdev->zbus->bus) 7672631f6b6SNiklas Schnelle return 0; 7682631f6b6SNiklas Schnelle 769*a50297cfSNiklas Schnelle /* For function 0 on a multi-function bus scan whole bus as we might 770*a50297cfSNiklas Schnelle * have to pick up existing functions waiting for it to allow creating 771*a50297cfSNiklas Schnelle * the PCI bus 772*a50297cfSNiklas Schnelle */ 773*a50297cfSNiklas Schnelle if (zdev->devfn == 0 && zdev->zbus->multifunction) 774*a50297cfSNiklas Schnelle rc = zpci_bus_scan_bus(zdev->zbus); 775*a50297cfSNiklas Schnelle else 776faf29a4dSNiklas Schnelle rc = zpci_bus_scan_device(zdev); 7772631f6b6SNiklas Schnelle 7782631f6b6SNiklas Schnelle return rc; 7792631f6b6SNiklas Schnelle } 7802631f6b6SNiklas Schnelle 7812631f6b6SNiklas Schnelle /** 7822631f6b6SNiklas Schnelle * zpci_deconfigure_device() - Deconfigure a zpci_dev 7832631f6b6SNiklas Schnelle * @zdev: The zpci_dev to configure 7842631f6b6SNiklas Schnelle * 7852631f6b6SNiklas Schnelle * Deconfigure a zPCI function that is currently configured and possibly known 7862631f6b6SNiklas Schnelle * to the common code PCI subsystem. 7872631f6b6SNiklas Schnelle * If any failure occurs the device is left as is. 7882631f6b6SNiklas Schnelle * 7892631f6b6SNiklas Schnelle * Return: 0 on success, or an error code otherwise 7902631f6b6SNiklas Schnelle */ 7912631f6b6SNiklas Schnelle int zpci_deconfigure_device(struct zpci_dev *zdev) 7922631f6b6SNiklas Schnelle { 7932631f6b6SNiklas Schnelle int rc; 7942631f6b6SNiklas Schnelle 7952631f6b6SNiklas Schnelle if (zdev->zbus->bus) 79695b3a8b4SNiklas Schnelle zpci_bus_remove_device(zdev, false); 7972631f6b6SNiklas Schnelle 7982631f6b6SNiklas Schnelle if (zdev_enabled(zdev)) { 7992631f6b6SNiklas Schnelle rc = zpci_disable_device(zdev); 8002631f6b6SNiklas Schnelle if (rc) 8012631f6b6SNiklas Schnelle return rc; 8022631f6b6SNiklas Schnelle } 8032631f6b6SNiklas Schnelle 8042631f6b6SNiklas Schnelle rc = sclp_pci_deconfigure(zdev->fid); 8052631f6b6SNiklas Schnelle zpci_dbg(3, "deconf fid:%x, rc:%d\n", zdev->fid, rc); 8062631f6b6SNiklas Schnelle if (rc) 8072631f6b6SNiklas Schnelle return rc; 8082631f6b6SNiklas Schnelle zdev->state = ZPCI_FN_STATE_STANDBY; 8092631f6b6SNiklas Schnelle 8102631f6b6SNiklas Schnelle return 0; 8112631f6b6SNiklas Schnelle } 8122631f6b6SNiklas Schnelle 81305bc1be6SPierre Morel void zpci_release_device(struct kref *kref) 814623bd44dSSebastian Ott { 81505bc1be6SPierre Morel struct zpci_dev *zdev = container_of(kref, struct zpci_dev, kref); 816a9045c22SNiklas Schnelle int ret; 817623bd44dSSebastian Ott 8182f0230b2SNiklas Schnelle if (zdev->zbus->bus) 81995b3a8b4SNiklas Schnelle zpci_bus_remove_device(zdev, false); 82044510d6fSPierre Morel 821f6576a1bSNiklas Schnelle if (zdev_enabled(zdev)) 82205bc1be6SPierre Morel zpci_disable_device(zdev); 823f6576a1bSNiklas Schnelle 824f6576a1bSNiklas Schnelle switch (zdev->state) { 825a9045c22SNiklas Schnelle case ZPCI_FN_STATE_CONFIGURED: 826a9045c22SNiklas Schnelle ret = sclp_pci_deconfigure(zdev->fid); 827a9045c22SNiklas Schnelle zpci_dbg(3, "deconf fid:%x, rc:%d\n", zdev->fid, ret); 828a9045c22SNiklas Schnelle fallthrough; 82905bc1be6SPierre Morel case ZPCI_FN_STATE_STANDBY: 83044510d6fSPierre Morel if (zdev->has_hp_slot) 83105bc1be6SPierre Morel zpci_exit_slot(zdev); 83205bc1be6SPierre Morel zpci_cleanup_bus_resources(zdev); 83305bc1be6SPierre Morel zpci_bus_device_unregister(zdev); 83405bc1be6SPierre Morel zpci_destroy_iommu(zdev); 83505bc1be6SPierre Morel fallthrough; 83605bc1be6SPierre Morel default: 83705bc1be6SPierre Morel break; 83805bc1be6SPierre Morel } 83905bc1be6SPierre Morel 84005bc1be6SPierre Morel spin_lock(&zpci_list_lock); 84105bc1be6SPierre Morel list_del(&zdev->entry); 84205bc1be6SPierre Morel spin_unlock(&zpci_list_lock); 84305bc1be6SPierre Morel zpci_dbg(3, "rem fid:%x\n", zdev->fid); 84405bc1be6SPierre Morel kfree(zdev); 845623bd44dSSebastian Ott } 846623bd44dSSebastian Ott 847bd3a1725SMartin Schwidefsky int zpci_report_error(struct pci_dev *pdev, 848bd3a1725SMartin Schwidefsky struct zpci_report_error_header *report) 849bd3a1725SMartin Schwidefsky { 850bd3a1725SMartin Schwidefsky struct zpci_dev *zdev = to_zpci(pdev); 851bd3a1725SMartin Schwidefsky 852bd3a1725SMartin Schwidefsky return sclp_pci_report(report, zdev->fh, zdev->fid); 853bd3a1725SMartin Schwidefsky } 854bd3a1725SMartin Schwidefsky EXPORT_SYMBOL(zpci_report_error); 855bd3a1725SMartin Schwidefsky 856cd248341SJan Glauber static int zpci_mem_init(void) 857cd248341SJan Glauber { 85880c544deSSebastian Ott BUILD_BUG_ON(!is_power_of_2(__alignof__(struct zpci_fmb)) || 85980c544deSSebastian Ott __alignof__(struct zpci_fmb) < sizeof(struct zpci_fmb)); 86080c544deSSebastian Ott 861d0b08853SJan Glauber zdev_fmb_cache = kmem_cache_create("PCI_FMB_cache", sizeof(struct zpci_fmb), 86280c544deSSebastian Ott __alignof__(struct zpci_fmb), 0, NULL); 863d0b08853SJan Glauber if (!zdev_fmb_cache) 864c506fff3SSebastian Ott goto error_fmb; 865d0b08853SJan Glauber 866c506fff3SSebastian Ott zpci_iomap_start = kcalloc(ZPCI_IOMAP_ENTRIES, 867c506fff3SSebastian Ott sizeof(*zpci_iomap_start), GFP_KERNEL); 868cd248341SJan Glauber if (!zpci_iomap_start) 8699a4da8a5SJan Glauber goto error_iomap; 870cd248341SJan Glauber 871c506fff3SSebastian Ott zpci_iomap_bitmap = kcalloc(BITS_TO_LONGS(ZPCI_IOMAP_ENTRIES), 872c506fff3SSebastian Ott sizeof(*zpci_iomap_bitmap), GFP_KERNEL); 873c506fff3SSebastian Ott if (!zpci_iomap_bitmap) 874c506fff3SSebastian Ott goto error_iomap_bitmap; 875c506fff3SSebastian Ott 876b02002ccSNiklas Schnelle if (static_branch_likely(&have_mio)) 877b02002ccSNiklas Schnelle clp_setup_writeback_mio(); 878b02002ccSNiklas Schnelle 879c506fff3SSebastian Ott return 0; 880c506fff3SSebastian Ott error_iomap_bitmap: 881c506fff3SSebastian Ott kfree(zpci_iomap_start); 8829a4da8a5SJan Glauber error_iomap: 883d0b08853SJan Glauber kmem_cache_destroy(zdev_fmb_cache); 884c506fff3SSebastian Ott error_fmb: 885cd248341SJan Glauber return -ENOMEM; 886cd248341SJan Glauber } 887cd248341SJan Glauber 888cd248341SJan Glauber static void zpci_mem_exit(void) 889cd248341SJan Glauber { 890c506fff3SSebastian Ott kfree(zpci_iomap_bitmap); 891cd248341SJan Glauber kfree(zpci_iomap_start); 892d0b08853SJan Glauber kmem_cache_destroy(zdev_fmb_cache); 893cd248341SJan Glauber } 894cd248341SJan Glauber 8956324b4deSSebastian Ott static unsigned int s390_pci_probe __initdata = 1; 89656271303SSebastian Ott static unsigned int s390_pci_no_mio __initdata; 897fbfe07d4SSebastian Ott unsigned int s390_pci_force_floating __initdata; 898aa3b7c29SSebastian Ott static unsigned int s390_pci_initialized; 899cd248341SJan Glauber 900cd248341SJan Glauber char * __init pcibios_setup(char *str) 901cd248341SJan Glauber { 902257608fbSSebastian Ott if (!strcmp(str, "off")) { 903257608fbSSebastian Ott s390_pci_probe = 0; 904cd248341SJan Glauber return NULL; 905cd248341SJan Glauber } 90656271303SSebastian Ott if (!strcmp(str, "nomio")) { 90756271303SSebastian Ott s390_pci_no_mio = 1; 90856271303SSebastian Ott return NULL; 90956271303SSebastian Ott } 910fbfe07d4SSebastian Ott if (!strcmp(str, "force_floating")) { 911fbfe07d4SSebastian Ott s390_pci_force_floating = 1; 912fbfe07d4SSebastian Ott return NULL; 913fbfe07d4SSebastian Ott } 9146cf17f9aSPierre Morel if (!strcmp(str, "norid")) { 9156cf17f9aSPierre Morel s390_pci_no_rid = 1; 9166cf17f9aSPierre Morel return NULL; 9176cf17f9aSPierre Morel } 918cd248341SJan Glauber return str; 919cd248341SJan Glauber } 920cd248341SJan Glauber 921aa3b7c29SSebastian Ott bool zpci_is_enabled(void) 922aa3b7c29SSebastian Ott { 923aa3b7c29SSebastian Ott return s390_pci_initialized; 924aa3b7c29SSebastian Ott } 925aa3b7c29SSebastian Ott 926cd248341SJan Glauber static int __init pci_base_init(void) 927cd248341SJan Glauber { 928cd248341SJan Glauber int rc; 929cd248341SJan Glauber 9301e5635d1SHeiko Carstens if (!s390_pci_probe) 931cd248341SJan Glauber return 0; 932cd248341SJan Glauber 933da78693eSNiklas Schnelle if (!test_facility(69) || !test_facility(71)) { 934da78693eSNiklas Schnelle pr_info("PCI is not supported because CPU facilities 69 or 71 are not available\n"); 935cd248341SJan Glauber return 0; 936da78693eSNiklas Schnelle } 937cd248341SJan Glauber 9389964f396SSebastian Ott if (test_facility(153) && !s390_pci_no_mio) { 93971ba41c9SSebastian Ott static_branch_enable(&have_mio); 9409964f396SSebastian Ott ctl_set_bit(2, 5); 9419964f396SSebastian Ott } 94271ba41c9SSebastian Ott 943d0b08853SJan Glauber rc = zpci_debug_init(); 944d0b08853SJan Glauber if (rc) 9451f44a225SMartin Schwidefsky goto out; 946d0b08853SJan Glauber 947cd248341SJan Glauber rc = zpci_mem_init(); 948cd248341SJan Glauber if (rc) 949cd248341SJan Glauber goto out_mem; 950cd248341SJan Glauber 9519a4da8a5SJan Glauber rc = zpci_irq_init(); 9529a4da8a5SJan Glauber if (rc) 9539a4da8a5SJan Glauber goto out_irq; 9549a4da8a5SJan Glauber 955828b35f6SJan Glauber rc = zpci_dma_init(); 956828b35f6SJan Glauber if (rc) 957828b35f6SJan Glauber goto out_dma; 958828b35f6SJan Glauber 9591d578966SSebastian Ott rc = clp_scan_pci_devices(); 960a755a45dSJan Glauber if (rc) 961a755a45dSJan Glauber goto out_find; 962a755a45dSJan Glauber 963aa3b7c29SSebastian Ott s390_pci_initialized = 1; 964cd248341SJan Glauber return 0; 965cd248341SJan Glauber 966a755a45dSJan Glauber out_find: 967828b35f6SJan Glauber zpci_dma_exit(); 968828b35f6SJan Glauber out_dma: 9699a4da8a5SJan Glauber zpci_irq_exit(); 9709a4da8a5SJan Glauber out_irq: 971cd248341SJan Glauber zpci_mem_exit(); 972cd248341SJan Glauber out_mem: 973d0b08853SJan Glauber zpci_debug_exit(); 9741f44a225SMartin Schwidefsky out: 975cd248341SJan Glauber return rc; 976cd248341SJan Glauber } 97767f43f38SSebastian Ott subsys_initcall_sync(pci_base_init); 978