1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright IBM Corp. 2012 4 * 5 * Author(s): 6 * Jan Glauber <jang@linux.vnet.ibm.com> 7 * 8 * The System z PCI code is a rewrite from a prototype by 9 * the following people (Kudoz!): 10 * Alexander Schmidt 11 * Christoph Raisch 12 * Hannes Hering 13 * Hoang-Nam Nguyen 14 * Jan-Bernd Themann 15 * Stefan Roscher 16 * Thomas Klein 17 */ 18 19 #define pr_fmt(fmt) "zpci: " 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/seq_file.h> 27 #include <linux/jump_label.h> 28 #include <linux/pci.h> 29 #include <linux/printk.h> 30 #include <linux/lockdep.h> 31 #include <linux/list_sort.h> 32 33 #include <asm/machine.h> 34 #include <asm/isc.h> 35 #include <asm/airq.h> 36 #include <asm/facility.h> 37 #include <asm/pci_insn.h> 38 #include <asm/pci_clp.h> 39 #include <asm/pci_dma.h> 40 41 #include "pci_bus.h" 42 #include "pci_iov.h" 43 44 /* list of all detected zpci devices */ 45 static LIST_HEAD(zpci_list); 46 static DEFINE_SPINLOCK(zpci_list_lock); 47 static DEFINE_MUTEX(zpci_add_remove_lock); 48 49 static DECLARE_BITMAP(zpci_domain, ZPCI_DOMAIN_BITMAP_SIZE); 50 static DEFINE_SPINLOCK(zpci_domain_lock); 51 52 #define ZPCI_IOMAP_ENTRIES \ 53 min(((unsigned long) ZPCI_NR_DEVICES * PCI_STD_NUM_BARS / 2), \ 54 ZPCI_IOMAP_MAX_ENTRIES) 55 56 unsigned int s390_pci_no_rid; 57 58 static DEFINE_SPINLOCK(zpci_iomap_lock); 59 static unsigned long *zpci_iomap_bitmap; 60 struct zpci_iomap_entry *zpci_iomap_start; 61 EXPORT_SYMBOL_GPL(zpci_iomap_start); 62 63 DEFINE_STATIC_KEY_FALSE(have_mio); 64 65 static struct kmem_cache *zdev_fmb_cache; 66 67 /* AEN structures that must be preserved over KVM module re-insertion */ 68 union zpci_sic_iib *zpci_aipb; 69 EXPORT_SYMBOL_GPL(zpci_aipb); 70 struct airq_iv *zpci_aif_sbv; 71 EXPORT_SYMBOL_GPL(zpci_aif_sbv); 72 73 void zpci_zdev_put(struct zpci_dev *zdev) 74 { 75 if (!zdev) 76 return; 77 mutex_lock(&zpci_add_remove_lock); 78 kref_put_lock(&zdev->kref, zpci_release_device, &zpci_list_lock); 79 mutex_unlock(&zpci_add_remove_lock); 80 } 81 82 struct zpci_dev *get_zdev_by_fid(u32 fid) 83 { 84 struct zpci_dev *tmp, *zdev = NULL; 85 86 spin_lock(&zpci_list_lock); 87 list_for_each_entry(tmp, &zpci_list, entry) { 88 if (tmp->fid == fid) { 89 zdev = tmp; 90 zpci_zdev_get(zdev); 91 break; 92 } 93 } 94 spin_unlock(&zpci_list_lock); 95 return zdev; 96 } 97 98 void zpci_remove_reserved_devices(void) 99 { 100 struct zpci_dev *tmp, *zdev; 101 enum zpci_state state; 102 LIST_HEAD(remove); 103 104 spin_lock(&zpci_list_lock); 105 list_for_each_entry_safe(zdev, tmp, &zpci_list, entry) { 106 if (zdev->state == ZPCI_FN_STATE_STANDBY && 107 !clp_get_state(zdev->fid, &state) && 108 state == ZPCI_FN_STATE_RESERVED) 109 list_move_tail(&zdev->entry, &remove); 110 } 111 spin_unlock(&zpci_list_lock); 112 113 list_for_each_entry_safe(zdev, tmp, &remove, entry) 114 zpci_device_reserved(zdev); 115 } 116 117 int pci_domain_nr(struct pci_bus *bus) 118 { 119 return ((struct zpci_bus *) bus->sysdata)->domain_nr; 120 } 121 EXPORT_SYMBOL_GPL(pci_domain_nr); 122 123 int pci_proc_domain(struct pci_bus *bus) 124 { 125 return pci_domain_nr(bus); 126 } 127 EXPORT_SYMBOL_GPL(pci_proc_domain); 128 129 /* Modify PCI: Register I/O address translation parameters */ 130 int zpci_register_ioat(struct zpci_dev *zdev, u8 dmaas, 131 u64 base, u64 limit, u64 iota, u8 *status) 132 { 133 u64 req = ZPCI_CREATE_REQ(zdev->fh, dmaas, ZPCI_MOD_FC_REG_IOAT); 134 struct zpci_fib fib = {0}; 135 u8 cc; 136 137 fib.pba = base; 138 /* Work around off by one in ISM virt device */ 139 if (zdev->pft == PCI_FUNC_TYPE_ISM && limit > base) 140 fib.pal = limit + (1 << 12); 141 else 142 fib.pal = limit; 143 fib.iota = iota; 144 fib.gd = zdev->gisa; 145 cc = zpci_mod_fc(req, &fib, status); 146 if (cc) 147 zpci_dbg(3, "reg ioat fid:%x, cc:%d, status:%d\n", zdev->fid, cc, *status); 148 return cc; 149 } 150 EXPORT_SYMBOL_GPL(zpci_register_ioat); 151 152 /* Modify PCI: Unregister I/O address translation parameters */ 153 int zpci_unregister_ioat(struct zpci_dev *zdev, u8 dmaas) 154 { 155 u64 req = ZPCI_CREATE_REQ(zdev->fh, dmaas, ZPCI_MOD_FC_DEREG_IOAT); 156 struct zpci_fib fib = {0}; 157 u8 cc, status; 158 159 fib.gd = zdev->gisa; 160 161 cc = zpci_mod_fc(req, &fib, &status); 162 if (cc) 163 zpci_dbg(3, "unreg ioat fid:%x, cc:%d, status:%d\n", zdev->fid, cc, status); 164 return cc; 165 } 166 167 /* Modify PCI: Set PCI function measurement parameters */ 168 int zpci_fmb_enable_device(struct zpci_dev *zdev) 169 { 170 u64 req = ZPCI_CREATE_REQ(zdev->fh, 0, ZPCI_MOD_FC_SET_MEASURE); 171 struct zpci_iommu_ctrs *ctrs; 172 struct zpci_fib fib = {0}; 173 unsigned long flags; 174 u8 cc, status; 175 176 if (zdev->fmb || sizeof(*zdev->fmb) < zdev->fmb_length) 177 return -EINVAL; 178 179 zdev->fmb = kmem_cache_zalloc(zdev_fmb_cache, GFP_KERNEL); 180 if (!zdev->fmb) 181 return -ENOMEM; 182 WARN_ON((u64) zdev->fmb & 0xf); 183 184 /* reset software counters */ 185 spin_lock_irqsave(&zdev->dom_lock, flags); 186 ctrs = zpci_get_iommu_ctrs(zdev); 187 if (ctrs) { 188 atomic64_set(&ctrs->mapped_pages, 0); 189 atomic64_set(&ctrs->unmapped_pages, 0); 190 atomic64_set(&ctrs->global_rpcits, 0); 191 atomic64_set(&ctrs->sync_map_rpcits, 0); 192 atomic64_set(&ctrs->sync_rpcits, 0); 193 } 194 spin_unlock_irqrestore(&zdev->dom_lock, flags); 195 196 197 fib.fmb_addr = virt_to_phys(zdev->fmb); 198 fib.gd = zdev->gisa; 199 cc = zpci_mod_fc(req, &fib, &status); 200 if (cc) { 201 kmem_cache_free(zdev_fmb_cache, zdev->fmb); 202 zdev->fmb = NULL; 203 } 204 return cc ? -EIO : 0; 205 } 206 207 /* Modify PCI: Disable PCI function measurement */ 208 int zpci_fmb_disable_device(struct zpci_dev *zdev) 209 { 210 u64 req = ZPCI_CREATE_REQ(zdev->fh, 0, ZPCI_MOD_FC_SET_MEASURE); 211 struct zpci_fib fib = {0}; 212 u8 cc, status; 213 214 if (!zdev->fmb) 215 return -EINVAL; 216 217 fib.gd = zdev->gisa; 218 219 /* Function measurement is disabled if fmb address is zero */ 220 cc = zpci_mod_fc(req, &fib, &status); 221 if (cc == 3) /* Function already gone. */ 222 cc = 0; 223 224 if (!cc) { 225 kmem_cache_free(zdev_fmb_cache, zdev->fmb); 226 zdev->fmb = NULL; 227 } 228 return cc ? -EIO : 0; 229 } 230 231 static int zpci_cfg_load(struct zpci_dev *zdev, int offset, u32 *val, u8 len) 232 { 233 u64 req = ZPCI_CREATE_REQ(zdev->fh, ZPCI_PCIAS_CFGSPC, len); 234 u64 data; 235 int rc; 236 237 rc = __zpci_load(&data, req, offset); 238 if (!rc) { 239 data = le64_to_cpu((__force __le64) data); 240 data >>= (8 - len) * 8; 241 *val = (u32) data; 242 } else 243 *val = 0xffffffff; 244 return rc; 245 } 246 247 static int zpci_cfg_store(struct zpci_dev *zdev, int offset, u32 val, u8 len) 248 { 249 u64 req = ZPCI_CREATE_REQ(zdev->fh, ZPCI_PCIAS_CFGSPC, len); 250 u64 data = val; 251 int rc; 252 253 data <<= (8 - len) * 8; 254 data = (__force u64) cpu_to_le64(data); 255 rc = __zpci_store(data, req, offset); 256 return rc; 257 } 258 259 resource_size_t pcibios_align_resource(void *data, const struct resource *res, 260 resource_size_t size, 261 resource_size_t align) 262 { 263 return 0; 264 } 265 266 void __iomem *ioremap_prot(phys_addr_t phys_addr, size_t size, 267 pgprot_t prot) 268 { 269 /* 270 * When PCI MIO instructions are unavailable the "physical" address 271 * encodes a hint for accessing the PCI memory space it represents. 272 * Just pass it unchanged such that ioread/iowrite can decode it. 273 */ 274 if (!static_branch_unlikely(&have_mio)) 275 return (void __iomem *)phys_addr; 276 277 return generic_ioremap_prot(phys_addr, size, prot); 278 } 279 EXPORT_SYMBOL(ioremap_prot); 280 281 void iounmap(volatile void __iomem *addr) 282 { 283 if (static_branch_likely(&have_mio)) 284 generic_iounmap(addr); 285 } 286 EXPORT_SYMBOL(iounmap); 287 288 /* Create a virtual mapping cookie for a PCI BAR */ 289 static void __iomem *pci_iomap_range_fh(struct pci_dev *pdev, int bar, 290 unsigned long offset, unsigned long max) 291 { 292 struct zpci_dev *zdev = to_zpci(pdev); 293 int idx; 294 295 idx = zdev->bars[bar].map_idx; 296 spin_lock(&zpci_iomap_lock); 297 /* Detect overrun */ 298 WARN_ON(!++zpci_iomap_start[idx].count); 299 zpci_iomap_start[idx].fh = zdev->fh; 300 zpci_iomap_start[idx].bar = bar; 301 spin_unlock(&zpci_iomap_lock); 302 303 return (void __iomem *) ZPCI_ADDR(idx) + offset; 304 } 305 306 static void __iomem *pci_iomap_range_mio(struct pci_dev *pdev, int bar, 307 unsigned long offset, 308 unsigned long max) 309 { 310 unsigned long barsize = pci_resource_len(pdev, bar); 311 struct zpci_dev *zdev = to_zpci(pdev); 312 void __iomem *iova; 313 314 iova = ioremap((unsigned long) zdev->bars[bar].mio_wt, barsize); 315 return iova ? iova + offset : iova; 316 } 317 318 void __iomem *pci_iomap_range(struct pci_dev *pdev, int bar, 319 unsigned long offset, unsigned long max) 320 { 321 if (bar >= PCI_STD_NUM_BARS || !pci_resource_len(pdev, bar)) 322 return NULL; 323 324 if (static_branch_likely(&have_mio)) 325 return pci_iomap_range_mio(pdev, bar, offset, max); 326 else 327 return pci_iomap_range_fh(pdev, bar, offset, max); 328 } 329 EXPORT_SYMBOL(pci_iomap_range); 330 331 void __iomem *pci_iomap(struct pci_dev *dev, int bar, unsigned long maxlen) 332 { 333 return pci_iomap_range(dev, bar, 0, maxlen); 334 } 335 EXPORT_SYMBOL(pci_iomap); 336 337 static void __iomem *pci_iomap_wc_range_mio(struct pci_dev *pdev, int bar, 338 unsigned long offset, unsigned long max) 339 { 340 unsigned long barsize = pci_resource_len(pdev, bar); 341 struct zpci_dev *zdev = to_zpci(pdev); 342 void __iomem *iova; 343 344 iova = ioremap((unsigned long) zdev->bars[bar].mio_wb, barsize); 345 return iova ? iova + offset : iova; 346 } 347 348 void __iomem *pci_iomap_wc_range(struct pci_dev *pdev, int bar, 349 unsigned long offset, unsigned long max) 350 { 351 if (bar >= PCI_STD_NUM_BARS || !pci_resource_len(pdev, bar)) 352 return NULL; 353 354 if (static_branch_likely(&have_mio)) 355 return pci_iomap_wc_range_mio(pdev, bar, offset, max); 356 else 357 return pci_iomap_range_fh(pdev, bar, offset, max); 358 } 359 EXPORT_SYMBOL(pci_iomap_wc_range); 360 361 void __iomem *pci_iomap_wc(struct pci_dev *dev, int bar, unsigned long maxlen) 362 { 363 return pci_iomap_wc_range(dev, bar, 0, maxlen); 364 } 365 EXPORT_SYMBOL(pci_iomap_wc); 366 367 static void pci_iounmap_fh(struct pci_dev *pdev, void __iomem *addr) 368 { 369 unsigned int idx = ZPCI_IDX(addr); 370 371 spin_lock(&zpci_iomap_lock); 372 /* Detect underrun */ 373 WARN_ON(!zpci_iomap_start[idx].count); 374 if (!--zpci_iomap_start[idx].count) { 375 zpci_iomap_start[idx].fh = 0; 376 zpci_iomap_start[idx].bar = 0; 377 } 378 spin_unlock(&zpci_iomap_lock); 379 } 380 381 static void pci_iounmap_mio(struct pci_dev *pdev, void __iomem *addr) 382 { 383 iounmap(addr); 384 } 385 386 void pci_iounmap(struct pci_dev *pdev, void __iomem *addr) 387 { 388 if (static_branch_likely(&have_mio)) 389 pci_iounmap_mio(pdev, addr); 390 else 391 pci_iounmap_fh(pdev, addr); 392 } 393 EXPORT_SYMBOL(pci_iounmap); 394 395 static int pci_read(struct pci_bus *bus, unsigned int devfn, int where, 396 int size, u32 *val) 397 { 398 struct zpci_dev *zdev = zdev_from_bus(bus, devfn); 399 400 return (zdev) ? zpci_cfg_load(zdev, where, val, size) : -ENODEV; 401 } 402 403 static int pci_write(struct pci_bus *bus, unsigned int devfn, int where, 404 int size, u32 val) 405 { 406 struct zpci_dev *zdev = zdev_from_bus(bus, devfn); 407 408 return (zdev) ? zpci_cfg_store(zdev, where, val, size) : -ENODEV; 409 } 410 411 static struct pci_ops pci_root_ops = { 412 .read = pci_read, 413 .write = pci_write, 414 }; 415 416 static void zpci_map_resources(struct pci_dev *pdev) 417 { 418 struct zpci_dev *zdev = to_zpci(pdev); 419 resource_size_t len; 420 int i; 421 422 for (i = 0; i < PCI_STD_NUM_BARS; i++) { 423 len = pci_resource_len(pdev, i); 424 if (!len) 425 continue; 426 427 if (zpci_use_mio(zdev)) 428 pdev->resource[i].start = 429 (resource_size_t __force) zdev->bars[i].mio_wt; 430 else 431 pdev->resource[i].start = (resource_size_t __force) 432 pci_iomap_range_fh(pdev, i, 0, 0); 433 pdev->resource[i].end = pdev->resource[i].start + len - 1; 434 } 435 436 zpci_iov_map_resources(pdev); 437 } 438 439 static void zpci_unmap_resources(struct pci_dev *pdev) 440 { 441 struct zpci_dev *zdev = to_zpci(pdev); 442 resource_size_t len; 443 int i; 444 445 if (zpci_use_mio(zdev)) 446 return; 447 448 for (i = 0; i < PCI_STD_NUM_BARS; i++) { 449 len = pci_resource_len(pdev, i); 450 if (!len) 451 continue; 452 pci_iounmap_fh(pdev, (void __iomem __force *) 453 pdev->resource[i].start); 454 } 455 } 456 457 static int zpci_alloc_iomap(struct zpci_dev *zdev) 458 { 459 unsigned long entry; 460 461 spin_lock(&zpci_iomap_lock); 462 entry = find_first_zero_bit(zpci_iomap_bitmap, ZPCI_IOMAP_ENTRIES); 463 if (entry == ZPCI_IOMAP_ENTRIES) { 464 spin_unlock(&zpci_iomap_lock); 465 return -ENOSPC; 466 } 467 set_bit(entry, zpci_iomap_bitmap); 468 spin_unlock(&zpci_iomap_lock); 469 return entry; 470 } 471 472 static void zpci_free_iomap(struct zpci_dev *zdev, int entry) 473 { 474 spin_lock(&zpci_iomap_lock); 475 memset(&zpci_iomap_start[entry], 0, sizeof(struct zpci_iomap_entry)); 476 clear_bit(entry, zpci_iomap_bitmap); 477 spin_unlock(&zpci_iomap_lock); 478 } 479 480 static void zpci_do_update_iomap_fh(struct zpci_dev *zdev, u32 fh) 481 { 482 int bar, idx; 483 484 spin_lock(&zpci_iomap_lock); 485 for (bar = 0; bar < PCI_STD_NUM_BARS; bar++) { 486 if (!zdev->bars[bar].size) 487 continue; 488 idx = zdev->bars[bar].map_idx; 489 if (!zpci_iomap_start[idx].count) 490 continue; 491 WRITE_ONCE(zpci_iomap_start[idx].fh, zdev->fh); 492 } 493 spin_unlock(&zpci_iomap_lock); 494 } 495 496 void zpci_update_fh(struct zpci_dev *zdev, u32 fh) 497 { 498 if (!fh || zdev->fh == fh) 499 return; 500 501 zdev->fh = fh; 502 if (zpci_use_mio(zdev)) 503 return; 504 if (zdev->has_resources && zdev_enabled(zdev)) 505 zpci_do_update_iomap_fh(zdev, fh); 506 } 507 508 static struct resource *__alloc_res(struct zpci_dev *zdev, unsigned long start, 509 unsigned long size, unsigned long flags) 510 { 511 struct resource *r; 512 513 r = kzalloc(sizeof(*r), GFP_KERNEL); 514 if (!r) 515 return NULL; 516 517 r->start = start; 518 r->end = r->start + size - 1; 519 r->flags = flags; 520 r->name = zdev->res_name; 521 522 if (request_resource(&iomem_resource, r)) { 523 kfree(r); 524 return NULL; 525 } 526 return r; 527 } 528 529 int zpci_setup_bus_resources(struct zpci_dev *zdev) 530 { 531 unsigned long addr, size, flags; 532 struct resource *res; 533 int i, entry; 534 535 snprintf(zdev->res_name, sizeof(zdev->res_name), 536 "PCI Bus %04x:%02x", zdev->uid, ZPCI_BUS_NR); 537 538 for (i = 0; i < PCI_STD_NUM_BARS; i++) { 539 if (!zdev->bars[i].size) 540 continue; 541 entry = zpci_alloc_iomap(zdev); 542 if (entry < 0) 543 return entry; 544 zdev->bars[i].map_idx = entry; 545 546 /* only MMIO is supported */ 547 flags = IORESOURCE_MEM; 548 if (zdev->bars[i].val & 8) 549 flags |= IORESOURCE_PREFETCH; 550 if (zdev->bars[i].val & 4) 551 flags |= IORESOURCE_MEM_64; 552 553 if (zpci_use_mio(zdev)) 554 addr = (unsigned long) zdev->bars[i].mio_wt; 555 else 556 addr = ZPCI_ADDR(entry); 557 size = 1UL << zdev->bars[i].size; 558 559 res = __alloc_res(zdev, addr, size, flags); 560 if (!res) { 561 zpci_free_iomap(zdev, entry); 562 return -ENOMEM; 563 } 564 zdev->bars[i].res = res; 565 } 566 zdev->has_resources = 1; 567 568 return 0; 569 } 570 571 static void zpci_cleanup_bus_resources(struct zpci_dev *zdev) 572 { 573 struct resource *res; 574 int i; 575 576 pci_lock_rescan_remove(); 577 for (i = 0; i < PCI_STD_NUM_BARS; i++) { 578 res = zdev->bars[i].res; 579 if (!res) 580 continue; 581 582 release_resource(res); 583 pci_bus_remove_resource(zdev->zbus->bus, res); 584 zpci_free_iomap(zdev, zdev->bars[i].map_idx); 585 zdev->bars[i].res = NULL; 586 kfree(res); 587 } 588 zdev->has_resources = 0; 589 pci_unlock_rescan_remove(); 590 } 591 592 int pcibios_device_add(struct pci_dev *pdev) 593 { 594 struct zpci_dev *zdev = to_zpci(pdev); 595 struct resource *res; 596 int i; 597 598 /* The pdev has a reference to the zdev via its bus */ 599 zpci_zdev_get(zdev); 600 if (pdev->is_physfn) 601 pdev->no_vf_scan = 1; 602 603 zpci_map_resources(pdev); 604 605 for (i = 0; i < PCI_STD_NUM_BARS; i++) { 606 res = &pdev->resource[i]; 607 if (res->parent || !res->flags) 608 continue; 609 pci_claim_resource(pdev, i); 610 } 611 612 return 0; 613 } 614 615 void pcibios_release_device(struct pci_dev *pdev) 616 { 617 struct zpci_dev *zdev = to_zpci(pdev); 618 619 zpci_unmap_resources(pdev); 620 zpci_zdev_put(zdev); 621 } 622 623 int pcibios_enable_device(struct pci_dev *pdev, int mask) 624 { 625 struct zpci_dev *zdev = to_zpci(pdev); 626 627 zpci_debug_init_device(zdev, dev_name(&pdev->dev)); 628 zpci_fmb_enable_device(zdev); 629 630 return pci_enable_resources(pdev, mask); 631 } 632 633 void pcibios_disable_device(struct pci_dev *pdev) 634 { 635 struct zpci_dev *zdev = to_zpci(pdev); 636 637 zpci_fmb_disable_device(zdev); 638 zpci_debug_exit_device(zdev); 639 } 640 641 static int __zpci_register_domain(int domain) 642 { 643 spin_lock(&zpci_domain_lock); 644 if (test_bit(domain, zpci_domain)) { 645 spin_unlock(&zpci_domain_lock); 646 pr_err("Domain %04x is already assigned\n", domain); 647 return -EEXIST; 648 } 649 set_bit(domain, zpci_domain); 650 spin_unlock(&zpci_domain_lock); 651 return domain; 652 } 653 654 static int __zpci_alloc_domain(void) 655 { 656 int domain; 657 658 spin_lock(&zpci_domain_lock); 659 /* 660 * We can always auto allocate domains below ZPCI_NR_DEVICES. 661 * There is either a free domain or we have reached the maximum in 662 * which case we would have bailed earlier. 663 */ 664 domain = find_first_zero_bit(zpci_domain, ZPCI_NR_DEVICES); 665 set_bit(domain, zpci_domain); 666 spin_unlock(&zpci_domain_lock); 667 return domain; 668 } 669 670 int zpci_alloc_domain(int domain) 671 { 672 if (zpci_unique_uid) { 673 if (domain) 674 return __zpci_register_domain(domain); 675 pr_warn("UID checking was active but no UID is provided: switching to automatic domain allocation\n"); 676 update_uid_checking(false); 677 } 678 return __zpci_alloc_domain(); 679 } 680 681 void zpci_free_domain(int domain) 682 { 683 spin_lock(&zpci_domain_lock); 684 clear_bit(domain, zpci_domain); 685 spin_unlock(&zpci_domain_lock); 686 } 687 688 689 int zpci_enable_device(struct zpci_dev *zdev) 690 { 691 u32 fh = zdev->fh; 692 int rc = 0; 693 694 if (clp_enable_fh(zdev, &fh, ZPCI_NR_DMA_SPACES)) 695 rc = -EIO; 696 else 697 zpci_update_fh(zdev, fh); 698 return rc; 699 } 700 EXPORT_SYMBOL_GPL(zpci_enable_device); 701 702 int zpci_reenable_device(struct zpci_dev *zdev) 703 { 704 u8 status; 705 int rc; 706 707 rc = zpci_enable_device(zdev); 708 if (rc) 709 return rc; 710 711 if (zdev->msi_nr_irqs > 0) { 712 rc = zpci_set_irq(zdev); 713 if (rc) 714 return rc; 715 } 716 717 rc = zpci_iommu_register_ioat(zdev, &status); 718 if (rc) 719 zpci_disable_device(zdev); 720 721 return rc; 722 } 723 EXPORT_SYMBOL_GPL(zpci_reenable_device); 724 725 int zpci_disable_device(struct zpci_dev *zdev) 726 { 727 u32 fh = zdev->fh; 728 int cc, rc = 0; 729 730 cc = clp_disable_fh(zdev, &fh); 731 if (!cc) { 732 zpci_update_fh(zdev, fh); 733 } else if (cc == CLP_RC_SETPCIFN_ALRDY) { 734 pr_info("Disabling PCI function %08x had no effect as it was already disabled\n", 735 zdev->fid); 736 /* Function is already disabled - update handle */ 737 rc = clp_refresh_fh(zdev->fid, &fh); 738 if (!rc) { 739 zpci_update_fh(zdev, fh); 740 rc = -EINVAL; 741 } 742 } else { 743 rc = -EIO; 744 } 745 return rc; 746 } 747 EXPORT_SYMBOL_GPL(zpci_disable_device); 748 749 /** 750 * zpci_hot_reset_device - perform a reset of the given zPCI function 751 * @zdev: the slot which should be reset 752 * 753 * Performs a low level reset of the zPCI function. The reset is low level in 754 * the sense that the zPCI function can be reset without detaching it from the 755 * common PCI subsystem. The reset may be performed while under control of 756 * either DMA or IOMMU APIs in which case the existing DMA/IOMMU translation 757 * table is reinstated at the end of the reset. 758 * 759 * After the reset the functions internal state is reset to an initial state 760 * equivalent to its state during boot when first probing a driver. 761 * Consequently after reset the PCI function requires re-initialization via the 762 * common PCI code including re-enabling IRQs via pci_alloc_irq_vectors() 763 * and enabling the function via e.g. pci_enable_device_flags(). The caller 764 * must guard against concurrent reset attempts. 765 * 766 * In most cases this function should not be called directly but through 767 * pci_reset_function() or pci_reset_bus() which handle the save/restore and 768 * locking - asserted by lockdep. 769 * 770 * Return: 0 on success and an error value otherwise 771 */ 772 int zpci_hot_reset_device(struct zpci_dev *zdev) 773 { 774 int rc; 775 776 lockdep_assert_held(&zdev->state_lock); 777 zpci_dbg(3, "rst fid:%x, fh:%x\n", zdev->fid, zdev->fh); 778 if (zdev_enabled(zdev)) { 779 /* Disables device access, DMAs and IRQs (reset state) */ 780 rc = zpci_disable_device(zdev); 781 /* 782 * Due to a z/VM vs LPAR inconsistency in the error state the 783 * FH may indicate an enabled device but disable says the 784 * device is already disabled don't treat it as an error here. 785 */ 786 if (rc == -EINVAL) 787 rc = 0; 788 if (rc) 789 return rc; 790 } 791 792 rc = zpci_reenable_device(zdev); 793 794 return rc; 795 } 796 797 /** 798 * zpci_create_device() - Create a new zpci_dev and add it to the zbus 799 * @fid: Function ID of the device to be created 800 * @fh: Current Function Handle of the device to be created 801 * @state: Initial state after creation either Standby or Configured 802 * 803 * Allocates a new struct zpci_dev and queries the platform for its details. 804 * If successful the device can subsequently be added to the zPCI subsystem 805 * using zpci_add_device(). 806 * 807 * Returns: the zdev on success or an error pointer otherwise 808 */ 809 struct zpci_dev *zpci_create_device(u32 fid, u32 fh, enum zpci_state state) 810 { 811 struct zpci_dev *zdev; 812 int rc; 813 814 zdev = kzalloc(sizeof(*zdev), GFP_KERNEL); 815 if (!zdev) 816 return ERR_PTR(-ENOMEM); 817 818 /* FID and Function Handle are the static/dynamic identifiers */ 819 zdev->fid = fid; 820 zdev->fh = fh; 821 822 /* Query function properties and update zdev */ 823 rc = clp_query_pci_fn(zdev); 824 if (rc) 825 goto error; 826 zdev->state = state; 827 828 mutex_init(&zdev->state_lock); 829 mutex_init(&zdev->fmb_lock); 830 mutex_init(&zdev->kzdev_lock); 831 832 return zdev; 833 834 error: 835 zpci_dbg(0, "crt fid:%x, rc:%d\n", fid, rc); 836 kfree(zdev); 837 return ERR_PTR(rc); 838 } 839 840 /** 841 * zpci_add_device() - Add a previously created zPCI device to the zPCI subsystem 842 * @zdev: The zPCI device to be added 843 * 844 * A struct zpci_dev is added to the zPCI subsystem and to a virtual PCI bus creating 845 * a new one as necessary. A hotplug slot is created and events start to be handled. 846 * If successful from this point on zpci_zdev_get() and zpci_zdev_put() must be used. 847 * If adding the struct zpci_dev fails the device was not added and should be freed. 848 * 849 * Return: 0 on success, or an error code otherwise 850 */ 851 int zpci_add_device(struct zpci_dev *zdev) 852 { 853 int rc; 854 855 mutex_lock(&zpci_add_remove_lock); 856 zpci_dbg(1, "add fid:%x, fh:%x, c:%d\n", zdev->fid, zdev->fh, zdev->state); 857 rc = zpci_init_iommu(zdev); 858 if (rc) 859 goto error; 860 861 rc = zpci_bus_device_register(zdev, &pci_root_ops); 862 if (rc) 863 goto error_destroy_iommu; 864 865 kref_init(&zdev->kref); 866 spin_lock(&zpci_list_lock); 867 list_add_tail(&zdev->entry, &zpci_list); 868 spin_unlock(&zpci_list_lock); 869 mutex_unlock(&zpci_add_remove_lock); 870 return 0; 871 872 error_destroy_iommu: 873 zpci_destroy_iommu(zdev); 874 error: 875 zpci_dbg(0, "add fid:%x, rc:%d\n", zdev->fid, rc); 876 mutex_unlock(&zpci_add_remove_lock); 877 return rc; 878 } 879 880 bool zpci_is_device_configured(struct zpci_dev *zdev) 881 { 882 enum zpci_state state = zdev->state; 883 884 return state != ZPCI_FN_STATE_RESERVED && 885 state != ZPCI_FN_STATE_STANDBY; 886 } 887 888 /** 889 * zpci_scan_configured_device() - Scan a freshly configured zpci_dev 890 * @zdev: The zpci_dev to be configured 891 * @fh: The general function handle supplied by the platform 892 * 893 * Given a device in the configuration state Configured, enables, scans and 894 * adds it to the common code PCI subsystem if possible. If any failure occurs, 895 * the zpci_dev is left disabled. 896 * 897 * Return: 0 on success, or an error code otherwise 898 */ 899 int zpci_scan_configured_device(struct zpci_dev *zdev, u32 fh) 900 { 901 zpci_update_fh(zdev, fh); 902 return zpci_bus_scan_device(zdev); 903 } 904 905 /** 906 * zpci_deconfigure_device() - Deconfigure a zpci_dev 907 * @zdev: The zpci_dev to configure 908 * 909 * Deconfigure a zPCI function that is currently configured and possibly known 910 * to the common code PCI subsystem. 911 * If any failure occurs the device is left as is. 912 * 913 * Return: 0 on success, or an error code otherwise 914 */ 915 int zpci_deconfigure_device(struct zpci_dev *zdev) 916 { 917 int rc; 918 919 lockdep_assert_held(&zdev->state_lock); 920 if (zdev->state != ZPCI_FN_STATE_CONFIGURED) 921 return 0; 922 923 if (zdev->zbus->bus) 924 zpci_bus_remove_device(zdev, false); 925 926 if (zdev_enabled(zdev)) { 927 rc = zpci_disable_device(zdev); 928 if (rc) 929 return rc; 930 } 931 932 rc = sclp_pci_deconfigure(zdev->fid); 933 zpci_dbg(3, "deconf fid:%x, rc:%d\n", zdev->fid, rc); 934 if (rc) 935 return rc; 936 zdev->state = ZPCI_FN_STATE_STANDBY; 937 938 return 0; 939 } 940 941 /** 942 * zpci_device_reserved() - Mark device as reserved 943 * @zdev: the zpci_dev that was reserved 944 * 945 * Handle the case that a given zPCI function was reserved by another system. 946 */ 947 void zpci_device_reserved(struct zpci_dev *zdev) 948 { 949 lockdep_assert_held(&zdev->state_lock); 950 /* We may declare the device reserved multiple times */ 951 if (zdev->state == ZPCI_FN_STATE_RESERVED) 952 return; 953 zdev->state = ZPCI_FN_STATE_RESERVED; 954 zpci_dbg(3, "rsv fid:%x\n", zdev->fid); 955 /* 956 * The underlying device is gone. Allow the zdev to be freed 957 * as soon as all other references are gone by accounting for 958 * the removal as a dropped reference. 959 */ 960 zpci_zdev_put(zdev); 961 } 962 963 void zpci_release_device(struct kref *kref) 964 __releases(&zpci_list_lock) 965 { 966 struct zpci_dev *zdev = container_of(kref, struct zpci_dev, kref); 967 968 lockdep_assert_held(&zpci_add_remove_lock); 969 WARN_ON(zdev->state != ZPCI_FN_STATE_RESERVED); 970 /* 971 * We already hold zpci_list_lock thanks to kref_put_lock(). 972 * This makes sure no new reference can be taken from the list. 973 */ 974 list_del(&zdev->entry); 975 spin_unlock(&zpci_list_lock); 976 977 if (zdev->has_hp_slot) 978 zpci_exit_slot(zdev); 979 980 if (zdev->has_resources) 981 zpci_cleanup_bus_resources(zdev); 982 983 zpci_bus_device_unregister(zdev); 984 zpci_destroy_iommu(zdev); 985 zpci_dbg(3, "rem fid:%x\n", zdev->fid); 986 kfree_rcu(zdev, rcu); 987 } 988 989 int zpci_report_error(struct pci_dev *pdev, 990 struct zpci_report_error_header *report) 991 { 992 struct zpci_dev *zdev = to_zpci(pdev); 993 994 return sclp_pci_report(report, zdev->fh, zdev->fid); 995 } 996 EXPORT_SYMBOL(zpci_report_error); 997 998 /** 999 * zpci_clear_error_state() - Clears the zPCI error state of the device 1000 * @zdev: The zdev for which the zPCI error state should be reset 1001 * 1002 * Clear the zPCI error state of the device. If clearing the zPCI error state 1003 * fails the device is left in the error state. In this case it may make sense 1004 * to call zpci_io_perm_failure() on the associated pdev if it exists. 1005 * 1006 * Returns: 0 on success, -EIO otherwise 1007 */ 1008 int zpci_clear_error_state(struct zpci_dev *zdev) 1009 { 1010 u64 req = ZPCI_CREATE_REQ(zdev->fh, 0, ZPCI_MOD_FC_RESET_ERROR); 1011 struct zpci_fib fib = {0}; 1012 u8 status; 1013 int cc; 1014 1015 cc = zpci_mod_fc(req, &fib, &status); 1016 if (cc) { 1017 zpci_dbg(3, "ces fid:%x, cc:%d, status:%x\n", zdev->fid, cc, status); 1018 return -EIO; 1019 } 1020 1021 return 0; 1022 } 1023 1024 /** 1025 * zpci_reset_load_store_blocked() - Re-enables L/S from error state 1026 * @zdev: The zdev for which to unblock load/store access 1027 * 1028 * Re-enables load/store access for a PCI function in the error state while 1029 * keeping DMA blocked. In this state drivers can poke MMIO space to determine 1030 * if error recovery is possible while catching any rogue DMA access from the 1031 * device. 1032 * 1033 * Returns: 0 on success, -EIO otherwise 1034 */ 1035 int zpci_reset_load_store_blocked(struct zpci_dev *zdev) 1036 { 1037 u64 req = ZPCI_CREATE_REQ(zdev->fh, 0, ZPCI_MOD_FC_RESET_BLOCK); 1038 struct zpci_fib fib = {0}; 1039 u8 status; 1040 int cc; 1041 1042 cc = zpci_mod_fc(req, &fib, &status); 1043 if (cc) { 1044 zpci_dbg(3, "rls fid:%x, cc:%d, status:%x\n", zdev->fid, cc, status); 1045 return -EIO; 1046 } 1047 1048 return 0; 1049 } 1050 1051 static int zpci_mem_init(void) 1052 { 1053 BUILD_BUG_ON(!is_power_of_2(__alignof__(struct zpci_fmb)) || 1054 __alignof__(struct zpci_fmb) < sizeof(struct zpci_fmb)); 1055 1056 zdev_fmb_cache = kmem_cache_create("PCI_FMB_cache", sizeof(struct zpci_fmb), 1057 __alignof__(struct zpci_fmb), 0, NULL); 1058 if (!zdev_fmb_cache) 1059 goto error_fmb; 1060 1061 zpci_iomap_start = kcalloc(ZPCI_IOMAP_ENTRIES, 1062 sizeof(*zpci_iomap_start), GFP_KERNEL); 1063 if (!zpci_iomap_start) 1064 goto error_iomap; 1065 1066 zpci_iomap_bitmap = kcalloc(BITS_TO_LONGS(ZPCI_IOMAP_ENTRIES), 1067 sizeof(*zpci_iomap_bitmap), GFP_KERNEL); 1068 if (!zpci_iomap_bitmap) 1069 goto error_iomap_bitmap; 1070 1071 if (static_branch_likely(&have_mio)) 1072 clp_setup_writeback_mio(); 1073 1074 return 0; 1075 error_iomap_bitmap: 1076 kfree(zpci_iomap_start); 1077 error_iomap: 1078 kmem_cache_destroy(zdev_fmb_cache); 1079 error_fmb: 1080 return -ENOMEM; 1081 } 1082 1083 static void zpci_mem_exit(void) 1084 { 1085 kfree(zpci_iomap_bitmap); 1086 kfree(zpci_iomap_start); 1087 kmem_cache_destroy(zdev_fmb_cache); 1088 } 1089 1090 static unsigned int s390_pci_probe __initdata = 1; 1091 unsigned int s390_pci_force_floating __initdata; 1092 static unsigned int s390_pci_initialized; 1093 1094 char * __init pcibios_setup(char *str) 1095 { 1096 if (!strcmp(str, "off")) { 1097 s390_pci_probe = 0; 1098 return NULL; 1099 } 1100 if (!strcmp(str, "nomio")) { 1101 clear_machine_feature(MFEATURE_PCI_MIO); 1102 return NULL; 1103 } 1104 if (!strcmp(str, "force_floating")) { 1105 s390_pci_force_floating = 1; 1106 return NULL; 1107 } 1108 if (!strcmp(str, "norid")) { 1109 s390_pci_no_rid = 1; 1110 return NULL; 1111 } 1112 return str; 1113 } 1114 1115 bool zpci_is_enabled(void) 1116 { 1117 return s390_pci_initialized; 1118 } 1119 1120 static int zpci_cmp_rid(void *priv, const struct list_head *a, 1121 const struct list_head *b) 1122 { 1123 struct zpci_dev *za = container_of(a, struct zpci_dev, entry); 1124 struct zpci_dev *zb = container_of(b, struct zpci_dev, entry); 1125 1126 /* 1127 * PCI functions without RID available maintain original order 1128 * between themselves but sort before those with RID. 1129 */ 1130 if (za->rid == zb->rid) 1131 return za->rid_available > zb->rid_available; 1132 /* 1133 * PCI functions with RID sort by RID ascending. 1134 */ 1135 return za->rid > zb->rid; 1136 } 1137 1138 static void zpci_add_devices(struct list_head *scan_list) 1139 { 1140 struct zpci_dev *zdev, *tmp; 1141 1142 list_sort(NULL, scan_list, &zpci_cmp_rid); 1143 list_for_each_entry_safe(zdev, tmp, scan_list, entry) { 1144 list_del_init(&zdev->entry); 1145 if (zpci_add_device(zdev)) 1146 kfree(zdev); 1147 } 1148 } 1149 1150 int zpci_scan_devices(void) 1151 { 1152 struct zpci_bus *zbus; 1153 LIST_HEAD(scan_list); 1154 int rc; 1155 1156 rc = clp_scan_pci_devices(&scan_list); 1157 if (rc) 1158 return rc; 1159 1160 zpci_add_devices(&scan_list); 1161 zpci_bus_for_each(zbus) { 1162 zpci_bus_scan_bus(zbus); 1163 cond_resched(); 1164 } 1165 return 0; 1166 } 1167 1168 static int __init pci_base_init(void) 1169 { 1170 int rc; 1171 1172 if (!s390_pci_probe) 1173 return 0; 1174 1175 if (!test_facility(69) || !test_facility(71)) { 1176 pr_info("PCI is not supported because CPU facilities 69 or 71 are not available\n"); 1177 return 0; 1178 } 1179 1180 if (test_machine_feature(MFEATURE_PCI_MIO)) { 1181 static_branch_enable(&have_mio); 1182 system_ctl_set_bit(2, CR2_MIO_ADDRESSING_BIT); 1183 } 1184 1185 rc = zpci_debug_init(); 1186 if (rc) 1187 goto out; 1188 1189 rc = zpci_mem_init(); 1190 if (rc) 1191 goto out_mem; 1192 1193 rc = zpci_irq_init(); 1194 if (rc) 1195 goto out_irq; 1196 1197 rc = zpci_scan_devices(); 1198 if (rc) 1199 goto out_find; 1200 1201 rc = zpci_fw_sysfs_init(); 1202 if (rc) 1203 goto out_find; 1204 1205 s390_pci_initialized = 1; 1206 return 0; 1207 1208 out_find: 1209 zpci_irq_exit(); 1210 out_irq: 1211 zpci_mem_exit(); 1212 out_mem: 1213 zpci_debug_exit(); 1214 out: 1215 return rc; 1216 } 1217 subsys_initcall_sync(pci_base_init); 1218