|
1 | 1 | // SPDX-License-Identifier: GPL-2.0-or-later
|
2 | 2 |
|
| 3 | +#include <inttypes.h> |
| 4 | + |
| 5 | +#include <stdint.h> |
| 6 | +#include <sys/ioctl.h> |
| 7 | +#include <sys/syslog.h> |
| 8 | +#include <sys/time.h> |
| 9 | +#include <linux/types.h> |
| 10 | + |
| 11 | +#include <libnvme.h> |
| 12 | + |
3 | 13 | #include "logging.h"
|
4 | 14 |
|
5 | 15 | int log_level;
|
@@ -33,3 +43,92 @@ int map_log_level(int verbose, bool quiet)
|
33 | 43 |
|
34 | 44 | return log_level;
|
35 | 45 | }
|
| 46 | + |
| 47 | +static void nvme_show_common(struct nvme_passthru_cmd *cmd) |
| 48 | +{ |
| 49 | + printf("opcode : %02x\n", cmd->opcode); |
| 50 | + printf("flags : %02x\n", cmd->flags); |
| 51 | + printf("rsvd1 : %04x\n", cmd->rsvd1); |
| 52 | + printf("nsid : %08x\n", cmd->nsid); |
| 53 | + printf("cdw2 : %08x\n", cmd->cdw2); |
| 54 | + printf("cdw3 : %08x\n", cmd->cdw3); |
| 55 | + printf("data_len : %08x\n", cmd->data_len); |
| 56 | + printf("metadata_len : %08x\n", cmd->metadata_len); |
| 57 | + printf("addr : %"PRIx64"\n", (uint64_t)(uintptr_t)cmd->addr); |
| 58 | + printf("metadata : %"PRIx64"\n", (uint64_t)(uintptr_t)cmd->metadata); |
| 59 | + printf("cdw10 : %08x\n", cmd->cdw10); |
| 60 | + printf("cdw11 : %08x\n", cmd->cdw11); |
| 61 | + printf("cdw12 : %08x\n", cmd->cdw12); |
| 62 | + printf("cdw13 : %08x\n", cmd->cdw13); |
| 63 | + printf("cdw14 : %08x\n", cmd->cdw14); |
| 64 | + printf("cdw15 : %08x\n", cmd->cdw15); |
| 65 | + printf("timeout_ms : %08x\n", cmd->timeout_ms); |
| 66 | +} |
| 67 | + |
| 68 | +static void nvme_show_command(struct nvme_passthru_cmd *cmd, int err, struct timeval start, |
| 69 | + struct timeval end) |
| 70 | +{ |
| 71 | + nvme_show_common(cmd); |
| 72 | + printf("result : %08x\n", cmd->result); |
| 73 | + printf("err : %d\n", err); |
| 74 | + printf("latency : %lu us\n", |
| 75 | + (end.tv_sec - start.tv_sec) * 1000000 + (end.tv_usec - start.tv_usec)); |
| 76 | +} |
| 77 | + |
| 78 | +static void nvme_show_command64(struct nvme_passthru_cmd64 *cmd, int err, struct timeval start, |
| 79 | + struct timeval end) |
| 80 | +{ |
| 81 | + nvme_show_common((struct nvme_passthru_cmd *)cmd); |
| 82 | + printf("result : %"PRIx64"\n", (uint64_t)(uintptr_t)cmd->result); |
| 83 | + printf("err : %d\n", err); |
| 84 | + printf("latency : %lu us\n", |
| 85 | + (end.tv_sec - start.tv_sec) * 1000000 + (end.tv_usec - start.tv_usec)); |
| 86 | +} |
| 87 | + |
| 88 | +int nvme_submit_passthru(int fd, unsigned long ioctl_cmd, |
| 89 | + struct nvme_passthru_cmd *cmd, __u32 *result) |
| 90 | +{ |
| 91 | + struct timeval start; |
| 92 | + struct timeval end; |
| 93 | + int err; |
| 94 | + |
| 95 | + if (log_level >= LOG_DEBUG) |
| 96 | + gettimeofday(&start, NULL); |
| 97 | + |
| 98 | + err = ioctl(fd, ioctl_cmd, cmd); |
| 99 | + |
| 100 | + if (log_level >= LOG_DEBUG) { |
| 101 | + gettimeofday(&end, NULL); |
| 102 | + nvme_show_command(cmd, err, start, end); |
| 103 | + } |
| 104 | + |
| 105 | + if (err >= 0 && result) |
| 106 | + *result = cmd->result; |
| 107 | + |
| 108 | + return err; |
| 109 | +} |
| 110 | + |
| 111 | +int nvme_submit_passthru64(int fd, unsigned long ioctl_cmd, |
| 112 | + struct nvme_passthru_cmd64 *cmd, |
| 113 | + __u64 *result) |
| 114 | +{ |
| 115 | + struct timeval start; |
| 116 | + struct timeval end; |
| 117 | + int err; |
| 118 | + |
| 119 | + if (log_level >= LOG_DEBUG) |
| 120 | + gettimeofday(&start, NULL); |
| 121 | + |
| 122 | + |
| 123 | + err = ioctl(fd, ioctl_cmd, cmd); |
| 124 | + |
| 125 | + if (log_level >= LOG_DEBUG) { |
| 126 | + gettimeofday(&end, NULL); |
| 127 | + nvme_show_command64(cmd, err, start, end); |
| 128 | + } |
| 129 | + |
| 130 | + if (err >= 0 && result) |
| 131 | + *result = cmd->result; |
| 132 | + |
| 133 | + return err; |
| 134 | +} |
0 commit comments