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> 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 288e8e25a77SSebastian Ott if (!pci_resource_len(pdev, bar) || bar >= PCI_BAR_COUNT) 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); 385614db269SSebastian Ott airq_iv_lock(aibv, ai); 3861f44a225SMartin Schwidefsky generic_handle_irq(airq_iv_get_data(aibv, ai)); 387614db269SSebastian Ott 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 */ 413614db269SSebastian Ott 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) { 423866f3576SSebastian Ott if (hwirq >= msi_vecs) 424866f3576SSebastian Ott break; 4251f44a225SMartin Schwidefsky irq = irq_alloc_desc(0); /* Alloc irq on node 0 */ 4260a0a9421SThomas Gleixner if (irq < 0) 427795818e8SSebastian Ott return -ENOMEM; 4281f44a225SMartin Schwidefsky rc = irq_set_msi_desc(irq, msi); 4299a4da8a5SJan Glauber if (rc) 430795818e8SSebastian Ott return rc; 4311f44a225SMartin Schwidefsky irq_set_chip_and_handler(irq, &zpci_irq_chip, 4321f44a225SMartin Schwidefsky handle_simple_irq); 4331f44a225SMartin Schwidefsky msg.data = hwirq; 4341f44a225SMartin Schwidefsky msg.address_lo = zdev->msi_addr & 0xffffffff; 4351f44a225SMartin Schwidefsky msg.address_hi = zdev->msi_addr >> 32; 43683a18912SJiang Liu pci_write_msi_msg(irq, &msg); 4371f44a225SMartin Schwidefsky airq_iv_set_data(zdev->aibv, hwirq, irq); 4381f44a225SMartin Schwidefsky hwirq++; 4399a4da8a5SJan Glauber } 4409a4da8a5SJan Glauber 4415d0d8f43SMartin Schwidefsky /* Enable adapter interrupts */ 4425d0d8f43SMartin Schwidefsky rc = zpci_set_airq(zdev); 4435d0d8f43SMartin Schwidefsky if (rc) 444795818e8SSebastian Ott return rc; 4455d0d8f43SMartin Schwidefsky 4465d0d8f43SMartin Schwidefsky return (msi_vecs == nvec) ? 0 : msi_vecs; 4479a4da8a5SJan Glauber } 4489a4da8a5SJan Glauber 4495d0d8f43SMartin Schwidefsky void arch_teardown_msi_irqs(struct pci_dev *pdev) 4509a4da8a5SJan Glauber { 451198a5278SSebastian Ott struct zpci_dev *zdev = to_zpci(pdev); 4529a4da8a5SJan Glauber struct msi_desc *msi; 4535d0d8f43SMartin Schwidefsky int rc; 4549a4da8a5SJan Glauber 4555d0d8f43SMartin Schwidefsky /* Disable adapter interrupts */ 4565d0d8f43SMartin Schwidefsky rc = zpci_clear_airq(zdev); 4571f1dcbd4SSebastian Ott if (rc) 4589a4da8a5SJan Glauber return; 4599a4da8a5SJan Glauber 4601f44a225SMartin Schwidefsky /* Release MSI interrupts */ 461df516f42SJiang Liu for_each_pci_msi_entry(msi, pdev) { 462795818e8SSebastian Ott if (!msi->irq) 463795818e8SSebastian Ott continue; 4648fb878c5SYijing Wang if (msi->msi_attrib.is_msix) 46523ed8d57SThomas Gleixner __pci_msix_desc_mask_irq(msi, 1); 4668fb878c5SYijing Wang else 46723ed8d57SThomas Gleixner __pci_msi_desc_mask_irq(msi, 1, 1); 4681f44a225SMartin Schwidefsky irq_set_msi_desc(msi->irq, NULL); 4691f44a225SMartin Schwidefsky irq_free_desc(msi->irq); 4701f44a225SMartin Schwidefsky msi->msg.address_lo = 0; 4711f44a225SMartin Schwidefsky msi->msg.address_hi = 0; 4721f44a225SMartin Schwidefsky msi->msg.data = 0; 4731f44a225SMartin Schwidefsky msi->irq = 0; 4741f44a225SMartin Schwidefsky } 4759a4da8a5SJan Glauber 476795818e8SSebastian Ott if (zdev->aisb != -1UL) { 4771f44a225SMartin Schwidefsky zpci_aibv[zdev->aisb] = NULL; 4785d0d8f43SMartin Schwidefsky airq_iv_free_bit(zpci_aisb_iv, zdev->aisb); 479795818e8SSebastian Ott zdev->aisb = -1UL; 480795818e8SSebastian Ott } 481795818e8SSebastian Ott if (zdev->aibv) { 482795818e8SSebastian Ott airq_iv_release(zdev->aibv); 483795818e8SSebastian Ott zdev->aibv = NULL; 484795818e8SSebastian Ott } 4859a4da8a5SJan Glauber } 4869a4da8a5SJan Glauber 487cfbb4a7aSSebastian Ott #ifdef CONFIG_PCI_IOV 488cfbb4a7aSSebastian Ott static struct resource iov_res = { 489cfbb4a7aSSebastian Ott .name = "PCI IOV res", 490cfbb4a7aSSebastian Ott .start = 0, 491cfbb4a7aSSebastian Ott .end = -1, 492cfbb4a7aSSebastian Ott .flags = IORESOURCE_MEM, 493cfbb4a7aSSebastian Ott }; 494cfbb4a7aSSebastian Ott #endif 495cfbb4a7aSSebastian Ott 4961803ba2dSSebastian Ott static void zpci_map_resources(struct pci_dev *pdev) 497cd248341SJan Glauber { 498cd248341SJan Glauber resource_size_t len; 499cd248341SJan Glauber int i; 500cd248341SJan Glauber 501cd248341SJan Glauber for (i = 0; i < PCI_BAR_COUNT; i++) { 502cd248341SJan Glauber len = pci_resource_len(pdev, i); 503cd248341SJan Glauber if (!len) 504cd248341SJan Glauber continue; 5055b9f2081SMartin Schwidefsky pdev->resource[i].start = 5065b9f2081SMartin Schwidefsky (resource_size_t __force) pci_iomap(pdev, i, 0); 507cd248341SJan Glauber pdev->resource[i].end = pdev->resource[i].start + len - 1; 508cd248341SJan Glauber } 509cfbb4a7aSSebastian Ott 510cfbb4a7aSSebastian Ott #ifdef CONFIG_PCI_IOV 511cfbb4a7aSSebastian Ott i = PCI_IOV_RESOURCES; 512cfbb4a7aSSebastian Ott 513cfbb4a7aSSebastian Ott for (; i < PCI_SRIOV_NUM_BARS + PCI_IOV_RESOURCES; i++) { 514cfbb4a7aSSebastian Ott len = pci_resource_len(pdev, i); 515cfbb4a7aSSebastian Ott if (!len) 516cfbb4a7aSSebastian Ott continue; 517cfbb4a7aSSebastian Ott pdev->resource[i].parent = &iov_res; 518cfbb4a7aSSebastian Ott } 519cfbb4a7aSSebastian Ott #endif 520944239c5SSebastian Ott } 521944239c5SSebastian Ott 5221803ba2dSSebastian Ott static void zpci_unmap_resources(struct pci_dev *pdev) 523944239c5SSebastian Ott { 524944239c5SSebastian Ott resource_size_t len; 525944239c5SSebastian Ott int i; 526944239c5SSebastian Ott 527944239c5SSebastian Ott for (i = 0; i < PCI_BAR_COUNT; i++) { 528944239c5SSebastian Ott len = pci_resource_len(pdev, i); 529944239c5SSebastian Ott if (!len) 530944239c5SSebastian Ott continue; 5315b9f2081SMartin Schwidefsky pci_iounmap(pdev, (void __iomem __force *) 5325b9f2081SMartin Schwidefsky pdev->resource[i].start); 533944239c5SSebastian Ott } 534944239c5SSebastian Ott } 535cd248341SJan Glauber 536561ecb0cSSebastian Ott static struct airq_struct zpci_airq = { 537561ecb0cSSebastian Ott .handler = zpci_irq_handler, 538561ecb0cSSebastian Ott .isc = PCI_ISC, 539561ecb0cSSebastian Ott }; 540561ecb0cSSebastian Ott 5419a4da8a5SJan Glauber static int __init zpci_irq_init(void) 5429a4da8a5SJan Glauber { 5435d0d8f43SMartin Schwidefsky int rc; 5449a4da8a5SJan Glauber 545f4eae94fSMartin Schwidefsky rc = register_adapter_interrupt(&zpci_airq); 546f4eae94fSMartin Schwidefsky if (rc) 5475d0d8f43SMartin Schwidefsky goto out; 548f4eae94fSMartin Schwidefsky /* Set summary to 1 to be called every time for the ISC. */ 549f4eae94fSMartin Schwidefsky *zpci_airq.lsi_ptr = 1; 5509a4da8a5SJan Glauber 5515d0d8f43SMartin Schwidefsky rc = -ENOMEM; 5525d0d8f43SMartin Schwidefsky zpci_aisb_iv = airq_iv_create(ZPCI_NR_DEVICES, AIRQ_IV_ALLOC); 5535d0d8f43SMartin Schwidefsky if (!zpci_aisb_iv) 5545d0d8f43SMartin Schwidefsky goto out_airq; 5559a4da8a5SJan Glauber 5569389339fSMartin Schwidefsky zpci_set_irq_ctrl(SIC_IRQ_MODE_SINGLE, NULL, PCI_ISC); 5579a4da8a5SJan Glauber return 0; 5589a4da8a5SJan Glauber 5595d0d8f43SMartin Schwidefsky out_airq: 5605d0d8f43SMartin Schwidefsky unregister_adapter_interrupt(&zpci_airq); 5615d0d8f43SMartin Schwidefsky out: 5629a4da8a5SJan Glauber return rc; 5639a4da8a5SJan Glauber } 5649a4da8a5SJan Glauber 5659a4da8a5SJan Glauber static void zpci_irq_exit(void) 5669a4da8a5SJan Glauber { 5675d0d8f43SMartin Schwidefsky airq_iv_release(zpci_aisb_iv); 568f4eae94fSMartin Schwidefsky unregister_adapter_interrupt(&zpci_airq); 5699a4da8a5SJan Glauber } 5709a4da8a5SJan Glauber 571cd248341SJan Glauber static int zpci_alloc_iomap(struct zpci_dev *zdev) 572cd248341SJan Glauber { 573bf19c94dSSebastian Ott unsigned long entry; 574cd248341SJan Glauber 575cd248341SJan Glauber spin_lock(&zpci_iomap_lock); 576c506fff3SSebastian Ott entry = find_first_zero_bit(zpci_iomap_bitmap, ZPCI_IOMAP_ENTRIES); 577c506fff3SSebastian Ott if (entry == ZPCI_IOMAP_ENTRIES) { 578cd248341SJan Glauber spin_unlock(&zpci_iomap_lock); 579cd248341SJan Glauber return -ENOSPC; 580cd248341SJan Glauber } 581c506fff3SSebastian Ott set_bit(entry, zpci_iomap_bitmap); 582cd248341SJan Glauber spin_unlock(&zpci_iomap_lock); 583cd248341SJan Glauber return entry; 584cd248341SJan Glauber } 585cd248341SJan Glauber 586cd248341SJan Glauber static void zpci_free_iomap(struct zpci_dev *zdev, int entry) 587cd248341SJan Glauber { 588cd248341SJan Glauber spin_lock(&zpci_iomap_lock); 589cd248341SJan Glauber memset(&zpci_iomap_start[entry], 0, sizeof(struct zpci_iomap_entry)); 590c506fff3SSebastian Ott clear_bit(entry, zpci_iomap_bitmap); 591cd248341SJan Glauber spin_unlock(&zpci_iomap_lock); 592cd248341SJan Glauber } 593cd248341SJan Glauber 5947a572a3aSSebastian Ott static struct resource *__alloc_res(struct zpci_dev *zdev, unsigned long start, 5957a572a3aSSebastian Ott unsigned long size, unsigned long flags) 5967a572a3aSSebastian Ott { 5977a572a3aSSebastian Ott struct resource *r; 5987a572a3aSSebastian Ott 5997a572a3aSSebastian Ott r = kzalloc(sizeof(*r), GFP_KERNEL); 6007a572a3aSSebastian Ott if (!r) 6017a572a3aSSebastian Ott return NULL; 6027a572a3aSSebastian Ott 6037a572a3aSSebastian Ott r->start = start; 6047a572a3aSSebastian Ott r->end = r->start + size - 1; 6057a572a3aSSebastian Ott r->flags = flags; 6067a572a3aSSebastian Ott r->name = zdev->res_name; 6077a572a3aSSebastian Ott 6087a572a3aSSebastian Ott if (request_resource(&iomem_resource, r)) { 6097a572a3aSSebastian Ott kfree(r); 6107a572a3aSSebastian Ott return NULL; 6117a572a3aSSebastian Ott } 6127a572a3aSSebastian Ott return r; 6137a572a3aSSebastian Ott } 6147a572a3aSSebastian Ott 6157a572a3aSSebastian Ott static int zpci_setup_bus_resources(struct zpci_dev *zdev, 6167a572a3aSSebastian Ott struct list_head *resources) 6177a572a3aSSebastian Ott { 6187a572a3aSSebastian Ott unsigned long addr, size, flags; 6197a572a3aSSebastian Ott struct resource *res; 6207a572a3aSSebastian Ott int i, entry; 6217a572a3aSSebastian Ott 6227a572a3aSSebastian Ott snprintf(zdev->res_name, sizeof(zdev->res_name), 6237a572a3aSSebastian Ott "PCI Bus %04x:%02x", zdev->domain, ZPCI_BUS_NR); 6247a572a3aSSebastian Ott 6257a572a3aSSebastian Ott for (i = 0; i < PCI_BAR_COUNT; i++) { 6267a572a3aSSebastian Ott if (!zdev->bars[i].size) 6277a572a3aSSebastian Ott continue; 6287a572a3aSSebastian Ott entry = zpci_alloc_iomap(zdev); 6297a572a3aSSebastian Ott if (entry < 0) 6307a572a3aSSebastian Ott return entry; 6317a572a3aSSebastian Ott zdev->bars[i].map_idx = entry; 6327a572a3aSSebastian Ott 6337a572a3aSSebastian Ott /* only MMIO is supported */ 6347a572a3aSSebastian Ott flags = IORESOURCE_MEM; 6357a572a3aSSebastian Ott if (zdev->bars[i].val & 8) 6367a572a3aSSebastian Ott flags |= IORESOURCE_PREFETCH; 6377a572a3aSSebastian Ott if (zdev->bars[i].val & 4) 6387a572a3aSSebastian Ott flags |= IORESOURCE_MEM_64; 6397a572a3aSSebastian Ott 6409e00caaeSSebastian Ott addr = ZPCI_ADDR(entry); 6417a572a3aSSebastian Ott size = 1UL << zdev->bars[i].size; 6427a572a3aSSebastian Ott 6437a572a3aSSebastian Ott res = __alloc_res(zdev, addr, size, flags); 6447a572a3aSSebastian Ott if (!res) { 6457a572a3aSSebastian Ott zpci_free_iomap(zdev, entry); 6467a572a3aSSebastian Ott return -ENOMEM; 6477a572a3aSSebastian Ott } 6487a572a3aSSebastian Ott zdev->bars[i].res = res; 6497a572a3aSSebastian Ott pci_add_resource(resources, res); 6507a572a3aSSebastian Ott } 6517a572a3aSSebastian Ott 6527a572a3aSSebastian Ott return 0; 6537a572a3aSSebastian Ott } 6547a572a3aSSebastian Ott 6557a572a3aSSebastian Ott static void zpci_cleanup_bus_resources(struct zpci_dev *zdev) 6567a572a3aSSebastian Ott { 6577a572a3aSSebastian Ott int i; 6587a572a3aSSebastian Ott 6597a572a3aSSebastian Ott for (i = 0; i < PCI_BAR_COUNT; i++) { 6602b1df724SSebastian Ott if (!zdev->bars[i].size || !zdev->bars[i].res) 6617a572a3aSSebastian Ott continue; 6627a572a3aSSebastian Ott 6637a572a3aSSebastian Ott zpci_free_iomap(zdev, zdev->bars[i].map_idx); 6647a572a3aSSebastian Ott release_resource(zdev->bars[i].res); 6657a572a3aSSebastian Ott kfree(zdev->bars[i].res); 6667a572a3aSSebastian Ott } 6677a572a3aSSebastian Ott } 6687a572a3aSSebastian Ott 669af0a8a84SSebastian Ott int pcibios_add_device(struct pci_dev *pdev) 670af0a8a84SSebastian Ott { 671cb809182SSebastian Ott struct resource *res; 672cb809182SSebastian Ott int i; 673cb809182SSebastian Ott 6747dc20ab1SSebastian Ott if (pdev->is_physfn) 6757dc20ab1SSebastian Ott pdev->no_vf_scan = 1; 6767dc20ab1SSebastian Ott 677ef4858c6SSebastian Ott pdev->dev.groups = zpci_attr_groups; 6785657933dSBart Van Assche pdev->dev.dma_ops = &s390_pci_dma_ops; 6791803ba2dSSebastian Ott zpci_map_resources(pdev); 680cb809182SSebastian Ott 681cb809182SSebastian Ott for (i = 0; i < PCI_BAR_COUNT; i++) { 682cb809182SSebastian Ott res = &pdev->resource[i]; 683cb809182SSebastian Ott if (res->parent || !res->flags) 684cb809182SSebastian Ott continue; 685cb809182SSebastian Ott pci_claim_resource(pdev, i); 686cb809182SSebastian Ott } 687cb809182SSebastian Ott 688cb809182SSebastian Ott return 0; 689cb809182SSebastian Ott } 690cb809182SSebastian Ott 6911803ba2dSSebastian Ott void pcibios_release_device(struct pci_dev *pdev) 6921803ba2dSSebastian Ott { 6931803ba2dSSebastian Ott zpci_unmap_resources(pdev); 6941803ba2dSSebastian Ott } 6951803ba2dSSebastian Ott 696cb809182SSebastian Ott int pcibios_enable_device(struct pci_dev *pdev, int mask) 697cb809182SSebastian Ott { 698198a5278SSebastian Ott struct zpci_dev *zdev = to_zpci(pdev); 699af0a8a84SSebastian Ott 7009a99649fSSebastian Ott zpci_debug_init_device(zdev, dev_name(&pdev->dev)); 701af0a8a84SSebastian Ott zpci_fmb_enable_device(zdev); 702af0a8a84SSebastian Ott 703d7533232SBjorn Helgaas return pci_enable_resources(pdev, mask); 704af0a8a84SSebastian Ott } 705af0a8a84SSebastian Ott 706cb809182SSebastian Ott void pcibios_disable_device(struct pci_dev *pdev) 707944239c5SSebastian Ott { 708198a5278SSebastian Ott struct zpci_dev *zdev = to_zpci(pdev); 709944239c5SSebastian Ott 710944239c5SSebastian Ott zpci_fmb_disable_device(zdev); 711944239c5SSebastian Ott zpci_debug_exit_device(zdev); 712944239c5SSebastian Ott } 713944239c5SSebastian Ott 71469db3b5eSSebastian Ott #ifdef CONFIG_HIBERNATE_CALLBACKS 71569db3b5eSSebastian Ott static int zpci_restore(struct device *dev) 71669db3b5eSSebastian Ott { 7171803ba2dSSebastian Ott struct pci_dev *pdev = to_pci_dev(dev); 718198a5278SSebastian Ott struct zpci_dev *zdev = to_zpci(pdev); 71969db3b5eSSebastian Ott int ret = 0; 72069db3b5eSSebastian Ott 72169db3b5eSSebastian Ott if (zdev->state != ZPCI_FN_STATE_ONLINE) 72269db3b5eSSebastian Ott goto out; 72369db3b5eSSebastian Ott 72469db3b5eSSebastian Ott ret = clp_enable_fh(zdev, ZPCI_NR_DMA_SPACES); 72569db3b5eSSebastian Ott if (ret) 72669db3b5eSSebastian Ott goto out; 72769db3b5eSSebastian Ott 7281803ba2dSSebastian Ott zpci_map_resources(pdev); 72969eea95cSGerald Schaefer zpci_register_ioat(zdev, 0, zdev->start_dma, zdev->end_dma, 73069db3b5eSSebastian Ott (u64) zdev->dma_table); 73169db3b5eSSebastian Ott 73269db3b5eSSebastian Ott out: 73369db3b5eSSebastian Ott return ret; 73469db3b5eSSebastian Ott } 73569db3b5eSSebastian Ott 73669db3b5eSSebastian Ott static int zpci_freeze(struct device *dev) 73769db3b5eSSebastian Ott { 7381803ba2dSSebastian Ott struct pci_dev *pdev = to_pci_dev(dev); 739198a5278SSebastian Ott struct zpci_dev *zdev = to_zpci(pdev); 74069db3b5eSSebastian Ott 74169db3b5eSSebastian Ott if (zdev->state != ZPCI_FN_STATE_ONLINE) 74269db3b5eSSebastian Ott return 0; 74369db3b5eSSebastian Ott 74469db3b5eSSebastian Ott zpci_unregister_ioat(zdev, 0); 7451803ba2dSSebastian Ott zpci_unmap_resources(pdev); 74669db3b5eSSebastian Ott return clp_disable_fh(zdev); 74769db3b5eSSebastian Ott } 74869db3b5eSSebastian Ott 74969db3b5eSSebastian Ott struct dev_pm_ops pcibios_pm_ops = { 75069db3b5eSSebastian Ott .thaw_noirq = zpci_restore, 75169db3b5eSSebastian Ott .freeze_noirq = zpci_freeze, 75269db3b5eSSebastian Ott .restore_noirq = zpci_restore, 75369db3b5eSSebastian Ott .poweroff_noirq = zpci_freeze, 75469db3b5eSSebastian Ott }; 75569db3b5eSSebastian Ott #endif /* CONFIG_HIBERNATE_CALLBACKS */ 75669db3b5eSSebastian Ott 757cd248341SJan Glauber static int zpci_alloc_domain(struct zpci_dev *zdev) 758cd248341SJan Glauber { 7595c5afd02SSebastian Ott if (zpci_unique_uid) { 7605c5afd02SSebastian Ott zdev->domain = (u16) zdev->uid; 761312e8462SSebastian Ott if (zdev->domain >= ZPCI_NR_DEVICES) 762312e8462SSebastian Ott return 0; 763312e8462SSebastian Ott 764312e8462SSebastian Ott spin_lock(&zpci_domain_lock); 765312e8462SSebastian Ott if (test_bit(zdev->domain, zpci_domain)) { 766312e8462SSebastian Ott spin_unlock(&zpci_domain_lock); 767312e8462SSebastian Ott return -EEXIST; 768312e8462SSebastian Ott } 769312e8462SSebastian Ott set_bit(zdev->domain, zpci_domain); 770312e8462SSebastian Ott spin_unlock(&zpci_domain_lock); 7715c5afd02SSebastian Ott return 0; 7725c5afd02SSebastian Ott } 7735c5afd02SSebastian Ott 774cd248341SJan Glauber spin_lock(&zpci_domain_lock); 775cd248341SJan Glauber zdev->domain = find_first_zero_bit(zpci_domain, ZPCI_NR_DEVICES); 776cd248341SJan Glauber if (zdev->domain == ZPCI_NR_DEVICES) { 777cd248341SJan Glauber spin_unlock(&zpci_domain_lock); 778cd248341SJan Glauber return -ENOSPC; 779cd248341SJan Glauber } 780cd248341SJan Glauber set_bit(zdev->domain, zpci_domain); 781cd248341SJan Glauber spin_unlock(&zpci_domain_lock); 782cd248341SJan Glauber return 0; 783cd248341SJan Glauber } 784cd248341SJan Glauber 785cd248341SJan Glauber static void zpci_free_domain(struct zpci_dev *zdev) 786cd248341SJan Glauber { 787312e8462SSebastian Ott if (zdev->domain >= ZPCI_NR_DEVICES) 7885c5afd02SSebastian Ott return; 7895c5afd02SSebastian Ott 790cd248341SJan Glauber spin_lock(&zpci_domain_lock); 791cd248341SJan Glauber clear_bit(zdev->domain, zpci_domain); 792cd248341SJan Glauber spin_unlock(&zpci_domain_lock); 793cd248341SJan Glauber } 794cd248341SJan Glauber 7957d594322SSebastian Ott void pcibios_remove_bus(struct pci_bus *bus) 7967d594322SSebastian Ott { 7977d594322SSebastian Ott struct zpci_dev *zdev = get_zdev_by_bus(bus); 7987d594322SSebastian Ott 7997d594322SSebastian Ott zpci_exit_slot(zdev); 8007d594322SSebastian Ott zpci_cleanup_bus_resources(zdev); 801f42c2235SJoerg Roedel zpci_destroy_iommu(zdev); 8027d594322SSebastian Ott zpci_free_domain(zdev); 8037d594322SSebastian Ott 8047d594322SSebastian Ott spin_lock(&zpci_list_lock); 8057d594322SSebastian Ott list_del(&zdev->entry); 8067d594322SSebastian Ott spin_unlock(&zpci_list_lock); 8077d594322SSebastian Ott 808be2c3676SSebastian Ott zpci_dbg(3, "rem fid:%x\n", zdev->fid); 8097d594322SSebastian Ott kfree(zdev); 8107d594322SSebastian Ott } 8117d594322SSebastian Ott 8127d594322SSebastian Ott static int zpci_scan_bus(struct zpci_dev *zdev) 8137d594322SSebastian Ott { 8147d594322SSebastian Ott LIST_HEAD(resources); 8157d594322SSebastian Ott int ret; 8167d594322SSebastian Ott 8177d594322SSebastian Ott ret = zpci_setup_bus_resources(zdev, &resources); 8187d594322SSebastian Ott if (ret) 8192b1df724SSebastian Ott goto error; 8207d594322SSebastian Ott 8217d594322SSebastian Ott zdev->bus = pci_scan_root_bus(NULL, ZPCI_BUS_NR, &pci_root_ops, 8227d594322SSebastian Ott zdev, &resources); 8237d594322SSebastian Ott if (!zdev->bus) { 8242b1df724SSebastian Ott ret = -EIO; 8252b1df724SSebastian Ott goto error; 8267d594322SSebastian Ott } 8277d594322SSebastian Ott zdev->bus->max_bus_speed = zdev->max_bus_speed; 828b97ea289SYijing Wang pci_bus_add_devices(zdev->bus); 8297d594322SSebastian Ott return 0; 8302b1df724SSebastian Ott 8312b1df724SSebastian Ott error: 8322b1df724SSebastian Ott zpci_cleanup_bus_resources(zdev); 8332b1df724SSebastian Ott pci_free_resource_list(&resources); 8342b1df724SSebastian Ott return ret; 8357d594322SSebastian Ott } 8367d594322SSebastian Ott 837a755a45dSJan Glauber int zpci_enable_device(struct zpci_dev *zdev) 838a755a45dSJan Glauber { 839a755a45dSJan Glauber int rc; 840a755a45dSJan Glauber 841a755a45dSJan Glauber rc = clp_enable_fh(zdev, ZPCI_NR_DMA_SPACES); 842a755a45dSJan Glauber if (rc) 843a755a45dSJan Glauber goto out; 844828b35f6SJan Glauber 845828b35f6SJan Glauber rc = zpci_dma_init_device(zdev); 846828b35f6SJan Glauber if (rc) 847828b35f6SJan Glauber goto out_dma; 8480ff70ec8SSebastian Ott 8490ff70ec8SSebastian Ott zdev->state = ZPCI_FN_STATE_ONLINE; 850a755a45dSJan Glauber return 0; 851828b35f6SJan Glauber 852828b35f6SJan Glauber out_dma: 853828b35f6SJan Glauber clp_disable_fh(zdev); 854a755a45dSJan Glauber out: 855a755a45dSJan Glauber return rc; 856a755a45dSJan Glauber } 857a755a45dSJan Glauber EXPORT_SYMBOL_GPL(zpci_enable_device); 858a755a45dSJan Glauber 859cb65a669SSebastian Ott int zpci_disable_device(struct zpci_dev *zdev) 860cb65a669SSebastian Ott { 861cb65a669SSebastian Ott zpci_dma_exit_device(zdev); 862cb65a669SSebastian Ott return clp_disable_fh(zdev); 863cb65a669SSebastian Ott } 864cb65a669SSebastian Ott EXPORT_SYMBOL_GPL(zpci_disable_device); 865cb65a669SSebastian Ott 866cd248341SJan Glauber int zpci_create_device(struct zpci_dev *zdev) 867cd248341SJan Glauber { 868cd248341SJan Glauber int rc; 869cd248341SJan Glauber 870cd248341SJan Glauber rc = zpci_alloc_domain(zdev); 871cd248341SJan Glauber if (rc) 872cd248341SJan Glauber goto out; 873cd248341SJan Glauber 874f42c2235SJoerg Roedel rc = zpci_init_iommu(zdev); 875f42c2235SJoerg Roedel if (rc) 876f42c2235SJoerg Roedel goto out_free; 877f42c2235SJoerg Roedel 87880ed156aSSebastian Ott mutex_init(&zdev->lock); 8791c21351bSSebastian Ott if (zdev->state == ZPCI_FN_STATE_CONFIGURED) { 8801c21351bSSebastian Ott rc = zpci_enable_device(zdev); 881cd248341SJan Glauber if (rc) 882f42c2235SJoerg Roedel goto out_destroy_iommu; 8831c21351bSSebastian Ott } 8841c21351bSSebastian Ott rc = zpci_scan_bus(zdev); 8851c21351bSSebastian Ott if (rc) 8861c21351bSSebastian Ott goto out_disable; 887cd248341SJan Glauber 88857b5918cSSebastian Ott spin_lock(&zpci_list_lock); 889cd248341SJan Glauber list_add_tail(&zdev->entry, &zpci_list); 89057b5918cSSebastian Ott spin_unlock(&zpci_list_lock); 891cd248341SJan Glauber 89267f43f38SSebastian Ott zpci_init_slot(zdev); 89367f43f38SSebastian Ott 894cd248341SJan Glauber return 0; 895cd248341SJan Glauber 8961c21351bSSebastian Ott out_disable: 8971c21351bSSebastian Ott if (zdev->state == ZPCI_FN_STATE_ONLINE) 8981c21351bSSebastian Ott zpci_disable_device(zdev); 899f42c2235SJoerg Roedel out_destroy_iommu: 900f42c2235SJoerg Roedel zpci_destroy_iommu(zdev); 9011c21351bSSebastian Ott out_free: 902cd248341SJan Glauber zpci_free_domain(zdev); 903cd248341SJan Glauber out: 904cd248341SJan Glauber return rc; 905cd248341SJan Glauber } 906cd248341SJan Glauber 907623bd44dSSebastian Ott void zpci_remove_device(struct zpci_dev *zdev) 908623bd44dSSebastian Ott { 909623bd44dSSebastian Ott if (!zdev->bus) 910623bd44dSSebastian Ott return; 911623bd44dSSebastian Ott 912623bd44dSSebastian Ott pci_stop_root_bus(zdev->bus); 913623bd44dSSebastian Ott pci_remove_root_bus(zdev->bus); 914623bd44dSSebastian Ott } 915623bd44dSSebastian Ott 916bd3a1725SMartin Schwidefsky int zpci_report_error(struct pci_dev *pdev, 917bd3a1725SMartin Schwidefsky struct zpci_report_error_header *report) 918bd3a1725SMartin Schwidefsky { 919bd3a1725SMartin Schwidefsky struct zpci_dev *zdev = to_zpci(pdev); 920bd3a1725SMartin Schwidefsky 921bd3a1725SMartin Schwidefsky return sclp_pci_report(report, zdev->fh, zdev->fid); 922bd3a1725SMartin Schwidefsky } 923bd3a1725SMartin Schwidefsky EXPORT_SYMBOL(zpci_report_error); 924bd3a1725SMartin Schwidefsky 925cd248341SJan Glauber static int zpci_mem_init(void) 926cd248341SJan Glauber { 92780c544deSSebastian Ott BUILD_BUG_ON(!is_power_of_2(__alignof__(struct zpci_fmb)) || 92880c544deSSebastian Ott __alignof__(struct zpci_fmb) < sizeof(struct zpci_fmb)); 92980c544deSSebastian Ott 930d0b08853SJan Glauber zdev_fmb_cache = kmem_cache_create("PCI_FMB_cache", sizeof(struct zpci_fmb), 93180c544deSSebastian Ott __alignof__(struct zpci_fmb), 0, NULL); 932d0b08853SJan Glauber if (!zdev_fmb_cache) 933c506fff3SSebastian Ott goto error_fmb; 934d0b08853SJan Glauber 935c506fff3SSebastian Ott zpci_iomap_start = kcalloc(ZPCI_IOMAP_ENTRIES, 936c506fff3SSebastian Ott sizeof(*zpci_iomap_start), GFP_KERNEL); 937cd248341SJan Glauber if (!zpci_iomap_start) 9389a4da8a5SJan Glauber goto error_iomap; 939cd248341SJan Glauber 940c506fff3SSebastian Ott zpci_iomap_bitmap = kcalloc(BITS_TO_LONGS(ZPCI_IOMAP_ENTRIES), 941c506fff3SSebastian Ott sizeof(*zpci_iomap_bitmap), GFP_KERNEL); 942c506fff3SSebastian Ott if (!zpci_iomap_bitmap) 943c506fff3SSebastian Ott goto error_iomap_bitmap; 944c506fff3SSebastian Ott 945c506fff3SSebastian Ott return 0; 946c506fff3SSebastian Ott error_iomap_bitmap: 947c506fff3SSebastian Ott kfree(zpci_iomap_start); 9489a4da8a5SJan Glauber error_iomap: 949d0b08853SJan Glauber kmem_cache_destroy(zdev_fmb_cache); 950c506fff3SSebastian Ott error_fmb: 951cd248341SJan Glauber return -ENOMEM; 952cd248341SJan Glauber } 953cd248341SJan Glauber 954cd248341SJan Glauber static void zpci_mem_exit(void) 955cd248341SJan Glauber { 956c506fff3SSebastian Ott kfree(zpci_iomap_bitmap); 957cd248341SJan Glauber kfree(zpci_iomap_start); 958d0b08853SJan Glauber kmem_cache_destroy(zdev_fmb_cache); 959cd248341SJan Glauber } 960cd248341SJan Glauber 961*6324b4deSSebastian Ott static unsigned int s390_pci_probe __initdata = 1; 962aa3b7c29SSebastian Ott static unsigned int s390_pci_initialized; 963cd248341SJan Glauber 964cd248341SJan Glauber char * __init pcibios_setup(char *str) 965cd248341SJan Glauber { 966257608fbSSebastian Ott if (!strcmp(str, "off")) { 967257608fbSSebastian Ott s390_pci_probe = 0; 968cd248341SJan Glauber return NULL; 969cd248341SJan Glauber } 970cd248341SJan Glauber return str; 971cd248341SJan Glauber } 972cd248341SJan Glauber 973aa3b7c29SSebastian Ott bool zpci_is_enabled(void) 974aa3b7c29SSebastian Ott { 975aa3b7c29SSebastian Ott return s390_pci_initialized; 976aa3b7c29SSebastian Ott } 977aa3b7c29SSebastian Ott 978cd248341SJan Glauber static int __init pci_base_init(void) 979cd248341SJan Glauber { 980cd248341SJan Glauber int rc; 981cd248341SJan Glauber 9821e5635d1SHeiko Carstens if (!s390_pci_probe) 983cd248341SJan Glauber return 0; 984cd248341SJan Glauber 98548070c73SChristian Borntraeger if (!test_facility(69) || !test_facility(71)) 986cd248341SJan Glauber return 0; 987cd248341SJan Glauber 988d0b08853SJan Glauber rc = zpci_debug_init(); 989d0b08853SJan Glauber if (rc) 9901f44a225SMartin Schwidefsky goto out; 991d0b08853SJan Glauber 992cd248341SJan Glauber rc = zpci_mem_init(); 993cd248341SJan Glauber if (rc) 994cd248341SJan Glauber goto out_mem; 995cd248341SJan Glauber 9969a4da8a5SJan Glauber rc = zpci_irq_init(); 9979a4da8a5SJan Glauber if (rc) 9989a4da8a5SJan Glauber goto out_irq; 9999a4da8a5SJan Glauber 1000828b35f6SJan Glauber rc = zpci_dma_init(); 1001828b35f6SJan Glauber if (rc) 1002828b35f6SJan Glauber goto out_dma; 1003828b35f6SJan Glauber 10041d578966SSebastian Ott rc = clp_scan_pci_devices(); 1005a755a45dSJan Glauber if (rc) 1006a755a45dSJan Glauber goto out_find; 1007a755a45dSJan Glauber 1008aa3b7c29SSebastian Ott s390_pci_initialized = 1; 1009cd248341SJan Glauber return 0; 1010cd248341SJan Glauber 1011a755a45dSJan Glauber out_find: 1012828b35f6SJan Glauber zpci_dma_exit(); 1013828b35f6SJan Glauber out_dma: 10149a4da8a5SJan Glauber zpci_irq_exit(); 10159a4da8a5SJan Glauber out_irq: 1016cd248341SJan Glauber zpci_mem_exit(); 1017cd248341SJan Glauber out_mem: 1018d0b08853SJan Glauber zpci_debug_exit(); 10191f44a225SMartin Schwidefsky out: 1020cd248341SJan Glauber return rc; 1021cd248341SJan Glauber } 102267f43f38SSebastian Ott subsys_initcall_sync(pci_base_init); 102357b5918cSSebastian Ott 102457b5918cSSebastian Ott void zpci_rescan(void) 102557b5918cSSebastian Ott { 1026aa3b7c29SSebastian Ott if (zpci_is_enabled()) 102757b5918cSSebastian Ott clp_rescan_pci_devices_simple(); 102857b5918cSSebastian Ott } 1029