diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2023-06-30 22:38:48 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2023-06-30 22:38:48 +0000 |
commit | 01c4d3d32c5044d3d17055c2d94d40fee9d130e1 (patch) | |
tree | 2c213cd5436bff644fa7023b94674a9c12d2e1af /plugins/scaleflux | |
parent | Adding upstream version 2.4+really2.4. (diff) | |
download | nvme-cli-upstream/2.5.tar.xz nvme-cli-upstream/2.5.zip |
Adding upstream version 2.5.upstream/2.5
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'plugins/scaleflux')
-rw-r--r-- | plugins/scaleflux/sfx-nvme.c | 689 | ||||
-rw-r--r-- | plugins/scaleflux/sfx-nvme.h | 2 |
2 files changed, 584 insertions, 107 deletions
diff --git a/plugins/scaleflux/sfx-nvme.c b/plugins/scaleflux/sfx-nvme.c index 4bcfbf6..01867c7 100644 --- a/plugins/scaleflux/sfx-nvme.c +++ b/plugins/scaleflux/sfx-nvme.c @@ -3,19 +3,23 @@ #include <errno.h> #include <stdio.h> #include <stdlib.h> +#include <string.h> #include <unistd.h> #include <linux/fs.h> #include <inttypes.h> #include <asm/byteorder.h> #include <sys/sysinfo.h> #include <sys/stat.h> -#include <unistd.h> +#include <sys/types.h> +#include <dirent.h> +#include <time.h> #include "common.h" #include "nvme.h" #include "libnvme.h" #include "plugin.h" #include "linux/types.h" +#include "nvme-wrap.h" #include "nvme-print.h" #define CREATE_CMD @@ -57,8 +61,7 @@ enum sfx_nvme_admin_opcode { nvme_admin_sfx_get_features = 0xd6, }; -struct sfx_freespace_ctx -{ +struct sfx_freespace_ctx { __u64 free_space; __u64 phy_cap; /* physical capacity, in unit of sector */ __u64 phy_space; /* physical space considering OP, in unit of sector */ @@ -66,6 +69,10 @@ struct sfx_freespace_ctx __u64 hw_used; /* hw space used in 4K */ __u64 app_written; /* app data written in 4K */ __u64 out_of_space; + __u64 map_unit; + __u64 max_user_space; + __u64 extendible_user_cap_lba_count; + __u64 friendly_change_cap_support; }; struct nvme_capacity_info { @@ -75,72 +82,73 @@ struct nvme_capacity_info { __u64 free_space; }; -struct __attribute__((packed)) nvme_additional_smart_log_item { +struct __packed nvme_additional_smart_log_item { __u8 key; __u8 _kp[2]; __u8 norm; __u8 _np; - union __attribute__((packed)) { + union __packed { __u8 raw[6]; - struct __attribute__((packed)) wear_level { + struct __packed wear_level { __le16 min; __le16 max; __le16 avg; } wear_level; - struct __attribute__((packed)) thermal_throttle { + struct __packed thermal_throttle { __u8 pct; __u32 count; } thermal_throttle; - } ; + }; __u8 _rp; -} ; +}; struct nvme_additional_smart_log { - struct nvme_additional_smart_log_item program_fail_cnt; - struct nvme_additional_smart_log_item erase_fail_cnt; - struct nvme_additional_smart_log_item wear_leveling_cnt; - struct nvme_additional_smart_log_item e2e_err_cnt; - struct nvme_additional_smart_log_item crc_err_cnt; - struct nvme_additional_smart_log_item timed_workload_media_wear; - struct nvme_additional_smart_log_item timed_workload_host_reads; - struct nvme_additional_smart_log_item timed_workload_timer; - struct nvme_additional_smart_log_item thermal_throttle_status; - struct nvme_additional_smart_log_item retry_buffer_overflow_cnt; - struct nvme_additional_smart_log_item pll_lock_loss_cnt; - struct nvme_additional_smart_log_item nand_bytes_written; - struct nvme_additional_smart_log_item host_bytes_written; - struct nvme_additional_smart_log_item raid_recover_cnt; // errors which can be recovered by RAID - struct nvme_additional_smart_log_item prog_timeout_cnt; - struct nvme_additional_smart_log_item erase_timeout_cnt; - struct nvme_additional_smart_log_item read_timeout_cnt; - struct nvme_additional_smart_log_item read_ecc_cnt;//retry cnt - struct nvme_additional_smart_log_item non_media_crc_err_cnt; - struct nvme_additional_smart_log_item compression_path_err_cnt; - struct nvme_additional_smart_log_item out_of_space_flag; - struct nvme_additional_smart_log_item physical_usage_ratio; - struct nvme_additional_smart_log_item grown_bb; //grown bad block + struct nvme_additional_smart_log_item program_fail_cnt; + struct nvme_additional_smart_log_item erase_fail_cnt; + struct nvme_additional_smart_log_item wear_leveling_cnt; + struct nvme_additional_smart_log_item e2e_err_cnt; + struct nvme_additional_smart_log_item crc_err_cnt; + struct nvme_additional_smart_log_item timed_workload_media_wear; + struct nvme_additional_smart_log_item timed_workload_host_reads; + struct nvme_additional_smart_log_item timed_workload_timer; + struct nvme_additional_smart_log_item thermal_throttle_status; + struct nvme_additional_smart_log_item retry_buffer_overflow_cnt; + struct nvme_additional_smart_log_item pll_lock_loss_cnt; + struct nvme_additional_smart_log_item nand_bytes_written; + struct nvme_additional_smart_log_item host_bytes_written; + struct nvme_additional_smart_log_item raid_recover_cnt; /* errors which can be recovered by RAID */ + struct nvme_additional_smart_log_item prog_timeout_cnt; + struct nvme_additional_smart_log_item erase_timeout_cnt; + struct nvme_additional_smart_log_item read_timeout_cnt; + struct nvme_additional_smart_log_item read_ecc_cnt; /* retry cnt */ + struct nvme_additional_smart_log_item non_media_crc_err_cnt; + struct nvme_additional_smart_log_item compression_path_err_cnt; + struct nvme_additional_smart_log_item out_of_space_flag; + struct nvme_additional_smart_log_item physical_usage_ratio; + struct nvme_additional_smart_log_item grown_bb; /* grown bad block */ }; int nvme_query_cap(int fd, __u32 nsid, __u32 data_len, void *data) { - int rc = 0; - struct nvme_passthru_cmd cmd = { - .opcode = nvme_admin_query_cap_info, - .nsid = nsid, - .addr = (__u64)(uintptr_t) data, - .data_len = data_len, - }; - - rc = ioctl(fd, SFX_GET_FREESPACE, data); - return rc == 0 ? 0 : nvme_submit_admin_passthru(fd, &cmd, NULL); + int rc = 0; + struct nvme_passthru_cmd cmd = { + .opcode = nvme_admin_query_cap_info, + .nsid = nsid, + .addr = (__u64)(uintptr_t) data, + .data_len = data_len, + }; + + rc = ioctl(fd, SFX_GET_FREESPACE, data); + return rc ? nvme_submit_admin_passthru(fd, &cmd, NULL) : 0; } + int nvme_change_cap(int fd, __u32 nsid, __u64 capacity) { struct nvme_passthru_cmd cmd = { - .opcode = nvme_admin_change_cap, - .nsid = nsid, - .cdw10 = (capacity & 0xffffffff), - .cdw11 = (capacity >> 32), + .opcode = nvme_admin_change_cap, + .nsid = nsid, + .cdw10 = (capacity & 0xffffffff), + .cdw11 = (capacity >> 32), }; return nvme_submit_admin_passthru(fd, &cmd, NULL); @@ -149,10 +157,10 @@ int nvme_change_cap(int fd, __u32 nsid, __u64 capacity) int nvme_sfx_set_features(int fd, __u32 nsid, __u32 fid, __u32 value) { struct nvme_passthru_cmd cmd = { - .opcode = nvme_admin_sfx_set_features, - .nsid = nsid, - .cdw10 = fid, - .cdw11 = value, + .opcode = nvme_admin_sfx_set_features, + .nsid = nsid, + .cdw10 = fid, + .cdw11 = value, }; return nvme_submit_admin_passthru(fd, &cmd, NULL); @@ -161,16 +169,15 @@ int nvme_sfx_set_features(int fd, __u32 nsid, __u32 fid, __u32 value) int nvme_sfx_get_features(int fd, __u32 nsid, __u32 fid, __u32 *result) { int err = 0; - struct nvme_passthru_cmd cmd = { - .opcode = nvme_admin_sfx_get_features, - .nsid = nsid, - .cdw10 = fid, + struct nvme_passthru_cmd cmd = { + .opcode = nvme_admin_sfx_get_features, + .nsid = nsid, + .cdw10 = fid, }; err = nvme_submit_admin_passthru(fd, &cmd, NULL); - if (!err && result) { + if (!err && result) *result = cmd.result; - } return err; } @@ -398,11 +405,11 @@ static void show_sfx_smart_log(struct nvme_additional_smart_log *smart, static int get_additional_smart_log(int argc, char **argv, struct command *cmd, struct plugin *plugin) { struct nvme_additional_smart_log smart_log; - char *desc = "Get ScaleFlux vendor specific additional smart log (optionally, "\ - "for the specified namespace), and show it."; + char *desc = + "Get ScaleFlux vendor specific additional smart log (optionally, for the specified namespace), and show it."; const char *namespace = "(optional) desired namespace"; const char *raw = "dump output in binary format"; - const char *json= "Dump output in json format"; + const char *json = "Dump output in json format"; struct nvme_dev *dev; struct config { __u32 namespace_id; @@ -438,14 +445,14 @@ static int get_additional_smart_log(int argc, char **argv, struct command *cmd, dev->name); else d_raw((unsigned char *)&smart_log, sizeof(smart_log)); - } - else if (err > 0) + } else if (err > 0) { nvme_show_status(err); + } dev_close(dev); return err; } -struct __attribute__((__packed__)) sfx_lat_stats_vanda { +struct __packed sfx_lat_stats_vanda { __u16 maj; __u16 min; __u32 bucket_1[32]; /* 0~1ms, step 32us */ @@ -456,7 +463,7 @@ struct __attribute__((__packed__)) sfx_lat_stats_vanda { __u32 bucket_6[1]; /* 4s+, specifically 4096ms+ */ }; -struct __attribute__((__packed__)) sfx_lat_stats_myrtle { +struct __packed sfx_lat_stats_myrtle { __u16 maj; __u16 min; __u32 bucket_1[64]; /* 0us~63us, step 1us */ @@ -482,7 +489,7 @@ struct __attribute__((__packed__)) sfx_lat_stats_myrtle { }; -struct __attribute__((__packed__)) sfx_lat_status_ver { +struct __packed sfx_lat_status_ver { __u16 maj; __u16 min; }; @@ -646,23 +653,22 @@ static int get_lat_stats_log(int argc, char **argv, struct command *cmd, struct sizeof(stats), (void *)&stats); if (!err) { if ((stats.ver.maj == VANDA_MAJOR_IDX) && (stats.ver.min == VANDA_MINOR_IDX)) { - if (!cfg.raw_binary) { + if (!cfg.raw_binary) show_lat_stats_vanda(&stats.vanda, cfg.write); - } else { + else d_raw((unsigned char *)&stats.vanda, sizeof(struct sfx_lat_stats_vanda)); - } } else if ((stats.ver.maj == MYRTLE_MAJOR_IDX) && (stats.ver.min == MYRTLE_MINOR_IDX)) { - if (!cfg.raw_binary) { + if (!cfg.raw_binary) show_lat_stats_myrtle(&stats.myrtle, cfg.write); - } else { + else d_raw((unsigned char *)&stats.myrtle, sizeof(struct sfx_lat_stats_myrtle)); - } } else { printf("ScaleFlux IO %s Command Latency Statistics Invalid Version Maj %d Min %d\n", cfg.write ? "Write" : "Read", stats.ver.maj, stats.ver.min); } - } else if (err > 0) + } else if (err > 0) { nvme_show_status(err); + } dev_close(dev); return err; } @@ -697,7 +703,7 @@ static int get_bb_table(int fd, __u32 nsid, unsigned char *buf, __u64 size) { if (fd < 0 || !buf || size != 256*4096*sizeof(unsigned char)) { fprintf(stderr, "Invalid Param \r\n"); - return EINVAL; + return -EINVAL; } return sfx_nvme_get_log(fd, nsid, SFX_LOG_BBT, size, (void *)buf); @@ -731,7 +737,7 @@ static void bd_table_show(unsigned char *bd_table, __u64 table_size) remap_gbb_count = *((__u32 *)(bd_table + 4 * sizeof(__u32))); bb_elem = (__u64 *)(bd_table + 5 * sizeof(__u32)); - printf("Bad Block Table \n"); + printf("Bad Block Table\n"); printf("MF_BB_COUNT: %u\n", mf_bb_count); printf("GROWN_BB_COUNT: %u\n", grown_bb_count); printf("TOTAL_BB_COUNT: %u\n", total_bb_count); @@ -792,7 +798,7 @@ static int sfx_get_bad_block(int argc, char **argv, struct command *cmd, struct err = get_bb_table(dev_fd(dev), 0xffffffff, data_buf, buf_size); if (err < 0) { perror("get-bad-block"); - } else if (err != 0) { + } else if (err) { nvme_show_status(err); } else { bd_table_show(data_buf, buf_size); @@ -816,6 +822,7 @@ static void show_cap_info(struct sfx_freespace_ctx *ctx) printf("used provisioned capacity:%5lluGB(0x%"PRIx64")\n", IDEMA_CAP2GB(ctx->phy_space) - IDEMA_CAP2GB(ctx->free_space), (uint64_t)(ctx->phy_space - ctx->free_space)); + printf("map_unit :0x%"PRIx64"K\n", (uint64_t)(ctx->map_unit * 4)); } static int query_cap_info(int argc, char **argv, struct command *cmd, struct plugin *plugin) @@ -845,11 +852,10 @@ static int query_cap_info(int argc, char **argv, struct command *cmd, struct plu } if (!err) { - if (!cfg.raw_binary) { + if (!cfg.raw_binary) show_cap_info(&ctx); - } else { + else d_raw((unsigned char *)&ctx, sizeof(ctx)); - } } dev_close(dev); return err; @@ -864,9 +870,8 @@ static int change_sanity_check(int fd, __u64 trg_in_4k, int *shrink) __u64 provisoned_cap_4k = 0; int extend = 0; - if (nvme_query_cap(fd, 0xffffffff, sizeof(freespace_ctx), &freespace_ctx)) { - return -1; - } + if (nvme_query_cap(fd, 0xffffffff, sizeof(freespace_ctx), &freespace_ctx)) + return -1; /* * capacity illegal check @@ -877,13 +882,12 @@ static int change_sanity_check(int fd, __u64 trg_in_4k, int *shrink) trg_in_4k > ((__u64)provisoned_cap_4k * 4)) { fprintf(stderr, "WARNING: Only support 1.0~4.0 x provisoned capacity!\n"); - if (trg_in_4k < provisoned_cap_4k) { + if (trg_in_4k < provisoned_cap_4k) fprintf(stderr, "WARNING: The target capacity is less than 1.0 x provisioned capacity!\n"); - } else { + else fprintf(stderr, "WARNING: The target capacity is larger than 4.0 x provisioned capacity!\n"); - } return -1; } if (trg_in_4k > ((__u64)provisoned_cap_4k*4)) { @@ -893,7 +897,7 @@ static int change_sanity_check(int fd, __u64 trg_in_4k, int *shrink) /* * check whether mem enough if extend - * */ + */ cur_in_4k = freespace_ctx.user_space >> (SFX_PAGE_SHIFT - SECTOR_SHIFT); extend = (cur_in_4k <= trg_in_4k); if (extend) { @@ -904,10 +908,9 @@ static int change_sanity_check(int fd, __u64 trg_in_4k, int *shrink) mem_need = (trg_in_4k - cur_in_4k) * 8; if (s_info.freeram <= 10 || mem_need > s_info.freeram) { fprintf(stderr, - "WARNING: Free memory is not enough! " - "Please drop cache or extend more memory and retry\n" - "WARNING: Memory needed is %"PRIu64", free memory is %"PRIu64"\n", - (uint64_t)mem_need, (uint64_t)s_info.freeram); + "WARNING: Free memory is not enough! Please drop cache or extend more memory and retry\n" + "WARNING: Memory needed is %"PRIu64", free memory is %"PRIu64"\n", + (uint64_t)mem_need, (uint64_t)s_info.freeram); return -1; } } @@ -926,6 +929,7 @@ static int change_sanity_check(int fd, __u64 trg_in_4k, int *shrink) static int sfx_confirm_change(const char *str) { unsigned char confirm; + fprintf(stderr, "WARNING: %s.\n" "Use the force [--force] option to suppress this warning.\n", str); @@ -935,7 +939,7 @@ static int sfx_confirm_change(const char *str) fprintf(stderr, "Cancled.\n"); return 0; } - fprintf(stderr, "Sending operation ... \n"); + fprintf(stderr, "Sending operation ...\n"); return 1; } @@ -993,11 +997,11 @@ static int change_cap(int argc, char **argv, struct command *cmd, struct plugin } err = nvme_change_cap(dev_fd(dev), 0xffffffff, cap_in_4k); - if (err < 0) + if (err < 0) { perror("sfx-change-cap"); - else if (err != 0) + } else if (err) { nvme_show_status(err); - else { + } else { printf("ScaleFlux change-capacity: success\n"); ioctl(dev_fd(dev), BLKRRPART); } @@ -1017,7 +1021,7 @@ static int sfx_verify_chr(int fd) if (!S_ISCHR(nvme_stat.st_mode)) { fprintf(stderr, "Error: requesting clean card on non-controller handle\n"); - return ENOTBLK; + return -ENOTBLK; } return 0; } @@ -1041,13 +1045,12 @@ static int sfx_clean_card(int fd) char *sfx_feature_to_string(int feature) { switch (feature) { - case SFX_FEAT_ATOMIC: - return "ATOMIC"; - case SFX_FEAT_UP_P_CAP: - return "UPDATE_PROVISION_CAPACITY"; - - default: - return "Unknown"; + case SFX_FEAT_ATOMIC: + return "ATOMIC"; + case SFX_FEAT_UP_P_CAP: + return "UPDATE_PROVISION_CAPACITY"; + default: + return "Unknown"; } } @@ -1093,7 +1096,7 @@ static int sfx_set_feature(int argc, char **argv, struct command *cmd, struct pl if (!cfg.feature_id) { fprintf(stderr, "feature-id required param\n"); dev_close(dev); - return EINVAL; + return -EINVAL; } if (cfg.feature_id == SFX_FEAT_CLR_CARD) { @@ -1107,7 +1110,7 @@ static int sfx_set_feature(int argc, char **argv, struct command *cmd, struct pl } - if (cfg.feature_id == SFX_FEAT_ATOMIC && cfg.value != 0) { + if (cfg.feature_id == SFX_FEAT_ATOMIC && cfg.value) { if (cfg.namespace_id != 0xffffffff) { err = nvme_identify_ns(dev_fd(dev), cfg.namespace_id, &ns); @@ -1125,14 +1128,14 @@ static int sfx_set_feature(int argc, char **argv, struct command *cmd, struct pl if ((ns.flbas & 0xf) != 1) { printf("Please change-sector size to 4K, then retry\n"); dev_close(dev); - return EFAULT; + return -EFAULT; } } } else if (cfg.feature_id == SFX_FEAT_UP_P_CAP) { if (cfg.value <= 0) { fprintf(stderr, "Invalid Param\n"); dev_close(dev); - return EINVAL; + return -EINVAL; } /*Warning for change pacp by GB*/ @@ -1153,8 +1156,9 @@ static int sfx_set_feature(int argc, char **argv, struct command *cmd, struct pl } else if (!err) { printf("ScaleFlux set-feature:%#02x (%s), value:%d\n", cfg.feature_id, sfx_feature_to_string(cfg.feature_id), cfg.value); - } else if (err > 0) + } else if (err > 0) { nvme_show_status(err); + } dev_close(dev); return err; @@ -1192,7 +1196,7 @@ static int sfx_get_feature(int argc, char **argv, struct command *cmd, struct pl if (!cfg.feature_id) { fprintf(stderr, "feature-id required param\n"); dev_close(dev); - return EINVAL; + return -EINVAL; } err = nvme_sfx_get_features(dev_fd(dev), cfg.namespace_id, @@ -1204,10 +1208,481 @@ static int sfx_get_feature(int argc, char **argv, struct command *cmd, struct pl } else if (!err) { printf("ScaleFlux get-feature:%02x (%s), value:%d\n", cfg.feature_id, sfx_feature_to_string(cfg.feature_id), result); - } else if (err > 0) + } else if (err > 0) { nvme_show_status(err); + } + + dev_close(dev); + return err; + +} + +static int nvme_parse_evtlog(void *pevent_log_info, __u32 log_len, char *output) +{ + __u32 offset = 0; + __u32 length = log_len; + __u16 fw_core; + __u64 fw_time; + __u8 code_level; + __u8 code_type; + char str_buffer[512]; + __u32 str_pos; + FILE *fd; + int err = 0; + + enum sfx_evtlog_level { + sfx_evtlog_level_warning, + sfx_evtlog_level_error, + }; + + const char *sfx_evtlog_warning[4] = { + "RESERVED", + "TOO_MANY_BB", + "LOW_SPACE", + "HIGH_TEMPERATURE" + }; + + const char *sfx_evtlog_error[14] = { + "RESERVED", + "HAS_ASSERT", + "HAS_PANIC_DUMP", + "INVALID_FORMAT_CAPACITY", + "MAT_FAILED", + "FREEZE_DUE_TO_RECOVERY_FAILED", + "RFS_BROKEN", + "MEDIA_ERR_ON_PAGE_IN", + "MEDIA_ERR_ON_MPAGE_HEADER", + "CAPACITOR_BROKEN", + "READONLY_DUE_TO_RECOVERY_FAILED", + "RD_ERR_IN_GSD_RECOVERY", + "RD_ERR_ON_PF_RECOVERY", + "MEDIA_ERR_ON_FULL_RECOVERY" + }; + + struct sfx_nvme_evtlog_info { + __u16 time_stamp[4]; + __u64 magic1; + __u8 reverse[10]; + char evt_name[32]; + __u64 magic2; + char fw_ver[24]; + char bl2_ver[32]; + __u16 code; + __u16 assert_id; + } __packed; + + struct sfx_nvme_evtlog_info *info = NULL; + + fd = fopen(output, "w+"); + if (!fd) { + fprintf(stderr, "Failed to open %s file to write\n", output); + err = ENOENT; + goto ret; + } + + while (length > 0) { + info = (struct sfx_nvme_evtlog_info *)(pevent_log_info + offset); + + if ((info->magic1 == 0x474F4C545645) && + (info->magic2 == 0x38B0B3ABA9BA)) { + + memset(str_buffer, 0, 512); + str_pos = 0; + + fw_core = info->time_stamp[3]; + snprintf(str_buffer + str_pos, 16, "[%d-", fw_core); + str_pos = strlen(str_buffer); + + fw_time = ((__u64)info->time_stamp[2] << 32) + ((__u64)info->time_stamp[1] << 16) + (__u64)info->time_stamp[0]; + convert_ts(fw_time, str_buffer + str_pos); + str_pos = strlen(str_buffer); + + strcpy(str_buffer + str_pos, "] event-log:\n"); + str_pos = strlen(str_buffer); + + snprintf(str_buffer + str_pos, 128, + " > fw_version: %s\n > bl2_version: %s\n", + info->fw_ver, info->bl2_ver); + str_pos = strlen(str_buffer); + + code_level = (info->code & 0x100) >> 8; + code_type = (info->code % 0x100); + if (code_level == sfx_evtlog_level_warning) { + snprintf(str_buffer + str_pos, 128, + " > error_str: [WARNING][%s]\n\n", + sfx_evtlog_warning[code_type]); + } else { + if (info->assert_id) + snprintf(str_buffer + str_pos, 128, + " > error_str: [ERROR][%s]\n > assert_id: %d\n\n", + sfx_evtlog_error[code_type], info->assert_id); + else + snprintf(str_buffer + str_pos, 128, + " > error_str: [ERROR][%s]\n\n", + sfx_evtlog_error[code_type]); + } + str_pos = strlen(str_buffer); + + if (fwrite(str_buffer, 1, str_pos, fd) != str_pos) { + fprintf(stderr, "Failed to write parse result to output file\n"); + goto close_fd; + } + } + + offset++; + length--; + + if (!(offset % (log_len / 100)) || (offset == log_len)) + util_spinner("Parse", (float) (offset) / (float) (log_len)); + } + + printf("\nParse-evtlog: Success\n"); + +close_fd: + fclose(fd); +ret: + return err; +} + +static int nvme_dump_evtlog(struct nvme_dev *dev, __u32 namespace_id, __u32 storage_medium, + char *file, bool parse, char *output) +{ + struct nvme_persistent_event_log *pevent; + void *pevent_log_info; + __u8 lsp_base; + __u32 offset = 0; + __u32 length = 0; + __u32 log_len; + __u32 single_len; + bool huge; + int err = 0; + FILE *fd = NULL; + struct nvme_get_log_args args = { + .args_size = sizeof(args), + .fd = dev_fd(dev), + .lid = NVME_LOG_LID_PERSISTENT_EVENT, + .nsid = namespace_id, + .lpo = NVME_LOG_LPO_NONE, + .lsp = NVME_LOG_LSP_NONE, + .lsi = NVME_LOG_LSI_NONE, + .rae = false, + .uuidx = NVME_UUID_NONE, + .csi = NVME_CSI_NVM, + .ot = false, + .len = 0, + .log = NULL, + .timeout = NVME_DEFAULT_IOCTL_TIMEOUT, + .result = NULL, + }; + + if (!storage_medium) { + lsp_base = 0; + single_len = 64 * 1024 - 4; + } else { + lsp_base = 4; + single_len = 32 * 1024; + } + + pevent = calloc(sizeof(*pevent), sizeof(__u8)); + if (!pevent) { + err = -ENOMEM; + goto ret; + } + + args.lsp = lsp_base + NVME_PEVENT_LOG_RELEASE_CTX; + args.log = pevent; + args.len = sizeof(*pevent); + + err = nvme_get_log(&args); + if (err) { + fprintf(stderr, "Unable to get evtlog lsp=0x%x, ret = 0x%x\n", args.lsp, err); + goto free_pevent; + } + + args.lsp = lsp_base + NVME_PEVENT_LOG_EST_CTX_AND_READ; + err = nvme_get_log(&args); + if (err) { + fprintf(stderr, "Unable to get evtlog lsp=0x%x, ret = 0x%x\n", args.lsp, err); + goto free_pevent; + } + + log_len = le64_to_cpu(pevent->tll); + if (log_len % 4) + log_len = (log_len / 4 + 1) * 4; + + pevent_log_info = nvme_alloc(single_len, &huge); + if (!pevent_log_info) { + err = -ENOMEM; + goto free_pevent; + } + + fd = fopen(file, "wb+"); + if (!fd) { + fprintf(stderr, "Failed to open %s file to write\n", file); + err = ENOENT; + goto free; + } + + args.lsp = lsp_base + NVME_PEVENT_LOG_READ; + args.log = pevent_log_info; + length = log_len; + while (length > 0) { + args.lpo = offset; + if (length > single_len) { + args.len = single_len; + } else { + memset(args.log, 0, args.len); + args.len = length; + } + err = nvme_get_log(&args); + if (err) { + fprintf(stderr, "Unable to get evtlog offset=0x%x len 0x%x ret = 0x%x\n", offset, args.len, err); + goto close_fd; + } + + if (fwrite(args.log, 1, args.len, fd) != args.len) { + fprintf(stderr, "Failed to write evtlog to file\n"); + goto close_fd; + } + + offset += args.len; + length -= args.len; + util_spinner("Parse", (float) (offset) / (float) (log_len)); + } + + printf("\nDump-evtlog: Success\n"); + + if (parse) { + nvme_free(pevent_log_info, huge); + pevent_log_info = nvme_alloc(log_len, &huge); + if (!pevent_log_info) { + fprintf(stderr, "Failed to alloc enough memory 0x%x to parse evtlog\n", log_len); + err = -ENOMEM; + goto close_fd; + } + + fclose(fd); + fd = fopen(file, "rb"); + if (!fd) { + fprintf(stderr, "Failed to open %s file to read\n", file); + err = ENOENT; + goto free; + } + if (fread(pevent_log_info, 1, log_len, fd) != log_len) { + fprintf(stderr, "Failed to read evtlog to buffer\n"); + goto close_fd; + } + + err = nvme_parse_evtlog(pevent_log_info, log_len, output); + } + +close_fd: + fclose(fd); +free: + nvme_free(pevent_log_info, huge); +free_pevent: + free(pevent); +ret: + return err; +} + +static int sfx_dump_evtlog(int argc, char **argv, struct command *cmd, struct plugin *plugin) +{ + char *desc = "dump evtlog into file and parse"; + const char *file = "evtlog file(required)"; + const char *namespace_id = "desired namespace"; + const char *storage_medium = "evtlog storage medium\n" + "0: nand(default) 1: nor"; + const char *parse = "parse error & warning evtlog from evtlog file"; + const char *output = "parse result output file"; + struct nvme_dev *dev; + int err = 0; + + struct config { + char *file; + __u32 namespace_id; + __u32 storage_medium; + bool parse; + char *output; + }; + struct config cfg = { + .file = NULL, + .namespace_id = 0xffffffff, + .storage_medium = 0, + .parse = false, + .output = NULL, + }; + + OPT_ARGS(opts) = { + OPT_FILE("file", 'f', &cfg.file, file), + OPT_UINT("namespace_id", 'n', &cfg.namespace_id, namespace_id), + OPT_UINT("storage_medium", 's', &cfg.storage_medium, storage_medium), + OPT_FLAG("parse", 'p', &cfg.parse, parse), + OPT_FILE("output", 'o', &cfg.output, output), + OPT_END() + }; + + err = parse_and_open(&dev, argc, argv, desc, opts); + if (err) + goto ret; + + if (!cfg.file) { + fprintf(stderr, "file required param\n"); + err = EINVAL; + goto close_dev; + } + + if (cfg.parse && !cfg.output) { + fprintf(stderr, "output file required if evtlog need be parsed\n"); + err = EINVAL; + goto close_dev; + } + err = nvme_dump_evtlog(dev, cfg.namespace_id, cfg.storage_medium, cfg.file, cfg.parse, cfg.output); + +close_dev: dev_close(dev); +ret: + return err; +} + +static int nvme_expand_cap(struct nvme_dev *dev, __u32 namespace_id, __u64 namespace_size, + __u64 namespace_cap, __u32 lbaf, __u32 units) +{ + struct dirent **devices; + char dev_name[32] = ""; + int i = 0; + int num = 0; + int err = 0; + + struct sfx_expand_cap_info { + __u64 namespace_size; + __u64 namespace_cap; + __u8 reserve[10]; + __u8 lbaf; + __u8 reserve1[5]; + } __packed; + + if (S_ISCHR(dev->direct.stat.st_mode)) + snprintf(dev_name, 32, "%sn%u", dev->name, namespace_id); + else + strcpy(dev_name, dev->name); + + num = scandir("/dev", &devices, nvme_namespace_filter, alphasort); + if (num <= 0) { + err = num; + goto ret; + } + + if (strcmp(dev_name, devices[num-1]->d_name)) { + fprintf(stderr, "Expand namespace not the last one\n"); + err = EINVAL; + goto free_devices; + } + + if (!units) { + namespace_size = IDEMA_CAP(namespace_size) / (1 << (lbaf * 3)); + namespace_cap = IDEMA_CAP(namespace_cap) / (1 << (lbaf * 3)); + } + + struct sfx_expand_cap_info info = { + .namespace_size = namespace_size, + .namespace_cap = namespace_cap, + .lbaf = lbaf, + }; + + struct nvme_passthru_cmd cmd = { + .opcode = nvme_admin_ns_mgmt, + .nsid = namespace_id, + .addr = (__u64)(uintptr_t)&info, + .data_len = sizeof(info), + .cdw10 = 0x0e, + }; + + err = nvme_submit_admin_passthru(dev_fd(dev), &cmd, NULL); + if (err) { + fprintf(stderr, "Create ns failed\n"); + nvme_show_status(err); + goto free_devices; + } + +free_devices: + for (i = 0; i < num; i++) + free(devices[i]); + free(devices); +ret: return err; +} + +static int sfx_expand_cap(int argc, char **argv, struct command *cmd, struct plugin *plugin) +{ + char *desc = "expand capacity"; + const char *namespace_id = "desired namespace"; + const char *namespace_size = "namespace size(required)"; + const char *namespace_cap = "namespace capacity(required)"; + const char *lbaf = "LBA format to apply\n" + "0: 512(default) 1: 4096"; + const char *units = "namespace size/capacity units\n" + "0: GB(default) 1: LBA"; + struct nvme_dev *dev; + int err = 0; + + struct config { + __u32 namespace_id; + __u64 namespace_size; + __u64 namespace_cap; + __u32 lbaf; + __u32 units; + }; + struct config cfg = { + .namespace_id = 0xffffffff, + .lbaf = 0, + .units = 0, + }; + + OPT_ARGS(opts) = { + OPT_UINT("namespace_id", 'n', &cfg.namespace_id, namespace_id), + OPT_LONG("namespace_size", 's', &cfg.namespace_size, namespace_size), + OPT_LONG("namespace_cap", 'c', &cfg.namespace_cap, namespace_cap), + OPT_UINT("lbaf", 'l', &cfg.lbaf, lbaf), + OPT_UINT("units", 'u', &cfg.units, units), + OPT_END() + }; + + err = parse_and_open(&dev, argc, argv, desc, opts); + if (err) + goto ret; + if (cfg.namespace_id == 0xffffffff) { + if (S_ISCHR(dev->direct.stat.st_mode)) { + fprintf(stderr, "namespace_id or blk device required\n"); + err = EINVAL; + goto ret; + } else { + cfg.namespace_id = atoi(&dev->name[strlen(dev->name) - 1]); + } + } + + if (!cfg.namespace_size) { + fprintf(stderr, "namespace_size required param\n"); + err = EINVAL; + goto close_dev; + } + + if (!cfg.namespace_cap) { + fprintf(stderr, "namespace_cap required param\n"); + err = EINVAL; + goto close_dev; + } + + err = nvme_expand_cap(dev, cfg.namespace_id, cfg.namespace_size, cfg.namespace_cap, cfg.lbaf, cfg.units); + if (err) + goto close_dev; + + printf("%s: Success, create nsid:%d\n", cmd->name, cfg.namespace_id); + +close_dev: + dev_close(dev); +ret: + return err; } diff --git a/plugins/scaleflux/sfx-nvme.h b/plugins/scaleflux/sfx-nvme.h index 0b95d92..53e2217 100644 --- a/plugins/scaleflux/sfx-nvme.h +++ b/plugins/scaleflux/sfx-nvme.h @@ -16,6 +16,8 @@ PLUGIN(NAME("sfx", "ScaleFlux vendor specific extensions", NVME_VERSION), ENTRY("change-cap", "Dynamic change capacity", change_cap) ENTRY("set-feature", "Set a feature", sfx_set_feature) ENTRY("get-feature", "Get a feature", sfx_get_feature) + ENTRY("dump-evtlog", "dump evtlog into file and parse warning & error log", sfx_dump_evtlog) + ENTRY("expand-cap", "expand the last namespace capacity lossless", sfx_expand_cap) ) ); |