xref: /linux/arch/s390/pci/pci.c (revision 09340b2fca007509c3cbc34fdc97961e0abfc589)
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 
6498b1d33dSMatthew Rosato /* AEN structures that must be preserved over KVM module re-insertion */
6598b1d33dSMatthew Rosato union zpci_sic_iib *zpci_aipb;
6698b1d33dSMatthew Rosato EXPORT_SYMBOL_GPL(zpci_aipb);
6798b1d33dSMatthew Rosato struct airq_iv *zpci_aif_sbv;
6898b1d33dSMatthew Rosato EXPORT_SYMBOL_GPL(zpci_aif_sbv);
6998b1d33dSMatthew Rosato 
70cd248341SJan Glauber struct zpci_dev *get_zdev_by_fid(u32 fid)
71cd248341SJan Glauber {
72cd248341SJan Glauber 	struct zpci_dev *tmp, *zdev = NULL;
73cd248341SJan Glauber 
7457b5918cSSebastian Ott 	spin_lock(&zpci_list_lock);
75cd248341SJan Glauber 	list_for_each_entry(tmp, &zpci_list, entry) {
76cd248341SJan Glauber 		if (tmp->fid == fid) {
77cd248341SJan Glauber 			zdev = tmp;
78c122383dSNiklas Schnelle 			zpci_zdev_get(zdev);
79cd248341SJan Glauber 			break;
80cd248341SJan Glauber 		}
81cd248341SJan Glauber 	}
8257b5918cSSebastian Ott 	spin_unlock(&zpci_list_lock);
83cd248341SJan Glauber 	return zdev;
84cd248341SJan Glauber }
85cd248341SJan Glauber 
8601553d9aSSebastian Ott void zpci_remove_reserved_devices(void)
8701553d9aSSebastian Ott {
8801553d9aSSebastian Ott 	struct zpci_dev *tmp, *zdev;
8901553d9aSSebastian Ott 	enum zpci_state state;
9001553d9aSSebastian Ott 	LIST_HEAD(remove);
9101553d9aSSebastian Ott 
9201553d9aSSebastian Ott 	spin_lock(&zpci_list_lock);
9301553d9aSSebastian Ott 	list_for_each_entry_safe(zdev, tmp, &zpci_list, entry) {
9401553d9aSSebastian Ott 		if (zdev->state == ZPCI_FN_STATE_STANDBY &&
9501553d9aSSebastian Ott 		    !clp_get_state(zdev->fid, &state) &&
9601553d9aSSebastian Ott 		    state == ZPCI_FN_STATE_RESERVED)
9701553d9aSSebastian Ott 			list_move_tail(&zdev->entry, &remove);
9801553d9aSSebastian Ott 	}
9901553d9aSSebastian Ott 	spin_unlock(&zpci_list_lock);
10001553d9aSSebastian Ott 
10101553d9aSSebastian Ott 	list_for_each_entry_safe(zdev, tmp, &remove, entry)
102a46044a9SNiklas Schnelle 		zpci_device_reserved(zdev);
103cd248341SJan Glauber }
104cd248341SJan Glauber 
105cd248341SJan Glauber int pci_domain_nr(struct pci_bus *bus)
106cd248341SJan Glauber {
10705bc1be6SPierre Morel 	return ((struct zpci_bus *) bus->sysdata)->domain_nr;
108cd248341SJan Glauber }
109cd248341SJan Glauber EXPORT_SYMBOL_GPL(pci_domain_nr);
110cd248341SJan Glauber 
111cd248341SJan Glauber int pci_proc_domain(struct pci_bus *bus)
112cd248341SJan Glauber {
113cd248341SJan Glauber 	return pci_domain_nr(bus);
114cd248341SJan Glauber }
115cd248341SJan Glauber EXPORT_SYMBOL_GPL(pci_proc_domain);
116cd248341SJan Glauber 
117828b35f6SJan Glauber /* Modify PCI: Register I/O address translation parameters */
118828b35f6SJan Glauber int zpci_register_ioat(struct zpci_dev *zdev, u8 dmaas,
119828b35f6SJan Glauber 		       u64 base, u64 limit, u64 iota)
120828b35f6SJan Glauber {
12172570834SSebastian Ott 	u64 req = ZPCI_CREATE_REQ(zdev->fh, dmaas, ZPCI_MOD_FC_REG_IOAT);
12272570834SSebastian Ott 	struct zpci_fib fib = {0};
1231f3f7681SNiklas Schnelle 	u8 cc, status;
124828b35f6SJan Glauber 
125828b35f6SJan Glauber 	WARN_ON_ONCE(iota & 0x3fff);
12672570834SSebastian Ott 	fib.pba = base;
12772570834SSebastian Ott 	fib.pal = limit;
12872570834SSebastian Ott 	fib.iota = iota | ZPCI_IOTA_RTTO_FLAG;
129c68468edSMatthew Rosato 	fib.gd = zdev->gisa;
1301f3f7681SNiklas Schnelle 	cc = zpci_mod_fc(req, &fib, &status);
1311f3f7681SNiklas Schnelle 	if (cc)
1321f3f7681SNiklas Schnelle 		zpci_dbg(3, "reg ioat fid:%x, cc:%d, status:%d\n", zdev->fid, cc, status);
1331f3f7681SNiklas Schnelle 	return cc;
134828b35f6SJan Glauber }
135*09340b2fSMatthew Rosato EXPORT_SYMBOL_GPL(zpci_register_ioat);
136828b35f6SJan Glauber 
137828b35f6SJan Glauber /* Modify PCI: Unregister I/O address translation parameters */
138828b35f6SJan Glauber int zpci_unregister_ioat(struct zpci_dev *zdev, u8 dmaas)
139828b35f6SJan Glauber {
14072570834SSebastian Ott 	u64 req = ZPCI_CREATE_REQ(zdev->fh, dmaas, ZPCI_MOD_FC_DEREG_IOAT);
14172570834SSebastian Ott 	struct zpci_fib fib = {0};
14272570834SSebastian Ott 	u8 cc, status;
143828b35f6SJan Glauber 
144c68468edSMatthew Rosato 	fib.gd = zdev->gisa;
145c68468edSMatthew Rosato 
14672570834SSebastian Ott 	cc = zpci_mod_fc(req, &fib, &status);
1471f3f7681SNiklas Schnelle 	if (cc)
1481f3f7681SNiklas Schnelle 		zpci_dbg(3, "unreg ioat fid:%x, cc:%d, status:%d\n", zdev->fid, cc, status);
1491f3f7681SNiklas Schnelle 	return cc;
150828b35f6SJan Glauber }
151828b35f6SJan Glauber 
152d0b08853SJan Glauber /* Modify PCI: Set PCI function measurement parameters */
153d0b08853SJan Glauber int zpci_fmb_enable_device(struct zpci_dev *zdev)
154d0b08853SJan Glauber {
1554e5bd780SSebastian Ott 	u64 req = ZPCI_CREATE_REQ(zdev->fh, 0, ZPCI_MOD_FC_SET_MEASURE);
1564e5bd780SSebastian Ott 	struct zpci_fib fib = {0};
1574e5bd780SSebastian Ott 	u8 cc, status;
158d0b08853SJan Glauber 
1590b7589ecSSebastian Ott 	if (zdev->fmb || sizeof(*zdev->fmb) < zdev->fmb_length)
160d0b08853SJan Glauber 		return -EINVAL;
161d0b08853SJan Glauber 
16208b42124SWei Yongjun 	zdev->fmb = kmem_cache_zalloc(zdev_fmb_cache, GFP_KERNEL);
163d0b08853SJan Glauber 	if (!zdev->fmb)
164d0b08853SJan Glauber 		return -ENOMEM;
165d0b08853SJan Glauber 	WARN_ON((u64) zdev->fmb & 0xf);
166d0b08853SJan Glauber 
1676001018aSSebastian Ott 	/* reset software counters */
1686001018aSSebastian Ott 	atomic64_set(&zdev->allocated_pages, 0);
1696001018aSSebastian Ott 	atomic64_set(&zdev->mapped_pages, 0);
1706001018aSSebastian Ott 	atomic64_set(&zdev->unmapped_pages, 0);
1716001018aSSebastian Ott 
1724e5bd780SSebastian Ott 	fib.fmb_addr = virt_to_phys(zdev->fmb);
173c68468edSMatthew Rosato 	fib.gd = zdev->gisa;
1744e5bd780SSebastian Ott 	cc = zpci_mod_fc(req, &fib, &status);
1754e5bd780SSebastian Ott 	if (cc) {
1764e5bd780SSebastian Ott 		kmem_cache_free(zdev_fmb_cache, zdev->fmb);
1774e5bd780SSebastian Ott 		zdev->fmb = NULL;
1784e5bd780SSebastian Ott 	}
1794e5bd780SSebastian Ott 	return cc ? -EIO : 0;
180d0b08853SJan Glauber }
181d0b08853SJan Glauber 
182d0b08853SJan Glauber /* Modify PCI: Disable PCI function measurement */
183d0b08853SJan Glauber int zpci_fmb_disable_device(struct zpci_dev *zdev)
184d0b08853SJan Glauber {
1854e5bd780SSebastian Ott 	u64 req = ZPCI_CREATE_REQ(zdev->fh, 0, ZPCI_MOD_FC_SET_MEASURE);
1864e5bd780SSebastian Ott 	struct zpci_fib fib = {0};
1874e5bd780SSebastian Ott 	u8 cc, status;
188d0b08853SJan Glauber 
189d0b08853SJan Glauber 	if (!zdev->fmb)
190d0b08853SJan Glauber 		return -EINVAL;
191d0b08853SJan Glauber 
192c68468edSMatthew Rosato 	fib.gd = zdev->gisa;
193c68468edSMatthew Rosato 
194d0b08853SJan Glauber 	/* Function measurement is disabled if fmb address is zero */
1954e5bd780SSebastian Ott 	cc = zpci_mod_fc(req, &fib, &status);
1964e5bd780SSebastian Ott 	if (cc == 3) /* Function already gone. */
1974e5bd780SSebastian Ott 		cc = 0;
198d0b08853SJan Glauber 
1994e5bd780SSebastian Ott 	if (!cc) {
200d0b08853SJan Glauber 		kmem_cache_free(zdev_fmb_cache, zdev->fmb);
201d0b08853SJan Glauber 		zdev->fmb = NULL;
2024e5bd780SSebastian Ott 	}
2034e5bd780SSebastian Ott 	return cc ? -EIO : 0;
204d0b08853SJan Glauber }
205d0b08853SJan Glauber 
206cd248341SJan Glauber static int zpci_cfg_load(struct zpci_dev *zdev, int offset, u32 *val, u8 len)
207cd248341SJan Glauber {
208cd248341SJan Glauber 	u64 req = ZPCI_CREATE_REQ(zdev->fh, ZPCI_PCIAS_CFGSPC, len);
209cd248341SJan Glauber 	u64 data;
210cd248341SJan Glauber 	int rc;
211cd248341SJan Glauber 
21281deca12SSebastian Ott 	rc = __zpci_load(&data, req, offset);
213b170bad4SSebastian Ott 	if (!rc) {
2145064cd35SSebastian Ott 		data = le64_to_cpu((__force __le64) data);
2155064cd35SSebastian Ott 		data >>= (8 - len) * 8;
216cd248341SJan Glauber 		*val = (u32) data;
217b170bad4SSebastian Ott 	} else
218cd248341SJan Glauber 		*val = 0xffffffff;
219cd248341SJan Glauber 	return rc;
220cd248341SJan Glauber }
221cd248341SJan Glauber 
222cd248341SJan Glauber static int zpci_cfg_store(struct zpci_dev *zdev, int offset, u32 val, u8 len)
223cd248341SJan Glauber {
224cd248341SJan Glauber 	u64 req = ZPCI_CREATE_REQ(zdev->fh, ZPCI_PCIAS_CFGSPC, len);
225cd248341SJan Glauber 	u64 data = val;
226cd248341SJan Glauber 	int rc;
227cd248341SJan Glauber 
2285064cd35SSebastian Ott 	data <<= (8 - len) * 8;
2295064cd35SSebastian Ott 	data = (__force u64) cpu_to_le64(data);
23081deca12SSebastian Ott 	rc = __zpci_store(data, req, offset);
231cd248341SJan Glauber 	return rc;
232cd248341SJan Glauber }
233cd248341SJan Glauber 
234cd248341SJan Glauber resource_size_t pcibios_align_resource(void *data, const struct resource *res,
235cd248341SJan Glauber 				       resource_size_t size,
236cd248341SJan Glauber 				       resource_size_t align)
237cd248341SJan Glauber {
238cd248341SJan Glauber 	return 0;
239cd248341SJan Glauber }
240cd248341SJan Glauber 
24187bc359bSJan Glauber /* combine single writes by using store-block insn */
24287bc359bSJan Glauber void __iowrite64_copy(void __iomem *to, const void *from, size_t count)
24387bc359bSJan Glauber {
24487bc359bSJan Glauber        zpci_memcpy_toio(to, from, count);
24587bc359bSJan Glauber }
24687bc359bSJan Glauber 
247b02002ccSNiklas Schnelle static void __iomem *__ioremap(phys_addr_t addr, size_t size, pgprot_t prot)
24871ba41c9SSebastian Ott {
249a999eb96SNiklas Schnelle 	unsigned long offset, vaddr;
25071ba41c9SSebastian Ott 	struct vm_struct *area;
251a999eb96SNiklas Schnelle 	phys_addr_t last_addr;
25271ba41c9SSebastian Ott 
253a999eb96SNiklas Schnelle 	last_addr = addr + size - 1;
254a999eb96SNiklas Schnelle 	if (!size || last_addr < addr)
25571ba41c9SSebastian Ott 		return NULL;
25671ba41c9SSebastian Ott 
25771ba41c9SSebastian Ott 	if (!static_branch_unlikely(&have_mio))
258a999eb96SNiklas Schnelle 		return (void __iomem *) addr;
25971ba41c9SSebastian Ott 
260a999eb96SNiklas Schnelle 	offset = addr & ~PAGE_MASK;
261a999eb96SNiklas Schnelle 	addr &= PAGE_MASK;
26271ba41c9SSebastian Ott 	size = PAGE_ALIGN(size + offset);
26371ba41c9SSebastian Ott 	area = get_vm_area(size, VM_IOREMAP);
26471ba41c9SSebastian Ott 	if (!area)
26571ba41c9SSebastian Ott 		return NULL;
26671ba41c9SSebastian Ott 
267a999eb96SNiklas Schnelle 	vaddr = (unsigned long) area->addr;
268b02002ccSNiklas Schnelle 	if (ioremap_page_range(vaddr, vaddr + size, addr, prot)) {
269a999eb96SNiklas Schnelle 		free_vm_area(area);
27071ba41c9SSebastian Ott 		return NULL;
27171ba41c9SSebastian Ott 	}
27271ba41c9SSebastian Ott 	return (void __iomem *) ((unsigned long) area->addr + offset);
27371ba41c9SSebastian Ott }
274b02002ccSNiklas Schnelle 
275b02002ccSNiklas Schnelle void __iomem *ioremap_prot(phys_addr_t addr, size_t size, unsigned long prot)
276b02002ccSNiklas Schnelle {
277b02002ccSNiklas Schnelle 	return __ioremap(addr, size, __pgprot(prot));
278b02002ccSNiklas Schnelle }
279b02002ccSNiklas Schnelle EXPORT_SYMBOL(ioremap_prot);
280b02002ccSNiklas Schnelle 
281b02002ccSNiklas Schnelle void __iomem *ioremap(phys_addr_t addr, size_t size)
282b02002ccSNiklas Schnelle {
283b02002ccSNiklas Schnelle 	return __ioremap(addr, size, PAGE_KERNEL);
284b02002ccSNiklas Schnelle }
28571ba41c9SSebastian Ott EXPORT_SYMBOL(ioremap);
28671ba41c9SSebastian Ott 
287b02002ccSNiklas Schnelle void __iomem *ioremap_wc(phys_addr_t addr, size_t size)
288b02002ccSNiklas Schnelle {
289b02002ccSNiklas Schnelle 	return __ioremap(addr, size, pgprot_writecombine(PAGE_KERNEL));
290b02002ccSNiklas Schnelle }
291b02002ccSNiklas Schnelle EXPORT_SYMBOL(ioremap_wc);
292b02002ccSNiklas Schnelle 
293b02002ccSNiklas Schnelle void __iomem *ioremap_wt(phys_addr_t addr, size_t size)
294b02002ccSNiklas Schnelle {
295b02002ccSNiklas Schnelle 	return __ioremap(addr, size, pgprot_writethrough(PAGE_KERNEL));
296b02002ccSNiklas Schnelle }
297b02002ccSNiklas Schnelle EXPORT_SYMBOL(ioremap_wt);
298b02002ccSNiklas Schnelle 
29971ba41c9SSebastian Ott void iounmap(volatile void __iomem *addr)
30071ba41c9SSebastian Ott {
30171ba41c9SSebastian Ott 	if (static_branch_likely(&have_mio))
30271ba41c9SSebastian Ott 		vunmap((__force void *) ((unsigned long) addr & PAGE_MASK));
30371ba41c9SSebastian Ott }
30471ba41c9SSebastian Ott EXPORT_SYMBOL(iounmap);
30571ba41c9SSebastian Ott 
306cd248341SJan Glauber /* Create a virtual mapping cookie for a PCI BAR */
30771ba41c9SSebastian Ott static void __iomem *pci_iomap_range_fh(struct pci_dev *pdev, int bar,
30871ba41c9SSebastian Ott 					unsigned long offset, unsigned long max)
309cd248341SJan Glauber {
310198a5278SSebastian Ott 	struct zpci_dev *zdev =	to_zpci(pdev);
311cd248341SJan Glauber 	int idx;
312cd248341SJan Glauber 
313cd248341SJan Glauber 	idx = zdev->bars[bar].map_idx;
314cd248341SJan Glauber 	spin_lock(&zpci_iomap_lock);
315f5e44f82SSebastian Ott 	/* Detect overrun */
316f5e44f82SSebastian Ott 	WARN_ON(!++zpci_iomap_start[idx].count);
317cd248341SJan Glauber 	zpci_iomap_start[idx].fh = zdev->fh;
318cd248341SJan Glauber 	zpci_iomap_start[idx].bar = bar;
319cd248341SJan Glauber 	spin_unlock(&zpci_iomap_lock);
320cd248341SJan Glauber 
3219e00caaeSSebastian Ott 	return (void __iomem *) ZPCI_ADDR(idx) + offset;
322cd248341SJan Glauber }
32371ba41c9SSebastian Ott 
32471ba41c9SSebastian Ott static void __iomem *pci_iomap_range_mio(struct pci_dev *pdev, int bar,
32571ba41c9SSebastian Ott 					 unsigned long offset,
32671ba41c9SSebastian Ott 					 unsigned long max)
32771ba41c9SSebastian Ott {
32871ba41c9SSebastian Ott 	unsigned long barsize = pci_resource_len(pdev, bar);
32971ba41c9SSebastian Ott 	struct zpci_dev *zdev = to_zpci(pdev);
33071ba41c9SSebastian Ott 	void __iomem *iova;
33171ba41c9SSebastian Ott 
33271ba41c9SSebastian Ott 	iova = ioremap((unsigned long) zdev->bars[bar].mio_wt, barsize);
33371ba41c9SSebastian Ott 	return iova ? iova + offset : iova;
33471ba41c9SSebastian Ott }
33571ba41c9SSebastian Ott 
33671ba41c9SSebastian Ott void __iomem *pci_iomap_range(struct pci_dev *pdev, int bar,
33771ba41c9SSebastian Ott 			      unsigned long offset, unsigned long max)
33871ba41c9SSebastian Ott {
339c9c13ba4SDenis Efremov 	if (bar >= PCI_STD_NUM_BARS || !pci_resource_len(pdev, bar))
34071ba41c9SSebastian Ott 		return NULL;
34171ba41c9SSebastian Ott 
34271ba41c9SSebastian Ott 	if (static_branch_likely(&have_mio))
34371ba41c9SSebastian Ott 		return pci_iomap_range_mio(pdev, bar, offset, max);
34471ba41c9SSebastian Ott 	else
34571ba41c9SSebastian Ott 		return pci_iomap_range_fh(pdev, bar, offset, max);
34671ba41c9SSebastian Ott }
347d9426083SSebastian Ott EXPORT_SYMBOL(pci_iomap_range);
3488cfc99b5SMichael S. Tsirkin 
3498cfc99b5SMichael S. Tsirkin void __iomem *pci_iomap(struct pci_dev *dev, int bar, unsigned long maxlen)
3508cfc99b5SMichael S. Tsirkin {
3518cfc99b5SMichael S. Tsirkin 	return pci_iomap_range(dev, bar, 0, maxlen);
3528cfc99b5SMichael S. Tsirkin }
3538cfc99b5SMichael S. Tsirkin EXPORT_SYMBOL(pci_iomap);
354cd248341SJan Glauber 
35571ba41c9SSebastian Ott static void __iomem *pci_iomap_wc_range_mio(struct pci_dev *pdev, int bar,
35671ba41c9SSebastian Ott 					    unsigned long offset, unsigned long max)
35771ba41c9SSebastian Ott {
35871ba41c9SSebastian Ott 	unsigned long barsize = pci_resource_len(pdev, bar);
35971ba41c9SSebastian Ott 	struct zpci_dev *zdev = to_zpci(pdev);
36071ba41c9SSebastian Ott 	void __iomem *iova;
36171ba41c9SSebastian Ott 
36271ba41c9SSebastian Ott 	iova = ioremap((unsigned long) zdev->bars[bar].mio_wb, barsize);
36371ba41c9SSebastian Ott 	return iova ? iova + offset : iova;
36471ba41c9SSebastian Ott }
36571ba41c9SSebastian Ott 
36671ba41c9SSebastian Ott void __iomem *pci_iomap_wc_range(struct pci_dev *pdev, int bar,
36771ba41c9SSebastian Ott 				 unsigned long offset, unsigned long max)
36871ba41c9SSebastian Ott {
369c9c13ba4SDenis Efremov 	if (bar >= PCI_STD_NUM_BARS || !pci_resource_len(pdev, bar))
37071ba41c9SSebastian Ott 		return NULL;
37171ba41c9SSebastian Ott 
37271ba41c9SSebastian Ott 	if (static_branch_likely(&have_mio))
37371ba41c9SSebastian Ott 		return pci_iomap_wc_range_mio(pdev, bar, offset, max);
37471ba41c9SSebastian Ott 	else
37571ba41c9SSebastian Ott 		return pci_iomap_range_fh(pdev, bar, offset, max);
37671ba41c9SSebastian Ott }
37771ba41c9SSebastian Ott EXPORT_SYMBOL(pci_iomap_wc_range);
37871ba41c9SSebastian Ott 
37971ba41c9SSebastian Ott void __iomem *pci_iomap_wc(struct pci_dev *dev, int bar, unsigned long maxlen)
38071ba41c9SSebastian Ott {
38171ba41c9SSebastian Ott 	return pci_iomap_wc_range(dev, bar, 0, maxlen);
38271ba41c9SSebastian Ott }
38371ba41c9SSebastian Ott EXPORT_SYMBOL(pci_iomap_wc);
38471ba41c9SSebastian Ott 
38571ba41c9SSebastian Ott static void pci_iounmap_fh(struct pci_dev *pdev, void __iomem *addr)
386cd248341SJan Glauber {
3879e00caaeSSebastian Ott 	unsigned int idx = ZPCI_IDX(addr);
388cd248341SJan Glauber 
389cd248341SJan Glauber 	spin_lock(&zpci_iomap_lock);
3908cfc99b5SMichael S. Tsirkin 	/* Detect underrun */
391f5e44f82SSebastian Ott 	WARN_ON(!zpci_iomap_start[idx].count);
3928cfc99b5SMichael S. Tsirkin 	if (!--zpci_iomap_start[idx].count) {
393cd248341SJan Glauber 		zpci_iomap_start[idx].fh = 0;
394cd248341SJan Glauber 		zpci_iomap_start[idx].bar = 0;
3958cfc99b5SMichael S. Tsirkin 	}
396cd248341SJan Glauber 	spin_unlock(&zpci_iomap_lock);
397cd248341SJan Glauber }
39871ba41c9SSebastian Ott 
39971ba41c9SSebastian Ott static void pci_iounmap_mio(struct pci_dev *pdev, void __iomem *addr)
40071ba41c9SSebastian Ott {
40171ba41c9SSebastian Ott 	iounmap(addr);
40271ba41c9SSebastian Ott }
40371ba41c9SSebastian Ott 
40471ba41c9SSebastian Ott void pci_iounmap(struct pci_dev *pdev, void __iomem *addr)
40571ba41c9SSebastian Ott {
40671ba41c9SSebastian Ott 	if (static_branch_likely(&have_mio))
40771ba41c9SSebastian Ott 		pci_iounmap_mio(pdev, addr);
40871ba41c9SSebastian Ott 	else
40971ba41c9SSebastian Ott 		pci_iounmap_fh(pdev, addr);
41071ba41c9SSebastian Ott }
411d9426083SSebastian Ott EXPORT_SYMBOL(pci_iounmap);
412cd248341SJan Glauber 
413cd248341SJan Glauber static int pci_read(struct pci_bus *bus, unsigned int devfn, int where,
414cd248341SJan Glauber 		    int size, u32 *val)
415cd248341SJan Glauber {
4167dcfe50fSNiklas Schnelle 	struct zpci_dev *zdev = zdev_from_bus(bus, devfn);
417cd248341SJan Glauber 
41844510d6fSPierre Morel 	return (zdev) ? zpci_cfg_load(zdev, where, val, size) : -ENODEV;
419cd248341SJan Glauber }
420cd248341SJan Glauber 
421cd248341SJan Glauber static int pci_write(struct pci_bus *bus, unsigned int devfn, int where,
422cd248341SJan Glauber 		     int size, u32 val)
423cd248341SJan Glauber {
4247dcfe50fSNiklas Schnelle 	struct zpci_dev *zdev = zdev_from_bus(bus, devfn);
425cd248341SJan Glauber 
42644510d6fSPierre Morel 	return (zdev) ? zpci_cfg_store(zdev, where, val, size) : -ENODEV;
427cd248341SJan Glauber }
428cd248341SJan Glauber 
429cd248341SJan Glauber static struct pci_ops pci_root_ops = {
430cd248341SJan Glauber 	.read = pci_read,
431cd248341SJan Glauber 	.write = pci_write,
432cd248341SJan Glauber };
433cd248341SJan Glauber 
4341803ba2dSSebastian Ott static void zpci_map_resources(struct pci_dev *pdev)
435cd248341SJan Glauber {
43671ba41c9SSebastian Ott 	struct zpci_dev *zdev = to_zpci(pdev);
437cd248341SJan Glauber 	resource_size_t len;
438cd248341SJan Glauber 	int i;
439cd248341SJan Glauber 
440c9c13ba4SDenis Efremov 	for (i = 0; i < PCI_STD_NUM_BARS; i++) {
441cd248341SJan Glauber 		len = pci_resource_len(pdev, i);
442cd248341SJan Glauber 		if (!len)
443cd248341SJan Glauber 			continue;
44471ba41c9SSebastian Ott 
445c7ff0e91SSebastian Ott 		if (zpci_use_mio(zdev))
44671ba41c9SSebastian Ott 			pdev->resource[i].start =
447df057c91SNiklas Schnelle 				(resource_size_t __force) zdev->bars[i].mio_wt;
44871ba41c9SSebastian Ott 		else
449c7ff0e91SSebastian Ott 			pdev->resource[i].start = (resource_size_t __force)
450c7ff0e91SSebastian Ott 				pci_iomap_range_fh(pdev, i, 0, 0);
451cd248341SJan Glauber 		pdev->resource[i].end = pdev->resource[i].start + len - 1;
452cd248341SJan Glauber 	}
453cfbb4a7aSSebastian Ott 
454abb95b75SNiklas Schnelle 	zpci_iov_map_resources(pdev);
455944239c5SSebastian Ott }
456944239c5SSebastian Ott 
4571803ba2dSSebastian Ott static void zpci_unmap_resources(struct pci_dev *pdev)
458944239c5SSebastian Ott {
459c7ff0e91SSebastian Ott 	struct zpci_dev *zdev = to_zpci(pdev);
460944239c5SSebastian Ott 	resource_size_t len;
461944239c5SSebastian Ott 	int i;
462944239c5SSebastian Ott 
463c7ff0e91SSebastian Ott 	if (zpci_use_mio(zdev))
46471ba41c9SSebastian Ott 		return;
46571ba41c9SSebastian Ott 
466c9c13ba4SDenis Efremov 	for (i = 0; i < PCI_STD_NUM_BARS; i++) {
467944239c5SSebastian Ott 		len = pci_resource_len(pdev, i);
468944239c5SSebastian Ott 		if (!len)
469944239c5SSebastian Ott 			continue;
470c7ff0e91SSebastian Ott 		pci_iounmap_fh(pdev, (void __iomem __force *)
4715b9f2081SMartin Schwidefsky 			       pdev->resource[i].start);
472944239c5SSebastian Ott 	}
473944239c5SSebastian Ott }
474cd248341SJan Glauber 
475cd248341SJan Glauber static int zpci_alloc_iomap(struct zpci_dev *zdev)
476cd248341SJan Glauber {
477bf19c94dSSebastian Ott 	unsigned long entry;
478cd248341SJan Glauber 
479cd248341SJan Glauber 	spin_lock(&zpci_iomap_lock);
480c506fff3SSebastian Ott 	entry = find_first_zero_bit(zpci_iomap_bitmap, ZPCI_IOMAP_ENTRIES);
481c506fff3SSebastian Ott 	if (entry == ZPCI_IOMAP_ENTRIES) {
482cd248341SJan Glauber 		spin_unlock(&zpci_iomap_lock);
483cd248341SJan Glauber 		return -ENOSPC;
484cd248341SJan Glauber 	}
485c506fff3SSebastian Ott 	set_bit(entry, zpci_iomap_bitmap);
486cd248341SJan Glauber 	spin_unlock(&zpci_iomap_lock);
487cd248341SJan Glauber 	return entry;
488cd248341SJan Glauber }
489cd248341SJan Glauber 
490cd248341SJan Glauber static void zpci_free_iomap(struct zpci_dev *zdev, int entry)
491cd248341SJan Glauber {
492cd248341SJan Glauber 	spin_lock(&zpci_iomap_lock);
493cd248341SJan Glauber 	memset(&zpci_iomap_start[entry], 0, sizeof(struct zpci_iomap_entry));
494c506fff3SSebastian Ott 	clear_bit(entry, zpci_iomap_bitmap);
495cd248341SJan Glauber 	spin_unlock(&zpci_iomap_lock);
496cd248341SJan Glauber }
497cd248341SJan Glauber 
4984fe20497SNiklas Schnelle static void zpci_do_update_iomap_fh(struct zpci_dev *zdev, u32 fh)
4994fe20497SNiklas Schnelle {
5004fe20497SNiklas Schnelle 	int bar, idx;
5014fe20497SNiklas Schnelle 
5024fe20497SNiklas Schnelle 	spin_lock(&zpci_iomap_lock);
5034fe20497SNiklas Schnelle 	for (bar = 0; bar < PCI_STD_NUM_BARS; bar++) {
5044fe20497SNiklas Schnelle 		if (!zdev->bars[bar].size)
5054fe20497SNiklas Schnelle 			continue;
5064fe20497SNiklas Schnelle 		idx = zdev->bars[bar].map_idx;
5074fe20497SNiklas Schnelle 		if (!zpci_iomap_start[idx].count)
5084fe20497SNiklas Schnelle 			continue;
5094fe20497SNiklas Schnelle 		WRITE_ONCE(zpci_iomap_start[idx].fh, zdev->fh);
5104fe20497SNiklas Schnelle 	}
5114fe20497SNiklas Schnelle 	spin_unlock(&zpci_iomap_lock);
5124fe20497SNiklas Schnelle }
5134fe20497SNiklas Schnelle 
5144fe20497SNiklas Schnelle void zpci_update_fh(struct zpci_dev *zdev, u32 fh)
5154fe20497SNiklas Schnelle {
5164fe20497SNiklas Schnelle 	if (!fh || zdev->fh == fh)
5174fe20497SNiklas Schnelle 		return;
5184fe20497SNiklas Schnelle 
5194fe20497SNiklas Schnelle 	zdev->fh = fh;
5204fe20497SNiklas Schnelle 	if (zpci_use_mio(zdev))
5214fe20497SNiklas Schnelle 		return;
5224fe20497SNiklas Schnelle 	if (zdev->has_resources && zdev_enabled(zdev))
5234fe20497SNiklas Schnelle 		zpci_do_update_iomap_fh(zdev, fh);
5244fe20497SNiklas Schnelle }
5254fe20497SNiklas Schnelle 
5267a572a3aSSebastian Ott static struct resource *__alloc_res(struct zpci_dev *zdev, unsigned long start,
5277a572a3aSSebastian Ott 				    unsigned long size, unsigned long flags)
5287a572a3aSSebastian Ott {
5297a572a3aSSebastian Ott 	struct resource *r;
5307a572a3aSSebastian Ott 
5317a572a3aSSebastian Ott 	r = kzalloc(sizeof(*r), GFP_KERNEL);
5327a572a3aSSebastian Ott 	if (!r)
5337a572a3aSSebastian Ott 		return NULL;
5347a572a3aSSebastian Ott 
5357a572a3aSSebastian Ott 	r->start = start;
5367a572a3aSSebastian Ott 	r->end = r->start + size - 1;
5377a572a3aSSebastian Ott 	r->flags = flags;
5387a572a3aSSebastian Ott 	r->name = zdev->res_name;
5397a572a3aSSebastian Ott 
5407a572a3aSSebastian Ott 	if (request_resource(&iomem_resource, r)) {
5417a572a3aSSebastian Ott 		kfree(r);
5427a572a3aSSebastian Ott 		return NULL;
5437a572a3aSSebastian Ott 	}
5447a572a3aSSebastian Ott 	return r;
5457a572a3aSSebastian Ott }
5467a572a3aSSebastian Ott 
54705bc1be6SPierre Morel int zpci_setup_bus_resources(struct zpci_dev *zdev,
5487a572a3aSSebastian Ott 			     struct list_head *resources)
5497a572a3aSSebastian Ott {
5507a572a3aSSebastian Ott 	unsigned long addr, size, flags;
5517a572a3aSSebastian Ott 	struct resource *res;
5527a572a3aSSebastian Ott 	int i, entry;
5537a572a3aSSebastian Ott 
5547a572a3aSSebastian Ott 	snprintf(zdev->res_name, sizeof(zdev->res_name),
55505bc1be6SPierre Morel 		 "PCI Bus %04x:%02x", zdev->uid, ZPCI_BUS_NR);
5567a572a3aSSebastian Ott 
557c9c13ba4SDenis Efremov 	for (i = 0; i < PCI_STD_NUM_BARS; i++) {
5587a572a3aSSebastian Ott 		if (!zdev->bars[i].size)
5597a572a3aSSebastian Ott 			continue;
5607a572a3aSSebastian Ott 		entry = zpci_alloc_iomap(zdev);
5617a572a3aSSebastian Ott 		if (entry < 0)
5627a572a3aSSebastian Ott 			return entry;
5637a572a3aSSebastian Ott 		zdev->bars[i].map_idx = entry;
5647a572a3aSSebastian Ott 
5657a572a3aSSebastian Ott 		/* only MMIO is supported */
5667a572a3aSSebastian Ott 		flags = IORESOURCE_MEM;
5677a572a3aSSebastian Ott 		if (zdev->bars[i].val & 8)
5687a572a3aSSebastian Ott 			flags |= IORESOURCE_PREFETCH;
5697a572a3aSSebastian Ott 		if (zdev->bars[i].val & 4)
5707a572a3aSSebastian Ott 			flags |= IORESOURCE_MEM_64;
5717a572a3aSSebastian Ott 
572c7ff0e91SSebastian Ott 		if (zpci_use_mio(zdev))
573df057c91SNiklas Schnelle 			addr = (unsigned long) zdev->bars[i].mio_wt;
574dcd33b23SSebastian Ott 		else
5759e00caaeSSebastian Ott 			addr = ZPCI_ADDR(entry);
5767a572a3aSSebastian Ott 		size = 1UL << zdev->bars[i].size;
5777a572a3aSSebastian Ott 
5787a572a3aSSebastian Ott 		res = __alloc_res(zdev, addr, size, flags);
5797a572a3aSSebastian Ott 		if (!res) {
5807a572a3aSSebastian Ott 			zpci_free_iomap(zdev, entry);
5817a572a3aSSebastian Ott 			return -ENOMEM;
5827a572a3aSSebastian Ott 		}
5837a572a3aSSebastian Ott 		zdev->bars[i].res = res;
5847a572a3aSSebastian Ott 		pci_add_resource(resources, res);
5857a572a3aSSebastian Ott 	}
586a50297cfSNiklas Schnelle 	zdev->has_resources = 1;
5877a572a3aSSebastian Ott 
5887a572a3aSSebastian Ott 	return 0;
5897a572a3aSSebastian Ott }
5907a572a3aSSebastian Ott 
5917a572a3aSSebastian Ott static void zpci_cleanup_bus_resources(struct zpci_dev *zdev)
5927a572a3aSSebastian Ott {
5937a572a3aSSebastian Ott 	int i;
5947a572a3aSSebastian Ott 
595c9c13ba4SDenis Efremov 	for (i = 0; i < PCI_STD_NUM_BARS; i++) {
5962b1df724SSebastian Ott 		if (!zdev->bars[i].size || !zdev->bars[i].res)
5977a572a3aSSebastian Ott 			continue;
5987a572a3aSSebastian Ott 
5997a572a3aSSebastian Ott 		zpci_free_iomap(zdev, zdev->bars[i].map_idx);
6007a572a3aSSebastian Ott 		release_resource(zdev->bars[i].res);
6017a572a3aSSebastian Ott 		kfree(zdev->bars[i].res);
6027a572a3aSSebastian Ott 	}
603a50297cfSNiklas Schnelle 	zdev->has_resources = 0;
6047a572a3aSSebastian Ott }
6057a572a3aSSebastian Ott 
60606dc660eSOliver O'Halloran int pcibios_device_add(struct pci_dev *pdev)
607af0a8a84SSebastian Ott {
6082a671f77SNiklas Schnelle 	struct zpci_dev *zdev = to_zpci(pdev);
609cb809182SSebastian Ott 	struct resource *res;
610cb809182SSebastian Ott 	int i;
611cb809182SSebastian Ott 
6122a671f77SNiklas Schnelle 	/* The pdev has a reference to the zdev via its bus */
6132a671f77SNiklas Schnelle 	zpci_zdev_get(zdev);
6147dc20ab1SSebastian Ott 	if (pdev->is_physfn)
6157dc20ab1SSebastian Ott 		pdev->no_vf_scan = 1;
6167dc20ab1SSebastian Ott 
617ef4858c6SSebastian Ott 	pdev->dev.groups = zpci_attr_groups;
6185657933dSBart Van Assche 	pdev->dev.dma_ops = &s390_pci_dma_ops;
6191803ba2dSSebastian Ott 	zpci_map_resources(pdev);
620cb809182SSebastian Ott 
621c9c13ba4SDenis Efremov 	for (i = 0; i < PCI_STD_NUM_BARS; i++) {
622cb809182SSebastian Ott 		res = &pdev->resource[i];
623cb809182SSebastian Ott 		if (res->parent || !res->flags)
624cb809182SSebastian Ott 			continue;
625cb809182SSebastian Ott 		pci_claim_resource(pdev, i);
626cb809182SSebastian Ott 	}
627cb809182SSebastian Ott 
628cb809182SSebastian Ott 	return 0;
629cb809182SSebastian Ott }
630cb809182SSebastian Ott 
6311803ba2dSSebastian Ott void pcibios_release_device(struct pci_dev *pdev)
6321803ba2dSSebastian Ott {
6332a671f77SNiklas Schnelle 	struct zpci_dev *zdev = to_zpci(pdev);
6342a671f77SNiklas Schnelle 
6351803ba2dSSebastian Ott 	zpci_unmap_resources(pdev);
6362a671f77SNiklas Schnelle 	zpci_zdev_put(zdev);
6371803ba2dSSebastian Ott }
6381803ba2dSSebastian Ott 
639cb809182SSebastian Ott int pcibios_enable_device(struct pci_dev *pdev, int mask)
640cb809182SSebastian Ott {
641198a5278SSebastian Ott 	struct zpci_dev *zdev = to_zpci(pdev);
642af0a8a84SSebastian Ott 
6439a99649fSSebastian Ott 	zpci_debug_init_device(zdev, dev_name(&pdev->dev));
644af0a8a84SSebastian Ott 	zpci_fmb_enable_device(zdev);
645af0a8a84SSebastian Ott 
646d7533232SBjorn Helgaas 	return pci_enable_resources(pdev, mask);
647af0a8a84SSebastian Ott }
648af0a8a84SSebastian Ott 
649cb809182SSebastian Ott void pcibios_disable_device(struct pci_dev *pdev)
650944239c5SSebastian Ott {
651198a5278SSebastian Ott 	struct zpci_dev *zdev = to_zpci(pdev);
652944239c5SSebastian Ott 
653944239c5SSebastian Ott 	zpci_fmb_disable_device(zdev);
654944239c5SSebastian Ott 	zpci_debug_exit_device(zdev);
655944239c5SSebastian Ott }
656944239c5SSebastian Ott 
65705bc1be6SPierre Morel static int __zpci_register_domain(int domain)
658cd248341SJan Glauber {
659969ae01bSNiklas Schnelle 	spin_lock(&zpci_domain_lock);
66005bc1be6SPierre Morel 	if (test_bit(domain, zpci_domain)) {
661969ae01bSNiklas Schnelle 		spin_unlock(&zpci_domain_lock);
66205bc1be6SPierre Morel 		pr_err("Domain %04x is already assigned\n", domain);
663312e8462SSebastian Ott 		return -EEXIST;
664312e8462SSebastian Ott 	}
66505bc1be6SPierre Morel 	set_bit(domain, zpci_domain);
666312e8462SSebastian Ott 	spin_unlock(&zpci_domain_lock);
66705bc1be6SPierre Morel 	return domain;
6685c5afd02SSebastian Ott }
66905bc1be6SPierre Morel 
67005bc1be6SPierre Morel static int __zpci_alloc_domain(void)
67105bc1be6SPierre Morel {
67205bc1be6SPierre Morel 	int domain;
67305bc1be6SPierre Morel 
67405bc1be6SPierre Morel 	spin_lock(&zpci_domain_lock);
675969ae01bSNiklas Schnelle 	/*
676969ae01bSNiklas Schnelle 	 * We can always auto allocate domains below ZPCI_NR_DEVICES.
677969ae01bSNiklas Schnelle 	 * There is either a free domain or we have reached the maximum in
678969ae01bSNiklas Schnelle 	 * which case we would have bailed earlier.
679969ae01bSNiklas Schnelle 	 */
68005bc1be6SPierre Morel 	domain = find_first_zero_bit(zpci_domain, ZPCI_NR_DEVICES);
68105bc1be6SPierre Morel 	set_bit(domain, zpci_domain);
682cd248341SJan Glauber 	spin_unlock(&zpci_domain_lock);
68305bc1be6SPierre Morel 	return domain;
684cd248341SJan Glauber }
685cd248341SJan Glauber 
68605bc1be6SPierre Morel int zpci_alloc_domain(int domain)
68705bc1be6SPierre Morel {
68805bc1be6SPierre Morel 	if (zpci_unique_uid) {
68905bc1be6SPierre Morel 		if (domain)
69005bc1be6SPierre Morel 			return __zpci_register_domain(domain);
69105bc1be6SPierre Morel 		pr_warn("UID checking was active but no UID is provided: switching to automatic domain allocation\n");
69205bc1be6SPierre Morel 		update_uid_checking(false);
69305bc1be6SPierre Morel 	}
69405bc1be6SPierre Morel 	return __zpci_alloc_domain();
69505bc1be6SPierre Morel }
69605bc1be6SPierre Morel 
69705bc1be6SPierre Morel void zpci_free_domain(int domain)
698cd248341SJan Glauber {
699cd248341SJan Glauber 	spin_lock(&zpci_domain_lock);
70005bc1be6SPierre Morel 	clear_bit(domain, zpci_domain);
701cd248341SJan Glauber 	spin_unlock(&zpci_domain_lock);
702cd248341SJan Glauber }
703cd248341SJan Glauber 
7047d594322SSebastian Ott 
705a755a45dSJan Glauber int zpci_enable_device(struct zpci_dev *zdev)
706a755a45dSJan Glauber {
707cc049eecSNiklas Schnelle 	u32 fh = zdev->fh;
7081f3f7681SNiklas Schnelle 	int rc = 0;
709a755a45dSJan Glauber 
7101f3f7681SNiklas Schnelle 	if (clp_enable_fh(zdev, &fh, ZPCI_NR_DMA_SPACES))
711f7addcddSNiklas Schnelle 		rc = -EIO;
7121f3f7681SNiklas Schnelle 	else
7134fe20497SNiklas Schnelle 		zpci_update_fh(zdev, fh);
714a755a45dSJan Glauber 	return rc;
715a755a45dSJan Glauber }
716*09340b2fSMatthew Rosato EXPORT_SYMBOL_GPL(zpci_enable_device);
717a755a45dSJan Glauber 
718cb65a669SSebastian Ott int zpci_disable_device(struct zpci_dev *zdev)
719cb65a669SSebastian Ott {
720cc049eecSNiklas Schnelle 	u32 fh = zdev->fh;
7218256addaSNiklas Schnelle 	int cc, rc = 0;
7228256addaSNiklas Schnelle 
723cc049eecSNiklas Schnelle 	cc = clp_disable_fh(zdev, &fh);
724cc049eecSNiklas Schnelle 	if (!cc) {
7254fe20497SNiklas Schnelle 		zpci_update_fh(zdev, fh);
726cc049eecSNiklas Schnelle 	} else if (cc == CLP_RC_SETPCIFN_ALRDY) {
7278256addaSNiklas Schnelle 		pr_info("Disabling PCI function %08x had no effect as it was already disabled\n",
7288256addaSNiklas Schnelle 			zdev->fid);
7298256addaSNiklas Schnelle 		/* Function is already disabled - update handle */
730cc049eecSNiklas Schnelle 		rc = clp_refresh_fh(zdev->fid, &fh);
731cc049eecSNiklas Schnelle 		if (!rc) {
7324fe20497SNiklas Schnelle 			zpci_update_fh(zdev, fh);
7338256addaSNiklas Schnelle 			rc = -EINVAL;
734cc049eecSNiklas Schnelle 		}
735cc049eecSNiklas Schnelle 	} else {
7368256addaSNiklas Schnelle 		rc = -EIO;
7378256addaSNiklas Schnelle 	}
7388256addaSNiklas Schnelle 	return rc;
739cb65a669SSebastian Ott }
740*09340b2fSMatthew Rosato EXPORT_SYMBOL_GPL(zpci_disable_device);
741cb65a669SSebastian Ott 
742ba764dd7SNiklas Schnelle /**
743da995d53SNiklas Schnelle  * zpci_hot_reset_device - perform a reset of the given zPCI function
744da995d53SNiklas Schnelle  * @zdev: the slot which should be reset
745da995d53SNiklas Schnelle  *
746da995d53SNiklas Schnelle  * Performs a low level reset of the zPCI function. The reset is low level in
747da995d53SNiklas Schnelle  * the sense that the zPCI function can be reset without detaching it from the
748da995d53SNiklas Schnelle  * common PCI subsystem. The reset may be performed while under control of
749da995d53SNiklas Schnelle  * either DMA or IOMMU APIs in which case the existing DMA/IOMMU translation
750da995d53SNiklas Schnelle  * table is reinstated at the end of the reset.
751da995d53SNiklas Schnelle  *
752da995d53SNiklas Schnelle  * After the reset the functions internal state is reset to an initial state
753da995d53SNiklas Schnelle  * equivalent to its state during boot when first probing a driver.
754da995d53SNiklas Schnelle  * Consequently after reset the PCI function requires re-initialization via the
755da995d53SNiklas Schnelle  * common PCI code including re-enabling IRQs via pci_alloc_irq_vectors()
756da995d53SNiklas Schnelle  * and enabling the function via e.g.pci_enablde_device_flags().The caller
757da995d53SNiklas Schnelle  * must guard against concurrent reset attempts.
758da995d53SNiklas Schnelle  *
759da995d53SNiklas Schnelle  * In most cases this function should not be called directly but through
760da995d53SNiklas Schnelle  * pci_reset_function() or pci_reset_bus() which handle the save/restore and
761da995d53SNiklas Schnelle  * locking.
762da995d53SNiklas Schnelle  *
763da995d53SNiklas Schnelle  * Return: 0 on success and an error value otherwise
764da995d53SNiklas Schnelle  */
765da995d53SNiklas Schnelle int zpci_hot_reset_device(struct zpci_dev *zdev)
766da995d53SNiklas Schnelle {
767da995d53SNiklas Schnelle 	int rc;
768da995d53SNiklas Schnelle 
769da995d53SNiklas Schnelle 	zpci_dbg(3, "rst fid:%x, fh:%x\n", zdev->fid, zdev->fh);
770da995d53SNiklas Schnelle 	if (zdev_enabled(zdev)) {
771da995d53SNiklas Schnelle 		/* Disables device access, DMAs and IRQs (reset state) */
772da995d53SNiklas Schnelle 		rc = zpci_disable_device(zdev);
773da995d53SNiklas Schnelle 		/*
774da995d53SNiklas Schnelle 		 * Due to a z/VM vs LPAR inconsistency in the error state the
775da995d53SNiklas Schnelle 		 * FH may indicate an enabled device but disable says the
776da995d53SNiklas Schnelle 		 * device is already disabled don't treat it as an error here.
777da995d53SNiklas Schnelle 		 */
778da995d53SNiklas Schnelle 		if (rc == -EINVAL)
779da995d53SNiklas Schnelle 			rc = 0;
780da995d53SNiklas Schnelle 		if (rc)
781da995d53SNiklas Schnelle 			return rc;
782da995d53SNiklas Schnelle 	}
783da995d53SNiklas Schnelle 
784da995d53SNiklas Schnelle 	rc = zpci_enable_device(zdev);
785da995d53SNiklas Schnelle 	if (rc)
786da995d53SNiklas Schnelle 		return rc;
787da995d53SNiklas Schnelle 
788da995d53SNiklas Schnelle 	if (zdev->dma_table)
789da995d53SNiklas Schnelle 		rc = zpci_register_ioat(zdev, 0, zdev->start_dma, zdev->end_dma,
790568de506SNiklas Schnelle 					virt_to_phys(zdev->dma_table));
791da995d53SNiklas Schnelle 	else
792da995d53SNiklas Schnelle 		rc = zpci_dma_init_device(zdev);
793da995d53SNiklas Schnelle 	if (rc) {
794da995d53SNiklas Schnelle 		zpci_disable_device(zdev);
795da995d53SNiklas Schnelle 		return rc;
796da995d53SNiklas Schnelle 	}
797da995d53SNiklas Schnelle 
798da995d53SNiklas Schnelle 	return 0;
799da995d53SNiklas Schnelle }
800da995d53SNiklas Schnelle 
801da995d53SNiklas Schnelle /**
802ba764dd7SNiklas Schnelle  * zpci_create_device() - Create a new zpci_dev and add it to the zbus
803ba764dd7SNiklas Schnelle  * @fid: Function ID of the device to be created
804ba764dd7SNiklas Schnelle  * @fh: Current Function Handle of the device to be created
805ba764dd7SNiklas Schnelle  * @state: Initial state after creation either Standby or Configured
806ba764dd7SNiklas Schnelle  *
807ba764dd7SNiklas Schnelle  * Creates a new zpci device and adds it to its, possibly newly created, zbus
808ba764dd7SNiklas Schnelle  * as well as zpci_list.
809ba764dd7SNiklas Schnelle  *
81014c87ba8SNiklas Schnelle  * Returns: the zdev on success or an error pointer otherwise
811ba764dd7SNiklas Schnelle  */
81214c87ba8SNiklas Schnelle struct zpci_dev *zpci_create_device(u32 fid, u32 fh, enum zpci_state state)
813cd248341SJan Glauber {
814ba764dd7SNiklas Schnelle 	struct zpci_dev *zdev;
815cd248341SJan Glauber 	int rc;
816cd248341SJan Glauber 
81752c79e63SNiklas Schnelle 	zpci_dbg(1, "add fid:%x, fh:%x, c:%d\n", fid, fh, state);
818ba764dd7SNiklas Schnelle 	zdev = kzalloc(sizeof(*zdev), GFP_KERNEL);
819ba764dd7SNiklas Schnelle 	if (!zdev)
82014c87ba8SNiklas Schnelle 		return ERR_PTR(-ENOMEM);
821ba764dd7SNiklas Schnelle 
822ba764dd7SNiklas Schnelle 	/* FID and Function Handle are the static/dynamic identifiers */
823ba764dd7SNiklas Schnelle 	zdev->fid = fid;
824ba764dd7SNiklas Schnelle 	zdev->fh = fh;
825ba764dd7SNiklas Schnelle 
826ba764dd7SNiklas Schnelle 	/* Query function properties and update zdev */
827ba764dd7SNiklas Schnelle 	rc = clp_query_pci_fn(zdev);
828ba764dd7SNiklas Schnelle 	if (rc)
829ba764dd7SNiklas Schnelle 		goto error;
830ba764dd7SNiklas Schnelle 	zdev->state =  state;
831ba764dd7SNiklas Schnelle 
83205bc1be6SPierre Morel 	kref_init(&zdev->kref);
833ba764dd7SNiklas Schnelle 	mutex_init(&zdev->lock);
834*09340b2fSMatthew Rosato 	mutex_init(&zdev->kzdev_lock);
835ba764dd7SNiklas Schnelle 
836ba764dd7SNiklas Schnelle 	rc = zpci_init_iommu(zdev);
837ba764dd7SNiklas Schnelle 	if (rc)
838ba764dd7SNiklas Schnelle 		goto error;
839ba764dd7SNiklas Schnelle 
840ba764dd7SNiklas Schnelle 	rc = zpci_bus_device_register(zdev, &pci_root_ops);
841ba764dd7SNiklas Schnelle 	if (rc)
842a50297cfSNiklas Schnelle 		goto error_destroy_iommu;
84305bc1be6SPierre Morel 
84405bc1be6SPierre Morel 	spin_lock(&zpci_list_lock);
84505bc1be6SPierre Morel 	list_add_tail(&zdev->entry, &zpci_list);
84605bc1be6SPierre Morel 	spin_unlock(&zpci_list_lock);
847cd248341SJan Glauber 
84814c87ba8SNiklas Schnelle 	return zdev;
849cd248341SJan Glauber 
850ba764dd7SNiklas Schnelle error_destroy_iommu:
851f42c2235SJoerg Roedel 	zpci_destroy_iommu(zdev);
852ba764dd7SNiklas Schnelle error:
853ba764dd7SNiklas Schnelle 	zpci_dbg(0, "add fid:%x, rc:%d\n", fid, rc);
854ba764dd7SNiklas Schnelle 	kfree(zdev);
85514c87ba8SNiklas Schnelle 	return ERR_PTR(rc);
856cd248341SJan Glauber }
857cd248341SJan Glauber 
858a46044a9SNiklas Schnelle bool zpci_is_device_configured(struct zpci_dev *zdev)
859a46044a9SNiklas Schnelle {
860a46044a9SNiklas Schnelle 	enum zpci_state state = zdev->state;
861a46044a9SNiklas Schnelle 
862a46044a9SNiklas Schnelle 	return state != ZPCI_FN_STATE_RESERVED &&
863a46044a9SNiklas Schnelle 		state != ZPCI_FN_STATE_STANDBY;
864a46044a9SNiklas Schnelle }
865a46044a9SNiklas Schnelle 
8662631f6b6SNiklas Schnelle /**
867a7f82c36SNiklas Schnelle  * zpci_scan_configured_device() - Scan a freshly configured zpci_dev
8682631f6b6SNiklas Schnelle  * @zdev: The zpci_dev to be configured
8692631f6b6SNiklas Schnelle  * @fh: The general function handle supplied by the platform
8702631f6b6SNiklas Schnelle  *
87161311e32SNiklas Schnelle  * Given a device in the configuration state Configured, enables, scans and
872a7f82c36SNiklas Schnelle  * adds it to the common code PCI subsystem if possible. If the PCI device is
873a7f82c36SNiklas Schnelle  * parked because we can not yet create a PCI bus because we have not seen
874a7f82c36SNiklas Schnelle  * function 0, it is ignored but will be scanned once function 0 appears.
875a7f82c36SNiklas Schnelle  * If any failure occurs, the zpci_dev is left disabled.
8762631f6b6SNiklas Schnelle  *
8772631f6b6SNiklas Schnelle  * Return: 0 on success, or an error code otherwise
8782631f6b6SNiklas Schnelle  */
879a7f82c36SNiklas Schnelle int zpci_scan_configured_device(struct zpci_dev *zdev, u32 fh)
8802631f6b6SNiklas Schnelle {
8812631f6b6SNiklas Schnelle 	int rc;
8822631f6b6SNiklas Schnelle 
8834fe20497SNiklas Schnelle 	zpci_update_fh(zdev, fh);
8842631f6b6SNiklas Schnelle 	/* the PCI function will be scanned once function 0 appears */
8852631f6b6SNiklas Schnelle 	if (!zdev->zbus->bus)
8862631f6b6SNiklas Schnelle 		return 0;
8872631f6b6SNiklas Schnelle 
888a50297cfSNiklas Schnelle 	/* For function 0 on a multi-function bus scan whole bus as we might
889a50297cfSNiklas Schnelle 	 * have to pick up existing functions waiting for it to allow creating
890a50297cfSNiklas Schnelle 	 * the PCI bus
891a50297cfSNiklas Schnelle 	 */
892a50297cfSNiklas Schnelle 	if (zdev->devfn == 0 && zdev->zbus->multifunction)
893a50297cfSNiklas Schnelle 		rc = zpci_bus_scan_bus(zdev->zbus);
894a50297cfSNiklas Schnelle 	else
895faf29a4dSNiklas Schnelle 		rc = zpci_bus_scan_device(zdev);
8962631f6b6SNiklas Schnelle 
8972631f6b6SNiklas Schnelle 	return rc;
8982631f6b6SNiklas Schnelle }
8992631f6b6SNiklas Schnelle 
9002631f6b6SNiklas Schnelle /**
9012631f6b6SNiklas Schnelle  * zpci_deconfigure_device() - Deconfigure a zpci_dev
9022631f6b6SNiklas Schnelle  * @zdev: The zpci_dev to configure
9032631f6b6SNiklas Schnelle  *
9042631f6b6SNiklas Schnelle  * Deconfigure a zPCI function that is currently configured and possibly known
9052631f6b6SNiklas Schnelle  * to the common code PCI subsystem.
9062631f6b6SNiklas Schnelle  * If any failure occurs the device is left as is.
9072631f6b6SNiklas Schnelle  *
9082631f6b6SNiklas Schnelle  * Return: 0 on success, or an error code otherwise
9092631f6b6SNiklas Schnelle  */
9102631f6b6SNiklas Schnelle int zpci_deconfigure_device(struct zpci_dev *zdev)
9112631f6b6SNiklas Schnelle {
9122631f6b6SNiklas Schnelle 	int rc;
9132631f6b6SNiklas Schnelle 
9142631f6b6SNiklas Schnelle 	if (zdev->zbus->bus)
91595b3a8b4SNiklas Schnelle 		zpci_bus_remove_device(zdev, false);
9162631f6b6SNiklas Schnelle 
9171f3f7681SNiklas Schnelle 	if (zdev->dma_table) {
9181f3f7681SNiklas Schnelle 		rc = zpci_dma_exit_device(zdev);
9191f3f7681SNiklas Schnelle 		if (rc)
9201f3f7681SNiklas Schnelle 			return rc;
9211f3f7681SNiklas Schnelle 	}
9222631f6b6SNiklas Schnelle 	if (zdev_enabled(zdev)) {
9232631f6b6SNiklas Schnelle 		rc = zpci_disable_device(zdev);
9242631f6b6SNiklas Schnelle 		if (rc)
9252631f6b6SNiklas Schnelle 			return rc;
9262631f6b6SNiklas Schnelle 	}
9272631f6b6SNiklas Schnelle 
9282631f6b6SNiklas Schnelle 	rc = sclp_pci_deconfigure(zdev->fid);
9292631f6b6SNiklas Schnelle 	zpci_dbg(3, "deconf fid:%x, rc:%d\n", zdev->fid, rc);
9302631f6b6SNiklas Schnelle 	if (rc)
9312631f6b6SNiklas Schnelle 		return rc;
9322631f6b6SNiklas Schnelle 	zdev->state = ZPCI_FN_STATE_STANDBY;
9332631f6b6SNiklas Schnelle 
9342631f6b6SNiklas Schnelle 	return 0;
9352631f6b6SNiklas Schnelle }
9362631f6b6SNiklas Schnelle 
937a46044a9SNiklas Schnelle /**
938a46044a9SNiklas Schnelle  * zpci_device_reserved() - Mark device as resverved
939a46044a9SNiklas Schnelle  * @zdev: the zpci_dev that was reserved
940a46044a9SNiklas Schnelle  *
941a46044a9SNiklas Schnelle  * Handle the case that a given zPCI function was reserved by another system.
942a46044a9SNiklas Schnelle  * After a call to this function the zpci_dev can not be found via
943a46044a9SNiklas Schnelle  * get_zdev_by_fid() anymore but may still be accessible via existing
944a46044a9SNiklas Schnelle  * references though it will not be functional anymore.
945a46044a9SNiklas Schnelle  */
946a46044a9SNiklas Schnelle void zpci_device_reserved(struct zpci_dev *zdev)
947a46044a9SNiklas Schnelle {
948a46044a9SNiklas Schnelle 	if (zdev->has_hp_slot)
949a46044a9SNiklas Schnelle 		zpci_exit_slot(zdev);
950a46044a9SNiklas Schnelle 	/*
951a46044a9SNiklas Schnelle 	 * Remove device from zpci_list as it is going away. This also
952a46044a9SNiklas Schnelle 	 * makes sure we ignore subsequent zPCI events for this device.
953a46044a9SNiklas Schnelle 	 */
954a46044a9SNiklas Schnelle 	spin_lock(&zpci_list_lock);
955a46044a9SNiklas Schnelle 	list_del(&zdev->entry);
956a46044a9SNiklas Schnelle 	spin_unlock(&zpci_list_lock);
957a46044a9SNiklas Schnelle 	zdev->state = ZPCI_FN_STATE_RESERVED;
958a46044a9SNiklas Schnelle 	zpci_dbg(3, "rsv fid:%x\n", zdev->fid);
959a46044a9SNiklas Schnelle 	zpci_zdev_put(zdev);
960a46044a9SNiklas Schnelle }
961a46044a9SNiklas Schnelle 
96205bc1be6SPierre Morel void zpci_release_device(struct kref *kref)
963623bd44dSSebastian Ott {
96405bc1be6SPierre Morel 	struct zpci_dev *zdev = container_of(kref, struct zpci_dev, kref);
965a9045c22SNiklas Schnelle 	int ret;
966623bd44dSSebastian Ott 
9672f0230b2SNiklas Schnelle 	if (zdev->zbus->bus)
96895b3a8b4SNiklas Schnelle 		zpci_bus_remove_device(zdev, false);
96944510d6fSPierre Morel 
9701f3f7681SNiklas Schnelle 	if (zdev->dma_table)
9711f3f7681SNiklas Schnelle 		zpci_dma_exit_device(zdev);
972f6576a1bSNiklas Schnelle 	if (zdev_enabled(zdev))
97305bc1be6SPierre Morel 		zpci_disable_device(zdev);
974f6576a1bSNiklas Schnelle 
975f6576a1bSNiklas Schnelle 	switch (zdev->state) {
976a9045c22SNiklas Schnelle 	case ZPCI_FN_STATE_CONFIGURED:
977a9045c22SNiklas Schnelle 		ret = sclp_pci_deconfigure(zdev->fid);
978a9045c22SNiklas Schnelle 		zpci_dbg(3, "deconf fid:%x, rc:%d\n", zdev->fid, ret);
979a9045c22SNiklas Schnelle 		fallthrough;
98005bc1be6SPierre Morel 	case ZPCI_FN_STATE_STANDBY:
98144510d6fSPierre Morel 		if (zdev->has_hp_slot)
98205bc1be6SPierre Morel 			zpci_exit_slot(zdev);
983a46044a9SNiklas Schnelle 		spin_lock(&zpci_list_lock);
984a46044a9SNiklas Schnelle 		list_del(&zdev->entry);
985a46044a9SNiklas Schnelle 		spin_unlock(&zpci_list_lock);
986a46044a9SNiklas Schnelle 		zpci_dbg(3, "rsv fid:%x\n", zdev->fid);
987a46044a9SNiklas Schnelle 		fallthrough;
988a46044a9SNiklas Schnelle 	case ZPCI_FN_STATE_RESERVED:
98902368b7cSNiklas Schnelle 		if (zdev->has_resources)
99005bc1be6SPierre Morel 			zpci_cleanup_bus_resources(zdev);
99105bc1be6SPierre Morel 		zpci_bus_device_unregister(zdev);
99205bc1be6SPierre Morel 		zpci_destroy_iommu(zdev);
99305bc1be6SPierre Morel 		fallthrough;
99405bc1be6SPierre Morel 	default:
99505bc1be6SPierre Morel 		break;
99605bc1be6SPierre Morel 	}
99705bc1be6SPierre Morel 	zpci_dbg(3, "rem fid:%x\n", zdev->fid);
99805bc1be6SPierre Morel 	kfree(zdev);
999623bd44dSSebastian Ott }
1000623bd44dSSebastian Ott 
1001bd3a1725SMartin Schwidefsky int zpci_report_error(struct pci_dev *pdev,
1002bd3a1725SMartin Schwidefsky 		      struct zpci_report_error_header *report)
1003bd3a1725SMartin Schwidefsky {
1004bd3a1725SMartin Schwidefsky 	struct zpci_dev *zdev = to_zpci(pdev);
1005bd3a1725SMartin Schwidefsky 
1006bd3a1725SMartin Schwidefsky 	return sclp_pci_report(report, zdev->fh, zdev->fid);
1007bd3a1725SMartin Schwidefsky }
1008bd3a1725SMartin Schwidefsky EXPORT_SYMBOL(zpci_report_error);
1009bd3a1725SMartin Schwidefsky 
10104cdf2f4eSNiklas Schnelle /**
10114cdf2f4eSNiklas Schnelle  * zpci_clear_error_state() - Clears the zPCI error state of the device
10124cdf2f4eSNiklas Schnelle  * @zdev: The zdev for which the zPCI error state should be reset
10134cdf2f4eSNiklas Schnelle  *
10144cdf2f4eSNiklas Schnelle  * Clear the zPCI error state of the device. If clearing the zPCI error state
10154cdf2f4eSNiklas Schnelle  * fails the device is left in the error state. In this case it may make sense
10164cdf2f4eSNiklas Schnelle  * to call zpci_io_perm_failure() on the associated pdev if it exists.
10174cdf2f4eSNiklas Schnelle  *
10184cdf2f4eSNiklas Schnelle  * Returns: 0 on success, -EIO otherwise
10194cdf2f4eSNiklas Schnelle  */
10204cdf2f4eSNiklas Schnelle int zpci_clear_error_state(struct zpci_dev *zdev)
10214cdf2f4eSNiklas Schnelle {
10224cdf2f4eSNiklas Schnelle 	u64 req = ZPCI_CREATE_REQ(zdev->fh, 0, ZPCI_MOD_FC_RESET_ERROR);
10234cdf2f4eSNiklas Schnelle 	struct zpci_fib fib = {0};
10244cdf2f4eSNiklas Schnelle 	u8 status;
10254cdf2f4eSNiklas Schnelle 	int cc;
10264cdf2f4eSNiklas Schnelle 
10274cdf2f4eSNiklas Schnelle 	cc = zpci_mod_fc(req, &fib, &status);
10284cdf2f4eSNiklas Schnelle 	if (cc) {
10294cdf2f4eSNiklas Schnelle 		zpci_dbg(3, "ces fid:%x, cc:%d, status:%x\n", zdev->fid, cc, status);
10304cdf2f4eSNiklas Schnelle 		return -EIO;
10314cdf2f4eSNiklas Schnelle 	}
10324cdf2f4eSNiklas Schnelle 
10334cdf2f4eSNiklas Schnelle 	return 0;
10344cdf2f4eSNiklas Schnelle }
10354cdf2f4eSNiklas Schnelle 
10364cdf2f4eSNiklas Schnelle /**
10374cdf2f4eSNiklas Schnelle  * zpci_reset_load_store_blocked() - Re-enables L/S from error state
10384cdf2f4eSNiklas Schnelle  * @zdev: The zdev for which to unblock load/store access
10394cdf2f4eSNiklas Schnelle  *
10404cdf2f4eSNiklas Schnelle  * Re-enables load/store access for a PCI function in the error state while
10414cdf2f4eSNiklas Schnelle  * keeping DMA blocked. In this state drivers can poke MMIO space to determine
10424cdf2f4eSNiklas Schnelle  * if error recovery is possible while catching any rogue DMA access from the
10434cdf2f4eSNiklas Schnelle  * device.
10444cdf2f4eSNiklas Schnelle  *
10454cdf2f4eSNiklas Schnelle  * Returns: 0 on success, -EIO otherwise
10464cdf2f4eSNiklas Schnelle  */
10474cdf2f4eSNiklas Schnelle int zpci_reset_load_store_blocked(struct zpci_dev *zdev)
10484cdf2f4eSNiklas Schnelle {
10494cdf2f4eSNiklas Schnelle 	u64 req = ZPCI_CREATE_REQ(zdev->fh, 0, ZPCI_MOD_FC_RESET_BLOCK);
10504cdf2f4eSNiklas Schnelle 	struct zpci_fib fib = {0};
10514cdf2f4eSNiklas Schnelle 	u8 status;
10524cdf2f4eSNiklas Schnelle 	int cc;
10534cdf2f4eSNiklas Schnelle 
10544cdf2f4eSNiklas Schnelle 	cc = zpci_mod_fc(req, &fib, &status);
10554cdf2f4eSNiklas Schnelle 	if (cc) {
10564cdf2f4eSNiklas Schnelle 		zpci_dbg(3, "rls fid:%x, cc:%d, status:%x\n", zdev->fid, cc, status);
10574cdf2f4eSNiklas Schnelle 		return -EIO;
10584cdf2f4eSNiklas Schnelle 	}
10594cdf2f4eSNiklas Schnelle 
10604cdf2f4eSNiklas Schnelle 	return 0;
10614cdf2f4eSNiklas Schnelle }
10624cdf2f4eSNiklas Schnelle 
1063cd248341SJan Glauber static int zpci_mem_init(void)
1064cd248341SJan Glauber {
106580c544deSSebastian Ott 	BUILD_BUG_ON(!is_power_of_2(__alignof__(struct zpci_fmb)) ||
106680c544deSSebastian Ott 		     __alignof__(struct zpci_fmb) < sizeof(struct zpci_fmb));
106780c544deSSebastian Ott 
1068d0b08853SJan Glauber 	zdev_fmb_cache = kmem_cache_create("PCI_FMB_cache", sizeof(struct zpci_fmb),
106980c544deSSebastian Ott 					   __alignof__(struct zpci_fmb), 0, NULL);
1070d0b08853SJan Glauber 	if (!zdev_fmb_cache)
1071c506fff3SSebastian Ott 		goto error_fmb;
1072d0b08853SJan Glauber 
1073c506fff3SSebastian Ott 	zpci_iomap_start = kcalloc(ZPCI_IOMAP_ENTRIES,
1074c506fff3SSebastian Ott 				   sizeof(*zpci_iomap_start), GFP_KERNEL);
1075cd248341SJan Glauber 	if (!zpci_iomap_start)
10769a4da8a5SJan Glauber 		goto error_iomap;
1077cd248341SJan Glauber 
1078c506fff3SSebastian Ott 	zpci_iomap_bitmap = kcalloc(BITS_TO_LONGS(ZPCI_IOMAP_ENTRIES),
1079c506fff3SSebastian Ott 				    sizeof(*zpci_iomap_bitmap), GFP_KERNEL);
1080c506fff3SSebastian Ott 	if (!zpci_iomap_bitmap)
1081c506fff3SSebastian Ott 		goto error_iomap_bitmap;
1082c506fff3SSebastian Ott 
1083b02002ccSNiklas Schnelle 	if (static_branch_likely(&have_mio))
1084b02002ccSNiklas Schnelle 		clp_setup_writeback_mio();
1085b02002ccSNiklas Schnelle 
1086c506fff3SSebastian Ott 	return 0;
1087c506fff3SSebastian Ott error_iomap_bitmap:
1088c506fff3SSebastian Ott 	kfree(zpci_iomap_start);
10899a4da8a5SJan Glauber error_iomap:
1090d0b08853SJan Glauber 	kmem_cache_destroy(zdev_fmb_cache);
1091c506fff3SSebastian Ott error_fmb:
1092cd248341SJan Glauber 	return -ENOMEM;
1093cd248341SJan Glauber }
1094cd248341SJan Glauber 
1095cd248341SJan Glauber static void zpci_mem_exit(void)
1096cd248341SJan Glauber {
1097c506fff3SSebastian Ott 	kfree(zpci_iomap_bitmap);
1098cd248341SJan Glauber 	kfree(zpci_iomap_start);
1099d0b08853SJan Glauber 	kmem_cache_destroy(zdev_fmb_cache);
1100cd248341SJan Glauber }
1101cd248341SJan Glauber 
11026324b4deSSebastian Ott static unsigned int s390_pci_probe __initdata = 1;
1103fbfe07d4SSebastian Ott unsigned int s390_pci_force_floating __initdata;
1104aa3b7c29SSebastian Ott static unsigned int s390_pci_initialized;
1105cd248341SJan Glauber 
1106cd248341SJan Glauber char * __init pcibios_setup(char *str)
1107cd248341SJan Glauber {
1108257608fbSSebastian Ott 	if (!strcmp(str, "off")) {
1109257608fbSSebastian Ott 		s390_pci_probe = 0;
1110cd248341SJan Glauber 		return NULL;
1111cd248341SJan Glauber 	}
111256271303SSebastian Ott 	if (!strcmp(str, "nomio")) {
11133322ba0dSNiklas Schnelle 		S390_lowcore.machine_flags &= ~MACHINE_FLAG_PCI_MIO;
111456271303SSebastian Ott 		return NULL;
111556271303SSebastian Ott 	}
1116fbfe07d4SSebastian Ott 	if (!strcmp(str, "force_floating")) {
1117fbfe07d4SSebastian Ott 		s390_pci_force_floating = 1;
1118fbfe07d4SSebastian Ott 		return NULL;
1119fbfe07d4SSebastian Ott 	}
11206cf17f9aSPierre Morel 	if (!strcmp(str, "norid")) {
11216cf17f9aSPierre Morel 		s390_pci_no_rid = 1;
11226cf17f9aSPierre Morel 		return NULL;
11236cf17f9aSPierre Morel 	}
1124cd248341SJan Glauber 	return str;
1125cd248341SJan Glauber }
1126cd248341SJan Glauber 
1127aa3b7c29SSebastian Ott bool zpci_is_enabled(void)
1128aa3b7c29SSebastian Ott {
1129aa3b7c29SSebastian Ott 	return s390_pci_initialized;
1130aa3b7c29SSebastian Ott }
1131aa3b7c29SSebastian Ott 
1132cd248341SJan Glauber static int __init pci_base_init(void)
1133cd248341SJan Glauber {
1134cd248341SJan Glauber 	int rc;
1135cd248341SJan Glauber 
11361e5635d1SHeiko Carstens 	if (!s390_pci_probe)
1137cd248341SJan Glauber 		return 0;
1138cd248341SJan Glauber 
1139da78693eSNiklas Schnelle 	if (!test_facility(69) || !test_facility(71)) {
1140da78693eSNiklas Schnelle 		pr_info("PCI is not supported because CPU facilities 69 or 71 are not available\n");
1141cd248341SJan Glauber 		return 0;
1142da78693eSNiklas Schnelle 	}
1143cd248341SJan Glauber 
11443322ba0dSNiklas Schnelle 	if (MACHINE_HAS_PCI_MIO) {
114571ba41c9SSebastian Ott 		static_branch_enable(&have_mio);
11469964f396SSebastian Ott 		ctl_set_bit(2, 5);
11479964f396SSebastian Ott 	}
114871ba41c9SSebastian Ott 
1149d0b08853SJan Glauber 	rc = zpci_debug_init();
1150d0b08853SJan Glauber 	if (rc)
11511f44a225SMartin Schwidefsky 		goto out;
1152d0b08853SJan Glauber 
1153cd248341SJan Glauber 	rc = zpci_mem_init();
1154cd248341SJan Glauber 	if (rc)
1155cd248341SJan Glauber 		goto out_mem;
1156cd248341SJan Glauber 
11579a4da8a5SJan Glauber 	rc = zpci_irq_init();
11589a4da8a5SJan Glauber 	if (rc)
11599a4da8a5SJan Glauber 		goto out_irq;
11609a4da8a5SJan Glauber 
1161828b35f6SJan Glauber 	rc = zpci_dma_init();
1162828b35f6SJan Glauber 	if (rc)
1163828b35f6SJan Glauber 		goto out_dma;
1164828b35f6SJan Glauber 
11651d578966SSebastian Ott 	rc = clp_scan_pci_devices();
1166a755a45dSJan Glauber 	if (rc)
1167a755a45dSJan Glauber 		goto out_find;
116814c87ba8SNiklas Schnelle 	zpci_bus_scan_busses();
1169a755a45dSJan Glauber 
1170aa3b7c29SSebastian Ott 	s390_pci_initialized = 1;
1171cd248341SJan Glauber 	return 0;
1172cd248341SJan Glauber 
1173a755a45dSJan Glauber out_find:
1174828b35f6SJan Glauber 	zpci_dma_exit();
1175828b35f6SJan Glauber out_dma:
11769a4da8a5SJan Glauber 	zpci_irq_exit();
11779a4da8a5SJan Glauber out_irq:
1178cd248341SJan Glauber 	zpci_mem_exit();
1179cd248341SJan Glauber out_mem:
1180d0b08853SJan Glauber 	zpci_debug_exit();
11811f44a225SMartin Schwidefsky out:
1182cd248341SJan Glauber 	return rc;
1183cd248341SJan Glauber }
118467f43f38SSebastian Ott subsys_initcall_sync(pci_base_init);
1185