diff mbox series

[2/3] cxl: Support Get/Set Shutdown State commands

Message ID 20241220160026.204055-3-dave@stgolabs.net
State New
Headers show
Series hw/cxl: Support dirty shutdown | expand

Commit Message

Davidlohr Bueso Dec. 20, 2024, 4 p.m. UTC
As per the latest spec, add mailbox commands 4203h and 4204h.
While upon reboot this operation is obviously a nop (flag is
cleared but the dirty shutdown count is not increased), it
can still serve to test some basic paths.

Signed-off-by: Davidlohr Bueso <dave@stgolabs.net>
---
 hw/cxl/cxl-mailbox-utils.c  | 49 +++++++++++++++++++++++++++++++++++++
 include/hw/cxl/cxl_device.h |  2 ++
 2 files changed, 51 insertions(+)

Comments

Fan Ni Dec. 21, 2024, 7:18 p.m. UTC | #1
On Fri, Dec 20, 2024 at 08:00:25AM -0800, Davidlohr Bueso wrote:
> As per the latest spec, add mailbox commands 4203h and 4204h.
> While upon reboot this operation is obviously a nop (flag is
> cleared but the dirty shutdown count is not increased), it
> can still serve to test some basic paths.
> 
> Signed-off-by: Davidlohr Bueso <dave@stgolabs.net>

...

> ---
>  hw/cxl/cxl-mailbox-utils.c  | 49 +++++++++++++++++++++++++++++++++++++
>  include/hw/cxl/cxl_device.h |  2 ++
>  2 files changed, 51 insertions(+)
> 
> diff --git a/hw/cxl/cxl-mailbox-utils.c b/hw/cxl/cxl-mailbox-utils.c
> index f6f993e7bc4f..ff1d3f50610c 100644
> --- a/hw/cxl/cxl-mailbox-utils.c
> +++ b/hw/cxl/cxl-mailbox-utils.c
> @@ -86,6 +86,9 @@ enum {
>          #define GET_PARTITION_INFO     0x0
>          #define GET_LSA       0x2
>          #define SET_LSA       0x3
> +    HEALTH_INFO_ALERTS = 0x42,
> +        #define GET_SHUTDOWN_STATE     0x3
> +        #define SET_SHUTDOWN_STATE     0x4
>      MEDIA_AND_POISON = 0x43,
>          #define GET_POISON_LIST        0x0
>          #define INJECT_POISON          0x1
> @@ -1721,6 +1724,48 @@ static CXLRetCode cmd_sanitize_overwrite(const struct cxl_cmd *cmd,
>      return CXL_MBOX_BG_STARTED;
>  }
>  
> +/* CXL r3.2 Section 8.2.10.9.3.4: Get Shutdown State (Opcode 4203h) */
> +static CXLRetCode cmd_health_get_shutdown_state(const struct cxl_cmd *cmd,
> +                                                uint8_t *payload_in,
> +                                                size_t len_in,
> +                                                uint8_t *payload_out,
> +                                                size_t *len_out,
> +                                                CXLCCI *cci)
> +{
> +    CXLType3Dev *ct3d = CXL_TYPE3(cci->d);
> +    CXLDeviceState *cxl_dstate = &ct3d->cxl_dstate;
> +    struct get_shutdown_state_pl {
> +        uint8_t state;
> +    } QEMU_PACKED;
> +    struct get_shutdown_state_pl *out = (void *)payload_out;
> +
> +    out->state = cxl_dstate->shutdown_state;
> +    *len_out = sizeof(out);
> +
> +    return CXL_MBOX_SUCCESS;
> +}
> +
> +/* CXL r3.2 Section 8.2.10.9.3.5: Set Shutdown State (Opcode 4204h) */
> +static CXLRetCode cmd_health_set_shutdown_state(const struct cxl_cmd *cmd,
> +                                                uint8_t *payload_in,
> +                                                size_t len_in,
> +                                                uint8_t *payload_out,
> +                                                size_t *len_out,
> +                                                CXLCCI *cci)
> +{
> +    CXLType3Dev *ct3d = CXL_TYPE3(cci->d);
> +    CXLDeviceState *cxl_dstate = &ct3d->cxl_dstate;
> +    struct set_shutdown_state_pl {
> +        uint8_t state;
> +    } QEMU_PACKED;
> +    struct set_shutdown_state_pl *in = (void *)payload_in;

Do we need to check the input size and return invalid input payload
size?

> +
> +    cxl_dstate->shutdown_state = in->state;
> +    *len_out = 0;
> +
> +    return CXL_MBOX_SUCCESS;
> +}
> +
>  static CXLRetCode cmd_get_security_state(const struct cxl_cmd *cmd,
>                                           uint8_t *payload_in,
>                                           size_t len_in,
> @@ -2866,6 +2911,10 @@ static const struct cxl_cmd cxl_cmd_set[256][256] = {
>           CXL_MBOX_BACKGROUND_OPERATION_ABORT)},
>      [PERSISTENT_MEM][GET_SECURITY_STATE] = { "GET_SECURITY_STATE",
>          cmd_get_security_state, 0, 0 },
> +    [HEALTH_INFO_ALERTS][GET_SHUTDOWN_STATE] = { "HEALTH_INFO_ALERTS_GET_SHUTDOWN_STATE",
> +        cmd_health_get_shutdown_state, 0, 0 },
> +    [HEALTH_INFO_ALERTS][SET_SHUTDOWN_STATE] = { "HEALTH_INFO_ALERTS_SET_SHUTDOWN_STATE",
> +        cmd_health_set_shutdown_state, 1, 0 },

Per the spec, I think the return value should be CXL_MBOX_IMMEDIATE_POLICY_CHANGE.

Fan
>      [MEDIA_AND_POISON][GET_POISON_LIST] = { "MEDIA_AND_POISON_GET_POISON_LIST",
>          cmd_media_get_poison_list, 16, 0 },
>      [MEDIA_AND_POISON][INJECT_POISON] = { "MEDIA_AND_POISON_INJECT_POISON",
> diff --git a/include/hw/cxl/cxl_device.h b/include/hw/cxl/cxl_device.h
> index a64739be25e9..69e6330fe66d 100644
> --- a/include/hw/cxl/cxl_device.h
> +++ b/include/hw/cxl/cxl_device.h
> @@ -292,6 +292,8 @@ typedef struct cxl_device_state {
>      CPMUState cpmu[CXL_NUM_CPMU_INSTANCES];
>      CHMUState chmu[1];
>      CXLEventLog event_logs[CXL_EVENT_TYPE_MAX];
> +
> +    uint8_t shutdown_state;
>  } CXLDeviceState;
>  
>  /* Initialize the register block for a device */
> -- 
> 2.39.5
>
Jonathan Cameron Dec. 23, 2024, 7:57 p.m. UTC | #2
On Sat, 21 Dec 2024 11:18:48 -0800
Fan Ni <nifan.cxl@gmail.com> wrote:

> On Fri, Dec 20, 2024 at 08:00:25AM -0800, Davidlohr Bueso wrote:
> > As per the latest spec, add mailbox commands 4203h and 4204h.
> > While upon reboot this operation is obviously a nop (flag is
> > cleared but the dirty shutdown count is not increased), it
> > can still serve to test some basic paths.
> > 
> > Signed-off-by: Davidlohr Bueso <dave@stgolabs.net>  
> 
> ...
> 
> > ---
> >  hw/cxl/cxl-mailbox-utils.c  | 49 +++++++++++++++++++++++++++++++++++++
> >  include/hw/cxl/cxl_device.h |  2 ++
> >  2 files changed, 51 insertions(+)
> > 
> > diff --git a/hw/cxl/cxl-mailbox-utils.c b/hw/cxl/cxl-mailbox-utils.c
> > index f6f993e7bc4f..ff1d3f50610c 100644
> > --- a/hw/cxl/cxl-mailbox-utils.c
> > +++ b/hw/cxl/cxl-mailbox-utils.c
> > @@ -86,6 +86,9 @@ enum {
> >          #define GET_PARTITION_INFO     0x0
> >          #define GET_LSA       0x2
> >          #define SET_LSA       0x3
> > +    HEALTH_INFO_ALERTS = 0x42,
> > +        #define GET_SHUTDOWN_STATE     0x3
> > +        #define SET_SHUTDOWN_STATE     0x4
> >      MEDIA_AND_POISON = 0x43,
> >          #define GET_POISON_LIST        0x0
> >          #define INJECT_POISON          0x1
> > @@ -1721,6 +1724,48 @@ static CXLRetCode cmd_sanitize_overwrite(const struct cxl_cmd *cmd,
> >      return CXL_MBOX_BG_STARTED;
> >  }
> >  
> > +/* CXL r3.2 Section 8.2.10.9.3.4: Get Shutdown State (Opcode 4203h) */
> > +static CXLRetCode cmd_health_get_shutdown_state(const struct cxl_cmd *cmd,
> > +                                                uint8_t *payload_in,
> > +                                                size_t len_in,
> > +                                                uint8_t *payload_out,
> > +                                                size_t *len_out,
> > +                                                CXLCCI *cci)
> > +{
> > +    CXLType3Dev *ct3d = CXL_TYPE3(cci->d);
> > +    CXLDeviceState *cxl_dstate = &ct3d->cxl_dstate;
> > +    struct get_shutdown_state_pl {
> > +        uint8_t state;
> > +    } QEMU_PACKED;
> > +    struct get_shutdown_state_pl *out = (void *)payload_out;
> > +
> > +    out->state = cxl_dstate->shutdown_state;
> > +    *len_out = sizeof(out);
> > +
> > +    return CXL_MBOX_SUCCESS;
> > +}
> > +
> > +/* CXL r3.2 Section 8.2.10.9.3.5: Set Shutdown State (Opcode 4204h) */
> > +static CXLRetCode cmd_health_set_shutdown_state(const struct cxl_cmd *cmd,
> > +                                                uint8_t *payload_in,
> > +                                                size_t len_in,
> > +                                                uint8_t *payload_out,
> > +                                                size_t *len_out,
> > +                                                CXLCCI *cci)
> > +{
> > +    CXLType3Dev *ct3d = CXL_TYPE3(cci->d);
> > +    CXLDeviceState *cxl_dstate = &ct3d->cxl_dstate;
> > +    struct set_shutdown_state_pl {
> > +        uint8_t state;
> > +    } QEMU_PACKED;
> > +    struct set_shutdown_state_pl *in = (void *)payload_in;  
> 
> Do we need to check the input size and return invalid input payload
> size?

Not for fixed size commands.  See check in cxl_process_cci_message.
It's the variable sized commands that trip us up and need explicit
checks.


> 
> > +
> > +    cxl_dstate->shutdown_state = in->state;
> > +    *len_out = 0;
> > +
> > +    return CXL_MBOX_SUCCESS;
> > +}
> > +
> >  static CXLRetCode cmd_get_security_state(const struct cxl_cmd *cmd,
> >                                           uint8_t *payload_in,
> >                                           size_t len_in,
> > @@ -2866,6 +2911,10 @@ static const struct cxl_cmd cxl_cmd_set[256][256] = {
> >           CXL_MBOX_BACKGROUND_OPERATION_ABORT)},
> >      [PERSISTENT_MEM][GET_SECURITY_STATE] = { "GET_SECURITY_STATE",
> >          cmd_get_security_state, 0, 0 },
> > +    [HEALTH_INFO_ALERTS][GET_SHUTDOWN_STATE] = { "HEALTH_INFO_ALERTS_GET_SHUTDOWN_STATE",
> > +        cmd_health_get_shutdown_state, 0, 0 },
> > +    [HEALTH_INFO_ALERTS][SET_SHUTDOWN_STATE] = { "HEALTH_INFO_ALERTS_SET_SHUTDOWN_STATE",
> > +        cmd_health_set_shutdown_state, 1, 0 },  
> 
> Per the spec, I think the return value should be CXL_MBOX_IMMEDIATE_POLICY_CHANGE.
> 
> Fan
> >      [MEDIA_AND_POISON][GET_POISON_LIST] = { "MEDIA_AND_POISON_GET_POISON_LIST",
> >          cmd_media_get_poison_list, 16, 0 },
> >      [MEDIA_AND_POISON][INJECT_POISON] = { "MEDIA_AND_POISON_INJECT_POISON",
> > diff --git a/include/hw/cxl/cxl_device.h b/include/hw/cxl/cxl_device.h
> > index a64739be25e9..69e6330fe66d 100644
> > --- a/include/hw/cxl/cxl_device.h
> > +++ b/include/hw/cxl/cxl_device.h
> > @@ -292,6 +292,8 @@ typedef struct cxl_device_state {
> >      CPMUState cpmu[CXL_NUM_CPMU_INSTANCES];
> >      CHMUState chmu[1];
> >      CXLEventLog event_logs[CXL_EVENT_TYPE_MAX];
> > +
> > +    uint8_t shutdown_state;
> >  } CXLDeviceState;
> >  
> >  /* Initialize the register block for a device */
> > -- 
> > 2.39.5
> >
Jonathan Cameron Dec. 23, 2024, 8:01 p.m. UTC | #3
On Fri, 20 Dec 2024 08:00:25 -0800
Davidlohr Bueso <dave@stgolabs.net> wrote:

> As per the latest spec, add mailbox commands 4203h and 4204h.
> While upon reboot this operation is obviously a nop (flag is
> cleared but the dirty shutdown count is not increased), it
> can still serve to test some basic paths.
> 
> Signed-off-by: Davidlohr Bueso <dave@stgolabs.net>
Hi Davidlohr,,

Some trivial stuff inline.


Thanks,

Jonathan

> ---
>  hw/cxl/cxl-mailbox-utils.c  | 49 +++++++++++++++++++++++++++++++++++++
>  include/hw/cxl/cxl_device.h |  2 ++
>  2 files changed, 51 insertions(+)
> 
> diff --git a/hw/cxl/cxl-mailbox-utils.c b/hw/cxl/cxl-mailbox-utils.c
> index f6f993e7bc4f..ff1d3f50610c 100644
> --- a/hw/cxl/cxl-mailbox-utils.c
> +++ b/hw/cxl/cxl-mailbox-utils.c
> @@ -86,6 +86,9 @@ enum {
>          #define GET_PARTITION_INFO     0x0
>          #define GET_LSA       0x2
>          #define SET_LSA       0x3
> +    HEALTH_INFO_ALERTS = 0x42,
> +        #define GET_SHUTDOWN_STATE     0x3
> +        #define SET_SHUTDOWN_STATE     0x4
>      MEDIA_AND_POISON = 0x43,
>          #define GET_POISON_LIST        0x0
>          #define INJECT_POISON          0x1
> @@ -1721,6 +1724,48 @@ static CXLRetCode cmd_sanitize_overwrite(const struct cxl_cmd *cmd,
>      return CXL_MBOX_BG_STARTED;
>  }
>  
> +/* CXL r3.2 Section 8.2.10.9.3.4: Get Shutdown State (Opcode 4203h) */
> +static CXLRetCode cmd_health_get_shutdown_state(const struct cxl_cmd *cmd,
> +                                                uint8_t *payload_in,
> +                                                size_t len_in,
> +                                                uint8_t *payload_out,
> +                                                size_t *len_out,
> +                                                CXLCCI *cci)
> +{
> +    CXLType3Dev *ct3d = CXL_TYPE3(cci->d);
> +    CXLDeviceState *cxl_dstate = &ct3d->cxl_dstate;
> +    struct get_shutdown_state_pl {
> +        uint8_t state;
> +    } QEMU_PACKED;
> +    struct get_shutdown_state_pl *out = (void *)payload_out;
We only need the (void *) for unnamed types.  Where we know the type
might as well get it right. E.g. see cmd_events_set_interrupt_policy.


Mind you, not much point in keeping the type named here.
	struct {
		uint8_t state;
	} QEMU_PACKED = (void *)payload_out;


> +
> +    out->state = cxl_dstate->shutdown_state;
> +    *len_out = sizeof(out);
> +
> +    return CXL_MBOX_SUCCESS;
> +}
> +
> +/* CXL r3.2 Section 8.2.10.9.3.5: Set Shutdown State (Opcode 4204h) */
> +static CXLRetCode cmd_health_set_shutdown_state(const struct cxl_cmd *cmd,
> +                                                uint8_t *payload_in,
> +                                                size_t len_in,
> +                                                uint8_t *payload_out,
> +                                                size_t *len_out,
> +                                                CXLCCI *cci)
> +{
> +    CXLType3Dev *ct3d = CXL_TYPE3(cci->d);
> +    CXLDeviceState *cxl_dstate = &ct3d->cxl_dstate;
> +    struct set_shutdown_state_pl {
> +        uint8_t state;
> +    } QEMU_PACKED;

As above. Naming the type isn't that useful and if you want
to should cast to that not a void *

> +    struct set_shutdown_state_pl *in = (void *)payload_in;
> +
> +    cxl_dstate->shutdown_state = in->state;
> +    *len_out = 0;
> +
> +    return CXL_MBOX_SUCCESS;
> +}
> +
>  static CXLRetCode cmd_get_security_state(const struct cxl_cmd *cmd,
>                                           uint8_t *payload_in,
>                                           size_t len_in,
> @@ -2866,6 +2911,10 @@ static const struct cxl_cmd cxl_cmd_set[256][256] = {
>           CXL_MBOX_BACKGROUND_OPERATION_ABORT)},
>      [PERSISTENT_MEM][GET_SECURITY_STATE] = { "GET_SECURITY_STATE",
>          cmd_get_security_state, 0, 0 },
> +    [HEALTH_INFO_ALERTS][GET_SHUTDOWN_STATE] = { "HEALTH_INFO_ALERTS_GET_SHUTDOWN_STATE",
> +        cmd_health_get_shutdown_state, 0, 0 },
> +    [HEALTH_INFO_ALERTS][SET_SHUTDOWN_STATE] = { "HEALTH_INFO_ALERTS_SET_SHUTDOWN_STATE",
> +        cmd_health_set_shutdown_state, 1, 0 },
>      [MEDIA_AND_POISON][GET_POISON_LIST] = { "MEDIA_AND_POISON_GET_POISON_LIST",
>          cmd_media_get_poison_list, 16, 0 },
>      [MEDIA_AND_POISON][INJECT_POISON] = { "MEDIA_AND_POISON_INJECT_POISON",
> diff --git a/include/hw/cxl/cxl_device.h b/include/hw/cxl/cxl_device.h
> index a64739be25e9..69e6330fe66d 100644
> --- a/include/hw/cxl/cxl_device.h
> +++ b/include/hw/cxl/cxl_device.h
> @@ -292,6 +292,8 @@ typedef struct cxl_device_state {
>      CPMUState cpmu[CXL_NUM_CPMU_INSTANCES];
>      CHMUState chmu[1];
>      CXLEventLog event_logs[CXL_EVENT_TYPE_MAX];
> +
> +    uint8_t shutdown_state;
>  } CXLDeviceState;
>  
>  /* Initialize the register block for a device */
diff mbox series

Patch

diff --git a/hw/cxl/cxl-mailbox-utils.c b/hw/cxl/cxl-mailbox-utils.c
index f6f993e7bc4f..ff1d3f50610c 100644
--- a/hw/cxl/cxl-mailbox-utils.c
+++ b/hw/cxl/cxl-mailbox-utils.c
@@ -86,6 +86,9 @@  enum {
         #define GET_PARTITION_INFO     0x0
         #define GET_LSA       0x2
         #define SET_LSA       0x3
+    HEALTH_INFO_ALERTS = 0x42,
+        #define GET_SHUTDOWN_STATE     0x3
+        #define SET_SHUTDOWN_STATE     0x4
     MEDIA_AND_POISON = 0x43,
         #define GET_POISON_LIST        0x0
         #define INJECT_POISON          0x1
@@ -1721,6 +1724,48 @@  static CXLRetCode cmd_sanitize_overwrite(const struct cxl_cmd *cmd,
     return CXL_MBOX_BG_STARTED;
 }
 
+/* CXL r3.2 Section 8.2.10.9.3.4: Get Shutdown State (Opcode 4203h) */
+static CXLRetCode cmd_health_get_shutdown_state(const struct cxl_cmd *cmd,
+                                                uint8_t *payload_in,
+                                                size_t len_in,
+                                                uint8_t *payload_out,
+                                                size_t *len_out,
+                                                CXLCCI *cci)
+{
+    CXLType3Dev *ct3d = CXL_TYPE3(cci->d);
+    CXLDeviceState *cxl_dstate = &ct3d->cxl_dstate;
+    struct get_shutdown_state_pl {
+        uint8_t state;
+    } QEMU_PACKED;
+    struct get_shutdown_state_pl *out = (void *)payload_out;
+
+    out->state = cxl_dstate->shutdown_state;
+    *len_out = sizeof(out);
+
+    return CXL_MBOX_SUCCESS;
+}
+
+/* CXL r3.2 Section 8.2.10.9.3.5: Set Shutdown State (Opcode 4204h) */
+static CXLRetCode cmd_health_set_shutdown_state(const struct cxl_cmd *cmd,
+                                                uint8_t *payload_in,
+                                                size_t len_in,
+                                                uint8_t *payload_out,
+                                                size_t *len_out,
+                                                CXLCCI *cci)
+{
+    CXLType3Dev *ct3d = CXL_TYPE3(cci->d);
+    CXLDeviceState *cxl_dstate = &ct3d->cxl_dstate;
+    struct set_shutdown_state_pl {
+        uint8_t state;
+    } QEMU_PACKED;
+    struct set_shutdown_state_pl *in = (void *)payload_in;
+
+    cxl_dstate->shutdown_state = in->state;
+    *len_out = 0;
+
+    return CXL_MBOX_SUCCESS;
+}
+
 static CXLRetCode cmd_get_security_state(const struct cxl_cmd *cmd,
                                          uint8_t *payload_in,
                                          size_t len_in,
@@ -2866,6 +2911,10 @@  static const struct cxl_cmd cxl_cmd_set[256][256] = {
          CXL_MBOX_BACKGROUND_OPERATION_ABORT)},
     [PERSISTENT_MEM][GET_SECURITY_STATE] = { "GET_SECURITY_STATE",
         cmd_get_security_state, 0, 0 },
+    [HEALTH_INFO_ALERTS][GET_SHUTDOWN_STATE] = { "HEALTH_INFO_ALERTS_GET_SHUTDOWN_STATE",
+        cmd_health_get_shutdown_state, 0, 0 },
+    [HEALTH_INFO_ALERTS][SET_SHUTDOWN_STATE] = { "HEALTH_INFO_ALERTS_SET_SHUTDOWN_STATE",
+        cmd_health_set_shutdown_state, 1, 0 },
     [MEDIA_AND_POISON][GET_POISON_LIST] = { "MEDIA_AND_POISON_GET_POISON_LIST",
         cmd_media_get_poison_list, 16, 0 },
     [MEDIA_AND_POISON][INJECT_POISON] = { "MEDIA_AND_POISON_INJECT_POISON",
diff --git a/include/hw/cxl/cxl_device.h b/include/hw/cxl/cxl_device.h
index a64739be25e9..69e6330fe66d 100644
--- a/include/hw/cxl/cxl_device.h
+++ b/include/hw/cxl/cxl_device.h
@@ -292,6 +292,8 @@  typedef struct cxl_device_state {
     CPMUState cpmu[CXL_NUM_CPMU_INSTANCES];
     CHMUState chmu[1];
     CXLEventLog event_logs[CXL_EVENT_TYPE_MAX];
+
+    uint8_t shutdown_state;
 } CXLDeviceState;
 
 /* Initialize the register block for a device */