1 /* 2 * Copyright IBM Corp. 2012 3 * 4 * Author(s): 5 * Jan Glauber <jang@linux.vnet.ibm.com> 6 * 7 * The System z PCI code is a rewrite from a prototype by 8 * the following people (Kudoz!): 9 * Alexander Schmidt 10 * Christoph Raisch 11 * Hannes Hering 12 * Hoang-Nam Nguyen 13 * Jan-Bernd Themann 14 * Stefan Roscher 15 * Thomas Klein 16 */ 17 18 #define KMSG_COMPONENT "zpci" 19 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt 20 21 #include <linux/kernel.h> 22 #include <linux/slab.h> 23 #include <linux/err.h> 24 #include <linux/export.h> 25 #include <linux/delay.h> 26 #include <linux/irq.h> 27 #include <linux/kernel_stat.h> 28 #include <linux/seq_file.h> 29 #include <linux/pci.h> 30 #include <linux/msi.h> 31 32 #include <asm/isc.h> 33 #include <asm/airq.h> 34 #include <asm/facility.h> 35 #include <asm/pci_insn.h> 36 #include <asm/pci_clp.h> 37 #include <asm/pci_dma.h> 38 39 #define DEBUG /* enable pr_debug */ 40 41 #define SIC_IRQ_MODE_ALL 0 42 #define SIC_IRQ_MODE_SINGLE 1 43 44 #define ZPCI_NR_DMA_SPACES 1 45 #define ZPCI_NR_DEVICES CONFIG_PCI_NR_FUNCTIONS 46 47 /* list of all detected zpci devices */ 48 static LIST_HEAD(zpci_list); 49 static DEFINE_SPINLOCK(zpci_list_lock); 50 51 static struct irq_chip zpci_irq_chip = { 52 .name = "zPCI", 53 .irq_unmask = pci_msi_unmask_irq, 54 .irq_mask = pci_msi_mask_irq, 55 }; 56 57 static DECLARE_BITMAP(zpci_domain, ZPCI_NR_DEVICES); 58 static DEFINE_SPINLOCK(zpci_domain_lock); 59 60 static struct airq_iv *zpci_aisb_iv; 61 static struct airq_iv *zpci_aibv[ZPCI_NR_DEVICES]; 62 63 #define ZPCI_IOMAP_ENTRIES \ 64 min(((unsigned long) ZPCI_NR_DEVICES * PCI_BAR_COUNT / 2), \ 65 ZPCI_IOMAP_MAX_ENTRIES) 66 67 static DEFINE_SPINLOCK(zpci_iomap_lock); 68 static unsigned long *zpci_iomap_bitmap; 69 struct zpci_iomap_entry *zpci_iomap_start; 70 EXPORT_SYMBOL_GPL(zpci_iomap_start); 71 72 static struct kmem_cache *zdev_fmb_cache; 73 74 struct zpci_dev *get_zdev_by_fid(u32 fid) 75 { 76 struct zpci_dev *tmp, *zdev = NULL; 77 78 spin_lock(&zpci_list_lock); 79 list_for_each_entry(tmp, &zpci_list, entry) { 80 if (tmp->fid == fid) { 81 zdev = tmp; 82 break; 83 } 84 } 85 spin_unlock(&zpci_list_lock); 86 return zdev; 87 } 88 89 static struct zpci_dev *get_zdev_by_bus(struct pci_bus *bus) 90 { 91 return (bus && bus->sysdata) ? (struct zpci_dev *) bus->sysdata : NULL; 92 } 93 94 int pci_domain_nr(struct pci_bus *bus) 95 { 96 return ((struct zpci_dev *) bus->sysdata)->domain; 97 } 98 EXPORT_SYMBOL_GPL(pci_domain_nr); 99 100 int pci_proc_domain(struct pci_bus *bus) 101 { 102 return pci_domain_nr(bus); 103 } 104 EXPORT_SYMBOL_GPL(pci_proc_domain); 105 106 /* Modify PCI: Register adapter interruptions */ 107 static int zpci_set_airq(struct zpci_dev *zdev) 108 { 109 u64 req = ZPCI_CREATE_REQ(zdev->fh, 0, ZPCI_MOD_FC_REG_INT); 110 struct zpci_fib fib = {0}; 111 112 fib.isc = PCI_ISC; 113 fib.sum = 1; /* enable summary notifications */ 114 fib.noi = airq_iv_end(zdev->aibv); 115 fib.aibv = (unsigned long) zdev->aibv->vector; 116 fib.aibvo = 0; /* each zdev has its own interrupt vector */ 117 fib.aisb = (unsigned long) zpci_aisb_iv->vector + (zdev->aisb/64)*8; 118 fib.aisbo = zdev->aisb & 63; 119 120 return zpci_mod_fc(req, &fib); 121 } 122 123 struct mod_pci_args { 124 u64 base; 125 u64 limit; 126 u64 iota; 127 u64 fmb_addr; 128 }; 129 130 static int mod_pci(struct zpci_dev *zdev, int fn, u8 dmaas, struct mod_pci_args *args) 131 { 132 u64 req = ZPCI_CREATE_REQ(zdev->fh, dmaas, fn); 133 struct zpci_fib fib = {0}; 134 135 fib.pba = args->base; 136 fib.pal = args->limit; 137 fib.iota = args->iota; 138 fib.fmb_addr = args->fmb_addr; 139 140 return zpci_mod_fc(req, &fib); 141 } 142 143 /* Modify PCI: Register I/O address translation parameters */ 144 int zpci_register_ioat(struct zpci_dev *zdev, u8 dmaas, 145 u64 base, u64 limit, u64 iota) 146 { 147 struct mod_pci_args args = { base, limit, iota, 0 }; 148 149 WARN_ON_ONCE(iota & 0x3fff); 150 args.iota |= ZPCI_IOTA_RTTO_FLAG; 151 return mod_pci(zdev, ZPCI_MOD_FC_REG_IOAT, dmaas, &args); 152 } 153 154 /* Modify PCI: Unregister I/O address translation parameters */ 155 int zpci_unregister_ioat(struct zpci_dev *zdev, u8 dmaas) 156 { 157 struct mod_pci_args args = { 0, 0, 0, 0 }; 158 159 return mod_pci(zdev, ZPCI_MOD_FC_DEREG_IOAT, dmaas, &args); 160 } 161 162 /* Modify PCI: Unregister adapter interruptions */ 163 static int zpci_clear_airq(struct zpci_dev *zdev) 164 { 165 struct mod_pci_args args = { 0, 0, 0, 0 }; 166 167 return mod_pci(zdev, ZPCI_MOD_FC_DEREG_INT, 0, &args); 168 } 169 170 /* Modify PCI: Set PCI function measurement parameters */ 171 int zpci_fmb_enable_device(struct zpci_dev *zdev) 172 { 173 struct mod_pci_args args = { 0, 0, 0, 0 }; 174 175 if (zdev->fmb || sizeof(*zdev->fmb) < zdev->fmb_length) 176 return -EINVAL; 177 178 zdev->fmb = kmem_cache_zalloc(zdev_fmb_cache, GFP_KERNEL); 179 if (!zdev->fmb) 180 return -ENOMEM; 181 WARN_ON((u64) zdev->fmb & 0xf); 182 183 /* reset software counters */ 184 atomic64_set(&zdev->allocated_pages, 0); 185 atomic64_set(&zdev->mapped_pages, 0); 186 atomic64_set(&zdev->unmapped_pages, 0); 187 188 args.fmb_addr = virt_to_phys(zdev->fmb); 189 return mod_pci(zdev, ZPCI_MOD_FC_SET_MEASURE, 0, &args); 190 } 191 192 /* Modify PCI: Disable PCI function measurement */ 193 int zpci_fmb_disable_device(struct zpci_dev *zdev) 194 { 195 struct mod_pci_args args = { 0, 0, 0, 0 }; 196 int rc; 197 198 if (!zdev->fmb) 199 return -EINVAL; 200 201 /* Function measurement is disabled if fmb address is zero */ 202 rc = mod_pci(zdev, ZPCI_MOD_FC_SET_MEASURE, 0, &args); 203 204 kmem_cache_free(zdev_fmb_cache, zdev->fmb); 205 zdev->fmb = NULL; 206 return rc; 207 } 208 209 static int zpci_cfg_load(struct zpci_dev *zdev, int offset, u32 *val, u8 len) 210 { 211 u64 req = ZPCI_CREATE_REQ(zdev->fh, ZPCI_PCIAS_CFGSPC, len); 212 u64 data; 213 int rc; 214 215 rc = zpci_load(&data, req, offset); 216 if (!rc) { 217 data = le64_to_cpu((__force __le64) data); 218 data >>= (8 - len) * 8; 219 *val = (u32) data; 220 } else 221 *val = 0xffffffff; 222 return rc; 223 } 224 225 static int zpci_cfg_store(struct zpci_dev *zdev, int offset, u32 val, u8 len) 226 { 227 u64 req = ZPCI_CREATE_REQ(zdev->fh, ZPCI_PCIAS_CFGSPC, len); 228 u64 data = val; 229 int rc; 230 231 data <<= (8 - len) * 8; 232 data = (__force u64) cpu_to_le64(data); 233 rc = zpci_store(data, req, offset); 234 return rc; 235 } 236 237 void pcibios_fixup_bus(struct pci_bus *bus) 238 { 239 } 240 241 resource_size_t pcibios_align_resource(void *data, const struct resource *res, 242 resource_size_t size, 243 resource_size_t align) 244 { 245 return 0; 246 } 247 248 /* combine single writes by using store-block insn */ 249 void __iowrite64_copy(void __iomem *to, const void *from, size_t count) 250 { 251 zpci_memcpy_toio(to, from, count); 252 } 253 254 /* Create a virtual mapping cookie for a PCI BAR */ 255 void __iomem *pci_iomap_range(struct pci_dev *pdev, 256 int bar, 257 unsigned long offset, 258 unsigned long max) 259 { 260 struct zpci_dev *zdev = to_zpci(pdev); 261 int idx; 262 263 if (!pci_resource_len(pdev, bar)) 264 return NULL; 265 266 idx = zdev->bars[bar].map_idx; 267 spin_lock(&zpci_iomap_lock); 268 /* Detect overrun */ 269 WARN_ON(!++zpci_iomap_start[idx].count); 270 zpci_iomap_start[idx].fh = zdev->fh; 271 zpci_iomap_start[idx].bar = bar; 272 spin_unlock(&zpci_iomap_lock); 273 274 return (void __iomem *) ZPCI_ADDR(idx) + offset; 275 } 276 EXPORT_SYMBOL(pci_iomap_range); 277 278 void __iomem *pci_iomap(struct pci_dev *dev, int bar, unsigned long maxlen) 279 { 280 return pci_iomap_range(dev, bar, 0, maxlen); 281 } 282 EXPORT_SYMBOL(pci_iomap); 283 284 void pci_iounmap(struct pci_dev *pdev, void __iomem *addr) 285 { 286 unsigned int idx = ZPCI_IDX(addr); 287 288 spin_lock(&zpci_iomap_lock); 289 /* Detect underrun */ 290 WARN_ON(!zpci_iomap_start[idx].count); 291 if (!--zpci_iomap_start[idx].count) { 292 zpci_iomap_start[idx].fh = 0; 293 zpci_iomap_start[idx].bar = 0; 294 } 295 spin_unlock(&zpci_iomap_lock); 296 } 297 EXPORT_SYMBOL(pci_iounmap); 298 299 static int pci_read(struct pci_bus *bus, unsigned int devfn, int where, 300 int size, u32 *val) 301 { 302 struct zpci_dev *zdev = get_zdev_by_bus(bus); 303 int ret; 304 305 if (!zdev || devfn != ZPCI_DEVFN) 306 ret = -ENODEV; 307 else 308 ret = zpci_cfg_load(zdev, where, val, size); 309 310 return ret; 311 } 312 313 static int pci_write(struct pci_bus *bus, unsigned int devfn, int where, 314 int size, u32 val) 315 { 316 struct zpci_dev *zdev = get_zdev_by_bus(bus); 317 int ret; 318 319 if (!zdev || devfn != ZPCI_DEVFN) 320 ret = -ENODEV; 321 else 322 ret = zpci_cfg_store(zdev, where, val, size); 323 324 return ret; 325 } 326 327 static struct pci_ops pci_root_ops = { 328 .read = pci_read, 329 .write = pci_write, 330 }; 331 332 static void zpci_irq_handler(struct airq_struct *airq) 333 { 334 unsigned long si, ai; 335 struct airq_iv *aibv; 336 int irqs_on = 0; 337 338 inc_irq_stat(IRQIO_PCI); 339 for (si = 0;;) { 340 /* Scan adapter summary indicator bit vector */ 341 si = airq_iv_scan(zpci_aisb_iv, si, airq_iv_end(zpci_aisb_iv)); 342 if (si == -1UL) { 343 if (irqs_on++) 344 /* End of second scan with interrupts on. */ 345 break; 346 /* First scan complete, reenable interrupts. */ 347 zpci_set_irq_ctrl(SIC_IRQ_MODE_SINGLE, NULL, PCI_ISC); 348 si = 0; 349 continue; 350 } 351 352 /* Scan the adapter interrupt vector for this device. */ 353 aibv = zpci_aibv[si]; 354 for (ai = 0;;) { 355 ai = airq_iv_scan(aibv, ai, airq_iv_end(aibv)); 356 if (ai == -1UL) 357 break; 358 inc_irq_stat(IRQIO_MSI); 359 airq_iv_lock(aibv, ai); 360 generic_handle_irq(airq_iv_get_data(aibv, ai)); 361 airq_iv_unlock(aibv, ai); 362 } 363 } 364 } 365 366 int arch_setup_msi_irqs(struct pci_dev *pdev, int nvec, int type) 367 { 368 struct zpci_dev *zdev = to_zpci(pdev); 369 unsigned int hwirq, msi_vecs; 370 unsigned long aisb; 371 struct msi_desc *msi; 372 struct msi_msg msg; 373 int rc, irq; 374 375 if (type == PCI_CAP_ID_MSI && nvec > 1) 376 return 1; 377 msi_vecs = min_t(unsigned int, nvec, zdev->max_msi); 378 379 /* Allocate adapter summary indicator bit */ 380 rc = -EIO; 381 aisb = airq_iv_alloc_bit(zpci_aisb_iv); 382 if (aisb == -1UL) 383 goto out; 384 zdev->aisb = aisb; 385 386 /* Create adapter interrupt vector */ 387 rc = -ENOMEM; 388 zdev->aibv = airq_iv_create(msi_vecs, AIRQ_IV_DATA | AIRQ_IV_BITLOCK); 389 if (!zdev->aibv) 390 goto out_si; 391 392 /* Wire up shortcut pointer */ 393 zpci_aibv[aisb] = zdev->aibv; 394 395 /* Request MSI interrupts */ 396 hwirq = 0; 397 for_each_pci_msi_entry(msi, pdev) { 398 rc = -EIO; 399 irq = irq_alloc_desc(0); /* Alloc irq on node 0 */ 400 if (irq < 0) 401 goto out_msi; 402 rc = irq_set_msi_desc(irq, msi); 403 if (rc) 404 goto out_msi; 405 irq_set_chip_and_handler(irq, &zpci_irq_chip, 406 handle_simple_irq); 407 msg.data = hwirq; 408 msg.address_lo = zdev->msi_addr & 0xffffffff; 409 msg.address_hi = zdev->msi_addr >> 32; 410 pci_write_msi_msg(irq, &msg); 411 airq_iv_set_data(zdev->aibv, hwirq, irq); 412 hwirq++; 413 } 414 415 /* Enable adapter interrupts */ 416 rc = zpci_set_airq(zdev); 417 if (rc) 418 goto out_msi; 419 420 return (msi_vecs == nvec) ? 0 : msi_vecs; 421 422 out_msi: 423 for_each_pci_msi_entry(msi, pdev) { 424 if (hwirq-- == 0) 425 break; 426 irq_set_msi_desc(msi->irq, NULL); 427 irq_free_desc(msi->irq); 428 msi->msg.address_lo = 0; 429 msi->msg.address_hi = 0; 430 msi->msg.data = 0; 431 msi->irq = 0; 432 } 433 zpci_aibv[aisb] = NULL; 434 airq_iv_release(zdev->aibv); 435 out_si: 436 airq_iv_free_bit(zpci_aisb_iv, aisb); 437 out: 438 return rc; 439 } 440 441 void arch_teardown_msi_irqs(struct pci_dev *pdev) 442 { 443 struct zpci_dev *zdev = to_zpci(pdev); 444 struct msi_desc *msi; 445 int rc; 446 447 /* Disable adapter interrupts */ 448 rc = zpci_clear_airq(zdev); 449 if (rc) 450 return; 451 452 /* Release MSI interrupts */ 453 for_each_pci_msi_entry(msi, pdev) { 454 if (msi->msi_attrib.is_msix) 455 __pci_msix_desc_mask_irq(msi, 1); 456 else 457 __pci_msi_desc_mask_irq(msi, 1, 1); 458 irq_set_msi_desc(msi->irq, NULL); 459 irq_free_desc(msi->irq); 460 msi->msg.address_lo = 0; 461 msi->msg.address_hi = 0; 462 msi->msg.data = 0; 463 msi->irq = 0; 464 } 465 466 zpci_aibv[zdev->aisb] = NULL; 467 airq_iv_release(zdev->aibv); 468 airq_iv_free_bit(zpci_aisb_iv, zdev->aisb); 469 } 470 471 static void zpci_map_resources(struct pci_dev *pdev) 472 { 473 resource_size_t len; 474 int i; 475 476 for (i = 0; i < PCI_BAR_COUNT; i++) { 477 len = pci_resource_len(pdev, i); 478 if (!len) 479 continue; 480 pdev->resource[i].start = 481 (resource_size_t __force) pci_iomap(pdev, i, 0); 482 pdev->resource[i].end = pdev->resource[i].start + len - 1; 483 } 484 } 485 486 static void zpci_unmap_resources(struct pci_dev *pdev) 487 { 488 resource_size_t len; 489 int i; 490 491 for (i = 0; i < PCI_BAR_COUNT; i++) { 492 len = pci_resource_len(pdev, i); 493 if (!len) 494 continue; 495 pci_iounmap(pdev, (void __iomem __force *) 496 pdev->resource[i].start); 497 } 498 } 499 500 static struct airq_struct zpci_airq = { 501 .handler = zpci_irq_handler, 502 .isc = PCI_ISC, 503 }; 504 505 static int __init zpci_irq_init(void) 506 { 507 int rc; 508 509 rc = register_adapter_interrupt(&zpci_airq); 510 if (rc) 511 goto out; 512 /* Set summary to 1 to be called every time for the ISC. */ 513 *zpci_airq.lsi_ptr = 1; 514 515 rc = -ENOMEM; 516 zpci_aisb_iv = airq_iv_create(ZPCI_NR_DEVICES, AIRQ_IV_ALLOC); 517 if (!zpci_aisb_iv) 518 goto out_airq; 519 520 zpci_set_irq_ctrl(SIC_IRQ_MODE_SINGLE, NULL, PCI_ISC); 521 return 0; 522 523 out_airq: 524 unregister_adapter_interrupt(&zpci_airq); 525 out: 526 return rc; 527 } 528 529 static void zpci_irq_exit(void) 530 { 531 airq_iv_release(zpci_aisb_iv); 532 unregister_adapter_interrupt(&zpci_airq); 533 } 534 535 static int zpci_alloc_iomap(struct zpci_dev *zdev) 536 { 537 unsigned long entry; 538 539 spin_lock(&zpci_iomap_lock); 540 entry = find_first_zero_bit(zpci_iomap_bitmap, ZPCI_IOMAP_ENTRIES); 541 if (entry == ZPCI_IOMAP_ENTRIES) { 542 spin_unlock(&zpci_iomap_lock); 543 return -ENOSPC; 544 } 545 set_bit(entry, zpci_iomap_bitmap); 546 spin_unlock(&zpci_iomap_lock); 547 return entry; 548 } 549 550 static void zpci_free_iomap(struct zpci_dev *zdev, int entry) 551 { 552 spin_lock(&zpci_iomap_lock); 553 memset(&zpci_iomap_start[entry], 0, sizeof(struct zpci_iomap_entry)); 554 clear_bit(entry, zpci_iomap_bitmap); 555 spin_unlock(&zpci_iomap_lock); 556 } 557 558 static struct resource *__alloc_res(struct zpci_dev *zdev, unsigned long start, 559 unsigned long size, unsigned long flags) 560 { 561 struct resource *r; 562 563 r = kzalloc(sizeof(*r), GFP_KERNEL); 564 if (!r) 565 return NULL; 566 567 r->start = start; 568 r->end = r->start + size - 1; 569 r->flags = flags; 570 r->name = zdev->res_name; 571 572 if (request_resource(&iomem_resource, r)) { 573 kfree(r); 574 return NULL; 575 } 576 return r; 577 } 578 579 static int zpci_setup_bus_resources(struct zpci_dev *zdev, 580 struct list_head *resources) 581 { 582 unsigned long addr, size, flags; 583 struct resource *res; 584 int i, entry; 585 586 snprintf(zdev->res_name, sizeof(zdev->res_name), 587 "PCI Bus %04x:%02x", zdev->domain, ZPCI_BUS_NR); 588 589 for (i = 0; i < PCI_BAR_COUNT; i++) { 590 if (!zdev->bars[i].size) 591 continue; 592 entry = zpci_alloc_iomap(zdev); 593 if (entry < 0) 594 return entry; 595 zdev->bars[i].map_idx = entry; 596 597 /* only MMIO is supported */ 598 flags = IORESOURCE_MEM; 599 if (zdev->bars[i].val & 8) 600 flags |= IORESOURCE_PREFETCH; 601 if (zdev->bars[i].val & 4) 602 flags |= IORESOURCE_MEM_64; 603 604 addr = ZPCI_ADDR(entry); 605 size = 1UL << zdev->bars[i].size; 606 607 res = __alloc_res(zdev, addr, size, flags); 608 if (!res) { 609 zpci_free_iomap(zdev, entry); 610 return -ENOMEM; 611 } 612 zdev->bars[i].res = res; 613 pci_add_resource(resources, res); 614 } 615 616 return 0; 617 } 618 619 static void zpci_cleanup_bus_resources(struct zpci_dev *zdev) 620 { 621 int i; 622 623 for (i = 0; i < PCI_BAR_COUNT; i++) { 624 if (!zdev->bars[i].size || !zdev->bars[i].res) 625 continue; 626 627 zpci_free_iomap(zdev, zdev->bars[i].map_idx); 628 release_resource(zdev->bars[i].res); 629 kfree(zdev->bars[i].res); 630 } 631 } 632 633 int pcibios_add_device(struct pci_dev *pdev) 634 { 635 struct resource *res; 636 int i; 637 638 pdev->dev.groups = zpci_attr_groups; 639 pdev->dev.dma_ops = &s390_pci_dma_ops; 640 zpci_map_resources(pdev); 641 642 for (i = 0; i < PCI_BAR_COUNT; i++) { 643 res = &pdev->resource[i]; 644 if (res->parent || !res->flags) 645 continue; 646 pci_claim_resource(pdev, i); 647 } 648 649 return 0; 650 } 651 652 void pcibios_release_device(struct pci_dev *pdev) 653 { 654 zpci_unmap_resources(pdev); 655 } 656 657 int pcibios_enable_device(struct pci_dev *pdev, int mask) 658 { 659 struct zpci_dev *zdev = to_zpci(pdev); 660 661 zpci_debug_init_device(zdev, dev_name(&pdev->dev)); 662 zpci_fmb_enable_device(zdev); 663 664 return pci_enable_resources(pdev, mask); 665 } 666 667 void pcibios_disable_device(struct pci_dev *pdev) 668 { 669 struct zpci_dev *zdev = to_zpci(pdev); 670 671 zpci_fmb_disable_device(zdev); 672 zpci_debug_exit_device(zdev); 673 } 674 675 #ifdef CONFIG_HIBERNATE_CALLBACKS 676 static int zpci_restore(struct device *dev) 677 { 678 struct pci_dev *pdev = to_pci_dev(dev); 679 struct zpci_dev *zdev = to_zpci(pdev); 680 int ret = 0; 681 682 if (zdev->state != ZPCI_FN_STATE_ONLINE) 683 goto out; 684 685 ret = clp_enable_fh(zdev, ZPCI_NR_DMA_SPACES); 686 if (ret) 687 goto out; 688 689 zpci_map_resources(pdev); 690 zpci_register_ioat(zdev, 0, zdev->start_dma, zdev->end_dma, 691 (u64) zdev->dma_table); 692 693 out: 694 return ret; 695 } 696 697 static int zpci_freeze(struct device *dev) 698 { 699 struct pci_dev *pdev = to_pci_dev(dev); 700 struct zpci_dev *zdev = to_zpci(pdev); 701 702 if (zdev->state != ZPCI_FN_STATE_ONLINE) 703 return 0; 704 705 zpci_unregister_ioat(zdev, 0); 706 zpci_unmap_resources(pdev); 707 return clp_disable_fh(zdev); 708 } 709 710 struct dev_pm_ops pcibios_pm_ops = { 711 .thaw_noirq = zpci_restore, 712 .freeze_noirq = zpci_freeze, 713 .restore_noirq = zpci_restore, 714 .poweroff_noirq = zpci_freeze, 715 }; 716 #endif /* CONFIG_HIBERNATE_CALLBACKS */ 717 718 static int zpci_alloc_domain(struct zpci_dev *zdev) 719 { 720 if (zpci_unique_uid) { 721 zdev->domain = (u16) zdev->uid; 722 return 0; 723 } 724 725 spin_lock(&zpci_domain_lock); 726 zdev->domain = find_first_zero_bit(zpci_domain, ZPCI_NR_DEVICES); 727 if (zdev->domain == ZPCI_NR_DEVICES) { 728 spin_unlock(&zpci_domain_lock); 729 return -ENOSPC; 730 } 731 set_bit(zdev->domain, zpci_domain); 732 spin_unlock(&zpci_domain_lock); 733 return 0; 734 } 735 736 static void zpci_free_domain(struct zpci_dev *zdev) 737 { 738 if (zpci_unique_uid) 739 return; 740 741 spin_lock(&zpci_domain_lock); 742 clear_bit(zdev->domain, zpci_domain); 743 spin_unlock(&zpci_domain_lock); 744 } 745 746 void pcibios_remove_bus(struct pci_bus *bus) 747 { 748 struct zpci_dev *zdev = get_zdev_by_bus(bus); 749 750 zpci_exit_slot(zdev); 751 zpci_cleanup_bus_resources(zdev); 752 zpci_free_domain(zdev); 753 754 spin_lock(&zpci_list_lock); 755 list_del(&zdev->entry); 756 spin_unlock(&zpci_list_lock); 757 758 kfree(zdev); 759 } 760 761 static int zpci_scan_bus(struct zpci_dev *zdev) 762 { 763 LIST_HEAD(resources); 764 int ret; 765 766 ret = zpci_setup_bus_resources(zdev, &resources); 767 if (ret) 768 goto error; 769 770 zdev->bus = pci_scan_root_bus(NULL, ZPCI_BUS_NR, &pci_root_ops, 771 zdev, &resources); 772 if (!zdev->bus) { 773 ret = -EIO; 774 goto error; 775 } 776 zdev->bus->max_bus_speed = zdev->max_bus_speed; 777 pci_bus_add_devices(zdev->bus); 778 return 0; 779 780 error: 781 zpci_cleanup_bus_resources(zdev); 782 pci_free_resource_list(&resources); 783 return ret; 784 } 785 786 int zpci_enable_device(struct zpci_dev *zdev) 787 { 788 int rc; 789 790 rc = clp_enable_fh(zdev, ZPCI_NR_DMA_SPACES); 791 if (rc) 792 goto out; 793 794 rc = zpci_dma_init_device(zdev); 795 if (rc) 796 goto out_dma; 797 798 zdev->state = ZPCI_FN_STATE_ONLINE; 799 return 0; 800 801 out_dma: 802 clp_disable_fh(zdev); 803 out: 804 return rc; 805 } 806 EXPORT_SYMBOL_GPL(zpci_enable_device); 807 808 int zpci_disable_device(struct zpci_dev *zdev) 809 { 810 zpci_dma_exit_device(zdev); 811 return clp_disable_fh(zdev); 812 } 813 EXPORT_SYMBOL_GPL(zpci_disable_device); 814 815 int zpci_create_device(struct zpci_dev *zdev) 816 { 817 int rc; 818 819 rc = zpci_alloc_domain(zdev); 820 if (rc) 821 goto out; 822 823 mutex_init(&zdev->lock); 824 if (zdev->state == ZPCI_FN_STATE_CONFIGURED) { 825 rc = zpci_enable_device(zdev); 826 if (rc) 827 goto out_free; 828 } 829 rc = zpci_scan_bus(zdev); 830 if (rc) 831 goto out_disable; 832 833 spin_lock(&zpci_list_lock); 834 list_add_tail(&zdev->entry, &zpci_list); 835 spin_unlock(&zpci_list_lock); 836 837 zpci_init_slot(zdev); 838 839 return 0; 840 841 out_disable: 842 if (zdev->state == ZPCI_FN_STATE_ONLINE) 843 zpci_disable_device(zdev); 844 out_free: 845 zpci_free_domain(zdev); 846 out: 847 return rc; 848 } 849 850 void zpci_stop_device(struct zpci_dev *zdev) 851 { 852 zpci_dma_exit_device(zdev); 853 /* 854 * Note: SCLP disables fh via set-pci-fn so don't 855 * do that here. 856 */ 857 } 858 EXPORT_SYMBOL_GPL(zpci_stop_device); 859 860 int zpci_report_error(struct pci_dev *pdev, 861 struct zpci_report_error_header *report) 862 { 863 struct zpci_dev *zdev = to_zpci(pdev); 864 865 return sclp_pci_report(report, zdev->fh, zdev->fid); 866 } 867 EXPORT_SYMBOL(zpci_report_error); 868 869 static int zpci_mem_init(void) 870 { 871 BUILD_BUG_ON(!is_power_of_2(__alignof__(struct zpci_fmb)) || 872 __alignof__(struct zpci_fmb) < sizeof(struct zpci_fmb)); 873 874 zdev_fmb_cache = kmem_cache_create("PCI_FMB_cache", sizeof(struct zpci_fmb), 875 __alignof__(struct zpci_fmb), 0, NULL); 876 if (!zdev_fmb_cache) 877 goto error_fmb; 878 879 zpci_iomap_start = kcalloc(ZPCI_IOMAP_ENTRIES, 880 sizeof(*zpci_iomap_start), GFP_KERNEL); 881 if (!zpci_iomap_start) 882 goto error_iomap; 883 884 zpci_iomap_bitmap = kcalloc(BITS_TO_LONGS(ZPCI_IOMAP_ENTRIES), 885 sizeof(*zpci_iomap_bitmap), GFP_KERNEL); 886 if (!zpci_iomap_bitmap) 887 goto error_iomap_bitmap; 888 889 return 0; 890 error_iomap_bitmap: 891 kfree(zpci_iomap_start); 892 error_iomap: 893 kmem_cache_destroy(zdev_fmb_cache); 894 error_fmb: 895 return -ENOMEM; 896 } 897 898 static void zpci_mem_exit(void) 899 { 900 kfree(zpci_iomap_bitmap); 901 kfree(zpci_iomap_start); 902 kmem_cache_destroy(zdev_fmb_cache); 903 } 904 905 static unsigned int s390_pci_probe = 1; 906 static unsigned int s390_pci_initialized; 907 908 char * __init pcibios_setup(char *str) 909 { 910 if (!strcmp(str, "off")) { 911 s390_pci_probe = 0; 912 return NULL; 913 } 914 return str; 915 } 916 917 bool zpci_is_enabled(void) 918 { 919 return s390_pci_initialized; 920 } 921 922 static int __init pci_base_init(void) 923 { 924 int rc; 925 926 if (!s390_pci_probe) 927 return 0; 928 929 if (!test_facility(69) || !test_facility(71) || !test_facility(72)) 930 return 0; 931 932 rc = zpci_debug_init(); 933 if (rc) 934 goto out; 935 936 rc = zpci_mem_init(); 937 if (rc) 938 goto out_mem; 939 940 rc = zpci_irq_init(); 941 if (rc) 942 goto out_irq; 943 944 rc = zpci_dma_init(); 945 if (rc) 946 goto out_dma; 947 948 rc = clp_scan_pci_devices(); 949 if (rc) 950 goto out_find; 951 952 s390_pci_initialized = 1; 953 return 0; 954 955 out_find: 956 zpci_dma_exit(); 957 out_dma: 958 zpci_irq_exit(); 959 out_irq: 960 zpci_mem_exit(); 961 out_mem: 962 zpci_debug_exit(); 963 out: 964 return rc; 965 } 966 subsys_initcall_sync(pci_base_init); 967 968 void zpci_rescan(void) 969 { 970 if (zpci_is_enabled()) 971 clp_rescan_pci_devices_simple(); 972 } 973