blob: bc980fd313d5137a44f083b1253cd14bc189edc6 [file] [log] [blame]
Greg Kroah-Hartmanadbb3902017-11-24 15:00:36 +01001// SPDX-License-Identifier: GPL-2.0
Jan Glaubercd248342012-11-29 12:50:30 +01002/*
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!):
Jan Glauberbedef752012-12-06 14:06:28 +010010 * Alexander Schmidt
11 * Christoph Raisch
12 * Hannes Hering
13 * Hoang-Nam Nguyen
14 * Jan-Bernd Themann
15 * Stefan Roscher
16 * Thomas Klein
Jan Glaubercd248342012-11-29 12:50:30 +010017 */
18
Gerald Schaefer896cb7e2014-07-16 17:21:01 +020019#define KMSG_COMPONENT "zpci"
20#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
Jan Glaubercd248342012-11-29 12:50:30 +010021
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>
Sebastian Ott71ba41c2019-04-14 15:38:01 +020028#include <linux/jump_label.h>
Jan Glaubercd248342012-11-29 12:50:30 +010029#include <linux/pci.h>
Niklas Schnelle794b8842019-11-28 09:30:00 +010030#include <linux/printk.h>
Jan Glaubercd248342012-11-29 12:50:30 +010031
Jan Glauber9a4da8a2012-11-29 13:05:05 +010032#include <asm/isc.h>
33#include <asm/airq.h>
Jan Glaubercd248342012-11-29 12:50:30 +010034#include <asm/facility.h>
35#include <asm/pci_insn.h>
Jan Glaubera755a452012-11-29 12:55:21 +010036#include <asm/pci_clp.h>
Jan Glauber828b35f2012-11-29 14:33:30 +010037#include <asm/pci_dma.h>
Jan Glaubercd248342012-11-29 12:50:30 +010038
Pierre Morel05bc1be2020-03-23 10:45:43 +010039#include "pci_bus.h"
Niklas Schnelleabb95b72020-08-17 10:29:23 +020040#include "pci_iov.h"
Pierre Morel05bc1be2020-03-23 10:45:43 +010041
Jan Glaubercd248342012-11-29 12:50:30 +010042/* list of all detected zpci devices */
Sebastian Ott67f43f32013-08-29 19:33:16 +020043static LIST_HEAD(zpci_list);
Sebastian Ott57b59182013-08-29 19:40:01 +020044static DEFINE_SPINLOCK(zpci_list_lock);
Martin Schwidefsky1f44a2252013-06-27 09:01:09 +020045
Niklas Schnelle969ae012020-03-17 12:59:37 +010046static DECLARE_BITMAP(zpci_domain, ZPCI_DOMAIN_BITMAP_SIZE);
Jan Glaubercd248342012-11-29 12:50:30 +010047static DEFINE_SPINLOCK(zpci_domain_lock);
48
Sebastian Ottc506fff32016-01-22 14:01:44 +010049#define ZPCI_IOMAP_ENTRIES \
Denis Efremovc9c13ba2019-09-28 02:43:08 +030050 min(((unsigned long) ZPCI_NR_DEVICES * PCI_STD_NUM_BARS / 2), \
Sebastian Ottc506fff32016-01-22 14:01:44 +010051 ZPCI_IOMAP_MAX_ENTRIES)
52
Pierre Morel6cf17f92020-02-07 13:35:08 +010053unsigned int s390_pci_no_rid;
54
Jan Glaubercd248342012-11-29 12:50:30 +010055static DEFINE_SPINLOCK(zpci_iomap_lock);
Sebastian Ottc506fff32016-01-22 14:01:44 +010056static unsigned long *zpci_iomap_bitmap;
Jan Glaubercd248342012-11-29 12:50:30 +010057struct zpci_iomap_entry *zpci_iomap_start;
58EXPORT_SYMBOL_GPL(zpci_iomap_start);
59
Sebastian Ott71ba41c2019-04-14 15:38:01 +020060DEFINE_STATIC_KEY_FALSE(have_mio);
61
Jan Glauberd0b08852012-12-11 14:53:35 +010062static struct kmem_cache *zdev_fmb_cache;
63
Jan Glaubercd248342012-11-29 12:50:30 +010064struct zpci_dev *get_zdev_by_fid(u32 fid)
65{
66 struct zpci_dev *tmp, *zdev = NULL;
67
Sebastian Ott57b59182013-08-29 19:40:01 +020068 spin_lock(&zpci_list_lock);
Jan Glaubercd248342012-11-29 12:50:30 +010069 list_for_each_entry(tmp, &zpci_list, entry) {
70 if (tmp->fid == fid) {
71 zdev = tmp;
Niklas Schnellec1223832021-09-20 09:32:21 +020072 zpci_zdev_get(zdev);
Jan Glaubercd248342012-11-29 12:50:30 +010073 break;
74 }
75 }
Sebastian Ott57b59182013-08-29 19:40:01 +020076 spin_unlock(&zpci_list_lock);
Jan Glaubercd248342012-11-29 12:50:30 +010077 return zdev;
78}
79
Sebastian Ott01553d92017-06-20 15:56:05 +020080void zpci_remove_reserved_devices(void)
81{
82 struct zpci_dev *tmp, *zdev;
83 enum zpci_state state;
84 LIST_HEAD(remove);
85
86 spin_lock(&zpci_list_lock);
87 list_for_each_entry_safe(zdev, tmp, &zpci_list, entry) {
88 if (zdev->state == ZPCI_FN_STATE_STANDBY &&
89 !clp_get_state(zdev->fid, &state) &&
90 state == ZPCI_FN_STATE_RESERVED)
91 list_move_tail(&zdev->entry, &remove);
92 }
93 spin_unlock(&zpci_list_lock);
94
95 list_for_each_entry_safe(zdev, tmp, &remove, entry)
Niklas Schnellea46044a2021-09-22 15:55:12 +020096 zpci_device_reserved(zdev);
Jan Glaubercd248342012-11-29 12:50:30 +010097}
98
99int pci_domain_nr(struct pci_bus *bus)
100{
Pierre Morel05bc1be2020-03-23 10:45:43 +0100101 return ((struct zpci_bus *) bus->sysdata)->domain_nr;
Jan Glaubercd248342012-11-29 12:50:30 +0100102}
103EXPORT_SYMBOL_GPL(pci_domain_nr);
104
105int pci_proc_domain(struct pci_bus *bus)
106{
107 return pci_domain_nr(bus);
108}
109EXPORT_SYMBOL_GPL(pci_proc_domain);
110
Jan Glauber828b35f2012-11-29 14:33:30 +0100111/* Modify PCI: Register I/O address translation parameters */
112int zpci_register_ioat(struct zpci_dev *zdev, u8 dmaas,
113 u64 base, u64 limit, u64 iota)
114{
Sebastian Ott72570832017-06-10 14:10:00 +0200115 u64 req = ZPCI_CREATE_REQ(zdev->fh, dmaas, ZPCI_MOD_FC_REG_IOAT);
116 struct zpci_fib fib = {0};
Niklas Schnelle1f3f7682021-07-16 11:53:37 +0200117 u8 cc, status;
Jan Glauber828b35f2012-11-29 14:33:30 +0100118
119 WARN_ON_ONCE(iota & 0x3fff);
Sebastian Ott72570832017-06-10 14:10:00 +0200120 fib.pba = base;
121 fib.pal = limit;
122 fib.iota = iota | ZPCI_IOTA_RTTO_FLAG;
Niklas Schnelle1f3f7682021-07-16 11:53:37 +0200123 cc = zpci_mod_fc(req, &fib, &status);
124 if (cc)
125 zpci_dbg(3, "reg ioat fid:%x, cc:%d, status:%d\n", zdev->fid, cc, status);
126 return cc;
Jan Glauber828b35f2012-11-29 14:33:30 +0100127}
128
129/* Modify PCI: Unregister I/O address translation parameters */
130int zpci_unregister_ioat(struct zpci_dev *zdev, u8 dmaas)
131{
Sebastian Ott72570832017-06-10 14:10:00 +0200132 u64 req = ZPCI_CREATE_REQ(zdev->fh, dmaas, ZPCI_MOD_FC_DEREG_IOAT);
133 struct zpci_fib fib = {0};
134 u8 cc, status;
Jan Glauber828b35f2012-11-29 14:33:30 +0100135
Sebastian Ott72570832017-06-10 14:10:00 +0200136 cc = zpci_mod_fc(req, &fib, &status);
Niklas Schnelle1f3f7682021-07-16 11:53:37 +0200137 if (cc)
138 zpci_dbg(3, "unreg ioat fid:%x, cc:%d, status:%d\n", zdev->fid, cc, status);
139 return cc;
Jan Glauber828b35f2012-11-29 14:33:30 +0100140}
141
Jan Glauberd0b08852012-12-11 14:53:35 +0100142/* Modify PCI: Set PCI function measurement parameters */
143int zpci_fmb_enable_device(struct zpci_dev *zdev)
144{
Sebastian Ott4e5bd782017-06-10 14:12:13 +0200145 u64 req = ZPCI_CREATE_REQ(zdev->fh, 0, ZPCI_MOD_FC_SET_MEASURE);
146 struct zpci_fib fib = {0};
147 u8 cc, status;
Jan Glauberd0b08852012-12-11 14:53:35 +0100148
Sebastian Ott0b7589e2016-06-15 13:07:51 +0200149 if (zdev->fmb || sizeof(*zdev->fmb) < zdev->fmb_length)
Jan Glauberd0b08852012-12-11 14:53:35 +0100150 return -EINVAL;
151
Wei Yongjun08b42122013-02-25 22:09:25 +0800152 zdev->fmb = kmem_cache_zalloc(zdev_fmb_cache, GFP_KERNEL);
Jan Glauberd0b08852012-12-11 14:53:35 +0100153 if (!zdev->fmb)
154 return -ENOMEM;
Jan Glauberd0b08852012-12-11 14:53:35 +0100155 WARN_ON((u64) zdev->fmb & 0xf);
156
Sebastian Ott60010182015-04-10 14:33:08 +0200157 /* reset software counters */
158 atomic64_set(&zdev->allocated_pages, 0);
159 atomic64_set(&zdev->mapped_pages, 0);
160 atomic64_set(&zdev->unmapped_pages, 0);
161
Sebastian Ott4e5bd782017-06-10 14:12:13 +0200162 fib.fmb_addr = virt_to_phys(zdev->fmb);
163 cc = zpci_mod_fc(req, &fib, &status);
164 if (cc) {
165 kmem_cache_free(zdev_fmb_cache, zdev->fmb);
166 zdev->fmb = NULL;
167 }
168 return cc ? -EIO : 0;
Jan Glauberd0b08852012-12-11 14:53:35 +0100169}
170
171/* Modify PCI: Disable PCI function measurement */
172int zpci_fmb_disable_device(struct zpci_dev *zdev)
173{
Sebastian Ott4e5bd782017-06-10 14:12:13 +0200174 u64 req = ZPCI_CREATE_REQ(zdev->fh, 0, ZPCI_MOD_FC_SET_MEASURE);
175 struct zpci_fib fib = {0};
176 u8 cc, status;
Jan Glauberd0b08852012-12-11 14:53:35 +0100177
178 if (!zdev->fmb)
179 return -EINVAL;
180
181 /* Function measurement is disabled if fmb address is zero */
Sebastian Ott4e5bd782017-06-10 14:12:13 +0200182 cc = zpci_mod_fc(req, &fib, &status);
183 if (cc == 3) /* Function already gone. */
184 cc = 0;
Jan Glauberd0b08852012-12-11 14:53:35 +0100185
Sebastian Ott4e5bd782017-06-10 14:12:13 +0200186 if (!cc) {
187 kmem_cache_free(zdev_fmb_cache, zdev->fmb);
188 zdev->fmb = NULL;
189 }
190 return cc ? -EIO : 0;
Jan Glauberd0b08852012-12-11 14:53:35 +0100191}
192
Jan Glaubercd248342012-11-29 12:50:30 +0100193static int zpci_cfg_load(struct zpci_dev *zdev, int offset, u32 *val, u8 len)
194{
195 u64 req = ZPCI_CREATE_REQ(zdev->fh, ZPCI_PCIAS_CFGSPC, len);
196 u64 data;
197 int rc;
198
Sebastian Ott81deca12019-04-14 16:25:54 +0200199 rc = __zpci_load(&data, req, offset);
Sebastian Ottb170bad2013-04-16 14:17:15 +0200200 if (!rc) {
Sebastian Ott5064cd32016-12-17 15:35:39 +0100201 data = le64_to_cpu((__force __le64) data);
202 data >>= (8 - len) * 8;
Jan Glaubercd248342012-11-29 12:50:30 +0100203 *val = (u32) data;
Sebastian Ottb170bad2013-04-16 14:17:15 +0200204 } else
Jan Glaubercd248342012-11-29 12:50:30 +0100205 *val = 0xffffffff;
206 return rc;
207}
208
209static int zpci_cfg_store(struct zpci_dev *zdev, int offset, u32 val, u8 len)
210{
211 u64 req = ZPCI_CREATE_REQ(zdev->fh, ZPCI_PCIAS_CFGSPC, len);
212 u64 data = val;
213 int rc;
214
Sebastian Ott5064cd32016-12-17 15:35:39 +0100215 data <<= (8 - len) * 8;
216 data = (__force u64) cpu_to_le64(data);
Sebastian Ott81deca12019-04-14 16:25:54 +0200217 rc = __zpci_store(data, req, offset);
Jan Glaubercd248342012-11-29 12:50:30 +0100218 return rc;
219}
220
Jan Glaubercd248342012-11-29 12:50:30 +0100221resource_size_t pcibios_align_resource(void *data, const struct resource *res,
222 resource_size_t size,
223 resource_size_t align)
224{
225 return 0;
226}
227
Jan Glauber87bc3592012-12-06 14:30:28 +0100228/* combine single writes by using store-block insn */
229void __iowrite64_copy(void __iomem *to, const void *from, size_t count)
230{
231 zpci_memcpy_toio(to, from, count);
232}
233
Niklas Schnelleb02002c2020-07-13 14:12:49 +0200234static void __iomem *__ioremap(phys_addr_t addr, size_t size, pgprot_t prot)
Sebastian Ott71ba41c2019-04-14 15:38:01 +0200235{
Niklas Schnellea999eb92020-02-28 10:27:22 +0100236 unsigned long offset, vaddr;
Sebastian Ott71ba41c2019-04-14 15:38:01 +0200237 struct vm_struct *area;
Niklas Schnellea999eb92020-02-28 10:27:22 +0100238 phys_addr_t last_addr;
Sebastian Ott71ba41c2019-04-14 15:38:01 +0200239
Niklas Schnellea999eb92020-02-28 10:27:22 +0100240 last_addr = addr + size - 1;
241 if (!size || last_addr < addr)
Sebastian Ott71ba41c2019-04-14 15:38:01 +0200242 return NULL;
243
244 if (!static_branch_unlikely(&have_mio))
Niklas Schnellea999eb92020-02-28 10:27:22 +0100245 return (void __iomem *) addr;
Sebastian Ott71ba41c2019-04-14 15:38:01 +0200246
Niklas Schnellea999eb92020-02-28 10:27:22 +0100247 offset = addr & ~PAGE_MASK;
248 addr &= PAGE_MASK;
Sebastian Ott71ba41c2019-04-14 15:38:01 +0200249 size = PAGE_ALIGN(size + offset);
250 area = get_vm_area(size, VM_IOREMAP);
251 if (!area)
252 return NULL;
253
Niklas Schnellea999eb92020-02-28 10:27:22 +0100254 vaddr = (unsigned long) area->addr;
Niklas Schnelleb02002c2020-07-13 14:12:49 +0200255 if (ioremap_page_range(vaddr, vaddr + size, addr, prot)) {
Niklas Schnellea999eb92020-02-28 10:27:22 +0100256 free_vm_area(area);
Sebastian Ott71ba41c2019-04-14 15:38:01 +0200257 return NULL;
258 }
259 return (void __iomem *) ((unsigned long) area->addr + offset);
260}
Niklas Schnelleb02002c2020-07-13 14:12:49 +0200261
262void __iomem *ioremap_prot(phys_addr_t addr, size_t size, unsigned long prot)
263{
264 return __ioremap(addr, size, __pgprot(prot));
265}
266EXPORT_SYMBOL(ioremap_prot);
267
268void __iomem *ioremap(phys_addr_t addr, size_t size)
269{
270 return __ioremap(addr, size, PAGE_KERNEL);
271}
Sebastian Ott71ba41c2019-04-14 15:38:01 +0200272EXPORT_SYMBOL(ioremap);
273
Niklas Schnelleb02002c2020-07-13 14:12:49 +0200274void __iomem *ioremap_wc(phys_addr_t addr, size_t size)
275{
276 return __ioremap(addr, size, pgprot_writecombine(PAGE_KERNEL));
277}
278EXPORT_SYMBOL(ioremap_wc);
279
280void __iomem *ioremap_wt(phys_addr_t addr, size_t size)
281{
282 return __ioremap(addr, size, pgprot_writethrough(PAGE_KERNEL));
283}
284EXPORT_SYMBOL(ioremap_wt);
285
Sebastian Ott71ba41c2019-04-14 15:38:01 +0200286void iounmap(volatile void __iomem *addr)
287{
288 if (static_branch_likely(&have_mio))
289 vunmap((__force void *) ((unsigned long) addr & PAGE_MASK));
290}
291EXPORT_SYMBOL(iounmap);
292
Jan Glaubercd248342012-11-29 12:50:30 +0100293/* Create a virtual mapping cookie for a PCI BAR */
Sebastian Ott71ba41c2019-04-14 15:38:01 +0200294static void __iomem *pci_iomap_range_fh(struct pci_dev *pdev, int bar,
295 unsigned long offset, unsigned long max)
Jan Glaubercd248342012-11-29 12:50:30 +0100296{
Sebastian Ott198a5272015-06-23 14:06:35 +0200297 struct zpci_dev *zdev = to_zpci(pdev);
Jan Glaubercd248342012-11-29 12:50:30 +0100298 int idx;
299
Jan Glaubercd248342012-11-29 12:50:30 +0100300 idx = zdev->bars[bar].map_idx;
301 spin_lock(&zpci_iomap_lock);
Michael S. Tsirkin8cfc99b2013-05-29 11:52:21 +0930302 /* Detect overrun */
Sebastian Ottf5e44f82016-01-22 14:11:21 +0100303 WARN_ON(!++zpci_iomap_start[idx].count);
304 zpci_iomap_start[idx].fh = zdev->fh;
305 zpci_iomap_start[idx].bar = bar;
Jan Glaubercd248342012-11-29 12:50:30 +0100306 spin_unlock(&zpci_iomap_lock);
307
Sebastian Ott9e00caa2016-01-22 13:58:42 +0100308 return (void __iomem *) ZPCI_ADDR(idx) + offset;
Jan Glaubercd248342012-11-29 12:50:30 +0100309}
Sebastian Ott71ba41c2019-04-14 15:38:01 +0200310
311static void __iomem *pci_iomap_range_mio(struct pci_dev *pdev, int bar,
312 unsigned long offset,
313 unsigned long max)
314{
315 unsigned long barsize = pci_resource_len(pdev, bar);
316 struct zpci_dev *zdev = to_zpci(pdev);
317 void __iomem *iova;
318
319 iova = ioremap((unsigned long) zdev->bars[bar].mio_wt, barsize);
320 return iova ? iova + offset : iova;
321}
322
323void __iomem *pci_iomap_range(struct pci_dev *pdev, int bar,
324 unsigned long offset, unsigned long max)
325{
Denis Efremovc9c13ba2019-09-28 02:43:08 +0300326 if (bar >= PCI_STD_NUM_BARS || !pci_resource_len(pdev, bar))
Sebastian Ott71ba41c2019-04-14 15:38:01 +0200327 return NULL;
328
329 if (static_branch_likely(&have_mio))
330 return pci_iomap_range_mio(pdev, bar, offset, max);
331 else
332 return pci_iomap_range_fh(pdev, bar, offset, max);
333}
Sebastian Ottd9426082015-02-27 16:43:55 +0100334EXPORT_SYMBOL(pci_iomap_range);
Michael S. Tsirkin8cfc99b2013-05-29 11:52:21 +0930335
336void __iomem *pci_iomap(struct pci_dev *dev, int bar, unsigned long maxlen)
337{
338 return pci_iomap_range(dev, bar, 0, maxlen);
339}
340EXPORT_SYMBOL(pci_iomap);
Jan Glaubercd248342012-11-29 12:50:30 +0100341
Sebastian Ott71ba41c2019-04-14 15:38:01 +0200342static void __iomem *pci_iomap_wc_range_mio(struct pci_dev *pdev, int bar,
343 unsigned long offset, unsigned long max)
344{
345 unsigned long barsize = pci_resource_len(pdev, bar);
346 struct zpci_dev *zdev = to_zpci(pdev);
347 void __iomem *iova;
348
349 iova = ioremap((unsigned long) zdev->bars[bar].mio_wb, barsize);
350 return iova ? iova + offset : iova;
351}
352
353void __iomem *pci_iomap_wc_range(struct pci_dev *pdev, int bar,
354 unsigned long offset, unsigned long max)
355{
Denis Efremovc9c13ba2019-09-28 02:43:08 +0300356 if (bar >= PCI_STD_NUM_BARS || !pci_resource_len(pdev, bar))
Sebastian Ott71ba41c2019-04-14 15:38:01 +0200357 return NULL;
358
359 if (static_branch_likely(&have_mio))
360 return pci_iomap_wc_range_mio(pdev, bar, offset, max);
361 else
362 return pci_iomap_range_fh(pdev, bar, offset, max);
363}
364EXPORT_SYMBOL(pci_iomap_wc_range);
365
366void __iomem *pci_iomap_wc(struct pci_dev *dev, int bar, unsigned long maxlen)
367{
368 return pci_iomap_wc_range(dev, bar, 0, maxlen);
369}
370EXPORT_SYMBOL(pci_iomap_wc);
371
372static void pci_iounmap_fh(struct pci_dev *pdev, void __iomem *addr)
Jan Glaubercd248342012-11-29 12:50:30 +0100373{
Sebastian Ott9e00caa2016-01-22 13:58:42 +0100374 unsigned int idx = ZPCI_IDX(addr);
Jan Glaubercd248342012-11-29 12:50:30 +0100375
Jan Glaubercd248342012-11-29 12:50:30 +0100376 spin_lock(&zpci_iomap_lock);
Michael S. Tsirkin8cfc99b2013-05-29 11:52:21 +0930377 /* Detect underrun */
Sebastian Ottf5e44f82016-01-22 14:11:21 +0100378 WARN_ON(!zpci_iomap_start[idx].count);
Michael S. Tsirkin8cfc99b2013-05-29 11:52:21 +0930379 if (!--zpci_iomap_start[idx].count) {
380 zpci_iomap_start[idx].fh = 0;
381 zpci_iomap_start[idx].bar = 0;
382 }
Jan Glaubercd248342012-11-29 12:50:30 +0100383 spin_unlock(&zpci_iomap_lock);
384}
Sebastian Ott71ba41c2019-04-14 15:38:01 +0200385
386static void pci_iounmap_mio(struct pci_dev *pdev, void __iomem *addr)
387{
388 iounmap(addr);
389}
390
391void pci_iounmap(struct pci_dev *pdev, void __iomem *addr)
392{
393 if (static_branch_likely(&have_mio))
394 pci_iounmap_mio(pdev, addr);
395 else
396 pci_iounmap_fh(pdev, addr);
397}
Sebastian Ottd9426082015-02-27 16:43:55 +0100398EXPORT_SYMBOL(pci_iounmap);
Jan Glaubercd248342012-11-29 12:50:30 +0100399
400static int pci_read(struct pci_bus *bus, unsigned int devfn, int where,
401 int size, u32 *val)
402{
Niklas Schnelle7dcfe502021-09-20 09:47:29 +0200403 struct zpci_dev *zdev = zdev_from_bus(bus, devfn);
Jan Glaubercd248342012-11-29 12:50:30 +0100404
Pierre Morel44510d62020-04-22 15:15:23 +0200405 return (zdev) ? zpci_cfg_load(zdev, where, val, size) : -ENODEV;
Jan Glaubercd248342012-11-29 12:50:30 +0100406}
407
408static int pci_write(struct pci_bus *bus, unsigned int devfn, int where,
409 int size, u32 val)
410{
Niklas Schnelle7dcfe502021-09-20 09:47:29 +0200411 struct zpci_dev *zdev = zdev_from_bus(bus, devfn);
Jan Glaubercd248342012-11-29 12:50:30 +0100412
Pierre Morel44510d62020-04-22 15:15:23 +0200413 return (zdev) ? zpci_cfg_store(zdev, where, val, size) : -ENODEV;
Jan Glaubercd248342012-11-29 12:50:30 +0100414}
415
416static struct pci_ops pci_root_ops = {
417 .read = pci_read,
418 .write = pci_write,
419};
420
Sebastian Ott1803ba22015-02-27 16:43:21 +0100421static void zpci_map_resources(struct pci_dev *pdev)
Jan Glaubercd248342012-11-29 12:50:30 +0100422{
Sebastian Ott71ba41c2019-04-14 15:38:01 +0200423 struct zpci_dev *zdev = to_zpci(pdev);
Jan Glaubercd248342012-11-29 12:50:30 +0100424 resource_size_t len;
425 int i;
426
Denis Efremovc9c13ba2019-09-28 02:43:08 +0300427 for (i = 0; i < PCI_STD_NUM_BARS; i++) {
Jan Glaubercd248342012-11-29 12:50:30 +0100428 len = pci_resource_len(pdev, i);
429 if (!len)
430 continue;
Sebastian Ott71ba41c2019-04-14 15:38:01 +0200431
Sebastian Ottc7ff0e92019-06-27 15:13:05 +0200432 if (zpci_use_mio(zdev))
Sebastian Ott71ba41c2019-04-14 15:38:01 +0200433 pdev->resource[i].start =
Niklas Schnelledf057c92020-02-27 12:17:18 +0100434 (resource_size_t __force) zdev->bars[i].mio_wt;
Sebastian Ott71ba41c2019-04-14 15:38:01 +0200435 else
Sebastian Ottc7ff0e92019-06-27 15:13:05 +0200436 pdev->resource[i].start = (resource_size_t __force)
437 pci_iomap_range_fh(pdev, i, 0, 0);
Jan Glaubercd248342012-11-29 12:50:30 +0100438 pdev->resource[i].end = pdev->resource[i].start + len - 1;
Jan Glaubercd248342012-11-29 12:50:30 +0100439 }
Sebastian Ottcfbb4a7a2018-09-12 12:47:37 +0200440
Niklas Schnelleabb95b72020-08-17 10:29:23 +0200441 zpci_iov_map_resources(pdev);
Sebastian Ott944239c2013-06-05 16:06:16 +0200442}
443
Sebastian Ott1803ba22015-02-27 16:43:21 +0100444static void zpci_unmap_resources(struct pci_dev *pdev)
Sebastian Ott944239c2013-06-05 16:06:16 +0200445{
Sebastian Ottc7ff0e92019-06-27 15:13:05 +0200446 struct zpci_dev *zdev = to_zpci(pdev);
Sebastian Ott944239c2013-06-05 16:06:16 +0200447 resource_size_t len;
448 int i;
449
Sebastian Ottc7ff0e92019-06-27 15:13:05 +0200450 if (zpci_use_mio(zdev))
Sebastian Ott71ba41c2019-04-14 15:38:01 +0200451 return;
452
Denis Efremovc9c13ba2019-09-28 02:43:08 +0300453 for (i = 0; i < PCI_STD_NUM_BARS; i++) {
Sebastian Ott944239c2013-06-05 16:06:16 +0200454 len = pci_resource_len(pdev, i);
455 if (!len)
456 continue;
Sebastian Ottc7ff0e92019-06-27 15:13:05 +0200457 pci_iounmap_fh(pdev, (void __iomem __force *)
458 pdev->resource[i].start);
Sebastian Ott944239c2013-06-05 16:06:16 +0200459 }
460}
Jan Glaubercd248342012-11-29 12:50:30 +0100461
Jan Glaubercd248342012-11-29 12:50:30 +0100462static int zpci_alloc_iomap(struct zpci_dev *zdev)
463{
Sebastian Ottbf19c942016-01-22 13:59:35 +0100464 unsigned long entry;
Jan Glaubercd248342012-11-29 12:50:30 +0100465
466 spin_lock(&zpci_iomap_lock);
Sebastian Ottc506fff32016-01-22 14:01:44 +0100467 entry = find_first_zero_bit(zpci_iomap_bitmap, ZPCI_IOMAP_ENTRIES);
468 if (entry == ZPCI_IOMAP_ENTRIES) {
Jan Glaubercd248342012-11-29 12:50:30 +0100469 spin_unlock(&zpci_iomap_lock);
470 return -ENOSPC;
471 }
Sebastian Ottc506fff32016-01-22 14:01:44 +0100472 set_bit(entry, zpci_iomap_bitmap);
Jan Glaubercd248342012-11-29 12:50:30 +0100473 spin_unlock(&zpci_iomap_lock);
474 return entry;
475}
476
477static void zpci_free_iomap(struct zpci_dev *zdev, int entry)
478{
479 spin_lock(&zpci_iomap_lock);
480 memset(&zpci_iomap_start[entry], 0, sizeof(struct zpci_iomap_entry));
Sebastian Ottc506fff32016-01-22 14:01:44 +0100481 clear_bit(entry, zpci_iomap_bitmap);
Jan Glaubercd248342012-11-29 12:50:30 +0100482 spin_unlock(&zpci_iomap_lock);
483}
484
Niklas Schnelle4fe20492021-07-07 10:42:43 +0200485static void zpci_do_update_iomap_fh(struct zpci_dev *zdev, u32 fh)
486{
487 int bar, idx;
488
489 spin_lock(&zpci_iomap_lock);
490 for (bar = 0; bar < PCI_STD_NUM_BARS; bar++) {
491 if (!zdev->bars[bar].size)
492 continue;
493 idx = zdev->bars[bar].map_idx;
494 if (!zpci_iomap_start[idx].count)
495 continue;
496 WRITE_ONCE(zpci_iomap_start[idx].fh, zdev->fh);
497 }
498 spin_unlock(&zpci_iomap_lock);
499}
500
501void zpci_update_fh(struct zpci_dev *zdev, u32 fh)
502{
503 if (!fh || zdev->fh == fh)
504 return;
505
506 zdev->fh = fh;
507 if (zpci_use_mio(zdev))
508 return;
509 if (zdev->has_resources && zdev_enabled(zdev))
510 zpci_do_update_iomap_fh(zdev, fh);
511}
512
Sebastian Ott7a572a3a2013-11-12 19:33:06 +0100513static struct resource *__alloc_res(struct zpci_dev *zdev, unsigned long start,
514 unsigned long size, unsigned long flags)
515{
516 struct resource *r;
517
518 r = kzalloc(sizeof(*r), GFP_KERNEL);
519 if (!r)
520 return NULL;
521
522 r->start = start;
523 r->end = r->start + size - 1;
524 r->flags = flags;
525 r->name = zdev->res_name;
526
527 if (request_resource(&iomem_resource, r)) {
528 kfree(r);
529 return NULL;
530 }
531 return r;
532}
533
Pierre Morel05bc1be2020-03-23 10:45:43 +0100534int zpci_setup_bus_resources(struct zpci_dev *zdev,
535 struct list_head *resources)
Sebastian Ott7a572a3a2013-11-12 19:33:06 +0100536{
537 unsigned long addr, size, flags;
538 struct resource *res;
539 int i, entry;
540
541 snprintf(zdev->res_name, sizeof(zdev->res_name),
Pierre Morel05bc1be2020-03-23 10:45:43 +0100542 "PCI Bus %04x:%02x", zdev->uid, ZPCI_BUS_NR);
Sebastian Ott7a572a3a2013-11-12 19:33:06 +0100543
Denis Efremovc9c13ba2019-09-28 02:43:08 +0300544 for (i = 0; i < PCI_STD_NUM_BARS; i++) {
Sebastian Ott7a572a3a2013-11-12 19:33:06 +0100545 if (!zdev->bars[i].size)
546 continue;
547 entry = zpci_alloc_iomap(zdev);
548 if (entry < 0)
549 return entry;
550 zdev->bars[i].map_idx = entry;
551
552 /* only MMIO is supported */
553 flags = IORESOURCE_MEM;
554 if (zdev->bars[i].val & 8)
555 flags |= IORESOURCE_PREFETCH;
556 if (zdev->bars[i].val & 4)
557 flags |= IORESOURCE_MEM_64;
558
Sebastian Ottc7ff0e92019-06-27 15:13:05 +0200559 if (zpci_use_mio(zdev))
Niklas Schnelledf057c92020-02-27 12:17:18 +0100560 addr = (unsigned long) zdev->bars[i].mio_wt;
Sebastian Ottdcd33b22019-05-16 14:19:51 +0200561 else
562 addr = ZPCI_ADDR(entry);
Sebastian Ott7a572a3a2013-11-12 19:33:06 +0100563 size = 1UL << zdev->bars[i].size;
564
565 res = __alloc_res(zdev, addr, size, flags);
566 if (!res) {
567 zpci_free_iomap(zdev, entry);
568 return -ENOMEM;
569 }
570 zdev->bars[i].res = res;
571 pci_add_resource(resources, res);
572 }
Niklas Schnellea50297c2021-02-12 14:19:31 +0100573 zdev->has_resources = 1;
Sebastian Ott7a572a3a2013-11-12 19:33:06 +0100574
575 return 0;
576}
577
578static void zpci_cleanup_bus_resources(struct zpci_dev *zdev)
579{
580 int i;
581
Denis Efremovc9c13ba2019-09-28 02:43:08 +0300582 for (i = 0; i < PCI_STD_NUM_BARS; i++) {
Sebastian Ott2b1df722015-07-28 19:10:45 +0200583 if (!zdev->bars[i].size || !zdev->bars[i].res)
Sebastian Ott7a572a3a2013-11-12 19:33:06 +0100584 continue;
585
586 zpci_free_iomap(zdev, zdev->bars[i].map_idx);
587 release_resource(zdev->bars[i].res);
588 kfree(zdev->bars[i].res);
589 }
Niklas Schnellea50297c2021-02-12 14:19:31 +0100590 zdev->has_resources = 0;
Sebastian Ott7a572a3a2013-11-12 19:33:06 +0100591}
592
Oliver O'Halloran06dc6602021-09-14 01:27:08 +1000593int pcibios_device_add(struct pci_dev *pdev)
Sebastian Ottaf0a8a82013-04-16 14:13:21 +0200594{
Niklas Schnelle2a671f72021-08-06 12:11:16 +0200595 struct zpci_dev *zdev = to_zpci(pdev);
Sebastian Ottcb809182013-08-29 19:34:37 +0200596 struct resource *res;
597 int i;
598
Niklas Schnelle2a671f72021-08-06 12:11:16 +0200599 /* The pdev has a reference to the zdev via its bus */
600 zpci_zdev_get(zdev);
Sebastian Ott7dc20ab2018-12-21 15:14:20 +0100601 if (pdev->is_physfn)
602 pdev->no_vf_scan = 1;
603
Sebastian Ottef4858c2014-04-30 14:50:09 -0600604 pdev->dev.groups = zpci_attr_groups;
Bart Van Assche56579332017-01-20 13:04:02 -0800605 pdev->dev.dma_ops = &s390_pci_dma_ops;
Sebastian Ott1803ba22015-02-27 16:43:21 +0100606 zpci_map_resources(pdev);
Sebastian Ottcb809182013-08-29 19:34:37 +0200607
Denis Efremovc9c13ba2019-09-28 02:43:08 +0300608 for (i = 0; i < PCI_STD_NUM_BARS; i++) {
Sebastian Ottcb809182013-08-29 19:34:37 +0200609 res = &pdev->resource[i];
610 if (res->parent || !res->flags)
611 continue;
612 pci_claim_resource(pdev, i);
613 }
614
615 return 0;
616}
617
Sebastian Ott1803ba22015-02-27 16:43:21 +0100618void pcibios_release_device(struct pci_dev *pdev)
619{
Niklas Schnelle2a671f72021-08-06 12:11:16 +0200620 struct zpci_dev *zdev = to_zpci(pdev);
621
Sebastian Ott1803ba22015-02-27 16:43:21 +0100622 zpci_unmap_resources(pdev);
Niklas Schnelle2a671f72021-08-06 12:11:16 +0200623 zpci_zdev_put(zdev);
Sebastian Ott1803ba22015-02-27 16:43:21 +0100624}
625
Sebastian Ottcb809182013-08-29 19:34:37 +0200626int pcibios_enable_device(struct pci_dev *pdev, int mask)
627{
Sebastian Ott198a5272015-06-23 14:06:35 +0200628 struct zpci_dev *zdev = to_zpci(pdev);
Sebastian Ottaf0a8a82013-04-16 14:13:21 +0200629
Sebastian Ott9a996492016-01-29 15:13:30 +0100630 zpci_debug_init_device(zdev, dev_name(&pdev->dev));
Sebastian Ottaf0a8a82013-04-16 14:13:21 +0200631 zpci_fmb_enable_device(zdev);
Sebastian Ottaf0a8a82013-04-16 14:13:21 +0200632
Bjorn Helgaasd7533232014-02-26 15:30:24 -0700633 return pci_enable_resources(pdev, mask);
Sebastian Ottaf0a8a82013-04-16 14:13:21 +0200634}
635
Sebastian Ottcb809182013-08-29 19:34:37 +0200636void pcibios_disable_device(struct pci_dev *pdev)
Sebastian Ott944239c2013-06-05 16:06:16 +0200637{
Sebastian Ott198a5272015-06-23 14:06:35 +0200638 struct zpci_dev *zdev = to_zpci(pdev);
Sebastian Ott944239c2013-06-05 16:06:16 +0200639
Sebastian Ott944239c2013-06-05 16:06:16 +0200640 zpci_fmb_disable_device(zdev);
641 zpci_debug_exit_device(zdev);
Sebastian Ott944239c2013-06-05 16:06:16 +0200642}
643
Pierre Morel05bc1be2020-03-23 10:45:43 +0100644static int __zpci_register_domain(int domain)
Jan Glaubercd248342012-11-29 12:50:30 +0100645{
Niklas Schnelle969ae012020-03-17 12:59:37 +0100646 spin_lock(&zpci_domain_lock);
Pierre Morel05bc1be2020-03-23 10:45:43 +0100647 if (test_bit(domain, zpci_domain)) {
Niklas Schnelle969ae012020-03-17 12:59:37 +0100648 spin_unlock(&zpci_domain_lock);
Pierre Morel05bc1be2020-03-23 10:45:43 +0100649 pr_err("Domain %04x is already assigned\n", domain);
650 return -EEXIST;
Niklas Schnelle969ae012020-03-17 12:59:37 +0100651 }
Pierre Morel05bc1be2020-03-23 10:45:43 +0100652 set_bit(domain, zpci_domain);
653 spin_unlock(&zpci_domain_lock);
654 return domain;
655}
Niklas Schnelle969ae012020-03-17 12:59:37 +0100656
Pierre Morel05bc1be2020-03-23 10:45:43 +0100657static int __zpci_alloc_domain(void)
658{
659 int domain;
Sebastian Ott312e8462017-06-21 10:20:35 +0200660
Pierre Morel05bc1be2020-03-23 10:45:43 +0100661 spin_lock(&zpci_domain_lock);
Niklas Schnelle969ae012020-03-17 12:59:37 +0100662 /*
663 * We can always auto allocate domains below ZPCI_NR_DEVICES.
664 * There is either a free domain or we have reached the maximum in
665 * which case we would have bailed earlier.
666 */
Pierre Morel05bc1be2020-03-23 10:45:43 +0100667 domain = find_first_zero_bit(zpci_domain, ZPCI_NR_DEVICES);
668 set_bit(domain, zpci_domain);
Jan Glaubercd248342012-11-29 12:50:30 +0100669 spin_unlock(&zpci_domain_lock);
Pierre Morel05bc1be2020-03-23 10:45:43 +0100670 return domain;
Jan Glaubercd248342012-11-29 12:50:30 +0100671}
672
Pierre Morel05bc1be2020-03-23 10:45:43 +0100673int zpci_alloc_domain(int domain)
674{
675 if (zpci_unique_uid) {
676 if (domain)
677 return __zpci_register_domain(domain);
678 pr_warn("UID checking was active but no UID is provided: switching to automatic domain allocation\n");
679 update_uid_checking(false);
680 }
681 return __zpci_alloc_domain();
682}
683
684void zpci_free_domain(int domain)
Jan Glaubercd248342012-11-29 12:50:30 +0100685{
686 spin_lock(&zpci_domain_lock);
Pierre Morel05bc1be2020-03-23 10:45:43 +0100687 clear_bit(domain, zpci_domain);
Jan Glaubercd248342012-11-29 12:50:30 +0100688 spin_unlock(&zpci_domain_lock);
689}
690
Sebastian Ott7d594322013-11-12 19:35:01 +0100691
Jan Glaubera755a452012-11-29 12:55:21 +0100692int zpci_enable_device(struct zpci_dev *zdev)
693{
Niklas Schnellecc049ee2021-07-22 11:44:08 +0200694 u32 fh = zdev->fh;
Niklas Schnelle1f3f7682021-07-16 11:53:37 +0200695 int rc = 0;
Jan Glaubera755a452012-11-29 12:55:21 +0100696
Niklas Schnelle1f3f7682021-07-16 11:53:37 +0200697 if (clp_enable_fh(zdev, &fh, ZPCI_NR_DMA_SPACES))
Niklas Schnellef7addcd2021-07-21 19:58:54 +0200698 rc = -EIO;
Niklas Schnelle1f3f7682021-07-16 11:53:37 +0200699 else
Niklas Schnelle4fe20492021-07-07 10:42:43 +0200700 zpci_update_fh(zdev, fh);
Jan Glaubera755a452012-11-29 12:55:21 +0100701 return rc;
702}
Jan Glaubera755a452012-11-29 12:55:21 +0100703
Sebastian Ottcb65a662013-04-16 14:12:17 +0200704int zpci_disable_device(struct zpci_dev *zdev)
705{
Niklas Schnellecc049ee2021-07-22 11:44:08 +0200706 u32 fh = zdev->fh;
Niklas Schnelle8256add2021-07-22 12:38:29 +0200707 int cc, rc = 0;
708
Niklas Schnellecc049ee2021-07-22 11:44:08 +0200709 cc = clp_disable_fh(zdev, &fh);
710 if (!cc) {
Niklas Schnelle4fe20492021-07-07 10:42:43 +0200711 zpci_update_fh(zdev, fh);
Niklas Schnellecc049ee2021-07-22 11:44:08 +0200712 } else if (cc == CLP_RC_SETPCIFN_ALRDY) {
Niklas Schnelle8256add2021-07-22 12:38:29 +0200713 pr_info("Disabling PCI function %08x had no effect as it was already disabled\n",
714 zdev->fid);
715 /* Function is already disabled - update handle */
Niklas Schnellecc049ee2021-07-22 11:44:08 +0200716 rc = clp_refresh_fh(zdev->fid, &fh);
717 if (!rc) {
Niklas Schnelle4fe20492021-07-07 10:42:43 +0200718 zpci_update_fh(zdev, fh);
Niklas Schnelle8256add2021-07-22 12:38:29 +0200719 rc = -EINVAL;
Niklas Schnellecc049ee2021-07-22 11:44:08 +0200720 }
721 } else {
Niklas Schnelle8256add2021-07-22 12:38:29 +0200722 rc = -EIO;
723 }
724 return rc;
Sebastian Ottcb65a662013-04-16 14:12:17 +0200725}
Sebastian Ottcb65a662013-04-16 14:12:17 +0200726
Niklas Schnelleba764dd2020-07-22 16:53:54 +0200727/**
Niklas Schnelleda995d52021-07-01 15:49:11 +0200728 * zpci_hot_reset_device - perform a reset of the given zPCI function
729 * @zdev: the slot which should be reset
730 *
731 * Performs a low level reset of the zPCI function. The reset is low level in
732 * the sense that the zPCI function can be reset without detaching it from the
733 * common PCI subsystem. The reset may be performed while under control of
734 * either DMA or IOMMU APIs in which case the existing DMA/IOMMU translation
735 * table is reinstated at the end of the reset.
736 *
737 * After the reset the functions internal state is reset to an initial state
738 * equivalent to its state during boot when first probing a driver.
739 * Consequently after reset the PCI function requires re-initialization via the
740 * common PCI code including re-enabling IRQs via pci_alloc_irq_vectors()
741 * and enabling the function via e.g.pci_enablde_device_flags().The caller
742 * must guard against concurrent reset attempts.
743 *
744 * In most cases this function should not be called directly but through
745 * pci_reset_function() or pci_reset_bus() which handle the save/restore and
746 * locking.
747 *
748 * Return: 0 on success and an error value otherwise
749 */
750int zpci_hot_reset_device(struct zpci_dev *zdev)
751{
752 int rc;
753
754 zpci_dbg(3, "rst fid:%x, fh:%x\n", zdev->fid, zdev->fh);
755 if (zdev_enabled(zdev)) {
756 /* Disables device access, DMAs and IRQs (reset state) */
757 rc = zpci_disable_device(zdev);
758 /*
759 * Due to a z/VM vs LPAR inconsistency in the error state the
760 * FH may indicate an enabled device but disable says the
761 * device is already disabled don't treat it as an error here.
762 */
763 if (rc == -EINVAL)
764 rc = 0;
765 if (rc)
766 return rc;
767 }
768
769 rc = zpci_enable_device(zdev);
770 if (rc)
771 return rc;
772
773 if (zdev->dma_table)
774 rc = zpci_register_ioat(zdev, 0, zdev->start_dma, zdev->end_dma,
Niklas Schnelle568de502021-11-25 16:46:00 +0100775 virt_to_phys(zdev->dma_table));
Niklas Schnelleda995d52021-07-01 15:49:11 +0200776 else
777 rc = zpci_dma_init_device(zdev);
778 if (rc) {
779 zpci_disable_device(zdev);
780 return rc;
781 }
782
783 return 0;
784}
785
786/**
Niklas Schnelleba764dd2020-07-22 16:53:54 +0200787 * zpci_create_device() - Create a new zpci_dev and add it to the zbus
788 * @fid: Function ID of the device to be created
789 * @fh: Current Function Handle of the device to be created
790 * @state: Initial state after creation either Standby or Configured
791 *
792 * Creates a new zpci device and adds it to its, possibly newly created, zbus
793 * as well as zpci_list.
794 *
Niklas Schnelle14c87ba2021-02-12 11:57:58 +0100795 * Returns: the zdev on success or an error pointer otherwise
Niklas Schnelleba764dd2020-07-22 16:53:54 +0200796 */
Niklas Schnelle14c87ba2021-02-12 11:57:58 +0100797struct zpci_dev *zpci_create_device(u32 fid, u32 fh, enum zpci_state state)
Jan Glaubercd248342012-11-29 12:50:30 +0100798{
Niklas Schnelleba764dd2020-07-22 16:53:54 +0200799 struct zpci_dev *zdev;
Jan Glaubercd248342012-11-29 12:50:30 +0100800 int rc;
801
Niklas Schnelle52c79e62022-03-18 16:25:31 +0100802 zpci_dbg(1, "add fid:%x, fh:%x, c:%d\n", fid, fh, state);
Niklas Schnelleba764dd2020-07-22 16:53:54 +0200803 zdev = kzalloc(sizeof(*zdev), GFP_KERNEL);
804 if (!zdev)
Niklas Schnelle14c87ba2021-02-12 11:57:58 +0100805 return ERR_PTR(-ENOMEM);
Niklas Schnelleba764dd2020-07-22 16:53:54 +0200806
807 /* FID and Function Handle are the static/dynamic identifiers */
808 zdev->fid = fid;
809 zdev->fh = fh;
810
811 /* Query function properties and update zdev */
812 rc = clp_query_pci_fn(zdev);
813 if (rc)
814 goto error;
815 zdev->state = state;
816
Pierre Morel05bc1be2020-03-23 10:45:43 +0100817 kref_init(&zdev->kref);
Niklas Schnelleba764dd2020-07-22 16:53:54 +0200818 mutex_init(&zdev->lock);
819
820 rc = zpci_init_iommu(zdev);
821 if (rc)
822 goto error;
823
Niklas Schnelleba764dd2020-07-22 16:53:54 +0200824 rc = zpci_bus_device_register(zdev, &pci_root_ops);
825 if (rc)
Niklas Schnellea50297c2021-02-12 14:19:31 +0100826 goto error_destroy_iommu;
Pierre Morel05bc1be2020-03-23 10:45:43 +0100827
828 spin_lock(&zpci_list_lock);
829 list_add_tail(&zdev->entry, &zpci_list);
830 spin_unlock(&zpci_list_lock);
Jan Glaubercd248342012-11-29 12:50:30 +0100831
Niklas Schnelle14c87ba2021-02-12 11:57:58 +0100832 return zdev;
Jan Glaubercd248342012-11-29 12:50:30 +0100833
Niklas Schnelleba764dd2020-07-22 16:53:54 +0200834error_destroy_iommu:
Joerg Roedelf42c2232017-04-27 14:44:06 +0200835 zpci_destroy_iommu(zdev);
Niklas Schnelleba764dd2020-07-22 16:53:54 +0200836error:
837 zpci_dbg(0, "add fid:%x, rc:%d\n", fid, rc);
838 kfree(zdev);
Niklas Schnelle14c87ba2021-02-12 11:57:58 +0100839 return ERR_PTR(rc);
Jan Glaubercd248342012-11-29 12:50:30 +0100840}
841
Niklas Schnellea46044a2021-09-22 15:55:12 +0200842bool zpci_is_device_configured(struct zpci_dev *zdev)
843{
844 enum zpci_state state = zdev->state;
845
846 return state != ZPCI_FN_STATE_RESERVED &&
847 state != ZPCI_FN_STATE_STANDBY;
848}
849
Niklas Schnelle2631f6b2020-11-03 10:41:20 +0100850/**
Niklas Schnellea7f82c32021-04-09 14:08:50 +0200851 * zpci_scan_configured_device() - Scan a freshly configured zpci_dev
Niklas Schnelle2631f6b2020-11-03 10:41:20 +0100852 * @zdev: The zpci_dev to be configured
853 * @fh: The general function handle supplied by the platform
854 *
Niklas Schnelle61311e32021-03-26 13:58:48 +0100855 * Given a device in the configuration state Configured, enables, scans and
Niklas Schnellea7f82c32021-04-09 14:08:50 +0200856 * adds it to the common code PCI subsystem if possible. If the PCI device is
857 * parked because we can not yet create a PCI bus because we have not seen
858 * function 0, it is ignored but will be scanned once function 0 appears.
859 * If any failure occurs, the zpci_dev is left disabled.
Niklas Schnelle2631f6b2020-11-03 10:41:20 +0100860 *
861 * Return: 0 on success, or an error code otherwise
862 */
Niklas Schnellea7f82c32021-04-09 14:08:50 +0200863int zpci_scan_configured_device(struct zpci_dev *zdev, u32 fh)
Niklas Schnelle2631f6b2020-11-03 10:41:20 +0100864{
Niklas Schnelle2631f6b2020-11-03 10:41:20 +0100865 int rc;
866
Niklas Schnelle4fe20492021-07-07 10:42:43 +0200867 zpci_update_fh(zdev, fh);
Niklas Schnelle2631f6b2020-11-03 10:41:20 +0100868 /* the PCI function will be scanned once function 0 appears */
869 if (!zdev->zbus->bus)
870 return 0;
871
Niklas Schnellea50297c2021-02-12 14:19:31 +0100872 /* For function 0 on a multi-function bus scan whole bus as we might
873 * have to pick up existing functions waiting for it to allow creating
874 * the PCI bus
875 */
876 if (zdev->devfn == 0 && zdev->zbus->multifunction)
877 rc = zpci_bus_scan_bus(zdev->zbus);
878 else
879 rc = zpci_bus_scan_device(zdev);
Niklas Schnelle2631f6b2020-11-03 10:41:20 +0100880
Niklas Schnelle2631f6b2020-11-03 10:41:20 +0100881 return rc;
882}
883
884/**
885 * zpci_deconfigure_device() - Deconfigure a zpci_dev
886 * @zdev: The zpci_dev to configure
887 *
888 * Deconfigure a zPCI function that is currently configured and possibly known
889 * to the common code PCI subsystem.
890 * If any failure occurs the device is left as is.
891 *
892 * Return: 0 on success, or an error code otherwise
893 */
894int zpci_deconfigure_device(struct zpci_dev *zdev)
895{
896 int rc;
897
898 if (zdev->zbus->bus)
Niklas Schnelle95b3a8b2021-01-26 13:58:28 +0100899 zpci_bus_remove_device(zdev, false);
Niklas Schnelle2631f6b2020-11-03 10:41:20 +0100900
Niklas Schnelle1f3f7682021-07-16 11:53:37 +0200901 if (zdev->dma_table) {
902 rc = zpci_dma_exit_device(zdev);
903 if (rc)
904 return rc;
905 }
Niklas Schnelle2631f6b2020-11-03 10:41:20 +0100906 if (zdev_enabled(zdev)) {
907 rc = zpci_disable_device(zdev);
908 if (rc)
909 return rc;
910 }
911
912 rc = sclp_pci_deconfigure(zdev->fid);
913 zpci_dbg(3, "deconf fid:%x, rc:%d\n", zdev->fid, rc);
914 if (rc)
915 return rc;
916 zdev->state = ZPCI_FN_STATE_STANDBY;
917
918 return 0;
919}
920
Niklas Schnellea46044a2021-09-22 15:55:12 +0200921/**
922 * zpci_device_reserved() - Mark device as resverved
923 * @zdev: the zpci_dev that was reserved
924 *
925 * Handle the case that a given zPCI function was reserved by another system.
926 * After a call to this function the zpci_dev can not be found via
927 * get_zdev_by_fid() anymore but may still be accessible via existing
928 * references though it will not be functional anymore.
929 */
930void zpci_device_reserved(struct zpci_dev *zdev)
931{
932 if (zdev->has_hp_slot)
933 zpci_exit_slot(zdev);
934 /*
935 * Remove device from zpci_list as it is going away. This also
936 * makes sure we ignore subsequent zPCI events for this device.
937 */
938 spin_lock(&zpci_list_lock);
939 list_del(&zdev->entry);
940 spin_unlock(&zpci_list_lock);
941 zdev->state = ZPCI_FN_STATE_RESERVED;
942 zpci_dbg(3, "rsv fid:%x\n", zdev->fid);
943 zpci_zdev_put(zdev);
944}
945
Pierre Morel05bc1be2020-03-23 10:45:43 +0100946void zpci_release_device(struct kref *kref)
Sebastian Ott623bd442017-05-09 12:27:30 +0200947{
Pierre Morel05bc1be2020-03-23 10:45:43 +0100948 struct zpci_dev *zdev = container_of(kref, struct zpci_dev, kref);
Niklas Schnellea9045c22021-03-05 14:32:02 +0100949 int ret;
Sebastian Ott623bd442017-05-09 12:27:30 +0200950
Niklas Schnelle2f0230b2020-08-03 17:46:32 +0200951 if (zdev->zbus->bus)
Niklas Schnelle95b3a8b2021-01-26 13:58:28 +0100952 zpci_bus_remove_device(zdev, false);
Pierre Morel44510d62020-04-22 15:15:23 +0200953
Niklas Schnelle1f3f7682021-07-16 11:53:37 +0200954 if (zdev->dma_table)
955 zpci_dma_exit_device(zdev);
Niklas Schnellef6576a12021-03-02 14:55:21 +0100956 if (zdev_enabled(zdev))
Pierre Morel05bc1be2020-03-23 10:45:43 +0100957 zpci_disable_device(zdev);
Niklas Schnellef6576a12021-03-02 14:55:21 +0100958
959 switch (zdev->state) {
Niklas Schnellea9045c22021-03-05 14:32:02 +0100960 case ZPCI_FN_STATE_CONFIGURED:
961 ret = sclp_pci_deconfigure(zdev->fid);
962 zpci_dbg(3, "deconf fid:%x, rc:%d\n", zdev->fid, ret);
963 fallthrough;
Pierre Morel05bc1be2020-03-23 10:45:43 +0100964 case ZPCI_FN_STATE_STANDBY:
Pierre Morel44510d62020-04-22 15:15:23 +0200965 if (zdev->has_hp_slot)
Pierre Morel05bc1be2020-03-23 10:45:43 +0100966 zpci_exit_slot(zdev);
Niklas Schnellea46044a2021-09-22 15:55:12 +0200967 spin_lock(&zpci_list_lock);
968 list_del(&zdev->entry);
969 spin_unlock(&zpci_list_lock);
970 zpci_dbg(3, "rsv fid:%x\n", zdev->fid);
971 fallthrough;
972 case ZPCI_FN_STATE_RESERVED:
Niklas Schnelle02368b72021-08-06 10:28:40 +0200973 if (zdev->has_resources)
974 zpci_cleanup_bus_resources(zdev);
Pierre Morel44510d62020-04-22 15:15:23 +0200975 zpci_bus_device_unregister(zdev);
976 zpci_destroy_iommu(zdev);
Pierre Morel05bc1be2020-03-23 10:45:43 +0100977 fallthrough;
978 default:
979 break;
980 }
Pierre Morel05bc1be2020-03-23 10:45:43 +0100981 zpci_dbg(3, "rem fid:%x\n", zdev->fid);
982 kfree(zdev);
Sebastian Ott623bd442017-05-09 12:27:30 +0200983}
984
Martin Schwidefskybd3a1722016-07-18 14:05:21 +0200985int zpci_report_error(struct pci_dev *pdev,
986 struct zpci_report_error_header *report)
987{
988 struct zpci_dev *zdev = to_zpci(pdev);
989
990 return sclp_pci_report(report, zdev->fh, zdev->fid);
991}
992EXPORT_SYMBOL(zpci_report_error);
993
Niklas Schnelle4cdf2f42021-07-07 11:00:01 +0200994/**
995 * zpci_clear_error_state() - Clears the zPCI error state of the device
996 * @zdev: The zdev for which the zPCI error state should be reset
997 *
998 * Clear the zPCI error state of the device. If clearing the zPCI error state
999 * fails the device is left in the error state. In this case it may make sense
1000 * to call zpci_io_perm_failure() on the associated pdev if it exists.
1001 *
1002 * Returns: 0 on success, -EIO otherwise
1003 */
1004int zpci_clear_error_state(struct zpci_dev *zdev)
1005{
1006 u64 req = ZPCI_CREATE_REQ(zdev->fh, 0, ZPCI_MOD_FC_RESET_ERROR);
1007 struct zpci_fib fib = {0};
1008 u8 status;
1009 int cc;
1010
1011 cc = zpci_mod_fc(req, &fib, &status);
1012 if (cc) {
1013 zpci_dbg(3, "ces fid:%x, cc:%d, status:%x\n", zdev->fid, cc, status);
1014 return -EIO;
1015 }
1016
1017 return 0;
1018}
1019
1020/**
1021 * zpci_reset_load_store_blocked() - Re-enables L/S from error state
1022 * @zdev: The zdev for which to unblock load/store access
1023 *
1024 * Re-enables load/store access for a PCI function in the error state while
1025 * keeping DMA blocked. In this state drivers can poke MMIO space to determine
1026 * if error recovery is possible while catching any rogue DMA access from the
1027 * device.
1028 *
1029 * Returns: 0 on success, -EIO otherwise
1030 */
1031int zpci_reset_load_store_blocked(struct zpci_dev *zdev)
1032{
1033 u64 req = ZPCI_CREATE_REQ(zdev->fh, 0, ZPCI_MOD_FC_RESET_BLOCK);
1034 struct zpci_fib fib = {0};
1035 u8 status;
1036 int cc;
1037
1038 cc = zpci_mod_fc(req, &fib, &status);
1039 if (cc) {
1040 zpci_dbg(3, "rls fid:%x, cc:%d, status:%x\n", zdev->fid, cc, status);
1041 return -EIO;
1042 }
1043
1044 return 0;
1045}
1046
Jan Glaubercd248342012-11-29 12:50:30 +01001047static int zpci_mem_init(void)
1048{
Sebastian Ott80c544de2016-03-14 15:47:23 +01001049 BUILD_BUG_ON(!is_power_of_2(__alignof__(struct zpci_fmb)) ||
1050 __alignof__(struct zpci_fmb) < sizeof(struct zpci_fmb));
1051
Jan Glauberd0b08852012-12-11 14:53:35 +01001052 zdev_fmb_cache = kmem_cache_create("PCI_FMB_cache", sizeof(struct zpci_fmb),
Sebastian Ott80c544de2016-03-14 15:47:23 +01001053 __alignof__(struct zpci_fmb), 0, NULL);
Jan Glauberd0b08852012-12-11 14:53:35 +01001054 if (!zdev_fmb_cache)
Sebastian Ottc506fff32016-01-22 14:01:44 +01001055 goto error_fmb;
Jan Glauberd0b08852012-12-11 14:53:35 +01001056
Sebastian Ottc506fff32016-01-22 14:01:44 +01001057 zpci_iomap_start = kcalloc(ZPCI_IOMAP_ENTRIES,
1058 sizeof(*zpci_iomap_start), GFP_KERNEL);
Jan Glaubercd248342012-11-29 12:50:30 +01001059 if (!zpci_iomap_start)
Jan Glauber9a4da8a2012-11-29 13:05:05 +01001060 goto error_iomap;
Jan Glaubercd248342012-11-29 12:50:30 +01001061
Sebastian Ottc506fff32016-01-22 14:01:44 +01001062 zpci_iomap_bitmap = kcalloc(BITS_TO_LONGS(ZPCI_IOMAP_ENTRIES),
1063 sizeof(*zpci_iomap_bitmap), GFP_KERNEL);
1064 if (!zpci_iomap_bitmap)
1065 goto error_iomap_bitmap;
1066
Niklas Schnelleb02002c2020-07-13 14:12:49 +02001067 if (static_branch_likely(&have_mio))
1068 clp_setup_writeback_mio();
1069
Sebastian Ottc506fff32016-01-22 14:01:44 +01001070 return 0;
1071error_iomap_bitmap:
1072 kfree(zpci_iomap_start);
Jan Glauber9a4da8a2012-11-29 13:05:05 +01001073error_iomap:
Jan Glauberd0b08852012-12-11 14:53:35 +01001074 kmem_cache_destroy(zdev_fmb_cache);
Sebastian Ottc506fff32016-01-22 14:01:44 +01001075error_fmb:
Jan Glaubercd248342012-11-29 12:50:30 +01001076 return -ENOMEM;
1077}
1078
1079static void zpci_mem_exit(void)
1080{
Sebastian Ottc506fff32016-01-22 14:01:44 +01001081 kfree(zpci_iomap_bitmap);
Jan Glaubercd248342012-11-29 12:50:30 +01001082 kfree(zpci_iomap_start);
Jan Glauberd0b08852012-12-11 14:53:35 +01001083 kmem_cache_destroy(zdev_fmb_cache);
Jan Glaubercd248342012-11-29 12:50:30 +01001084}
1085
Sebastian Ott6324b4d2019-02-12 16:23:13 +01001086static unsigned int s390_pci_probe __initdata = 1;
Sebastian Ottfbfe07d2019-02-26 16:07:32 +01001087unsigned int s390_pci_force_floating __initdata;
Sebastian Ottaa3b7c22013-12-12 17:48:32 +01001088static unsigned int s390_pci_initialized;
Jan Glaubercd248342012-11-29 12:50:30 +01001089
1090char * __init pcibios_setup(char *str)
1091{
Sebastian Ott257608f2013-12-12 17:55:22 +01001092 if (!strcmp(str, "off")) {
1093 s390_pci_probe = 0;
Jan Glaubercd248342012-11-29 12:50:30 +01001094 return NULL;
1095 }
Sebastian Ott56271302019-04-18 21:39:06 +02001096 if (!strcmp(str, "nomio")) {
Niklas Schnelle3322ba02021-07-08 14:55:42 +02001097 S390_lowcore.machine_flags &= ~MACHINE_FLAG_PCI_MIO;
Sebastian Ott56271302019-04-18 21:39:06 +02001098 return NULL;
1099 }
Sebastian Ottfbfe07d2019-02-26 16:07:32 +01001100 if (!strcmp(str, "force_floating")) {
1101 s390_pci_force_floating = 1;
1102 return NULL;
1103 }
Pierre Morel6cf17f92020-02-07 13:35:08 +01001104 if (!strcmp(str, "norid")) {
1105 s390_pci_no_rid = 1;
1106 return NULL;
1107 }
Jan Glaubercd248342012-11-29 12:50:30 +01001108 return str;
1109}
1110
Sebastian Ottaa3b7c22013-12-12 17:48:32 +01001111bool zpci_is_enabled(void)
1112{
1113 return s390_pci_initialized;
1114}
1115
Jan Glaubercd248342012-11-29 12:50:30 +01001116static int __init pci_base_init(void)
1117{
1118 int rc;
1119
Heiko Carstens1e5635d2013-01-30 15:52:16 +01001120 if (!s390_pci_probe)
Jan Glaubercd248342012-11-29 12:50:30 +01001121 return 0;
1122
Niklas Schnelleda786932020-10-26 10:01:24 +01001123 if (!test_facility(69) || !test_facility(71)) {
1124 pr_info("PCI is not supported because CPU facilities 69 or 71 are not available\n");
Jan Glaubercd248342012-11-29 12:50:30 +01001125 return 0;
Niklas Schnelleda786932020-10-26 10:01:24 +01001126 }
Jan Glaubercd248342012-11-29 12:50:30 +01001127
Niklas Schnelle3322ba02021-07-08 14:55:42 +02001128 if (MACHINE_HAS_PCI_MIO) {
Sebastian Ott71ba41c2019-04-14 15:38:01 +02001129 static_branch_enable(&have_mio);
Sebastian Ott9964f392019-07-10 13:08:06 +02001130 ctl_set_bit(2, 5);
1131 }
Sebastian Ott71ba41c2019-04-14 15:38:01 +02001132
Jan Glauberd0b08852012-12-11 14:53:35 +01001133 rc = zpci_debug_init();
1134 if (rc)
Martin Schwidefsky1f44a2252013-06-27 09:01:09 +02001135 goto out;
Jan Glauberd0b08852012-12-11 14:53:35 +01001136
Jan Glaubercd248342012-11-29 12:50:30 +01001137 rc = zpci_mem_init();
1138 if (rc)
1139 goto out_mem;
1140
Jan Glauber9a4da8a2012-11-29 13:05:05 +01001141 rc = zpci_irq_init();
1142 if (rc)
1143 goto out_irq;
1144
Jan Glauber828b35f2012-11-29 14:33:30 +01001145 rc = zpci_dma_init();
1146 if (rc)
1147 goto out_dma;
1148
Sebastian Ott1d578962013-08-29 19:37:28 +02001149 rc = clp_scan_pci_devices();
Jan Glaubera755a452012-11-29 12:55:21 +01001150 if (rc)
1151 goto out_find;
Niklas Schnelle14c87ba2021-02-12 11:57:58 +01001152 zpci_bus_scan_busses();
Jan Glaubera755a452012-11-29 12:55:21 +01001153
Sebastian Ottaa3b7c22013-12-12 17:48:32 +01001154 s390_pci_initialized = 1;
Jan Glaubercd248342012-11-29 12:50:30 +01001155 return 0;
1156
Jan Glaubera755a452012-11-29 12:55:21 +01001157out_find:
Jan Glauber828b35f2012-11-29 14:33:30 +01001158 zpci_dma_exit();
1159out_dma:
Jan Glauber9a4da8a2012-11-29 13:05:05 +01001160 zpci_irq_exit();
1161out_irq:
Jan Glaubercd248342012-11-29 12:50:30 +01001162 zpci_mem_exit();
1163out_mem:
Jan Glauberd0b08852012-12-11 14:53:35 +01001164 zpci_debug_exit();
Martin Schwidefsky1f44a2252013-06-27 09:01:09 +02001165out:
Jan Glaubercd248342012-11-29 12:50:30 +01001166 return rc;
1167}
Sebastian Ott67f43f32013-08-29 19:33:16 +02001168subsys_initcall_sync(pci_base_init);