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", 538fb878c5SYijing Wang .irq_unmask = unmask_msi_irq, 548fb878c5SYijing Wang .irq_mask = mask_msi_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 71cd248341SJan Glauber /* I/O Map */ 72cd248341SJan Glauber static DEFINE_SPINLOCK(zpci_iomap_lock); 73cd248341SJan Glauber static DECLARE_BITMAP(zpci_iomap, ZPCI_IOMAP_MAX_ENTRIES); 74cd248341SJan Glauber struct zpci_iomap_entry *zpci_iomap_start; 75cd248341SJan Glauber EXPORT_SYMBOL_GPL(zpci_iomap_start); 76cd248341SJan Glauber 77d0b08853SJan Glauber static struct kmem_cache *zdev_fmb_cache; 78d0b08853SJan Glauber 79cd248341SJan Glauber struct zpci_dev *get_zdev(struct pci_dev *pdev) 80cd248341SJan Glauber { 81cd248341SJan Glauber return (struct zpci_dev *) pdev->sysdata; 82cd248341SJan Glauber } 83cd248341SJan Glauber 84cd248341SJan Glauber struct zpci_dev *get_zdev_by_fid(u32 fid) 85cd248341SJan Glauber { 86cd248341SJan Glauber struct zpci_dev *tmp, *zdev = NULL; 87cd248341SJan Glauber 8857b5918cSSebastian Ott spin_lock(&zpci_list_lock); 89cd248341SJan Glauber list_for_each_entry(tmp, &zpci_list, entry) { 90cd248341SJan Glauber if (tmp->fid == fid) { 91cd248341SJan Glauber zdev = tmp; 92cd248341SJan Glauber break; 93cd248341SJan Glauber } 94cd248341SJan Glauber } 9557b5918cSSebastian Ott spin_unlock(&zpci_list_lock); 96cd248341SJan Glauber return zdev; 97cd248341SJan Glauber } 98cd248341SJan Glauber 99cd248341SJan Glauber static struct zpci_dev *get_zdev_by_bus(struct pci_bus *bus) 100cd248341SJan Glauber { 101cd248341SJan Glauber return (bus && bus->sysdata) ? (struct zpci_dev *) bus->sysdata : NULL; 102cd248341SJan Glauber } 103cd248341SJan Glauber 104cd248341SJan Glauber int pci_domain_nr(struct pci_bus *bus) 105cd248341SJan Glauber { 106cd248341SJan Glauber return ((struct zpci_dev *) bus->sysdata)->domain; 107cd248341SJan Glauber } 108cd248341SJan Glauber EXPORT_SYMBOL_GPL(pci_domain_nr); 109cd248341SJan Glauber 110cd248341SJan Glauber int pci_proc_domain(struct pci_bus *bus) 111cd248341SJan Glauber { 112cd248341SJan Glauber return pci_domain_nr(bus); 113cd248341SJan Glauber } 114cd248341SJan Glauber EXPORT_SYMBOL_GPL(pci_proc_domain); 115cd248341SJan Glauber 1169a4da8a5SJan Glauber /* Modify PCI: Register adapter interruptions */ 1175d0d8f43SMartin Schwidefsky static int zpci_set_airq(struct zpci_dev *zdev) 1189a4da8a5SJan Glauber { 1199a4da8a5SJan Glauber u64 req = ZPCI_CREATE_REQ(zdev->fh, 0, ZPCI_MOD_FC_REG_INT); 120cb4deb69SSebastian Ott struct zpci_fib fib = {0}; 1219a4da8a5SJan Glauber 122cb4deb69SSebastian Ott fib.isc = PCI_ISC; 123cb4deb69SSebastian Ott fib.sum = 1; /* enable summary notifications */ 124cb4deb69SSebastian Ott fib.noi = airq_iv_end(zdev->aibv); 125cb4deb69SSebastian Ott fib.aibv = (unsigned long) zdev->aibv->vector; 126cb4deb69SSebastian Ott fib.aibvo = 0; /* each zdev has its own interrupt vector */ 127cb4deb69SSebastian Ott fib.aisb = (unsigned long) zpci_aisb_iv->vector + (zdev->aisb/64)*8; 128cb4deb69SSebastian Ott fib.aisbo = zdev->aisb & 63; 1299a4da8a5SJan Glauber 130cb4deb69SSebastian Ott return zpci_mod_fc(req, &fib); 1319a4da8a5SJan Glauber } 1329a4da8a5SJan Glauber 1339a4da8a5SJan Glauber struct mod_pci_args { 1349a4da8a5SJan Glauber u64 base; 1359a4da8a5SJan Glauber u64 limit; 1369a4da8a5SJan Glauber u64 iota; 137d0b08853SJan Glauber u64 fmb_addr; 1389a4da8a5SJan Glauber }; 1399a4da8a5SJan Glauber 1409a4da8a5SJan Glauber static int mod_pci(struct zpci_dev *zdev, int fn, u8 dmaas, struct mod_pci_args *args) 1419a4da8a5SJan Glauber { 1429a4da8a5SJan Glauber u64 req = ZPCI_CREATE_REQ(zdev->fh, dmaas, fn); 143cb4deb69SSebastian Ott struct zpci_fib fib = {0}; 1449a4da8a5SJan Glauber 145cb4deb69SSebastian Ott fib.pba = args->base; 146cb4deb69SSebastian Ott fib.pal = args->limit; 147cb4deb69SSebastian Ott fib.iota = args->iota; 148cb4deb69SSebastian Ott fib.fmb_addr = args->fmb_addr; 1499a4da8a5SJan Glauber 150cb4deb69SSebastian Ott return zpci_mod_fc(req, &fib); 1519a4da8a5SJan Glauber } 1529a4da8a5SJan Glauber 153828b35f6SJan Glauber /* Modify PCI: Register I/O address translation parameters */ 154828b35f6SJan Glauber int zpci_register_ioat(struct zpci_dev *zdev, u8 dmaas, 155828b35f6SJan Glauber u64 base, u64 limit, u64 iota) 156828b35f6SJan Glauber { 157d0b08853SJan Glauber struct mod_pci_args args = { base, limit, iota, 0 }; 158828b35f6SJan Glauber 159828b35f6SJan Glauber WARN_ON_ONCE(iota & 0x3fff); 160828b35f6SJan Glauber args.iota |= ZPCI_IOTA_RTTO_FLAG; 161828b35f6SJan Glauber return mod_pci(zdev, ZPCI_MOD_FC_REG_IOAT, dmaas, &args); 162828b35f6SJan Glauber } 163828b35f6SJan Glauber 164828b35f6SJan Glauber /* Modify PCI: Unregister I/O address translation parameters */ 165828b35f6SJan Glauber int zpci_unregister_ioat(struct zpci_dev *zdev, u8 dmaas) 166828b35f6SJan Glauber { 167d0b08853SJan Glauber struct mod_pci_args args = { 0, 0, 0, 0 }; 168828b35f6SJan Glauber 169828b35f6SJan Glauber return mod_pci(zdev, ZPCI_MOD_FC_DEREG_IOAT, dmaas, &args); 170828b35f6SJan Glauber } 171828b35f6SJan Glauber 1729a4da8a5SJan Glauber /* Modify PCI: Unregister adapter interruptions */ 1735d0d8f43SMartin Schwidefsky static int zpci_clear_airq(struct zpci_dev *zdev) 1749a4da8a5SJan Glauber { 175d0b08853SJan Glauber struct mod_pci_args args = { 0, 0, 0, 0 }; 1769a4da8a5SJan Glauber 1779a4da8a5SJan Glauber return mod_pci(zdev, ZPCI_MOD_FC_DEREG_INT, 0, &args); 1789a4da8a5SJan Glauber } 1799a4da8a5SJan Glauber 180d0b08853SJan Glauber /* Modify PCI: Set PCI function measurement parameters */ 181d0b08853SJan Glauber int zpci_fmb_enable_device(struct zpci_dev *zdev) 182d0b08853SJan Glauber { 183d0b08853SJan Glauber struct mod_pci_args args = { 0, 0, 0, 0 }; 184d0b08853SJan Glauber 185d0b08853SJan Glauber if (zdev->fmb) 186d0b08853SJan Glauber return -EINVAL; 187d0b08853SJan Glauber 18808b42124SWei Yongjun zdev->fmb = kmem_cache_zalloc(zdev_fmb_cache, GFP_KERNEL); 189d0b08853SJan Glauber if (!zdev->fmb) 190d0b08853SJan Glauber return -ENOMEM; 191d0b08853SJan Glauber WARN_ON((u64) zdev->fmb & 0xf); 192d0b08853SJan Glauber 193d0b08853SJan Glauber args.fmb_addr = virt_to_phys(zdev->fmb); 194d0b08853SJan Glauber return mod_pci(zdev, ZPCI_MOD_FC_SET_MEASURE, 0, &args); 195d0b08853SJan Glauber } 196d0b08853SJan Glauber 197d0b08853SJan Glauber /* Modify PCI: Disable PCI function measurement */ 198d0b08853SJan Glauber int zpci_fmb_disable_device(struct zpci_dev *zdev) 199d0b08853SJan Glauber { 200d0b08853SJan Glauber struct mod_pci_args args = { 0, 0, 0, 0 }; 201d0b08853SJan Glauber int rc; 202d0b08853SJan Glauber 203d0b08853SJan Glauber if (!zdev->fmb) 204d0b08853SJan Glauber return -EINVAL; 205d0b08853SJan Glauber 206d0b08853SJan Glauber /* Function measurement is disabled if fmb address is zero */ 207d0b08853SJan Glauber rc = mod_pci(zdev, ZPCI_MOD_FC_SET_MEASURE, 0, &args); 208d0b08853SJan Glauber 209d0b08853SJan Glauber kmem_cache_free(zdev_fmb_cache, zdev->fmb); 210d0b08853SJan Glauber zdev->fmb = NULL; 211d0b08853SJan Glauber return rc; 212d0b08853SJan Glauber } 213d0b08853SJan Glauber 214cd248341SJan Glauber #define ZPCI_PCIAS_CFGSPC 15 215cd248341SJan Glauber 216cd248341SJan Glauber static int zpci_cfg_load(struct zpci_dev *zdev, int offset, u32 *val, u8 len) 217cd248341SJan Glauber { 218cd248341SJan Glauber u64 req = ZPCI_CREATE_REQ(zdev->fh, ZPCI_PCIAS_CFGSPC, len); 219cd248341SJan Glauber u64 data; 220cd248341SJan Glauber int rc; 221cd248341SJan Glauber 2229389339fSMartin Schwidefsky rc = zpci_load(&data, req, offset); 223b170bad4SSebastian Ott if (!rc) { 224cd248341SJan Glauber data = data << ((8 - len) * 8); 225cd248341SJan Glauber data = le64_to_cpu(data); 226cd248341SJan Glauber *val = (u32) data; 227b170bad4SSebastian Ott } else 228cd248341SJan Glauber *val = 0xffffffff; 229cd248341SJan Glauber return rc; 230cd248341SJan Glauber } 231cd248341SJan Glauber 232cd248341SJan Glauber static int zpci_cfg_store(struct zpci_dev *zdev, int offset, u32 val, u8 len) 233cd248341SJan Glauber { 234cd248341SJan Glauber u64 req = ZPCI_CREATE_REQ(zdev->fh, ZPCI_PCIAS_CFGSPC, len); 235cd248341SJan Glauber u64 data = val; 236cd248341SJan Glauber int rc; 237cd248341SJan Glauber 238cd248341SJan Glauber data = cpu_to_le64(data); 239cd248341SJan Glauber data = data >> ((8 - len) * 8); 2409389339fSMartin Schwidefsky rc = zpci_store(data, req, offset); 241cd248341SJan Glauber return rc; 242cd248341SJan Glauber } 243cd248341SJan Glauber 244b881bc46SGreg Kroah-Hartman void pcibios_fixup_bus(struct pci_bus *bus) 245cd248341SJan Glauber { 246cd248341SJan Glauber } 247cd248341SJan Glauber 248cd248341SJan Glauber resource_size_t pcibios_align_resource(void *data, const struct resource *res, 249cd248341SJan Glauber resource_size_t size, 250cd248341SJan Glauber resource_size_t align) 251cd248341SJan Glauber { 252cd248341SJan Glauber return 0; 253cd248341SJan Glauber } 254cd248341SJan Glauber 25587bc359bSJan Glauber /* combine single writes by using store-block insn */ 25687bc359bSJan Glauber void __iowrite64_copy(void __iomem *to, const void *from, size_t count) 25787bc359bSJan Glauber { 25887bc359bSJan Glauber zpci_memcpy_toio(to, from, count); 25987bc359bSJan Glauber } 26087bc359bSJan Glauber 261cd248341SJan Glauber /* Create a virtual mapping cookie for a PCI BAR */ 262cd248341SJan Glauber void __iomem *pci_iomap(struct pci_dev *pdev, int bar, unsigned long max) 263cd248341SJan Glauber { 264cd248341SJan Glauber struct zpci_dev *zdev = get_zdev(pdev); 265cd248341SJan Glauber u64 addr; 266cd248341SJan Glauber int idx; 267cd248341SJan Glauber 268cd248341SJan Glauber if ((bar & 7) != bar) 269cd248341SJan Glauber return NULL; 270cd248341SJan Glauber 271cd248341SJan Glauber idx = zdev->bars[bar].map_idx; 272cd248341SJan Glauber spin_lock(&zpci_iomap_lock); 273cd248341SJan Glauber zpci_iomap_start[idx].fh = zdev->fh; 274cd248341SJan Glauber zpci_iomap_start[idx].bar = bar; 275cd248341SJan Glauber spin_unlock(&zpci_iomap_lock); 276cd248341SJan Glauber 277cd248341SJan Glauber addr = ZPCI_IOMAP_ADDR_BASE | ((u64) idx << 48); 278cd248341SJan Glauber return (void __iomem *) addr; 279cd248341SJan Glauber } 280cd248341SJan Glauber EXPORT_SYMBOL_GPL(pci_iomap); 281cd248341SJan Glauber 282cd248341SJan Glauber void pci_iounmap(struct pci_dev *pdev, void __iomem *addr) 283cd248341SJan Glauber { 284cd248341SJan Glauber unsigned int idx; 285cd248341SJan Glauber 286cd248341SJan Glauber idx = (((__force u64) addr) & ~ZPCI_IOMAP_ADDR_BASE) >> 48; 287cd248341SJan Glauber spin_lock(&zpci_iomap_lock); 288cd248341SJan Glauber zpci_iomap_start[idx].fh = 0; 289cd248341SJan Glauber zpci_iomap_start[idx].bar = 0; 290cd248341SJan Glauber spin_unlock(&zpci_iomap_lock); 291cd248341SJan Glauber } 292cd248341SJan Glauber EXPORT_SYMBOL_GPL(pci_iounmap); 293cd248341SJan Glauber 294cd248341SJan Glauber static int pci_read(struct pci_bus *bus, unsigned int devfn, int where, 295cd248341SJan Glauber int size, u32 *val) 296cd248341SJan Glauber { 297cd248341SJan Glauber struct zpci_dev *zdev = get_zdev_by_bus(bus); 2982c3700bbSSebastian Ott int ret; 299cd248341SJan Glauber 300cd248341SJan Glauber if (!zdev || devfn != ZPCI_DEVFN) 3012c3700bbSSebastian Ott ret = -ENODEV; 3022c3700bbSSebastian Ott else 3032c3700bbSSebastian Ott ret = zpci_cfg_load(zdev, where, val, size); 3042c3700bbSSebastian Ott 3052c3700bbSSebastian Ott return ret; 306cd248341SJan Glauber } 307cd248341SJan Glauber 308cd248341SJan Glauber static int pci_write(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_store(zdev, where, val, size); 3182c3700bbSSebastian Ott 3192c3700bbSSebastian Ott return ret; 320cd248341SJan Glauber } 321cd248341SJan Glauber 322cd248341SJan Glauber static struct pci_ops pci_root_ops = { 323cd248341SJan Glauber .read = pci_read, 324cd248341SJan Glauber .write = pci_write, 325cd248341SJan Glauber }; 326cd248341SJan Glauber 327f4eae94fSMartin Schwidefsky static void zpci_irq_handler(struct airq_struct *airq) 3289a4da8a5SJan Glauber { 3295d0d8f43SMartin Schwidefsky unsigned long si, ai; 3301f44a225SMartin Schwidefsky struct airq_iv *aibv; 3315d0d8f43SMartin Schwidefsky int irqs_on = 0; 3329a4da8a5SJan Glauber 333420f42ecSHeiko Carstens inc_irq_stat(IRQIO_PCI); 3345d0d8f43SMartin Schwidefsky for (si = 0;;) { 3355d0d8f43SMartin Schwidefsky /* Scan adapter summary indicator bit vector */ 3365d0d8f43SMartin Schwidefsky si = airq_iv_scan(zpci_aisb_iv, si, airq_iv_end(zpci_aisb_iv)); 3375d0d8f43SMartin Schwidefsky if (si == -1UL) { 3385d0d8f43SMartin Schwidefsky if (irqs_on++) 3395d0d8f43SMartin Schwidefsky /* End of second scan with interrupts on. */ 3405d0d8f43SMartin Schwidefsky break; 3415d0d8f43SMartin Schwidefsky /* First scan complete, reenable interrupts. */ 3429389339fSMartin Schwidefsky zpci_set_irq_ctrl(SIC_IRQ_MODE_SINGLE, NULL, PCI_ISC); 3435d0d8f43SMartin Schwidefsky si = 0; 3445d0d8f43SMartin Schwidefsky continue; 3459a4da8a5SJan Glauber } 3465d0d8f43SMartin Schwidefsky 3475d0d8f43SMartin Schwidefsky /* Scan the adapter interrupt vector for this device. */ 3481f44a225SMartin Schwidefsky aibv = zpci_aibv[si]; 3495d0d8f43SMartin Schwidefsky for (ai = 0;;) { 3501f44a225SMartin Schwidefsky ai = airq_iv_scan(aibv, ai, airq_iv_end(aibv)); 3515d0d8f43SMartin Schwidefsky if (ai == -1UL) 3525d0d8f43SMartin Schwidefsky break; 3535d0d8f43SMartin Schwidefsky inc_irq_stat(IRQIO_MSI); 3541f44a225SMartin Schwidefsky airq_iv_lock(aibv, ai); 3551f44a225SMartin Schwidefsky generic_handle_irq(airq_iv_get_data(aibv, ai)); 3561f44a225SMartin Schwidefsky airq_iv_unlock(aibv, ai); 3575d0d8f43SMartin Schwidefsky } 3585d0d8f43SMartin Schwidefsky } 3595d0d8f43SMartin Schwidefsky } 3605d0d8f43SMartin Schwidefsky 3615d0d8f43SMartin Schwidefsky int arch_setup_msi_irqs(struct pci_dev *pdev, int nvec, int type) 3629a4da8a5SJan Glauber { 3639a4da8a5SJan Glauber struct zpci_dev *zdev = get_zdev(pdev); 3640a0a9421SThomas Gleixner unsigned int hwirq, msi_vecs; 3655d0d8f43SMartin Schwidefsky unsigned long aisb; 3669a4da8a5SJan Glauber struct msi_desc *msi; 3671f44a225SMartin Schwidefsky struct msi_msg msg; 3680a0a9421SThomas Gleixner int rc, irq; 3699a4da8a5SJan Glauber 370a384c892SAlexander Gordeev if (type == PCI_CAP_ID_MSI && nvec > 1) 371a384c892SAlexander Gordeev return 1; 372b19148f6SSebastian Ott msi_vecs = min_t(unsigned int, nvec, zdev->max_msi); 3739a4da8a5SJan Glauber 3745d0d8f43SMartin Schwidefsky /* Allocate adapter summary indicator bit */ 3755d0d8f43SMartin Schwidefsky rc = -EIO; 3765d0d8f43SMartin Schwidefsky aisb = airq_iv_alloc_bit(zpci_aisb_iv); 3775d0d8f43SMartin Schwidefsky if (aisb == -1UL) 3785d0d8f43SMartin Schwidefsky goto out; 3799a4da8a5SJan Glauber zdev->aisb = aisb; 3809a4da8a5SJan Glauber 3815d0d8f43SMartin Schwidefsky /* Create adapter interrupt vector */ 3825d0d8f43SMartin Schwidefsky rc = -ENOMEM; 3831f44a225SMartin Schwidefsky zdev->aibv = airq_iv_create(msi_vecs, AIRQ_IV_DATA | AIRQ_IV_BITLOCK); 3845d0d8f43SMartin Schwidefsky if (!zdev->aibv) 3855d0d8f43SMartin Schwidefsky goto out_si; 3869a4da8a5SJan Glauber 3875d0d8f43SMartin Schwidefsky /* Wire up shortcut pointer */ 3881f44a225SMartin Schwidefsky zpci_aibv[aisb] = zdev->aibv; 3895d0d8f43SMartin Schwidefsky 3901f44a225SMartin Schwidefsky /* Request MSI interrupts */ 3911f44a225SMartin Schwidefsky hwirq = 0; 3929a4da8a5SJan Glauber list_for_each_entry(msi, &pdev->msi_list, list) { 3931f44a225SMartin Schwidefsky rc = -EIO; 3941f44a225SMartin Schwidefsky irq = irq_alloc_desc(0); /* Alloc irq on node 0 */ 3950a0a9421SThomas Gleixner if (irq < 0) 3961f44a225SMartin Schwidefsky goto out_msi; 3971f44a225SMartin Schwidefsky rc = irq_set_msi_desc(irq, msi); 3989a4da8a5SJan Glauber if (rc) 3995d0d8f43SMartin Schwidefsky goto out_msi; 4001f44a225SMartin Schwidefsky irq_set_chip_and_handler(irq, &zpci_irq_chip, 4011f44a225SMartin Schwidefsky handle_simple_irq); 4021f44a225SMartin Schwidefsky msg.data = hwirq; 4031f44a225SMartin Schwidefsky msg.address_lo = zdev->msi_addr & 0xffffffff; 4041f44a225SMartin Schwidefsky msg.address_hi = zdev->msi_addr >> 32; 4051f44a225SMartin Schwidefsky write_msi_msg(irq, &msg); 4061f44a225SMartin Schwidefsky airq_iv_set_data(zdev->aibv, hwirq, irq); 4071f44a225SMartin Schwidefsky hwirq++; 4089a4da8a5SJan Glauber } 4099a4da8a5SJan Glauber 4105d0d8f43SMartin Schwidefsky /* Enable adapter interrupts */ 4115d0d8f43SMartin Schwidefsky rc = zpci_set_airq(zdev); 4125d0d8f43SMartin Schwidefsky if (rc) 4135d0d8f43SMartin Schwidefsky goto out_msi; 4145d0d8f43SMartin Schwidefsky 4155d0d8f43SMartin Schwidefsky return (msi_vecs == nvec) ? 0 : msi_vecs; 4165d0d8f43SMartin Schwidefsky 4175d0d8f43SMartin Schwidefsky out_msi: 4185d0d8f43SMartin Schwidefsky list_for_each_entry(msi, &pdev->msi_list, list) { 4191f44a225SMartin Schwidefsky if (hwirq-- == 0) 4205d0d8f43SMartin Schwidefsky break; 4211f44a225SMartin Schwidefsky irq_set_msi_desc(msi->irq, NULL); 4221f44a225SMartin Schwidefsky irq_free_desc(msi->irq); 4231f44a225SMartin Schwidefsky msi->msg.address_lo = 0; 4241f44a225SMartin Schwidefsky msi->msg.address_hi = 0; 4251f44a225SMartin Schwidefsky msi->msg.data = 0; 4261f44a225SMartin Schwidefsky msi->irq = 0; 4275d0d8f43SMartin Schwidefsky } 4281f44a225SMartin Schwidefsky zpci_aibv[aisb] = NULL; 4295d0d8f43SMartin Schwidefsky airq_iv_release(zdev->aibv); 4305d0d8f43SMartin Schwidefsky out_si: 4315d0d8f43SMartin Schwidefsky airq_iv_free_bit(zpci_aisb_iv, aisb); 4325d0d8f43SMartin Schwidefsky out: 4339a4da8a5SJan Glauber return rc; 4349a4da8a5SJan Glauber } 4359a4da8a5SJan Glauber 4365d0d8f43SMartin Schwidefsky void arch_teardown_msi_irqs(struct pci_dev *pdev) 4379a4da8a5SJan Glauber { 4389a4da8a5SJan Glauber struct zpci_dev *zdev = get_zdev(pdev); 4399a4da8a5SJan Glauber struct msi_desc *msi; 4405d0d8f43SMartin Schwidefsky int rc; 4419a4da8a5SJan Glauber 4425d0d8f43SMartin Schwidefsky /* Disable adapter interrupts */ 4435d0d8f43SMartin Schwidefsky rc = zpci_clear_airq(zdev); 4441f1dcbd4SSebastian Ott if (rc) 4459a4da8a5SJan Glauber return; 4469a4da8a5SJan Glauber 4471f44a225SMartin Schwidefsky /* Release MSI interrupts */ 4481f44a225SMartin Schwidefsky list_for_each_entry(msi, &pdev->msi_list, list) { 4498fb878c5SYijing Wang if (msi->msi_attrib.is_msix) 4508fb878c5SYijing Wang default_msix_mask_irq(msi, 1); 4518fb878c5SYijing Wang else 4528fb878c5SYijing Wang default_msi_mask_irq(msi, 1, 1); 4531f44a225SMartin Schwidefsky irq_set_msi_desc(msi->irq, NULL); 4541f44a225SMartin Schwidefsky irq_free_desc(msi->irq); 4551f44a225SMartin Schwidefsky msi->msg.address_lo = 0; 4561f44a225SMartin Schwidefsky msi->msg.address_hi = 0; 4571f44a225SMartin Schwidefsky msi->msg.data = 0; 4581f44a225SMartin Schwidefsky msi->irq = 0; 4591f44a225SMartin Schwidefsky } 4609a4da8a5SJan Glauber 4611f44a225SMartin Schwidefsky zpci_aibv[zdev->aisb] = NULL; 4625d0d8f43SMartin Schwidefsky airq_iv_release(zdev->aibv); 4635d0d8f43SMartin Schwidefsky airq_iv_free_bit(zpci_aisb_iv, zdev->aisb); 4649a4da8a5SJan Glauber } 4659a4da8a5SJan Glauber 466cd248341SJan Glauber static void zpci_map_resources(struct zpci_dev *zdev) 467cd248341SJan Glauber { 468cd248341SJan Glauber struct pci_dev *pdev = zdev->pdev; 469cd248341SJan Glauber resource_size_t len; 470cd248341SJan Glauber int i; 471cd248341SJan Glauber 472cd248341SJan Glauber for (i = 0; i < PCI_BAR_COUNT; i++) { 473cd248341SJan Glauber len = pci_resource_len(pdev, i); 474cd248341SJan Glauber if (!len) 475cd248341SJan Glauber continue; 476*5b9f2081SMartin Schwidefsky pdev->resource[i].start = 477*5b9f2081SMartin Schwidefsky (resource_size_t __force) pci_iomap(pdev, i, 0); 478cd248341SJan Glauber pdev->resource[i].end = pdev->resource[i].start + len - 1; 479cd248341SJan Glauber } 480944239c5SSebastian Ott } 481944239c5SSebastian Ott 482944239c5SSebastian Ott static void zpci_unmap_resources(struct zpci_dev *zdev) 483944239c5SSebastian Ott { 484944239c5SSebastian Ott struct pci_dev *pdev = zdev->pdev; 485944239c5SSebastian Ott resource_size_t len; 486944239c5SSebastian Ott int i; 487944239c5SSebastian Ott 488944239c5SSebastian Ott for (i = 0; i < PCI_BAR_COUNT; i++) { 489944239c5SSebastian Ott len = pci_resource_len(pdev, i); 490944239c5SSebastian Ott if (!len) 491944239c5SSebastian Ott continue; 492*5b9f2081SMartin Schwidefsky pci_iounmap(pdev, (void __iomem __force *) 493*5b9f2081SMartin Schwidefsky pdev->resource[i].start); 494944239c5SSebastian Ott } 495944239c5SSebastian Ott } 496cd248341SJan Glauber 4979a4da8a5SJan Glauber static int __init zpci_irq_init(void) 4989a4da8a5SJan Glauber { 4995d0d8f43SMartin Schwidefsky int rc; 5009a4da8a5SJan Glauber 501f4eae94fSMartin Schwidefsky rc = register_adapter_interrupt(&zpci_airq); 502f4eae94fSMartin Schwidefsky if (rc) 5035d0d8f43SMartin Schwidefsky goto out; 504f4eae94fSMartin Schwidefsky /* Set summary to 1 to be called every time for the ISC. */ 505f4eae94fSMartin Schwidefsky *zpci_airq.lsi_ptr = 1; 5069a4da8a5SJan Glauber 5075d0d8f43SMartin Schwidefsky rc = -ENOMEM; 5085d0d8f43SMartin Schwidefsky zpci_aisb_iv = airq_iv_create(ZPCI_NR_DEVICES, AIRQ_IV_ALLOC); 5095d0d8f43SMartin Schwidefsky if (!zpci_aisb_iv) 5105d0d8f43SMartin Schwidefsky goto out_airq; 5119a4da8a5SJan Glauber 5129389339fSMartin Schwidefsky zpci_set_irq_ctrl(SIC_IRQ_MODE_SINGLE, NULL, PCI_ISC); 5139a4da8a5SJan Glauber return 0; 5149a4da8a5SJan Glauber 5155d0d8f43SMartin Schwidefsky out_airq: 5165d0d8f43SMartin Schwidefsky unregister_adapter_interrupt(&zpci_airq); 5175d0d8f43SMartin Schwidefsky out: 5189a4da8a5SJan Glauber return rc; 5199a4da8a5SJan Glauber } 5209a4da8a5SJan Glauber 5219a4da8a5SJan Glauber static void zpci_irq_exit(void) 5229a4da8a5SJan Glauber { 5235d0d8f43SMartin Schwidefsky airq_iv_release(zpci_aisb_iv); 524f4eae94fSMartin Schwidefsky unregister_adapter_interrupt(&zpci_airq); 5259a4da8a5SJan Glauber } 5269a4da8a5SJan Glauber 527cd248341SJan Glauber static int zpci_alloc_iomap(struct zpci_dev *zdev) 528cd248341SJan Glauber { 529cd248341SJan Glauber int entry; 530cd248341SJan Glauber 531cd248341SJan Glauber spin_lock(&zpci_iomap_lock); 532cd248341SJan Glauber entry = find_first_zero_bit(zpci_iomap, ZPCI_IOMAP_MAX_ENTRIES); 533cd248341SJan Glauber if (entry == ZPCI_IOMAP_MAX_ENTRIES) { 534cd248341SJan Glauber spin_unlock(&zpci_iomap_lock); 535cd248341SJan Glauber return -ENOSPC; 536cd248341SJan Glauber } 537cd248341SJan Glauber set_bit(entry, zpci_iomap); 538cd248341SJan Glauber spin_unlock(&zpci_iomap_lock); 539cd248341SJan Glauber return entry; 540cd248341SJan Glauber } 541cd248341SJan Glauber 542cd248341SJan Glauber static void zpci_free_iomap(struct zpci_dev *zdev, int entry) 543cd248341SJan Glauber { 544cd248341SJan Glauber spin_lock(&zpci_iomap_lock); 545cd248341SJan Glauber memset(&zpci_iomap_start[entry], 0, sizeof(struct zpci_iomap_entry)); 546cd248341SJan Glauber clear_bit(entry, zpci_iomap); 547cd248341SJan Glauber spin_unlock(&zpci_iomap_lock); 548cd248341SJan Glauber } 549cd248341SJan Glauber 5507a572a3aSSebastian Ott static struct resource *__alloc_res(struct zpci_dev *zdev, unsigned long start, 5517a572a3aSSebastian Ott unsigned long size, unsigned long flags) 5527a572a3aSSebastian Ott { 5537a572a3aSSebastian Ott struct resource *r; 5547a572a3aSSebastian Ott 5557a572a3aSSebastian Ott r = kzalloc(sizeof(*r), GFP_KERNEL); 5567a572a3aSSebastian Ott if (!r) 5577a572a3aSSebastian Ott return NULL; 5587a572a3aSSebastian Ott 5597a572a3aSSebastian Ott r->start = start; 5607a572a3aSSebastian Ott r->end = r->start + size - 1; 5617a572a3aSSebastian Ott r->flags = flags; 5627a572a3aSSebastian Ott r->name = zdev->res_name; 5637a572a3aSSebastian Ott 5647a572a3aSSebastian Ott if (request_resource(&iomem_resource, r)) { 5657a572a3aSSebastian Ott kfree(r); 5667a572a3aSSebastian Ott return NULL; 5677a572a3aSSebastian Ott } 5687a572a3aSSebastian Ott return r; 5697a572a3aSSebastian Ott } 5707a572a3aSSebastian Ott 5717a572a3aSSebastian Ott static int zpci_setup_bus_resources(struct zpci_dev *zdev, 5727a572a3aSSebastian Ott struct list_head *resources) 5737a572a3aSSebastian Ott { 5747a572a3aSSebastian Ott unsigned long addr, size, flags; 5757a572a3aSSebastian Ott struct resource *res; 5767a572a3aSSebastian Ott int i, entry; 5777a572a3aSSebastian Ott 5787a572a3aSSebastian Ott snprintf(zdev->res_name, sizeof(zdev->res_name), 5797a572a3aSSebastian Ott "PCI Bus %04x:%02x", zdev->domain, ZPCI_BUS_NR); 5807a572a3aSSebastian Ott 5817a572a3aSSebastian Ott for (i = 0; i < PCI_BAR_COUNT; i++) { 5827a572a3aSSebastian Ott if (!zdev->bars[i].size) 5837a572a3aSSebastian Ott continue; 5847a572a3aSSebastian Ott entry = zpci_alloc_iomap(zdev); 5857a572a3aSSebastian Ott if (entry < 0) 5867a572a3aSSebastian Ott return entry; 5877a572a3aSSebastian Ott zdev->bars[i].map_idx = entry; 5887a572a3aSSebastian Ott 5897a572a3aSSebastian Ott /* only MMIO is supported */ 5907a572a3aSSebastian Ott flags = IORESOURCE_MEM; 5917a572a3aSSebastian Ott if (zdev->bars[i].val & 8) 5927a572a3aSSebastian Ott flags |= IORESOURCE_PREFETCH; 5937a572a3aSSebastian Ott if (zdev->bars[i].val & 4) 5947a572a3aSSebastian Ott flags |= IORESOURCE_MEM_64; 5957a572a3aSSebastian Ott 5967a572a3aSSebastian Ott addr = ZPCI_IOMAP_ADDR_BASE + ((u64) entry << 48); 5977a572a3aSSebastian Ott 5987a572a3aSSebastian Ott size = 1UL << zdev->bars[i].size; 5997a572a3aSSebastian Ott 6007a572a3aSSebastian Ott res = __alloc_res(zdev, addr, size, flags); 6017a572a3aSSebastian Ott if (!res) { 6027a572a3aSSebastian Ott zpci_free_iomap(zdev, entry); 6037a572a3aSSebastian Ott return -ENOMEM; 6047a572a3aSSebastian Ott } 6057a572a3aSSebastian Ott zdev->bars[i].res = res; 6067a572a3aSSebastian Ott pci_add_resource(resources, res); 6077a572a3aSSebastian Ott } 6087a572a3aSSebastian Ott 6097a572a3aSSebastian Ott return 0; 6107a572a3aSSebastian Ott } 6117a572a3aSSebastian Ott 6127a572a3aSSebastian Ott static void zpci_cleanup_bus_resources(struct zpci_dev *zdev) 6137a572a3aSSebastian Ott { 6147a572a3aSSebastian Ott int i; 6157a572a3aSSebastian Ott 6167a572a3aSSebastian Ott for (i = 0; i < PCI_BAR_COUNT; i++) { 6177a572a3aSSebastian Ott if (!zdev->bars[i].size) 6187a572a3aSSebastian Ott continue; 6197a572a3aSSebastian Ott 6207a572a3aSSebastian Ott zpci_free_iomap(zdev, zdev->bars[i].map_idx); 6217a572a3aSSebastian Ott release_resource(zdev->bars[i].res); 6227a572a3aSSebastian Ott kfree(zdev->bars[i].res); 6237a572a3aSSebastian Ott } 6247a572a3aSSebastian Ott } 6257a572a3aSSebastian Ott 626af0a8a84SSebastian Ott int pcibios_add_device(struct pci_dev *pdev) 627af0a8a84SSebastian Ott { 628af0a8a84SSebastian Ott struct zpci_dev *zdev = get_zdev(pdev); 629cb809182SSebastian Ott struct resource *res; 630cb809182SSebastian Ott int i; 631cb809182SSebastian Ott 632cb809182SSebastian Ott zdev->pdev = pdev; 633ef4858c6SSebastian Ott pdev->dev.groups = zpci_attr_groups; 634cb809182SSebastian Ott zpci_map_resources(zdev); 635cb809182SSebastian Ott 636cb809182SSebastian Ott for (i = 0; i < PCI_BAR_COUNT; i++) { 637cb809182SSebastian Ott res = &pdev->resource[i]; 638cb809182SSebastian Ott if (res->parent || !res->flags) 639cb809182SSebastian Ott continue; 640cb809182SSebastian Ott pci_claim_resource(pdev, i); 641cb809182SSebastian Ott } 642cb809182SSebastian Ott 643cb809182SSebastian Ott return 0; 644cb809182SSebastian Ott } 645cb809182SSebastian Ott 646cb809182SSebastian Ott int pcibios_enable_device(struct pci_dev *pdev, int mask) 647cb809182SSebastian Ott { 648cb809182SSebastian Ott struct zpci_dev *zdev = get_zdev(pdev); 649af0a8a84SSebastian Ott 6501c21351bSSebastian Ott zdev->pdev = pdev; 651af0a8a84SSebastian Ott zpci_debug_init_device(zdev); 652af0a8a84SSebastian Ott zpci_fmb_enable_device(zdev); 653af0a8a84SSebastian Ott zpci_map_resources(zdev); 654af0a8a84SSebastian Ott 655d7533232SBjorn Helgaas return pci_enable_resources(pdev, mask); 656af0a8a84SSebastian Ott } 657af0a8a84SSebastian Ott 658cb809182SSebastian Ott void pcibios_disable_device(struct pci_dev *pdev) 659944239c5SSebastian Ott { 660944239c5SSebastian Ott struct zpci_dev *zdev = get_zdev(pdev); 661944239c5SSebastian Ott 662944239c5SSebastian Ott zpci_unmap_resources(zdev); 663944239c5SSebastian Ott zpci_fmb_disable_device(zdev); 664944239c5SSebastian Ott zpci_debug_exit_device(zdev); 665944239c5SSebastian Ott zdev->pdev = NULL; 666944239c5SSebastian Ott } 667944239c5SSebastian Ott 66869db3b5eSSebastian Ott #ifdef CONFIG_HIBERNATE_CALLBACKS 66969db3b5eSSebastian Ott static int zpci_restore(struct device *dev) 67069db3b5eSSebastian Ott { 67169db3b5eSSebastian Ott struct zpci_dev *zdev = get_zdev(to_pci_dev(dev)); 67269db3b5eSSebastian Ott int ret = 0; 67369db3b5eSSebastian Ott 67469db3b5eSSebastian Ott if (zdev->state != ZPCI_FN_STATE_ONLINE) 67569db3b5eSSebastian Ott goto out; 67669db3b5eSSebastian Ott 67769db3b5eSSebastian Ott ret = clp_enable_fh(zdev, ZPCI_NR_DMA_SPACES); 67869db3b5eSSebastian Ott if (ret) 67969db3b5eSSebastian Ott goto out; 68069db3b5eSSebastian Ott 68169db3b5eSSebastian Ott zpci_map_resources(zdev); 68269db3b5eSSebastian Ott zpci_register_ioat(zdev, 0, zdev->start_dma + PAGE_OFFSET, 68369db3b5eSSebastian Ott zdev->start_dma + zdev->iommu_size - 1, 68469db3b5eSSebastian Ott (u64) zdev->dma_table); 68569db3b5eSSebastian Ott 68669db3b5eSSebastian Ott out: 68769db3b5eSSebastian Ott return ret; 68869db3b5eSSebastian Ott } 68969db3b5eSSebastian Ott 69069db3b5eSSebastian Ott static int zpci_freeze(struct device *dev) 69169db3b5eSSebastian Ott { 69269db3b5eSSebastian Ott struct zpci_dev *zdev = get_zdev(to_pci_dev(dev)); 69369db3b5eSSebastian Ott 69469db3b5eSSebastian Ott if (zdev->state != ZPCI_FN_STATE_ONLINE) 69569db3b5eSSebastian Ott return 0; 69669db3b5eSSebastian Ott 69769db3b5eSSebastian Ott zpci_unregister_ioat(zdev, 0); 69869db3b5eSSebastian Ott return clp_disable_fh(zdev); 69969db3b5eSSebastian Ott } 70069db3b5eSSebastian Ott 70169db3b5eSSebastian Ott struct dev_pm_ops pcibios_pm_ops = { 70269db3b5eSSebastian Ott .thaw_noirq = zpci_restore, 70369db3b5eSSebastian Ott .freeze_noirq = zpci_freeze, 70469db3b5eSSebastian Ott .restore_noirq = zpci_restore, 70569db3b5eSSebastian Ott .poweroff_noirq = zpci_freeze, 70669db3b5eSSebastian Ott }; 70769db3b5eSSebastian Ott #endif /* CONFIG_HIBERNATE_CALLBACKS */ 70869db3b5eSSebastian Ott 709cd248341SJan Glauber static int zpci_alloc_domain(struct zpci_dev *zdev) 710cd248341SJan Glauber { 711cd248341SJan Glauber spin_lock(&zpci_domain_lock); 712cd248341SJan Glauber zdev->domain = find_first_zero_bit(zpci_domain, ZPCI_NR_DEVICES); 713cd248341SJan Glauber if (zdev->domain == ZPCI_NR_DEVICES) { 714cd248341SJan Glauber spin_unlock(&zpci_domain_lock); 715cd248341SJan Glauber return -ENOSPC; 716cd248341SJan Glauber } 717cd248341SJan Glauber set_bit(zdev->domain, zpci_domain); 718cd248341SJan Glauber spin_unlock(&zpci_domain_lock); 719cd248341SJan Glauber return 0; 720cd248341SJan Glauber } 721cd248341SJan Glauber 722cd248341SJan Glauber static void zpci_free_domain(struct zpci_dev *zdev) 723cd248341SJan Glauber { 724cd248341SJan Glauber spin_lock(&zpci_domain_lock); 725cd248341SJan Glauber clear_bit(zdev->domain, zpci_domain); 726cd248341SJan Glauber spin_unlock(&zpci_domain_lock); 727cd248341SJan Glauber } 728cd248341SJan Glauber 7297d594322SSebastian Ott void pcibios_remove_bus(struct pci_bus *bus) 7307d594322SSebastian Ott { 7317d594322SSebastian Ott struct zpci_dev *zdev = get_zdev_by_bus(bus); 7327d594322SSebastian Ott 7337d594322SSebastian Ott zpci_exit_slot(zdev); 7347d594322SSebastian Ott zpci_cleanup_bus_resources(zdev); 7357d594322SSebastian Ott zpci_free_domain(zdev); 7367d594322SSebastian Ott 7377d594322SSebastian Ott spin_lock(&zpci_list_lock); 7387d594322SSebastian Ott list_del(&zdev->entry); 7397d594322SSebastian Ott spin_unlock(&zpci_list_lock); 7407d594322SSebastian Ott 7417d594322SSebastian Ott kfree(zdev); 7427d594322SSebastian Ott } 7437d594322SSebastian Ott 7447d594322SSebastian Ott static int zpci_scan_bus(struct zpci_dev *zdev) 7457d594322SSebastian Ott { 7467d594322SSebastian Ott LIST_HEAD(resources); 7477d594322SSebastian Ott int ret; 7487d594322SSebastian Ott 7497d594322SSebastian Ott ret = zpci_setup_bus_resources(zdev, &resources); 7507d594322SSebastian Ott if (ret) 7517d594322SSebastian Ott return ret; 7527d594322SSebastian Ott 7537d594322SSebastian Ott zdev->bus = pci_scan_root_bus(NULL, ZPCI_BUS_NR, &pci_root_ops, 7547d594322SSebastian Ott zdev, &resources); 7557d594322SSebastian Ott if (!zdev->bus) { 7567d594322SSebastian Ott zpci_cleanup_bus_resources(zdev); 7577d594322SSebastian Ott return -EIO; 7587d594322SSebastian Ott } 7597d594322SSebastian Ott 7607d594322SSebastian Ott zdev->bus->max_bus_speed = zdev->max_bus_speed; 7617d594322SSebastian Ott return 0; 7627d594322SSebastian Ott } 7637d594322SSebastian Ott 764a755a45dSJan Glauber int zpci_enable_device(struct zpci_dev *zdev) 765a755a45dSJan Glauber { 766a755a45dSJan Glauber int rc; 767a755a45dSJan Glauber 768a755a45dSJan Glauber rc = clp_enable_fh(zdev, ZPCI_NR_DMA_SPACES); 769a755a45dSJan Glauber if (rc) 770a755a45dSJan Glauber goto out; 771828b35f6SJan Glauber 772828b35f6SJan Glauber rc = zpci_dma_init_device(zdev); 773828b35f6SJan Glauber if (rc) 774828b35f6SJan Glauber goto out_dma; 7750ff70ec8SSebastian Ott 7760ff70ec8SSebastian Ott zdev->state = ZPCI_FN_STATE_ONLINE; 777a755a45dSJan Glauber return 0; 778828b35f6SJan Glauber 779828b35f6SJan Glauber out_dma: 780828b35f6SJan Glauber clp_disable_fh(zdev); 781a755a45dSJan Glauber out: 782a755a45dSJan Glauber return rc; 783a755a45dSJan Glauber } 784a755a45dSJan Glauber EXPORT_SYMBOL_GPL(zpci_enable_device); 785a755a45dSJan Glauber 786cb65a669SSebastian Ott int zpci_disable_device(struct zpci_dev *zdev) 787cb65a669SSebastian Ott { 788cb65a669SSebastian Ott zpci_dma_exit_device(zdev); 789cb65a669SSebastian Ott return clp_disable_fh(zdev); 790cb65a669SSebastian Ott } 791cb65a669SSebastian Ott EXPORT_SYMBOL_GPL(zpci_disable_device); 792cb65a669SSebastian Ott 793cd248341SJan Glauber int zpci_create_device(struct zpci_dev *zdev) 794cd248341SJan Glauber { 795cd248341SJan Glauber int rc; 796cd248341SJan Glauber 797cd248341SJan Glauber rc = zpci_alloc_domain(zdev); 798cd248341SJan Glauber if (rc) 799cd248341SJan Glauber goto out; 800cd248341SJan Glauber 8011c21351bSSebastian Ott if (zdev->state == ZPCI_FN_STATE_CONFIGURED) { 8021c21351bSSebastian Ott rc = zpci_enable_device(zdev); 803cd248341SJan Glauber if (rc) 8041c21351bSSebastian Ott goto out_free; 8051c21351bSSebastian Ott } 8061c21351bSSebastian Ott rc = zpci_scan_bus(zdev); 8071c21351bSSebastian Ott if (rc) 8081c21351bSSebastian Ott goto out_disable; 809cd248341SJan Glauber 81057b5918cSSebastian Ott spin_lock(&zpci_list_lock); 811cd248341SJan Glauber list_add_tail(&zdev->entry, &zpci_list); 81257b5918cSSebastian Ott spin_unlock(&zpci_list_lock); 813cd248341SJan Glauber 81467f43f38SSebastian Ott zpci_init_slot(zdev); 81567f43f38SSebastian Ott 816cd248341SJan Glauber return 0; 817cd248341SJan Glauber 8181c21351bSSebastian Ott out_disable: 8191c21351bSSebastian Ott if (zdev->state == ZPCI_FN_STATE_ONLINE) 8201c21351bSSebastian Ott zpci_disable_device(zdev); 8211c21351bSSebastian Ott out_free: 822cd248341SJan Glauber zpci_free_domain(zdev); 823cd248341SJan Glauber out: 824cd248341SJan Glauber return rc; 825cd248341SJan Glauber } 826cd248341SJan Glauber 827cd248341SJan Glauber void zpci_stop_device(struct zpci_dev *zdev) 828cd248341SJan Glauber { 829828b35f6SJan Glauber zpci_dma_exit_device(zdev); 830cd248341SJan Glauber /* 831cd248341SJan Glauber * Note: SCLP disables fh via set-pci-fn so don't 832cd248341SJan Glauber * do that here. 833cd248341SJan Glauber */ 834cd248341SJan Glauber } 835cd248341SJan Glauber EXPORT_SYMBOL_GPL(zpci_stop_device); 836cd248341SJan Glauber 837cd248341SJan Glauber static inline int barsize(u8 size) 838cd248341SJan Glauber { 839cd248341SJan Glauber return (size) ? (1 << size) >> 10 : 0; 840cd248341SJan Glauber } 841cd248341SJan Glauber 842cd248341SJan Glauber static int zpci_mem_init(void) 843cd248341SJan Glauber { 844d0b08853SJan Glauber zdev_fmb_cache = kmem_cache_create("PCI_FMB_cache", sizeof(struct zpci_fmb), 845d0b08853SJan Glauber 16, 0, NULL); 846d0b08853SJan Glauber if (!zdev_fmb_cache) 8471f44a225SMartin Schwidefsky goto error_zdev; 848d0b08853SJan Glauber 849cd248341SJan Glauber /* TODO: use realloc */ 850cd248341SJan Glauber zpci_iomap_start = kzalloc(ZPCI_IOMAP_MAX_ENTRIES * sizeof(*zpci_iomap_start), 851cd248341SJan Glauber GFP_KERNEL); 852cd248341SJan Glauber if (!zpci_iomap_start) 8539a4da8a5SJan Glauber goto error_iomap; 854cd248341SJan Glauber return 0; 855cd248341SJan Glauber 8569a4da8a5SJan Glauber error_iomap: 857d0b08853SJan Glauber kmem_cache_destroy(zdev_fmb_cache); 858cd248341SJan Glauber error_zdev: 859cd248341SJan Glauber return -ENOMEM; 860cd248341SJan Glauber } 861cd248341SJan Glauber 862cd248341SJan Glauber static void zpci_mem_exit(void) 863cd248341SJan Glauber { 864cd248341SJan Glauber kfree(zpci_iomap_start); 865d0b08853SJan Glauber kmem_cache_destroy(zdev_fmb_cache); 866cd248341SJan Glauber } 867cd248341SJan Glauber 868257608fbSSebastian Ott static unsigned int s390_pci_probe = 1; 869aa3b7c29SSebastian Ott static unsigned int s390_pci_initialized; 870cd248341SJan Glauber 871cd248341SJan Glauber char * __init pcibios_setup(char *str) 872cd248341SJan Glauber { 873257608fbSSebastian Ott if (!strcmp(str, "off")) { 874257608fbSSebastian Ott s390_pci_probe = 0; 875cd248341SJan Glauber return NULL; 876cd248341SJan Glauber } 877cd248341SJan Glauber return str; 878cd248341SJan Glauber } 879cd248341SJan Glauber 880aa3b7c29SSebastian Ott bool zpci_is_enabled(void) 881aa3b7c29SSebastian Ott { 882aa3b7c29SSebastian Ott return s390_pci_initialized; 883aa3b7c29SSebastian Ott } 884aa3b7c29SSebastian Ott 885cd248341SJan Glauber static int __init pci_base_init(void) 886cd248341SJan Glauber { 887cd248341SJan Glauber int rc; 888cd248341SJan Glauber 8891e5635d1SHeiko Carstens if (!s390_pci_probe) 890cd248341SJan Glauber return 0; 891cd248341SJan Glauber 892cd248341SJan Glauber if (!test_facility(2) || !test_facility(69) 893cd248341SJan Glauber || !test_facility(71) || !test_facility(72)) 894cd248341SJan Glauber return 0; 895cd248341SJan Glauber 896d0b08853SJan Glauber rc = zpci_debug_init(); 897d0b08853SJan Glauber if (rc) 8981f44a225SMartin Schwidefsky goto out; 899d0b08853SJan Glauber 900cd248341SJan Glauber rc = zpci_mem_init(); 901cd248341SJan Glauber if (rc) 902cd248341SJan Glauber goto out_mem; 903cd248341SJan Glauber 9049a4da8a5SJan Glauber rc = zpci_irq_init(); 9059a4da8a5SJan Glauber if (rc) 9069a4da8a5SJan Glauber goto out_irq; 9079a4da8a5SJan Glauber 908828b35f6SJan Glauber rc = zpci_dma_init(); 909828b35f6SJan Glauber if (rc) 910828b35f6SJan Glauber goto out_dma; 911828b35f6SJan Glauber 9121d578966SSebastian Ott rc = clp_scan_pci_devices(); 913a755a45dSJan Glauber if (rc) 914a755a45dSJan Glauber goto out_find; 915a755a45dSJan Glauber 916aa3b7c29SSebastian Ott s390_pci_initialized = 1; 917cd248341SJan Glauber return 0; 918cd248341SJan Glauber 919a755a45dSJan Glauber out_find: 920828b35f6SJan Glauber zpci_dma_exit(); 921828b35f6SJan Glauber out_dma: 9229a4da8a5SJan Glauber zpci_irq_exit(); 9239a4da8a5SJan Glauber out_irq: 924cd248341SJan Glauber zpci_mem_exit(); 925cd248341SJan Glauber out_mem: 926d0b08853SJan Glauber zpci_debug_exit(); 9271f44a225SMartin Schwidefsky out: 928cd248341SJan Glauber return rc; 929cd248341SJan Glauber } 93067f43f38SSebastian Ott subsys_initcall_sync(pci_base_init); 93157b5918cSSebastian Ott 93257b5918cSSebastian Ott void zpci_rescan(void) 93357b5918cSSebastian Ott { 934aa3b7c29SSebastian Ott if (zpci_is_enabled()) 93557b5918cSSebastian Ott clp_rescan_pci_devices_simple(); 93657b5918cSSebastian Ott } 937