1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  *  Copyright (C) 2004 - 2010 Vladislav Bolkhovitin <vst@vlnb.net>
4  *  Copyright (C) 2004 - 2005 Leonid Stoljar
5  *  Copyright (C) 2006 Nathaniel Clark <nate@misrule.us>
6  *  Copyright (C) 2007 - 2010 ID7 Ltd.
7  *
8  *  Forward port and refactoring to modern qla2xxx and target/configfs
9  *
10  *  Copyright (C) 2010-2011 Nicholas A. Bellinger <nab@kernel.org>
11  *
12  *  Additional file for the target driver support.
13  */
14 /*
15  * This is the global def file that is useful for including from the
16  * target portion.
17  */
18 
19 #ifndef __QLA_TARGET_H
20 #define __QLA_TARGET_H
21 
22 #include "qla_def.h"
23 #include "qla_dsd.h"
24 
25 /*
26  * Must be changed on any change in any initiator visible interfaces or
27  * data in the target add-on
28  */
29 #define QLA2XXX_TARGET_MAGIC	269
30 
31 /*
32  * Must be changed on any change in any target visible interfaces or
33  * data in the initiator
34  */
35 #define QLA2XXX_INITIATOR_MAGIC   57222
36 
37 #define QLA2XXX_INI_MODE_STR_EXCLUSIVE	"exclusive"
38 #define QLA2XXX_INI_MODE_STR_DISABLED	"disabled"
39 #define QLA2XXX_INI_MODE_STR_ENABLED	"enabled"
40 #define QLA2XXX_INI_MODE_STR_DUAL		"dual"
41 
42 #define QLA2XXX_INI_MODE_EXCLUSIVE	0
43 #define QLA2XXX_INI_MODE_DISABLED	1
44 #define QLA2XXX_INI_MODE_ENABLED	2
45 #define QLA2XXX_INI_MODE_DUAL	3
46 
47 #define QLA2XXX_COMMAND_COUNT_INIT	250
48 #define QLA2XXX_IMMED_NOTIFY_COUNT_INIT 250
49 
50 /*
51  * Used to mark which completion handles (for RIO Status's) are for CTIO's
52  * vs. regular (non-target) info. This is checked for in
53  * qla2x00_process_response_queue() to see if a handle coming back in a
54  * multi-complete should come to the tgt driver or be handled there by qla2xxx
55  */
56 #define CTIO_COMPLETION_HANDLE_MARK	BIT_29
57 #if (CTIO_COMPLETION_HANDLE_MARK <= DEFAULT_OUTSTANDING_COMMANDS)
58 #error "CTIO_COMPLETION_HANDLE_MARK not larger than "
59 	"DEFAULT_OUTSTANDING_COMMANDS"
60 #endif
61 #define HANDLE_IS_CTIO_COMP(h) (h & CTIO_COMPLETION_HANDLE_MARK)
62 
63 /* Used to mark CTIO as intermediate */
64 #define CTIO_INTERMEDIATE_HANDLE_MARK	BIT_30
65 #define QLA_TGT_NULL_HANDLE	0
66 
67 #define QLA_TGT_HANDLE_MASK  0xF0000000
68 #define QLA_QPID_HANDLE_MASK 0x00FF0000 /* qpair id mask */
69 #define QLA_CMD_HANDLE_MASK  0x0000FFFF
70 #define QLA_TGT_SKIP_HANDLE	(0xFFFFFFFF & ~QLA_TGT_HANDLE_MASK)
71 
72 #define QLA_QPID_HANDLE_SHIFT 16
73 #define GET_QID(_h) ((_h & QLA_QPID_HANDLE_MASK) >> QLA_QPID_HANDLE_SHIFT)
74 
75 
76 #ifndef OF_SS_MODE_0
77 /*
78  * ISP target entries - Flags bit definitions.
79  */
80 #define OF_SS_MODE_0        0
81 #define OF_SS_MODE_1        1
82 #define OF_SS_MODE_2        2
83 #define OF_SS_MODE_3        3
84 
85 #define OF_EXPL_CONF        BIT_5       /* Explicit Confirmation Requested */
86 #define OF_DATA_IN          BIT_6       /* Data in to initiator */
87 					/*  (data from target to initiator) */
88 #define OF_DATA_OUT         BIT_7       /* Data out from initiator */
89 					/*  (data from initiator to target) */
90 #define OF_NO_DATA          (BIT_7 | BIT_6)
91 #define OF_INC_RC           BIT_8       /* Increment command resource count */
92 #define OF_FAST_POST        BIT_9       /* Enable mailbox fast posting. */
93 #define OF_CONF_REQ         BIT_13      /* Confirmation Requested */
94 #define OF_TERM_EXCH        BIT_14      /* Terminate exchange */
95 #define OF_SSTS             BIT_15      /* Send SCSI status */
96 #endif
97 
98 #ifndef QLA_TGT_DATASEGS_PER_CMD32
99 #define QLA_TGT_DATASEGS_PER_CMD32	3
100 #define QLA_TGT_DATASEGS_PER_CONT32	7
101 #define QLA_TGT_MAX_SG32(ql) \
102 	(((ql) > 0) ? (QLA_TGT_DATASEGS_PER_CMD32 + \
103 		QLA_TGT_DATASEGS_PER_CONT32*((ql) - 1)) : 0)
104 
105 #define QLA_TGT_DATASEGS_PER_CMD64	2
106 #define QLA_TGT_DATASEGS_PER_CONT64	5
107 #define QLA_TGT_MAX_SG64(ql) \
108 	(((ql) > 0) ? (QLA_TGT_DATASEGS_PER_CMD64 + \
109 		QLA_TGT_DATASEGS_PER_CONT64*((ql) - 1)) : 0)
110 #endif
111 
112 #ifndef QLA_TGT_DATASEGS_PER_CMD_24XX
113 #define QLA_TGT_DATASEGS_PER_CMD_24XX	1
114 #define QLA_TGT_DATASEGS_PER_CONT_24XX	5
115 #define QLA_TGT_MAX_SG_24XX(ql) \
116 	(min(1270, ((ql) > 0) ? (QLA_TGT_DATASEGS_PER_CMD_24XX + \
117 		QLA_TGT_DATASEGS_PER_CONT_24XX*((ql) - 1)) : 0))
118 #endif
119 
120 #define GET_TARGET_ID(ha, iocb) ((HAS_EXTENDED_IDS(ha))			\
121 			 ? le16_to_cpu((iocb)->u.isp2x.target.extended)	\
122 			 : (uint16_t)(iocb)->u.isp2x.target.id.standard)
123 
124 #ifndef NOTIFY_ACK_TYPE
125 #define NOTIFY_ACK_TYPE 0x0E	  /* Notify acknowledge entry. */
126 /*
127  * ISP queue -	notify acknowledge entry structure definition.
128  *		This is sent to the ISP from the target driver.
129  */
130 struct nack_to_isp {
131 	uint8_t	 entry_type;		    /* Entry type. */
132 	uint8_t	 entry_count;		    /* Entry count. */
133 	uint8_t	 sys_define;		    /* System defined. */
134 	uint8_t	 entry_status;		    /* Entry Status. */
135 	union {
136 		struct {
137 			__le32	sys_define_2; /* System defined. */
138 			target_id_t target;
139 			uint8_t	 target_id;
140 			uint8_t	 reserved_1;
141 			__le16	flags;
142 			__le16	resp_code;
143 			__le16	status;
144 			__le16	task_flags;
145 			__le16	seq_id;
146 			__le16	srr_rx_id;
147 			__le32	srr_rel_offs;
148 			__le16	srr_ui;
149 			__le16	srr_flags;
150 			__le16	srr_reject_code;
151 			uint8_t  srr_reject_vendor_uniq;
152 			uint8_t  srr_reject_code_expl;
153 			uint8_t  reserved_2[24];
154 		} isp2x;
155 		struct {
156 			uint32_t handle;
157 			__le16	nport_handle;
158 			uint16_t reserved_1;
159 			__le16	flags;
160 			__le16	srr_rx_id;
161 			__le16	status;
162 			uint8_t  status_subcode;
163 			uint8_t  fw_handle;
164 			__le32	exchange_address;
165 			__le32	srr_rel_offs;
166 			__le16	srr_ui;
167 			__le16	srr_flags;
168 			uint8_t  reserved_4[19];
169 			uint8_t  vp_index;
170 			uint8_t  srr_reject_vendor_uniq;
171 			uint8_t  srr_reject_code_expl;
172 			uint8_t  srr_reject_code;
173 			uint8_t  reserved_5[5];
174 		} isp24;
175 	} u;
176 	uint8_t  reserved[2];
177 	__le16	ox_id;
178 } __packed;
179 #define NOTIFY_ACK_FLAGS_TERMINATE	BIT_3
180 #define NOTIFY_ACK_SRR_FLAGS_ACCEPT	0
181 #define NOTIFY_ACK_SRR_FLAGS_REJECT	1
182 
183 #define NOTIFY_ACK_SRR_REJECT_REASON_UNABLE_TO_PERFORM	0x9
184 
185 #define NOTIFY_ACK_SRR_FLAGS_REJECT_EXPL_NO_EXPL		0
186 #define NOTIFY_ACK_SRR_FLAGS_REJECT_EXPL_UNABLE_TO_SUPPLY_DATA	0x2a
187 
188 #define NOTIFY_ACK_SUCCESS      0x01
189 #endif
190 
191 #ifndef ACCEPT_TGT_IO_TYPE
192 #define ACCEPT_TGT_IO_TYPE 0x16 /* Accept target I/O entry. */
193 #endif
194 
195 #ifndef CONTINUE_TGT_IO_TYPE
196 #define CONTINUE_TGT_IO_TYPE 0x17
197 /*
198  * ISP queue -	Continue Target I/O (CTIO) entry for status mode 0 structure.
199  *		This structure is sent to the ISP 2xxx from target driver.
200  */
201 struct ctio_to_2xxx {
202 	uint8_t	 entry_type;		/* Entry type. */
203 	uint8_t	 entry_count;		/* Entry count. */
204 	uint8_t	 sys_define;		/* System defined. */
205 	uint8_t	 entry_status;		/* Entry Status. */
206 	uint32_t handle;		/* System defined handle */
207 	target_id_t target;
208 	__le16	rx_id;
209 	__le16	flags;
210 	__le16	status;
211 	__le16	timeout;		/* 0 = 30 seconds, 0xFFFF = disable */
212 	__le16	dseg_count;		/* Data segment count. */
213 	__le32	relative_offset;
214 	__le32	residual;
215 	__le16	reserved_1[3];
216 	__le16	scsi_status;
217 	__le32	transfer_length;
218 	struct dsd32 dsd[3];
219 } __packed;
220 #define ATIO_PATH_INVALID       0x07
221 #define ATIO_CANT_PROV_CAP      0x16
222 #define ATIO_CDB_VALID          0x3D
223 
224 #define ATIO_EXEC_READ          BIT_1
225 #define ATIO_EXEC_WRITE         BIT_0
226 #endif
227 
228 #ifndef CTIO_A64_TYPE
229 #define CTIO_A64_TYPE 0x1F
230 #define CTIO_SUCCESS			0x01
231 #define CTIO_ABORTED			0x02
232 #define CTIO_INVALID_RX_ID		0x08
233 #define CTIO_TIMEOUT			0x0B
234 #define CTIO_DIF_ERROR			0x0C     /* DIF error detected  */
235 #define CTIO_LIP_RESET			0x0E
236 #define CTIO_TARGET_RESET		0x17
237 #define CTIO_PORT_UNAVAILABLE		0x28
238 #define CTIO_PORT_LOGGED_OUT		0x29
239 #define CTIO_PORT_CONF_CHANGED		0x2A
240 #define CTIO_SRR_RECEIVED		0x45
241 #endif
242 
243 #ifndef CTIO_RET_TYPE
244 #define CTIO_RET_TYPE	0x17		/* CTIO return entry */
245 #define ATIO_TYPE7 0x06 /* Accept target I/O entry for 24xx */
246 #endif
247 
248 struct fcp_hdr {
249 	uint8_t  r_ctl;
250 	be_id_t  d_id;
251 	uint8_t  cs_ctl;
252 	be_id_t  s_id;
253 	uint8_t  type;
254 	uint8_t  f_ctl[3];
255 	uint8_t  seq_id;
256 	uint8_t  df_ctl;
257 	uint16_t seq_cnt;
258 	__be16   ox_id;
259 	uint16_t rx_id;
260 	__le32	parameter;
261 };
262 
263 struct fcp_hdr_le {
264 	le_id_t  d_id;
265 	uint8_t  r_ctl;
266 	le_id_t  s_id;
267 	uint8_t  cs_ctl;
268 	uint8_t  f_ctl[3];
269 	uint8_t  type;
270 	__le16	seq_cnt;
271 	uint8_t  df_ctl;
272 	uint8_t  seq_id;
273 	__le16	rx_id;
274 	__le16	ox_id;
275 	__le32	parameter;
276 };
277 
278 #define F_CTL_EXCH_CONTEXT_RESP	BIT_23
279 #define F_CTL_SEQ_CONTEXT_RESIP	BIT_22
280 #define F_CTL_LAST_SEQ		BIT_20
281 #define F_CTL_END_SEQ		BIT_19
282 #define F_CTL_SEQ_INITIATIVE	BIT_16
283 
284 #define R_CTL_BASIC_LINK_SERV	0x80
285 #define R_CTL_B_ACC		0x4
286 #define R_CTL_B_RJT		0x5
287 
288 struct atio7_fcp_cmnd {
289 	uint64_t lun;
290 	uint8_t  cmnd_ref;
291 	uint8_t  task_attr:3;
292 	uint8_t  reserved:5;
293 	uint8_t  task_mgmt_flags;
294 #define FCP_CMND_TASK_MGMT_CLEAR_ACA		6
295 #define FCP_CMND_TASK_MGMT_TARGET_RESET		5
296 #define FCP_CMND_TASK_MGMT_LU_RESET		4
297 #define FCP_CMND_TASK_MGMT_CLEAR_TASK_SET	2
298 #define FCP_CMND_TASK_MGMT_ABORT_TASK_SET	1
299 	uint8_t  wrdata:1;
300 	uint8_t  rddata:1;
301 	uint8_t  add_cdb_len:6;
302 	uint8_t  cdb[16];
303 	/*
304 	 * add_cdb is optional and can absent from struct atio7_fcp_cmnd. Size 4
305 	 * only to make sizeof(struct atio7_fcp_cmnd) be as expected by
306 	 * BUILD_BUG_ON in qlt_init().
307 	 */
308 	uint8_t  add_cdb[4];
309 	/* __le32	data_length; */
310 } __packed;
311 
312 /*
313  * ISP queue -	Accept Target I/O (ATIO) type entry IOCB structure.
314  *		This is sent from the ISP to the target driver.
315  */
316 struct atio_from_isp {
317 	union {
318 		struct {
319 			__le16	entry_hdr;
320 			uint8_t  sys_define;   /* System defined. */
321 			uint8_t  entry_status; /* Entry Status.   */
322 			__le32	sys_define_2; /* System defined. */
323 			target_id_t target;
324 			__le16	rx_id;
325 			__le16	flags;
326 			__le16	status;
327 			uint8_t  command_ref;
328 			uint8_t  task_codes;
329 			uint8_t  task_flags;
330 			uint8_t  execution_codes;
331 			uint8_t  cdb[MAX_CMDSZ];
332 			__le32	data_length;
333 			__le16	lun;
334 			uint8_t  initiator_port_name[WWN_SIZE]; /* on qla23xx */
335 			__le16	reserved_32[6];
336 			__le16	ox_id;
337 		} isp2x;
338 		struct {
339 			__le16	entry_hdr;
340 			uint8_t  fcp_cmnd_len_low;
341 			uint8_t  fcp_cmnd_len_high:4;
342 			uint8_t  attr:4;
343 			__le32	exchange_addr;
344 #define ATIO_EXCHANGE_ADDRESS_UNKNOWN	0xFFFFFFFF
345 			struct fcp_hdr fcp_hdr;
346 			struct atio7_fcp_cmnd fcp_cmnd;
347 		} isp24;
348 		struct {
349 			uint8_t  entry_type;	/* Entry type. */
350 			uint8_t  entry_count;	/* Entry count. */
351 			__le16	 attr_n_length;
352 #define FCP_CMD_LENGTH_MASK 0x0fff
353 #define FCP_CMD_LENGTH_MIN  0x38
354 			uint8_t  data[56];
355 			__le32	signature;
356 #define ATIO_PROCESSED 0xDEADDEAD		/* Signature */
357 		} raw;
358 	} u;
359 } __packed;
360 
361 static inline int fcpcmd_is_corrupted(struct atio *atio)
362 {
363 	if (atio->entry_type == ATIO_TYPE7 &&
364 	    ((le16_to_cpu(atio->attr_n_length) & FCP_CMD_LENGTH_MASK) <
365 	     FCP_CMD_LENGTH_MIN))
366 		return 1;
367 	else
368 		return 0;
369 }
370 
371 /* adjust corrupted atio so we won't trip over the same entry again. */
372 static inline void adjust_corrupted_atio(struct atio_from_isp *atio)
373 {
374 	atio->u.raw.attr_n_length = cpu_to_le16(FCP_CMD_LENGTH_MIN);
375 	atio->u.isp24.fcp_cmnd.add_cdb_len = 0;
376 }
377 
378 static inline int get_datalen_for_atio(struct atio_from_isp *atio)
379 {
380 	int len = atio->u.isp24.fcp_cmnd.add_cdb_len;
381 
382 	return get_unaligned_be32(&atio->u.isp24.fcp_cmnd.add_cdb[len * 4]);
383 }
384 
385 #define CTIO_TYPE7 0x12 /* Continue target I/O entry (for 24xx) */
386 
387 /*
388  * ISP queue -	Continue Target I/O (ATIO) type 7 entry (for 24xx) structure.
389  *		This structure is sent to the ISP 24xx from the target driver.
390  */
391 
392 struct ctio7_to_24xx {
393 	uint8_t	 entry_type;		    /* Entry type. */
394 	uint8_t	 entry_count;		    /* Entry count. */
395 	uint8_t	 sys_define;		    /* System defined. */
396 	uint8_t	 entry_status;		    /* Entry Status. */
397 	uint32_t handle;		    /* System defined handle */
398 	__le16	nport_handle;
399 #define CTIO7_NHANDLE_UNRECOGNIZED	0xFFFF
400 	__le16	timeout;
401 	__le16	dseg_count;		    /* Data segment count. */
402 	uint8_t  vp_index;
403 	uint8_t  add_flags;
404 	le_id_t  initiator_id;
405 	uint8_t  reserved;
406 	__le32	exchange_addr;
407 	union {
408 		struct {
409 			__le16	reserved1;
410 			__le16 flags;
411 			__le32	residual;
412 			__le16 ox_id;
413 			__le16	scsi_status;
414 			__le32	relative_offset;
415 			__le32	reserved2;
416 			__le32	transfer_length;
417 			__le32	reserved3;
418 			struct dsd64 dsd;
419 		} status0;
420 		struct {
421 			__le16	sense_length;
422 			__le16 flags;
423 			__le32	residual;
424 			__le16 ox_id;
425 			__le16	scsi_status;
426 			__le16	response_len;
427 			__le16	reserved;
428 			uint8_t sense_data[24];
429 		} status1;
430 	} u;
431 } __packed;
432 
433 /*
434  * ISP queue - CTIO type 7 from ISP 24xx to target driver
435  * returned entry structure.
436  */
437 struct ctio7_from_24xx {
438 	uint8_t	 entry_type;		    /* Entry type. */
439 	uint8_t	 entry_count;		    /* Entry count. */
440 	uint8_t	 sys_define;		    /* System defined. */
441 	uint8_t	 entry_status;		    /* Entry Status. */
442 	uint32_t handle;		    /* System defined handle */
443 	__le16	status;
444 	__le16	timeout;
445 	__le16	dseg_count;		    /* Data segment count. */
446 	uint8_t  vp_index;
447 	uint8_t  reserved1[5];
448 	__le32	exchange_address;
449 	__le16	reserved2;
450 	__le16	flags;
451 	__le32	residual;
452 	__le16	ox_id;
453 	__le16	reserved3;
454 	__le32	relative_offset;
455 	uint8_t  reserved4[24];
456 } __packed;
457 
458 /* CTIO7 flags values */
459 #define CTIO7_FLAGS_SEND_STATUS		BIT_15
460 #define CTIO7_FLAGS_TERMINATE		BIT_14
461 #define CTIO7_FLAGS_CONFORM_REQ		BIT_13
462 #define CTIO7_FLAGS_DONT_RET_CTIO	BIT_8
463 #define CTIO7_FLAGS_STATUS_MODE_0	0
464 #define CTIO7_FLAGS_STATUS_MODE_1	BIT_6
465 #define CTIO7_FLAGS_STATUS_MODE_2	BIT_7
466 #define CTIO7_FLAGS_EXPLICIT_CONFORM	BIT_5
467 #define CTIO7_FLAGS_CONFIRM_SATISF	BIT_4
468 #define CTIO7_FLAGS_DSD_PTR		BIT_2
469 #define CTIO7_FLAGS_DATA_IN		BIT_1 /* data to initiator */
470 #define CTIO7_FLAGS_DATA_OUT		BIT_0 /* data from initiator */
471 
472 #define ELS_PLOGI			0x3
473 #define ELS_FLOGI			0x4
474 #define ELS_LOGO			0x5
475 #define ELS_PRLI			0x20
476 #define ELS_PRLO			0x21
477 #define ELS_TPRLO			0x24
478 #define ELS_PDISC			0x50
479 #define ELS_ADISC			0x52
480 
481 /*
482  *CTIO Type CRC_2 IOCB
483  */
484 struct ctio_crc2_to_fw {
485 	uint8_t entry_type;		/* Entry type. */
486 #define CTIO_CRC2 0x7A
487 	uint8_t entry_count;		/* Entry count. */
488 	uint8_t sys_define;		/* System defined. */
489 	uint8_t entry_status;		/* Entry Status. */
490 
491 	uint32_t handle;		/* System handle. */
492 	__le16	nport_handle;		/* N_PORT handle. */
493 	__le16 timeout;		/* Command timeout. */
494 
495 	__le16	dseg_count;		/* Data segment count. */
496 	uint8_t  vp_index;
497 	uint8_t  add_flags;		/* additional flags */
498 #define CTIO_CRC2_AF_DIF_DSD_ENA BIT_3
499 
500 	le_id_t  initiator_id;		/* initiator ID */
501 	uint8_t  reserved1;
502 	__le32	exchange_addr;		/* rcv exchange address */
503 	__le16	reserved2;
504 	__le16 flags;			/* refer to CTIO7 flags values */
505 	__le32	residual;
506 	__le16 ox_id;
507 	__le16	scsi_status;
508 	__le32 relative_offset;
509 	__le32	reserved5;
510 	__le32 transfer_length;		/* total fc transfer length */
511 	__le32	reserved6;
512 	__le64	 crc_context_address __packed; /* Data segment address. */
513 	__le16	crc_context_len;	/* Data segment length. */
514 	__le16	reserved_1;		/* MUST be set to 0. */
515 };
516 
517 /* CTIO Type CRC_x Status IOCB */
518 struct ctio_crc_from_fw {
519 	uint8_t entry_type;		/* Entry type. */
520 	uint8_t entry_count;		/* Entry count. */
521 	uint8_t sys_define;		/* System defined. */
522 	uint8_t entry_status;		/* Entry Status. */
523 
524 	uint32_t handle;		/* System handle. */
525 	__le16	status;
526 	__le16	timeout;		/* Command timeout. */
527 	__le16	dseg_count;		/* Data segment count. */
528 	__le32	reserved1;
529 	__le16	state_flags;
530 #define CTIO_CRC_SF_DIF_CHOPPED BIT_4
531 
532 	__le32	exchange_address;	/* rcv exchange address */
533 	__le16	reserved2;
534 	__le16	flags;
535 	__le32	resid_xfer_length;
536 	__le16	ox_id;
537 	uint8_t  reserved3[12];
538 	__le16	runt_guard;		/* reported runt blk guard */
539 	uint8_t  actual_dif[8];
540 	uint8_t  expected_dif[8];
541 } __packed;
542 
543 /*
544  * ISP queue - ABTS received/response entries structure definition for 24xx.
545  */
546 #define ABTS_RECV_24XX		0x54 /* ABTS received (for 24xx) */
547 #define ABTS_RESP_24XX		0x55 /* ABTS responce (for 24xx) */
548 
549 /*
550  * ISP queue -	ABTS received IOCB entry structure definition for 24xx.
551  *		The ABTS BLS received from the wire is sent to the
552  *		target driver by the ISP 24xx.
553  *		The IOCB is placed on the response queue.
554  */
555 struct abts_recv_from_24xx {
556 	uint8_t	 entry_type;		    /* Entry type. */
557 	uint8_t	 entry_count;		    /* Entry count. */
558 	uint8_t	 sys_define;		    /* System defined. */
559 	uint8_t	 entry_status;		    /* Entry Status. */
560 	uint8_t  reserved_1[6];
561 	__le16	nport_handle;
562 	uint8_t  reserved_2[2];
563 	uint8_t  vp_index;
564 	uint8_t  reserved_3:4;
565 	uint8_t  sof_type:4;
566 	__le32	exchange_address;
567 	struct fcp_hdr_le fcp_hdr_le;
568 	uint8_t  reserved_4[16];
569 	__le32	exchange_addr_to_abort;
570 } __packed;
571 
572 #define ABTS_PARAM_ABORT_SEQ		BIT_0
573 
574 struct ba_acc_le {
575 	__le16	reserved;
576 	uint8_t  seq_id_last;
577 	uint8_t  seq_id_valid;
578 #define SEQ_ID_VALID	0x80
579 #define SEQ_ID_INVALID	0x00
580 	__le16	rx_id;
581 	__le16	ox_id;
582 	__le16	high_seq_cnt;
583 	__le16	low_seq_cnt;
584 } __packed;
585 
586 struct ba_rjt_le {
587 	uint8_t vendor_uniq;
588 	uint8_t reason_expl;
589 	uint8_t reason_code;
590 #define BA_RJT_REASON_CODE_INVALID_COMMAND	0x1
591 #define BA_RJT_REASON_CODE_UNABLE_TO_PERFORM	0x9
592 	uint8_t reserved;
593 } __packed;
594 
595 /*
596  * ISP queue -	ABTS Response IOCB entry structure definition for 24xx.
597  *		The ABTS response to the ABTS received is sent by the
598  *		target driver to the ISP 24xx.
599  *		The IOCB is placed on the request queue.
600  */
601 struct abts_resp_to_24xx {
602 	uint8_t	 entry_type;		    /* Entry type. */
603 	uint8_t	 entry_count;		    /* Entry count. */
604 	uint8_t	 sys_define;		    /* System defined. */
605 	uint8_t	 entry_status;		    /* Entry Status. */
606 	uint32_t handle;
607 	__le16	reserved_1;
608 	__le16	nport_handle;
609 	__le16	control_flags;
610 #define ABTS_CONTR_FLG_TERM_EXCHG	BIT_0
611 	uint8_t  vp_index;
612 	uint8_t  reserved_3:4;
613 	uint8_t  sof_type:4;
614 	__le32	exchange_address;
615 	struct fcp_hdr_le fcp_hdr_le;
616 	union {
617 		struct ba_acc_le ba_acct;
618 		struct ba_rjt_le ba_rjt;
619 	} __packed payload;
620 	__le32	reserved_4;
621 	__le32	exchange_addr_to_abort;
622 } __packed;
623 
624 /*
625  * ISP queue -	ABTS Response IOCB from ISP24xx Firmware entry structure.
626  *		The ABTS response with completion status to the ABTS response
627  *		(sent by the target driver to the ISP 24xx) is sent by the
628  *		ISP24xx firmware to the target driver.
629  *		The IOCB is placed on the response queue.
630  */
631 struct abts_resp_from_24xx_fw {
632 	uint8_t	 entry_type;		    /* Entry type. */
633 	uint8_t	 entry_count;		    /* Entry count. */
634 	uint8_t	 sys_define;		    /* System defined. */
635 	uint8_t	 entry_status;		    /* Entry Status. */
636 	uint32_t handle;
637 	__le16	compl_status;
638 #define ABTS_RESP_COMPL_SUCCESS		0
639 #define ABTS_RESP_COMPL_SUBCODE_ERROR	0x31
640 	__le16	nport_handle;
641 	__le16	reserved_1;
642 	uint8_t  reserved_2;
643 	uint8_t  reserved_3:4;
644 	uint8_t  sof_type:4;
645 	__le32	exchange_address;
646 	struct fcp_hdr_le fcp_hdr_le;
647 	uint8_t reserved_4[8];
648 	__le32	error_subcode1;
649 #define ABTS_RESP_SUBCODE_ERR_ABORTED_EXCH_NOT_TERM	0x1E
650 	__le32	error_subcode2;
651 	__le32	exchange_addr_to_abort;
652 } __packed;
653 
654 /********************************************************************\
655  * Type Definitions used by initiator & target halves
656 \********************************************************************/
657 
658 struct qla_tgt_mgmt_cmd;
659 struct fc_port;
660 struct qla_tgt_cmd;
661 
662 /*
663  * This structure provides a template of function calls that the
664  * target driver (from within qla_target.c) can issue to the
665  * target module (tcm_qla2xxx).
666  */
667 struct qla_tgt_func_tmpl {
668 	struct qla_tgt_cmd *(*find_cmd_by_tag)(struct fc_port *, uint64_t);
669 	int (*handle_cmd)(struct scsi_qla_host *, struct qla_tgt_cmd *,
670 			unsigned char *, uint32_t, int, int, int);
671 	void (*handle_data)(struct qla_tgt_cmd *);
672 	int (*handle_tmr)(struct qla_tgt_mgmt_cmd *, u64, uint16_t,
673 			uint32_t);
674 	struct qla_tgt_cmd *(*get_cmd)(struct fc_port *);
675 	void (*rel_cmd)(struct qla_tgt_cmd *);
676 	void (*free_cmd)(struct qla_tgt_cmd *);
677 	void (*free_mcmd)(struct qla_tgt_mgmt_cmd *);
678 	void (*free_session)(struct fc_port *);
679 
680 	int (*check_initiator_node_acl)(struct scsi_qla_host *, unsigned char *,
681 					struct fc_port *);
682 	void (*update_sess)(struct fc_port *, port_id_t, uint16_t, bool);
683 	struct fc_port *(*find_sess_by_loop_id)(struct scsi_qla_host *,
684 						const uint16_t);
685 	struct fc_port *(*find_sess_by_s_id)(struct scsi_qla_host *,
686 					     const be_id_t);
687 	void (*clear_nacl_from_fcport_map)(struct fc_port *);
688 	void (*put_sess)(struct fc_port *);
689 	void (*shutdown_sess)(struct fc_port *);
690 	int (*get_dif_tags)(struct qla_tgt_cmd *cmd, uint16_t *pfw_prot_opts);
691 	int (*chk_dif_tags)(uint32_t tag);
692 	void (*add_target)(struct scsi_qla_host *);
693 	void (*remove_target)(struct scsi_qla_host *);
694 };
695 
696 int qla2x00_wait_for_hba_online(struct scsi_qla_host *);
697 
698 #include <target/target_core_base.h>
699 
700 #define QLA_TGT_TIMEOUT			10	/* in seconds */
701 
702 #define QLA_TGT_MAX_HW_PENDING_TIME	60 /* in seconds */
703 
704 /* Immediate notify status constants */
705 #define IMM_NTFY_LIP_RESET          0x000E
706 #define IMM_NTFY_LIP_LINK_REINIT    0x000F
707 #define IMM_NTFY_IOCB_OVERFLOW      0x0016
708 #define IMM_NTFY_ABORT_TASK         0x0020
709 #define IMM_NTFY_PORT_LOGOUT        0x0029
710 #define IMM_NTFY_PORT_CONFIG        0x002A
711 #define IMM_NTFY_GLBL_TPRLO         0x002D
712 #define IMM_NTFY_GLBL_LOGO          0x002E
713 #define IMM_NTFY_RESOURCE           0x0034
714 #define IMM_NTFY_MSG_RX             0x0036
715 #define IMM_NTFY_SRR                0x0045
716 #define IMM_NTFY_ELS                0x0046
717 
718 /* Immediate notify task flags */
719 #define IMM_NTFY_TASK_MGMT_SHIFT    8
720 
721 #define QLA_TGT_CLEAR_ACA               0x40
722 #define QLA_TGT_TARGET_RESET            0x20
723 #define QLA_TGT_LUN_RESET               0x10
724 #define QLA_TGT_CLEAR_TS                0x04
725 #define QLA_TGT_ABORT_TS                0x02
726 #define QLA_TGT_ABORT_ALL_SESS          0xFFFF
727 #define QLA_TGT_ABORT_ALL               0xFFFE
728 #define QLA_TGT_NEXUS_LOSS_SESS         0xFFFD
729 #define QLA_TGT_NEXUS_LOSS              0xFFFC
730 #define QLA_TGT_ABTS			0xFFFB
731 #define QLA_TGT_2G_ABORT_TASK		0xFFFA
732 
733 /* Notify Acknowledge flags */
734 #define NOTIFY_ACK_RES_COUNT        BIT_8
735 #define NOTIFY_ACK_CLEAR_LIP_RESET  BIT_5
736 #define NOTIFY_ACK_TM_RESP_CODE_VALID BIT_4
737 
738 /* Command's states */
739 #define QLA_TGT_STATE_NEW		0 /* New command + target processing */
740 #define QLA_TGT_STATE_NEED_DATA		1 /* target needs data to continue */
741 #define QLA_TGT_STATE_DATA_IN		2 /* Data arrived + target processing */
742 #define QLA_TGT_STATE_PROCESSED		3 /* target done processing */
743 
744 /* ATIO task_codes field */
745 #define ATIO_SIMPLE_QUEUE           0
746 #define ATIO_HEAD_OF_QUEUE          1
747 #define ATIO_ORDERED_QUEUE          2
748 #define ATIO_ACA_QUEUE              4
749 #define ATIO_UNTAGGED               5
750 
751 /* TM failed response codes, see FCP (9.4.11 FCP_RSP_INFO) */
752 #define	FC_TM_SUCCESS               0
753 #define	FC_TM_BAD_FCP_DATA          1
754 #define	FC_TM_BAD_CMD               2
755 #define	FC_TM_FCP_DATA_MISMATCH     3
756 #define	FC_TM_REJECT                4
757 #define FC_TM_FAILED                5
758 
759 #define QLA_TGT_SENSE_VALID(sense)  ((sense != NULL) && \
760 				(((const uint8_t *)(sense))[0] & 0x70) == 0x70)
761 
762 struct qla_port_24xx_data {
763 	uint8_t port_name[WWN_SIZE];
764 	uint16_t loop_id;
765 	uint16_t reserved;
766 };
767 
768 struct qla_qpair_hint {
769 	struct list_head hint_elem;
770 	struct qla_qpair *qpair;
771 	u16 cpuid;
772 	uint8_t cmd_cnt;
773 };
774 
775 struct qla_tgt {
776 	struct scsi_qla_host *vha;
777 	struct qla_hw_data *ha;
778 	struct btree_head64 lun_qpair_map;
779 	struct qla_qpair_hint *qphints;
780 	/*
781 	 * To sync between IRQ handlers and qlt_target_release(). Needed,
782 	 * because req_pkt() can drop/reaquire HW lock inside. Protected by
783 	 * HW lock.
784 	 */
785 	int atio_irq_cmd_count;
786 
787 	int sg_tablesize;
788 
789 	/* Target's flags, serialized by pha->hardware_lock */
790 	unsigned int link_reinit_iocb_pending:1;
791 
792 	/*
793 	 * Protected by tgt_mutex AND hardware_lock for writing and tgt_mutex
794 	 * OR hardware_lock for reading.
795 	 */
796 	int tgt_stop; /* the target mode driver is being stopped */
797 	int tgt_stopped; /* the target mode driver has been stopped */
798 
799 	/* Count of sessions refering qla_tgt. Protected by hardware_lock. */
800 	int sess_count;
801 
802 	/* Protected by hardware_lock */
803 	struct list_head del_sess_list;
804 
805 	spinlock_t sess_work_lock;
806 	struct list_head sess_works_list;
807 	struct work_struct sess_work;
808 
809 	struct imm_ntfy_from_isp link_reinit_iocb;
810 	wait_queue_head_t waitQ;
811 	int notify_ack_expected;
812 	int abts_resp_expected;
813 	int modify_lun_expected;
814 	atomic_t tgt_global_resets_count;
815 	struct list_head tgt_list_entry;
816 };
817 
818 struct qla_tgt_sess_op {
819 	struct scsi_qla_host *vha;
820 	uint32_t chip_reset;
821 	struct atio_from_isp atio;
822 	struct work_struct work;
823 	struct list_head cmd_list;
824 	bool aborted;
825 	struct rsp_que *rsp;
826 };
827 
828 enum trace_flags {
829 	TRC_NEW_CMD = BIT_0,
830 	TRC_DO_WORK = BIT_1,
831 	TRC_DO_WORK_ERR = BIT_2,
832 	TRC_XFR_RDY = BIT_3,
833 	TRC_XMIT_DATA = BIT_4,
834 	TRC_XMIT_STATUS = BIT_5,
835 	TRC_SRR_RSP =  BIT_6,
836 	TRC_SRR_XRDY = BIT_7,
837 	TRC_SRR_TERM = BIT_8,
838 	TRC_SRR_CTIO = BIT_9,
839 	TRC_FLUSH = BIT_10,
840 	TRC_CTIO_ERR = BIT_11,
841 	TRC_CTIO_DONE = BIT_12,
842 	TRC_CTIO_ABORTED =  BIT_13,
843 	TRC_CTIO_STRANGE = BIT_14,
844 	TRC_CMD_DONE = BIT_15,
845 	TRC_CMD_CHK_STOP = BIT_16,
846 	TRC_CMD_FREE = BIT_17,
847 	TRC_DATA_IN = BIT_18,
848 	TRC_ABORT = BIT_19,
849 	TRC_DIF_ERR = BIT_20,
850 };
851 
852 struct qla_tgt_cmd {
853 	/*
854 	 * Do not move cmd_type field. it needs to line up with srb->cmd_type
855 	 */
856 	uint8_t cmd_type;
857 	uint8_t pad[7];
858 	struct se_cmd se_cmd;
859 	struct list_head sess_cmd_list;
860 	struct fc_port *sess;
861 	struct qla_qpair *qpair;
862 	uint32_t reset_count;
863 	int state;
864 	struct work_struct work;
865 	/* Sense buffer that will be mapped into outgoing status */
866 	unsigned char sense_buffer[TRANSPORT_SENSE_BUFFER];
867 
868 	spinlock_t cmd_lock;
869 	/* to save extra sess dereferences */
870 	unsigned int conf_compl_supported:1;
871 	unsigned int sg_mapped:1;
872 	unsigned int free_sg:1;
873 	unsigned int write_data_transferred:1;
874 	unsigned int q_full:1;
875 	unsigned int term_exchg:1;
876 	unsigned int cmd_sent_to_fw:1;
877 	unsigned int cmd_in_wq:1;
878 
879 	/*
880 	 * This variable may be set from outside the LIO and I/O completion
881 	 * callback functions. Do not declare this member variable as a
882 	 * bitfield to avoid a read-modify-write operation when this variable
883 	 * is set.
884 	 */
885 	unsigned int aborted;
886 
887 	struct scatterlist *sg;	/* cmd data buffer SG vector */
888 	int sg_cnt;		/* SG segments count */
889 	int bufflen;		/* cmd buffer length */
890 	int offset;
891 	u64 unpacked_lun;
892 	enum dma_data_direction dma_data_direction;
893 
894 	uint16_t ctio_flags;
895 	uint16_t vp_idx;
896 	uint16_t loop_id;	/* to save extra sess dereferences */
897 	struct qla_tgt *tgt;	/* to save extra sess dereferences */
898 	struct scsi_qla_host *vha;
899 	struct list_head cmd_list;
900 
901 	struct atio_from_isp atio;
902 
903 	uint8_t ctx_dsd_alloced;
904 
905 	/* T10-DIF */
906 #define DIF_ERR_NONE 0
907 #define DIF_ERR_GRD 1
908 #define DIF_ERR_REF 2
909 #define DIF_ERR_APP 3
910 	int8_t dif_err_code;
911 	struct scatterlist *prot_sg;
912 	uint32_t prot_sg_cnt;
913 	uint32_t blk_sz, num_blks;
914 	uint8_t scsi_status, sense_key, asc, ascq;
915 
916 	struct crc_context *ctx;
917 	const uint8_t	*cdb;
918 	uint64_t	lba;
919 	uint16_t	a_guard, e_guard, a_app_tag, e_app_tag;
920 	uint32_t	a_ref_tag, e_ref_tag;
921 #define DIF_BUNDL_DMA_VALID 1
922 	uint16_t prot_flags;
923 
924 	uint64_t jiffies_at_alloc;
925 	uint64_t jiffies_at_free;
926 
927 	enum trace_flags trc_flags;
928 };
929 
930 struct qla_tgt_sess_work_param {
931 	struct list_head sess_works_list_entry;
932 
933 #define QLA_TGT_SESS_WORK_ABORT	1
934 #define QLA_TGT_SESS_WORK_TM	2
935 	int type;
936 
937 	union {
938 		struct abts_recv_from_24xx abts;
939 		struct imm_ntfy_from_isp tm_iocb;
940 		struct atio_from_isp tm_iocb2;
941 	};
942 };
943 
944 struct qla_tgt_mgmt_cmd {
945 	uint8_t cmd_type;
946 	uint8_t pad[3];
947 	uint16_t tmr_func;
948 	uint8_t fc_tm_rsp;
949 	uint8_t abort_io_attr;
950 	struct fc_port *sess;
951 	struct qla_qpair *qpair;
952 	struct scsi_qla_host *vha;
953 	struct se_cmd se_cmd;
954 	struct work_struct free_work;
955 	unsigned int flags;
956 #define QLA24XX_MGMT_SEND_NACK	BIT_0
957 #define QLA24XX_MGMT_ABORT_IO_ATTR_VALID BIT_1
958 	uint32_t reset_count;
959 	struct work_struct work;
960 	uint64_t unpacked_lun;
961 	union {
962 		struct atio_from_isp atio;
963 		struct imm_ntfy_from_isp imm_ntfy;
964 		struct abts_recv_from_24xx abts;
965 	} __packed orig_iocb;
966 };
967 
968 struct qla_tgt_prm {
969 	struct qla_tgt_cmd *cmd;
970 	struct qla_tgt *tgt;
971 	void *pkt;
972 	struct scatterlist *sg;	/* cmd data buffer SG vector */
973 	unsigned char *sense_buffer;
974 	int seg_cnt;
975 	int req_cnt;
976 	uint16_t rq_result;
977 	int sense_buffer_len;
978 	int residual;
979 	int add_status_pkt;
980 	/* dif */
981 	struct scatterlist *prot_sg;
982 	uint16_t prot_seg_cnt;
983 	uint16_t tot_dsds;
984 };
985 
986 /* Check for Switch reserved address */
987 #define IS_SW_RESV_ADDR(_s_id) \
988 	((_s_id.b.domain == 0xff) && ((_s_id.b.area & 0xf0) == 0xf0))
989 
990 #define QLA_TGT_XMIT_DATA		1
991 #define QLA_TGT_XMIT_STATUS		2
992 #define QLA_TGT_XMIT_ALL		(QLA_TGT_XMIT_STATUS|QLA_TGT_XMIT_DATA)
993 
994 
995 extern struct qla_tgt_data qla_target;
996 
997 /*
998  * Function prototypes for qla_target.c logic used by qla2xxx LLD code.
999  */
1000 extern int qlt_add_target(struct qla_hw_data *, struct scsi_qla_host *);
1001 extern int qlt_remove_target(struct qla_hw_data *, struct scsi_qla_host *);
1002 extern int qlt_lport_register(void *, u64, u64, u64,
1003 			int (*callback)(struct scsi_qla_host *, void *, u64, u64));
1004 extern void qlt_lport_deregister(struct scsi_qla_host *);
1005 extern void qlt_unreg_sess(struct fc_port *);
1006 extern void qlt_fc_port_added(struct scsi_qla_host *, fc_port_t *);
1007 extern void qlt_fc_port_deleted(struct scsi_qla_host *, fc_port_t *, int);
1008 extern int __init qlt_init(void);
1009 extern void qlt_exit(void);
1010 extern void qlt_update_vp_map(struct scsi_qla_host *, int);
1011 extern void qlt_free_session_done(struct work_struct *);
1012 /*
1013  * This macro is used during early initializations when host->active_mode
1014  * is not set. Right now, ha value is ignored.
1015  */
1016 #define QLA_TGT_MODE_ENABLED() (ql2x_ini_mode != QLA2XXX_INI_MODE_ENABLED)
1017 
1018 extern int ql2x_ini_mode;
1019 
1020 static inline bool qla_tgt_mode_enabled(struct scsi_qla_host *ha)
1021 {
1022 	return ha->host->active_mode == MODE_TARGET;
1023 }
1024 
1025 static inline bool qla_ini_mode_enabled(struct scsi_qla_host *ha)
1026 {
1027 	return ha->host->active_mode == MODE_INITIATOR;
1028 }
1029 
1030 static inline bool qla_dual_mode_enabled(struct scsi_qla_host *ha)
1031 {
1032 	return (ha->host->active_mode == MODE_DUAL);
1033 }
1034 
1035 static inline uint32_t sid_to_key(const be_id_t s_id)
1036 {
1037 	return s_id.domain << 16 |
1038 		s_id.area << 8 |
1039 		s_id.al_pa;
1040 }
1041 
1042 /*
1043  * Exported symbols from qla_target.c LLD logic used by qla2xxx code..
1044  */
1045 extern void qlt_response_pkt_all_vps(struct scsi_qla_host *, struct rsp_que *,
1046 	response_t *);
1047 extern int qlt_rdy_to_xfer(struct qla_tgt_cmd *);
1048 extern int qlt_xmit_response(struct qla_tgt_cmd *, int, uint8_t);
1049 extern int qlt_abort_cmd(struct qla_tgt_cmd *);
1050 extern void qlt_xmit_tm_rsp(struct qla_tgt_mgmt_cmd *);
1051 extern void qlt_free_mcmd(struct qla_tgt_mgmt_cmd *);
1052 extern void qlt_free_cmd(struct qla_tgt_cmd *cmd);
1053 extern void qlt_async_event(uint16_t, struct scsi_qla_host *, uint16_t *);
1054 extern void qlt_enable_vha(struct scsi_qla_host *);
1055 extern void qlt_vport_create(struct scsi_qla_host *, struct qla_hw_data *);
1056 extern u8 qlt_rff_id(struct scsi_qla_host *);
1057 extern void qlt_init_atio_q_entries(struct scsi_qla_host *);
1058 extern void qlt_24xx_process_atio_queue(struct scsi_qla_host *, uint8_t);
1059 extern void qlt_24xx_config_rings(struct scsi_qla_host *);
1060 extern void qlt_24xx_config_nvram_stage1(struct scsi_qla_host *,
1061 	struct nvram_24xx *);
1062 extern void qlt_24xx_config_nvram_stage2(struct scsi_qla_host *,
1063 	struct init_cb_24xx *);
1064 extern void qlt_81xx_config_nvram_stage2(struct scsi_qla_host *,
1065 	struct init_cb_81xx *);
1066 extern void qlt_81xx_config_nvram_stage1(struct scsi_qla_host *,
1067 	struct nvram_81xx *);
1068 extern int qlt_24xx_process_response_error(struct scsi_qla_host *,
1069 	struct sts_entry_24xx *);
1070 extern void qlt_modify_vp_config(struct scsi_qla_host *,
1071 	struct vp_config_entry_24xx *);
1072 extern void qlt_probe_one_stage1(struct scsi_qla_host *, struct qla_hw_data *);
1073 extern int qlt_mem_alloc(struct qla_hw_data *);
1074 extern void qlt_mem_free(struct qla_hw_data *);
1075 extern int qlt_stop_phase1(struct qla_tgt *);
1076 extern void qlt_stop_phase2(struct qla_tgt *);
1077 extern irqreturn_t qla83xx_msix_atio_q(int, void *);
1078 extern void qlt_83xx_iospace_config(struct qla_hw_data *);
1079 extern int qlt_free_qfull_cmds(struct qla_qpair *);
1080 extern void qlt_logo_completion_handler(fc_port_t *, int);
1081 extern void qlt_do_generation_tick(struct scsi_qla_host *, int *);
1082 
1083 void qlt_send_resp_ctio(struct qla_qpair *, struct qla_tgt_cmd *, uint8_t,
1084     uint8_t, uint8_t, uint8_t);
1085 
1086 #endif /* __QLA_TARGET_H */
1087