target_core_user.h 3.6 KB

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