xref: /linux/arch/s390/pci/pci.c (revision 71ba41c9b1d91042960e9d92a5c8f52dc8531eda)
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>
28*71ba41c9SSebastian Ott #include <linux/jump_label.h>
29cd248341SJan Glauber #include <linux/pci.h>
30cd248341SJan Glauber 
319a4da8a5SJan Glauber #include <asm/isc.h>
329a4da8a5SJan Glauber #include <asm/airq.h>
33cd248341SJan Glauber #include <asm/facility.h>
34cd248341SJan Glauber #include <asm/pci_insn.h>
35a755a45dSJan Glauber #include <asm/pci_clp.h>
36828b35f6SJan Glauber #include <asm/pci_dma.h>
37cd248341SJan Glauber 
38cd248341SJan Glauber /* list of all detected zpci devices */
3967f43f38SSebastian Ott static LIST_HEAD(zpci_list);
4057b5918cSSebastian Ott static DEFINE_SPINLOCK(zpci_list_lock);
411f44a225SMartin Schwidefsky 
42cd248341SJan Glauber static DECLARE_BITMAP(zpci_domain, ZPCI_NR_DEVICES);
43cd248341SJan Glauber static DEFINE_SPINLOCK(zpci_domain_lock);
44cd248341SJan Glauber 
45c506fff3SSebastian Ott #define ZPCI_IOMAP_ENTRIES						\
46b0c8ce89SSebastian Ott 	min(((unsigned long) ZPCI_NR_DEVICES * PCI_BAR_COUNT / 2),	\
47c506fff3SSebastian Ott 	    ZPCI_IOMAP_MAX_ENTRIES)
48c506fff3SSebastian Ott 
49cd248341SJan Glauber static DEFINE_SPINLOCK(zpci_iomap_lock);
50c506fff3SSebastian Ott static unsigned long *zpci_iomap_bitmap;
51cd248341SJan Glauber struct zpci_iomap_entry *zpci_iomap_start;
52cd248341SJan Glauber EXPORT_SYMBOL_GPL(zpci_iomap_start);
53cd248341SJan Glauber 
54*71ba41c9SSebastian Ott DEFINE_STATIC_KEY_FALSE(have_mio);
55*71ba41c9SSebastian Ott 
56d0b08853SJan Glauber static struct kmem_cache *zdev_fmb_cache;
57d0b08853SJan Glauber 
58cd248341SJan Glauber struct zpci_dev *get_zdev_by_fid(u32 fid)
59cd248341SJan Glauber {
60cd248341SJan Glauber 	struct zpci_dev *tmp, *zdev = NULL;
61cd248341SJan Glauber 
6257b5918cSSebastian Ott 	spin_lock(&zpci_list_lock);
63cd248341SJan Glauber 	list_for_each_entry(tmp, &zpci_list, entry) {
64cd248341SJan Glauber 		if (tmp->fid == fid) {
65cd248341SJan Glauber 			zdev = tmp;
66cd248341SJan Glauber 			break;
67cd248341SJan Glauber 		}
68cd248341SJan Glauber 	}
6957b5918cSSebastian Ott 	spin_unlock(&zpci_list_lock);
70cd248341SJan Glauber 	return zdev;
71cd248341SJan Glauber }
72cd248341SJan Glauber 
7301553d9aSSebastian Ott void zpci_remove_reserved_devices(void)
7401553d9aSSebastian Ott {
7501553d9aSSebastian Ott 	struct zpci_dev *tmp, *zdev;
7601553d9aSSebastian Ott 	enum zpci_state state;
7701553d9aSSebastian Ott 	LIST_HEAD(remove);
7801553d9aSSebastian Ott 
7901553d9aSSebastian Ott 	spin_lock(&zpci_list_lock);
8001553d9aSSebastian Ott 	list_for_each_entry_safe(zdev, tmp, &zpci_list, entry) {
8101553d9aSSebastian Ott 		if (zdev->state == ZPCI_FN_STATE_STANDBY &&
8201553d9aSSebastian Ott 		    !clp_get_state(zdev->fid, &state) &&
8301553d9aSSebastian Ott 		    state == ZPCI_FN_STATE_RESERVED)
8401553d9aSSebastian Ott 			list_move_tail(&zdev->entry, &remove);
8501553d9aSSebastian Ott 	}
8601553d9aSSebastian Ott 	spin_unlock(&zpci_list_lock);
8701553d9aSSebastian Ott 
8801553d9aSSebastian Ott 	list_for_each_entry_safe(zdev, tmp, &remove, entry)
8901553d9aSSebastian Ott 		zpci_remove_device(zdev);
9001553d9aSSebastian Ott }
9101553d9aSSebastian Ott 
92cd248341SJan Glauber static struct zpci_dev *get_zdev_by_bus(struct pci_bus *bus)
93cd248341SJan Glauber {
94cd248341SJan Glauber 	return (bus && bus->sysdata) ? (struct zpci_dev *) bus->sysdata : NULL;
95cd248341SJan Glauber }
96cd248341SJan Glauber 
97cd248341SJan Glauber int pci_domain_nr(struct pci_bus *bus)
98cd248341SJan Glauber {
99cd248341SJan Glauber 	return ((struct zpci_dev *) bus->sysdata)->domain;
100cd248341SJan Glauber }
101cd248341SJan Glauber EXPORT_SYMBOL_GPL(pci_domain_nr);
102cd248341SJan Glauber 
103cd248341SJan Glauber int pci_proc_domain(struct pci_bus *bus)
104cd248341SJan Glauber {
105cd248341SJan Glauber 	return pci_domain_nr(bus);
106cd248341SJan Glauber }
107cd248341SJan Glauber EXPORT_SYMBOL_GPL(pci_proc_domain);
108cd248341SJan Glauber 
109828b35f6SJan Glauber /* Modify PCI: Register I/O address translation parameters */
110828b35f6SJan Glauber int zpci_register_ioat(struct zpci_dev *zdev, u8 dmaas,
111828b35f6SJan Glauber 		       u64 base, u64 limit, u64 iota)
112828b35f6SJan Glauber {
11372570834SSebastian Ott 	u64 req = ZPCI_CREATE_REQ(zdev->fh, dmaas, ZPCI_MOD_FC_REG_IOAT);
11472570834SSebastian Ott 	struct zpci_fib fib = {0};
11572570834SSebastian Ott 	u8 status;
116828b35f6SJan Glauber 
117828b35f6SJan Glauber 	WARN_ON_ONCE(iota & 0x3fff);
11872570834SSebastian Ott 	fib.pba = base;
11972570834SSebastian Ott 	fib.pal = limit;
12072570834SSebastian Ott 	fib.iota = iota | ZPCI_IOTA_RTTO_FLAG;
12172570834SSebastian Ott 	return zpci_mod_fc(req, &fib, &status) ? -EIO : 0;
122828b35f6SJan Glauber }
123828b35f6SJan Glauber 
124828b35f6SJan Glauber /* Modify PCI: Unregister I/O address translation parameters */
125828b35f6SJan Glauber int zpci_unregister_ioat(struct zpci_dev *zdev, u8 dmaas)
126828b35f6SJan Glauber {
12772570834SSebastian Ott 	u64 req = ZPCI_CREATE_REQ(zdev->fh, dmaas, ZPCI_MOD_FC_DEREG_IOAT);
12872570834SSebastian Ott 	struct zpci_fib fib = {0};
12972570834SSebastian Ott 	u8 cc, status;
130828b35f6SJan Glauber 
13172570834SSebastian Ott 	cc = zpci_mod_fc(req, &fib, &status);
13272570834SSebastian Ott 	if (cc == 3) /* Function already gone. */
13372570834SSebastian Ott 		cc = 0;
13472570834SSebastian Ott 	return cc ? -EIO : 0;
135828b35f6SJan Glauber }
136828b35f6SJan Glauber 
137d0b08853SJan Glauber /* Modify PCI: Set PCI function measurement parameters */
138d0b08853SJan Glauber int zpci_fmb_enable_device(struct zpci_dev *zdev)
139d0b08853SJan Glauber {
1404e5bd780SSebastian Ott 	u64 req = ZPCI_CREATE_REQ(zdev->fh, 0, ZPCI_MOD_FC_SET_MEASURE);
1414e5bd780SSebastian Ott 	struct zpci_fib fib = {0};
1424e5bd780SSebastian Ott 	u8 cc, status;
143d0b08853SJan Glauber 
1440b7589ecSSebastian Ott 	if (zdev->fmb || sizeof(*zdev->fmb) < zdev->fmb_length)
145d0b08853SJan Glauber 		return -EINVAL;
146d0b08853SJan Glauber 
14708b42124SWei Yongjun 	zdev->fmb = kmem_cache_zalloc(zdev_fmb_cache, GFP_KERNEL);
148d0b08853SJan Glauber 	if (!zdev->fmb)
149d0b08853SJan Glauber 		return -ENOMEM;
150d0b08853SJan Glauber 	WARN_ON((u64) zdev->fmb & 0xf);
151d0b08853SJan Glauber 
1526001018aSSebastian Ott 	/* reset software counters */
1536001018aSSebastian Ott 	atomic64_set(&zdev->allocated_pages, 0);
1546001018aSSebastian Ott 	atomic64_set(&zdev->mapped_pages, 0);
1556001018aSSebastian Ott 	atomic64_set(&zdev->unmapped_pages, 0);
1566001018aSSebastian Ott 
1574e5bd780SSebastian Ott 	fib.fmb_addr = virt_to_phys(zdev->fmb);
1584e5bd780SSebastian Ott 	cc = zpci_mod_fc(req, &fib, &status);
1594e5bd780SSebastian Ott 	if (cc) {
1604e5bd780SSebastian Ott 		kmem_cache_free(zdev_fmb_cache, zdev->fmb);
1614e5bd780SSebastian Ott 		zdev->fmb = NULL;
1624e5bd780SSebastian Ott 	}
1634e5bd780SSebastian Ott 	return cc ? -EIO : 0;
164d0b08853SJan Glauber }
165d0b08853SJan Glauber 
166d0b08853SJan Glauber /* Modify PCI: Disable PCI function measurement */
167d0b08853SJan Glauber int zpci_fmb_disable_device(struct zpci_dev *zdev)
168d0b08853SJan Glauber {
1694e5bd780SSebastian Ott 	u64 req = ZPCI_CREATE_REQ(zdev->fh, 0, ZPCI_MOD_FC_SET_MEASURE);
1704e5bd780SSebastian Ott 	struct zpci_fib fib = {0};
1714e5bd780SSebastian Ott 	u8 cc, status;
172d0b08853SJan Glauber 
173d0b08853SJan Glauber 	if (!zdev->fmb)
174d0b08853SJan Glauber 		return -EINVAL;
175d0b08853SJan Glauber 
176d0b08853SJan Glauber 	/* Function measurement is disabled if fmb address is zero */
1774e5bd780SSebastian Ott 	cc = zpci_mod_fc(req, &fib, &status);
1784e5bd780SSebastian Ott 	if (cc == 3) /* Function already gone. */
1794e5bd780SSebastian Ott 		cc = 0;
180d0b08853SJan Glauber 
1814e5bd780SSebastian Ott 	if (!cc) {
182d0b08853SJan Glauber 		kmem_cache_free(zdev_fmb_cache, zdev->fmb);
183d0b08853SJan Glauber 		zdev->fmb = NULL;
1844e5bd780SSebastian Ott 	}
1854e5bd780SSebastian Ott 	return cc ? -EIO : 0;
186d0b08853SJan Glauber }
187d0b08853SJan Glauber 
188cd248341SJan Glauber static int zpci_cfg_load(struct zpci_dev *zdev, int offset, u32 *val, u8 len)
189cd248341SJan Glauber {
190cd248341SJan Glauber 	u64 req = ZPCI_CREATE_REQ(zdev->fh, ZPCI_PCIAS_CFGSPC, len);
191cd248341SJan Glauber 	u64 data;
192cd248341SJan Glauber 	int rc;
193cd248341SJan Glauber 
19481deca12SSebastian Ott 	rc = __zpci_load(&data, req, offset);
195b170bad4SSebastian Ott 	if (!rc) {
1965064cd35SSebastian Ott 		data = le64_to_cpu((__force __le64) data);
1975064cd35SSebastian Ott 		data >>= (8 - len) * 8;
198cd248341SJan Glauber 		*val = (u32) data;
199b170bad4SSebastian Ott 	} else
200cd248341SJan Glauber 		*val = 0xffffffff;
201cd248341SJan Glauber 	return rc;
202cd248341SJan Glauber }
203cd248341SJan Glauber 
204cd248341SJan Glauber static int zpci_cfg_store(struct zpci_dev *zdev, int offset, u32 val, u8 len)
205cd248341SJan Glauber {
206cd248341SJan Glauber 	u64 req = ZPCI_CREATE_REQ(zdev->fh, ZPCI_PCIAS_CFGSPC, len);
207cd248341SJan Glauber 	u64 data = val;
208cd248341SJan Glauber 	int rc;
209cd248341SJan Glauber 
2105064cd35SSebastian Ott 	data <<= (8 - len) * 8;
2115064cd35SSebastian Ott 	data = (__force u64) cpu_to_le64(data);
21281deca12SSebastian Ott 	rc = __zpci_store(data, req, offset);
213cd248341SJan Glauber 	return rc;
214cd248341SJan Glauber }
215cd248341SJan Glauber 
216cd248341SJan Glauber resource_size_t pcibios_align_resource(void *data, const struct resource *res,
217cd248341SJan Glauber 				       resource_size_t size,
218cd248341SJan Glauber 				       resource_size_t align)
219cd248341SJan Glauber {
220cd248341SJan Glauber 	return 0;
221cd248341SJan Glauber }
222cd248341SJan Glauber 
22387bc359bSJan Glauber /* combine single writes by using store-block insn */
22487bc359bSJan Glauber void __iowrite64_copy(void __iomem *to, const void *from, size_t count)
22587bc359bSJan Glauber {
22687bc359bSJan Glauber        zpci_memcpy_toio(to, from, count);
22787bc359bSJan Glauber }
22887bc359bSJan Glauber 
229*71ba41c9SSebastian Ott void __iomem *ioremap(unsigned long ioaddr, unsigned long size)
230*71ba41c9SSebastian Ott {
231*71ba41c9SSebastian Ott 	struct vm_struct *area;
232*71ba41c9SSebastian Ott 	unsigned long offset;
233*71ba41c9SSebastian Ott 
234*71ba41c9SSebastian Ott 	if (!size)
235*71ba41c9SSebastian Ott 		return NULL;
236*71ba41c9SSebastian Ott 
237*71ba41c9SSebastian Ott 	if (!static_branch_unlikely(&have_mio))
238*71ba41c9SSebastian Ott 		return (void __iomem *) ioaddr;
239*71ba41c9SSebastian Ott 
240*71ba41c9SSebastian Ott 	offset = ioaddr & ~PAGE_MASK;
241*71ba41c9SSebastian Ott 	ioaddr &= PAGE_MASK;
242*71ba41c9SSebastian Ott 	size = PAGE_ALIGN(size + offset);
243*71ba41c9SSebastian Ott 	area = get_vm_area(size, VM_IOREMAP);
244*71ba41c9SSebastian Ott 	if (!area)
245*71ba41c9SSebastian Ott 		return NULL;
246*71ba41c9SSebastian Ott 
247*71ba41c9SSebastian Ott 	if (ioremap_page_range((unsigned long) area->addr,
248*71ba41c9SSebastian Ott 			       (unsigned long) area->addr + size,
249*71ba41c9SSebastian Ott 			       ioaddr, PAGE_KERNEL)) {
250*71ba41c9SSebastian Ott 		vunmap(area->addr);
251*71ba41c9SSebastian Ott 		return NULL;
252*71ba41c9SSebastian Ott 	}
253*71ba41c9SSebastian Ott 	return (void __iomem *) ((unsigned long) area->addr + offset);
254*71ba41c9SSebastian Ott }
255*71ba41c9SSebastian Ott EXPORT_SYMBOL(ioremap);
256*71ba41c9SSebastian Ott 
257*71ba41c9SSebastian Ott void iounmap(volatile void __iomem *addr)
258*71ba41c9SSebastian Ott {
259*71ba41c9SSebastian Ott 	if (static_branch_likely(&have_mio))
260*71ba41c9SSebastian Ott 		vunmap((__force void *) ((unsigned long) addr & PAGE_MASK));
261*71ba41c9SSebastian Ott }
262*71ba41c9SSebastian Ott EXPORT_SYMBOL(iounmap);
263*71ba41c9SSebastian Ott 
264cd248341SJan Glauber /* Create a virtual mapping cookie for a PCI BAR */
265*71ba41c9SSebastian Ott static void __iomem *pci_iomap_range_fh(struct pci_dev *pdev, int bar,
266*71ba41c9SSebastian Ott 					unsigned long offset, unsigned long max)
267cd248341SJan Glauber {
268198a5278SSebastian Ott 	struct zpci_dev *zdev =	to_zpci(pdev);
269cd248341SJan Glauber 	int idx;
270cd248341SJan Glauber 
271cd248341SJan Glauber 	idx = zdev->bars[bar].map_idx;
272cd248341SJan Glauber 	spin_lock(&zpci_iomap_lock);
273f5e44f82SSebastian Ott 	/* Detect overrun */
274f5e44f82SSebastian Ott 	WARN_ON(!++zpci_iomap_start[idx].count);
275cd248341SJan Glauber 	zpci_iomap_start[idx].fh = zdev->fh;
276cd248341SJan Glauber 	zpci_iomap_start[idx].bar = bar;
277cd248341SJan Glauber 	spin_unlock(&zpci_iomap_lock);
278cd248341SJan Glauber 
2799e00caaeSSebastian Ott 	return (void __iomem *) ZPCI_ADDR(idx) + offset;
280cd248341SJan Glauber }
281*71ba41c9SSebastian Ott 
282*71ba41c9SSebastian Ott static void __iomem *pci_iomap_range_mio(struct pci_dev *pdev, int bar,
283*71ba41c9SSebastian Ott 					 unsigned long offset,
284*71ba41c9SSebastian Ott 					 unsigned long max)
285*71ba41c9SSebastian Ott {
286*71ba41c9SSebastian Ott 	unsigned long barsize = pci_resource_len(pdev, bar);
287*71ba41c9SSebastian Ott 	struct zpci_dev *zdev = to_zpci(pdev);
288*71ba41c9SSebastian Ott 	void __iomem *iova;
289*71ba41c9SSebastian Ott 
290*71ba41c9SSebastian Ott 	iova = ioremap((unsigned long) zdev->bars[bar].mio_wt, barsize);
291*71ba41c9SSebastian Ott 	return iova ? iova + offset : iova;
292*71ba41c9SSebastian Ott }
293*71ba41c9SSebastian Ott 
294*71ba41c9SSebastian Ott void __iomem *pci_iomap_range(struct pci_dev *pdev, int bar,
295*71ba41c9SSebastian Ott 			      unsigned long offset, unsigned long max)
296*71ba41c9SSebastian Ott {
297*71ba41c9SSebastian Ott 	if (!pci_resource_len(pdev, bar) || bar >= PCI_BAR_COUNT)
298*71ba41c9SSebastian Ott 		return NULL;
299*71ba41c9SSebastian Ott 
300*71ba41c9SSebastian Ott 	if (static_branch_likely(&have_mio))
301*71ba41c9SSebastian Ott 		return pci_iomap_range_mio(pdev, bar, offset, max);
302*71ba41c9SSebastian Ott 	else
303*71ba41c9SSebastian Ott 		return pci_iomap_range_fh(pdev, bar, offset, max);
304*71ba41c9SSebastian Ott }
305d9426083SSebastian Ott EXPORT_SYMBOL(pci_iomap_range);
3068cfc99b5SMichael S. Tsirkin 
3078cfc99b5SMichael S. Tsirkin void __iomem *pci_iomap(struct pci_dev *dev, int bar, unsigned long maxlen)
3088cfc99b5SMichael S. Tsirkin {
3098cfc99b5SMichael S. Tsirkin 	return pci_iomap_range(dev, bar, 0, maxlen);
3108cfc99b5SMichael S. Tsirkin }
3118cfc99b5SMichael S. Tsirkin EXPORT_SYMBOL(pci_iomap);
312cd248341SJan Glauber 
313*71ba41c9SSebastian Ott static void __iomem *pci_iomap_wc_range_mio(struct pci_dev *pdev, int bar,
314*71ba41c9SSebastian Ott 					    unsigned long offset, unsigned long max)
315*71ba41c9SSebastian Ott {
316*71ba41c9SSebastian Ott 	unsigned long barsize = pci_resource_len(pdev, bar);
317*71ba41c9SSebastian Ott 	struct zpci_dev *zdev = to_zpci(pdev);
318*71ba41c9SSebastian Ott 	void __iomem *iova;
319*71ba41c9SSebastian Ott 
320*71ba41c9SSebastian Ott 	iova = ioremap((unsigned long) zdev->bars[bar].mio_wb, barsize);
321*71ba41c9SSebastian Ott 	return iova ? iova + offset : iova;
322*71ba41c9SSebastian Ott }
323*71ba41c9SSebastian Ott 
324*71ba41c9SSebastian Ott void __iomem *pci_iomap_wc_range(struct pci_dev *pdev, int bar,
325*71ba41c9SSebastian Ott 				 unsigned long offset, unsigned long max)
326*71ba41c9SSebastian Ott {
327*71ba41c9SSebastian Ott 	if (!pci_resource_len(pdev, bar) || bar >= PCI_BAR_COUNT)
328*71ba41c9SSebastian Ott 		return NULL;
329*71ba41c9SSebastian Ott 
330*71ba41c9SSebastian Ott 	if (static_branch_likely(&have_mio))
331*71ba41c9SSebastian Ott 		return pci_iomap_wc_range_mio(pdev, bar, offset, max);
332*71ba41c9SSebastian Ott 	else
333*71ba41c9SSebastian Ott 		return pci_iomap_range_fh(pdev, bar, offset, max);
334*71ba41c9SSebastian Ott }
335*71ba41c9SSebastian Ott EXPORT_SYMBOL(pci_iomap_wc_range);
336*71ba41c9SSebastian Ott 
337*71ba41c9SSebastian Ott void __iomem *pci_iomap_wc(struct pci_dev *dev, int bar, unsigned long maxlen)
338*71ba41c9SSebastian Ott {
339*71ba41c9SSebastian Ott 	return pci_iomap_wc_range(dev, bar, 0, maxlen);
340*71ba41c9SSebastian Ott }
341*71ba41c9SSebastian Ott EXPORT_SYMBOL(pci_iomap_wc);
342*71ba41c9SSebastian Ott 
343*71ba41c9SSebastian Ott static void pci_iounmap_fh(struct pci_dev *pdev, void __iomem *addr)
344cd248341SJan Glauber {
3459e00caaeSSebastian Ott 	unsigned int idx = ZPCI_IDX(addr);
346cd248341SJan Glauber 
347cd248341SJan Glauber 	spin_lock(&zpci_iomap_lock);
3488cfc99b5SMichael S. Tsirkin 	/* Detect underrun */
349f5e44f82SSebastian Ott 	WARN_ON(!zpci_iomap_start[idx].count);
3508cfc99b5SMichael S. Tsirkin 	if (!--zpci_iomap_start[idx].count) {
351cd248341SJan Glauber 		zpci_iomap_start[idx].fh = 0;
352cd248341SJan Glauber 		zpci_iomap_start[idx].bar = 0;
3538cfc99b5SMichael S. Tsirkin 	}
354cd248341SJan Glauber 	spin_unlock(&zpci_iomap_lock);
355cd248341SJan Glauber }
356*71ba41c9SSebastian Ott 
357*71ba41c9SSebastian Ott static void pci_iounmap_mio(struct pci_dev *pdev, void __iomem *addr)
358*71ba41c9SSebastian Ott {
359*71ba41c9SSebastian Ott 	iounmap(addr);
360*71ba41c9SSebastian Ott }
361*71ba41c9SSebastian Ott 
362*71ba41c9SSebastian Ott void pci_iounmap(struct pci_dev *pdev, void __iomem *addr)
363*71ba41c9SSebastian Ott {
364*71ba41c9SSebastian Ott 	if (static_branch_likely(&have_mio))
365*71ba41c9SSebastian Ott 		pci_iounmap_mio(pdev, addr);
366*71ba41c9SSebastian Ott 	else
367*71ba41c9SSebastian Ott 		pci_iounmap_fh(pdev, addr);
368*71ba41c9SSebastian Ott }
369d9426083SSebastian Ott EXPORT_SYMBOL(pci_iounmap);
370cd248341SJan Glauber 
371cd248341SJan Glauber static int pci_read(struct pci_bus *bus, unsigned int devfn, int where,
372cd248341SJan Glauber 		    int size, u32 *val)
373cd248341SJan Glauber {
374cd248341SJan Glauber 	struct zpci_dev *zdev = get_zdev_by_bus(bus);
3752c3700bbSSebastian Ott 	int ret;
376cd248341SJan Glauber 
377cd248341SJan Glauber 	if (!zdev || devfn != ZPCI_DEVFN)
3782c3700bbSSebastian Ott 		ret = -ENODEV;
3792c3700bbSSebastian Ott 	else
3802c3700bbSSebastian Ott 		ret = zpci_cfg_load(zdev, where, val, size);
3812c3700bbSSebastian Ott 
3822c3700bbSSebastian Ott 	return ret;
383cd248341SJan Glauber }
384cd248341SJan Glauber 
385cd248341SJan Glauber static int pci_write(struct pci_bus *bus, unsigned int devfn, int where,
386cd248341SJan Glauber 		     int size, u32 val)
387cd248341SJan Glauber {
388cd248341SJan Glauber 	struct zpci_dev *zdev = get_zdev_by_bus(bus);
3892c3700bbSSebastian Ott 	int ret;
390cd248341SJan Glauber 
391cd248341SJan Glauber 	if (!zdev || devfn != ZPCI_DEVFN)
3922c3700bbSSebastian Ott 		ret = -ENODEV;
3932c3700bbSSebastian Ott 	else
3942c3700bbSSebastian Ott 		ret = zpci_cfg_store(zdev, where, val, size);
3952c3700bbSSebastian Ott 
3962c3700bbSSebastian Ott 	return ret;
397cd248341SJan Glauber }
398cd248341SJan Glauber 
399cd248341SJan Glauber static struct pci_ops pci_root_ops = {
400cd248341SJan Glauber 	.read = pci_read,
401cd248341SJan Glauber 	.write = pci_write,
402cd248341SJan Glauber };
403cd248341SJan Glauber 
404cfbb4a7aSSebastian Ott #ifdef CONFIG_PCI_IOV
405cfbb4a7aSSebastian Ott static struct resource iov_res = {
406cfbb4a7aSSebastian Ott 	.name	= "PCI IOV res",
407cfbb4a7aSSebastian Ott 	.start	= 0,
408cfbb4a7aSSebastian Ott 	.end	= -1,
409cfbb4a7aSSebastian Ott 	.flags	= IORESOURCE_MEM,
410cfbb4a7aSSebastian Ott };
411cfbb4a7aSSebastian Ott #endif
412cfbb4a7aSSebastian Ott 
4131803ba2dSSebastian Ott static void zpci_map_resources(struct pci_dev *pdev)
414cd248341SJan Glauber {
415*71ba41c9SSebastian Ott 	struct zpci_dev *zdev = to_zpci(pdev);
416cd248341SJan Glauber 	resource_size_t len;
417cd248341SJan Glauber 	int i;
418cd248341SJan Glauber 
419cd248341SJan Glauber 	for (i = 0; i < PCI_BAR_COUNT; i++) {
420cd248341SJan Glauber 		len = pci_resource_len(pdev, i);
421cd248341SJan Glauber 		if (!len)
422cd248341SJan Glauber 			continue;
423*71ba41c9SSebastian Ott 
424*71ba41c9SSebastian Ott 		if (static_branch_likely(&have_mio))
425*71ba41c9SSebastian Ott 			pdev->resource[i].start =
426*71ba41c9SSebastian Ott 				(resource_size_t __force) zdev->bars[i].mio_wb;
427*71ba41c9SSebastian Ott 		else
4285b9f2081SMartin Schwidefsky 			pdev->resource[i].start =
4295b9f2081SMartin Schwidefsky 				(resource_size_t __force) pci_iomap(pdev, i, 0);
430cd248341SJan Glauber 		pdev->resource[i].end = pdev->resource[i].start + len - 1;
431cd248341SJan Glauber 	}
432cfbb4a7aSSebastian Ott 
433cfbb4a7aSSebastian Ott #ifdef CONFIG_PCI_IOV
434cfbb4a7aSSebastian Ott 	i = PCI_IOV_RESOURCES;
435cfbb4a7aSSebastian Ott 
436cfbb4a7aSSebastian Ott 	for (; i < PCI_SRIOV_NUM_BARS + PCI_IOV_RESOURCES; i++) {
437cfbb4a7aSSebastian Ott 		len = pci_resource_len(pdev, i);
438cfbb4a7aSSebastian Ott 		if (!len)
439cfbb4a7aSSebastian Ott 			continue;
440cfbb4a7aSSebastian Ott 		pdev->resource[i].parent = &iov_res;
441cfbb4a7aSSebastian Ott 	}
442cfbb4a7aSSebastian Ott #endif
443944239c5SSebastian Ott }
444944239c5SSebastian Ott 
4451803ba2dSSebastian Ott static void zpci_unmap_resources(struct pci_dev *pdev)
446944239c5SSebastian Ott {
447944239c5SSebastian Ott 	resource_size_t len;
448944239c5SSebastian Ott 	int i;
449944239c5SSebastian Ott 
450*71ba41c9SSebastian Ott 	if (static_branch_likely(&have_mio))
451*71ba41c9SSebastian Ott 		return;
452*71ba41c9SSebastian Ott 
453944239c5SSebastian Ott 	for (i = 0; i < PCI_BAR_COUNT; i++) {
454944239c5SSebastian Ott 		len = pci_resource_len(pdev, i);
455944239c5SSebastian Ott 		if (!len)
456944239c5SSebastian Ott 			continue;
4575b9f2081SMartin Schwidefsky 		pci_iounmap(pdev, (void __iomem __force *)
4585b9f2081SMartin Schwidefsky 			    pdev->resource[i].start);
459944239c5SSebastian Ott 	}
460944239c5SSebastian Ott }
461cd248341SJan Glauber 
462cd248341SJan Glauber static int zpci_alloc_iomap(struct zpci_dev *zdev)
463cd248341SJan Glauber {
464bf19c94dSSebastian Ott 	unsigned long entry;
465cd248341SJan Glauber 
466cd248341SJan Glauber 	spin_lock(&zpci_iomap_lock);
467c506fff3SSebastian Ott 	entry = find_first_zero_bit(zpci_iomap_bitmap, ZPCI_IOMAP_ENTRIES);
468c506fff3SSebastian Ott 	if (entry == ZPCI_IOMAP_ENTRIES) {
469cd248341SJan Glauber 		spin_unlock(&zpci_iomap_lock);
470cd248341SJan Glauber 		return -ENOSPC;
471cd248341SJan Glauber 	}
472c506fff3SSebastian Ott 	set_bit(entry, zpci_iomap_bitmap);
473cd248341SJan Glauber 	spin_unlock(&zpci_iomap_lock);
474cd248341SJan Glauber 	return entry;
475cd248341SJan Glauber }
476cd248341SJan Glauber 
477cd248341SJan Glauber static void zpci_free_iomap(struct zpci_dev *zdev, int entry)
478cd248341SJan Glauber {
479cd248341SJan Glauber 	spin_lock(&zpci_iomap_lock);
480cd248341SJan Glauber 	memset(&zpci_iomap_start[entry], 0, sizeof(struct zpci_iomap_entry));
481c506fff3SSebastian Ott 	clear_bit(entry, zpci_iomap_bitmap);
482cd248341SJan Glauber 	spin_unlock(&zpci_iomap_lock);
483cd248341SJan Glauber }
484cd248341SJan Glauber 
4857a572a3aSSebastian Ott static struct resource *__alloc_res(struct zpci_dev *zdev, unsigned long start,
4867a572a3aSSebastian Ott 				    unsigned long size, unsigned long flags)
4877a572a3aSSebastian Ott {
4887a572a3aSSebastian Ott 	struct resource *r;
4897a572a3aSSebastian Ott 
4907a572a3aSSebastian Ott 	r = kzalloc(sizeof(*r), GFP_KERNEL);
4917a572a3aSSebastian Ott 	if (!r)
4927a572a3aSSebastian Ott 		return NULL;
4937a572a3aSSebastian Ott 
4947a572a3aSSebastian Ott 	r->start = start;
4957a572a3aSSebastian Ott 	r->end = r->start + size - 1;
4967a572a3aSSebastian Ott 	r->flags = flags;
4977a572a3aSSebastian Ott 	r->name = zdev->res_name;
4987a572a3aSSebastian Ott 
4997a572a3aSSebastian Ott 	if (request_resource(&iomem_resource, r)) {
5007a572a3aSSebastian Ott 		kfree(r);
5017a572a3aSSebastian Ott 		return NULL;
5027a572a3aSSebastian Ott 	}
5037a572a3aSSebastian Ott 	return r;
5047a572a3aSSebastian Ott }
5057a572a3aSSebastian Ott 
5067a572a3aSSebastian Ott static int zpci_setup_bus_resources(struct zpci_dev *zdev,
5077a572a3aSSebastian Ott 				    struct list_head *resources)
5087a572a3aSSebastian Ott {
5097a572a3aSSebastian Ott 	unsigned long addr, size, flags;
5107a572a3aSSebastian Ott 	struct resource *res;
5117a572a3aSSebastian Ott 	int i, entry;
5127a572a3aSSebastian Ott 
5137a572a3aSSebastian Ott 	snprintf(zdev->res_name, sizeof(zdev->res_name),
5147a572a3aSSebastian Ott 		 "PCI Bus %04x:%02x", zdev->domain, ZPCI_BUS_NR);
5157a572a3aSSebastian Ott 
5167a572a3aSSebastian Ott 	for (i = 0; i < PCI_BAR_COUNT; i++) {
5177a572a3aSSebastian Ott 		if (!zdev->bars[i].size)
5187a572a3aSSebastian Ott 			continue;
5197a572a3aSSebastian Ott 		entry = zpci_alloc_iomap(zdev);
5207a572a3aSSebastian Ott 		if (entry < 0)
5217a572a3aSSebastian Ott 			return entry;
5227a572a3aSSebastian Ott 		zdev->bars[i].map_idx = entry;
5237a572a3aSSebastian Ott 
5247a572a3aSSebastian Ott 		/* only MMIO is supported */
5257a572a3aSSebastian Ott 		flags = IORESOURCE_MEM;
5267a572a3aSSebastian Ott 		if (zdev->bars[i].val & 8)
5277a572a3aSSebastian Ott 			flags |= IORESOURCE_PREFETCH;
5287a572a3aSSebastian Ott 		if (zdev->bars[i].val & 4)
5297a572a3aSSebastian Ott 			flags |= IORESOURCE_MEM_64;
5307a572a3aSSebastian Ott 
5319e00caaeSSebastian Ott 		addr = ZPCI_ADDR(entry);
5327a572a3aSSebastian Ott 		size = 1UL << zdev->bars[i].size;
5337a572a3aSSebastian Ott 
5347a572a3aSSebastian Ott 		res = __alloc_res(zdev, addr, size, flags);
5357a572a3aSSebastian Ott 		if (!res) {
5367a572a3aSSebastian Ott 			zpci_free_iomap(zdev, entry);
5377a572a3aSSebastian Ott 			return -ENOMEM;
5387a572a3aSSebastian Ott 		}
5397a572a3aSSebastian Ott 		zdev->bars[i].res = res;
5407a572a3aSSebastian Ott 		pci_add_resource(resources, res);
5417a572a3aSSebastian Ott 	}
5427a572a3aSSebastian Ott 
5437a572a3aSSebastian Ott 	return 0;
5447a572a3aSSebastian Ott }
5457a572a3aSSebastian Ott 
5467a572a3aSSebastian Ott static void zpci_cleanup_bus_resources(struct zpci_dev *zdev)
5477a572a3aSSebastian Ott {
5487a572a3aSSebastian Ott 	int i;
5497a572a3aSSebastian Ott 
5507a572a3aSSebastian Ott 	for (i = 0; i < PCI_BAR_COUNT; i++) {
5512b1df724SSebastian Ott 		if (!zdev->bars[i].size || !zdev->bars[i].res)
5527a572a3aSSebastian Ott 			continue;
5537a572a3aSSebastian Ott 
5547a572a3aSSebastian Ott 		zpci_free_iomap(zdev, zdev->bars[i].map_idx);
5557a572a3aSSebastian Ott 		release_resource(zdev->bars[i].res);
5567a572a3aSSebastian Ott 		kfree(zdev->bars[i].res);
5577a572a3aSSebastian Ott 	}
5587a572a3aSSebastian Ott }
5597a572a3aSSebastian Ott 
560af0a8a84SSebastian Ott int pcibios_add_device(struct pci_dev *pdev)
561af0a8a84SSebastian Ott {
562cb809182SSebastian Ott 	struct resource *res;
563cb809182SSebastian Ott 	int i;
564cb809182SSebastian Ott 
5657dc20ab1SSebastian Ott 	if (pdev->is_physfn)
5667dc20ab1SSebastian Ott 		pdev->no_vf_scan = 1;
5677dc20ab1SSebastian Ott 
568ef4858c6SSebastian Ott 	pdev->dev.groups = zpci_attr_groups;
5695657933dSBart Van Assche 	pdev->dev.dma_ops = &s390_pci_dma_ops;
5701803ba2dSSebastian Ott 	zpci_map_resources(pdev);
571cb809182SSebastian Ott 
572cb809182SSebastian Ott 	for (i = 0; i < PCI_BAR_COUNT; i++) {
573cb809182SSebastian Ott 		res = &pdev->resource[i];
574cb809182SSebastian Ott 		if (res->parent || !res->flags)
575cb809182SSebastian Ott 			continue;
576cb809182SSebastian Ott 		pci_claim_resource(pdev, i);
577cb809182SSebastian Ott 	}
578cb809182SSebastian Ott 
579cb809182SSebastian Ott 	return 0;
580cb809182SSebastian Ott }
581cb809182SSebastian Ott 
5821803ba2dSSebastian Ott void pcibios_release_device(struct pci_dev *pdev)
5831803ba2dSSebastian Ott {
5841803ba2dSSebastian Ott 	zpci_unmap_resources(pdev);
5851803ba2dSSebastian Ott }
5861803ba2dSSebastian Ott 
587cb809182SSebastian Ott int pcibios_enable_device(struct pci_dev *pdev, int mask)
588cb809182SSebastian Ott {
589198a5278SSebastian Ott 	struct zpci_dev *zdev = to_zpci(pdev);
590af0a8a84SSebastian Ott 
5919a99649fSSebastian Ott 	zpci_debug_init_device(zdev, dev_name(&pdev->dev));
592af0a8a84SSebastian Ott 	zpci_fmb_enable_device(zdev);
593af0a8a84SSebastian Ott 
594d7533232SBjorn Helgaas 	return pci_enable_resources(pdev, mask);
595af0a8a84SSebastian Ott }
596af0a8a84SSebastian Ott 
597cb809182SSebastian Ott void pcibios_disable_device(struct pci_dev *pdev)
598944239c5SSebastian Ott {
599198a5278SSebastian Ott 	struct zpci_dev *zdev = to_zpci(pdev);
600944239c5SSebastian Ott 
601944239c5SSebastian Ott 	zpci_fmb_disable_device(zdev);
602944239c5SSebastian Ott 	zpci_debug_exit_device(zdev);
603944239c5SSebastian Ott }
604944239c5SSebastian Ott 
60569db3b5eSSebastian Ott #ifdef CONFIG_HIBERNATE_CALLBACKS
60669db3b5eSSebastian Ott static int zpci_restore(struct device *dev)
60769db3b5eSSebastian Ott {
6081803ba2dSSebastian Ott 	struct pci_dev *pdev = to_pci_dev(dev);
609198a5278SSebastian Ott 	struct zpci_dev *zdev = to_zpci(pdev);
61069db3b5eSSebastian Ott 	int ret = 0;
61169db3b5eSSebastian Ott 
61269db3b5eSSebastian Ott 	if (zdev->state != ZPCI_FN_STATE_ONLINE)
61369db3b5eSSebastian Ott 		goto out;
61469db3b5eSSebastian Ott 
61569db3b5eSSebastian Ott 	ret = clp_enable_fh(zdev, ZPCI_NR_DMA_SPACES);
61669db3b5eSSebastian Ott 	if (ret)
61769db3b5eSSebastian Ott 		goto out;
61869db3b5eSSebastian Ott 
6191803ba2dSSebastian Ott 	zpci_map_resources(pdev);
62069eea95cSGerald Schaefer 	zpci_register_ioat(zdev, 0, zdev->start_dma, zdev->end_dma,
62169db3b5eSSebastian Ott 			   (u64) zdev->dma_table);
62269db3b5eSSebastian Ott 
62369db3b5eSSebastian Ott out:
62469db3b5eSSebastian Ott 	return ret;
62569db3b5eSSebastian Ott }
62669db3b5eSSebastian Ott 
62769db3b5eSSebastian Ott static int zpci_freeze(struct device *dev)
62869db3b5eSSebastian Ott {
6291803ba2dSSebastian Ott 	struct pci_dev *pdev = to_pci_dev(dev);
630198a5278SSebastian Ott 	struct zpci_dev *zdev = to_zpci(pdev);
63169db3b5eSSebastian Ott 
63269db3b5eSSebastian Ott 	if (zdev->state != ZPCI_FN_STATE_ONLINE)
63369db3b5eSSebastian Ott 		return 0;
63469db3b5eSSebastian Ott 
63569db3b5eSSebastian Ott 	zpci_unregister_ioat(zdev, 0);
6361803ba2dSSebastian Ott 	zpci_unmap_resources(pdev);
63769db3b5eSSebastian Ott 	return clp_disable_fh(zdev);
63869db3b5eSSebastian Ott }
63969db3b5eSSebastian Ott 
64069db3b5eSSebastian Ott struct dev_pm_ops pcibios_pm_ops = {
64169db3b5eSSebastian Ott 	.thaw_noirq = zpci_restore,
64269db3b5eSSebastian Ott 	.freeze_noirq = zpci_freeze,
64369db3b5eSSebastian Ott 	.restore_noirq = zpci_restore,
64469db3b5eSSebastian Ott 	.poweroff_noirq = zpci_freeze,
64569db3b5eSSebastian Ott };
64669db3b5eSSebastian Ott #endif /* CONFIG_HIBERNATE_CALLBACKS */
64769db3b5eSSebastian Ott 
648cd248341SJan Glauber static int zpci_alloc_domain(struct zpci_dev *zdev)
649cd248341SJan Glauber {
6505c5afd02SSebastian Ott 	if (zpci_unique_uid) {
6515c5afd02SSebastian Ott 		zdev->domain = (u16) zdev->uid;
652312e8462SSebastian Ott 		if (zdev->domain >= ZPCI_NR_DEVICES)
653312e8462SSebastian Ott 			return 0;
654312e8462SSebastian Ott 
655312e8462SSebastian Ott 		spin_lock(&zpci_domain_lock);
656312e8462SSebastian Ott 		if (test_bit(zdev->domain, zpci_domain)) {
657312e8462SSebastian Ott 			spin_unlock(&zpci_domain_lock);
658312e8462SSebastian Ott 			return -EEXIST;
659312e8462SSebastian Ott 		}
660312e8462SSebastian Ott 		set_bit(zdev->domain, zpci_domain);
661312e8462SSebastian Ott 		spin_unlock(&zpci_domain_lock);
6625c5afd02SSebastian Ott 		return 0;
6635c5afd02SSebastian Ott 	}
6645c5afd02SSebastian Ott 
665cd248341SJan Glauber 	spin_lock(&zpci_domain_lock);
666cd248341SJan Glauber 	zdev->domain = find_first_zero_bit(zpci_domain, ZPCI_NR_DEVICES);
667cd248341SJan Glauber 	if (zdev->domain == ZPCI_NR_DEVICES) {
668cd248341SJan Glauber 		spin_unlock(&zpci_domain_lock);
669cd248341SJan Glauber 		return -ENOSPC;
670cd248341SJan Glauber 	}
671cd248341SJan Glauber 	set_bit(zdev->domain, zpci_domain);
672cd248341SJan Glauber 	spin_unlock(&zpci_domain_lock);
673cd248341SJan Glauber 	return 0;
674cd248341SJan Glauber }
675cd248341SJan Glauber 
676cd248341SJan Glauber static void zpci_free_domain(struct zpci_dev *zdev)
677cd248341SJan Glauber {
678312e8462SSebastian Ott 	if (zdev->domain >= ZPCI_NR_DEVICES)
6795c5afd02SSebastian Ott 		return;
6805c5afd02SSebastian Ott 
681cd248341SJan Glauber 	spin_lock(&zpci_domain_lock);
682cd248341SJan Glauber 	clear_bit(zdev->domain, zpci_domain);
683cd248341SJan Glauber 	spin_unlock(&zpci_domain_lock);
684cd248341SJan Glauber }
685cd248341SJan Glauber 
6867d594322SSebastian Ott void pcibios_remove_bus(struct pci_bus *bus)
6877d594322SSebastian Ott {
6887d594322SSebastian Ott 	struct zpci_dev *zdev = get_zdev_by_bus(bus);
6897d594322SSebastian Ott 
6907d594322SSebastian Ott 	zpci_exit_slot(zdev);
6917d594322SSebastian Ott 	zpci_cleanup_bus_resources(zdev);
692f42c2235SJoerg Roedel 	zpci_destroy_iommu(zdev);
6937d594322SSebastian Ott 	zpci_free_domain(zdev);
6947d594322SSebastian Ott 
6957d594322SSebastian Ott 	spin_lock(&zpci_list_lock);
6967d594322SSebastian Ott 	list_del(&zdev->entry);
6977d594322SSebastian Ott 	spin_unlock(&zpci_list_lock);
6987d594322SSebastian Ott 
699be2c3676SSebastian Ott 	zpci_dbg(3, "rem fid:%x\n", zdev->fid);
7007d594322SSebastian Ott 	kfree(zdev);
7017d594322SSebastian Ott }
7027d594322SSebastian Ott 
7037d594322SSebastian Ott static int zpci_scan_bus(struct zpci_dev *zdev)
7047d594322SSebastian Ott {
7057d594322SSebastian Ott 	LIST_HEAD(resources);
7067d594322SSebastian Ott 	int ret;
7077d594322SSebastian Ott 
7087d594322SSebastian Ott 	ret = zpci_setup_bus_resources(zdev, &resources);
7097d594322SSebastian Ott 	if (ret)
7102b1df724SSebastian Ott 		goto error;
7117d594322SSebastian Ott 
7127d594322SSebastian Ott 	zdev->bus = pci_scan_root_bus(NULL, ZPCI_BUS_NR, &pci_root_ops,
7137d594322SSebastian Ott 				      zdev, &resources);
7147d594322SSebastian Ott 	if (!zdev->bus) {
7152b1df724SSebastian Ott 		ret = -EIO;
7162b1df724SSebastian Ott 		goto error;
7177d594322SSebastian Ott 	}
7187d594322SSebastian Ott 	zdev->bus->max_bus_speed = zdev->max_bus_speed;
719b97ea289SYijing Wang 	pci_bus_add_devices(zdev->bus);
7207d594322SSebastian Ott 	return 0;
7212b1df724SSebastian Ott 
7222b1df724SSebastian Ott error:
7232b1df724SSebastian Ott 	zpci_cleanup_bus_resources(zdev);
7242b1df724SSebastian Ott 	pci_free_resource_list(&resources);
7252b1df724SSebastian Ott 	return ret;
7267d594322SSebastian Ott }
7277d594322SSebastian Ott 
728a755a45dSJan Glauber int zpci_enable_device(struct zpci_dev *zdev)
729a755a45dSJan Glauber {
730a755a45dSJan Glauber 	int rc;
731a755a45dSJan Glauber 
732a755a45dSJan Glauber 	rc = clp_enable_fh(zdev, ZPCI_NR_DMA_SPACES);
733a755a45dSJan Glauber 	if (rc)
734a755a45dSJan Glauber 		goto out;
735828b35f6SJan Glauber 
736828b35f6SJan Glauber 	rc = zpci_dma_init_device(zdev);
737828b35f6SJan Glauber 	if (rc)
738828b35f6SJan Glauber 		goto out_dma;
7390ff70ec8SSebastian Ott 
7400ff70ec8SSebastian Ott 	zdev->state = ZPCI_FN_STATE_ONLINE;
741a755a45dSJan Glauber 	return 0;
742828b35f6SJan Glauber 
743828b35f6SJan Glauber out_dma:
744828b35f6SJan Glauber 	clp_disable_fh(zdev);
745a755a45dSJan Glauber out:
746a755a45dSJan Glauber 	return rc;
747a755a45dSJan Glauber }
748a755a45dSJan Glauber EXPORT_SYMBOL_GPL(zpci_enable_device);
749a755a45dSJan Glauber 
750cb65a669SSebastian Ott int zpci_disable_device(struct zpci_dev *zdev)
751cb65a669SSebastian Ott {
752cb65a669SSebastian Ott 	zpci_dma_exit_device(zdev);
753cb65a669SSebastian Ott 	return clp_disable_fh(zdev);
754cb65a669SSebastian Ott }
755cb65a669SSebastian Ott EXPORT_SYMBOL_GPL(zpci_disable_device);
756cb65a669SSebastian Ott 
757cd248341SJan Glauber int zpci_create_device(struct zpci_dev *zdev)
758cd248341SJan Glauber {
759cd248341SJan Glauber 	int rc;
760cd248341SJan Glauber 
761cd248341SJan Glauber 	rc = zpci_alloc_domain(zdev);
762cd248341SJan Glauber 	if (rc)
763cd248341SJan Glauber 		goto out;
764cd248341SJan Glauber 
765f42c2235SJoerg Roedel 	rc = zpci_init_iommu(zdev);
766f42c2235SJoerg Roedel 	if (rc)
767f42c2235SJoerg Roedel 		goto out_free;
768f42c2235SJoerg Roedel 
76980ed156aSSebastian Ott 	mutex_init(&zdev->lock);
7701c21351bSSebastian Ott 	if (zdev->state == ZPCI_FN_STATE_CONFIGURED) {
7711c21351bSSebastian Ott 		rc = zpci_enable_device(zdev);
772cd248341SJan Glauber 		if (rc)
773f42c2235SJoerg Roedel 			goto out_destroy_iommu;
7741c21351bSSebastian Ott 	}
7751c21351bSSebastian Ott 	rc = zpci_scan_bus(zdev);
7761c21351bSSebastian Ott 	if (rc)
7771c21351bSSebastian Ott 		goto out_disable;
778cd248341SJan Glauber 
77957b5918cSSebastian Ott 	spin_lock(&zpci_list_lock);
780cd248341SJan Glauber 	list_add_tail(&zdev->entry, &zpci_list);
78157b5918cSSebastian Ott 	spin_unlock(&zpci_list_lock);
782cd248341SJan Glauber 
78367f43f38SSebastian Ott 	zpci_init_slot(zdev);
78467f43f38SSebastian Ott 
785cd248341SJan Glauber 	return 0;
786cd248341SJan Glauber 
7871c21351bSSebastian Ott out_disable:
7881c21351bSSebastian Ott 	if (zdev->state == ZPCI_FN_STATE_ONLINE)
7891c21351bSSebastian Ott 		zpci_disable_device(zdev);
790f42c2235SJoerg Roedel out_destroy_iommu:
791f42c2235SJoerg Roedel 	zpci_destroy_iommu(zdev);
7921c21351bSSebastian Ott out_free:
793cd248341SJan Glauber 	zpci_free_domain(zdev);
794cd248341SJan Glauber out:
795cd248341SJan Glauber 	return rc;
796cd248341SJan Glauber }
797cd248341SJan Glauber 
798623bd44dSSebastian Ott void zpci_remove_device(struct zpci_dev *zdev)
799623bd44dSSebastian Ott {
800623bd44dSSebastian Ott 	if (!zdev->bus)
801623bd44dSSebastian Ott 		return;
802623bd44dSSebastian Ott 
803623bd44dSSebastian Ott 	pci_stop_root_bus(zdev->bus);
804623bd44dSSebastian Ott 	pci_remove_root_bus(zdev->bus);
805623bd44dSSebastian Ott }
806623bd44dSSebastian Ott 
807bd3a1725SMartin Schwidefsky int zpci_report_error(struct pci_dev *pdev,
808bd3a1725SMartin Schwidefsky 		      struct zpci_report_error_header *report)
809bd3a1725SMartin Schwidefsky {
810bd3a1725SMartin Schwidefsky 	struct zpci_dev *zdev = to_zpci(pdev);
811bd3a1725SMartin Schwidefsky 
812bd3a1725SMartin Schwidefsky 	return sclp_pci_report(report, zdev->fh, zdev->fid);
813bd3a1725SMartin Schwidefsky }
814bd3a1725SMartin Schwidefsky EXPORT_SYMBOL(zpci_report_error);
815bd3a1725SMartin Schwidefsky 
816cd248341SJan Glauber static int zpci_mem_init(void)
817cd248341SJan Glauber {
81880c544deSSebastian Ott 	BUILD_BUG_ON(!is_power_of_2(__alignof__(struct zpci_fmb)) ||
81980c544deSSebastian Ott 		     __alignof__(struct zpci_fmb) < sizeof(struct zpci_fmb));
82080c544deSSebastian Ott 
821d0b08853SJan Glauber 	zdev_fmb_cache = kmem_cache_create("PCI_FMB_cache", sizeof(struct zpci_fmb),
82280c544deSSebastian Ott 					   __alignof__(struct zpci_fmb), 0, NULL);
823d0b08853SJan Glauber 	if (!zdev_fmb_cache)
824c506fff3SSebastian Ott 		goto error_fmb;
825d0b08853SJan Glauber 
826c506fff3SSebastian Ott 	zpci_iomap_start = kcalloc(ZPCI_IOMAP_ENTRIES,
827c506fff3SSebastian Ott 				   sizeof(*zpci_iomap_start), GFP_KERNEL);
828cd248341SJan Glauber 	if (!zpci_iomap_start)
8299a4da8a5SJan Glauber 		goto error_iomap;
830cd248341SJan Glauber 
831c506fff3SSebastian Ott 	zpci_iomap_bitmap = kcalloc(BITS_TO_LONGS(ZPCI_IOMAP_ENTRIES),
832c506fff3SSebastian Ott 				    sizeof(*zpci_iomap_bitmap), GFP_KERNEL);
833c506fff3SSebastian Ott 	if (!zpci_iomap_bitmap)
834c506fff3SSebastian Ott 		goto error_iomap_bitmap;
835c506fff3SSebastian Ott 
836c506fff3SSebastian Ott 	return 0;
837c506fff3SSebastian Ott error_iomap_bitmap:
838c506fff3SSebastian Ott 	kfree(zpci_iomap_start);
8399a4da8a5SJan Glauber error_iomap:
840d0b08853SJan Glauber 	kmem_cache_destroy(zdev_fmb_cache);
841c506fff3SSebastian Ott error_fmb:
842cd248341SJan Glauber 	return -ENOMEM;
843cd248341SJan Glauber }
844cd248341SJan Glauber 
845cd248341SJan Glauber static void zpci_mem_exit(void)
846cd248341SJan Glauber {
847c506fff3SSebastian Ott 	kfree(zpci_iomap_bitmap);
848cd248341SJan Glauber 	kfree(zpci_iomap_start);
849d0b08853SJan Glauber 	kmem_cache_destroy(zdev_fmb_cache);
850cd248341SJan Glauber }
851cd248341SJan Glauber 
8526324b4deSSebastian Ott static unsigned int s390_pci_probe __initdata = 1;
853fbfe07d4SSebastian Ott unsigned int s390_pci_force_floating __initdata;
854aa3b7c29SSebastian Ott static unsigned int s390_pci_initialized;
855cd248341SJan Glauber 
856cd248341SJan Glauber char * __init pcibios_setup(char *str)
857cd248341SJan Glauber {
858257608fbSSebastian Ott 	if (!strcmp(str, "off")) {
859257608fbSSebastian Ott 		s390_pci_probe = 0;
860cd248341SJan Glauber 		return NULL;
861cd248341SJan Glauber 	}
862fbfe07d4SSebastian Ott 	if (!strcmp(str, "force_floating")) {
863fbfe07d4SSebastian Ott 		s390_pci_force_floating = 1;
864fbfe07d4SSebastian Ott 		return NULL;
865fbfe07d4SSebastian Ott 	}
866cd248341SJan Glauber 	return str;
867cd248341SJan Glauber }
868cd248341SJan Glauber 
869aa3b7c29SSebastian Ott bool zpci_is_enabled(void)
870aa3b7c29SSebastian Ott {
871aa3b7c29SSebastian Ott 	return s390_pci_initialized;
872aa3b7c29SSebastian Ott }
873aa3b7c29SSebastian Ott 
874cd248341SJan Glauber static int __init pci_base_init(void)
875cd248341SJan Glauber {
876cd248341SJan Glauber 	int rc;
877cd248341SJan Glauber 
8781e5635d1SHeiko Carstens 	if (!s390_pci_probe)
879cd248341SJan Glauber 		return 0;
880cd248341SJan Glauber 
88148070c73SChristian Borntraeger 	if (!test_facility(69) || !test_facility(71))
882cd248341SJan Glauber 		return 0;
883cd248341SJan Glauber 
884*71ba41c9SSebastian Ott 	if (test_facility(153))
885*71ba41c9SSebastian Ott 		static_branch_enable(&have_mio);
886*71ba41c9SSebastian Ott 
887d0b08853SJan Glauber 	rc = zpci_debug_init();
888d0b08853SJan Glauber 	if (rc)
8891f44a225SMartin Schwidefsky 		goto out;
890d0b08853SJan Glauber 
891cd248341SJan Glauber 	rc = zpci_mem_init();
892cd248341SJan Glauber 	if (rc)
893cd248341SJan Glauber 		goto out_mem;
894cd248341SJan Glauber 
8959a4da8a5SJan Glauber 	rc = zpci_irq_init();
8969a4da8a5SJan Glauber 	if (rc)
8979a4da8a5SJan Glauber 		goto out_irq;
8989a4da8a5SJan Glauber 
899828b35f6SJan Glauber 	rc = zpci_dma_init();
900828b35f6SJan Glauber 	if (rc)
901828b35f6SJan Glauber 		goto out_dma;
902828b35f6SJan Glauber 
9031d578966SSebastian Ott 	rc = clp_scan_pci_devices();
904a755a45dSJan Glauber 	if (rc)
905a755a45dSJan Glauber 		goto out_find;
906a755a45dSJan Glauber 
907aa3b7c29SSebastian Ott 	s390_pci_initialized = 1;
908cd248341SJan Glauber 	return 0;
909cd248341SJan Glauber 
910a755a45dSJan Glauber out_find:
911828b35f6SJan Glauber 	zpci_dma_exit();
912828b35f6SJan Glauber out_dma:
9139a4da8a5SJan Glauber 	zpci_irq_exit();
9149a4da8a5SJan Glauber out_irq:
915cd248341SJan Glauber 	zpci_mem_exit();
916cd248341SJan Glauber out_mem:
917d0b08853SJan Glauber 	zpci_debug_exit();
9181f44a225SMartin Schwidefsky out:
919cd248341SJan Glauber 	return rc;
920cd248341SJan Glauber }
92167f43f38SSebastian Ott subsys_initcall_sync(pci_base_init);
92257b5918cSSebastian Ott 
92357b5918cSSebastian Ott void zpci_rescan(void)
92457b5918cSSebastian Ott {
925aa3b7c29SSebastian Ott 	if (zpci_is_enabled())
92657b5918cSSebastian Ott 		clp_rescan_pci_devices_simple();
92757b5918cSSebastian Ott }
928