xref: /linux/arch/s390/pci/pci.c (revision c68468ed3416ea88d7b14dabb1fa584c3a90cd85)
1adbb3901SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2cd248341SJan Glauber /*
3cd248341SJan Glauber  * Copyright IBM Corp. 2012
4cd248341SJan Glauber  *
5cd248341SJan Glauber  * Author(s):
6cd248341SJan Glauber  *   Jan Glauber <jang@linux.vnet.ibm.com>
7cd248341SJan Glauber  *
8cd248341SJan Glauber  * The System z PCI code is a rewrite from a prototype by
9cd248341SJan Glauber  * the following people (Kudoz!):
10bedef755SJan Glauber  *   Alexander Schmidt
11bedef755SJan Glauber  *   Christoph Raisch
12bedef755SJan Glauber  *   Hannes Hering
13bedef755SJan Glauber  *   Hoang-Nam Nguyen
14bedef755SJan Glauber  *   Jan-Bernd Themann
15bedef755SJan Glauber  *   Stefan Roscher
16bedef755SJan Glauber  *   Thomas Klein
17cd248341SJan Glauber  */
18cd248341SJan Glauber 
19896cb7e6SGerald Schaefer #define KMSG_COMPONENT "zpci"
20896cb7e6SGerald Schaefer #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
21cd248341SJan Glauber 
22cd248341SJan Glauber #include <linux/kernel.h>
23cd248341SJan Glauber #include <linux/slab.h>
24cd248341SJan Glauber #include <linux/err.h>
25cd248341SJan Glauber #include <linux/export.h>
26cd248341SJan Glauber #include <linux/delay.h>
27cd248341SJan Glauber #include <linux/seq_file.h>
2871ba41c9SSebastian Ott #include <linux/jump_label.h>
29cd248341SJan Glauber #include <linux/pci.h>
30794b8846SNiklas Schnelle #include <linux/printk.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 
3905bc1be6SPierre Morel #include "pci_bus.h"
40abb95b75SNiklas Schnelle #include "pci_iov.h"
4105bc1be6SPierre Morel 
42cd248341SJan Glauber /* list of all detected zpci devices */
4367f43f38SSebastian Ott static LIST_HEAD(zpci_list);
4457b5918cSSebastian Ott static DEFINE_SPINLOCK(zpci_list_lock);
451f44a225SMartin Schwidefsky 
46969ae01bSNiklas Schnelle static DECLARE_BITMAP(zpci_domain, ZPCI_DOMAIN_BITMAP_SIZE);
47cd248341SJan Glauber static DEFINE_SPINLOCK(zpci_domain_lock);
48cd248341SJan Glauber 
49c506fff3SSebastian Ott #define ZPCI_IOMAP_ENTRIES						\
50c9c13ba4SDenis Efremov 	min(((unsigned long) ZPCI_NR_DEVICES * PCI_STD_NUM_BARS / 2),	\
51c506fff3SSebastian Ott 	    ZPCI_IOMAP_MAX_ENTRIES)
52c506fff3SSebastian Ott 
536cf17f9aSPierre Morel unsigned int s390_pci_no_rid;
546cf17f9aSPierre Morel 
55cd248341SJan Glauber static DEFINE_SPINLOCK(zpci_iomap_lock);
56c506fff3SSebastian Ott static unsigned long *zpci_iomap_bitmap;
57cd248341SJan Glauber struct zpci_iomap_entry *zpci_iomap_start;
58cd248341SJan Glauber EXPORT_SYMBOL_GPL(zpci_iomap_start);
59cd248341SJan Glauber 
6071ba41c9SSebastian Ott DEFINE_STATIC_KEY_FALSE(have_mio);
6171ba41c9SSebastian Ott 
62d0b08853SJan Glauber static struct kmem_cache *zdev_fmb_cache;
63d0b08853SJan Glauber 
64cd248341SJan Glauber struct zpci_dev *get_zdev_by_fid(u32 fid)
65cd248341SJan Glauber {
66cd248341SJan Glauber 	struct zpci_dev *tmp, *zdev = NULL;
67cd248341SJan Glauber 
6857b5918cSSebastian Ott 	spin_lock(&zpci_list_lock);
69cd248341SJan Glauber 	list_for_each_entry(tmp, &zpci_list, entry) {
70cd248341SJan Glauber 		if (tmp->fid == fid) {
71cd248341SJan Glauber 			zdev = tmp;
72c122383dSNiklas Schnelle 			zpci_zdev_get(zdev);
73cd248341SJan Glauber 			break;
74cd248341SJan Glauber 		}
75cd248341SJan Glauber 	}
7657b5918cSSebastian Ott 	spin_unlock(&zpci_list_lock);
77cd248341SJan Glauber 	return zdev;
78cd248341SJan Glauber }
79cd248341SJan Glauber 
8001553d9aSSebastian Ott void zpci_remove_reserved_devices(void)
8101553d9aSSebastian Ott {
8201553d9aSSebastian Ott 	struct zpci_dev *tmp, *zdev;
8301553d9aSSebastian Ott 	enum zpci_state state;
8401553d9aSSebastian Ott 	LIST_HEAD(remove);
8501553d9aSSebastian Ott 
8601553d9aSSebastian Ott 	spin_lock(&zpci_list_lock);
8701553d9aSSebastian Ott 	list_for_each_entry_safe(zdev, tmp, &zpci_list, entry) {
8801553d9aSSebastian Ott 		if (zdev->state == ZPCI_FN_STATE_STANDBY &&
8901553d9aSSebastian Ott 		    !clp_get_state(zdev->fid, &state) &&
9001553d9aSSebastian Ott 		    state == ZPCI_FN_STATE_RESERVED)
9101553d9aSSebastian Ott 			list_move_tail(&zdev->entry, &remove);
9201553d9aSSebastian Ott 	}
9301553d9aSSebastian Ott 	spin_unlock(&zpci_list_lock);
9401553d9aSSebastian Ott 
9501553d9aSSebastian Ott 	list_for_each_entry_safe(zdev, tmp, &remove, entry)
96a46044a9SNiklas Schnelle 		zpci_device_reserved(zdev);
97cd248341SJan Glauber }
98cd248341SJan Glauber 
99cd248341SJan Glauber int pci_domain_nr(struct pci_bus *bus)
100cd248341SJan Glauber {
10105bc1be6SPierre Morel 	return ((struct zpci_bus *) bus->sysdata)->domain_nr;
102cd248341SJan Glauber }
103cd248341SJan Glauber EXPORT_SYMBOL_GPL(pci_domain_nr);
104cd248341SJan Glauber 
105cd248341SJan Glauber int pci_proc_domain(struct pci_bus *bus)
106cd248341SJan Glauber {
107cd248341SJan Glauber 	return pci_domain_nr(bus);
108cd248341SJan Glauber }
109cd248341SJan Glauber EXPORT_SYMBOL_GPL(pci_proc_domain);
110cd248341SJan Glauber 
111828b35f6SJan Glauber /* Modify PCI: Register I/O address translation parameters */
112828b35f6SJan Glauber int zpci_register_ioat(struct zpci_dev *zdev, u8 dmaas,
113828b35f6SJan Glauber 		       u64 base, u64 limit, u64 iota)
114828b35f6SJan Glauber {
11572570834SSebastian Ott 	u64 req = ZPCI_CREATE_REQ(zdev->fh, dmaas, ZPCI_MOD_FC_REG_IOAT);
11672570834SSebastian Ott 	struct zpci_fib fib = {0};
1171f3f7681SNiklas Schnelle 	u8 cc, status;
118828b35f6SJan Glauber 
119828b35f6SJan Glauber 	WARN_ON_ONCE(iota & 0x3fff);
12072570834SSebastian Ott 	fib.pba = base;
12172570834SSebastian Ott 	fib.pal = limit;
12272570834SSebastian Ott 	fib.iota = iota | ZPCI_IOTA_RTTO_FLAG;
123*c68468edSMatthew Rosato 	fib.gd = zdev->gisa;
1241f3f7681SNiklas Schnelle 	cc = zpci_mod_fc(req, &fib, &status);
1251f3f7681SNiklas Schnelle 	if (cc)
1261f3f7681SNiklas Schnelle 		zpci_dbg(3, "reg ioat fid:%x, cc:%d, status:%d\n", zdev->fid, cc, status);
1271f3f7681SNiklas Schnelle 	return cc;
128828b35f6SJan Glauber }
129828b35f6SJan Glauber 
130828b35f6SJan Glauber /* Modify PCI: Unregister I/O address translation parameters */
131828b35f6SJan Glauber int zpci_unregister_ioat(struct zpci_dev *zdev, u8 dmaas)
132828b35f6SJan Glauber {
13372570834SSebastian Ott 	u64 req = ZPCI_CREATE_REQ(zdev->fh, dmaas, ZPCI_MOD_FC_DEREG_IOAT);
13472570834SSebastian Ott 	struct zpci_fib fib = {0};
13572570834SSebastian Ott 	u8 cc, status;
136828b35f6SJan Glauber 
137*c68468edSMatthew Rosato 	fib.gd = zdev->gisa;
138*c68468edSMatthew Rosato 
13972570834SSebastian Ott 	cc = zpci_mod_fc(req, &fib, &status);
1401f3f7681SNiklas Schnelle 	if (cc)
1411f3f7681SNiklas Schnelle 		zpci_dbg(3, "unreg ioat fid:%x, cc:%d, status:%d\n", zdev->fid, cc, status);
1421f3f7681SNiklas Schnelle 	return cc;
143828b35f6SJan Glauber }
144828b35f6SJan Glauber 
145d0b08853SJan Glauber /* Modify PCI: Set PCI function measurement parameters */
146d0b08853SJan Glauber int zpci_fmb_enable_device(struct zpci_dev *zdev)
147d0b08853SJan Glauber {
1484e5bd780SSebastian Ott 	u64 req = ZPCI_CREATE_REQ(zdev->fh, 0, ZPCI_MOD_FC_SET_MEASURE);
1494e5bd780SSebastian Ott 	struct zpci_fib fib = {0};
1504e5bd780SSebastian Ott 	u8 cc, status;
151d0b08853SJan Glauber 
1520b7589ecSSebastian Ott 	if (zdev->fmb || sizeof(*zdev->fmb) < zdev->fmb_length)
153d0b08853SJan Glauber 		return -EINVAL;
154d0b08853SJan Glauber 
15508b42124SWei Yongjun 	zdev->fmb = kmem_cache_zalloc(zdev_fmb_cache, GFP_KERNEL);
156d0b08853SJan Glauber 	if (!zdev->fmb)
157d0b08853SJan Glauber 		return -ENOMEM;
158d0b08853SJan Glauber 	WARN_ON((u64) zdev->fmb & 0xf);
159d0b08853SJan Glauber 
1606001018aSSebastian Ott 	/* reset software counters */
1616001018aSSebastian Ott 	atomic64_set(&zdev->allocated_pages, 0);
1626001018aSSebastian Ott 	atomic64_set(&zdev->mapped_pages, 0);
1636001018aSSebastian Ott 	atomic64_set(&zdev->unmapped_pages, 0);
1646001018aSSebastian Ott 
1654e5bd780SSebastian Ott 	fib.fmb_addr = virt_to_phys(zdev->fmb);
166*c68468edSMatthew Rosato 	fib.gd = zdev->gisa;
1674e5bd780SSebastian Ott 	cc = zpci_mod_fc(req, &fib, &status);
1684e5bd780SSebastian Ott 	if (cc) {
1694e5bd780SSebastian Ott 		kmem_cache_free(zdev_fmb_cache, zdev->fmb);
1704e5bd780SSebastian Ott 		zdev->fmb = NULL;
1714e5bd780SSebastian Ott 	}
1724e5bd780SSebastian Ott 	return cc ? -EIO : 0;
173d0b08853SJan Glauber }
174d0b08853SJan Glauber 
175d0b08853SJan Glauber /* Modify PCI: Disable PCI function measurement */
176d0b08853SJan Glauber int zpci_fmb_disable_device(struct zpci_dev *zdev)
177d0b08853SJan Glauber {
1784e5bd780SSebastian Ott 	u64 req = ZPCI_CREATE_REQ(zdev->fh, 0, ZPCI_MOD_FC_SET_MEASURE);
1794e5bd780SSebastian Ott 	struct zpci_fib fib = {0};
1804e5bd780SSebastian Ott 	u8 cc, status;
181d0b08853SJan Glauber 
182d0b08853SJan Glauber 	if (!zdev->fmb)
183d0b08853SJan Glauber 		return -EINVAL;
184d0b08853SJan Glauber 
185*c68468edSMatthew Rosato 	fib.gd = zdev->gisa;
186*c68468edSMatthew Rosato 
187d0b08853SJan Glauber 	/* Function measurement is disabled if fmb address is zero */
1884e5bd780SSebastian Ott 	cc = zpci_mod_fc(req, &fib, &status);
1894e5bd780SSebastian Ott 	if (cc == 3) /* Function already gone. */
1904e5bd780SSebastian Ott 		cc = 0;
191d0b08853SJan Glauber 
1924e5bd780SSebastian Ott 	if (!cc) {
193d0b08853SJan Glauber 		kmem_cache_free(zdev_fmb_cache, zdev->fmb);
194d0b08853SJan Glauber 		zdev->fmb = NULL;
1954e5bd780SSebastian Ott 	}
1964e5bd780SSebastian Ott 	return cc ? -EIO : 0;
197d0b08853SJan Glauber }
198d0b08853SJan Glauber 
199cd248341SJan Glauber static int zpci_cfg_load(struct zpci_dev *zdev, int offset, u32 *val, u8 len)
200cd248341SJan Glauber {
201cd248341SJan Glauber 	u64 req = ZPCI_CREATE_REQ(zdev->fh, ZPCI_PCIAS_CFGSPC, len);
202cd248341SJan Glauber 	u64 data;
203cd248341SJan Glauber 	int rc;
204cd248341SJan Glauber 
20581deca12SSebastian Ott 	rc = __zpci_load(&data, req, offset);
206b170bad4SSebastian Ott 	if (!rc) {
2075064cd35SSebastian Ott 		data = le64_to_cpu((__force __le64) data);
2085064cd35SSebastian Ott 		data >>= (8 - len) * 8;
209cd248341SJan Glauber 		*val = (u32) data;
210b170bad4SSebastian Ott 	} else
211cd248341SJan Glauber 		*val = 0xffffffff;
212cd248341SJan Glauber 	return rc;
213cd248341SJan Glauber }
214cd248341SJan Glauber 
215cd248341SJan Glauber static int zpci_cfg_store(struct zpci_dev *zdev, int offset, u32 val, u8 len)
216cd248341SJan Glauber {
217cd248341SJan Glauber 	u64 req = ZPCI_CREATE_REQ(zdev->fh, ZPCI_PCIAS_CFGSPC, len);
218cd248341SJan Glauber 	u64 data = val;
219cd248341SJan Glauber 	int rc;
220cd248341SJan Glauber 
2215064cd35SSebastian Ott 	data <<= (8 - len) * 8;
2225064cd35SSebastian Ott 	data = (__force u64) cpu_to_le64(data);
22381deca12SSebastian Ott 	rc = __zpci_store(data, req, offset);
224cd248341SJan Glauber 	return rc;
225cd248341SJan Glauber }
226cd248341SJan Glauber 
227cd248341SJan Glauber resource_size_t pcibios_align_resource(void *data, const struct resource *res,
228cd248341SJan Glauber 				       resource_size_t size,
229cd248341SJan Glauber 				       resource_size_t align)
230cd248341SJan Glauber {
231cd248341SJan Glauber 	return 0;
232cd248341SJan Glauber }
233cd248341SJan Glauber 
23487bc359bSJan Glauber /* combine single writes by using store-block insn */
23587bc359bSJan Glauber void __iowrite64_copy(void __iomem *to, const void *from, size_t count)
23687bc359bSJan Glauber {
23787bc359bSJan Glauber        zpci_memcpy_toio(to, from, count);
23887bc359bSJan Glauber }
23987bc359bSJan Glauber 
240b02002ccSNiklas Schnelle static void __iomem *__ioremap(phys_addr_t addr, size_t size, pgprot_t prot)
24171ba41c9SSebastian Ott {
242a999eb96SNiklas Schnelle 	unsigned long offset, vaddr;
24371ba41c9SSebastian Ott 	struct vm_struct *area;
244a999eb96SNiklas Schnelle 	phys_addr_t last_addr;
24571ba41c9SSebastian Ott 
246a999eb96SNiklas Schnelle 	last_addr = addr + size - 1;
247a999eb96SNiklas Schnelle 	if (!size || last_addr < addr)
24871ba41c9SSebastian Ott 		return NULL;
24971ba41c9SSebastian Ott 
25071ba41c9SSebastian Ott 	if (!static_branch_unlikely(&have_mio))
251a999eb96SNiklas Schnelle 		return (void __iomem *) addr;
25271ba41c9SSebastian Ott 
253a999eb96SNiklas Schnelle 	offset = addr & ~PAGE_MASK;
254a999eb96SNiklas Schnelle 	addr &= PAGE_MASK;
25571ba41c9SSebastian Ott 	size = PAGE_ALIGN(size + offset);
25671ba41c9SSebastian Ott 	area = get_vm_area(size, VM_IOREMAP);
25771ba41c9SSebastian Ott 	if (!area)
25871ba41c9SSebastian Ott 		return NULL;
25971ba41c9SSebastian Ott 
260a999eb96SNiklas Schnelle 	vaddr = (unsigned long) area->addr;
261b02002ccSNiklas Schnelle 	if (ioremap_page_range(vaddr, vaddr + size, addr, prot)) {
262a999eb96SNiklas Schnelle 		free_vm_area(area);
26371ba41c9SSebastian Ott 		return NULL;
26471ba41c9SSebastian Ott 	}
26571ba41c9SSebastian Ott 	return (void __iomem *) ((unsigned long) area->addr + offset);
26671ba41c9SSebastian Ott }
267b02002ccSNiklas Schnelle 
268b02002ccSNiklas Schnelle void __iomem *ioremap_prot(phys_addr_t addr, size_t size, unsigned long prot)
269b02002ccSNiklas Schnelle {
270b02002ccSNiklas Schnelle 	return __ioremap(addr, size, __pgprot(prot));
271b02002ccSNiklas Schnelle }
272b02002ccSNiklas Schnelle EXPORT_SYMBOL(ioremap_prot);
273b02002ccSNiklas Schnelle 
274b02002ccSNiklas Schnelle void __iomem *ioremap(phys_addr_t addr, size_t size)
275b02002ccSNiklas Schnelle {
276b02002ccSNiklas Schnelle 	return __ioremap(addr, size, PAGE_KERNEL);
277b02002ccSNiklas Schnelle }
27871ba41c9SSebastian Ott EXPORT_SYMBOL(ioremap);
27971ba41c9SSebastian Ott 
280b02002ccSNiklas Schnelle void __iomem *ioremap_wc(phys_addr_t addr, size_t size)
281b02002ccSNiklas Schnelle {
282b02002ccSNiklas Schnelle 	return __ioremap(addr, size, pgprot_writecombine(PAGE_KERNEL));
283b02002ccSNiklas Schnelle }
284b02002ccSNiklas Schnelle EXPORT_SYMBOL(ioremap_wc);
285b02002ccSNiklas Schnelle 
286b02002ccSNiklas Schnelle void __iomem *ioremap_wt(phys_addr_t addr, size_t size)
287b02002ccSNiklas Schnelle {
288b02002ccSNiklas Schnelle 	return __ioremap(addr, size, pgprot_writethrough(PAGE_KERNEL));
289b02002ccSNiklas Schnelle }
290b02002ccSNiklas Schnelle EXPORT_SYMBOL(ioremap_wt);
291b02002ccSNiklas Schnelle 
29271ba41c9SSebastian Ott void iounmap(volatile void __iomem *addr)
29371ba41c9SSebastian Ott {
29471ba41c9SSebastian Ott 	if (static_branch_likely(&have_mio))
29571ba41c9SSebastian Ott 		vunmap((__force void *) ((unsigned long) addr & PAGE_MASK));
29671ba41c9SSebastian Ott }
29771ba41c9SSebastian Ott EXPORT_SYMBOL(iounmap);
29871ba41c9SSebastian Ott 
299cd248341SJan Glauber /* Create a virtual mapping cookie for a PCI BAR */
30071ba41c9SSebastian Ott static void __iomem *pci_iomap_range_fh(struct pci_dev *pdev, int bar,
30171ba41c9SSebastian Ott 					unsigned long offset, unsigned long max)
302cd248341SJan Glauber {
303198a5278SSebastian Ott 	struct zpci_dev *zdev =	to_zpci(pdev);
304cd248341SJan Glauber 	int idx;
305cd248341SJan Glauber 
306cd248341SJan Glauber 	idx = zdev->bars[bar].map_idx;
307cd248341SJan Glauber 	spin_lock(&zpci_iomap_lock);
308f5e44f82SSebastian Ott 	/* Detect overrun */
309f5e44f82SSebastian Ott 	WARN_ON(!++zpci_iomap_start[idx].count);
310cd248341SJan Glauber 	zpci_iomap_start[idx].fh = zdev->fh;
311cd248341SJan Glauber 	zpci_iomap_start[idx].bar = bar;
312cd248341SJan Glauber 	spin_unlock(&zpci_iomap_lock);
313cd248341SJan Glauber 
3149e00caaeSSebastian Ott 	return (void __iomem *) ZPCI_ADDR(idx) + offset;
315cd248341SJan Glauber }
31671ba41c9SSebastian Ott 
31771ba41c9SSebastian Ott static void __iomem *pci_iomap_range_mio(struct pci_dev *pdev, int bar,
31871ba41c9SSebastian Ott 					 unsigned long offset,
31971ba41c9SSebastian Ott 					 unsigned long max)
32071ba41c9SSebastian Ott {
32171ba41c9SSebastian Ott 	unsigned long barsize = pci_resource_len(pdev, bar);
32271ba41c9SSebastian Ott 	struct zpci_dev *zdev = to_zpci(pdev);
32371ba41c9SSebastian Ott 	void __iomem *iova;
32471ba41c9SSebastian Ott 
32571ba41c9SSebastian Ott 	iova = ioremap((unsigned long) zdev->bars[bar].mio_wt, barsize);
32671ba41c9SSebastian Ott 	return iova ? iova + offset : iova;
32771ba41c9SSebastian Ott }
32871ba41c9SSebastian Ott 
32971ba41c9SSebastian Ott void __iomem *pci_iomap_range(struct pci_dev *pdev, int bar,
33071ba41c9SSebastian Ott 			      unsigned long offset, unsigned long max)
33171ba41c9SSebastian Ott {
332c9c13ba4SDenis Efremov 	if (bar >= PCI_STD_NUM_BARS || !pci_resource_len(pdev, bar))
33371ba41c9SSebastian Ott 		return NULL;
33471ba41c9SSebastian Ott 
33571ba41c9SSebastian Ott 	if (static_branch_likely(&have_mio))
33671ba41c9SSebastian Ott 		return pci_iomap_range_mio(pdev, bar, offset, max);
33771ba41c9SSebastian Ott 	else
33871ba41c9SSebastian Ott 		return pci_iomap_range_fh(pdev, bar, offset, max);
33971ba41c9SSebastian Ott }
340d9426083SSebastian Ott EXPORT_SYMBOL(pci_iomap_range);
3418cfc99b5SMichael S. Tsirkin 
3428cfc99b5SMichael S. Tsirkin void __iomem *pci_iomap(struct pci_dev *dev, int bar, unsigned long maxlen)
3438cfc99b5SMichael S. Tsirkin {
3448cfc99b5SMichael S. Tsirkin 	return pci_iomap_range(dev, bar, 0, maxlen);
3458cfc99b5SMichael S. Tsirkin }
3468cfc99b5SMichael S. Tsirkin EXPORT_SYMBOL(pci_iomap);
347cd248341SJan Glauber 
34871ba41c9SSebastian Ott static void __iomem *pci_iomap_wc_range_mio(struct pci_dev *pdev, int bar,
34971ba41c9SSebastian Ott 					    unsigned long offset, unsigned long max)
35071ba41c9SSebastian Ott {
35171ba41c9SSebastian Ott 	unsigned long barsize = pci_resource_len(pdev, bar);
35271ba41c9SSebastian Ott 	struct zpci_dev *zdev = to_zpci(pdev);
35371ba41c9SSebastian Ott 	void __iomem *iova;
35471ba41c9SSebastian Ott 
35571ba41c9SSebastian Ott 	iova = ioremap((unsigned long) zdev->bars[bar].mio_wb, barsize);
35671ba41c9SSebastian Ott 	return iova ? iova + offset : iova;
35771ba41c9SSebastian Ott }
35871ba41c9SSebastian Ott 
35971ba41c9SSebastian Ott void __iomem *pci_iomap_wc_range(struct pci_dev *pdev, int bar,
36071ba41c9SSebastian Ott 				 unsigned long offset, unsigned long max)
36171ba41c9SSebastian Ott {
362c9c13ba4SDenis Efremov 	if (bar >= PCI_STD_NUM_BARS || !pci_resource_len(pdev, bar))
36371ba41c9SSebastian Ott 		return NULL;
36471ba41c9SSebastian Ott 
36571ba41c9SSebastian Ott 	if (static_branch_likely(&have_mio))
36671ba41c9SSebastian Ott 		return pci_iomap_wc_range_mio(pdev, bar, offset, max);
36771ba41c9SSebastian Ott 	else
36871ba41c9SSebastian Ott 		return pci_iomap_range_fh(pdev, bar, offset, max);
36971ba41c9SSebastian Ott }
37071ba41c9SSebastian Ott EXPORT_SYMBOL(pci_iomap_wc_range);
37171ba41c9SSebastian Ott 
37271ba41c9SSebastian Ott void __iomem *pci_iomap_wc(struct pci_dev *dev, int bar, unsigned long maxlen)
37371ba41c9SSebastian Ott {
37471ba41c9SSebastian Ott 	return pci_iomap_wc_range(dev, bar, 0, maxlen);
37571ba41c9SSebastian Ott }
37671ba41c9SSebastian Ott EXPORT_SYMBOL(pci_iomap_wc);
37771ba41c9SSebastian Ott 
37871ba41c9SSebastian Ott static void pci_iounmap_fh(struct pci_dev *pdev, void __iomem *addr)
379cd248341SJan Glauber {
3809e00caaeSSebastian Ott 	unsigned int idx = ZPCI_IDX(addr);
381cd248341SJan Glauber 
382cd248341SJan Glauber 	spin_lock(&zpci_iomap_lock);
3838cfc99b5SMichael S. Tsirkin 	/* Detect underrun */
384f5e44f82SSebastian Ott 	WARN_ON(!zpci_iomap_start[idx].count);
3858cfc99b5SMichael S. Tsirkin 	if (!--zpci_iomap_start[idx].count) {
386cd248341SJan Glauber 		zpci_iomap_start[idx].fh = 0;
387cd248341SJan Glauber 		zpci_iomap_start[idx].bar = 0;
3888cfc99b5SMichael S. Tsirkin 	}
389cd248341SJan Glauber 	spin_unlock(&zpci_iomap_lock);
390cd248341SJan Glauber }
39171ba41c9SSebastian Ott 
39271ba41c9SSebastian Ott static void pci_iounmap_mio(struct pci_dev *pdev, void __iomem *addr)
39371ba41c9SSebastian Ott {
39471ba41c9SSebastian Ott 	iounmap(addr);
39571ba41c9SSebastian Ott }
39671ba41c9SSebastian Ott 
39771ba41c9SSebastian Ott void pci_iounmap(struct pci_dev *pdev, void __iomem *addr)
39871ba41c9SSebastian Ott {
39971ba41c9SSebastian Ott 	if (static_branch_likely(&have_mio))
40071ba41c9SSebastian Ott 		pci_iounmap_mio(pdev, addr);
40171ba41c9SSebastian Ott 	else
40271ba41c9SSebastian Ott 		pci_iounmap_fh(pdev, addr);
40371ba41c9SSebastian Ott }
404d9426083SSebastian Ott EXPORT_SYMBOL(pci_iounmap);
405cd248341SJan Glauber 
406cd248341SJan Glauber static int pci_read(struct pci_bus *bus, unsigned int devfn, int where,
407cd248341SJan Glauber 		    int size, u32 *val)
408cd248341SJan Glauber {
4097dcfe50fSNiklas Schnelle 	struct zpci_dev *zdev = zdev_from_bus(bus, devfn);
410cd248341SJan Glauber 
41144510d6fSPierre Morel 	return (zdev) ? zpci_cfg_load(zdev, where, val, size) : -ENODEV;
412cd248341SJan Glauber }
413cd248341SJan Glauber 
414cd248341SJan Glauber static int pci_write(struct pci_bus *bus, unsigned int devfn, int where,
415cd248341SJan Glauber 		     int size, u32 val)
416cd248341SJan Glauber {
4177dcfe50fSNiklas Schnelle 	struct zpci_dev *zdev = zdev_from_bus(bus, devfn);
418cd248341SJan Glauber 
41944510d6fSPierre Morel 	return (zdev) ? zpci_cfg_store(zdev, where, val, size) : -ENODEV;
420cd248341SJan Glauber }
421cd248341SJan Glauber 
422cd248341SJan Glauber static struct pci_ops pci_root_ops = {
423cd248341SJan Glauber 	.read = pci_read,
424cd248341SJan Glauber 	.write = pci_write,
425cd248341SJan Glauber };
426cd248341SJan Glauber 
4271803ba2dSSebastian Ott static void zpci_map_resources(struct pci_dev *pdev)
428cd248341SJan Glauber {
42971ba41c9SSebastian Ott 	struct zpci_dev *zdev = to_zpci(pdev);
430cd248341SJan Glauber 	resource_size_t len;
431cd248341SJan Glauber 	int i;
432cd248341SJan Glauber 
433c9c13ba4SDenis Efremov 	for (i = 0; i < PCI_STD_NUM_BARS; i++) {
434cd248341SJan Glauber 		len = pci_resource_len(pdev, i);
435cd248341SJan Glauber 		if (!len)
436cd248341SJan Glauber 			continue;
43771ba41c9SSebastian Ott 
438c7ff0e91SSebastian Ott 		if (zpci_use_mio(zdev))
43971ba41c9SSebastian Ott 			pdev->resource[i].start =
440df057c91SNiklas Schnelle 				(resource_size_t __force) zdev->bars[i].mio_wt;
44171ba41c9SSebastian Ott 		else
442c7ff0e91SSebastian Ott 			pdev->resource[i].start = (resource_size_t __force)
443c7ff0e91SSebastian Ott 				pci_iomap_range_fh(pdev, i, 0, 0);
444cd248341SJan Glauber 		pdev->resource[i].end = pdev->resource[i].start + len - 1;
445cd248341SJan Glauber 	}
446cfbb4a7aSSebastian Ott 
447abb95b75SNiklas Schnelle 	zpci_iov_map_resources(pdev);
448944239c5SSebastian Ott }
449944239c5SSebastian Ott 
4501803ba2dSSebastian Ott static void zpci_unmap_resources(struct pci_dev *pdev)
451944239c5SSebastian Ott {
452c7ff0e91SSebastian Ott 	struct zpci_dev *zdev = to_zpci(pdev);
453944239c5SSebastian Ott 	resource_size_t len;
454944239c5SSebastian Ott 	int i;
455944239c5SSebastian Ott 
456c7ff0e91SSebastian Ott 	if (zpci_use_mio(zdev))
45771ba41c9SSebastian Ott 		return;
45871ba41c9SSebastian Ott 
459c9c13ba4SDenis Efremov 	for (i = 0; i < PCI_STD_NUM_BARS; i++) {
460944239c5SSebastian Ott 		len = pci_resource_len(pdev, i);
461944239c5SSebastian Ott 		if (!len)
462944239c5SSebastian Ott 			continue;
463c7ff0e91SSebastian Ott 		pci_iounmap_fh(pdev, (void __iomem __force *)
4645b9f2081SMartin Schwidefsky 			       pdev->resource[i].start);
465944239c5SSebastian Ott 	}
466944239c5SSebastian Ott }
467cd248341SJan Glauber 
468cd248341SJan Glauber static int zpci_alloc_iomap(struct zpci_dev *zdev)
469cd248341SJan Glauber {
470bf19c94dSSebastian Ott 	unsigned long entry;
471cd248341SJan Glauber 
472cd248341SJan Glauber 	spin_lock(&zpci_iomap_lock);
473c506fff3SSebastian Ott 	entry = find_first_zero_bit(zpci_iomap_bitmap, ZPCI_IOMAP_ENTRIES);
474c506fff3SSebastian Ott 	if (entry == ZPCI_IOMAP_ENTRIES) {
475cd248341SJan Glauber 		spin_unlock(&zpci_iomap_lock);
476cd248341SJan Glauber 		return -ENOSPC;
477cd248341SJan Glauber 	}
478c506fff3SSebastian Ott 	set_bit(entry, zpci_iomap_bitmap);
479cd248341SJan Glauber 	spin_unlock(&zpci_iomap_lock);
480cd248341SJan Glauber 	return entry;
481cd248341SJan Glauber }
482cd248341SJan Glauber 
483cd248341SJan Glauber static void zpci_free_iomap(struct zpci_dev *zdev, int entry)
484cd248341SJan Glauber {
485cd248341SJan Glauber 	spin_lock(&zpci_iomap_lock);
486cd248341SJan Glauber 	memset(&zpci_iomap_start[entry], 0, sizeof(struct zpci_iomap_entry));
487c506fff3SSebastian Ott 	clear_bit(entry, zpci_iomap_bitmap);
488cd248341SJan Glauber 	spin_unlock(&zpci_iomap_lock);
489cd248341SJan Glauber }
490cd248341SJan Glauber 
4914fe20497SNiklas Schnelle static void zpci_do_update_iomap_fh(struct zpci_dev *zdev, u32 fh)
4924fe20497SNiklas Schnelle {
4934fe20497SNiklas Schnelle 	int bar, idx;
4944fe20497SNiklas Schnelle 
4954fe20497SNiklas Schnelle 	spin_lock(&zpci_iomap_lock);
4964fe20497SNiklas Schnelle 	for (bar = 0; bar < PCI_STD_NUM_BARS; bar++) {
4974fe20497SNiklas Schnelle 		if (!zdev->bars[bar].size)
4984fe20497SNiklas Schnelle 			continue;
4994fe20497SNiklas Schnelle 		idx = zdev->bars[bar].map_idx;
5004fe20497SNiklas Schnelle 		if (!zpci_iomap_start[idx].count)
5014fe20497SNiklas Schnelle 			continue;
5024fe20497SNiklas Schnelle 		WRITE_ONCE(zpci_iomap_start[idx].fh, zdev->fh);
5034fe20497SNiklas Schnelle 	}
5044fe20497SNiklas Schnelle 	spin_unlock(&zpci_iomap_lock);
5054fe20497SNiklas Schnelle }
5064fe20497SNiklas Schnelle 
5074fe20497SNiklas Schnelle void zpci_update_fh(struct zpci_dev *zdev, u32 fh)
5084fe20497SNiklas Schnelle {
5094fe20497SNiklas Schnelle 	if (!fh || zdev->fh == fh)
5104fe20497SNiklas Schnelle 		return;
5114fe20497SNiklas Schnelle 
5124fe20497SNiklas Schnelle 	zdev->fh = fh;
5134fe20497SNiklas Schnelle 	if (zpci_use_mio(zdev))
5144fe20497SNiklas Schnelle 		return;
5154fe20497SNiklas Schnelle 	if (zdev->has_resources && zdev_enabled(zdev))
5164fe20497SNiklas Schnelle 		zpci_do_update_iomap_fh(zdev, fh);
5174fe20497SNiklas Schnelle }
5184fe20497SNiklas Schnelle 
5197a572a3aSSebastian Ott static struct resource *__alloc_res(struct zpci_dev *zdev, unsigned long start,
5207a572a3aSSebastian Ott 				    unsigned long size, unsigned long flags)
5217a572a3aSSebastian Ott {
5227a572a3aSSebastian Ott 	struct resource *r;
5237a572a3aSSebastian Ott 
5247a572a3aSSebastian Ott 	r = kzalloc(sizeof(*r), GFP_KERNEL);
5257a572a3aSSebastian Ott 	if (!r)
5267a572a3aSSebastian Ott 		return NULL;
5277a572a3aSSebastian Ott 
5287a572a3aSSebastian Ott 	r->start = start;
5297a572a3aSSebastian Ott 	r->end = r->start + size - 1;
5307a572a3aSSebastian Ott 	r->flags = flags;
5317a572a3aSSebastian Ott 	r->name = zdev->res_name;
5327a572a3aSSebastian Ott 
5337a572a3aSSebastian Ott 	if (request_resource(&iomem_resource, r)) {
5347a572a3aSSebastian Ott 		kfree(r);
5357a572a3aSSebastian Ott 		return NULL;
5367a572a3aSSebastian Ott 	}
5377a572a3aSSebastian Ott 	return r;
5387a572a3aSSebastian Ott }
5397a572a3aSSebastian Ott 
54005bc1be6SPierre Morel int zpci_setup_bus_resources(struct zpci_dev *zdev,
5417a572a3aSSebastian Ott 			     struct list_head *resources)
5427a572a3aSSebastian Ott {
5437a572a3aSSebastian Ott 	unsigned long addr, size, flags;
5447a572a3aSSebastian Ott 	struct resource *res;
5457a572a3aSSebastian Ott 	int i, entry;
5467a572a3aSSebastian Ott 
5477a572a3aSSebastian Ott 	snprintf(zdev->res_name, sizeof(zdev->res_name),
54805bc1be6SPierre Morel 		 "PCI Bus %04x:%02x", zdev->uid, ZPCI_BUS_NR);
5497a572a3aSSebastian Ott 
550c9c13ba4SDenis Efremov 	for (i = 0; i < PCI_STD_NUM_BARS; i++) {
5517a572a3aSSebastian Ott 		if (!zdev->bars[i].size)
5527a572a3aSSebastian Ott 			continue;
5537a572a3aSSebastian Ott 		entry = zpci_alloc_iomap(zdev);
5547a572a3aSSebastian Ott 		if (entry < 0)
5557a572a3aSSebastian Ott 			return entry;
5567a572a3aSSebastian Ott 		zdev->bars[i].map_idx = entry;
5577a572a3aSSebastian Ott 
5587a572a3aSSebastian Ott 		/* only MMIO is supported */
5597a572a3aSSebastian Ott 		flags = IORESOURCE_MEM;
5607a572a3aSSebastian Ott 		if (zdev->bars[i].val & 8)
5617a572a3aSSebastian Ott 			flags |= IORESOURCE_PREFETCH;
5627a572a3aSSebastian Ott 		if (zdev->bars[i].val & 4)
5637a572a3aSSebastian Ott 			flags |= IORESOURCE_MEM_64;
5647a572a3aSSebastian Ott 
565c7ff0e91SSebastian Ott 		if (zpci_use_mio(zdev))
566df057c91SNiklas Schnelle 			addr = (unsigned long) zdev->bars[i].mio_wt;
567dcd33b23SSebastian Ott 		else
5689e00caaeSSebastian Ott 			addr = ZPCI_ADDR(entry);
5697a572a3aSSebastian Ott 		size = 1UL << zdev->bars[i].size;
5707a572a3aSSebastian Ott 
5717a572a3aSSebastian Ott 		res = __alloc_res(zdev, addr, size, flags);
5727a572a3aSSebastian Ott 		if (!res) {
5737a572a3aSSebastian Ott 			zpci_free_iomap(zdev, entry);
5747a572a3aSSebastian Ott 			return -ENOMEM;
5757a572a3aSSebastian Ott 		}
5767a572a3aSSebastian Ott 		zdev->bars[i].res = res;
5777a572a3aSSebastian Ott 		pci_add_resource(resources, res);
5787a572a3aSSebastian Ott 	}
579a50297cfSNiklas Schnelle 	zdev->has_resources = 1;
5807a572a3aSSebastian Ott 
5817a572a3aSSebastian Ott 	return 0;
5827a572a3aSSebastian Ott }
5837a572a3aSSebastian Ott 
5847a572a3aSSebastian Ott static void zpci_cleanup_bus_resources(struct zpci_dev *zdev)
5857a572a3aSSebastian Ott {
5867a572a3aSSebastian Ott 	int i;
5877a572a3aSSebastian Ott 
588c9c13ba4SDenis Efremov 	for (i = 0; i < PCI_STD_NUM_BARS; i++) {
5892b1df724SSebastian Ott 		if (!zdev->bars[i].size || !zdev->bars[i].res)
5907a572a3aSSebastian Ott 			continue;
5917a572a3aSSebastian Ott 
5927a572a3aSSebastian Ott 		zpci_free_iomap(zdev, zdev->bars[i].map_idx);
5937a572a3aSSebastian Ott 		release_resource(zdev->bars[i].res);
5947a572a3aSSebastian Ott 		kfree(zdev->bars[i].res);
5957a572a3aSSebastian Ott 	}
596a50297cfSNiklas Schnelle 	zdev->has_resources = 0;
5977a572a3aSSebastian Ott }
5987a572a3aSSebastian Ott 
59906dc660eSOliver O'Halloran int pcibios_device_add(struct pci_dev *pdev)
600af0a8a84SSebastian Ott {
6012a671f77SNiklas Schnelle 	struct zpci_dev *zdev = to_zpci(pdev);
602cb809182SSebastian Ott 	struct resource *res;
603cb809182SSebastian Ott 	int i;
604cb809182SSebastian Ott 
6052a671f77SNiklas Schnelle 	/* The pdev has a reference to the zdev via its bus */
6062a671f77SNiklas Schnelle 	zpci_zdev_get(zdev);
6077dc20ab1SSebastian Ott 	if (pdev->is_physfn)
6087dc20ab1SSebastian Ott 		pdev->no_vf_scan = 1;
6097dc20ab1SSebastian Ott 
610ef4858c6SSebastian Ott 	pdev->dev.groups = zpci_attr_groups;
6115657933dSBart Van Assche 	pdev->dev.dma_ops = &s390_pci_dma_ops;
6121803ba2dSSebastian Ott 	zpci_map_resources(pdev);
613cb809182SSebastian Ott 
614c9c13ba4SDenis Efremov 	for (i = 0; i < PCI_STD_NUM_BARS; i++) {
615cb809182SSebastian Ott 		res = &pdev->resource[i];
616cb809182SSebastian Ott 		if (res->parent || !res->flags)
617cb809182SSebastian Ott 			continue;
618cb809182SSebastian Ott 		pci_claim_resource(pdev, i);
619cb809182SSebastian Ott 	}
620cb809182SSebastian Ott 
621cb809182SSebastian Ott 	return 0;
622cb809182SSebastian Ott }
623cb809182SSebastian Ott 
6241803ba2dSSebastian Ott void pcibios_release_device(struct pci_dev *pdev)
6251803ba2dSSebastian Ott {
6262a671f77SNiklas Schnelle 	struct zpci_dev *zdev = to_zpci(pdev);
6272a671f77SNiklas Schnelle 
6281803ba2dSSebastian Ott 	zpci_unmap_resources(pdev);
6292a671f77SNiklas Schnelle 	zpci_zdev_put(zdev);
6301803ba2dSSebastian Ott }
6311803ba2dSSebastian Ott 
632cb809182SSebastian Ott int pcibios_enable_device(struct pci_dev *pdev, int mask)
633cb809182SSebastian Ott {
634198a5278SSebastian Ott 	struct zpci_dev *zdev = to_zpci(pdev);
635af0a8a84SSebastian Ott 
6369a99649fSSebastian Ott 	zpci_debug_init_device(zdev, dev_name(&pdev->dev));
637af0a8a84SSebastian Ott 	zpci_fmb_enable_device(zdev);
638af0a8a84SSebastian Ott 
639d7533232SBjorn Helgaas 	return pci_enable_resources(pdev, mask);
640af0a8a84SSebastian Ott }
641af0a8a84SSebastian Ott 
642cb809182SSebastian Ott void pcibios_disable_device(struct pci_dev *pdev)
643944239c5SSebastian Ott {
644198a5278SSebastian Ott 	struct zpci_dev *zdev = to_zpci(pdev);
645944239c5SSebastian Ott 
646944239c5SSebastian Ott 	zpci_fmb_disable_device(zdev);
647944239c5SSebastian Ott 	zpci_debug_exit_device(zdev);
648944239c5SSebastian Ott }
649944239c5SSebastian Ott 
65005bc1be6SPierre Morel static int __zpci_register_domain(int domain)
651cd248341SJan Glauber {
652969ae01bSNiklas Schnelle 	spin_lock(&zpci_domain_lock);
65305bc1be6SPierre Morel 	if (test_bit(domain, zpci_domain)) {
654969ae01bSNiklas Schnelle 		spin_unlock(&zpci_domain_lock);
65505bc1be6SPierre Morel 		pr_err("Domain %04x is already assigned\n", domain);
656312e8462SSebastian Ott 		return -EEXIST;
657312e8462SSebastian Ott 	}
65805bc1be6SPierre Morel 	set_bit(domain, zpci_domain);
659312e8462SSebastian Ott 	spin_unlock(&zpci_domain_lock);
66005bc1be6SPierre Morel 	return domain;
6615c5afd02SSebastian Ott }
66205bc1be6SPierre Morel 
66305bc1be6SPierre Morel static int __zpci_alloc_domain(void)
66405bc1be6SPierre Morel {
66505bc1be6SPierre Morel 	int domain;
66605bc1be6SPierre Morel 
66705bc1be6SPierre Morel 	spin_lock(&zpci_domain_lock);
668969ae01bSNiklas Schnelle 	/*
669969ae01bSNiklas Schnelle 	 * We can always auto allocate domains below ZPCI_NR_DEVICES.
670969ae01bSNiklas Schnelle 	 * There is either a free domain or we have reached the maximum in
671969ae01bSNiklas Schnelle 	 * which case we would have bailed earlier.
672969ae01bSNiklas Schnelle 	 */
67305bc1be6SPierre Morel 	domain = find_first_zero_bit(zpci_domain, ZPCI_NR_DEVICES);
67405bc1be6SPierre Morel 	set_bit(domain, zpci_domain);
675cd248341SJan Glauber 	spin_unlock(&zpci_domain_lock);
67605bc1be6SPierre Morel 	return domain;
677cd248341SJan Glauber }
678cd248341SJan Glauber 
67905bc1be6SPierre Morel int zpci_alloc_domain(int domain)
68005bc1be6SPierre Morel {
68105bc1be6SPierre Morel 	if (zpci_unique_uid) {
68205bc1be6SPierre Morel 		if (domain)
68305bc1be6SPierre Morel 			return __zpci_register_domain(domain);
68405bc1be6SPierre Morel 		pr_warn("UID checking was active but no UID is provided: switching to automatic domain allocation\n");
68505bc1be6SPierre Morel 		update_uid_checking(false);
68605bc1be6SPierre Morel 	}
68705bc1be6SPierre Morel 	return __zpci_alloc_domain();
68805bc1be6SPierre Morel }
68905bc1be6SPierre Morel 
69005bc1be6SPierre Morel void zpci_free_domain(int domain)
691cd248341SJan Glauber {
692cd248341SJan Glauber 	spin_lock(&zpci_domain_lock);
69305bc1be6SPierre Morel 	clear_bit(domain, zpci_domain);
694cd248341SJan Glauber 	spin_unlock(&zpci_domain_lock);
695cd248341SJan Glauber }
696cd248341SJan Glauber 
6977d594322SSebastian Ott 
698a755a45dSJan Glauber int zpci_enable_device(struct zpci_dev *zdev)
699a755a45dSJan Glauber {
700cc049eecSNiklas Schnelle 	u32 fh = zdev->fh;
7011f3f7681SNiklas Schnelle 	int rc = 0;
702a755a45dSJan Glauber 
7031f3f7681SNiklas Schnelle 	if (clp_enable_fh(zdev, &fh, ZPCI_NR_DMA_SPACES))
704f7addcddSNiklas Schnelle 		rc = -EIO;
7051f3f7681SNiklas Schnelle 	else
7064fe20497SNiklas Schnelle 		zpci_update_fh(zdev, fh);
707a755a45dSJan Glauber 	return rc;
708a755a45dSJan Glauber }
709a755a45dSJan Glauber 
710cb65a669SSebastian Ott int zpci_disable_device(struct zpci_dev *zdev)
711cb65a669SSebastian Ott {
712cc049eecSNiklas Schnelle 	u32 fh = zdev->fh;
7138256addaSNiklas Schnelle 	int cc, rc = 0;
7148256addaSNiklas Schnelle 
715cc049eecSNiklas Schnelle 	cc = clp_disable_fh(zdev, &fh);
716cc049eecSNiklas Schnelle 	if (!cc) {
7174fe20497SNiklas Schnelle 		zpci_update_fh(zdev, fh);
718cc049eecSNiklas Schnelle 	} else if (cc == CLP_RC_SETPCIFN_ALRDY) {
7198256addaSNiklas Schnelle 		pr_info("Disabling PCI function %08x had no effect as it was already disabled\n",
7208256addaSNiklas Schnelle 			zdev->fid);
7218256addaSNiklas Schnelle 		/* Function is already disabled - update handle */
722cc049eecSNiklas Schnelle 		rc = clp_refresh_fh(zdev->fid, &fh);
723cc049eecSNiklas Schnelle 		if (!rc) {
7244fe20497SNiklas Schnelle 			zpci_update_fh(zdev, fh);
7258256addaSNiklas Schnelle 			rc = -EINVAL;
726cc049eecSNiklas Schnelle 		}
727cc049eecSNiklas Schnelle 	} else {
7288256addaSNiklas Schnelle 		rc = -EIO;
7298256addaSNiklas Schnelle 	}
7308256addaSNiklas Schnelle 	return rc;
731cb65a669SSebastian Ott }
732cb65a669SSebastian Ott 
733ba764dd7SNiklas Schnelle /**
734da995d53SNiklas Schnelle  * zpci_hot_reset_device - perform a reset of the given zPCI function
735da995d53SNiklas Schnelle  * @zdev: the slot which should be reset
736da995d53SNiklas Schnelle  *
737da995d53SNiklas Schnelle  * Performs a low level reset of the zPCI function. The reset is low level in
738da995d53SNiklas Schnelle  * the sense that the zPCI function can be reset without detaching it from the
739da995d53SNiklas Schnelle  * common PCI subsystem. The reset may be performed while under control of
740da995d53SNiklas Schnelle  * either DMA or IOMMU APIs in which case the existing DMA/IOMMU translation
741da995d53SNiklas Schnelle  * table is reinstated at the end of the reset.
742da995d53SNiklas Schnelle  *
743da995d53SNiklas Schnelle  * After the reset the functions internal state is reset to an initial state
744da995d53SNiklas Schnelle  * equivalent to its state during boot when first probing a driver.
745da995d53SNiklas Schnelle  * Consequently after reset the PCI function requires re-initialization via the
746da995d53SNiklas Schnelle  * common PCI code including re-enabling IRQs via pci_alloc_irq_vectors()
747da995d53SNiklas Schnelle  * and enabling the function via e.g.pci_enablde_device_flags().The caller
748da995d53SNiklas Schnelle  * must guard against concurrent reset attempts.
749da995d53SNiklas Schnelle  *
750da995d53SNiklas Schnelle  * In most cases this function should not be called directly but through
751da995d53SNiklas Schnelle  * pci_reset_function() or pci_reset_bus() which handle the save/restore and
752da995d53SNiklas Schnelle  * locking.
753da995d53SNiklas Schnelle  *
754da995d53SNiklas Schnelle  * Return: 0 on success and an error value otherwise
755da995d53SNiklas Schnelle  */
756da995d53SNiklas Schnelle int zpci_hot_reset_device(struct zpci_dev *zdev)
757da995d53SNiklas Schnelle {
758da995d53SNiklas Schnelle 	int rc;
759da995d53SNiklas Schnelle 
760da995d53SNiklas Schnelle 	zpci_dbg(3, "rst fid:%x, fh:%x\n", zdev->fid, zdev->fh);
761da995d53SNiklas Schnelle 	if (zdev_enabled(zdev)) {
762da995d53SNiklas Schnelle 		/* Disables device access, DMAs and IRQs (reset state) */
763da995d53SNiklas Schnelle 		rc = zpci_disable_device(zdev);
764da995d53SNiklas Schnelle 		/*
765da995d53SNiklas Schnelle 		 * Due to a z/VM vs LPAR inconsistency in the error state the
766da995d53SNiklas Schnelle 		 * FH may indicate an enabled device but disable says the
767da995d53SNiklas Schnelle 		 * device is already disabled don't treat it as an error here.
768da995d53SNiklas Schnelle 		 */
769da995d53SNiklas Schnelle 		if (rc == -EINVAL)
770da995d53SNiklas Schnelle 			rc = 0;
771da995d53SNiklas Schnelle 		if (rc)
772da995d53SNiklas Schnelle 			return rc;
773da995d53SNiklas Schnelle 	}
774da995d53SNiklas Schnelle 
775da995d53SNiklas Schnelle 	rc = zpci_enable_device(zdev);
776da995d53SNiklas Schnelle 	if (rc)
777da995d53SNiklas Schnelle 		return rc;
778da995d53SNiklas Schnelle 
779da995d53SNiklas Schnelle 	if (zdev->dma_table)
780da995d53SNiklas Schnelle 		rc = zpci_register_ioat(zdev, 0, zdev->start_dma, zdev->end_dma,
781568de506SNiklas Schnelle 					virt_to_phys(zdev->dma_table));
782da995d53SNiklas Schnelle 	else
783da995d53SNiklas Schnelle 		rc = zpci_dma_init_device(zdev);
784da995d53SNiklas Schnelle 	if (rc) {
785da995d53SNiklas Schnelle 		zpci_disable_device(zdev);
786da995d53SNiklas Schnelle 		return rc;
787da995d53SNiklas Schnelle 	}
788da995d53SNiklas Schnelle 
789da995d53SNiklas Schnelle 	return 0;
790da995d53SNiklas Schnelle }
791da995d53SNiklas Schnelle 
792da995d53SNiklas Schnelle /**
793ba764dd7SNiklas Schnelle  * zpci_create_device() - Create a new zpci_dev and add it to the zbus
794ba764dd7SNiklas Schnelle  * @fid: Function ID of the device to be created
795ba764dd7SNiklas Schnelle  * @fh: Current Function Handle of the device to be created
796ba764dd7SNiklas Schnelle  * @state: Initial state after creation either Standby or Configured
797ba764dd7SNiklas Schnelle  *
798ba764dd7SNiklas Schnelle  * Creates a new zpci device and adds it to its, possibly newly created, zbus
799ba764dd7SNiklas Schnelle  * as well as zpci_list.
800ba764dd7SNiklas Schnelle  *
80114c87ba8SNiklas Schnelle  * Returns: the zdev on success or an error pointer otherwise
802ba764dd7SNiklas Schnelle  */
80314c87ba8SNiklas Schnelle struct zpci_dev *zpci_create_device(u32 fid, u32 fh, enum zpci_state state)
804cd248341SJan Glauber {
805ba764dd7SNiklas Schnelle 	struct zpci_dev *zdev;
806cd248341SJan Glauber 	int rc;
807cd248341SJan Glauber 
80852c79e63SNiklas Schnelle 	zpci_dbg(1, "add fid:%x, fh:%x, c:%d\n", fid, fh, state);
809ba764dd7SNiklas Schnelle 	zdev = kzalloc(sizeof(*zdev), GFP_KERNEL);
810ba764dd7SNiklas Schnelle 	if (!zdev)
81114c87ba8SNiklas Schnelle 		return ERR_PTR(-ENOMEM);
812ba764dd7SNiklas Schnelle 
813ba764dd7SNiklas Schnelle 	/* FID and Function Handle are the static/dynamic identifiers */
814ba764dd7SNiklas Schnelle 	zdev->fid = fid;
815ba764dd7SNiklas Schnelle 	zdev->fh = fh;
816ba764dd7SNiklas Schnelle 
817ba764dd7SNiklas Schnelle 	/* Query function properties and update zdev */
818ba764dd7SNiklas Schnelle 	rc = clp_query_pci_fn(zdev);
819ba764dd7SNiklas Schnelle 	if (rc)
820ba764dd7SNiklas Schnelle 		goto error;
821ba764dd7SNiklas Schnelle 	zdev->state =  state;
822ba764dd7SNiklas Schnelle 
82305bc1be6SPierre Morel 	kref_init(&zdev->kref);
824ba764dd7SNiklas Schnelle 	mutex_init(&zdev->lock);
825ba764dd7SNiklas Schnelle 
826ba764dd7SNiklas Schnelle 	rc = zpci_init_iommu(zdev);
827ba764dd7SNiklas Schnelle 	if (rc)
828ba764dd7SNiklas Schnelle 		goto error;
829ba764dd7SNiklas Schnelle 
830ba764dd7SNiklas Schnelle 	rc = zpci_bus_device_register(zdev, &pci_root_ops);
831ba764dd7SNiklas Schnelle 	if (rc)
832a50297cfSNiklas Schnelle 		goto error_destroy_iommu;
83305bc1be6SPierre Morel 
83405bc1be6SPierre Morel 	spin_lock(&zpci_list_lock);
83505bc1be6SPierre Morel 	list_add_tail(&zdev->entry, &zpci_list);
83605bc1be6SPierre Morel 	spin_unlock(&zpci_list_lock);
837cd248341SJan Glauber 
83814c87ba8SNiklas Schnelle 	return zdev;
839cd248341SJan Glauber 
840ba764dd7SNiklas Schnelle error_destroy_iommu:
841f42c2235SJoerg Roedel 	zpci_destroy_iommu(zdev);
842ba764dd7SNiklas Schnelle error:
843ba764dd7SNiklas Schnelle 	zpci_dbg(0, "add fid:%x, rc:%d\n", fid, rc);
844ba764dd7SNiklas Schnelle 	kfree(zdev);
84514c87ba8SNiklas Schnelle 	return ERR_PTR(rc);
846cd248341SJan Glauber }
847cd248341SJan Glauber 
848a46044a9SNiklas Schnelle bool zpci_is_device_configured(struct zpci_dev *zdev)
849a46044a9SNiklas Schnelle {
850a46044a9SNiklas Schnelle 	enum zpci_state state = zdev->state;
851a46044a9SNiklas Schnelle 
852a46044a9SNiklas Schnelle 	return state != ZPCI_FN_STATE_RESERVED &&
853a46044a9SNiklas Schnelle 		state != ZPCI_FN_STATE_STANDBY;
854a46044a9SNiklas Schnelle }
855a46044a9SNiklas Schnelle 
8562631f6b6SNiklas Schnelle /**
857a7f82c36SNiklas Schnelle  * zpci_scan_configured_device() - Scan a freshly configured zpci_dev
8582631f6b6SNiklas Schnelle  * @zdev: The zpci_dev to be configured
8592631f6b6SNiklas Schnelle  * @fh: The general function handle supplied by the platform
8602631f6b6SNiklas Schnelle  *
86161311e32SNiklas Schnelle  * Given a device in the configuration state Configured, enables, scans and
862a7f82c36SNiklas Schnelle  * adds it to the common code PCI subsystem if possible. If the PCI device is
863a7f82c36SNiklas Schnelle  * parked because we can not yet create a PCI bus because we have not seen
864a7f82c36SNiklas Schnelle  * function 0, it is ignored but will be scanned once function 0 appears.
865a7f82c36SNiklas Schnelle  * If any failure occurs, the zpci_dev is left disabled.
8662631f6b6SNiklas Schnelle  *
8672631f6b6SNiklas Schnelle  * Return: 0 on success, or an error code otherwise
8682631f6b6SNiklas Schnelle  */
869a7f82c36SNiklas Schnelle int zpci_scan_configured_device(struct zpci_dev *zdev, u32 fh)
8702631f6b6SNiklas Schnelle {
8712631f6b6SNiklas Schnelle 	int rc;
8722631f6b6SNiklas Schnelle 
8734fe20497SNiklas Schnelle 	zpci_update_fh(zdev, fh);
8742631f6b6SNiklas Schnelle 	/* the PCI function will be scanned once function 0 appears */
8752631f6b6SNiklas Schnelle 	if (!zdev->zbus->bus)
8762631f6b6SNiklas Schnelle 		return 0;
8772631f6b6SNiklas Schnelle 
878a50297cfSNiklas Schnelle 	/* For function 0 on a multi-function bus scan whole bus as we might
879a50297cfSNiklas Schnelle 	 * have to pick up existing functions waiting for it to allow creating
880a50297cfSNiklas Schnelle 	 * the PCI bus
881a50297cfSNiklas Schnelle 	 */
882a50297cfSNiklas Schnelle 	if (zdev->devfn == 0 && zdev->zbus->multifunction)
883a50297cfSNiklas Schnelle 		rc = zpci_bus_scan_bus(zdev->zbus);
884a50297cfSNiklas Schnelle 	else
885faf29a4dSNiklas Schnelle 		rc = zpci_bus_scan_device(zdev);
8862631f6b6SNiklas Schnelle 
8872631f6b6SNiklas Schnelle 	return rc;
8882631f6b6SNiklas Schnelle }
8892631f6b6SNiklas Schnelle 
8902631f6b6SNiklas Schnelle /**
8912631f6b6SNiklas Schnelle  * zpci_deconfigure_device() - Deconfigure a zpci_dev
8922631f6b6SNiklas Schnelle  * @zdev: The zpci_dev to configure
8932631f6b6SNiklas Schnelle  *
8942631f6b6SNiklas Schnelle  * Deconfigure a zPCI function that is currently configured and possibly known
8952631f6b6SNiklas Schnelle  * to the common code PCI subsystem.
8962631f6b6SNiklas Schnelle  * If any failure occurs the device is left as is.
8972631f6b6SNiklas Schnelle  *
8982631f6b6SNiklas Schnelle  * Return: 0 on success, or an error code otherwise
8992631f6b6SNiklas Schnelle  */
9002631f6b6SNiklas Schnelle int zpci_deconfigure_device(struct zpci_dev *zdev)
9012631f6b6SNiklas Schnelle {
9022631f6b6SNiklas Schnelle 	int rc;
9032631f6b6SNiklas Schnelle 
9042631f6b6SNiklas Schnelle 	if (zdev->zbus->bus)
90595b3a8b4SNiklas Schnelle 		zpci_bus_remove_device(zdev, false);
9062631f6b6SNiklas Schnelle 
9071f3f7681SNiklas Schnelle 	if (zdev->dma_table) {
9081f3f7681SNiklas Schnelle 		rc = zpci_dma_exit_device(zdev);
9091f3f7681SNiklas Schnelle 		if (rc)
9101f3f7681SNiklas Schnelle 			return rc;
9111f3f7681SNiklas Schnelle 	}
9122631f6b6SNiklas Schnelle 	if (zdev_enabled(zdev)) {
9132631f6b6SNiklas Schnelle 		rc = zpci_disable_device(zdev);
9142631f6b6SNiklas Schnelle 		if (rc)
9152631f6b6SNiklas Schnelle 			return rc;
9162631f6b6SNiklas Schnelle 	}
9172631f6b6SNiklas Schnelle 
9182631f6b6SNiklas Schnelle 	rc = sclp_pci_deconfigure(zdev->fid);
9192631f6b6SNiklas Schnelle 	zpci_dbg(3, "deconf fid:%x, rc:%d\n", zdev->fid, rc);
9202631f6b6SNiklas Schnelle 	if (rc)
9212631f6b6SNiklas Schnelle 		return rc;
9222631f6b6SNiklas Schnelle 	zdev->state = ZPCI_FN_STATE_STANDBY;
9232631f6b6SNiklas Schnelle 
9242631f6b6SNiklas Schnelle 	return 0;
9252631f6b6SNiklas Schnelle }
9262631f6b6SNiklas Schnelle 
927a46044a9SNiklas Schnelle /**
928a46044a9SNiklas Schnelle  * zpci_device_reserved() - Mark device as resverved
929a46044a9SNiklas Schnelle  * @zdev: the zpci_dev that was reserved
930a46044a9SNiklas Schnelle  *
931a46044a9SNiklas Schnelle  * Handle the case that a given zPCI function was reserved by another system.
932a46044a9SNiklas Schnelle  * After a call to this function the zpci_dev can not be found via
933a46044a9SNiklas Schnelle  * get_zdev_by_fid() anymore but may still be accessible via existing
934a46044a9SNiklas Schnelle  * references though it will not be functional anymore.
935a46044a9SNiklas Schnelle  */
936a46044a9SNiklas Schnelle void zpci_device_reserved(struct zpci_dev *zdev)
937a46044a9SNiklas Schnelle {
938a46044a9SNiklas Schnelle 	if (zdev->has_hp_slot)
939a46044a9SNiklas Schnelle 		zpci_exit_slot(zdev);
940a46044a9SNiklas Schnelle 	/*
941a46044a9SNiklas Schnelle 	 * Remove device from zpci_list as it is going away. This also
942a46044a9SNiklas Schnelle 	 * makes sure we ignore subsequent zPCI events for this device.
943a46044a9SNiklas Schnelle 	 */
944a46044a9SNiklas Schnelle 	spin_lock(&zpci_list_lock);
945a46044a9SNiklas Schnelle 	list_del(&zdev->entry);
946a46044a9SNiklas Schnelle 	spin_unlock(&zpci_list_lock);
947a46044a9SNiklas Schnelle 	zdev->state = ZPCI_FN_STATE_RESERVED;
948a46044a9SNiklas Schnelle 	zpci_dbg(3, "rsv fid:%x\n", zdev->fid);
949a46044a9SNiklas Schnelle 	zpci_zdev_put(zdev);
950a46044a9SNiklas Schnelle }
951a46044a9SNiklas Schnelle 
95205bc1be6SPierre Morel void zpci_release_device(struct kref *kref)
953623bd44dSSebastian Ott {
95405bc1be6SPierre Morel 	struct zpci_dev *zdev = container_of(kref, struct zpci_dev, kref);
955a9045c22SNiklas Schnelle 	int ret;
956623bd44dSSebastian Ott 
9572f0230b2SNiklas Schnelle 	if (zdev->zbus->bus)
95895b3a8b4SNiklas Schnelle 		zpci_bus_remove_device(zdev, false);
95944510d6fSPierre Morel 
9601f3f7681SNiklas Schnelle 	if (zdev->dma_table)
9611f3f7681SNiklas Schnelle 		zpci_dma_exit_device(zdev);
962f6576a1bSNiklas Schnelle 	if (zdev_enabled(zdev))
96305bc1be6SPierre Morel 		zpci_disable_device(zdev);
964f6576a1bSNiklas Schnelle 
965f6576a1bSNiklas Schnelle 	switch (zdev->state) {
966a9045c22SNiklas Schnelle 	case ZPCI_FN_STATE_CONFIGURED:
967a9045c22SNiklas Schnelle 		ret = sclp_pci_deconfigure(zdev->fid);
968a9045c22SNiklas Schnelle 		zpci_dbg(3, "deconf fid:%x, rc:%d\n", zdev->fid, ret);
969a9045c22SNiklas Schnelle 		fallthrough;
97005bc1be6SPierre Morel 	case ZPCI_FN_STATE_STANDBY:
97144510d6fSPierre Morel 		if (zdev->has_hp_slot)
97205bc1be6SPierre Morel 			zpci_exit_slot(zdev);
973a46044a9SNiklas Schnelle 		spin_lock(&zpci_list_lock);
974a46044a9SNiklas Schnelle 		list_del(&zdev->entry);
975a46044a9SNiklas Schnelle 		spin_unlock(&zpci_list_lock);
976a46044a9SNiklas Schnelle 		zpci_dbg(3, "rsv fid:%x\n", zdev->fid);
977a46044a9SNiklas Schnelle 		fallthrough;
978a46044a9SNiklas Schnelle 	case ZPCI_FN_STATE_RESERVED:
97902368b7cSNiklas Schnelle 		if (zdev->has_resources)
98005bc1be6SPierre Morel 			zpci_cleanup_bus_resources(zdev);
98105bc1be6SPierre Morel 		zpci_bus_device_unregister(zdev);
98205bc1be6SPierre Morel 		zpci_destroy_iommu(zdev);
98305bc1be6SPierre Morel 		fallthrough;
98405bc1be6SPierre Morel 	default:
98505bc1be6SPierre Morel 		break;
98605bc1be6SPierre Morel 	}
98705bc1be6SPierre Morel 	zpci_dbg(3, "rem fid:%x\n", zdev->fid);
98805bc1be6SPierre Morel 	kfree(zdev);
989623bd44dSSebastian Ott }
990623bd44dSSebastian Ott 
991bd3a1725SMartin Schwidefsky int zpci_report_error(struct pci_dev *pdev,
992bd3a1725SMartin Schwidefsky 		      struct zpci_report_error_header *report)
993bd3a1725SMartin Schwidefsky {
994bd3a1725SMartin Schwidefsky 	struct zpci_dev *zdev = to_zpci(pdev);
995bd3a1725SMartin Schwidefsky 
996bd3a1725SMartin Schwidefsky 	return sclp_pci_report(report, zdev->fh, zdev->fid);
997bd3a1725SMartin Schwidefsky }
998bd3a1725SMartin Schwidefsky EXPORT_SYMBOL(zpci_report_error);
999bd3a1725SMartin Schwidefsky 
10004cdf2f4eSNiklas Schnelle /**
10014cdf2f4eSNiklas Schnelle  * zpci_clear_error_state() - Clears the zPCI error state of the device
10024cdf2f4eSNiklas Schnelle  * @zdev: The zdev for which the zPCI error state should be reset
10034cdf2f4eSNiklas Schnelle  *
10044cdf2f4eSNiklas Schnelle  * Clear the zPCI error state of the device. If clearing the zPCI error state
10054cdf2f4eSNiklas Schnelle  * fails the device is left in the error state. In this case it may make sense
10064cdf2f4eSNiklas Schnelle  * to call zpci_io_perm_failure() on the associated pdev if it exists.
10074cdf2f4eSNiklas Schnelle  *
10084cdf2f4eSNiklas Schnelle  * Returns: 0 on success, -EIO otherwise
10094cdf2f4eSNiklas Schnelle  */
10104cdf2f4eSNiklas Schnelle int zpci_clear_error_state(struct zpci_dev *zdev)
10114cdf2f4eSNiklas Schnelle {
10124cdf2f4eSNiklas Schnelle 	u64 req = ZPCI_CREATE_REQ(zdev->fh, 0, ZPCI_MOD_FC_RESET_ERROR);
10134cdf2f4eSNiklas Schnelle 	struct zpci_fib fib = {0};
10144cdf2f4eSNiklas Schnelle 	u8 status;
10154cdf2f4eSNiklas Schnelle 	int cc;
10164cdf2f4eSNiklas Schnelle 
10174cdf2f4eSNiklas Schnelle 	cc = zpci_mod_fc(req, &fib, &status);
10184cdf2f4eSNiklas Schnelle 	if (cc) {
10194cdf2f4eSNiklas Schnelle 		zpci_dbg(3, "ces fid:%x, cc:%d, status:%x\n", zdev->fid, cc, status);
10204cdf2f4eSNiklas Schnelle 		return -EIO;
10214cdf2f4eSNiklas Schnelle 	}
10224cdf2f4eSNiklas Schnelle 
10234cdf2f4eSNiklas Schnelle 	return 0;
10244cdf2f4eSNiklas Schnelle }
10254cdf2f4eSNiklas Schnelle 
10264cdf2f4eSNiklas Schnelle /**
10274cdf2f4eSNiklas Schnelle  * zpci_reset_load_store_blocked() - Re-enables L/S from error state
10284cdf2f4eSNiklas Schnelle  * @zdev: The zdev for which to unblock load/store access
10294cdf2f4eSNiklas Schnelle  *
10304cdf2f4eSNiklas Schnelle  * Re-enables load/store access for a PCI function in the error state while
10314cdf2f4eSNiklas Schnelle  * keeping DMA blocked. In this state drivers can poke MMIO space to determine
10324cdf2f4eSNiklas Schnelle  * if error recovery is possible while catching any rogue DMA access from the
10334cdf2f4eSNiklas Schnelle  * device.
10344cdf2f4eSNiklas Schnelle  *
10354cdf2f4eSNiklas Schnelle  * Returns: 0 on success, -EIO otherwise
10364cdf2f4eSNiklas Schnelle  */
10374cdf2f4eSNiklas Schnelle int zpci_reset_load_store_blocked(struct zpci_dev *zdev)
10384cdf2f4eSNiklas Schnelle {
10394cdf2f4eSNiklas Schnelle 	u64 req = ZPCI_CREATE_REQ(zdev->fh, 0, ZPCI_MOD_FC_RESET_BLOCK);
10404cdf2f4eSNiklas Schnelle 	struct zpci_fib fib = {0};
10414cdf2f4eSNiklas Schnelle 	u8 status;
10424cdf2f4eSNiklas Schnelle 	int cc;
10434cdf2f4eSNiklas Schnelle 
10444cdf2f4eSNiklas Schnelle 	cc = zpci_mod_fc(req, &fib, &status);
10454cdf2f4eSNiklas Schnelle 	if (cc) {
10464cdf2f4eSNiklas Schnelle 		zpci_dbg(3, "rls fid:%x, cc:%d, status:%x\n", zdev->fid, cc, status);
10474cdf2f4eSNiklas Schnelle 		return -EIO;
10484cdf2f4eSNiklas Schnelle 	}
10494cdf2f4eSNiklas Schnelle 
10504cdf2f4eSNiklas Schnelle 	return 0;
10514cdf2f4eSNiklas Schnelle }
10524cdf2f4eSNiklas Schnelle 
1053cd248341SJan Glauber static int zpci_mem_init(void)
1054cd248341SJan Glauber {
105580c544deSSebastian Ott 	BUILD_BUG_ON(!is_power_of_2(__alignof__(struct zpci_fmb)) ||
105680c544deSSebastian Ott 		     __alignof__(struct zpci_fmb) < sizeof(struct zpci_fmb));
105780c544deSSebastian Ott 
1058d0b08853SJan Glauber 	zdev_fmb_cache = kmem_cache_create("PCI_FMB_cache", sizeof(struct zpci_fmb),
105980c544deSSebastian Ott 					   __alignof__(struct zpci_fmb), 0, NULL);
1060d0b08853SJan Glauber 	if (!zdev_fmb_cache)
1061c506fff3SSebastian Ott 		goto error_fmb;
1062d0b08853SJan Glauber 
1063c506fff3SSebastian Ott 	zpci_iomap_start = kcalloc(ZPCI_IOMAP_ENTRIES,
1064c506fff3SSebastian Ott 				   sizeof(*zpci_iomap_start), GFP_KERNEL);
1065cd248341SJan Glauber 	if (!zpci_iomap_start)
10669a4da8a5SJan Glauber 		goto error_iomap;
1067cd248341SJan Glauber 
1068c506fff3SSebastian Ott 	zpci_iomap_bitmap = kcalloc(BITS_TO_LONGS(ZPCI_IOMAP_ENTRIES),
1069c506fff3SSebastian Ott 				    sizeof(*zpci_iomap_bitmap), GFP_KERNEL);
1070c506fff3SSebastian Ott 	if (!zpci_iomap_bitmap)
1071c506fff3SSebastian Ott 		goto error_iomap_bitmap;
1072c506fff3SSebastian Ott 
1073b02002ccSNiklas Schnelle 	if (static_branch_likely(&have_mio))
1074b02002ccSNiklas Schnelle 		clp_setup_writeback_mio();
1075b02002ccSNiklas Schnelle 
1076c506fff3SSebastian Ott 	return 0;
1077c506fff3SSebastian Ott error_iomap_bitmap:
1078c506fff3SSebastian Ott 	kfree(zpci_iomap_start);
10799a4da8a5SJan Glauber error_iomap:
1080d0b08853SJan Glauber 	kmem_cache_destroy(zdev_fmb_cache);
1081c506fff3SSebastian Ott error_fmb:
1082cd248341SJan Glauber 	return -ENOMEM;
1083cd248341SJan Glauber }
1084cd248341SJan Glauber 
1085cd248341SJan Glauber static void zpci_mem_exit(void)
1086cd248341SJan Glauber {
1087c506fff3SSebastian Ott 	kfree(zpci_iomap_bitmap);
1088cd248341SJan Glauber 	kfree(zpci_iomap_start);
1089d0b08853SJan Glauber 	kmem_cache_destroy(zdev_fmb_cache);
1090cd248341SJan Glauber }
1091cd248341SJan Glauber 
10926324b4deSSebastian Ott static unsigned int s390_pci_probe __initdata = 1;
1093fbfe07d4SSebastian Ott unsigned int s390_pci_force_floating __initdata;
1094aa3b7c29SSebastian Ott static unsigned int s390_pci_initialized;
1095cd248341SJan Glauber 
1096cd248341SJan Glauber char * __init pcibios_setup(char *str)
1097cd248341SJan Glauber {
1098257608fbSSebastian Ott 	if (!strcmp(str, "off")) {
1099257608fbSSebastian Ott 		s390_pci_probe = 0;
1100cd248341SJan Glauber 		return NULL;
1101cd248341SJan Glauber 	}
110256271303SSebastian Ott 	if (!strcmp(str, "nomio")) {
11033322ba0dSNiklas Schnelle 		S390_lowcore.machine_flags &= ~MACHINE_FLAG_PCI_MIO;
110456271303SSebastian Ott 		return NULL;
110556271303SSebastian Ott 	}
1106fbfe07d4SSebastian Ott 	if (!strcmp(str, "force_floating")) {
1107fbfe07d4SSebastian Ott 		s390_pci_force_floating = 1;
1108fbfe07d4SSebastian Ott 		return NULL;
1109fbfe07d4SSebastian Ott 	}
11106cf17f9aSPierre Morel 	if (!strcmp(str, "norid")) {
11116cf17f9aSPierre Morel 		s390_pci_no_rid = 1;
11126cf17f9aSPierre Morel 		return NULL;
11136cf17f9aSPierre Morel 	}
1114cd248341SJan Glauber 	return str;
1115cd248341SJan Glauber }
1116cd248341SJan Glauber 
1117aa3b7c29SSebastian Ott bool zpci_is_enabled(void)
1118aa3b7c29SSebastian Ott {
1119aa3b7c29SSebastian Ott 	return s390_pci_initialized;
1120aa3b7c29SSebastian Ott }
1121aa3b7c29SSebastian Ott 
1122cd248341SJan Glauber static int __init pci_base_init(void)
1123cd248341SJan Glauber {
1124cd248341SJan Glauber 	int rc;
1125cd248341SJan Glauber 
11261e5635d1SHeiko Carstens 	if (!s390_pci_probe)
1127cd248341SJan Glauber 		return 0;
1128cd248341SJan Glauber 
1129da78693eSNiklas Schnelle 	if (!test_facility(69) || !test_facility(71)) {
1130da78693eSNiklas Schnelle 		pr_info("PCI is not supported because CPU facilities 69 or 71 are not available\n");
1131cd248341SJan Glauber 		return 0;
1132da78693eSNiklas Schnelle 	}
1133cd248341SJan Glauber 
11343322ba0dSNiklas Schnelle 	if (MACHINE_HAS_PCI_MIO) {
113571ba41c9SSebastian Ott 		static_branch_enable(&have_mio);
11369964f396SSebastian Ott 		ctl_set_bit(2, 5);
11379964f396SSebastian Ott 	}
113871ba41c9SSebastian Ott 
1139d0b08853SJan Glauber 	rc = zpci_debug_init();
1140d0b08853SJan Glauber 	if (rc)
11411f44a225SMartin Schwidefsky 		goto out;
1142d0b08853SJan Glauber 
1143cd248341SJan Glauber 	rc = zpci_mem_init();
1144cd248341SJan Glauber 	if (rc)
1145cd248341SJan Glauber 		goto out_mem;
1146cd248341SJan Glauber 
11479a4da8a5SJan Glauber 	rc = zpci_irq_init();
11489a4da8a5SJan Glauber 	if (rc)
11499a4da8a5SJan Glauber 		goto out_irq;
11509a4da8a5SJan Glauber 
1151828b35f6SJan Glauber 	rc = zpci_dma_init();
1152828b35f6SJan Glauber 	if (rc)
1153828b35f6SJan Glauber 		goto out_dma;
1154828b35f6SJan Glauber 
11551d578966SSebastian Ott 	rc = clp_scan_pci_devices();
1156a755a45dSJan Glauber 	if (rc)
1157a755a45dSJan Glauber 		goto out_find;
115814c87ba8SNiklas Schnelle 	zpci_bus_scan_busses();
1159a755a45dSJan Glauber 
1160aa3b7c29SSebastian Ott 	s390_pci_initialized = 1;
1161cd248341SJan Glauber 	return 0;
1162cd248341SJan Glauber 
1163a755a45dSJan Glauber out_find:
1164828b35f6SJan Glauber 	zpci_dma_exit();
1165828b35f6SJan Glauber out_dma:
11669a4da8a5SJan Glauber 	zpci_irq_exit();
11679a4da8a5SJan Glauber out_irq:
1168cd248341SJan Glauber 	zpci_mem_exit();
1169cd248341SJan Glauber out_mem:
1170d0b08853SJan Glauber 	zpci_debug_exit();
11711f44a225SMartin Schwidefsky out:
1172cd248341SJan Glauber 	return rc;
1173cd248341SJan Glauber }
117467f43f38SSebastian Ott subsys_initcall_sync(pci_base_init);
1175