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