s390/pci: remove per device debug attribute
[firefly-linux-kernel-4.4.55.git] / arch / s390 / pci / pci.c
1 /*
2  * Copyright IBM Corp. 2012
3  *
4  * Author(s):
5  *   Jan Glauber <jang@linux.vnet.ibm.com>
6  *
7  * The System z PCI code is a rewrite from a prototype by
8  * the following people (Kudoz!):
9  *   Alexander Schmidt
10  *   Christoph Raisch
11  *   Hannes Hering
12  *   Hoang-Nam Nguyen
13  *   Jan-Bernd Themann
14  *   Stefan Roscher
15  *   Thomas Klein
16  */
17
18 #define COMPONENT "zPCI"
19 #define pr_fmt(fmt) COMPONENT ": " 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/irq.h>
27 #include <linux/kernel_stat.h>
28 #include <linux/seq_file.h>
29 #include <linux/pci.h>
30 #include <linux/msi.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 #define DEBUG                           /* enable pr_debug */
40
41 #define SIC_IRQ_MODE_ALL                0
42 #define SIC_IRQ_MODE_SINGLE             1
43
44 #define ZPCI_NR_DMA_SPACES              1
45 #define ZPCI_MSI_VEC_BITS               6
46 #define ZPCI_NR_DEVICES                 CONFIG_PCI_NR_FUNCTIONS
47
48 /* list of all detected zpci devices */
49 LIST_HEAD(zpci_list);
50 EXPORT_SYMBOL_GPL(zpci_list);
51 DEFINE_MUTEX(zpci_list_lock);
52 EXPORT_SYMBOL_GPL(zpci_list_lock);
53
54 static struct pci_hp_callback_ops *hotplug_ops;
55
56 static DECLARE_BITMAP(zpci_domain, ZPCI_NR_DEVICES);
57 static DEFINE_SPINLOCK(zpci_domain_lock);
58
59 struct callback {
60         irq_handler_t   handler;
61         void            *data;
62 };
63
64 struct zdev_irq_map {
65         unsigned long   aibv;           /* AI bit vector */
66         int             msi_vecs;       /* consecutive MSI-vectors used */
67         int             __unused;
68         struct callback cb[ZPCI_NR_MSI_VECS]; /* callback handler array */
69         spinlock_t      lock;           /* protect callbacks against de-reg */
70 };
71
72 struct intr_bucket {
73         /* amap of adapters, one bit per dev, corresponds to one irq nr */
74         unsigned long   *alloc;
75         /* AI summary bit, global page for all devices */
76         unsigned long   *aisb;
77         /* pointer to aibv and callback data in zdev */
78         struct zdev_irq_map *imap[ZPCI_NR_DEVICES];
79         /* protects the whole bucket struct */
80         spinlock_t      lock;
81 };
82
83 static struct intr_bucket *bucket;
84
85 /* Adapter local summary indicator */
86 static u8 *zpci_irq_si;
87
88 /* I/O Map */
89 static DEFINE_SPINLOCK(zpci_iomap_lock);
90 static DECLARE_BITMAP(zpci_iomap, ZPCI_IOMAP_MAX_ENTRIES);
91 struct zpci_iomap_entry *zpci_iomap_start;
92 EXPORT_SYMBOL_GPL(zpci_iomap_start);
93
94 /* highest irq summary bit */
95 static int __read_mostly aisb_max;
96
97 static struct kmem_cache *zdev_irq_cache;
98 static struct kmem_cache *zdev_fmb_cache;
99
100 static inline int irq_to_msi_nr(unsigned int irq)
101 {
102         return irq & ZPCI_MSI_MASK;
103 }
104
105 static inline int irq_to_dev_nr(unsigned int irq)
106 {
107         return irq >> ZPCI_MSI_VEC_BITS;
108 }
109
110 static inline struct zdev_irq_map *get_imap(unsigned int irq)
111 {
112         return bucket->imap[irq_to_dev_nr(irq)];
113 }
114
115 struct zpci_dev *get_zdev(struct pci_dev *pdev)
116 {
117         return (struct zpci_dev *) pdev->sysdata;
118 }
119
120 struct zpci_dev *get_zdev_by_fid(u32 fid)
121 {
122         struct zpci_dev *tmp, *zdev = NULL;
123
124         mutex_lock(&zpci_list_lock);
125         list_for_each_entry(tmp, &zpci_list, entry) {
126                 if (tmp->fid == fid) {
127                         zdev = tmp;
128                         break;
129                 }
130         }
131         mutex_unlock(&zpci_list_lock);
132         return zdev;
133 }
134
135 bool zpci_fid_present(u32 fid)
136 {
137         return (get_zdev_by_fid(fid) != NULL) ? true : false;
138 }
139
140 static struct zpci_dev *get_zdev_by_bus(struct pci_bus *bus)
141 {
142         return (bus && bus->sysdata) ? (struct zpci_dev *) bus->sysdata : NULL;
143 }
144
145 int pci_domain_nr(struct pci_bus *bus)
146 {
147         return ((struct zpci_dev *) bus->sysdata)->domain;
148 }
149 EXPORT_SYMBOL_GPL(pci_domain_nr);
150
151 int pci_proc_domain(struct pci_bus *bus)
152 {
153         return pci_domain_nr(bus);
154 }
155 EXPORT_SYMBOL_GPL(pci_proc_domain);
156
157 /* Modify PCI: Register adapter interruptions */
158 static int zpci_register_airq(struct zpci_dev *zdev, unsigned int aisb,
159                               u64 aibv)
160 {
161         u64 req = ZPCI_CREATE_REQ(zdev->fh, 0, ZPCI_MOD_FC_REG_INT);
162         struct zpci_fib *fib;
163         int rc;
164
165         fib = (void *) get_zeroed_page(GFP_KERNEL);
166         if (!fib)
167                 return -ENOMEM;
168
169         fib->isc = PCI_ISC;
170         fib->noi = zdev->irq_map->msi_vecs;
171         fib->sum = 1;           /* enable summary notifications */
172         fib->aibv = aibv;
173         fib->aibvo = 0;         /* every function has its own page */
174         fib->aisb = (u64) bucket->aisb + aisb / 8;
175         fib->aisbo = aisb & ZPCI_MSI_MASK;
176
177         rc = s390pci_mod_fc(req, fib);
178         pr_debug("%s mpcifc returned noi: %d\n", __func__, fib->noi);
179
180         free_page((unsigned long) fib);
181         return rc;
182 }
183
184 struct mod_pci_args {
185         u64 base;
186         u64 limit;
187         u64 iota;
188         u64 fmb_addr;
189 };
190
191 static int mod_pci(struct zpci_dev *zdev, int fn, u8 dmaas, struct mod_pci_args *args)
192 {
193         u64 req = ZPCI_CREATE_REQ(zdev->fh, dmaas, fn);
194         struct zpci_fib *fib;
195         int rc;
196
197         /* The FIB must be available even if it's not used */
198         fib = (void *) get_zeroed_page(GFP_KERNEL);
199         if (!fib)
200                 return -ENOMEM;
201
202         fib->pba = args->base;
203         fib->pal = args->limit;
204         fib->iota = args->iota;
205         fib->fmb_addr = args->fmb_addr;
206
207         rc = s390pci_mod_fc(req, fib);
208         free_page((unsigned long) fib);
209         return rc;
210 }
211
212 /* Modify PCI: Register I/O address translation parameters */
213 int zpci_register_ioat(struct zpci_dev *zdev, u8 dmaas,
214                        u64 base, u64 limit, u64 iota)
215 {
216         struct mod_pci_args args = { base, limit, iota, 0 };
217
218         WARN_ON_ONCE(iota & 0x3fff);
219         args.iota |= ZPCI_IOTA_RTTO_FLAG;
220         return mod_pci(zdev, ZPCI_MOD_FC_REG_IOAT, dmaas, &args);
221 }
222
223 /* Modify PCI: Unregister I/O address translation parameters */
224 int zpci_unregister_ioat(struct zpci_dev *zdev, u8 dmaas)
225 {
226         struct mod_pci_args args = { 0, 0, 0, 0 };
227
228         return mod_pci(zdev, ZPCI_MOD_FC_DEREG_IOAT, dmaas, &args);
229 }
230
231 /* Modify PCI: Unregister adapter interruptions */
232 static int zpci_unregister_airq(struct zpci_dev *zdev)
233 {
234         struct mod_pci_args args = { 0, 0, 0, 0 };
235
236         return mod_pci(zdev, ZPCI_MOD_FC_DEREG_INT, 0, &args);
237 }
238
239 /* Modify PCI: Set PCI function measurement parameters */
240 int zpci_fmb_enable_device(struct zpci_dev *zdev)
241 {
242         struct mod_pci_args args = { 0, 0, 0, 0 };
243
244         if (zdev->fmb)
245                 return -EINVAL;
246
247         zdev->fmb = kmem_cache_zalloc(zdev_fmb_cache, GFP_KERNEL);
248         if (!zdev->fmb)
249                 return -ENOMEM;
250         WARN_ON((u64) zdev->fmb & 0xf);
251
252         args.fmb_addr = virt_to_phys(zdev->fmb);
253         return mod_pci(zdev, ZPCI_MOD_FC_SET_MEASURE, 0, &args);
254 }
255
256 /* Modify PCI: Disable PCI function measurement */
257 int zpci_fmb_disable_device(struct zpci_dev *zdev)
258 {
259         struct mod_pci_args args = { 0, 0, 0, 0 };
260         int rc;
261
262         if (!zdev->fmb)
263                 return -EINVAL;
264
265         /* Function measurement is disabled if fmb address is zero */
266         rc = mod_pci(zdev, ZPCI_MOD_FC_SET_MEASURE, 0, &args);
267
268         kmem_cache_free(zdev_fmb_cache, zdev->fmb);
269         zdev->fmb = NULL;
270         return rc;
271 }
272
273 #define ZPCI_PCIAS_CFGSPC       15
274
275 static int zpci_cfg_load(struct zpci_dev *zdev, int offset, u32 *val, u8 len)
276 {
277         u64 req = ZPCI_CREATE_REQ(zdev->fh, ZPCI_PCIAS_CFGSPC, len);
278         u64 data;
279         int rc;
280
281         rc = s390pci_load(&data, req, offset);
282         if (!rc) {
283                 data = data << ((8 - len) * 8);
284                 data = le64_to_cpu(data);
285                 *val = (u32) data;
286         } else
287                 *val = 0xffffffff;
288         return rc;
289 }
290
291 static int zpci_cfg_store(struct zpci_dev *zdev, int offset, u32 val, u8 len)
292 {
293         u64 req = ZPCI_CREATE_REQ(zdev->fh, ZPCI_PCIAS_CFGSPC, len);
294         u64 data = val;
295         int rc;
296
297         data = cpu_to_le64(data);
298         data = data >> ((8 - len) * 8);
299         rc = s390pci_store(data, req, offset);
300         return rc;
301 }
302
303 void enable_irq(unsigned int irq)
304 {
305         struct msi_desc *msi = irq_get_msi_desc(irq);
306
307         zpci_msi_set_mask_bits(msi, 1, 0);
308 }
309 EXPORT_SYMBOL_GPL(enable_irq);
310
311 void disable_irq(unsigned int irq)
312 {
313         struct msi_desc *msi = irq_get_msi_desc(irq);
314
315         zpci_msi_set_mask_bits(msi, 1, 1);
316 }
317 EXPORT_SYMBOL_GPL(disable_irq);
318
319 void pcibios_fixup_bus(struct pci_bus *bus)
320 {
321 }
322
323 resource_size_t pcibios_align_resource(void *data, const struct resource *res,
324                                        resource_size_t size,
325                                        resource_size_t align)
326 {
327         return 0;
328 }
329
330 /* combine single writes by using store-block insn */
331 void __iowrite64_copy(void __iomem *to, const void *from, size_t count)
332 {
333        zpci_memcpy_toio(to, from, count);
334 }
335
336 /* Create a virtual mapping cookie for a PCI BAR */
337 void __iomem *pci_iomap(struct pci_dev *pdev, int bar, unsigned long max)
338 {
339         struct zpci_dev *zdev = get_zdev(pdev);
340         u64 addr;
341         int idx;
342
343         if ((bar & 7) != bar)
344                 return NULL;
345
346         idx = zdev->bars[bar].map_idx;
347         spin_lock(&zpci_iomap_lock);
348         zpci_iomap_start[idx].fh = zdev->fh;
349         zpci_iomap_start[idx].bar = bar;
350         spin_unlock(&zpci_iomap_lock);
351
352         addr = ZPCI_IOMAP_ADDR_BASE | ((u64) idx << 48);
353         return (void __iomem *) addr;
354 }
355 EXPORT_SYMBOL_GPL(pci_iomap);
356
357 void pci_iounmap(struct pci_dev *pdev, void __iomem *addr)
358 {
359         unsigned int idx;
360
361         idx = (((__force u64) addr) & ~ZPCI_IOMAP_ADDR_BASE) >> 48;
362         spin_lock(&zpci_iomap_lock);
363         zpci_iomap_start[idx].fh = 0;
364         zpci_iomap_start[idx].bar = 0;
365         spin_unlock(&zpci_iomap_lock);
366 }
367 EXPORT_SYMBOL_GPL(pci_iounmap);
368
369 static int pci_read(struct pci_bus *bus, unsigned int devfn, int where,
370                     int size, u32 *val)
371 {
372         struct zpci_dev *zdev = get_zdev_by_bus(bus);
373         int ret;
374
375         if (!zdev || devfn != ZPCI_DEVFN)
376                 ret = -ENODEV;
377         else
378                 ret = zpci_cfg_load(zdev, where, val, size);
379
380         return ret;
381 }
382
383 static int pci_write(struct pci_bus *bus, unsigned int devfn, int where,
384                      int size, u32 val)
385 {
386         struct zpci_dev *zdev = get_zdev_by_bus(bus);
387         int ret;
388
389         if (!zdev || devfn != ZPCI_DEVFN)
390                 ret = -ENODEV;
391         else
392                 ret = zpci_cfg_store(zdev, where, val, size);
393
394         return ret;
395 }
396
397 static struct pci_ops pci_root_ops = {
398         .read = pci_read,
399         .write = pci_write,
400 };
401
402 /* store the last handled bit to implement fair scheduling of devices */
403 static DEFINE_PER_CPU(unsigned long, next_sbit);
404
405 static void zpci_irq_handler(void *dont, void *need)
406 {
407         unsigned long sbit, mbit, last = 0, start = __get_cpu_var(next_sbit);
408         int rescan = 0, max = aisb_max;
409         struct zdev_irq_map *imap;
410
411         inc_irq_stat(IRQIO_PCI);
412         sbit = start;
413
414 scan:
415         /* find summary_bit */
416         for_each_set_bit_left_cont(sbit, bucket->aisb, max) {
417                 clear_bit(63 - (sbit & 63), bucket->aisb + (sbit >> 6));
418                 last = sbit;
419
420                 /* find vector bit */
421                 imap = bucket->imap[sbit];
422                 for_each_set_bit_left(mbit, &imap->aibv, imap->msi_vecs) {
423                         inc_irq_stat(IRQIO_MSI);
424                         clear_bit(63 - mbit, &imap->aibv);
425
426                         spin_lock(&imap->lock);
427                         if (imap->cb[mbit].handler)
428                                 imap->cb[mbit].handler(mbit,
429                                         imap->cb[mbit].data);
430                         spin_unlock(&imap->lock);
431                 }
432         }
433
434         if (rescan)
435                 goto out;
436
437         /* scan the skipped bits */
438         if (start > 0) {
439                 sbit = 0;
440                 max = start;
441                 start = 0;
442                 goto scan;
443         }
444
445         /* enable interrupts again */
446         set_irq_ctrl(SIC_IRQ_MODE_SINGLE, NULL, PCI_ISC);
447
448         /* check again to not lose initiative */
449         rmb();
450         max = aisb_max;
451         sbit = find_first_bit_left(bucket->aisb, max);
452         if (sbit != max) {
453                 rescan++;
454                 goto scan;
455         }
456 out:
457         /* store next device bit to scan */
458         __get_cpu_var(next_sbit) = (++last >= aisb_max) ? 0 : last;
459 }
460
461 /* msi_vecs - number of requested interrupts, 0 place function to error state */
462 static int zpci_setup_msi(struct pci_dev *pdev, int msi_vecs)
463 {
464         struct zpci_dev *zdev = get_zdev(pdev);
465         unsigned int aisb, msi_nr;
466         struct msi_desc *msi;
467         int rc;
468
469         /* store the number of used MSI vectors */
470         zdev->irq_map->msi_vecs = min(msi_vecs, ZPCI_NR_MSI_VECS);
471
472         spin_lock(&bucket->lock);
473         aisb = find_first_zero_bit(bucket->alloc, PAGE_SIZE);
474         /* alloc map exhausted? */
475         if (aisb == PAGE_SIZE) {
476                 spin_unlock(&bucket->lock);
477                 return -EIO;
478         }
479         set_bit(aisb, bucket->alloc);
480         spin_unlock(&bucket->lock);
481
482         zdev->aisb = aisb;
483         if (aisb + 1 > aisb_max)
484                 aisb_max = aisb + 1;
485
486         /* wire up IRQ shortcut pointer */
487         bucket->imap[zdev->aisb] = zdev->irq_map;
488         pr_debug("%s: imap[%u] linked to %p\n", __func__, zdev->aisb, zdev->irq_map);
489
490         /* TODO: irq number 0 wont be found if we return less than requested MSIs.
491          * ignore it for now and fix in common code.
492          */
493         msi_nr = aisb << ZPCI_MSI_VEC_BITS;
494
495         list_for_each_entry(msi, &pdev->msi_list, list) {
496                 rc = zpci_setup_msi_irq(zdev, msi, msi_nr,
497                                           aisb << ZPCI_MSI_VEC_BITS);
498                 if (rc)
499                         return rc;
500                 msi_nr++;
501         }
502
503         rc = zpci_register_airq(zdev, aisb, (u64) &zdev->irq_map->aibv);
504         if (rc) {
505                 clear_bit(aisb, bucket->alloc);
506                 dev_err(&pdev->dev, "register MSI failed with: %d\n", rc);
507                 return rc;
508         }
509         return (zdev->irq_map->msi_vecs == msi_vecs) ?
510                 0 : zdev->irq_map->msi_vecs;
511 }
512
513 static void zpci_teardown_msi(struct pci_dev *pdev)
514 {
515         struct zpci_dev *zdev = get_zdev(pdev);
516         struct msi_desc *msi;
517         int aisb, rc;
518
519         rc = zpci_unregister_airq(zdev);
520         if (rc) {
521                 dev_err(&pdev->dev, "deregister MSI failed with: %d\n", rc);
522                 return;
523         }
524
525         msi = list_first_entry(&pdev->msi_list, struct msi_desc, list);
526         aisb = irq_to_dev_nr(msi->irq);
527
528         list_for_each_entry(msi, &pdev->msi_list, list)
529                 zpci_teardown_msi_irq(zdev, msi);
530
531         clear_bit(aisb, bucket->alloc);
532         if (aisb + 1 == aisb_max)
533                 aisb_max--;
534 }
535
536 int arch_setup_msi_irqs(struct pci_dev *pdev, int nvec, int type)
537 {
538         pr_debug("%s: requesting %d MSI-X interrupts...", __func__, nvec);
539         if (type != PCI_CAP_ID_MSIX && type != PCI_CAP_ID_MSI)
540                 return -EINVAL;
541         return zpci_setup_msi(pdev, nvec);
542 }
543
544 void arch_teardown_msi_irqs(struct pci_dev *pdev)
545 {
546         pr_info("%s: on pdev: %p\n", __func__, pdev);
547         zpci_teardown_msi(pdev);
548 }
549
550 static void zpci_map_resources(struct zpci_dev *zdev)
551 {
552         struct pci_dev *pdev = zdev->pdev;
553         resource_size_t len;
554         int i;
555
556         for (i = 0; i < PCI_BAR_COUNT; i++) {
557                 len = pci_resource_len(pdev, i);
558                 if (!len)
559                         continue;
560                 pdev->resource[i].start = (resource_size_t) pci_iomap(pdev, i, 0);
561                 pdev->resource[i].end = pdev->resource[i].start + len - 1;
562                 pr_debug("BAR%i: -> start: %Lx  end: %Lx\n",
563                         i, pdev->resource[i].start, pdev->resource[i].end);
564         }
565 }
566
567 static void zpci_unmap_resources(struct zpci_dev *zdev)
568 {
569         struct pci_dev *pdev = zdev->pdev;
570         resource_size_t len;
571         int i;
572
573         for (i = 0; i < PCI_BAR_COUNT; i++) {
574                 len = pci_resource_len(pdev, i);
575                 if (!len)
576                         continue;
577                 pci_iounmap(pdev, (void *) pdev->resource[i].start);
578         }
579 }
580
581 struct zpci_dev *zpci_alloc_device(void)
582 {
583         struct zpci_dev *zdev;
584
585         /* Alloc memory for our private pci device data */
586         zdev = kzalloc(sizeof(*zdev), GFP_KERNEL);
587         if (!zdev)
588                 return ERR_PTR(-ENOMEM);
589
590         /* Alloc aibv & callback space */
591         zdev->irq_map = kmem_cache_zalloc(zdev_irq_cache, GFP_KERNEL);
592         if (!zdev->irq_map)
593                 goto error;
594         WARN_ON((u64) zdev->irq_map & 0xff);
595         return zdev;
596
597 error:
598         kfree(zdev);
599         return ERR_PTR(-ENOMEM);
600 }
601
602 void zpci_free_device(struct zpci_dev *zdev)
603 {
604         kmem_cache_free(zdev_irq_cache, zdev->irq_map);
605         kfree(zdev);
606 }
607
608 /*
609  * Too late for any s390 specific setup, since interrupts must be set up
610  * already which requires DMA setup too and the pci scan will access the
611  * config space, which only works if the function handle is enabled.
612  */
613 int pcibios_enable_device(struct pci_dev *pdev, int mask)
614 {
615         struct resource *res;
616         u16 cmd;
617         int i;
618
619         pci_read_config_word(pdev, PCI_COMMAND, &cmd);
620
621         for (i = 0; i < PCI_BAR_COUNT; i++) {
622                 res = &pdev->resource[i];
623
624                 if (res->flags & IORESOURCE_IO)
625                         return -EINVAL;
626
627                 if (res->flags & IORESOURCE_MEM)
628                         cmd |= PCI_COMMAND_MEMORY;
629         }
630         pci_write_config_word(pdev, PCI_COMMAND, cmd);
631         return 0;
632 }
633
634 int pcibios_add_platform_entries(struct pci_dev *pdev)
635 {
636         return zpci_sysfs_add_device(&pdev->dev);
637 }
638
639 int zpci_request_irq(unsigned int irq, irq_handler_t handler, void *data)
640 {
641         int msi_nr = irq_to_msi_nr(irq);
642         struct zdev_irq_map *imap;
643         struct msi_desc *msi;
644
645         msi = irq_get_msi_desc(irq);
646         if (!msi)
647                 return -EIO;
648
649         imap = get_imap(irq);
650         spin_lock_init(&imap->lock);
651
652         pr_debug("%s: register handler for IRQ:MSI %d:%d\n", __func__, irq >> 6, msi_nr);
653         imap->cb[msi_nr].handler = handler;
654         imap->cb[msi_nr].data = data;
655
656         /*
657          * The generic MSI code returns with the interrupt disabled on the
658          * card, using the MSI mask bits. Firmware doesn't appear to unmask
659          * at that level, so we do it here by hand.
660          */
661         zpci_msi_set_mask_bits(msi, 1, 0);
662         return 0;
663 }
664
665 void zpci_free_irq(unsigned int irq)
666 {
667         struct zdev_irq_map *imap = get_imap(irq);
668         int msi_nr = irq_to_msi_nr(irq);
669         unsigned long flags;
670
671         pr_debug("%s: for irq: %d\n", __func__, irq);
672
673         spin_lock_irqsave(&imap->lock, flags);
674         imap->cb[msi_nr].handler = NULL;
675         imap->cb[msi_nr].data = NULL;
676         spin_unlock_irqrestore(&imap->lock, flags);
677 }
678
679 int request_irq(unsigned int irq, irq_handler_t handler,
680                 unsigned long irqflags, const char *devname, void *dev_id)
681 {
682         pr_debug("%s: irq: %d  handler: %p  flags: %lx  dev: %s\n",
683                 __func__, irq, handler, irqflags, devname);
684
685         return zpci_request_irq(irq, handler, dev_id);
686 }
687 EXPORT_SYMBOL_GPL(request_irq);
688
689 void free_irq(unsigned int irq, void *dev_id)
690 {
691         zpci_free_irq(irq);
692 }
693 EXPORT_SYMBOL_GPL(free_irq);
694
695 static int __init zpci_irq_init(void)
696 {
697         int cpu, rc;
698
699         bucket = kzalloc(sizeof(*bucket), GFP_KERNEL);
700         if (!bucket)
701                 return -ENOMEM;
702
703         bucket->aisb = (unsigned long *) get_zeroed_page(GFP_KERNEL);
704         if (!bucket->aisb) {
705                 rc = -ENOMEM;
706                 goto out_aisb;
707         }
708
709         bucket->alloc = (unsigned long *) get_zeroed_page(GFP_KERNEL);
710         if (!bucket->alloc) {
711                 rc = -ENOMEM;
712                 goto out_alloc;
713         }
714
715         isc_register(PCI_ISC);
716         zpci_irq_si = s390_register_adapter_interrupt(&zpci_irq_handler, NULL, PCI_ISC);
717         if (IS_ERR(zpci_irq_si)) {
718                 rc = PTR_ERR(zpci_irq_si);
719                 zpci_irq_si = NULL;
720                 goto out_ai;
721         }
722
723         for_each_online_cpu(cpu)
724                 per_cpu(next_sbit, cpu) = 0;
725
726         spin_lock_init(&bucket->lock);
727         /* set summary to 1 to be called every time for the ISC */
728         *zpci_irq_si = 1;
729         set_irq_ctrl(SIC_IRQ_MODE_SINGLE, NULL, PCI_ISC);
730         return 0;
731
732 out_ai:
733         isc_unregister(PCI_ISC);
734         free_page((unsigned long) bucket->alloc);
735 out_alloc:
736         free_page((unsigned long) bucket->aisb);
737 out_aisb:
738         kfree(bucket);
739         return rc;
740 }
741
742 static void zpci_irq_exit(void)
743 {
744         free_page((unsigned long) bucket->alloc);
745         free_page((unsigned long) bucket->aisb);
746         s390_unregister_adapter_interrupt(zpci_irq_si, PCI_ISC);
747         isc_unregister(PCI_ISC);
748         kfree(bucket);
749 }
750
751 static struct resource *zpci_alloc_bus_resource(unsigned long start, unsigned long size,
752                                                 unsigned long flags, int domain)
753 {
754         struct resource *r;
755         char *name;
756         int rc;
757
758         r = kzalloc(sizeof(*r), GFP_KERNEL);
759         if (!r)
760                 return ERR_PTR(-ENOMEM);
761         r->start = start;
762         r->end = r->start + size - 1;
763         r->flags = flags;
764         r->parent = &iomem_resource;
765         name = kmalloc(18, GFP_KERNEL);
766         if (!name) {
767                 kfree(r);
768                 return ERR_PTR(-ENOMEM);
769         }
770         sprintf(name, "PCI Bus: %04x:%02x", domain, ZPCI_BUS_NR);
771         r->name = name;
772
773         rc = request_resource(&iomem_resource, r);
774         if (rc)
775                 pr_debug("request resource %pR failed\n", r);
776         return r;
777 }
778
779 static int zpci_alloc_iomap(struct zpci_dev *zdev)
780 {
781         int entry;
782
783         spin_lock(&zpci_iomap_lock);
784         entry = find_first_zero_bit(zpci_iomap, ZPCI_IOMAP_MAX_ENTRIES);
785         if (entry == ZPCI_IOMAP_MAX_ENTRIES) {
786                 spin_unlock(&zpci_iomap_lock);
787                 return -ENOSPC;
788         }
789         set_bit(entry, zpci_iomap);
790         spin_unlock(&zpci_iomap_lock);
791         return entry;
792 }
793
794 static void zpci_free_iomap(struct zpci_dev *zdev, int entry)
795 {
796         spin_lock(&zpci_iomap_lock);
797         memset(&zpci_iomap_start[entry], 0, sizeof(struct zpci_iomap_entry));
798         clear_bit(entry, zpci_iomap);
799         spin_unlock(&zpci_iomap_lock);
800 }
801
802 int pcibios_add_device(struct pci_dev *pdev)
803 {
804         struct zpci_dev *zdev = get_zdev(pdev);
805
806         zdev->pdev = pdev;
807         zpci_debug_init_device(zdev);
808         zpci_fmb_enable_device(zdev);
809         zpci_map_resources(zdev);
810
811         return 0;
812 }
813
814 void pcibios_release_device(struct pci_dev *pdev)
815 {
816         struct zpci_dev *zdev = get_zdev(pdev);
817
818         zpci_unmap_resources(zdev);
819         zpci_fmb_disable_device(zdev);
820         zpci_debug_exit_device(zdev);
821         zdev->pdev = NULL;
822 }
823
824 static int zpci_scan_bus(struct zpci_dev *zdev)
825 {
826         struct resource *res;
827         LIST_HEAD(resources);
828         int i;
829
830         /* allocate mapping entry for each used bar */
831         for (i = 0; i < PCI_BAR_COUNT; i++) {
832                 unsigned long addr, size, flags;
833                 int entry;
834
835                 if (!zdev->bars[i].size)
836                         continue;
837                 entry = zpci_alloc_iomap(zdev);
838                 if (entry < 0)
839                         return entry;
840                 zdev->bars[i].map_idx = entry;
841
842                 /* only MMIO is supported */
843                 flags = IORESOURCE_MEM;
844                 if (zdev->bars[i].val & 8)
845                         flags |= IORESOURCE_PREFETCH;
846                 if (zdev->bars[i].val & 4)
847                         flags |= IORESOURCE_MEM_64;
848
849                 addr = ZPCI_IOMAP_ADDR_BASE + ((u64) entry << 48);
850
851                 size = 1UL << zdev->bars[i].size;
852
853                 res = zpci_alloc_bus_resource(addr, size, flags, zdev->domain);
854                 if (IS_ERR(res)) {
855                         zpci_free_iomap(zdev, entry);
856                         return PTR_ERR(res);
857                 }
858                 pci_add_resource(&resources, res);
859         }
860
861         zdev->bus = pci_scan_root_bus(NULL, ZPCI_BUS_NR, &pci_root_ops,
862                                       zdev, &resources);
863         if (!zdev->bus)
864                 return -EIO;
865
866         zdev->bus->max_bus_speed = zdev->max_bus_speed;
867         return 0;
868 }
869
870 static int zpci_alloc_domain(struct zpci_dev *zdev)
871 {
872         spin_lock(&zpci_domain_lock);
873         zdev->domain = find_first_zero_bit(zpci_domain, ZPCI_NR_DEVICES);
874         if (zdev->domain == ZPCI_NR_DEVICES) {
875                 spin_unlock(&zpci_domain_lock);
876                 return -ENOSPC;
877         }
878         set_bit(zdev->domain, zpci_domain);
879         spin_unlock(&zpci_domain_lock);
880         return 0;
881 }
882
883 static void zpci_free_domain(struct zpci_dev *zdev)
884 {
885         spin_lock(&zpci_domain_lock);
886         clear_bit(zdev->domain, zpci_domain);
887         spin_unlock(&zpci_domain_lock);
888 }
889
890 int zpci_enable_device(struct zpci_dev *zdev)
891 {
892         int rc;
893
894         rc = clp_enable_fh(zdev, ZPCI_NR_DMA_SPACES);
895         if (rc)
896                 goto out;
897         pr_info("Enabled fh: 0x%x fid: 0x%x\n", zdev->fh, zdev->fid);
898
899         rc = zpci_dma_init_device(zdev);
900         if (rc)
901                 goto out_dma;
902         return 0;
903
904 out_dma:
905         clp_disable_fh(zdev);
906 out:
907         return rc;
908 }
909 EXPORT_SYMBOL_GPL(zpci_enable_device);
910
911 int zpci_disable_device(struct zpci_dev *zdev)
912 {
913         zpci_dma_exit_device(zdev);
914         return clp_disable_fh(zdev);
915 }
916 EXPORT_SYMBOL_GPL(zpci_disable_device);
917
918 int zpci_create_device(struct zpci_dev *zdev)
919 {
920         int rc;
921
922         rc = zpci_alloc_domain(zdev);
923         if (rc)
924                 goto out;
925
926         if (zdev->state == ZPCI_FN_STATE_CONFIGURED) {
927                 rc = zpci_enable_device(zdev);
928                 if (rc)
929                         goto out_free;
930
931                 zdev->state = ZPCI_FN_STATE_ONLINE;
932         }
933         rc = zpci_scan_bus(zdev);
934         if (rc)
935                 goto out_disable;
936
937         mutex_lock(&zpci_list_lock);
938         list_add_tail(&zdev->entry, &zpci_list);
939         if (hotplug_ops)
940                 hotplug_ops->create_slot(zdev);
941         mutex_unlock(&zpci_list_lock);
942
943         return 0;
944
945 out_disable:
946         if (zdev->state == ZPCI_FN_STATE_ONLINE)
947                 zpci_disable_device(zdev);
948 out_free:
949         zpci_free_domain(zdev);
950 out:
951         return rc;
952 }
953
954 void zpci_stop_device(struct zpci_dev *zdev)
955 {
956         zpci_dma_exit_device(zdev);
957         /*
958          * Note: SCLP disables fh via set-pci-fn so don't
959          * do that here.
960          */
961 }
962 EXPORT_SYMBOL_GPL(zpci_stop_device);
963
964 static inline int barsize(u8 size)
965 {
966         return (size) ? (1 << size) >> 10 : 0;
967 }
968
969 static int zpci_mem_init(void)
970 {
971         zdev_irq_cache = kmem_cache_create("PCI_IRQ_cache", sizeof(struct zdev_irq_map),
972                                 L1_CACHE_BYTES, SLAB_HWCACHE_ALIGN, NULL);
973         if (!zdev_irq_cache)
974                 goto error_zdev;
975
976         zdev_fmb_cache = kmem_cache_create("PCI_FMB_cache", sizeof(struct zpci_fmb),
977                                 16, 0, NULL);
978         if (!zdev_fmb_cache)
979                 goto error_fmb;
980
981         /* TODO: use realloc */
982         zpci_iomap_start = kzalloc(ZPCI_IOMAP_MAX_ENTRIES * sizeof(*zpci_iomap_start),
983                                    GFP_KERNEL);
984         if (!zpci_iomap_start)
985                 goto error_iomap;
986         return 0;
987
988 error_iomap:
989         kmem_cache_destroy(zdev_fmb_cache);
990 error_fmb:
991         kmem_cache_destroy(zdev_irq_cache);
992 error_zdev:
993         return -ENOMEM;
994 }
995
996 static void zpci_mem_exit(void)
997 {
998         kfree(zpci_iomap_start);
999         kmem_cache_destroy(zdev_irq_cache);
1000         kmem_cache_destroy(zdev_fmb_cache);
1001 }
1002
1003 void zpci_register_hp_ops(struct pci_hp_callback_ops *ops)
1004 {
1005         mutex_lock(&zpci_list_lock);
1006         hotplug_ops = ops;
1007         mutex_unlock(&zpci_list_lock);
1008 }
1009 EXPORT_SYMBOL_GPL(zpci_register_hp_ops);
1010
1011 void zpci_deregister_hp_ops(void)
1012 {
1013         mutex_lock(&zpci_list_lock);
1014         hotplug_ops = NULL;
1015         mutex_unlock(&zpci_list_lock);
1016 }
1017 EXPORT_SYMBOL_GPL(zpci_deregister_hp_ops);
1018
1019 unsigned int s390_pci_probe;
1020 EXPORT_SYMBOL_GPL(s390_pci_probe);
1021
1022 char * __init pcibios_setup(char *str)
1023 {
1024         if (!strcmp(str, "on")) {
1025                 s390_pci_probe = 1;
1026                 return NULL;
1027         }
1028         return str;
1029 }
1030
1031 static int __init pci_base_init(void)
1032 {
1033         int rc;
1034
1035         if (!s390_pci_probe)
1036                 return 0;
1037
1038         if (!test_facility(2) || !test_facility(69)
1039             || !test_facility(71) || !test_facility(72))
1040                 return 0;
1041
1042         pr_info("Probing PCI hardware: PCI:%d  SID:%d  AEN:%d\n",
1043                 test_facility(69), test_facility(70),
1044                 test_facility(71));
1045
1046         rc = zpci_debug_init();
1047         if (rc)
1048                 return rc;
1049
1050         rc = zpci_mem_init();
1051         if (rc)
1052                 goto out_mem;
1053
1054         rc = zpci_msihash_init();
1055         if (rc)
1056                 goto out_hash;
1057
1058         rc = zpci_irq_init();
1059         if (rc)
1060                 goto out_irq;
1061
1062         rc = zpci_dma_init();
1063         if (rc)
1064                 goto out_dma;
1065
1066         rc = clp_find_pci_devices();
1067         if (rc)
1068                 goto out_find;
1069
1070         return 0;
1071
1072 out_find:
1073         zpci_dma_exit();
1074 out_dma:
1075         zpci_irq_exit();
1076 out_irq:
1077         zpci_msihash_exit();
1078 out_hash:
1079         zpci_mem_exit();
1080 out_mem:
1081         zpci_debug_exit();
1082         return rc;
1083 }
1084 subsys_initcall(pci_base_init);