1cd248341SJan Glauber /* 2cd248341SJan Glauber * Copyright IBM Corp. 2012 3cd248341SJan Glauber * 4cd248341SJan Glauber * Author(s): 5cd248341SJan Glauber * Jan Glauber <jang@linux.vnet.ibm.com> 6cd248341SJan Glauber * 7cd248341SJan Glauber * The System z PCI code is a rewrite from a prototype by 8cd248341SJan Glauber * the following people (Kudoz!): 9bedef755SJan Glauber * Alexander Schmidt 10bedef755SJan Glauber * Christoph Raisch 11bedef755SJan Glauber * Hannes Hering 12bedef755SJan Glauber * Hoang-Nam Nguyen 13bedef755SJan Glauber * Jan-Bernd Themann 14bedef755SJan Glauber * Stefan Roscher 15bedef755SJan Glauber * Thomas Klein 16cd248341SJan Glauber */ 17cd248341SJan Glauber 18896cb7e6SGerald Schaefer #define KMSG_COMPONENT "zpci" 19896cb7e6SGerald Schaefer #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt 20cd248341SJan Glauber 21cd248341SJan Glauber #include <linux/kernel.h> 22cd248341SJan Glauber #include <linux/slab.h> 23cd248341SJan Glauber #include <linux/err.h> 24cd248341SJan Glauber #include <linux/export.h> 25cd248341SJan Glauber #include <linux/delay.h> 269a4da8a5SJan Glauber #include <linux/irq.h> 279a4da8a5SJan Glauber #include <linux/kernel_stat.h> 28cd248341SJan Glauber #include <linux/seq_file.h> 29cd248341SJan Glauber #include <linux/pci.h> 30cd248341SJan Glauber #include <linux/msi.h> 31cd248341SJan Glauber 329a4da8a5SJan Glauber #include <asm/isc.h> 339a4da8a5SJan Glauber #include <asm/airq.h> 34cd248341SJan Glauber #include <asm/facility.h> 35cd248341SJan Glauber #include <asm/pci_insn.h> 36a755a45dSJan Glauber #include <asm/pci_clp.h> 37828b35f6SJan Glauber #include <asm/pci_dma.h> 38cd248341SJan Glauber 39cd248341SJan Glauber #define DEBUG /* enable pr_debug */ 40cd248341SJan Glauber 419a4da8a5SJan Glauber #define SIC_IRQ_MODE_ALL 0 429a4da8a5SJan Glauber #define SIC_IRQ_MODE_SINGLE 1 439a4da8a5SJan Glauber 44cd248341SJan Glauber #define ZPCI_NR_DMA_SPACES 1 45cd248341SJan Glauber #define ZPCI_NR_DEVICES CONFIG_PCI_NR_FUNCTIONS 46cd248341SJan Glauber 47cd248341SJan Glauber /* list of all detected zpci devices */ 4867f43f38SSebastian Ott static LIST_HEAD(zpci_list); 4957b5918cSSebastian Ott static DEFINE_SPINLOCK(zpci_list_lock); 501f44a225SMartin Schwidefsky 511f44a225SMartin Schwidefsky static struct irq_chip zpci_irq_chip = { 521f44a225SMartin Schwidefsky .name = "zPCI", 53280510f1SThomas Gleixner .irq_unmask = pci_msi_unmask_irq, 54280510f1SThomas Gleixner .irq_mask = pci_msi_mask_irq, 551f44a225SMartin Schwidefsky }; 561f44a225SMartin Schwidefsky 57cd248341SJan Glauber static DECLARE_BITMAP(zpci_domain, ZPCI_NR_DEVICES); 58cd248341SJan Glauber static DEFINE_SPINLOCK(zpci_domain_lock); 59cd248341SJan Glauber 605d0d8f43SMartin Schwidefsky static struct airq_iv *zpci_aisb_iv; 611f44a225SMartin Schwidefsky static struct airq_iv *zpci_aibv[ZPCI_NR_DEVICES]; 629a4da8a5SJan Glauber 63c506fff3SSebastian Ott #define ZPCI_IOMAP_ENTRIES \ 64b0c8ce89SSebastian Ott min(((unsigned long) ZPCI_NR_DEVICES * PCI_BAR_COUNT / 2), \ 65c506fff3SSebastian Ott ZPCI_IOMAP_MAX_ENTRIES) 66c506fff3SSebastian Ott 67cd248341SJan Glauber static DEFINE_SPINLOCK(zpci_iomap_lock); 68c506fff3SSebastian Ott static unsigned long *zpci_iomap_bitmap; 69cd248341SJan Glauber struct zpci_iomap_entry *zpci_iomap_start; 70cd248341SJan Glauber EXPORT_SYMBOL_GPL(zpci_iomap_start); 71cd248341SJan Glauber 72d0b08853SJan Glauber static struct kmem_cache *zdev_fmb_cache; 73d0b08853SJan Glauber 74cd248341SJan Glauber struct zpci_dev *get_zdev_by_fid(u32 fid) 75cd248341SJan Glauber { 76cd248341SJan Glauber struct zpci_dev *tmp, *zdev = NULL; 77cd248341SJan Glauber 7857b5918cSSebastian Ott spin_lock(&zpci_list_lock); 79cd248341SJan Glauber list_for_each_entry(tmp, &zpci_list, entry) { 80cd248341SJan Glauber if (tmp->fid == fid) { 81cd248341SJan Glauber zdev = tmp; 82cd248341SJan Glauber break; 83cd248341SJan Glauber } 84cd248341SJan Glauber } 8557b5918cSSebastian Ott spin_unlock(&zpci_list_lock); 86cd248341SJan Glauber return zdev; 87cd248341SJan Glauber } 88cd248341SJan Glauber 89cd248341SJan Glauber static struct zpci_dev *get_zdev_by_bus(struct pci_bus *bus) 90cd248341SJan Glauber { 91cd248341SJan Glauber return (bus && bus->sysdata) ? (struct zpci_dev *) bus->sysdata : NULL; 92cd248341SJan Glauber } 93cd248341SJan Glauber 94cd248341SJan Glauber int pci_domain_nr(struct pci_bus *bus) 95cd248341SJan Glauber { 96cd248341SJan Glauber return ((struct zpci_dev *) bus->sysdata)->domain; 97cd248341SJan Glauber } 98cd248341SJan Glauber EXPORT_SYMBOL_GPL(pci_domain_nr); 99cd248341SJan Glauber 100cd248341SJan Glauber int pci_proc_domain(struct pci_bus *bus) 101cd248341SJan Glauber { 102cd248341SJan Glauber return pci_domain_nr(bus); 103cd248341SJan Glauber } 104cd248341SJan Glauber EXPORT_SYMBOL_GPL(pci_proc_domain); 105cd248341SJan Glauber 1069a4da8a5SJan Glauber /* Modify PCI: Register adapter interruptions */ 1075d0d8f43SMartin Schwidefsky static int zpci_set_airq(struct zpci_dev *zdev) 1089a4da8a5SJan Glauber { 1099a4da8a5SJan Glauber u64 req = ZPCI_CREATE_REQ(zdev->fh, 0, ZPCI_MOD_FC_REG_INT); 110cb4deb69SSebastian Ott struct zpci_fib fib = {0}; 1114dfbd3efSSebastian Ott u8 status; 1129a4da8a5SJan Glauber 113cb4deb69SSebastian Ott fib.isc = PCI_ISC; 114cb4deb69SSebastian Ott fib.sum = 1; /* enable summary notifications */ 115cb4deb69SSebastian Ott fib.noi = airq_iv_end(zdev->aibv); 116cb4deb69SSebastian Ott fib.aibv = (unsigned long) zdev->aibv->vector; 117cb4deb69SSebastian Ott fib.aibvo = 0; /* each zdev has its own interrupt vector */ 118cb4deb69SSebastian Ott fib.aisb = (unsigned long) zpci_aisb_iv->vector + (zdev->aisb/64)*8; 119cb4deb69SSebastian Ott fib.aisbo = zdev->aisb & 63; 1209a4da8a5SJan Glauber 1214dfbd3efSSebastian Ott return zpci_mod_fc(req, &fib, &status) ? -EIO : 0; 1224dfbd3efSSebastian Ott } 1234dfbd3efSSebastian Ott 1244dfbd3efSSebastian Ott /* Modify PCI: Unregister adapter interruptions */ 1254dfbd3efSSebastian Ott static int zpci_clear_airq(struct zpci_dev *zdev) 1264dfbd3efSSebastian Ott { 1274dfbd3efSSebastian Ott u64 req = ZPCI_CREATE_REQ(zdev->fh, 0, ZPCI_MOD_FC_DEREG_INT); 1284dfbd3efSSebastian Ott struct zpci_fib fib = {0}; 1294dfbd3efSSebastian Ott u8 cc, status; 1304dfbd3efSSebastian Ott 1314dfbd3efSSebastian Ott cc = zpci_mod_fc(req, &fib, &status); 1324dfbd3efSSebastian Ott if (cc == 3 || (cc == 1 && status == 24)) 1334dfbd3efSSebastian Ott /* Function already gone or IRQs already deregistered. */ 1344dfbd3efSSebastian Ott cc = 0; 1354dfbd3efSSebastian Ott 1364dfbd3efSSebastian Ott return cc ? -EIO : 0; 1379a4da8a5SJan Glauber } 1389a4da8a5SJan Glauber 139828b35f6SJan Glauber /* Modify PCI: Register I/O address translation parameters */ 140828b35f6SJan Glauber int zpci_register_ioat(struct zpci_dev *zdev, u8 dmaas, 141828b35f6SJan Glauber u64 base, u64 limit, u64 iota) 142828b35f6SJan Glauber { 14372570834SSebastian Ott u64 req = ZPCI_CREATE_REQ(zdev->fh, dmaas, ZPCI_MOD_FC_REG_IOAT); 14472570834SSebastian Ott struct zpci_fib fib = {0}; 14572570834SSebastian Ott u8 status; 146828b35f6SJan Glauber 147828b35f6SJan Glauber WARN_ON_ONCE(iota & 0x3fff); 14872570834SSebastian Ott fib.pba = base; 14972570834SSebastian Ott fib.pal = limit; 15072570834SSebastian Ott fib.iota = iota | ZPCI_IOTA_RTTO_FLAG; 15172570834SSebastian Ott return zpci_mod_fc(req, &fib, &status) ? -EIO : 0; 152828b35f6SJan Glauber } 153828b35f6SJan Glauber 154828b35f6SJan Glauber /* Modify PCI: Unregister I/O address translation parameters */ 155828b35f6SJan Glauber int zpci_unregister_ioat(struct zpci_dev *zdev, u8 dmaas) 156828b35f6SJan Glauber { 15772570834SSebastian Ott u64 req = ZPCI_CREATE_REQ(zdev->fh, dmaas, ZPCI_MOD_FC_DEREG_IOAT); 15872570834SSebastian Ott struct zpci_fib fib = {0}; 15972570834SSebastian Ott u8 cc, status; 160828b35f6SJan Glauber 16172570834SSebastian Ott cc = zpci_mod_fc(req, &fib, &status); 16272570834SSebastian Ott if (cc == 3) /* Function already gone. */ 16372570834SSebastian Ott cc = 0; 16472570834SSebastian Ott return cc ? -EIO : 0; 165828b35f6SJan Glauber } 166828b35f6SJan Glauber 167d0b08853SJan Glauber /* Modify PCI: Set PCI function measurement parameters */ 168d0b08853SJan Glauber int zpci_fmb_enable_device(struct zpci_dev *zdev) 169d0b08853SJan Glauber { 1704e5bd780SSebastian Ott u64 req = ZPCI_CREATE_REQ(zdev->fh, 0, ZPCI_MOD_FC_SET_MEASURE); 1714e5bd780SSebastian Ott struct zpci_fib fib = {0}; 1724e5bd780SSebastian Ott u8 cc, status; 173d0b08853SJan Glauber 1740b7589ecSSebastian Ott if (zdev->fmb || sizeof(*zdev->fmb) < zdev->fmb_length) 175d0b08853SJan Glauber return -EINVAL; 176d0b08853SJan Glauber 17708b42124SWei Yongjun zdev->fmb = kmem_cache_zalloc(zdev_fmb_cache, GFP_KERNEL); 178d0b08853SJan Glauber if (!zdev->fmb) 179d0b08853SJan Glauber return -ENOMEM; 180d0b08853SJan Glauber WARN_ON((u64) zdev->fmb & 0xf); 181d0b08853SJan Glauber 1826001018aSSebastian Ott /* reset software counters */ 1836001018aSSebastian Ott atomic64_set(&zdev->allocated_pages, 0); 1846001018aSSebastian Ott atomic64_set(&zdev->mapped_pages, 0); 1856001018aSSebastian Ott atomic64_set(&zdev->unmapped_pages, 0); 1866001018aSSebastian Ott 1874e5bd780SSebastian Ott fib.fmb_addr = virt_to_phys(zdev->fmb); 1884e5bd780SSebastian Ott cc = zpci_mod_fc(req, &fib, &status); 1894e5bd780SSebastian Ott if (cc) { 1904e5bd780SSebastian Ott kmem_cache_free(zdev_fmb_cache, zdev->fmb); 1914e5bd780SSebastian Ott zdev->fmb = NULL; 1924e5bd780SSebastian Ott } 1934e5bd780SSebastian Ott return cc ? -EIO : 0; 194d0b08853SJan Glauber } 195d0b08853SJan Glauber 196d0b08853SJan Glauber /* Modify PCI: Disable PCI function measurement */ 197d0b08853SJan Glauber int zpci_fmb_disable_device(struct zpci_dev *zdev) 198d0b08853SJan Glauber { 1994e5bd780SSebastian Ott u64 req = ZPCI_CREATE_REQ(zdev->fh, 0, ZPCI_MOD_FC_SET_MEASURE); 2004e5bd780SSebastian Ott struct zpci_fib fib = {0}; 2014e5bd780SSebastian Ott u8 cc, status; 202d0b08853SJan Glauber 203d0b08853SJan Glauber if (!zdev->fmb) 204d0b08853SJan Glauber return -EINVAL; 205d0b08853SJan Glauber 206d0b08853SJan Glauber /* Function measurement is disabled if fmb address is zero */ 2074e5bd780SSebastian Ott cc = zpci_mod_fc(req, &fib, &status); 2084e5bd780SSebastian Ott if (cc == 3) /* Function already gone. */ 2094e5bd780SSebastian Ott cc = 0; 210d0b08853SJan Glauber 2114e5bd780SSebastian Ott if (!cc) { 212d0b08853SJan Glauber kmem_cache_free(zdev_fmb_cache, zdev->fmb); 213d0b08853SJan Glauber zdev->fmb = NULL; 2144e5bd780SSebastian Ott } 2154e5bd780SSebastian Ott return cc ? -EIO : 0; 216d0b08853SJan Glauber } 217d0b08853SJan Glauber 218cd248341SJan Glauber static int zpci_cfg_load(struct zpci_dev *zdev, int offset, u32 *val, u8 len) 219cd248341SJan Glauber { 220cd248341SJan Glauber u64 req = ZPCI_CREATE_REQ(zdev->fh, ZPCI_PCIAS_CFGSPC, len); 221cd248341SJan Glauber u64 data; 222cd248341SJan Glauber int rc; 223cd248341SJan Glauber 2249389339fSMartin Schwidefsky rc = zpci_load(&data, req, offset); 225b170bad4SSebastian Ott if (!rc) { 2265064cd35SSebastian Ott data = le64_to_cpu((__force __le64) data); 2275064cd35SSebastian Ott data >>= (8 - len) * 8; 228cd248341SJan Glauber *val = (u32) data; 229b170bad4SSebastian Ott } else 230cd248341SJan Glauber *val = 0xffffffff; 231cd248341SJan Glauber return rc; 232cd248341SJan Glauber } 233cd248341SJan Glauber 234cd248341SJan Glauber static int zpci_cfg_store(struct zpci_dev *zdev, int offset, u32 val, u8 len) 235cd248341SJan Glauber { 236cd248341SJan Glauber u64 req = ZPCI_CREATE_REQ(zdev->fh, ZPCI_PCIAS_CFGSPC, len); 237cd248341SJan Glauber u64 data = val; 238cd248341SJan Glauber int rc; 239cd248341SJan Glauber 2405064cd35SSebastian Ott data <<= (8 - len) * 8; 2415064cd35SSebastian Ott data = (__force u64) cpu_to_le64(data); 2429389339fSMartin Schwidefsky rc = zpci_store(data, req, offset); 243cd248341SJan Glauber return rc; 244cd248341SJan Glauber } 245cd248341SJan Glauber 246b881bc46SGreg Kroah-Hartman void pcibios_fixup_bus(struct pci_bus *bus) 247cd248341SJan Glauber { 248cd248341SJan Glauber } 249cd248341SJan Glauber 250cd248341SJan Glauber resource_size_t pcibios_align_resource(void *data, const struct resource *res, 251cd248341SJan Glauber resource_size_t size, 252cd248341SJan Glauber resource_size_t align) 253cd248341SJan Glauber { 254cd248341SJan Glauber return 0; 255cd248341SJan Glauber } 256cd248341SJan Glauber 25787bc359bSJan Glauber /* combine single writes by using store-block insn */ 25887bc359bSJan Glauber void __iowrite64_copy(void __iomem *to, const void *from, size_t count) 25987bc359bSJan Glauber { 26087bc359bSJan Glauber zpci_memcpy_toio(to, from, count); 26187bc359bSJan Glauber } 26287bc359bSJan Glauber 263cd248341SJan Glauber /* Create a virtual mapping cookie for a PCI BAR */ 2648cfc99b5SMichael S. Tsirkin void __iomem *pci_iomap_range(struct pci_dev *pdev, 2658cfc99b5SMichael S. Tsirkin int bar, 2668cfc99b5SMichael S. Tsirkin unsigned long offset, 2678cfc99b5SMichael S. Tsirkin unsigned long max) 268cd248341SJan Glauber { 269198a5278SSebastian Ott struct zpci_dev *zdev = to_zpci(pdev); 270cd248341SJan Glauber int idx; 271cd248341SJan Glauber 272c0cabaddSSebastian Ott if (!pci_resource_len(pdev, bar)) 273cd248341SJan Glauber return NULL; 274cd248341SJan Glauber 275cd248341SJan Glauber idx = zdev->bars[bar].map_idx; 276cd248341SJan Glauber spin_lock(&zpci_iomap_lock); 277f5e44f82SSebastian Ott /* Detect overrun */ 278f5e44f82SSebastian Ott WARN_ON(!++zpci_iomap_start[idx].count); 279cd248341SJan Glauber zpci_iomap_start[idx].fh = zdev->fh; 280cd248341SJan Glauber zpci_iomap_start[idx].bar = bar; 281cd248341SJan Glauber spin_unlock(&zpci_iomap_lock); 282cd248341SJan Glauber 2839e00caaeSSebastian Ott return (void __iomem *) ZPCI_ADDR(idx) + offset; 284cd248341SJan Glauber } 285d9426083SSebastian Ott EXPORT_SYMBOL(pci_iomap_range); 2868cfc99b5SMichael S. Tsirkin 2878cfc99b5SMichael S. Tsirkin void __iomem *pci_iomap(struct pci_dev *dev, int bar, unsigned long maxlen) 2888cfc99b5SMichael S. Tsirkin { 2898cfc99b5SMichael S. Tsirkin return pci_iomap_range(dev, bar, 0, maxlen); 2908cfc99b5SMichael S. Tsirkin } 2918cfc99b5SMichael S. Tsirkin EXPORT_SYMBOL(pci_iomap); 292cd248341SJan Glauber 293cd248341SJan Glauber void pci_iounmap(struct pci_dev *pdev, void __iomem *addr) 294cd248341SJan Glauber { 2959e00caaeSSebastian Ott unsigned int idx = ZPCI_IDX(addr); 296cd248341SJan Glauber 297cd248341SJan Glauber spin_lock(&zpci_iomap_lock); 2988cfc99b5SMichael S. Tsirkin /* Detect underrun */ 299f5e44f82SSebastian Ott WARN_ON(!zpci_iomap_start[idx].count); 3008cfc99b5SMichael S. Tsirkin if (!--zpci_iomap_start[idx].count) { 301cd248341SJan Glauber zpci_iomap_start[idx].fh = 0; 302cd248341SJan Glauber zpci_iomap_start[idx].bar = 0; 3038cfc99b5SMichael S. Tsirkin } 304cd248341SJan Glauber spin_unlock(&zpci_iomap_lock); 305cd248341SJan Glauber } 306d9426083SSebastian Ott EXPORT_SYMBOL(pci_iounmap); 307cd248341SJan Glauber 308cd248341SJan Glauber static int pci_read(struct pci_bus *bus, unsigned int devfn, int where, 309cd248341SJan Glauber int size, u32 *val) 310cd248341SJan Glauber { 311cd248341SJan Glauber struct zpci_dev *zdev = get_zdev_by_bus(bus); 3122c3700bbSSebastian Ott int ret; 313cd248341SJan Glauber 314cd248341SJan Glauber if (!zdev || devfn != ZPCI_DEVFN) 3152c3700bbSSebastian Ott ret = -ENODEV; 3162c3700bbSSebastian Ott else 3172c3700bbSSebastian Ott ret = zpci_cfg_load(zdev, where, val, size); 3182c3700bbSSebastian Ott 3192c3700bbSSebastian Ott return ret; 320cd248341SJan Glauber } 321cd248341SJan Glauber 322cd248341SJan Glauber static int pci_write(struct pci_bus *bus, unsigned int devfn, int where, 323cd248341SJan Glauber int size, u32 val) 324cd248341SJan Glauber { 325cd248341SJan Glauber struct zpci_dev *zdev = get_zdev_by_bus(bus); 3262c3700bbSSebastian Ott int ret; 327cd248341SJan Glauber 328cd248341SJan Glauber if (!zdev || devfn != ZPCI_DEVFN) 3292c3700bbSSebastian Ott ret = -ENODEV; 3302c3700bbSSebastian Ott else 3312c3700bbSSebastian Ott ret = zpci_cfg_store(zdev, where, val, size); 3322c3700bbSSebastian Ott 3332c3700bbSSebastian Ott return ret; 334cd248341SJan Glauber } 335cd248341SJan Glauber 336cd248341SJan Glauber static struct pci_ops pci_root_ops = { 337cd248341SJan Glauber .read = pci_read, 338cd248341SJan Glauber .write = pci_write, 339cd248341SJan Glauber }; 340cd248341SJan Glauber 341f4eae94fSMartin Schwidefsky static void zpci_irq_handler(struct airq_struct *airq) 3429a4da8a5SJan Glauber { 3435d0d8f43SMartin Schwidefsky unsigned long si, ai; 3441f44a225SMartin Schwidefsky struct airq_iv *aibv; 3455d0d8f43SMartin Schwidefsky int irqs_on = 0; 3469a4da8a5SJan Glauber 347420f42ecSHeiko Carstens inc_irq_stat(IRQIO_PCI); 3485d0d8f43SMartin Schwidefsky for (si = 0;;) { 3495d0d8f43SMartin Schwidefsky /* Scan adapter summary indicator bit vector */ 3505d0d8f43SMartin Schwidefsky si = airq_iv_scan(zpci_aisb_iv, si, airq_iv_end(zpci_aisb_iv)); 3515d0d8f43SMartin Schwidefsky if (si == -1UL) { 3525d0d8f43SMartin Schwidefsky if (irqs_on++) 3535d0d8f43SMartin Schwidefsky /* End of second scan with interrupts on. */ 3545d0d8f43SMartin Schwidefsky break; 3555d0d8f43SMartin Schwidefsky /* First scan complete, reenable interrupts. */ 3569389339fSMartin Schwidefsky zpci_set_irq_ctrl(SIC_IRQ_MODE_SINGLE, NULL, PCI_ISC); 3575d0d8f43SMartin Schwidefsky si = 0; 3585d0d8f43SMartin Schwidefsky continue; 3599a4da8a5SJan Glauber } 3605d0d8f43SMartin Schwidefsky 3615d0d8f43SMartin Schwidefsky /* Scan the adapter interrupt vector for this device. */ 3621f44a225SMartin Schwidefsky aibv = zpci_aibv[si]; 3635d0d8f43SMartin Schwidefsky for (ai = 0;;) { 3641f44a225SMartin Schwidefsky ai = airq_iv_scan(aibv, ai, airq_iv_end(aibv)); 3655d0d8f43SMartin Schwidefsky if (ai == -1UL) 3665d0d8f43SMartin Schwidefsky break; 3675d0d8f43SMartin Schwidefsky inc_irq_stat(IRQIO_MSI); 3681f44a225SMartin Schwidefsky airq_iv_lock(aibv, ai); 3691f44a225SMartin Schwidefsky generic_handle_irq(airq_iv_get_data(aibv, ai)); 3701f44a225SMartin Schwidefsky airq_iv_unlock(aibv, ai); 3715d0d8f43SMartin Schwidefsky } 3725d0d8f43SMartin Schwidefsky } 3735d0d8f43SMartin Schwidefsky } 3745d0d8f43SMartin Schwidefsky 3755d0d8f43SMartin Schwidefsky int arch_setup_msi_irqs(struct pci_dev *pdev, int nvec, int type) 3769a4da8a5SJan Glauber { 377198a5278SSebastian Ott struct zpci_dev *zdev = to_zpci(pdev); 3780a0a9421SThomas Gleixner unsigned int hwirq, msi_vecs; 3795d0d8f43SMartin Schwidefsky unsigned long aisb; 3809a4da8a5SJan Glauber struct msi_desc *msi; 3811f44a225SMartin Schwidefsky struct msi_msg msg; 3820a0a9421SThomas Gleixner int rc, irq; 3839a4da8a5SJan Glauber 384795818e8SSebastian Ott zdev->aisb = -1UL; 385a384c892SAlexander Gordeev if (type == PCI_CAP_ID_MSI && nvec > 1) 386a384c892SAlexander Gordeev return 1; 387b19148f6SSebastian Ott msi_vecs = min_t(unsigned int, nvec, zdev->max_msi); 3889a4da8a5SJan Glauber 3895d0d8f43SMartin Schwidefsky /* Allocate adapter summary indicator bit */ 3905d0d8f43SMartin Schwidefsky aisb = airq_iv_alloc_bit(zpci_aisb_iv); 3915d0d8f43SMartin Schwidefsky if (aisb == -1UL) 392795818e8SSebastian Ott return -EIO; 3939a4da8a5SJan Glauber zdev->aisb = aisb; 3949a4da8a5SJan Glauber 3955d0d8f43SMartin Schwidefsky /* Create adapter interrupt vector */ 3961f44a225SMartin Schwidefsky zdev->aibv = airq_iv_create(msi_vecs, AIRQ_IV_DATA | AIRQ_IV_BITLOCK); 3975d0d8f43SMartin Schwidefsky if (!zdev->aibv) 398795818e8SSebastian Ott return -ENOMEM; 3999a4da8a5SJan Glauber 4005d0d8f43SMartin Schwidefsky /* Wire up shortcut pointer */ 4011f44a225SMartin Schwidefsky zpci_aibv[aisb] = zdev->aibv; 4025d0d8f43SMartin Schwidefsky 4031f44a225SMartin Schwidefsky /* Request MSI interrupts */ 4041f44a225SMartin Schwidefsky hwirq = 0; 405df516f42SJiang Liu for_each_pci_msi_entry(msi, pdev) { 4061f44a225SMartin Schwidefsky rc = -EIO; 4071f44a225SMartin Schwidefsky irq = irq_alloc_desc(0); /* Alloc irq on node 0 */ 4080a0a9421SThomas Gleixner if (irq < 0) 409795818e8SSebastian Ott return -ENOMEM; 4101f44a225SMartin Schwidefsky rc = irq_set_msi_desc(irq, msi); 4119a4da8a5SJan Glauber if (rc) 412795818e8SSebastian Ott return rc; 4131f44a225SMartin Schwidefsky irq_set_chip_and_handler(irq, &zpci_irq_chip, 4141f44a225SMartin Schwidefsky handle_simple_irq); 4151f44a225SMartin Schwidefsky msg.data = hwirq; 4161f44a225SMartin Schwidefsky msg.address_lo = zdev->msi_addr & 0xffffffff; 4171f44a225SMartin Schwidefsky msg.address_hi = zdev->msi_addr >> 32; 41883a18912SJiang Liu pci_write_msi_msg(irq, &msg); 4191f44a225SMartin Schwidefsky airq_iv_set_data(zdev->aibv, hwirq, irq); 4201f44a225SMartin Schwidefsky hwirq++; 4219a4da8a5SJan Glauber } 4229a4da8a5SJan Glauber 4235d0d8f43SMartin Schwidefsky /* Enable adapter interrupts */ 4245d0d8f43SMartin Schwidefsky rc = zpci_set_airq(zdev); 4255d0d8f43SMartin Schwidefsky if (rc) 426795818e8SSebastian Ott return rc; 4275d0d8f43SMartin Schwidefsky 4285d0d8f43SMartin Schwidefsky return (msi_vecs == nvec) ? 0 : msi_vecs; 4299a4da8a5SJan Glauber } 4309a4da8a5SJan Glauber 4315d0d8f43SMartin Schwidefsky void arch_teardown_msi_irqs(struct pci_dev *pdev) 4329a4da8a5SJan Glauber { 433198a5278SSebastian Ott struct zpci_dev *zdev = to_zpci(pdev); 4349a4da8a5SJan Glauber struct msi_desc *msi; 4355d0d8f43SMartin Schwidefsky int rc; 4369a4da8a5SJan Glauber 4375d0d8f43SMartin Schwidefsky /* Disable adapter interrupts */ 4385d0d8f43SMartin Schwidefsky rc = zpci_clear_airq(zdev); 4391f1dcbd4SSebastian Ott if (rc) 4409a4da8a5SJan Glauber return; 4419a4da8a5SJan Glauber 4421f44a225SMartin Schwidefsky /* Release MSI interrupts */ 443df516f42SJiang Liu for_each_pci_msi_entry(msi, pdev) { 444795818e8SSebastian Ott if (!msi->irq) 445795818e8SSebastian Ott continue; 4468fb878c5SYijing Wang if (msi->msi_attrib.is_msix) 44723ed8d57SThomas Gleixner __pci_msix_desc_mask_irq(msi, 1); 4488fb878c5SYijing Wang else 44923ed8d57SThomas Gleixner __pci_msi_desc_mask_irq(msi, 1, 1); 4501f44a225SMartin Schwidefsky irq_set_msi_desc(msi->irq, NULL); 4511f44a225SMartin Schwidefsky irq_free_desc(msi->irq); 4521f44a225SMartin Schwidefsky msi->msg.address_lo = 0; 4531f44a225SMartin Schwidefsky msi->msg.address_hi = 0; 4541f44a225SMartin Schwidefsky msi->msg.data = 0; 4551f44a225SMartin Schwidefsky msi->irq = 0; 4561f44a225SMartin Schwidefsky } 4579a4da8a5SJan Glauber 458795818e8SSebastian Ott if (zdev->aisb != -1UL) { 4591f44a225SMartin Schwidefsky zpci_aibv[zdev->aisb] = NULL; 4605d0d8f43SMartin Schwidefsky airq_iv_free_bit(zpci_aisb_iv, zdev->aisb); 461795818e8SSebastian Ott zdev->aisb = -1UL; 462795818e8SSebastian Ott } 463795818e8SSebastian Ott if (zdev->aibv) { 464795818e8SSebastian Ott airq_iv_release(zdev->aibv); 465795818e8SSebastian Ott zdev->aibv = NULL; 466795818e8SSebastian Ott } 4679a4da8a5SJan Glauber } 4689a4da8a5SJan Glauber 4691803ba2dSSebastian Ott static void zpci_map_resources(struct pci_dev *pdev) 470cd248341SJan Glauber { 471cd248341SJan Glauber resource_size_t len; 472cd248341SJan Glauber int i; 473cd248341SJan Glauber 474cd248341SJan Glauber for (i = 0; i < PCI_BAR_COUNT; i++) { 475cd248341SJan Glauber len = pci_resource_len(pdev, i); 476cd248341SJan Glauber if (!len) 477cd248341SJan Glauber continue; 4785b9f2081SMartin Schwidefsky pdev->resource[i].start = 4795b9f2081SMartin Schwidefsky (resource_size_t __force) pci_iomap(pdev, i, 0); 480cd248341SJan Glauber pdev->resource[i].end = pdev->resource[i].start + len - 1; 481cd248341SJan Glauber } 482944239c5SSebastian Ott } 483944239c5SSebastian Ott 4841803ba2dSSebastian Ott static void zpci_unmap_resources(struct pci_dev *pdev) 485944239c5SSebastian Ott { 486944239c5SSebastian Ott resource_size_t len; 487944239c5SSebastian Ott int i; 488944239c5SSebastian Ott 489944239c5SSebastian Ott for (i = 0; i < PCI_BAR_COUNT; i++) { 490944239c5SSebastian Ott len = pci_resource_len(pdev, i); 491944239c5SSebastian Ott if (!len) 492944239c5SSebastian Ott continue; 4935b9f2081SMartin Schwidefsky pci_iounmap(pdev, (void __iomem __force *) 4945b9f2081SMartin Schwidefsky pdev->resource[i].start); 495944239c5SSebastian Ott } 496944239c5SSebastian Ott } 497cd248341SJan Glauber 498561ecb0cSSebastian Ott static struct airq_struct zpci_airq = { 499561ecb0cSSebastian Ott .handler = zpci_irq_handler, 500561ecb0cSSebastian Ott .isc = PCI_ISC, 501561ecb0cSSebastian Ott }; 502561ecb0cSSebastian Ott 5039a4da8a5SJan Glauber static int __init zpci_irq_init(void) 5049a4da8a5SJan Glauber { 5055d0d8f43SMartin Schwidefsky int rc; 5069a4da8a5SJan Glauber 507f4eae94fSMartin Schwidefsky rc = register_adapter_interrupt(&zpci_airq); 508f4eae94fSMartin Schwidefsky if (rc) 5095d0d8f43SMartin Schwidefsky goto out; 510f4eae94fSMartin Schwidefsky /* Set summary to 1 to be called every time for the ISC. */ 511f4eae94fSMartin Schwidefsky *zpci_airq.lsi_ptr = 1; 5129a4da8a5SJan Glauber 5135d0d8f43SMartin Schwidefsky rc = -ENOMEM; 5145d0d8f43SMartin Schwidefsky zpci_aisb_iv = airq_iv_create(ZPCI_NR_DEVICES, AIRQ_IV_ALLOC); 5155d0d8f43SMartin Schwidefsky if (!zpci_aisb_iv) 5165d0d8f43SMartin Schwidefsky goto out_airq; 5179a4da8a5SJan Glauber 5189389339fSMartin Schwidefsky zpci_set_irq_ctrl(SIC_IRQ_MODE_SINGLE, NULL, PCI_ISC); 5199a4da8a5SJan Glauber return 0; 5209a4da8a5SJan Glauber 5215d0d8f43SMartin Schwidefsky out_airq: 5225d0d8f43SMartin Schwidefsky unregister_adapter_interrupt(&zpci_airq); 5235d0d8f43SMartin Schwidefsky out: 5249a4da8a5SJan Glauber return rc; 5259a4da8a5SJan Glauber } 5269a4da8a5SJan Glauber 5279a4da8a5SJan Glauber static void zpci_irq_exit(void) 5289a4da8a5SJan Glauber { 5295d0d8f43SMartin Schwidefsky airq_iv_release(zpci_aisb_iv); 530f4eae94fSMartin Schwidefsky unregister_adapter_interrupt(&zpci_airq); 5319a4da8a5SJan Glauber } 5329a4da8a5SJan Glauber 533cd248341SJan Glauber static int zpci_alloc_iomap(struct zpci_dev *zdev) 534cd248341SJan Glauber { 535bf19c94dSSebastian Ott unsigned long entry; 536cd248341SJan Glauber 537cd248341SJan Glauber spin_lock(&zpci_iomap_lock); 538c506fff3SSebastian Ott entry = find_first_zero_bit(zpci_iomap_bitmap, ZPCI_IOMAP_ENTRIES); 539c506fff3SSebastian Ott if (entry == ZPCI_IOMAP_ENTRIES) { 540cd248341SJan Glauber spin_unlock(&zpci_iomap_lock); 541cd248341SJan Glauber return -ENOSPC; 542cd248341SJan Glauber } 543c506fff3SSebastian Ott set_bit(entry, zpci_iomap_bitmap); 544cd248341SJan Glauber spin_unlock(&zpci_iomap_lock); 545cd248341SJan Glauber return entry; 546cd248341SJan Glauber } 547cd248341SJan Glauber 548cd248341SJan Glauber static void zpci_free_iomap(struct zpci_dev *zdev, int entry) 549cd248341SJan Glauber { 550cd248341SJan Glauber spin_lock(&zpci_iomap_lock); 551cd248341SJan Glauber memset(&zpci_iomap_start[entry], 0, sizeof(struct zpci_iomap_entry)); 552c506fff3SSebastian Ott clear_bit(entry, zpci_iomap_bitmap); 553cd248341SJan Glauber spin_unlock(&zpci_iomap_lock); 554cd248341SJan Glauber } 555cd248341SJan Glauber 5567a572a3aSSebastian Ott static struct resource *__alloc_res(struct zpci_dev *zdev, unsigned long start, 5577a572a3aSSebastian Ott unsigned long size, unsigned long flags) 5587a572a3aSSebastian Ott { 5597a572a3aSSebastian Ott struct resource *r; 5607a572a3aSSebastian Ott 5617a572a3aSSebastian Ott r = kzalloc(sizeof(*r), GFP_KERNEL); 5627a572a3aSSebastian Ott if (!r) 5637a572a3aSSebastian Ott return NULL; 5647a572a3aSSebastian Ott 5657a572a3aSSebastian Ott r->start = start; 5667a572a3aSSebastian Ott r->end = r->start + size - 1; 5677a572a3aSSebastian Ott r->flags = flags; 5687a572a3aSSebastian Ott r->name = zdev->res_name; 5697a572a3aSSebastian Ott 5707a572a3aSSebastian Ott if (request_resource(&iomem_resource, r)) { 5717a572a3aSSebastian Ott kfree(r); 5727a572a3aSSebastian Ott return NULL; 5737a572a3aSSebastian Ott } 5747a572a3aSSebastian Ott return r; 5757a572a3aSSebastian Ott } 5767a572a3aSSebastian Ott 5777a572a3aSSebastian Ott static int zpci_setup_bus_resources(struct zpci_dev *zdev, 5787a572a3aSSebastian Ott struct list_head *resources) 5797a572a3aSSebastian Ott { 5807a572a3aSSebastian Ott unsigned long addr, size, flags; 5817a572a3aSSebastian Ott struct resource *res; 5827a572a3aSSebastian Ott int i, entry; 5837a572a3aSSebastian Ott 5847a572a3aSSebastian Ott snprintf(zdev->res_name, sizeof(zdev->res_name), 5857a572a3aSSebastian Ott "PCI Bus %04x:%02x", zdev->domain, ZPCI_BUS_NR); 5867a572a3aSSebastian Ott 5877a572a3aSSebastian Ott for (i = 0; i < PCI_BAR_COUNT; i++) { 5887a572a3aSSebastian Ott if (!zdev->bars[i].size) 5897a572a3aSSebastian Ott continue; 5907a572a3aSSebastian Ott entry = zpci_alloc_iomap(zdev); 5917a572a3aSSebastian Ott if (entry < 0) 5927a572a3aSSebastian Ott return entry; 5937a572a3aSSebastian Ott zdev->bars[i].map_idx = entry; 5947a572a3aSSebastian Ott 5957a572a3aSSebastian Ott /* only MMIO is supported */ 5967a572a3aSSebastian Ott flags = IORESOURCE_MEM; 5977a572a3aSSebastian Ott if (zdev->bars[i].val & 8) 5987a572a3aSSebastian Ott flags |= IORESOURCE_PREFETCH; 5997a572a3aSSebastian Ott if (zdev->bars[i].val & 4) 6007a572a3aSSebastian Ott flags |= IORESOURCE_MEM_64; 6017a572a3aSSebastian Ott 6029e00caaeSSebastian Ott addr = ZPCI_ADDR(entry); 6037a572a3aSSebastian Ott size = 1UL << zdev->bars[i].size; 6047a572a3aSSebastian Ott 6057a572a3aSSebastian Ott res = __alloc_res(zdev, addr, size, flags); 6067a572a3aSSebastian Ott if (!res) { 6077a572a3aSSebastian Ott zpci_free_iomap(zdev, entry); 6087a572a3aSSebastian Ott return -ENOMEM; 6097a572a3aSSebastian Ott } 6107a572a3aSSebastian Ott zdev->bars[i].res = res; 6117a572a3aSSebastian Ott pci_add_resource(resources, res); 6127a572a3aSSebastian Ott } 6137a572a3aSSebastian Ott 6147a572a3aSSebastian Ott return 0; 6157a572a3aSSebastian Ott } 6167a572a3aSSebastian Ott 6177a572a3aSSebastian Ott static void zpci_cleanup_bus_resources(struct zpci_dev *zdev) 6187a572a3aSSebastian Ott { 6197a572a3aSSebastian Ott int i; 6207a572a3aSSebastian Ott 6217a572a3aSSebastian Ott for (i = 0; i < PCI_BAR_COUNT; i++) { 6222b1df724SSebastian Ott if (!zdev->bars[i].size || !zdev->bars[i].res) 6237a572a3aSSebastian Ott continue; 6247a572a3aSSebastian Ott 6257a572a3aSSebastian Ott zpci_free_iomap(zdev, zdev->bars[i].map_idx); 6267a572a3aSSebastian Ott release_resource(zdev->bars[i].res); 6277a572a3aSSebastian Ott kfree(zdev->bars[i].res); 6287a572a3aSSebastian Ott } 6297a572a3aSSebastian Ott } 6307a572a3aSSebastian Ott 631af0a8a84SSebastian Ott int pcibios_add_device(struct pci_dev *pdev) 632af0a8a84SSebastian Ott { 633cb809182SSebastian Ott struct resource *res; 634cb809182SSebastian Ott int i; 635cb809182SSebastian Ott 636ef4858c6SSebastian Ott pdev->dev.groups = zpci_attr_groups; 6375657933dSBart Van Assche pdev->dev.dma_ops = &s390_pci_dma_ops; 6381803ba2dSSebastian Ott zpci_map_resources(pdev); 639cb809182SSebastian Ott 640cb809182SSebastian Ott for (i = 0; i < PCI_BAR_COUNT; i++) { 641cb809182SSebastian Ott res = &pdev->resource[i]; 642cb809182SSebastian Ott if (res->parent || !res->flags) 643cb809182SSebastian Ott continue; 644cb809182SSebastian Ott pci_claim_resource(pdev, i); 645cb809182SSebastian Ott } 646cb809182SSebastian Ott 647cb809182SSebastian Ott return 0; 648cb809182SSebastian Ott } 649cb809182SSebastian Ott 6501803ba2dSSebastian Ott void pcibios_release_device(struct pci_dev *pdev) 6511803ba2dSSebastian Ott { 6521803ba2dSSebastian Ott zpci_unmap_resources(pdev); 6531803ba2dSSebastian Ott } 6541803ba2dSSebastian Ott 655cb809182SSebastian Ott int pcibios_enable_device(struct pci_dev *pdev, int mask) 656cb809182SSebastian Ott { 657198a5278SSebastian Ott struct zpci_dev *zdev = to_zpci(pdev); 658af0a8a84SSebastian Ott 6599a99649fSSebastian Ott zpci_debug_init_device(zdev, dev_name(&pdev->dev)); 660af0a8a84SSebastian Ott zpci_fmb_enable_device(zdev); 661af0a8a84SSebastian Ott 662d7533232SBjorn Helgaas return pci_enable_resources(pdev, mask); 663af0a8a84SSebastian Ott } 664af0a8a84SSebastian Ott 665cb809182SSebastian Ott void pcibios_disable_device(struct pci_dev *pdev) 666944239c5SSebastian Ott { 667198a5278SSebastian Ott struct zpci_dev *zdev = to_zpci(pdev); 668944239c5SSebastian Ott 669944239c5SSebastian Ott zpci_fmb_disable_device(zdev); 670944239c5SSebastian Ott zpci_debug_exit_device(zdev); 671944239c5SSebastian Ott } 672944239c5SSebastian Ott 67369db3b5eSSebastian Ott #ifdef CONFIG_HIBERNATE_CALLBACKS 67469db3b5eSSebastian Ott static int zpci_restore(struct device *dev) 67569db3b5eSSebastian Ott { 6761803ba2dSSebastian Ott struct pci_dev *pdev = to_pci_dev(dev); 677198a5278SSebastian Ott struct zpci_dev *zdev = to_zpci(pdev); 67869db3b5eSSebastian Ott int ret = 0; 67969db3b5eSSebastian Ott 68069db3b5eSSebastian Ott if (zdev->state != ZPCI_FN_STATE_ONLINE) 68169db3b5eSSebastian Ott goto out; 68269db3b5eSSebastian Ott 68369db3b5eSSebastian Ott ret = clp_enable_fh(zdev, ZPCI_NR_DMA_SPACES); 68469db3b5eSSebastian Ott if (ret) 68569db3b5eSSebastian Ott goto out; 68669db3b5eSSebastian Ott 6871803ba2dSSebastian Ott zpci_map_resources(pdev); 68869eea95cSGerald Schaefer zpci_register_ioat(zdev, 0, zdev->start_dma, zdev->end_dma, 68969db3b5eSSebastian Ott (u64) zdev->dma_table); 69069db3b5eSSebastian Ott 69169db3b5eSSebastian Ott out: 69269db3b5eSSebastian Ott return ret; 69369db3b5eSSebastian Ott } 69469db3b5eSSebastian Ott 69569db3b5eSSebastian Ott static int zpci_freeze(struct device *dev) 69669db3b5eSSebastian Ott { 6971803ba2dSSebastian Ott struct pci_dev *pdev = to_pci_dev(dev); 698198a5278SSebastian Ott struct zpci_dev *zdev = to_zpci(pdev); 69969db3b5eSSebastian Ott 70069db3b5eSSebastian Ott if (zdev->state != ZPCI_FN_STATE_ONLINE) 70169db3b5eSSebastian Ott return 0; 70269db3b5eSSebastian Ott 70369db3b5eSSebastian Ott zpci_unregister_ioat(zdev, 0); 7041803ba2dSSebastian Ott zpci_unmap_resources(pdev); 70569db3b5eSSebastian Ott return clp_disable_fh(zdev); 70669db3b5eSSebastian Ott } 70769db3b5eSSebastian Ott 70869db3b5eSSebastian Ott struct dev_pm_ops pcibios_pm_ops = { 70969db3b5eSSebastian Ott .thaw_noirq = zpci_restore, 71069db3b5eSSebastian Ott .freeze_noirq = zpci_freeze, 71169db3b5eSSebastian Ott .restore_noirq = zpci_restore, 71269db3b5eSSebastian Ott .poweroff_noirq = zpci_freeze, 71369db3b5eSSebastian Ott }; 71469db3b5eSSebastian Ott #endif /* CONFIG_HIBERNATE_CALLBACKS */ 71569db3b5eSSebastian Ott 716cd248341SJan Glauber static int zpci_alloc_domain(struct zpci_dev *zdev) 717cd248341SJan Glauber { 7185c5afd02SSebastian Ott if (zpci_unique_uid) { 7195c5afd02SSebastian Ott zdev->domain = (u16) zdev->uid; 7205c5afd02SSebastian Ott return 0; 7215c5afd02SSebastian Ott } 7225c5afd02SSebastian Ott 723cd248341SJan Glauber spin_lock(&zpci_domain_lock); 724cd248341SJan Glauber zdev->domain = find_first_zero_bit(zpci_domain, ZPCI_NR_DEVICES); 725cd248341SJan Glauber if (zdev->domain == ZPCI_NR_DEVICES) { 726cd248341SJan Glauber spin_unlock(&zpci_domain_lock); 727cd248341SJan Glauber return -ENOSPC; 728cd248341SJan Glauber } 729cd248341SJan Glauber set_bit(zdev->domain, zpci_domain); 730cd248341SJan Glauber spin_unlock(&zpci_domain_lock); 731cd248341SJan Glauber return 0; 732cd248341SJan Glauber } 733cd248341SJan Glauber 734cd248341SJan Glauber static void zpci_free_domain(struct zpci_dev *zdev) 735cd248341SJan Glauber { 7365c5afd02SSebastian Ott if (zpci_unique_uid) 7375c5afd02SSebastian Ott return; 7385c5afd02SSebastian Ott 739cd248341SJan Glauber spin_lock(&zpci_domain_lock); 740cd248341SJan Glauber clear_bit(zdev->domain, zpci_domain); 741cd248341SJan Glauber spin_unlock(&zpci_domain_lock); 742cd248341SJan Glauber } 743cd248341SJan Glauber 7447d594322SSebastian Ott void pcibios_remove_bus(struct pci_bus *bus) 7457d594322SSebastian Ott { 7467d594322SSebastian Ott struct zpci_dev *zdev = get_zdev_by_bus(bus); 7477d594322SSebastian Ott 7487d594322SSebastian Ott zpci_exit_slot(zdev); 7497d594322SSebastian Ott zpci_cleanup_bus_resources(zdev); 7507d594322SSebastian Ott zpci_free_domain(zdev); 7517d594322SSebastian Ott 7527d594322SSebastian Ott spin_lock(&zpci_list_lock); 7537d594322SSebastian Ott list_del(&zdev->entry); 7547d594322SSebastian Ott spin_unlock(&zpci_list_lock); 7557d594322SSebastian Ott 7567d594322SSebastian Ott kfree(zdev); 7577d594322SSebastian Ott } 7587d594322SSebastian Ott 7597d594322SSebastian Ott static int zpci_scan_bus(struct zpci_dev *zdev) 7607d594322SSebastian Ott { 7617d594322SSebastian Ott LIST_HEAD(resources); 7627d594322SSebastian Ott int ret; 7637d594322SSebastian Ott 7647d594322SSebastian Ott ret = zpci_setup_bus_resources(zdev, &resources); 7657d594322SSebastian Ott if (ret) 7662b1df724SSebastian Ott goto error; 7677d594322SSebastian Ott 7687d594322SSebastian Ott zdev->bus = pci_scan_root_bus(NULL, ZPCI_BUS_NR, &pci_root_ops, 7697d594322SSebastian Ott zdev, &resources); 7707d594322SSebastian Ott if (!zdev->bus) { 7712b1df724SSebastian Ott ret = -EIO; 7722b1df724SSebastian Ott goto error; 7737d594322SSebastian Ott } 7747d594322SSebastian Ott zdev->bus->max_bus_speed = zdev->max_bus_speed; 775b97ea289SYijing Wang pci_bus_add_devices(zdev->bus); 7767d594322SSebastian Ott return 0; 7772b1df724SSebastian Ott 7782b1df724SSebastian Ott error: 7792b1df724SSebastian Ott zpci_cleanup_bus_resources(zdev); 7802b1df724SSebastian Ott pci_free_resource_list(&resources); 7812b1df724SSebastian Ott return ret; 7827d594322SSebastian Ott } 7837d594322SSebastian Ott 784a755a45dSJan Glauber int zpci_enable_device(struct zpci_dev *zdev) 785a755a45dSJan Glauber { 786a755a45dSJan Glauber int rc; 787a755a45dSJan Glauber 788a755a45dSJan Glauber rc = clp_enable_fh(zdev, ZPCI_NR_DMA_SPACES); 789a755a45dSJan Glauber if (rc) 790a755a45dSJan Glauber goto out; 791828b35f6SJan Glauber 792828b35f6SJan Glauber rc = zpci_dma_init_device(zdev); 793828b35f6SJan Glauber if (rc) 794828b35f6SJan Glauber goto out_dma; 7950ff70ec8SSebastian Ott 7960ff70ec8SSebastian Ott zdev->state = ZPCI_FN_STATE_ONLINE; 797a755a45dSJan Glauber return 0; 798828b35f6SJan Glauber 799828b35f6SJan Glauber out_dma: 800828b35f6SJan Glauber clp_disable_fh(zdev); 801a755a45dSJan Glauber out: 802a755a45dSJan Glauber return rc; 803a755a45dSJan Glauber } 804a755a45dSJan Glauber EXPORT_SYMBOL_GPL(zpci_enable_device); 805a755a45dSJan Glauber 806cb65a669SSebastian Ott int zpci_disable_device(struct zpci_dev *zdev) 807cb65a669SSebastian Ott { 808cb65a669SSebastian Ott zpci_dma_exit_device(zdev); 809cb65a669SSebastian Ott return clp_disable_fh(zdev); 810cb65a669SSebastian Ott } 811cb65a669SSebastian Ott EXPORT_SYMBOL_GPL(zpci_disable_device); 812cb65a669SSebastian Ott 813cd248341SJan Glauber int zpci_create_device(struct zpci_dev *zdev) 814cd248341SJan Glauber { 815cd248341SJan Glauber int rc; 816cd248341SJan Glauber 817cd248341SJan Glauber rc = zpci_alloc_domain(zdev); 818cd248341SJan Glauber if (rc) 819cd248341SJan Glauber goto out; 820cd248341SJan Glauber 82180ed156aSSebastian Ott mutex_init(&zdev->lock); 8221c21351bSSebastian Ott if (zdev->state == ZPCI_FN_STATE_CONFIGURED) { 8231c21351bSSebastian Ott rc = zpci_enable_device(zdev); 824cd248341SJan Glauber if (rc) 8251c21351bSSebastian Ott goto out_free; 8261c21351bSSebastian Ott } 8271c21351bSSebastian Ott rc = zpci_scan_bus(zdev); 8281c21351bSSebastian Ott if (rc) 8291c21351bSSebastian Ott goto out_disable; 830cd248341SJan Glauber 83157b5918cSSebastian Ott spin_lock(&zpci_list_lock); 832cd248341SJan Glauber list_add_tail(&zdev->entry, &zpci_list); 83357b5918cSSebastian Ott spin_unlock(&zpci_list_lock); 834cd248341SJan Glauber 83567f43f38SSebastian Ott zpci_init_slot(zdev); 83667f43f38SSebastian Ott 837cd248341SJan Glauber return 0; 838cd248341SJan Glauber 8391c21351bSSebastian Ott out_disable: 8401c21351bSSebastian Ott if (zdev->state == ZPCI_FN_STATE_ONLINE) 8411c21351bSSebastian Ott zpci_disable_device(zdev); 8421c21351bSSebastian Ott out_free: 843cd248341SJan Glauber zpci_free_domain(zdev); 844cd248341SJan Glauber out: 845cd248341SJan Glauber return rc; 846cd248341SJan Glauber } 847cd248341SJan Glauber 848cd248341SJan Glauber void zpci_stop_device(struct zpci_dev *zdev) 849cd248341SJan Glauber { 850828b35f6SJan Glauber zpci_dma_exit_device(zdev); 851cd248341SJan Glauber /* 852cd248341SJan Glauber * Note: SCLP disables fh via set-pci-fn so don't 853cd248341SJan Glauber * do that here. 854cd248341SJan Glauber */ 855cd248341SJan Glauber } 856cd248341SJan Glauber EXPORT_SYMBOL_GPL(zpci_stop_device); 857cd248341SJan Glauber 858*623bd44dSSebastian Ott void zpci_remove_device(struct zpci_dev *zdev) 859*623bd44dSSebastian Ott { 860*623bd44dSSebastian Ott if (!zdev->bus) 861*623bd44dSSebastian Ott return; 862*623bd44dSSebastian Ott 863*623bd44dSSebastian Ott pci_stop_root_bus(zdev->bus); 864*623bd44dSSebastian Ott pci_remove_root_bus(zdev->bus); 865*623bd44dSSebastian Ott } 866*623bd44dSSebastian Ott 867bd3a1725SMartin Schwidefsky int zpci_report_error(struct pci_dev *pdev, 868bd3a1725SMartin Schwidefsky struct zpci_report_error_header *report) 869bd3a1725SMartin Schwidefsky { 870bd3a1725SMartin Schwidefsky struct zpci_dev *zdev = to_zpci(pdev); 871bd3a1725SMartin Schwidefsky 872bd3a1725SMartin Schwidefsky return sclp_pci_report(report, zdev->fh, zdev->fid); 873bd3a1725SMartin Schwidefsky } 874bd3a1725SMartin Schwidefsky EXPORT_SYMBOL(zpci_report_error); 875bd3a1725SMartin Schwidefsky 876cd248341SJan Glauber static int zpci_mem_init(void) 877cd248341SJan Glauber { 87880c544deSSebastian Ott BUILD_BUG_ON(!is_power_of_2(__alignof__(struct zpci_fmb)) || 87980c544deSSebastian Ott __alignof__(struct zpci_fmb) < sizeof(struct zpci_fmb)); 88080c544deSSebastian Ott 881d0b08853SJan Glauber zdev_fmb_cache = kmem_cache_create("PCI_FMB_cache", sizeof(struct zpci_fmb), 88280c544deSSebastian Ott __alignof__(struct zpci_fmb), 0, NULL); 883d0b08853SJan Glauber if (!zdev_fmb_cache) 884c506fff3SSebastian Ott goto error_fmb; 885d0b08853SJan Glauber 886c506fff3SSebastian Ott zpci_iomap_start = kcalloc(ZPCI_IOMAP_ENTRIES, 887c506fff3SSebastian Ott sizeof(*zpci_iomap_start), GFP_KERNEL); 888cd248341SJan Glauber if (!zpci_iomap_start) 8899a4da8a5SJan Glauber goto error_iomap; 890cd248341SJan Glauber 891c506fff3SSebastian Ott zpci_iomap_bitmap = kcalloc(BITS_TO_LONGS(ZPCI_IOMAP_ENTRIES), 892c506fff3SSebastian Ott sizeof(*zpci_iomap_bitmap), GFP_KERNEL); 893c506fff3SSebastian Ott if (!zpci_iomap_bitmap) 894c506fff3SSebastian Ott goto error_iomap_bitmap; 895c506fff3SSebastian Ott 896c506fff3SSebastian Ott return 0; 897c506fff3SSebastian Ott error_iomap_bitmap: 898c506fff3SSebastian Ott kfree(zpci_iomap_start); 8999a4da8a5SJan Glauber error_iomap: 900d0b08853SJan Glauber kmem_cache_destroy(zdev_fmb_cache); 901c506fff3SSebastian Ott error_fmb: 902cd248341SJan Glauber return -ENOMEM; 903cd248341SJan Glauber } 904cd248341SJan Glauber 905cd248341SJan Glauber static void zpci_mem_exit(void) 906cd248341SJan Glauber { 907c506fff3SSebastian Ott kfree(zpci_iomap_bitmap); 908cd248341SJan Glauber kfree(zpci_iomap_start); 909d0b08853SJan Glauber kmem_cache_destroy(zdev_fmb_cache); 910cd248341SJan Glauber } 911cd248341SJan Glauber 912257608fbSSebastian Ott static unsigned int s390_pci_probe = 1; 913aa3b7c29SSebastian Ott static unsigned int s390_pci_initialized; 914cd248341SJan Glauber 915cd248341SJan Glauber char * __init pcibios_setup(char *str) 916cd248341SJan Glauber { 917257608fbSSebastian Ott if (!strcmp(str, "off")) { 918257608fbSSebastian Ott s390_pci_probe = 0; 919cd248341SJan Glauber return NULL; 920cd248341SJan Glauber } 921cd248341SJan Glauber return str; 922cd248341SJan Glauber } 923cd248341SJan Glauber 924aa3b7c29SSebastian Ott bool zpci_is_enabled(void) 925aa3b7c29SSebastian Ott { 926aa3b7c29SSebastian Ott return s390_pci_initialized; 927aa3b7c29SSebastian Ott } 928aa3b7c29SSebastian Ott 929cd248341SJan Glauber static int __init pci_base_init(void) 930cd248341SJan Glauber { 931cd248341SJan Glauber int rc; 932cd248341SJan Glauber 9331e5635d1SHeiko Carstens if (!s390_pci_probe) 934cd248341SJan Glauber return 0; 935cd248341SJan Glauber 93686cd741bSHeiko Carstens if (!test_facility(69) || !test_facility(71) || !test_facility(72)) 937cd248341SJan Glauber return 0; 938cd248341SJan Glauber 939d0b08853SJan Glauber rc = zpci_debug_init(); 940d0b08853SJan Glauber if (rc) 9411f44a225SMartin Schwidefsky goto out; 942d0b08853SJan Glauber 943cd248341SJan Glauber rc = zpci_mem_init(); 944cd248341SJan Glauber if (rc) 945cd248341SJan Glauber goto out_mem; 946cd248341SJan Glauber 9479a4da8a5SJan Glauber rc = zpci_irq_init(); 9489a4da8a5SJan Glauber if (rc) 9499a4da8a5SJan Glauber goto out_irq; 9509a4da8a5SJan Glauber 951828b35f6SJan Glauber rc = zpci_dma_init(); 952828b35f6SJan Glauber if (rc) 953828b35f6SJan Glauber goto out_dma; 954828b35f6SJan Glauber 9551d578966SSebastian Ott rc = clp_scan_pci_devices(); 956a755a45dSJan Glauber if (rc) 957a755a45dSJan Glauber goto out_find; 958a755a45dSJan Glauber 959aa3b7c29SSebastian Ott s390_pci_initialized = 1; 960cd248341SJan Glauber return 0; 961cd248341SJan Glauber 962a755a45dSJan Glauber out_find: 963828b35f6SJan Glauber zpci_dma_exit(); 964828b35f6SJan Glauber out_dma: 9659a4da8a5SJan Glauber zpci_irq_exit(); 9669a4da8a5SJan Glauber out_irq: 967cd248341SJan Glauber zpci_mem_exit(); 968cd248341SJan Glauber out_mem: 969d0b08853SJan Glauber zpci_debug_exit(); 9701f44a225SMartin Schwidefsky out: 971cd248341SJan Glauber return rc; 972cd248341SJan Glauber } 97367f43f38SSebastian Ott subsys_initcall_sync(pci_base_init); 97457b5918cSSebastian Ott 97557b5918cSSebastian Ott void zpci_rescan(void) 97657b5918cSSebastian Ott { 977aa3b7c29SSebastian Ott if (zpci_is_enabled()) 97857b5918cSSebastian Ott clp_rescan_pci_devices_simple(); 97957b5918cSSebastian Ott } 980