1cd248341SJan Glauber /* 2cd248341SJan Glauber * Copyright IBM Corp. 2012 3cd248341SJan Glauber * 4cd248341SJan Glauber * Author(s): 5cd248341SJan Glauber * Jan Glauber <jang@linux.vnet.ibm.com> 6cd248341SJan Glauber * 7cd248341SJan Glauber * The System z PCI code is a rewrite from a prototype by 8cd248341SJan Glauber * the following people (Kudoz!): 9bedef755SJan Glauber * Alexander Schmidt 10bedef755SJan Glauber * Christoph Raisch 11bedef755SJan Glauber * Hannes Hering 12bedef755SJan Glauber * Hoang-Nam Nguyen 13bedef755SJan Glauber * Jan-Bernd Themann 14bedef755SJan Glauber * Stefan Roscher 15bedef755SJan Glauber * Thomas Klein 16cd248341SJan Glauber */ 17cd248341SJan Glauber 18896cb7e6SGerald Schaefer #define KMSG_COMPONENT "zpci" 19896cb7e6SGerald Schaefer #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt 20cd248341SJan Glauber 21cd248341SJan Glauber #include <linux/kernel.h> 22cd248341SJan Glauber #include <linux/slab.h> 23cd248341SJan Glauber #include <linux/err.h> 24cd248341SJan Glauber #include <linux/export.h> 25cd248341SJan Glauber #include <linux/delay.h> 269a4da8a5SJan Glauber #include <linux/irq.h> 279a4da8a5SJan Glauber #include <linux/kernel_stat.h> 28cd248341SJan Glauber #include <linux/seq_file.h> 29cd248341SJan Glauber #include <linux/pci.h> 30cd248341SJan Glauber #include <linux/msi.h> 31cd248341SJan Glauber 329a4da8a5SJan Glauber #include <asm/isc.h> 339a4da8a5SJan Glauber #include <asm/airq.h> 34cd248341SJan Glauber #include <asm/facility.h> 35cd248341SJan Glauber #include <asm/pci_insn.h> 36a755a45dSJan Glauber #include <asm/pci_clp.h> 37828b35f6SJan Glauber #include <asm/pci_dma.h> 38cd248341SJan Glauber 39cd248341SJan Glauber #define DEBUG /* enable pr_debug */ 40cd248341SJan Glauber 419a4da8a5SJan Glauber #define SIC_IRQ_MODE_ALL 0 429a4da8a5SJan Glauber #define SIC_IRQ_MODE_SINGLE 1 439a4da8a5SJan Glauber 44cd248341SJan Glauber #define ZPCI_NR_DMA_SPACES 1 45cd248341SJan Glauber #define ZPCI_NR_DEVICES CONFIG_PCI_NR_FUNCTIONS 46cd248341SJan Glauber 47cd248341SJan Glauber /* list of all detected zpci devices */ 4867f43f38SSebastian Ott static LIST_HEAD(zpci_list); 4957b5918cSSebastian Ott static DEFINE_SPINLOCK(zpci_list_lock); 501f44a225SMartin Schwidefsky 511f44a225SMartin Schwidefsky static struct irq_chip zpci_irq_chip = { 521f44a225SMartin Schwidefsky .name = "zPCI", 53280510f1SThomas Gleixner .irq_unmask = pci_msi_unmask_irq, 54280510f1SThomas Gleixner .irq_mask = pci_msi_mask_irq, 551f44a225SMartin Schwidefsky }; 561f44a225SMartin Schwidefsky 57cd248341SJan Glauber static DECLARE_BITMAP(zpci_domain, ZPCI_NR_DEVICES); 58cd248341SJan Glauber static DEFINE_SPINLOCK(zpci_domain_lock); 59cd248341SJan Glauber 605d0d8f43SMartin Schwidefsky static struct airq_iv *zpci_aisb_iv; 611f44a225SMartin Schwidefsky static struct airq_iv *zpci_aibv[ZPCI_NR_DEVICES]; 629a4da8a5SJan Glauber 63c506fff3SSebastian Ott #define ZPCI_IOMAP_ENTRIES \ 64b0c8ce89SSebastian Ott min(((unsigned long) ZPCI_NR_DEVICES * PCI_BAR_COUNT / 2), \ 65c506fff3SSebastian Ott ZPCI_IOMAP_MAX_ENTRIES) 66c506fff3SSebastian Ott 67cd248341SJan Glauber static DEFINE_SPINLOCK(zpci_iomap_lock); 68c506fff3SSebastian Ott static unsigned long *zpci_iomap_bitmap; 69cd248341SJan Glauber struct zpci_iomap_entry *zpci_iomap_start; 70cd248341SJan Glauber EXPORT_SYMBOL_GPL(zpci_iomap_start); 71cd248341SJan Glauber 72d0b08853SJan Glauber static struct kmem_cache *zdev_fmb_cache; 73d0b08853SJan Glauber 74cd248341SJan Glauber struct zpci_dev *get_zdev_by_fid(u32 fid) 75cd248341SJan Glauber { 76cd248341SJan Glauber struct zpci_dev *tmp, *zdev = NULL; 77cd248341SJan Glauber 7857b5918cSSebastian Ott spin_lock(&zpci_list_lock); 79cd248341SJan Glauber list_for_each_entry(tmp, &zpci_list, entry) { 80cd248341SJan Glauber if (tmp->fid == fid) { 81cd248341SJan Glauber zdev = tmp; 82cd248341SJan Glauber break; 83cd248341SJan Glauber } 84cd248341SJan Glauber } 8557b5918cSSebastian Ott spin_unlock(&zpci_list_lock); 86cd248341SJan Glauber return zdev; 87cd248341SJan Glauber } 88cd248341SJan Glauber 8901553d9aSSebastian Ott void zpci_remove_reserved_devices(void) 9001553d9aSSebastian Ott { 9101553d9aSSebastian Ott struct zpci_dev *tmp, *zdev; 9201553d9aSSebastian Ott enum zpci_state state; 9301553d9aSSebastian Ott LIST_HEAD(remove); 9401553d9aSSebastian Ott 9501553d9aSSebastian Ott spin_lock(&zpci_list_lock); 9601553d9aSSebastian Ott list_for_each_entry_safe(zdev, tmp, &zpci_list, entry) { 9701553d9aSSebastian Ott if (zdev->state == ZPCI_FN_STATE_STANDBY && 9801553d9aSSebastian Ott !clp_get_state(zdev->fid, &state) && 9901553d9aSSebastian Ott state == ZPCI_FN_STATE_RESERVED) 10001553d9aSSebastian Ott list_move_tail(&zdev->entry, &remove); 10101553d9aSSebastian Ott } 10201553d9aSSebastian Ott spin_unlock(&zpci_list_lock); 10301553d9aSSebastian Ott 10401553d9aSSebastian Ott list_for_each_entry_safe(zdev, tmp, &remove, entry) 10501553d9aSSebastian Ott zpci_remove_device(zdev); 10601553d9aSSebastian Ott } 10701553d9aSSebastian Ott 108cd248341SJan Glauber static struct zpci_dev *get_zdev_by_bus(struct pci_bus *bus) 109cd248341SJan Glauber { 110cd248341SJan Glauber return (bus && bus->sysdata) ? (struct zpci_dev *) bus->sysdata : NULL; 111cd248341SJan Glauber } 112cd248341SJan Glauber 113cd248341SJan Glauber int pci_domain_nr(struct pci_bus *bus) 114cd248341SJan Glauber { 115cd248341SJan Glauber return ((struct zpci_dev *) bus->sysdata)->domain; 116cd248341SJan Glauber } 117cd248341SJan Glauber EXPORT_SYMBOL_GPL(pci_domain_nr); 118cd248341SJan Glauber 119cd248341SJan Glauber int pci_proc_domain(struct pci_bus *bus) 120cd248341SJan Glauber { 121cd248341SJan Glauber return pci_domain_nr(bus); 122cd248341SJan Glauber } 123cd248341SJan Glauber EXPORT_SYMBOL_GPL(pci_proc_domain); 124cd248341SJan Glauber 1259a4da8a5SJan Glauber /* Modify PCI: Register adapter interruptions */ 1265d0d8f43SMartin Schwidefsky static int zpci_set_airq(struct zpci_dev *zdev) 1279a4da8a5SJan Glauber { 1289a4da8a5SJan Glauber u64 req = ZPCI_CREATE_REQ(zdev->fh, 0, ZPCI_MOD_FC_REG_INT); 129cb4deb69SSebastian Ott struct zpci_fib fib = {0}; 1304dfbd3efSSebastian Ott u8 status; 1319a4da8a5SJan Glauber 132cb4deb69SSebastian Ott fib.isc = PCI_ISC; 133cb4deb69SSebastian Ott fib.sum = 1; /* enable summary notifications */ 134cb4deb69SSebastian Ott fib.noi = airq_iv_end(zdev->aibv); 135cb4deb69SSebastian Ott fib.aibv = (unsigned long) zdev->aibv->vector; 136cb4deb69SSebastian Ott fib.aibvo = 0; /* each zdev has its own interrupt vector */ 137cb4deb69SSebastian Ott fib.aisb = (unsigned long) zpci_aisb_iv->vector + (zdev->aisb/64)*8; 138cb4deb69SSebastian Ott fib.aisbo = zdev->aisb & 63; 1399a4da8a5SJan Glauber 1404dfbd3efSSebastian Ott return zpci_mod_fc(req, &fib, &status) ? -EIO : 0; 1414dfbd3efSSebastian Ott } 1424dfbd3efSSebastian Ott 1434dfbd3efSSebastian Ott /* Modify PCI: Unregister adapter interruptions */ 1444dfbd3efSSebastian Ott static int zpci_clear_airq(struct zpci_dev *zdev) 1454dfbd3efSSebastian Ott { 1464dfbd3efSSebastian Ott u64 req = ZPCI_CREATE_REQ(zdev->fh, 0, ZPCI_MOD_FC_DEREG_INT); 1474dfbd3efSSebastian Ott struct zpci_fib fib = {0}; 1484dfbd3efSSebastian Ott u8 cc, status; 1494dfbd3efSSebastian Ott 1504dfbd3efSSebastian Ott cc = zpci_mod_fc(req, &fib, &status); 1514dfbd3efSSebastian Ott if (cc == 3 || (cc == 1 && status == 24)) 1524dfbd3efSSebastian Ott /* Function already gone or IRQs already deregistered. */ 1534dfbd3efSSebastian Ott cc = 0; 1544dfbd3efSSebastian Ott 1554dfbd3efSSebastian Ott return cc ? -EIO : 0; 1569a4da8a5SJan Glauber } 1579a4da8a5SJan Glauber 158828b35f6SJan Glauber /* Modify PCI: Register I/O address translation parameters */ 159828b35f6SJan Glauber int zpci_register_ioat(struct zpci_dev *zdev, u8 dmaas, 160828b35f6SJan Glauber u64 base, u64 limit, u64 iota) 161828b35f6SJan Glauber { 16272570834SSebastian Ott u64 req = ZPCI_CREATE_REQ(zdev->fh, dmaas, ZPCI_MOD_FC_REG_IOAT); 16372570834SSebastian Ott struct zpci_fib fib = {0}; 16472570834SSebastian Ott u8 status; 165828b35f6SJan Glauber 166828b35f6SJan Glauber WARN_ON_ONCE(iota & 0x3fff); 16772570834SSebastian Ott fib.pba = base; 16872570834SSebastian Ott fib.pal = limit; 16972570834SSebastian Ott fib.iota = iota | ZPCI_IOTA_RTTO_FLAG; 17072570834SSebastian Ott return zpci_mod_fc(req, &fib, &status) ? -EIO : 0; 171828b35f6SJan Glauber } 172828b35f6SJan Glauber 173828b35f6SJan Glauber /* Modify PCI: Unregister I/O address translation parameters */ 174828b35f6SJan Glauber int zpci_unregister_ioat(struct zpci_dev *zdev, u8 dmaas) 175828b35f6SJan Glauber { 17672570834SSebastian Ott u64 req = ZPCI_CREATE_REQ(zdev->fh, dmaas, ZPCI_MOD_FC_DEREG_IOAT); 17772570834SSebastian Ott struct zpci_fib fib = {0}; 17872570834SSebastian Ott u8 cc, status; 179828b35f6SJan Glauber 18072570834SSebastian Ott cc = zpci_mod_fc(req, &fib, &status); 18172570834SSebastian Ott if (cc == 3) /* Function already gone. */ 18272570834SSebastian Ott cc = 0; 18372570834SSebastian Ott return cc ? -EIO : 0; 184828b35f6SJan Glauber } 185828b35f6SJan Glauber 186d0b08853SJan Glauber /* Modify PCI: Set PCI function measurement parameters */ 187d0b08853SJan Glauber int zpci_fmb_enable_device(struct zpci_dev *zdev) 188d0b08853SJan Glauber { 1894e5bd780SSebastian Ott u64 req = ZPCI_CREATE_REQ(zdev->fh, 0, ZPCI_MOD_FC_SET_MEASURE); 1904e5bd780SSebastian Ott struct zpci_fib fib = {0}; 1914e5bd780SSebastian Ott u8 cc, status; 192d0b08853SJan Glauber 1930b7589ecSSebastian Ott if (zdev->fmb || sizeof(*zdev->fmb) < zdev->fmb_length) 194d0b08853SJan Glauber return -EINVAL; 195d0b08853SJan Glauber 19608b42124SWei Yongjun zdev->fmb = kmem_cache_zalloc(zdev_fmb_cache, GFP_KERNEL); 197d0b08853SJan Glauber if (!zdev->fmb) 198d0b08853SJan Glauber return -ENOMEM; 199d0b08853SJan Glauber WARN_ON((u64) zdev->fmb & 0xf); 200d0b08853SJan Glauber 2016001018aSSebastian Ott /* reset software counters */ 2026001018aSSebastian Ott atomic64_set(&zdev->allocated_pages, 0); 2036001018aSSebastian Ott atomic64_set(&zdev->mapped_pages, 0); 2046001018aSSebastian Ott atomic64_set(&zdev->unmapped_pages, 0); 2056001018aSSebastian Ott 2064e5bd780SSebastian Ott fib.fmb_addr = virt_to_phys(zdev->fmb); 2074e5bd780SSebastian Ott cc = zpci_mod_fc(req, &fib, &status); 2084e5bd780SSebastian Ott if (cc) { 2094e5bd780SSebastian Ott kmem_cache_free(zdev_fmb_cache, zdev->fmb); 2104e5bd780SSebastian Ott zdev->fmb = NULL; 2114e5bd780SSebastian Ott } 2124e5bd780SSebastian Ott return cc ? -EIO : 0; 213d0b08853SJan Glauber } 214d0b08853SJan Glauber 215d0b08853SJan Glauber /* Modify PCI: Disable PCI function measurement */ 216d0b08853SJan Glauber int zpci_fmb_disable_device(struct zpci_dev *zdev) 217d0b08853SJan Glauber { 2184e5bd780SSebastian Ott u64 req = ZPCI_CREATE_REQ(zdev->fh, 0, ZPCI_MOD_FC_SET_MEASURE); 2194e5bd780SSebastian Ott struct zpci_fib fib = {0}; 2204e5bd780SSebastian Ott u8 cc, status; 221d0b08853SJan Glauber 222d0b08853SJan Glauber if (!zdev->fmb) 223d0b08853SJan Glauber return -EINVAL; 224d0b08853SJan Glauber 225d0b08853SJan Glauber /* Function measurement is disabled if fmb address is zero */ 2264e5bd780SSebastian Ott cc = zpci_mod_fc(req, &fib, &status); 2274e5bd780SSebastian Ott if (cc == 3) /* Function already gone. */ 2284e5bd780SSebastian Ott cc = 0; 229d0b08853SJan Glauber 2304e5bd780SSebastian Ott if (!cc) { 231d0b08853SJan Glauber kmem_cache_free(zdev_fmb_cache, zdev->fmb); 232d0b08853SJan Glauber zdev->fmb = NULL; 2334e5bd780SSebastian Ott } 2344e5bd780SSebastian Ott return cc ? -EIO : 0; 235d0b08853SJan Glauber } 236d0b08853SJan Glauber 237cd248341SJan Glauber static int zpci_cfg_load(struct zpci_dev *zdev, int offset, u32 *val, u8 len) 238cd248341SJan Glauber { 239cd248341SJan Glauber u64 req = ZPCI_CREATE_REQ(zdev->fh, ZPCI_PCIAS_CFGSPC, len); 240cd248341SJan Glauber u64 data; 241cd248341SJan Glauber int rc; 242cd248341SJan Glauber 2439389339fSMartin Schwidefsky rc = zpci_load(&data, req, offset); 244b170bad4SSebastian Ott if (!rc) { 2455064cd35SSebastian Ott data = le64_to_cpu((__force __le64) data); 2465064cd35SSebastian Ott data >>= (8 - len) * 8; 247cd248341SJan Glauber *val = (u32) data; 248b170bad4SSebastian Ott } else 249cd248341SJan Glauber *val = 0xffffffff; 250cd248341SJan Glauber return rc; 251cd248341SJan Glauber } 252cd248341SJan Glauber 253cd248341SJan Glauber static int zpci_cfg_store(struct zpci_dev *zdev, int offset, u32 val, u8 len) 254cd248341SJan Glauber { 255cd248341SJan Glauber u64 req = ZPCI_CREATE_REQ(zdev->fh, ZPCI_PCIAS_CFGSPC, len); 256cd248341SJan Glauber u64 data = val; 257cd248341SJan Glauber int rc; 258cd248341SJan Glauber 2595064cd35SSebastian Ott data <<= (8 - len) * 8; 2605064cd35SSebastian Ott data = (__force u64) cpu_to_le64(data); 2619389339fSMartin Schwidefsky rc = zpci_store(data, req, offset); 262cd248341SJan Glauber return rc; 263cd248341SJan Glauber } 264cd248341SJan Glauber 265b881bc46SGreg Kroah-Hartman void pcibios_fixup_bus(struct pci_bus *bus) 266cd248341SJan Glauber { 267cd248341SJan Glauber } 268cd248341SJan Glauber 269cd248341SJan Glauber resource_size_t pcibios_align_resource(void *data, const struct resource *res, 270cd248341SJan Glauber resource_size_t size, 271cd248341SJan Glauber resource_size_t align) 272cd248341SJan Glauber { 273cd248341SJan Glauber return 0; 274cd248341SJan Glauber } 275cd248341SJan Glauber 27687bc359bSJan Glauber /* combine single writes by using store-block insn */ 27787bc359bSJan Glauber void __iowrite64_copy(void __iomem *to, const void *from, size_t count) 27887bc359bSJan Glauber { 27987bc359bSJan Glauber zpci_memcpy_toio(to, from, count); 28087bc359bSJan Glauber } 28187bc359bSJan Glauber 282cd248341SJan Glauber /* Create a virtual mapping cookie for a PCI BAR */ 2838cfc99b5SMichael S. Tsirkin void __iomem *pci_iomap_range(struct pci_dev *pdev, 2848cfc99b5SMichael S. Tsirkin int bar, 2858cfc99b5SMichael S. Tsirkin unsigned long offset, 2868cfc99b5SMichael S. Tsirkin unsigned long max) 287cd248341SJan Glauber { 288198a5278SSebastian Ott struct zpci_dev *zdev = to_zpci(pdev); 289cd248341SJan Glauber int idx; 290cd248341SJan Glauber 291c0cabaddSSebastian Ott if (!pci_resource_len(pdev, bar)) 292cd248341SJan Glauber return NULL; 293cd248341SJan Glauber 294cd248341SJan Glauber idx = zdev->bars[bar].map_idx; 295cd248341SJan Glauber spin_lock(&zpci_iomap_lock); 296f5e44f82SSebastian Ott /* Detect overrun */ 297f5e44f82SSebastian Ott WARN_ON(!++zpci_iomap_start[idx].count); 298cd248341SJan Glauber zpci_iomap_start[idx].fh = zdev->fh; 299cd248341SJan Glauber zpci_iomap_start[idx].bar = bar; 300cd248341SJan Glauber spin_unlock(&zpci_iomap_lock); 301cd248341SJan Glauber 3029e00caaeSSebastian Ott return (void __iomem *) ZPCI_ADDR(idx) + offset; 303cd248341SJan Glauber } 304d9426083SSebastian Ott EXPORT_SYMBOL(pci_iomap_range); 3058cfc99b5SMichael S. Tsirkin 3068cfc99b5SMichael S. Tsirkin void __iomem *pci_iomap(struct pci_dev *dev, int bar, unsigned long maxlen) 3078cfc99b5SMichael S. Tsirkin { 3088cfc99b5SMichael S. Tsirkin return pci_iomap_range(dev, bar, 0, maxlen); 3098cfc99b5SMichael S. Tsirkin } 3108cfc99b5SMichael S. Tsirkin EXPORT_SYMBOL(pci_iomap); 311cd248341SJan Glauber 312cd248341SJan Glauber void pci_iounmap(struct pci_dev *pdev, void __iomem *addr) 313cd248341SJan Glauber { 3149e00caaeSSebastian Ott unsigned int idx = ZPCI_IDX(addr); 315cd248341SJan Glauber 316cd248341SJan Glauber spin_lock(&zpci_iomap_lock); 3178cfc99b5SMichael S. Tsirkin /* Detect underrun */ 318f5e44f82SSebastian Ott WARN_ON(!zpci_iomap_start[idx].count); 3198cfc99b5SMichael S. Tsirkin if (!--zpci_iomap_start[idx].count) { 320cd248341SJan Glauber zpci_iomap_start[idx].fh = 0; 321cd248341SJan Glauber zpci_iomap_start[idx].bar = 0; 3228cfc99b5SMichael S. Tsirkin } 323cd248341SJan Glauber spin_unlock(&zpci_iomap_lock); 324cd248341SJan Glauber } 325d9426083SSebastian Ott EXPORT_SYMBOL(pci_iounmap); 326cd248341SJan Glauber 327cd248341SJan Glauber static int pci_read(struct pci_bus *bus, unsigned int devfn, int where, 328cd248341SJan Glauber int size, u32 *val) 329cd248341SJan Glauber { 330cd248341SJan Glauber struct zpci_dev *zdev = get_zdev_by_bus(bus); 3312c3700bbSSebastian Ott int ret; 332cd248341SJan Glauber 333cd248341SJan Glauber if (!zdev || devfn != ZPCI_DEVFN) 3342c3700bbSSebastian Ott ret = -ENODEV; 3352c3700bbSSebastian Ott else 3362c3700bbSSebastian Ott ret = zpci_cfg_load(zdev, where, val, size); 3372c3700bbSSebastian Ott 3382c3700bbSSebastian Ott return ret; 339cd248341SJan Glauber } 340cd248341SJan Glauber 341cd248341SJan Glauber static int pci_write(struct pci_bus *bus, unsigned int devfn, int where, 342cd248341SJan Glauber int size, u32 val) 343cd248341SJan Glauber { 344cd248341SJan Glauber struct zpci_dev *zdev = get_zdev_by_bus(bus); 3452c3700bbSSebastian Ott int ret; 346cd248341SJan Glauber 347cd248341SJan Glauber if (!zdev || devfn != ZPCI_DEVFN) 3482c3700bbSSebastian Ott ret = -ENODEV; 3492c3700bbSSebastian Ott else 3502c3700bbSSebastian Ott ret = zpci_cfg_store(zdev, where, val, size); 3512c3700bbSSebastian Ott 3522c3700bbSSebastian Ott return ret; 353cd248341SJan Glauber } 354cd248341SJan Glauber 355cd248341SJan Glauber static struct pci_ops pci_root_ops = { 356cd248341SJan Glauber .read = pci_read, 357cd248341SJan Glauber .write = pci_write, 358cd248341SJan Glauber }; 359cd248341SJan Glauber 360f4eae94fSMartin Schwidefsky static void zpci_irq_handler(struct airq_struct *airq) 3619a4da8a5SJan Glauber { 3625d0d8f43SMartin Schwidefsky unsigned long si, ai; 3631f44a225SMartin Schwidefsky struct airq_iv *aibv; 3645d0d8f43SMartin Schwidefsky int irqs_on = 0; 3659a4da8a5SJan Glauber 366420f42ecSHeiko Carstens inc_irq_stat(IRQIO_PCI); 3675d0d8f43SMartin Schwidefsky for (si = 0;;) { 3685d0d8f43SMartin Schwidefsky /* Scan adapter summary indicator bit vector */ 3695d0d8f43SMartin Schwidefsky si = airq_iv_scan(zpci_aisb_iv, si, airq_iv_end(zpci_aisb_iv)); 3705d0d8f43SMartin Schwidefsky if (si == -1UL) { 3715d0d8f43SMartin Schwidefsky if (irqs_on++) 3725d0d8f43SMartin Schwidefsky /* End of second scan with interrupts on. */ 3735d0d8f43SMartin Schwidefsky break; 3745d0d8f43SMartin Schwidefsky /* First scan complete, reenable interrupts. */ 3759389339fSMartin Schwidefsky zpci_set_irq_ctrl(SIC_IRQ_MODE_SINGLE, NULL, PCI_ISC); 3765d0d8f43SMartin Schwidefsky si = 0; 3775d0d8f43SMartin Schwidefsky continue; 3789a4da8a5SJan Glauber } 3795d0d8f43SMartin Schwidefsky 3805d0d8f43SMartin Schwidefsky /* Scan the adapter interrupt vector for this device. */ 3811f44a225SMartin Schwidefsky aibv = zpci_aibv[si]; 3825d0d8f43SMartin Schwidefsky for (ai = 0;;) { 3831f44a225SMartin Schwidefsky ai = airq_iv_scan(aibv, ai, airq_iv_end(aibv)); 3845d0d8f43SMartin Schwidefsky if (ai == -1UL) 3855d0d8f43SMartin Schwidefsky break; 3865d0d8f43SMartin Schwidefsky inc_irq_stat(IRQIO_MSI); 3871f44a225SMartin Schwidefsky airq_iv_lock(aibv, ai); 3881f44a225SMartin Schwidefsky generic_handle_irq(airq_iv_get_data(aibv, ai)); 3891f44a225SMartin Schwidefsky airq_iv_unlock(aibv, ai); 3905d0d8f43SMartin Schwidefsky } 3915d0d8f43SMartin Schwidefsky } 3925d0d8f43SMartin Schwidefsky } 3935d0d8f43SMartin Schwidefsky 3945d0d8f43SMartin Schwidefsky int arch_setup_msi_irqs(struct pci_dev *pdev, int nvec, int type) 3959a4da8a5SJan Glauber { 396198a5278SSebastian Ott struct zpci_dev *zdev = to_zpci(pdev); 3970a0a9421SThomas Gleixner unsigned int hwirq, msi_vecs; 3985d0d8f43SMartin Schwidefsky unsigned long aisb; 3999a4da8a5SJan Glauber struct msi_desc *msi; 4001f44a225SMartin Schwidefsky struct msi_msg msg; 4010a0a9421SThomas Gleixner int rc, irq; 4029a4da8a5SJan Glauber 403795818e8SSebastian Ott zdev->aisb = -1UL; 404a384c892SAlexander Gordeev if (type == PCI_CAP_ID_MSI && nvec > 1) 405a384c892SAlexander Gordeev return 1; 406b19148f6SSebastian Ott msi_vecs = min_t(unsigned int, nvec, zdev->max_msi); 4079a4da8a5SJan Glauber 4085d0d8f43SMartin Schwidefsky /* Allocate adapter summary indicator bit */ 4095d0d8f43SMartin Schwidefsky aisb = airq_iv_alloc_bit(zpci_aisb_iv); 4105d0d8f43SMartin Schwidefsky if (aisb == -1UL) 411795818e8SSebastian Ott return -EIO; 4129a4da8a5SJan Glauber zdev->aisb = aisb; 4139a4da8a5SJan Glauber 4145d0d8f43SMartin Schwidefsky /* Create adapter interrupt vector */ 4151f44a225SMartin Schwidefsky zdev->aibv = airq_iv_create(msi_vecs, AIRQ_IV_DATA | AIRQ_IV_BITLOCK); 4165d0d8f43SMartin Schwidefsky if (!zdev->aibv) 417795818e8SSebastian Ott return -ENOMEM; 4189a4da8a5SJan Glauber 4195d0d8f43SMartin Schwidefsky /* Wire up shortcut pointer */ 4201f44a225SMartin Schwidefsky zpci_aibv[aisb] = zdev->aibv; 4215d0d8f43SMartin Schwidefsky 4221f44a225SMartin Schwidefsky /* Request MSI interrupts */ 4231f44a225SMartin Schwidefsky hwirq = 0; 424df516f42SJiang Liu for_each_pci_msi_entry(msi, pdev) { 4251f44a225SMartin Schwidefsky rc = -EIO; 4261f44a225SMartin Schwidefsky irq = irq_alloc_desc(0); /* Alloc irq on node 0 */ 4270a0a9421SThomas Gleixner if (irq < 0) 428795818e8SSebastian Ott return -ENOMEM; 4291f44a225SMartin Schwidefsky rc = irq_set_msi_desc(irq, msi); 4309a4da8a5SJan Glauber if (rc) 431795818e8SSebastian Ott return rc; 4321f44a225SMartin Schwidefsky irq_set_chip_and_handler(irq, &zpci_irq_chip, 4331f44a225SMartin Schwidefsky handle_simple_irq); 4341f44a225SMartin Schwidefsky msg.data = hwirq; 4351f44a225SMartin Schwidefsky msg.address_lo = zdev->msi_addr & 0xffffffff; 4361f44a225SMartin Schwidefsky msg.address_hi = zdev->msi_addr >> 32; 43783a18912SJiang Liu pci_write_msi_msg(irq, &msg); 4381f44a225SMartin Schwidefsky airq_iv_set_data(zdev->aibv, hwirq, irq); 4391f44a225SMartin Schwidefsky hwirq++; 4409a4da8a5SJan Glauber } 4419a4da8a5SJan Glauber 4425d0d8f43SMartin Schwidefsky /* Enable adapter interrupts */ 4435d0d8f43SMartin Schwidefsky rc = zpci_set_airq(zdev); 4445d0d8f43SMartin Schwidefsky if (rc) 445795818e8SSebastian Ott return rc; 4465d0d8f43SMartin Schwidefsky 4475d0d8f43SMartin Schwidefsky return (msi_vecs == nvec) ? 0 : msi_vecs; 4489a4da8a5SJan Glauber } 4499a4da8a5SJan Glauber 4505d0d8f43SMartin Schwidefsky void arch_teardown_msi_irqs(struct pci_dev *pdev) 4519a4da8a5SJan Glauber { 452198a5278SSebastian Ott struct zpci_dev *zdev = to_zpci(pdev); 4539a4da8a5SJan Glauber struct msi_desc *msi; 4545d0d8f43SMartin Schwidefsky int rc; 4559a4da8a5SJan Glauber 4565d0d8f43SMartin Schwidefsky /* Disable adapter interrupts */ 4575d0d8f43SMartin Schwidefsky rc = zpci_clear_airq(zdev); 4581f1dcbd4SSebastian Ott if (rc) 4599a4da8a5SJan Glauber return; 4609a4da8a5SJan Glauber 4611f44a225SMartin Schwidefsky /* Release MSI interrupts */ 462df516f42SJiang Liu for_each_pci_msi_entry(msi, pdev) { 463795818e8SSebastian Ott if (!msi->irq) 464795818e8SSebastian Ott continue; 4658fb878c5SYijing Wang if (msi->msi_attrib.is_msix) 46623ed8d57SThomas Gleixner __pci_msix_desc_mask_irq(msi, 1); 4678fb878c5SYijing Wang else 46823ed8d57SThomas Gleixner __pci_msi_desc_mask_irq(msi, 1, 1); 4691f44a225SMartin Schwidefsky irq_set_msi_desc(msi->irq, NULL); 4701f44a225SMartin Schwidefsky irq_free_desc(msi->irq); 4711f44a225SMartin Schwidefsky msi->msg.address_lo = 0; 4721f44a225SMartin Schwidefsky msi->msg.address_hi = 0; 4731f44a225SMartin Schwidefsky msi->msg.data = 0; 4741f44a225SMartin Schwidefsky msi->irq = 0; 4751f44a225SMartin Schwidefsky } 4769a4da8a5SJan Glauber 477795818e8SSebastian Ott if (zdev->aisb != -1UL) { 4781f44a225SMartin Schwidefsky zpci_aibv[zdev->aisb] = NULL; 4795d0d8f43SMartin Schwidefsky airq_iv_free_bit(zpci_aisb_iv, zdev->aisb); 480795818e8SSebastian Ott zdev->aisb = -1UL; 481795818e8SSebastian Ott } 482795818e8SSebastian Ott if (zdev->aibv) { 483795818e8SSebastian Ott airq_iv_release(zdev->aibv); 484795818e8SSebastian Ott zdev->aibv = NULL; 485795818e8SSebastian Ott } 4869a4da8a5SJan Glauber } 4879a4da8a5SJan Glauber 4881803ba2dSSebastian Ott static void zpci_map_resources(struct pci_dev *pdev) 489cd248341SJan Glauber { 490cd248341SJan Glauber resource_size_t len; 491cd248341SJan Glauber int i; 492cd248341SJan Glauber 493cd248341SJan Glauber for (i = 0; i < PCI_BAR_COUNT; i++) { 494cd248341SJan Glauber len = pci_resource_len(pdev, i); 495cd248341SJan Glauber if (!len) 496cd248341SJan Glauber continue; 4975b9f2081SMartin Schwidefsky pdev->resource[i].start = 4985b9f2081SMartin Schwidefsky (resource_size_t __force) pci_iomap(pdev, i, 0); 499cd248341SJan Glauber pdev->resource[i].end = pdev->resource[i].start + len - 1; 500cd248341SJan Glauber } 501944239c5SSebastian Ott } 502944239c5SSebastian Ott 5031803ba2dSSebastian Ott static void zpci_unmap_resources(struct pci_dev *pdev) 504944239c5SSebastian Ott { 505944239c5SSebastian Ott resource_size_t len; 506944239c5SSebastian Ott int i; 507944239c5SSebastian Ott 508944239c5SSebastian Ott for (i = 0; i < PCI_BAR_COUNT; i++) { 509944239c5SSebastian Ott len = pci_resource_len(pdev, i); 510944239c5SSebastian Ott if (!len) 511944239c5SSebastian Ott continue; 5125b9f2081SMartin Schwidefsky pci_iounmap(pdev, (void __iomem __force *) 5135b9f2081SMartin Schwidefsky pdev->resource[i].start); 514944239c5SSebastian Ott } 515944239c5SSebastian Ott } 516cd248341SJan Glauber 517561ecb0cSSebastian Ott static struct airq_struct zpci_airq = { 518561ecb0cSSebastian Ott .handler = zpci_irq_handler, 519561ecb0cSSebastian Ott .isc = PCI_ISC, 520561ecb0cSSebastian Ott }; 521561ecb0cSSebastian Ott 5229a4da8a5SJan Glauber static int __init zpci_irq_init(void) 5239a4da8a5SJan Glauber { 5245d0d8f43SMartin Schwidefsky int rc; 5259a4da8a5SJan Glauber 526f4eae94fSMartin Schwidefsky rc = register_adapter_interrupt(&zpci_airq); 527f4eae94fSMartin Schwidefsky if (rc) 5285d0d8f43SMartin Schwidefsky goto out; 529f4eae94fSMartin Schwidefsky /* Set summary to 1 to be called every time for the ISC. */ 530f4eae94fSMartin Schwidefsky *zpci_airq.lsi_ptr = 1; 5319a4da8a5SJan Glauber 5325d0d8f43SMartin Schwidefsky rc = -ENOMEM; 5335d0d8f43SMartin Schwidefsky zpci_aisb_iv = airq_iv_create(ZPCI_NR_DEVICES, AIRQ_IV_ALLOC); 5345d0d8f43SMartin Schwidefsky if (!zpci_aisb_iv) 5355d0d8f43SMartin Schwidefsky goto out_airq; 5369a4da8a5SJan Glauber 5379389339fSMartin Schwidefsky zpci_set_irq_ctrl(SIC_IRQ_MODE_SINGLE, NULL, PCI_ISC); 5389a4da8a5SJan Glauber return 0; 5399a4da8a5SJan Glauber 5405d0d8f43SMartin Schwidefsky out_airq: 5415d0d8f43SMartin Schwidefsky unregister_adapter_interrupt(&zpci_airq); 5425d0d8f43SMartin Schwidefsky out: 5439a4da8a5SJan Glauber return rc; 5449a4da8a5SJan Glauber } 5459a4da8a5SJan Glauber 5469a4da8a5SJan Glauber static void zpci_irq_exit(void) 5479a4da8a5SJan Glauber { 5485d0d8f43SMartin Schwidefsky airq_iv_release(zpci_aisb_iv); 549f4eae94fSMartin Schwidefsky unregister_adapter_interrupt(&zpci_airq); 5509a4da8a5SJan Glauber } 5519a4da8a5SJan Glauber 552cd248341SJan Glauber static int zpci_alloc_iomap(struct zpci_dev *zdev) 553cd248341SJan Glauber { 554bf19c94dSSebastian Ott unsigned long entry; 555cd248341SJan Glauber 556cd248341SJan Glauber spin_lock(&zpci_iomap_lock); 557c506fff3SSebastian Ott entry = find_first_zero_bit(zpci_iomap_bitmap, ZPCI_IOMAP_ENTRIES); 558c506fff3SSebastian Ott if (entry == ZPCI_IOMAP_ENTRIES) { 559cd248341SJan Glauber spin_unlock(&zpci_iomap_lock); 560cd248341SJan Glauber return -ENOSPC; 561cd248341SJan Glauber } 562c506fff3SSebastian Ott set_bit(entry, zpci_iomap_bitmap); 563cd248341SJan Glauber spin_unlock(&zpci_iomap_lock); 564cd248341SJan Glauber return entry; 565cd248341SJan Glauber } 566cd248341SJan Glauber 567cd248341SJan Glauber static void zpci_free_iomap(struct zpci_dev *zdev, int entry) 568cd248341SJan Glauber { 569cd248341SJan Glauber spin_lock(&zpci_iomap_lock); 570cd248341SJan Glauber memset(&zpci_iomap_start[entry], 0, sizeof(struct zpci_iomap_entry)); 571c506fff3SSebastian Ott clear_bit(entry, zpci_iomap_bitmap); 572cd248341SJan Glauber spin_unlock(&zpci_iomap_lock); 573cd248341SJan Glauber } 574cd248341SJan Glauber 5757a572a3aSSebastian Ott static struct resource *__alloc_res(struct zpci_dev *zdev, unsigned long start, 5767a572a3aSSebastian Ott unsigned long size, unsigned long flags) 5777a572a3aSSebastian Ott { 5787a572a3aSSebastian Ott struct resource *r; 5797a572a3aSSebastian Ott 5807a572a3aSSebastian Ott r = kzalloc(sizeof(*r), GFP_KERNEL); 5817a572a3aSSebastian Ott if (!r) 5827a572a3aSSebastian Ott return NULL; 5837a572a3aSSebastian Ott 5847a572a3aSSebastian Ott r->start = start; 5857a572a3aSSebastian Ott r->end = r->start + size - 1; 5867a572a3aSSebastian Ott r->flags = flags; 5877a572a3aSSebastian Ott r->name = zdev->res_name; 5887a572a3aSSebastian Ott 5897a572a3aSSebastian Ott if (request_resource(&iomem_resource, r)) { 5907a572a3aSSebastian Ott kfree(r); 5917a572a3aSSebastian Ott return NULL; 5927a572a3aSSebastian Ott } 5937a572a3aSSebastian Ott return r; 5947a572a3aSSebastian Ott } 5957a572a3aSSebastian Ott 5967a572a3aSSebastian Ott static int zpci_setup_bus_resources(struct zpci_dev *zdev, 5977a572a3aSSebastian Ott struct list_head *resources) 5987a572a3aSSebastian Ott { 5997a572a3aSSebastian Ott unsigned long addr, size, flags; 6007a572a3aSSebastian Ott struct resource *res; 6017a572a3aSSebastian Ott int i, entry; 6027a572a3aSSebastian Ott 6037a572a3aSSebastian Ott snprintf(zdev->res_name, sizeof(zdev->res_name), 6047a572a3aSSebastian Ott "PCI Bus %04x:%02x", zdev->domain, ZPCI_BUS_NR); 6057a572a3aSSebastian Ott 6067a572a3aSSebastian Ott for (i = 0; i < PCI_BAR_COUNT; i++) { 6077a572a3aSSebastian Ott if (!zdev->bars[i].size) 6087a572a3aSSebastian Ott continue; 6097a572a3aSSebastian Ott entry = zpci_alloc_iomap(zdev); 6107a572a3aSSebastian Ott if (entry < 0) 6117a572a3aSSebastian Ott return entry; 6127a572a3aSSebastian Ott zdev->bars[i].map_idx = entry; 6137a572a3aSSebastian Ott 6147a572a3aSSebastian Ott /* only MMIO is supported */ 6157a572a3aSSebastian Ott flags = IORESOURCE_MEM; 6167a572a3aSSebastian Ott if (zdev->bars[i].val & 8) 6177a572a3aSSebastian Ott flags |= IORESOURCE_PREFETCH; 6187a572a3aSSebastian Ott if (zdev->bars[i].val & 4) 6197a572a3aSSebastian Ott flags |= IORESOURCE_MEM_64; 6207a572a3aSSebastian Ott 6219e00caaeSSebastian Ott addr = ZPCI_ADDR(entry); 6227a572a3aSSebastian Ott size = 1UL << zdev->bars[i].size; 6237a572a3aSSebastian Ott 6247a572a3aSSebastian Ott res = __alloc_res(zdev, addr, size, flags); 6257a572a3aSSebastian Ott if (!res) { 6267a572a3aSSebastian Ott zpci_free_iomap(zdev, entry); 6277a572a3aSSebastian Ott return -ENOMEM; 6287a572a3aSSebastian Ott } 6297a572a3aSSebastian Ott zdev->bars[i].res = res; 6307a572a3aSSebastian Ott pci_add_resource(resources, res); 6317a572a3aSSebastian Ott } 6327a572a3aSSebastian Ott 6337a572a3aSSebastian Ott return 0; 6347a572a3aSSebastian Ott } 6357a572a3aSSebastian Ott 6367a572a3aSSebastian Ott static void zpci_cleanup_bus_resources(struct zpci_dev *zdev) 6377a572a3aSSebastian Ott { 6387a572a3aSSebastian Ott int i; 6397a572a3aSSebastian Ott 6407a572a3aSSebastian Ott for (i = 0; i < PCI_BAR_COUNT; i++) { 6412b1df724SSebastian Ott if (!zdev->bars[i].size || !zdev->bars[i].res) 6427a572a3aSSebastian Ott continue; 6437a572a3aSSebastian Ott 6447a572a3aSSebastian Ott zpci_free_iomap(zdev, zdev->bars[i].map_idx); 6457a572a3aSSebastian Ott release_resource(zdev->bars[i].res); 6467a572a3aSSebastian Ott kfree(zdev->bars[i].res); 6477a572a3aSSebastian Ott } 6487a572a3aSSebastian Ott } 6497a572a3aSSebastian Ott 650af0a8a84SSebastian Ott int pcibios_add_device(struct pci_dev *pdev) 651af0a8a84SSebastian Ott { 652cb809182SSebastian Ott struct resource *res; 653cb809182SSebastian Ott int i; 654cb809182SSebastian Ott 655ef4858c6SSebastian Ott pdev->dev.groups = zpci_attr_groups; 6565657933dSBart Van Assche pdev->dev.dma_ops = &s390_pci_dma_ops; 6571803ba2dSSebastian Ott zpci_map_resources(pdev); 658cb809182SSebastian Ott 659cb809182SSebastian Ott for (i = 0; i < PCI_BAR_COUNT; i++) { 660cb809182SSebastian Ott res = &pdev->resource[i]; 661cb809182SSebastian Ott if (res->parent || !res->flags) 662cb809182SSebastian Ott continue; 663cb809182SSebastian Ott pci_claim_resource(pdev, i); 664cb809182SSebastian Ott } 665cb809182SSebastian Ott 666cb809182SSebastian Ott return 0; 667cb809182SSebastian Ott } 668cb809182SSebastian Ott 6691803ba2dSSebastian Ott void pcibios_release_device(struct pci_dev *pdev) 6701803ba2dSSebastian Ott { 6711803ba2dSSebastian Ott zpci_unmap_resources(pdev); 6721803ba2dSSebastian Ott } 6731803ba2dSSebastian Ott 674cb809182SSebastian Ott int pcibios_enable_device(struct pci_dev *pdev, int mask) 675cb809182SSebastian Ott { 676198a5278SSebastian Ott struct zpci_dev *zdev = to_zpci(pdev); 677af0a8a84SSebastian Ott 6789a99649fSSebastian Ott zpci_debug_init_device(zdev, dev_name(&pdev->dev)); 679af0a8a84SSebastian Ott zpci_fmb_enable_device(zdev); 680af0a8a84SSebastian Ott 681d7533232SBjorn Helgaas return pci_enable_resources(pdev, mask); 682af0a8a84SSebastian Ott } 683af0a8a84SSebastian Ott 684cb809182SSebastian Ott void pcibios_disable_device(struct pci_dev *pdev) 685944239c5SSebastian Ott { 686198a5278SSebastian Ott struct zpci_dev *zdev = to_zpci(pdev); 687944239c5SSebastian Ott 688944239c5SSebastian Ott zpci_fmb_disable_device(zdev); 689944239c5SSebastian Ott zpci_debug_exit_device(zdev); 690944239c5SSebastian Ott } 691944239c5SSebastian Ott 69269db3b5eSSebastian Ott #ifdef CONFIG_HIBERNATE_CALLBACKS 69369db3b5eSSebastian Ott static int zpci_restore(struct device *dev) 69469db3b5eSSebastian Ott { 6951803ba2dSSebastian Ott struct pci_dev *pdev = to_pci_dev(dev); 696198a5278SSebastian Ott struct zpci_dev *zdev = to_zpci(pdev); 69769db3b5eSSebastian Ott int ret = 0; 69869db3b5eSSebastian Ott 69969db3b5eSSebastian Ott if (zdev->state != ZPCI_FN_STATE_ONLINE) 70069db3b5eSSebastian Ott goto out; 70169db3b5eSSebastian Ott 70269db3b5eSSebastian Ott ret = clp_enable_fh(zdev, ZPCI_NR_DMA_SPACES); 70369db3b5eSSebastian Ott if (ret) 70469db3b5eSSebastian Ott goto out; 70569db3b5eSSebastian Ott 7061803ba2dSSebastian Ott zpci_map_resources(pdev); 70769eea95cSGerald Schaefer zpci_register_ioat(zdev, 0, zdev->start_dma, zdev->end_dma, 70869db3b5eSSebastian Ott (u64) zdev->dma_table); 70969db3b5eSSebastian Ott 71069db3b5eSSebastian Ott out: 71169db3b5eSSebastian Ott return ret; 71269db3b5eSSebastian Ott } 71369db3b5eSSebastian Ott 71469db3b5eSSebastian Ott static int zpci_freeze(struct device *dev) 71569db3b5eSSebastian Ott { 7161803ba2dSSebastian Ott struct pci_dev *pdev = to_pci_dev(dev); 717198a5278SSebastian Ott struct zpci_dev *zdev = to_zpci(pdev); 71869db3b5eSSebastian Ott 71969db3b5eSSebastian Ott if (zdev->state != ZPCI_FN_STATE_ONLINE) 72069db3b5eSSebastian Ott return 0; 72169db3b5eSSebastian Ott 72269db3b5eSSebastian Ott zpci_unregister_ioat(zdev, 0); 7231803ba2dSSebastian Ott zpci_unmap_resources(pdev); 72469db3b5eSSebastian Ott return clp_disable_fh(zdev); 72569db3b5eSSebastian Ott } 72669db3b5eSSebastian Ott 72769db3b5eSSebastian Ott struct dev_pm_ops pcibios_pm_ops = { 72869db3b5eSSebastian Ott .thaw_noirq = zpci_restore, 72969db3b5eSSebastian Ott .freeze_noirq = zpci_freeze, 73069db3b5eSSebastian Ott .restore_noirq = zpci_restore, 73169db3b5eSSebastian Ott .poweroff_noirq = zpci_freeze, 73269db3b5eSSebastian Ott }; 73369db3b5eSSebastian Ott #endif /* CONFIG_HIBERNATE_CALLBACKS */ 73469db3b5eSSebastian Ott 735cd248341SJan Glauber static int zpci_alloc_domain(struct zpci_dev *zdev) 736cd248341SJan Glauber { 7375c5afd02SSebastian Ott if (zpci_unique_uid) { 7385c5afd02SSebastian Ott zdev->domain = (u16) zdev->uid; 7395c5afd02SSebastian Ott return 0; 7405c5afd02SSebastian Ott } 7415c5afd02SSebastian Ott 742cd248341SJan Glauber spin_lock(&zpci_domain_lock); 743cd248341SJan Glauber zdev->domain = find_first_zero_bit(zpci_domain, ZPCI_NR_DEVICES); 744cd248341SJan Glauber if (zdev->domain == ZPCI_NR_DEVICES) { 745cd248341SJan Glauber spin_unlock(&zpci_domain_lock); 746cd248341SJan Glauber return -ENOSPC; 747cd248341SJan Glauber } 748cd248341SJan Glauber set_bit(zdev->domain, zpci_domain); 749cd248341SJan Glauber spin_unlock(&zpci_domain_lock); 750cd248341SJan Glauber return 0; 751cd248341SJan Glauber } 752cd248341SJan Glauber 753cd248341SJan Glauber static void zpci_free_domain(struct zpci_dev *zdev) 754cd248341SJan Glauber { 7555c5afd02SSebastian Ott if (zpci_unique_uid) 7565c5afd02SSebastian Ott return; 7575c5afd02SSebastian Ott 758cd248341SJan Glauber spin_lock(&zpci_domain_lock); 759cd248341SJan Glauber clear_bit(zdev->domain, zpci_domain); 760cd248341SJan Glauber spin_unlock(&zpci_domain_lock); 761cd248341SJan Glauber } 762cd248341SJan Glauber 7637d594322SSebastian Ott void pcibios_remove_bus(struct pci_bus *bus) 7647d594322SSebastian Ott { 7657d594322SSebastian Ott struct zpci_dev *zdev = get_zdev_by_bus(bus); 7667d594322SSebastian Ott 7677d594322SSebastian Ott zpci_exit_slot(zdev); 7687d594322SSebastian Ott zpci_cleanup_bus_resources(zdev); 7697d594322SSebastian Ott zpci_free_domain(zdev); 7707d594322SSebastian Ott 7717d594322SSebastian Ott spin_lock(&zpci_list_lock); 7727d594322SSebastian Ott list_del(&zdev->entry); 7737d594322SSebastian Ott spin_unlock(&zpci_list_lock); 7747d594322SSebastian Ott 775*be2c3676SSebastian Ott zpci_dbg(3, "rem fid:%x\n", zdev->fid); 7767d594322SSebastian Ott kfree(zdev); 7777d594322SSebastian Ott } 7787d594322SSebastian Ott 7797d594322SSebastian Ott static int zpci_scan_bus(struct zpci_dev *zdev) 7807d594322SSebastian Ott { 7817d594322SSebastian Ott LIST_HEAD(resources); 7827d594322SSebastian Ott int ret; 7837d594322SSebastian Ott 7847d594322SSebastian Ott ret = zpci_setup_bus_resources(zdev, &resources); 7857d594322SSebastian Ott if (ret) 7862b1df724SSebastian Ott goto error; 7877d594322SSebastian Ott 7887d594322SSebastian Ott zdev->bus = pci_scan_root_bus(NULL, ZPCI_BUS_NR, &pci_root_ops, 7897d594322SSebastian Ott zdev, &resources); 7907d594322SSebastian Ott if (!zdev->bus) { 7912b1df724SSebastian Ott ret = -EIO; 7922b1df724SSebastian Ott goto error; 7937d594322SSebastian Ott } 7947d594322SSebastian Ott zdev->bus->max_bus_speed = zdev->max_bus_speed; 795b97ea289SYijing Wang pci_bus_add_devices(zdev->bus); 7967d594322SSebastian Ott return 0; 7972b1df724SSebastian Ott 7982b1df724SSebastian Ott error: 7992b1df724SSebastian Ott zpci_cleanup_bus_resources(zdev); 8002b1df724SSebastian Ott pci_free_resource_list(&resources); 8012b1df724SSebastian Ott return ret; 8027d594322SSebastian Ott } 8037d594322SSebastian Ott 804a755a45dSJan Glauber int zpci_enable_device(struct zpci_dev *zdev) 805a755a45dSJan Glauber { 806a755a45dSJan Glauber int rc; 807a755a45dSJan Glauber 808a755a45dSJan Glauber rc = clp_enable_fh(zdev, ZPCI_NR_DMA_SPACES); 809a755a45dSJan Glauber if (rc) 810a755a45dSJan Glauber goto out; 811828b35f6SJan Glauber 812828b35f6SJan Glauber rc = zpci_dma_init_device(zdev); 813828b35f6SJan Glauber if (rc) 814828b35f6SJan Glauber goto out_dma; 8150ff70ec8SSebastian Ott 8160ff70ec8SSebastian Ott zdev->state = ZPCI_FN_STATE_ONLINE; 817a755a45dSJan Glauber return 0; 818828b35f6SJan Glauber 819828b35f6SJan Glauber out_dma: 820828b35f6SJan Glauber clp_disable_fh(zdev); 821a755a45dSJan Glauber out: 822a755a45dSJan Glauber return rc; 823a755a45dSJan Glauber } 824a755a45dSJan Glauber EXPORT_SYMBOL_GPL(zpci_enable_device); 825a755a45dSJan Glauber 826cb65a669SSebastian Ott int zpci_disable_device(struct zpci_dev *zdev) 827cb65a669SSebastian Ott { 828cb65a669SSebastian Ott zpci_dma_exit_device(zdev); 829cb65a669SSebastian Ott return clp_disable_fh(zdev); 830cb65a669SSebastian Ott } 831cb65a669SSebastian Ott EXPORT_SYMBOL_GPL(zpci_disable_device); 832cb65a669SSebastian Ott 833cd248341SJan Glauber int zpci_create_device(struct zpci_dev *zdev) 834cd248341SJan Glauber { 835cd248341SJan Glauber int rc; 836cd248341SJan Glauber 837cd248341SJan Glauber rc = zpci_alloc_domain(zdev); 838cd248341SJan Glauber if (rc) 839cd248341SJan Glauber goto out; 840cd248341SJan Glauber 84180ed156aSSebastian Ott mutex_init(&zdev->lock); 8421c21351bSSebastian Ott if (zdev->state == ZPCI_FN_STATE_CONFIGURED) { 8431c21351bSSebastian Ott rc = zpci_enable_device(zdev); 844cd248341SJan Glauber if (rc) 8451c21351bSSebastian Ott goto out_free; 8461c21351bSSebastian Ott } 8471c21351bSSebastian Ott rc = zpci_scan_bus(zdev); 8481c21351bSSebastian Ott if (rc) 8491c21351bSSebastian Ott goto out_disable; 850cd248341SJan Glauber 85157b5918cSSebastian Ott spin_lock(&zpci_list_lock); 852cd248341SJan Glauber list_add_tail(&zdev->entry, &zpci_list); 85357b5918cSSebastian Ott spin_unlock(&zpci_list_lock); 854cd248341SJan Glauber 85567f43f38SSebastian Ott zpci_init_slot(zdev); 85667f43f38SSebastian Ott 857cd248341SJan Glauber return 0; 858cd248341SJan Glauber 8591c21351bSSebastian Ott out_disable: 8601c21351bSSebastian Ott if (zdev->state == ZPCI_FN_STATE_ONLINE) 8611c21351bSSebastian Ott zpci_disable_device(zdev); 8621c21351bSSebastian Ott out_free: 863cd248341SJan Glauber zpci_free_domain(zdev); 864cd248341SJan Glauber out: 865cd248341SJan Glauber return rc; 866cd248341SJan Glauber } 867cd248341SJan Glauber 868623bd44dSSebastian Ott void zpci_remove_device(struct zpci_dev *zdev) 869623bd44dSSebastian Ott { 870623bd44dSSebastian Ott if (!zdev->bus) 871623bd44dSSebastian Ott return; 872623bd44dSSebastian Ott 873623bd44dSSebastian Ott pci_stop_root_bus(zdev->bus); 874623bd44dSSebastian Ott pci_remove_root_bus(zdev->bus); 875623bd44dSSebastian Ott } 876623bd44dSSebastian Ott 877bd3a1725SMartin Schwidefsky int zpci_report_error(struct pci_dev *pdev, 878bd3a1725SMartin Schwidefsky struct zpci_report_error_header *report) 879bd3a1725SMartin Schwidefsky { 880bd3a1725SMartin Schwidefsky struct zpci_dev *zdev = to_zpci(pdev); 881bd3a1725SMartin Schwidefsky 882bd3a1725SMartin Schwidefsky return sclp_pci_report(report, zdev->fh, zdev->fid); 883bd3a1725SMartin Schwidefsky } 884bd3a1725SMartin Schwidefsky EXPORT_SYMBOL(zpci_report_error); 885bd3a1725SMartin Schwidefsky 886cd248341SJan Glauber static int zpci_mem_init(void) 887cd248341SJan Glauber { 88880c544deSSebastian Ott BUILD_BUG_ON(!is_power_of_2(__alignof__(struct zpci_fmb)) || 88980c544deSSebastian Ott __alignof__(struct zpci_fmb) < sizeof(struct zpci_fmb)); 89080c544deSSebastian Ott 891d0b08853SJan Glauber zdev_fmb_cache = kmem_cache_create("PCI_FMB_cache", sizeof(struct zpci_fmb), 89280c544deSSebastian Ott __alignof__(struct zpci_fmb), 0, NULL); 893d0b08853SJan Glauber if (!zdev_fmb_cache) 894c506fff3SSebastian Ott goto error_fmb; 895d0b08853SJan Glauber 896c506fff3SSebastian Ott zpci_iomap_start = kcalloc(ZPCI_IOMAP_ENTRIES, 897c506fff3SSebastian Ott sizeof(*zpci_iomap_start), GFP_KERNEL); 898cd248341SJan Glauber if (!zpci_iomap_start) 8999a4da8a5SJan Glauber goto error_iomap; 900cd248341SJan Glauber 901c506fff3SSebastian Ott zpci_iomap_bitmap = kcalloc(BITS_TO_LONGS(ZPCI_IOMAP_ENTRIES), 902c506fff3SSebastian Ott sizeof(*zpci_iomap_bitmap), GFP_KERNEL); 903c506fff3SSebastian Ott if (!zpci_iomap_bitmap) 904c506fff3SSebastian Ott goto error_iomap_bitmap; 905c506fff3SSebastian Ott 906c506fff3SSebastian Ott return 0; 907c506fff3SSebastian Ott error_iomap_bitmap: 908c506fff3SSebastian Ott kfree(zpci_iomap_start); 9099a4da8a5SJan Glauber error_iomap: 910d0b08853SJan Glauber kmem_cache_destroy(zdev_fmb_cache); 911c506fff3SSebastian Ott error_fmb: 912cd248341SJan Glauber return -ENOMEM; 913cd248341SJan Glauber } 914cd248341SJan Glauber 915cd248341SJan Glauber static void zpci_mem_exit(void) 916cd248341SJan Glauber { 917c506fff3SSebastian Ott kfree(zpci_iomap_bitmap); 918cd248341SJan Glauber kfree(zpci_iomap_start); 919d0b08853SJan Glauber kmem_cache_destroy(zdev_fmb_cache); 920cd248341SJan Glauber } 921cd248341SJan Glauber 922257608fbSSebastian Ott static unsigned int s390_pci_probe = 1; 923aa3b7c29SSebastian Ott static unsigned int s390_pci_initialized; 924cd248341SJan Glauber 925cd248341SJan Glauber char * __init pcibios_setup(char *str) 926cd248341SJan Glauber { 927257608fbSSebastian Ott if (!strcmp(str, "off")) { 928257608fbSSebastian Ott s390_pci_probe = 0; 929cd248341SJan Glauber return NULL; 930cd248341SJan Glauber } 931cd248341SJan Glauber return str; 932cd248341SJan Glauber } 933cd248341SJan Glauber 934aa3b7c29SSebastian Ott bool zpci_is_enabled(void) 935aa3b7c29SSebastian Ott { 936aa3b7c29SSebastian Ott return s390_pci_initialized; 937aa3b7c29SSebastian Ott } 938aa3b7c29SSebastian Ott 939cd248341SJan Glauber static int __init pci_base_init(void) 940cd248341SJan Glauber { 941cd248341SJan Glauber int rc; 942cd248341SJan Glauber 9431e5635d1SHeiko Carstens if (!s390_pci_probe) 944cd248341SJan Glauber return 0; 945cd248341SJan Glauber 94686cd741bSHeiko Carstens if (!test_facility(69) || !test_facility(71) || !test_facility(72)) 947cd248341SJan Glauber return 0; 948cd248341SJan Glauber 949d0b08853SJan Glauber rc = zpci_debug_init(); 950d0b08853SJan Glauber if (rc) 9511f44a225SMartin Schwidefsky goto out; 952d0b08853SJan Glauber 953cd248341SJan Glauber rc = zpci_mem_init(); 954cd248341SJan Glauber if (rc) 955cd248341SJan Glauber goto out_mem; 956cd248341SJan Glauber 9579a4da8a5SJan Glauber rc = zpci_irq_init(); 9589a4da8a5SJan Glauber if (rc) 9599a4da8a5SJan Glauber goto out_irq; 9609a4da8a5SJan Glauber 961828b35f6SJan Glauber rc = zpci_dma_init(); 962828b35f6SJan Glauber if (rc) 963828b35f6SJan Glauber goto out_dma; 964828b35f6SJan Glauber 9651d578966SSebastian Ott rc = clp_scan_pci_devices(); 966a755a45dSJan Glauber if (rc) 967a755a45dSJan Glauber goto out_find; 968a755a45dSJan Glauber 969aa3b7c29SSebastian Ott s390_pci_initialized = 1; 970cd248341SJan Glauber return 0; 971cd248341SJan Glauber 972a755a45dSJan Glauber out_find: 973828b35f6SJan Glauber zpci_dma_exit(); 974828b35f6SJan Glauber out_dma: 9759a4da8a5SJan Glauber zpci_irq_exit(); 9769a4da8a5SJan Glauber out_irq: 977cd248341SJan Glauber zpci_mem_exit(); 978cd248341SJan Glauber out_mem: 979d0b08853SJan Glauber zpci_debug_exit(); 9801f44a225SMartin Schwidefsky out: 981cd248341SJan Glauber return rc; 982cd248341SJan Glauber } 98367f43f38SSebastian Ott subsys_initcall_sync(pci_base_init); 98457b5918cSSebastian Ott 98557b5918cSSebastian Ott void zpci_rescan(void) 98657b5918cSSebastian Ott { 987aa3b7c29SSebastian Ott if (zpci_is_enabled()) 98857b5918cSSebastian Ott clp_rescan_pci_devices_simple(); 98957b5918cSSebastian Ott } 990