xref: /linux/arch/s390/pci/pci.c (revision 40286d6379aacfcc053253ef78dc78b09addffda)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright IBM Corp. 2012
4  *
5  * Author(s):
6  *   Jan Glauber <jang@linux.vnet.ibm.com>
7  *
8  * The System z PCI code is a rewrite from a prototype by
9  * the following people (Kudoz!):
10  *   Alexander Schmidt
11  *   Christoph Raisch
12  *   Hannes Hering
13  *   Hoang-Nam Nguyen
14  *   Jan-Bernd Themann
15  *   Stefan Roscher
16  *   Thomas Klein
17  */
18 
19 #define pr_fmt(fmt) "zpci: " fmt
20 
21 #include <linux/kernel.h>
22 #include <linux/slab.h>
23 #include <linux/err.h>
24 #include <linux/export.h>
25 #include <linux/delay.h>
26 #include <linux/seq_file.h>
27 #include <linux/jump_label.h>
28 #include <linux/pci.h>
29 #include <linux/printk.h>
30 #include <linux/lockdep.h>
31 #include <linux/list_sort.h>
32 
33 #include <asm/machine.h>
34 #include <asm/isc.h>
35 #include <asm/airq.h>
36 #include <asm/facility.h>
37 #include <asm/pci_insn.h>
38 #include <asm/pci_clp.h>
39 #include <asm/pci_dma.h>
40 
41 #include "pci_bus.h"
42 #include "pci_iov.h"
43 
44 /* list of all detected zpci devices */
45 static LIST_HEAD(zpci_list);
46 static DEFINE_SPINLOCK(zpci_list_lock);
47 static DEFINE_MUTEX(zpci_add_remove_lock);
48 
49 static DECLARE_BITMAP(zpci_domain, ZPCI_DOMAIN_BITMAP_SIZE);
50 static DEFINE_SPINLOCK(zpci_domain_lock);
51 
52 #define ZPCI_IOMAP_ENTRIES						\
53 	min(((unsigned long) ZPCI_NR_DEVICES * PCI_STD_NUM_BARS / 2),	\
54 	    ZPCI_IOMAP_MAX_ENTRIES)
55 
56 unsigned int s390_pci_no_rid;
57 
58 static DEFINE_SPINLOCK(zpci_iomap_lock);
59 static unsigned long *zpci_iomap_bitmap;
60 struct zpci_iomap_entry *zpci_iomap_start;
61 EXPORT_SYMBOL_GPL(zpci_iomap_start);
62 
63 DEFINE_STATIC_KEY_FALSE(have_mio);
64 
65 static struct kmem_cache *zdev_fmb_cache;
66 
67 /* AEN structures that must be preserved over KVM module re-insertion */
68 union zpci_sic_iib *zpci_aipb;
69 EXPORT_SYMBOL_GPL(zpci_aipb);
70 struct airq_iv *zpci_aif_sbv;
71 EXPORT_SYMBOL_GPL(zpci_aif_sbv);
72 
73 void zpci_zdev_put(struct zpci_dev *zdev)
74 {
75 	if (!zdev)
76 		return;
77 	mutex_lock(&zpci_add_remove_lock);
78 	kref_put_lock(&zdev->kref, zpci_release_device, &zpci_list_lock);
79 	mutex_unlock(&zpci_add_remove_lock);
80 }
81 
82 struct zpci_dev *get_zdev_by_fid(u32 fid)
83 {
84 	struct zpci_dev *tmp, *zdev = NULL;
85 
86 	spin_lock(&zpci_list_lock);
87 	list_for_each_entry(tmp, &zpci_list, entry) {
88 		if (tmp->fid == fid) {
89 			zdev = tmp;
90 			zpci_zdev_get(zdev);
91 			break;
92 		}
93 	}
94 	spin_unlock(&zpci_list_lock);
95 	return zdev;
96 }
97 
98 void zpci_remove_reserved_devices(void)
99 {
100 	struct zpci_dev *tmp, *zdev;
101 	enum zpci_state state;
102 	LIST_HEAD(remove);
103 
104 	spin_lock(&zpci_list_lock);
105 	list_for_each_entry_safe(zdev, tmp, &zpci_list, entry) {
106 		if (zdev->state == ZPCI_FN_STATE_STANDBY &&
107 		    !clp_get_state(zdev->fid, &state) &&
108 		    state == ZPCI_FN_STATE_RESERVED)
109 			list_move_tail(&zdev->entry, &remove);
110 	}
111 	spin_unlock(&zpci_list_lock);
112 
113 	list_for_each_entry_safe(zdev, tmp, &remove, entry)
114 		zpci_device_reserved(zdev);
115 }
116 
117 int pci_domain_nr(struct pci_bus *bus)
118 {
119 	return ((struct zpci_bus *) bus->sysdata)->domain_nr;
120 }
121 EXPORT_SYMBOL_GPL(pci_domain_nr);
122 
123 int pci_proc_domain(struct pci_bus *bus)
124 {
125 	return pci_domain_nr(bus);
126 }
127 EXPORT_SYMBOL_GPL(pci_proc_domain);
128 
129 /* Modify PCI: Register I/O address translation parameters */
130 int zpci_register_ioat(struct zpci_dev *zdev, u8 dmaas,
131 		       u64 base, u64 limit, u64 iota, u8 *status)
132 {
133 	u64 req = ZPCI_CREATE_REQ(zdev->fh, dmaas, ZPCI_MOD_FC_REG_IOAT);
134 	struct zpci_fib fib = {0};
135 	u8 cc;
136 
137 	fib.pba = base;
138 	/* Work around off by one in ISM virt device */
139 	if (zdev->pft == PCI_FUNC_TYPE_ISM && limit > base)
140 		fib.pal = limit + (1 << 12);
141 	else
142 		fib.pal = limit;
143 	fib.iota = iota;
144 	fib.gd = zdev->gisa;
145 	cc = zpci_mod_fc(req, &fib, status);
146 	if (cc)
147 		zpci_dbg(3, "reg ioat fid:%x, cc:%d, status:%d\n", zdev->fid, cc, *status);
148 	return cc;
149 }
150 EXPORT_SYMBOL_GPL(zpci_register_ioat);
151 
152 /* Modify PCI: Unregister I/O address translation parameters */
153 int zpci_unregister_ioat(struct zpci_dev *zdev, u8 dmaas)
154 {
155 	u64 req = ZPCI_CREATE_REQ(zdev->fh, dmaas, ZPCI_MOD_FC_DEREG_IOAT);
156 	struct zpci_fib fib = {0};
157 	u8 cc, status;
158 
159 	fib.gd = zdev->gisa;
160 
161 	cc = zpci_mod_fc(req, &fib, &status);
162 	if (cc)
163 		zpci_dbg(3, "unreg ioat fid:%x, cc:%d, status:%d\n", zdev->fid, cc, status);
164 	return cc;
165 }
166 
167 /* Modify PCI: Set PCI function measurement parameters */
168 int zpci_fmb_enable_device(struct zpci_dev *zdev)
169 {
170 	u64 req = ZPCI_CREATE_REQ(zdev->fh, 0, ZPCI_MOD_FC_SET_MEASURE);
171 	struct zpci_iommu_ctrs *ctrs;
172 	struct zpci_fib fib = {0};
173 	unsigned long flags;
174 	u8 cc, status;
175 
176 	if (zdev->fmb || sizeof(*zdev->fmb) < zdev->fmb_length)
177 		return -EINVAL;
178 
179 	zdev->fmb = kmem_cache_zalloc(zdev_fmb_cache, GFP_KERNEL);
180 	if (!zdev->fmb)
181 		return -ENOMEM;
182 	WARN_ON((u64) zdev->fmb & 0xf);
183 
184 	/* reset software counters */
185 	spin_lock_irqsave(&zdev->dom_lock, flags);
186 	ctrs = zpci_get_iommu_ctrs(zdev);
187 	if (ctrs) {
188 		atomic64_set(&ctrs->mapped_pages, 0);
189 		atomic64_set(&ctrs->unmapped_pages, 0);
190 		atomic64_set(&ctrs->global_rpcits, 0);
191 		atomic64_set(&ctrs->sync_map_rpcits, 0);
192 		atomic64_set(&ctrs->sync_rpcits, 0);
193 	}
194 	spin_unlock_irqrestore(&zdev->dom_lock, flags);
195 
196 
197 	fib.fmb_addr = virt_to_phys(zdev->fmb);
198 	fib.gd = zdev->gisa;
199 	cc = zpci_mod_fc(req, &fib, &status);
200 	if (cc) {
201 		kmem_cache_free(zdev_fmb_cache, zdev->fmb);
202 		zdev->fmb = NULL;
203 	}
204 	return cc ? -EIO : 0;
205 }
206 
207 /* Modify PCI: Disable PCI function measurement */
208 int zpci_fmb_disable_device(struct zpci_dev *zdev)
209 {
210 	u64 req = ZPCI_CREATE_REQ(zdev->fh, 0, ZPCI_MOD_FC_SET_MEASURE);
211 	struct zpci_fib fib = {0};
212 	u8 cc, status;
213 
214 	if (!zdev->fmb)
215 		return -EINVAL;
216 
217 	fib.gd = zdev->gisa;
218 
219 	/* Function measurement is disabled if fmb address is zero */
220 	cc = zpci_mod_fc(req, &fib, &status);
221 	if (cc == 3) /* Function already gone. */
222 		cc = 0;
223 
224 	if (!cc) {
225 		kmem_cache_free(zdev_fmb_cache, zdev->fmb);
226 		zdev->fmb = NULL;
227 	}
228 	return cc ? -EIO : 0;
229 }
230 
231 static int zpci_cfg_load(struct zpci_dev *zdev, int offset, u32 *val, u8 len)
232 {
233 	u64 req = ZPCI_CREATE_REQ(zdev->fh, ZPCI_PCIAS_CFGSPC, len);
234 	int rc = -ENODEV;
235 	u64 data;
236 
237 	if (!zdev_enabled(zdev))
238 		goto out_err;
239 
240 	rc = __zpci_load(&data, req, offset);
241 	if (rc)
242 		goto out_err;
243 	data = le64_to_cpu((__force __le64)data);
244 	data >>= (8 - len) * 8;
245 	*val = (u32)data;
246 	return 0;
247 
248 out_err:
249 	PCI_SET_ERROR_RESPONSE(val);
250 	return rc;
251 }
252 
253 static int zpci_cfg_store(struct zpci_dev *zdev, int offset, u32 val, u8 len)
254 {
255 	u64 req = ZPCI_CREATE_REQ(zdev->fh, ZPCI_PCIAS_CFGSPC, len);
256 	int rc = -ENODEV;
257 	u64 data = val;
258 
259 	if (!zdev_enabled(zdev))
260 		return rc;
261 
262 	data <<= (8 - len) * 8;
263 	data = (__force u64) cpu_to_le64(data);
264 	rc = __zpci_store(data, req, offset);
265 	return rc;
266 }
267 
268 resource_size_t pcibios_align_resource(void *data, const struct resource *res,
269 				       const struct resource *empty_res,
270 				       resource_size_t size,
271 				       resource_size_t align)
272 {
273 	return 0;
274 }
275 
276 void __iomem *ioremap_prot(phys_addr_t phys_addr, size_t size,
277 			   pgprot_t prot)
278 {
279 	/*
280 	 * When PCI MIO instructions are unavailable the "physical" address
281 	 * encodes a hint for accessing the PCI memory space it represents.
282 	 * Just pass it unchanged such that ioread/iowrite can decode it.
283 	 */
284 	if (!static_branch_unlikely(&have_mio))
285 		return (void __iomem *)phys_addr;
286 
287 	return generic_ioremap_prot(phys_addr, size, prot);
288 }
289 EXPORT_SYMBOL(ioremap_prot);
290 
291 void iounmap(volatile void __iomem *addr)
292 {
293 	if (static_branch_likely(&have_mio))
294 		generic_iounmap(addr);
295 }
296 EXPORT_SYMBOL(iounmap);
297 
298 /* Create a virtual mapping cookie for a PCI BAR */
299 static void __iomem *pci_iomap_range_fh(struct pci_dev *pdev, int bar,
300 					unsigned long offset, unsigned long max)
301 {
302 	struct zpci_dev *zdev =	to_zpci(pdev);
303 	int idx;
304 
305 	idx = zdev->bars[bar].map_idx;
306 	spin_lock(&zpci_iomap_lock);
307 	/* Detect overrun */
308 	WARN_ON(!++zpci_iomap_start[idx].count);
309 	zpci_iomap_start[idx].fh = zdev->fh;
310 	zpci_iomap_start[idx].bar = bar;
311 	spin_unlock(&zpci_iomap_lock);
312 
313 	return (void __iomem *) ZPCI_ADDR(idx) + offset;
314 }
315 
316 static void __iomem *pci_iomap_range_mio(struct pci_dev *pdev, int bar,
317 					 unsigned long offset,
318 					 unsigned long max)
319 {
320 	unsigned long barsize = pci_resource_len(pdev, bar);
321 	struct zpci_dev *zdev = to_zpci(pdev);
322 	void __iomem *iova;
323 
324 	iova = ioremap((unsigned long) zdev->bars[bar].mio_wt, barsize);
325 	return iova ? iova + offset : iova;
326 }
327 
328 void __iomem *pci_iomap_range(struct pci_dev *pdev, int bar,
329 			      unsigned long offset, unsigned long max)
330 {
331 	if (bar >= PCI_STD_NUM_BARS || !pci_resource_len(pdev, bar))
332 		return NULL;
333 
334 	if (static_branch_likely(&have_mio))
335 		return pci_iomap_range_mio(pdev, bar, offset, max);
336 	else
337 		return pci_iomap_range_fh(pdev, bar, offset, max);
338 }
339 EXPORT_SYMBOL(pci_iomap_range);
340 
341 void __iomem *pci_iomap(struct pci_dev *dev, int bar, unsigned long maxlen)
342 {
343 	return pci_iomap_range(dev, bar, 0, maxlen);
344 }
345 EXPORT_SYMBOL(pci_iomap);
346 
347 static void __iomem *pci_iomap_wc_range_mio(struct pci_dev *pdev, int bar,
348 					    unsigned long offset, unsigned long max)
349 {
350 	unsigned long barsize = pci_resource_len(pdev, bar);
351 	struct zpci_dev *zdev = to_zpci(pdev);
352 	void __iomem *iova;
353 
354 	iova = ioremap((unsigned long) zdev->bars[bar].mio_wb, barsize);
355 	return iova ? iova + offset : iova;
356 }
357 
358 void __iomem *pci_iomap_wc_range(struct pci_dev *pdev, int bar,
359 				 unsigned long offset, unsigned long max)
360 {
361 	if (bar >= PCI_STD_NUM_BARS || !pci_resource_len(pdev, bar))
362 		return NULL;
363 
364 	if (static_branch_likely(&have_mio))
365 		return pci_iomap_wc_range_mio(pdev, bar, offset, max);
366 	else
367 		return pci_iomap_range_fh(pdev, bar, offset, max);
368 }
369 EXPORT_SYMBOL(pci_iomap_wc_range);
370 
371 void __iomem *pci_iomap_wc(struct pci_dev *dev, int bar, unsigned long maxlen)
372 {
373 	return pci_iomap_wc_range(dev, bar, 0, maxlen);
374 }
375 EXPORT_SYMBOL(pci_iomap_wc);
376 
377 static void pci_iounmap_fh(struct pci_dev *pdev, void __iomem *addr)
378 {
379 	unsigned int idx = ZPCI_IDX(addr);
380 
381 	spin_lock(&zpci_iomap_lock);
382 	/* Detect underrun */
383 	WARN_ON(!zpci_iomap_start[idx].count);
384 	if (!--zpci_iomap_start[idx].count) {
385 		zpci_iomap_start[idx].fh = 0;
386 		zpci_iomap_start[idx].bar = 0;
387 	}
388 	spin_unlock(&zpci_iomap_lock);
389 }
390 
391 static void pci_iounmap_mio(struct pci_dev *pdev, void __iomem *addr)
392 {
393 	iounmap(addr);
394 }
395 
396 void pci_iounmap(struct pci_dev *pdev, void __iomem *addr)
397 {
398 	if (static_branch_likely(&have_mio))
399 		pci_iounmap_mio(pdev, addr);
400 	else
401 		pci_iounmap_fh(pdev, addr);
402 }
403 EXPORT_SYMBOL(pci_iounmap);
404 
405 static int pci_read(struct pci_bus *bus, unsigned int devfn, int where,
406 		    int size, u32 *val)
407 {
408 	struct zpci_dev *zdev = zdev_from_bus(bus, devfn);
409 
410 	if (!zdev || zpci_cfg_load(zdev, where, val, size))
411 		return PCIBIOS_DEVICE_NOT_FOUND;
412 	return PCIBIOS_SUCCESSFUL;
413 }
414 
415 static int pci_write(struct pci_bus *bus, unsigned int devfn, int where,
416 		     int size, u32 val)
417 {
418 	struct zpci_dev *zdev = zdev_from_bus(bus, devfn);
419 
420 	if (!zdev || zpci_cfg_store(zdev, where, val, size))
421 		return PCIBIOS_DEVICE_NOT_FOUND;
422 	return PCIBIOS_SUCCESSFUL;
423 }
424 
425 static struct pci_ops pci_root_ops = {
426 	.read = pci_read,
427 	.write = pci_write,
428 };
429 
430 static void zpci_map_resources(struct pci_dev *pdev)
431 {
432 	struct zpci_dev *zdev = to_zpci(pdev);
433 	resource_size_t len;
434 	int i;
435 
436 	for (i = 0; i < PCI_STD_NUM_BARS; i++) {
437 		len = pci_resource_len(pdev, i);
438 		if (!len)
439 			continue;
440 
441 		if (zpci_use_mio(zdev))
442 			pdev->resource[i].start =
443 				(resource_size_t __force) zdev->bars[i].mio_wt;
444 		else
445 			pdev->resource[i].start = (resource_size_t __force)
446 				pci_iomap_range_fh(pdev, i, 0, 0);
447 		pdev->resource[i].end = pdev->resource[i].start + len - 1;
448 	}
449 
450 	zpci_iov_map_resources(pdev);
451 }
452 
453 static void zpci_unmap_resources(struct pci_dev *pdev)
454 {
455 	struct zpci_dev *zdev = to_zpci(pdev);
456 	resource_size_t len;
457 	int i;
458 
459 	if (zpci_use_mio(zdev))
460 		return;
461 
462 	for (i = 0; i < PCI_STD_NUM_BARS; i++) {
463 		len = pci_resource_len(pdev, i);
464 		if (!len)
465 			continue;
466 		pci_iounmap_fh(pdev, (void __iomem __force *)
467 			       pdev->resource[i].start);
468 	}
469 }
470 
471 static int zpci_alloc_iomap(struct zpci_dev *zdev)
472 {
473 	unsigned long entry;
474 
475 	spin_lock(&zpci_iomap_lock);
476 	entry = find_first_zero_bit(zpci_iomap_bitmap, ZPCI_IOMAP_ENTRIES);
477 	if (entry == ZPCI_IOMAP_ENTRIES) {
478 		spin_unlock(&zpci_iomap_lock);
479 		return -ENOSPC;
480 	}
481 	set_bit(entry, zpci_iomap_bitmap);
482 	spin_unlock(&zpci_iomap_lock);
483 	return entry;
484 }
485 
486 static void zpci_free_iomap(struct zpci_dev *zdev, int entry)
487 {
488 	spin_lock(&zpci_iomap_lock);
489 	memset(&zpci_iomap_start[entry], 0, sizeof(struct zpci_iomap_entry));
490 	clear_bit(entry, zpci_iomap_bitmap);
491 	spin_unlock(&zpci_iomap_lock);
492 }
493 
494 static void zpci_do_update_iomap_fh(struct zpci_dev *zdev, u32 fh)
495 {
496 	int bar, idx;
497 
498 	spin_lock(&zpci_iomap_lock);
499 	for (bar = 0; bar < PCI_STD_NUM_BARS; bar++) {
500 		if (!zdev->bars[bar].size)
501 			continue;
502 		idx = zdev->bars[bar].map_idx;
503 		if (!zpci_iomap_start[idx].count)
504 			continue;
505 		WRITE_ONCE(zpci_iomap_start[idx].fh, zdev->fh);
506 	}
507 	spin_unlock(&zpci_iomap_lock);
508 }
509 
510 void zpci_update_fh(struct zpci_dev *zdev, u32 fh)
511 {
512 	if (!fh || zdev->fh == fh)
513 		return;
514 
515 	zdev->fh = fh;
516 	if (zpci_use_mio(zdev))
517 		return;
518 	if (zdev->has_resources && zdev_enabled(zdev))
519 		zpci_do_update_iomap_fh(zdev, fh);
520 }
521 
522 static struct resource *__alloc_res(struct zpci_dev *zdev, unsigned long start,
523 				    unsigned long size, unsigned long flags)
524 {
525 	struct resource *r;
526 
527 	r = kzalloc_obj(*r);
528 	if (!r)
529 		return NULL;
530 
531 	r->start = start;
532 	r->end = r->start + size - 1;
533 	r->flags = flags;
534 	r->name = zdev->res_name;
535 
536 	if (request_resource(&iomem_resource, r)) {
537 		kfree(r);
538 		return NULL;
539 	}
540 	return r;
541 }
542 
543 int zpci_setup_bus_resources(struct zpci_dev *zdev)
544 {
545 	unsigned long addr, size, flags;
546 	struct resource *res;
547 	int i, entry;
548 
549 	snprintf(zdev->res_name, sizeof(zdev->res_name),
550 		 "PCI Bus %04x:%02x", zdev->uid, ZPCI_BUS_NR);
551 
552 	for (i = 0; i < PCI_STD_NUM_BARS; i++) {
553 		if (!zdev->bars[i].size)
554 			continue;
555 		entry = zpci_alloc_iomap(zdev);
556 		if (entry < 0)
557 			return entry;
558 		zdev->bars[i].map_idx = entry;
559 
560 		/* only MMIO is supported */
561 		flags = IORESOURCE_MEM;
562 		if (zdev->bars[i].val & 8)
563 			flags |= IORESOURCE_PREFETCH;
564 		if (zdev->bars[i].val & 4)
565 			flags |= IORESOURCE_MEM_64;
566 
567 		if (zpci_use_mio(zdev))
568 			addr = (unsigned long) zdev->bars[i].mio_wt;
569 		else
570 			addr = ZPCI_ADDR(entry);
571 		size = 1UL << zdev->bars[i].size;
572 
573 		res = __alloc_res(zdev, addr, size, flags);
574 		if (!res) {
575 			zpci_free_iomap(zdev, entry);
576 			return -ENOMEM;
577 		}
578 		zdev->bars[i].res = res;
579 	}
580 	zdev->has_resources = 1;
581 
582 	return 0;
583 }
584 
585 static void zpci_cleanup_bus_resources(struct zpci_dev *zdev)
586 {
587 	struct resource *res;
588 	int i;
589 
590 	pci_lock_rescan_remove();
591 	for (i = 0; i < PCI_STD_NUM_BARS; i++) {
592 		res = zdev->bars[i].res;
593 		if (!res)
594 			continue;
595 
596 		release_resource(res);
597 		pci_bus_remove_resource(zdev->zbus->bus, res);
598 		zpci_free_iomap(zdev, zdev->bars[i].map_idx);
599 		zdev->bars[i].res = NULL;
600 		kfree(res);
601 	}
602 	zdev->has_resources = 0;
603 	pci_unlock_rescan_remove();
604 }
605 
606 int pcibios_device_add(struct pci_dev *pdev)
607 {
608 	struct zpci_dev *zdev = to_zpci(pdev);
609 	struct resource *res;
610 	int i;
611 
612 	/* The pdev has a reference to the zdev via its bus */
613 	zpci_zdev_get(zdev);
614 	if (pdev->is_physfn)
615 		pdev->no_vf_scan = 1;
616 
617 	zpci_map_resources(pdev);
618 
619 	for (i = 0; i < PCI_STD_NUM_BARS; i++) {
620 		res = &pdev->resource[i];
621 		if (res->parent || !res->flags)
622 			continue;
623 		pci_claim_resource(pdev, i);
624 	}
625 
626 	return 0;
627 }
628 
629 void pcibios_release_device(struct pci_dev *pdev)
630 {
631 	struct zpci_dev *zdev = to_zpci(pdev);
632 
633 	zpci_unmap_resources(pdev);
634 	zpci_zdev_put(zdev);
635 }
636 
637 int pcibios_enable_device(struct pci_dev *pdev, int mask)
638 {
639 	struct zpci_dev *zdev = to_zpci(pdev);
640 
641 	zpci_debug_init_device(zdev, dev_name(&pdev->dev));
642 	zpci_fmb_enable_device(zdev);
643 
644 	return pci_enable_resources(pdev, mask);
645 }
646 
647 void pcibios_disable_device(struct pci_dev *pdev)
648 {
649 	struct zpci_dev *zdev = to_zpci(pdev);
650 
651 	zpci_fmb_disable_device(zdev);
652 	zpci_debug_exit_device(zdev);
653 }
654 
655 static int __zpci_register_domain(int domain)
656 {
657 	spin_lock(&zpci_domain_lock);
658 	if (test_bit(domain, zpci_domain)) {
659 		spin_unlock(&zpci_domain_lock);
660 		pr_err("Domain %04x is already assigned\n", domain);
661 		return -EEXIST;
662 	}
663 	set_bit(domain, zpci_domain);
664 	spin_unlock(&zpci_domain_lock);
665 	return domain;
666 }
667 
668 static int __zpci_alloc_domain(void)
669 {
670 	int domain;
671 
672 	spin_lock(&zpci_domain_lock);
673 	/*
674 	 * We can always auto allocate domains below ZPCI_NR_DEVICES.
675 	 * There is either a free domain or we have reached the maximum in
676 	 * which case we would have bailed earlier.
677 	 */
678 	domain = find_first_zero_bit(zpci_domain, ZPCI_NR_DEVICES);
679 	set_bit(domain, zpci_domain);
680 	spin_unlock(&zpci_domain_lock);
681 	return domain;
682 }
683 
684 int zpci_alloc_domain(int domain)
685 {
686 	if (zpci_unique_uid) {
687 		if (domain)
688 			return __zpci_register_domain(domain);
689 		pr_warn("UID checking was active but no UID is provided: switching to automatic domain allocation\n");
690 		update_uid_checking(false);
691 	}
692 	return __zpci_alloc_domain();
693 }
694 
695 void zpci_free_domain(int domain)
696 {
697 	spin_lock(&zpci_domain_lock);
698 	clear_bit(domain, zpci_domain);
699 	spin_unlock(&zpci_domain_lock);
700 }
701 
702 
703 int zpci_enable_device(struct zpci_dev *zdev)
704 {
705 	u32 fh = zdev->fh;
706 	int rc = 0;
707 
708 	if (clp_enable_fh(zdev, &fh, ZPCI_NR_DMA_SPACES))
709 		rc = -EIO;
710 	else
711 		zpci_update_fh(zdev, fh);
712 	return rc;
713 }
714 EXPORT_SYMBOL_GPL(zpci_enable_device);
715 
716 int zpci_reenable_device(struct zpci_dev *zdev)
717 {
718 	u8 status;
719 	int rc;
720 
721 	rc = zpci_enable_device(zdev);
722 	if (rc)
723 		return rc;
724 
725 	if (zdev->msi_nr_irqs > 0) {
726 		rc = zpci_set_irq(zdev);
727 		if (rc)
728 			return rc;
729 	}
730 
731 	rc = zpci_iommu_register_ioat(zdev, &status);
732 	if (rc)
733 		zpci_disable_device(zdev);
734 
735 	return rc;
736 }
737 EXPORT_SYMBOL_GPL(zpci_reenable_device);
738 
739 int zpci_disable_device(struct zpci_dev *zdev)
740 {
741 	u32 fh = zdev->fh;
742 	int cc, rc = 0;
743 
744 	cc = clp_disable_fh(zdev, &fh);
745 	if (!cc) {
746 		zpci_update_fh(zdev, fh);
747 	} else if (cc == CLP_RC_SETPCIFN_ALRDY) {
748 		pr_info("Disabling PCI function %08x had no effect as it was already disabled\n",
749 			zdev->fid);
750 		/* Function is already disabled - update handle */
751 		rc = clp_refresh_fh(zdev->fid, &fh);
752 		if (!rc) {
753 			zpci_update_fh(zdev, fh);
754 			rc = -EINVAL;
755 		}
756 	} else {
757 		rc = -EIO;
758 	}
759 	return rc;
760 }
761 EXPORT_SYMBOL_GPL(zpci_disable_device);
762 
763 /**
764  * zpci_hot_reset_device - perform a reset of the given zPCI function
765  * @zdev: the slot which should be reset
766  *
767  * Performs a low level reset of the zPCI function. The reset is low level in
768  * the sense that the zPCI function can be reset without detaching it from the
769  * common PCI subsystem. The reset may be performed while under control of
770  * either DMA or IOMMU APIs in which case the existing DMA/IOMMU translation
771  * table is reinstated at the end of the reset.
772  *
773  * After the reset the functions internal state is reset to an initial state
774  * equivalent to its state during boot when first probing a driver.
775  * Consequently after reset the PCI function requires re-initialization via the
776  * common PCI code including re-enabling IRQs via pci_alloc_irq_vectors()
777  * and enabling the function via e.g. pci_enable_device_flags(). The caller
778  * must guard against concurrent reset attempts.
779  *
780  * In most cases this function should not be called directly but through
781  * pci_reset_function() or pci_reset_bus() which handle the save/restore and
782  * locking - asserted by lockdep.
783  *
784  * Return: 0 on success and an error value otherwise
785  */
786 int zpci_hot_reset_device(struct zpci_dev *zdev)
787 {
788 	int rc;
789 
790 	lockdep_assert_held(&zdev->state_lock);
791 	zpci_dbg(3, "rst fid:%x, fh:%x\n", zdev->fid, zdev->fh);
792 	if (zdev_enabled(zdev)) {
793 		/* Disables device access, DMAs and IRQs (reset state) */
794 		rc = zpci_disable_device(zdev);
795 		/*
796 		 * Due to a z/VM vs LPAR inconsistency in the error state the
797 		 * FH may indicate an enabled device but disable says the
798 		 * device is already disabled don't treat it as an error here.
799 		 */
800 		if (rc == -EINVAL)
801 			rc = 0;
802 		if (rc)
803 			return rc;
804 	}
805 
806 	rc = zpci_reenable_device(zdev);
807 
808 	return rc;
809 }
810 
811 /**
812  * zpci_create_device() - Create a new zpci_dev and add it to the zbus
813  * @fid: Function ID of the device to be created
814  * @fh: Current Function Handle of the device to be created
815  * @state: Initial state after creation either Standby or Configured
816  *
817  * Allocates a new struct zpci_dev and queries the platform for its details.
818  * If successful the device can subsequently be added to the zPCI subsystem
819  * using zpci_add_device().
820  *
821  * Returns: the zdev on success or an error pointer otherwise
822  */
823 struct zpci_dev *zpci_create_device(u32 fid, u32 fh, enum zpci_state state)
824 {
825 	struct zpci_dev *zdev;
826 	int rc;
827 
828 	zdev = kzalloc_obj(*zdev);
829 	if (!zdev)
830 		return ERR_PTR(-ENOMEM);
831 
832 	/* FID and Function Handle are the static/dynamic identifiers */
833 	zdev->fid = fid;
834 	zdev->fh = fh;
835 
836 	/* Query function properties and update zdev */
837 	rc = clp_query_pci_fn(zdev);
838 	if (rc)
839 		goto error;
840 	zdev->state =  state;
841 
842 	mutex_init(&zdev->state_lock);
843 	mutex_init(&zdev->fmb_lock);
844 	mutex_init(&zdev->kzdev_lock);
845 
846 	return zdev;
847 
848 error:
849 	zpci_dbg(0, "crt fid:%x, rc:%d\n", fid, rc);
850 	kfree(zdev);
851 	return ERR_PTR(rc);
852 }
853 
854 /**
855  * zpci_add_device() - Add a previously created zPCI device to the zPCI subsystem
856  * @zdev: The zPCI device to be added
857  *
858  * A struct zpci_dev is added to the zPCI subsystem and to a virtual PCI bus creating
859  * a new one as necessary. A hotplug slot is created and events start to be handled.
860  * If successful from this point on zpci_zdev_get() and zpci_zdev_put() must be used.
861  * If adding the struct zpci_dev fails the device was not added and should be freed.
862  *
863  * Return: 0 on success, or an error code otherwise
864  */
865 int zpci_add_device(struct zpci_dev *zdev)
866 {
867 	int rc;
868 
869 	mutex_lock(&zpci_add_remove_lock);
870 	zpci_dbg(1, "add fid:%x, fh:%x, c:%d\n", zdev->fid, zdev->fh, zdev->state);
871 	rc = zpci_init_iommu(zdev);
872 	if (rc)
873 		goto error;
874 
875 	rc = zpci_bus_device_register(zdev, &pci_root_ops);
876 	if (rc)
877 		goto error_destroy_iommu;
878 
879 	kref_init(&zdev->kref);
880 	spin_lock(&zpci_list_lock);
881 	list_add_tail(&zdev->entry, &zpci_list);
882 	spin_unlock(&zpci_list_lock);
883 	mutex_unlock(&zpci_add_remove_lock);
884 	return 0;
885 
886 error_destroy_iommu:
887 	zpci_destroy_iommu(zdev);
888 error:
889 	zpci_dbg(0, "add fid:%x, rc:%d\n", zdev->fid, rc);
890 	mutex_unlock(&zpci_add_remove_lock);
891 	return rc;
892 }
893 
894 bool zpci_is_device_configured(struct zpci_dev *zdev)
895 {
896 	enum zpci_state state = zdev->state;
897 
898 	return state != ZPCI_FN_STATE_RESERVED &&
899 		state != ZPCI_FN_STATE_STANDBY;
900 }
901 
902 /**
903  * zpci_scan_configured_device() - Scan a freshly configured zpci_dev
904  * @zdev: The zpci_dev to be configured
905  * @fh: The general function handle supplied by the platform
906  *
907  * Given a device in the configuration state Configured, enables, scans and
908  * adds it to the common code PCI subsystem if possible. If any failure occurs,
909  * the zpci_dev is left disabled.
910  *
911  * Return: 0 on success, or an error code otherwise
912  */
913 int zpci_scan_configured_device(struct zpci_dev *zdev, u32 fh)
914 {
915 	zpci_update_fh(zdev, fh);
916 	return zpci_bus_scan_device(zdev);
917 }
918 
919 /**
920  * zpci_deconfigure_device() - Deconfigure a zpci_dev
921  * @zdev: The zpci_dev to configure
922  *
923  * Deconfigure a zPCI function that is currently configured and possibly known
924  * to the common code PCI subsystem.
925  * If any failure occurs the device is left as is.
926  *
927  * Return: 0 on success, or an error code otherwise
928  */
929 int zpci_deconfigure_device(struct zpci_dev *zdev)
930 {
931 	int rc;
932 
933 	lockdep_assert_held(&zdev->state_lock);
934 	if (zdev->state != ZPCI_FN_STATE_CONFIGURED)
935 		return 0;
936 
937 	if (zdev->zbus->bus)
938 		zpci_bus_remove_device(zdev, false);
939 
940 	if (zdev_enabled(zdev)) {
941 		rc = zpci_disable_device(zdev);
942 		if (rc)
943 			return rc;
944 	}
945 
946 	rc = sclp_pci_deconfigure(zdev->fid);
947 	zpci_dbg(3, "deconf fid:%x, rc:%d\n", zdev->fid, rc);
948 	if (rc)
949 		return rc;
950 	zdev->state = ZPCI_FN_STATE_STANDBY;
951 
952 	return 0;
953 }
954 
955 /**
956  * zpci_device_reserved() - Mark device as reserved
957  * @zdev: the zpci_dev that was reserved
958  *
959  * Handle the case that a given zPCI function was reserved by another system.
960  */
961 void zpci_device_reserved(struct zpci_dev *zdev)
962 {
963 	lockdep_assert_held(&zdev->state_lock);
964 	/* We may declare the device reserved multiple times */
965 	if (zdev->state == ZPCI_FN_STATE_RESERVED)
966 		return;
967 	zdev->state = ZPCI_FN_STATE_RESERVED;
968 	zpci_dbg(3, "rsv fid:%x\n", zdev->fid);
969 	/*
970 	 * The underlying device is gone. Allow the zdev to be freed
971 	 * as soon as all other references are gone by accounting for
972 	 * the removal as a dropped reference.
973 	 */
974 	zpci_zdev_put(zdev);
975 }
976 
977 void zpci_release_device(struct kref *kref)
978 	__releases(&zpci_list_lock)
979 {
980 	struct zpci_dev *zdev = container_of(kref, struct zpci_dev, kref);
981 
982 	lockdep_assert_held(&zpci_add_remove_lock);
983 	WARN_ON(zdev->state != ZPCI_FN_STATE_RESERVED);
984 	/*
985 	 * We already hold zpci_list_lock thanks to kref_put_lock().
986 	 * This makes sure no new reference can be taken from the list.
987 	 */
988 	list_del(&zdev->entry);
989 	spin_unlock(&zpci_list_lock);
990 
991 	if (zdev->has_hp_slot)
992 		zpci_exit_slot(zdev);
993 
994 	if (zdev->has_resources)
995 		zpci_cleanup_bus_resources(zdev);
996 
997 	zpci_bus_device_unregister(zdev);
998 	zpci_destroy_iommu(zdev);
999 	zpci_dbg(3, "rem fid:%x\n", zdev->fid);
1000 	kfree_rcu(zdev, rcu);
1001 }
1002 
1003 int zpci_report_error(struct pci_dev *pdev,
1004 		      struct zpci_report_error_header *report)
1005 {
1006 	struct zpci_dev *zdev = to_zpci(pdev);
1007 
1008 	return sclp_pci_report(report, zdev->fh, zdev->fid);
1009 }
1010 EXPORT_SYMBOL(zpci_report_error);
1011 
1012 /**
1013  * zpci_clear_error_state() - Clears the zPCI error state of the device
1014  * @zdev: The zdev for which the zPCI error state should be reset
1015  *
1016  * Clear the zPCI error state of the device. If clearing the zPCI error state
1017  * fails the device is left in the error state. In this case it may make sense
1018  * to call zpci_io_perm_failure() on the associated pdev if it exists.
1019  *
1020  * Returns: 0 on success, -EIO otherwise
1021  */
1022 int zpci_clear_error_state(struct zpci_dev *zdev)
1023 {
1024 	u64 req = ZPCI_CREATE_REQ(zdev->fh, 0, ZPCI_MOD_FC_RESET_ERROR);
1025 	struct zpci_fib fib = {0};
1026 	u8 status;
1027 	int cc;
1028 
1029 	cc = zpci_mod_fc(req, &fib, &status);
1030 	if (cc) {
1031 		zpci_dbg(3, "ces fid:%x, cc:%d, status:%x\n", zdev->fid, cc, status);
1032 		return -EIO;
1033 	}
1034 
1035 	return 0;
1036 }
1037 
1038 /**
1039  * zpci_reset_load_store_blocked() - Re-enables L/S from error state
1040  * @zdev: The zdev for which to unblock load/store access
1041  *
1042  * Re-enables load/store access for a PCI function in the error state while
1043  * keeping DMA blocked. In this state drivers can poke MMIO space to determine
1044  * if error recovery is possible while catching any rogue DMA access from the
1045  * device.
1046  *
1047  * Returns: 0 on success, -EIO otherwise
1048  */
1049 int zpci_reset_load_store_blocked(struct zpci_dev *zdev)
1050 {
1051 	u64 req = ZPCI_CREATE_REQ(zdev->fh, 0, ZPCI_MOD_FC_RESET_BLOCK);
1052 	struct zpci_fib fib = {0};
1053 	u8 status;
1054 	int cc;
1055 
1056 	cc = zpci_mod_fc(req, &fib, &status);
1057 	if (cc) {
1058 		zpci_dbg(3, "rls fid:%x, cc:%d, status:%x\n", zdev->fid, cc, status);
1059 		return -EIO;
1060 	}
1061 
1062 	return 0;
1063 }
1064 
1065 static int zpci_mem_init(void)
1066 {
1067 	BUILD_BUG_ON(!is_power_of_2(__alignof__(struct zpci_fmb)) ||
1068 		     __alignof__(struct zpci_fmb) < sizeof(struct zpci_fmb));
1069 	BUILD_BUG_ON((CONFIG_ILLEGAL_POINTER_VALUE + 0x10000 > ZPCI_IOMAP_ADDR_BASE) &&
1070 		     (CONFIG_ILLEGAL_POINTER_VALUE <= ZPCI_IOMAP_ADDR_MAX));
1071 
1072 	zdev_fmb_cache = kmem_cache_create("PCI_FMB_cache", sizeof(struct zpci_fmb),
1073 					   __alignof__(struct zpci_fmb), 0, NULL);
1074 	if (!zdev_fmb_cache)
1075 		goto error_fmb;
1076 
1077 	zpci_iomap_start = kzalloc_objs(*zpci_iomap_start, ZPCI_IOMAP_ENTRIES);
1078 	if (!zpci_iomap_start)
1079 		goto error_iomap;
1080 
1081 	zpci_iomap_bitmap = kcalloc(BITS_TO_LONGS(ZPCI_IOMAP_ENTRIES),
1082 				    sizeof(*zpci_iomap_bitmap), GFP_KERNEL);
1083 	if (!zpci_iomap_bitmap)
1084 		goto error_iomap_bitmap;
1085 
1086 	if (static_branch_likely(&have_mio))
1087 		clp_setup_writeback_mio();
1088 
1089 	return 0;
1090 error_iomap_bitmap:
1091 	kfree(zpci_iomap_start);
1092 error_iomap:
1093 	kmem_cache_destroy(zdev_fmb_cache);
1094 error_fmb:
1095 	return -ENOMEM;
1096 }
1097 
1098 static void zpci_mem_exit(void)
1099 {
1100 	kfree(zpci_iomap_bitmap);
1101 	kfree(zpci_iomap_start);
1102 	kmem_cache_destroy(zdev_fmb_cache);
1103 }
1104 
1105 static unsigned int s390_pci_probe __initdata = 1;
1106 unsigned int s390_pci_force_floating __initdata;
1107 static unsigned int s390_pci_initialized;
1108 
1109 char * __init pcibios_setup(char *str)
1110 {
1111 	if (!strcmp(str, "off")) {
1112 		s390_pci_probe = 0;
1113 		return NULL;
1114 	}
1115 	if (!strcmp(str, "nomio")) {
1116 		clear_machine_feature(MFEATURE_PCI_MIO);
1117 		return NULL;
1118 	}
1119 	if (!strcmp(str, "force_floating")) {
1120 		s390_pci_force_floating = 1;
1121 		return NULL;
1122 	}
1123 	if (!strcmp(str, "norid")) {
1124 		s390_pci_no_rid = 1;
1125 		return NULL;
1126 	}
1127 	return str;
1128 }
1129 
1130 bool zpci_is_enabled(void)
1131 {
1132 	return s390_pci_initialized;
1133 }
1134 
1135 static int zpci_cmp_rid(void *priv, const struct list_head *a,
1136 			const struct list_head *b)
1137 {
1138 	struct zpci_dev *za = container_of(a, struct zpci_dev, entry);
1139 	struct zpci_dev *zb = container_of(b, struct zpci_dev, entry);
1140 
1141 	/*
1142 	 * PCI functions without RID available maintain original order
1143 	 * between themselves but sort before those with RID.
1144 	 */
1145 	if (za->rid == zb->rid)
1146 		return za->rid_available > zb->rid_available;
1147 	/*
1148 	 * PCI functions with RID sort by RID ascending.
1149 	 */
1150 	return za->rid > zb->rid;
1151 }
1152 
1153 static void zpci_add_devices(struct list_head *scan_list)
1154 {
1155 	struct zpci_dev *zdev, *tmp;
1156 
1157 	list_sort(NULL, scan_list, &zpci_cmp_rid);
1158 	list_for_each_entry_safe(zdev, tmp, scan_list, entry) {
1159 		list_del_init(&zdev->entry);
1160 		if (zpci_add_device(zdev))
1161 			kfree(zdev);
1162 	}
1163 }
1164 
1165 int zpci_scan_devices(void)
1166 {
1167 	struct zpci_bus *zbus;
1168 	LIST_HEAD(scan_list);
1169 	int rc;
1170 
1171 	rc = clp_scan_pci_devices(&scan_list);
1172 	if (rc)
1173 		return rc;
1174 
1175 	zpci_add_devices(&scan_list);
1176 	zpci_bus_for_each(zbus) {
1177 		zpci_bus_scan_bus(zbus);
1178 		cond_resched();
1179 	}
1180 	return 0;
1181 }
1182 
1183 static int __init pci_base_init(void)
1184 {
1185 	int rc;
1186 
1187 	if (!s390_pci_probe)
1188 		return 0;
1189 
1190 	if (!test_facility(69) || !test_facility(71)) {
1191 		pr_info("PCI is not supported because CPU facilities 69 or 71 are not available\n");
1192 		return 0;
1193 	}
1194 
1195 	if (test_machine_feature(MFEATURE_PCI_MIO)) {
1196 		static_branch_enable(&have_mio);
1197 		system_ctl_set_bit(2, CR2_MIO_ADDRESSING_BIT);
1198 	}
1199 
1200 	rc = zpci_debug_init();
1201 	if (rc)
1202 		goto out;
1203 
1204 	rc = zpci_mem_init();
1205 	if (rc)
1206 		goto out_mem;
1207 
1208 	rc = zpci_irq_init();
1209 	if (rc)
1210 		goto out_irq;
1211 
1212 	rc = zpci_scan_devices();
1213 	if (rc)
1214 		goto out_find;
1215 
1216 	rc = zpci_fw_sysfs_init();
1217 	if (rc)
1218 		goto out_find;
1219 
1220 	s390_pci_initialized = 1;
1221 	return 0;
1222 
1223 out_find:
1224 	zpci_irq_exit();
1225 out_irq:
1226 	zpci_mem_exit();
1227 out_mem:
1228 	zpci_debug_exit();
1229 out:
1230 	return rc;
1231 }
1232 subsys_initcall_sync(pci_base_init);
1233