2 * Copyright (C) 2017 NXP Semiconductors
3 * Copyright (C) 2017 Bin Meng <bmeng.cn@gmail.com>
5 * SPDX-License-Identifier: GPL-2.0+
13 #include <linux/math64.h>
15 static int nvme_curr_device;
17 static int do_nvme_scan(cmd_tbl_t *cmdtp, int flag,
18 int argc, char * const argv[])
22 ret = nvme_scan_namespace();
24 return CMD_RET_FAILURE;
26 return CMD_RET_SUCCESS;
29 static int do_nvme_list(cmd_tbl_t *cmdtp, int flag,
30 int argc, char * const argv[])
32 blk_list_devices(IF_TYPE_NVME);
34 return CMD_RET_SUCCESS;
37 static int do_nvme_info(cmd_tbl_t *cmdtp, int flag,
38 int argc, char * const argv[])
45 devnum = (int)simple_strtoul(argv[1], NULL, 10);
47 devnum = nvme_curr_device;
49 ret = blk_get_device(IF_TYPE_NVME, devnum, &udev);
51 return CMD_RET_FAILURE;
53 nvme_print_info(udev);
55 return CMD_RET_SUCCESS;
58 static int do_nvme_device(cmd_tbl_t *cmdtp, int flag,
59 int argc, char * const argv[])
62 int devnum = (int)simple_strtoul(argv[1], NULL, 10);
64 if (!blk_show_device(IF_TYPE_NVME, devnum)) {
65 nvme_curr_device = devnum;
66 printf("... is now current device\n");
68 return CMD_RET_FAILURE;
71 blk_show_device(IF_TYPE_NVME, nvme_curr_device);
74 return CMD_RET_SUCCESS;
77 static int do_nvme_part(cmd_tbl_t *cmdtp, int flag,
78 int argc, char * const argv[])
81 int devnum = (int)simple_strtoul(argv[2], NULL, 10);
83 if (blk_print_part_devnum(IF_TYPE_NVME, devnum)) {
84 printf("\nNVMe device %d not available\n", devnum);
85 return CMD_RET_FAILURE;
88 blk_print_part_devnum(IF_TYPE_NVME, nvme_curr_device);
91 return CMD_RET_SUCCESS;
94 static int do_nvme_read(cmd_tbl_t *cmdtp, int flag, int argc,
101 ulong addr = simple_strtoul(argv[1], NULL, 16);
102 ulong cnt = simple_strtoul(argv[3], NULL, 16);
104 lbaint_t blk = simple_strtoul(argv[2], NULL, 16);
106 printf("\nNVMe read: device %d block # " LBAFU " count %ld ... ",
107 nvme_curr_device, blk, cnt);
110 n = blk_read_devnum(IF_TYPE_NVME, nvme_curr_device, blk,
112 time = get_timer(time);
114 printf("read: %s\n", (n == cnt) ? "OK" : "ERROR");
115 printf("%lu bytes read in %lu ms", cnt * 512, time);
118 print_size(div_u64(cnt * 512, time) * 1000, "/s");
123 return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
126 static int do_nvme_write(cmd_tbl_t *cmdtp, int flag, int argc,
131 return CMD_RET_USAGE;
133 ulong addr = simple_strtoul(argv[1], NULL, 16);
134 ulong cnt = simple_strtoul(argv[3], NULL, 16);
136 lbaint_t blk = simple_strtoul(argv[2], NULL, 16);
138 printf("\nNVMe write: device %d block # " LBAFU " count %ld ... ",
139 nvme_curr_device, blk, cnt);
142 n = blk_write_devnum(IF_TYPE_NVME, nvme_curr_device, blk,
144 time = get_timer(time);
146 printf("write: %s\n", (n == cnt) ? "OK" : "ERROR");
147 printf("%lu bytes write in %lu ms", cnt * 512, time);
150 print_size(div_u64(cnt * 512, time) * 1000, "/s");
155 return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
158 static cmd_tbl_t cmd_nvme[] = {
159 U_BOOT_CMD_MKENT(scan, 1, 1, do_nvme_scan, "", ""),
160 U_BOOT_CMD_MKENT(list, 1, 1, do_nvme_list, "", ""),
161 U_BOOT_CMD_MKENT(info, 2, 1, do_nvme_info, "", ""),
162 U_BOOT_CMD_MKENT(device, 2, 1, do_nvme_device, "", ""),
163 U_BOOT_CMD_MKENT(part, 2, 1, do_nvme_part, "", ""),
164 U_BOOT_CMD_MKENT(write, 4, 0, do_nvme_write, "", ""),
165 U_BOOT_CMD_MKENT(read, 4, 0, do_nvme_read, "", "")
168 static int do_nvmecops(cmd_tbl_t *cmdtp, int flag, int argc,
173 cp = find_cmd_tbl(argv[1], cmd_nvme, ARRAY_SIZE(cmd_nvme));
178 if (cp == NULL || argc > cp->maxargs)
179 return CMD_RET_USAGE;
181 if (flag == CMD_FLAG_REPEAT && !cp->repeatable)
182 return CMD_RET_SUCCESS;
184 return cp->cmd(cmdtp, flag, argc, argv);
188 nvme, 8, 1, do_nvmecops,
189 "NVM Express sub-system",
190 "\nnvme scan - scan NVMe blk devices\n"
191 "nvme list - show all available NVMe blk devices\n"
192 "nvme info [dev]- show current or a specific NVMe blk device\n"
193 "nvme device [dev] - show or set current device\n"
194 "nvme part [dev] - print partition table\n"
195 "nvme read addr blk# cnt\n"
196 "nvme write addr blk# cnt"