Skip to content

mi: add nvme_mi_mi_controller_health_status_poll - #1144

Merged
igaw merged 2 commits into
linux-nvme:masterfrom
drakedog2008:ctrl-health-poll
Nov 13, 2026
Merged

igaw merged 2 commits into
linux-nvme:masterfrom
drakedog2008:ctrl-health-poll

Conversation

@drakedog2008

Copy link
Copy Markdown
Contributor

Implement Controller Health Status Poll (Opcode 02h) defined in NVMe-MI 2.0 (Section 5.3 & 5.3.1).

  • Add struct nvme_mi_ctrl_health_poll_args to allow comprehensive query configuration (start_ctrl_id, clear, all, inc_pci, inc_sriov_pf, inc_sriov_vf, and filter_* flags).
  • Add nvme_mi_mi_controller_health_status_poll() with auto-paging multi-submit loop when num_entries > 255 (up to 255 entries per submit, advancing SCTLID = last_ctlid + 1).
  • Add convenience inline helper nvme_mi_mi_controller_health_status_poll_all() for querying all controllers starting from ID 0.
  • Update struct nvme_mi_ctrl_health_status (CHDS) to include the 16-bit chsc[2] (Controller Health Status Changed Flags) at offset 9 and rsvd11[5].
  • Add enum nvme_mi_chsc and update enum nvme_mi_csts with TCIDA.
  • Export nvme_mi_mi_controller_health_status_poll in libnvme-mi.map.
  • Add unit tests in test/mi.c covering single-entry query, selective flags, auto-paging across chunk boundaries, and error handling.

@drakedog2008

Copy link
Copy Markdown
Contributor Author

Hi @jk-ozlabs,

Can you help review this PR?

@jk-ozlabs jk-ozlabs self-assigned this Sep 3, 2026
@igaw

igaw commented Sep 5, 2026

Copy link
Copy Markdown
Collaborator

Would you mind to rebase it, there is a conflict due to changes I merged before this one?

And could you check if the libnvme3 has also all bits? I think I added this change but better to double check. Thanks!

@drakedog2008

Copy link
Copy Markdown
Contributor Author

Hi @igaw

I rebased the code and created a similar PR for the libnvme3

linux-nvme/nvme-cli#4006

Comment thread src/nvme/mi.h Resolved
Comment thread src/nvme/types.h Resolved
Comment thread src/nvme/mi.c Resolved
@igaw

igaw commented Sep 10, 2026

Copy link
Copy Markdown
Collaborator

Hi @igaw

I rebased the code and created a similar PR for the libnvme3

Thanks!

The v3 has been released, do you really need this for v1? I'd rather not add new features to the libnvme v1 library at this piont.

@drakedog2008

Copy link
Copy Markdown
Contributor Author

Hi @igaw
I rebased the code and created a similar PR for the libnvme3

Thanks!

The v3 has been released, do you really need this for v1? I'd rather not add new features to the libnvme v1 library at this piont.

Currently our nvme (mi) daemon is built upon the libnvme. We haven't upgraded to the libnvme3 yet and we are adding the nvme event (by health status polling) support for some NVMe-MI 1.2 devices.

@igaw

igaw commented Sep 15, 2026

Copy link
Copy Markdown
Collaborator

Currently our nvme (mi) daemon is built upon the libnvme. We haven't upgraded to the libnvme3 yet and we are adding the nvme event (by health status polling) support for some NVMe-MI 1.2 devices.

Alright, I don't want to make your life harder for no reason. I am fine taking this for libnvme 1. This gives you time for the migration and you don't have to deal with two things at the same time. Just don't expect that libnvme 1 will be actively maintainer for ever.

@jk-ozlabs jk-ozlabs left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly look good, just a few comments inline.

Main concern is the caller-visible representation of chscf, but other things are pretty trivial.

Comment thread src/nvme/types.h Outdated
__u8 spare;
__u8 cwarn;
__u8 rsvd9[7];
__u8 chscf[2];

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit odd representing it as an array of u8, whereas the spec defines a bit-set over a 16-bit field (which you have replicated in nvme_mi_chscf) meaning that users of the field need to split the bitmask over the array entries.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason chscf was declared as __u8 chscf[2] is that it sits at byte offset 9 (an odd byte offset immediately following __u8 cwarn at offset 8):

  • ctlid (__le16, bytes 0..1)
  • csts (__le16, bytes 2..3)
  • ctemp (__le16, bytes 4..5)
  • pdlu (__u8, byte 6)
  • spare (__u8, byte 7)
  • cwarn (__u8, byte 8)
  • chscf (bytes 9..10)

Because struct nvme_mi_ctrl_health_status is not marked __attribute__((packed)), declaring __le16 chscf; causes the compiler to insert 1 byte of alignment padding at offset 9, shifting chscf to bytes 10..11 and inflating sizeof(struct nvme_mi_ctrl_health_status) from 16 to 18 bytes. We followed the existing pattern in types.h for unaligned 16-bit fields in unpacked structs (such as __u8 temperature[2] at offset 1 in struct nvme_smart_log and __u8 regctl[2] at offset 5 in struct nvme_resv_status).

If you prefer exposing it directly as a 16-bit type, we can either:

  1. Add __attribute__((packed)) to struct nvme_mi_ctrl_health_status and change the field to __le16 chscf; (matching the packed wire structs in mi.h), or
  2. Keep __u8 chscf[2] and add a static inline __u16 helper to extract the 16-bit bitmask.

Happy to switch to __le16 chscf with __attribute__((packed)) if you prefer!

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think packed would be preferable here; the downside is an API change, but all existing fields should be binary compatible with the old struct.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW, since the nvme spec tends to ignore API/ABI stability, we can also ignore it for the nvme spec part. That means I am fine with changing the data structs, this will only break on source code level. The approach is to notify users by adding an entry in NEWS.md. Obviously NEWS.md is only available in nvme-cli 3. libnvme1 users will notice when the compiler starts complaining :)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, packed sounds like the best approach here then.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks both! Switched to __le16 chscf with __attribute__((packed)) on struct nvme_mi_ctrl_health_status. The struct stays 16 bytes and all existing fields keep their offsets; I added asserts in test/mi.c for sizeof() == 16 and offsetof(chscf) == 9. examples/mi-mctp.c now just uses le16_to_cpu(chs->chscf).

Comment thread src/nvme/mi.c
nvme_mi_ep_t ep, __u16 start_ctrl_id, bool clear, __u32 dw0_flags,
__u32 dw1_flags, struct nvme_mi_ctrl_health_status *entries,
unsigned int *num_entries)
{

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor, but some inconsistent indentation here, and a couple of sites below for similar function declarations.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ack, updated the function signatures and call sites in src/nvme/mi.h, src/nvme/mi.c, and test/mi.c to align wrapped parameters with the opening parenthesis.

Comment thread src/nvme/mi.c
rc = nvme_mi_submit(ep, &req, &resp);
if (rc) {
*num_entries = total;
return rc;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function semantics regarding partial results (and hence a truncated num_entries) isn't super clear. Are you intending to always set num_entries (and return a partial result) including on error return values?

This would be good to clarify on the function doc.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. Originally *num_entries = total; was set on error to indicate how many entries were populated if a later chunk failed mid-transfer, but mutating the caller's input capacity on error is surprising (especially if chunk 1 fails and overwrites *num_entries with 0).

I have updated the implementation so *num_entries is only updated on success (return 0), and clarified this in the kernel-doc comment in src/nvme/mi.h.

Comment thread src/nvme/mi.c Outdated
(dw0_flags & (NVME_MI_CHSP_DW0_ALL |
NVME_MI_CHSP_DW0_INCVF |
NVME_MI_CHSP_DW0_INCPF |
NVME_MI_CHSP_DW0_INCF));

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor, but might be neater to mask the dw0_flags and dw1_flags before constructing the cdw0 / cdw1 values, and you would no longer need to do that on every iteration.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, moved the dw0_flags masking and cdw1 construction outside the while loop so they are only computed once.

Comment thread src/nvme/mi.c
break;

if (le16_to_cpu(entries[total - 1].ctlid) == 0xffff)
break;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not clear on what this is indicating, as there is no specific mention of this case in the spec. Can you add a comment?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since SCTLID in Dword 0 is a 16-bit field (0x0000–0xffff), if the last returned entry has ctlid == 0xffff, there can be no higher Controller IDs to poll, and advancing current_sctlid = last_ctlid + 1 would wrap current_sctlid & 0xffff back to 0. Added an inline comment explaining this guard.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, makes sense.

@igaw

igaw commented Oct 13, 2026

Copy link
Copy Markdown
Collaborator

What's the status here?

@drakedog2008

Copy link
Copy Markdown
Contributor Author

Hi @igaw and @jk-ozlabs,

Sincere apologies for the long delay in getting back to this PR—I was pulled away by other higher-priority tasks over the past few weeks, and I finally have the bandwidth to come back and wrap this up. Thank you both for your patience and thorough reviews!

I have replied to the inline comments, updated the code accordingly, and rebased the branch onto the latest master.

@jk-ozlabs

Copy link
Copy Markdown
Collaborator

Looks good, just a follow-up to the struct layout below.

@jk-ozlabs jk-ozlabs left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, this looks good to me now.

If you feel like it, you could move the struct-size asserts out of the test case and into build-time static assertions of the library, but we'd need to work carefully with compiler coverage to do that. That's also fine as a future change too, if you prefer.

@igaw

igaw commented Nov 13, 2026

Copy link
Copy Markdown
Collaborator

FWIW, ccan ships build_assert

Implement Controller Health Status Poll (Opcode 02h) defined in
NVMe-MI 2.0 (Section 5.3 & 5.3.1).

- Add enum nvme_mi_chsp_dw0 and enum nvme_mi_chsp_dw1 matching
  Figures 94 and 95 of the specification.
- Add nvme_mi_mi_controller_health_status_poll() with direct
  parameters and an auto-paging multi-submit loop when num_entries >
  255 (up to 255 entries per submit, advancing SCTLID = last_ctlid + 1).
- Add convenience inline helper
  nvme_mi_mi_controller_health_status_poll_all() for querying all
  controllers starting from ID 0.
- Update struct nvme_mi_ctrl_health_status (CHDS) to include the 16-bit
  chscf[2] (Controller Health Status Changed Flags) at offset 9 and
  rsvd11[5].
- Add enum nvme_mi_chscf and update enum nvme_mi_csts with TCIDA.
- Export nvme_mi_mi_controller_health_status_poll in libnvme-mi.map.
- Add unit tests in test/mi.c covering single-entry query, selective
  flags, auto-paging across chunk boundaries, and error handling.

Signed-off-by: Hao Jiang <jianghao@google.com>
Update the controllers subcommand in mi-mctp to display controller
health status alongside controller topology and PCI configuration.

- Poll all controller health entries using
  nvme_mi_mi_controller_health_status_poll_all().
- Group health information by controller ID inside show_ctrl().
- Add show_chscf_flags() to decode active Controller Health Status
  Changed Flags (CHSCF) whenever the status changed field is non-zero.

Signed-off-by: Hao Jiang <jianghao@google.com>
@drakedog2008

Copy link
Copy Markdown
Contributor Author

Thanks for the approval, and thanks @igaw for the pointer!

I moved the size/offset checks out of the test into src/nvme/mi.c as file-scope _Static_asserts, right after the existing AEM struct asserts, so they use a pattern the CI compilers (gcc/clang, gnu99) already build:

_Static_assert(sizeof(struct nvme_mi_ctrl_health_status) == 16,
	"size_of_nvme_mi_ctrl_health_status_is_not_16_bytes");
_Static_assert(offsetof(struct nvme_mi_ctrl_health_status, chscf) == 9,
	"offset_of_nvme_mi_ctrl_health_status_chscf_is_not_9");

I kept them out of the public headers since those are also included from C++. I considered ccan's BUILD_ASSERT, but it only works inside a function, so following the existing _Static_assert block seemed cleaner. Happy to switch if you'd prefer the ccan macro.

@igaw

igaw commented Nov 13, 2026

Copy link
Copy Markdown
Collaborator

Ah know I remember, _Static_assert should be fine too. 👍

@igaw
igaw merged commit c222454 into linux-nvme:master Nov 13, 2026
12 checks passed
@igaw

igaw commented Nov 13, 2026

Copy link
Copy Markdown
Collaborator

Thanks everyone!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants