[PATCH 1 of 2] inmates: x86: match pci device class in ivshmem-demo

6 views
Skip to first unread message

Henning Schild

unread,
Nov 30, 2016, 11:49:32 AM11/30/16
to jailho...@googlegroups.com, Henning Schild
Match the device class and "protocol" while probing and skip unknow
devices.

Signed-off-by: Henning Schild <henning...@siemens.com>

diff --git a/inmates/demos/x86/ivshmem-demo.c b/inmates/demos/x86/ivshmem-demo.c
--- a/inmates/demos/x86/ivshmem-demo.c
+++ b/inmates/demos/x86/ivshmem-demo.c
@@ -114,6 +114,7 @@
{
int i;
int bdf = 0;
+ unsigned int class_rev;
struct ivshmem_dev_data *d;
volatile char *shmem;

@@ -121,14 +122,19 @@

int_init();

-again:
- bdf = pci_find_device(VENDORID, DEVICEID, bdf);
- if (bdf != -1) {
+ while ((ndevices < MAX_NDEV) &&
+ (-1 != (bdf = pci_find_device(VENDORID, DEVICEID, bdf)))) {
printk("IVSHMEM: Found %04x:%04x at %02x:%02x.%x\n",
pci_read_config(bdf, PCI_CFG_VENDOR_ID, 2),
pci_read_config(bdf, PCI_CFG_DEVICE_ID, 2),
bdf >> 8, (bdf >> 3) & 0x1f, bdf & 0x3);
-
+ class_rev = pci_read_config(bdf, 0x8, 4);
+ if (class_rev != PCI_DEV_CLASS_OTHER << 24) {
+ printk("IVSHMEM: class/revision %08x, not supported "
+ "skipping device\n", class_rev);
+ bdf++;
+ continue;
+ }
ndevices++;
d = devs + ndevices - 1;
d->bdf = bdf;
@@ -141,8 +147,6 @@
int_set_handler(IRQ_VECTOR + ndevices - 1, irq_handler);
pci_msix_set_vector(bdf, IRQ_VECTOR + ndevices - 1, 0);
bdf++;
- if (ndevices < MAX_NDEV)
- goto again;
}

if (!ndevices) {
diff --git a/inmates/lib/x86/inmate.h b/inmates/lib/x86/inmate.h
--- a/inmates/lib/x86/inmate.h
+++ b/inmates/lib/x86/inmate.h
@@ -53,6 +53,8 @@

#define PCI_ID_ANY 0xffff

+#define PCI_DEV_CLASS_OTHER 0xff
+
#define PCI_CAP_MSI 0x05
#define PCI_CAP_MSIX 0x11

Jan Kiszka

unread,
Nov 30, 2016, 12:18:51 PM11/30/16
to Henning Schild, jailho...@googlegroups.com
Maybe explicitly state here that we are matching on
SHMEM_PROTO_UNDEFINED as well.

> + printk("IVSHMEM: class/revision %08x, not supported "
> + "skipping device\n", class_rev);
> + bdf++;
> + continue;
> + }
> ndevices++;
> d = devs + ndevices - 1;
> d->bdf = bdf;
> @@ -141,8 +147,6 @@
> int_set_handler(IRQ_VECTOR + ndevices - 1, irq_handler);
> pci_msix_set_vector(bdf, IRQ_VECTOR + ndevices - 1, 0);
> bdf++;
> - if (ndevices < MAX_NDEV)
> - goto again;
> }
>
> if (!ndevices) {
> diff --git a/inmates/lib/x86/inmate.h b/inmates/lib/x86/inmate.h
> --- a/inmates/lib/x86/inmate.h
> +++ b/inmates/lib/x86/inmate.h
> @@ -53,6 +53,8 @@
>
> #define PCI_ID_ANY 0xffff
>
> +#define PCI_DEV_CLASS_OTHER 0xff
> +
> #define PCI_CAP_MSI 0x05
> #define PCI_CAP_MSIX 0x11
>
>

Jan

--
Siemens AG, Corporate Technology, CT RDA ITP SES-DE
Corporate Competence Center Embedded Linux

Henning Schild

unread,
Dec 5, 2016, 7:19:51 AM12/5/16
to jailho...@googlegroups.com, henning...@siemens.com, Jan Kiszka
Match the device class and "protocol" while probing and skip unknow
devices.

Signed-off-by: Henning Schild <henning...@siemens.com>

diff --git a/inmates/demos/x86/ivshmem-demo.c b/inmates/demos/x86/ivshmem-demo.c
--- a/inmates/demos/x86/ivshmem-demo.c
+++ b/inmates/demos/x86/ivshmem-demo.c
@@ -1,7 +1,7 @@
/*
* Jailhouse, a Linux-based partitioning hypervisor
*
- * Copyright (c) Siemens AG, 2014
+ * Copyright (c) Siemens AG, 2016
*
* Authors:
* Henning Schild <henning...@siemens.com>
@@ -17,6 +17,8 @@
#define IVSHMEM_CFG_SHMEM_PTR 0x40
#define IVSHMEM_CFG_SHMEM_SZ 0x48

+#define JAILHOUSE_SHMEM_PROTO_UNDEFINED 0x0000
+
#define IRQ_VECTOR 32

#define MAX_NDEV 4
@@ -110,23 +112,36 @@
printk("IVSHMEM: got interrupt ... %d\n", irq_counter++);
}

+static int match_class_rev(unsigned int class_rev)
+{
+ return (class_rev & 0xffffff00) ==
+ ((PCI_DEV_CLASS_OTHER << 24 |
+ JAILHOUSE_SHMEM_PROTO_UNDEFINED << 8) & 0xffffff00);
+}
+
void inmate_main(void)
{
int i;
int bdf = 0;
+ unsigned int class_rev;
struct ivshmem_dev_data *d;
volatile char *shmem;

int_init();

-again:
- bdf = pci_find_device(VENDORID, DEVICEID, bdf);
- if (bdf != -1) {
+ while ((ndevices < MAX_NDEV) &&
+ (-1 != (bdf = pci_find_device(VENDORID, DEVICEID, bdf)))) {
printk("IVSHMEM: Found %04x:%04x at %02x:%02x.%x\n",
pci_read_config(bdf, PCI_CFG_VENDOR_ID, 2),
pci_read_config(bdf, PCI_CFG_DEVICE_ID, 2),
bdf >> 8, (bdf >> 3) & 0x1f, bdf & 0x3);
-
+ class_rev = pci_read_config(bdf, 0x8, 4);
+ if (!match_class_rev(class_rev)) {
+ printk("IVSHMEM: class/revision %08x, not supported "
+ "skipping device\n", class_rev);
+ bdf++;
+ continue;
+ }
ndevices++;
d = devs + ndevices - 1;
d->bdf = bdf;
@@ -139,8 +154,6 @@

Jan Kiszka

unread,
Dec 5, 2016, 7:37:24 AM12/5/16
to Henning Schild, jailho...@googlegroups.com
On 2016-12-05 13:20, Henning Schild wrote:
> Match the device class and "protocol" while probing and skip unknow
> devices.
>
> Signed-off-by: Henning Schild <henning...@siemens.com>
>
> diff --git a/inmates/demos/x86/ivshmem-demo.c b/inmates/demos/x86/ivshmem-demo.c
> --- a/inmates/demos/x86/ivshmem-demo.c
> +++ b/inmates/demos/x86/ivshmem-demo.c
> @@ -1,7 +1,7 @@
> /*
> * Jailhouse, a Linux-based partitioning hypervisor
> *
> - * Copyright (c) Siemens AG, 2014
> + * Copyright (c) Siemens AG, 2016

"2014-2016"

> *
> * Authors:
> * Henning Schild <henning...@siemens.com>
> @@ -17,6 +17,8 @@
> #define IVSHMEM_CFG_SHMEM_PTR 0x40
> #define IVSHMEM_CFG_SHMEM_SZ 0x48
>
> +#define JAILHOUSE_SHMEM_PROTO_UNDEFINED 0x0000
> +
> #define IRQ_VECTOR 32
>
> #define MAX_NDEV 4
> @@ -110,23 +112,36 @@
> printk("IVSHMEM: got interrupt ... %d\n", irq_counter++);
> }
>
> +static int match_class_rev(unsigned int class_rev)
> +{
> + return (class_rev & 0xffffff00) ==
> + ((PCI_DEV_CLASS_OTHER << 24 |
> + JAILHOUSE_SHMEM_PROTO_UNDEFINED << 8) & 0xffffff00);

The masking of constants is a bit overkill, so is moving this simple
condition into single-user function IMHO.

> +}
> +
> void inmate_main(void)
> {
> int i;
> int bdf = 0;
> + unsigned int class_rev;
> struct ivshmem_dev_data *d;
> volatile char *shmem;
>
> int_init();
>
> -again:
> - bdf = pci_find_device(VENDORID, DEVICEID, bdf);
> - if (bdf != -1) {
> + while ((ndevices < MAX_NDEV) &&
> + (-1 != (bdf = pci_find_device(VENDORID, DEVICEID, bdf)))) {
> printk("IVSHMEM: Found %04x:%04x at %02x:%02x.%x\n",
> pci_read_config(bdf, PCI_CFG_VENDOR_ID, 2),
> pci_read_config(bdf, PCI_CFG_DEVICE_ID, 2),
> bdf >> 8, (bdf >> 3) & 0x1f, bdf & 0x3);
> -
> + class_rev = pci_read_config(bdf, 0x8, 4);
> + if (!match_class_rev(class_rev)) {
> + printk("IVSHMEM: class/revision %08x, not supported "
> + "skipping device\n", class_rev);

Missed in round #1: This will generate a warning when running in QEMU
e.g. (or any other system config that also has a VETH device). Rather
report about the picked device, I would say.

> + bdf++;
> + continue;
> + }
> ndevices++;
> d = devs + ndevices - 1;
> d->bdf = bdf;
> @@ -139,8 +154,6 @@
> int_set_handler(IRQ_VECTOR + ndevices - 1, irq_handler);
> pci_msix_set_vector(bdf, IRQ_VECTOR + ndevices - 1, 0);
> bdf++;
> - if (ndevices < MAX_NDEV)
> - goto again;
> }
>
> if (!ndevices) {
> diff --git a/inmates/lib/x86/inmate.h b/inmates/lib/x86/inmate.h
> --- a/inmates/lib/x86/inmate.h
> +++ b/inmates/lib/x86/inmate.h
> @@ -53,6 +53,8 @@
>
> #define PCI_ID_ANY 0xffff
>
> +#define PCI_DEV_CLASS_OTHER 0xff
> +
> #define PCI_CAP_MSI 0x05
> #define PCI_CAP_MSIX 0x11
>
>

Henning Schild

unread,
Dec 5, 2016, 7:53:04 AM12/5/16
to jailho...@googlegroups.com, henning...@siemens.com, Jan Kiszka
Match the device class and "protocol" while probing and skip unknow
devices.

Signed-off-by: Henning Schild <henning...@siemens.com>

diff --git a/inmates/demos/x86/ivshmem-demo.c b/inmates/demos/x86/ivshmem-demo.c
--- a/inmates/demos/x86/ivshmem-demo.c
+++ b/inmates/demos/x86/ivshmem-demo.c
@@ -1,7 +1,7 @@
/*
* Jailhouse, a Linux-based partitioning hypervisor
*
- * Copyright (c) Siemens AG, 2014
+ * Copyright (c) Siemens AG, 2014-2016
*
* Authors:
* Henning Schild <henning...@siemens.com>
@@ -17,6 +17,8 @@
#define IVSHMEM_CFG_SHMEM_PTR 0x40
#define IVSHMEM_CFG_SHMEM_SZ 0x48

+#define JAILHOUSE_SHMEM_PROTO_UNDEFINED 0x0000
+
#define IRQ_VECTOR 32

#define MAX_NDEV 4
@@ -114,19 +116,26 @@
{
int i;
int bdf = 0;
+ unsigned int class_rev;
struct ivshmem_dev_data *d;
volatile char *shmem;

int_init();

-again:
- bdf = pci_find_device(VENDORID, DEVICEID, bdf);
- if (bdf != -1) {
+ while ((ndevices < MAX_NDEV) &&
+ (-1 != (bdf = pci_find_device(VENDORID, DEVICEID, bdf)))) {
printk("IVSHMEM: Found %04x:%04x at %02x:%02x.%x\n",
pci_read_config(bdf, PCI_CFG_VENDOR_ID, 2),
pci_read_config(bdf, PCI_CFG_DEVICE_ID, 2),
bdf >> 8, (bdf >> 3) & 0x1f, bdf & 0x3);
-
+ class_rev = pci_read_config(bdf, 0x8, 4);
+ if (class_rev != (PCI_DEV_CLASS_OTHER << 24 |
+ JAILHOUSE_SHMEM_PROTO_UNDEFINED << 8)) {
+ printk("IVSHMEM: class/revision %08x, not supported "
+ "skipping device\n", class_rev);
+ bdf++;
+ continue;
+ }
ndevices++;
d = devs + ndevices - 1;
d->bdf = bdf;
@@ -139,8 +148,6 @@

Jan Kiszka

unread,
Dec 6, 2016, 5:37:43 AM12/6/16
to Henning Schild, jailho...@googlegroups.com
Thanks, merged this and rebased patch 2 over next. Will post the
underlying ivshmem fixes and refactorings soon.
Reply all
Reply to author
Forward
0 new messages