xref: /openbmc/linux/arch/s390/pci/pci.c (revision 96d3c5a7d20ec546e44695983fe0508c6f904248)
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 
get_zdev_by_fid(u32 fid)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 
zpci_remove_reserved_devices(void)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 
pci_domain_nr(struct pci_bus * bus)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 
pci_proc_domain(struct pci_bus * bus)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 */
zpci_register_ioat(struct zpci_dev * zdev,u8 dmaas,u64 base,u64 limit,u64 iota,u8 * status)118828b35f6SJan Glauber int zpci_register_ioat(struct zpci_dev *zdev, u8 dmaas,
11959bbf596SNiklas Schnelle 		       u64 base, u64 limit, u64 iota, u8 *status)
120828b35f6SJan Glauber {
12172570834SSebastian Ott 	u64 req = ZPCI_CREATE_REQ(zdev->fh, dmaas, ZPCI_MOD_FC_REG_IOAT);
12272570834SSebastian Ott 	struct zpci_fib fib = {0};
12359bbf596SNiklas Schnelle 	u8 cc;
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;
13059bbf596SNiklas Schnelle 	cc = zpci_mod_fc(req, &fib, status);
1311f3f7681SNiklas Schnelle 	if (cc)
13259bbf596SNiklas Schnelle 		zpci_dbg(3, "reg ioat fid:%x, cc:%d, status:%d\n", zdev->fid, cc, *status);
1331f3f7681SNiklas Schnelle 	return cc;
134828b35f6SJan Glauber }
13509340b2fSMatthew Rosato EXPORT_SYMBOL_GPL(zpci_register_ioat);
136828b35f6SJan Glauber 
137828b35f6SJan Glauber /* Modify PCI: Unregister I/O address translation parameters */
zpci_unregister_ioat(struct zpci_dev * zdev,u8 dmaas)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 */
zpci_fmb_enable_device(struct zpci_dev * zdev)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 */
zpci_fmb_disable_device(struct zpci_dev * zdev)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 
zpci_cfg_load(struct zpci_dev * zdev,int offset,u32 * val,u8 len)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 
zpci_cfg_store(struct zpci_dev * zdev,int offset,u32 val,u8 len)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 
pcibios_align_resource(void * data,const struct resource * res,resource_size_t size,resource_size_t align)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 */
__iowrite64_copy(void __iomem * to,const void * from,size_t count)24287bc359bSJan Glauber void __iowrite64_copy(void __iomem *to, const void *from, size_t count)
24387bc359bSJan Glauber {
244*ef6566d1SJason Gunthorpe 	zpci_memcpy_toio(to, from, count * 8);
24587bc359bSJan Glauber }
24687bc359bSJan Glauber 
ioremap_prot(phys_addr_t phys_addr,size_t size,unsigned long prot)247b43b3fffSBaoquan He void __iomem *ioremap_prot(phys_addr_t phys_addr, size_t size,
248b43b3fffSBaoquan He 			   unsigned long prot)
24971ba41c9SSebastian Ott {
250b43b3fffSBaoquan He 	/*
251b43b3fffSBaoquan He 	 * When PCI MIO instructions are unavailable the "physical" address
252b43b3fffSBaoquan He 	 * encodes a hint for accessing the PCI memory space it represents.
253b43b3fffSBaoquan He 	 * Just pass it unchanged such that ioread/iowrite can decode it.
254b43b3fffSBaoquan He 	 */
25571ba41c9SSebastian Ott 	if (!static_branch_unlikely(&have_mio))
256b43b3fffSBaoquan He 		return (void __iomem *)phys_addr;
25771ba41c9SSebastian Ott 
258b43b3fffSBaoquan He 	return generic_ioremap_prot(phys_addr, size, __pgprot(prot));
259b02002ccSNiklas Schnelle }
260b02002ccSNiklas Schnelle EXPORT_SYMBOL(ioremap_prot);
261b02002ccSNiklas Schnelle 
iounmap(volatile void __iomem * addr)26271ba41c9SSebastian Ott void iounmap(volatile void __iomem *addr)
26371ba41c9SSebastian Ott {
26471ba41c9SSebastian Ott 	if (static_branch_likely(&have_mio))
265b43b3fffSBaoquan He 		generic_iounmap(addr);
26671ba41c9SSebastian Ott }
26771ba41c9SSebastian Ott EXPORT_SYMBOL(iounmap);
26871ba41c9SSebastian Ott 
269cd248341SJan Glauber /* Create a virtual mapping cookie for a PCI BAR */
pci_iomap_range_fh(struct pci_dev * pdev,int bar,unsigned long offset,unsigned long max)27071ba41c9SSebastian Ott static void __iomem *pci_iomap_range_fh(struct pci_dev *pdev, int bar,
27171ba41c9SSebastian Ott 					unsigned long offset, unsigned long max)
272cd248341SJan Glauber {
273198a5278SSebastian Ott 	struct zpci_dev *zdev =	to_zpci(pdev);
274cd248341SJan Glauber 	int idx;
275cd248341SJan Glauber 
276cd248341SJan Glauber 	idx = zdev->bars[bar].map_idx;
277cd248341SJan Glauber 	spin_lock(&zpci_iomap_lock);
278f5e44f82SSebastian Ott 	/* Detect overrun */
279f5e44f82SSebastian Ott 	WARN_ON(!++zpci_iomap_start[idx].count);
280cd248341SJan Glauber 	zpci_iomap_start[idx].fh = zdev->fh;
281cd248341SJan Glauber 	zpci_iomap_start[idx].bar = bar;
282cd248341SJan Glauber 	spin_unlock(&zpci_iomap_lock);
283cd248341SJan Glauber 
2849e00caaeSSebastian Ott 	return (void __iomem *) ZPCI_ADDR(idx) + offset;
285cd248341SJan Glauber }
28671ba41c9SSebastian Ott 
pci_iomap_range_mio(struct pci_dev * pdev,int bar,unsigned long offset,unsigned long max)28771ba41c9SSebastian Ott static void __iomem *pci_iomap_range_mio(struct pci_dev *pdev, int bar,
28871ba41c9SSebastian Ott 					 unsigned long offset,
28971ba41c9SSebastian Ott 					 unsigned long max)
29071ba41c9SSebastian Ott {
29171ba41c9SSebastian Ott 	unsigned long barsize = pci_resource_len(pdev, bar);
29271ba41c9SSebastian Ott 	struct zpci_dev *zdev = to_zpci(pdev);
29371ba41c9SSebastian Ott 	void __iomem *iova;
29471ba41c9SSebastian Ott 
29571ba41c9SSebastian Ott 	iova = ioremap((unsigned long) zdev->bars[bar].mio_wt, barsize);
29671ba41c9SSebastian Ott 	return iova ? iova + offset : iova;
29771ba41c9SSebastian Ott }
29871ba41c9SSebastian Ott 
pci_iomap_range(struct pci_dev * pdev,int bar,unsigned long offset,unsigned long max)29971ba41c9SSebastian Ott void __iomem *pci_iomap_range(struct pci_dev *pdev, int bar,
30071ba41c9SSebastian Ott 			      unsigned long offset, unsigned long max)
30171ba41c9SSebastian Ott {
302c9c13ba4SDenis Efremov 	if (bar >= PCI_STD_NUM_BARS || !pci_resource_len(pdev, bar))
30371ba41c9SSebastian Ott 		return NULL;
30471ba41c9SSebastian Ott 
30571ba41c9SSebastian Ott 	if (static_branch_likely(&have_mio))
30671ba41c9SSebastian Ott 		return pci_iomap_range_mio(pdev, bar, offset, max);
30771ba41c9SSebastian Ott 	else
30871ba41c9SSebastian Ott 		return pci_iomap_range_fh(pdev, bar, offset, max);
30971ba41c9SSebastian Ott }
310d9426083SSebastian Ott EXPORT_SYMBOL(pci_iomap_range);
3118cfc99b5SMichael S. Tsirkin 
pci_iomap(struct pci_dev * dev,int bar,unsigned long maxlen)3128cfc99b5SMichael S. Tsirkin void __iomem *pci_iomap(struct pci_dev *dev, int bar, unsigned long maxlen)
3138cfc99b5SMichael S. Tsirkin {
3148cfc99b5SMichael S. Tsirkin 	return pci_iomap_range(dev, bar, 0, maxlen);
3158cfc99b5SMichael S. Tsirkin }
3168cfc99b5SMichael S. Tsirkin EXPORT_SYMBOL(pci_iomap);
317cd248341SJan Glauber 
pci_iomap_wc_range_mio(struct pci_dev * pdev,int bar,unsigned long offset,unsigned long max)31871ba41c9SSebastian Ott static void __iomem *pci_iomap_wc_range_mio(struct pci_dev *pdev, int bar,
31971ba41c9SSebastian Ott 					    unsigned long offset, 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_wb, barsize);
32671ba41c9SSebastian Ott 	return iova ? iova + offset : iova;
32771ba41c9SSebastian Ott }
32871ba41c9SSebastian Ott 
pci_iomap_wc_range(struct pci_dev * pdev,int bar,unsigned long offset,unsigned long max)32971ba41c9SSebastian Ott void __iomem *pci_iomap_wc_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_wc_range_mio(pdev, bar, offset, max);
33771ba41c9SSebastian Ott 	else
33871ba41c9SSebastian Ott 		return pci_iomap_range_fh(pdev, bar, offset, max);
33971ba41c9SSebastian Ott }
34071ba41c9SSebastian Ott EXPORT_SYMBOL(pci_iomap_wc_range);
34171ba41c9SSebastian Ott 
pci_iomap_wc(struct pci_dev * dev,int bar,unsigned long maxlen)34271ba41c9SSebastian Ott void __iomem *pci_iomap_wc(struct pci_dev *dev, int bar, unsigned long maxlen)
34371ba41c9SSebastian Ott {
34471ba41c9SSebastian Ott 	return pci_iomap_wc_range(dev, bar, 0, maxlen);
34571ba41c9SSebastian Ott }
34671ba41c9SSebastian Ott EXPORT_SYMBOL(pci_iomap_wc);
34771ba41c9SSebastian Ott 
pci_iounmap_fh(struct pci_dev * pdev,void __iomem * addr)34871ba41c9SSebastian Ott static void pci_iounmap_fh(struct pci_dev *pdev, void __iomem *addr)
349cd248341SJan Glauber {
3509e00caaeSSebastian Ott 	unsigned int idx = ZPCI_IDX(addr);
351cd248341SJan Glauber 
352cd248341SJan Glauber 	spin_lock(&zpci_iomap_lock);
3538cfc99b5SMichael S. Tsirkin 	/* Detect underrun */
354f5e44f82SSebastian Ott 	WARN_ON(!zpci_iomap_start[idx].count);
3558cfc99b5SMichael S. Tsirkin 	if (!--zpci_iomap_start[idx].count) {
356cd248341SJan Glauber 		zpci_iomap_start[idx].fh = 0;
357cd248341SJan Glauber 		zpci_iomap_start[idx].bar = 0;
3588cfc99b5SMichael S. Tsirkin 	}
359cd248341SJan Glauber 	spin_unlock(&zpci_iomap_lock);
360cd248341SJan Glauber }
36171ba41c9SSebastian Ott 
pci_iounmap_mio(struct pci_dev * pdev,void __iomem * addr)36271ba41c9SSebastian Ott static void pci_iounmap_mio(struct pci_dev *pdev, void __iomem *addr)
36371ba41c9SSebastian Ott {
36471ba41c9SSebastian Ott 	iounmap(addr);
36571ba41c9SSebastian Ott }
36671ba41c9SSebastian Ott 
pci_iounmap(struct pci_dev * pdev,void __iomem * addr)36771ba41c9SSebastian Ott void pci_iounmap(struct pci_dev *pdev, void __iomem *addr)
36871ba41c9SSebastian Ott {
36971ba41c9SSebastian Ott 	if (static_branch_likely(&have_mio))
37071ba41c9SSebastian Ott 		pci_iounmap_mio(pdev, addr);
37171ba41c9SSebastian Ott 	else
37271ba41c9SSebastian Ott 		pci_iounmap_fh(pdev, addr);
37371ba41c9SSebastian Ott }
374d9426083SSebastian Ott EXPORT_SYMBOL(pci_iounmap);
375cd248341SJan Glauber 
pci_read(struct pci_bus * bus,unsigned int devfn,int where,int size,u32 * val)376cd248341SJan Glauber static int pci_read(struct pci_bus *bus, unsigned int devfn, int where,
377cd248341SJan Glauber 		    int size, u32 *val)
378cd248341SJan Glauber {
3797dcfe50fSNiklas Schnelle 	struct zpci_dev *zdev = zdev_from_bus(bus, devfn);
380cd248341SJan Glauber 
38144510d6fSPierre Morel 	return (zdev) ? zpci_cfg_load(zdev, where, val, size) : -ENODEV;
382cd248341SJan Glauber }
383cd248341SJan Glauber 
pci_write(struct pci_bus * bus,unsigned int devfn,int where,int size,u32 val)384cd248341SJan Glauber static int pci_write(struct pci_bus *bus, unsigned int devfn, int where,
385cd248341SJan Glauber 		     int size, u32 val)
386cd248341SJan Glauber {
3877dcfe50fSNiklas Schnelle 	struct zpci_dev *zdev = zdev_from_bus(bus, devfn);
388cd248341SJan Glauber 
38944510d6fSPierre Morel 	return (zdev) ? zpci_cfg_store(zdev, where, val, size) : -ENODEV;
390cd248341SJan Glauber }
391cd248341SJan Glauber 
392cd248341SJan Glauber static struct pci_ops pci_root_ops = {
393cd248341SJan Glauber 	.read = pci_read,
394cd248341SJan Glauber 	.write = pci_write,
395cd248341SJan Glauber };
396cd248341SJan Glauber 
zpci_map_resources(struct pci_dev * pdev)3971803ba2dSSebastian Ott static void zpci_map_resources(struct pci_dev *pdev)
398cd248341SJan Glauber {
39971ba41c9SSebastian Ott 	struct zpci_dev *zdev = to_zpci(pdev);
400cd248341SJan Glauber 	resource_size_t len;
401cd248341SJan Glauber 	int i;
402cd248341SJan Glauber 
403c9c13ba4SDenis Efremov 	for (i = 0; i < PCI_STD_NUM_BARS; i++) {
404cd248341SJan Glauber 		len = pci_resource_len(pdev, i);
405cd248341SJan Glauber 		if (!len)
406cd248341SJan Glauber 			continue;
40771ba41c9SSebastian Ott 
408c7ff0e91SSebastian Ott 		if (zpci_use_mio(zdev))
40971ba41c9SSebastian Ott 			pdev->resource[i].start =
410df057c91SNiklas Schnelle 				(resource_size_t __force) zdev->bars[i].mio_wt;
41171ba41c9SSebastian Ott 		else
412c7ff0e91SSebastian Ott 			pdev->resource[i].start = (resource_size_t __force)
413c7ff0e91SSebastian Ott 				pci_iomap_range_fh(pdev, i, 0, 0);
414cd248341SJan Glauber 		pdev->resource[i].end = pdev->resource[i].start + len - 1;
415cd248341SJan Glauber 	}
416cfbb4a7aSSebastian Ott 
417abb95b75SNiklas Schnelle 	zpci_iov_map_resources(pdev);
418944239c5SSebastian Ott }
419944239c5SSebastian Ott 
zpci_unmap_resources(struct pci_dev * pdev)4201803ba2dSSebastian Ott static void zpci_unmap_resources(struct pci_dev *pdev)
421944239c5SSebastian Ott {
422c7ff0e91SSebastian Ott 	struct zpci_dev *zdev = to_zpci(pdev);
423944239c5SSebastian Ott 	resource_size_t len;
424944239c5SSebastian Ott 	int i;
425944239c5SSebastian Ott 
426c7ff0e91SSebastian Ott 	if (zpci_use_mio(zdev))
42771ba41c9SSebastian Ott 		return;
42871ba41c9SSebastian Ott 
429c9c13ba4SDenis Efremov 	for (i = 0; i < PCI_STD_NUM_BARS; i++) {
430944239c5SSebastian Ott 		len = pci_resource_len(pdev, i);
431944239c5SSebastian Ott 		if (!len)
432944239c5SSebastian Ott 			continue;
433c7ff0e91SSebastian Ott 		pci_iounmap_fh(pdev, (void __iomem __force *)
4345b9f2081SMartin Schwidefsky 			       pdev->resource[i].start);
435944239c5SSebastian Ott 	}
436944239c5SSebastian Ott }
437cd248341SJan Glauber 
zpci_alloc_iomap(struct zpci_dev * zdev)438cd248341SJan Glauber static int zpci_alloc_iomap(struct zpci_dev *zdev)
439cd248341SJan Glauber {
440bf19c94dSSebastian Ott 	unsigned long entry;
441cd248341SJan Glauber 
442cd248341SJan Glauber 	spin_lock(&zpci_iomap_lock);
443c506fff3SSebastian Ott 	entry = find_first_zero_bit(zpci_iomap_bitmap, ZPCI_IOMAP_ENTRIES);
444c506fff3SSebastian Ott 	if (entry == ZPCI_IOMAP_ENTRIES) {
445cd248341SJan Glauber 		spin_unlock(&zpci_iomap_lock);
446cd248341SJan Glauber 		return -ENOSPC;
447cd248341SJan Glauber 	}
448c506fff3SSebastian Ott 	set_bit(entry, zpci_iomap_bitmap);
449cd248341SJan Glauber 	spin_unlock(&zpci_iomap_lock);
450cd248341SJan Glauber 	return entry;
451cd248341SJan Glauber }
452cd248341SJan Glauber 
zpci_free_iomap(struct zpci_dev * zdev,int entry)453cd248341SJan Glauber static void zpci_free_iomap(struct zpci_dev *zdev, int entry)
454cd248341SJan Glauber {
455cd248341SJan Glauber 	spin_lock(&zpci_iomap_lock);
456cd248341SJan Glauber 	memset(&zpci_iomap_start[entry], 0, sizeof(struct zpci_iomap_entry));
457c506fff3SSebastian Ott 	clear_bit(entry, zpci_iomap_bitmap);
458cd248341SJan Glauber 	spin_unlock(&zpci_iomap_lock);
459cd248341SJan Glauber }
460cd248341SJan Glauber 
zpci_do_update_iomap_fh(struct zpci_dev * zdev,u32 fh)4614fe20497SNiklas Schnelle static void zpci_do_update_iomap_fh(struct zpci_dev *zdev, u32 fh)
4624fe20497SNiklas Schnelle {
4634fe20497SNiklas Schnelle 	int bar, idx;
4644fe20497SNiklas Schnelle 
4654fe20497SNiklas Schnelle 	spin_lock(&zpci_iomap_lock);
4664fe20497SNiklas Schnelle 	for (bar = 0; bar < PCI_STD_NUM_BARS; bar++) {
4674fe20497SNiklas Schnelle 		if (!zdev->bars[bar].size)
4684fe20497SNiklas Schnelle 			continue;
4694fe20497SNiklas Schnelle 		idx = zdev->bars[bar].map_idx;
4704fe20497SNiklas Schnelle 		if (!zpci_iomap_start[idx].count)
4714fe20497SNiklas Schnelle 			continue;
4724fe20497SNiklas Schnelle 		WRITE_ONCE(zpci_iomap_start[idx].fh, zdev->fh);
4734fe20497SNiklas Schnelle 	}
4744fe20497SNiklas Schnelle 	spin_unlock(&zpci_iomap_lock);
4754fe20497SNiklas Schnelle }
4764fe20497SNiklas Schnelle 
zpci_update_fh(struct zpci_dev * zdev,u32 fh)4774fe20497SNiklas Schnelle void zpci_update_fh(struct zpci_dev *zdev, u32 fh)
4784fe20497SNiklas Schnelle {
4794fe20497SNiklas Schnelle 	if (!fh || zdev->fh == fh)
4804fe20497SNiklas Schnelle 		return;
4814fe20497SNiklas Schnelle 
4824fe20497SNiklas Schnelle 	zdev->fh = fh;
4834fe20497SNiklas Schnelle 	if (zpci_use_mio(zdev))
4844fe20497SNiklas Schnelle 		return;
4854fe20497SNiklas Schnelle 	if (zdev->has_resources && zdev_enabled(zdev))
4864fe20497SNiklas Schnelle 		zpci_do_update_iomap_fh(zdev, fh);
4874fe20497SNiklas Schnelle }
4884fe20497SNiklas Schnelle 
__alloc_res(struct zpci_dev * zdev,unsigned long start,unsigned long size,unsigned long flags)4897a572a3aSSebastian Ott static struct resource *__alloc_res(struct zpci_dev *zdev, unsigned long start,
4907a572a3aSSebastian Ott 				    unsigned long size, unsigned long flags)
4917a572a3aSSebastian Ott {
4927a572a3aSSebastian Ott 	struct resource *r;
4937a572a3aSSebastian Ott 
4947a572a3aSSebastian Ott 	r = kzalloc(sizeof(*r), GFP_KERNEL);
4957a572a3aSSebastian Ott 	if (!r)
4967a572a3aSSebastian Ott 		return NULL;
4977a572a3aSSebastian Ott 
4987a572a3aSSebastian Ott 	r->start = start;
4997a572a3aSSebastian Ott 	r->end = r->start + size - 1;
5007a572a3aSSebastian Ott 	r->flags = flags;
5017a572a3aSSebastian Ott 	r->name = zdev->res_name;
5027a572a3aSSebastian Ott 
5037a572a3aSSebastian Ott 	if (request_resource(&iomem_resource, r)) {
5047a572a3aSSebastian Ott 		kfree(r);
5057a572a3aSSebastian Ott 		return NULL;
5067a572a3aSSebastian Ott 	}
5077a572a3aSSebastian Ott 	return r;
5087a572a3aSSebastian Ott }
5097a572a3aSSebastian Ott 
zpci_setup_bus_resources(struct zpci_dev * zdev)510ab909509SNiklas Schnelle int zpci_setup_bus_resources(struct zpci_dev *zdev)
5117a572a3aSSebastian Ott {
5127a572a3aSSebastian Ott 	unsigned long addr, size, flags;
5137a572a3aSSebastian Ott 	struct resource *res;
5147a572a3aSSebastian Ott 	int i, entry;
5157a572a3aSSebastian Ott 
5167a572a3aSSebastian Ott 	snprintf(zdev->res_name, sizeof(zdev->res_name),
51705bc1be6SPierre Morel 		 "PCI Bus %04x:%02x", zdev->uid, ZPCI_BUS_NR);
5187a572a3aSSebastian Ott 
519c9c13ba4SDenis Efremov 	for (i = 0; i < PCI_STD_NUM_BARS; i++) {
5207a572a3aSSebastian Ott 		if (!zdev->bars[i].size)
5217a572a3aSSebastian Ott 			continue;
5227a572a3aSSebastian Ott 		entry = zpci_alloc_iomap(zdev);
5237a572a3aSSebastian Ott 		if (entry < 0)
5247a572a3aSSebastian Ott 			return entry;
5257a572a3aSSebastian Ott 		zdev->bars[i].map_idx = entry;
5267a572a3aSSebastian Ott 
5277a572a3aSSebastian Ott 		/* only MMIO is supported */
5287a572a3aSSebastian Ott 		flags = IORESOURCE_MEM;
5297a572a3aSSebastian Ott 		if (zdev->bars[i].val & 8)
5307a572a3aSSebastian Ott 			flags |= IORESOURCE_PREFETCH;
5317a572a3aSSebastian Ott 		if (zdev->bars[i].val & 4)
5327a572a3aSSebastian Ott 			flags |= IORESOURCE_MEM_64;
5337a572a3aSSebastian Ott 
534c7ff0e91SSebastian Ott 		if (zpci_use_mio(zdev))
535df057c91SNiklas Schnelle 			addr = (unsigned long) zdev->bars[i].mio_wt;
536dcd33b23SSebastian Ott 		else
5379e00caaeSSebastian Ott 			addr = ZPCI_ADDR(entry);
5387a572a3aSSebastian Ott 		size = 1UL << zdev->bars[i].size;
5397a572a3aSSebastian Ott 
5407a572a3aSSebastian Ott 		res = __alloc_res(zdev, addr, size, flags);
5417a572a3aSSebastian Ott 		if (!res) {
5427a572a3aSSebastian Ott 			zpci_free_iomap(zdev, entry);
5437a572a3aSSebastian Ott 			return -ENOMEM;
5447a572a3aSSebastian Ott 		}
5457a572a3aSSebastian Ott 		zdev->bars[i].res = res;
5467a572a3aSSebastian Ott 	}
547a50297cfSNiklas Schnelle 	zdev->has_resources = 1;
5487a572a3aSSebastian Ott 
5497a572a3aSSebastian Ott 	return 0;
5507a572a3aSSebastian Ott }
5517a572a3aSSebastian Ott 
zpci_cleanup_bus_resources(struct zpci_dev * zdev)5527a572a3aSSebastian Ott static void zpci_cleanup_bus_resources(struct zpci_dev *zdev)
5537a572a3aSSebastian Ott {
554ab909509SNiklas Schnelle 	struct resource *res;
5557a572a3aSSebastian Ott 	int i;
5567a572a3aSSebastian Ott 
557ab909509SNiklas Schnelle 	pci_lock_rescan_remove();
558c9c13ba4SDenis Efremov 	for (i = 0; i < PCI_STD_NUM_BARS; i++) {
559ab909509SNiklas Schnelle 		res = zdev->bars[i].res;
560ab909509SNiklas Schnelle 		if (!res)
5617a572a3aSSebastian Ott 			continue;
5627a572a3aSSebastian Ott 
563ab909509SNiklas Schnelle 		release_resource(res);
564ab909509SNiklas Schnelle 		pci_bus_remove_resource(zdev->zbus->bus, res);
5657a572a3aSSebastian Ott 		zpci_free_iomap(zdev, zdev->bars[i].map_idx);
566ab909509SNiklas Schnelle 		zdev->bars[i].res = NULL;
567ab909509SNiklas Schnelle 		kfree(res);
5687a572a3aSSebastian Ott 	}
569a50297cfSNiklas Schnelle 	zdev->has_resources = 0;
570ab909509SNiklas Schnelle 	pci_unlock_rescan_remove();
5717a572a3aSSebastian Ott }
5727a572a3aSSebastian Ott 
pcibios_device_add(struct pci_dev * pdev)57306dc660eSOliver O'Halloran int pcibios_device_add(struct pci_dev *pdev)
574af0a8a84SSebastian Ott {
5752a671f77SNiklas Schnelle 	struct zpci_dev *zdev = to_zpci(pdev);
576cb809182SSebastian Ott 	struct resource *res;
577cb809182SSebastian Ott 	int i;
578cb809182SSebastian Ott 
5792a671f77SNiklas Schnelle 	/* The pdev has a reference to the zdev via its bus */
5802a671f77SNiklas Schnelle 	zpci_zdev_get(zdev);
5817dc20ab1SSebastian Ott 	if (pdev->is_physfn)
5827dc20ab1SSebastian Ott 		pdev->no_vf_scan = 1;
5837dc20ab1SSebastian Ott 
584ef4858c6SSebastian Ott 	pdev->dev.groups = zpci_attr_groups;
5855657933dSBart Van Assche 	pdev->dev.dma_ops = &s390_pci_dma_ops;
5861803ba2dSSebastian Ott 	zpci_map_resources(pdev);
587cb809182SSebastian Ott 
588c9c13ba4SDenis Efremov 	for (i = 0; i < PCI_STD_NUM_BARS; i++) {
589cb809182SSebastian Ott 		res = &pdev->resource[i];
590cb809182SSebastian Ott 		if (res->parent || !res->flags)
591cb809182SSebastian Ott 			continue;
592cb809182SSebastian Ott 		pci_claim_resource(pdev, i);
593cb809182SSebastian Ott 	}
594cb809182SSebastian Ott 
595cb809182SSebastian Ott 	return 0;
596cb809182SSebastian Ott }
597cb809182SSebastian Ott 
pcibios_release_device(struct pci_dev * pdev)5981803ba2dSSebastian Ott void pcibios_release_device(struct pci_dev *pdev)
5991803ba2dSSebastian Ott {
6002a671f77SNiklas Schnelle 	struct zpci_dev *zdev = to_zpci(pdev);
6012a671f77SNiklas Schnelle 
6021803ba2dSSebastian Ott 	zpci_unmap_resources(pdev);
6032a671f77SNiklas Schnelle 	zpci_zdev_put(zdev);
6041803ba2dSSebastian Ott }
6051803ba2dSSebastian Ott 
pcibios_enable_device(struct pci_dev * pdev,int mask)606cb809182SSebastian Ott int pcibios_enable_device(struct pci_dev *pdev, int mask)
607cb809182SSebastian Ott {
608198a5278SSebastian Ott 	struct zpci_dev *zdev = to_zpci(pdev);
609af0a8a84SSebastian Ott 
6109a99649fSSebastian Ott 	zpci_debug_init_device(zdev, dev_name(&pdev->dev));
611af0a8a84SSebastian Ott 	zpci_fmb_enable_device(zdev);
612af0a8a84SSebastian Ott 
613d7533232SBjorn Helgaas 	return pci_enable_resources(pdev, mask);
614af0a8a84SSebastian Ott }
615af0a8a84SSebastian Ott 
pcibios_disable_device(struct pci_dev * pdev)616cb809182SSebastian Ott void pcibios_disable_device(struct pci_dev *pdev)
617944239c5SSebastian Ott {
618198a5278SSebastian Ott 	struct zpci_dev *zdev = to_zpci(pdev);
619944239c5SSebastian Ott 
620944239c5SSebastian Ott 	zpci_fmb_disable_device(zdev);
621944239c5SSebastian Ott 	zpci_debug_exit_device(zdev);
622944239c5SSebastian Ott }
623944239c5SSebastian Ott 
__zpci_register_domain(int domain)62405bc1be6SPierre Morel static int __zpci_register_domain(int domain)
625cd248341SJan Glauber {
626969ae01bSNiklas Schnelle 	spin_lock(&zpci_domain_lock);
62705bc1be6SPierre Morel 	if (test_bit(domain, zpci_domain)) {
628969ae01bSNiklas Schnelle 		spin_unlock(&zpci_domain_lock);
62905bc1be6SPierre Morel 		pr_err("Domain %04x is already assigned\n", domain);
630312e8462SSebastian Ott 		return -EEXIST;
631312e8462SSebastian Ott 	}
63205bc1be6SPierre Morel 	set_bit(domain, zpci_domain);
633312e8462SSebastian Ott 	spin_unlock(&zpci_domain_lock);
63405bc1be6SPierre Morel 	return domain;
6355c5afd02SSebastian Ott }
63605bc1be6SPierre Morel 
__zpci_alloc_domain(void)63705bc1be6SPierre Morel static int __zpci_alloc_domain(void)
63805bc1be6SPierre Morel {
63905bc1be6SPierre Morel 	int domain;
64005bc1be6SPierre Morel 
64105bc1be6SPierre Morel 	spin_lock(&zpci_domain_lock);
642969ae01bSNiklas Schnelle 	/*
643969ae01bSNiklas Schnelle 	 * We can always auto allocate domains below ZPCI_NR_DEVICES.
644969ae01bSNiklas Schnelle 	 * There is either a free domain or we have reached the maximum in
645969ae01bSNiklas Schnelle 	 * which case we would have bailed earlier.
646969ae01bSNiklas Schnelle 	 */
64705bc1be6SPierre Morel 	domain = find_first_zero_bit(zpci_domain, ZPCI_NR_DEVICES);
64805bc1be6SPierre Morel 	set_bit(domain, zpci_domain);
649cd248341SJan Glauber 	spin_unlock(&zpci_domain_lock);
65005bc1be6SPierre Morel 	return domain;
651cd248341SJan Glauber }
652cd248341SJan Glauber 
zpci_alloc_domain(int domain)65305bc1be6SPierre Morel int zpci_alloc_domain(int domain)
65405bc1be6SPierre Morel {
65505bc1be6SPierre Morel 	if (zpci_unique_uid) {
65605bc1be6SPierre Morel 		if (domain)
65705bc1be6SPierre Morel 			return __zpci_register_domain(domain);
65805bc1be6SPierre Morel 		pr_warn("UID checking was active but no UID is provided: switching to automatic domain allocation\n");
65905bc1be6SPierre Morel 		update_uid_checking(false);
66005bc1be6SPierre Morel 	}
66105bc1be6SPierre Morel 	return __zpci_alloc_domain();
66205bc1be6SPierre Morel }
66305bc1be6SPierre Morel 
zpci_free_domain(int domain)66405bc1be6SPierre Morel void zpci_free_domain(int domain)
665cd248341SJan Glauber {
666cd248341SJan Glauber 	spin_lock(&zpci_domain_lock);
66705bc1be6SPierre Morel 	clear_bit(domain, zpci_domain);
668cd248341SJan Glauber 	spin_unlock(&zpci_domain_lock);
669cd248341SJan Glauber }
670cd248341SJan Glauber 
6717d594322SSebastian Ott 
zpci_enable_device(struct zpci_dev * zdev)672a755a45dSJan Glauber int zpci_enable_device(struct zpci_dev *zdev)
673a755a45dSJan Glauber {
674cc049eecSNiklas Schnelle 	u32 fh = zdev->fh;
6751f3f7681SNiklas Schnelle 	int rc = 0;
676a755a45dSJan Glauber 
6771f3f7681SNiklas Schnelle 	if (clp_enable_fh(zdev, &fh, ZPCI_NR_DMA_SPACES))
678f7addcddSNiklas Schnelle 		rc = -EIO;
6791f3f7681SNiklas Schnelle 	else
6804fe20497SNiklas Schnelle 		zpci_update_fh(zdev, fh);
681a755a45dSJan Glauber 	return rc;
682a755a45dSJan Glauber }
68309340b2fSMatthew Rosato EXPORT_SYMBOL_GPL(zpci_enable_device);
684a755a45dSJan Glauber 
zpci_disable_device(struct zpci_dev * zdev)685cb65a669SSebastian Ott int zpci_disable_device(struct zpci_dev *zdev)
686cb65a669SSebastian Ott {
687cc049eecSNiklas Schnelle 	u32 fh = zdev->fh;
6888256addaSNiklas Schnelle 	int cc, rc = 0;
6898256addaSNiklas Schnelle 
690cc049eecSNiklas Schnelle 	cc = clp_disable_fh(zdev, &fh);
691cc049eecSNiklas Schnelle 	if (!cc) {
6924fe20497SNiklas Schnelle 		zpci_update_fh(zdev, fh);
693cc049eecSNiklas Schnelle 	} else if (cc == CLP_RC_SETPCIFN_ALRDY) {
6948256addaSNiklas Schnelle 		pr_info("Disabling PCI function %08x had no effect as it was already disabled\n",
6958256addaSNiklas Schnelle 			zdev->fid);
6968256addaSNiklas Schnelle 		/* Function is already disabled - update handle */
697cc049eecSNiklas Schnelle 		rc = clp_refresh_fh(zdev->fid, &fh);
698cc049eecSNiklas Schnelle 		if (!rc) {
6994fe20497SNiklas Schnelle 			zpci_update_fh(zdev, fh);
7008256addaSNiklas Schnelle 			rc = -EINVAL;
701cc049eecSNiklas Schnelle 		}
702cc049eecSNiklas Schnelle 	} else {
7038256addaSNiklas Schnelle 		rc = -EIO;
7048256addaSNiklas Schnelle 	}
7058256addaSNiklas Schnelle 	return rc;
706cb65a669SSebastian Ott }
70709340b2fSMatthew Rosato EXPORT_SYMBOL_GPL(zpci_disable_device);
708cb65a669SSebastian Ott 
709ba764dd7SNiklas Schnelle /**
710da995d53SNiklas Schnelle  * zpci_hot_reset_device - perform a reset of the given zPCI function
711da995d53SNiklas Schnelle  * @zdev: the slot which should be reset
712da995d53SNiklas Schnelle  *
713da995d53SNiklas Schnelle  * Performs a low level reset of the zPCI function. The reset is low level in
714da995d53SNiklas Schnelle  * the sense that the zPCI function can be reset without detaching it from the
715da995d53SNiklas Schnelle  * common PCI subsystem. The reset may be performed while under control of
716da995d53SNiklas Schnelle  * either DMA or IOMMU APIs in which case the existing DMA/IOMMU translation
717da995d53SNiklas Schnelle  * table is reinstated at the end of the reset.
718da995d53SNiklas Schnelle  *
719da995d53SNiklas Schnelle  * After the reset the functions internal state is reset to an initial state
720da995d53SNiklas Schnelle  * equivalent to its state during boot when first probing a driver.
721da995d53SNiklas Schnelle  * Consequently after reset the PCI function requires re-initialization via the
722da995d53SNiklas Schnelle  * common PCI code including re-enabling IRQs via pci_alloc_irq_vectors()
723da995d53SNiklas Schnelle  * and enabling the function via e.g.pci_enablde_device_flags().The caller
724da995d53SNiklas Schnelle  * must guard against concurrent reset attempts.
725da995d53SNiklas Schnelle  *
726da995d53SNiklas Schnelle  * In most cases this function should not be called directly but through
727da995d53SNiklas Schnelle  * pci_reset_function() or pci_reset_bus() which handle the save/restore and
728da995d53SNiklas Schnelle  * locking.
729da995d53SNiklas Schnelle  *
730da995d53SNiklas Schnelle  * Return: 0 on success and an error value otherwise
731da995d53SNiklas Schnelle  */
zpci_hot_reset_device(struct zpci_dev * zdev)732da995d53SNiklas Schnelle int zpci_hot_reset_device(struct zpci_dev *zdev)
733da995d53SNiklas Schnelle {
73459bbf596SNiklas Schnelle 	u8 status;
735da995d53SNiklas Schnelle 	int rc;
736da995d53SNiklas Schnelle 
737da995d53SNiklas Schnelle 	zpci_dbg(3, "rst fid:%x, fh:%x\n", zdev->fid, zdev->fh);
738da995d53SNiklas Schnelle 	if (zdev_enabled(zdev)) {
739da995d53SNiklas Schnelle 		/* Disables device access, DMAs and IRQs (reset state) */
740da995d53SNiklas Schnelle 		rc = zpci_disable_device(zdev);
741da995d53SNiklas Schnelle 		/*
742da995d53SNiklas Schnelle 		 * Due to a z/VM vs LPAR inconsistency in the error state the
743da995d53SNiklas Schnelle 		 * FH may indicate an enabled device but disable says the
744da995d53SNiklas Schnelle 		 * device is already disabled don't treat it as an error here.
745da995d53SNiklas Schnelle 		 */
746da995d53SNiklas Schnelle 		if (rc == -EINVAL)
747da995d53SNiklas Schnelle 			rc = 0;
748da995d53SNiklas Schnelle 		if (rc)
749da995d53SNiklas Schnelle 			return rc;
750da995d53SNiklas Schnelle 	}
751da995d53SNiklas Schnelle 
752da995d53SNiklas Schnelle 	rc = zpci_enable_device(zdev);
753da995d53SNiklas Schnelle 	if (rc)
754da995d53SNiklas Schnelle 		return rc;
755da995d53SNiklas Schnelle 
756da995d53SNiklas Schnelle 	if (zdev->dma_table)
757da995d53SNiklas Schnelle 		rc = zpci_register_ioat(zdev, 0, zdev->start_dma, zdev->end_dma,
75859bbf596SNiklas Schnelle 					virt_to_phys(zdev->dma_table), &status);
759da995d53SNiklas Schnelle 	else
760da995d53SNiklas Schnelle 		rc = zpci_dma_init_device(zdev);
761da995d53SNiklas Schnelle 	if (rc) {
762da995d53SNiklas Schnelle 		zpci_disable_device(zdev);
763da995d53SNiklas Schnelle 		return rc;
764da995d53SNiklas Schnelle 	}
765da995d53SNiklas Schnelle 
766da995d53SNiklas Schnelle 	return 0;
767da995d53SNiklas Schnelle }
768da995d53SNiklas Schnelle 
769da995d53SNiklas Schnelle /**
770ba764dd7SNiklas Schnelle  * zpci_create_device() - Create a new zpci_dev and add it to the zbus
771ba764dd7SNiklas Schnelle  * @fid: Function ID of the device to be created
772ba764dd7SNiklas Schnelle  * @fh: Current Function Handle of the device to be created
773ba764dd7SNiklas Schnelle  * @state: Initial state after creation either Standby or Configured
774ba764dd7SNiklas Schnelle  *
775ba764dd7SNiklas Schnelle  * Creates a new zpci device and adds it to its, possibly newly created, zbus
776ba764dd7SNiklas Schnelle  * as well as zpci_list.
777ba764dd7SNiklas Schnelle  *
77814c87ba8SNiklas Schnelle  * Returns: the zdev on success or an error pointer otherwise
779ba764dd7SNiklas Schnelle  */
zpci_create_device(u32 fid,u32 fh,enum zpci_state state)78014c87ba8SNiklas Schnelle struct zpci_dev *zpci_create_device(u32 fid, u32 fh, enum zpci_state state)
781cd248341SJan Glauber {
782ba764dd7SNiklas Schnelle 	struct zpci_dev *zdev;
783cd248341SJan Glauber 	int rc;
784cd248341SJan Glauber 
78552c79e63SNiklas Schnelle 	zpci_dbg(1, "add fid:%x, fh:%x, c:%d\n", fid, fh, state);
786ba764dd7SNiklas Schnelle 	zdev = kzalloc(sizeof(*zdev), GFP_KERNEL);
787ba764dd7SNiklas Schnelle 	if (!zdev)
78814c87ba8SNiklas Schnelle 		return ERR_PTR(-ENOMEM);
789ba764dd7SNiklas Schnelle 
790ba764dd7SNiklas Schnelle 	/* FID and Function Handle are the static/dynamic identifiers */
791ba764dd7SNiklas Schnelle 	zdev->fid = fid;
792ba764dd7SNiklas Schnelle 	zdev->fh = fh;
793ba764dd7SNiklas Schnelle 
794ba764dd7SNiklas Schnelle 	/* Query function properties and update zdev */
795ba764dd7SNiklas Schnelle 	rc = clp_query_pci_fn(zdev);
796ba764dd7SNiklas Schnelle 	if (rc)
797ba764dd7SNiklas Schnelle 		goto error;
798ba764dd7SNiklas Schnelle 	zdev->state =  state;
799ba764dd7SNiklas Schnelle 
80005bc1be6SPierre Morel 	kref_init(&zdev->kref);
801ba764dd7SNiklas Schnelle 	mutex_init(&zdev->lock);
80209340b2fSMatthew Rosato 	mutex_init(&zdev->kzdev_lock);
803ba764dd7SNiklas Schnelle 
804ba764dd7SNiklas Schnelle 	rc = zpci_init_iommu(zdev);
805ba764dd7SNiklas Schnelle 	if (rc)
806ba764dd7SNiklas Schnelle 		goto error;
807ba764dd7SNiklas Schnelle 
808ba764dd7SNiklas Schnelle 	rc = zpci_bus_device_register(zdev, &pci_root_ops);
809ba764dd7SNiklas Schnelle 	if (rc)
810a50297cfSNiklas Schnelle 		goto error_destroy_iommu;
81105bc1be6SPierre Morel 
81205bc1be6SPierre Morel 	spin_lock(&zpci_list_lock);
81305bc1be6SPierre Morel 	list_add_tail(&zdev->entry, &zpci_list);
81405bc1be6SPierre Morel 	spin_unlock(&zpci_list_lock);
815cd248341SJan Glauber 
81614c87ba8SNiklas Schnelle 	return zdev;
817cd248341SJan Glauber 
818ba764dd7SNiklas Schnelle error_destroy_iommu:
819f42c2235SJoerg Roedel 	zpci_destroy_iommu(zdev);
820ba764dd7SNiklas Schnelle error:
821ba764dd7SNiklas Schnelle 	zpci_dbg(0, "add fid:%x, rc:%d\n", fid, rc);
822ba764dd7SNiklas Schnelle 	kfree(zdev);
82314c87ba8SNiklas Schnelle 	return ERR_PTR(rc);
824cd248341SJan Glauber }
825cd248341SJan Glauber 
zpci_is_device_configured(struct zpci_dev * zdev)826a46044a9SNiklas Schnelle bool zpci_is_device_configured(struct zpci_dev *zdev)
827a46044a9SNiklas Schnelle {
828a46044a9SNiklas Schnelle 	enum zpci_state state = zdev->state;
829a46044a9SNiklas Schnelle 
830a46044a9SNiklas Schnelle 	return state != ZPCI_FN_STATE_RESERVED &&
831a46044a9SNiklas Schnelle 		state != ZPCI_FN_STATE_STANDBY;
832a46044a9SNiklas Schnelle }
833a46044a9SNiklas Schnelle 
8342631f6b6SNiklas Schnelle /**
835a7f82c36SNiklas Schnelle  * zpci_scan_configured_device() - Scan a freshly configured zpci_dev
8362631f6b6SNiklas Schnelle  * @zdev: The zpci_dev to be configured
8372631f6b6SNiklas Schnelle  * @fh: The general function handle supplied by the platform
8382631f6b6SNiklas Schnelle  *
83961311e32SNiklas Schnelle  * Given a device in the configuration state Configured, enables, scans and
84045e5f0c0SNiklas Schnelle  * adds it to the common code PCI subsystem if possible. If any failure occurs,
84145e5f0c0SNiklas Schnelle  * the zpci_dev is left disabled.
8422631f6b6SNiklas Schnelle  *
8432631f6b6SNiklas Schnelle  * Return: 0 on success, or an error code otherwise
8442631f6b6SNiklas Schnelle  */
zpci_scan_configured_device(struct zpci_dev * zdev,u32 fh)845a7f82c36SNiklas Schnelle int zpci_scan_configured_device(struct zpci_dev *zdev, u32 fh)
8462631f6b6SNiklas Schnelle {
8474fe20497SNiklas Schnelle 	zpci_update_fh(zdev, fh);
84845e5f0c0SNiklas Schnelle 	return zpci_bus_scan_device(zdev);
8492631f6b6SNiklas Schnelle }
8502631f6b6SNiklas Schnelle 
8512631f6b6SNiklas Schnelle /**
8522631f6b6SNiklas Schnelle  * zpci_deconfigure_device() - Deconfigure a zpci_dev
8532631f6b6SNiklas Schnelle  * @zdev: The zpci_dev to configure
8542631f6b6SNiklas Schnelle  *
8552631f6b6SNiklas Schnelle  * Deconfigure a zPCI function that is currently configured and possibly known
8562631f6b6SNiklas Schnelle  * to the common code PCI subsystem.
8572631f6b6SNiklas Schnelle  * If any failure occurs the device is left as is.
8582631f6b6SNiklas Schnelle  *
8592631f6b6SNiklas Schnelle  * Return: 0 on success, or an error code otherwise
8602631f6b6SNiklas Schnelle  */
zpci_deconfigure_device(struct zpci_dev * zdev)8612631f6b6SNiklas Schnelle int zpci_deconfigure_device(struct zpci_dev *zdev)
8622631f6b6SNiklas Schnelle {
8632631f6b6SNiklas Schnelle 	int rc;
8642631f6b6SNiklas Schnelle 
8652631f6b6SNiklas Schnelle 	if (zdev->zbus->bus)
86695b3a8b4SNiklas Schnelle 		zpci_bus_remove_device(zdev, false);
8672631f6b6SNiklas Schnelle 
8681f3f7681SNiklas Schnelle 	if (zdev->dma_table) {
8691f3f7681SNiklas Schnelle 		rc = zpci_dma_exit_device(zdev);
8701f3f7681SNiklas Schnelle 		if (rc)
8711f3f7681SNiklas Schnelle 			return rc;
8721f3f7681SNiklas Schnelle 	}
8732631f6b6SNiklas Schnelle 	if (zdev_enabled(zdev)) {
8742631f6b6SNiklas Schnelle 		rc = zpci_disable_device(zdev);
8752631f6b6SNiklas Schnelle 		if (rc)
8762631f6b6SNiklas Schnelle 			return rc;
8772631f6b6SNiklas Schnelle 	}
8782631f6b6SNiklas Schnelle 
8792631f6b6SNiklas Schnelle 	rc = sclp_pci_deconfigure(zdev->fid);
8802631f6b6SNiklas Schnelle 	zpci_dbg(3, "deconf fid:%x, rc:%d\n", zdev->fid, rc);
8812631f6b6SNiklas Schnelle 	if (rc)
8822631f6b6SNiklas Schnelle 		return rc;
8832631f6b6SNiklas Schnelle 	zdev->state = ZPCI_FN_STATE_STANDBY;
8842631f6b6SNiklas Schnelle 
8852631f6b6SNiklas Schnelle 	return 0;
8862631f6b6SNiklas Schnelle }
8872631f6b6SNiklas Schnelle 
888a46044a9SNiklas Schnelle /**
889a46044a9SNiklas Schnelle  * zpci_device_reserved() - Mark device as resverved
890a46044a9SNiklas Schnelle  * @zdev: the zpci_dev that was reserved
891a46044a9SNiklas Schnelle  *
892a46044a9SNiklas Schnelle  * Handle the case that a given zPCI function was reserved by another system.
893a46044a9SNiklas Schnelle  * After a call to this function the zpci_dev can not be found via
894a46044a9SNiklas Schnelle  * get_zdev_by_fid() anymore but may still be accessible via existing
895a46044a9SNiklas Schnelle  * references though it will not be functional anymore.
896a46044a9SNiklas Schnelle  */
zpci_device_reserved(struct zpci_dev * zdev)897a46044a9SNiklas Schnelle void zpci_device_reserved(struct zpci_dev *zdev)
898a46044a9SNiklas Schnelle {
899a46044a9SNiklas Schnelle 	if (zdev->has_hp_slot)
900a46044a9SNiklas Schnelle 		zpci_exit_slot(zdev);
901a46044a9SNiklas Schnelle 	/*
902a46044a9SNiklas Schnelle 	 * Remove device from zpci_list as it is going away. This also
903a46044a9SNiklas Schnelle 	 * makes sure we ignore subsequent zPCI events for this device.
904a46044a9SNiklas Schnelle 	 */
905a46044a9SNiklas Schnelle 	spin_lock(&zpci_list_lock);
906a46044a9SNiklas Schnelle 	list_del(&zdev->entry);
907a46044a9SNiklas Schnelle 	spin_unlock(&zpci_list_lock);
908a46044a9SNiklas Schnelle 	zdev->state = ZPCI_FN_STATE_RESERVED;
909a46044a9SNiklas Schnelle 	zpci_dbg(3, "rsv fid:%x\n", zdev->fid);
910a46044a9SNiklas Schnelle 	zpci_zdev_put(zdev);
911a46044a9SNiklas Schnelle }
912a46044a9SNiklas Schnelle 
zpci_release_device(struct kref * kref)91305bc1be6SPierre Morel void zpci_release_device(struct kref *kref)
914623bd44dSSebastian Ott {
91505bc1be6SPierre Morel 	struct zpci_dev *zdev = container_of(kref, struct zpci_dev, kref);
916a9045c22SNiklas Schnelle 	int ret;
917623bd44dSSebastian Ott 
9182f0230b2SNiklas Schnelle 	if (zdev->zbus->bus)
91995b3a8b4SNiklas Schnelle 		zpci_bus_remove_device(zdev, false);
92044510d6fSPierre Morel 
9211f3f7681SNiklas Schnelle 	if (zdev->dma_table)
9221f3f7681SNiklas Schnelle 		zpci_dma_exit_device(zdev);
923f6576a1bSNiklas Schnelle 	if (zdev_enabled(zdev))
92405bc1be6SPierre Morel 		zpci_disable_device(zdev);
925f6576a1bSNiklas Schnelle 
926f6576a1bSNiklas Schnelle 	switch (zdev->state) {
927a9045c22SNiklas Schnelle 	case ZPCI_FN_STATE_CONFIGURED:
928a9045c22SNiklas Schnelle 		ret = sclp_pci_deconfigure(zdev->fid);
929a9045c22SNiklas Schnelle 		zpci_dbg(3, "deconf fid:%x, rc:%d\n", zdev->fid, ret);
930a9045c22SNiklas Schnelle 		fallthrough;
93105bc1be6SPierre Morel 	case ZPCI_FN_STATE_STANDBY:
93244510d6fSPierre Morel 		if (zdev->has_hp_slot)
93305bc1be6SPierre Morel 			zpci_exit_slot(zdev);
934a46044a9SNiklas Schnelle 		spin_lock(&zpci_list_lock);
935a46044a9SNiklas Schnelle 		list_del(&zdev->entry);
936a46044a9SNiklas Schnelle 		spin_unlock(&zpci_list_lock);
937a46044a9SNiklas Schnelle 		zpci_dbg(3, "rsv fid:%x\n", zdev->fid);
938a46044a9SNiklas Schnelle 		fallthrough;
939a46044a9SNiklas Schnelle 	case ZPCI_FN_STATE_RESERVED:
94002368b7cSNiklas Schnelle 		if (zdev->has_resources)
94105bc1be6SPierre Morel 			zpci_cleanup_bus_resources(zdev);
94205bc1be6SPierre Morel 		zpci_bus_device_unregister(zdev);
94305bc1be6SPierre Morel 		zpci_destroy_iommu(zdev);
94405bc1be6SPierre Morel 		fallthrough;
94505bc1be6SPierre Morel 	default:
94605bc1be6SPierre Morel 		break;
94705bc1be6SPierre Morel 	}
94805bc1be6SPierre Morel 	zpci_dbg(3, "rem fid:%x\n", zdev->fid);
9492ba8336dSNiklas Schnelle 	kfree_rcu(zdev, rcu);
950623bd44dSSebastian Ott }
951623bd44dSSebastian Ott 
zpci_report_error(struct pci_dev * pdev,struct zpci_report_error_header * report)952bd3a1725SMartin Schwidefsky int zpci_report_error(struct pci_dev *pdev,
953bd3a1725SMartin Schwidefsky 		      struct zpci_report_error_header *report)
954bd3a1725SMartin Schwidefsky {
955bd3a1725SMartin Schwidefsky 	struct zpci_dev *zdev = to_zpci(pdev);
956bd3a1725SMartin Schwidefsky 
957bd3a1725SMartin Schwidefsky 	return sclp_pci_report(report, zdev->fh, zdev->fid);
958bd3a1725SMartin Schwidefsky }
959bd3a1725SMartin Schwidefsky EXPORT_SYMBOL(zpci_report_error);
960bd3a1725SMartin Schwidefsky 
9614cdf2f4eSNiklas Schnelle /**
9624cdf2f4eSNiklas Schnelle  * zpci_clear_error_state() - Clears the zPCI error state of the device
9634cdf2f4eSNiklas Schnelle  * @zdev: The zdev for which the zPCI error state should be reset
9644cdf2f4eSNiklas Schnelle  *
9654cdf2f4eSNiklas Schnelle  * Clear the zPCI error state of the device. If clearing the zPCI error state
9664cdf2f4eSNiklas Schnelle  * fails the device is left in the error state. In this case it may make sense
9674cdf2f4eSNiklas Schnelle  * to call zpci_io_perm_failure() on the associated pdev if it exists.
9684cdf2f4eSNiklas Schnelle  *
9694cdf2f4eSNiklas Schnelle  * Returns: 0 on success, -EIO otherwise
9704cdf2f4eSNiklas Schnelle  */
zpci_clear_error_state(struct zpci_dev * zdev)9714cdf2f4eSNiklas Schnelle int zpci_clear_error_state(struct zpci_dev *zdev)
9724cdf2f4eSNiklas Schnelle {
9734cdf2f4eSNiklas Schnelle 	u64 req = ZPCI_CREATE_REQ(zdev->fh, 0, ZPCI_MOD_FC_RESET_ERROR);
9744cdf2f4eSNiklas Schnelle 	struct zpci_fib fib = {0};
9754cdf2f4eSNiklas Schnelle 	u8 status;
9764cdf2f4eSNiklas Schnelle 	int cc;
9774cdf2f4eSNiklas Schnelle 
9784cdf2f4eSNiklas Schnelle 	cc = zpci_mod_fc(req, &fib, &status);
9794cdf2f4eSNiklas Schnelle 	if (cc) {
9804cdf2f4eSNiklas Schnelle 		zpci_dbg(3, "ces fid:%x, cc:%d, status:%x\n", zdev->fid, cc, status);
9814cdf2f4eSNiklas Schnelle 		return -EIO;
9824cdf2f4eSNiklas Schnelle 	}
9834cdf2f4eSNiklas Schnelle 
9844cdf2f4eSNiklas Schnelle 	return 0;
9854cdf2f4eSNiklas Schnelle }
9864cdf2f4eSNiklas Schnelle 
9874cdf2f4eSNiklas Schnelle /**
9884cdf2f4eSNiklas Schnelle  * zpci_reset_load_store_blocked() - Re-enables L/S from error state
9894cdf2f4eSNiklas Schnelle  * @zdev: The zdev for which to unblock load/store access
9904cdf2f4eSNiklas Schnelle  *
9914cdf2f4eSNiklas Schnelle  * Re-enables load/store access for a PCI function in the error state while
9924cdf2f4eSNiklas Schnelle  * keeping DMA blocked. In this state drivers can poke MMIO space to determine
9934cdf2f4eSNiklas Schnelle  * if error recovery is possible while catching any rogue DMA access from the
9944cdf2f4eSNiklas Schnelle  * device.
9954cdf2f4eSNiklas Schnelle  *
9964cdf2f4eSNiklas Schnelle  * Returns: 0 on success, -EIO otherwise
9974cdf2f4eSNiklas Schnelle  */
zpci_reset_load_store_blocked(struct zpci_dev * zdev)9984cdf2f4eSNiklas Schnelle int zpci_reset_load_store_blocked(struct zpci_dev *zdev)
9994cdf2f4eSNiklas Schnelle {
10004cdf2f4eSNiklas Schnelle 	u64 req = ZPCI_CREATE_REQ(zdev->fh, 0, ZPCI_MOD_FC_RESET_BLOCK);
10014cdf2f4eSNiklas Schnelle 	struct zpci_fib fib = {0};
10024cdf2f4eSNiklas Schnelle 	u8 status;
10034cdf2f4eSNiklas Schnelle 	int cc;
10044cdf2f4eSNiklas Schnelle 
10054cdf2f4eSNiklas Schnelle 	cc = zpci_mod_fc(req, &fib, &status);
10064cdf2f4eSNiklas Schnelle 	if (cc) {
10074cdf2f4eSNiklas Schnelle 		zpci_dbg(3, "rls fid:%x, cc:%d, status:%x\n", zdev->fid, cc, status);
10084cdf2f4eSNiklas Schnelle 		return -EIO;
10094cdf2f4eSNiklas Schnelle 	}
10104cdf2f4eSNiklas Schnelle 
10114cdf2f4eSNiklas Schnelle 	return 0;
10124cdf2f4eSNiklas Schnelle }
10134cdf2f4eSNiklas Schnelle 
zpci_mem_init(void)1014cd248341SJan Glauber static int zpci_mem_init(void)
1015cd248341SJan Glauber {
101680c544deSSebastian Ott 	BUILD_BUG_ON(!is_power_of_2(__alignof__(struct zpci_fmb)) ||
101780c544deSSebastian Ott 		     __alignof__(struct zpci_fmb) < sizeof(struct zpci_fmb));
101880c544deSSebastian Ott 
1019d0b08853SJan Glauber 	zdev_fmb_cache = kmem_cache_create("PCI_FMB_cache", sizeof(struct zpci_fmb),
102080c544deSSebastian Ott 					   __alignof__(struct zpci_fmb), 0, NULL);
1021d0b08853SJan Glauber 	if (!zdev_fmb_cache)
1022c506fff3SSebastian Ott 		goto error_fmb;
1023d0b08853SJan Glauber 
1024c506fff3SSebastian Ott 	zpci_iomap_start = kcalloc(ZPCI_IOMAP_ENTRIES,
1025c506fff3SSebastian Ott 				   sizeof(*zpci_iomap_start), GFP_KERNEL);
1026cd248341SJan Glauber 	if (!zpci_iomap_start)
10279a4da8a5SJan Glauber 		goto error_iomap;
1028cd248341SJan Glauber 
1029c506fff3SSebastian Ott 	zpci_iomap_bitmap = kcalloc(BITS_TO_LONGS(ZPCI_IOMAP_ENTRIES),
1030c506fff3SSebastian Ott 				    sizeof(*zpci_iomap_bitmap), GFP_KERNEL);
1031c506fff3SSebastian Ott 	if (!zpci_iomap_bitmap)
1032c506fff3SSebastian Ott 		goto error_iomap_bitmap;
1033c506fff3SSebastian Ott 
1034b02002ccSNiklas Schnelle 	if (static_branch_likely(&have_mio))
1035b02002ccSNiklas Schnelle 		clp_setup_writeback_mio();
1036b02002ccSNiklas Schnelle 
1037c506fff3SSebastian Ott 	return 0;
1038c506fff3SSebastian Ott error_iomap_bitmap:
1039c506fff3SSebastian Ott 	kfree(zpci_iomap_start);
10409a4da8a5SJan Glauber error_iomap:
1041d0b08853SJan Glauber 	kmem_cache_destroy(zdev_fmb_cache);
1042c506fff3SSebastian Ott error_fmb:
1043cd248341SJan Glauber 	return -ENOMEM;
1044cd248341SJan Glauber }
1045cd248341SJan Glauber 
zpci_mem_exit(void)1046cd248341SJan Glauber static void zpci_mem_exit(void)
1047cd248341SJan Glauber {
1048c506fff3SSebastian Ott 	kfree(zpci_iomap_bitmap);
1049cd248341SJan Glauber 	kfree(zpci_iomap_start);
1050d0b08853SJan Glauber 	kmem_cache_destroy(zdev_fmb_cache);
1051cd248341SJan Glauber }
1052cd248341SJan Glauber 
10536324b4deSSebastian Ott static unsigned int s390_pci_probe __initdata = 1;
1054fbfe07d4SSebastian Ott unsigned int s390_pci_force_floating __initdata;
1055aa3b7c29SSebastian Ott static unsigned int s390_pci_initialized;
1056cd248341SJan Glauber 
pcibios_setup(char * str)1057cd248341SJan Glauber char * __init pcibios_setup(char *str)
1058cd248341SJan Glauber {
1059257608fbSSebastian Ott 	if (!strcmp(str, "off")) {
1060257608fbSSebastian Ott 		s390_pci_probe = 0;
1061cd248341SJan Glauber 		return NULL;
1062cd248341SJan Glauber 	}
106356271303SSebastian Ott 	if (!strcmp(str, "nomio")) {
10643322ba0dSNiklas Schnelle 		S390_lowcore.machine_flags &= ~MACHINE_FLAG_PCI_MIO;
106556271303SSebastian Ott 		return NULL;
106656271303SSebastian Ott 	}
1067fbfe07d4SSebastian Ott 	if (!strcmp(str, "force_floating")) {
1068fbfe07d4SSebastian Ott 		s390_pci_force_floating = 1;
1069fbfe07d4SSebastian Ott 		return NULL;
1070fbfe07d4SSebastian Ott 	}
10716cf17f9aSPierre Morel 	if (!strcmp(str, "norid")) {
10726cf17f9aSPierre Morel 		s390_pci_no_rid = 1;
10736cf17f9aSPierre Morel 		return NULL;
10746cf17f9aSPierre Morel 	}
1075cd248341SJan Glauber 	return str;
1076cd248341SJan Glauber }
1077cd248341SJan Glauber 
zpci_is_enabled(void)1078aa3b7c29SSebastian Ott bool zpci_is_enabled(void)
1079aa3b7c29SSebastian Ott {
1080aa3b7c29SSebastian Ott 	return s390_pci_initialized;
1081aa3b7c29SSebastian Ott }
1082aa3b7c29SSebastian Ott 
pci_base_init(void)1083cd248341SJan Glauber static int __init pci_base_init(void)
1084cd248341SJan Glauber {
1085cd248341SJan Glauber 	int rc;
1086cd248341SJan Glauber 
10871e5635d1SHeiko Carstens 	if (!s390_pci_probe)
1088cd248341SJan Glauber 		return 0;
1089cd248341SJan Glauber 
1090da78693eSNiklas Schnelle 	if (!test_facility(69) || !test_facility(71)) {
1091da78693eSNiklas Schnelle 		pr_info("PCI is not supported because CPU facilities 69 or 71 are not available\n");
1092cd248341SJan Glauber 		return 0;
1093da78693eSNiklas Schnelle 	}
1094cd248341SJan Glauber 
10953322ba0dSNiklas Schnelle 	if (MACHINE_HAS_PCI_MIO) {
109671ba41c9SSebastian Ott 		static_branch_enable(&have_mio);
10979964f396SSebastian Ott 		ctl_set_bit(2, 5);
10989964f396SSebastian Ott 	}
109971ba41c9SSebastian Ott 
1100d0b08853SJan Glauber 	rc = zpci_debug_init();
1101d0b08853SJan Glauber 	if (rc)
11021f44a225SMartin Schwidefsky 		goto out;
1103d0b08853SJan Glauber 
1104cd248341SJan Glauber 	rc = zpci_mem_init();
1105cd248341SJan Glauber 	if (rc)
1106cd248341SJan Glauber 		goto out_mem;
1107cd248341SJan Glauber 
11089a4da8a5SJan Glauber 	rc = zpci_irq_init();
11099a4da8a5SJan Glauber 	if (rc)
11109a4da8a5SJan Glauber 		goto out_irq;
11119a4da8a5SJan Glauber 
1112828b35f6SJan Glauber 	rc = zpci_dma_init();
1113828b35f6SJan Glauber 	if (rc)
1114828b35f6SJan Glauber 		goto out_dma;
1115828b35f6SJan Glauber 
11161d578966SSebastian Ott 	rc = clp_scan_pci_devices();
1117a755a45dSJan Glauber 	if (rc)
1118a755a45dSJan Glauber 		goto out_find;
111914c87ba8SNiklas Schnelle 	zpci_bus_scan_busses();
1120a755a45dSJan Glauber 
1121aa3b7c29SSebastian Ott 	s390_pci_initialized = 1;
1122cd248341SJan Glauber 	return 0;
1123cd248341SJan Glauber 
1124a755a45dSJan Glauber out_find:
1125828b35f6SJan Glauber 	zpci_dma_exit();
1126828b35f6SJan Glauber out_dma:
11279a4da8a5SJan Glauber 	zpci_irq_exit();
11289a4da8a5SJan Glauber out_irq:
1129cd248341SJan Glauber 	zpci_mem_exit();
1130cd248341SJan Glauber out_mem:
1131d0b08853SJan Glauber 	zpci_debug_exit();
11321f44a225SMartin Schwidefsky out:
1133cd248341SJan Glauber 	return rc;
1134cd248341SJan Glauber }
113567f43f38SSebastian Ott subsys_initcall_sync(pci_base_init);
1136