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