Skip to contentSkip to conversation

s390x/pci: Improve zPCI to cover more cases

PATCH Superseded Pierre Morel 3 participants 27 messages Nov 16, 2017 – Nov 22, 2017

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v2 cover s390x/pci: Improve zPCI to cover more cases

This patch fixes the following BUG:
Even a guest is able to detect virtio_pci device, the init function the Linux virtio_pci driver will hang because zPCI does not support the subregions used by virtio_pci.

It follows that right now the PCI support is very limited (e.g. pass through of a host vfio device)
To enable features like virtio-pci several modifications needs to be done.

As already stated above, Virtio-PCI uses subregions, which may eventually be discontinuous inside bars instead of a single flat region often used by real devices.
The address offset being formerly calculated from the BAR base address must be adapted to the subregions instead of to the single region.

This patch provides the new calculation for the three kind of BAR access, zPCI STORE, zPCI LOAD and zPCI STORE BLOCK done by zPCI.

We use the opportunity to
- enhance the fault detection for zPCI STORE and LOAD,
- enhance the fault detection and to provide the maximum STORE BLOCK block size, maxstbl, for zPCI STORE BLOCK
- factor out part of the code used to calculate the offset and access the BARs,
- factor out the code for endianess conversion.

Pierre Morel (7):
s390x/pci: factor out endianess conversion s390x/pci: rework PCI STORE
s390x/pci: rework PCI LOAD
s390x/pci: rework PCI STORE BLOCK s390x/pci: move the memory region read from pcilg s390x/pci: move the memory region write from pcistg s390x/pci: search for subregion inside the BARs

 hw/s390x/s390-pci-bus.h  |   1 +
 hw/s390x/s390-pci-inst.c | 247 ++++++++++++++++++++++++++++-------------------
 hw/s390x/s390-pci-inst.h |   2 +-
 3 files changed, 150 insertions(+), 100 deletions(-)
Signature · 8 lines
2.7.4

Changelog:

- suppress fallthrough to PCI_ROM_SLOT to handle it in the default case.
- reword most of the patch commit messages
- add comments to the endianness conversion
- reword somme comments inside the patches
#1 lore raw

PATCH v2 1/7 s390x/pci: factor out endianess conversion

There are two places where the same endianness conversion is done.
Let's factor this out into a static function.

---
 hw/s390x/s390-pci-inst.c | 59 +++++++++++++++++++++++++++---------------------
 1 file changed, 33 insertions(+), 26 deletions(-)
hw/s390x/s390-pci-inst.c +33 -26
index 8e088f3..ded1556 100644
@@ -314,6 +314,36 @@ out:
     return 0;
 }
 
+/**
+ * This function is called when endianess is fixed, whatever the host endianess
+ * is, like in our case from s390x BIG endian registers to little endian PCI
+ * Bars, to translate the uint64_t pointed from one endianess to the other.
+ *
+ * @ptr: a pointer to a uint64_t data field
+ * @len: the length of the valid data, must be 1,2,4 or 8
+ */
+static int zpci_endian_swap(uint64_t *ptr, uint8_t len)
+{
+    uint64_t data = *ptr;
+    switch (len) {
+    case 1:
+        break;
+    case 2:
+        data = bswap16(data);
+        break;
+    case 4:
+        data = bswap32(data);
+        break;
+    case 8:
+        data = bswap64(data);
+        break;
+    default:
+        return -EINVAL;
+    }
+    *ptr = data;
+    return 0;
+}
+
 int pcilg_service_call(S390CPU *cpu, uint8_t r1, uint8_t r2)
 {
     CPUS390XState *env = &cpu->env;
@@ -385,19 +415,7 @@ int pcilg_service_call(S390CPU *cpu, uint8_t r1, uint8_t r2)
         data =  pci_host_config_read_common(
                    pbdev->pdev, offset, pci_config_size(pbdev->pdev), len);
 
-        switch (len) {
-        case 1:
-            break;
-        case 2:
-            data = bswap16(data);
-            break;
-        case 4:
-            data = bswap32(data);
-            break;
-        case 8:
-            data = bswap64(data);
-            break;
-        default:
+        if (zpci_endian_swap(&data, len)) {
             program_interrupt(env, PGM_OPERAND, 4);
             return 0;
         }
@@ -500,19 +518,8 @@ int pcistg_service_call(S390CPU *cpu, uint8_t r1, uint8_t r2)
             program_interrupt(env, PGM_OPERAND, 4);
             return 0;
         }
-        switch (len) {
-        case 1:
-            break;
-        case 2:
-            data = bswap16(data);
-            break;
-        case 4:
-            data = bswap32(data);
-            break;
-        case 8:
-            data = bswap64(data);
-            break;
-        default:
+
+        if (zpci_endian_swap(&data, len)) {
             program_interrupt(env, PGM_OPERAND, 4);
             return 0;
         }
Signature · 1 line
2.7.4
#2 lore raw

Re: [Qemu-devel] [PATCH v2 1/7] s390x/pci: factor out endianess conversion

Quoted 7 lines · On Thu, 16 Nov 2017 18:51:49 +0100 Pierre Morel <[email protected]>…

There are two places where the same endianness conversion is done.
Let's factor this out into a static function.

Your two s-o-bs look a bit odd...

Quoted 9 lines
---
 hw/s390x/s390-pci-inst.c | 59 +++++++++++++++++++++++++++---------------------
 1 file changed, 33 insertions(+), 26 deletions(-)
hw/s390x/s390-pci-inst.c +0 -0
index 8e088f3..ded1556 100644

hw/s390x/s390-pci-inst.c out: · line 320

Quoted 7 lines
hw/s390x/s390-pci-inst.c +4 -0
@@ -314,6 +314,36 @@ out:
     return 0;
 }
 
+/**
+ * This function is called when endianess is fixed, whatever the host endianess
+ * is, like in our case from s390x BIG endian registers to little endian PCI
+ * Bars, to translate the uint64_t pointed from one endianess to the other.

I would rephrase this a bit:

"Swap data contained in s390x big endian registers to little endian PCI bars."

That makes it clear that this function is for a specialized use case.

hw/s390x/s390-pci-inst.c out: · line 327

Quoted 7 lines
hw/s390x/s390-pci-inst.c +7 -0
+ *
+ * @ptr: a pointer to a uint64_t data field
+ * @len: the length of the valid data, must be 1,2,4 or 8
+ */
+static int zpci_endian_swap(uint64_t *ptr, uint8_t len)
+{
+    uint64_t data = *ptr;

Please add an extra empty line.

Quoted 12 lines
hw/s390x/s390-pci-inst.c +12 -0
+    switch (len) {
+    case 1:
+        break;
+    case 2:
+        data = bswap16(data);
+        break;
+    case 4:
+        data = bswap32(data);
+        break;
+    case 8:
+        data = bswap64(data);
+        break;

hw/s390x/s390-pci-inst.c out: · line 349

Quoted 10 lines
hw/s390x/s390-pci-inst.c +7 -0
+    default:
+        return -EINVAL;
+    }
+    *ptr = data;
+    return 0;
+}
+
 int pcilg_service_call(S390CPU *cpu, uint8_t r1, uint8_t r2)
 {
     CPUS390XState *env = &cpu->env;

Other than the nits above, looks good.

#3 lore raw

Re: [Qemu-devel] [PATCH v2 1/7] s390x/pci: factor out endianess conversion

Quoted 32 lines · Cornelia Huck
Quoted 7 lines · On Thu, 16 Nov 2017 18:51:49 +0100 Pierre Morel <[email protected]>…

There are two places where the same endianness conversion is done.
Let's factor this out into a static function.

Your two s-o-bs look a bit odd...

Quoted 9 lines
---
  hw/s390x/s390-pci-inst.c | 59 +++++++++++++++++++++++++++---------------------
  1 file changed, 33 insertions(+), 26 deletions(-)
hw/s390x/s390-pci-inst.c +0 -0
index 8e088f3..ded1556 100644

hw/s390x/s390-pci-inst.c out: · line 320

Quoted 7 lines
hw/s390x/s390-pci-inst.c +4 -0
@@ -314,6 +314,36 @@ out:
      return 0;
  }
 
+/**
+ * This function is called when endianess is fixed, whatever the host endianess
+ * is, like in our case from s390x BIG endian registers to little endian PCI
+ * Bars, to translate the uint64_t pointed from one endianess to the other.

I would rephrase this a bit:

Quoted 2 lines

"Swap data contained in s390x big endian registers to little endian PCI bars."

Simpler and clearer. thanks.

Quoted 12 lines

That makes it clear that this function is for a specialized use case.

hw/s390x/s390-pci-inst.c out: · line 327

Quoted 7 lines
hw/s390x/s390-pci-inst.c +7 -0
+ *
+ * @ptr: a pointer to a uint64_t data field
+ * @len: the length of the valid data, must be 1,2,4 or 8
+ */
+static int zpci_endian_swap(uint64_t *ptr, uint8_t len)
+{
+    uint64_t data = *ptr;

Please add an extra empty line.

yes.

Quoted 17 lines
Quoted 17 lines
hw/s390x/s390-pci-inst.c +16 -0
+    switch (len) {
+    case 1:
+        break;
+    case 2:
+        data = bswap16(data);
+        break;
+    case 4:
+        data = bswap32(data);
+        break;
+    case 8:
+        data = bswap64(data);
+        break;
+    default:
+        return -EINVAL;
+    }
+    *ptr = data;
Quoted 9 lines

hw/s390x/s390-pci-inst.c out: · line 349

Quoted 6 lines
hw/s390x/s390-pci-inst.c +3 -0
+    return 0;
+}
+
  int pcilg_service_call(S390CPU *cpu, uint8_t r1, uint8_t r2)
  {
      CPUS390XState *env = &cpu->env;

Other than the nits above, looks good.

Thanks for the review

Pierre

Signature · 2 lines
Pierre Morel
Linux/KVM/QEMU in Böblingen - Germany
#4 lore raw

PATCH v2 2/7 s390x/pci: rework PCI STORE

In reply to Pierre Morel

Enhance the fault detection, correction of the fault reporting.

---
 hw/s390x/s390-pci-inst.c | 39 ++++++++++++++++++++++-----------------
 1 file changed, 22 insertions(+), 17 deletions(-)
hw/s390x/s390-pci-inst.c +22 -17
index ded1556..df0c8f0 100644
@@ -470,6 +470,12 @@ int pcistg_service_call(S390CPU *cpu, uint8_t r1, uint8_t r2)
     pcias = (env->regs[r2] >> 16) & 0xf;
     len = env->regs[r2] & 0xf;
     offset = env->regs[r2 + 1];
+    data = env->regs[r1];
+
+    if (!(fh & FH_MASK_ENABLE)) {
+        setcc(cpu, ZPCI_PCI_LS_INVAL_HANDLE);
+        return 0;
+    }
 
     pbdev = s390_pci_find_dev_by_fh(s390_get_phb(), fh);
     if (!pbdev) {
@@ -479,12 +485,7 @@ int pcistg_service_call(S390CPU *cpu, uint8_t r1, uint8_t r2)
     }
 
     switch (pbdev->state) {
-    case ZPCI_FS_RESERVED:
-    case ZPCI_FS_STANDBY:
-    case ZPCI_FS_DISABLED:
     case ZPCI_FS_PERMANENT_ERROR:
-        setcc(cpu, ZPCI_PCI_LS_INVAL_HANDLE);
-        return 0;
     case ZPCI_FS_ERROR:
         setcc(cpu, ZPCI_PCI_LS_ERR);
         s390_set_status_code(env, r2, ZPCI_PCI_ST_BLOCKED);
@@ -493,9 +494,13 @@ int pcistg_service_call(S390CPU *cpu, uint8_t r1, uint8_t r2)
         break;
     }
 
-    data = env->regs[r1];
-    if (pcias < 6) {
-        if ((8 - (offset & 0x7)) < len) {
+    /* Test that the pcias is valid and do the appropriates operations */
+    switch (pcias) {
+    case 0 ... 5:
+        /* Check length:
+         * A length of 0 is invalid and length should not cross a double word
+         */
+        if (!len || (len > (8 - (offset & 0x7)))) {
             program_interrupt(env, PGM_OPERAND, 4);
             return 0;
         }
@@ -513,21 +518,21 @@ int pcistg_service_call(S390CPU *cpu, uint8_t r1, uint8_t r2)
             program_interrupt(env, PGM_OPERAND, 4);
             return 0;
         }
-    } else if (pcias == 15) {
-        if ((4 - (offset & 0x3)) < len) {
-            program_interrupt(env, PGM_OPERAND, 4);
-            return 0;
-        }
-
-        if (zpci_endian_swap(&data, len)) {
+        break;
+    case 15:
+        /* ZPCI uses the pseudo BAR number 15 as configuration space */
+        /* possible access lengths are 1,2,4 and must not cross a word */
+        if (!len || (len > (4 - (offset & 0x3))) || len == 3) {
             program_interrupt(env, PGM_OPERAND, 4);
             return 0;
         }
-
+        /* len = 1,2,4 so we do not need to test */
+        zpci_endian_swap(&data, len);
         pci_host_config_write_common(pbdev->pdev, offset,
                                      pci_config_size(pbdev->pdev),
                                      data, len);
-    } else {
+        break;
+    default:
         DPRINTF("pcistg invalid space\n");
         setcc(cpu, ZPCI_PCI_LS_ERR);
         s390_set_status_code(env, r2, ZPCI_PCI_ST_INVAL_AS);
Signature · 1 line
2.7.4
#5 lore raw

Re: [Qemu-devel] [PATCH v2 2/7] s390x/pci: rework PCI STORE

Quoted 5 lines · On Thu, 16 Nov 2017 18:51:50 +0100 Pierre Morel <[email protected]>…

Enhance the fault detection, correction of the fault reporting.

Same double s-o-b.

Quoted 35 lines
---
 hw/s390x/s390-pci-inst.c | 39 ++++++++++++++++++++++-----------------
 1 file changed, 22 insertions(+), 17 deletions(-)
hw/s390x/s390-pci-inst.c +6 -5
index ded1556..df0c8f0 100644
@@ -470,6 +470,12 @@ int pcistg_service_call(S390CPU *cpu, uint8_t r1, uint8_t r2)
     pcias = (env->regs[r2] >> 16) & 0xf;
     len = env->regs[r2] & 0xf;
     offset = env->regs[r2 + 1];
+    data = env->regs[r1];
+
+    if (!(fh & FH_MASK_ENABLE)) {
+        setcc(cpu, ZPCI_PCI_LS_INVAL_HANDLE);
+        return 0;
+    }
 
     pbdev = s390_pci_find_dev_by_fh(s390_get_phb(), fh);
     if (!pbdev) {
@@ -479,12 +485,7 @@ int pcistg_service_call(S390CPU *cpu, uint8_t r1, uint8_t r2)
     }
 
     switch (pbdev->state) {
-    case ZPCI_FS_RESERVED:
-    case ZPCI_FS_STANDBY:
-    case ZPCI_FS_DISABLED:
     case ZPCI_FS_PERMANENT_ERROR:
-        setcc(cpu, ZPCI_PCI_LS_INVAL_HANDLE);
-        return 0;
     case ZPCI_FS_ERROR:
         setcc(cpu, ZPCI_PCI_LS_ERR);
         s390_set_status_code(env, r2, ZPCI_PCI_ST_BLOCKED);

hw/s390x/s390-pci-inst.c int pcistg_service_call(S390CPU *cpu, uint8_t r1, uint8_t r2) · line 499

Quoted 9 lines
hw/s390x/s390-pci-inst.c +3 -3
@@ -493,9 +494,13 @@ int pcistg_service_call(S390CPU *cpu, uint8_t r1, uint8_t r2)
         break;
     }
 
-    data = env->regs[r1];
-    if (pcias < 6) {
-        if ((8 - (offset & 0x7)) < len) {
+    /* Test that the pcias is valid and do the appropriates operations */
+    switch (pcias) {
+    case 0 ... 5:

Make this
case 0...5:
?

Quoted 28 lines
hw/s390x/s390-pci-inst.c +10 -8
+        /* Check length:
+         * A length of 0 is invalid and length should not cross a double word
+         */
+        if (!len || (len > (8 - (offset & 0x7)))) {
             program_interrupt(env, PGM_OPERAND, 4);
             return 0;
         }
@@ -513,21 +518,21 @@ int pcistg_service_call(S390CPU *cpu, uint8_t r1, uint8_t r2)
             program_interrupt(env, PGM_OPERAND, 4);
             return 0;
         }
-    } else if (pcias == 15) {
-        if ((4 - (offset & 0x3)) < len) {
-            program_interrupt(env, PGM_OPERAND, 4);
-            return 0;
-        }
-
-        if (zpci_endian_swap(&data, len)) {
+        break;
+    case 15:
+        /* ZPCI uses the pseudo BAR number 15 as configuration space */
+        /* possible access lengths are 1,2,4 and must not cross a word */
+        if (!len || (len > (4 - (offset & 0x3))) || len == 3) {
             program_interrupt(env, PGM_OPERAND, 4);
             return 0;
         }
-
+        /* len = 1,2,4 so we do not need to test */

hw/s390x/s390-pci-inst.c int pcistg_service_call(S390CPU *cpu, uint8_t r1, uint8_t r2) · line 538

Quoted 10 lines
hw/s390x/s390-pci-inst.c +3 -1
+        zpci_endian_swap(&data, len);
         pci_host_config_write_common(pbdev->pdev, offset,
                                      pci_config_size(pbdev->pdev),
                                      data, len);
-    } else {
+        break;
+    default:
         DPRINTF("pcistg invalid space\n");
         setcc(cpu, ZPCI_PCI_LS_ERR);
         s390_set_status_code(env, r2, ZPCI_PCI_ST_INVAL_AS);

Other than the nits, looks good.

#6 lore raw

Re: [Qemu-devel] [PATCH v2 2/7] s390x/pci: rework PCI STORE

Quoted 7 lines · On Tue, 21 Nov 2017 11:38:45 +0100 Cornelia Huck <[email protected]> wrote:
Quoted 4 lines · On Thu, 16 Nov 2017 18:51:50 +0100 Pierre Morel <[email protected]>…
(quoted diff) +0 -0
@@ -493,9 +494,13 @@ int pcistg_service_call(S390CPU *cpu, uint8_t r1, uint8_t r2)
         break;
     }
 
Quoted 10 lines

hw/s390x/s390-pci-inst.c int pcistg_service_call(S390CPU *cpu, uint8_t r1, uint8_t r2) · line 499

Quoted 6 lines
hw/s390x/s390-pci-inst.c +3 -3
-    data = env->regs[r1];
-    if (pcias < 6) {
-        if ((8 - (offset & 0x7)) < len) {
+    /* Test that the pcias is valid and do the appropriates operations */
+    switch (pcias) {
+    case 0 ... 5:  

Make this
case 0...5:
?

...only that it confuses the compiler when using numbers. We can either keep the slightly ugly version, or introduce #defines. ZPCI_PCIAS_IOREGION_{MIN,MAX} (and maybe ZPCI_PCIAS_CONFIG for 15)?

Quoted 41 lines
Quoted 29 lines
hw/s390x/s390-pci-inst.c +10 -8
+        /* Check length:
+         * A length of 0 is invalid and length should not cross a double word
+         */
+        if (!len || (len > (8 - (offset & 0x7)))) {
             program_interrupt(env, PGM_OPERAND, 4);
             return 0;
         }
@@ -513,21 +518,21 @@ int pcistg_service_call(S390CPU *cpu, uint8_t r1, uint8_t r2)
             program_interrupt(env, PGM_OPERAND, 4);
             return 0;
         }
-    } else if (pcias == 15) {
-        if ((4 - (offset & 0x3)) < len) {
-            program_interrupt(env, PGM_OPERAND, 4);
-            return 0;
-        }
-
-        if (zpci_endian_swap(&data, len)) {
+        break;
+    case 15:
+        /* ZPCI uses the pseudo BAR number 15 as configuration space */
+        /* possible access lengths are 1,2,4 and must not cross a word */
+        if (!len || (len > (4 - (offset & 0x3))) || len == 3) {
             program_interrupt(env, PGM_OPERAND, 4);
             return 0;
         }
-
+        /* len = 1,2,4 so we do not need to test */

hw/s390x/s390-pci-inst.c int pcistg_service_call(S390CPU *cpu, uint8_t r1, uint8_t r2) · line 538

Quoted 10 lines
hw/s390x/s390-pci-inst.c +3 -1
+        zpci_endian_swap(&data, len);
         pci_host_config_write_common(pbdev->pdev, offset,
                                      pci_config_size(pbdev->pdev),
                                      data, len);
-    } else {
+        break;
+    default:
         DPRINTF("pcistg invalid space\n");
         setcc(cpu, ZPCI_PCI_LS_ERR);
         s390_set_status_code(env, r2, ZPCI_PCI_ST_INVAL_AS);  

Other than the nits, looks good.

#7 lore raw

Re: [Qemu-devel] [PATCH v2 2/7] s390x/pci: rework PCI STORE

Quoted 21 lines · Cornelia Huck
Quoted 7 lines · On Tue, 21 Nov 2017 11:38:45 +0100 Cornelia Huck <[email protected]> wrote:
Quoted 4 lines · On Thu, 16 Nov 2017 18:51:50 +0100 Pierre Morel <[email protected]>…
(quoted diff) +0 -0
@@ -493,9 +494,13 @@ int pcistg_service_call(S390CPU *cpu, uint8_t r1, uint8_t r2)
          break;
      }
 
Quoted 10 lines

hw/s390x/s390-pci-inst.c int pcistg_service_call(S390CPU *cpu, uint8_t r1, uint8_t r2) · line 499

Quoted 6 lines
hw/s390x/s390-pci-inst.c +3 -3
-    data = env->regs[r1];
-    if (pcias < 6) {
-        if ((8 - (offset & 0x7)) < len) {
+    /* Test that the pcias is valid and do the appropriates operations */
+    switch (pcias) {
+    case 0 ... 5:

Make this
case 0...5:
?

Quoted 1 line

...only that it confuses the compiler when using numbers. We can either

I did not see this as I replied to the previous email, sorry.

Quoted 2 lines

keep the slightly ugly version, or introduce #defines. ZPCI_PCIAS_IOREGION_{MIN,MAX} (and maybe ZPCI_PCIAS_CONFIG for 15)?

I agree something is missing.
But I am not sure that a #define brings clarity.
I would prefer to add a comment.

	/* A ZPCI PCI card may use any BAR from BAR 0 to BAR 5 */

?

Quoted 40 lines
Quoted 39 lines
Quoted 27 lines
hw/s390x/s390-pci-inst.c +9 -8
+         * A length of 0 is invalid and length should not cross a double word
+         */
+        if (!len || (len > (8 - (offset & 0x7)))) {
              program_interrupt(env, PGM_OPERAND, 4);
              return 0;
          }
@@ -513,21 +518,21 @@ int pcistg_service_call(S390CPU *cpu, uint8_t r1, uint8_t r2)
              program_interrupt(env, PGM_OPERAND, 4);
              return 0;
          }
-    } else if (pcias == 15) {
-        if ((4 - (offset & 0x3)) < len) {
-            program_interrupt(env, PGM_OPERAND, 4);
-            return 0;
-        }
-
-        if (zpci_endian_swap(&data, len)) {
+        break;
+    case 15:
+        /* ZPCI uses the pseudo BAR number 15 as configuration space */
+        /* possible access lengths are 1,2,4 and must not cross a word */
+        if (!len || (len > (4 - (offset & 0x3))) || len == 3) {
              program_interrupt(env, PGM_OPERAND, 4);
              return 0;
          }
-
+        /* len = 1,2,4 so we do not need to test */

hw/s390x/s390-pci-inst.c int pcistg_service_call(S390CPU *cpu, uint8_t r1, uint8_t r2) · line 538

Quoted 10 lines
hw/s390x/s390-pci-inst.c +3 -1
+        zpci_endian_swap(&data, len);
          pci_host_config_write_common(pbdev->pdev, offset,
                                       pci_config_size(pbdev->pdev),
                                       data, len);
-    } else {
+        break;
+    default:
          DPRINTF("pcistg invalid space\n");
          setcc(cpu, ZPCI_PCI_LS_ERR);
          s390_set_status_code(env, r2, ZPCI_PCI_ST_INVAL_AS);

Other than the nits, looks good.

Signature · 2 lines
Pierre Morel
Linux/KVM/QEMU in Böblingen - Germany
#8 lore raw

Re: [Qemu-devel] [PATCH v2 2/7] s390x/pci: rework PCI STORE

Quoted 30 lines · On Tue, 21 Nov 2017 19:03:17 +0100 Pierre Morel <[email protected]>…
Quoted 21 lines · Cornelia Huck
Quoted 7 lines · On Tue, 21 Nov 2017 11:38:45 +0100 Cornelia Huck <[email protected]> wrote:
Quoted 4 lines · On Thu, 16 Nov 2017 18:51:50 +0100 Pierre Morel <[email protected]>…
(quoted diff) +0 -0
@@ -493,9 +494,13 @@ int pcistg_service_call(S390CPU *cpu, uint8_t r1, uint8_t r2)
          break;
      }
 
Quoted 10 lines

hw/s390x/s390-pci-inst.c int pcistg_service_call(S390CPU *cpu, uint8_t r1, uint8_t r2) · line 499

Quoted 6 lines
hw/s390x/s390-pci-inst.c +3 -3
-    data = env->regs[r1];
-    if (pcias < 6) {
-        if ((8 - (offset & 0x7)) < len) {
+    /* Test that the pcias is valid and do the appropriates operations */
+    switch (pcias) {
+    case 0 ... 5:  

Make this
case 0...5:
?

Quoted 1 line

...only that it confuses the compiler when using numbers. We can either

I did not see this as I replied to the previous email, sorry.

Quoted 2 lines

keep the slightly ugly version, or introduce #defines. ZPCI_PCIAS_IOREGION_{MIN,MAX} (and maybe ZPCI_PCIAS_CONFIG for 15)?

Quoted 6 lines

I agree something is missing.
But I am not sure that a #define brings clarity.
I would prefer to add a comment.

	/* A ZPCI PCI card may use any BAR from BAR 0 to BAR 5 */

?

It's more a speaking value vs. magic numbers thing. A comment does not hurt either, though :)

(And we get rid of the spacing as well.)

Quoted 44 lines
Quoted 42 lines
Quoted 41 lines
Quoted 29 lines
hw/s390x/s390-pci-inst.c +9 -8
+         * A length of 0 is invalid and length should not cross a double word
+         */
+        if (!len || (len > (8 - (offset & 0x7)))) {
              program_interrupt(env, PGM_OPERAND, 4);
              return 0;
          }
@@ -513,21 +518,21 @@ int pcistg_service_call(S390CPU *cpu, uint8_t r1, uint8_t r2)
              program_interrupt(env, PGM_OPERAND, 4);
              return 0;
          }
-    } else if (pcias == 15) {
-        if ((4 - (offset & 0x3)) < len) {
-            program_interrupt(env, PGM_OPERAND, 4);
-            return 0;
-        }
-
-        if (zpci_endian_swap(&data, len)) {
+        break;
+    case 15:
+        /* ZPCI uses the pseudo BAR number 15 as configuration space */
+        /* possible access lengths are 1,2,4 and must not cross a word */
+        if (!len || (len > (4 - (offset & 0x3))) || len == 3) {
              program_interrupt(env, PGM_OPERAND, 4);
              return 0;
          }
-
+        /* len = 1,2,4 so we do not need to test */

hw/s390x/s390-pci-inst.c int pcistg_service_call(S390CPU *cpu, uint8_t r1, uint8_t r2) · line 538

Quoted 10 lines
hw/s390x/s390-pci-inst.c +3 -1
+        zpci_endian_swap(&data, len);
          pci_host_config_write_common(pbdev->pdev, offset,
                                       pci_config_size(pbdev->pdev),
                                       data, len);
-    } else {
+        break;
+    default:
          DPRINTF("pcistg invalid space\n");
          setcc(cpu, ZPCI_PCI_LS_ERR);
          s390_set_status_code(env, r2, ZPCI_PCI_ST_INVAL_AS);  

Other than the nits, looks good.

#9 lore raw

Re: [Qemu-devel] [PATCH v2 2/7] s390x/pci: rework PCI STORE

Quoted 38 lines · Cornelia Huck
Quoted 35 lines · On Tue, 21 Nov 2017 19:03:17 +0100 Pierre Morel <[email protected]>…
Quoted 21 lines · Cornelia Huck
Quoted 7 lines · On Tue, 21 Nov 2017 11:38:45 +0100 Cornelia Huck <[email protected]> wrote:
Quoted 4 lines · On Thu, 16 Nov 2017 18:51:50 +0100 Pierre Morel <[email protected]>…
(quoted diff) +0 -0
@@ -493,9 +494,13 @@ int pcistg_service_call(S390CPU *cpu, uint8_t r1, uint8_t r2)
           break;
       }
 
Quoted 10 lines

hw/s390x/s390-pci-inst.c int pcistg_service_call(S390CPU *cpu, uint8_t r1, uint8_t r2) · line 499

Quoted 6 lines
hw/s390x/s390-pci-inst.c +3 -3
-    data = env->regs[r1];
-    if (pcias < 6) {
-        if ((8 - (offset & 0x7)) < len) {
+    /* Test that the pcias is valid and do the appropriates operations */
+    switch (pcias) {
+    case 0 ... 5:

Make this
case 0...5:
?

Quoted 1 line

...only that it confuses the compiler when using numbers. We can either

I did not see this as I replied to the previous email, sorry.

Quoted 2 lines

keep the slightly ugly version, or introduce #defines. ZPCI_PCIAS_IOREGION_{MIN,MAX} (and maybe ZPCI_PCIAS_CONFIG for 15)?

I agree something is missing.
But I am not sure that a #define brings clarity.
I would prefer to add a comment.

	/* A ZPCI PCI card may use any BAR from BAR 0 to BAR 5 */
Quoted 6 lines
Quoted 1 line

?

It's more a speaking value vs. magic numbers thing. A comment does not hurt either, though :)

(And we get rid of the spacing as well.)

OK, thanks.

Quoted 46 lines
Quoted 45 lines
Quoted 43 lines
Quoted 42 lines
Quoted 30 lines
hw/s390x/s390-pci-inst.c +9 -8
+         * A length of 0 is invalid and length should not cross a double word
+         */
+        if (!len || (len > (8 - (offset & 0x7)))) {
               program_interrupt(env, PGM_OPERAND, 4);
               return 0;
           }
@@ -513,21 +518,21 @@ int pcistg_service_call(S390CPU *cpu, uint8_t r1, uint8_t r2)
               program_interrupt(env, PGM_OPERAND, 4);
               return 0;
           }
-    } else if (pcias == 15) {
-        if ((4 - (offset & 0x3)) < len) {
-            program_interrupt(env, PGM_OPERAND, 4);
-            return 0;
-        }
-
-        if (zpci_endian_swap(&data, len)) {
+        break;
+    case 15:
+        /* ZPCI uses the pseudo BAR number 15 as configuration space */
+        /* possible access lengths are 1,2,4 and must not cross a word */
+        if (!len || (len > (4 - (offset & 0x3))) || len == 3) {
               program_interrupt(env, PGM_OPERAND, 4);
               return 0;
           }
-
+        /* len = 1,2,4 so we do not need to test */

hw/s390x/s390-pci-inst.c int pcistg_service_call(S390CPU *cpu, uint8_t r1, uint8_t r2) · line 538

Quoted 10 lines
hw/s390x/s390-pci-inst.c +3 -1
+        zpci_endian_swap(&data, len);
           pci_host_config_write_common(pbdev->pdev, offset,
                                        pci_config_size(pbdev->pdev),
                                        data, len);
-    } else {
+        break;
+    default:
           DPRINTF("pcistg invalid space\n");
           setcc(cpu, ZPCI_PCI_LS_ERR);
           s390_set_status_code(env, r2, ZPCI_PCI_ST_INVAL_AS);

Other than the nits, looks good.

Signature · 2 lines
Pierre Morel
Linux/KVM/QEMU in Böblingen - Germany
#10 lore raw

Re: [Qemu-devel] [PATCH v2 2/7] s390x/pci: rework PCI STORE

In reply to Cornelia Huck
Quoted 10 lines · Cornelia Huck
Quoted 5 lines · On Thu, 16 Nov 2017 18:51:50 +0100 Pierre Morel <[email protected]>…

Enhance the fault detection, correction of the fault reporting.

Same double s-o-b.

removed.
Thanks

Quoted 39 lines
Quoted 39 lines
---
  hw/s390x/s390-pci-inst.c | 39 ++++++++++++++++++++++-----------------
  1 file changed, 22 insertions(+), 17 deletions(-)
hw/s390x/s390-pci-inst.c +6 -5
index ded1556..df0c8f0 100644
@@ -470,6 +470,12 @@ int pcistg_service_call(S390CPU *cpu, uint8_t r1, uint8_t r2)
      pcias = (env->regs[r2] >> 16) & 0xf;
      len = env->regs[r2] & 0xf;
      offset = env->regs[r2 + 1];
+    data = env->regs[r1];
+
+    if (!(fh & FH_MASK_ENABLE)) {
+        setcc(cpu, ZPCI_PCI_LS_INVAL_HANDLE);
+        return 0;
+    }
 
      pbdev = s390_pci_find_dev_by_fh(s390_get_phb(), fh);
      if (!pbdev) {
@@ -479,12 +485,7 @@ int pcistg_service_call(S390CPU *cpu, uint8_t r1, uint8_t r2)
      }
 
      switch (pbdev->state) {
-    case ZPCI_FS_RESERVED:
-    case ZPCI_FS_STANDBY:
-    case ZPCI_FS_DISABLED:
      case ZPCI_FS_PERMANENT_ERROR:
-        setcc(cpu, ZPCI_PCI_LS_INVAL_HANDLE);
-        return 0;
      case ZPCI_FS_ERROR:
          setcc(cpu, ZPCI_PCI_LS_ERR);
          s390_set_status_code(env, r2, ZPCI_PCI_ST_BLOCKED);
@@ -493,9 +494,13 @@ int pcistg_service_call(S390CPU *cpu, uint8_t r1, uint8_t r2)
          break;
      }
 
Quoted 10 lines

hw/s390x/s390-pci-inst.c int pcistg_service_call(S390CPU *cpu, uint8_t r1, uint8_t r2) · line 499

Quoted 6 lines
hw/s390x/s390-pci-inst.c +3 -3
-    data = env->regs[r1];
-    if (pcias < 6) {
-        if ((8 - (offset & 0x7)) < len) {
+    /* Test that the pcias is valid and do the appropriates operations */
+    switch (pcias) {
+    case 0 ... 5:

Make this
case 0...5:
?

Compiler complains.
a blanc around the three points seems right.

Quoted 33 lines
Quoted 33 lines
hw/s390x/s390-pci-inst.c +11 -8
+        /* Check length:
+         * A length of 0 is invalid and length should not cross a double word
+         */
+        if (!len || (len > (8 - (offset & 0x7)))) {
              program_interrupt(env, PGM_OPERAND, 4);
              return 0;
          }
@@ -513,21 +518,21 @@ int pcistg_service_call(S390CPU *cpu, uint8_t r1, uint8_t r2)
              program_interrupt(env, PGM_OPERAND, 4);
              return 0;
          }
-    } else if (pcias == 15) {
-        if ((4 - (offset & 0x3)) < len) {
-            program_interrupt(env, PGM_OPERAND, 4);
-            return 0;
-        }
-
-        if (zpci_endian_swap(&data, len)) {
+        break;
+    case 15:
+        /* ZPCI uses the pseudo BAR number 15 as configuration space */
+        /* possible access lengths are 1,2,4 and must not cross a word */
+        if (!len || (len > (4 - (offset & 0x3))) || len == 3) {
              program_interrupt(env, PGM_OPERAND, 4);
              return 0;
          }
-
+        /* len = 1,2,4 so we do not need to test */
+        zpci_endian_swap(&data, len);
          pci_host_config_write_common(pbdev->pdev, offset,
                                       pci_config_size(pbdev->pdev),
                                       data, len);
Quoted 9 lines

hw/s390x/s390-pci-inst.c int pcistg_service_call(S390CPU *cpu, uint8_t r1, uint8_t r2) · line 538

Quoted 6 lines
hw/s390x/s390-pci-inst.c +2 -1
-    } else {
+        break;
+    default:
          DPRINTF("pcistg invalid space\n");
          setcc(cpu, ZPCI_PCI_LS_ERR);
          s390_set_status_code(env, r2, ZPCI_PCI_ST_INVAL_AS);

Other than the nits, looks good.

Thanks for reviewing, I remove the double signature

Regards,

Pierre

Signature · 2 lines
Pierre Morel
Linux/KVM/QEMU in Böblingen - Germany
#11 lore raw

PATCH v2 3/7 s390x/pci: rework PCI LOAD

In reply to Pierre Morel

Enhance the fault detection, correction of the fault reporting.

---
 hw/s390x/s390-pci-inst.c | 25 ++++++++++++++-----------
 1 file changed, 14 insertions(+), 11 deletions(-)
hw/s390x/s390-pci-inst.c +14 -11
index df0c8f0..cbc6421 100644
@@ -373,6 +373,11 @@ int pcilg_service_call(S390CPU *cpu, uint8_t r1, uint8_t r2)
     len = env->regs[r2] & 0xf;
     offset = env->regs[r2 + 1];
 
+    if (!(fh & FH_MASK_ENABLE)) {
+        setcc(cpu, ZPCI_PCI_LS_INVAL_HANDLE);
+        return 0;
+    }
+
     pbdev = s390_pci_find_dev_by_fh(s390_get_phb(), fh);
     if (!pbdev) {
         DPRINTF("pcilg no pci dev\n");
@@ -381,12 +386,7 @@ int pcilg_service_call(S390CPU *cpu, uint8_t r1, uint8_t r2)
     }
 
     switch (pbdev->state) {
-    case ZPCI_FS_RESERVED:
-    case ZPCI_FS_STANDBY:
-    case ZPCI_FS_DISABLED:
     case ZPCI_FS_PERMANENT_ERROR:
-        setcc(cpu, ZPCI_PCI_LS_INVAL_HANDLE);
-        return 0;
     case ZPCI_FS_ERROR:
         setcc(cpu, ZPCI_PCI_LS_ERR);
         s390_set_status_code(env, r2, ZPCI_PCI_ST_BLOCKED);
@@ -395,8 +395,9 @@ int pcilg_service_call(S390CPU *cpu, uint8_t r1, uint8_t r2)
         break;
     }
 
-    if (pcias < 6) {
-        if ((8 - (offset & 0x7)) < len) {
+    switch (pcias) {
+    case 0 ... 5:
+        if (!len || (len > (8 - (offset & 0x7)))) {
             program_interrupt(env, PGM_OPERAND, 4);
             return 0;
         }
@@ -407,8 +408,9 @@ int pcilg_service_call(S390CPU *cpu, uint8_t r1, uint8_t r2)
             program_interrupt(env, PGM_OPERAND, 4);
             return 0;
         }
-    } else if (pcias == 15) {
-        if ((4 - (offset & 0x3)) < len) {
+        break;
+    case 15:
+        if (!len || (len > (4 - (offset & 0x3))) || len == 3) {
             program_interrupt(env, PGM_OPERAND, 4);
             return 0;
         }
@@ -419,8 +421,9 @@ int pcilg_service_call(S390CPU *cpu, uint8_t r1, uint8_t r2)
             program_interrupt(env, PGM_OPERAND, 4);
             return 0;
         }
-    } else {
-        DPRINTF("invalid space\n");
+        break;
+    default:
+        DPRINTF("pcilg invalid space\n");
         setcc(cpu, ZPCI_PCI_LS_ERR);
         s390_set_status_code(env, r2, ZPCI_PCI_ST_INVAL_AS);
         return 0;
Signature · 1 line
2.7.4
#12 lore raw

Re: [Qemu-devel] [PATCH v2 3/7] s390x/pci: rework PCI LOAD

Quoted 5 lines · On Thu, 16 Nov 2017 18:51:51 +0100 Pierre Morel <[email protected]>…

Enhance the fault detection, correction of the fault reporting.

Same as last patch...

Quoted 34 lines
---
 hw/s390x/s390-pci-inst.c | 25 ++++++++++++++-----------
 1 file changed, 14 insertions(+), 11 deletions(-)
hw/s390x/s390-pci-inst.c +5 -5
index df0c8f0..cbc6421 100644
@@ -373,6 +373,11 @@ int pcilg_service_call(S390CPU *cpu, uint8_t r1, uint8_t r2)
     len = env->regs[r2] & 0xf;
     offset = env->regs[r2 + 1];
 
+    if (!(fh & FH_MASK_ENABLE)) {
+        setcc(cpu, ZPCI_PCI_LS_INVAL_HANDLE);
+        return 0;
+    }
+
     pbdev = s390_pci_find_dev_by_fh(s390_get_phb(), fh);
     if (!pbdev) {
         DPRINTF("pcilg no pci dev\n");
@@ -381,12 +386,7 @@ int pcilg_service_call(S390CPU *cpu, uint8_t r1, uint8_t r2)
     }
 
     switch (pbdev->state) {
-    case ZPCI_FS_RESERVED:
-    case ZPCI_FS_STANDBY:
-    case ZPCI_FS_DISABLED:
     case ZPCI_FS_PERMANENT_ERROR:
-        setcc(cpu, ZPCI_PCI_LS_INVAL_HANDLE);
-        return 0;
     case ZPCI_FS_ERROR:
         setcc(cpu, ZPCI_PCI_LS_ERR);
         s390_set_status_code(env, r2, ZPCI_PCI_ST_BLOCKED);

hw/s390x/s390-pci-inst.c int pcilg_service_call(S390CPU *cpu, uint8_t r1, uint8_t r2) · line 399

Quoted 7 lines
hw/s390x/s390-pci-inst.c +2 -2
@@ -395,8 +395,9 @@ int pcilg_service_call(S390CPU *cpu, uint8_t r1, uint8_t r2)
         break;
     }
 
-    if (pcias < 6) {
-        if ((8 - (offset & 0x7)) < len) {
+    switch (pcias) {
+    case 0 ... 5:

...and here...

Quoted 18 lines
hw/s390x/s390-pci-inst.c +4 -2
+        if (!len || (len > (8 - (offset & 0x7)))) {
             program_interrupt(env, PGM_OPERAND, 4);
             return 0;
         }
@@ -407,8 +408,9 @@ int pcilg_service_call(S390CPU *cpu, uint8_t r1, uint8_t r2)
             program_interrupt(env, PGM_OPERAND, 4);
             return 0;
         }
-    } else if (pcias == 15) {
-        if ((4 - (offset & 0x3)) < len) {
+        break;
+    case 15:
+        if (!len || (len > (4 - (offset & 0x3))) || len == 3) {
             program_interrupt(env, PGM_OPERAND, 4);
             return 0;
         }
@@ -419,8 +421,9 @@ int pcilg_service_call(S390CPU *cpu, uint8_t r1, uint8_t r2)
             program_interrupt(env, PGM_OPERAND, 4);

hw/s390x/s390-pci-inst.c int pcilg_service_call(S390CPU *cpu, uint8_t r1, uint8_t r2) · line 429

Quoted 10 lines
hw/s390x/s390-pci-inst.c +3 -2
             return 0;
         }
-    } else {
-        DPRINTF("invalid space\n");
+        break;
+    default:
+        DPRINTF("pcilg invalid space\n");
         setcc(cpu, ZPCI_PCI_LS_ERR);
         s390_set_status_code(env, r2, ZPCI_PCI_ST_INVAL_AS);
         return 0;

...and again here :) (good other than the nits)

#13 lore raw

PATCH v2 4/7 s390x/pci: rework PCI STORE BLOCK

In reply to Pierre Morel

Enhance the fault detection.

Fixup the precedence to check the destination path existance before checking for the source accessibility.

Add the maxstbl entry to both the Query PCI Function Group response and the PCIBusDevice structure.

Initialize the maxstbl to 128 per default until we get the actual data from the hardware.

---
 hw/s390x/s390-pci-bus.h  |  1 +
 hw/s390x/s390-pci-inst.c | 62 +++++++++++++++++++++++++++++-------------------
 hw/s390x/s390-pci-inst.h |  2 +-
 3 files changed, 40 insertions(+), 25 deletions(-)
hw/s390x/s390-pci-bus.h +1 -0
index 560bd82..2993f0d 100644
@@ -284,6 +284,7 @@ struct S390PCIBusDevice {
     uint64_t fmb_addr;
     uint8_t isc;
     uint16_t noi;
+    uint16_t maxstbl;
     uint8_t sum;
     S390MsixInfo msix;
     AdapterRoutes routes;
hw/s390x/s390-pci-inst.c +38 -24

107 lines not on this page · raw message

hw/s390x/s390-pci-inst.h +1 -1
index 94a959f..d6aeadd 100644
@@ -162,7 +162,7 @@ typedef struct ClpRspQueryPciGrp {
 #define CLP_RSP_QPCIG_MASK_FRAME   0x2
 #define CLP_RSP_QPCIG_MASK_REFRESH 0x1
     uint8_t fr;
-    uint16_t reserved2;
+    uint16_t maxstbl;
     uint16_t mui;
     uint64_t reserved3;
     uint64_t dasm; /* dma address space mask */
Signature · 1 line
2.7.4
#14 lore raw

Re: [Qemu-devel] [PATCH v2 4/7] s390x/pci: rework PCI STORE BLOCK

Quoted 26 lines · On Thu, 16 Nov 2017 18:51:52 +0100 Pierre Morel <[email protected]>…

Enhance the fault detection.

Fixup the precedence to check the destination path existance before checking for the source accessibility.

Add the maxstbl entry to both the Query PCI Function Group response and the PCIBusDevice structure.

Initialize the maxstbl to 128 per default until we get the actual data from the hardware.

---
 hw/s390x/s390-pci-bus.h  |  1 +
 hw/s390x/s390-pci-inst.c | 62 +++++++++++++++++++++++++++++-------------------
 hw/s390x/s390-pci-inst.h |  2 +-
 3 files changed, 40 insertions(+), 25 deletions(-)
hw/s390x/s390-pci-inst.c +3 -0
@@ -700,8 +685,33 @@ int pcistb_service_call(S390CPU *cpu, uint8_t r1, uint8_t r3, uint64_t gaddr,
         break;
     }
 
+    if (pcias > 5) {
+        DPRINTF("pcistb invalid space\n");
+        setcc(cpu, ZPCI_PCI_LS_ERR);

hw/s390x/s390-pci-inst.c int pcistb_service_call(S390CPU *cpu, uint8_t r1, uint8_t r3, uint64_t gaddr, · line 700

Quoted 10 lines
hw/s390x/s390-pci-inst.c +10 -0
+        s390_set_status_code(env, r1, ZPCI_PCI_ST_INVAL_AS);
+        return 0;
+    }
+
+    /* Verify the address, offset and length */
+    /* offset must be a multiple of 8 */
+    if (offset % 8) {
+        goto addressing_error;
+    }
+    /* Length must be greater than 8, a multiple of 8, not greater maxstbl */

"not greater than maxstlb"

Quoted 8 lines
hw/s390x/s390-pci-inst.c +8 -0
+    if ((len <= 8) || (len % 8) || (len > pbdev->maxstbl)) {
+        goto addressing_error;
+    }
+    /* Do not cross a 4K-byte boundary */
+    if (((offset & 0xfff) + len) > 0x1000) {
+        goto addressing_error;
+    }
+    /* Guest address must be double word aligned */

hw/s390x/s390-pci-inst.c int pcistb_service_call(S390CPU *cpu, uint8_t r1, uint8_t r3, uint64_t gaddr, · line 717

Quoted 10 lines
hw/s390x/s390-pci-inst.c +5 -1
+    if (gaddr & 0x07UL) {
+        goto addressing_error;
+    }
+
     mr = pbdev->pdev->io_regions[pcias].memory;
-    if (!memory_region_access_valid(mr, env->regs[r3], len, true)) {
+    if (!memory_region_access_valid(mr, offset, len, true)) {
         program_interrupt(env, PGM_OPERAND, 6);
         return 0;
     }

Looks good.

#15 lore raw

Re: [Qemu-devel] [PATCH v2 4/7] s390x/pci: rework PCI STORE BLOCK

Quoted 26 lines · Cornelia Huck
Quoted 23 lines · On Thu, 16 Nov 2017 18:51:52 +0100 Pierre Morel <[email protected]>…

Enhance the fault detection.

Fixup the precedence to check the destination path existance before checking for the source accessibility.

Add the maxstbl entry to both the Query PCI Function Group response and the PCIBusDevice structure.

Initialize the maxstbl to 128 per default until we get the actual data from the hardware.

---
  hw/s390x/s390-pci-bus.h  |  1 +
  hw/s390x/s390-pci-inst.c | 62 +++++++++++++++++++++++++++++-------------------
  hw/s390x/s390-pci-inst.h |  2 +-
  3 files changed, 40 insertions(+), 25 deletions(-)
(quoted diff) +0 -0
@@ -700,8 +685,33 @@ int pcistb_service_call(S390CPU *cpu, uint8_t r1, uint8_t r3, uint64_t gaddr,
          break;
      }
 
Quoted 15 lines

hw/s390x/s390-pci-inst.c int pcistb_service_call(S390CPU *cpu, uint8_t r1, uint8_t r3, uint64_t gaddr, · line 700

Quoted 13 lines
hw/s390x/s390-pci-inst.c +13 -0
+    if (pcias > 5) {
+        DPRINTF("pcistb invalid space\n");
+        setcc(cpu, ZPCI_PCI_LS_ERR);
+        s390_set_status_code(env, r1, ZPCI_PCI_ST_INVAL_AS);
+        return 0;
+    }
+
+    /* Verify the address, offset and length */
+    /* offset must be a multiple of 8 */
+    if (offset % 8) {
+        goto addressing_error;
+    }
+    /* Length must be greater than 8, a multiple of 8, not greater maxstbl */

"not greater than maxstlb"

Better I know but greater that 80 characters, this is why I preferred broken English. What do I do ? break the line or English ?

Quoted 22 lines
Quoted 9 lines
hw/s390x/s390-pci-inst.c +8 -0
+    if ((len <= 8) || (len % 8) || (len > pbdev->maxstbl)) {
+        goto addressing_error;
+    }
+    /* Do not cross a 4K-byte boundary */
+    if (((offset & 0xfff) + len) > 0x1000) {
+        goto addressing_error;
+    }
+    /* Guest address must be double word aligned */

hw/s390x/s390-pci-inst.c int pcistb_service_call(S390CPU *cpu, uint8_t r1, uint8_t r3, uint64_t gaddr, · line 717

Quoted 10 lines
hw/s390x/s390-pci-inst.c +5 -1
+    if (gaddr & 0x07UL) {
+        goto addressing_error;
+    }
+
      mr = pbdev->pdev->io_regions[pcias].memory;
-    if (!memory_region_access_valid(mr, env->regs[r3], len, true)) {
+    if (!memory_region_access_valid(mr, offset, len, true)) {
          program_interrupt(env, PGM_OPERAND, 6);
          return 0;
      }

Looks good.

Signature · 2 lines
Pierre Morel
Linux/KVM/QEMU in Böblingen - Germany
#16 lore raw

Re: [Qemu-devel] [PATCH v2 4/7] s390x/pci: rework PCI STORE BLOCK

Quoted 45 lines · Pierre Morel
Quoted 35 lines · Cornelia Huck
Quoted 32 lines · On Thu, 16 Nov 2017 18:51:52 +0100 Pierre Morel <[email protected]>…

Enhance the fault detection.

Fixup the precedence to check the destination path existance before checking for the source accessibility.

Add the maxstbl entry to both the Query PCI Function Group response and the PCIBusDevice structure.

Initialize the maxstbl to 128 per default until we get the actual data from the hardware.

---
  hw/s390x/s390-pci-bus.h  |  1 +
  hw/s390x/s390-pci-inst.c | 62
+++++++++++++++++++++++++++++-------------------
  hw/s390x/s390-pci-inst.h |  2 +-

  3 files changed, 40 insertions(+), 25 deletions(-)
hw/s390x/s390-pci-inst.c +7 -0
@@ -700,8 +685,33 @@ int pcistb_service_call(S390CPU *cpu, uint8_t 
 r1, uint8_t r3, uint64_t gaddr,
           break;
       }
   +    if (pcias > 5) {
+        DPRINTF("pcistb invalid space\n");
+        setcc(cpu, ZPCI_PCI_LS_ERR);
+        s390_set_status_code(env, r1, ZPCI_PCI_ST_INVAL_AS);
+        return 0;
+    }
+
+    /* Verify the address, offset and length */
Quoted 8 lines
Quoted 6 lines
hw/s390x/s390-pci-inst.c +5 -0
+    /* offset must be a multiple of 8 */
+    if (offset % 8) {
+        goto addressing_error;
+    }
+    /* Length must be greater than 8, a multiple of 8, not greater 

maxstbl */

"not greater than maxstlb"

Quoted 2 lines

Better I know but greater that 80 characters, this is why I preferred broken English. What do I do ? break the line or English ?

less than?

Quoted 25 lines
Quoted 23 lines
Quoted 12 lines

+    if ((len <= 8) || (len % 8) || (len > pbdev->maxstbl)) { +        goto addressing_error;
+    }
+    /* Do not cross a 4K-byte boundary */
+    if (((offset & 0xfff) + len) > 0x1000) {
+        goto addressing_error;
+    }
+    /* Guest address must be double word aligned */ +    if (gaddr & 0x07UL) {
+        goto addressing_error;

Quoted 8 lines

+    }
+
      mr = pbdev->pdev->io_regions[pcias].memory;
-    if (!memory_region_access_valid(mr, env->regs[r3], len, true)) { +    if (!memory_region_access_valid(mr, offset, len, true)) { program_interrupt(env, PGM_OPERAND, 6);
          return 0;
      }

Looks good.

#17 lore raw

Re: [Qemu-devel] [PATCH v2 4/7] s390x/pci: rework PCI STORE BLOCK

Quoted 46 lines · On Wed, 22 Nov 2017 13:15:21 +0800 Yi Min Zhao <[email protected]>…
Quoted 45 lines · Pierre Morel
Quoted 43 lines · Cornelia Huck
Quoted 37 lines · On Thu, 16 Nov 2017 18:51:52 +0100 Pierre Morel <[email protected]>…

Enhance the fault detection.

Fixup the precedence to check the destination path existance before checking for the source accessibility.

Add the maxstbl entry to both the Query PCI Function Group response and the PCIBusDevice structure.

Initialize the maxstbl to 128 per default until we get the actual data from the hardware.

---
  hw/s390x/s390-pci-bus.h  |  1 +
  hw/s390x/s390-pci-inst.c | 62
+++++++++++++++++++++++++++++-------------------
  hw/s390x/s390-pci-inst.h |  2 +-

  3 files changed, 40 insertions(+), 25 deletions(-)  
hw/s390x/s390-pci-inst.c +12 -0
@@ -700,8 +685,33 @@ int pcistb_service_call(S390CPU *cpu, uint8_t 
 r1, uint8_t r3, uint64_t gaddr,
           break;
       }
   +    if (pcias > 5) {
+        DPRINTF("pcistb invalid space\n");
+        setcc(cpu, ZPCI_PCI_LS_ERR);
+        s390_set_status_code(env, r1, ZPCI_PCI_ST_INVAL_AS);
+        return 0;
+    }
+
+    /* Verify the address, offset and length */
+    /* offset must be a multiple of 8 */
+    if (offset % 8) {
+        goto addressing_error;
+    }
+    /* Length must be greater than 8, a multiple of 8, not greater 
Quoted 1 line

maxstbl */

"not greater than maxstlb"

Quoted 4 lines
Quoted 3 lines

Better I know but greater that 80 characters, this is why I preferred broken English.
What do I do ? break the line or English ?

less than?

It's less or equal, no?

In any case, I prefer a multi-line comment over awkward language.

Quoted 26 lines
Quoted 25 lines
Quoted 23 lines
Quoted 12 lines

+    if ((len <= 8) || (len % 8) || (len > pbdev->maxstbl)) { +        goto addressing_error;
+    }
+    /* Do not cross a 4K-byte boundary */
+    if (((offset & 0xfff) + len) > 0x1000) {
+        goto addressing_error;
+    }
+    /* Guest address must be double word aligned */ +    if (gaddr & 0x07UL) {
+        goto addressing_error;

Quoted 8 lines

+    }
+
      mr = pbdev->pdev->io_regions[pcias].memory;
-    if (!memory_region_access_valid(mr, env->regs[r3], len, true)) { +    if (!memory_region_access_valid(mr, offset, len, true)) { program_interrupt(env, PGM_OPERAND, 6);
          return 0;
      }

Looks good.

#18 lore raw

Re: [Qemu-devel] [PATCH v2 4/7] s390x/pci: rework PCI STORE BLOCK

Quoted 49 lines · Cornelia Huck
Quoted 46 lines · On Wed, 22 Nov 2017 13:15:21 +0800 Yi Min Zhao <[email protected]>…
Quoted 45 lines · Pierre Morel
Quoted 43 lines · Cornelia Huck
Quoted 37 lines · On Thu, 16 Nov 2017 18:51:52 +0100 Pierre Morel <[email protected]>…

Enhance the fault detection.

Fixup the precedence to check the destination path existance before checking for the source accessibility.

Add the maxstbl entry to both the Query PCI Function Group response and the PCIBusDevice structure.

Initialize the maxstbl to 128 per default until we get the actual data from the hardware.

---
   hw/s390x/s390-pci-bus.h  |  1 +
   hw/s390x/s390-pci-inst.c | 62
+++++++++++++++++++++++++++++-------------------
   hw/s390x/s390-pci-inst.h |  2 +-
   3 files changed, 40 insertions(+), 25 deletions(-)
hw/s390x/s390-pci-inst.c +12 -0
@@ -700,8 +685,33 @@ int pcistb_service_call(S390CPU *cpu, uint8_t
 r1, uint8_t r3, uint64_t gaddr,
           break;
       }
   +    if (pcias > 5) {
+        DPRINTF("pcistb invalid space\n");
+        setcc(cpu, ZPCI_PCI_LS_ERR);
+        s390_set_status_code(env, r1, ZPCI_PCI_ST_INVAL_AS);
+        return 0;
+    }
+
+    /* Verify the address, offset and length */
+    /* offset must be a multiple of 8 */
+    if (offset % 8) {
+        goto addressing_error;
+    }
+    /* Length must be greater than 8, a multiple of 8, not greater
Quoted 1 line

maxstbl */

"not greater than maxstlb"

Quoted 9 lines
Quoted 4 lines
Quoted 3 lines

Better I know but greater that 80 characters, this is why I preferred broken English.
What do I do ? break the line or English ?

less than?

It's less or equal, no?

In any case, I prefer a multi-line comment over awkward language.

OK, thanks

Quoted 27 lines
Quoted 26 lines
Quoted 25 lines
Quoted 23 lines
Quoted 12 lines

+    if ((len <= 8) || (len % 8) || (len > pbdev->maxstbl)) { +        goto addressing_error;
+    }
+    /* Do not cross a 4K-byte boundary */
+    if (((offset & 0xfff) + len) > 0x1000) {
+        goto addressing_error;
+    }
+    /* Guest address must be double word aligned */ +    if (gaddr & 0x07UL) {
+        goto addressing_error;

Quoted 8 lines

+    }
+
      mr = pbdev->pdev->io_regions[pcias].memory;
-    if (!memory_region_access_valid(mr, env->regs[r3], len, true)) { +    if (!memory_region_access_valid(mr, offset, len, true)) {
          program_interrupt(env, PGM_OPERAND, 6);
          return 0;
      }

Looks good.

Signature · 2 lines
Pierre Morel
Linux/KVM/QEMU in Böblingen - Germany
#19 lore raw

PATCH v2 5/7 s390x/pci: move the memory region read from pcilg

In reply to Pierre Morel

Let's move the memory region read from pcilg into a dedicated function. This allows us to prepare a later patch.

---
 hw/s390x/s390-pci-inst.c | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)
hw/s390x/s390-pci-inst.c +11 -4
index 83b68a9..d46d4cd 100644
@@ -345,13 +345,22 @@ static int zpci_endian_swap(uint64_t *ptr, uint8_t len)
     return 0;
 }
 
+static MemTxResult zpci_read_bar(S390PCIBusDevice *pbdev, uint8_t pcias,
+                                 uint64_t offset, uint64_t *data, uint8_t len)
+{
+    MemoryRegion *mr;
+
+    mr = pbdev->pdev->io_regions[pcias].memory;
+    return memory_region_dispatch_read(mr, offset, data, len,
+                                       MEMTXATTRS_UNSPECIFIED);
+}
+
 int pcilg_service_call(S390CPU *cpu, uint8_t r1, uint8_t r2)
 {
     CPUS390XState *env = &cpu->env;
     S390PCIBusDevice *pbdev;
     uint64_t offset;
     uint64_t data;
-    MemoryRegion *mr;
     MemTxResult result;
     uint8_t len;
     uint32_t fh;
@@ -402,9 +411,7 @@ int pcilg_service_call(S390CPU *cpu, uint8_t r1, uint8_t r2)
             program_interrupt(env, PGM_OPERAND, 4);
             return 0;
         }
-        mr = pbdev->pdev->io_regions[pcias].memory;
-        result = memory_region_dispatch_read(mr, offset, &data, len,
-                                             MEMTXATTRS_UNSPECIFIED);
+        result = zpci_read_bar(pbdev, pcias, offset, &data, len);
         if (result != MEMTX_OK) {
             program_interrupt(env, PGM_OPERAND, 4);
             return 0;
Signature · 1 line
2.7.4
#20 lore raw

Re: [Qemu-devel] [PATCH v2 5/7] s390x/pci: move the memory region read from pcilg

Quoted 8 lines · On Thu, 16 Nov 2017 18:51:53 +0100 Pierre Morel <[email protected]>…

Let's move the memory region read from pcilg into a dedicated function. This allows us to prepare a later patch.

---
 hw/s390x/s390-pci-inst.c | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)

Looks good.

#21 lore raw

PATCH v2 6/7 s390x/pci: move the memory region write from pcistg

In reply to Pierre Morel

Let's move the memory region write from pcistg into a dedicated function.
This allows us to prepare a later patch searching for subregions inside of the memory region.

---
 hw/s390x/s390-pci-inst.c | 27 +++++++++++++++++----------
 1 file changed, 17 insertions(+), 10 deletions(-)
hw/s390x/s390-pci-inst.c +17 -10
index d46d4cd..eef3b89 100644
@@ -454,12 +454,27 @@ static int trap_msix(S390PCIBusDevice *pbdev, uint64_t offset, uint8_t pcias)
     }
 }
 
+static MemTxResult zpci_write_bar(S390PCIBusDevice *pbdev, uint8_t pcias,
+                                  uint64_t offset, uint64_t data, uint8_t len)
+{
+    MemoryRegion *mr;
+
+    if (trap_msix(pbdev, offset, pcias)) {
+        offset = offset - pbdev->msix.table_offset;
+        mr = &pbdev->pdev->msix_table_mmio;
+    } else {
+        mr = pbdev->pdev->io_regions[pcias].memory;
+    }
+
+    return memory_region_dispatch_write(mr, offset, data, len,
+                                        MEMTXATTRS_UNSPECIFIED);
+}
+
 int pcistg_service_call(S390CPU *cpu, uint8_t r1, uint8_t r2)
 {
     CPUS390XState *env = &cpu->env;
     uint64_t offset, data;
     S390PCIBusDevice *pbdev;
-    MemoryRegion *mr;
     MemTxResult result;
     uint8_t len;
     uint32_t fh;
@@ -516,15 +531,7 @@ int pcistg_service_call(S390CPU *cpu, uint8_t r1, uint8_t r2)
             return 0;
         }
 
-        if (trap_msix(pbdev, offset, pcias)) {
-            offset = offset - pbdev->msix.table_offset;
-            mr = &pbdev->pdev->msix_table_mmio;
-        } else {
-            mr = pbdev->pdev->io_regions[pcias].memory;
-        }
-
-        result = memory_region_dispatch_write(mr, offset, data, len,
-                                     MEMTXATTRS_UNSPECIFIED);
+        result = zpci_write_bar(pbdev, pcias, offset, data, len);
         if (result != MEMTX_OK) {
             program_interrupt(env, PGM_OPERAND, 4);
             return 0;
Signature · 1 line
2.7.4
#22 lore raw

Re: [Qemu-devel] [PATCH v2 6/7] s390x/pci: move the memory region write from pcistg

Quoted 10 lines · On Thu, 16 Nov 2017 18:51:54 +0100 Pierre Morel <[email protected]>…

Let's move the memory region write from pcistg into a dedicated function.
This allows us to prepare a later patch searching for subregions inside of the memory region.

---
 hw/s390x/s390-pci-inst.c | 27 +++++++++++++++++----------
 1 file changed, 17 insertions(+), 10 deletions(-)

Looks good.

#23 lore raw

PATCH v2 7/7 s390x/pci: search for subregion inside the BARs

In reply to Pierre Morel

When dispatching memory access to PCI BAR region, we must look for possible subregions, used by the PCI device to map different memory areas inside the same PCI BAR.

Since the data offset we received is calculated starting at the region start address we need to adjust the offset for the subregion.

The data offset inside the subregion is calculated by substracting the subregion's starting address from the data offset in the region.

The access to the MSIX region is now handled in a generic way, we do not need the specific trap_msix() function anymore.

---
 hw/s390x/s390-pci-inst.c | 44 +++++++++++++++++++++++++-------------------
 1 file changed, 25 insertions(+), 19 deletions(-)
hw/s390x/s390-pci-inst.c +25 -19
index eef3b89..33d08c6 100644
@@ -345,12 +345,31 @@ static int zpci_endian_swap(uint64_t *ptr, uint8_t len)
     return 0;
 }
 
+static MemoryRegion *s390_get_subregion(MemoryRegion *mr, uint64_t offset,
+                                        uint8_t len)
+{
+    MemoryRegion *other;
+    uint64_t subregion_size;
+
+    QTAILQ_FOREACH(other, &mr->subregions, subregions_link) {
+        subregion_size = int128_get64(other->size);
+        if ((offset >= other->addr) &&
+            (offset + len) <= (other->addr + subregion_size)) {
+            mr = other;
+            break;
+        }
+    }
+    return mr;
+}
+
 static MemTxResult zpci_read_bar(S390PCIBusDevice *pbdev, uint8_t pcias,
                                  uint64_t offset, uint64_t *data, uint8_t len)
 {
     MemoryRegion *mr;
 
     mr = pbdev->pdev->io_regions[pcias].memory;
+    mr = s390_get_subregion(mr, offset, len);
+    offset -= mr->addr;
     return memory_region_dispatch_read(mr, offset, data, len,
                                        MEMTXATTRS_UNSPECIFIED);
 }
@@ -442,30 +461,14 @@ int pcilg_service_call(S390CPU *cpu, uint8_t r1, uint8_t r2)
     return 0;
 }
 
-static int trap_msix(S390PCIBusDevice *pbdev, uint64_t offset, uint8_t pcias)
-{
-    if (pbdev->msix.available && pbdev->msix.table_bar == pcias &&
-        offset >= pbdev->msix.table_offset &&
-        offset < (pbdev->msix.table_offset +
-                  pbdev->msix.entries * PCI_MSIX_ENTRY_SIZE)) {
-        return 1;
-    } else {
-        return 0;
-    }
-}
-
 static MemTxResult zpci_write_bar(S390PCIBusDevice *pbdev, uint8_t pcias,
                                   uint64_t offset, uint64_t data, uint8_t len)
 {
     MemoryRegion *mr;
 
-    if (trap_msix(pbdev, offset, pcias)) {
-        offset = offset - pbdev->msix.table_offset;
-        mr = &pbdev->pdev->msix_table_mmio;
-    } else {
-        mr = pbdev->pdev->io_regions[pcias].memory;
-    }
-
+    mr = pbdev->pdev->io_regions[pcias].memory;
+    mr = s390_get_subregion(mr, offset, len);
+    offset -= mr->addr;
     return memory_region_dispatch_write(mr, offset, data, len,
                                         MEMTXATTRS_UNSPECIFIED);
 }
@@ -725,6 +728,9 @@ int pcistb_service_call(S390CPU *cpu, uint8_t r1, uint8_t r3, uint64_t gaddr,
     }
 
     mr = pbdev->pdev->io_regions[pcias].memory;
+    mr = s390_get_subregion(mr, offset, len);
+    offset -= mr->addr;
+
     if (!memory_region_access_valid(mr, offset, len, true)) {
         program_interrupt(env, PGM_OPERAND, 6);
         return 0;
Signature · 1 line
2.7.4
#24 lore raw

Re: [Qemu-devel] [PATCH v2 7/7] s390x/pci: search for subregion inside the BARs

Quoted 10 lines · On Thu, 16 Nov 2017 18:51:55 +0100 Pierre Morel <[email protected]>…

When dispatching memory access to PCI BAR region, we must look for possible subregions, used by the PCI device to map different memory areas inside the same PCI BAR.

Since the data offset we received is calculated starting at the region start address we need to adjust the offset for the subregion.

The data offset inside the subregion is calculated by substracting the subregion's starting address from the data offset in the region.

Quoted 8 lines

The access to the MSIX region is now handled in a generic way, we do not need the specific trap_msix() function anymore.

---
 hw/s390x/s390-pci-inst.c | 44 +++++++++++++++++++++++++-------------------
 1 file changed, 25 insertions(+), 19 deletions(-)

This one also looks fine.

#25 lore raw
In reply to Pierre Morel
Quoted 19 lines · On Thu, 16 Nov 2017 18:51:48 +0100 Pierre Morel <[email protected]>…

This patch fixes the following BUG:
Even a guest is able to detect virtio_pci device, the init function the Linux virtio_pci driver will hang because zPCI does not support the subregions used by virtio_pci.

It follows that right now the PCI support is very limited (e.g. pass through of a host vfio device)
To enable features like virtio-pci several modifications needs to be done.

As already stated above, Virtio-PCI uses subregions, which may eventually be discontinuous inside bars instead of a single flat region often used by real devices.
The address offset being formerly calculated from the BAR base address must be adapted to the subregions instead of to the single region.

This patch provides the new calculation for the three kind of BAR access, zPCI STORE, zPCI LOAD and zPCI STORE BLOCK done by zPCI.

Quoted 7 lines

We use the opportunity to
- enhance the fault detection for zPCI STORE and LOAD,
- enhance the fault detection and to provide the maximum STORE BLOCK block size, maxstbl, for zPCI STORE BLOCK
- factor out part of the code used to calculate the offset and access the BARs,
- factor out the code for endianess conversion.

I'll play with this a bit, but I think this is good for s390-next.

[I'd still like to play with this under tcg as well to make sure we are endian-clean, but I'm making slower progress there than I hoped.]

#26 lore raw
Quoted 30 lines · Cornelia Huck
Quoted 19 lines · On Thu, 16 Nov 2017 18:51:48 +0100 Pierre Morel <[email protected]>…

This patch fixes the following BUG:
Even a guest is able to detect virtio_pci device, the init function the Linux virtio_pci driver will hang because zPCI does not support the subregions used by virtio_pci.

It follows that right now the PCI support is very limited (e.g. pass through of a host vfio device)
To enable features like virtio-pci several modifications needs to be done.

As already stated above, Virtio-PCI uses subregions, which may eventually be discontinuous inside bars instead of a single flat region often used by real devices.
The address offset being formerly calculated from the BAR base address must be adapted to the subregions instead of to the single region.

This patch provides the new calculation for the three kind of BAR access, zPCI STORE, zPCI LOAD and zPCI STORE BLOCK done by zPCI.

Quoted 7 lines

We use the opportunity to
- enhance the fault detection for zPCI STORE and LOAD,
- enhance the fault detection and to provide the maximum STORE BLOCK block size, maxstbl, for zPCI STORE BLOCK
- factor out part of the code used to calculate the offset and access the BARs,
- factor out the code for endianess conversion.

Quoted 5 lines

I'll play with this a bit, but I think this is good for s390-next.

[I'd still like to play with this under tcg as well to make sure we are endian-clean, but I'm making slower progress there than I hoped.]

OK, thanks.
I wait for the answers for the points I am still not sure, (#define and broken English) and send a v3 with the corrections.

Best regards,

Pierre

Signature · 2 lines
Pierre Morel
Linux/KVM/QEMU in Böblingen - Germany