s390/pci: add error message for UID collision
[linux-2.6-microblaze.git] / arch / s390 / pci / pci.c
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
32 #include <asm/isc.h>
33 #include <asm/airq.h>
34 #include <asm/facility.h>
35 #include <asm/pci_insn.h>
36 #include <asm/pci_clp.h>
37 #include <asm/pci_dma.h>
38
39 /* list of all detected zpci devices */
40 static LIST_HEAD(zpci_list);
41 static DEFINE_SPINLOCK(zpci_list_lock);
42
43 static DECLARE_BITMAP(zpci_domain, ZPCI_NR_DEVICES);
44 static DEFINE_SPINLOCK(zpci_domain_lock);
45
46 #define ZPCI_IOMAP_ENTRIES                                              \
47         min(((unsigned long) ZPCI_NR_DEVICES * PCI_BAR_COUNT / 2),      \
48             ZPCI_IOMAP_MAX_ENTRIES)
49
50 static DEFINE_SPINLOCK(zpci_iomap_lock);
51 static unsigned long *zpci_iomap_bitmap;
52 struct zpci_iomap_entry *zpci_iomap_start;
53 EXPORT_SYMBOL_GPL(zpci_iomap_start);
54
55 DEFINE_STATIC_KEY_FALSE(have_mio);
56
57 static struct kmem_cache *zdev_fmb_cache;
58
59 struct zpci_dev *get_zdev_by_fid(u32 fid)
60 {
61         struct zpci_dev *tmp, *zdev = NULL;
62
63         spin_lock(&zpci_list_lock);
64         list_for_each_entry(tmp, &zpci_list, entry) {
65                 if (tmp->fid == fid) {
66                         zdev = tmp;
67                         break;
68                 }
69         }
70         spin_unlock(&zpci_list_lock);
71         return zdev;
72 }
73
74 void zpci_remove_reserved_devices(void)
75 {
76         struct zpci_dev *tmp, *zdev;
77         enum zpci_state state;
78         LIST_HEAD(remove);
79
80         spin_lock(&zpci_list_lock);
81         list_for_each_entry_safe(zdev, tmp, &zpci_list, entry) {
82                 if (zdev->state == ZPCI_FN_STATE_STANDBY &&
83                     !clp_get_state(zdev->fid, &state) &&
84                     state == ZPCI_FN_STATE_RESERVED)
85                         list_move_tail(&zdev->entry, &remove);
86         }
87         spin_unlock(&zpci_list_lock);
88
89         list_for_each_entry_safe(zdev, tmp, &remove, entry)
90                 zpci_remove_device(zdev);
91 }
92
93 static struct zpci_dev *get_zdev_by_bus(struct pci_bus *bus)
94 {
95         return (bus && bus->sysdata) ? (struct zpci_dev *) bus->sysdata : NULL;
96 }
97
98 int pci_domain_nr(struct pci_bus *bus)
99 {
100         return ((struct zpci_dev *) bus->sysdata)->domain;
101 }
102 EXPORT_SYMBOL_GPL(pci_domain_nr);
103
104 int pci_proc_domain(struct pci_bus *bus)
105 {
106         return pci_domain_nr(bus);
107 }
108 EXPORT_SYMBOL_GPL(pci_proc_domain);
109
110 /* Modify PCI: Register I/O address translation parameters */
111 int zpci_register_ioat(struct zpci_dev *zdev, u8 dmaas,
112                        u64 base, u64 limit, u64 iota)
113 {
114         u64 req = ZPCI_CREATE_REQ(zdev->fh, dmaas, ZPCI_MOD_FC_REG_IOAT);
115         struct zpci_fib fib = {0};
116         u8 status;
117
118         WARN_ON_ONCE(iota & 0x3fff);
119         fib.pba = base;
120         fib.pal = limit;
121         fib.iota = iota | ZPCI_IOTA_RTTO_FLAG;
122         return zpci_mod_fc(req, &fib, &status) ? -EIO : 0;
123 }
124
125 /* Modify PCI: Unregister I/O address translation parameters */
126 int zpci_unregister_ioat(struct zpci_dev *zdev, u8 dmaas)
127 {
128         u64 req = ZPCI_CREATE_REQ(zdev->fh, dmaas, ZPCI_MOD_FC_DEREG_IOAT);
129         struct zpci_fib fib = {0};
130         u8 cc, status;
131
132         cc = zpci_mod_fc(req, &fib, &status);
133         if (cc == 3) /* Function already gone. */
134                 cc = 0;
135         return cc ? -EIO : 0;
136 }
137
138 /* Modify PCI: Set PCI function measurement parameters */
139 int zpci_fmb_enable_device(struct zpci_dev *zdev)
140 {
141         u64 req = ZPCI_CREATE_REQ(zdev->fh, 0, ZPCI_MOD_FC_SET_MEASURE);
142         struct zpci_fib fib = {0};
143         u8 cc, status;
144
145         if (zdev->fmb || sizeof(*zdev->fmb) < zdev->fmb_length)
146                 return -EINVAL;
147
148         zdev->fmb = kmem_cache_zalloc(zdev_fmb_cache, GFP_KERNEL);
149         if (!zdev->fmb)
150                 return -ENOMEM;
151         WARN_ON((u64) zdev->fmb & 0xf);
152
153         /* reset software counters */
154         atomic64_set(&zdev->allocated_pages, 0);
155         atomic64_set(&zdev->mapped_pages, 0);
156         atomic64_set(&zdev->unmapped_pages, 0);
157
158         fib.fmb_addr = virt_to_phys(zdev->fmb);
159         cc = zpci_mod_fc(req, &fib, &status);
160         if (cc) {
161                 kmem_cache_free(zdev_fmb_cache, zdev->fmb);
162                 zdev->fmb = NULL;
163         }
164         return cc ? -EIO : 0;
165 }
166
167 /* Modify PCI: Disable PCI function measurement */
168 int zpci_fmb_disable_device(struct zpci_dev *zdev)
169 {
170         u64 req = ZPCI_CREATE_REQ(zdev->fh, 0, ZPCI_MOD_FC_SET_MEASURE);
171         struct zpci_fib fib = {0};
172         u8 cc, status;
173
174         if (!zdev->fmb)
175                 return -EINVAL;
176
177         /* Function measurement is disabled if fmb address is zero */
178         cc = zpci_mod_fc(req, &fib, &status);
179         if (cc == 3) /* Function already gone. */
180                 cc = 0;
181
182         if (!cc) {
183                 kmem_cache_free(zdev_fmb_cache, zdev->fmb);
184                 zdev->fmb = NULL;
185         }
186         return cc ? -EIO : 0;
187 }
188
189 static int zpci_cfg_load(struct zpci_dev *zdev, int offset, u32 *val, u8 len)
190 {
191         u64 req = ZPCI_CREATE_REQ(zdev->fh, ZPCI_PCIAS_CFGSPC, len);
192         u64 data;
193         int rc;
194
195         rc = __zpci_load(&data, req, offset);
196         if (!rc) {
197                 data = le64_to_cpu((__force __le64) data);
198                 data >>= (8 - len) * 8;
199                 *val = (u32) data;
200         } else
201                 *val = 0xffffffff;
202         return rc;
203 }
204
205 static int zpci_cfg_store(struct zpci_dev *zdev, int offset, u32 val, u8 len)
206 {
207         u64 req = ZPCI_CREATE_REQ(zdev->fh, ZPCI_PCIAS_CFGSPC, len);
208         u64 data = val;
209         int rc;
210
211         data <<= (8 - len) * 8;
212         data = (__force u64) cpu_to_le64(data);
213         rc = __zpci_store(data, req, offset);
214         return rc;
215 }
216
217 resource_size_t pcibios_align_resource(void *data, const struct resource *res,
218                                        resource_size_t size,
219                                        resource_size_t align)
220 {
221         return 0;
222 }
223
224 /* combine single writes by using store-block insn */
225 void __iowrite64_copy(void __iomem *to, const void *from, size_t count)
226 {
227        zpci_memcpy_toio(to, from, count);
228 }
229
230 void __iomem *ioremap(unsigned long ioaddr, unsigned long size)
231 {
232         struct vm_struct *area;
233         unsigned long offset;
234
235         if (!size)
236                 return NULL;
237
238         if (!static_branch_unlikely(&have_mio))
239                 return (void __iomem *) ioaddr;
240
241         offset = ioaddr & ~PAGE_MASK;
242         ioaddr &= PAGE_MASK;
243         size = PAGE_ALIGN(size + offset);
244         area = get_vm_area(size, VM_IOREMAP);
245         if (!area)
246                 return NULL;
247
248         if (ioremap_page_range((unsigned long) area->addr,
249                                (unsigned long) area->addr + size,
250                                ioaddr, PAGE_KERNEL)) {
251                 vunmap(area->addr);
252                 return NULL;
253         }
254         return (void __iomem *) ((unsigned long) area->addr + offset);
255 }
256 EXPORT_SYMBOL(ioremap);
257
258 void iounmap(volatile void __iomem *addr)
259 {
260         if (static_branch_likely(&have_mio))
261                 vunmap((__force void *) ((unsigned long) addr & PAGE_MASK));
262 }
263 EXPORT_SYMBOL(iounmap);
264
265 /* Create a virtual mapping cookie for a PCI BAR */
266 static void __iomem *pci_iomap_range_fh(struct pci_dev *pdev, int bar,
267                                         unsigned long offset, unsigned long max)
268 {
269         struct zpci_dev *zdev = to_zpci(pdev);
270         int idx;
271
272         idx = zdev->bars[bar].map_idx;
273         spin_lock(&zpci_iomap_lock);
274         /* Detect overrun */
275         WARN_ON(!++zpci_iomap_start[idx].count);
276         zpci_iomap_start[idx].fh = zdev->fh;
277         zpci_iomap_start[idx].bar = bar;
278         spin_unlock(&zpci_iomap_lock);
279
280         return (void __iomem *) ZPCI_ADDR(idx) + offset;
281 }
282
283 static void __iomem *pci_iomap_range_mio(struct pci_dev *pdev, int bar,
284                                          unsigned long offset,
285                                          unsigned long max)
286 {
287         unsigned long barsize = pci_resource_len(pdev, bar);
288         struct zpci_dev *zdev = to_zpci(pdev);
289         void __iomem *iova;
290
291         iova = ioremap((unsigned long) zdev->bars[bar].mio_wt, barsize);
292         return iova ? iova + offset : iova;
293 }
294
295 void __iomem *pci_iomap_range(struct pci_dev *pdev, int bar,
296                               unsigned long offset, unsigned long max)
297 {
298         if (!pci_resource_len(pdev, bar) || bar >= PCI_BAR_COUNT)
299                 return NULL;
300
301         if (static_branch_likely(&have_mio))
302                 return pci_iomap_range_mio(pdev, bar, offset, max);
303         else
304                 return pci_iomap_range_fh(pdev, bar, offset, max);
305 }
306 EXPORT_SYMBOL(pci_iomap_range);
307
308 void __iomem *pci_iomap(struct pci_dev *dev, int bar, unsigned long maxlen)
309 {
310         return pci_iomap_range(dev, bar, 0, maxlen);
311 }
312 EXPORT_SYMBOL(pci_iomap);
313
314 static void __iomem *pci_iomap_wc_range_mio(struct pci_dev *pdev, int bar,
315                                             unsigned long offset, unsigned long max)
316 {
317         unsigned long barsize = pci_resource_len(pdev, bar);
318         struct zpci_dev *zdev = to_zpci(pdev);
319         void __iomem *iova;
320
321         iova = ioremap((unsigned long) zdev->bars[bar].mio_wb, barsize);
322         return iova ? iova + offset : iova;
323 }
324
325 void __iomem *pci_iomap_wc_range(struct pci_dev *pdev, int bar,
326                                  unsigned long offset, unsigned long max)
327 {
328         if (!pci_resource_len(pdev, bar) || bar >= PCI_BAR_COUNT)
329                 return NULL;
330
331         if (static_branch_likely(&have_mio))
332                 return pci_iomap_wc_range_mio(pdev, bar, offset, max);
333         else
334                 return pci_iomap_range_fh(pdev, bar, offset, max);
335 }
336 EXPORT_SYMBOL(pci_iomap_wc_range);
337
338 void __iomem *pci_iomap_wc(struct pci_dev *dev, int bar, unsigned long maxlen)
339 {
340         return pci_iomap_wc_range(dev, bar, 0, maxlen);
341 }
342 EXPORT_SYMBOL(pci_iomap_wc);
343
344 static void pci_iounmap_fh(struct pci_dev *pdev, void __iomem *addr)
345 {
346         unsigned int idx = ZPCI_IDX(addr);
347
348         spin_lock(&zpci_iomap_lock);
349         /* Detect underrun */
350         WARN_ON(!zpci_iomap_start[idx].count);
351         if (!--zpci_iomap_start[idx].count) {
352                 zpci_iomap_start[idx].fh = 0;
353                 zpci_iomap_start[idx].bar = 0;
354         }
355         spin_unlock(&zpci_iomap_lock);
356 }
357
358 static void pci_iounmap_mio(struct pci_dev *pdev, void __iomem *addr)
359 {
360         iounmap(addr);
361 }
362
363 void pci_iounmap(struct pci_dev *pdev, void __iomem *addr)
364 {
365         if (static_branch_likely(&have_mio))
366                 pci_iounmap_mio(pdev, addr);
367         else
368                 pci_iounmap_fh(pdev, addr);
369 }
370 EXPORT_SYMBOL(pci_iounmap);
371
372 static int pci_read(struct pci_bus *bus, unsigned int devfn, int where,
373                     int size, u32 *val)
374 {
375         struct zpci_dev *zdev = get_zdev_by_bus(bus);
376         int ret;
377
378         if (!zdev || devfn != ZPCI_DEVFN)
379                 ret = -ENODEV;
380         else
381                 ret = zpci_cfg_load(zdev, where, val, size);
382
383         return ret;
384 }
385
386 static int pci_write(struct pci_bus *bus, unsigned int devfn, int where,
387                      int size, u32 val)
388 {
389         struct zpci_dev *zdev = get_zdev_by_bus(bus);
390         int ret;
391
392         if (!zdev || devfn != ZPCI_DEVFN)
393                 ret = -ENODEV;
394         else
395                 ret = zpci_cfg_store(zdev, where, val, size);
396
397         return ret;
398 }
399
400 static struct pci_ops pci_root_ops = {
401         .read = pci_read,
402         .write = pci_write,
403 };
404
405 #ifdef CONFIG_PCI_IOV
406 static struct resource iov_res = {
407         .name   = "PCI IOV res",
408         .start  = 0,
409         .end    = -1,
410         .flags  = IORESOURCE_MEM,
411 };
412 #endif
413
414 static void zpci_map_resources(struct pci_dev *pdev)
415 {
416         struct zpci_dev *zdev = to_zpci(pdev);
417         resource_size_t len;
418         int i;
419
420         for (i = 0; i < PCI_BAR_COUNT; i++) {
421                 len = pci_resource_len(pdev, i);
422                 if (!len)
423                         continue;
424
425                 if (zpci_use_mio(zdev))
426                         pdev->resource[i].start =
427                                 (resource_size_t __force) zdev->bars[i].mio_wb;
428                 else
429                         pdev->resource[i].start = (resource_size_t __force)
430                                 pci_iomap_range_fh(pdev, i, 0, 0);
431                 pdev->resource[i].end = pdev->resource[i].start + len - 1;
432         }
433
434 #ifdef CONFIG_PCI_IOV
435         for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
436                 int bar = i + PCI_IOV_RESOURCES;
437
438                 len = pci_resource_len(pdev, bar);
439                 if (!len)
440                         continue;
441                 pdev->resource[bar].parent = &iov_res;
442         }
443 #endif
444 }
445
446 static void zpci_unmap_resources(struct pci_dev *pdev)
447 {
448         struct zpci_dev *zdev = to_zpci(pdev);
449         resource_size_t len;
450         int i;
451
452         if (zpci_use_mio(zdev))
453                 return;
454
455         for (i = 0; i < PCI_BAR_COUNT; i++) {
456                 len = pci_resource_len(pdev, i);
457                 if (!len)
458                         continue;
459                 pci_iounmap_fh(pdev, (void __iomem __force *)
460                                pdev->resource[i].start);
461         }
462 }
463
464 static int zpci_alloc_iomap(struct zpci_dev *zdev)
465 {
466         unsigned long entry;
467
468         spin_lock(&zpci_iomap_lock);
469         entry = find_first_zero_bit(zpci_iomap_bitmap, ZPCI_IOMAP_ENTRIES);
470         if (entry == ZPCI_IOMAP_ENTRIES) {
471                 spin_unlock(&zpci_iomap_lock);
472                 return -ENOSPC;
473         }
474         set_bit(entry, zpci_iomap_bitmap);
475         spin_unlock(&zpci_iomap_lock);
476         return entry;
477 }
478
479 static void zpci_free_iomap(struct zpci_dev *zdev, int entry)
480 {
481         spin_lock(&zpci_iomap_lock);
482         memset(&zpci_iomap_start[entry], 0, sizeof(struct zpci_iomap_entry));
483         clear_bit(entry, zpci_iomap_bitmap);
484         spin_unlock(&zpci_iomap_lock);
485 }
486
487 static struct resource *__alloc_res(struct zpci_dev *zdev, unsigned long start,
488                                     unsigned long size, unsigned long flags)
489 {
490         struct resource *r;
491
492         r = kzalloc(sizeof(*r), GFP_KERNEL);
493         if (!r)
494                 return NULL;
495
496         r->start = start;
497         r->end = r->start + size - 1;
498         r->flags = flags;
499         r->name = zdev->res_name;
500
501         if (request_resource(&iomem_resource, r)) {
502                 kfree(r);
503                 return NULL;
504         }
505         return r;
506 }
507
508 static int zpci_setup_bus_resources(struct zpci_dev *zdev,
509                                     struct list_head *resources)
510 {
511         unsigned long addr, size, flags;
512         struct resource *res;
513         int i, entry;
514
515         snprintf(zdev->res_name, sizeof(zdev->res_name),
516                  "PCI Bus %04x:%02x", zdev->domain, ZPCI_BUS_NR);
517
518         for (i = 0; i < PCI_BAR_COUNT; i++) {
519                 if (!zdev->bars[i].size)
520                         continue;
521                 entry = zpci_alloc_iomap(zdev);
522                 if (entry < 0)
523                         return entry;
524                 zdev->bars[i].map_idx = entry;
525
526                 /* only MMIO is supported */
527                 flags = IORESOURCE_MEM;
528                 if (zdev->bars[i].val & 8)
529                         flags |= IORESOURCE_PREFETCH;
530                 if (zdev->bars[i].val & 4)
531                         flags |= IORESOURCE_MEM_64;
532
533                 if (zpci_use_mio(zdev))
534                         addr = (unsigned long) zdev->bars[i].mio_wb;
535                 else
536                         addr = ZPCI_ADDR(entry);
537                 size = 1UL << zdev->bars[i].size;
538
539                 res = __alloc_res(zdev, addr, size, flags);
540                 if (!res) {
541                         zpci_free_iomap(zdev, entry);
542                         return -ENOMEM;
543                 }
544                 zdev->bars[i].res = res;
545                 pci_add_resource(resources, res);
546         }
547
548         return 0;
549 }
550
551 static void zpci_cleanup_bus_resources(struct zpci_dev *zdev)
552 {
553         int i;
554
555         for (i = 0; i < PCI_BAR_COUNT; i++) {
556                 if (!zdev->bars[i].size || !zdev->bars[i].res)
557                         continue;
558
559                 zpci_free_iomap(zdev, zdev->bars[i].map_idx);
560                 release_resource(zdev->bars[i].res);
561                 kfree(zdev->bars[i].res);
562         }
563 }
564
565 int pcibios_add_device(struct pci_dev *pdev)
566 {
567         struct resource *res;
568         int i;
569
570         if (pdev->is_physfn)
571                 pdev->no_vf_scan = 1;
572
573         pdev->dev.groups = zpci_attr_groups;
574         pdev->dev.dma_ops = &s390_pci_dma_ops;
575         zpci_map_resources(pdev);
576
577         for (i = 0; i < PCI_BAR_COUNT; i++) {
578                 res = &pdev->resource[i];
579                 if (res->parent || !res->flags)
580                         continue;
581                 pci_claim_resource(pdev, i);
582         }
583
584         return 0;
585 }
586
587 void pcibios_release_device(struct pci_dev *pdev)
588 {
589         zpci_unmap_resources(pdev);
590 }
591
592 int pcibios_enable_device(struct pci_dev *pdev, int mask)
593 {
594         struct zpci_dev *zdev = to_zpci(pdev);
595
596         zpci_debug_init_device(zdev, dev_name(&pdev->dev));
597         zpci_fmb_enable_device(zdev);
598
599         return pci_enable_resources(pdev, mask);
600 }
601
602 void pcibios_disable_device(struct pci_dev *pdev)
603 {
604         struct zpci_dev *zdev = to_zpci(pdev);
605
606         zpci_fmb_disable_device(zdev);
607         zpci_debug_exit_device(zdev);
608 }
609
610 #ifdef CONFIG_HIBERNATE_CALLBACKS
611 static int zpci_restore(struct device *dev)
612 {
613         struct pci_dev *pdev = to_pci_dev(dev);
614         struct zpci_dev *zdev = to_zpci(pdev);
615         int ret = 0;
616
617         if (zdev->state != ZPCI_FN_STATE_ONLINE)
618                 goto out;
619
620         ret = clp_enable_fh(zdev, ZPCI_NR_DMA_SPACES);
621         if (ret)
622                 goto out;
623
624         zpci_map_resources(pdev);
625         zpci_register_ioat(zdev, 0, zdev->start_dma, zdev->end_dma,
626                            (u64) zdev->dma_table);
627
628 out:
629         return ret;
630 }
631
632 static int zpci_freeze(struct device *dev)
633 {
634         struct pci_dev *pdev = to_pci_dev(dev);
635         struct zpci_dev *zdev = to_zpci(pdev);
636
637         if (zdev->state != ZPCI_FN_STATE_ONLINE)
638                 return 0;
639
640         zpci_unregister_ioat(zdev, 0);
641         zpci_unmap_resources(pdev);
642         return clp_disable_fh(zdev);
643 }
644
645 struct dev_pm_ops pcibios_pm_ops = {
646         .thaw_noirq = zpci_restore,
647         .freeze_noirq = zpci_freeze,
648         .restore_noirq = zpci_restore,
649         .poweroff_noirq = zpci_freeze,
650 };
651 #endif /* CONFIG_HIBERNATE_CALLBACKS */
652
653 static int zpci_alloc_domain(struct zpci_dev *zdev)
654 {
655         if (zpci_unique_uid) {
656                 zdev->domain = (u16) zdev->uid;
657                 if (zdev->domain >= ZPCI_NR_DEVICES)
658                         return 0;
659
660                 spin_lock(&zpci_domain_lock);
661                 if (test_bit(zdev->domain, zpci_domain)) {
662                         spin_unlock(&zpci_domain_lock);
663                         pr_err("Adding PCI function %08x failed because domain %04x is already assigned\n",
664                                 zdev->fid, zdev->domain);
665                         return -EEXIST;
666                 }
667                 set_bit(zdev->domain, zpci_domain);
668                 spin_unlock(&zpci_domain_lock);
669                 return 0;
670         }
671
672         spin_lock(&zpci_domain_lock);
673         zdev->domain = find_first_zero_bit(zpci_domain, ZPCI_NR_DEVICES);
674         if (zdev->domain == ZPCI_NR_DEVICES) {
675                 spin_unlock(&zpci_domain_lock);
676                 return -ENOSPC;
677         }
678         set_bit(zdev->domain, zpci_domain);
679         spin_unlock(&zpci_domain_lock);
680         return 0;
681 }
682
683 static void zpci_free_domain(struct zpci_dev *zdev)
684 {
685         if (zdev->domain >= ZPCI_NR_DEVICES)
686                 return;
687
688         spin_lock(&zpci_domain_lock);
689         clear_bit(zdev->domain, zpci_domain);
690         spin_unlock(&zpci_domain_lock);
691 }
692
693 void pcibios_remove_bus(struct pci_bus *bus)
694 {
695         struct zpci_dev *zdev = get_zdev_by_bus(bus);
696
697         zpci_exit_slot(zdev);
698         zpci_cleanup_bus_resources(zdev);
699         zpci_destroy_iommu(zdev);
700         zpci_free_domain(zdev);
701
702         spin_lock(&zpci_list_lock);
703         list_del(&zdev->entry);
704         spin_unlock(&zpci_list_lock);
705
706         zpci_dbg(3, "rem fid:%x\n", zdev->fid);
707         kfree(zdev);
708 }
709
710 static int zpci_scan_bus(struct zpci_dev *zdev)
711 {
712         LIST_HEAD(resources);
713         int ret;
714
715         ret = zpci_setup_bus_resources(zdev, &resources);
716         if (ret)
717                 goto error;
718
719         zdev->bus = pci_scan_root_bus(NULL, ZPCI_BUS_NR, &pci_root_ops,
720                                       zdev, &resources);
721         if (!zdev->bus) {
722                 ret = -EIO;
723                 goto error;
724         }
725         zdev->bus->max_bus_speed = zdev->max_bus_speed;
726         pci_bus_add_devices(zdev->bus);
727         return 0;
728
729 error:
730         zpci_cleanup_bus_resources(zdev);
731         pci_free_resource_list(&resources);
732         return ret;
733 }
734
735 int zpci_enable_device(struct zpci_dev *zdev)
736 {
737         int rc;
738
739         rc = clp_enable_fh(zdev, ZPCI_NR_DMA_SPACES);
740         if (rc)
741                 goto out;
742
743         rc = zpci_dma_init_device(zdev);
744         if (rc)
745                 goto out_dma;
746
747         zdev->state = ZPCI_FN_STATE_ONLINE;
748         return 0;
749
750 out_dma:
751         clp_disable_fh(zdev);
752 out:
753         return rc;
754 }
755 EXPORT_SYMBOL_GPL(zpci_enable_device);
756
757 int zpci_disable_device(struct zpci_dev *zdev)
758 {
759         zpci_dma_exit_device(zdev);
760         return clp_disable_fh(zdev);
761 }
762 EXPORT_SYMBOL_GPL(zpci_disable_device);
763
764 int zpci_create_device(struct zpci_dev *zdev)
765 {
766         int rc;
767
768         rc = zpci_alloc_domain(zdev);
769         if (rc)
770                 goto out;
771
772         rc = zpci_init_iommu(zdev);
773         if (rc)
774                 goto out_free;
775
776         mutex_init(&zdev->lock);
777         if (zdev->state == ZPCI_FN_STATE_CONFIGURED) {
778                 rc = zpci_enable_device(zdev);
779                 if (rc)
780                         goto out_destroy_iommu;
781         }
782         rc = zpci_scan_bus(zdev);
783         if (rc)
784                 goto out_disable;
785
786         spin_lock(&zpci_list_lock);
787         list_add_tail(&zdev->entry, &zpci_list);
788         spin_unlock(&zpci_list_lock);
789
790         zpci_init_slot(zdev);
791
792         return 0;
793
794 out_disable:
795         if (zdev->state == ZPCI_FN_STATE_ONLINE)
796                 zpci_disable_device(zdev);
797 out_destroy_iommu:
798         zpci_destroy_iommu(zdev);
799 out_free:
800         zpci_free_domain(zdev);
801 out:
802         return rc;
803 }
804
805 void zpci_remove_device(struct zpci_dev *zdev)
806 {
807         if (!zdev->bus)
808                 return;
809
810         pci_stop_root_bus(zdev->bus);
811         pci_remove_root_bus(zdev->bus);
812 }
813
814 int zpci_report_error(struct pci_dev *pdev,
815                       struct zpci_report_error_header *report)
816 {
817         struct zpci_dev *zdev = to_zpci(pdev);
818
819         return sclp_pci_report(report, zdev->fh, zdev->fid);
820 }
821 EXPORT_SYMBOL(zpci_report_error);
822
823 static int zpci_mem_init(void)
824 {
825         BUILD_BUG_ON(!is_power_of_2(__alignof__(struct zpci_fmb)) ||
826                      __alignof__(struct zpci_fmb) < sizeof(struct zpci_fmb));
827
828         zdev_fmb_cache = kmem_cache_create("PCI_FMB_cache", sizeof(struct zpci_fmb),
829                                            __alignof__(struct zpci_fmb), 0, NULL);
830         if (!zdev_fmb_cache)
831                 goto error_fmb;
832
833         zpci_iomap_start = kcalloc(ZPCI_IOMAP_ENTRIES,
834                                    sizeof(*zpci_iomap_start), GFP_KERNEL);
835         if (!zpci_iomap_start)
836                 goto error_iomap;
837
838         zpci_iomap_bitmap = kcalloc(BITS_TO_LONGS(ZPCI_IOMAP_ENTRIES),
839                                     sizeof(*zpci_iomap_bitmap), GFP_KERNEL);
840         if (!zpci_iomap_bitmap)
841                 goto error_iomap_bitmap;
842
843         return 0;
844 error_iomap_bitmap:
845         kfree(zpci_iomap_start);
846 error_iomap:
847         kmem_cache_destroy(zdev_fmb_cache);
848 error_fmb:
849         return -ENOMEM;
850 }
851
852 static void zpci_mem_exit(void)
853 {
854         kfree(zpci_iomap_bitmap);
855         kfree(zpci_iomap_start);
856         kmem_cache_destroy(zdev_fmb_cache);
857 }
858
859 static unsigned int s390_pci_probe __initdata = 1;
860 static unsigned int s390_pci_no_mio __initdata;
861 unsigned int s390_pci_force_floating __initdata;
862 static unsigned int s390_pci_initialized;
863
864 char * __init pcibios_setup(char *str)
865 {
866         if (!strcmp(str, "off")) {
867                 s390_pci_probe = 0;
868                 return NULL;
869         }
870         if (!strcmp(str, "nomio")) {
871                 s390_pci_no_mio = 1;
872                 return NULL;
873         }
874         if (!strcmp(str, "force_floating")) {
875                 s390_pci_force_floating = 1;
876                 return NULL;
877         }
878         return str;
879 }
880
881 bool zpci_is_enabled(void)
882 {
883         return s390_pci_initialized;
884 }
885
886 static int __init pci_base_init(void)
887 {
888         int rc;
889
890         if (!s390_pci_probe)
891                 return 0;
892
893         if (!test_facility(69) || !test_facility(71))
894                 return 0;
895
896         if (test_facility(153) && !s390_pci_no_mio) {
897                 static_branch_enable(&have_mio);
898                 ctl_set_bit(2, 5);
899         }
900
901         rc = zpci_debug_init();
902         if (rc)
903                 goto out;
904
905         rc = zpci_mem_init();
906         if (rc)
907                 goto out_mem;
908
909         rc = zpci_irq_init();
910         if (rc)
911                 goto out_irq;
912
913         rc = zpci_dma_init();
914         if (rc)
915                 goto out_dma;
916
917         rc = clp_scan_pci_devices();
918         if (rc)
919                 goto out_find;
920
921         s390_pci_initialized = 1;
922         return 0;
923
924 out_find:
925         zpci_dma_exit();
926 out_dma:
927         zpci_irq_exit();
928 out_irq:
929         zpci_mem_exit();
930 out_mem:
931         zpci_debug_exit();
932 out:
933         return rc;
934 }
935 subsys_initcall_sync(pci_base_init);
936
937 void zpci_rescan(void)
938 {
939         if (zpci_is_enabled())
940                 clp_rescan_pci_devices_simple();
941 }