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