1*adbb3901SGreg 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> 279a4da8a5SJan Glauber #include <linux/irq.h> 289a4da8a5SJan Glauber #include <linux/kernel_stat.h> 29cd248341SJan Glauber #include <linux/seq_file.h> 30cd248341SJan Glauber #include <linux/pci.h> 31cd248341SJan Glauber #include <linux/msi.h> 32cd248341SJan Glauber 339a4da8a5SJan Glauber #include <asm/isc.h> 349a4da8a5SJan Glauber #include <asm/airq.h> 35cd248341SJan Glauber #include <asm/facility.h> 36cd248341SJan Glauber #include <asm/pci_insn.h> 37a755a45dSJan Glauber #include <asm/pci_clp.h> 38828b35f6SJan Glauber #include <asm/pci_dma.h> 39cd248341SJan Glauber 40cd248341SJan Glauber #define DEBUG /* enable pr_debug */ 41cd248341SJan Glauber 429a4da8a5SJan Glauber #define SIC_IRQ_MODE_ALL 0 439a4da8a5SJan Glauber #define SIC_IRQ_MODE_SINGLE 1 449a4da8a5SJan Glauber 45cd248341SJan Glauber #define ZPCI_NR_DMA_SPACES 1 46cd248341SJan Glauber #define ZPCI_NR_DEVICES CONFIG_PCI_NR_FUNCTIONS 47cd248341SJan Glauber 48cd248341SJan Glauber /* list of all detected zpci devices */ 4967f43f38SSebastian Ott static LIST_HEAD(zpci_list); 5057b5918cSSebastian Ott static DEFINE_SPINLOCK(zpci_list_lock); 511f44a225SMartin Schwidefsky 521f44a225SMartin Schwidefsky static struct irq_chip zpci_irq_chip = { 531f44a225SMartin Schwidefsky .name = "zPCI", 54280510f1SThomas Gleixner .irq_unmask = pci_msi_unmask_irq, 55280510f1SThomas Gleixner .irq_mask = pci_msi_mask_irq, 561f44a225SMartin Schwidefsky }; 571f44a225SMartin Schwidefsky 58cd248341SJan Glauber static DECLARE_BITMAP(zpci_domain, ZPCI_NR_DEVICES); 59cd248341SJan Glauber static DEFINE_SPINLOCK(zpci_domain_lock); 60cd248341SJan Glauber 615d0d8f43SMartin Schwidefsky static struct airq_iv *zpci_aisb_iv; 621f44a225SMartin Schwidefsky static struct airq_iv *zpci_aibv[ZPCI_NR_DEVICES]; 639a4da8a5SJan Glauber 64c506fff3SSebastian Ott #define ZPCI_IOMAP_ENTRIES \ 65b0c8ce89SSebastian Ott min(((unsigned long) ZPCI_NR_DEVICES * PCI_BAR_COUNT / 2), \ 66c506fff3SSebastian Ott ZPCI_IOMAP_MAX_ENTRIES) 67c506fff3SSebastian Ott 68cd248341SJan Glauber static DEFINE_SPINLOCK(zpci_iomap_lock); 69c506fff3SSebastian Ott static unsigned long *zpci_iomap_bitmap; 70cd248341SJan Glauber struct zpci_iomap_entry *zpci_iomap_start; 71cd248341SJan Glauber EXPORT_SYMBOL_GPL(zpci_iomap_start); 72cd248341SJan Glauber 73d0b08853SJan Glauber static struct kmem_cache *zdev_fmb_cache; 74d0b08853SJan Glauber 75cd248341SJan Glauber struct zpci_dev *get_zdev_by_fid(u32 fid) 76cd248341SJan Glauber { 77cd248341SJan Glauber struct zpci_dev *tmp, *zdev = NULL; 78cd248341SJan Glauber 7957b5918cSSebastian Ott spin_lock(&zpci_list_lock); 80cd248341SJan Glauber list_for_each_entry(tmp, &zpci_list, entry) { 81cd248341SJan Glauber if (tmp->fid == fid) { 82cd248341SJan Glauber zdev = tmp; 83cd248341SJan Glauber break; 84cd248341SJan Glauber } 85cd248341SJan Glauber } 8657b5918cSSebastian Ott spin_unlock(&zpci_list_lock); 87cd248341SJan Glauber return zdev; 88cd248341SJan Glauber } 89cd248341SJan Glauber 9001553d9aSSebastian Ott void zpci_remove_reserved_devices(void) 9101553d9aSSebastian Ott { 9201553d9aSSebastian Ott struct zpci_dev *tmp, *zdev; 9301553d9aSSebastian Ott enum zpci_state state; 9401553d9aSSebastian Ott LIST_HEAD(remove); 9501553d9aSSebastian Ott 9601553d9aSSebastian Ott spin_lock(&zpci_list_lock); 9701553d9aSSebastian Ott list_for_each_entry_safe(zdev, tmp, &zpci_list, entry) { 9801553d9aSSebastian Ott if (zdev->state == ZPCI_FN_STATE_STANDBY && 9901553d9aSSebastian Ott !clp_get_state(zdev->fid, &state) && 10001553d9aSSebastian Ott state == ZPCI_FN_STATE_RESERVED) 10101553d9aSSebastian Ott list_move_tail(&zdev->entry, &remove); 10201553d9aSSebastian Ott } 10301553d9aSSebastian Ott spin_unlock(&zpci_list_lock); 10401553d9aSSebastian Ott 10501553d9aSSebastian Ott list_for_each_entry_safe(zdev, tmp, &remove, entry) 10601553d9aSSebastian Ott zpci_remove_device(zdev); 10701553d9aSSebastian Ott } 10801553d9aSSebastian Ott 109cd248341SJan Glauber static struct zpci_dev *get_zdev_by_bus(struct pci_bus *bus) 110cd248341SJan Glauber { 111cd248341SJan Glauber return (bus && bus->sysdata) ? (struct zpci_dev *) bus->sysdata : NULL; 112cd248341SJan Glauber } 113cd248341SJan Glauber 114cd248341SJan Glauber int pci_domain_nr(struct pci_bus *bus) 115cd248341SJan Glauber { 116cd248341SJan Glauber return ((struct zpci_dev *) bus->sysdata)->domain; 117cd248341SJan Glauber } 118cd248341SJan Glauber EXPORT_SYMBOL_GPL(pci_domain_nr); 119cd248341SJan Glauber 120cd248341SJan Glauber int pci_proc_domain(struct pci_bus *bus) 121cd248341SJan Glauber { 122cd248341SJan Glauber return pci_domain_nr(bus); 123cd248341SJan Glauber } 124cd248341SJan Glauber EXPORT_SYMBOL_GPL(pci_proc_domain); 125cd248341SJan Glauber 1269a4da8a5SJan Glauber /* Modify PCI: Register adapter interruptions */ 1275d0d8f43SMartin Schwidefsky static int zpci_set_airq(struct zpci_dev *zdev) 1289a4da8a5SJan Glauber { 1299a4da8a5SJan Glauber u64 req = ZPCI_CREATE_REQ(zdev->fh, 0, ZPCI_MOD_FC_REG_INT); 130cb4deb69SSebastian Ott struct zpci_fib fib = {0}; 1314dfbd3efSSebastian Ott u8 status; 1329a4da8a5SJan Glauber 133cb4deb69SSebastian Ott fib.isc = PCI_ISC; 134cb4deb69SSebastian Ott fib.sum = 1; /* enable summary notifications */ 135cb4deb69SSebastian Ott fib.noi = airq_iv_end(zdev->aibv); 136cb4deb69SSebastian Ott fib.aibv = (unsigned long) zdev->aibv->vector; 137cb4deb69SSebastian Ott fib.aibvo = 0; /* each zdev has its own interrupt vector */ 138cb4deb69SSebastian Ott fib.aisb = (unsigned long) zpci_aisb_iv->vector + (zdev->aisb/64)*8; 139cb4deb69SSebastian Ott fib.aisbo = zdev->aisb & 63; 1409a4da8a5SJan Glauber 1414dfbd3efSSebastian Ott return zpci_mod_fc(req, &fib, &status) ? -EIO : 0; 1424dfbd3efSSebastian Ott } 1434dfbd3efSSebastian Ott 1444dfbd3efSSebastian Ott /* Modify PCI: Unregister adapter interruptions */ 1454dfbd3efSSebastian Ott static int zpci_clear_airq(struct zpci_dev *zdev) 1464dfbd3efSSebastian Ott { 1474dfbd3efSSebastian Ott u64 req = ZPCI_CREATE_REQ(zdev->fh, 0, ZPCI_MOD_FC_DEREG_INT); 1484dfbd3efSSebastian Ott struct zpci_fib fib = {0}; 1494dfbd3efSSebastian Ott u8 cc, status; 1504dfbd3efSSebastian Ott 1514dfbd3efSSebastian Ott cc = zpci_mod_fc(req, &fib, &status); 1524dfbd3efSSebastian Ott if (cc == 3 || (cc == 1 && status == 24)) 1534dfbd3efSSebastian Ott /* Function already gone or IRQs already deregistered. */ 1544dfbd3efSSebastian Ott cc = 0; 1554dfbd3efSSebastian Ott 1564dfbd3efSSebastian Ott return cc ? -EIO : 0; 1579a4da8a5SJan Glauber } 1589a4da8a5SJan Glauber 159828b35f6SJan Glauber /* Modify PCI: Register I/O address translation parameters */ 160828b35f6SJan Glauber int zpci_register_ioat(struct zpci_dev *zdev, u8 dmaas, 161828b35f6SJan Glauber u64 base, u64 limit, u64 iota) 162828b35f6SJan Glauber { 16372570834SSebastian Ott u64 req = ZPCI_CREATE_REQ(zdev->fh, dmaas, ZPCI_MOD_FC_REG_IOAT); 16472570834SSebastian Ott struct zpci_fib fib = {0}; 16572570834SSebastian Ott u8 status; 166828b35f6SJan Glauber 167828b35f6SJan Glauber WARN_ON_ONCE(iota & 0x3fff); 16872570834SSebastian Ott fib.pba = base; 16972570834SSebastian Ott fib.pal = limit; 17072570834SSebastian Ott fib.iota = iota | ZPCI_IOTA_RTTO_FLAG; 17172570834SSebastian Ott return zpci_mod_fc(req, &fib, &status) ? -EIO : 0; 172828b35f6SJan Glauber } 173828b35f6SJan Glauber 174828b35f6SJan Glauber /* Modify PCI: Unregister I/O address translation parameters */ 175828b35f6SJan Glauber int zpci_unregister_ioat(struct zpci_dev *zdev, u8 dmaas) 176828b35f6SJan Glauber { 17772570834SSebastian Ott u64 req = ZPCI_CREATE_REQ(zdev->fh, dmaas, ZPCI_MOD_FC_DEREG_IOAT); 17872570834SSebastian Ott struct zpci_fib fib = {0}; 17972570834SSebastian Ott u8 cc, status; 180828b35f6SJan Glauber 18172570834SSebastian Ott cc = zpci_mod_fc(req, &fib, &status); 18272570834SSebastian Ott if (cc == 3) /* Function already gone. */ 18372570834SSebastian Ott cc = 0; 18472570834SSebastian Ott return cc ? -EIO : 0; 185828b35f6SJan Glauber } 186828b35f6SJan Glauber 187d0b08853SJan Glauber /* Modify PCI: Set PCI function measurement parameters */ 188d0b08853SJan Glauber int zpci_fmb_enable_device(struct zpci_dev *zdev) 189d0b08853SJan Glauber { 1904e5bd780SSebastian Ott u64 req = ZPCI_CREATE_REQ(zdev->fh, 0, ZPCI_MOD_FC_SET_MEASURE); 1914e5bd780SSebastian Ott struct zpci_fib fib = {0}; 1924e5bd780SSebastian Ott u8 cc, status; 193d0b08853SJan Glauber 1940b7589ecSSebastian Ott if (zdev->fmb || sizeof(*zdev->fmb) < zdev->fmb_length) 195d0b08853SJan Glauber return -EINVAL; 196d0b08853SJan Glauber 19708b42124SWei Yongjun zdev->fmb = kmem_cache_zalloc(zdev_fmb_cache, GFP_KERNEL); 198d0b08853SJan Glauber if (!zdev->fmb) 199d0b08853SJan Glauber return -ENOMEM; 200d0b08853SJan Glauber WARN_ON((u64) zdev->fmb & 0xf); 201d0b08853SJan Glauber 2026001018aSSebastian Ott /* reset software counters */ 2036001018aSSebastian Ott atomic64_set(&zdev->allocated_pages, 0); 2046001018aSSebastian Ott atomic64_set(&zdev->mapped_pages, 0); 2056001018aSSebastian Ott atomic64_set(&zdev->unmapped_pages, 0); 2066001018aSSebastian Ott 2074e5bd780SSebastian Ott fib.fmb_addr = virt_to_phys(zdev->fmb); 2084e5bd780SSebastian Ott cc = zpci_mod_fc(req, &fib, &status); 2094e5bd780SSebastian Ott if (cc) { 2104e5bd780SSebastian Ott kmem_cache_free(zdev_fmb_cache, zdev->fmb); 2114e5bd780SSebastian Ott zdev->fmb = NULL; 2124e5bd780SSebastian Ott } 2134e5bd780SSebastian Ott return cc ? -EIO : 0; 214d0b08853SJan Glauber } 215d0b08853SJan Glauber 216d0b08853SJan Glauber /* Modify PCI: Disable PCI function measurement */ 217d0b08853SJan Glauber int zpci_fmb_disable_device(struct zpci_dev *zdev) 218d0b08853SJan Glauber { 2194e5bd780SSebastian Ott u64 req = ZPCI_CREATE_REQ(zdev->fh, 0, ZPCI_MOD_FC_SET_MEASURE); 2204e5bd780SSebastian Ott struct zpci_fib fib = {0}; 2214e5bd780SSebastian Ott u8 cc, status; 222d0b08853SJan Glauber 223d0b08853SJan Glauber if (!zdev->fmb) 224d0b08853SJan Glauber return -EINVAL; 225d0b08853SJan Glauber 226d0b08853SJan Glauber /* Function measurement is disabled if fmb address is zero */ 2274e5bd780SSebastian Ott cc = zpci_mod_fc(req, &fib, &status); 2284e5bd780SSebastian Ott if (cc == 3) /* Function already gone. */ 2294e5bd780SSebastian Ott cc = 0; 230d0b08853SJan Glauber 2314e5bd780SSebastian Ott if (!cc) { 232d0b08853SJan Glauber kmem_cache_free(zdev_fmb_cache, zdev->fmb); 233d0b08853SJan Glauber zdev->fmb = NULL; 2344e5bd780SSebastian Ott } 2354e5bd780SSebastian Ott return cc ? -EIO : 0; 236d0b08853SJan Glauber } 237d0b08853SJan Glauber 238cd248341SJan Glauber static int zpci_cfg_load(struct zpci_dev *zdev, int offset, u32 *val, u8 len) 239cd248341SJan Glauber { 240cd248341SJan Glauber u64 req = ZPCI_CREATE_REQ(zdev->fh, ZPCI_PCIAS_CFGSPC, len); 241cd248341SJan Glauber u64 data; 242cd248341SJan Glauber int rc; 243cd248341SJan Glauber 2449389339fSMartin Schwidefsky rc = zpci_load(&data, req, offset); 245b170bad4SSebastian Ott if (!rc) { 2465064cd35SSebastian Ott data = le64_to_cpu((__force __le64) data); 2475064cd35SSebastian Ott data >>= (8 - len) * 8; 248cd248341SJan Glauber *val = (u32) data; 249b170bad4SSebastian Ott } else 250cd248341SJan Glauber *val = 0xffffffff; 251cd248341SJan Glauber return rc; 252cd248341SJan Glauber } 253cd248341SJan Glauber 254cd248341SJan Glauber static int zpci_cfg_store(struct zpci_dev *zdev, int offset, u32 val, u8 len) 255cd248341SJan Glauber { 256cd248341SJan Glauber u64 req = ZPCI_CREATE_REQ(zdev->fh, ZPCI_PCIAS_CFGSPC, len); 257cd248341SJan Glauber u64 data = val; 258cd248341SJan Glauber int rc; 259cd248341SJan Glauber 2605064cd35SSebastian Ott data <<= (8 - len) * 8; 2615064cd35SSebastian Ott data = (__force u64) cpu_to_le64(data); 2629389339fSMartin Schwidefsky rc = zpci_store(data, req, offset); 263cd248341SJan Glauber return rc; 264cd248341SJan Glauber } 265cd248341SJan Glauber 266cd248341SJan Glauber resource_size_t pcibios_align_resource(void *data, const struct resource *res, 267cd248341SJan Glauber resource_size_t size, 268cd248341SJan Glauber resource_size_t align) 269cd248341SJan Glauber { 270cd248341SJan Glauber return 0; 271cd248341SJan Glauber } 272cd248341SJan Glauber 27387bc359bSJan Glauber /* combine single writes by using store-block insn */ 27487bc359bSJan Glauber void __iowrite64_copy(void __iomem *to, const void *from, size_t count) 27587bc359bSJan Glauber { 27687bc359bSJan Glauber zpci_memcpy_toio(to, from, count); 27787bc359bSJan Glauber } 27887bc359bSJan Glauber 279cd248341SJan Glauber /* Create a virtual mapping cookie for a PCI BAR */ 2808cfc99b5SMichael S. Tsirkin void __iomem *pci_iomap_range(struct pci_dev *pdev, 2818cfc99b5SMichael S. Tsirkin int bar, 2828cfc99b5SMichael S. Tsirkin unsigned long offset, 2838cfc99b5SMichael S. Tsirkin unsigned long max) 284cd248341SJan Glauber { 285198a5278SSebastian Ott struct zpci_dev *zdev = to_zpci(pdev); 286cd248341SJan Glauber int idx; 287cd248341SJan Glauber 288c0cabaddSSebastian Ott if (!pci_resource_len(pdev, bar)) 289cd248341SJan Glauber return NULL; 290cd248341SJan Glauber 291cd248341SJan Glauber idx = zdev->bars[bar].map_idx; 292cd248341SJan Glauber spin_lock(&zpci_iomap_lock); 293f5e44f82SSebastian Ott /* Detect overrun */ 294f5e44f82SSebastian Ott WARN_ON(!++zpci_iomap_start[idx].count); 295cd248341SJan Glauber zpci_iomap_start[idx].fh = zdev->fh; 296cd248341SJan Glauber zpci_iomap_start[idx].bar = bar; 297cd248341SJan Glauber spin_unlock(&zpci_iomap_lock); 298cd248341SJan Glauber 2999e00caaeSSebastian Ott return (void __iomem *) ZPCI_ADDR(idx) + offset; 300cd248341SJan Glauber } 301d9426083SSebastian Ott EXPORT_SYMBOL(pci_iomap_range); 3028cfc99b5SMichael S. Tsirkin 3038cfc99b5SMichael S. Tsirkin void __iomem *pci_iomap(struct pci_dev *dev, int bar, unsigned long maxlen) 3048cfc99b5SMichael S. Tsirkin { 3058cfc99b5SMichael S. Tsirkin return pci_iomap_range(dev, bar, 0, maxlen); 3068cfc99b5SMichael S. Tsirkin } 3078cfc99b5SMichael S. Tsirkin EXPORT_SYMBOL(pci_iomap); 308cd248341SJan Glauber 309cd248341SJan Glauber void pci_iounmap(struct pci_dev *pdev, void __iomem *addr) 310cd248341SJan Glauber { 3119e00caaeSSebastian Ott unsigned int idx = ZPCI_IDX(addr); 312cd248341SJan Glauber 313cd248341SJan Glauber spin_lock(&zpci_iomap_lock); 3148cfc99b5SMichael S. Tsirkin /* Detect underrun */ 315f5e44f82SSebastian Ott WARN_ON(!zpci_iomap_start[idx].count); 3168cfc99b5SMichael S. Tsirkin if (!--zpci_iomap_start[idx].count) { 317cd248341SJan Glauber zpci_iomap_start[idx].fh = 0; 318cd248341SJan Glauber zpci_iomap_start[idx].bar = 0; 3198cfc99b5SMichael S. Tsirkin } 320cd248341SJan Glauber spin_unlock(&zpci_iomap_lock); 321cd248341SJan Glauber } 322d9426083SSebastian Ott EXPORT_SYMBOL(pci_iounmap); 323cd248341SJan Glauber 324cd248341SJan Glauber static int pci_read(struct pci_bus *bus, unsigned int devfn, int where, 325cd248341SJan Glauber int size, u32 *val) 326cd248341SJan Glauber { 327cd248341SJan Glauber struct zpci_dev *zdev = get_zdev_by_bus(bus); 3282c3700bbSSebastian Ott int ret; 329cd248341SJan Glauber 330cd248341SJan Glauber if (!zdev || devfn != ZPCI_DEVFN) 3312c3700bbSSebastian Ott ret = -ENODEV; 3322c3700bbSSebastian Ott else 3332c3700bbSSebastian Ott ret = zpci_cfg_load(zdev, where, val, size); 3342c3700bbSSebastian Ott 3352c3700bbSSebastian Ott return ret; 336cd248341SJan Glauber } 337cd248341SJan Glauber 338cd248341SJan Glauber static int pci_write(struct pci_bus *bus, unsigned int devfn, int where, 339cd248341SJan Glauber int size, u32 val) 340cd248341SJan Glauber { 341cd248341SJan Glauber struct zpci_dev *zdev = get_zdev_by_bus(bus); 3422c3700bbSSebastian Ott int ret; 343cd248341SJan Glauber 344cd248341SJan Glauber if (!zdev || devfn != ZPCI_DEVFN) 3452c3700bbSSebastian Ott ret = -ENODEV; 3462c3700bbSSebastian Ott else 3472c3700bbSSebastian Ott ret = zpci_cfg_store(zdev, where, val, size); 3482c3700bbSSebastian Ott 3492c3700bbSSebastian Ott return ret; 350cd248341SJan Glauber } 351cd248341SJan Glauber 352cd248341SJan Glauber static struct pci_ops pci_root_ops = { 353cd248341SJan Glauber .read = pci_read, 354cd248341SJan Glauber .write = pci_write, 355cd248341SJan Glauber }; 356cd248341SJan Glauber 357f4eae94fSMartin Schwidefsky static void zpci_irq_handler(struct airq_struct *airq) 3589a4da8a5SJan Glauber { 3595d0d8f43SMartin Schwidefsky unsigned long si, ai; 3601f44a225SMartin Schwidefsky struct airq_iv *aibv; 3615d0d8f43SMartin Schwidefsky int irqs_on = 0; 3629a4da8a5SJan Glauber 363420f42ecSHeiko Carstens inc_irq_stat(IRQIO_PCI); 3645d0d8f43SMartin Schwidefsky for (si = 0;;) { 3655d0d8f43SMartin Schwidefsky /* Scan adapter summary indicator bit vector */ 3665d0d8f43SMartin Schwidefsky si = airq_iv_scan(zpci_aisb_iv, si, airq_iv_end(zpci_aisb_iv)); 3675d0d8f43SMartin Schwidefsky if (si == -1UL) { 3685d0d8f43SMartin Schwidefsky if (irqs_on++) 3695d0d8f43SMartin Schwidefsky /* End of second scan with interrupts on. */ 3705d0d8f43SMartin Schwidefsky break; 3715d0d8f43SMartin Schwidefsky /* First scan complete, reenable interrupts. */ 37248070c73SChristian Borntraeger if (zpci_set_irq_ctrl(SIC_IRQ_MODE_SINGLE, NULL, PCI_ISC)) 37348070c73SChristian Borntraeger break; 3745d0d8f43SMartin Schwidefsky si = 0; 3755d0d8f43SMartin Schwidefsky continue; 3769a4da8a5SJan Glauber } 3775d0d8f43SMartin Schwidefsky 3785d0d8f43SMartin Schwidefsky /* Scan the adapter interrupt vector for this device. */ 3791f44a225SMartin Schwidefsky aibv = zpci_aibv[si]; 3805d0d8f43SMartin Schwidefsky for (ai = 0;;) { 3811f44a225SMartin Schwidefsky ai = airq_iv_scan(aibv, ai, airq_iv_end(aibv)); 3825d0d8f43SMartin Schwidefsky if (ai == -1UL) 3835d0d8f43SMartin Schwidefsky break; 3845d0d8f43SMartin Schwidefsky inc_irq_stat(IRQIO_MSI); 3851f44a225SMartin Schwidefsky airq_iv_lock(aibv, ai); 3861f44a225SMartin Schwidefsky generic_handle_irq(airq_iv_get_data(aibv, ai)); 3871f44a225SMartin Schwidefsky airq_iv_unlock(aibv, ai); 3885d0d8f43SMartin Schwidefsky } 3895d0d8f43SMartin Schwidefsky } 3905d0d8f43SMartin Schwidefsky } 3915d0d8f43SMartin Schwidefsky 3925d0d8f43SMartin Schwidefsky int arch_setup_msi_irqs(struct pci_dev *pdev, int nvec, int type) 3939a4da8a5SJan Glauber { 394198a5278SSebastian Ott struct zpci_dev *zdev = to_zpci(pdev); 3950a0a9421SThomas Gleixner unsigned int hwirq, msi_vecs; 3965d0d8f43SMartin Schwidefsky unsigned long aisb; 3979a4da8a5SJan Glauber struct msi_desc *msi; 3981f44a225SMartin Schwidefsky struct msi_msg msg; 3990a0a9421SThomas Gleixner int rc, irq; 4009a4da8a5SJan Glauber 401795818e8SSebastian Ott zdev->aisb = -1UL; 402a384c892SAlexander Gordeev if (type == PCI_CAP_ID_MSI && nvec > 1) 403a384c892SAlexander Gordeev return 1; 404b19148f6SSebastian Ott msi_vecs = min_t(unsigned int, nvec, zdev->max_msi); 4059a4da8a5SJan Glauber 4065d0d8f43SMartin Schwidefsky /* Allocate adapter summary indicator bit */ 4075d0d8f43SMartin Schwidefsky aisb = airq_iv_alloc_bit(zpci_aisb_iv); 4085d0d8f43SMartin Schwidefsky if (aisb == -1UL) 409795818e8SSebastian Ott return -EIO; 4109a4da8a5SJan Glauber zdev->aisb = aisb; 4119a4da8a5SJan Glauber 4125d0d8f43SMartin Schwidefsky /* Create adapter interrupt vector */ 4131f44a225SMartin Schwidefsky zdev->aibv = airq_iv_create(msi_vecs, AIRQ_IV_DATA | AIRQ_IV_BITLOCK); 4145d0d8f43SMartin Schwidefsky if (!zdev->aibv) 415795818e8SSebastian Ott return -ENOMEM; 4169a4da8a5SJan Glauber 4175d0d8f43SMartin Schwidefsky /* Wire up shortcut pointer */ 4181f44a225SMartin Schwidefsky zpci_aibv[aisb] = zdev->aibv; 4195d0d8f43SMartin Schwidefsky 4201f44a225SMartin Schwidefsky /* Request MSI interrupts */ 4211f44a225SMartin Schwidefsky hwirq = 0; 422df516f42SJiang Liu for_each_pci_msi_entry(msi, pdev) { 4231f44a225SMartin Schwidefsky rc = -EIO; 4241f44a225SMartin Schwidefsky irq = irq_alloc_desc(0); /* Alloc irq on node 0 */ 4250a0a9421SThomas Gleixner if (irq < 0) 426795818e8SSebastian Ott return -ENOMEM; 4271f44a225SMartin Schwidefsky rc = irq_set_msi_desc(irq, msi); 4289a4da8a5SJan Glauber if (rc) 429795818e8SSebastian Ott return rc; 4301f44a225SMartin Schwidefsky irq_set_chip_and_handler(irq, &zpci_irq_chip, 4311f44a225SMartin Schwidefsky handle_simple_irq); 4321f44a225SMartin Schwidefsky msg.data = hwirq; 4331f44a225SMartin Schwidefsky msg.address_lo = zdev->msi_addr & 0xffffffff; 4341f44a225SMartin Schwidefsky msg.address_hi = zdev->msi_addr >> 32; 43583a18912SJiang Liu pci_write_msi_msg(irq, &msg); 4361f44a225SMartin Schwidefsky airq_iv_set_data(zdev->aibv, hwirq, irq); 4371f44a225SMartin Schwidefsky hwirq++; 4389a4da8a5SJan Glauber } 4399a4da8a5SJan Glauber 4405d0d8f43SMartin Schwidefsky /* Enable adapter interrupts */ 4415d0d8f43SMartin Schwidefsky rc = zpci_set_airq(zdev); 4425d0d8f43SMartin Schwidefsky if (rc) 443795818e8SSebastian Ott return rc; 4445d0d8f43SMartin Schwidefsky 4455d0d8f43SMartin Schwidefsky return (msi_vecs == nvec) ? 0 : msi_vecs; 4469a4da8a5SJan Glauber } 4479a4da8a5SJan Glauber 4485d0d8f43SMartin Schwidefsky void arch_teardown_msi_irqs(struct pci_dev *pdev) 4499a4da8a5SJan Glauber { 450198a5278SSebastian Ott struct zpci_dev *zdev = to_zpci(pdev); 4519a4da8a5SJan Glauber struct msi_desc *msi; 4525d0d8f43SMartin Schwidefsky int rc; 4539a4da8a5SJan Glauber 4545d0d8f43SMartin Schwidefsky /* Disable adapter interrupts */ 4555d0d8f43SMartin Schwidefsky rc = zpci_clear_airq(zdev); 4561f1dcbd4SSebastian Ott if (rc) 4579a4da8a5SJan Glauber return; 4589a4da8a5SJan Glauber 4591f44a225SMartin Schwidefsky /* Release MSI interrupts */ 460df516f42SJiang Liu for_each_pci_msi_entry(msi, pdev) { 461795818e8SSebastian Ott if (!msi->irq) 462795818e8SSebastian Ott continue; 4638fb878c5SYijing Wang if (msi->msi_attrib.is_msix) 46423ed8d57SThomas Gleixner __pci_msix_desc_mask_irq(msi, 1); 4658fb878c5SYijing Wang else 46623ed8d57SThomas Gleixner __pci_msi_desc_mask_irq(msi, 1, 1); 4671f44a225SMartin Schwidefsky irq_set_msi_desc(msi->irq, NULL); 4681f44a225SMartin Schwidefsky irq_free_desc(msi->irq); 4691f44a225SMartin Schwidefsky msi->msg.address_lo = 0; 4701f44a225SMartin Schwidefsky msi->msg.address_hi = 0; 4711f44a225SMartin Schwidefsky msi->msg.data = 0; 4721f44a225SMartin Schwidefsky msi->irq = 0; 4731f44a225SMartin Schwidefsky } 4749a4da8a5SJan Glauber 475795818e8SSebastian Ott if (zdev->aisb != -1UL) { 4761f44a225SMartin Schwidefsky zpci_aibv[zdev->aisb] = NULL; 4775d0d8f43SMartin Schwidefsky airq_iv_free_bit(zpci_aisb_iv, zdev->aisb); 478795818e8SSebastian Ott zdev->aisb = -1UL; 479795818e8SSebastian Ott } 480795818e8SSebastian Ott if (zdev->aibv) { 481795818e8SSebastian Ott airq_iv_release(zdev->aibv); 482795818e8SSebastian Ott zdev->aibv = NULL; 483795818e8SSebastian Ott } 4849a4da8a5SJan Glauber } 4859a4da8a5SJan Glauber 4861803ba2dSSebastian Ott static void zpci_map_resources(struct pci_dev *pdev) 487cd248341SJan Glauber { 488cd248341SJan Glauber resource_size_t len; 489cd248341SJan Glauber int i; 490cd248341SJan Glauber 491cd248341SJan Glauber for (i = 0; i < PCI_BAR_COUNT; i++) { 492cd248341SJan Glauber len = pci_resource_len(pdev, i); 493cd248341SJan Glauber if (!len) 494cd248341SJan Glauber continue; 4955b9f2081SMartin Schwidefsky pdev->resource[i].start = 4965b9f2081SMartin Schwidefsky (resource_size_t __force) pci_iomap(pdev, i, 0); 497cd248341SJan Glauber pdev->resource[i].end = pdev->resource[i].start + len - 1; 498cd248341SJan Glauber } 499944239c5SSebastian Ott } 500944239c5SSebastian Ott 5011803ba2dSSebastian Ott static void zpci_unmap_resources(struct pci_dev *pdev) 502944239c5SSebastian Ott { 503944239c5SSebastian Ott resource_size_t len; 504944239c5SSebastian Ott int i; 505944239c5SSebastian Ott 506944239c5SSebastian Ott for (i = 0; i < PCI_BAR_COUNT; i++) { 507944239c5SSebastian Ott len = pci_resource_len(pdev, i); 508944239c5SSebastian Ott if (!len) 509944239c5SSebastian Ott continue; 5105b9f2081SMartin Schwidefsky pci_iounmap(pdev, (void __iomem __force *) 5115b9f2081SMartin Schwidefsky pdev->resource[i].start); 512944239c5SSebastian Ott } 513944239c5SSebastian Ott } 514cd248341SJan Glauber 515561ecb0cSSebastian Ott static struct airq_struct zpci_airq = { 516561ecb0cSSebastian Ott .handler = zpci_irq_handler, 517561ecb0cSSebastian Ott .isc = PCI_ISC, 518561ecb0cSSebastian Ott }; 519561ecb0cSSebastian Ott 5209a4da8a5SJan Glauber static int __init zpci_irq_init(void) 5219a4da8a5SJan Glauber { 5225d0d8f43SMartin Schwidefsky int rc; 5239a4da8a5SJan Glauber 524f4eae94fSMartin Schwidefsky rc = register_adapter_interrupt(&zpci_airq); 525f4eae94fSMartin Schwidefsky if (rc) 5265d0d8f43SMartin Schwidefsky goto out; 527f4eae94fSMartin Schwidefsky /* Set summary to 1 to be called every time for the ISC. */ 528f4eae94fSMartin Schwidefsky *zpci_airq.lsi_ptr = 1; 5299a4da8a5SJan Glauber 5305d0d8f43SMartin Schwidefsky rc = -ENOMEM; 5315d0d8f43SMartin Schwidefsky zpci_aisb_iv = airq_iv_create(ZPCI_NR_DEVICES, AIRQ_IV_ALLOC); 5325d0d8f43SMartin Schwidefsky if (!zpci_aisb_iv) 5335d0d8f43SMartin Schwidefsky goto out_airq; 5349a4da8a5SJan Glauber 5359389339fSMartin Schwidefsky zpci_set_irq_ctrl(SIC_IRQ_MODE_SINGLE, NULL, PCI_ISC); 5369a4da8a5SJan Glauber return 0; 5379a4da8a5SJan Glauber 5385d0d8f43SMartin Schwidefsky out_airq: 5395d0d8f43SMartin Schwidefsky unregister_adapter_interrupt(&zpci_airq); 5405d0d8f43SMartin Schwidefsky out: 5419a4da8a5SJan Glauber return rc; 5429a4da8a5SJan Glauber } 5439a4da8a5SJan Glauber 5449a4da8a5SJan Glauber static void zpci_irq_exit(void) 5459a4da8a5SJan Glauber { 5465d0d8f43SMartin Schwidefsky airq_iv_release(zpci_aisb_iv); 547f4eae94fSMartin Schwidefsky unregister_adapter_interrupt(&zpci_airq); 5489a4da8a5SJan Glauber } 5499a4da8a5SJan Glauber 550cd248341SJan Glauber static int zpci_alloc_iomap(struct zpci_dev *zdev) 551cd248341SJan Glauber { 552bf19c94dSSebastian Ott unsigned long entry; 553cd248341SJan Glauber 554cd248341SJan Glauber spin_lock(&zpci_iomap_lock); 555c506fff3SSebastian Ott entry = find_first_zero_bit(zpci_iomap_bitmap, ZPCI_IOMAP_ENTRIES); 556c506fff3SSebastian Ott if (entry == ZPCI_IOMAP_ENTRIES) { 557cd248341SJan Glauber spin_unlock(&zpci_iomap_lock); 558cd248341SJan Glauber return -ENOSPC; 559cd248341SJan Glauber } 560c506fff3SSebastian Ott set_bit(entry, zpci_iomap_bitmap); 561cd248341SJan Glauber spin_unlock(&zpci_iomap_lock); 562cd248341SJan Glauber return entry; 563cd248341SJan Glauber } 564cd248341SJan Glauber 565cd248341SJan Glauber static void zpci_free_iomap(struct zpci_dev *zdev, int entry) 566cd248341SJan Glauber { 567cd248341SJan Glauber spin_lock(&zpci_iomap_lock); 568cd248341SJan Glauber memset(&zpci_iomap_start[entry], 0, sizeof(struct zpci_iomap_entry)); 569c506fff3SSebastian Ott clear_bit(entry, zpci_iomap_bitmap); 570cd248341SJan Glauber spin_unlock(&zpci_iomap_lock); 571cd248341SJan Glauber } 572cd248341SJan Glauber 5737a572a3aSSebastian Ott static struct resource *__alloc_res(struct zpci_dev *zdev, unsigned long start, 5747a572a3aSSebastian Ott unsigned long size, unsigned long flags) 5757a572a3aSSebastian Ott { 5767a572a3aSSebastian Ott struct resource *r; 5777a572a3aSSebastian Ott 5787a572a3aSSebastian Ott r = kzalloc(sizeof(*r), GFP_KERNEL); 5797a572a3aSSebastian Ott if (!r) 5807a572a3aSSebastian Ott return NULL; 5817a572a3aSSebastian Ott 5827a572a3aSSebastian Ott r->start = start; 5837a572a3aSSebastian Ott r->end = r->start + size - 1; 5847a572a3aSSebastian Ott r->flags = flags; 5857a572a3aSSebastian Ott r->name = zdev->res_name; 5867a572a3aSSebastian Ott 5877a572a3aSSebastian Ott if (request_resource(&iomem_resource, r)) { 5887a572a3aSSebastian Ott kfree(r); 5897a572a3aSSebastian Ott return NULL; 5907a572a3aSSebastian Ott } 5917a572a3aSSebastian Ott return r; 5927a572a3aSSebastian Ott } 5937a572a3aSSebastian Ott 5947a572a3aSSebastian Ott static int zpci_setup_bus_resources(struct zpci_dev *zdev, 5957a572a3aSSebastian Ott struct list_head *resources) 5967a572a3aSSebastian Ott { 5977a572a3aSSebastian Ott unsigned long addr, size, flags; 5987a572a3aSSebastian Ott struct resource *res; 5997a572a3aSSebastian Ott int i, entry; 6007a572a3aSSebastian Ott 6017a572a3aSSebastian Ott snprintf(zdev->res_name, sizeof(zdev->res_name), 6027a572a3aSSebastian Ott "PCI Bus %04x:%02x", zdev->domain, ZPCI_BUS_NR); 6037a572a3aSSebastian Ott 6047a572a3aSSebastian Ott for (i = 0; i < PCI_BAR_COUNT; i++) { 6057a572a3aSSebastian Ott if (!zdev->bars[i].size) 6067a572a3aSSebastian Ott continue; 6077a572a3aSSebastian Ott entry = zpci_alloc_iomap(zdev); 6087a572a3aSSebastian Ott if (entry < 0) 6097a572a3aSSebastian Ott return entry; 6107a572a3aSSebastian Ott zdev->bars[i].map_idx = entry; 6117a572a3aSSebastian Ott 6127a572a3aSSebastian Ott /* only MMIO is supported */ 6137a572a3aSSebastian Ott flags = IORESOURCE_MEM; 6147a572a3aSSebastian Ott if (zdev->bars[i].val & 8) 6157a572a3aSSebastian Ott flags |= IORESOURCE_PREFETCH; 6167a572a3aSSebastian Ott if (zdev->bars[i].val & 4) 6177a572a3aSSebastian Ott flags |= IORESOURCE_MEM_64; 6187a572a3aSSebastian Ott 6199e00caaeSSebastian Ott addr = ZPCI_ADDR(entry); 6207a572a3aSSebastian Ott size = 1UL << zdev->bars[i].size; 6217a572a3aSSebastian Ott 6227a572a3aSSebastian Ott res = __alloc_res(zdev, addr, size, flags); 6237a572a3aSSebastian Ott if (!res) { 6247a572a3aSSebastian Ott zpci_free_iomap(zdev, entry); 6257a572a3aSSebastian Ott return -ENOMEM; 6267a572a3aSSebastian Ott } 6277a572a3aSSebastian Ott zdev->bars[i].res = res; 6287a572a3aSSebastian Ott pci_add_resource(resources, res); 6297a572a3aSSebastian Ott } 6307a572a3aSSebastian Ott 6317a572a3aSSebastian Ott return 0; 6327a572a3aSSebastian Ott } 6337a572a3aSSebastian Ott 6347a572a3aSSebastian Ott static void zpci_cleanup_bus_resources(struct zpci_dev *zdev) 6357a572a3aSSebastian Ott { 6367a572a3aSSebastian Ott int i; 6377a572a3aSSebastian Ott 6387a572a3aSSebastian Ott for (i = 0; i < PCI_BAR_COUNT; i++) { 6392b1df724SSebastian Ott if (!zdev->bars[i].size || !zdev->bars[i].res) 6407a572a3aSSebastian Ott continue; 6417a572a3aSSebastian Ott 6427a572a3aSSebastian Ott zpci_free_iomap(zdev, zdev->bars[i].map_idx); 6437a572a3aSSebastian Ott release_resource(zdev->bars[i].res); 6447a572a3aSSebastian Ott kfree(zdev->bars[i].res); 6457a572a3aSSebastian Ott } 6467a572a3aSSebastian Ott } 6477a572a3aSSebastian Ott 648af0a8a84SSebastian Ott int pcibios_add_device(struct pci_dev *pdev) 649af0a8a84SSebastian Ott { 650cb809182SSebastian Ott struct resource *res; 651cb809182SSebastian Ott int i; 652cb809182SSebastian Ott 653ef4858c6SSebastian Ott pdev->dev.groups = zpci_attr_groups; 6545657933dSBart Van Assche pdev->dev.dma_ops = &s390_pci_dma_ops; 6551803ba2dSSebastian Ott zpci_map_resources(pdev); 656cb809182SSebastian Ott 657cb809182SSebastian Ott for (i = 0; i < PCI_BAR_COUNT; i++) { 658cb809182SSebastian Ott res = &pdev->resource[i]; 659cb809182SSebastian Ott if (res->parent || !res->flags) 660cb809182SSebastian Ott continue; 661cb809182SSebastian Ott pci_claim_resource(pdev, i); 662cb809182SSebastian Ott } 663cb809182SSebastian Ott 664cb809182SSebastian Ott return 0; 665cb809182SSebastian Ott } 666cb809182SSebastian Ott 6671803ba2dSSebastian Ott void pcibios_release_device(struct pci_dev *pdev) 6681803ba2dSSebastian Ott { 6691803ba2dSSebastian Ott zpci_unmap_resources(pdev); 6701803ba2dSSebastian Ott } 6711803ba2dSSebastian Ott 672cb809182SSebastian Ott int pcibios_enable_device(struct pci_dev *pdev, int mask) 673cb809182SSebastian Ott { 674198a5278SSebastian Ott struct zpci_dev *zdev = to_zpci(pdev); 675af0a8a84SSebastian Ott 6769a99649fSSebastian Ott zpci_debug_init_device(zdev, dev_name(&pdev->dev)); 677af0a8a84SSebastian Ott zpci_fmb_enable_device(zdev); 678af0a8a84SSebastian Ott 679d7533232SBjorn Helgaas return pci_enable_resources(pdev, mask); 680af0a8a84SSebastian Ott } 681af0a8a84SSebastian Ott 682cb809182SSebastian Ott void pcibios_disable_device(struct pci_dev *pdev) 683944239c5SSebastian Ott { 684198a5278SSebastian Ott struct zpci_dev *zdev = to_zpci(pdev); 685944239c5SSebastian Ott 686944239c5SSebastian Ott zpci_fmb_disable_device(zdev); 687944239c5SSebastian Ott zpci_debug_exit_device(zdev); 688944239c5SSebastian Ott } 689944239c5SSebastian Ott 69069db3b5eSSebastian Ott #ifdef CONFIG_HIBERNATE_CALLBACKS 69169db3b5eSSebastian Ott static int zpci_restore(struct device *dev) 69269db3b5eSSebastian Ott { 6931803ba2dSSebastian Ott struct pci_dev *pdev = to_pci_dev(dev); 694198a5278SSebastian Ott struct zpci_dev *zdev = to_zpci(pdev); 69569db3b5eSSebastian Ott int ret = 0; 69669db3b5eSSebastian Ott 69769db3b5eSSebastian Ott if (zdev->state != ZPCI_FN_STATE_ONLINE) 69869db3b5eSSebastian Ott goto out; 69969db3b5eSSebastian Ott 70069db3b5eSSebastian Ott ret = clp_enable_fh(zdev, ZPCI_NR_DMA_SPACES); 70169db3b5eSSebastian Ott if (ret) 70269db3b5eSSebastian Ott goto out; 70369db3b5eSSebastian Ott 7041803ba2dSSebastian Ott zpci_map_resources(pdev); 70569eea95cSGerald Schaefer zpci_register_ioat(zdev, 0, zdev->start_dma, zdev->end_dma, 70669db3b5eSSebastian Ott (u64) zdev->dma_table); 70769db3b5eSSebastian Ott 70869db3b5eSSebastian Ott out: 70969db3b5eSSebastian Ott return ret; 71069db3b5eSSebastian Ott } 71169db3b5eSSebastian Ott 71269db3b5eSSebastian Ott static int zpci_freeze(struct device *dev) 71369db3b5eSSebastian Ott { 7141803ba2dSSebastian Ott struct pci_dev *pdev = to_pci_dev(dev); 715198a5278SSebastian Ott struct zpci_dev *zdev = to_zpci(pdev); 71669db3b5eSSebastian Ott 71769db3b5eSSebastian Ott if (zdev->state != ZPCI_FN_STATE_ONLINE) 71869db3b5eSSebastian Ott return 0; 71969db3b5eSSebastian Ott 72069db3b5eSSebastian Ott zpci_unregister_ioat(zdev, 0); 7211803ba2dSSebastian Ott zpci_unmap_resources(pdev); 72269db3b5eSSebastian Ott return clp_disable_fh(zdev); 72369db3b5eSSebastian Ott } 72469db3b5eSSebastian Ott 72569db3b5eSSebastian Ott struct dev_pm_ops pcibios_pm_ops = { 72669db3b5eSSebastian Ott .thaw_noirq = zpci_restore, 72769db3b5eSSebastian Ott .freeze_noirq = zpci_freeze, 72869db3b5eSSebastian Ott .restore_noirq = zpci_restore, 72969db3b5eSSebastian Ott .poweroff_noirq = zpci_freeze, 73069db3b5eSSebastian Ott }; 73169db3b5eSSebastian Ott #endif /* CONFIG_HIBERNATE_CALLBACKS */ 73269db3b5eSSebastian Ott 733cd248341SJan Glauber static int zpci_alloc_domain(struct zpci_dev *zdev) 734cd248341SJan Glauber { 7355c5afd02SSebastian Ott if (zpci_unique_uid) { 7365c5afd02SSebastian Ott zdev->domain = (u16) zdev->uid; 737312e8462SSebastian Ott if (zdev->domain >= ZPCI_NR_DEVICES) 738312e8462SSebastian Ott return 0; 739312e8462SSebastian Ott 740312e8462SSebastian Ott spin_lock(&zpci_domain_lock); 741312e8462SSebastian Ott if (test_bit(zdev->domain, zpci_domain)) { 742312e8462SSebastian Ott spin_unlock(&zpci_domain_lock); 743312e8462SSebastian Ott return -EEXIST; 744312e8462SSebastian Ott } 745312e8462SSebastian Ott set_bit(zdev->domain, zpci_domain); 746312e8462SSebastian Ott spin_unlock(&zpci_domain_lock); 7475c5afd02SSebastian Ott return 0; 7485c5afd02SSebastian Ott } 7495c5afd02SSebastian Ott 750cd248341SJan Glauber spin_lock(&zpci_domain_lock); 751cd248341SJan Glauber zdev->domain = find_first_zero_bit(zpci_domain, ZPCI_NR_DEVICES); 752cd248341SJan Glauber if (zdev->domain == ZPCI_NR_DEVICES) { 753cd248341SJan Glauber spin_unlock(&zpci_domain_lock); 754cd248341SJan Glauber return -ENOSPC; 755cd248341SJan Glauber } 756cd248341SJan Glauber set_bit(zdev->domain, zpci_domain); 757cd248341SJan Glauber spin_unlock(&zpci_domain_lock); 758cd248341SJan Glauber return 0; 759cd248341SJan Glauber } 760cd248341SJan Glauber 761cd248341SJan Glauber static void zpci_free_domain(struct zpci_dev *zdev) 762cd248341SJan Glauber { 763312e8462SSebastian Ott if (zdev->domain >= ZPCI_NR_DEVICES) 7645c5afd02SSebastian Ott return; 7655c5afd02SSebastian Ott 766cd248341SJan Glauber spin_lock(&zpci_domain_lock); 767cd248341SJan Glauber clear_bit(zdev->domain, zpci_domain); 768cd248341SJan Glauber spin_unlock(&zpci_domain_lock); 769cd248341SJan Glauber } 770cd248341SJan Glauber 7717d594322SSebastian Ott void pcibios_remove_bus(struct pci_bus *bus) 7727d594322SSebastian Ott { 7737d594322SSebastian Ott struct zpci_dev *zdev = get_zdev_by_bus(bus); 7747d594322SSebastian Ott 7757d594322SSebastian Ott zpci_exit_slot(zdev); 7767d594322SSebastian Ott zpci_cleanup_bus_resources(zdev); 777f42c2235SJoerg Roedel zpci_destroy_iommu(zdev); 7787d594322SSebastian Ott zpci_free_domain(zdev); 7797d594322SSebastian Ott 7807d594322SSebastian Ott spin_lock(&zpci_list_lock); 7817d594322SSebastian Ott list_del(&zdev->entry); 7827d594322SSebastian Ott spin_unlock(&zpci_list_lock); 7837d594322SSebastian Ott 784be2c3676SSebastian Ott zpci_dbg(3, "rem fid:%x\n", zdev->fid); 7857d594322SSebastian Ott kfree(zdev); 7867d594322SSebastian Ott } 7877d594322SSebastian Ott 7887d594322SSebastian Ott static int zpci_scan_bus(struct zpci_dev *zdev) 7897d594322SSebastian Ott { 7907d594322SSebastian Ott LIST_HEAD(resources); 7917d594322SSebastian Ott int ret; 7927d594322SSebastian Ott 7937d594322SSebastian Ott ret = zpci_setup_bus_resources(zdev, &resources); 7947d594322SSebastian Ott if (ret) 7952b1df724SSebastian Ott goto error; 7967d594322SSebastian Ott 7977d594322SSebastian Ott zdev->bus = pci_scan_root_bus(NULL, ZPCI_BUS_NR, &pci_root_ops, 7987d594322SSebastian Ott zdev, &resources); 7997d594322SSebastian Ott if (!zdev->bus) { 8002b1df724SSebastian Ott ret = -EIO; 8012b1df724SSebastian Ott goto error; 8027d594322SSebastian Ott } 8037d594322SSebastian Ott zdev->bus->max_bus_speed = zdev->max_bus_speed; 804b97ea289SYijing Wang pci_bus_add_devices(zdev->bus); 8057d594322SSebastian Ott return 0; 8062b1df724SSebastian Ott 8072b1df724SSebastian Ott error: 8082b1df724SSebastian Ott zpci_cleanup_bus_resources(zdev); 8092b1df724SSebastian Ott pci_free_resource_list(&resources); 8102b1df724SSebastian Ott return ret; 8117d594322SSebastian Ott } 8127d594322SSebastian Ott 813a755a45dSJan Glauber int zpci_enable_device(struct zpci_dev *zdev) 814a755a45dSJan Glauber { 815a755a45dSJan Glauber int rc; 816a755a45dSJan Glauber 817a755a45dSJan Glauber rc = clp_enable_fh(zdev, ZPCI_NR_DMA_SPACES); 818a755a45dSJan Glauber if (rc) 819a755a45dSJan Glauber goto out; 820828b35f6SJan Glauber 821828b35f6SJan Glauber rc = zpci_dma_init_device(zdev); 822828b35f6SJan Glauber if (rc) 823828b35f6SJan Glauber goto out_dma; 8240ff70ec8SSebastian Ott 8250ff70ec8SSebastian Ott zdev->state = ZPCI_FN_STATE_ONLINE; 826a755a45dSJan Glauber return 0; 827828b35f6SJan Glauber 828828b35f6SJan Glauber out_dma: 829828b35f6SJan Glauber clp_disable_fh(zdev); 830a755a45dSJan Glauber out: 831a755a45dSJan Glauber return rc; 832a755a45dSJan Glauber } 833a755a45dSJan Glauber EXPORT_SYMBOL_GPL(zpci_enable_device); 834a755a45dSJan Glauber 835cb65a669SSebastian Ott int zpci_disable_device(struct zpci_dev *zdev) 836cb65a669SSebastian Ott { 837cb65a669SSebastian Ott zpci_dma_exit_device(zdev); 838cb65a669SSebastian Ott return clp_disable_fh(zdev); 839cb65a669SSebastian Ott } 840cb65a669SSebastian Ott EXPORT_SYMBOL_GPL(zpci_disable_device); 841cb65a669SSebastian Ott 842cd248341SJan Glauber int zpci_create_device(struct zpci_dev *zdev) 843cd248341SJan Glauber { 844cd248341SJan Glauber int rc; 845cd248341SJan Glauber 846cd248341SJan Glauber rc = zpci_alloc_domain(zdev); 847cd248341SJan Glauber if (rc) 848cd248341SJan Glauber goto out; 849cd248341SJan Glauber 850f42c2235SJoerg Roedel rc = zpci_init_iommu(zdev); 851f42c2235SJoerg Roedel if (rc) 852f42c2235SJoerg Roedel goto out_free; 853f42c2235SJoerg Roedel 85480ed156aSSebastian Ott mutex_init(&zdev->lock); 8551c21351bSSebastian Ott if (zdev->state == ZPCI_FN_STATE_CONFIGURED) { 8561c21351bSSebastian Ott rc = zpci_enable_device(zdev); 857cd248341SJan Glauber if (rc) 858f42c2235SJoerg Roedel goto out_destroy_iommu; 8591c21351bSSebastian Ott } 8601c21351bSSebastian Ott rc = zpci_scan_bus(zdev); 8611c21351bSSebastian Ott if (rc) 8621c21351bSSebastian Ott goto out_disable; 863cd248341SJan Glauber 86457b5918cSSebastian Ott spin_lock(&zpci_list_lock); 865cd248341SJan Glauber list_add_tail(&zdev->entry, &zpci_list); 86657b5918cSSebastian Ott spin_unlock(&zpci_list_lock); 867cd248341SJan Glauber 86867f43f38SSebastian Ott zpci_init_slot(zdev); 86967f43f38SSebastian Ott 870cd248341SJan Glauber return 0; 871cd248341SJan Glauber 8721c21351bSSebastian Ott out_disable: 8731c21351bSSebastian Ott if (zdev->state == ZPCI_FN_STATE_ONLINE) 8741c21351bSSebastian Ott zpci_disable_device(zdev); 875f42c2235SJoerg Roedel out_destroy_iommu: 876f42c2235SJoerg Roedel zpci_destroy_iommu(zdev); 8771c21351bSSebastian Ott out_free: 878cd248341SJan Glauber zpci_free_domain(zdev); 879cd248341SJan Glauber out: 880cd248341SJan Glauber return rc; 881cd248341SJan Glauber } 882cd248341SJan Glauber 883623bd44dSSebastian Ott void zpci_remove_device(struct zpci_dev *zdev) 884623bd44dSSebastian Ott { 885623bd44dSSebastian Ott if (!zdev->bus) 886623bd44dSSebastian Ott return; 887623bd44dSSebastian Ott 888623bd44dSSebastian Ott pci_stop_root_bus(zdev->bus); 889623bd44dSSebastian Ott pci_remove_root_bus(zdev->bus); 890623bd44dSSebastian Ott } 891623bd44dSSebastian Ott 892bd3a1725SMartin Schwidefsky int zpci_report_error(struct pci_dev *pdev, 893bd3a1725SMartin Schwidefsky struct zpci_report_error_header *report) 894bd3a1725SMartin Schwidefsky { 895bd3a1725SMartin Schwidefsky struct zpci_dev *zdev = to_zpci(pdev); 896bd3a1725SMartin Schwidefsky 897bd3a1725SMartin Schwidefsky return sclp_pci_report(report, zdev->fh, zdev->fid); 898bd3a1725SMartin Schwidefsky } 899bd3a1725SMartin Schwidefsky EXPORT_SYMBOL(zpci_report_error); 900bd3a1725SMartin Schwidefsky 901cd248341SJan Glauber static int zpci_mem_init(void) 902cd248341SJan Glauber { 90380c544deSSebastian Ott BUILD_BUG_ON(!is_power_of_2(__alignof__(struct zpci_fmb)) || 90480c544deSSebastian Ott __alignof__(struct zpci_fmb) < sizeof(struct zpci_fmb)); 90580c544deSSebastian Ott 906d0b08853SJan Glauber zdev_fmb_cache = kmem_cache_create("PCI_FMB_cache", sizeof(struct zpci_fmb), 90780c544deSSebastian Ott __alignof__(struct zpci_fmb), 0, NULL); 908d0b08853SJan Glauber if (!zdev_fmb_cache) 909c506fff3SSebastian Ott goto error_fmb; 910d0b08853SJan Glauber 911c506fff3SSebastian Ott zpci_iomap_start = kcalloc(ZPCI_IOMAP_ENTRIES, 912c506fff3SSebastian Ott sizeof(*zpci_iomap_start), GFP_KERNEL); 913cd248341SJan Glauber if (!zpci_iomap_start) 9149a4da8a5SJan Glauber goto error_iomap; 915cd248341SJan Glauber 916c506fff3SSebastian Ott zpci_iomap_bitmap = kcalloc(BITS_TO_LONGS(ZPCI_IOMAP_ENTRIES), 917c506fff3SSebastian Ott sizeof(*zpci_iomap_bitmap), GFP_KERNEL); 918c506fff3SSebastian Ott if (!zpci_iomap_bitmap) 919c506fff3SSebastian Ott goto error_iomap_bitmap; 920c506fff3SSebastian Ott 921c506fff3SSebastian Ott return 0; 922c506fff3SSebastian Ott error_iomap_bitmap: 923c506fff3SSebastian Ott kfree(zpci_iomap_start); 9249a4da8a5SJan Glauber error_iomap: 925d0b08853SJan Glauber kmem_cache_destroy(zdev_fmb_cache); 926c506fff3SSebastian Ott error_fmb: 927cd248341SJan Glauber return -ENOMEM; 928cd248341SJan Glauber } 929cd248341SJan Glauber 930cd248341SJan Glauber static void zpci_mem_exit(void) 931cd248341SJan Glauber { 932c506fff3SSebastian Ott kfree(zpci_iomap_bitmap); 933cd248341SJan Glauber kfree(zpci_iomap_start); 934d0b08853SJan Glauber kmem_cache_destroy(zdev_fmb_cache); 935cd248341SJan Glauber } 936cd248341SJan Glauber 937257608fbSSebastian Ott static unsigned int s390_pci_probe = 1; 938aa3b7c29SSebastian Ott static unsigned int s390_pci_initialized; 939cd248341SJan Glauber 940cd248341SJan Glauber char * __init pcibios_setup(char *str) 941cd248341SJan Glauber { 942257608fbSSebastian Ott if (!strcmp(str, "off")) { 943257608fbSSebastian Ott s390_pci_probe = 0; 944cd248341SJan Glauber return NULL; 945cd248341SJan Glauber } 946cd248341SJan Glauber return str; 947cd248341SJan Glauber } 948cd248341SJan Glauber 949aa3b7c29SSebastian Ott bool zpci_is_enabled(void) 950aa3b7c29SSebastian Ott { 951aa3b7c29SSebastian Ott return s390_pci_initialized; 952aa3b7c29SSebastian Ott } 953aa3b7c29SSebastian Ott 954cd248341SJan Glauber static int __init pci_base_init(void) 955cd248341SJan Glauber { 956cd248341SJan Glauber int rc; 957cd248341SJan Glauber 9581e5635d1SHeiko Carstens if (!s390_pci_probe) 959cd248341SJan Glauber return 0; 960cd248341SJan Glauber 96148070c73SChristian Borntraeger if (!test_facility(69) || !test_facility(71)) 962cd248341SJan Glauber return 0; 963cd248341SJan Glauber 964d0b08853SJan Glauber rc = zpci_debug_init(); 965d0b08853SJan Glauber if (rc) 9661f44a225SMartin Schwidefsky goto out; 967d0b08853SJan Glauber 968cd248341SJan Glauber rc = zpci_mem_init(); 969cd248341SJan Glauber if (rc) 970cd248341SJan Glauber goto out_mem; 971cd248341SJan Glauber 9729a4da8a5SJan Glauber rc = zpci_irq_init(); 9739a4da8a5SJan Glauber if (rc) 9749a4da8a5SJan Glauber goto out_irq; 9759a4da8a5SJan Glauber 976828b35f6SJan Glauber rc = zpci_dma_init(); 977828b35f6SJan Glauber if (rc) 978828b35f6SJan Glauber goto out_dma; 979828b35f6SJan Glauber 9801d578966SSebastian Ott rc = clp_scan_pci_devices(); 981a755a45dSJan Glauber if (rc) 982a755a45dSJan Glauber goto out_find; 983a755a45dSJan Glauber 984aa3b7c29SSebastian Ott s390_pci_initialized = 1; 985cd248341SJan Glauber return 0; 986cd248341SJan Glauber 987a755a45dSJan Glauber out_find: 988828b35f6SJan Glauber zpci_dma_exit(); 989828b35f6SJan Glauber out_dma: 9909a4da8a5SJan Glauber zpci_irq_exit(); 9919a4da8a5SJan Glauber out_irq: 992cd248341SJan Glauber zpci_mem_exit(); 993cd248341SJan Glauber out_mem: 994d0b08853SJan Glauber zpci_debug_exit(); 9951f44a225SMartin Schwidefsky out: 996cd248341SJan Glauber return rc; 997cd248341SJan Glauber } 99867f43f38SSebastian Ott subsys_initcall_sync(pci_base_init); 99957b5918cSSebastian Ott 100057b5918cSSebastian Ott void zpci_rescan(void) 100157b5918cSSebastian Ott { 1002aa3b7c29SSebastian Ott if (zpci_is_enabled()) 100357b5918cSSebastian Ott clp_rescan_pci_devices_simple(); 100457b5918cSSebastian Ott } 1005