xref: /openbmc/qemu/hw/s390x/s390-pci-inst.c (revision fe1127da)
1 /*
2  * s390 PCI instructions
3  *
4  * Copyright 2014 IBM Corp.
5  * Author(s): Frank Blaschka <frank.blaschka@de.ibm.com>
6  *            Hong Bo Li <lihbbj@cn.ibm.com>
7  *            Yi Min Zhao <zyimin@cn.ibm.com>
8  *
9  * This work is licensed under the terms of the GNU GPL, version 2 or (at
10  * your option) any later version. See the COPYING file in the top-level
11  * directory.
12  */
13 
14 #include "qemu/osdep.h"
15 #include "cpu.h"
16 #include "exec/memop.h"
17 #include "exec/memory-internal.h"
18 #include "qemu/error-report.h"
19 #include "sysemu/hw_accel.h"
20 #include "hw/s390x/s390-pci-inst.h"
21 #include "hw/s390x/s390-pci-bus.h"
22 #include "hw/s390x/tod.h"
23 
24 #ifndef DEBUG_S390PCI_INST
25 #define DEBUG_S390PCI_INST  0
26 #endif
27 
28 #define DPRINTF(fmt, ...)                                          \
29     do {                                                           \
30         if (DEBUG_S390PCI_INST) {                                  \
31             fprintf(stderr, "s390pci-inst: " fmt, ## __VA_ARGS__); \
32         }                                                          \
33     } while (0)
34 
35 static inline void inc_dma_avail(S390PCIIOMMU *iommu)
36 {
37     if (iommu->dma_limit) {
38         iommu->dma_limit->avail++;
39     }
40 }
41 
42 static inline void dec_dma_avail(S390PCIIOMMU *iommu)
43 {
44     if (iommu->dma_limit) {
45         iommu->dma_limit->avail--;
46     }
47 }
48 
49 static void s390_set_status_code(CPUS390XState *env,
50                                  uint8_t r, uint64_t status_code)
51 {
52     env->regs[r] &= ~0xff000000ULL;
53     env->regs[r] |= (status_code & 0xff) << 24;
54 }
55 
56 static int list_pci(ClpReqRspListPci *rrb, uint8_t *cc)
57 {
58     S390PCIBusDevice *pbdev = NULL;
59     S390pciState *s = s390_get_phb();
60     uint32_t res_code, initial_l2, g_l2;
61     int rc, i;
62     uint64_t resume_token;
63 
64     rc = 0;
65     if (lduw_p(&rrb->request.hdr.len) != 32) {
66         res_code = CLP_RC_LEN;
67         rc = -EINVAL;
68         goto out;
69     }
70 
71     if ((ldl_p(&rrb->request.fmt) & CLP_MASK_FMT) != 0) {
72         res_code = CLP_RC_FMT;
73         rc = -EINVAL;
74         goto out;
75     }
76 
77     if ((ldl_p(&rrb->request.fmt) & ~CLP_MASK_FMT) != 0 ||
78         ldq_p(&rrb->request.reserved1) != 0) {
79         res_code = CLP_RC_RESNOT0;
80         rc = -EINVAL;
81         goto out;
82     }
83 
84     resume_token = ldq_p(&rrb->request.resume_token);
85 
86     if (resume_token) {
87         pbdev = s390_pci_find_dev_by_idx(s, resume_token);
88         if (!pbdev) {
89             res_code = CLP_RC_LISTPCI_BADRT;
90             rc = -EINVAL;
91             goto out;
92         }
93     } else {
94         pbdev = s390_pci_find_next_avail_dev(s, NULL);
95     }
96 
97     if (lduw_p(&rrb->response.hdr.len) < 48) {
98         res_code = CLP_RC_8K;
99         rc = -EINVAL;
100         goto out;
101     }
102 
103     initial_l2 = lduw_p(&rrb->response.hdr.len);
104     if ((initial_l2 - LIST_PCI_HDR_LEN) % sizeof(ClpFhListEntry)
105         != 0) {
106         res_code = CLP_RC_LEN;
107         rc = -EINVAL;
108         *cc = 3;
109         goto out;
110     }
111 
112     stl_p(&rrb->response.fmt, 0);
113     stq_p(&rrb->response.reserved1, 0);
114     stl_p(&rrb->response.mdd, FH_MASK_SHM);
115     stw_p(&rrb->response.max_fn, PCI_MAX_FUNCTIONS);
116     rrb->response.flags = UID_CHECKING_ENABLED;
117     rrb->response.entry_size = sizeof(ClpFhListEntry);
118 
119     i = 0;
120     g_l2 = LIST_PCI_HDR_LEN;
121     while (g_l2 < initial_l2 && pbdev) {
122         stw_p(&rrb->response.fh_list[i].device_id,
123             pci_get_word(pbdev->pdev->config + PCI_DEVICE_ID));
124         stw_p(&rrb->response.fh_list[i].vendor_id,
125             pci_get_word(pbdev->pdev->config + PCI_VENDOR_ID));
126         /* Ignore RESERVED devices. */
127         stl_p(&rrb->response.fh_list[i].config,
128             pbdev->state == ZPCI_FS_STANDBY ? 0 : 1 << 31);
129         stl_p(&rrb->response.fh_list[i].fid, pbdev->fid);
130         stl_p(&rrb->response.fh_list[i].fh, pbdev->fh);
131 
132         g_l2 += sizeof(ClpFhListEntry);
133         /* Add endian check for DPRINTF? */
134         DPRINTF("g_l2 %d vendor id 0x%x device id 0x%x fid 0x%x fh 0x%x\n",
135                 g_l2,
136                 lduw_p(&rrb->response.fh_list[i].vendor_id),
137                 lduw_p(&rrb->response.fh_list[i].device_id),
138                 ldl_p(&rrb->response.fh_list[i].fid),
139                 ldl_p(&rrb->response.fh_list[i].fh));
140         pbdev = s390_pci_find_next_avail_dev(s, pbdev);
141         i++;
142     }
143 
144     if (!pbdev) {
145         resume_token = 0;
146     } else {
147         resume_token = pbdev->fh & FH_MASK_INDEX;
148     }
149     stq_p(&rrb->response.resume_token, resume_token);
150     stw_p(&rrb->response.hdr.len, g_l2);
151     stw_p(&rrb->response.hdr.rsp, CLP_RC_OK);
152 out:
153     if (rc) {
154         DPRINTF("list pci failed rc 0x%x\n", rc);
155         stw_p(&rrb->response.hdr.rsp, res_code);
156     }
157     return rc;
158 }
159 
160 int clp_service_call(S390CPU *cpu, uint8_t r2, uintptr_t ra)
161 {
162     ClpReqHdr *reqh;
163     ClpRspHdr *resh;
164     S390PCIBusDevice *pbdev;
165     uint32_t req_len;
166     uint32_t res_len;
167     uint8_t buffer[4096 * 2];
168     uint8_t cc = 0;
169     CPUS390XState *env = &cpu->env;
170     S390pciState *s = s390_get_phb();
171     int i;
172 
173     if (env->psw.mask & PSW_MASK_PSTATE) {
174         s390_program_interrupt(env, PGM_PRIVILEGED, ra);
175         return 0;
176     }
177 
178     if (s390_cpu_virt_mem_read(cpu, env->regs[r2], r2, buffer, sizeof(*reqh))) {
179         s390_cpu_virt_mem_handle_exc(cpu, ra);
180         return 0;
181     }
182     reqh = (ClpReqHdr *)buffer;
183     req_len = lduw_p(&reqh->len);
184     if (req_len < 16 || req_len > 8184 || (req_len % 8 != 0)) {
185         s390_program_interrupt(env, PGM_OPERAND, ra);
186         return 0;
187     }
188 
189     if (s390_cpu_virt_mem_read(cpu, env->regs[r2], r2, buffer,
190                                req_len + sizeof(*resh))) {
191         s390_cpu_virt_mem_handle_exc(cpu, ra);
192         return 0;
193     }
194     resh = (ClpRspHdr *)(buffer + req_len);
195     res_len = lduw_p(&resh->len);
196     if (res_len < 8 || res_len > 8176 || (res_len % 8 != 0)) {
197         s390_program_interrupt(env, PGM_OPERAND, ra);
198         return 0;
199     }
200     if ((req_len + res_len) > 8192) {
201         s390_program_interrupt(env, PGM_OPERAND, ra);
202         return 0;
203     }
204 
205     if (s390_cpu_virt_mem_read(cpu, env->regs[r2], r2, buffer,
206                                req_len + res_len)) {
207         s390_cpu_virt_mem_handle_exc(cpu, ra);
208         return 0;
209     }
210 
211     if (req_len != 32) {
212         stw_p(&resh->rsp, CLP_RC_LEN);
213         goto out;
214     }
215 
216     switch (lduw_p(&reqh->cmd)) {
217     case CLP_LIST_PCI: {
218         ClpReqRspListPci *rrb = (ClpReqRspListPci *)buffer;
219         list_pci(rrb, &cc);
220         break;
221     }
222     case CLP_SET_PCI_FN: {
223         ClpReqSetPci *reqsetpci = (ClpReqSetPci *)reqh;
224         ClpRspSetPci *ressetpci = (ClpRspSetPci *)resh;
225 
226         pbdev = s390_pci_find_dev_by_fh(s, ldl_p(&reqsetpci->fh));
227         if (!pbdev) {
228                 stw_p(&ressetpci->hdr.rsp, CLP_RC_SETPCIFN_FH);
229                 goto out;
230         }
231 
232         switch (reqsetpci->oc) {
233         case CLP_SET_ENABLE_PCI_FN:
234             switch (reqsetpci->ndas) {
235             case 0:
236                 stw_p(&ressetpci->hdr.rsp, CLP_RC_SETPCIFN_DMAAS);
237                 goto out;
238             case 1:
239                 break;
240             default:
241                 stw_p(&ressetpci->hdr.rsp, CLP_RC_SETPCIFN_RES);
242                 goto out;
243             }
244 
245             if (pbdev->fh & FH_MASK_ENABLE) {
246                 stw_p(&ressetpci->hdr.rsp, CLP_RC_SETPCIFN_FHOP);
247                 goto out;
248             }
249 
250             pbdev->fh |= FH_MASK_ENABLE;
251             pbdev->state = ZPCI_FS_ENABLED;
252             stl_p(&ressetpci->fh, pbdev->fh);
253             stw_p(&ressetpci->hdr.rsp, CLP_RC_OK);
254             break;
255         case CLP_SET_DISABLE_PCI_FN:
256             if (!(pbdev->fh & FH_MASK_ENABLE)) {
257                 stw_p(&ressetpci->hdr.rsp, CLP_RC_SETPCIFN_FHOP);
258                 goto out;
259             }
260             device_legacy_reset(DEVICE(pbdev));
261             pbdev->fh &= ~FH_MASK_ENABLE;
262             pbdev->state = ZPCI_FS_DISABLED;
263             stl_p(&ressetpci->fh, pbdev->fh);
264             stw_p(&ressetpci->hdr.rsp, CLP_RC_OK);
265             break;
266         default:
267             DPRINTF("unknown set pci command\n");
268             stw_p(&ressetpci->hdr.rsp, CLP_RC_SETPCIFN_FHOP);
269             break;
270         }
271         break;
272     }
273     case CLP_QUERY_PCI_FN: {
274         ClpReqQueryPci *reqquery = (ClpReqQueryPci *)reqh;
275         ClpRspQueryPci *resquery = (ClpRspQueryPci *)resh;
276 
277         pbdev = s390_pci_find_dev_by_fh(s, ldl_p(&reqquery->fh));
278         if (!pbdev) {
279             DPRINTF("query pci no pci dev\n");
280             stw_p(&resquery->hdr.rsp, CLP_RC_SETPCIFN_FH);
281             goto out;
282         }
283 
284         stq_p(&resquery->sdma, pbdev->zpci_fn.sdma);
285         stq_p(&resquery->edma, pbdev->zpci_fn.edma);
286         stw_p(&resquery->pchid, pbdev->zpci_fn.pchid);
287         resquery->flags = pbdev->zpci_fn.flags;
288         resquery->pfgid = pbdev->zpci_fn.pfgid;
289         stl_p(&resquery->fid, pbdev->zpci_fn.fid);
290         stl_p(&resquery->uid, pbdev->zpci_fn.uid);
291 
292         for (i = 0; i < PCI_BAR_COUNT; i++) {
293             uint32_t data = pci_get_long(pbdev->pdev->config +
294                 PCI_BASE_ADDRESS_0 + (i * 4));
295 
296             stl_p(&resquery->bar[i], data);
297             resquery->bar_size[i] = pbdev->pdev->io_regions[i].size ?
298                                     ctz64(pbdev->pdev->io_regions[i].size) : 0;
299             DPRINTF("bar %d addr 0x%x size 0x%" PRIx64 "barsize 0x%x\n", i,
300                     ldl_p(&resquery->bar[i]),
301                     pbdev->pdev->io_regions[i].size,
302                     resquery->bar_size[i]);
303         }
304 
305         stw_p(&resquery->hdr.rsp, CLP_RC_OK);
306         break;
307     }
308     case CLP_QUERY_PCI_FNGRP: {
309         ClpRspQueryPciGrp *resgrp = (ClpRspQueryPciGrp *)resh;
310 
311         ClpReqQueryPciGrp *reqgrp = (ClpReqQueryPciGrp *)reqh;
312         S390PCIGroup *group;
313 
314         group = s390_group_find(reqgrp->g);
315         if (!group) {
316             /* We do not allow access to unknown groups */
317             /* The group must have been obtained with a vfio device */
318             stw_p(&resgrp->hdr.rsp, CLP_RC_QUERYPCIFG_PFGID);
319             goto out;
320         }
321         resgrp->fr = group->zpci_group.fr;
322         stq_p(&resgrp->dasm, group->zpci_group.dasm);
323         stq_p(&resgrp->msia, group->zpci_group.msia);
324         stw_p(&resgrp->mui, group->zpci_group.mui);
325         stw_p(&resgrp->i, group->zpci_group.i);
326         stw_p(&resgrp->maxstbl, group->zpci_group.maxstbl);
327         resgrp->version = group->zpci_group.version;
328         stw_p(&resgrp->hdr.rsp, CLP_RC_OK);
329         break;
330     }
331     default:
332         DPRINTF("unknown clp command\n");
333         stw_p(&resh->rsp, CLP_RC_CMD);
334         break;
335     }
336 
337 out:
338     if (s390_cpu_virt_mem_write(cpu, env->regs[r2], r2, buffer,
339                                 req_len + res_len)) {
340         s390_cpu_virt_mem_handle_exc(cpu, ra);
341         return 0;
342     }
343     setcc(cpu, cc);
344     return 0;
345 }
346 
347 /**
348  * Swap data contained in s390x big endian registers to little endian
349  * PCI bars.
350  *
351  * @ptr: a pointer to a uint64_t data field
352  * @len: the length of the valid data, must be 1,2,4 or 8
353  */
354 static int zpci_endian_swap(uint64_t *ptr, uint8_t len)
355 {
356     uint64_t data = *ptr;
357 
358     switch (len) {
359     case 1:
360         break;
361     case 2:
362         data = bswap16(data);
363         break;
364     case 4:
365         data = bswap32(data);
366         break;
367     case 8:
368         data = bswap64(data);
369         break;
370     default:
371         return -EINVAL;
372     }
373     *ptr = data;
374     return 0;
375 }
376 
377 static MemoryRegion *s390_get_subregion(MemoryRegion *mr, uint64_t offset,
378                                         uint8_t len)
379 {
380     MemoryRegion *subregion;
381     uint64_t subregion_size;
382 
383     QTAILQ_FOREACH(subregion, &mr->subregions, subregions_link) {
384         subregion_size = int128_get64(subregion->size);
385         if ((offset >= subregion->addr) &&
386             (offset + len) <= (subregion->addr + subregion_size)) {
387             mr = subregion;
388             break;
389         }
390     }
391     return mr;
392 }
393 
394 static MemTxResult zpci_read_bar(S390PCIBusDevice *pbdev, uint8_t pcias,
395                                  uint64_t offset, uint64_t *data, uint8_t len)
396 {
397     MemoryRegion *mr;
398 
399     mr = pbdev->pdev->io_regions[pcias].memory;
400     mr = s390_get_subregion(mr, offset, len);
401     offset -= mr->addr;
402     return memory_region_dispatch_read(mr, offset, data,
403                                        size_memop(len) | MO_BE,
404                                        MEMTXATTRS_UNSPECIFIED);
405 }
406 
407 int pcilg_service_call(S390CPU *cpu, uint8_t r1, uint8_t r2, uintptr_t ra)
408 {
409     CPUS390XState *env = &cpu->env;
410     S390PCIBusDevice *pbdev;
411     uint64_t offset;
412     uint64_t data;
413     MemTxResult result;
414     uint8_t len;
415     uint32_t fh;
416     uint8_t pcias;
417 
418     if (env->psw.mask & PSW_MASK_PSTATE) {
419         s390_program_interrupt(env, PGM_PRIVILEGED, ra);
420         return 0;
421     }
422 
423     if (r2 & 0x1) {
424         s390_program_interrupt(env, PGM_SPECIFICATION, ra);
425         return 0;
426     }
427 
428     fh = env->regs[r2] >> 32;
429     pcias = (env->regs[r2] >> 16) & 0xf;
430     len = env->regs[r2] & 0xf;
431     offset = env->regs[r2 + 1];
432 
433     if (!(fh & FH_MASK_ENABLE)) {
434         setcc(cpu, ZPCI_PCI_LS_INVAL_HANDLE);
435         return 0;
436     }
437 
438     pbdev = s390_pci_find_dev_by_fh(s390_get_phb(), fh);
439     if (!pbdev) {
440         DPRINTF("pcilg no pci dev\n");
441         setcc(cpu, ZPCI_PCI_LS_INVAL_HANDLE);
442         return 0;
443     }
444 
445     switch (pbdev->state) {
446     case ZPCI_FS_PERMANENT_ERROR:
447     case ZPCI_FS_ERROR:
448         setcc(cpu, ZPCI_PCI_LS_ERR);
449         s390_set_status_code(env, r2, ZPCI_PCI_ST_BLOCKED);
450         return 0;
451     default:
452         break;
453     }
454 
455     switch (pcias) {
456     case ZPCI_IO_BAR_MIN...ZPCI_IO_BAR_MAX:
457         if (!len || (len > (8 - (offset & 0x7)))) {
458             s390_program_interrupt(env, PGM_OPERAND, ra);
459             return 0;
460         }
461         result = zpci_read_bar(pbdev, pcias, offset, &data, len);
462         if (result != MEMTX_OK) {
463             s390_program_interrupt(env, PGM_OPERAND, ra);
464             return 0;
465         }
466         break;
467     case ZPCI_CONFIG_BAR:
468         if (!len || (len > (4 - (offset & 0x3))) || len == 3) {
469             s390_program_interrupt(env, PGM_OPERAND, ra);
470             return 0;
471         }
472         data =  pci_host_config_read_common(
473                    pbdev->pdev, offset, pci_config_size(pbdev->pdev), len);
474 
475         if (zpci_endian_swap(&data, len)) {
476             s390_program_interrupt(env, PGM_OPERAND, ra);
477             return 0;
478         }
479         break;
480     default:
481         DPRINTF("pcilg invalid space\n");
482         setcc(cpu, ZPCI_PCI_LS_ERR);
483         s390_set_status_code(env, r2, ZPCI_PCI_ST_INVAL_AS);
484         return 0;
485     }
486 
487     pbdev->fmb.counter[ZPCI_FMB_CNT_LD]++;
488 
489     env->regs[r1] = data;
490     setcc(cpu, ZPCI_PCI_LS_OK);
491     return 0;
492 }
493 
494 static MemTxResult zpci_write_bar(S390PCIBusDevice *pbdev, uint8_t pcias,
495                                   uint64_t offset, uint64_t data, uint8_t len)
496 {
497     MemoryRegion *mr;
498 
499     mr = pbdev->pdev->io_regions[pcias].memory;
500     mr = s390_get_subregion(mr, offset, len);
501     offset -= mr->addr;
502     return memory_region_dispatch_write(mr, offset, data,
503                                         size_memop(len) | MO_BE,
504                                         MEMTXATTRS_UNSPECIFIED);
505 }
506 
507 int pcistg_service_call(S390CPU *cpu, uint8_t r1, uint8_t r2, uintptr_t ra)
508 {
509     CPUS390XState *env = &cpu->env;
510     uint64_t offset, data;
511     S390PCIBusDevice *pbdev;
512     MemTxResult result;
513     uint8_t len;
514     uint32_t fh;
515     uint8_t pcias;
516 
517     if (env->psw.mask & PSW_MASK_PSTATE) {
518         s390_program_interrupt(env, PGM_PRIVILEGED, ra);
519         return 0;
520     }
521 
522     if (r2 & 0x1) {
523         s390_program_interrupt(env, PGM_SPECIFICATION, ra);
524         return 0;
525     }
526 
527     fh = env->regs[r2] >> 32;
528     pcias = (env->regs[r2] >> 16) & 0xf;
529     len = env->regs[r2] & 0xf;
530     offset = env->regs[r2 + 1];
531     data = env->regs[r1];
532 
533     if (!(fh & FH_MASK_ENABLE)) {
534         setcc(cpu, ZPCI_PCI_LS_INVAL_HANDLE);
535         return 0;
536     }
537 
538     pbdev = s390_pci_find_dev_by_fh(s390_get_phb(), fh);
539     if (!pbdev) {
540         DPRINTF("pcistg no pci dev\n");
541         setcc(cpu, ZPCI_PCI_LS_INVAL_HANDLE);
542         return 0;
543     }
544 
545     switch (pbdev->state) {
546     /* ZPCI_FS_RESERVED, ZPCI_FS_STANDBY and ZPCI_FS_DISABLED
547      * are already covered by the FH_MASK_ENABLE check above
548      */
549     case ZPCI_FS_PERMANENT_ERROR:
550     case ZPCI_FS_ERROR:
551         setcc(cpu, ZPCI_PCI_LS_ERR);
552         s390_set_status_code(env, r2, ZPCI_PCI_ST_BLOCKED);
553         return 0;
554     default:
555         break;
556     }
557 
558     switch (pcias) {
559         /* A ZPCI PCI card may use any BAR from BAR 0 to BAR 5 */
560     case ZPCI_IO_BAR_MIN...ZPCI_IO_BAR_MAX:
561         /* Check length:
562          * A length of 0 is invalid and length should not cross a double word
563          */
564         if (!len || (len > (8 - (offset & 0x7)))) {
565             s390_program_interrupt(env, PGM_OPERAND, ra);
566             return 0;
567         }
568 
569         result = zpci_write_bar(pbdev, pcias, offset, data, len);
570         if (result != MEMTX_OK) {
571             s390_program_interrupt(env, PGM_OPERAND, ra);
572             return 0;
573         }
574         break;
575     case ZPCI_CONFIG_BAR:
576         /* ZPCI uses the pseudo BAR number 15 as configuration space */
577         /* possible access lengths are 1,2,4 and must not cross a word */
578         if (!len || (len > (4 - (offset & 0x3))) || len == 3) {
579             s390_program_interrupt(env, PGM_OPERAND, ra);
580             return 0;
581         }
582         /* len = 1,2,4 so we do not need to test */
583         zpci_endian_swap(&data, len);
584         pci_host_config_write_common(pbdev->pdev, offset,
585                                      pci_config_size(pbdev->pdev),
586                                      data, len);
587         break;
588     default:
589         DPRINTF("pcistg invalid space\n");
590         setcc(cpu, ZPCI_PCI_LS_ERR);
591         s390_set_status_code(env, r2, ZPCI_PCI_ST_INVAL_AS);
592         return 0;
593     }
594 
595     pbdev->fmb.counter[ZPCI_FMB_CNT_ST]++;
596 
597     setcc(cpu, ZPCI_PCI_LS_OK);
598     return 0;
599 }
600 
601 static uint32_t s390_pci_update_iotlb(S390PCIIOMMU *iommu,
602                                       S390IOTLBEntry *entry)
603 {
604     S390IOTLBEntry *cache = g_hash_table_lookup(iommu->iotlb, &entry->iova);
605     IOMMUTLBEvent event = {
606         .type = entry->perm ? IOMMU_NOTIFIER_MAP : IOMMU_NOTIFIER_UNMAP,
607         .entry = {
608             .target_as = &address_space_memory,
609             .iova = entry->iova,
610             .translated_addr = entry->translated_addr,
611             .perm = entry->perm,
612             .addr_mask = ~PAGE_MASK,
613         },
614     };
615 
616     if (event.type == IOMMU_NOTIFIER_UNMAP) {
617         if (!cache) {
618             goto out;
619         }
620         g_hash_table_remove(iommu->iotlb, &entry->iova);
621         inc_dma_avail(iommu);
622     } else {
623         if (cache) {
624             if (cache->perm == entry->perm &&
625                 cache->translated_addr == entry->translated_addr) {
626                 goto out;
627             }
628 
629             event.type = IOMMU_NOTIFIER_UNMAP;
630             event.entry.perm = IOMMU_NONE;
631             memory_region_notify_iommu(&iommu->iommu_mr, 0, event);
632             event.type = IOMMU_NOTIFIER_MAP;
633             event.entry.perm = entry->perm;
634         }
635 
636         cache = g_new(S390IOTLBEntry, 1);
637         cache->iova = entry->iova;
638         cache->translated_addr = entry->translated_addr;
639         cache->len = PAGE_SIZE;
640         cache->perm = entry->perm;
641         g_hash_table_replace(iommu->iotlb, &cache->iova, cache);
642         dec_dma_avail(iommu);
643     }
644 
645     memory_region_notify_iommu(&iommu->iommu_mr, 0, event);
646 
647 out:
648     return iommu->dma_limit ? iommu->dma_limit->avail : 1;
649 }
650 
651 int rpcit_service_call(S390CPU *cpu, uint8_t r1, uint8_t r2, uintptr_t ra)
652 {
653     CPUS390XState *env = &cpu->env;
654     uint32_t fh;
655     uint16_t error = 0;
656     S390PCIBusDevice *pbdev;
657     S390PCIIOMMU *iommu;
658     S390IOTLBEntry entry;
659     hwaddr start, end;
660     uint32_t dma_avail;
661 
662     if (env->psw.mask & PSW_MASK_PSTATE) {
663         s390_program_interrupt(env, PGM_PRIVILEGED, ra);
664         return 0;
665     }
666 
667     if (r2 & 0x1) {
668         s390_program_interrupt(env, PGM_SPECIFICATION, ra);
669         return 0;
670     }
671 
672     fh = env->regs[r1] >> 32;
673     start = env->regs[r2];
674     end = start + env->regs[r2 + 1];
675 
676     pbdev = s390_pci_find_dev_by_fh(s390_get_phb(), fh);
677     if (!pbdev) {
678         DPRINTF("rpcit no pci dev\n");
679         setcc(cpu, ZPCI_PCI_LS_INVAL_HANDLE);
680         return 0;
681     }
682 
683     switch (pbdev->state) {
684     case ZPCI_FS_RESERVED:
685     case ZPCI_FS_STANDBY:
686     case ZPCI_FS_DISABLED:
687     case ZPCI_FS_PERMANENT_ERROR:
688         setcc(cpu, ZPCI_PCI_LS_INVAL_HANDLE);
689         return 0;
690     case ZPCI_FS_ERROR:
691         setcc(cpu, ZPCI_PCI_LS_ERR);
692         s390_set_status_code(env, r1, ZPCI_MOD_ST_ERROR_RECOVER);
693         return 0;
694     default:
695         break;
696     }
697 
698     iommu = pbdev->iommu;
699     if (iommu->dma_limit) {
700         dma_avail = iommu->dma_limit->avail;
701     } else {
702         dma_avail = 1;
703     }
704     if (!iommu->g_iota) {
705         error = ERR_EVENT_INVALAS;
706         goto err;
707     }
708 
709     if (end < iommu->pba || start > iommu->pal) {
710         error = ERR_EVENT_OORANGE;
711         goto err;
712     }
713 
714     while (start < end) {
715         error = s390_guest_io_table_walk(iommu->g_iota, start, &entry);
716         if (error) {
717             break;
718         }
719 
720         start += entry.len;
721         while (entry.iova < start && entry.iova < end &&
722                (dma_avail > 0 || entry.perm == IOMMU_NONE)) {
723             dma_avail = s390_pci_update_iotlb(iommu, &entry);
724             entry.iova += PAGE_SIZE;
725             entry.translated_addr += PAGE_SIZE;
726         }
727     }
728 err:
729     if (error) {
730         pbdev->state = ZPCI_FS_ERROR;
731         setcc(cpu, ZPCI_PCI_LS_ERR);
732         s390_set_status_code(env, r1, ZPCI_PCI_ST_FUNC_IN_ERR);
733         s390_pci_generate_error_event(error, pbdev->fh, pbdev->fid, start, 0);
734     } else {
735         pbdev->fmb.counter[ZPCI_FMB_CNT_RPCIT]++;
736         if (dma_avail > 0) {
737             setcc(cpu, ZPCI_PCI_LS_OK);
738         } else {
739             /* vfio DMA mappings are exhausted, trigger a RPCIT */
740             setcc(cpu, ZPCI_PCI_LS_ERR);
741             s390_set_status_code(env, r1, ZPCI_RPCIT_ST_INSUFF_RES);
742         }
743     }
744     return 0;
745 }
746 
747 int pcistb_service_call(S390CPU *cpu, uint8_t r1, uint8_t r3, uint64_t gaddr,
748                         uint8_t ar, uintptr_t ra)
749 {
750     CPUS390XState *env = &cpu->env;
751     S390PCIBusDevice *pbdev;
752     MemoryRegion *mr;
753     MemTxResult result;
754     uint64_t offset;
755     int i;
756     uint32_t fh;
757     uint8_t pcias;
758     uint8_t len;
759     uint8_t buffer[128];
760 
761     if (env->psw.mask & PSW_MASK_PSTATE) {
762         s390_program_interrupt(env, PGM_PRIVILEGED, ra);
763         return 0;
764     }
765 
766     fh = env->regs[r1] >> 32;
767     pcias = (env->regs[r1] >> 16) & 0xf;
768     len = env->regs[r1] & 0xff;
769     offset = env->regs[r3];
770 
771     if (!(fh & FH_MASK_ENABLE)) {
772         setcc(cpu, ZPCI_PCI_LS_INVAL_HANDLE);
773         return 0;
774     }
775 
776     pbdev = s390_pci_find_dev_by_fh(s390_get_phb(), fh);
777     if (!pbdev) {
778         DPRINTF("pcistb no pci dev fh 0x%x\n", fh);
779         setcc(cpu, ZPCI_PCI_LS_INVAL_HANDLE);
780         return 0;
781     }
782 
783     switch (pbdev->state) {
784     case ZPCI_FS_PERMANENT_ERROR:
785     case ZPCI_FS_ERROR:
786         setcc(cpu, ZPCI_PCI_LS_ERR);
787         s390_set_status_code(env, r1, ZPCI_PCI_ST_BLOCKED);
788         return 0;
789     default:
790         break;
791     }
792 
793     if (pcias > ZPCI_IO_BAR_MAX) {
794         DPRINTF("pcistb invalid space\n");
795         setcc(cpu, ZPCI_PCI_LS_ERR);
796         s390_set_status_code(env, r1, ZPCI_PCI_ST_INVAL_AS);
797         return 0;
798     }
799 
800     /* Verify the address, offset and length */
801     /* offset must be a multiple of 8 */
802     if (offset % 8) {
803         goto specification_error;
804     }
805     /* Length must be greater than 8, a multiple of 8 */
806     /* and not greater than maxstbl */
807     if ((len <= 8) || (len % 8) ||
808         (len > pbdev->pci_group->zpci_group.maxstbl)) {
809         goto specification_error;
810     }
811     /* Do not cross a 4K-byte boundary */
812     if (((offset & 0xfff) + len) > 0x1000) {
813         goto specification_error;
814     }
815     /* Guest address must be double word aligned */
816     if (gaddr & 0x07UL) {
817         goto specification_error;
818     }
819 
820     mr = pbdev->pdev->io_regions[pcias].memory;
821     mr = s390_get_subregion(mr, offset, len);
822     offset -= mr->addr;
823 
824     if (!memory_region_access_valid(mr, offset, len, true,
825                                     MEMTXATTRS_UNSPECIFIED)) {
826         s390_program_interrupt(env, PGM_OPERAND, ra);
827         return 0;
828     }
829 
830     if (s390_cpu_virt_mem_read(cpu, gaddr, ar, buffer, len)) {
831         s390_cpu_virt_mem_handle_exc(cpu, ra);
832         return 0;
833     }
834 
835     for (i = 0; i < len / 8; i++) {
836         result = memory_region_dispatch_write(mr, offset + i * 8,
837                                               ldq_p(buffer + i * 8),
838                                               MO_64, MEMTXATTRS_UNSPECIFIED);
839         if (result != MEMTX_OK) {
840             s390_program_interrupt(env, PGM_OPERAND, ra);
841             return 0;
842         }
843     }
844 
845     pbdev->fmb.counter[ZPCI_FMB_CNT_STB]++;
846 
847     setcc(cpu, ZPCI_PCI_LS_OK);
848     return 0;
849 
850 specification_error:
851     s390_program_interrupt(env, PGM_SPECIFICATION, ra);
852     return 0;
853 }
854 
855 static int reg_irqs(CPUS390XState *env, S390PCIBusDevice *pbdev, ZpciFib fib)
856 {
857     int ret, len;
858     uint8_t isc = FIB_DATA_ISC(ldl_p(&fib.data));
859 
860     pbdev->routes.adapter.adapter_id = css_get_adapter_id(
861                                        CSS_IO_ADAPTER_PCI, isc);
862     pbdev->summary_ind = get_indicator(ldq_p(&fib.aisb), sizeof(uint64_t));
863     len = BITS_TO_LONGS(FIB_DATA_NOI(ldl_p(&fib.data))) * sizeof(unsigned long);
864     pbdev->indicator = get_indicator(ldq_p(&fib.aibv), len);
865 
866     ret = map_indicator(&pbdev->routes.adapter, pbdev->summary_ind);
867     if (ret) {
868         goto out;
869     }
870 
871     ret = map_indicator(&pbdev->routes.adapter, pbdev->indicator);
872     if (ret) {
873         goto out;
874     }
875 
876     pbdev->routes.adapter.summary_addr = ldq_p(&fib.aisb);
877     pbdev->routes.adapter.summary_offset = FIB_DATA_AISBO(ldl_p(&fib.data));
878     pbdev->routes.adapter.ind_addr = ldq_p(&fib.aibv);
879     pbdev->routes.adapter.ind_offset = FIB_DATA_AIBVO(ldl_p(&fib.data));
880     pbdev->isc = isc;
881     pbdev->noi = FIB_DATA_NOI(ldl_p(&fib.data));
882     pbdev->sum = FIB_DATA_SUM(ldl_p(&fib.data));
883 
884     DPRINTF("reg_irqs adapter id %d\n", pbdev->routes.adapter.adapter_id);
885     return 0;
886 out:
887     release_indicator(&pbdev->routes.adapter, pbdev->summary_ind);
888     release_indicator(&pbdev->routes.adapter, pbdev->indicator);
889     pbdev->summary_ind = NULL;
890     pbdev->indicator = NULL;
891     return ret;
892 }
893 
894 int pci_dereg_irqs(S390PCIBusDevice *pbdev)
895 {
896     release_indicator(&pbdev->routes.adapter, pbdev->summary_ind);
897     release_indicator(&pbdev->routes.adapter, pbdev->indicator);
898 
899     pbdev->summary_ind = NULL;
900     pbdev->indicator = NULL;
901     pbdev->routes.adapter.summary_addr = 0;
902     pbdev->routes.adapter.summary_offset = 0;
903     pbdev->routes.adapter.ind_addr = 0;
904     pbdev->routes.adapter.ind_offset = 0;
905     pbdev->isc = 0;
906     pbdev->noi = 0;
907     pbdev->sum = 0;
908 
909     DPRINTF("dereg_irqs adapter id %d\n", pbdev->routes.adapter.adapter_id);
910     return 0;
911 }
912 
913 static int reg_ioat(CPUS390XState *env, S390PCIIOMMU *iommu, ZpciFib fib,
914                     uintptr_t ra)
915 {
916     uint64_t pba = ldq_p(&fib.pba);
917     uint64_t pal = ldq_p(&fib.pal);
918     uint64_t g_iota = ldq_p(&fib.iota);
919     uint8_t dt = (g_iota >> 2) & 0x7;
920     uint8_t t = (g_iota >> 11) & 0x1;
921 
922     pba &= ~0xfff;
923     pal |= 0xfff;
924     if (pba > pal || pba < ZPCI_SDMA_ADDR || pal > ZPCI_EDMA_ADDR) {
925         s390_program_interrupt(env, PGM_OPERAND, ra);
926         return -EINVAL;
927     }
928 
929     /* currently we only support designation type 1 with translation */
930     if (!(dt == ZPCI_IOTA_RTTO && t)) {
931         error_report("unsupported ioat dt %d t %d", dt, t);
932         s390_program_interrupt(env, PGM_OPERAND, ra);
933         return -EINVAL;
934     }
935 
936     iommu->pba = pba;
937     iommu->pal = pal;
938     iommu->g_iota = g_iota;
939 
940     s390_pci_iommu_enable(iommu);
941 
942     return 0;
943 }
944 
945 void pci_dereg_ioat(S390PCIIOMMU *iommu)
946 {
947     s390_pci_iommu_disable(iommu);
948     iommu->pba = 0;
949     iommu->pal = 0;
950     iommu->g_iota = 0;
951 }
952 
953 void fmb_timer_free(S390PCIBusDevice *pbdev)
954 {
955     if (pbdev->fmb_timer) {
956         timer_del(pbdev->fmb_timer);
957         timer_free(pbdev->fmb_timer);
958         pbdev->fmb_timer = NULL;
959     }
960     pbdev->fmb_addr = 0;
961     memset(&pbdev->fmb, 0, sizeof(ZpciFmb));
962 }
963 
964 static int fmb_do_update(S390PCIBusDevice *pbdev, int offset, uint64_t val,
965                          int len)
966 {
967     MemTxResult ret;
968     uint64_t dst = pbdev->fmb_addr + offset;
969 
970     switch (len) {
971     case 8:
972         address_space_stq_be(&address_space_memory, dst, val,
973                              MEMTXATTRS_UNSPECIFIED,
974                              &ret);
975         break;
976     case 4:
977         address_space_stl_be(&address_space_memory, dst, val,
978                              MEMTXATTRS_UNSPECIFIED,
979                              &ret);
980         break;
981     case 2:
982         address_space_stw_be(&address_space_memory, dst, val,
983                              MEMTXATTRS_UNSPECIFIED,
984                              &ret);
985         break;
986     case 1:
987         address_space_stb(&address_space_memory, dst, val,
988                           MEMTXATTRS_UNSPECIFIED,
989                           &ret);
990         break;
991     default:
992         ret = MEMTX_ERROR;
993         break;
994     }
995     if (ret != MEMTX_OK) {
996         s390_pci_generate_error_event(ERR_EVENT_FMBA, pbdev->fh, pbdev->fid,
997                                       pbdev->fmb_addr, 0);
998         fmb_timer_free(pbdev);
999     }
1000 
1001     return ret;
1002 }
1003 
1004 static void fmb_update(void *opaque)
1005 {
1006     S390PCIBusDevice *pbdev = opaque;
1007     int64_t t = qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL);
1008     int i;
1009 
1010     /* Update U bit */
1011     pbdev->fmb.last_update *= 2;
1012     pbdev->fmb.last_update |= UPDATE_U_BIT;
1013     if (fmb_do_update(pbdev, offsetof(ZpciFmb, last_update),
1014                       pbdev->fmb.last_update,
1015                       sizeof(pbdev->fmb.last_update))) {
1016         return;
1017     }
1018 
1019     /* Update FMB sample count */
1020     if (fmb_do_update(pbdev, offsetof(ZpciFmb, sample),
1021                       pbdev->fmb.sample++,
1022                       sizeof(pbdev->fmb.sample))) {
1023         return;
1024     }
1025 
1026     /* Update FMB counters */
1027     for (i = 0; i < ZPCI_FMB_CNT_MAX; i++) {
1028         if (fmb_do_update(pbdev, offsetof(ZpciFmb, counter[i]),
1029                           pbdev->fmb.counter[i],
1030                           sizeof(pbdev->fmb.counter[0]))) {
1031             return;
1032         }
1033     }
1034 
1035     /* Clear U bit and update the time */
1036     pbdev->fmb.last_update = time2tod(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL));
1037     pbdev->fmb.last_update *= 2;
1038     if (fmb_do_update(pbdev, offsetof(ZpciFmb, last_update),
1039                       pbdev->fmb.last_update,
1040                       sizeof(pbdev->fmb.last_update))) {
1041         return;
1042     }
1043     timer_mod(pbdev->fmb_timer, t + DEFAULT_MUI);
1044 }
1045 
1046 int mpcifc_service_call(S390CPU *cpu, uint8_t r1, uint64_t fiba, uint8_t ar,
1047                         uintptr_t ra)
1048 {
1049     CPUS390XState *env = &cpu->env;
1050     uint8_t oc, dmaas;
1051     uint32_t fh;
1052     ZpciFib fib;
1053     S390PCIBusDevice *pbdev;
1054     uint64_t cc = ZPCI_PCI_LS_OK;
1055 
1056     if (env->psw.mask & PSW_MASK_PSTATE) {
1057         s390_program_interrupt(env, PGM_PRIVILEGED, ra);
1058         return 0;
1059     }
1060 
1061     oc = env->regs[r1] & 0xff;
1062     dmaas = (env->regs[r1] >> 16) & 0xff;
1063     fh = env->regs[r1] >> 32;
1064 
1065     if (fiba & 0x7) {
1066         s390_program_interrupt(env, PGM_SPECIFICATION, ra);
1067         return 0;
1068     }
1069 
1070     pbdev = s390_pci_find_dev_by_fh(s390_get_phb(), fh);
1071     if (!pbdev) {
1072         DPRINTF("mpcifc no pci dev fh 0x%x\n", fh);
1073         setcc(cpu, ZPCI_PCI_LS_INVAL_HANDLE);
1074         return 0;
1075     }
1076 
1077     switch (pbdev->state) {
1078     case ZPCI_FS_RESERVED:
1079     case ZPCI_FS_STANDBY:
1080     case ZPCI_FS_DISABLED:
1081     case ZPCI_FS_PERMANENT_ERROR:
1082         setcc(cpu, ZPCI_PCI_LS_INVAL_HANDLE);
1083         return 0;
1084     default:
1085         break;
1086     }
1087 
1088     if (s390_cpu_virt_mem_read(cpu, fiba, ar, (uint8_t *)&fib, sizeof(fib))) {
1089         s390_cpu_virt_mem_handle_exc(cpu, ra);
1090         return 0;
1091     }
1092 
1093     if (fib.fmt != 0) {
1094         s390_program_interrupt(env, PGM_OPERAND, ra);
1095         return 0;
1096     }
1097 
1098     switch (oc) {
1099     case ZPCI_MOD_FC_REG_INT:
1100         if (pbdev->summary_ind) {
1101             cc = ZPCI_PCI_LS_ERR;
1102             s390_set_status_code(env, r1, ZPCI_MOD_ST_SEQUENCE);
1103         } else if (reg_irqs(env, pbdev, fib)) {
1104             cc = ZPCI_PCI_LS_ERR;
1105             s390_set_status_code(env, r1, ZPCI_MOD_ST_RES_NOT_AVAIL);
1106         }
1107         break;
1108     case ZPCI_MOD_FC_DEREG_INT:
1109         if (!pbdev->summary_ind) {
1110             cc = ZPCI_PCI_LS_ERR;
1111             s390_set_status_code(env, r1, ZPCI_MOD_ST_SEQUENCE);
1112         } else {
1113             pci_dereg_irqs(pbdev);
1114         }
1115         break;
1116     case ZPCI_MOD_FC_REG_IOAT:
1117         if (dmaas != 0) {
1118             cc = ZPCI_PCI_LS_ERR;
1119             s390_set_status_code(env, r1, ZPCI_MOD_ST_DMAAS_INVAL);
1120         } else if (pbdev->iommu->enabled) {
1121             cc = ZPCI_PCI_LS_ERR;
1122             s390_set_status_code(env, r1, ZPCI_MOD_ST_SEQUENCE);
1123         } else if (reg_ioat(env, pbdev->iommu, fib, ra)) {
1124             cc = ZPCI_PCI_LS_ERR;
1125             s390_set_status_code(env, r1, ZPCI_MOD_ST_INSUF_RES);
1126         }
1127         break;
1128     case ZPCI_MOD_FC_DEREG_IOAT:
1129         if (dmaas != 0) {
1130             cc = ZPCI_PCI_LS_ERR;
1131             s390_set_status_code(env, r1, ZPCI_MOD_ST_DMAAS_INVAL);
1132         } else if (!pbdev->iommu->enabled) {
1133             cc = ZPCI_PCI_LS_ERR;
1134             s390_set_status_code(env, r1, ZPCI_MOD_ST_SEQUENCE);
1135         } else {
1136             pci_dereg_ioat(pbdev->iommu);
1137         }
1138         break;
1139     case ZPCI_MOD_FC_REREG_IOAT:
1140         if (dmaas != 0) {
1141             cc = ZPCI_PCI_LS_ERR;
1142             s390_set_status_code(env, r1, ZPCI_MOD_ST_DMAAS_INVAL);
1143         } else if (!pbdev->iommu->enabled) {
1144             cc = ZPCI_PCI_LS_ERR;
1145             s390_set_status_code(env, r1, ZPCI_MOD_ST_SEQUENCE);
1146         } else {
1147             pci_dereg_ioat(pbdev->iommu);
1148             if (reg_ioat(env, pbdev->iommu, fib, ra)) {
1149                 cc = ZPCI_PCI_LS_ERR;
1150                 s390_set_status_code(env, r1, ZPCI_MOD_ST_INSUF_RES);
1151             }
1152         }
1153         break;
1154     case ZPCI_MOD_FC_RESET_ERROR:
1155         switch (pbdev->state) {
1156         case ZPCI_FS_BLOCKED:
1157         case ZPCI_FS_ERROR:
1158             pbdev->state = ZPCI_FS_ENABLED;
1159             break;
1160         default:
1161             cc = ZPCI_PCI_LS_ERR;
1162             s390_set_status_code(env, r1, ZPCI_MOD_ST_SEQUENCE);
1163         }
1164         break;
1165     case ZPCI_MOD_FC_RESET_BLOCK:
1166         switch (pbdev->state) {
1167         case ZPCI_FS_ERROR:
1168             pbdev->state = ZPCI_FS_BLOCKED;
1169             break;
1170         default:
1171             cc = ZPCI_PCI_LS_ERR;
1172             s390_set_status_code(env, r1, ZPCI_MOD_ST_SEQUENCE);
1173         }
1174         break;
1175     case ZPCI_MOD_FC_SET_MEASURE: {
1176         uint64_t fmb_addr = ldq_p(&fib.fmb_addr);
1177 
1178         if (fmb_addr & FMBK_MASK) {
1179             cc = ZPCI_PCI_LS_ERR;
1180             s390_pci_generate_error_event(ERR_EVENT_FMBPRO, pbdev->fh,
1181                                           pbdev->fid, fmb_addr, 0);
1182             fmb_timer_free(pbdev);
1183             break;
1184         }
1185 
1186         if (!fmb_addr) {
1187             /* Stop updating FMB. */
1188             fmb_timer_free(pbdev);
1189             break;
1190         }
1191 
1192         if (!pbdev->fmb_timer) {
1193             pbdev->fmb_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL,
1194                                             fmb_update, pbdev);
1195         } else if (timer_pending(pbdev->fmb_timer)) {
1196             /* Remove pending timer to update FMB address. */
1197             timer_del(pbdev->fmb_timer);
1198         }
1199         pbdev->fmb_addr = fmb_addr;
1200         timer_mod(pbdev->fmb_timer,
1201                   qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + DEFAULT_MUI);
1202         break;
1203     }
1204     default:
1205         s390_program_interrupt(&cpu->env, PGM_OPERAND, ra);
1206         cc = ZPCI_PCI_LS_ERR;
1207     }
1208 
1209     setcc(cpu, cc);
1210     return 0;
1211 }
1212 
1213 int stpcifc_service_call(S390CPU *cpu, uint8_t r1, uint64_t fiba, uint8_t ar,
1214                          uintptr_t ra)
1215 {
1216     CPUS390XState *env = &cpu->env;
1217     uint8_t dmaas;
1218     uint32_t fh;
1219     ZpciFib fib;
1220     S390PCIBusDevice *pbdev;
1221     uint32_t data;
1222     uint64_t cc = ZPCI_PCI_LS_OK;
1223 
1224     if (env->psw.mask & PSW_MASK_PSTATE) {
1225         s390_program_interrupt(env, PGM_PRIVILEGED, ra);
1226         return 0;
1227     }
1228 
1229     fh = env->regs[r1] >> 32;
1230     dmaas = (env->regs[r1] >> 16) & 0xff;
1231 
1232     if (dmaas) {
1233         setcc(cpu, ZPCI_PCI_LS_ERR);
1234         s390_set_status_code(env, r1, ZPCI_STPCIFC_ST_INVAL_DMAAS);
1235         return 0;
1236     }
1237 
1238     if (fiba & 0x7) {
1239         s390_program_interrupt(env, PGM_SPECIFICATION, ra);
1240         return 0;
1241     }
1242 
1243     pbdev = s390_pci_find_dev_by_idx(s390_get_phb(), fh & FH_MASK_INDEX);
1244     if (!pbdev) {
1245         setcc(cpu, ZPCI_PCI_LS_INVAL_HANDLE);
1246         return 0;
1247     }
1248 
1249     memset(&fib, 0, sizeof(fib));
1250 
1251     switch (pbdev->state) {
1252     case ZPCI_FS_RESERVED:
1253     case ZPCI_FS_STANDBY:
1254         setcc(cpu, ZPCI_PCI_LS_INVAL_HANDLE);
1255         return 0;
1256     case ZPCI_FS_DISABLED:
1257         if (fh & FH_MASK_ENABLE) {
1258             setcc(cpu, ZPCI_PCI_LS_INVAL_HANDLE);
1259             return 0;
1260         }
1261         goto out;
1262     /* BLOCKED bit is set to one coincident with the setting of ERROR bit.
1263      * FH Enabled bit is set to one in states of ENABLED, BLOCKED or ERROR. */
1264     case ZPCI_FS_ERROR:
1265         fib.fc |= 0x20;
1266         /* fallthrough */
1267     case ZPCI_FS_BLOCKED:
1268         fib.fc |= 0x40;
1269         /* fallthrough */
1270     case ZPCI_FS_ENABLED:
1271         fib.fc |= 0x80;
1272         if (pbdev->iommu->enabled) {
1273             fib.fc |= 0x10;
1274         }
1275         if (!(fh & FH_MASK_ENABLE)) {
1276             env->regs[r1] |= 1ULL << 63;
1277         }
1278         break;
1279     case ZPCI_FS_PERMANENT_ERROR:
1280         setcc(cpu, ZPCI_PCI_LS_ERR);
1281         s390_set_status_code(env, r1, ZPCI_STPCIFC_ST_PERM_ERROR);
1282         return 0;
1283     }
1284 
1285     stq_p(&fib.pba, pbdev->iommu->pba);
1286     stq_p(&fib.pal, pbdev->iommu->pal);
1287     stq_p(&fib.iota, pbdev->iommu->g_iota);
1288     stq_p(&fib.aibv, pbdev->routes.adapter.ind_addr);
1289     stq_p(&fib.aisb, pbdev->routes.adapter.summary_addr);
1290     stq_p(&fib.fmb_addr, pbdev->fmb_addr);
1291 
1292     data = ((uint32_t)pbdev->isc << 28) | ((uint32_t)pbdev->noi << 16) |
1293            ((uint32_t)pbdev->routes.adapter.ind_offset << 8) |
1294            ((uint32_t)pbdev->sum << 7) | pbdev->routes.adapter.summary_offset;
1295     stl_p(&fib.data, data);
1296 
1297 out:
1298     if (s390_cpu_virt_mem_write(cpu, fiba, ar, (uint8_t *)&fib, sizeof(fib))) {
1299         s390_cpu_virt_mem_handle_exc(cpu, ra);
1300         return 0;
1301     }
1302 
1303     setcc(cpu, cc);
1304     return 0;
1305 }
1306