Skip to content

Commit

Permalink
util: Use lookup table to return nvme opcode string
Browse files Browse the repository at this point in the history
Also move the functions into util from ioctl.

Signed-off-by: Tokunori Ikegami <[email protected]>
  • Loading branch information
ikegami-t committed Jan 14, 2024
1 parent 72d9f96 commit 1a08c34
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 124 deletions.
106 changes: 0 additions & 106 deletions src/nvme/ioctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,112 +114,6 @@ static void nvme_show_command(struct nvme_passthru_cmd *cmd, int err)
printf("err : %d\n", err);
}

const char *nvme_admin_to_string(__u8 opcode)
{
switch (opcode) {
case nvme_admin_delete_sq:
return "Delete I/O Submission Queue";
case nvme_admin_create_sq:
return "Create I/O Submission Queue";
case nvme_admin_get_log_page:
return "Get Log Page";
case nvme_admin_delete_cq:
return "Delete I/O Completion Queue";
case nvme_admin_create_cq:
return "Create I/O Completion Queue";
case nvme_admin_identify:
return "Identify";
case nvme_admin_abort_cmd:
return "Abort";
case nvme_admin_set_features:
return "Set Features";
case nvme_admin_get_features:
return "Get Features";
case nvme_admin_async_event:
return "Asynchronous Event Request";
case nvme_admin_ns_mgmt:
return "Namespace Management";
case nvme_admin_fw_commit:
return "Firmware Commit";
case nvme_admin_fw_download:
return "Firmware Image Download";
case nvme_admin_dev_self_test:
return "Device Self-test";
case nvme_admin_ns_attach:
return "Namespace Attachment";
case nvme_admin_keep_alive:
return "Keep Alive";
case nvme_admin_directive_send:
return "Directive Send";
case nvme_admin_directive_recv:
return "Directive Receive";
case nvme_admin_virtual_mgmt:
return "Virtualization Management";
case nvme_admin_nvme_mi_send:
return "NVMe-MI Send";
case nvme_admin_nvme_mi_recv:
return "NVMe-MI Receive";
case nvme_admin_dbbuf:
return "Doorbell Buffer Config";
case nvme_admin_format_nvm:
return "Format NVM";
case nvme_admin_security_send:
return "Security Send";
case nvme_admin_security_recv:
return "Security Receive";
case nvme_admin_sanitize_nvm:
return "Sanitize";
case nvme_admin_get_lba_status:
return "Get LBA Status";
default:
break;
}

return NULL;
}

const char *nvme_nvm_to_string(__u8 opcode)
{
switch (opcode) {
case nvme_cmd_flush:
return "Flush";
case nvme_cmd_write:
return "Write";
case nvme_cmd_read:
return "Read";
case nvme_cmd_write_uncor:
return "Write Uncorrectable";
case nvme_cmd_compare:
return "Compare";
case nvme_cmd_write_zeroes:
return "Write Zeroes";
case nvme_cmd_dsm:
return "Dataset Management";
case nvme_cmd_resv_register:
return "Reservation Register";
case nvme_cmd_resv_report:
return "Reservation Report";
case nvme_cmd_resv_acquire:
return "Reservation Acquire";
case nvme_cmd_resv_release:
return "Reservation Release";
case nvme_cmd_verify:
return "Verify";
case nvme_cmd_copy:
return "Copy";
case nvme_zns_cmd_mgmt_send:
return "Zone Management Send";
case nvme_zns_cmd_mgmt_recv:
return "Zone Management Receive";
case nvme_zns_cmd_append:
return "Zone Append";
default:
break;
}

return NULL;
}

static const char *nvme_cmd_to_string(bool admin, __u8 opcode)
{
const char *cmd_name;
Expand Down
18 changes: 0 additions & 18 deletions src/nvme/ioctl.h
Original file line number Diff line number Diff line change
Expand Up @@ -4072,22 +4072,4 @@ void nvme_set_latency(bool latency);
* Return: false if disabled or true if enabled.
*/
bool nvme_get_latency(void);

/**
* nvme_admin_to_string - Returns string describing nvme admin opcode
* @opcode: Admin opcode for an nvme command
*
* Return: representation of the admin opcode if it is an admin opcode field,
* or NULL if opcode is unknown.
*/
const char *nvme_admin_to_string(__u8 opcode);

/**
* nvme_nvm_to_string - Returns string describing nvme nvm opcode
* @opcode: NVM opcode for an nvme command
*
* Return: representation of the nvm opcode if it is an nvm opcode field,
* or NULL if opcode is unknown.
*/
const char *nvme_nvm_to_string(__u8 opcode);
#endif /* _LIBNVME_IOCTL_H */
65 changes: 65 additions & 0 deletions src/nvme/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -1116,3 +1116,68 @@ void *__nvme_alloc(size_t len)
memset(p, 0, _len);
return p;
}

static const char * const admin_opcode[] = {
[nvme_admin_delete_sq] = "Delete I/O Submission Queue",
[nvme_admin_create_sq] = "Create I/O Submission Queue",
[nvme_admin_get_log_page] = "Get Log Page",
[nvme_admin_delete_cq] = "Delete I/O Completion Queue",
[nvme_admin_create_cq] = "Create I/O Completion Queue",
[nvme_admin_identify] = "Identify",
[nvme_admin_abort_cmd] = "Abort",
[nvme_admin_set_features] = "Set Features",
[nvme_admin_get_features] = "Get Features",
[nvme_admin_async_event] = "Asynchronous Event Request",
[nvme_admin_ns_mgmt] = "Namespace Management",
[nvme_admin_fw_commit] = "Firmware Commit",
[nvme_admin_fw_download] = "Firmware Image Download",
[nvme_admin_dev_self_test] = "Device Self-test",
[nvme_admin_ns_attach] = "Namespace Attachment",
[nvme_admin_keep_alive] = "Keep Alive",
[nvme_admin_directive_send] = "Directive Send",
[nvme_admin_directive_recv] = "Directive Receive",
[nvme_admin_virtual_mgmt] = "Virtualization Management",
[nvme_admin_nvme_mi_send] = "NVMe-MI Send",
[nvme_admin_nvme_mi_recv] = "NVMe-MI Receive",
[nvme_admin_dbbuf] = "Doorbell Buffer Config",
[nvme_admin_format_nvm] = "Format NVM",
[nvme_admin_security_send] = "Security Send",
[nvme_admin_security_recv] = "Security Receive",
[nvme_admin_sanitize_nvm] = "Sanitize",
[nvme_admin_get_lba_status] = "Get LBA Status",
};

static const char * const nvm_opcode[] = {
[nvme_cmd_flush] = "Flush",
[nvme_cmd_write] = "Write",
[nvme_cmd_read] = "Read",
[nvme_cmd_write_uncor] = "Write Uncorrectable",
[nvme_cmd_compare] = "Compare",
[nvme_cmd_write_zeroes] = "Write Zeroes",
[nvme_cmd_dsm] = "Dataset Management",
[nvme_cmd_resv_register] = "Reservation Register",
[nvme_cmd_resv_report] = "Reservation Report",
[nvme_cmd_resv_acquire] = "Reservation Acquire",
[nvme_cmd_resv_release] = "Reservation Release",
[nvme_cmd_verify] = "Verify",
[nvme_cmd_copy] = "Copy",
[nvme_zns_cmd_mgmt_send] = "Zone Management Send",
[nvme_zns_cmd_mgmt_recv] = "Zone Management Receive",
[nvme_zns_cmd_append] = "Zone Append",
};

const char *nvme_admin_to_string(__u8 opcode)
{
if (opcode < ARRAY_SIZE(admin_opcode))
return admin_opcode[opcode];

return NULL;
}

const char *nvme_nvm_to_string(__u8 opcode)
{
if (opcode < ARRAY_SIZE(nvm_opcode))
return nvm_opcode[opcode];

return NULL;
}
17 changes: 17 additions & 0 deletions src/nvme/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -702,4 +702,21 @@ const char *nvme_iface_matching_addr(const struct ifaddrs *iface_list, const cha
*/
bool nvme_iface_primary_addr_matches(const struct ifaddrs *iface_list, const char *iface, const char *addr);

/**
* nvme_admin_to_string - Returns string describing nvme admin opcode
* @opcode: Admin opcode for an nvme command
*
* Return: representation of the admin opcode if it is an admin opcode field,
* or NULL if opcode is unknown.
*/
const char *nvme_admin_to_string(__u8 opcode);

/**
* nvme_nvm_to_string - Returns string describing nvme nvm opcode
* @opcode: NVM opcode for an nvme command
*
* Return: representation of the nvm opcode if it is an nvm opcode field,
* or NULL if opcode is unknown.
*/
const char *nvme_nvm_to_string(__u8 opcode);
#endif /* _LIBNVME_UTIL_H */

0 comments on commit 1a08c34

Please sign in to comment.