1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright IBM Corp. 2012
6 * Jan Glauber <jang@linux.vnet.ibm.com>
8 * The System z PCI code is a rewrite from a prototype by
9 * the following people (Kudoz!):
19 #define KMSG_COMPONENT "zpci"
20 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
22 #include <linux/kernel.h>
23 #include <linux/slab.h>
24 #include <linux/err.h>
25 #include <linux/export.h>
26 #include <linux/delay.h>
27 #include <linux/seq_file.h>
28 #include <linux/jump_label.h>
29 #include <linux/pci.h>
30 #include <linux/printk.h>
34 #include <asm/facility.h>
35 #include <asm/pci_insn.h>
36 #include <asm/pci_clp.h>
37 #include <asm/pci_dma.h>
42 /* list of all detected zpci devices */
43 static LIST_HEAD(zpci_list);
44 static DEFINE_SPINLOCK(zpci_list_lock);
46 static DECLARE_BITMAP(zpci_domain, ZPCI_DOMAIN_BITMAP_SIZE);
47 static DEFINE_SPINLOCK(zpci_domain_lock);
49 #define ZPCI_IOMAP_ENTRIES \
50 min(((unsigned long) ZPCI_NR_DEVICES * PCI_STD_NUM_BARS / 2), \
51 ZPCI_IOMAP_MAX_ENTRIES)
53 unsigned int s390_pci_no_rid;
55 static DEFINE_SPINLOCK(zpci_iomap_lock);
56 static unsigned long *zpci_iomap_bitmap;
57 struct zpci_iomap_entry *zpci_iomap_start;
58 EXPORT_SYMBOL_GPL(zpci_iomap_start);
60 DEFINE_STATIC_KEY_FALSE(have_mio);
62 static struct kmem_cache *zdev_fmb_cache;
64 struct zpci_dev *get_zdev_by_fid(u32 fid)
66 struct zpci_dev *tmp, *zdev = NULL;
68 spin_lock(&zpci_list_lock);
69 list_for_each_entry(tmp, &zpci_list, entry) {
70 if (tmp->fid == fid) {
76 spin_unlock(&zpci_list_lock);
80 void zpci_remove_reserved_devices(void)
82 struct zpci_dev *tmp, *zdev;
83 enum zpci_state state;
86 spin_lock(&zpci_list_lock);
87 list_for_each_entry_safe(zdev, tmp, &zpci_list, entry) {
88 if (zdev->state == ZPCI_FN_STATE_STANDBY &&
89 !clp_get_state(zdev->fid, &state) &&
90 state == ZPCI_FN_STATE_RESERVED)
91 list_move_tail(&zdev->entry, &remove);
93 spin_unlock(&zpci_list_lock);
95 list_for_each_entry_safe(zdev, tmp, &remove, entry)
96 zpci_device_reserved(zdev);
99 int pci_domain_nr(struct pci_bus *bus)
101 return ((struct zpci_bus *) bus->sysdata)->domain_nr;
103 EXPORT_SYMBOL_GPL(pci_domain_nr);
105 int pci_proc_domain(struct pci_bus *bus)
107 return pci_domain_nr(bus);
109 EXPORT_SYMBOL_GPL(pci_proc_domain);
111 /* Modify PCI: Register I/O address translation parameters */
112 int zpci_register_ioat(struct zpci_dev *zdev, u8 dmaas,
113 u64 base, u64 limit, u64 iota)
115 u64 req = ZPCI_CREATE_REQ(zdev->fh, dmaas, ZPCI_MOD_FC_REG_IOAT);
116 struct zpci_fib fib = {0};
119 WARN_ON_ONCE(iota & 0x3fff);
122 fib.iota = iota | ZPCI_IOTA_RTTO_FLAG;
123 cc = zpci_mod_fc(req, &fib, &status);
125 zpci_dbg(3, "reg ioat fid:%x, cc:%d, status:%d\n", zdev->fid, cc, status);
129 /* Modify PCI: Unregister I/O address translation parameters */
130 int zpci_unregister_ioat(struct zpci_dev *zdev, u8 dmaas)
132 u64 req = ZPCI_CREATE_REQ(zdev->fh, dmaas, ZPCI_MOD_FC_DEREG_IOAT);
133 struct zpci_fib fib = {0};
136 cc = zpci_mod_fc(req, &fib, &status);
138 zpci_dbg(3, "unreg ioat fid:%x, cc:%d, status:%d\n", zdev->fid, cc, status);
142 /* Modify PCI: Set PCI function measurement parameters */
143 int zpci_fmb_enable_device(struct zpci_dev *zdev)
145 u64 req = ZPCI_CREATE_REQ(zdev->fh, 0, ZPCI_MOD_FC_SET_MEASURE);
146 struct zpci_fib fib = {0};
149 if (zdev->fmb || sizeof(*zdev->fmb) < zdev->fmb_length)
152 zdev->fmb = kmem_cache_zalloc(zdev_fmb_cache, GFP_KERNEL);
155 WARN_ON((u64) zdev->fmb & 0xf);
157 /* reset software counters */
158 atomic64_set(&zdev->allocated_pages, 0);
159 atomic64_set(&zdev->mapped_pages, 0);
160 atomic64_set(&zdev->unmapped_pages, 0);
162 fib.fmb_addr = virt_to_phys(zdev->fmb);
163 cc = zpci_mod_fc(req, &fib, &status);
165 kmem_cache_free(zdev_fmb_cache, zdev->fmb);
168 return cc ? -EIO : 0;
171 /* Modify PCI: Disable PCI function measurement */
172 int zpci_fmb_disable_device(struct zpci_dev *zdev)
174 u64 req = ZPCI_CREATE_REQ(zdev->fh, 0, ZPCI_MOD_FC_SET_MEASURE);
175 struct zpci_fib fib = {0};
181 /* Function measurement is disabled if fmb address is zero */
182 cc = zpci_mod_fc(req, &fib, &status);
183 if (cc == 3) /* Function already gone. */
187 kmem_cache_free(zdev_fmb_cache, zdev->fmb);
190 return cc ? -EIO : 0;
193 static int zpci_cfg_load(struct zpci_dev *zdev, int offset, u32 *val, u8 len)
195 u64 req = ZPCI_CREATE_REQ(zdev->fh, ZPCI_PCIAS_CFGSPC, len);
199 rc = __zpci_load(&data, req, offset);
201 data = le64_to_cpu((__force __le64) data);
202 data >>= (8 - len) * 8;
209 static int zpci_cfg_store(struct zpci_dev *zdev, int offset, u32 val, u8 len)
211 u64 req = ZPCI_CREATE_REQ(zdev->fh, ZPCI_PCIAS_CFGSPC, len);
215 data <<= (8 - len) * 8;
216 data = (__force u64) cpu_to_le64(data);
217 rc = __zpci_store(data, req, offset);
221 resource_size_t pcibios_align_resource(void *data, const struct resource *res,
222 resource_size_t size,
223 resource_size_t align)
228 /* combine single writes by using store-block insn */
229 void __iowrite64_copy(void __iomem *to, const void *from, size_t count)
231 zpci_memcpy_toio(to, from, count);
234 static void __iomem *__ioremap(phys_addr_t addr, size_t size, pgprot_t prot)
236 unsigned long offset, vaddr;
237 struct vm_struct *area;
238 phys_addr_t last_addr;
240 last_addr = addr + size - 1;
241 if (!size || last_addr < addr)
244 if (!static_branch_unlikely(&have_mio))
245 return (void __iomem *) addr;
247 offset = addr & ~PAGE_MASK;
249 size = PAGE_ALIGN(size + offset);
250 area = get_vm_area(size, VM_IOREMAP);
254 vaddr = (unsigned long) area->addr;
255 if (ioremap_page_range(vaddr, vaddr + size, addr, prot)) {
259 return (void __iomem *) ((unsigned long) area->addr + offset);
262 void __iomem *ioremap_prot(phys_addr_t addr, size_t size, unsigned long prot)
264 return __ioremap(addr, size, __pgprot(prot));
266 EXPORT_SYMBOL(ioremap_prot);
268 void __iomem *ioremap(phys_addr_t addr, size_t size)
270 return __ioremap(addr, size, PAGE_KERNEL);
272 EXPORT_SYMBOL(ioremap);
274 void __iomem *ioremap_wc(phys_addr_t addr, size_t size)
276 return __ioremap(addr, size, pgprot_writecombine(PAGE_KERNEL));
278 EXPORT_SYMBOL(ioremap_wc);
280 void __iomem *ioremap_wt(phys_addr_t addr, size_t size)
282 return __ioremap(addr, size, pgprot_writethrough(PAGE_KERNEL));
284 EXPORT_SYMBOL(ioremap_wt);
286 void iounmap(volatile void __iomem *addr)
288 if (static_branch_likely(&have_mio))
289 vunmap((__force void *) ((unsigned long) addr & PAGE_MASK));
291 EXPORT_SYMBOL(iounmap);
293 /* Create a virtual mapping cookie for a PCI BAR */
294 static void __iomem *pci_iomap_range_fh(struct pci_dev *pdev, int bar,
295 unsigned long offset, unsigned long max)
297 struct zpci_dev *zdev = to_zpci(pdev);
300 idx = zdev->bars[bar].map_idx;
301 spin_lock(&zpci_iomap_lock);
303 WARN_ON(!++zpci_iomap_start[idx].count);
304 zpci_iomap_start[idx].fh = zdev->fh;
305 zpci_iomap_start[idx].bar = bar;
306 spin_unlock(&zpci_iomap_lock);
308 return (void __iomem *) ZPCI_ADDR(idx) + offset;
311 static void __iomem *pci_iomap_range_mio(struct pci_dev *pdev, int bar,
312 unsigned long offset,
315 unsigned long barsize = pci_resource_len(pdev, bar);
316 struct zpci_dev *zdev = to_zpci(pdev);
319 iova = ioremap((unsigned long) zdev->bars[bar].mio_wt, barsize);
320 return iova ? iova + offset : iova;
323 void __iomem *pci_iomap_range(struct pci_dev *pdev, int bar,
324 unsigned long offset, unsigned long max)
326 if (bar >= PCI_STD_NUM_BARS || !pci_resource_len(pdev, bar))
329 if (static_branch_likely(&have_mio))
330 return pci_iomap_range_mio(pdev, bar, offset, max);
332 return pci_iomap_range_fh(pdev, bar, offset, max);
334 EXPORT_SYMBOL(pci_iomap_range);
336 void __iomem *pci_iomap(struct pci_dev *dev, int bar, unsigned long maxlen)
338 return pci_iomap_range(dev, bar, 0, maxlen);
340 EXPORT_SYMBOL(pci_iomap);
342 static void __iomem *pci_iomap_wc_range_mio(struct pci_dev *pdev, int bar,
343 unsigned long offset, unsigned long max)
345 unsigned long barsize = pci_resource_len(pdev, bar);
346 struct zpci_dev *zdev = to_zpci(pdev);
349 iova = ioremap((unsigned long) zdev->bars[bar].mio_wb, barsize);
350 return iova ? iova + offset : iova;
353 void __iomem *pci_iomap_wc_range(struct pci_dev *pdev, int bar,
354 unsigned long offset, unsigned long max)
356 if (bar >= PCI_STD_NUM_BARS || !pci_resource_len(pdev, bar))
359 if (static_branch_likely(&have_mio))
360 return pci_iomap_wc_range_mio(pdev, bar, offset, max);
362 return pci_iomap_range_fh(pdev, bar, offset, max);
364 EXPORT_SYMBOL(pci_iomap_wc_range);
366 void __iomem *pci_iomap_wc(struct pci_dev *dev, int bar, unsigned long maxlen)
368 return pci_iomap_wc_range(dev, bar, 0, maxlen);
370 EXPORT_SYMBOL(pci_iomap_wc);
372 static void pci_iounmap_fh(struct pci_dev *pdev, void __iomem *addr)
374 unsigned int idx = ZPCI_IDX(addr);
376 spin_lock(&zpci_iomap_lock);
377 /* Detect underrun */
378 WARN_ON(!zpci_iomap_start[idx].count);
379 if (!--zpci_iomap_start[idx].count) {
380 zpci_iomap_start[idx].fh = 0;
381 zpci_iomap_start[idx].bar = 0;
383 spin_unlock(&zpci_iomap_lock);
386 static void pci_iounmap_mio(struct pci_dev *pdev, void __iomem *addr)
391 void pci_iounmap(struct pci_dev *pdev, void __iomem *addr)
393 if (static_branch_likely(&have_mio))
394 pci_iounmap_mio(pdev, addr);
396 pci_iounmap_fh(pdev, addr);
398 EXPORT_SYMBOL(pci_iounmap);
400 static int pci_read(struct pci_bus *bus, unsigned int devfn, int where,
403 struct zpci_dev *zdev = zdev_from_bus(bus, devfn);
405 return (zdev) ? zpci_cfg_load(zdev, where, val, size) : -ENODEV;
408 static int pci_write(struct pci_bus *bus, unsigned int devfn, int where,
411 struct zpci_dev *zdev = zdev_from_bus(bus, devfn);
413 return (zdev) ? zpci_cfg_store(zdev, where, val, size) : -ENODEV;
416 static struct pci_ops pci_root_ops = {
421 static void zpci_map_resources(struct pci_dev *pdev)
423 struct zpci_dev *zdev = to_zpci(pdev);
427 for (i = 0; i < PCI_STD_NUM_BARS; i++) {
428 len = pci_resource_len(pdev, i);
432 if (zpci_use_mio(zdev))
433 pdev->resource[i].start =
434 (resource_size_t __force) zdev->bars[i].mio_wt;
436 pdev->resource[i].start = (resource_size_t __force)
437 pci_iomap_range_fh(pdev, i, 0, 0);
438 pdev->resource[i].end = pdev->resource[i].start + len - 1;
441 zpci_iov_map_resources(pdev);
444 static void zpci_unmap_resources(struct pci_dev *pdev)
446 struct zpci_dev *zdev = to_zpci(pdev);
450 if (zpci_use_mio(zdev))
453 for (i = 0; i < PCI_STD_NUM_BARS; i++) {
454 len = pci_resource_len(pdev, i);
457 pci_iounmap_fh(pdev, (void __iomem __force *)
458 pdev->resource[i].start);
462 static int zpci_alloc_iomap(struct zpci_dev *zdev)
466 spin_lock(&zpci_iomap_lock);
467 entry = find_first_zero_bit(zpci_iomap_bitmap, ZPCI_IOMAP_ENTRIES);
468 if (entry == ZPCI_IOMAP_ENTRIES) {
469 spin_unlock(&zpci_iomap_lock);
472 set_bit(entry, zpci_iomap_bitmap);
473 spin_unlock(&zpci_iomap_lock);
477 static void zpci_free_iomap(struct zpci_dev *zdev, int entry)
479 spin_lock(&zpci_iomap_lock);
480 memset(&zpci_iomap_start[entry], 0, sizeof(struct zpci_iomap_entry));
481 clear_bit(entry, zpci_iomap_bitmap);
482 spin_unlock(&zpci_iomap_lock);
485 static void zpci_do_update_iomap_fh(struct zpci_dev *zdev, u32 fh)
489 spin_lock(&zpci_iomap_lock);
490 for (bar = 0; bar < PCI_STD_NUM_BARS; bar++) {
491 if (!zdev->bars[bar].size)
493 idx = zdev->bars[bar].map_idx;
494 if (!zpci_iomap_start[idx].count)
496 WRITE_ONCE(zpci_iomap_start[idx].fh, zdev->fh);
498 spin_unlock(&zpci_iomap_lock);
501 void zpci_update_fh(struct zpci_dev *zdev, u32 fh)
503 if (!fh || zdev->fh == fh)
507 if (zpci_use_mio(zdev))
509 if (zdev->has_resources && zdev_enabled(zdev))
510 zpci_do_update_iomap_fh(zdev, fh);
513 static struct resource *__alloc_res(struct zpci_dev *zdev, unsigned long start,
514 unsigned long size, unsigned long flags)
518 r = kzalloc(sizeof(*r), GFP_KERNEL);
523 r->end = r->start + size - 1;
525 r->name = zdev->res_name;
527 if (request_resource(&iomem_resource, r)) {
534 int zpci_setup_bus_resources(struct zpci_dev *zdev,
535 struct list_head *resources)
537 unsigned long addr, size, flags;
538 struct resource *res;
541 snprintf(zdev->res_name, sizeof(zdev->res_name),
542 "PCI Bus %04x:%02x", zdev->uid, ZPCI_BUS_NR);
544 for (i = 0; i < PCI_STD_NUM_BARS; i++) {
545 if (!zdev->bars[i].size)
547 entry = zpci_alloc_iomap(zdev);
550 zdev->bars[i].map_idx = entry;
552 /* only MMIO is supported */
553 flags = IORESOURCE_MEM;
554 if (zdev->bars[i].val & 8)
555 flags |= IORESOURCE_PREFETCH;
556 if (zdev->bars[i].val & 4)
557 flags |= IORESOURCE_MEM_64;
559 if (zpci_use_mio(zdev))
560 addr = (unsigned long) zdev->bars[i].mio_wt;
562 addr = ZPCI_ADDR(entry);
563 size = 1UL << zdev->bars[i].size;
565 res = __alloc_res(zdev, addr, size, flags);
567 zpci_free_iomap(zdev, entry);
570 zdev->bars[i].res = res;
571 pci_add_resource(resources, res);
573 zdev->has_resources = 1;
578 static void zpci_cleanup_bus_resources(struct zpci_dev *zdev)
582 for (i = 0; i < PCI_STD_NUM_BARS; i++) {
583 if (!zdev->bars[i].size || !zdev->bars[i].res)
586 zpci_free_iomap(zdev, zdev->bars[i].map_idx);
587 release_resource(zdev->bars[i].res);
588 kfree(zdev->bars[i].res);
590 zdev->has_resources = 0;
593 int pcibios_device_add(struct pci_dev *pdev)
595 struct zpci_dev *zdev = to_zpci(pdev);
596 struct resource *res;
599 /* The pdev has a reference to the zdev via its bus */
602 pdev->no_vf_scan = 1;
604 pdev->dev.groups = zpci_attr_groups;
605 pdev->dev.dma_ops = &s390_pci_dma_ops;
606 zpci_map_resources(pdev);
608 for (i = 0; i < PCI_STD_NUM_BARS; i++) {
609 res = &pdev->resource[i];
610 if (res->parent || !res->flags)
612 pci_claim_resource(pdev, i);
618 void pcibios_release_device(struct pci_dev *pdev)
620 struct zpci_dev *zdev = to_zpci(pdev);
622 zpci_unmap_resources(pdev);
626 int pcibios_enable_device(struct pci_dev *pdev, int mask)
628 struct zpci_dev *zdev = to_zpci(pdev);
630 zpci_debug_init_device(zdev, dev_name(&pdev->dev));
631 zpci_fmb_enable_device(zdev);
633 return pci_enable_resources(pdev, mask);
636 void pcibios_disable_device(struct pci_dev *pdev)
638 struct zpci_dev *zdev = to_zpci(pdev);
640 zpci_fmb_disable_device(zdev);
641 zpci_debug_exit_device(zdev);
644 static int __zpci_register_domain(int domain)
646 spin_lock(&zpci_domain_lock);
647 if (test_bit(domain, zpci_domain)) {
648 spin_unlock(&zpci_domain_lock);
649 pr_err("Domain %04x is already assigned\n", domain);
652 set_bit(domain, zpci_domain);
653 spin_unlock(&zpci_domain_lock);
657 static int __zpci_alloc_domain(void)
661 spin_lock(&zpci_domain_lock);
663 * We can always auto allocate domains below ZPCI_NR_DEVICES.
664 * There is either a free domain or we have reached the maximum in
665 * which case we would have bailed earlier.
667 domain = find_first_zero_bit(zpci_domain, ZPCI_NR_DEVICES);
668 set_bit(domain, zpci_domain);
669 spin_unlock(&zpci_domain_lock);
673 int zpci_alloc_domain(int domain)
675 if (zpci_unique_uid) {
677 return __zpci_register_domain(domain);
678 pr_warn("UID checking was active but no UID is provided: switching to automatic domain allocation\n");
679 update_uid_checking(false);
681 return __zpci_alloc_domain();
684 void zpci_free_domain(int domain)
686 spin_lock(&zpci_domain_lock);
687 clear_bit(domain, zpci_domain);
688 spin_unlock(&zpci_domain_lock);
692 int zpci_enable_device(struct zpci_dev *zdev)
697 if (clp_enable_fh(zdev, &fh, ZPCI_NR_DMA_SPACES))
700 zpci_update_fh(zdev, fh);
704 int zpci_disable_device(struct zpci_dev *zdev)
709 cc = clp_disable_fh(zdev, &fh);
711 zpci_update_fh(zdev, fh);
712 } else if (cc == CLP_RC_SETPCIFN_ALRDY) {
713 pr_info("Disabling PCI function %08x had no effect as it was already disabled\n",
715 /* Function is already disabled - update handle */
716 rc = clp_refresh_fh(zdev->fid, &fh);
718 zpci_update_fh(zdev, fh);
728 * zpci_hot_reset_device - perform a reset of the given zPCI function
729 * @zdev: the slot which should be reset
731 * Performs a low level reset of the zPCI function. The reset is low level in
732 * the sense that the zPCI function can be reset without detaching it from the
733 * common PCI subsystem. The reset may be performed while under control of
734 * either DMA or IOMMU APIs in which case the existing DMA/IOMMU translation
735 * table is reinstated at the end of the reset.
737 * After the reset the functions internal state is reset to an initial state
738 * equivalent to its state during boot when first probing a driver.
739 * Consequently after reset the PCI function requires re-initialization via the
740 * common PCI code including re-enabling IRQs via pci_alloc_irq_vectors()
741 * and enabling the function via e.g.pci_enablde_device_flags().The caller
742 * must guard against concurrent reset attempts.
744 * In most cases this function should not be called directly but through
745 * pci_reset_function() or pci_reset_bus() which handle the save/restore and
748 * Return: 0 on success and an error value otherwise
750 int zpci_hot_reset_device(struct zpci_dev *zdev)
754 zpci_dbg(3, "rst fid:%x, fh:%x\n", zdev->fid, zdev->fh);
755 if (zdev_enabled(zdev)) {
756 /* Disables device access, DMAs and IRQs (reset state) */
757 rc = zpci_disable_device(zdev);
759 * Due to a z/VM vs LPAR inconsistency in the error state the
760 * FH may indicate an enabled device but disable says the
761 * device is already disabled don't treat it as an error here.
769 rc = zpci_enable_device(zdev);
774 rc = zpci_register_ioat(zdev, 0, zdev->start_dma, zdev->end_dma,
775 virt_to_phys(zdev->dma_table));
777 rc = zpci_dma_init_device(zdev);
779 zpci_disable_device(zdev);
787 * zpci_create_device() - Create a new zpci_dev and add it to the zbus
788 * @fid: Function ID of the device to be created
789 * @fh: Current Function Handle of the device to be created
790 * @state: Initial state after creation either Standby or Configured
792 * Creates a new zpci device and adds it to its, possibly newly created, zbus
793 * as well as zpci_list.
795 * Returns: the zdev on success or an error pointer otherwise
797 struct zpci_dev *zpci_create_device(u32 fid, u32 fh, enum zpci_state state)
799 struct zpci_dev *zdev;
802 zpci_dbg(1, "add fid:%x, fh:%x, c:%d\n", fid, fh, state);
803 zdev = kzalloc(sizeof(*zdev), GFP_KERNEL);
805 return ERR_PTR(-ENOMEM);
807 /* FID and Function Handle are the static/dynamic identifiers */
811 /* Query function properties and update zdev */
812 rc = clp_query_pci_fn(zdev);
817 kref_init(&zdev->kref);
818 mutex_init(&zdev->lock);
820 rc = zpci_init_iommu(zdev);
824 rc = zpci_bus_device_register(zdev, &pci_root_ops);
826 goto error_destroy_iommu;
828 spin_lock(&zpci_list_lock);
829 list_add_tail(&zdev->entry, &zpci_list);
830 spin_unlock(&zpci_list_lock);
835 zpci_destroy_iommu(zdev);
837 zpci_dbg(0, "add fid:%x, rc:%d\n", fid, rc);
842 bool zpci_is_device_configured(struct zpci_dev *zdev)
844 enum zpci_state state = zdev->state;
846 return state != ZPCI_FN_STATE_RESERVED &&
847 state != ZPCI_FN_STATE_STANDBY;
851 * zpci_scan_configured_device() - Scan a freshly configured zpci_dev
852 * @zdev: The zpci_dev to be configured
853 * @fh: The general function handle supplied by the platform
855 * Given a device in the configuration state Configured, enables, scans and
856 * adds it to the common code PCI subsystem if possible. If the PCI device is
857 * parked because we can not yet create a PCI bus because we have not seen
858 * function 0, it is ignored but will be scanned once function 0 appears.
859 * If any failure occurs, the zpci_dev is left disabled.
861 * Return: 0 on success, or an error code otherwise
863 int zpci_scan_configured_device(struct zpci_dev *zdev, u32 fh)
867 zpci_update_fh(zdev, fh);
868 /* the PCI function will be scanned once function 0 appears */
869 if (!zdev->zbus->bus)
872 /* For function 0 on a multi-function bus scan whole bus as we might
873 * have to pick up existing functions waiting for it to allow creating
876 if (zdev->devfn == 0 && zdev->zbus->multifunction)
877 rc = zpci_bus_scan_bus(zdev->zbus);
879 rc = zpci_bus_scan_device(zdev);
885 * zpci_deconfigure_device() - Deconfigure a zpci_dev
886 * @zdev: The zpci_dev to configure
888 * Deconfigure a zPCI function that is currently configured and possibly known
889 * to the common code PCI subsystem.
890 * If any failure occurs the device is left as is.
892 * Return: 0 on success, or an error code otherwise
894 int zpci_deconfigure_device(struct zpci_dev *zdev)
899 zpci_bus_remove_device(zdev, false);
901 if (zdev->dma_table) {
902 rc = zpci_dma_exit_device(zdev);
906 if (zdev_enabled(zdev)) {
907 rc = zpci_disable_device(zdev);
912 rc = sclp_pci_deconfigure(zdev->fid);
913 zpci_dbg(3, "deconf fid:%x, rc:%d\n", zdev->fid, rc);
916 zdev->state = ZPCI_FN_STATE_STANDBY;
922 * zpci_device_reserved() - Mark device as resverved
923 * @zdev: the zpci_dev that was reserved
925 * Handle the case that a given zPCI function was reserved by another system.
926 * After a call to this function the zpci_dev can not be found via
927 * get_zdev_by_fid() anymore but may still be accessible via existing
928 * references though it will not be functional anymore.
930 void zpci_device_reserved(struct zpci_dev *zdev)
932 if (zdev->has_hp_slot)
933 zpci_exit_slot(zdev);
935 * Remove device from zpci_list as it is going away. This also
936 * makes sure we ignore subsequent zPCI events for this device.
938 spin_lock(&zpci_list_lock);
939 list_del(&zdev->entry);
940 spin_unlock(&zpci_list_lock);
941 zdev->state = ZPCI_FN_STATE_RESERVED;
942 zpci_dbg(3, "rsv fid:%x\n", zdev->fid);
946 void zpci_release_device(struct kref *kref)
948 struct zpci_dev *zdev = container_of(kref, struct zpci_dev, kref);
952 zpci_bus_remove_device(zdev, false);
955 zpci_dma_exit_device(zdev);
956 if (zdev_enabled(zdev))
957 zpci_disable_device(zdev);
959 switch (zdev->state) {
960 case ZPCI_FN_STATE_CONFIGURED:
961 ret = sclp_pci_deconfigure(zdev->fid);
962 zpci_dbg(3, "deconf fid:%x, rc:%d\n", zdev->fid, ret);
964 case ZPCI_FN_STATE_STANDBY:
965 if (zdev->has_hp_slot)
966 zpci_exit_slot(zdev);
967 spin_lock(&zpci_list_lock);
968 list_del(&zdev->entry);
969 spin_unlock(&zpci_list_lock);
970 zpci_dbg(3, "rsv fid:%x\n", zdev->fid);
972 case ZPCI_FN_STATE_RESERVED:
973 if (zdev->has_resources)
974 zpci_cleanup_bus_resources(zdev);
975 zpci_bus_device_unregister(zdev);
976 zpci_destroy_iommu(zdev);
981 zpci_dbg(3, "rem fid:%x\n", zdev->fid);
985 int zpci_report_error(struct pci_dev *pdev,
986 struct zpci_report_error_header *report)
988 struct zpci_dev *zdev = to_zpci(pdev);
990 return sclp_pci_report(report, zdev->fh, zdev->fid);
992 EXPORT_SYMBOL(zpci_report_error);
995 * zpci_clear_error_state() - Clears the zPCI error state of the device
996 * @zdev: The zdev for which the zPCI error state should be reset
998 * Clear the zPCI error state of the device. If clearing the zPCI error state
999 * fails the device is left in the error state. In this case it may make sense
1000 * to call zpci_io_perm_failure() on the associated pdev if it exists.
1002 * Returns: 0 on success, -EIO otherwise
1004 int zpci_clear_error_state(struct zpci_dev *zdev)
1006 u64 req = ZPCI_CREATE_REQ(zdev->fh, 0, ZPCI_MOD_FC_RESET_ERROR);
1007 struct zpci_fib fib = {0};
1011 cc = zpci_mod_fc(req, &fib, &status);
1013 zpci_dbg(3, "ces fid:%x, cc:%d, status:%x\n", zdev->fid, cc, status);
1021 * zpci_reset_load_store_blocked() - Re-enables L/S from error state
1022 * @zdev: The zdev for which to unblock load/store access
1024 * Re-enables load/store access for a PCI function in the error state while
1025 * keeping DMA blocked. In this state drivers can poke MMIO space to determine
1026 * if error recovery is possible while catching any rogue DMA access from the
1029 * Returns: 0 on success, -EIO otherwise
1031 int zpci_reset_load_store_blocked(struct zpci_dev *zdev)
1033 u64 req = ZPCI_CREATE_REQ(zdev->fh, 0, ZPCI_MOD_FC_RESET_BLOCK);
1034 struct zpci_fib fib = {0};
1038 cc = zpci_mod_fc(req, &fib, &status);
1040 zpci_dbg(3, "rls fid:%x, cc:%d, status:%x\n", zdev->fid, cc, status);
1047 static int zpci_mem_init(void)
1049 BUILD_BUG_ON(!is_power_of_2(__alignof__(struct zpci_fmb)) ||
1050 __alignof__(struct zpci_fmb) < sizeof(struct zpci_fmb));
1052 zdev_fmb_cache = kmem_cache_create("PCI_FMB_cache", sizeof(struct zpci_fmb),
1053 __alignof__(struct zpci_fmb), 0, NULL);
1054 if (!zdev_fmb_cache)
1057 zpci_iomap_start = kcalloc(ZPCI_IOMAP_ENTRIES,
1058 sizeof(*zpci_iomap_start), GFP_KERNEL);
1059 if (!zpci_iomap_start)
1062 zpci_iomap_bitmap = kcalloc(BITS_TO_LONGS(ZPCI_IOMAP_ENTRIES),
1063 sizeof(*zpci_iomap_bitmap), GFP_KERNEL);
1064 if (!zpci_iomap_bitmap)
1065 goto error_iomap_bitmap;
1067 if (static_branch_likely(&have_mio))
1068 clp_setup_writeback_mio();
1072 kfree(zpci_iomap_start);
1074 kmem_cache_destroy(zdev_fmb_cache);
1079 static void zpci_mem_exit(void)
1081 kfree(zpci_iomap_bitmap);
1082 kfree(zpci_iomap_start);
1083 kmem_cache_destroy(zdev_fmb_cache);
1086 static unsigned int s390_pci_probe __initdata = 1;
1087 unsigned int s390_pci_force_floating __initdata;
1088 static unsigned int s390_pci_initialized;
1090 char * __init pcibios_setup(char *str)
1092 if (!strcmp(str, "off")) {
1096 if (!strcmp(str, "nomio")) {
1097 S390_lowcore.machine_flags &= ~MACHINE_FLAG_PCI_MIO;
1100 if (!strcmp(str, "force_floating")) {
1101 s390_pci_force_floating = 1;
1104 if (!strcmp(str, "norid")) {
1105 s390_pci_no_rid = 1;
1111 bool zpci_is_enabled(void)
1113 return s390_pci_initialized;
1116 static int __init pci_base_init(void)
1120 if (!s390_pci_probe)
1123 if (!test_facility(69) || !test_facility(71)) {
1124 pr_info("PCI is not supported because CPU facilities 69 or 71 are not available\n");
1128 if (MACHINE_HAS_PCI_MIO) {
1129 static_branch_enable(&have_mio);
1133 rc = zpci_debug_init();
1137 rc = zpci_mem_init();
1141 rc = zpci_irq_init();
1145 rc = zpci_dma_init();
1149 rc = clp_scan_pci_devices();
1152 zpci_bus_scan_busses();
1154 s390_pci_initialized = 1;
1168 subsys_initcall_sync(pci_base_init);