From 3d7137df6515e49d2534e577180d6ac7105b7eaa Mon Sep 17 00:00:00 2001 From: Martin Belanger Date: Wed, 28 Sep 2022 08:57:33 -0400 Subject: [PATCH] examples: LID supported must be shifted right by 16 To access the LID Specific Field (see Base specs) the values from the Get Supported Log pages must be shifted right by 16. The example code was missing the shift. Also added better exception handling example. Signed-off-by: Martin Belanger --- examples/discover-loop.py | 15 ++++++++++----- libnvme/README.md | 26 +++++++++++++------------- 2 files changed, 23 insertions(+), 18 deletions(-) diff --git a/examples/discover-loop.py b/examples/discover-loop.py index 9b007c92..09a976be 100644 --- a/examples/discover-loop.py +++ b/examples/discover-loop.py @@ -21,13 +21,13 @@ import sys import pprint from libnvme import nvme -def disc_supp_str(disc_log_page_support): +def disc_supp_str(dlp_supp_opts): d = { nvme.NVMF_LOG_DISC_LID_EXTDLPES: "Extended Discovery Log Page Entry Supported (EXTDLPES)", nvme.NVMF_LOG_DISC_LID_PLEOS: "Port Local Entries Only Supported (PLEOS)", nvme.NVMF_LOG_DISC_LID_ALLSUBES: "All NVM Subsystem Entries Supported (ALLSUBES)", } - return [txt for msk, txt in d.items() if disc_log_page_support & msk] + return [txt for msk, txt in d.items() if dlp_supp_opts & msk] r = nvme.root() h = nvme.host(r) @@ -40,11 +40,16 @@ except Exception as e: print("connected to %s subsys %s" % (c.name, c.subsystem.name)) slp = c.supported_log_pages() -disc_log_page_support = slp[nvme.NVME_LOG_LID_DISCOVER] if slp is not None else 0 -print(f"LID {nvme.NVME_LOG_LID_DISCOVER}h (Discovery), supports: {disc_supp_str(disc_log_page_support)}") try: - lsp = nvme.NVMF_LOG_DISC_LSP_PLEO if disc_log_page_support & nvme.NVMF_LOG_DISC_LID_PLEOS else 0 + dlp_supp_opts = slp[nvme.NVME_LOG_LID_DISCOVER] >> 16 +except (TypeError, IndexError): + dlp_supp_opts = 0 + +print(f"LID {nvme.NVME_LOG_LID_DISCOVER}h (Discovery), supports: {disc_supp_str(dlp_supp_opts)}") + +try: + lsp = nvme.NVMF_LOG_DISC_LSP_PLEO if dlp_supp_opts & nvme.NVMF_LOG_DISC_LID_PLEOS else 0 d = c.discover(lsp=lsp) print(pprint.pformat(d)) except Exception as e: diff --git a/libnvme/README.md b/libnvme/README.md index 9071c362..31957155 100644 --- a/libnvme/README.md +++ b/libnvme/README.md @@ -10,13 +10,13 @@ import sys import pprint from libnvme import nvme -def disc_supp_str(disc_log_page_support): - d = { - nvme.NVMF_LOG_DISC_LID_EXTDLPES: "Extended Discovery Log Page Entry Supported (EXTDLPES)", - nvme.NVMF_LOG_DISC_LID_PLEOS: "Port Local Entries Only Supported (PLEOS)", - nvme.NVMF_LOG_DISC_LID_ALLSUBES: "All NVM Subsystem Entries Supported (ALLSUBES)", +def disc_supp_str(dlp_supp_opts): + bitmap = { + nvme.NVMF_LOG_DISC_LID_EXTDLPES: "EXTDLPES", + nvme.NVMF_LOG_DISC_LID_PLEOS: "PLEOS", + nvme.NVMF_LOG_DISC_LID_ALLSUBES: "ALLSUBES", } - return [txt for msk, txt in d.items() if disc_log_page_support & msk] + return [txt for msk, txt in bitmap.items() if dlp_supp_opts & msk] root = nvme.root() # This is a singleton root.log_level('debug') # Optional: extra debug info @@ -40,15 +40,15 @@ except Exception as e: sys.exit(f'Failed to connect: {e}') supported_log_pages = ctrl.supported_log_pages() -if supported_log_pages is not None: - disc_log_page_support = supported_log_pages[nvme.NVME_LOG_LID_DISCOVER] - print(f"LID {nvme.NVME_LOG_LID_DISCOVER:02x}h (Discovery), supports: {disc_supp_str(disc_log_page_support)}") +try: + # Get the supported options for the Get Discovery Log Page command + dlp_supp_opts = supported_log_pages[nvme.NVME_LOG_LID_DISCOVER] >> 16 +except (TypeError, IndexError): + dlp_supp_opts = 0 +print(f"LID {nvme.NVME_LOG_LID_DISCOVER:02x}h (Discovery), supports: {disc_supp_str(dlp_supp_opts)}") try: - if disc_log_page_support and (disc_log_page_support & nvme.NVMF_LOG_DISC_LID_PLEOS): - lsp = nvme.NVMF_LOG_DISC_LSP_PLEO - else: - lsp = 0 + lsp = nvme.NVMF_LOG_DISC_LSP_PLEO if dlp_supp_opts & nvme.NVMF_LOG_DISC_LID_PLEOS else 0 log_pages = ctrl.discover(lsp=lsp) print(pprint.pformat(log_pages)) except Exception as e: -- 2.50.1