|
| 1 | +// SPDX-License-Identifier: GPL-3.0+ |
| 2 | +// Copyright (C) 2026 Swarna Prabhu, Samsung Electronics |
| 3 | +/* |
| 4 | + * Simple test exercising the admin queue accesses via io_uring passthrough |
| 5 | + * commands. |
| 6 | + */ |
| 7 | +#include <errno.h> |
| 8 | +#include <fcntl.h> |
| 9 | +#include <stdio.h> |
| 10 | +#include <stdlib.h> |
| 11 | +#include <string.h> |
| 12 | +#include <liburing.h> |
| 13 | +#include <linux/nvme_ioctl.h> |
| 14 | + |
| 15 | +#define NVME_IDENTIFY_ADMIN_CMD 0x06 /* Identify command using admin queue */ |
| 16 | +#define NVME_IDENTIFY_CNS_CTRL 0x01 /* Identify controller command to a NVME device */ |
| 17 | +struct nvme_id_ctrl { |
| 18 | + __le16 vid; |
| 19 | + __le16 ssvid; |
| 20 | + char sn[20]; |
| 21 | + char mn[40]; |
| 22 | + char fr[8]; |
| 23 | + __u8 rab; |
| 24 | + __u8 ieee[3]; |
| 25 | + char pad[4020]; |
| 26 | +}; |
| 27 | + |
| 28 | + |
| 29 | +int main(int argc, char **argv) |
| 30 | +{ |
| 31 | + int fd, ret; |
| 32 | + struct nvme_passthru_cmd *cmd; |
| 33 | + struct nvme_id_ctrl *nctrl; |
| 34 | + struct io_uring nvring; |
| 35 | + int queue_depth = 80; |
| 36 | + struct io_uring_sqe *sqe = NULL; |
| 37 | + struct io_uring_cqe *cqe = NULL; |
| 38 | + |
| 39 | + if (argc < 2) { |
| 40 | + fprintf(stderr, "usage: %s /dev/nvmeXnY\n", argv[0]); |
| 41 | + return -EINVAL; |
| 42 | + } |
| 43 | + |
| 44 | + fd = open(argv[1], O_RDONLY); |
| 45 | + if (fd < 0) { |
| 46 | + perror("open"); |
| 47 | + return -errno; |
| 48 | + } |
| 49 | + |
| 50 | + nctrl = (struct nvme_id_ctrl *)calloc(1, sizeof(struct nvme_id_ctrl)); |
| 51 | + if (!nctrl) { |
| 52 | + fprintf(stderr, "Memory allocation failure\n"); |
| 53 | + ret = -ENOMEM; |
| 54 | + goto free_fd; |
| 55 | + } |
| 56 | + |
| 57 | + ret = io_uring_queue_init(queue_depth, &nvring, IORING_SETUP_SQE128 | IORING_SETUP_CQE32); |
| 58 | + if (ret < 0) { |
| 59 | + fprintf(stderr, "Initialize io uring fail %d \n", ret); |
| 60 | + goto free_nctrl; |
| 61 | + } |
| 62 | + /* Prepare the SQE to use the IORING_OP_URING_CMD opcode */ |
| 63 | + sqe = io_uring_get_sqe(&nvring); |
| 64 | + sqe->fd = fd; |
| 65 | + sqe->opcode = IORING_OP_URING_CMD; |
| 66 | + sqe->cmd_op = NVME_URING_CMD_ADMIN; |
| 67 | + |
| 68 | + cmd = (struct nvme_passthru_cmd *)&sqe->cmd; |
| 69 | + memset(cmd, 0, sizeof(*cmd)); |
| 70 | + |
| 71 | + /* populate the cmd struct for the opcode */ |
| 72 | + cmd->opcode = NVME_IDENTIFY_ADMIN_CMD; |
| 73 | + cmd->addr = (__u64)(uintptr_t)nctrl; |
| 74 | + cmd->data_len = sizeof(struct nvme_id_ctrl); |
| 75 | + cmd->cdw10 = NVME_IDENTIFY_CNS_CTRL; |
| 76 | + |
| 77 | + /*submit the SQE */ |
| 78 | + io_uring_submit(&nvring); |
| 79 | + |
| 80 | + ret = io_uring_wait_cqe(&nvring, &cqe); |
| 81 | + |
| 82 | + if (ret < 0) { |
| 83 | + fprintf(stderr, "wait_cqe: %s\n", strerror(-ret)); |
| 84 | + } else if (cqe && cqe->res < 0) { |
| 85 | + fprintf(stderr, "Command failed (cqe->res): %d\n", cqe->res); |
| 86 | + ret = cqe->res; |
| 87 | + } else { |
| 88 | + ret = 0; |
| 89 | + } |
| 90 | + |
| 91 | + if (cqe) |
| 92 | + io_uring_cqe_seen(&nvring, cqe); |
| 93 | + io_uring_queue_exit(&nvring); |
| 94 | +free_nctrl: |
| 95 | + free(nctrl); |
| 96 | +free_fd: |
| 97 | + close(fd); |
| 98 | + |
| 99 | + return ret; |
| 100 | +} |
0 commit comments