vscsiif.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /* SPDX-License-Identifier: MIT */
  2. /******************************************************************************
  3. * vscsiif.h
  4. *
  5. * Based on the blkif.h code.
  6. *
  7. * Copyright(c) FUJITSU Limited 2008.
  8. */
  9. #ifndef __XEN__PUBLIC_IO_SCSI_H__
  10. #define __XEN__PUBLIC_IO_SCSI_H__
  11. #include "ring.h"
  12. #include "../grant_table.h"
  13. /*
  14. * Feature and Parameter Negotiation
  15. * =================================
  16. * The two halves of a Xen pvSCSI driver utilize nodes within the XenStore to
  17. * communicate capabilities and to negotiate operating parameters. This
  18. * section enumerates these nodes which reside in the respective front and
  19. * backend portions of the XenStore, following the XenBus convention.
  20. *
  21. * Any specified default value is in effect if the corresponding XenBus node
  22. * is not present in the XenStore.
  23. *
  24. * XenStore nodes in sections marked "PRIVATE" are solely for use by the
  25. * driver side whose XenBus tree contains them.
  26. *
  27. *****************************************************************************
  28. * Backend XenBus Nodes
  29. *****************************************************************************
  30. *
  31. *------------------ Backend Device Identification (PRIVATE) ------------------
  32. *
  33. * p-devname
  34. * Values: string
  35. *
  36. * A free string used to identify the physical device (e.g. a disk name).
  37. *
  38. * p-dev
  39. * Values: string
  40. *
  41. * A string specifying the backend device: either a 4-tuple "h:c:t:l"
  42. * (host, controller, target, lun, all integers), or a WWN (e.g.
  43. * "naa.60014054ac780582:0").
  44. *
  45. * v-dev
  46. * Values: string
  47. *
  48. * A string specifying the frontend device in form of a 4-tuple "h:c:t:l"
  49. * (host, controller, target, lun, all integers).
  50. *
  51. *--------------------------------- Features ---------------------------------
  52. *
  53. * feature-sg-grant
  54. * Values: unsigned [VSCSIIF_SG_TABLESIZE...65535]
  55. * Default Value: 0
  56. *
  57. * Specifies the maximum number of scatter/gather elements in grant pages
  58. * supported. If not set, the backend supports up to VSCSIIF_SG_TABLESIZE
  59. * SG elements specified directly in the request.
  60. *
  61. *****************************************************************************
  62. * Frontend XenBus Nodes
  63. *****************************************************************************
  64. *
  65. *----------------------- Request Transport Parameters -----------------------
  66. *
  67. * event-channel
  68. * Values: unsigned
  69. *
  70. * The identifier of the Xen event channel used to signal activity
  71. * in the ring buffer.
  72. *
  73. * ring-ref
  74. * Values: unsigned
  75. *
  76. * The Xen grant reference granting permission for the backend to map
  77. * the sole page in a single page sized ring buffer.
  78. *
  79. * protocol
  80. * Values: string (XEN_IO_PROTO_ABI_*)
  81. * Default Value: XEN_IO_PROTO_ABI_NATIVE
  82. *
  83. * The machine ABI rules governing the format of all ring request and
  84. * response structures.
  85. */
  86. /*
  87. * Xenstore format in practice
  88. * ===========================
  89. *
  90. * The backend driver uses a single_host:many_devices notation to manage domU
  91. * devices. Everything is stored in /local/domain/<backend_domid>/backend/vscsi/.
  92. * The xenstore layout looks like this (dom0 is assumed to be the backend_domid):
  93. *
  94. * <domid>/<vhost>/feature-host = "0"
  95. * <domid>/<vhost>/frontend = "/local/domain/<domid>/device/vscsi/0"
  96. * <domid>/<vhost>/frontend-id = "<domid>"
  97. * <domid>/<vhost>/online = "1"
  98. * <domid>/<vhost>/state = "4"
  99. * <domid>/<vhost>/vscsi-devs/dev-0/p-dev = "8:0:2:1" or "naa.wwn:lun"
  100. * <domid>/<vhost>/vscsi-devs/dev-0/state = "4"
  101. * <domid>/<vhost>/vscsi-devs/dev-0/v-dev = "0:0:0:0"
  102. * <domid>/<vhost>/vscsi-devs/dev-1/p-dev = "8:0:2:2"
  103. * <domid>/<vhost>/vscsi-devs/dev-1/state = "4"
  104. * <domid>/<vhost>/vscsi-devs/dev-1/v-dev = "0:0:1:0"
  105. *
  106. * The frontend driver maintains its state in
  107. * /local/domain/<domid>/device/vscsi/.
  108. *
  109. * <vhost>/backend = "/local/domain/0/backend/vscsi/<domid>/<vhost>"
  110. * <vhost>/backend-id = "0"
  111. * <vhost>/event-channel = "20"
  112. * <vhost>/ring-ref = "43"
  113. * <vhost>/state = "4"
  114. * <vhost>/vscsi-devs/dev-0/state = "4"
  115. * <vhost>/vscsi-devs/dev-1/state = "4"
  116. *
  117. * In addition to the entries for backend and frontend these flags are stored
  118. * for the toolstack:
  119. *
  120. * <domid>/<vhost>/vscsi-devs/dev-1/p-devname = "/dev/$device"
  121. * <domid>/<vhost>/libxl_ctrl_index = "0"
  122. *
  123. *
  124. * Backend/frontend protocol
  125. * =========================
  126. *
  127. * To create a vhost along with a device:
  128. * <domid>/<vhost>/feature-host = "0"
  129. * <domid>/<vhost>/frontend = "/local/domain/<domid>/device/vscsi/0"
  130. * <domid>/<vhost>/frontend-id = "<domid>"
  131. * <domid>/<vhost>/online = "1"
  132. * <domid>/<vhost>/state = "1"
  133. * <domid>/<vhost>/vscsi-devs/dev-0/p-dev = "8:0:2:1"
  134. * <domid>/<vhost>/vscsi-devs/dev-0/state = "1"
  135. * <domid>/<vhost>/vscsi-devs/dev-0/v-dev = "0:0:0:0"
  136. * Wait for <domid>/<vhost>/state + <domid>/<vhost>/vscsi-devs/dev-0/state become 4
  137. *
  138. * To add another device to a vhost:
  139. * <domid>/<vhost>/state = "7"
  140. * <domid>/<vhost>/vscsi-devs/dev-1/p-dev = "8:0:2:2"
  141. * <domid>/<vhost>/vscsi-devs/dev-1/state = "1"
  142. * <domid>/<vhost>/vscsi-devs/dev-1/v-dev = "0:0:1:0"
  143. * Wait for <domid>/<vhost>/state + <domid>/<vhost>/vscsi-devs/dev-1/state become 4
  144. *
  145. * To remove a device from a vhost:
  146. * <domid>/<vhost>/state = "7"
  147. * <domid>/<vhost>/vscsi-devs/dev-1/state = "5"
  148. * Wait for <domid>/<vhost>/state to become 4
  149. * Wait for <domid>/<vhost>/vscsi-devs/dev-1/state become 6
  150. * Remove <domid>/<vhost>/vscsi-devs/dev-1/{state,p-dev,v-dev,p-devname}
  151. * Remove <domid>/<vhost>/vscsi-devs/dev-1/
  152. *
  153. */
  154. /* Requests from the frontend to the backend */
  155. /*
  156. * Request a SCSI operation specified via a CDB in vscsiif_request.cmnd.
  157. * The target is specified via channel, id and lun.
  158. *
  159. * The operation to be performed is specified via a CDB in cmnd[], the length
  160. * of the CDB is in cmd_len. sc_data_direction specifies the direction of data
  161. * (to the device, from the device, or none at all).
  162. *
  163. * If data is to be transferred to or from the device the buffer(s) in the
  164. * guest memory is/are specified via one or multiple scsiif_request_segment
  165. * descriptors each specifying a memory page via a grant_ref_t, a offset into
  166. * the page and the length of the area in that page. All scsiif_request_segment
  167. * areas concatenated form the resulting data buffer used by the operation.
  168. * If the number of scsiif_request_segment areas is not too large (less than
  169. * or equal VSCSIIF_SG_TABLESIZE) the areas can be specified directly in the
  170. * seg[] array and the number of valid scsiif_request_segment elements is to be
  171. * set in nr_segments.
  172. *
  173. * If "feature-sg-grant" in the Xenstore is set it is possible to specify more
  174. * than VSCSIIF_SG_TABLESIZE scsiif_request_segment elements via indirection.
  175. * The maximum number of allowed scsiif_request_segment elements is the value
  176. * of the "feature-sg-grant" entry from Xenstore. When using indirection the
  177. * seg[] array doesn't contain specifications of the data buffers, but
  178. * references to scsiif_request_segment arrays, which in turn reference the
  179. * data buffers. While nr_segments holds the number of populated seg[] entries
  180. * (plus the set VSCSIIF_SG_GRANT bit), the number of scsiif_request_segment
  181. * elements referencing the target data buffers is calculated from the lengths
  182. * of the seg[] elements (the sum of all valid seg[].length divided by the
  183. * size of one scsiif_request_segment structure). The frontend may use a mix of
  184. * direct and indirect requests.
  185. */
  186. #define VSCSIIF_ACT_SCSI_CDB 1
  187. /*
  188. * Request abort of a running operation for the specified target given by
  189. * channel, id, lun and the operation's rqid in ref_rqid.
  190. */
  191. #define VSCSIIF_ACT_SCSI_ABORT 2
  192. /*
  193. * Request a device reset of the specified target (channel and id).
  194. */
  195. #define VSCSIIF_ACT_SCSI_RESET 3
  196. /*
  197. * Preset scatter/gather elements for a following request. Deprecated.
  198. * Keeping the define only to avoid usage of the value "4" for other actions.
  199. */
  200. #define VSCSIIF_ACT_SCSI_SG_PRESET 4
  201. /*
  202. * Maximum scatter/gather segments per request.
  203. *
  204. * Considering balance between allocating at least 16 "vscsiif_request"
  205. * structures on one page (4096 bytes) and the number of scatter/gather
  206. * elements needed, we decided to use 26 as a magic number.
  207. *
  208. * If "feature-sg-grant" is set, more scatter/gather elements can be specified
  209. * by placing them in one or more (up to VSCSIIF_SG_TABLESIZE) granted pages.
  210. * In this case the vscsiif_request seg elements don't contain references to
  211. * the user data, but to the SG elements referencing the user data.
  212. */
  213. #define VSCSIIF_SG_TABLESIZE 26
  214. /*
  215. * based on Linux kernel 2.6.18, still valid
  216. *
  217. * Changing these values requires support of multiple protocols via the rings
  218. * as "old clients" will blindly use these values and the resulting structure
  219. * sizes.
  220. */
  221. #define VSCSIIF_MAX_COMMAND_SIZE 16
  222. #define VSCSIIF_SENSE_BUFFERSIZE 96
  223. #define VSCSIIF_PAGE_SIZE 4096
  224. struct scsiif_request_segment {
  225. grant_ref_t gref;
  226. uint16_t offset;
  227. uint16_t length;
  228. };
  229. #define VSCSIIF_SG_PER_PAGE (VSCSIIF_PAGE_SIZE / \
  230. sizeof(struct scsiif_request_segment))
  231. /* Size of one request is 252 bytes */
  232. struct vscsiif_request {
  233. uint16_t rqid; /* private guest value, echoed in resp */
  234. uint8_t act; /* command between backend and frontend */
  235. uint8_t cmd_len; /* valid CDB bytes */
  236. uint8_t cmnd[VSCSIIF_MAX_COMMAND_SIZE]; /* the CDB */
  237. uint16_t timeout_per_command; /* deprecated */
  238. uint16_t channel, id, lun; /* (virtual) device specification */
  239. uint16_t ref_rqid; /* command abort reference */
  240. uint8_t sc_data_direction; /* for DMA_TO_DEVICE(1)
  241. DMA_FROM_DEVICE(2)
  242. DMA_NONE(3) requests */
  243. uint8_t nr_segments; /* Number of pieces of scatter-gather */
  244. /*
  245. * flag in nr_segments: SG elements via grant page
  246. *
  247. * If VSCSIIF_SG_GRANT is set, the low 7 bits of nr_segments specify the number
  248. * of grant pages containing SG elements. Usable if "feature-sg-grant" set.
  249. */
  250. #define VSCSIIF_SG_GRANT 0x80
  251. struct scsiif_request_segment seg[VSCSIIF_SG_TABLESIZE];
  252. uint32_t reserved[3];
  253. };
  254. /* Size of one response is 252 bytes */
  255. struct vscsiif_response {
  256. uint16_t rqid; /* identifies request */
  257. uint8_t padding;
  258. uint8_t sense_len;
  259. uint8_t sense_buffer[VSCSIIF_SENSE_BUFFERSIZE];
  260. int32_t rslt;
  261. uint32_t residual_len; /* request bufflen -
  262. return the value from physical device */
  263. uint32_t reserved[36];
  264. };
  265. /* SCSI I/O status from vscsiif_response->rslt */
  266. #define XEN_VSCSIIF_RSLT_STATUS(x) ((x) & 0x00ff)
  267. /* Host I/O status from vscsiif_response->rslt */
  268. #define XEN_VSCSIIF_RSLT_HOST(x) (((x) & 0x00ff0000) >> 16)
  269. #define XEN_VSCSIIF_RSLT_HOST_OK 0
  270. /* Couldn't connect before timeout */
  271. #define XEN_VSCSIIF_RSLT_HOST_NO_CONNECT 1
  272. /* Bus busy through timeout */
  273. #define XEN_VSCSIIF_RSLT_HOST_BUS_BUSY 2
  274. /* Timed out for other reason */
  275. #define XEN_VSCSIIF_RSLT_HOST_TIME_OUT 3
  276. /* Bad target */
  277. #define XEN_VSCSIIF_RSLT_HOST_BAD_TARGET 4
  278. /* Abort for some other reason */
  279. #define XEN_VSCSIIF_RSLT_HOST_ABORT 5
  280. /* Parity error */
  281. #define XEN_VSCSIIF_RSLT_HOST_PARITY 6
  282. /* Internal error */
  283. #define XEN_VSCSIIF_RSLT_HOST_ERROR 7
  284. /* Reset by somebody */
  285. #define XEN_VSCSIIF_RSLT_HOST_RESET 8
  286. /* Unexpected interrupt */
  287. #define XEN_VSCSIIF_RSLT_HOST_BAD_INTR 9
  288. /* Force command past mid-layer */
  289. #define XEN_VSCSIIF_RSLT_HOST_PASSTHROUGH 10
  290. /* Retry requested */
  291. #define XEN_VSCSIIF_RSLT_HOST_SOFT_ERROR 11
  292. /* Hidden retry requested */
  293. #define XEN_VSCSIIF_RSLT_HOST_IMM_RETRY 12
  294. /* Requeue command requested */
  295. #define XEN_VSCSIIF_RSLT_HOST_REQUEUE 13
  296. /* Transport error disrupted I/O */
  297. #define XEN_VSCSIIF_RSLT_HOST_TRANSPORT_DISRUPTED 14
  298. /* Transport class fastfailed */
  299. #define XEN_VSCSIIF_RSLT_HOST_TRANSPORT_FAILFAST 15
  300. /* Permanent target failure */
  301. #define XEN_VSCSIIF_RSLT_HOST_TARGET_FAILURE 16
  302. /* Permanent nexus failure on path */
  303. #define XEN_VSCSIIF_RSLT_HOST_NEXUS_FAILURE 17
  304. /* Space allocation on device failed */
  305. #define XEN_VSCSIIF_RSLT_HOST_ALLOC_FAILURE 18
  306. /* Medium error */
  307. #define XEN_VSCSIIF_RSLT_HOST_MEDIUM_ERROR 19
  308. /* Transport marginal errors */
  309. #define XEN_VSCSIIF_RSLT_HOST_TRANSPORT_MARGINAL 20
  310. /* Result values of reset operations */
  311. #define XEN_VSCSIIF_RSLT_RESET_SUCCESS 0x2002
  312. #define XEN_VSCSIIF_RSLT_RESET_FAILED 0x2003
  313. DEFINE_RING_TYPES(vscsiif, struct vscsiif_request, struct vscsiif_response);
  314. #endif /*__XEN__PUBLIC_IO_SCSI_H__*/