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