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 63f4eae94fSMartin Schwidefsky /* Adapter interrupt definitions */ 64f4eae94fSMartin Schwidefsky static void zpci_irq_handler(struct airq_struct *airq); 65f4eae94fSMartin Schwidefsky 66f4eae94fSMartin Schwidefsky static struct airq_struct zpci_airq = { 67f4eae94fSMartin Schwidefsky .handler = zpci_irq_handler, 68f4eae94fSMartin Schwidefsky .isc = PCI_ISC, 69f4eae94fSMartin Schwidefsky }; 709a4da8a5SJan Glauber 71c506fff3SSebastian Ott #define ZPCI_IOMAP_ENTRIES \ 72c506fff3SSebastian Ott min(((unsigned long) CONFIG_PCI_NR_FUNCTIONS * PCI_BAR_COUNT), \ 73c506fff3SSebastian Ott ZPCI_IOMAP_MAX_ENTRIES) 74c506fff3SSebastian Ott 75cd248341SJan Glauber static DEFINE_SPINLOCK(zpci_iomap_lock); 76c506fff3SSebastian Ott static unsigned long *zpci_iomap_bitmap; 77cd248341SJan Glauber struct zpci_iomap_entry *zpci_iomap_start; 78cd248341SJan Glauber EXPORT_SYMBOL_GPL(zpci_iomap_start); 79cd248341SJan Glauber 80d0b08853SJan Glauber static struct kmem_cache *zdev_fmb_cache; 81d0b08853SJan Glauber 82cd248341SJan Glauber struct zpci_dev *get_zdev_by_fid(u32 fid) 83cd248341SJan Glauber { 84cd248341SJan Glauber struct zpci_dev *tmp, *zdev = NULL; 85cd248341SJan Glauber 8657b5918cSSebastian Ott spin_lock(&zpci_list_lock); 87cd248341SJan Glauber list_for_each_entry(tmp, &zpci_list, entry) { 88cd248341SJan Glauber if (tmp->fid == fid) { 89cd248341SJan Glauber zdev = tmp; 90cd248341SJan Glauber break; 91cd248341SJan Glauber } 92cd248341SJan Glauber } 9357b5918cSSebastian Ott spin_unlock(&zpci_list_lock); 94cd248341SJan Glauber return zdev; 95cd248341SJan Glauber } 96cd248341SJan Glauber 97cd248341SJan Glauber static struct zpci_dev *get_zdev_by_bus(struct pci_bus *bus) 98cd248341SJan Glauber { 99cd248341SJan Glauber return (bus && bus->sysdata) ? (struct zpci_dev *) bus->sysdata : NULL; 100cd248341SJan Glauber } 101cd248341SJan Glauber 102cd248341SJan Glauber int pci_domain_nr(struct pci_bus *bus) 103cd248341SJan Glauber { 104cd248341SJan Glauber return ((struct zpci_dev *) bus->sysdata)->domain; 105cd248341SJan Glauber } 106cd248341SJan Glauber EXPORT_SYMBOL_GPL(pci_domain_nr); 107cd248341SJan Glauber 108cd248341SJan Glauber int pci_proc_domain(struct pci_bus *bus) 109cd248341SJan Glauber { 110cd248341SJan Glauber return pci_domain_nr(bus); 111cd248341SJan Glauber } 112cd248341SJan Glauber EXPORT_SYMBOL_GPL(pci_proc_domain); 113cd248341SJan Glauber 1149a4da8a5SJan Glauber /* Modify PCI: Register adapter interruptions */ 1155d0d8f43SMartin Schwidefsky static int zpci_set_airq(struct zpci_dev *zdev) 1169a4da8a5SJan Glauber { 1179a4da8a5SJan Glauber u64 req = ZPCI_CREATE_REQ(zdev->fh, 0, ZPCI_MOD_FC_REG_INT); 118cb4deb69SSebastian Ott struct zpci_fib fib = {0}; 1199a4da8a5SJan Glauber 120cb4deb69SSebastian Ott fib.isc = PCI_ISC; 121cb4deb69SSebastian Ott fib.sum = 1; /* enable summary notifications */ 122cb4deb69SSebastian Ott fib.noi = airq_iv_end(zdev->aibv); 123cb4deb69SSebastian Ott fib.aibv = (unsigned long) zdev->aibv->vector; 124cb4deb69SSebastian Ott fib.aibvo = 0; /* each zdev has its own interrupt vector */ 125cb4deb69SSebastian Ott fib.aisb = (unsigned long) zpci_aisb_iv->vector + (zdev->aisb/64)*8; 126cb4deb69SSebastian Ott fib.aisbo = zdev->aisb & 63; 1279a4da8a5SJan Glauber 128cb4deb69SSebastian Ott return zpci_mod_fc(req, &fib); 1299a4da8a5SJan Glauber } 1309a4da8a5SJan Glauber 1319a4da8a5SJan Glauber struct mod_pci_args { 1329a4da8a5SJan Glauber u64 base; 1339a4da8a5SJan Glauber u64 limit; 1349a4da8a5SJan Glauber u64 iota; 135d0b08853SJan Glauber u64 fmb_addr; 1369a4da8a5SJan Glauber }; 1379a4da8a5SJan Glauber 1389a4da8a5SJan Glauber static int mod_pci(struct zpci_dev *zdev, int fn, u8 dmaas, struct mod_pci_args *args) 1399a4da8a5SJan Glauber { 1409a4da8a5SJan Glauber u64 req = ZPCI_CREATE_REQ(zdev->fh, dmaas, fn); 141cb4deb69SSebastian Ott struct zpci_fib fib = {0}; 1429a4da8a5SJan Glauber 143cb4deb69SSebastian Ott fib.pba = args->base; 144cb4deb69SSebastian Ott fib.pal = args->limit; 145cb4deb69SSebastian Ott fib.iota = args->iota; 146cb4deb69SSebastian Ott fib.fmb_addr = args->fmb_addr; 1479a4da8a5SJan Glauber 148cb4deb69SSebastian Ott return zpci_mod_fc(req, &fib); 1499a4da8a5SJan Glauber } 1509a4da8a5SJan Glauber 151828b35f6SJan Glauber /* Modify PCI: Register I/O address translation parameters */ 152828b35f6SJan Glauber int zpci_register_ioat(struct zpci_dev *zdev, u8 dmaas, 153828b35f6SJan Glauber u64 base, u64 limit, u64 iota) 154828b35f6SJan Glauber { 155d0b08853SJan Glauber struct mod_pci_args args = { base, limit, iota, 0 }; 156828b35f6SJan Glauber 157828b35f6SJan Glauber WARN_ON_ONCE(iota & 0x3fff); 158828b35f6SJan Glauber args.iota |= ZPCI_IOTA_RTTO_FLAG; 159828b35f6SJan Glauber return mod_pci(zdev, ZPCI_MOD_FC_REG_IOAT, dmaas, &args); 160828b35f6SJan Glauber } 161828b35f6SJan Glauber 162828b35f6SJan Glauber /* Modify PCI: Unregister I/O address translation parameters */ 163828b35f6SJan Glauber int zpci_unregister_ioat(struct zpci_dev *zdev, u8 dmaas) 164828b35f6SJan Glauber { 165d0b08853SJan Glauber struct mod_pci_args args = { 0, 0, 0, 0 }; 166828b35f6SJan Glauber 167828b35f6SJan Glauber return mod_pci(zdev, ZPCI_MOD_FC_DEREG_IOAT, dmaas, &args); 168828b35f6SJan Glauber } 169828b35f6SJan Glauber 1709a4da8a5SJan Glauber /* Modify PCI: Unregister adapter interruptions */ 1715d0d8f43SMartin Schwidefsky static int zpci_clear_airq(struct zpci_dev *zdev) 1729a4da8a5SJan Glauber { 173d0b08853SJan Glauber struct mod_pci_args args = { 0, 0, 0, 0 }; 1749a4da8a5SJan Glauber 1759a4da8a5SJan Glauber return mod_pci(zdev, ZPCI_MOD_FC_DEREG_INT, 0, &args); 1769a4da8a5SJan Glauber } 1779a4da8a5SJan Glauber 178d0b08853SJan Glauber /* Modify PCI: Set PCI function measurement parameters */ 179d0b08853SJan Glauber int zpci_fmb_enable_device(struct zpci_dev *zdev) 180d0b08853SJan Glauber { 181d0b08853SJan Glauber struct mod_pci_args args = { 0, 0, 0, 0 }; 182d0b08853SJan Glauber 183*0b7589ecSSebastian Ott if (zdev->fmb || sizeof(*zdev->fmb) < zdev->fmb_length) 184d0b08853SJan Glauber return -EINVAL; 185d0b08853SJan Glauber 18608b42124SWei Yongjun zdev->fmb = kmem_cache_zalloc(zdev_fmb_cache, GFP_KERNEL); 187d0b08853SJan Glauber if (!zdev->fmb) 188d0b08853SJan Glauber return -ENOMEM; 189d0b08853SJan Glauber WARN_ON((u64) zdev->fmb & 0xf); 190d0b08853SJan Glauber 1916001018aSSebastian Ott /* reset software counters */ 1926001018aSSebastian Ott atomic64_set(&zdev->allocated_pages, 0); 1936001018aSSebastian Ott atomic64_set(&zdev->mapped_pages, 0); 1946001018aSSebastian Ott atomic64_set(&zdev->unmapped_pages, 0); 1956001018aSSebastian Ott 196d0b08853SJan Glauber args.fmb_addr = virt_to_phys(zdev->fmb); 197d0b08853SJan Glauber return mod_pci(zdev, ZPCI_MOD_FC_SET_MEASURE, 0, &args); 198d0b08853SJan Glauber } 199d0b08853SJan Glauber 200d0b08853SJan Glauber /* Modify PCI: Disable PCI function measurement */ 201d0b08853SJan Glauber int zpci_fmb_disable_device(struct zpci_dev *zdev) 202d0b08853SJan Glauber { 203d0b08853SJan Glauber struct mod_pci_args args = { 0, 0, 0, 0 }; 204d0b08853SJan Glauber int rc; 205d0b08853SJan Glauber 206d0b08853SJan Glauber if (!zdev->fmb) 207d0b08853SJan Glauber return -EINVAL; 208d0b08853SJan Glauber 209d0b08853SJan Glauber /* Function measurement is disabled if fmb address is zero */ 210d0b08853SJan Glauber rc = mod_pci(zdev, ZPCI_MOD_FC_SET_MEASURE, 0, &args); 211d0b08853SJan Glauber 212d0b08853SJan Glauber kmem_cache_free(zdev_fmb_cache, zdev->fmb); 213d0b08853SJan Glauber zdev->fmb = NULL; 214d0b08853SJan Glauber return rc; 215d0b08853SJan Glauber } 216d0b08853SJan Glauber 217cd248341SJan Glauber #define ZPCI_PCIAS_CFGSPC 15 218cd248341SJan Glauber 219cd248341SJan Glauber static int zpci_cfg_load(struct zpci_dev *zdev, int offset, u32 *val, u8 len) 220cd248341SJan Glauber { 221cd248341SJan Glauber u64 req = ZPCI_CREATE_REQ(zdev->fh, ZPCI_PCIAS_CFGSPC, len); 222cd248341SJan Glauber u64 data; 223cd248341SJan Glauber int rc; 224cd248341SJan Glauber 2259389339fSMartin Schwidefsky rc = zpci_load(&data, req, offset); 226b170bad4SSebastian Ott if (!rc) { 227cd248341SJan Glauber data = data << ((8 - len) * 8); 228cd248341SJan Glauber data = le64_to_cpu(data); 229cd248341SJan Glauber *val = (u32) data; 230b170bad4SSebastian Ott } else 231cd248341SJan Glauber *val = 0xffffffff; 232cd248341SJan Glauber return rc; 233cd248341SJan Glauber } 234cd248341SJan Glauber 235cd248341SJan Glauber static int zpci_cfg_store(struct zpci_dev *zdev, int offset, u32 val, u8 len) 236cd248341SJan Glauber { 237cd248341SJan Glauber u64 req = ZPCI_CREATE_REQ(zdev->fh, ZPCI_PCIAS_CFGSPC, len); 238cd248341SJan Glauber u64 data = val; 239cd248341SJan Glauber int rc; 240cd248341SJan Glauber 241cd248341SJan Glauber data = cpu_to_le64(data); 242cd248341SJan Glauber data = data >> ((8 - len) * 8); 2439389339fSMartin Schwidefsky rc = zpci_store(data, req, offset); 244cd248341SJan Glauber return rc; 245cd248341SJan Glauber } 246cd248341SJan Glauber 247b881bc46SGreg Kroah-Hartman void pcibios_fixup_bus(struct pci_bus *bus) 248cd248341SJan Glauber { 249cd248341SJan Glauber } 250cd248341SJan Glauber 251cd248341SJan Glauber resource_size_t pcibios_align_resource(void *data, const struct resource *res, 252cd248341SJan Glauber resource_size_t size, 253cd248341SJan Glauber resource_size_t align) 254cd248341SJan Glauber { 255cd248341SJan Glauber return 0; 256cd248341SJan Glauber } 257cd248341SJan Glauber 25887bc359bSJan Glauber /* combine single writes by using store-block insn */ 25987bc359bSJan Glauber void __iowrite64_copy(void __iomem *to, const void *from, size_t count) 26087bc359bSJan Glauber { 26187bc359bSJan Glauber zpci_memcpy_toio(to, from, count); 26287bc359bSJan Glauber } 26387bc359bSJan Glauber 264cd248341SJan Glauber /* Create a virtual mapping cookie for a PCI BAR */ 2658cfc99b5SMichael S. Tsirkin void __iomem *pci_iomap_range(struct pci_dev *pdev, 2668cfc99b5SMichael S. Tsirkin int bar, 2678cfc99b5SMichael S. Tsirkin unsigned long offset, 2688cfc99b5SMichael S. Tsirkin unsigned long max) 269cd248341SJan Glauber { 270198a5278SSebastian Ott struct zpci_dev *zdev = to_zpci(pdev); 271cd248341SJan Glauber int idx; 272cd248341SJan Glauber 273c0cabaddSSebastian Ott if (!pci_resource_len(pdev, bar)) 274cd248341SJan Glauber return NULL; 275cd248341SJan Glauber 276cd248341SJan Glauber idx = zdev->bars[bar].map_idx; 277cd248341SJan Glauber spin_lock(&zpci_iomap_lock); 278f5e44f82SSebastian Ott /* Detect overrun */ 279f5e44f82SSebastian Ott WARN_ON(!++zpci_iomap_start[idx].count); 280cd248341SJan Glauber zpci_iomap_start[idx].fh = zdev->fh; 281cd248341SJan Glauber zpci_iomap_start[idx].bar = bar; 282cd248341SJan Glauber spin_unlock(&zpci_iomap_lock); 283cd248341SJan Glauber 2849e00caaeSSebastian Ott return (void __iomem *) ZPCI_ADDR(idx) + offset; 285cd248341SJan Glauber } 286d9426083SSebastian Ott EXPORT_SYMBOL(pci_iomap_range); 2878cfc99b5SMichael S. Tsirkin 2888cfc99b5SMichael S. Tsirkin void __iomem *pci_iomap(struct pci_dev *dev, int bar, unsigned long maxlen) 2898cfc99b5SMichael S. Tsirkin { 2908cfc99b5SMichael S. Tsirkin return pci_iomap_range(dev, bar, 0, maxlen); 2918cfc99b5SMichael S. Tsirkin } 2928cfc99b5SMichael S. Tsirkin EXPORT_SYMBOL(pci_iomap); 293cd248341SJan Glauber 294cd248341SJan Glauber void pci_iounmap(struct pci_dev *pdev, void __iomem *addr) 295cd248341SJan Glauber { 2969e00caaeSSebastian Ott unsigned int idx = ZPCI_IDX(addr); 297cd248341SJan Glauber 298cd248341SJan Glauber spin_lock(&zpci_iomap_lock); 2998cfc99b5SMichael S. Tsirkin /* Detect underrun */ 300f5e44f82SSebastian Ott WARN_ON(!zpci_iomap_start[idx].count); 3018cfc99b5SMichael S. Tsirkin if (!--zpci_iomap_start[idx].count) { 302cd248341SJan Glauber zpci_iomap_start[idx].fh = 0; 303cd248341SJan Glauber zpci_iomap_start[idx].bar = 0; 3048cfc99b5SMichael S. Tsirkin } 305cd248341SJan Glauber spin_unlock(&zpci_iomap_lock); 306cd248341SJan Glauber } 307d9426083SSebastian Ott EXPORT_SYMBOL(pci_iounmap); 308cd248341SJan Glauber 309cd248341SJan Glauber static int pci_read(struct pci_bus *bus, unsigned int devfn, int where, 310cd248341SJan Glauber int size, u32 *val) 311cd248341SJan Glauber { 312cd248341SJan Glauber struct zpci_dev *zdev = get_zdev_by_bus(bus); 3132c3700bbSSebastian Ott int ret; 314cd248341SJan Glauber 315cd248341SJan Glauber if (!zdev || devfn != ZPCI_DEVFN) 3162c3700bbSSebastian Ott ret = -ENODEV; 3172c3700bbSSebastian Ott else 3182c3700bbSSebastian Ott ret = zpci_cfg_load(zdev, where, val, size); 3192c3700bbSSebastian Ott 3202c3700bbSSebastian Ott return ret; 321cd248341SJan Glauber } 322cd248341SJan Glauber 323cd248341SJan Glauber static int pci_write(struct pci_bus *bus, unsigned int devfn, int where, 324cd248341SJan Glauber int size, u32 val) 325cd248341SJan Glauber { 326cd248341SJan Glauber struct zpci_dev *zdev = get_zdev_by_bus(bus); 3272c3700bbSSebastian Ott int ret; 328cd248341SJan Glauber 329cd248341SJan Glauber if (!zdev || devfn != ZPCI_DEVFN) 3302c3700bbSSebastian Ott ret = -ENODEV; 3312c3700bbSSebastian Ott else 3322c3700bbSSebastian Ott ret = zpci_cfg_store(zdev, where, val, size); 3332c3700bbSSebastian Ott 3342c3700bbSSebastian Ott return ret; 335cd248341SJan Glauber } 336cd248341SJan Glauber 337cd248341SJan Glauber static struct pci_ops pci_root_ops = { 338cd248341SJan Glauber .read = pci_read, 339cd248341SJan Glauber .write = pci_write, 340cd248341SJan Glauber }; 341cd248341SJan Glauber 342f4eae94fSMartin Schwidefsky static void zpci_irq_handler(struct airq_struct *airq) 3439a4da8a5SJan Glauber { 3445d0d8f43SMartin Schwidefsky unsigned long si, ai; 3451f44a225SMartin Schwidefsky struct airq_iv *aibv; 3465d0d8f43SMartin Schwidefsky int irqs_on = 0; 3479a4da8a5SJan Glauber 348420f42ecSHeiko Carstens inc_irq_stat(IRQIO_PCI); 3495d0d8f43SMartin Schwidefsky for (si = 0;;) { 3505d0d8f43SMartin Schwidefsky /* Scan adapter summary indicator bit vector */ 3515d0d8f43SMartin Schwidefsky si = airq_iv_scan(zpci_aisb_iv, si, airq_iv_end(zpci_aisb_iv)); 3525d0d8f43SMartin Schwidefsky if (si == -1UL) { 3535d0d8f43SMartin Schwidefsky if (irqs_on++) 3545d0d8f43SMartin Schwidefsky /* End of second scan with interrupts on. */ 3555d0d8f43SMartin Schwidefsky break; 3565d0d8f43SMartin Schwidefsky /* First scan complete, reenable interrupts. */ 3579389339fSMartin Schwidefsky zpci_set_irq_ctrl(SIC_IRQ_MODE_SINGLE, NULL, PCI_ISC); 3585d0d8f43SMartin Schwidefsky si = 0; 3595d0d8f43SMartin Schwidefsky continue; 3609a4da8a5SJan Glauber } 3615d0d8f43SMartin Schwidefsky 3625d0d8f43SMartin Schwidefsky /* Scan the adapter interrupt vector for this device. */ 3631f44a225SMartin Schwidefsky aibv = zpci_aibv[si]; 3645d0d8f43SMartin Schwidefsky for (ai = 0;;) { 3651f44a225SMartin Schwidefsky ai = airq_iv_scan(aibv, ai, airq_iv_end(aibv)); 3665d0d8f43SMartin Schwidefsky if (ai == -1UL) 3675d0d8f43SMartin Schwidefsky break; 3685d0d8f43SMartin Schwidefsky inc_irq_stat(IRQIO_MSI); 3691f44a225SMartin Schwidefsky airq_iv_lock(aibv, ai); 3701f44a225SMartin Schwidefsky generic_handle_irq(airq_iv_get_data(aibv, ai)); 3711f44a225SMartin Schwidefsky airq_iv_unlock(aibv, ai); 3725d0d8f43SMartin Schwidefsky } 3735d0d8f43SMartin Schwidefsky } 3745d0d8f43SMartin Schwidefsky } 3755d0d8f43SMartin Schwidefsky 3765d0d8f43SMartin Schwidefsky int arch_setup_msi_irqs(struct pci_dev *pdev, int nvec, int type) 3779a4da8a5SJan Glauber { 378198a5278SSebastian Ott struct zpci_dev *zdev = to_zpci(pdev); 3790a0a9421SThomas Gleixner unsigned int hwirq, msi_vecs; 3805d0d8f43SMartin Schwidefsky unsigned long aisb; 3819a4da8a5SJan Glauber struct msi_desc *msi; 3821f44a225SMartin Schwidefsky struct msi_msg msg; 3830a0a9421SThomas Gleixner int rc, irq; 3849a4da8a5SJan Glauber 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 rc = -EIO; 3915d0d8f43SMartin Schwidefsky aisb = airq_iv_alloc_bit(zpci_aisb_iv); 3925d0d8f43SMartin Schwidefsky if (aisb == -1UL) 3935d0d8f43SMartin Schwidefsky goto out; 3949a4da8a5SJan Glauber zdev->aisb = aisb; 3959a4da8a5SJan Glauber 3965d0d8f43SMartin Schwidefsky /* Create adapter interrupt vector */ 3975d0d8f43SMartin Schwidefsky rc = -ENOMEM; 3981f44a225SMartin Schwidefsky zdev->aibv = airq_iv_create(msi_vecs, AIRQ_IV_DATA | AIRQ_IV_BITLOCK); 3995d0d8f43SMartin Schwidefsky if (!zdev->aibv) 4005d0d8f43SMartin Schwidefsky goto out_si; 4019a4da8a5SJan Glauber 4025d0d8f43SMartin Schwidefsky /* Wire up shortcut pointer */ 4031f44a225SMartin Schwidefsky zpci_aibv[aisb] = zdev->aibv; 4045d0d8f43SMartin Schwidefsky 4051f44a225SMartin Schwidefsky /* Request MSI interrupts */ 4061f44a225SMartin Schwidefsky hwirq = 0; 407df516f42SJiang Liu for_each_pci_msi_entry(msi, pdev) { 4081f44a225SMartin Schwidefsky rc = -EIO; 4091f44a225SMartin Schwidefsky irq = irq_alloc_desc(0); /* Alloc irq on node 0 */ 4100a0a9421SThomas Gleixner if (irq < 0) 4111f44a225SMartin Schwidefsky goto out_msi; 4121f44a225SMartin Schwidefsky rc = irq_set_msi_desc(irq, msi); 4139a4da8a5SJan Glauber if (rc) 4145d0d8f43SMartin Schwidefsky goto out_msi; 4151f44a225SMartin Schwidefsky irq_set_chip_and_handler(irq, &zpci_irq_chip, 4161f44a225SMartin Schwidefsky handle_simple_irq); 4171f44a225SMartin Schwidefsky msg.data = hwirq; 4181f44a225SMartin Schwidefsky msg.address_lo = zdev->msi_addr & 0xffffffff; 4191f44a225SMartin Schwidefsky msg.address_hi = zdev->msi_addr >> 32; 42083a18912SJiang Liu pci_write_msi_msg(irq, &msg); 4211f44a225SMartin Schwidefsky airq_iv_set_data(zdev->aibv, hwirq, irq); 4221f44a225SMartin Schwidefsky hwirq++; 4239a4da8a5SJan Glauber } 4249a4da8a5SJan Glauber 4255d0d8f43SMartin Schwidefsky /* Enable adapter interrupts */ 4265d0d8f43SMartin Schwidefsky rc = zpci_set_airq(zdev); 4275d0d8f43SMartin Schwidefsky if (rc) 4285d0d8f43SMartin Schwidefsky goto out_msi; 4295d0d8f43SMartin Schwidefsky 4305d0d8f43SMartin Schwidefsky return (msi_vecs == nvec) ? 0 : msi_vecs; 4315d0d8f43SMartin Schwidefsky 4325d0d8f43SMartin Schwidefsky out_msi: 433df516f42SJiang Liu for_each_pci_msi_entry(msi, pdev) { 4341f44a225SMartin Schwidefsky if (hwirq-- == 0) 4355d0d8f43SMartin Schwidefsky break; 4361f44a225SMartin Schwidefsky irq_set_msi_desc(msi->irq, NULL); 4371f44a225SMartin Schwidefsky irq_free_desc(msi->irq); 4381f44a225SMartin Schwidefsky msi->msg.address_lo = 0; 4391f44a225SMartin Schwidefsky msi->msg.address_hi = 0; 4401f44a225SMartin Schwidefsky msi->msg.data = 0; 4411f44a225SMartin Schwidefsky msi->irq = 0; 4425d0d8f43SMartin Schwidefsky } 4431f44a225SMartin Schwidefsky zpci_aibv[aisb] = NULL; 4445d0d8f43SMartin Schwidefsky airq_iv_release(zdev->aibv); 4455d0d8f43SMartin Schwidefsky out_si: 4465d0d8f43SMartin Schwidefsky airq_iv_free_bit(zpci_aisb_iv, aisb); 4475d0d8f43SMartin Schwidefsky out: 4489a4da8a5SJan Glauber return rc; 4499a4da8a5SJan Glauber } 4509a4da8a5SJan Glauber 4515d0d8f43SMartin Schwidefsky void arch_teardown_msi_irqs(struct pci_dev *pdev) 4529a4da8a5SJan Glauber { 453198a5278SSebastian Ott struct zpci_dev *zdev = to_zpci(pdev); 4549a4da8a5SJan Glauber struct msi_desc *msi; 4555d0d8f43SMartin Schwidefsky int rc; 4569a4da8a5SJan Glauber 4575d0d8f43SMartin Schwidefsky /* Disable adapter interrupts */ 4585d0d8f43SMartin Schwidefsky rc = zpci_clear_airq(zdev); 4591f1dcbd4SSebastian Ott if (rc) 4609a4da8a5SJan Glauber return; 4619a4da8a5SJan Glauber 4621f44a225SMartin Schwidefsky /* Release MSI interrupts */ 463df516f42SJiang Liu for_each_pci_msi_entry(msi, pdev) { 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 4761f44a225SMartin Schwidefsky zpci_aibv[zdev->aisb] = NULL; 4775d0d8f43SMartin Schwidefsky airq_iv_release(zdev->aibv); 4785d0d8f43SMartin Schwidefsky airq_iv_free_bit(zpci_aisb_iv, zdev->aisb); 4799a4da8a5SJan Glauber } 4809a4da8a5SJan Glauber 4811803ba2dSSebastian Ott static void zpci_map_resources(struct pci_dev *pdev) 482cd248341SJan Glauber { 483cd248341SJan Glauber resource_size_t len; 484cd248341SJan Glauber int i; 485cd248341SJan Glauber 486cd248341SJan Glauber for (i = 0; i < PCI_BAR_COUNT; i++) { 487cd248341SJan Glauber len = pci_resource_len(pdev, i); 488cd248341SJan Glauber if (!len) 489cd248341SJan Glauber continue; 4905b9f2081SMartin Schwidefsky pdev->resource[i].start = 4915b9f2081SMartin Schwidefsky (resource_size_t __force) pci_iomap(pdev, i, 0); 492cd248341SJan Glauber pdev->resource[i].end = pdev->resource[i].start + len - 1; 493cd248341SJan Glauber } 494944239c5SSebastian Ott } 495944239c5SSebastian Ott 4961803ba2dSSebastian Ott static void zpci_unmap_resources(struct pci_dev *pdev) 497944239c5SSebastian Ott { 498944239c5SSebastian Ott resource_size_t len; 499944239c5SSebastian Ott int i; 500944239c5SSebastian Ott 501944239c5SSebastian Ott for (i = 0; i < PCI_BAR_COUNT; i++) { 502944239c5SSebastian Ott len = pci_resource_len(pdev, i); 503944239c5SSebastian Ott if (!len) 504944239c5SSebastian Ott continue; 5055b9f2081SMartin Schwidefsky pci_iounmap(pdev, (void __iomem __force *) 5065b9f2081SMartin Schwidefsky pdev->resource[i].start); 507944239c5SSebastian Ott } 508944239c5SSebastian Ott } 509cd248341SJan Glauber 5109a4da8a5SJan Glauber static int __init zpci_irq_init(void) 5119a4da8a5SJan Glauber { 5125d0d8f43SMartin Schwidefsky int rc; 5139a4da8a5SJan Glauber 514f4eae94fSMartin Schwidefsky rc = register_adapter_interrupt(&zpci_airq); 515f4eae94fSMartin Schwidefsky if (rc) 5165d0d8f43SMartin Schwidefsky goto out; 517f4eae94fSMartin Schwidefsky /* Set summary to 1 to be called every time for the ISC. */ 518f4eae94fSMartin Schwidefsky *zpci_airq.lsi_ptr = 1; 5199a4da8a5SJan Glauber 5205d0d8f43SMartin Schwidefsky rc = -ENOMEM; 5215d0d8f43SMartin Schwidefsky zpci_aisb_iv = airq_iv_create(ZPCI_NR_DEVICES, AIRQ_IV_ALLOC); 5225d0d8f43SMartin Schwidefsky if (!zpci_aisb_iv) 5235d0d8f43SMartin Schwidefsky goto out_airq; 5249a4da8a5SJan Glauber 5259389339fSMartin Schwidefsky zpci_set_irq_ctrl(SIC_IRQ_MODE_SINGLE, NULL, PCI_ISC); 5269a4da8a5SJan Glauber return 0; 5279a4da8a5SJan Glauber 5285d0d8f43SMartin Schwidefsky out_airq: 5295d0d8f43SMartin Schwidefsky unregister_adapter_interrupt(&zpci_airq); 5305d0d8f43SMartin Schwidefsky out: 5319a4da8a5SJan Glauber return rc; 5329a4da8a5SJan Glauber } 5339a4da8a5SJan Glauber 5349a4da8a5SJan Glauber static void zpci_irq_exit(void) 5359a4da8a5SJan Glauber { 5365d0d8f43SMartin Schwidefsky airq_iv_release(zpci_aisb_iv); 537f4eae94fSMartin Schwidefsky unregister_adapter_interrupt(&zpci_airq); 5389a4da8a5SJan Glauber } 5399a4da8a5SJan Glauber 540cd248341SJan Glauber static int zpci_alloc_iomap(struct zpci_dev *zdev) 541cd248341SJan Glauber { 542bf19c94dSSebastian Ott unsigned long entry; 543cd248341SJan Glauber 544cd248341SJan Glauber spin_lock(&zpci_iomap_lock); 545c506fff3SSebastian Ott entry = find_first_zero_bit(zpci_iomap_bitmap, ZPCI_IOMAP_ENTRIES); 546c506fff3SSebastian Ott if (entry == ZPCI_IOMAP_ENTRIES) { 547cd248341SJan Glauber spin_unlock(&zpci_iomap_lock); 548cd248341SJan Glauber return -ENOSPC; 549cd248341SJan Glauber } 550c506fff3SSebastian Ott set_bit(entry, zpci_iomap_bitmap); 551cd248341SJan Glauber spin_unlock(&zpci_iomap_lock); 552cd248341SJan Glauber return entry; 553cd248341SJan Glauber } 554cd248341SJan Glauber 555cd248341SJan Glauber static void zpci_free_iomap(struct zpci_dev *zdev, int entry) 556cd248341SJan Glauber { 557cd248341SJan Glauber spin_lock(&zpci_iomap_lock); 558cd248341SJan Glauber memset(&zpci_iomap_start[entry], 0, sizeof(struct zpci_iomap_entry)); 559c506fff3SSebastian Ott clear_bit(entry, zpci_iomap_bitmap); 560cd248341SJan Glauber spin_unlock(&zpci_iomap_lock); 561cd248341SJan Glauber } 562cd248341SJan Glauber 5637a572a3aSSebastian Ott static struct resource *__alloc_res(struct zpci_dev *zdev, unsigned long start, 5647a572a3aSSebastian Ott unsigned long size, unsigned long flags) 5657a572a3aSSebastian Ott { 5667a572a3aSSebastian Ott struct resource *r; 5677a572a3aSSebastian Ott 5687a572a3aSSebastian Ott r = kzalloc(sizeof(*r), GFP_KERNEL); 5697a572a3aSSebastian Ott if (!r) 5707a572a3aSSebastian Ott return NULL; 5717a572a3aSSebastian Ott 5727a572a3aSSebastian Ott r->start = start; 5737a572a3aSSebastian Ott r->end = r->start + size - 1; 5747a572a3aSSebastian Ott r->flags = flags; 5757a572a3aSSebastian Ott r->name = zdev->res_name; 5767a572a3aSSebastian Ott 5777a572a3aSSebastian Ott if (request_resource(&iomem_resource, r)) { 5787a572a3aSSebastian Ott kfree(r); 5797a572a3aSSebastian Ott return NULL; 5807a572a3aSSebastian Ott } 5817a572a3aSSebastian Ott return r; 5827a572a3aSSebastian Ott } 5837a572a3aSSebastian Ott 5847a572a3aSSebastian Ott static int zpci_setup_bus_resources(struct zpci_dev *zdev, 5857a572a3aSSebastian Ott struct list_head *resources) 5867a572a3aSSebastian Ott { 5877a572a3aSSebastian Ott unsigned long addr, size, flags; 5887a572a3aSSebastian Ott struct resource *res; 5897a572a3aSSebastian Ott int i, entry; 5907a572a3aSSebastian Ott 5917a572a3aSSebastian Ott snprintf(zdev->res_name, sizeof(zdev->res_name), 5927a572a3aSSebastian Ott "PCI Bus %04x:%02x", zdev->domain, ZPCI_BUS_NR); 5937a572a3aSSebastian Ott 5947a572a3aSSebastian Ott for (i = 0; i < PCI_BAR_COUNT; i++) { 5957a572a3aSSebastian Ott if (!zdev->bars[i].size) 5967a572a3aSSebastian Ott continue; 5977a572a3aSSebastian Ott entry = zpci_alloc_iomap(zdev); 5987a572a3aSSebastian Ott if (entry < 0) 5997a572a3aSSebastian Ott return entry; 6007a572a3aSSebastian Ott zdev->bars[i].map_idx = entry; 6017a572a3aSSebastian Ott 6027a572a3aSSebastian Ott /* only MMIO is supported */ 6037a572a3aSSebastian Ott flags = IORESOURCE_MEM; 6047a572a3aSSebastian Ott if (zdev->bars[i].val & 8) 6057a572a3aSSebastian Ott flags |= IORESOURCE_PREFETCH; 6067a572a3aSSebastian Ott if (zdev->bars[i].val & 4) 6077a572a3aSSebastian Ott flags |= IORESOURCE_MEM_64; 6087a572a3aSSebastian Ott 6099e00caaeSSebastian Ott addr = ZPCI_ADDR(entry); 6107a572a3aSSebastian Ott size = 1UL << zdev->bars[i].size; 6117a572a3aSSebastian Ott 6127a572a3aSSebastian Ott res = __alloc_res(zdev, addr, size, flags); 6137a572a3aSSebastian Ott if (!res) { 6147a572a3aSSebastian Ott zpci_free_iomap(zdev, entry); 6157a572a3aSSebastian Ott return -ENOMEM; 6167a572a3aSSebastian Ott } 6177a572a3aSSebastian Ott zdev->bars[i].res = res; 6187a572a3aSSebastian Ott pci_add_resource(resources, res); 6197a572a3aSSebastian Ott } 6207a572a3aSSebastian Ott 6217a572a3aSSebastian Ott return 0; 6227a572a3aSSebastian Ott } 6237a572a3aSSebastian Ott 6247a572a3aSSebastian Ott static void zpci_cleanup_bus_resources(struct zpci_dev *zdev) 6257a572a3aSSebastian Ott { 6267a572a3aSSebastian Ott int i; 6277a572a3aSSebastian Ott 6287a572a3aSSebastian Ott for (i = 0; i < PCI_BAR_COUNT; i++) { 6292b1df724SSebastian Ott if (!zdev->bars[i].size || !zdev->bars[i].res) 6307a572a3aSSebastian Ott continue; 6317a572a3aSSebastian Ott 6327a572a3aSSebastian Ott zpci_free_iomap(zdev, zdev->bars[i].map_idx); 6337a572a3aSSebastian Ott release_resource(zdev->bars[i].res); 6347a572a3aSSebastian Ott kfree(zdev->bars[i].res); 6357a572a3aSSebastian Ott } 6367a572a3aSSebastian Ott } 6377a572a3aSSebastian Ott 638af0a8a84SSebastian Ott int pcibios_add_device(struct pci_dev *pdev) 639af0a8a84SSebastian Ott { 640cb809182SSebastian Ott struct resource *res; 641cb809182SSebastian Ott int i; 642cb809182SSebastian Ott 643ef4858c6SSebastian Ott pdev->dev.groups = zpci_attr_groups; 644e82becfcSChristian Borntraeger pdev->dev.archdata.dma_ops = &s390_pci_dma_ops; 6451803ba2dSSebastian Ott zpci_map_resources(pdev); 646cb809182SSebastian Ott 647cb809182SSebastian Ott for (i = 0; i < PCI_BAR_COUNT; i++) { 648cb809182SSebastian Ott res = &pdev->resource[i]; 649cb809182SSebastian Ott if (res->parent || !res->flags) 650cb809182SSebastian Ott continue; 651cb809182SSebastian Ott pci_claim_resource(pdev, i); 652cb809182SSebastian Ott } 653cb809182SSebastian Ott 654cb809182SSebastian Ott return 0; 655cb809182SSebastian Ott } 656cb809182SSebastian Ott 6571803ba2dSSebastian Ott void pcibios_release_device(struct pci_dev *pdev) 6581803ba2dSSebastian Ott { 6591803ba2dSSebastian Ott zpci_unmap_resources(pdev); 6601803ba2dSSebastian Ott } 6611803ba2dSSebastian Ott 662cb809182SSebastian Ott int pcibios_enable_device(struct pci_dev *pdev, int mask) 663cb809182SSebastian Ott { 664198a5278SSebastian Ott struct zpci_dev *zdev = to_zpci(pdev); 665af0a8a84SSebastian Ott 6669a99649fSSebastian Ott zpci_debug_init_device(zdev, dev_name(&pdev->dev)); 667af0a8a84SSebastian Ott zpci_fmb_enable_device(zdev); 668af0a8a84SSebastian Ott 669d7533232SBjorn Helgaas return pci_enable_resources(pdev, mask); 670af0a8a84SSebastian Ott } 671af0a8a84SSebastian Ott 672cb809182SSebastian Ott void pcibios_disable_device(struct pci_dev *pdev) 673944239c5SSebastian Ott { 674198a5278SSebastian Ott struct zpci_dev *zdev = to_zpci(pdev); 675944239c5SSebastian Ott 676944239c5SSebastian Ott zpci_fmb_disable_device(zdev); 677944239c5SSebastian Ott zpci_debug_exit_device(zdev); 678944239c5SSebastian Ott } 679944239c5SSebastian Ott 68069db3b5eSSebastian Ott #ifdef CONFIG_HIBERNATE_CALLBACKS 68169db3b5eSSebastian Ott static int zpci_restore(struct device *dev) 68269db3b5eSSebastian Ott { 6831803ba2dSSebastian Ott struct pci_dev *pdev = to_pci_dev(dev); 684198a5278SSebastian Ott struct zpci_dev *zdev = to_zpci(pdev); 68569db3b5eSSebastian Ott int ret = 0; 68669db3b5eSSebastian Ott 68769db3b5eSSebastian Ott if (zdev->state != ZPCI_FN_STATE_ONLINE) 68869db3b5eSSebastian Ott goto out; 68969db3b5eSSebastian Ott 69069db3b5eSSebastian Ott ret = clp_enable_fh(zdev, ZPCI_NR_DMA_SPACES); 69169db3b5eSSebastian Ott if (ret) 69269db3b5eSSebastian Ott goto out; 69369db3b5eSSebastian Ott 6941803ba2dSSebastian Ott zpci_map_resources(pdev); 69569eea95cSGerald Schaefer zpci_register_ioat(zdev, 0, zdev->start_dma, zdev->end_dma, 69669db3b5eSSebastian Ott (u64) zdev->dma_table); 69769db3b5eSSebastian Ott 69869db3b5eSSebastian Ott out: 69969db3b5eSSebastian Ott return ret; 70069db3b5eSSebastian Ott } 70169db3b5eSSebastian Ott 70269db3b5eSSebastian Ott static int zpci_freeze(struct device *dev) 70369db3b5eSSebastian Ott { 7041803ba2dSSebastian Ott struct pci_dev *pdev = to_pci_dev(dev); 705198a5278SSebastian Ott struct zpci_dev *zdev = to_zpci(pdev); 70669db3b5eSSebastian Ott 70769db3b5eSSebastian Ott if (zdev->state != ZPCI_FN_STATE_ONLINE) 70869db3b5eSSebastian Ott return 0; 70969db3b5eSSebastian Ott 71069db3b5eSSebastian Ott zpci_unregister_ioat(zdev, 0); 7111803ba2dSSebastian Ott zpci_unmap_resources(pdev); 71269db3b5eSSebastian Ott return clp_disable_fh(zdev); 71369db3b5eSSebastian Ott } 71469db3b5eSSebastian Ott 71569db3b5eSSebastian Ott struct dev_pm_ops pcibios_pm_ops = { 71669db3b5eSSebastian Ott .thaw_noirq = zpci_restore, 71769db3b5eSSebastian Ott .freeze_noirq = zpci_freeze, 71869db3b5eSSebastian Ott .restore_noirq = zpci_restore, 71969db3b5eSSebastian Ott .poweroff_noirq = zpci_freeze, 72069db3b5eSSebastian Ott }; 72169db3b5eSSebastian Ott #endif /* CONFIG_HIBERNATE_CALLBACKS */ 72269db3b5eSSebastian Ott 723cd248341SJan Glauber static int zpci_alloc_domain(struct zpci_dev *zdev) 724cd248341SJan Glauber { 7255c5afd02SSebastian Ott if (zpci_unique_uid) { 7265c5afd02SSebastian Ott zdev->domain = (u16) zdev->uid; 7275c5afd02SSebastian Ott return 0; 7285c5afd02SSebastian Ott } 7295c5afd02SSebastian Ott 730cd248341SJan Glauber spin_lock(&zpci_domain_lock); 731cd248341SJan Glauber zdev->domain = find_first_zero_bit(zpci_domain, ZPCI_NR_DEVICES); 732cd248341SJan Glauber if (zdev->domain == ZPCI_NR_DEVICES) { 733cd248341SJan Glauber spin_unlock(&zpci_domain_lock); 734cd248341SJan Glauber return -ENOSPC; 735cd248341SJan Glauber } 736cd248341SJan Glauber set_bit(zdev->domain, zpci_domain); 737cd248341SJan Glauber spin_unlock(&zpci_domain_lock); 738cd248341SJan Glauber return 0; 739cd248341SJan Glauber } 740cd248341SJan Glauber 741cd248341SJan Glauber static void zpci_free_domain(struct zpci_dev *zdev) 742cd248341SJan Glauber { 7435c5afd02SSebastian Ott if (zpci_unique_uid) 7445c5afd02SSebastian Ott return; 7455c5afd02SSebastian Ott 746cd248341SJan Glauber spin_lock(&zpci_domain_lock); 747cd248341SJan Glauber clear_bit(zdev->domain, zpci_domain); 748cd248341SJan Glauber spin_unlock(&zpci_domain_lock); 749cd248341SJan Glauber } 750cd248341SJan Glauber 7517d594322SSebastian Ott void pcibios_remove_bus(struct pci_bus *bus) 7527d594322SSebastian Ott { 7537d594322SSebastian Ott struct zpci_dev *zdev = get_zdev_by_bus(bus); 7547d594322SSebastian Ott 7557d594322SSebastian Ott zpci_exit_slot(zdev); 7567d594322SSebastian Ott zpci_cleanup_bus_resources(zdev); 7577d594322SSebastian Ott zpci_free_domain(zdev); 7587d594322SSebastian Ott 7597d594322SSebastian Ott spin_lock(&zpci_list_lock); 7607d594322SSebastian Ott list_del(&zdev->entry); 7617d594322SSebastian Ott spin_unlock(&zpci_list_lock); 7627d594322SSebastian Ott 7637d594322SSebastian Ott kfree(zdev); 7647d594322SSebastian Ott } 7657d594322SSebastian Ott 7667d594322SSebastian Ott static int zpci_scan_bus(struct zpci_dev *zdev) 7677d594322SSebastian Ott { 7687d594322SSebastian Ott LIST_HEAD(resources); 7697d594322SSebastian Ott int ret; 7707d594322SSebastian Ott 7717d594322SSebastian Ott ret = zpci_setup_bus_resources(zdev, &resources); 7727d594322SSebastian Ott if (ret) 7732b1df724SSebastian Ott goto error; 7747d594322SSebastian Ott 7757d594322SSebastian Ott zdev->bus = pci_scan_root_bus(NULL, ZPCI_BUS_NR, &pci_root_ops, 7767d594322SSebastian Ott zdev, &resources); 7777d594322SSebastian Ott if (!zdev->bus) { 7782b1df724SSebastian Ott ret = -EIO; 7792b1df724SSebastian Ott goto error; 7807d594322SSebastian Ott } 7817d594322SSebastian Ott zdev->bus->max_bus_speed = zdev->max_bus_speed; 782b97ea289SYijing Wang pci_bus_add_devices(zdev->bus); 7837d594322SSebastian Ott return 0; 7842b1df724SSebastian Ott 7852b1df724SSebastian Ott error: 7862b1df724SSebastian Ott zpci_cleanup_bus_resources(zdev); 7872b1df724SSebastian Ott pci_free_resource_list(&resources); 7882b1df724SSebastian Ott return ret; 7897d594322SSebastian Ott } 7907d594322SSebastian Ott 791a755a45dSJan Glauber int zpci_enable_device(struct zpci_dev *zdev) 792a755a45dSJan Glauber { 793a755a45dSJan Glauber int rc; 794a755a45dSJan Glauber 795a755a45dSJan Glauber rc = clp_enable_fh(zdev, ZPCI_NR_DMA_SPACES); 796a755a45dSJan Glauber if (rc) 797a755a45dSJan Glauber goto out; 798828b35f6SJan Glauber 799828b35f6SJan Glauber rc = zpci_dma_init_device(zdev); 800828b35f6SJan Glauber if (rc) 801828b35f6SJan Glauber goto out_dma; 8020ff70ec8SSebastian Ott 8030ff70ec8SSebastian Ott zdev->state = ZPCI_FN_STATE_ONLINE; 804a755a45dSJan Glauber return 0; 805828b35f6SJan Glauber 806828b35f6SJan Glauber out_dma: 807828b35f6SJan Glauber clp_disable_fh(zdev); 808a755a45dSJan Glauber out: 809a755a45dSJan Glauber return rc; 810a755a45dSJan Glauber } 811a755a45dSJan Glauber EXPORT_SYMBOL_GPL(zpci_enable_device); 812a755a45dSJan Glauber 813cb65a669SSebastian Ott int zpci_disable_device(struct zpci_dev *zdev) 814cb65a669SSebastian Ott { 815cb65a669SSebastian Ott zpci_dma_exit_device(zdev); 816cb65a669SSebastian Ott return clp_disable_fh(zdev); 817cb65a669SSebastian Ott } 818cb65a669SSebastian Ott EXPORT_SYMBOL_GPL(zpci_disable_device); 819cb65a669SSebastian Ott 820cd248341SJan Glauber int zpci_create_device(struct zpci_dev *zdev) 821cd248341SJan Glauber { 822cd248341SJan Glauber int rc; 823cd248341SJan Glauber 824cd248341SJan Glauber rc = zpci_alloc_domain(zdev); 825cd248341SJan Glauber if (rc) 826cd248341SJan Glauber goto out; 827cd248341SJan Glauber 82880ed156aSSebastian Ott mutex_init(&zdev->lock); 8291c21351bSSebastian Ott if (zdev->state == ZPCI_FN_STATE_CONFIGURED) { 8301c21351bSSebastian Ott rc = zpci_enable_device(zdev); 831cd248341SJan Glauber if (rc) 8321c21351bSSebastian Ott goto out_free; 8331c21351bSSebastian Ott } 8341c21351bSSebastian Ott rc = zpci_scan_bus(zdev); 8351c21351bSSebastian Ott if (rc) 8361c21351bSSebastian Ott goto out_disable; 837cd248341SJan Glauber 83857b5918cSSebastian Ott spin_lock(&zpci_list_lock); 839cd248341SJan Glauber list_add_tail(&zdev->entry, &zpci_list); 84057b5918cSSebastian Ott spin_unlock(&zpci_list_lock); 841cd248341SJan Glauber 84267f43f38SSebastian Ott zpci_init_slot(zdev); 84367f43f38SSebastian Ott 844cd248341SJan Glauber return 0; 845cd248341SJan Glauber 8461c21351bSSebastian Ott out_disable: 8471c21351bSSebastian Ott if (zdev->state == ZPCI_FN_STATE_ONLINE) 8481c21351bSSebastian Ott zpci_disable_device(zdev); 8491c21351bSSebastian Ott out_free: 850cd248341SJan Glauber zpci_free_domain(zdev); 851cd248341SJan Glauber out: 852cd248341SJan Glauber return rc; 853cd248341SJan Glauber } 854cd248341SJan Glauber 855cd248341SJan Glauber void zpci_stop_device(struct zpci_dev *zdev) 856cd248341SJan Glauber { 857828b35f6SJan Glauber zpci_dma_exit_device(zdev); 858cd248341SJan Glauber /* 859cd248341SJan Glauber * Note: SCLP disables fh via set-pci-fn so don't 860cd248341SJan Glauber * do that here. 861cd248341SJan Glauber */ 862cd248341SJan Glauber } 863cd248341SJan Glauber EXPORT_SYMBOL_GPL(zpci_stop_device); 864cd248341SJan Glauber 865bd3a1725SMartin Schwidefsky int zpci_report_error(struct pci_dev *pdev, 866bd3a1725SMartin Schwidefsky struct zpci_report_error_header *report) 867bd3a1725SMartin Schwidefsky { 868bd3a1725SMartin Schwidefsky struct zpci_dev *zdev = to_zpci(pdev); 869bd3a1725SMartin Schwidefsky 870bd3a1725SMartin Schwidefsky return sclp_pci_report(report, zdev->fh, zdev->fid); 871bd3a1725SMartin Schwidefsky } 872bd3a1725SMartin Schwidefsky EXPORT_SYMBOL(zpci_report_error); 873bd3a1725SMartin Schwidefsky 874cd248341SJan Glauber static inline int barsize(u8 size) 875cd248341SJan Glauber { 876cd248341SJan Glauber return (size) ? (1 << size) >> 10 : 0; 877cd248341SJan Glauber } 878cd248341SJan Glauber 879cd248341SJan Glauber static int zpci_mem_init(void) 880cd248341SJan Glauber { 88180c544deSSebastian Ott BUILD_BUG_ON(!is_power_of_2(__alignof__(struct zpci_fmb)) || 88280c544deSSebastian Ott __alignof__(struct zpci_fmb) < sizeof(struct zpci_fmb)); 88380c544deSSebastian Ott 884d0b08853SJan Glauber zdev_fmb_cache = kmem_cache_create("PCI_FMB_cache", sizeof(struct zpci_fmb), 88580c544deSSebastian Ott __alignof__(struct zpci_fmb), 0, NULL); 886d0b08853SJan Glauber if (!zdev_fmb_cache) 887c506fff3SSebastian Ott goto error_fmb; 888d0b08853SJan Glauber 889c506fff3SSebastian Ott zpci_iomap_start = kcalloc(ZPCI_IOMAP_ENTRIES, 890c506fff3SSebastian Ott sizeof(*zpci_iomap_start), GFP_KERNEL); 891cd248341SJan Glauber if (!zpci_iomap_start) 8929a4da8a5SJan Glauber goto error_iomap; 893cd248341SJan Glauber 894c506fff3SSebastian Ott zpci_iomap_bitmap = kcalloc(BITS_TO_LONGS(ZPCI_IOMAP_ENTRIES), 895c506fff3SSebastian Ott sizeof(*zpci_iomap_bitmap), GFP_KERNEL); 896c506fff3SSebastian Ott if (!zpci_iomap_bitmap) 897c506fff3SSebastian Ott goto error_iomap_bitmap; 898c506fff3SSebastian Ott 899c506fff3SSebastian Ott return 0; 900c506fff3SSebastian Ott error_iomap_bitmap: 901c506fff3SSebastian Ott kfree(zpci_iomap_start); 9029a4da8a5SJan Glauber error_iomap: 903d0b08853SJan Glauber kmem_cache_destroy(zdev_fmb_cache); 904c506fff3SSebastian Ott error_fmb: 905cd248341SJan Glauber return -ENOMEM; 906cd248341SJan Glauber } 907cd248341SJan Glauber 908cd248341SJan Glauber static void zpci_mem_exit(void) 909cd248341SJan Glauber { 910c506fff3SSebastian Ott kfree(zpci_iomap_bitmap); 911cd248341SJan Glauber kfree(zpci_iomap_start); 912d0b08853SJan Glauber kmem_cache_destroy(zdev_fmb_cache); 913cd248341SJan Glauber } 914cd248341SJan Glauber 915257608fbSSebastian Ott static unsigned int s390_pci_probe = 1; 916aa3b7c29SSebastian Ott static unsigned int s390_pci_initialized; 917cd248341SJan Glauber 918cd248341SJan Glauber char * __init pcibios_setup(char *str) 919cd248341SJan Glauber { 920257608fbSSebastian Ott if (!strcmp(str, "off")) { 921257608fbSSebastian Ott s390_pci_probe = 0; 922cd248341SJan Glauber return NULL; 923cd248341SJan Glauber } 924cd248341SJan Glauber return str; 925cd248341SJan Glauber } 926cd248341SJan Glauber 927aa3b7c29SSebastian Ott bool zpci_is_enabled(void) 928aa3b7c29SSebastian Ott { 929aa3b7c29SSebastian Ott return s390_pci_initialized; 930aa3b7c29SSebastian Ott } 931aa3b7c29SSebastian Ott 932cd248341SJan Glauber static int __init pci_base_init(void) 933cd248341SJan Glauber { 934cd248341SJan Glauber int rc; 935cd248341SJan Glauber 9361e5635d1SHeiko Carstens if (!s390_pci_probe) 937cd248341SJan Glauber return 0; 938cd248341SJan Glauber 93986cd741bSHeiko Carstens if (!test_facility(69) || !test_facility(71) || !test_facility(72)) 940cd248341SJan Glauber return 0; 941cd248341SJan Glauber 942d0b08853SJan Glauber rc = zpci_debug_init(); 943d0b08853SJan Glauber if (rc) 9441f44a225SMartin Schwidefsky goto out; 945d0b08853SJan Glauber 946cd248341SJan Glauber rc = zpci_mem_init(); 947cd248341SJan Glauber if (rc) 948cd248341SJan Glauber goto out_mem; 949cd248341SJan Glauber 9509a4da8a5SJan Glauber rc = zpci_irq_init(); 9519a4da8a5SJan Glauber if (rc) 9529a4da8a5SJan Glauber goto out_irq; 9539a4da8a5SJan Glauber 954828b35f6SJan Glauber rc = zpci_dma_init(); 955828b35f6SJan Glauber if (rc) 956828b35f6SJan Glauber goto out_dma; 957828b35f6SJan Glauber 9581d578966SSebastian Ott rc = clp_scan_pci_devices(); 959a755a45dSJan Glauber if (rc) 960a755a45dSJan Glauber goto out_find; 961a755a45dSJan Glauber 962aa3b7c29SSebastian Ott s390_pci_initialized = 1; 963cd248341SJan Glauber return 0; 964cd248341SJan Glauber 965a755a45dSJan Glauber out_find: 966828b35f6SJan Glauber zpci_dma_exit(); 967828b35f6SJan Glauber out_dma: 9689a4da8a5SJan Glauber zpci_irq_exit(); 9699a4da8a5SJan Glauber out_irq: 970cd248341SJan Glauber zpci_mem_exit(); 971cd248341SJan Glauber out_mem: 972d0b08853SJan Glauber zpci_debug_exit(); 9731f44a225SMartin Schwidefsky out: 974cd248341SJan Glauber return rc; 975cd248341SJan Glauber } 97667f43f38SSebastian Ott subsys_initcall_sync(pci_base_init); 97757b5918cSSebastian Ott 97857b5918cSSebastian Ott void zpci_rescan(void) 97957b5918cSSebastian Ott { 980aa3b7c29SSebastian Ott if (zpci_is_enabled()) 98157b5918cSSebastian Ott clp_rescan_pci_devices_simple(); 98257b5918cSSebastian Ott } 983