target_core_user.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
  2. #ifndef __TARGET_CORE_USER_H
  3. #define __TARGET_CORE_USER_H
  4. /* This header will be used by application too */
  5. #include <linux/types.h>
  6. #include <linux/uio.h>
  7. #define TCMU_VERSION "2.0"
  8. /**
  9. * DOC: Ring Design
  10. * Ring Design
  11. * -----------
  12. *
  13. * The mmaped area is divided into three parts:
  14. * 1) The mailbox (struct tcmu_mailbox, below);
  15. * 2) The command ring;
  16. * 3) Everything beyond the command ring (data).
  17. *
  18. * The mailbox tells userspace the offset of the command ring from the
  19. * start of the shared memory region, and how big the command ring is.
  20. *
  21. * The kernel passes SCSI commands to userspace by putting a struct
  22. * tcmu_cmd_entry in the ring, updating mailbox->cmd_head, and poking
  23. * userspace via UIO's interrupt mechanism.
  24. *
  25. * tcmu_cmd_entry contains a header. If the header type is PAD,
  26. * userspace should skip hdr->length bytes (mod cmdr_size) to find the
  27. * next cmd_entry.
  28. *
  29. * Otherwise, the entry will contain offsets into the mmaped area that
  30. * contain the cdb and data buffers -- the latter accessible via the
  31. * iov array. iov addresses are also offsets into the shared area.
  32. *
  33. * When userspace is completed handling the command, set
  34. * entry->rsp.scsi_status, fill in rsp.sense_buffer if appropriate,
  35. * and also set mailbox->cmd_tail equal to the old cmd_tail plus
  36. * hdr->length, mod cmdr_size. If cmd_tail doesn't equal cmd_head, it
  37. * should process the next packet the same way, and so on.
  38. */
  39. #define TCMU_MAILBOX_VERSION 2
  40. #define ALIGN_SIZE 64 /* Should be enough for most CPUs */
  41. #define TCMU_MAILBOX_FLAG_CAP_OOOC (1 << 0) /* Out-of-order completions */
  42. #define TCMU_MAILBOX_FLAG_CAP_READ_LEN (1 << 1) /* Read data length */
  43. struct tcmu_mailbox {
  44. __u16 version;
  45. __u16 flags;
  46. __u32 cmdr_off;
  47. __u32 cmdr_size;
  48. __u32 cmd_head;
  49. /* Updated by user. On its own cacheline */
  50. __u32 cmd_tail __attribute__((__aligned__(ALIGN_SIZE)));
  51. } __packed;
  52. enum tcmu_opcode {
  53. TCMU_OP_PAD = 0,
  54. TCMU_OP_CMD,
  55. };
  56. /*
  57. * Only a few opcodes, and length is 8-byte aligned, so use low bits for opcode.
  58. */
  59. struct tcmu_cmd_entry_hdr {
  60. __u32 len_op;
  61. __u16 cmd_id;
  62. __u8 kflags;
  63. #define TCMU_UFLAG_UNKNOWN_OP 0x1
  64. #define TCMU_UFLAG_READ_LEN 0x2
  65. __u8 uflags;
  66. } __packed;
  67. #define TCMU_OP_MASK 0x7
  68. static inline enum tcmu_opcode tcmu_hdr_get_op(__u32 len_op)
  69. {
  70. return len_op & TCMU_OP_MASK;
  71. }
  72. static inline void tcmu_hdr_set_op(__u32 *len_op, enum tcmu_opcode op)
  73. {
  74. *len_op &= ~TCMU_OP_MASK;
  75. *len_op |= (op & TCMU_OP_MASK);
  76. }
  77. static inline __u32 tcmu_hdr_get_len(__u32 len_op)
  78. {
  79. return len_op & ~TCMU_OP_MASK;
  80. }
  81. static inline void tcmu_hdr_set_len(__u32 *len_op, __u32 len)
  82. {
  83. *len_op &= TCMU_OP_MASK;
  84. *len_op |= len;
  85. }
  86. /* Currently the same as SCSI_SENSE_BUFFERSIZE */
  87. #define TCMU_SENSE_BUFFERSIZE 96
  88. struct tcmu_cmd_entry {
  89. struct tcmu_cmd_entry_hdr hdr;
  90. union {
  91. struct {
  92. __u32 iov_cnt;
  93. __u32 iov_bidi_cnt;
  94. __u32 iov_dif_cnt;
  95. __u64 cdb_off;
  96. __u64 __pad1;
  97. __u64 __pad2;
  98. struct iovec iov[0];
  99. } req;
  100. struct {
  101. __u8 scsi_status;
  102. __u8 __pad1;
  103. __u16 __pad2;
  104. __u32 read_len;
  105. char sense_buffer[TCMU_SENSE_BUFFERSIZE];
  106. } rsp;
  107. };
  108. } __packed;
  109. #define TCMU_OP_ALIGN_SIZE sizeof(__u64)
  110. enum tcmu_genl_cmd {
  111. TCMU_CMD_UNSPEC,
  112. TCMU_CMD_ADDED_DEVICE,
  113. TCMU_CMD_REMOVED_DEVICE,
  114. TCMU_CMD_RECONFIG_DEVICE,
  115. TCMU_CMD_ADDED_DEVICE_DONE,
  116. TCMU_CMD_REMOVED_DEVICE_DONE,
  117. TCMU_CMD_RECONFIG_DEVICE_DONE,
  118. TCMU_CMD_SET_FEATURES,
  119. __TCMU_CMD_MAX,
  120. };
  121. #define TCMU_CMD_MAX (__TCMU_CMD_MAX - 1)
  122. enum tcmu_genl_attr {
  123. TCMU_ATTR_UNSPEC,
  124. TCMU_ATTR_DEVICE,
  125. TCMU_ATTR_MINOR,
  126. TCMU_ATTR_PAD,
  127. TCMU_ATTR_DEV_CFG,
  128. TCMU_ATTR_DEV_SIZE,
  129. TCMU_ATTR_WRITECACHE,
  130. TCMU_ATTR_CMD_STATUS,
  131. TCMU_ATTR_DEVICE_ID,
  132. TCMU_ATTR_SUPP_KERN_CMD_REPLY,
  133. __TCMU_ATTR_MAX,
  134. };
  135. #define TCMU_ATTR_MAX (__TCMU_ATTR_MAX - 1)
  136. #endif