xref: /openbmc/qemu/hw/s390x/s390-pci-inst.c (revision 3b9bd3f4)
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     uint16_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] & 0x1fff;
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     for (i = 0; i < len; i += 8) {
825         if (!memory_region_access_valid(mr, offset + i, 8, true,
826                                         MEMTXATTRS_UNSPECIFIED)) {
827             s390_program_interrupt(env, PGM_OPERAND, ra);
828             return 0;
829         }
830     }
831 
832     if (s390_cpu_virt_mem_read(cpu, gaddr, ar, buffer, len)) {
833         s390_cpu_virt_mem_handle_exc(cpu, ra);
834         return 0;
835     }
836 
837     for (i = 0; i < len / 8; i++) {
838         result = memory_region_dispatch_write(mr, offset + i * 8,
839                                               ldq_p(buffer + i * 8),
840                                               MO_64, MEMTXATTRS_UNSPECIFIED);
841         if (result != MEMTX_OK) {
842             s390_program_interrupt(env, PGM_OPERAND, ra);
843             return 0;
844         }
845     }
846 
847     pbdev->fmb.counter[ZPCI_FMB_CNT_STB]++;
848 
849     setcc(cpu, ZPCI_PCI_LS_OK);
850     return 0;
851 
852 specification_error:
853     s390_program_interrupt(env, PGM_SPECIFICATION, ra);
854     return 0;
855 }
856 
857 static int reg_irqs(CPUS390XState *env, S390PCIBusDevice *pbdev, ZpciFib fib)
858 {
859     int ret, len;
860     uint8_t isc = FIB_DATA_ISC(ldl_p(&fib.data));
861 
862     pbdev->routes.adapter.adapter_id = css_get_adapter_id(
863                                        CSS_IO_ADAPTER_PCI, isc);
864     pbdev->summary_ind = get_indicator(ldq_p(&fib.aisb), sizeof(uint64_t));
865     len = BITS_TO_LONGS(FIB_DATA_NOI(ldl_p(&fib.data))) * sizeof(unsigned long);
866     pbdev->indicator = get_indicator(ldq_p(&fib.aibv), len);
867 
868     ret = map_indicator(&pbdev->routes.adapter, pbdev->summary_ind);
869     if (ret) {
870         goto out;
871     }
872 
873     ret = map_indicator(&pbdev->routes.adapter, pbdev->indicator);
874     if (ret) {
875         goto out;
876     }
877 
878     pbdev->routes.adapter.summary_addr = ldq_p(&fib.aisb);
879     pbdev->routes.adapter.summary_offset = FIB_DATA_AISBO(ldl_p(&fib.data));
880     pbdev->routes.adapter.ind_addr = ldq_p(&fib.aibv);
881     pbdev->routes.adapter.ind_offset = FIB_DATA_AIBVO(ldl_p(&fib.data));
882     pbdev->isc = isc;
883     pbdev->noi = FIB_DATA_NOI(ldl_p(&fib.data));
884     pbdev->sum = FIB_DATA_SUM(ldl_p(&fib.data));
885 
886     DPRINTF("reg_irqs adapter id %d\n", pbdev->routes.adapter.adapter_id);
887     return 0;
888 out:
889     release_indicator(&pbdev->routes.adapter, pbdev->summary_ind);
890     release_indicator(&pbdev->routes.adapter, pbdev->indicator);
891     pbdev->summary_ind = NULL;
892     pbdev->indicator = NULL;
893     return ret;
894 }
895 
896 int pci_dereg_irqs(S390PCIBusDevice *pbdev)
897 {
898     release_indicator(&pbdev->routes.adapter, pbdev->summary_ind);
899     release_indicator(&pbdev->routes.adapter, pbdev->indicator);
900 
901     pbdev->summary_ind = NULL;
902     pbdev->indicator = NULL;
903     pbdev->routes.adapter.summary_addr = 0;
904     pbdev->routes.adapter.summary_offset = 0;
905     pbdev->routes.adapter.ind_addr = 0;
906     pbdev->routes.adapter.ind_offset = 0;
907     pbdev->isc = 0;
908     pbdev->noi = 0;
909     pbdev->sum = 0;
910 
911     DPRINTF("dereg_irqs adapter id %d\n", pbdev->routes.adapter.adapter_id);
912     return 0;
913 }
914 
915 static int reg_ioat(CPUS390XState *env, S390PCIIOMMU *iommu, ZpciFib fib,
916                     uintptr_t ra)
917 {
918     uint64_t pba = ldq_p(&fib.pba);
919     uint64_t pal = ldq_p(&fib.pal);
920     uint64_t g_iota = ldq_p(&fib.iota);
921     uint8_t dt = (g_iota >> 2) & 0x7;
922     uint8_t t = (g_iota >> 11) & 0x1;
923 
924     pba &= ~0xfff;
925     pal |= 0xfff;
926     if (pba > pal || pba < ZPCI_SDMA_ADDR || pal > ZPCI_EDMA_ADDR) {
927         s390_program_interrupt(env, PGM_OPERAND, ra);
928         return -EINVAL;
929     }
930 
931     /* currently we only support designation type 1 with translation */
932     if (!(dt == ZPCI_IOTA_RTTO && t)) {
933         error_report("unsupported ioat dt %d t %d", dt, t);
934         s390_program_interrupt(env, PGM_OPERAND, ra);
935         return -EINVAL;
936     }
937 
938     iommu->pba = pba;
939     iommu->pal = pal;
940     iommu->g_iota = g_iota;
941 
942     s390_pci_iommu_enable(iommu);
943 
944     return 0;
945 }
946 
947 void pci_dereg_ioat(S390PCIIOMMU *iommu)
948 {
949     s390_pci_iommu_disable(iommu);
950     iommu->pba = 0;
951     iommu->pal = 0;
952     iommu->g_iota = 0;
953 }
954 
955 void fmb_timer_free(S390PCIBusDevice *pbdev)
956 {
957     if (pbdev->fmb_timer) {
958         timer_del(pbdev->fmb_timer);
959         timer_free(pbdev->fmb_timer);
960         pbdev->fmb_timer = NULL;
961     }
962     pbdev->fmb_addr = 0;
963     memset(&pbdev->fmb, 0, sizeof(ZpciFmb));
964 }
965 
966 static int fmb_do_update(S390PCIBusDevice *pbdev, int offset, uint64_t val,
967                          int len)
968 {
969     MemTxResult ret;
970     uint64_t dst = pbdev->fmb_addr + offset;
971 
972     switch (len) {
973     case 8:
974         address_space_stq_be(&address_space_memory, dst, val,
975                              MEMTXATTRS_UNSPECIFIED,
976                              &ret);
977         break;
978     case 4:
979         address_space_stl_be(&address_space_memory, dst, val,
980                              MEMTXATTRS_UNSPECIFIED,
981                              &ret);
982         break;
983     case 2:
984         address_space_stw_be(&address_space_memory, dst, val,
985                              MEMTXATTRS_UNSPECIFIED,
986                              &ret);
987         break;
988     case 1:
989         address_space_stb(&address_space_memory, dst, val,
990                           MEMTXATTRS_UNSPECIFIED,
991                           &ret);
992         break;
993     default:
994         ret = MEMTX_ERROR;
995         break;
996     }
997     if (ret != MEMTX_OK) {
998         s390_pci_generate_error_event(ERR_EVENT_FMBA, pbdev->fh, pbdev->fid,
999                                       pbdev->fmb_addr, 0);
1000         fmb_timer_free(pbdev);
1001     }
1002 
1003     return ret;
1004 }
1005 
1006 static void fmb_update(void *opaque)
1007 {
1008     S390PCIBusDevice *pbdev = opaque;
1009     int64_t t = qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL);
1010     int i;
1011 
1012     /* Update U bit */
1013     pbdev->fmb.last_update *= 2;
1014     pbdev->fmb.last_update |= UPDATE_U_BIT;
1015     if (fmb_do_update(pbdev, offsetof(ZpciFmb, last_update),
1016                       pbdev->fmb.last_update,
1017                       sizeof(pbdev->fmb.last_update))) {
1018         return;
1019     }
1020 
1021     /* Update FMB sample count */
1022     if (fmb_do_update(pbdev, offsetof(ZpciFmb, sample),
1023                       pbdev->fmb.sample++,
1024                       sizeof(pbdev->fmb.sample))) {
1025         return;
1026     }
1027 
1028     /* Update FMB counters */
1029     for (i = 0; i < ZPCI_FMB_CNT_MAX; i++) {
1030         if (fmb_do_update(pbdev, offsetof(ZpciFmb, counter[i]),
1031                           pbdev->fmb.counter[i],
1032                           sizeof(pbdev->fmb.counter[0]))) {
1033             return;
1034         }
1035     }
1036 
1037     /* Clear U bit and update the time */
1038     pbdev->fmb.last_update = time2tod(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL));
1039     pbdev->fmb.last_update *= 2;
1040     if (fmb_do_update(pbdev, offsetof(ZpciFmb, last_update),
1041                       pbdev->fmb.last_update,
1042                       sizeof(pbdev->fmb.last_update))) {
1043         return;
1044     }
1045     timer_mod(pbdev->fmb_timer, t + DEFAULT_MUI);
1046 }
1047 
1048 int mpcifc_service_call(S390CPU *cpu, uint8_t r1, uint64_t fiba, uint8_t ar,
1049                         uintptr_t ra)
1050 {
1051     CPUS390XState *env = &cpu->env;
1052     uint8_t oc, dmaas;
1053     uint32_t fh;
1054     ZpciFib fib;
1055     S390PCIBusDevice *pbdev;
1056     uint64_t cc = ZPCI_PCI_LS_OK;
1057 
1058     if (env->psw.mask & PSW_MASK_PSTATE) {
1059         s390_program_interrupt(env, PGM_PRIVILEGED, ra);
1060         return 0;
1061     }
1062 
1063     oc = env->regs[r1] & 0xff;
1064     dmaas = (env->regs[r1] >> 16) & 0xff;
1065     fh = env->regs[r1] >> 32;
1066 
1067     if (fiba & 0x7) {
1068         s390_program_interrupt(env, PGM_SPECIFICATION, ra);
1069         return 0;
1070     }
1071 
1072     pbdev = s390_pci_find_dev_by_fh(s390_get_phb(), fh);
1073     if (!pbdev) {
1074         DPRINTF("mpcifc no pci dev fh 0x%x\n", fh);
1075         setcc(cpu, ZPCI_PCI_LS_INVAL_HANDLE);
1076         return 0;
1077     }
1078 
1079     switch (pbdev->state) {
1080     case ZPCI_FS_RESERVED:
1081     case ZPCI_FS_STANDBY:
1082     case ZPCI_FS_DISABLED:
1083     case ZPCI_FS_PERMANENT_ERROR:
1084         setcc(cpu, ZPCI_PCI_LS_INVAL_HANDLE);
1085         return 0;
1086     default:
1087         break;
1088     }
1089 
1090     if (s390_cpu_virt_mem_read(cpu, fiba, ar, (uint8_t *)&fib, sizeof(fib))) {
1091         s390_cpu_virt_mem_handle_exc(cpu, ra);
1092         return 0;
1093     }
1094 
1095     if (fib.fmt != 0) {
1096         s390_program_interrupt(env, PGM_OPERAND, ra);
1097         return 0;
1098     }
1099 
1100     switch (oc) {
1101     case ZPCI_MOD_FC_REG_INT:
1102         if (pbdev->summary_ind) {
1103             cc = ZPCI_PCI_LS_ERR;
1104             s390_set_status_code(env, r1, ZPCI_MOD_ST_SEQUENCE);
1105         } else if (reg_irqs(env, pbdev, fib)) {
1106             cc = ZPCI_PCI_LS_ERR;
1107             s390_set_status_code(env, r1, ZPCI_MOD_ST_RES_NOT_AVAIL);
1108         }
1109         break;
1110     case ZPCI_MOD_FC_DEREG_INT:
1111         if (!pbdev->summary_ind) {
1112             cc = ZPCI_PCI_LS_ERR;
1113             s390_set_status_code(env, r1, ZPCI_MOD_ST_SEQUENCE);
1114         } else {
1115             pci_dereg_irqs(pbdev);
1116         }
1117         break;
1118     case ZPCI_MOD_FC_REG_IOAT:
1119         if (dmaas != 0) {
1120             cc = ZPCI_PCI_LS_ERR;
1121             s390_set_status_code(env, r1, ZPCI_MOD_ST_DMAAS_INVAL);
1122         } else if (pbdev->iommu->enabled) {
1123             cc = ZPCI_PCI_LS_ERR;
1124             s390_set_status_code(env, r1, ZPCI_MOD_ST_SEQUENCE);
1125         } else if (reg_ioat(env, pbdev->iommu, fib, ra)) {
1126             cc = ZPCI_PCI_LS_ERR;
1127             s390_set_status_code(env, r1, ZPCI_MOD_ST_INSUF_RES);
1128         }
1129         break;
1130     case ZPCI_MOD_FC_DEREG_IOAT:
1131         if (dmaas != 0) {
1132             cc = ZPCI_PCI_LS_ERR;
1133             s390_set_status_code(env, r1, ZPCI_MOD_ST_DMAAS_INVAL);
1134         } else if (!pbdev->iommu->enabled) {
1135             cc = ZPCI_PCI_LS_ERR;
1136             s390_set_status_code(env, r1, ZPCI_MOD_ST_SEQUENCE);
1137         } else {
1138             pci_dereg_ioat(pbdev->iommu);
1139         }
1140         break;
1141     case ZPCI_MOD_FC_REREG_IOAT:
1142         if (dmaas != 0) {
1143             cc = ZPCI_PCI_LS_ERR;
1144             s390_set_status_code(env, r1, ZPCI_MOD_ST_DMAAS_INVAL);
1145         } else if (!pbdev->iommu->enabled) {
1146             cc = ZPCI_PCI_LS_ERR;
1147             s390_set_status_code(env, r1, ZPCI_MOD_ST_SEQUENCE);
1148         } else {
1149             pci_dereg_ioat(pbdev->iommu);
1150             if (reg_ioat(env, pbdev->iommu, fib, ra)) {
1151                 cc = ZPCI_PCI_LS_ERR;
1152                 s390_set_status_code(env, r1, ZPCI_MOD_ST_INSUF_RES);
1153             }
1154         }
1155         break;
1156     case ZPCI_MOD_FC_RESET_ERROR:
1157         switch (pbdev->state) {
1158         case ZPCI_FS_BLOCKED:
1159         case ZPCI_FS_ERROR:
1160             pbdev->state = ZPCI_FS_ENABLED;
1161             break;
1162         default:
1163             cc = ZPCI_PCI_LS_ERR;
1164             s390_set_status_code(env, r1, ZPCI_MOD_ST_SEQUENCE);
1165         }
1166         break;
1167     case ZPCI_MOD_FC_RESET_BLOCK:
1168         switch (pbdev->state) {
1169         case ZPCI_FS_ERROR:
1170             pbdev->state = ZPCI_FS_BLOCKED;
1171             break;
1172         default:
1173             cc = ZPCI_PCI_LS_ERR;
1174             s390_set_status_code(env, r1, ZPCI_MOD_ST_SEQUENCE);
1175         }
1176         break;
1177     case ZPCI_MOD_FC_SET_MEASURE: {
1178         uint64_t fmb_addr = ldq_p(&fib.fmb_addr);
1179 
1180         if (fmb_addr & FMBK_MASK) {
1181             cc = ZPCI_PCI_LS_ERR;
1182             s390_pci_generate_error_event(ERR_EVENT_FMBPRO, pbdev->fh,
1183                                           pbdev->fid, fmb_addr, 0);
1184             fmb_timer_free(pbdev);
1185             break;
1186         }
1187 
1188         if (!fmb_addr) {
1189             /* Stop updating FMB. */
1190             fmb_timer_free(pbdev);
1191             break;
1192         }
1193 
1194         if (!pbdev->fmb_timer) {
1195             pbdev->fmb_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL,
1196                                             fmb_update, pbdev);
1197         } else if (timer_pending(pbdev->fmb_timer)) {
1198             /* Remove pending timer to update FMB address. */
1199             timer_del(pbdev->fmb_timer);
1200         }
1201         pbdev->fmb_addr = fmb_addr;
1202         timer_mod(pbdev->fmb_timer,
1203                   qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + DEFAULT_MUI);
1204         break;
1205     }
1206     default:
1207         s390_program_interrupt(&cpu->env, PGM_OPERAND, ra);
1208         cc = ZPCI_PCI_LS_ERR;
1209     }
1210 
1211     setcc(cpu, cc);
1212     return 0;
1213 }
1214 
1215 int stpcifc_service_call(S390CPU *cpu, uint8_t r1, uint64_t fiba, uint8_t ar,
1216                          uintptr_t ra)
1217 {
1218     CPUS390XState *env = &cpu->env;
1219     uint8_t dmaas;
1220     uint32_t fh;
1221     ZpciFib fib;
1222     S390PCIBusDevice *pbdev;
1223     uint32_t data;
1224     uint64_t cc = ZPCI_PCI_LS_OK;
1225 
1226     if (env->psw.mask & PSW_MASK_PSTATE) {
1227         s390_program_interrupt(env, PGM_PRIVILEGED, ra);
1228         return 0;
1229     }
1230 
1231     fh = env->regs[r1] >> 32;
1232     dmaas = (env->regs[r1] >> 16) & 0xff;
1233 
1234     if (dmaas) {
1235         setcc(cpu, ZPCI_PCI_LS_ERR);
1236         s390_set_status_code(env, r1, ZPCI_STPCIFC_ST_INVAL_DMAAS);
1237         return 0;
1238     }
1239 
1240     if (fiba & 0x7) {
1241         s390_program_interrupt(env, PGM_SPECIFICATION, ra);
1242         return 0;
1243     }
1244 
1245     pbdev = s390_pci_find_dev_by_idx(s390_get_phb(), fh & FH_MASK_INDEX);
1246     if (!pbdev) {
1247         setcc(cpu, ZPCI_PCI_LS_INVAL_HANDLE);
1248         return 0;
1249     }
1250 
1251     memset(&fib, 0, sizeof(fib));
1252 
1253     switch (pbdev->state) {
1254     case ZPCI_FS_RESERVED:
1255     case ZPCI_FS_STANDBY:
1256         setcc(cpu, ZPCI_PCI_LS_INVAL_HANDLE);
1257         return 0;
1258     case ZPCI_FS_DISABLED:
1259         if (fh & FH_MASK_ENABLE) {
1260             setcc(cpu, ZPCI_PCI_LS_INVAL_HANDLE);
1261             return 0;
1262         }
1263         goto out;
1264     /* BLOCKED bit is set to one coincident with the setting of ERROR bit.
1265      * FH Enabled bit is set to one in states of ENABLED, BLOCKED or ERROR. */
1266     case ZPCI_FS_ERROR:
1267         fib.fc |= 0x20;
1268         /* fallthrough */
1269     case ZPCI_FS_BLOCKED:
1270         fib.fc |= 0x40;
1271         /* fallthrough */
1272     case ZPCI_FS_ENABLED:
1273         fib.fc |= 0x80;
1274         if (pbdev->iommu->enabled) {
1275             fib.fc |= 0x10;
1276         }
1277         if (!(fh & FH_MASK_ENABLE)) {
1278             env->regs[r1] |= 1ULL << 63;
1279         }
1280         break;
1281     case ZPCI_FS_PERMANENT_ERROR:
1282         setcc(cpu, ZPCI_PCI_LS_ERR);
1283         s390_set_status_code(env, r1, ZPCI_STPCIFC_ST_PERM_ERROR);
1284         return 0;
1285     }
1286 
1287     stq_p(&fib.pba, pbdev->iommu->pba);
1288     stq_p(&fib.pal, pbdev->iommu->pal);
1289     stq_p(&fib.iota, pbdev->iommu->g_iota);
1290     stq_p(&fib.aibv, pbdev->routes.adapter.ind_addr);
1291     stq_p(&fib.aisb, pbdev->routes.adapter.summary_addr);
1292     stq_p(&fib.fmb_addr, pbdev->fmb_addr);
1293 
1294     data = ((uint32_t)pbdev->isc << 28) | ((uint32_t)pbdev->noi << 16) |
1295            ((uint32_t)pbdev->routes.adapter.ind_offset << 8) |
1296            ((uint32_t)pbdev->sum << 7) | pbdev->routes.adapter.summary_offset;
1297     stl_p(&fib.data, data);
1298 
1299 out:
1300     if (s390_cpu_virt_mem_write(cpu, fiba, ar, (uint8_t *)&fib, sizeof(fib))) {
1301         s390_cpu_virt_mem_handle_exc(cpu, ra);
1302         return 0;
1303     }
1304 
1305     setcc(cpu, cc);
1306     return 0;
1307 }
1308