target_core_base.h 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef TARGET_CORE_BASE_H
  3. #define TARGET_CORE_BASE_H
  4. #include <linux/configfs.h> /* struct config_group */
  5. #include <linux/dma-direction.h> /* enum dma_data_direction */
  6. #include <linux/sbitmap.h>
  7. #include <linux/percpu-refcount.h>
  8. #include <linux/semaphore.h> /* struct semaphore */
  9. #include <linux/completion.h>
  10. #define TARGET_CORE_VERSION "v5.0"
  11. /*
  12. * Maximum size of a CDB that can be stored in se_cmd without allocating
  13. * memory dynamically for the CDB.
  14. */
  15. #define TCM_MAX_COMMAND_SIZE 32
  16. /*
  17. * From include/scsi/scsi_cmnd.h:SCSI_SENSE_BUFFERSIZE, currently
  18. * defined 96, but the real limit is 252 (or 260 including the header)
  19. */
  20. #define TRANSPORT_SENSE_BUFFER 96
  21. /* Used by transport_send_check_condition_and_sense() */
  22. #define SPC_SENSE_KEY_OFFSET 2
  23. #define SPC_ADD_SENSE_LEN_OFFSET 7
  24. #define SPC_DESC_TYPE_OFFSET 8
  25. #define SPC_ADDITIONAL_DESC_LEN_OFFSET 9
  26. #define SPC_VALIDITY_OFFSET 10
  27. #define SPC_ASC_KEY_OFFSET 12
  28. #define SPC_ASCQ_KEY_OFFSET 13
  29. #define TRANSPORT_IQN_LEN 224
  30. /* Used by target_core_store_alua_lu_gp() and target_core_alua_lu_gp_show_attr_members() */
  31. #define LU_GROUP_NAME_BUF 256
  32. /* Used by core_alua_store_tg_pt_gp_info() and target_core_alua_tg_pt_gp_show_attr_members() */
  33. #define TG_PT_GROUP_NAME_BUF 256
  34. /* Used to parse VPD into struct t10_vpd */
  35. #define VPD_TMP_BUF_SIZE 254
  36. /* Used by transport_generic_cmd_sequencer() */
  37. #define READ_BLOCK_LEN 6
  38. #define READ_CAP_LEN 8
  39. #define READ_POSITION_LEN 20
  40. #define INQUIRY_LEN 36
  41. /* Used by transport_get_inquiry_vpd_serial() */
  42. #define INQUIRY_VPD_SERIAL_LEN 254
  43. /* Used by transport_get_inquiry_vpd_device_ident() */
  44. #define INQUIRY_VPD_DEVICE_IDENTIFIER_LEN 254
  45. #define INQUIRY_VENDOR_LEN 8
  46. #define INQUIRY_MODEL_LEN 16
  47. #define INQUIRY_REVISION_LEN 4
  48. /* Attempts before moving from SHORT to LONG */
  49. #define PYX_TRANSPORT_WINDOW_CLOSED_THRESHOLD 3
  50. #define PYX_TRANSPORT_WINDOW_CLOSED_WAIT_SHORT 3 /* In milliseconds */
  51. #define PYX_TRANSPORT_WINDOW_CLOSED_WAIT_LONG 10 /* In milliseconds */
  52. #define PYX_TRANSPORT_STATUS_INTERVAL 5 /* In seconds */
  53. /* struct se_dev_attrib sanity values */
  54. /* Default max_unmap_lba_count */
  55. #define DA_MAX_UNMAP_LBA_COUNT 0
  56. /* Default max_unmap_block_desc_count */
  57. #define DA_MAX_UNMAP_BLOCK_DESC_COUNT 0
  58. /* Default unmap_granularity */
  59. #define DA_UNMAP_GRANULARITY_DEFAULT 0
  60. /* Default unmap_granularity_alignment */
  61. #define DA_UNMAP_GRANULARITY_ALIGNMENT_DEFAULT 0
  62. /* Default unmap_zeroes_data */
  63. #define DA_UNMAP_ZEROES_DATA_DEFAULT 0
  64. /* Default max_write_same_len, disabled by default */
  65. #define DA_MAX_WRITE_SAME_LEN 0
  66. /* Use a model alias based on the configfs backend device name */
  67. #define DA_EMULATE_MODEL_ALIAS 0
  68. /* Emulation for WriteCache and SYNCHRONIZE_CACHE */
  69. #define DA_EMULATE_WRITE_CACHE 0
  70. /* Emulation for TASK_ABORTED status (TAS) by default */
  71. #define DA_EMULATE_TAS 1
  72. /* Emulation for Thin Provisioning UNMAP using block/blk-lib.c:blkdev_issue_discard() */
  73. #define DA_EMULATE_TPU 0
  74. /*
  75. * Emulation for Thin Provisioning WRITE_SAME w/ UNMAP=1 bit using
  76. * block/blk-lib.c:blkdev_issue_discard()
  77. */
  78. #define DA_EMULATE_TPWS 0
  79. /* Emulation for CompareAndWrite (AtomicTestandSet) by default */
  80. #define DA_EMULATE_CAW 1
  81. /* Emulation for 3rd Party Copy (ExtendedCopy) by default */
  82. #define DA_EMULATE_3PC 1
  83. /* No Emulation for PSCSI by default */
  84. #define DA_EMULATE_ALUA 0
  85. /* Emulate SCSI2 RESERVE/RELEASE and Persistent Reservations by default */
  86. #define DA_EMULATE_PR 1
  87. /* Emulation for REPORT SUPPORTED OPERATION CODES */
  88. #define DA_EMULATE_RSOC 1
  89. /* Enforce SCSI Initiator Port TransportID with 'ISID' for PR */
  90. #define DA_ENFORCE_PR_ISIDS 1
  91. /* Force SPC-3 PR Activate Persistence across Target Power Loss */
  92. #define DA_FORCE_PR_APTPL 0
  93. #define DA_STATUS_MAX_SECTORS_MIN 16
  94. #define DA_STATUS_MAX_SECTORS_MAX 8192
  95. /* By default don't report non-rotating (solid state) medium */
  96. #define DA_IS_NONROT 0
  97. /* Queue Algorithm Modifier default for restricted reordering in control mode page */
  98. #define DA_EMULATE_REST_REORD 0
  99. #define SE_INQUIRY_BUF 1024
  100. #define SE_MODE_PAGE_BUF 512
  101. #define SE_SENSE_BUF 96
  102. /* Peripheral Device Text Identification Information */
  103. #define PD_TEXT_ID_INFO_LEN 256
  104. enum target_submit_type {
  105. /* Use the fabric driver's default submission type */
  106. TARGET_FABRIC_DEFAULT_SUBMIT,
  107. /* Submit from the calling context */
  108. TARGET_DIRECT_SUBMIT,
  109. /* Defer submission to the LIO workqueue */
  110. TARGET_QUEUE_SUBMIT,
  111. };
  112. /* struct se_hba->hba_flags */
  113. enum hba_flags_table {
  114. HBA_FLAGS_INTERNAL_USE = 0x01,
  115. HBA_FLAGS_PSCSI_MODE = 0x02,
  116. };
  117. /* Special transport agnostic struct se_cmd->t_states */
  118. enum transport_state_table {
  119. TRANSPORT_NO_STATE = 0,
  120. TRANSPORT_NEW_CMD = 1,
  121. TRANSPORT_WRITE_PENDING = 3,
  122. TRANSPORT_PROCESSING = 5,
  123. TRANSPORT_COMPLETE = 6,
  124. TRANSPORT_ISTATE_PROCESSING = 11,
  125. TRANSPORT_COMPLETE_QF_WP = 18,
  126. TRANSPORT_COMPLETE_QF_OK = 19,
  127. TRANSPORT_COMPLETE_QF_ERR = 20,
  128. };
  129. /* Used for struct se_cmd->se_cmd_flags */
  130. enum se_cmd_flags_table {
  131. SCF_SUPPORTED_SAM_OPCODE = (1 << 0),
  132. SCF_TRANSPORT_TASK_SENSE = (1 << 1),
  133. SCF_EMULATED_TASK_SENSE = (1 << 2),
  134. SCF_SCSI_DATA_CDB = (1 << 3),
  135. SCF_SCSI_TMR_CDB = (1 << 4),
  136. SCF_FUA = (1 << 5),
  137. SCF_SE_LUN_CMD = (1 << 6),
  138. SCF_BIDI = (1 << 7),
  139. SCF_SENT_CHECK_CONDITION = (1 << 8),
  140. SCF_OVERFLOW_BIT = (1 << 9),
  141. SCF_UNDERFLOW_BIT = (1 << 10),
  142. SCF_ALUA_NON_OPTIMIZED = (1 << 11),
  143. SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC = (1 << 12),
  144. SCF_COMPARE_AND_WRITE = (1 << 13),
  145. SCF_PASSTHROUGH_PROT_SG_TO_MEM_NOALLOC = (1 << 14),
  146. SCF_ACK_KREF = (1 << 15),
  147. SCF_USE_CPUID = (1 << 16),
  148. SCF_TASK_ATTR_SET = (1 << 17),
  149. SCF_TREAT_READ_AS_NORMAL = (1 << 18),
  150. SCF_TASK_ORDERED_SYNC = (1 << 19),
  151. SCF_ATOMIC = (1 << 20),
  152. };
  153. /*
  154. * Used by transport_send_check_condition_and_sense()
  155. * to signal which ASC/ASCQ sense payload should be built.
  156. */
  157. typedef unsigned __bitwise sense_reason_t;
  158. enum tcm_sense_reason_table {
  159. #define R(x) (__force sense_reason_t )(x)
  160. TCM_NO_SENSE = R(0x00),
  161. TCM_NON_EXISTENT_LUN = R(0x01),
  162. TCM_UNSUPPORTED_SCSI_OPCODE = R(0x02),
  163. TCM_INCORRECT_AMOUNT_OF_DATA = R(0x03),
  164. TCM_UNEXPECTED_UNSOLICITED_DATA = R(0x04),
  165. TCM_SERVICE_CRC_ERROR = R(0x05),
  166. TCM_SNACK_REJECTED = R(0x06),
  167. TCM_SECTOR_COUNT_TOO_MANY = R(0x07),
  168. TCM_INVALID_CDB_FIELD = R(0x08),
  169. TCM_INVALID_PARAMETER_LIST = R(0x09),
  170. TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE = R(0x0a),
  171. TCM_UNKNOWN_MODE_PAGE = R(0x0b),
  172. TCM_WRITE_PROTECTED = R(0x0c),
  173. TCM_CHECK_CONDITION_ABORT_CMD = R(0x0d),
  174. TCM_CHECK_CONDITION_UNIT_ATTENTION = R(0x0e),
  175. TCM_RESERVATION_CONFLICT = R(0x10),
  176. TCM_ADDRESS_OUT_OF_RANGE = R(0x11),
  177. TCM_OUT_OF_RESOURCES = R(0x12),
  178. TCM_PARAMETER_LIST_LENGTH_ERROR = R(0x13),
  179. TCM_MISCOMPARE_VERIFY = R(0x14),
  180. TCM_LOGICAL_BLOCK_GUARD_CHECK_FAILED = R(0x15),
  181. TCM_LOGICAL_BLOCK_APP_TAG_CHECK_FAILED = R(0x16),
  182. TCM_LOGICAL_BLOCK_REF_TAG_CHECK_FAILED = R(0x17),
  183. TCM_COPY_TARGET_DEVICE_NOT_REACHABLE = R(0x18),
  184. TCM_TOO_MANY_TARGET_DESCS = R(0x19),
  185. TCM_UNSUPPORTED_TARGET_DESC_TYPE_CODE = R(0x1a),
  186. TCM_TOO_MANY_SEGMENT_DESCS = R(0x1b),
  187. TCM_UNSUPPORTED_SEGMENT_DESC_TYPE_CODE = R(0x1c),
  188. TCM_INSUFFICIENT_REGISTRATION_RESOURCES = R(0x1d),
  189. TCM_LUN_BUSY = R(0x1e),
  190. TCM_INVALID_FIELD_IN_COMMAND_IU = R(0x1f),
  191. TCM_ALUA_TG_PT_STANDBY = R(0x20),
  192. TCM_ALUA_TG_PT_UNAVAILABLE = R(0x21),
  193. TCM_ALUA_STATE_TRANSITION = R(0x22),
  194. TCM_ALUA_OFFLINE = R(0x23),
  195. #undef R
  196. };
  197. enum target_sc_flags_table {
  198. TARGET_SCF_BIDI_OP = 0x01,
  199. TARGET_SCF_ACK_KREF = 0x02,
  200. TARGET_SCF_UNKNOWN_SIZE = 0x04,
  201. TARGET_SCF_USE_CPUID = 0x08,
  202. };
  203. /* fabric independent task management function values */
  204. enum tcm_tmreq_table {
  205. TMR_ABORT_TASK = 1,
  206. TMR_ABORT_TASK_SET = 2,
  207. TMR_CLEAR_ACA = 3,
  208. TMR_CLEAR_TASK_SET = 4,
  209. TMR_LUN_RESET = 5,
  210. TMR_TARGET_WARM_RESET = 6,
  211. TMR_TARGET_COLD_RESET = 7,
  212. TMR_LUN_RESET_PRO = 0x80,
  213. TMR_UNKNOWN = 0xff,
  214. };
  215. /* fabric independent task management response values */
  216. enum tcm_tmrsp_table {
  217. TMR_FUNCTION_FAILED = 0,
  218. TMR_FUNCTION_COMPLETE = 1,
  219. TMR_TASK_DOES_NOT_EXIST = 2,
  220. TMR_LUN_DOES_NOT_EXIST = 3,
  221. TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED = 4,
  222. TMR_FUNCTION_REJECTED = 5,
  223. };
  224. /*
  225. * Used for target SCSI statistics
  226. */
  227. typedef enum {
  228. SCSI_INST_INDEX,
  229. SCSI_AUTH_INTR_INDEX,
  230. SCSI_INDEX_TYPE_MAX
  231. } scsi_index_t;
  232. struct se_cmd;
  233. struct t10_alua_lba_map_member {
  234. struct list_head lba_map_mem_list;
  235. int lba_map_mem_alua_state;
  236. int lba_map_mem_alua_pg_id;
  237. };
  238. struct t10_alua_lba_map {
  239. u64 lba_map_first_lba;
  240. u64 lba_map_last_lba;
  241. struct list_head lba_map_list;
  242. struct list_head lba_map_mem_list;
  243. };
  244. struct t10_alua {
  245. /* ALUA Target Port Group ID */
  246. u16 alua_tg_pt_gps_counter;
  247. u32 alua_tg_pt_gps_count;
  248. /* Referrals support */
  249. spinlock_t lba_map_lock;
  250. u32 lba_map_segment_size;
  251. u32 lba_map_segment_multiplier;
  252. struct list_head lba_map_list;
  253. spinlock_t tg_pt_gps_lock;
  254. struct se_device *t10_dev;
  255. /* Used for default ALUA Target Port Group */
  256. struct t10_alua_tg_pt_gp *default_tg_pt_gp;
  257. /* Used for default ALUA Target Port Group ConfigFS group */
  258. struct config_group alua_tg_pt_gps_group;
  259. struct list_head tg_pt_gps_list;
  260. };
  261. struct t10_alua_lu_gp {
  262. u16 lu_gp_id;
  263. int lu_gp_valid_id;
  264. u32 lu_gp_members;
  265. atomic_t lu_gp_ref_cnt;
  266. spinlock_t lu_gp_lock;
  267. struct config_group lu_gp_group;
  268. struct list_head lu_gp_node;
  269. struct list_head lu_gp_mem_list;
  270. };
  271. struct t10_alua_lu_gp_member {
  272. bool lu_gp_assoc;
  273. atomic_t lu_gp_mem_ref_cnt;
  274. spinlock_t lu_gp_mem_lock;
  275. struct t10_alua_lu_gp *lu_gp;
  276. struct se_device *lu_gp_mem_dev;
  277. struct list_head lu_gp_mem_list;
  278. };
  279. struct t10_alua_tg_pt_gp {
  280. u16 tg_pt_gp_id;
  281. int tg_pt_gp_valid_id;
  282. int tg_pt_gp_alua_supported_states;
  283. int tg_pt_gp_alua_access_status;
  284. int tg_pt_gp_alua_access_type;
  285. int tg_pt_gp_nonop_delay_msecs;
  286. int tg_pt_gp_trans_delay_msecs;
  287. int tg_pt_gp_implicit_trans_secs;
  288. int tg_pt_gp_pref;
  289. int tg_pt_gp_write_metadata;
  290. u32 tg_pt_gp_members;
  291. int tg_pt_gp_alua_access_state;
  292. atomic_t tg_pt_gp_ref_cnt;
  293. spinlock_t tg_pt_gp_lock;
  294. struct mutex tg_pt_gp_transition_mutex;
  295. struct se_device *tg_pt_gp_dev;
  296. struct config_group tg_pt_gp_group;
  297. struct list_head tg_pt_gp_list;
  298. struct list_head tg_pt_gp_lun_list;
  299. struct se_lun *tg_pt_gp_alua_lun;
  300. struct se_node_acl *tg_pt_gp_alua_nacl;
  301. };
  302. struct t10_vpd {
  303. unsigned char device_identifier[INQUIRY_VPD_DEVICE_IDENTIFIER_LEN];
  304. int protocol_identifier_set;
  305. u32 protocol_identifier;
  306. u32 device_identifier_code_set;
  307. u32 association;
  308. u32 device_identifier_type;
  309. struct list_head vpd_list;
  310. };
  311. struct t10_wwn {
  312. /*
  313. * SCSI left aligned strings may not be null terminated. +1 to ensure a
  314. * null terminator is always present.
  315. */
  316. char vendor[INQUIRY_VENDOR_LEN + 1];
  317. char model[INQUIRY_MODEL_LEN + 1];
  318. char revision[INQUIRY_REVISION_LEN + 1];
  319. char unit_serial[INQUIRY_VPD_SERIAL_LEN];
  320. u32 company_id;
  321. spinlock_t t10_vpd_lock;
  322. struct se_device *t10_dev;
  323. struct config_group t10_wwn_group;
  324. struct list_head t10_vpd_list;
  325. char pd_text_id_info[PD_TEXT_ID_INFO_LEN];
  326. };
  327. struct t10_pr_registration {
  328. /* Used for fabrics that contain WWN+ISID */
  329. #define PR_REG_ISID_LEN 16
  330. /* PR_REG_ISID_LEN + ',i,0x' */
  331. #define PR_REG_ISID_ID_LEN (PR_REG_ISID_LEN + 5)
  332. char pr_reg_isid[PR_REG_ISID_LEN];
  333. /* Used during APTPL metadata reading */
  334. #define PR_APTPL_MAX_IPORT_LEN 256
  335. unsigned char pr_iport[PR_APTPL_MAX_IPORT_LEN];
  336. /* Used during APTPL metadata reading */
  337. #define PR_APTPL_MAX_TPORT_LEN 256
  338. unsigned char pr_tport[PR_APTPL_MAX_TPORT_LEN];
  339. u16 pr_aptpl_rpti;
  340. u16 pr_reg_tpgt;
  341. /* Reservation effects all target ports */
  342. int pr_reg_all_tg_pt;
  343. /* Activate Persistence across Target Power Loss */
  344. int pr_reg_aptpl;
  345. int pr_res_holder;
  346. int pr_res_type;
  347. int pr_res_scope;
  348. /* Used for fabric initiator WWPNs using a ISID */
  349. bool isid_present_at_reg;
  350. u64 pr_res_mapped_lun;
  351. u64 pr_aptpl_target_lun;
  352. u16 tg_pt_sep_rtpi;
  353. u32 pr_res_generation;
  354. u64 pr_reg_bin_isid;
  355. u64 pr_res_key;
  356. atomic_t pr_res_holders;
  357. struct se_node_acl *pr_reg_nacl;
  358. /* Used by ALL_TG_PT=1 registration with deve->pr_ref taken */
  359. struct se_dev_entry *pr_reg_deve;
  360. struct list_head pr_reg_list;
  361. struct list_head pr_reg_abort_list;
  362. struct list_head pr_reg_aptpl_list;
  363. struct list_head pr_reg_atp_list;
  364. struct list_head pr_reg_atp_mem_list;
  365. };
  366. struct t10_reservation {
  367. /* Reservation effects all target ports */
  368. int pr_all_tg_pt;
  369. /* Activate Persistence across Target Power Loss enabled
  370. * for SCSI device */
  371. int pr_aptpl_active;
  372. #define PR_APTPL_BUF_LEN 262144
  373. u32 pr_generation;
  374. spinlock_t registration_lock;
  375. spinlock_t aptpl_reg_lock;
  376. /*
  377. * This will always be set by one individual I_T Nexus.
  378. * However with all_tg_pt=1, other I_T Nexus from the
  379. * same initiator can access PR reg/res info on a different
  380. * target port.
  381. *
  382. * There is also the 'All Registrants' case, where there is
  383. * a single *pr_res_holder of the reservation, but all
  384. * registrations are considered reservation holders.
  385. */
  386. struct se_node_acl *pr_res_holder;
  387. struct list_head registration_list;
  388. struct list_head aptpl_reg_list;
  389. };
  390. struct se_tmr_req {
  391. /* Task Management function to be performed */
  392. u8 function;
  393. /* Task Management response to send */
  394. u8 response;
  395. int call_transport;
  396. /* Reference to ITT that Task Mgmt should be performed */
  397. u64 ref_task_tag;
  398. void *fabric_tmr_ptr;
  399. struct se_cmd *task_cmd;
  400. struct se_device *tmr_dev;
  401. struct list_head tmr_list;
  402. };
  403. enum target_prot_op {
  404. TARGET_PROT_NORMAL = 0,
  405. TARGET_PROT_DIN_INSERT = (1 << 0),
  406. TARGET_PROT_DOUT_INSERT = (1 << 1),
  407. TARGET_PROT_DIN_STRIP = (1 << 2),
  408. TARGET_PROT_DOUT_STRIP = (1 << 3),
  409. TARGET_PROT_DIN_PASS = (1 << 4),
  410. TARGET_PROT_DOUT_PASS = (1 << 5),
  411. };
  412. #define TARGET_PROT_ALL TARGET_PROT_DIN_INSERT | TARGET_PROT_DOUT_INSERT | \
  413. TARGET_PROT_DIN_STRIP | TARGET_PROT_DOUT_STRIP | \
  414. TARGET_PROT_DIN_PASS | TARGET_PROT_DOUT_PASS
  415. enum target_prot_type {
  416. TARGET_DIF_TYPE0_PROT,
  417. TARGET_DIF_TYPE1_PROT,
  418. TARGET_DIF_TYPE2_PROT,
  419. TARGET_DIF_TYPE3_PROT,
  420. };
  421. /* Emulation for UNIT ATTENTION Interlock Control */
  422. enum target_ua_intlck_ctrl {
  423. TARGET_UA_INTLCK_CTRL_CLEAR = 0,
  424. TARGET_UA_INTLCK_CTRL_NO_CLEAR = 1,
  425. TARGET_UA_INTLCK_CTRL_ESTABLISH_UA = 2,
  426. };
  427. enum target_core_dif_check {
  428. TARGET_DIF_CHECK_GUARD = 0x1 << 0,
  429. TARGET_DIF_CHECK_APPTAG = 0x1 << 1,
  430. TARGET_DIF_CHECK_REFTAG = 0x1 << 2,
  431. };
  432. /* for sam_task_attr */
  433. #define TCM_SIMPLE_TAG 0x20
  434. #define TCM_HEAD_TAG 0x21
  435. #define TCM_ORDERED_TAG 0x22
  436. #define TCM_ACA_TAG 0x24
  437. struct se_cmd {
  438. /* Used for fail with specific sense codes */
  439. sense_reason_t sense_reason;
  440. /* SAM response code being sent to initiator */
  441. u8 scsi_status;
  442. u16 scsi_sense_length;
  443. unsigned unknown_data_length:1;
  444. bool state_active:1;
  445. u64 tag; /* SAM command identifier aka task tag */
  446. /* Delay for ALUA Active/NonOptimized state access in milliseconds */
  447. int alua_nonop_delay;
  448. /* See include/linux/dma-mapping.h */
  449. enum dma_data_direction data_direction;
  450. /* For SAM Task Attribute */
  451. int sam_task_attr;
  452. /* Used for se_sess->sess_tag_pool */
  453. unsigned int map_tag;
  454. int map_cpu;
  455. /* Transport protocol dependent state, see transport_state_table */
  456. enum transport_state_table t_state;
  457. /* See se_cmd_flags_table */
  458. u32 se_cmd_flags;
  459. /* Total size in bytes associated with command */
  460. u32 data_length;
  461. u32 residual_count;
  462. u64 orig_fe_lun;
  463. /* Persistent Reservation key */
  464. u64 pr_res_key;
  465. /* Used for sense data */
  466. void *sense_buffer;
  467. struct list_head se_delayed_node;
  468. struct list_head se_qf_node;
  469. struct se_device *se_dev;
  470. struct se_lun *se_lun;
  471. /* Only used for internal passthrough and legacy TCM fabric modules */
  472. struct se_session *se_sess;
  473. struct target_cmd_counter *cmd_cnt;
  474. struct se_tmr_req *se_tmr_req;
  475. struct llist_node se_cmd_list;
  476. struct completion *free_compl;
  477. struct completion *abrt_compl;
  478. const struct target_core_fabric_ops *se_tfo;
  479. sense_reason_t (*execute_cmd)(struct se_cmd *);
  480. sense_reason_t (*transport_complete_callback)(struct se_cmd *, bool, int *);
  481. void *protocol_data;
  482. unsigned char *t_task_cdb;
  483. unsigned char __t_task_cdb[TCM_MAX_COMMAND_SIZE];
  484. unsigned long long t_task_lba;
  485. unsigned int t_task_nolb;
  486. unsigned int transport_state;
  487. #define CMD_T_ABORTED (1 << 0)
  488. #define CMD_T_ACTIVE (1 << 1)
  489. #define CMD_T_COMPLETE (1 << 2)
  490. #define CMD_T_SENT (1 << 4)
  491. #define CMD_T_STOP (1 << 5)
  492. #define CMD_T_TAS (1 << 10)
  493. #define CMD_T_FABRIC_STOP (1 << 11)
  494. spinlock_t t_state_lock;
  495. struct kref cmd_kref;
  496. struct completion t_transport_stop_comp;
  497. struct work_struct work;
  498. struct scatterlist *t_data_sg;
  499. struct scatterlist *t_data_sg_orig;
  500. unsigned int t_data_nents;
  501. unsigned int t_data_nents_orig;
  502. void *t_data_vmap;
  503. struct scatterlist *t_bidi_data_sg;
  504. unsigned int t_bidi_data_nents;
  505. /* Used for lun->lun_ref counting */
  506. int lun_ref_active;
  507. struct list_head state_list;
  508. /* backend private data */
  509. void *priv;
  510. /* DIF related members */
  511. enum target_prot_op prot_op;
  512. enum target_prot_type prot_type;
  513. u8 prot_checks;
  514. bool prot_pto;
  515. u32 prot_length;
  516. u32 reftag_seed;
  517. struct scatterlist *t_prot_sg;
  518. unsigned int t_prot_nents;
  519. sense_reason_t pi_err;
  520. u64 sense_info;
  521. /*
  522. * CPU LIO will execute the cmd on. Defaults to the CPU the cmd is
  523. * initialized on. Drivers can override.
  524. */
  525. int cpuid;
  526. };
  527. struct se_ua {
  528. u8 ua_asc;
  529. u8 ua_ascq;
  530. struct list_head ua_nacl_list;
  531. };
  532. struct se_node_acl {
  533. char initiatorname[TRANSPORT_IQN_LEN];
  534. /* Used to signal demo mode created ACL, disabled by default */
  535. bool dynamic_node_acl;
  536. bool dynamic_stop;
  537. u32 queue_depth;
  538. u32 acl_index;
  539. enum target_prot_type saved_prot_type;
  540. #define MAX_ACL_TAG_SIZE 64
  541. char acl_tag[MAX_ACL_TAG_SIZE];
  542. /* Used for PR SPEC_I_PT=1 and REGISTER_AND_MOVE */
  543. atomic_t acl_pr_ref_count;
  544. struct hlist_head lun_entry_hlist;
  545. struct se_session *nacl_sess;
  546. struct se_portal_group *se_tpg;
  547. struct mutex lun_entry_mutex;
  548. spinlock_t nacl_sess_lock;
  549. struct config_group acl_group;
  550. struct config_group acl_attrib_group;
  551. struct config_group acl_auth_group;
  552. struct config_group acl_param_group;
  553. struct config_group acl_fabric_stat_group;
  554. struct list_head acl_list;
  555. struct list_head acl_sess_list;
  556. struct completion acl_free_comp;
  557. struct kref acl_kref;
  558. };
  559. static inline struct se_node_acl *acl_to_nacl(struct config_item *item)
  560. {
  561. return container_of(to_config_group(item), struct se_node_acl,
  562. acl_group);
  563. }
  564. static inline struct se_node_acl *attrib_to_nacl(struct config_item *item)
  565. {
  566. return container_of(to_config_group(item), struct se_node_acl,
  567. acl_attrib_group);
  568. }
  569. static inline struct se_node_acl *auth_to_nacl(struct config_item *item)
  570. {
  571. return container_of(to_config_group(item), struct se_node_acl,
  572. acl_auth_group);
  573. }
  574. static inline struct se_node_acl *param_to_nacl(struct config_item *item)
  575. {
  576. return container_of(to_config_group(item), struct se_node_acl,
  577. acl_param_group);
  578. }
  579. static inline struct se_node_acl *fabric_stat_to_nacl(struct config_item *item)
  580. {
  581. return container_of(to_config_group(item), struct se_node_acl,
  582. acl_fabric_stat_group);
  583. }
  584. struct target_cmd_counter {
  585. struct percpu_ref refcnt;
  586. wait_queue_head_t refcnt_wq;
  587. struct completion stop_done;
  588. atomic_t stopped;
  589. };
  590. struct se_session {
  591. u64 sess_bin_isid;
  592. enum target_prot_op sup_prot_ops;
  593. enum target_prot_type sess_prot_type;
  594. struct se_node_acl *se_node_acl;
  595. struct se_portal_group *se_tpg;
  596. void *fabric_sess_ptr;
  597. struct list_head sess_list;
  598. struct list_head sess_acl_list;
  599. spinlock_t sess_cmd_lock;
  600. void *sess_cmd_map;
  601. struct sbitmap_queue sess_tag_pool;
  602. struct target_cmd_counter *cmd_cnt;
  603. };
  604. struct se_device;
  605. struct se_transform_info;
  606. struct scatterlist;
  607. struct se_ml_stat_grps {
  608. struct config_group stat_group;
  609. struct config_group scsi_auth_intr_group;
  610. struct config_group scsi_att_intr_port_group;
  611. };
  612. struct se_lun_acl {
  613. u64 mapped_lun;
  614. struct se_node_acl *se_lun_nacl;
  615. struct se_lun *se_lun;
  616. struct config_group se_lun_group;
  617. struct se_ml_stat_grps ml_stat_grps;
  618. };
  619. struct se_dev_entry_io_stats {
  620. u64 total_cmds;
  621. u64 read_bytes;
  622. u64 write_bytes;
  623. };
  624. struct se_dev_entry {
  625. u64 mapped_lun;
  626. u64 pr_res_key;
  627. u64 creation_time;
  628. bool lun_access_ro;
  629. u32 attach_count;
  630. struct se_dev_entry_io_stats __percpu *stats;
  631. /* Used for PR SPEC_I_PT=1 and REGISTER_AND_MOVE */
  632. struct kref pr_kref;
  633. struct completion pr_comp;
  634. struct se_lun_acl *se_lun_acl;
  635. spinlock_t ua_lock;
  636. struct se_lun *se_lun;
  637. #define DEF_PR_REG_ACTIVE 1
  638. unsigned long deve_flags;
  639. struct list_head alua_port_list;
  640. struct list_head lun_link;
  641. struct list_head ua_list;
  642. struct hlist_node link;
  643. struct rcu_head rcu_head;
  644. };
  645. struct se_dev_attrib {
  646. bool emulate_model_alias;
  647. bool emulate_dpo; /* deprecated */
  648. bool emulate_fua_write;
  649. bool emulate_fua_read; /* deprecated */
  650. bool emulate_write_cache;
  651. enum target_ua_intlck_ctrl emulate_ua_intlck_ctrl;
  652. bool emulate_tas;
  653. bool emulate_tpu;
  654. bool emulate_tpws;
  655. bool emulate_caw;
  656. bool emulate_3pc;
  657. bool emulate_pr;
  658. bool emulate_rsoc;
  659. enum target_prot_type pi_prot_type;
  660. enum target_prot_type hw_pi_prot_type;
  661. bool pi_prot_verify;
  662. bool enforce_pr_isids;
  663. bool force_pr_aptpl;
  664. bool is_nonrot;
  665. bool emulate_rest_reord;
  666. bool unmap_zeroes_data;
  667. u32 hw_block_size;
  668. u32 block_size;
  669. u32 hw_max_sectors;
  670. u32 optimal_sectors;
  671. u32 hw_queue_depth;
  672. u32 queue_depth;
  673. u32 max_unmap_lba_count;
  674. u32 max_unmap_block_desc_count;
  675. u32 unmap_granularity;
  676. u32 unmap_granularity_alignment;
  677. u32 max_write_same_len;
  678. u32 atomic_max_len;
  679. u32 atomic_alignment;
  680. u32 atomic_granularity;
  681. u32 atomic_max_with_boundary;
  682. u32 atomic_max_boundary;
  683. u8 submit_type;
  684. struct se_device *da_dev;
  685. struct config_group da_group;
  686. };
  687. struct se_port_stat_grps {
  688. struct config_group stat_group;
  689. struct config_group scsi_port_group;
  690. struct config_group scsi_tgt_port_group;
  691. struct config_group scsi_transport_group;
  692. };
  693. struct scsi_port_stats {
  694. u64 cmd_pdus;
  695. u64 tx_data_octets;
  696. u64 rx_data_octets;
  697. };
  698. struct se_lun {
  699. u64 unpacked_lun;
  700. bool lun_shutdown;
  701. bool lun_access_ro;
  702. u32 lun_index;
  703. atomic_t lun_acl_count;
  704. struct se_device __rcu *lun_se_dev;
  705. struct list_head lun_deve_list;
  706. spinlock_t lun_deve_lock;
  707. /* ALUA state */
  708. int lun_tg_pt_secondary_stat;
  709. int lun_tg_pt_secondary_write_md;
  710. atomic_t lun_tg_pt_secondary_offline;
  711. struct mutex lun_tg_pt_md_mutex;
  712. /* ALUA target port group linkage */
  713. struct list_head lun_tg_pt_gp_link;
  714. struct t10_alua_tg_pt_gp __rcu *lun_tg_pt_gp;
  715. spinlock_t lun_tg_pt_gp_lock;
  716. struct se_portal_group *lun_tpg;
  717. struct scsi_port_stats __percpu *lun_stats;
  718. struct config_group lun_group;
  719. struct se_port_stat_grps port_stat_grps;
  720. struct completion lun_shutdown_comp;
  721. struct percpu_ref lun_ref;
  722. struct list_head lun_dev_link;
  723. struct hlist_node link;
  724. struct rcu_head rcu_head;
  725. };
  726. struct se_dev_stat_grps {
  727. struct config_group stat_group;
  728. struct config_group scsi_dev_group;
  729. struct config_group scsi_tgt_dev_group;
  730. struct config_group scsi_lu_group;
  731. };
  732. struct se_cmd_queue {
  733. struct llist_head cmd_list;
  734. struct work_struct work;
  735. };
  736. struct se_dev_plug {
  737. struct se_device *se_dev;
  738. };
  739. struct se_device_queue {
  740. struct list_head state_list;
  741. spinlock_t lock;
  742. struct se_cmd_queue sq;
  743. };
  744. struct se_dev_io_stats {
  745. u64 total_cmds;
  746. u64 read_bytes;
  747. u64 write_bytes;
  748. };
  749. struct se_device {
  750. /* Used for SAM Task Attribute ordering */
  751. u32 dev_cur_ordered_id;
  752. u32 dev_flags;
  753. #define DF_CONFIGURED 0x00000001
  754. #define DF_FIRMWARE_VPD_UNIT_SERIAL 0x00000002
  755. #define DF_EMULATED_VPD_UNIT_SERIAL 0x00000004
  756. #define DF_USING_UDEV_PATH 0x00000008
  757. #define DF_USING_ALIAS 0x00000010
  758. #define DF_READ_ONLY 0x00000020
  759. u8 transport_flags;
  760. /* Physical device queue depth */
  761. u32 queue_depth;
  762. /* Used for SPC-2 reservations enforce of ISIDs */
  763. u64 dev_res_bin_isid;
  764. /* Pointer to transport specific device structure */
  765. u32 dev_index;
  766. u64 creation_time;
  767. atomic_long_t num_resets;
  768. atomic_long_t aborts_complete;
  769. atomic_long_t aborts_no_task;
  770. struct se_dev_io_stats __percpu *stats;
  771. /* Active commands on this virtual SE device */
  772. struct percpu_ref non_ordered;
  773. bool ordered_sync_in_progress;
  774. atomic_t dev_qf_count;
  775. u32 export_count;
  776. spinlock_t delayed_cmd_lock;
  777. spinlock_t dev_reservation_lock;
  778. unsigned int dev_reservation_flags;
  779. #define DRF_SPC2_RESERVATIONS 0x00000001
  780. #define DRF_SPC2_RESERVATIONS_WITH_ISID 0x00000002
  781. spinlock_t se_port_lock;
  782. spinlock_t se_tmr_lock;
  783. spinlock_t qf_cmd_lock;
  784. struct semaphore caw_sem;
  785. /* Used for legacy SPC-2 reservations */
  786. struct se_session *reservation_holder;
  787. /* Used for ALUA Logical Unit Group membership */
  788. struct t10_alua_lu_gp_member *dev_alua_lu_gp_mem;
  789. /* Used for SPC-3 Persistent Reservations */
  790. struct t10_pr_registration *dev_pr_res_holder;
  791. struct list_head dev_sep_list;
  792. struct list_head dev_tmr_list;
  793. struct work_struct qf_work_queue;
  794. struct work_struct delayed_cmd_work;
  795. struct list_head delayed_cmd_list;
  796. struct list_head qf_cmd_list;
  797. /* Pointer to associated SE HBA */
  798. struct se_hba *se_hba;
  799. /* T10 Inquiry and VPD WWN Information */
  800. struct t10_wwn t10_wwn;
  801. /* T10 Asymmetric Logical Unit Assignment for Target Ports */
  802. struct t10_alua t10_alua;
  803. /* T10 SPC-2 + SPC-3 Reservations */
  804. struct t10_reservation t10_pr;
  805. struct se_dev_attrib dev_attrib;
  806. struct config_group dev_action_group;
  807. struct config_group dev_group;
  808. struct config_group dev_pr_group;
  809. struct se_dev_stat_grps dev_stat_grps;
  810. #define SE_DEV_ALIAS_LEN 512 /* must be less than PAGE_SIZE */
  811. unsigned char dev_alias[SE_DEV_ALIAS_LEN];
  812. #define SE_UDEV_PATH_LEN 512 /* must be less than PAGE_SIZE */
  813. unsigned char udev_path[SE_UDEV_PATH_LEN];
  814. /* Pointer to template of function pointers for transport */
  815. const struct target_backend_ops *transport;
  816. struct se_lun xcopy_lun;
  817. /* Protection Information */
  818. int prot_length;
  819. /* For se_lun->lun_se_dev RCU read-side critical access */
  820. u32 hba_index;
  821. struct rcu_head rcu_head;
  822. int queue_cnt;
  823. struct se_device_queue *queues;
  824. struct mutex lun_reset_mutex;
  825. };
  826. struct target_opcode_descriptor {
  827. u8 support:3;
  828. u8 serv_action_valid:1;
  829. u8 opcode;
  830. u16 service_action;
  831. u32 cdb_size;
  832. u8 specific_timeout;
  833. u16 nominal_timeout;
  834. u16 recommended_timeout;
  835. bool (*enabled)(const struct target_opcode_descriptor *descr,
  836. struct se_cmd *cmd);
  837. void (*update_usage_bits)(u8 *usage_bits,
  838. struct se_device *dev);
  839. u8 usage_bits[];
  840. };
  841. struct se_hba {
  842. u16 hba_tpgt;
  843. u32 hba_id;
  844. /* See hba_flags_table */
  845. u32 hba_flags;
  846. /* Virtual iSCSI devices attached. */
  847. u32 dev_count;
  848. u32 hba_index;
  849. /* Pointer to transport specific host structure. */
  850. void *hba_ptr;
  851. struct list_head hba_node;
  852. spinlock_t device_lock;
  853. struct config_group hba_group;
  854. struct mutex hba_access_mutex;
  855. struct target_backend *backend;
  856. };
  857. struct se_tpg_np {
  858. struct se_portal_group *tpg_np_parent;
  859. struct config_group tpg_np_group;
  860. };
  861. static inline struct se_tpg_np *to_tpg_np(struct config_item *item)
  862. {
  863. return container_of(to_config_group(item), struct se_tpg_np,
  864. tpg_np_group);
  865. }
  866. struct se_portal_group {
  867. /*
  868. * PROTOCOL IDENTIFIER value per SPC4, 7.5.1.
  869. *
  870. * Negative values can be used by fabric drivers for internal use TPGs.
  871. */
  872. int proto_id;
  873. bool enabled;
  874. /* RELATIVE TARGET PORT IDENTIFIER */
  875. u16 tpg_rtpi;
  876. bool rtpi_manual;
  877. /* Used for PR SPEC_I_PT=1 and REGISTER_AND_MOVE */
  878. atomic_t tpg_pr_ref_count;
  879. /* Spinlock for adding/removing ACLed Nodes */
  880. struct mutex acl_node_mutex;
  881. /* Spinlock for adding/removing sessions */
  882. spinlock_t session_lock;
  883. struct mutex tpg_lun_mutex;
  884. /* linked list for initiator ACL list */
  885. struct list_head acl_node_list;
  886. struct hlist_head tpg_lun_hlist;
  887. struct se_lun *tpg_virt_lun0;
  888. /* List of TCM sessions associated wth this TPG */
  889. struct list_head tpg_sess_list;
  890. /* Pointer to $FABRIC_MOD dependent code */
  891. const struct target_core_fabric_ops *se_tpg_tfo;
  892. struct se_wwn *se_tpg_wwn;
  893. struct config_group tpg_group;
  894. struct config_group tpg_lun_group;
  895. struct config_group tpg_np_group;
  896. struct config_group tpg_acl_group;
  897. struct config_group tpg_attrib_group;
  898. struct config_group tpg_auth_group;
  899. struct config_group tpg_param_group;
  900. };
  901. static inline struct se_portal_group *to_tpg(struct config_item *item)
  902. {
  903. return container_of(to_config_group(item), struct se_portal_group,
  904. tpg_group);
  905. }
  906. static inline struct se_portal_group *attrib_to_tpg(struct config_item *item)
  907. {
  908. return container_of(to_config_group(item), struct se_portal_group,
  909. tpg_attrib_group);
  910. }
  911. static inline struct se_portal_group *auth_to_tpg(struct config_item *item)
  912. {
  913. return container_of(to_config_group(item), struct se_portal_group,
  914. tpg_auth_group);
  915. }
  916. static inline struct se_portal_group *param_to_tpg(struct config_item *item)
  917. {
  918. return container_of(to_config_group(item), struct se_portal_group,
  919. tpg_param_group);
  920. }
  921. enum {
  922. /* Use se_cmd's cpuid for completion */
  923. SE_COMPL_AFFINITY_CPUID = -1,
  924. /* Complete on current CPU */
  925. SE_COMPL_AFFINITY_CURR_CPU = -2,
  926. };
  927. struct se_wwn {
  928. struct target_fabric_configfs *wwn_tf;
  929. void *priv;
  930. struct config_group wwn_group;
  931. struct config_group fabric_stat_group;
  932. struct config_group param_group;
  933. int cmd_compl_affinity;
  934. };
  935. static inline void atomic_inc_mb(atomic_t *v)
  936. {
  937. smp_mb__before_atomic();
  938. atomic_inc(v);
  939. smp_mb__after_atomic();
  940. }
  941. static inline void atomic_dec_mb(atomic_t *v)
  942. {
  943. smp_mb__before_atomic();
  944. atomic_dec(v);
  945. smp_mb__after_atomic();
  946. }
  947. static inline void target_free_tag(struct se_session *sess, struct se_cmd *cmd)
  948. {
  949. sbitmap_queue_clear(&sess->sess_tag_pool, cmd->map_tag, cmd->map_cpu);
  950. }
  951. #endif /* TARGET_CORE_BASE_H */