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