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 18cd248341SJan Glauber #define COMPONENT "zPCI" 19cd248341SJan Glauber #define pr_fmt(fmt) 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 */ 48*67f43f38SSebastian Ott static LIST_HEAD(zpci_list); 49*67f43f38SSebastian Ott static DEFINE_MUTEX(zpci_list_lock); 501f44a225SMartin Schwidefsky 511f44a225SMartin Schwidefsky static void zpci_enable_irq(struct irq_data *data); 521f44a225SMartin Schwidefsky static void zpci_disable_irq(struct irq_data *data); 531f44a225SMartin Schwidefsky 541f44a225SMartin Schwidefsky static struct irq_chip zpci_irq_chip = { 551f44a225SMartin Schwidefsky .name = "zPCI", 561f44a225SMartin Schwidefsky .irq_unmask = zpci_enable_irq, 571f44a225SMartin Schwidefsky .irq_mask = zpci_disable_irq, 581f44a225SMartin Schwidefsky }; 591f44a225SMartin Schwidefsky 60cd248341SJan Glauber static DECLARE_BITMAP(zpci_domain, ZPCI_NR_DEVICES); 61cd248341SJan Glauber static DEFINE_SPINLOCK(zpci_domain_lock); 62cd248341SJan Glauber 635d0d8f43SMartin Schwidefsky static struct airq_iv *zpci_aisb_iv; 641f44a225SMartin Schwidefsky static struct airq_iv *zpci_aibv[ZPCI_NR_DEVICES]; 659a4da8a5SJan Glauber 66f4eae94fSMartin Schwidefsky /* Adapter interrupt definitions */ 67f4eae94fSMartin Schwidefsky static void zpci_irq_handler(struct airq_struct *airq); 68f4eae94fSMartin Schwidefsky 69f4eae94fSMartin Schwidefsky static struct airq_struct zpci_airq = { 70f4eae94fSMartin Schwidefsky .handler = zpci_irq_handler, 71f4eae94fSMartin Schwidefsky .isc = PCI_ISC, 72f4eae94fSMartin Schwidefsky }; 739a4da8a5SJan Glauber 74cd248341SJan Glauber /* I/O Map */ 75cd248341SJan Glauber static DEFINE_SPINLOCK(zpci_iomap_lock); 76cd248341SJan Glauber static DECLARE_BITMAP(zpci_iomap, ZPCI_IOMAP_MAX_ENTRIES); 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(struct pci_dev *pdev) 83cd248341SJan Glauber { 84cd248341SJan Glauber return (struct zpci_dev *) pdev->sysdata; 85cd248341SJan Glauber } 86cd248341SJan Glauber 87cd248341SJan Glauber struct zpci_dev *get_zdev_by_fid(u32 fid) 88cd248341SJan Glauber { 89cd248341SJan Glauber struct zpci_dev *tmp, *zdev = NULL; 90cd248341SJan Glauber 91cd248341SJan Glauber mutex_lock(&zpci_list_lock); 92cd248341SJan Glauber list_for_each_entry(tmp, &zpci_list, entry) { 93cd248341SJan Glauber if (tmp->fid == fid) { 94cd248341SJan Glauber zdev = tmp; 95cd248341SJan Glauber break; 96cd248341SJan Glauber } 97cd248341SJan Glauber } 98cd248341SJan Glauber mutex_unlock(&zpci_list_lock); 99cd248341SJan Glauber return zdev; 100cd248341SJan Glauber } 101cd248341SJan Glauber 102cd248341SJan Glauber bool zpci_fid_present(u32 fid) 103cd248341SJan Glauber { 104cd248341SJan Glauber return (get_zdev_by_fid(fid) != NULL) ? true : false; 105cd248341SJan Glauber } 106cd248341SJan Glauber 107cd248341SJan Glauber static struct zpci_dev *get_zdev_by_bus(struct pci_bus *bus) 108cd248341SJan Glauber { 109cd248341SJan Glauber return (bus && bus->sysdata) ? (struct zpci_dev *) bus->sysdata : NULL; 110cd248341SJan Glauber } 111cd248341SJan Glauber 112cd248341SJan Glauber int pci_domain_nr(struct pci_bus *bus) 113cd248341SJan Glauber { 114cd248341SJan Glauber return ((struct zpci_dev *) bus->sysdata)->domain; 115cd248341SJan Glauber } 116cd248341SJan Glauber EXPORT_SYMBOL_GPL(pci_domain_nr); 117cd248341SJan Glauber 118cd248341SJan Glauber int pci_proc_domain(struct pci_bus *bus) 119cd248341SJan Glauber { 120cd248341SJan Glauber return pci_domain_nr(bus); 121cd248341SJan Glauber } 122cd248341SJan Glauber EXPORT_SYMBOL_GPL(pci_proc_domain); 123cd248341SJan Glauber 1249a4da8a5SJan Glauber /* Modify PCI: Register adapter interruptions */ 1255d0d8f43SMartin Schwidefsky static int zpci_set_airq(struct zpci_dev *zdev) 1269a4da8a5SJan Glauber { 1279a4da8a5SJan Glauber u64 req = ZPCI_CREATE_REQ(zdev->fh, 0, ZPCI_MOD_FC_REG_INT); 1289a4da8a5SJan Glauber struct zpci_fib *fib; 1299a4da8a5SJan Glauber int rc; 1309a4da8a5SJan Glauber 1319a4da8a5SJan Glauber fib = (void *) get_zeroed_page(GFP_KERNEL); 1329a4da8a5SJan Glauber if (!fib) 1339a4da8a5SJan Glauber return -ENOMEM; 1349a4da8a5SJan Glauber 1359a4da8a5SJan Glauber fib->isc = PCI_ISC; 1369a4da8a5SJan Glauber fib->sum = 1; /* enable summary notifications */ 1375d0d8f43SMartin Schwidefsky fib->noi = airq_iv_end(zdev->aibv); 1385d0d8f43SMartin Schwidefsky fib->aibv = (unsigned long) zdev->aibv->vector; 1395d0d8f43SMartin Schwidefsky fib->aibvo = 0; /* each zdev has its own interrupt vector */ 1405d0d8f43SMartin Schwidefsky fib->aisb = (unsigned long) zpci_aisb_iv->vector + (zdev->aisb/64)*8; 1415d0d8f43SMartin Schwidefsky fib->aisbo = zdev->aisb & 63; 1429a4da8a5SJan Glauber 1439389339fSMartin Schwidefsky rc = zpci_mod_fc(req, fib); 1449a4da8a5SJan Glauber pr_debug("%s mpcifc returned noi: %d\n", __func__, fib->noi); 1459a4da8a5SJan Glauber 1469a4da8a5SJan Glauber free_page((unsigned long) fib); 1479a4da8a5SJan Glauber return rc; 1489a4da8a5SJan Glauber } 1499a4da8a5SJan Glauber 1509a4da8a5SJan Glauber struct mod_pci_args { 1519a4da8a5SJan Glauber u64 base; 1529a4da8a5SJan Glauber u64 limit; 1539a4da8a5SJan Glauber u64 iota; 154d0b08853SJan Glauber u64 fmb_addr; 1559a4da8a5SJan Glauber }; 1569a4da8a5SJan Glauber 1579a4da8a5SJan Glauber static int mod_pci(struct zpci_dev *zdev, int fn, u8 dmaas, struct mod_pci_args *args) 1589a4da8a5SJan Glauber { 1599a4da8a5SJan Glauber u64 req = ZPCI_CREATE_REQ(zdev->fh, dmaas, fn); 1609a4da8a5SJan Glauber struct zpci_fib *fib; 1619a4da8a5SJan Glauber int rc; 1629a4da8a5SJan Glauber 1639a4da8a5SJan Glauber /* The FIB must be available even if it's not used */ 1649a4da8a5SJan Glauber fib = (void *) get_zeroed_page(GFP_KERNEL); 1659a4da8a5SJan Glauber if (!fib) 1669a4da8a5SJan Glauber return -ENOMEM; 1679a4da8a5SJan Glauber 1689a4da8a5SJan Glauber fib->pba = args->base; 1699a4da8a5SJan Glauber fib->pal = args->limit; 1709a4da8a5SJan Glauber fib->iota = args->iota; 171d0b08853SJan Glauber fib->fmb_addr = args->fmb_addr; 1729a4da8a5SJan Glauber 1739389339fSMartin Schwidefsky rc = zpci_mod_fc(req, fib); 1749a4da8a5SJan Glauber free_page((unsigned long) fib); 1759a4da8a5SJan Glauber return rc; 1769a4da8a5SJan Glauber } 1779a4da8a5SJan Glauber 178828b35f6SJan Glauber /* Modify PCI: Register I/O address translation parameters */ 179828b35f6SJan Glauber int zpci_register_ioat(struct zpci_dev *zdev, u8 dmaas, 180828b35f6SJan Glauber u64 base, u64 limit, u64 iota) 181828b35f6SJan Glauber { 182d0b08853SJan Glauber struct mod_pci_args args = { base, limit, iota, 0 }; 183828b35f6SJan Glauber 184828b35f6SJan Glauber WARN_ON_ONCE(iota & 0x3fff); 185828b35f6SJan Glauber args.iota |= ZPCI_IOTA_RTTO_FLAG; 186828b35f6SJan Glauber return mod_pci(zdev, ZPCI_MOD_FC_REG_IOAT, dmaas, &args); 187828b35f6SJan Glauber } 188828b35f6SJan Glauber 189828b35f6SJan Glauber /* Modify PCI: Unregister I/O address translation parameters */ 190828b35f6SJan Glauber int zpci_unregister_ioat(struct zpci_dev *zdev, u8 dmaas) 191828b35f6SJan Glauber { 192d0b08853SJan Glauber struct mod_pci_args args = { 0, 0, 0, 0 }; 193828b35f6SJan Glauber 194828b35f6SJan Glauber return mod_pci(zdev, ZPCI_MOD_FC_DEREG_IOAT, dmaas, &args); 195828b35f6SJan Glauber } 196828b35f6SJan Glauber 1979a4da8a5SJan Glauber /* Modify PCI: Unregister adapter interruptions */ 1985d0d8f43SMartin Schwidefsky static int zpci_clear_airq(struct zpci_dev *zdev) 1999a4da8a5SJan Glauber { 200d0b08853SJan Glauber struct mod_pci_args args = { 0, 0, 0, 0 }; 2019a4da8a5SJan Glauber 2029a4da8a5SJan Glauber return mod_pci(zdev, ZPCI_MOD_FC_DEREG_INT, 0, &args); 2039a4da8a5SJan Glauber } 2049a4da8a5SJan Glauber 205d0b08853SJan Glauber /* Modify PCI: Set PCI function measurement parameters */ 206d0b08853SJan Glauber int zpci_fmb_enable_device(struct zpci_dev *zdev) 207d0b08853SJan Glauber { 208d0b08853SJan Glauber struct mod_pci_args args = { 0, 0, 0, 0 }; 209d0b08853SJan Glauber 210d0b08853SJan Glauber if (zdev->fmb) 211d0b08853SJan Glauber return -EINVAL; 212d0b08853SJan Glauber 21308b42124SWei Yongjun zdev->fmb = kmem_cache_zalloc(zdev_fmb_cache, GFP_KERNEL); 214d0b08853SJan Glauber if (!zdev->fmb) 215d0b08853SJan Glauber return -ENOMEM; 216d0b08853SJan Glauber WARN_ON((u64) zdev->fmb & 0xf); 217d0b08853SJan Glauber 218d0b08853SJan Glauber args.fmb_addr = virt_to_phys(zdev->fmb); 219d0b08853SJan Glauber return mod_pci(zdev, ZPCI_MOD_FC_SET_MEASURE, 0, &args); 220d0b08853SJan Glauber } 221d0b08853SJan Glauber 222d0b08853SJan Glauber /* Modify PCI: Disable PCI function measurement */ 223d0b08853SJan Glauber int zpci_fmb_disable_device(struct zpci_dev *zdev) 224d0b08853SJan Glauber { 225d0b08853SJan Glauber struct mod_pci_args args = { 0, 0, 0, 0 }; 226d0b08853SJan Glauber int rc; 227d0b08853SJan Glauber 228d0b08853SJan Glauber if (!zdev->fmb) 229d0b08853SJan Glauber return -EINVAL; 230d0b08853SJan Glauber 231d0b08853SJan Glauber /* Function measurement is disabled if fmb address is zero */ 232d0b08853SJan Glauber rc = mod_pci(zdev, ZPCI_MOD_FC_SET_MEASURE, 0, &args); 233d0b08853SJan Glauber 234d0b08853SJan Glauber kmem_cache_free(zdev_fmb_cache, zdev->fmb); 235d0b08853SJan Glauber zdev->fmb = NULL; 236d0b08853SJan Glauber return rc; 237d0b08853SJan Glauber } 238d0b08853SJan Glauber 239cd248341SJan Glauber #define ZPCI_PCIAS_CFGSPC 15 240cd248341SJan Glauber 241cd248341SJan Glauber static int zpci_cfg_load(struct zpci_dev *zdev, int offset, u32 *val, u8 len) 242cd248341SJan Glauber { 243cd248341SJan Glauber u64 req = ZPCI_CREATE_REQ(zdev->fh, ZPCI_PCIAS_CFGSPC, len); 244cd248341SJan Glauber u64 data; 245cd248341SJan Glauber int rc; 246cd248341SJan Glauber 2479389339fSMartin Schwidefsky rc = zpci_load(&data, req, offset); 248b170bad4SSebastian Ott if (!rc) { 249cd248341SJan Glauber data = data << ((8 - len) * 8); 250cd248341SJan Glauber data = le64_to_cpu(data); 251cd248341SJan Glauber *val = (u32) data; 252b170bad4SSebastian Ott } else 253cd248341SJan Glauber *val = 0xffffffff; 254cd248341SJan Glauber return rc; 255cd248341SJan Glauber } 256cd248341SJan Glauber 257cd248341SJan Glauber static int zpci_cfg_store(struct zpci_dev *zdev, int offset, u32 val, u8 len) 258cd248341SJan Glauber { 259cd248341SJan Glauber u64 req = ZPCI_CREATE_REQ(zdev->fh, ZPCI_PCIAS_CFGSPC, len); 260cd248341SJan Glauber u64 data = val; 261cd248341SJan Glauber int rc; 262cd248341SJan Glauber 263cd248341SJan Glauber data = cpu_to_le64(data); 264cd248341SJan Glauber data = data >> ((8 - len) * 8); 2659389339fSMartin Schwidefsky rc = zpci_store(data, req, offset); 266cd248341SJan Glauber return rc; 267cd248341SJan Glauber } 268cd248341SJan Glauber 2691f44a225SMartin Schwidefsky static int zpci_msi_set_mask_bits(struct msi_desc *msi, u32 mask, u32 flag) 2709a4da8a5SJan Glauber { 2711f44a225SMartin Schwidefsky int offset, pos; 2721f44a225SMartin Schwidefsky u32 mask_bits; 2731f44a225SMartin Schwidefsky 2741f44a225SMartin Schwidefsky if (msi->msi_attrib.is_msix) { 2751f44a225SMartin Schwidefsky offset = msi->msi_attrib.entry_nr * PCI_MSIX_ENTRY_SIZE + 2761f44a225SMartin Schwidefsky PCI_MSIX_ENTRY_VECTOR_CTRL; 2771f44a225SMartin Schwidefsky msi->masked = readl(msi->mask_base + offset); 2781f44a225SMartin Schwidefsky writel(flag, msi->mask_base + offset); 2791f44a225SMartin Schwidefsky } else if (msi->msi_attrib.maskbit) { 2801f44a225SMartin Schwidefsky pos = (long) msi->mask_base; 2811f44a225SMartin Schwidefsky pci_read_config_dword(msi->dev, pos, &mask_bits); 2821f44a225SMartin Schwidefsky mask_bits &= ~(mask); 2831f44a225SMartin Schwidefsky mask_bits |= flag & mask; 2841f44a225SMartin Schwidefsky pci_write_config_dword(msi->dev, pos, mask_bits); 2851f44a225SMartin Schwidefsky } else 2861f44a225SMartin Schwidefsky return 0; 2871f44a225SMartin Schwidefsky 2881f44a225SMartin Schwidefsky msi->msi_attrib.maskbit = !!flag; 2891f44a225SMartin Schwidefsky return 1; 2901f44a225SMartin Schwidefsky } 2911f44a225SMartin Schwidefsky 2921f44a225SMartin Schwidefsky static void zpci_enable_irq(struct irq_data *data) 2931f44a225SMartin Schwidefsky { 2941f44a225SMartin Schwidefsky struct msi_desc *msi = irq_get_msi_desc(data->irq); 2959a4da8a5SJan Glauber 2969a4da8a5SJan Glauber zpci_msi_set_mask_bits(msi, 1, 0); 2979a4da8a5SJan Glauber } 2989a4da8a5SJan Glauber 2991f44a225SMartin Schwidefsky static void zpci_disable_irq(struct irq_data *data) 3009a4da8a5SJan Glauber { 3011f44a225SMartin Schwidefsky struct msi_desc *msi = irq_get_msi_desc(data->irq); 3029a4da8a5SJan Glauber 3039a4da8a5SJan Glauber zpci_msi_set_mask_bits(msi, 1, 1); 3049a4da8a5SJan Glauber } 3059a4da8a5SJan Glauber 306b881bc46SGreg Kroah-Hartman void pcibios_fixup_bus(struct pci_bus *bus) 307cd248341SJan Glauber { 308cd248341SJan Glauber } 309cd248341SJan Glauber 310cd248341SJan Glauber resource_size_t pcibios_align_resource(void *data, const struct resource *res, 311cd248341SJan Glauber resource_size_t size, 312cd248341SJan Glauber resource_size_t align) 313cd248341SJan Glauber { 314cd248341SJan Glauber return 0; 315cd248341SJan Glauber } 316cd248341SJan Glauber 31787bc359bSJan Glauber /* combine single writes by using store-block insn */ 31887bc359bSJan Glauber void __iowrite64_copy(void __iomem *to, const void *from, size_t count) 31987bc359bSJan Glauber { 32087bc359bSJan Glauber zpci_memcpy_toio(to, from, count); 32187bc359bSJan Glauber } 32287bc359bSJan Glauber 323cd248341SJan Glauber /* Create a virtual mapping cookie for a PCI BAR */ 324cd248341SJan Glauber void __iomem *pci_iomap(struct pci_dev *pdev, int bar, unsigned long max) 325cd248341SJan Glauber { 326cd248341SJan Glauber struct zpci_dev *zdev = get_zdev(pdev); 327cd248341SJan Glauber u64 addr; 328cd248341SJan Glauber int idx; 329cd248341SJan Glauber 330cd248341SJan Glauber if ((bar & 7) != bar) 331cd248341SJan Glauber return NULL; 332cd248341SJan Glauber 333cd248341SJan Glauber idx = zdev->bars[bar].map_idx; 334cd248341SJan Glauber spin_lock(&zpci_iomap_lock); 335cd248341SJan Glauber zpci_iomap_start[idx].fh = zdev->fh; 336cd248341SJan Glauber zpci_iomap_start[idx].bar = bar; 337cd248341SJan Glauber spin_unlock(&zpci_iomap_lock); 338cd248341SJan Glauber 339cd248341SJan Glauber addr = ZPCI_IOMAP_ADDR_BASE | ((u64) idx << 48); 340cd248341SJan Glauber return (void __iomem *) addr; 341cd248341SJan Glauber } 342cd248341SJan Glauber EXPORT_SYMBOL_GPL(pci_iomap); 343cd248341SJan Glauber 344cd248341SJan Glauber void pci_iounmap(struct pci_dev *pdev, void __iomem *addr) 345cd248341SJan Glauber { 346cd248341SJan Glauber unsigned int idx; 347cd248341SJan Glauber 348cd248341SJan Glauber idx = (((__force u64) addr) & ~ZPCI_IOMAP_ADDR_BASE) >> 48; 349cd248341SJan Glauber spin_lock(&zpci_iomap_lock); 350cd248341SJan Glauber zpci_iomap_start[idx].fh = 0; 351cd248341SJan Glauber zpci_iomap_start[idx].bar = 0; 352cd248341SJan Glauber spin_unlock(&zpci_iomap_lock); 353cd248341SJan Glauber } 354cd248341SJan Glauber EXPORT_SYMBOL_GPL(pci_iounmap); 355cd248341SJan Glauber 356cd248341SJan Glauber static int pci_read(struct pci_bus *bus, unsigned int devfn, int where, 357cd248341SJan Glauber int size, u32 *val) 358cd248341SJan Glauber { 359cd248341SJan Glauber struct zpci_dev *zdev = get_zdev_by_bus(bus); 3602c3700bbSSebastian Ott int ret; 361cd248341SJan Glauber 362cd248341SJan Glauber if (!zdev || devfn != ZPCI_DEVFN) 3632c3700bbSSebastian Ott ret = -ENODEV; 3642c3700bbSSebastian Ott else 3652c3700bbSSebastian Ott ret = zpci_cfg_load(zdev, where, val, size); 3662c3700bbSSebastian Ott 3672c3700bbSSebastian Ott return ret; 368cd248341SJan Glauber } 369cd248341SJan Glauber 370cd248341SJan Glauber static int pci_write(struct pci_bus *bus, unsigned int devfn, int where, 371cd248341SJan Glauber int size, u32 val) 372cd248341SJan Glauber { 373cd248341SJan Glauber struct zpci_dev *zdev = get_zdev_by_bus(bus); 3742c3700bbSSebastian Ott int ret; 375cd248341SJan Glauber 376cd248341SJan Glauber if (!zdev || devfn != ZPCI_DEVFN) 3772c3700bbSSebastian Ott ret = -ENODEV; 3782c3700bbSSebastian Ott else 3792c3700bbSSebastian Ott ret = zpci_cfg_store(zdev, where, val, size); 3802c3700bbSSebastian Ott 3812c3700bbSSebastian Ott return ret; 382cd248341SJan Glauber } 383cd248341SJan Glauber 384cd248341SJan Glauber static struct pci_ops pci_root_ops = { 385cd248341SJan Glauber .read = pci_read, 386cd248341SJan Glauber .write = pci_write, 387cd248341SJan Glauber }; 388cd248341SJan Glauber 389f4eae94fSMartin Schwidefsky static void zpci_irq_handler(struct airq_struct *airq) 3909a4da8a5SJan Glauber { 3915d0d8f43SMartin Schwidefsky unsigned long si, ai; 3921f44a225SMartin Schwidefsky struct airq_iv *aibv; 3935d0d8f43SMartin Schwidefsky int irqs_on = 0; 3949a4da8a5SJan Glauber 395420f42ecSHeiko Carstens inc_irq_stat(IRQIO_PCI); 3965d0d8f43SMartin Schwidefsky for (si = 0;;) { 3975d0d8f43SMartin Schwidefsky /* Scan adapter summary indicator bit vector */ 3985d0d8f43SMartin Schwidefsky si = airq_iv_scan(zpci_aisb_iv, si, airq_iv_end(zpci_aisb_iv)); 3995d0d8f43SMartin Schwidefsky if (si == -1UL) { 4005d0d8f43SMartin Schwidefsky if (irqs_on++) 4015d0d8f43SMartin Schwidefsky /* End of second scan with interrupts on. */ 4025d0d8f43SMartin Schwidefsky break; 4035d0d8f43SMartin Schwidefsky /* First scan complete, reenable interrupts. */ 4049389339fSMartin Schwidefsky zpci_set_irq_ctrl(SIC_IRQ_MODE_SINGLE, NULL, PCI_ISC); 4055d0d8f43SMartin Schwidefsky si = 0; 4065d0d8f43SMartin Schwidefsky continue; 4079a4da8a5SJan Glauber } 4085d0d8f43SMartin Schwidefsky 4095d0d8f43SMartin Schwidefsky /* Scan the adapter interrupt vector for this device. */ 4101f44a225SMartin Schwidefsky aibv = zpci_aibv[si]; 4115d0d8f43SMartin Schwidefsky for (ai = 0;;) { 4121f44a225SMartin Schwidefsky ai = airq_iv_scan(aibv, ai, airq_iv_end(aibv)); 4135d0d8f43SMartin Schwidefsky if (ai == -1UL) 4145d0d8f43SMartin Schwidefsky break; 4155d0d8f43SMartin Schwidefsky inc_irq_stat(IRQIO_MSI); 4161f44a225SMartin Schwidefsky airq_iv_lock(aibv, ai); 4171f44a225SMartin Schwidefsky generic_handle_irq(airq_iv_get_data(aibv, ai)); 4181f44a225SMartin Schwidefsky airq_iv_unlock(aibv, ai); 4195d0d8f43SMartin Schwidefsky } 4205d0d8f43SMartin Schwidefsky } 4215d0d8f43SMartin Schwidefsky } 4225d0d8f43SMartin Schwidefsky 4235d0d8f43SMartin Schwidefsky int arch_setup_msi_irqs(struct pci_dev *pdev, int nvec, int type) 4249a4da8a5SJan Glauber { 4259a4da8a5SJan Glauber struct zpci_dev *zdev = get_zdev(pdev); 4261f44a225SMartin Schwidefsky unsigned int hwirq, irq, msi_vecs; 4275d0d8f43SMartin Schwidefsky unsigned long aisb; 4289a4da8a5SJan Glauber struct msi_desc *msi; 4291f44a225SMartin Schwidefsky struct msi_msg msg; 4309a4da8a5SJan Glauber int rc; 4319a4da8a5SJan Glauber 4325d0d8f43SMartin Schwidefsky pr_debug("%s: requesting %d MSI-X interrupts...", __func__, nvec); 4335d0d8f43SMartin Schwidefsky if (type != PCI_CAP_ID_MSIX && type != PCI_CAP_ID_MSI) 4345d0d8f43SMartin Schwidefsky return -EINVAL; 4355d0d8f43SMartin Schwidefsky msi_vecs = min(nvec, ZPCI_MSI_VEC_MAX); 4361f44a225SMartin Schwidefsky msi_vecs = min_t(unsigned int, msi_vecs, CONFIG_PCI_NR_MSI); 4379a4da8a5SJan Glauber 4385d0d8f43SMartin Schwidefsky /* Allocate adapter summary indicator bit */ 4395d0d8f43SMartin Schwidefsky rc = -EIO; 4405d0d8f43SMartin Schwidefsky aisb = airq_iv_alloc_bit(zpci_aisb_iv); 4415d0d8f43SMartin Schwidefsky if (aisb == -1UL) 4425d0d8f43SMartin Schwidefsky goto out; 4439a4da8a5SJan Glauber zdev->aisb = aisb; 4449a4da8a5SJan Glauber 4455d0d8f43SMartin Schwidefsky /* Create adapter interrupt vector */ 4465d0d8f43SMartin Schwidefsky rc = -ENOMEM; 4471f44a225SMartin Schwidefsky zdev->aibv = airq_iv_create(msi_vecs, AIRQ_IV_DATA | AIRQ_IV_BITLOCK); 4485d0d8f43SMartin Schwidefsky if (!zdev->aibv) 4495d0d8f43SMartin Schwidefsky goto out_si; 4509a4da8a5SJan Glauber 4515d0d8f43SMartin Schwidefsky /* Wire up shortcut pointer */ 4521f44a225SMartin Schwidefsky zpci_aibv[aisb] = zdev->aibv; 4535d0d8f43SMartin Schwidefsky 4541f44a225SMartin Schwidefsky /* Request MSI interrupts */ 4551f44a225SMartin Schwidefsky hwirq = 0; 4569a4da8a5SJan Glauber list_for_each_entry(msi, &pdev->msi_list, list) { 4571f44a225SMartin Schwidefsky rc = -EIO; 4581f44a225SMartin Schwidefsky irq = irq_alloc_desc(0); /* Alloc irq on node 0 */ 4591f44a225SMartin Schwidefsky if (irq == NO_IRQ) 4601f44a225SMartin Schwidefsky goto out_msi; 4611f44a225SMartin Schwidefsky rc = irq_set_msi_desc(irq, msi); 4629a4da8a5SJan Glauber if (rc) 4635d0d8f43SMartin Schwidefsky goto out_msi; 4641f44a225SMartin Schwidefsky irq_set_chip_and_handler(irq, &zpci_irq_chip, 4651f44a225SMartin Schwidefsky handle_simple_irq); 4661f44a225SMartin Schwidefsky msg.data = hwirq; 4671f44a225SMartin Schwidefsky msg.address_lo = zdev->msi_addr & 0xffffffff; 4681f44a225SMartin Schwidefsky msg.address_hi = zdev->msi_addr >> 32; 4691f44a225SMartin Schwidefsky write_msi_msg(irq, &msg); 4701f44a225SMartin Schwidefsky airq_iv_set_data(zdev->aibv, hwirq, irq); 4711f44a225SMartin Schwidefsky hwirq++; 4729a4da8a5SJan Glauber } 4739a4da8a5SJan Glauber 4745d0d8f43SMartin Schwidefsky /* Enable adapter interrupts */ 4755d0d8f43SMartin Schwidefsky rc = zpci_set_airq(zdev); 4765d0d8f43SMartin Schwidefsky if (rc) 4775d0d8f43SMartin Schwidefsky goto out_msi; 4785d0d8f43SMartin Schwidefsky 4795d0d8f43SMartin Schwidefsky return (msi_vecs == nvec) ? 0 : msi_vecs; 4805d0d8f43SMartin Schwidefsky 4815d0d8f43SMartin Schwidefsky out_msi: 4825d0d8f43SMartin Schwidefsky list_for_each_entry(msi, &pdev->msi_list, list) { 4831f44a225SMartin Schwidefsky if (hwirq-- == 0) 4845d0d8f43SMartin Schwidefsky break; 4851f44a225SMartin Schwidefsky irq_set_msi_desc(msi->irq, NULL); 4861f44a225SMartin Schwidefsky irq_free_desc(msi->irq); 4871f44a225SMartin Schwidefsky msi->msg.address_lo = 0; 4881f44a225SMartin Schwidefsky msi->msg.address_hi = 0; 4891f44a225SMartin Schwidefsky msi->msg.data = 0; 4901f44a225SMartin Schwidefsky msi->irq = 0; 4915d0d8f43SMartin Schwidefsky } 4921f44a225SMartin Schwidefsky zpci_aibv[aisb] = NULL; 4935d0d8f43SMartin Schwidefsky airq_iv_release(zdev->aibv); 4945d0d8f43SMartin Schwidefsky out_si: 4955d0d8f43SMartin Schwidefsky airq_iv_free_bit(zpci_aisb_iv, aisb); 4965d0d8f43SMartin Schwidefsky out: 4979a4da8a5SJan Glauber dev_err(&pdev->dev, "register MSI failed with: %d\n", rc); 4989a4da8a5SJan Glauber return rc; 4999a4da8a5SJan Glauber } 5009a4da8a5SJan Glauber 5015d0d8f43SMartin Schwidefsky void arch_teardown_msi_irqs(struct pci_dev *pdev) 5029a4da8a5SJan Glauber { 5039a4da8a5SJan Glauber struct zpci_dev *zdev = get_zdev(pdev); 5049a4da8a5SJan Glauber struct msi_desc *msi; 5055d0d8f43SMartin Schwidefsky int rc; 5069a4da8a5SJan Glauber 5075d0d8f43SMartin Schwidefsky pr_info("%s: on pdev: %p\n", __func__, pdev); 5085d0d8f43SMartin Schwidefsky 5095d0d8f43SMartin Schwidefsky /* Disable adapter interrupts */ 5105d0d8f43SMartin Schwidefsky rc = zpci_clear_airq(zdev); 5119a4da8a5SJan Glauber if (rc) { 5129a4da8a5SJan Glauber dev_err(&pdev->dev, "deregister MSI failed with: %d\n", rc); 5139a4da8a5SJan Glauber return; 5149a4da8a5SJan Glauber } 5159a4da8a5SJan Glauber 5161f44a225SMartin Schwidefsky /* Release MSI interrupts */ 5171f44a225SMartin Schwidefsky list_for_each_entry(msi, &pdev->msi_list, list) { 5181f44a225SMartin Schwidefsky zpci_msi_set_mask_bits(msi, 1, 1); 5191f44a225SMartin Schwidefsky irq_set_msi_desc(msi->irq, NULL); 5201f44a225SMartin Schwidefsky irq_free_desc(msi->irq); 5211f44a225SMartin Schwidefsky msi->msg.address_lo = 0; 5221f44a225SMartin Schwidefsky msi->msg.address_hi = 0; 5231f44a225SMartin Schwidefsky msi->msg.data = 0; 5241f44a225SMartin Schwidefsky msi->irq = 0; 5251f44a225SMartin Schwidefsky } 5269a4da8a5SJan Glauber 5271f44a225SMartin Schwidefsky zpci_aibv[zdev->aisb] = NULL; 5285d0d8f43SMartin Schwidefsky airq_iv_release(zdev->aibv); 5295d0d8f43SMartin Schwidefsky airq_iv_free_bit(zpci_aisb_iv, zdev->aisb); 5309a4da8a5SJan Glauber } 5319a4da8a5SJan Glauber 532cd248341SJan Glauber static void zpci_map_resources(struct zpci_dev *zdev) 533cd248341SJan Glauber { 534cd248341SJan Glauber struct pci_dev *pdev = zdev->pdev; 535cd248341SJan Glauber resource_size_t len; 536cd248341SJan Glauber int i; 537cd248341SJan Glauber 538cd248341SJan Glauber for (i = 0; i < PCI_BAR_COUNT; i++) { 539cd248341SJan Glauber len = pci_resource_len(pdev, i); 540cd248341SJan Glauber if (!len) 541cd248341SJan Glauber continue; 542cd248341SJan Glauber pdev->resource[i].start = (resource_size_t) pci_iomap(pdev, i, 0); 543cd248341SJan Glauber pdev->resource[i].end = pdev->resource[i].start + len - 1; 544cd248341SJan Glauber pr_debug("BAR%i: -> start: %Lx end: %Lx\n", 545cd248341SJan Glauber i, pdev->resource[i].start, pdev->resource[i].end); 546cd248341SJan Glauber } 547944239c5SSebastian Ott } 548944239c5SSebastian Ott 549944239c5SSebastian Ott static void zpci_unmap_resources(struct zpci_dev *zdev) 550944239c5SSebastian Ott { 551944239c5SSebastian Ott struct pci_dev *pdev = zdev->pdev; 552944239c5SSebastian Ott resource_size_t len; 553944239c5SSebastian Ott int i; 554944239c5SSebastian Ott 555944239c5SSebastian Ott for (i = 0; i < PCI_BAR_COUNT; i++) { 556944239c5SSebastian Ott len = pci_resource_len(pdev, i); 557944239c5SSebastian Ott if (!len) 558944239c5SSebastian Ott continue; 559944239c5SSebastian Ott pci_iounmap(pdev, (void *) pdev->resource[i].start); 560944239c5SSebastian Ott } 561944239c5SSebastian Ott } 562cd248341SJan Glauber 563cd248341SJan Glauber struct zpci_dev *zpci_alloc_device(void) 564cd248341SJan Glauber { 565cd248341SJan Glauber struct zpci_dev *zdev; 566cd248341SJan Glauber 567cd248341SJan Glauber /* Alloc memory for our private pci device data */ 568cd248341SJan Glauber zdev = kzalloc(sizeof(*zdev), GFP_KERNEL); 5695d0d8f43SMartin Schwidefsky return zdev ? : ERR_PTR(-ENOMEM); 570cd248341SJan Glauber } 571cd248341SJan Glauber 572cd248341SJan Glauber void zpci_free_device(struct zpci_dev *zdev) 573cd248341SJan Glauber { 574cd248341SJan Glauber kfree(zdev); 575cd248341SJan Glauber } 576cd248341SJan Glauber 577cd248341SJan Glauber /* 578cd248341SJan Glauber * Too late for any s390 specific setup, since interrupts must be set up 579cd248341SJan Glauber * already which requires DMA setup too and the pci scan will access the 580cd248341SJan Glauber * config space, which only works if the function handle is enabled. 581cd248341SJan Glauber */ 582cd248341SJan Glauber int pcibios_enable_device(struct pci_dev *pdev, int mask) 583cd248341SJan Glauber { 584cd248341SJan Glauber struct resource *res; 585cd248341SJan Glauber u16 cmd; 586cd248341SJan Glauber int i; 587cd248341SJan Glauber 588cd248341SJan Glauber pci_read_config_word(pdev, PCI_COMMAND, &cmd); 589cd248341SJan Glauber 590cd248341SJan Glauber for (i = 0; i < PCI_BAR_COUNT; i++) { 591cd248341SJan Glauber res = &pdev->resource[i]; 592cd248341SJan Glauber 593cd248341SJan Glauber if (res->flags & IORESOURCE_IO) 594cd248341SJan Glauber return -EINVAL; 595cd248341SJan Glauber 596cd248341SJan Glauber if (res->flags & IORESOURCE_MEM) 597cd248341SJan Glauber cmd |= PCI_COMMAND_MEMORY; 598cd248341SJan Glauber } 599cd248341SJan Glauber pci_write_config_word(pdev, PCI_COMMAND, cmd); 600cd248341SJan Glauber return 0; 601cd248341SJan Glauber } 602cd248341SJan Glauber 6031e8da956SJan Glauber int pcibios_add_platform_entries(struct pci_dev *pdev) 6041e8da956SJan Glauber { 6051e8da956SJan Glauber return zpci_sysfs_add_device(&pdev->dev); 6061e8da956SJan Glauber } 6071e8da956SJan Glauber 6089a4da8a5SJan Glauber static int __init zpci_irq_init(void) 6099a4da8a5SJan Glauber { 6105d0d8f43SMartin Schwidefsky int rc; 6119a4da8a5SJan Glauber 612f4eae94fSMartin Schwidefsky rc = register_adapter_interrupt(&zpci_airq); 613f4eae94fSMartin Schwidefsky if (rc) 6145d0d8f43SMartin Schwidefsky goto out; 615f4eae94fSMartin Schwidefsky /* Set summary to 1 to be called every time for the ISC. */ 616f4eae94fSMartin Schwidefsky *zpci_airq.lsi_ptr = 1; 6179a4da8a5SJan Glauber 6185d0d8f43SMartin Schwidefsky rc = -ENOMEM; 6195d0d8f43SMartin Schwidefsky zpci_aisb_iv = airq_iv_create(ZPCI_NR_DEVICES, AIRQ_IV_ALLOC); 6205d0d8f43SMartin Schwidefsky if (!zpci_aisb_iv) 6215d0d8f43SMartin Schwidefsky goto out_airq; 6229a4da8a5SJan Glauber 6239389339fSMartin Schwidefsky zpci_set_irq_ctrl(SIC_IRQ_MODE_SINGLE, NULL, PCI_ISC); 6249a4da8a5SJan Glauber return 0; 6259a4da8a5SJan Glauber 6265d0d8f43SMartin Schwidefsky out_airq: 6275d0d8f43SMartin Schwidefsky unregister_adapter_interrupt(&zpci_airq); 6285d0d8f43SMartin Schwidefsky out: 6299a4da8a5SJan Glauber return rc; 6309a4da8a5SJan Glauber } 6319a4da8a5SJan Glauber 6329a4da8a5SJan Glauber static void zpci_irq_exit(void) 6339a4da8a5SJan Glauber { 6345d0d8f43SMartin Schwidefsky airq_iv_release(zpci_aisb_iv); 635f4eae94fSMartin Schwidefsky unregister_adapter_interrupt(&zpci_airq); 6369a4da8a5SJan Glauber } 6379a4da8a5SJan Glauber 638cd248341SJan Glauber static struct resource *zpci_alloc_bus_resource(unsigned long start, unsigned long size, 639cd248341SJan Glauber unsigned long flags, int domain) 640cd248341SJan Glauber { 641cd248341SJan Glauber struct resource *r; 642cd248341SJan Glauber char *name; 643cd248341SJan Glauber int rc; 644cd248341SJan Glauber 645cd248341SJan Glauber r = kzalloc(sizeof(*r), GFP_KERNEL); 646cd248341SJan Glauber if (!r) 647cd248341SJan Glauber return ERR_PTR(-ENOMEM); 648cd248341SJan Glauber r->start = start; 649cd248341SJan Glauber r->end = r->start + size - 1; 650cd248341SJan Glauber r->flags = flags; 651cd248341SJan Glauber r->parent = &iomem_resource; 652cd248341SJan Glauber name = kmalloc(18, GFP_KERNEL); 653cd248341SJan Glauber if (!name) { 654cd248341SJan Glauber kfree(r); 655cd248341SJan Glauber return ERR_PTR(-ENOMEM); 656cd248341SJan Glauber } 657cd248341SJan Glauber sprintf(name, "PCI Bus: %04x:%02x", domain, ZPCI_BUS_NR); 658cd248341SJan Glauber r->name = name; 659cd248341SJan Glauber 660cd248341SJan Glauber rc = request_resource(&iomem_resource, r); 661cd248341SJan Glauber if (rc) 662cd248341SJan Glauber pr_debug("request resource %pR failed\n", r); 663cd248341SJan Glauber return r; 664cd248341SJan Glauber } 665cd248341SJan Glauber 666cd248341SJan Glauber static int zpci_alloc_iomap(struct zpci_dev *zdev) 667cd248341SJan Glauber { 668cd248341SJan Glauber int entry; 669cd248341SJan Glauber 670cd248341SJan Glauber spin_lock(&zpci_iomap_lock); 671cd248341SJan Glauber entry = find_first_zero_bit(zpci_iomap, ZPCI_IOMAP_MAX_ENTRIES); 672cd248341SJan Glauber if (entry == ZPCI_IOMAP_MAX_ENTRIES) { 673cd248341SJan Glauber spin_unlock(&zpci_iomap_lock); 674cd248341SJan Glauber return -ENOSPC; 675cd248341SJan Glauber } 676cd248341SJan Glauber set_bit(entry, zpci_iomap); 677cd248341SJan Glauber spin_unlock(&zpci_iomap_lock); 678cd248341SJan Glauber return entry; 679cd248341SJan Glauber } 680cd248341SJan Glauber 681cd248341SJan Glauber static void zpci_free_iomap(struct zpci_dev *zdev, int entry) 682cd248341SJan Glauber { 683cd248341SJan Glauber spin_lock(&zpci_iomap_lock); 684cd248341SJan Glauber memset(&zpci_iomap_start[entry], 0, sizeof(struct zpci_iomap_entry)); 685cd248341SJan Glauber clear_bit(entry, zpci_iomap); 686cd248341SJan Glauber spin_unlock(&zpci_iomap_lock); 687cd248341SJan Glauber } 688cd248341SJan Glauber 689af0a8a84SSebastian Ott int pcibios_add_device(struct pci_dev *pdev) 690af0a8a84SSebastian Ott { 691af0a8a84SSebastian Ott struct zpci_dev *zdev = get_zdev(pdev); 692af0a8a84SSebastian Ott 6931c21351bSSebastian Ott zdev->pdev = pdev; 694af0a8a84SSebastian Ott zpci_debug_init_device(zdev); 695af0a8a84SSebastian Ott zpci_fmb_enable_device(zdev); 696af0a8a84SSebastian Ott zpci_map_resources(zdev); 697af0a8a84SSebastian Ott 698af0a8a84SSebastian Ott return 0; 699af0a8a84SSebastian Ott } 700af0a8a84SSebastian Ott 701944239c5SSebastian Ott void pcibios_release_device(struct pci_dev *pdev) 702944239c5SSebastian Ott { 703944239c5SSebastian Ott struct zpci_dev *zdev = get_zdev(pdev); 704944239c5SSebastian Ott 705944239c5SSebastian Ott zpci_unmap_resources(zdev); 706944239c5SSebastian Ott zpci_fmb_disable_device(zdev); 707944239c5SSebastian Ott zpci_debug_exit_device(zdev); 708944239c5SSebastian Ott zdev->pdev = NULL; 709944239c5SSebastian Ott } 710944239c5SSebastian Ott 7111c21351bSSebastian Ott static int zpci_scan_bus(struct zpci_dev *zdev) 712cd248341SJan Glauber { 713cd248341SJan Glauber struct resource *res; 714cd248341SJan Glauber LIST_HEAD(resources); 715cd248341SJan Glauber int i; 716cd248341SJan Glauber 717cd248341SJan Glauber /* allocate mapping entry for each used bar */ 718cd248341SJan Glauber for (i = 0; i < PCI_BAR_COUNT; i++) { 719cd248341SJan Glauber unsigned long addr, size, flags; 720cd248341SJan Glauber int entry; 721cd248341SJan Glauber 722cd248341SJan Glauber if (!zdev->bars[i].size) 723cd248341SJan Glauber continue; 724cd248341SJan Glauber entry = zpci_alloc_iomap(zdev); 725cd248341SJan Glauber if (entry < 0) 726cd248341SJan Glauber return entry; 727cd248341SJan Glauber zdev->bars[i].map_idx = entry; 728cd248341SJan Glauber 729cd248341SJan Glauber /* only MMIO is supported */ 730cd248341SJan Glauber flags = IORESOURCE_MEM; 731cd248341SJan Glauber if (zdev->bars[i].val & 8) 732cd248341SJan Glauber flags |= IORESOURCE_PREFETCH; 733cd248341SJan Glauber if (zdev->bars[i].val & 4) 734cd248341SJan Glauber flags |= IORESOURCE_MEM_64; 735cd248341SJan Glauber 736cd248341SJan Glauber addr = ZPCI_IOMAP_ADDR_BASE + ((u64) entry << 48); 737cd248341SJan Glauber 738cd248341SJan Glauber size = 1UL << zdev->bars[i].size; 739cd248341SJan Glauber 740cd248341SJan Glauber res = zpci_alloc_bus_resource(addr, size, flags, zdev->domain); 741cd248341SJan Glauber if (IS_ERR(res)) { 742cd248341SJan Glauber zpci_free_iomap(zdev, entry); 743cd248341SJan Glauber return PTR_ERR(res); 744cd248341SJan Glauber } 745cd248341SJan Glauber pci_add_resource(&resources, res); 746cd248341SJan Glauber } 747cd248341SJan Glauber 7481c21351bSSebastian Ott zdev->bus = pci_scan_root_bus(NULL, ZPCI_BUS_NR, &pci_root_ops, 749cd248341SJan Glauber zdev, &resources); 750cd248341SJan Glauber if (!zdev->bus) 751cd248341SJan Glauber return -EIO; 752cd248341SJan Glauber 753cd248341SJan Glauber zdev->bus->max_bus_speed = zdev->max_bus_speed; 754cd248341SJan Glauber return 0; 755cd248341SJan Glauber } 756cd248341SJan Glauber 757cd248341SJan Glauber static int zpci_alloc_domain(struct zpci_dev *zdev) 758cd248341SJan Glauber { 759cd248341SJan Glauber spin_lock(&zpci_domain_lock); 760cd248341SJan Glauber zdev->domain = find_first_zero_bit(zpci_domain, ZPCI_NR_DEVICES); 761cd248341SJan Glauber if (zdev->domain == ZPCI_NR_DEVICES) { 762cd248341SJan Glauber spin_unlock(&zpci_domain_lock); 763cd248341SJan Glauber return -ENOSPC; 764cd248341SJan Glauber } 765cd248341SJan Glauber set_bit(zdev->domain, zpci_domain); 766cd248341SJan Glauber spin_unlock(&zpci_domain_lock); 767cd248341SJan Glauber return 0; 768cd248341SJan Glauber } 769cd248341SJan Glauber 770cd248341SJan Glauber static void zpci_free_domain(struct zpci_dev *zdev) 771cd248341SJan Glauber { 772cd248341SJan Glauber spin_lock(&zpci_domain_lock); 773cd248341SJan Glauber clear_bit(zdev->domain, zpci_domain); 774cd248341SJan Glauber spin_unlock(&zpci_domain_lock); 775cd248341SJan Glauber } 776cd248341SJan Glauber 777a755a45dSJan Glauber int zpci_enable_device(struct zpci_dev *zdev) 778a755a45dSJan Glauber { 779a755a45dSJan Glauber int rc; 780a755a45dSJan Glauber 781a755a45dSJan Glauber rc = clp_enable_fh(zdev, ZPCI_NR_DMA_SPACES); 782a755a45dSJan Glauber if (rc) 783a755a45dSJan Glauber goto out; 784a755a45dSJan Glauber pr_info("Enabled fh: 0x%x fid: 0x%x\n", zdev->fh, zdev->fid); 785828b35f6SJan Glauber 786828b35f6SJan Glauber rc = zpci_dma_init_device(zdev); 787828b35f6SJan Glauber if (rc) 788828b35f6SJan Glauber goto out_dma; 789a755a45dSJan Glauber return 0; 790828b35f6SJan Glauber 791828b35f6SJan Glauber out_dma: 792828b35f6SJan Glauber clp_disable_fh(zdev); 793a755a45dSJan Glauber out: 794a755a45dSJan Glauber return rc; 795a755a45dSJan Glauber } 796a755a45dSJan Glauber EXPORT_SYMBOL_GPL(zpci_enable_device); 797a755a45dSJan Glauber 798cb65a669SSebastian Ott int zpci_disable_device(struct zpci_dev *zdev) 799cb65a669SSebastian Ott { 800cb65a669SSebastian Ott zpci_dma_exit_device(zdev); 801cb65a669SSebastian Ott return clp_disable_fh(zdev); 802cb65a669SSebastian Ott } 803cb65a669SSebastian Ott EXPORT_SYMBOL_GPL(zpci_disable_device); 804cb65a669SSebastian Ott 805cd248341SJan Glauber int zpci_create_device(struct zpci_dev *zdev) 806cd248341SJan Glauber { 807cd248341SJan Glauber int rc; 808cd248341SJan Glauber 809cd248341SJan Glauber rc = zpci_alloc_domain(zdev); 810cd248341SJan Glauber if (rc) 811cd248341SJan Glauber goto out; 812cd248341SJan Glauber 8131c21351bSSebastian Ott if (zdev->state == ZPCI_FN_STATE_CONFIGURED) { 8141c21351bSSebastian Ott rc = zpci_enable_device(zdev); 815cd248341SJan Glauber if (rc) 8161c21351bSSebastian Ott goto out_free; 8171c21351bSSebastian Ott 8181c21351bSSebastian Ott zdev->state = ZPCI_FN_STATE_ONLINE; 8191c21351bSSebastian Ott } 8201c21351bSSebastian Ott rc = zpci_scan_bus(zdev); 8211c21351bSSebastian Ott if (rc) 8221c21351bSSebastian Ott goto out_disable; 823cd248341SJan Glauber 824cd248341SJan Glauber mutex_lock(&zpci_list_lock); 825cd248341SJan Glauber list_add_tail(&zdev->entry, &zpci_list); 826cd248341SJan Glauber mutex_unlock(&zpci_list_lock); 827cd248341SJan Glauber 828*67f43f38SSebastian Ott zpci_init_slot(zdev); 829*67f43f38SSebastian Ott 830cd248341SJan Glauber return 0; 831cd248341SJan Glauber 8321c21351bSSebastian Ott out_disable: 8331c21351bSSebastian Ott if (zdev->state == ZPCI_FN_STATE_ONLINE) 8341c21351bSSebastian Ott zpci_disable_device(zdev); 8351c21351bSSebastian Ott out_free: 836cd248341SJan Glauber zpci_free_domain(zdev); 837cd248341SJan Glauber out: 838cd248341SJan Glauber return rc; 839cd248341SJan Glauber } 840cd248341SJan Glauber 841cd248341SJan Glauber void zpci_stop_device(struct zpci_dev *zdev) 842cd248341SJan Glauber { 843828b35f6SJan Glauber zpci_dma_exit_device(zdev); 844cd248341SJan Glauber /* 845cd248341SJan Glauber * Note: SCLP disables fh via set-pci-fn so don't 846cd248341SJan Glauber * do that here. 847cd248341SJan Glauber */ 848cd248341SJan Glauber } 849cd248341SJan Glauber EXPORT_SYMBOL_GPL(zpci_stop_device); 850cd248341SJan Glauber 851cd248341SJan Glauber static inline int barsize(u8 size) 852cd248341SJan Glauber { 853cd248341SJan Glauber return (size) ? (1 << size) >> 10 : 0; 854cd248341SJan Glauber } 855cd248341SJan Glauber 856cd248341SJan Glauber static int zpci_mem_init(void) 857cd248341SJan Glauber { 858d0b08853SJan Glauber zdev_fmb_cache = kmem_cache_create("PCI_FMB_cache", sizeof(struct zpci_fmb), 859d0b08853SJan Glauber 16, 0, NULL); 860d0b08853SJan Glauber if (!zdev_fmb_cache) 8611f44a225SMartin Schwidefsky goto error_zdev; 862d0b08853SJan Glauber 863cd248341SJan Glauber /* TODO: use realloc */ 864cd248341SJan Glauber zpci_iomap_start = kzalloc(ZPCI_IOMAP_MAX_ENTRIES * sizeof(*zpci_iomap_start), 865cd248341SJan Glauber GFP_KERNEL); 866cd248341SJan Glauber if (!zpci_iomap_start) 8679a4da8a5SJan Glauber goto error_iomap; 868cd248341SJan Glauber return 0; 869cd248341SJan Glauber 8709a4da8a5SJan Glauber error_iomap: 871d0b08853SJan Glauber kmem_cache_destroy(zdev_fmb_cache); 872cd248341SJan Glauber error_zdev: 873cd248341SJan Glauber return -ENOMEM; 874cd248341SJan Glauber } 875cd248341SJan Glauber 876cd248341SJan Glauber static void zpci_mem_exit(void) 877cd248341SJan Glauber { 878cd248341SJan Glauber kfree(zpci_iomap_start); 879d0b08853SJan Glauber kmem_cache_destroy(zdev_fmb_cache); 880cd248341SJan Glauber } 881cd248341SJan Glauber 882*67f43f38SSebastian Ott static unsigned int s390_pci_probe; 883cd248341SJan Glauber 884cd248341SJan Glauber char * __init pcibios_setup(char *str) 885cd248341SJan Glauber { 88689b0dc95SSebastian Ott if (!strcmp(str, "on")) { 88789b0dc95SSebastian Ott s390_pci_probe = 1; 888cd248341SJan Glauber return NULL; 889cd248341SJan Glauber } 890cd248341SJan Glauber return str; 891cd248341SJan Glauber } 892cd248341SJan Glauber 893cd248341SJan Glauber static int __init pci_base_init(void) 894cd248341SJan Glauber { 895cd248341SJan Glauber int rc; 896cd248341SJan Glauber 8971e5635d1SHeiko Carstens if (!s390_pci_probe) 898cd248341SJan Glauber return 0; 899cd248341SJan Glauber 900cd248341SJan Glauber if (!test_facility(2) || !test_facility(69) 901cd248341SJan Glauber || !test_facility(71) || !test_facility(72)) 902cd248341SJan Glauber return 0; 903cd248341SJan Glauber 904cd248341SJan Glauber pr_info("Probing PCI hardware: PCI:%d SID:%d AEN:%d\n", 905cd248341SJan Glauber test_facility(69), test_facility(70), 906cd248341SJan Glauber test_facility(71)); 907cd248341SJan Glauber 908d0b08853SJan Glauber rc = zpci_debug_init(); 909d0b08853SJan Glauber if (rc) 9101f44a225SMartin Schwidefsky goto out; 911d0b08853SJan Glauber 912cd248341SJan Glauber rc = zpci_mem_init(); 913cd248341SJan Glauber if (rc) 914cd248341SJan Glauber goto out_mem; 915cd248341SJan Glauber 9169a4da8a5SJan Glauber rc = zpci_irq_init(); 9179a4da8a5SJan Glauber if (rc) 9189a4da8a5SJan Glauber goto out_irq; 9199a4da8a5SJan Glauber 920828b35f6SJan Glauber rc = zpci_dma_init(); 921828b35f6SJan Glauber if (rc) 922828b35f6SJan Glauber goto out_dma; 923828b35f6SJan Glauber 924a755a45dSJan Glauber rc = clp_find_pci_devices(); 925a755a45dSJan Glauber if (rc) 926a755a45dSJan Glauber goto out_find; 927a755a45dSJan Glauber 928cd248341SJan Glauber return 0; 929cd248341SJan Glauber 930a755a45dSJan Glauber out_find: 931828b35f6SJan Glauber zpci_dma_exit(); 932828b35f6SJan Glauber out_dma: 9339a4da8a5SJan Glauber zpci_irq_exit(); 9349a4da8a5SJan Glauber out_irq: 935cd248341SJan Glauber zpci_mem_exit(); 936cd248341SJan Glauber out_mem: 937d0b08853SJan Glauber zpci_debug_exit(); 9381f44a225SMartin Schwidefsky out: 939cd248341SJan Glauber return rc; 940cd248341SJan Glauber } 941*67f43f38SSebastian Ott subsys_initcall_sync(pci_base_init); 942