xref: /linux/drivers/scsi/qla2xxx/qla_target.c (revision 005438a8eef063495ac059d128eea71b58de50e5)
1 /*
2  *  qla_target.c SCSI LLD infrastructure for QLogic 22xx/23xx/24xx/25xx
3  *
4  *  based on qla2x00t.c code:
5  *
6  *  Copyright (C) 2004 - 2010 Vladislav Bolkhovitin <vst@vlnb.net>
7  *  Copyright (C) 2004 - 2005 Leonid Stoljar
8  *  Copyright (C) 2006 Nathaniel Clark <nate@misrule.us>
9  *  Copyright (C) 2006 - 2010 ID7 Ltd.
10  *
11  *  Forward port and refactoring to modern qla2xxx and target/configfs
12  *
13  *  Copyright (C) 2010-2013 Nicholas A. Bellinger <nab@kernel.org>
14  *
15  *  This program is free software; you can redistribute it and/or
16  *  modify it under the terms of the GNU General Public License
17  *  as published by the Free Software Foundation, version 2
18  *  of the License.
19  *
20  *  This program is distributed in the hope that it will be useful,
21  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
22  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23  *  GNU General Public License for more details.
24  */
25 
26 #include <linux/module.h>
27 #include <linux/init.h>
28 #include <linux/types.h>
29 #include <linux/blkdev.h>
30 #include <linux/interrupt.h>
31 #include <linux/pci.h>
32 #include <linux/delay.h>
33 #include <linux/list.h>
34 #include <linux/workqueue.h>
35 #include <asm/unaligned.h>
36 #include <scsi/scsi.h>
37 #include <scsi/scsi_host.h>
38 #include <scsi/scsi_tcq.h>
39 #include <target/target_core_base.h>
40 #include <target/target_core_fabric.h>
41 
42 #include "qla_def.h"
43 #include "qla_target.h"
44 
45 static int ql2xtgt_tape_enable;
46 module_param(ql2xtgt_tape_enable, int, S_IRUGO|S_IWUSR);
47 MODULE_PARM_DESC(ql2xtgt_tape_enable,
48 		"Enables Sequence level error recovery (aka FC Tape). Default is 0 - no SLER. 1 - Enable SLER.");
49 
50 static char *qlini_mode = QLA2XXX_INI_MODE_STR_ENABLED;
51 module_param(qlini_mode, charp, S_IRUGO);
52 MODULE_PARM_DESC(qlini_mode,
53 	"Determines when initiator mode will be enabled. Possible values: "
54 	"\"exclusive\" - initiator mode will be enabled on load, "
55 	"disabled on enabling target mode and then on disabling target mode "
56 	"enabled back; "
57 	"\"disabled\" - initiator mode will never be enabled; "
58 	"\"enabled\" (default) - initiator mode will always stay enabled.");
59 
60 int ql2x_ini_mode = QLA2XXX_INI_MODE_EXCLUSIVE;
61 
62 static int temp_sam_status = SAM_STAT_BUSY;
63 
64 /*
65  * From scsi/fc/fc_fcp.h
66  */
67 enum fcp_resp_rsp_codes {
68 	FCP_TMF_CMPL = 0,
69 	FCP_DATA_LEN_INVALID = 1,
70 	FCP_CMND_FIELDS_INVALID = 2,
71 	FCP_DATA_PARAM_MISMATCH = 3,
72 	FCP_TMF_REJECTED = 4,
73 	FCP_TMF_FAILED = 5,
74 	FCP_TMF_INVALID_LUN = 9,
75 };
76 
77 /*
78  * fc_pri_ta from scsi/fc/fc_fcp.h
79  */
80 #define FCP_PTA_SIMPLE      0   /* simple task attribute */
81 #define FCP_PTA_HEADQ       1   /* head of queue task attribute */
82 #define FCP_PTA_ORDERED     2   /* ordered task attribute */
83 #define FCP_PTA_ACA         4   /* auto. contingent allegiance */
84 #define FCP_PTA_MASK        7   /* mask for task attribute field */
85 #define FCP_PRI_SHIFT       3   /* priority field starts in bit 3 */
86 #define FCP_PRI_RESVD_MASK  0x80        /* reserved bits in priority field */
87 
88 /*
89  * This driver calls qla2x00_alloc_iocbs() and qla2x00_issue_marker(), which
90  * must be called under HW lock and could unlock/lock it inside.
91  * It isn't an issue, since in the current implementation on the time when
92  * those functions are called:
93  *
94  *   - Either context is IRQ and only IRQ handler can modify HW data,
95  *     including rings related fields,
96  *
97  *   - Or access to target mode variables from struct qla_tgt doesn't
98  *     cross those functions boundaries, except tgt_stop, which
99  *     additionally protected by irq_cmd_count.
100  */
101 /* Predefs for callbacks handed to qla2xxx LLD */
102 static void qlt_24xx_atio_pkt(struct scsi_qla_host *ha,
103 	struct atio_from_isp *pkt);
104 static void qlt_response_pkt(struct scsi_qla_host *ha, response_t *pkt);
105 static int qlt_issue_task_mgmt(struct qla_tgt_sess *sess, uint32_t lun,
106 	int fn, void *iocb, int flags);
107 static void qlt_send_term_exchange(struct scsi_qla_host *ha, struct qla_tgt_cmd
108 	*cmd, struct atio_from_isp *atio, int ha_locked);
109 static void qlt_reject_free_srr_imm(struct scsi_qla_host *ha,
110 	struct qla_tgt_srr_imm *imm, int ha_lock);
111 static void qlt_abort_cmd_on_host_reset(struct scsi_qla_host *vha,
112 	struct qla_tgt_cmd *cmd);
113 static void qlt_alloc_qfull_cmd(struct scsi_qla_host *vha,
114 	struct atio_from_isp *atio, uint16_t status, int qfull);
115 static void qlt_disable_vha(struct scsi_qla_host *vha);
116 /*
117  * Global Variables
118  */
119 static struct kmem_cache *qla_tgt_mgmt_cmd_cachep;
120 static mempool_t *qla_tgt_mgmt_cmd_mempool;
121 static struct workqueue_struct *qla_tgt_wq;
122 static DEFINE_MUTEX(qla_tgt_mutex);
123 static LIST_HEAD(qla_tgt_glist);
124 
125 /* ha->hardware_lock supposed to be held on entry (to protect tgt->sess_list) */
126 static struct qla_tgt_sess *qlt_find_sess_by_port_name(
127 	struct qla_tgt *tgt,
128 	const uint8_t *port_name)
129 {
130 	struct qla_tgt_sess *sess;
131 
132 	list_for_each_entry(sess, &tgt->sess_list, sess_list_entry) {
133 		if (!memcmp(sess->port_name, port_name, WWN_SIZE))
134 			return sess;
135 	}
136 
137 	return NULL;
138 }
139 
140 /* Might release hw lock, then reaquire!! */
141 static inline int qlt_issue_marker(struct scsi_qla_host *vha, int vha_locked)
142 {
143 	/* Send marker if required */
144 	if (unlikely(vha->marker_needed != 0)) {
145 		int rc = qla2x00_issue_marker(vha, vha_locked);
146 		if (rc != QLA_SUCCESS) {
147 			ql_dbg(ql_dbg_tgt, vha, 0xe03d,
148 			    "qla_target(%d): issue_marker() failed\n",
149 			    vha->vp_idx);
150 		}
151 		return rc;
152 	}
153 	return QLA_SUCCESS;
154 }
155 
156 static inline
157 struct scsi_qla_host *qlt_find_host_by_d_id(struct scsi_qla_host *vha,
158 	uint8_t *d_id)
159 {
160 	struct qla_hw_data *ha = vha->hw;
161 	uint8_t vp_idx;
162 
163 	if ((vha->d_id.b.area != d_id[1]) || (vha->d_id.b.domain != d_id[0]))
164 		return NULL;
165 
166 	if (vha->d_id.b.al_pa == d_id[2])
167 		return vha;
168 
169 	BUG_ON(ha->tgt.tgt_vp_map == NULL);
170 	vp_idx = ha->tgt.tgt_vp_map[d_id[2]].idx;
171 	if (likely(test_bit(vp_idx, ha->vp_idx_map)))
172 		return ha->tgt.tgt_vp_map[vp_idx].vha;
173 
174 	return NULL;
175 }
176 
177 static inline
178 struct scsi_qla_host *qlt_find_host_by_vp_idx(struct scsi_qla_host *vha,
179 	uint16_t vp_idx)
180 {
181 	struct qla_hw_data *ha = vha->hw;
182 
183 	if (vha->vp_idx == vp_idx)
184 		return vha;
185 
186 	BUG_ON(ha->tgt.tgt_vp_map == NULL);
187 	if (likely(test_bit(vp_idx, ha->vp_idx_map)))
188 		return ha->tgt.tgt_vp_map[vp_idx].vha;
189 
190 	return NULL;
191 }
192 
193 static inline void qlt_incr_num_pend_cmds(struct scsi_qla_host *vha)
194 {
195 	unsigned long flags;
196 
197 	spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
198 
199 	vha->hw->tgt.num_pend_cmds++;
200 	if (vha->hw->tgt.num_pend_cmds > vha->hw->qla_stats.stat_max_pend_cmds)
201 		vha->hw->qla_stats.stat_max_pend_cmds =
202 			vha->hw->tgt.num_pend_cmds;
203 	spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
204 }
205 static inline void qlt_decr_num_pend_cmds(struct scsi_qla_host *vha)
206 {
207 	unsigned long flags;
208 
209 	spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
210 	vha->hw->tgt.num_pend_cmds--;
211 	spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
212 }
213 
214 static void qlt_24xx_atio_pkt_all_vps(struct scsi_qla_host *vha,
215 	struct atio_from_isp *atio)
216 {
217 	ql_dbg(ql_dbg_tgt, vha, 0xe072,
218 		"%s: qla_target(%d): type %x ox_id %04x\n",
219 		__func__, vha->vp_idx, atio->u.raw.entry_type,
220 		be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id));
221 
222 	switch (atio->u.raw.entry_type) {
223 	case ATIO_TYPE7:
224 	{
225 		struct scsi_qla_host *host = qlt_find_host_by_d_id(vha,
226 		    atio->u.isp24.fcp_hdr.d_id);
227 		if (unlikely(NULL == host)) {
228 			ql_dbg(ql_dbg_tgt, vha, 0xe03e,
229 			    "qla_target(%d): Received ATIO_TYPE7 "
230 			    "with unknown d_id %x:%x:%x\n", vha->vp_idx,
231 			    atio->u.isp24.fcp_hdr.d_id[0],
232 			    atio->u.isp24.fcp_hdr.d_id[1],
233 			    atio->u.isp24.fcp_hdr.d_id[2]);
234 			break;
235 		}
236 		qlt_24xx_atio_pkt(host, atio);
237 		break;
238 	}
239 
240 	case IMMED_NOTIFY_TYPE:
241 	{
242 		struct scsi_qla_host *host = vha;
243 		struct imm_ntfy_from_isp *entry =
244 		    (struct imm_ntfy_from_isp *)atio;
245 
246 		if ((entry->u.isp24.vp_index != 0xFF) &&
247 		    (entry->u.isp24.nport_handle != 0xFFFF)) {
248 			host = qlt_find_host_by_vp_idx(vha,
249 			    entry->u.isp24.vp_index);
250 			if (unlikely(!host)) {
251 				ql_dbg(ql_dbg_tgt, vha, 0xe03f,
252 				    "qla_target(%d): Received "
253 				    "ATIO (IMMED_NOTIFY_TYPE) "
254 				    "with unknown vp_index %d\n",
255 				    vha->vp_idx, entry->u.isp24.vp_index);
256 				break;
257 			}
258 		}
259 		qlt_24xx_atio_pkt(host, atio);
260 		break;
261 	}
262 
263 	default:
264 		ql_dbg(ql_dbg_tgt, vha, 0xe040,
265 		    "qla_target(%d): Received unknown ATIO atio "
266 		    "type %x\n", vha->vp_idx, atio->u.raw.entry_type);
267 		break;
268 	}
269 
270 	return;
271 }
272 
273 void qlt_response_pkt_all_vps(struct scsi_qla_host *vha, response_t *pkt)
274 {
275 	switch (pkt->entry_type) {
276 	case CTIO_CRC2:
277 		ql_dbg(ql_dbg_tgt, vha, 0xe073,
278 			"qla_target(%d):%s: CRC2 Response pkt\n",
279 			vha->vp_idx, __func__);
280 	case CTIO_TYPE7:
281 	{
282 		struct ctio7_from_24xx *entry = (struct ctio7_from_24xx *)pkt;
283 		struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
284 		    entry->vp_index);
285 		if (unlikely(!host)) {
286 			ql_dbg(ql_dbg_tgt, vha, 0xe041,
287 			    "qla_target(%d): Response pkt (CTIO_TYPE7) "
288 			    "received, with unknown vp_index %d\n",
289 			    vha->vp_idx, entry->vp_index);
290 			break;
291 		}
292 		qlt_response_pkt(host, pkt);
293 		break;
294 	}
295 
296 	case IMMED_NOTIFY_TYPE:
297 	{
298 		struct scsi_qla_host *host = vha;
299 		struct imm_ntfy_from_isp *entry =
300 		    (struct imm_ntfy_from_isp *)pkt;
301 
302 		host = qlt_find_host_by_vp_idx(vha, entry->u.isp24.vp_index);
303 		if (unlikely(!host)) {
304 			ql_dbg(ql_dbg_tgt, vha, 0xe042,
305 			    "qla_target(%d): Response pkt (IMMED_NOTIFY_TYPE) "
306 			    "received, with unknown vp_index %d\n",
307 			    vha->vp_idx, entry->u.isp24.vp_index);
308 			break;
309 		}
310 		qlt_response_pkt(host, pkt);
311 		break;
312 	}
313 
314 	case NOTIFY_ACK_TYPE:
315 	{
316 		struct scsi_qla_host *host = vha;
317 		struct nack_to_isp *entry = (struct nack_to_isp *)pkt;
318 
319 		if (0xFF != entry->u.isp24.vp_index) {
320 			host = qlt_find_host_by_vp_idx(vha,
321 			    entry->u.isp24.vp_index);
322 			if (unlikely(!host)) {
323 				ql_dbg(ql_dbg_tgt, vha, 0xe043,
324 				    "qla_target(%d): Response "
325 				    "pkt (NOTIFY_ACK_TYPE) "
326 				    "received, with unknown "
327 				    "vp_index %d\n", vha->vp_idx,
328 				    entry->u.isp24.vp_index);
329 				break;
330 			}
331 		}
332 		qlt_response_pkt(host, pkt);
333 		break;
334 	}
335 
336 	case ABTS_RECV_24XX:
337 	{
338 		struct abts_recv_from_24xx *entry =
339 		    (struct abts_recv_from_24xx *)pkt;
340 		struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
341 		    entry->vp_index);
342 		if (unlikely(!host)) {
343 			ql_dbg(ql_dbg_tgt, vha, 0xe044,
344 			    "qla_target(%d): Response pkt "
345 			    "(ABTS_RECV_24XX) received, with unknown "
346 			    "vp_index %d\n", vha->vp_idx, entry->vp_index);
347 			break;
348 		}
349 		qlt_response_pkt(host, pkt);
350 		break;
351 	}
352 
353 	case ABTS_RESP_24XX:
354 	{
355 		struct abts_resp_to_24xx *entry =
356 		    (struct abts_resp_to_24xx *)pkt;
357 		struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
358 		    entry->vp_index);
359 		if (unlikely(!host)) {
360 			ql_dbg(ql_dbg_tgt, vha, 0xe045,
361 			    "qla_target(%d): Response pkt "
362 			    "(ABTS_RECV_24XX) received, with unknown "
363 			    "vp_index %d\n", vha->vp_idx, entry->vp_index);
364 			break;
365 		}
366 		qlt_response_pkt(host, pkt);
367 		break;
368 	}
369 
370 	default:
371 		qlt_response_pkt(vha, pkt);
372 		break;
373 	}
374 
375 }
376 
377 static void qlt_free_session_done(struct work_struct *work)
378 {
379 	struct qla_tgt_sess *sess = container_of(work, struct qla_tgt_sess,
380 	    free_work);
381 	struct qla_tgt *tgt = sess->tgt;
382 	struct scsi_qla_host *vha = sess->vha;
383 	struct qla_hw_data *ha = vha->hw;
384 
385 	BUG_ON(!tgt);
386 	/*
387 	 * Release the target session for FC Nexus from fabric module code.
388 	 */
389 	if (sess->se_sess != NULL)
390 		ha->tgt.tgt_ops->free_session(sess);
391 
392 	ql_dbg(ql_dbg_tgt_mgt, vha, 0xf001,
393 	    "Unregistration of sess %p finished\n", sess);
394 
395 	kfree(sess);
396 	/*
397 	 * We need to protect against race, when tgt is freed before or
398 	 * inside wake_up()
399 	 */
400 	tgt->sess_count--;
401 	if (tgt->sess_count == 0)
402 		wake_up_all(&tgt->waitQ);
403 }
404 
405 /* ha->hardware_lock supposed to be held on entry */
406 void qlt_unreg_sess(struct qla_tgt_sess *sess)
407 {
408 	struct scsi_qla_host *vha = sess->vha;
409 
410 	vha->hw->tgt.tgt_ops->clear_nacl_from_fcport_map(sess);
411 
412 	list_del(&sess->sess_list_entry);
413 	if (sess->deleted)
414 		list_del(&sess->del_list_entry);
415 
416 	INIT_WORK(&sess->free_work, qlt_free_session_done);
417 	schedule_work(&sess->free_work);
418 }
419 EXPORT_SYMBOL(qlt_unreg_sess);
420 
421 /* ha->hardware_lock supposed to be held on entry */
422 static int qlt_reset(struct scsi_qla_host *vha, void *iocb, int mcmd)
423 {
424 	struct qla_hw_data *ha = vha->hw;
425 	struct qla_tgt_sess *sess = NULL;
426 	uint32_t unpacked_lun, lun = 0;
427 	uint16_t loop_id;
428 	int res = 0;
429 	struct imm_ntfy_from_isp *n = (struct imm_ntfy_from_isp *)iocb;
430 	struct atio_from_isp *a = (struct atio_from_isp *)iocb;
431 
432 	loop_id = le16_to_cpu(n->u.isp24.nport_handle);
433 	if (loop_id == 0xFFFF) {
434 #if 0 /* FIXME: Re-enable Global event handling.. */
435 		/* Global event */
436 		atomic_inc(&ha->tgt.qla_tgt->tgt_global_resets_count);
437 		qlt_clear_tgt_db(ha->tgt.qla_tgt);
438 		if (!list_empty(&ha->tgt.qla_tgt->sess_list)) {
439 			sess = list_entry(ha->tgt.qla_tgt->sess_list.next,
440 			    typeof(*sess), sess_list_entry);
441 			switch (mcmd) {
442 			case QLA_TGT_NEXUS_LOSS_SESS:
443 				mcmd = QLA_TGT_NEXUS_LOSS;
444 				break;
445 			case QLA_TGT_ABORT_ALL_SESS:
446 				mcmd = QLA_TGT_ABORT_ALL;
447 				break;
448 			case QLA_TGT_NEXUS_LOSS:
449 			case QLA_TGT_ABORT_ALL:
450 				break;
451 			default:
452 				ql_dbg(ql_dbg_tgt, vha, 0xe046,
453 				    "qla_target(%d): Not allowed "
454 				    "command %x in %s", vha->vp_idx,
455 				    mcmd, __func__);
456 				sess = NULL;
457 				break;
458 			}
459 		} else
460 			sess = NULL;
461 #endif
462 	} else {
463 		sess = ha->tgt.tgt_ops->find_sess_by_loop_id(vha, loop_id);
464 	}
465 
466 	ql_dbg(ql_dbg_tgt, vha, 0xe000,
467 	    "Using sess for qla_tgt_reset: %p\n", sess);
468 	if (!sess) {
469 		res = -ESRCH;
470 		return res;
471 	}
472 
473 	ql_dbg(ql_dbg_tgt, vha, 0xe047,
474 	    "scsi(%ld): resetting (session %p from port %8phC mcmd %x, "
475 	    "loop_id %d)\n", vha->host_no, sess, sess->port_name,
476 	    mcmd, loop_id);
477 
478 	lun = a->u.isp24.fcp_cmnd.lun;
479 	unpacked_lun = scsilun_to_int((struct scsi_lun *)&lun);
480 
481 	return qlt_issue_task_mgmt(sess, unpacked_lun, mcmd,
482 	    iocb, QLA24XX_MGMT_SEND_NACK);
483 }
484 
485 /* ha->hardware_lock supposed to be held on entry */
486 static void qlt_schedule_sess_for_deletion(struct qla_tgt_sess *sess,
487 	bool immediate)
488 {
489 	struct qla_tgt *tgt = sess->tgt;
490 	uint32_t dev_loss_tmo = tgt->ha->port_down_retry_count + 5;
491 
492 	if (sess->deleted)
493 		return;
494 
495 	ql_dbg(ql_dbg_tgt, sess->vha, 0xe001,
496 	    "Scheduling sess %p for deletion\n", sess);
497 	list_add_tail(&sess->del_list_entry, &tgt->del_sess_list);
498 	sess->deleted = 1;
499 
500 	if (immediate)
501 		dev_loss_tmo = 0;
502 
503 	sess->expires = jiffies + dev_loss_tmo * HZ;
504 
505 	ql_dbg(ql_dbg_tgt, sess->vha, 0xe048,
506 	    "qla_target(%d): session for port %8phC (loop ID %d) scheduled for "
507 	    "deletion in %u secs (expires: %lu) immed: %d\n",
508 	    sess->vha->vp_idx, sess->port_name, sess->loop_id, dev_loss_tmo,
509 	    sess->expires, immediate);
510 
511 	if (immediate)
512 		schedule_delayed_work(&tgt->sess_del_work, 0);
513 	else
514 		schedule_delayed_work(&tgt->sess_del_work,
515 		    sess->expires - jiffies);
516 }
517 
518 /* ha->hardware_lock supposed to be held on entry */
519 static void qlt_clear_tgt_db(struct qla_tgt *tgt)
520 {
521 	struct qla_tgt_sess *sess;
522 
523 	list_for_each_entry(sess, &tgt->sess_list, sess_list_entry)
524 		qlt_schedule_sess_for_deletion(sess, true);
525 
526 	/* At this point tgt could be already dead */
527 }
528 
529 static int qla24xx_get_loop_id(struct scsi_qla_host *vha, const uint8_t *s_id,
530 	uint16_t *loop_id)
531 {
532 	struct qla_hw_data *ha = vha->hw;
533 	dma_addr_t gid_list_dma;
534 	struct gid_list_info *gid_list;
535 	char *id_iter;
536 	int res, rc, i;
537 	uint16_t entries;
538 
539 	gid_list = dma_alloc_coherent(&ha->pdev->dev, qla2x00_gid_list_size(ha),
540 	    &gid_list_dma, GFP_KERNEL);
541 	if (!gid_list) {
542 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf044,
543 		    "qla_target(%d): DMA Alloc failed of %u\n",
544 		    vha->vp_idx, qla2x00_gid_list_size(ha));
545 		return -ENOMEM;
546 	}
547 
548 	/* Get list of logged in devices */
549 	rc = qla2x00_get_id_list(vha, gid_list, gid_list_dma, &entries);
550 	if (rc != QLA_SUCCESS) {
551 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf045,
552 		    "qla_target(%d): get_id_list() failed: %x\n",
553 		    vha->vp_idx, rc);
554 		res = -1;
555 		goto out_free_id_list;
556 	}
557 
558 	id_iter = (char *)gid_list;
559 	res = -1;
560 	for (i = 0; i < entries; i++) {
561 		struct gid_list_info *gid = (struct gid_list_info *)id_iter;
562 		if ((gid->al_pa == s_id[2]) &&
563 		    (gid->area == s_id[1]) &&
564 		    (gid->domain == s_id[0])) {
565 			*loop_id = le16_to_cpu(gid->loop_id);
566 			res = 0;
567 			break;
568 		}
569 		id_iter += ha->gid_list_info_size;
570 	}
571 
572 out_free_id_list:
573 	dma_free_coherent(&ha->pdev->dev, qla2x00_gid_list_size(ha),
574 	    gid_list, gid_list_dma);
575 	return res;
576 }
577 
578 /* ha->hardware_lock supposed to be held on entry */
579 static void qlt_undelete_sess(struct qla_tgt_sess *sess)
580 {
581 	BUG_ON(!sess->deleted);
582 
583 	list_del(&sess->del_list_entry);
584 	sess->deleted = 0;
585 }
586 
587 static void qlt_del_sess_work_fn(struct delayed_work *work)
588 {
589 	struct qla_tgt *tgt = container_of(work, struct qla_tgt,
590 	    sess_del_work);
591 	struct scsi_qla_host *vha = tgt->vha;
592 	struct qla_hw_data *ha = vha->hw;
593 	struct qla_tgt_sess *sess;
594 	unsigned long flags, elapsed;
595 
596 	spin_lock_irqsave(&ha->hardware_lock, flags);
597 	while (!list_empty(&tgt->del_sess_list)) {
598 		sess = list_entry(tgt->del_sess_list.next, typeof(*sess),
599 		    del_list_entry);
600 		elapsed = jiffies;
601 		if (time_after_eq(elapsed, sess->expires)) {
602 			qlt_undelete_sess(sess);
603 
604 			ql_dbg(ql_dbg_tgt_mgt, vha, 0xf004,
605 			    "Timeout: sess %p about to be deleted\n",
606 			    sess);
607 			ha->tgt.tgt_ops->shutdown_sess(sess);
608 			ha->tgt.tgt_ops->put_sess(sess);
609 		} else {
610 			schedule_delayed_work(&tgt->sess_del_work,
611 			    sess->expires - elapsed);
612 			break;
613 		}
614 	}
615 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
616 }
617 
618 /*
619  * Adds an extra ref to allow to drop hw lock after adding sess to the list.
620  * Caller must put it.
621  */
622 static struct qla_tgt_sess *qlt_create_sess(
623 	struct scsi_qla_host *vha,
624 	fc_port_t *fcport,
625 	bool local)
626 {
627 	struct qla_hw_data *ha = vha->hw;
628 	struct qla_tgt_sess *sess;
629 	unsigned long flags;
630 	unsigned char be_sid[3];
631 
632 	/* Check to avoid double sessions */
633 	spin_lock_irqsave(&ha->hardware_lock, flags);
634 	list_for_each_entry(sess, &vha->vha_tgt.qla_tgt->sess_list,
635 				sess_list_entry) {
636 		if (!memcmp(sess->port_name, fcport->port_name, WWN_SIZE)) {
637 			ql_dbg(ql_dbg_tgt_mgt, vha, 0xf005,
638 			    "Double sess %p found (s_id %x:%x:%x, "
639 			    "loop_id %d), updating to d_id %x:%x:%x, "
640 			    "loop_id %d", sess, sess->s_id.b.domain,
641 			    sess->s_id.b.al_pa, sess->s_id.b.area,
642 			    sess->loop_id, fcport->d_id.b.domain,
643 			    fcport->d_id.b.al_pa, fcport->d_id.b.area,
644 			    fcport->loop_id);
645 
646 			if (sess->deleted)
647 				qlt_undelete_sess(sess);
648 
649 			kref_get(&sess->se_sess->sess_kref);
650 			ha->tgt.tgt_ops->update_sess(sess, fcport->d_id, fcport->loop_id,
651 						(fcport->flags & FCF_CONF_COMP_SUPPORTED));
652 
653 			if (sess->local && !local)
654 				sess->local = 0;
655 			spin_unlock_irqrestore(&ha->hardware_lock, flags);
656 
657 			return sess;
658 		}
659 	}
660 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
661 
662 	sess = kzalloc(sizeof(*sess), GFP_KERNEL);
663 	if (!sess) {
664 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04a,
665 		    "qla_target(%u): session allocation failed, all commands "
666 		    "from port %8phC will be refused", vha->vp_idx,
667 		    fcport->port_name);
668 
669 		return NULL;
670 	}
671 	sess->tgt = vha->vha_tgt.qla_tgt;
672 	sess->vha = vha;
673 	sess->s_id = fcport->d_id;
674 	sess->loop_id = fcport->loop_id;
675 	sess->local = local;
676 
677 	ql_dbg(ql_dbg_tgt_mgt, vha, 0xf006,
678 	    "Adding sess %p to tgt %p via ->check_initiator_node_acl()\n",
679 	    sess, vha->vha_tgt.qla_tgt);
680 
681 	be_sid[0] = sess->s_id.b.domain;
682 	be_sid[1] = sess->s_id.b.area;
683 	be_sid[2] = sess->s_id.b.al_pa;
684 	/*
685 	 * Determine if this fc_port->port_name is allowed to access
686 	 * target mode using explict NodeACLs+MappedLUNs, or using
687 	 * TPG demo mode.  If this is successful a target mode FC nexus
688 	 * is created.
689 	 */
690 	if (ha->tgt.tgt_ops->check_initiator_node_acl(vha,
691 	    &fcport->port_name[0], sess, &be_sid[0], fcport->loop_id) < 0) {
692 		kfree(sess);
693 		return NULL;
694 	}
695 	/*
696 	 * Take an extra reference to ->sess_kref here to handle qla_tgt_sess
697 	 * access across ->hardware_lock reaquire.
698 	 */
699 	kref_get(&sess->se_sess->sess_kref);
700 
701 	sess->conf_compl_supported = (fcport->flags & FCF_CONF_COMP_SUPPORTED);
702 	BUILD_BUG_ON(sizeof(sess->port_name) != sizeof(fcport->port_name));
703 	memcpy(sess->port_name, fcport->port_name, sizeof(sess->port_name));
704 
705 	spin_lock_irqsave(&ha->hardware_lock, flags);
706 	list_add_tail(&sess->sess_list_entry, &vha->vha_tgt.qla_tgt->sess_list);
707 	vha->vha_tgt.qla_tgt->sess_count++;
708 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
709 
710 	ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04b,
711 	    "qla_target(%d): %ssession for wwn %8phC (loop_id %d, "
712 	    "s_id %x:%x:%x, confirmed completion %ssupported) added\n",
713 	    vha->vp_idx, local ?  "local " : "", fcport->port_name,
714 	    fcport->loop_id, sess->s_id.b.domain, sess->s_id.b.area,
715 	    sess->s_id.b.al_pa, sess->conf_compl_supported ?  "" : "not ");
716 
717 	return sess;
718 }
719 
720 /*
721  * Called from drivers/scsi/qla2xxx/qla_init.c:qla2x00_reg_remote_port()
722  */
723 void qlt_fc_port_added(struct scsi_qla_host *vha, fc_port_t *fcport)
724 {
725 	struct qla_hw_data *ha = vha->hw;
726 	struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
727 	struct qla_tgt_sess *sess;
728 	unsigned long flags;
729 
730 	if (!vha->hw->tgt.tgt_ops)
731 		return;
732 
733 	if (!tgt || (fcport->port_type != FCT_INITIATOR))
734 		return;
735 
736 	if (qla_ini_mode_enabled(vha))
737 		return;
738 
739 	spin_lock_irqsave(&ha->hardware_lock, flags);
740 	if (tgt->tgt_stop) {
741 		spin_unlock_irqrestore(&ha->hardware_lock, flags);
742 		return;
743 	}
744 	sess = qlt_find_sess_by_port_name(tgt, fcport->port_name);
745 	if (!sess) {
746 		spin_unlock_irqrestore(&ha->hardware_lock, flags);
747 
748 		mutex_lock(&vha->vha_tgt.tgt_mutex);
749 		sess = qlt_create_sess(vha, fcport, false);
750 		mutex_unlock(&vha->vha_tgt.tgt_mutex);
751 
752 		spin_lock_irqsave(&ha->hardware_lock, flags);
753 	} else {
754 		kref_get(&sess->se_sess->sess_kref);
755 
756 		if (sess->deleted) {
757 			qlt_undelete_sess(sess);
758 
759 			ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04c,
760 			    "qla_target(%u): %ssession for port %8phC "
761 			    "(loop ID %d) reappeared\n", vha->vp_idx,
762 			    sess->local ? "local " : "", sess->port_name,
763 			    sess->loop_id);
764 
765 			ql_dbg(ql_dbg_tgt_mgt, vha, 0xf007,
766 			    "Reappeared sess %p\n", sess);
767 		}
768 		ha->tgt.tgt_ops->update_sess(sess, fcport->d_id, fcport->loop_id,
769 					(fcport->flags & FCF_CONF_COMP_SUPPORTED));
770 	}
771 
772 	if (sess && sess->local) {
773 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04d,
774 		    "qla_target(%u): local session for "
775 		    "port %8phC (loop ID %d) became global\n", vha->vp_idx,
776 		    fcport->port_name, sess->loop_id);
777 		sess->local = 0;
778 	}
779 	ha->tgt.tgt_ops->put_sess(sess);
780 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
781 }
782 
783 void qlt_fc_port_deleted(struct scsi_qla_host *vha, fc_port_t *fcport)
784 {
785 	struct qla_hw_data *ha = vha->hw;
786 	struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
787 	struct qla_tgt_sess *sess;
788 	unsigned long flags;
789 
790 	if (!vha->hw->tgt.tgt_ops)
791 		return;
792 
793 	if (!tgt || (fcport->port_type != FCT_INITIATOR))
794 		return;
795 
796 	spin_lock_irqsave(&ha->hardware_lock, flags);
797 	if (tgt->tgt_stop) {
798 		spin_unlock_irqrestore(&ha->hardware_lock, flags);
799 		return;
800 	}
801 	sess = qlt_find_sess_by_port_name(tgt, fcport->port_name);
802 	if (!sess) {
803 		spin_unlock_irqrestore(&ha->hardware_lock, flags);
804 		return;
805 	}
806 
807 	ql_dbg(ql_dbg_tgt_mgt, vha, 0xf008, "qla_tgt_fc_port_deleted %p", sess);
808 
809 	sess->local = 1;
810 	qlt_schedule_sess_for_deletion(sess, false);
811 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
812 }
813 
814 static inline int test_tgt_sess_count(struct qla_tgt *tgt)
815 {
816 	struct qla_hw_data *ha = tgt->ha;
817 	unsigned long flags;
818 	int res;
819 	/*
820 	 * We need to protect against race, when tgt is freed before or
821 	 * inside wake_up()
822 	 */
823 	spin_lock_irqsave(&ha->hardware_lock, flags);
824 	ql_dbg(ql_dbg_tgt, tgt->vha, 0xe002,
825 	    "tgt %p, empty(sess_list)=%d sess_count=%d\n",
826 	    tgt, list_empty(&tgt->sess_list), tgt->sess_count);
827 	res = (tgt->sess_count == 0);
828 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
829 
830 	return res;
831 }
832 
833 /* Called by tcm_qla2xxx configfs code */
834 int qlt_stop_phase1(struct qla_tgt *tgt)
835 {
836 	struct scsi_qla_host *vha = tgt->vha;
837 	struct qla_hw_data *ha = tgt->ha;
838 	unsigned long flags;
839 
840 	mutex_lock(&qla_tgt_mutex);
841 	if (!vha->fc_vport) {
842 		struct Scsi_Host *sh = vha->host;
843 		struct fc_host_attrs *fc_host = shost_to_fc_host(sh);
844 		bool npiv_vports;
845 
846 		spin_lock_irqsave(sh->host_lock, flags);
847 		npiv_vports = (fc_host->npiv_vports_inuse);
848 		spin_unlock_irqrestore(sh->host_lock, flags);
849 
850 		if (npiv_vports) {
851 			mutex_unlock(&qla_tgt_mutex);
852 			return -EPERM;
853 		}
854 	}
855 	if (tgt->tgt_stop || tgt->tgt_stopped) {
856 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04e,
857 		    "Already in tgt->tgt_stop or tgt_stopped state\n");
858 		mutex_unlock(&qla_tgt_mutex);
859 		return -EPERM;
860 	}
861 
862 	ql_dbg(ql_dbg_tgt, vha, 0xe003, "Stopping target for host %ld(%p)\n",
863 	    vha->host_no, vha);
864 	/*
865 	 * Mutex needed to sync with qla_tgt_fc_port_[added,deleted].
866 	 * Lock is needed, because we still can get an incoming packet.
867 	 */
868 	mutex_lock(&vha->vha_tgt.tgt_mutex);
869 	spin_lock_irqsave(&ha->hardware_lock, flags);
870 	tgt->tgt_stop = 1;
871 	qlt_clear_tgt_db(tgt);
872 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
873 	mutex_unlock(&vha->vha_tgt.tgt_mutex);
874 	mutex_unlock(&qla_tgt_mutex);
875 
876 	flush_delayed_work(&tgt->sess_del_work);
877 
878 	ql_dbg(ql_dbg_tgt_mgt, vha, 0xf009,
879 	    "Waiting for sess works (tgt %p)", tgt);
880 	spin_lock_irqsave(&tgt->sess_work_lock, flags);
881 	while (!list_empty(&tgt->sess_works_list)) {
882 		spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
883 		flush_scheduled_work();
884 		spin_lock_irqsave(&tgt->sess_work_lock, flags);
885 	}
886 	spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
887 
888 	ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00a,
889 	    "Waiting for tgt %p: list_empty(sess_list)=%d "
890 	    "sess_count=%d\n", tgt, list_empty(&tgt->sess_list),
891 	    tgt->sess_count);
892 
893 	wait_event(tgt->waitQ, test_tgt_sess_count(tgt));
894 
895 	/* Big hammer */
896 	if (!ha->flags.host_shutting_down && qla_tgt_mode_enabled(vha))
897 		qlt_disable_vha(vha);
898 
899 	/* Wait for sessions to clear out (just in case) */
900 	wait_event(tgt->waitQ, test_tgt_sess_count(tgt));
901 	return 0;
902 }
903 EXPORT_SYMBOL(qlt_stop_phase1);
904 
905 /* Called by tcm_qla2xxx configfs code */
906 void qlt_stop_phase2(struct qla_tgt *tgt)
907 {
908 	struct qla_hw_data *ha = tgt->ha;
909 	scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
910 	unsigned long flags;
911 
912 	if (tgt->tgt_stopped) {
913 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04f,
914 		    "Already in tgt->tgt_stopped state\n");
915 		dump_stack();
916 		return;
917 	}
918 
919 	ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00b,
920 	    "Waiting for %d IRQ commands to complete (tgt %p)",
921 	    tgt->irq_cmd_count, tgt);
922 
923 	mutex_lock(&vha->vha_tgt.tgt_mutex);
924 	spin_lock_irqsave(&ha->hardware_lock, flags);
925 	while (tgt->irq_cmd_count != 0) {
926 		spin_unlock_irqrestore(&ha->hardware_lock, flags);
927 		udelay(2);
928 		spin_lock_irqsave(&ha->hardware_lock, flags);
929 	}
930 	tgt->tgt_stop = 0;
931 	tgt->tgt_stopped = 1;
932 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
933 	mutex_unlock(&vha->vha_tgt.tgt_mutex);
934 
935 	ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00c, "Stop of tgt %p finished",
936 	    tgt);
937 }
938 EXPORT_SYMBOL(qlt_stop_phase2);
939 
940 /* Called from qlt_remove_target() -> qla2x00_remove_one() */
941 static void qlt_release(struct qla_tgt *tgt)
942 {
943 	scsi_qla_host_t *vha = tgt->vha;
944 
945 	if ((vha->vha_tgt.qla_tgt != NULL) && !tgt->tgt_stopped)
946 		qlt_stop_phase2(tgt);
947 
948 	vha->vha_tgt.qla_tgt = NULL;
949 
950 	ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00d,
951 	    "Release of tgt %p finished\n", tgt);
952 
953 	kfree(tgt);
954 }
955 
956 /* ha->hardware_lock supposed to be held on entry */
957 static int qlt_sched_sess_work(struct qla_tgt *tgt, int type,
958 	const void *param, unsigned int param_size)
959 {
960 	struct qla_tgt_sess_work_param *prm;
961 	unsigned long flags;
962 
963 	prm = kzalloc(sizeof(*prm), GFP_ATOMIC);
964 	if (!prm) {
965 		ql_dbg(ql_dbg_tgt_mgt, tgt->vha, 0xf050,
966 		    "qla_target(%d): Unable to create session "
967 		    "work, command will be refused", 0);
968 		return -ENOMEM;
969 	}
970 
971 	ql_dbg(ql_dbg_tgt_mgt, tgt->vha, 0xf00e,
972 	    "Scheduling work (type %d, prm %p)"
973 	    " to find session for param %p (size %d, tgt %p)\n",
974 	    type, prm, param, param_size, tgt);
975 
976 	prm->type = type;
977 	memcpy(&prm->tm_iocb, param, param_size);
978 
979 	spin_lock_irqsave(&tgt->sess_work_lock, flags);
980 	list_add_tail(&prm->sess_works_list_entry, &tgt->sess_works_list);
981 	spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
982 
983 	schedule_work(&tgt->sess_work);
984 
985 	return 0;
986 }
987 
988 /*
989  * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
990  */
991 static void qlt_send_notify_ack(struct scsi_qla_host *vha,
992 	struct imm_ntfy_from_isp *ntfy,
993 	uint32_t add_flags, uint16_t resp_code, int resp_code_valid,
994 	uint16_t srr_flags, uint16_t srr_reject_code, uint8_t srr_explan)
995 {
996 	struct qla_hw_data *ha = vha->hw;
997 	request_t *pkt;
998 	struct nack_to_isp *nack;
999 
1000 	ql_dbg(ql_dbg_tgt, vha, 0xe004, "Sending NOTIFY_ACK (ha=%p)\n", ha);
1001 
1002 	/* Send marker if required */
1003 	if (qlt_issue_marker(vha, 1) != QLA_SUCCESS)
1004 		return;
1005 
1006 	pkt = (request_t *)qla2x00_alloc_iocbs(vha, NULL);
1007 	if (!pkt) {
1008 		ql_dbg(ql_dbg_tgt, vha, 0xe049,
1009 		    "qla_target(%d): %s failed: unable to allocate "
1010 		    "request packet\n", vha->vp_idx, __func__);
1011 		return;
1012 	}
1013 
1014 	if (vha->vha_tgt.qla_tgt != NULL)
1015 		vha->vha_tgt.qla_tgt->notify_ack_expected++;
1016 
1017 	pkt->entry_type = NOTIFY_ACK_TYPE;
1018 	pkt->entry_count = 1;
1019 
1020 	nack = (struct nack_to_isp *)pkt;
1021 	nack->ox_id = ntfy->ox_id;
1022 
1023 	nack->u.isp24.nport_handle = ntfy->u.isp24.nport_handle;
1024 	if (le16_to_cpu(ntfy->u.isp24.status) == IMM_NTFY_ELS) {
1025 		nack->u.isp24.flags = ntfy->u.isp24.flags &
1026 			__constant_cpu_to_le32(NOTIFY24XX_FLAGS_PUREX_IOCB);
1027 	}
1028 	nack->u.isp24.srr_rx_id = ntfy->u.isp24.srr_rx_id;
1029 	nack->u.isp24.status = ntfy->u.isp24.status;
1030 	nack->u.isp24.status_subcode = ntfy->u.isp24.status_subcode;
1031 	nack->u.isp24.fw_handle = ntfy->u.isp24.fw_handle;
1032 	nack->u.isp24.exchange_address = ntfy->u.isp24.exchange_address;
1033 	nack->u.isp24.srr_rel_offs = ntfy->u.isp24.srr_rel_offs;
1034 	nack->u.isp24.srr_ui = ntfy->u.isp24.srr_ui;
1035 	nack->u.isp24.srr_flags = cpu_to_le16(srr_flags);
1036 	nack->u.isp24.srr_reject_code = srr_reject_code;
1037 	nack->u.isp24.srr_reject_code_expl = srr_explan;
1038 	nack->u.isp24.vp_index = ntfy->u.isp24.vp_index;
1039 
1040 	ql_dbg(ql_dbg_tgt, vha, 0xe005,
1041 	    "qla_target(%d): Sending 24xx Notify Ack %d\n",
1042 	    vha->vp_idx, nack->u.isp24.status);
1043 
1044 	/* Memory Barrier */
1045 	wmb();
1046 	qla2x00_start_iocbs(vha, vha->req);
1047 }
1048 
1049 /*
1050  * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1051  */
1052 static void qlt_24xx_send_abts_resp(struct scsi_qla_host *vha,
1053 	struct abts_recv_from_24xx *abts, uint32_t status,
1054 	bool ids_reversed)
1055 {
1056 	struct qla_hw_data *ha = vha->hw;
1057 	struct abts_resp_to_24xx *resp;
1058 	uint32_t f_ctl;
1059 	uint8_t *p;
1060 
1061 	ql_dbg(ql_dbg_tgt, vha, 0xe006,
1062 	    "Sending task mgmt ABTS response (ha=%p, atio=%p, status=%x\n",
1063 	    ha, abts, status);
1064 
1065 	/* Send marker if required */
1066 	if (qlt_issue_marker(vha, 1) != QLA_SUCCESS)
1067 		return;
1068 
1069 	resp = (struct abts_resp_to_24xx *)qla2x00_alloc_iocbs_ready(vha, NULL);
1070 	if (!resp) {
1071 		ql_dbg(ql_dbg_tgt, vha, 0xe04a,
1072 		    "qla_target(%d): %s failed: unable to allocate "
1073 		    "request packet", vha->vp_idx, __func__);
1074 		return;
1075 	}
1076 
1077 	resp->entry_type = ABTS_RESP_24XX;
1078 	resp->entry_count = 1;
1079 	resp->nport_handle = abts->nport_handle;
1080 	resp->vp_index = vha->vp_idx;
1081 	resp->sof_type = abts->sof_type;
1082 	resp->exchange_address = abts->exchange_address;
1083 	resp->fcp_hdr_le = abts->fcp_hdr_le;
1084 	f_ctl = __constant_cpu_to_le32(F_CTL_EXCH_CONTEXT_RESP |
1085 	    F_CTL_LAST_SEQ | F_CTL_END_SEQ |
1086 	    F_CTL_SEQ_INITIATIVE);
1087 	p = (uint8_t *)&f_ctl;
1088 	resp->fcp_hdr_le.f_ctl[0] = *p++;
1089 	resp->fcp_hdr_le.f_ctl[1] = *p++;
1090 	resp->fcp_hdr_le.f_ctl[2] = *p;
1091 	if (ids_reversed) {
1092 		resp->fcp_hdr_le.d_id[0] = abts->fcp_hdr_le.d_id[0];
1093 		resp->fcp_hdr_le.d_id[1] = abts->fcp_hdr_le.d_id[1];
1094 		resp->fcp_hdr_le.d_id[2] = abts->fcp_hdr_le.d_id[2];
1095 		resp->fcp_hdr_le.s_id[0] = abts->fcp_hdr_le.s_id[0];
1096 		resp->fcp_hdr_le.s_id[1] = abts->fcp_hdr_le.s_id[1];
1097 		resp->fcp_hdr_le.s_id[2] = abts->fcp_hdr_le.s_id[2];
1098 	} else {
1099 		resp->fcp_hdr_le.d_id[0] = abts->fcp_hdr_le.s_id[0];
1100 		resp->fcp_hdr_le.d_id[1] = abts->fcp_hdr_le.s_id[1];
1101 		resp->fcp_hdr_le.d_id[2] = abts->fcp_hdr_le.s_id[2];
1102 		resp->fcp_hdr_le.s_id[0] = abts->fcp_hdr_le.d_id[0];
1103 		resp->fcp_hdr_le.s_id[1] = abts->fcp_hdr_le.d_id[1];
1104 		resp->fcp_hdr_le.s_id[2] = abts->fcp_hdr_le.d_id[2];
1105 	}
1106 	resp->exchange_addr_to_abort = abts->exchange_addr_to_abort;
1107 	if (status == FCP_TMF_CMPL) {
1108 		resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_ACC;
1109 		resp->payload.ba_acct.seq_id_valid = SEQ_ID_INVALID;
1110 		resp->payload.ba_acct.low_seq_cnt = 0x0000;
1111 		resp->payload.ba_acct.high_seq_cnt = 0xFFFF;
1112 		resp->payload.ba_acct.ox_id = abts->fcp_hdr_le.ox_id;
1113 		resp->payload.ba_acct.rx_id = abts->fcp_hdr_le.rx_id;
1114 	} else {
1115 		resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_RJT;
1116 		resp->payload.ba_rjt.reason_code =
1117 			BA_RJT_REASON_CODE_UNABLE_TO_PERFORM;
1118 		/* Other bytes are zero */
1119 	}
1120 
1121 	vha->vha_tgt.qla_tgt->abts_resp_expected++;
1122 
1123 	/* Memory Barrier */
1124 	wmb();
1125 	qla2x00_start_iocbs(vha, vha->req);
1126 }
1127 
1128 /*
1129  * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1130  */
1131 static void qlt_24xx_retry_term_exchange(struct scsi_qla_host *vha,
1132 	struct abts_resp_from_24xx_fw *entry)
1133 {
1134 	struct ctio7_to_24xx *ctio;
1135 
1136 	ql_dbg(ql_dbg_tgt, vha, 0xe007,
1137 	    "Sending retry TERM EXCH CTIO7 (ha=%p)\n", vha->hw);
1138 	/* Send marker if required */
1139 	if (qlt_issue_marker(vha, 1) != QLA_SUCCESS)
1140 		return;
1141 
1142 	ctio = (struct ctio7_to_24xx *)qla2x00_alloc_iocbs_ready(vha, NULL);
1143 	if (ctio == NULL) {
1144 		ql_dbg(ql_dbg_tgt, vha, 0xe04b,
1145 		    "qla_target(%d): %s failed: unable to allocate "
1146 		    "request packet\n", vha->vp_idx, __func__);
1147 		return;
1148 	}
1149 
1150 	/*
1151 	 * We've got on entrance firmware's response on by us generated
1152 	 * ABTS response. So, in it ID fields are reversed.
1153 	 */
1154 
1155 	ctio->entry_type = CTIO_TYPE7;
1156 	ctio->entry_count = 1;
1157 	ctio->nport_handle = entry->nport_handle;
1158 	ctio->handle = QLA_TGT_SKIP_HANDLE |	CTIO_COMPLETION_HANDLE_MARK;
1159 	ctio->timeout = __constant_cpu_to_le16(QLA_TGT_TIMEOUT);
1160 	ctio->vp_index = vha->vp_idx;
1161 	ctio->initiator_id[0] = entry->fcp_hdr_le.d_id[0];
1162 	ctio->initiator_id[1] = entry->fcp_hdr_le.d_id[1];
1163 	ctio->initiator_id[2] = entry->fcp_hdr_le.d_id[2];
1164 	ctio->exchange_addr = entry->exchange_addr_to_abort;
1165 	ctio->u.status1.flags =
1166 	    __constant_cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1 |
1167 		CTIO7_FLAGS_TERMINATE);
1168 	ctio->u.status1.ox_id = cpu_to_le16(entry->fcp_hdr_le.ox_id);
1169 
1170 	/* Memory Barrier */
1171 	wmb();
1172 	qla2x00_start_iocbs(vha, vha->req);
1173 
1174 	qlt_24xx_send_abts_resp(vha, (struct abts_recv_from_24xx *)entry,
1175 	    FCP_TMF_CMPL, true);
1176 }
1177 
1178 /* ha->hardware_lock supposed to be held on entry */
1179 static int __qlt_24xx_handle_abts(struct scsi_qla_host *vha,
1180 	struct abts_recv_from_24xx *abts, struct qla_tgt_sess *sess)
1181 {
1182 	struct qla_hw_data *ha = vha->hw;
1183 	struct se_session *se_sess = sess->se_sess;
1184 	struct qla_tgt_mgmt_cmd *mcmd;
1185 	struct se_cmd *se_cmd;
1186 	u32 lun = 0;
1187 	int rc;
1188 	bool found_lun = false;
1189 
1190 	spin_lock(&se_sess->sess_cmd_lock);
1191 	list_for_each_entry(se_cmd, &se_sess->sess_cmd_list, se_cmd_list) {
1192 		struct qla_tgt_cmd *cmd =
1193 			container_of(se_cmd, struct qla_tgt_cmd, se_cmd);
1194 		if (se_cmd->tag == abts->exchange_addr_to_abort) {
1195 			lun = cmd->unpacked_lun;
1196 			found_lun = true;
1197 			break;
1198 		}
1199 	}
1200 	spin_unlock(&se_sess->sess_cmd_lock);
1201 
1202 	if (!found_lun)
1203 		return -ENOENT;
1204 
1205 	ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00f,
1206 	    "qla_target(%d): task abort (tag=%d)\n",
1207 	    vha->vp_idx, abts->exchange_addr_to_abort);
1208 
1209 	mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
1210 	if (mcmd == NULL) {
1211 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf051,
1212 		    "qla_target(%d): %s: Allocation of ABORT cmd failed",
1213 		    vha->vp_idx, __func__);
1214 		return -ENOMEM;
1215 	}
1216 	memset(mcmd, 0, sizeof(*mcmd));
1217 
1218 	mcmd->sess = sess;
1219 	memcpy(&mcmd->orig_iocb.abts, abts, sizeof(mcmd->orig_iocb.abts));
1220 	mcmd->reset_count = vha->hw->chip_reset;
1221 
1222 	rc = ha->tgt.tgt_ops->handle_tmr(mcmd, lun, TMR_ABORT_TASK,
1223 	    abts->exchange_addr_to_abort);
1224 	if (rc != 0) {
1225 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf052,
1226 		    "qla_target(%d):  tgt_ops->handle_tmr()"
1227 		    " failed: %d", vha->vp_idx, rc);
1228 		mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
1229 		return -EFAULT;
1230 	}
1231 
1232 	return 0;
1233 }
1234 
1235 /*
1236  * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1237  */
1238 static void qlt_24xx_handle_abts(struct scsi_qla_host *vha,
1239 	struct abts_recv_from_24xx *abts)
1240 {
1241 	struct qla_hw_data *ha = vha->hw;
1242 	struct qla_tgt_sess *sess;
1243 	uint32_t tag = abts->exchange_addr_to_abort;
1244 	uint8_t s_id[3];
1245 	int rc;
1246 
1247 	if (le32_to_cpu(abts->fcp_hdr_le.parameter) & ABTS_PARAM_ABORT_SEQ) {
1248 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf053,
1249 		    "qla_target(%d): ABTS: Abort Sequence not "
1250 		    "supported\n", vha->vp_idx);
1251 		qlt_24xx_send_abts_resp(vha, abts, FCP_TMF_REJECTED, false);
1252 		return;
1253 	}
1254 
1255 	if (tag == ATIO_EXCHANGE_ADDRESS_UNKNOWN) {
1256 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf010,
1257 		    "qla_target(%d): ABTS: Unknown Exchange "
1258 		    "Address received\n", vha->vp_idx);
1259 		qlt_24xx_send_abts_resp(vha, abts, FCP_TMF_REJECTED, false);
1260 		return;
1261 	}
1262 
1263 	ql_dbg(ql_dbg_tgt_mgt, vha, 0xf011,
1264 	    "qla_target(%d): task abort (s_id=%x:%x:%x, "
1265 	    "tag=%d, param=%x)\n", vha->vp_idx, abts->fcp_hdr_le.s_id[2],
1266 	    abts->fcp_hdr_le.s_id[1], abts->fcp_hdr_le.s_id[0], tag,
1267 	    le32_to_cpu(abts->fcp_hdr_le.parameter));
1268 
1269 	s_id[0] = abts->fcp_hdr_le.s_id[2];
1270 	s_id[1] = abts->fcp_hdr_le.s_id[1];
1271 	s_id[2] = abts->fcp_hdr_le.s_id[0];
1272 
1273 	sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id);
1274 	if (!sess) {
1275 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf012,
1276 		    "qla_target(%d): task abort for non-existant session\n",
1277 		    vha->vp_idx);
1278 		rc = qlt_sched_sess_work(vha->vha_tgt.qla_tgt,
1279 		    QLA_TGT_SESS_WORK_ABORT, abts, sizeof(*abts));
1280 		if (rc != 0) {
1281 			qlt_24xx_send_abts_resp(vha, abts, FCP_TMF_REJECTED,
1282 			    false);
1283 		}
1284 		return;
1285 	}
1286 
1287 	rc = __qlt_24xx_handle_abts(vha, abts, sess);
1288 	if (rc != 0) {
1289 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf054,
1290 		    "qla_target(%d): __qlt_24xx_handle_abts() failed: %d\n",
1291 		    vha->vp_idx, rc);
1292 		qlt_24xx_send_abts_resp(vha, abts, FCP_TMF_REJECTED, false);
1293 		return;
1294 	}
1295 }
1296 
1297 /*
1298  * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1299  */
1300 static void qlt_24xx_send_task_mgmt_ctio(struct scsi_qla_host *ha,
1301 	struct qla_tgt_mgmt_cmd *mcmd, uint32_t resp_code)
1302 {
1303 	struct atio_from_isp *atio = &mcmd->orig_iocb.atio;
1304 	struct ctio7_to_24xx *ctio;
1305 	uint16_t temp;
1306 
1307 	ql_dbg(ql_dbg_tgt, ha, 0xe008,
1308 	    "Sending task mgmt CTIO7 (ha=%p, atio=%p, resp_code=%x\n",
1309 	    ha, atio, resp_code);
1310 
1311 	/* Send marker if required */
1312 	if (qlt_issue_marker(ha, 1) != QLA_SUCCESS)
1313 		return;
1314 
1315 	ctio = (struct ctio7_to_24xx *)qla2x00_alloc_iocbs(ha, NULL);
1316 	if (ctio == NULL) {
1317 		ql_dbg(ql_dbg_tgt, ha, 0xe04c,
1318 		    "qla_target(%d): %s failed: unable to allocate "
1319 		    "request packet\n", ha->vp_idx, __func__);
1320 		return;
1321 	}
1322 
1323 	ctio->entry_type = CTIO_TYPE7;
1324 	ctio->entry_count = 1;
1325 	ctio->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
1326 	ctio->nport_handle = mcmd->sess->loop_id;
1327 	ctio->timeout = __constant_cpu_to_le16(QLA_TGT_TIMEOUT);
1328 	ctio->vp_index = ha->vp_idx;
1329 	ctio->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
1330 	ctio->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
1331 	ctio->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
1332 	ctio->exchange_addr = atio->u.isp24.exchange_addr;
1333 	ctio->u.status1.flags = (atio->u.isp24.attr << 9) |
1334 	    __constant_cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1 |
1335 		CTIO7_FLAGS_SEND_STATUS);
1336 	temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
1337 	ctio->u.status1.ox_id = cpu_to_le16(temp);
1338 	ctio->u.status1.scsi_status =
1339 	    __constant_cpu_to_le16(SS_RESPONSE_INFO_LEN_VALID);
1340 	ctio->u.status1.response_len = __constant_cpu_to_le16(8);
1341 	ctio->u.status1.sense_data[0] = resp_code;
1342 
1343 	/* Memory Barrier */
1344 	wmb();
1345 	qla2x00_start_iocbs(ha, ha->req);
1346 }
1347 
1348 void qlt_free_mcmd(struct qla_tgt_mgmt_cmd *mcmd)
1349 {
1350 	mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
1351 }
1352 EXPORT_SYMBOL(qlt_free_mcmd);
1353 
1354 /* callback from target fabric module code */
1355 void qlt_xmit_tm_rsp(struct qla_tgt_mgmt_cmd *mcmd)
1356 {
1357 	struct scsi_qla_host *vha = mcmd->sess->vha;
1358 	struct qla_hw_data *ha = vha->hw;
1359 	unsigned long flags;
1360 
1361 	ql_dbg(ql_dbg_tgt_mgt, vha, 0xf013,
1362 	    "TM response mcmd (%p) status %#x state %#x",
1363 	    mcmd, mcmd->fc_tm_rsp, mcmd->flags);
1364 
1365 	spin_lock_irqsave(&ha->hardware_lock, flags);
1366 
1367 	if (qla2x00_reset_active(vha) || mcmd->reset_count != ha->chip_reset) {
1368 		/*
1369 		 * Either a chip reset is active or this request was from
1370 		 * previous life, just abort the processing.
1371 		 */
1372 		ql_dbg(ql_dbg_async, vha, 0xe100,
1373 			"RESET-TMR active/old-count/new-count = %d/%d/%d.\n",
1374 			qla2x00_reset_active(vha), mcmd->reset_count,
1375 			ha->chip_reset);
1376 		ha->tgt.tgt_ops->free_mcmd(mcmd);
1377 		spin_unlock_irqrestore(&ha->hardware_lock, flags);
1378 		return;
1379 	}
1380 
1381 	if (mcmd->flags == QLA24XX_MGMT_SEND_NACK)
1382 		qlt_send_notify_ack(vha, &mcmd->orig_iocb.imm_ntfy,
1383 		    0, 0, 0, 0, 0, 0);
1384 	else {
1385 		if (mcmd->se_cmd.se_tmr_req->function == TMR_ABORT_TASK)
1386 			qlt_24xx_send_abts_resp(vha, &mcmd->orig_iocb.abts,
1387 			    mcmd->fc_tm_rsp, false);
1388 		else
1389 			qlt_24xx_send_task_mgmt_ctio(vha, mcmd,
1390 			    mcmd->fc_tm_rsp);
1391 	}
1392 	/*
1393 	 * Make the callback for ->free_mcmd() to queue_work() and invoke
1394 	 * target_put_sess_cmd() to drop cmd_kref to 1.  The final
1395 	 * target_put_sess_cmd() call will be made from TFO->check_stop_free()
1396 	 * -> tcm_qla2xxx_check_stop_free() to release the TMR associated se_cmd
1397 	 * descriptor after TFO->queue_tm_rsp() -> tcm_qla2xxx_queue_tm_rsp() ->
1398 	 * qlt_xmit_tm_rsp() returns here..
1399 	 */
1400 	ha->tgt.tgt_ops->free_mcmd(mcmd);
1401 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
1402 }
1403 EXPORT_SYMBOL(qlt_xmit_tm_rsp);
1404 
1405 /* No locks */
1406 static int qlt_pci_map_calc_cnt(struct qla_tgt_prm *prm)
1407 {
1408 	struct qla_tgt_cmd *cmd = prm->cmd;
1409 
1410 	BUG_ON(cmd->sg_cnt == 0);
1411 
1412 	prm->sg = (struct scatterlist *)cmd->sg;
1413 	prm->seg_cnt = pci_map_sg(prm->tgt->ha->pdev, cmd->sg,
1414 	    cmd->sg_cnt, cmd->dma_data_direction);
1415 	if (unlikely(prm->seg_cnt == 0))
1416 		goto out_err;
1417 
1418 	prm->cmd->sg_mapped = 1;
1419 
1420 	if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL) {
1421 		/*
1422 		 * If greater than four sg entries then we need to allocate
1423 		 * the continuation entries
1424 		 */
1425 		if (prm->seg_cnt > prm->tgt->datasegs_per_cmd)
1426 			prm->req_cnt += DIV_ROUND_UP(prm->seg_cnt -
1427 			prm->tgt->datasegs_per_cmd,
1428 			prm->tgt->datasegs_per_cont);
1429 	} else {
1430 		/* DIF */
1431 		if ((cmd->se_cmd.prot_op == TARGET_PROT_DIN_INSERT) ||
1432 		    (cmd->se_cmd.prot_op == TARGET_PROT_DOUT_STRIP)) {
1433 			prm->seg_cnt = DIV_ROUND_UP(cmd->bufflen, cmd->blk_sz);
1434 			prm->tot_dsds = prm->seg_cnt;
1435 		} else
1436 			prm->tot_dsds = prm->seg_cnt;
1437 
1438 		if (cmd->prot_sg_cnt) {
1439 			prm->prot_sg      = cmd->prot_sg;
1440 			prm->prot_seg_cnt = pci_map_sg(prm->tgt->ha->pdev,
1441 				cmd->prot_sg, cmd->prot_sg_cnt,
1442 				cmd->dma_data_direction);
1443 			if (unlikely(prm->prot_seg_cnt == 0))
1444 				goto out_err;
1445 
1446 			if ((cmd->se_cmd.prot_op == TARGET_PROT_DIN_INSERT) ||
1447 			    (cmd->se_cmd.prot_op == TARGET_PROT_DOUT_STRIP)) {
1448 				/* Dif Bundling not support here */
1449 				prm->prot_seg_cnt = DIV_ROUND_UP(cmd->bufflen,
1450 								cmd->blk_sz);
1451 				prm->tot_dsds += prm->prot_seg_cnt;
1452 			} else
1453 				prm->tot_dsds += prm->prot_seg_cnt;
1454 		}
1455 	}
1456 
1457 	return 0;
1458 
1459 out_err:
1460 	ql_dbg(ql_dbg_tgt, prm->cmd->vha, 0xe04d,
1461 	    "qla_target(%d): PCI mapping failed: sg_cnt=%d",
1462 	    0, prm->cmd->sg_cnt);
1463 	return -1;
1464 }
1465 
1466 static void qlt_unmap_sg(struct scsi_qla_host *vha, struct qla_tgt_cmd *cmd)
1467 {
1468 	struct qla_hw_data *ha = vha->hw;
1469 
1470 	if (!cmd->sg_mapped)
1471 		return;
1472 
1473 	pci_unmap_sg(ha->pdev, cmd->sg, cmd->sg_cnt, cmd->dma_data_direction);
1474 	cmd->sg_mapped = 0;
1475 
1476 	if (cmd->prot_sg_cnt)
1477 		pci_unmap_sg(ha->pdev, cmd->prot_sg, cmd->prot_sg_cnt,
1478 			cmd->dma_data_direction);
1479 
1480 	if (cmd->ctx_dsd_alloced)
1481 		qla2x00_clean_dsd_pool(ha, NULL, cmd);
1482 
1483 	if (cmd->ctx)
1484 		dma_pool_free(ha->dl_dma_pool, cmd->ctx, cmd->ctx->crc_ctx_dma);
1485 }
1486 
1487 static int qlt_check_reserve_free_req(struct scsi_qla_host *vha,
1488 	uint32_t req_cnt)
1489 {
1490 	uint32_t cnt, cnt_in;
1491 
1492 	if (vha->req->cnt < (req_cnt + 2)) {
1493 		cnt = (uint16_t)RD_REG_DWORD(vha->req->req_q_out);
1494 		cnt_in = (uint16_t)RD_REG_DWORD(vha->req->req_q_in);
1495 
1496 		if  (vha->req->ring_index < cnt)
1497 			vha->req->cnt = cnt - vha->req->ring_index;
1498 		else
1499 			vha->req->cnt = vha->req->length -
1500 			    (vha->req->ring_index - cnt);
1501 	}
1502 
1503 	if (unlikely(vha->req->cnt < (req_cnt + 2))) {
1504 		ql_dbg(ql_dbg_io, vha, 0x305a,
1505 		    "qla_target(%d): There is no room in the request ring: vha->req->ring_index=%d, vha->req->cnt=%d, req_cnt=%d Req-out=%d Req-in=%d Req-Length=%d\n",
1506 		    vha->vp_idx, vha->req->ring_index,
1507 		    vha->req->cnt, req_cnt, cnt, cnt_in, vha->req->length);
1508 		return -EAGAIN;
1509 	}
1510 	vha->req->cnt -= req_cnt;
1511 
1512 	return 0;
1513 }
1514 
1515 /*
1516  * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1517  */
1518 static inline void *qlt_get_req_pkt(struct scsi_qla_host *vha)
1519 {
1520 	/* Adjust ring index. */
1521 	vha->req->ring_index++;
1522 	if (vha->req->ring_index == vha->req->length) {
1523 		vha->req->ring_index = 0;
1524 		vha->req->ring_ptr = vha->req->ring;
1525 	} else {
1526 		vha->req->ring_ptr++;
1527 	}
1528 	return (cont_entry_t *)vha->req->ring_ptr;
1529 }
1530 
1531 /* ha->hardware_lock supposed to be held on entry */
1532 static inline uint32_t qlt_make_handle(struct scsi_qla_host *vha)
1533 {
1534 	struct qla_hw_data *ha = vha->hw;
1535 	uint32_t h;
1536 
1537 	h = ha->tgt.current_handle;
1538 	/* always increment cmd handle */
1539 	do {
1540 		++h;
1541 		if (h > DEFAULT_OUTSTANDING_COMMANDS)
1542 			h = 1; /* 0 is QLA_TGT_NULL_HANDLE */
1543 		if (h == ha->tgt.current_handle) {
1544 			ql_dbg(ql_dbg_io, vha, 0x305b,
1545 			    "qla_target(%d): Ran out of "
1546 			    "empty cmd slots in ha %p\n", vha->vp_idx, ha);
1547 			h = QLA_TGT_NULL_HANDLE;
1548 			break;
1549 		}
1550 	} while ((h == QLA_TGT_NULL_HANDLE) ||
1551 	    (h == QLA_TGT_SKIP_HANDLE) ||
1552 	    (ha->tgt.cmds[h-1] != NULL));
1553 
1554 	if (h != QLA_TGT_NULL_HANDLE)
1555 		ha->tgt.current_handle = h;
1556 
1557 	return h;
1558 }
1559 
1560 /* ha->hardware_lock supposed to be held on entry */
1561 static int qlt_24xx_build_ctio_pkt(struct qla_tgt_prm *prm,
1562 	struct scsi_qla_host *vha)
1563 {
1564 	uint32_t h;
1565 	struct ctio7_to_24xx *pkt;
1566 	struct qla_hw_data *ha = vha->hw;
1567 	struct atio_from_isp *atio = &prm->cmd->atio;
1568 	uint16_t temp;
1569 
1570 	pkt = (struct ctio7_to_24xx *)vha->req->ring_ptr;
1571 	prm->pkt = pkt;
1572 	memset(pkt, 0, sizeof(*pkt));
1573 
1574 	pkt->entry_type = CTIO_TYPE7;
1575 	pkt->entry_count = (uint8_t)prm->req_cnt;
1576 	pkt->vp_index = vha->vp_idx;
1577 
1578 	h = qlt_make_handle(vha);
1579 	if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
1580 		/*
1581 		 * CTIO type 7 from the firmware doesn't provide a way to
1582 		 * know the initiator's LOOP ID, hence we can't find
1583 		 * the session and, so, the command.
1584 		 */
1585 		return -EAGAIN;
1586 	} else
1587 		ha->tgt.cmds[h-1] = prm->cmd;
1588 
1589 	pkt->handle = h | CTIO_COMPLETION_HANDLE_MARK;
1590 	pkt->nport_handle = prm->cmd->loop_id;
1591 	pkt->timeout = __constant_cpu_to_le16(QLA_TGT_TIMEOUT);
1592 	pkt->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
1593 	pkt->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
1594 	pkt->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
1595 	pkt->exchange_addr = atio->u.isp24.exchange_addr;
1596 	pkt->u.status0.flags |= (atio->u.isp24.attr << 9);
1597 	temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
1598 	pkt->u.status0.ox_id = cpu_to_le16(temp);
1599 	pkt->u.status0.relative_offset = cpu_to_le32(prm->cmd->offset);
1600 
1601 	return 0;
1602 }
1603 
1604 /*
1605  * ha->hardware_lock supposed to be held on entry. We have already made sure
1606  * that there is sufficient amount of request entries to not drop it.
1607  */
1608 static void qlt_load_cont_data_segments(struct qla_tgt_prm *prm,
1609 	struct scsi_qla_host *vha)
1610 {
1611 	int cnt;
1612 	uint32_t *dword_ptr;
1613 	int enable_64bit_addressing = prm->tgt->tgt_enable_64bit_addr;
1614 
1615 	/* Build continuation packets */
1616 	while (prm->seg_cnt > 0) {
1617 		cont_a64_entry_t *cont_pkt64 =
1618 			(cont_a64_entry_t *)qlt_get_req_pkt(vha);
1619 
1620 		/*
1621 		 * Make sure that from cont_pkt64 none of
1622 		 * 64-bit specific fields used for 32-bit
1623 		 * addressing. Cast to (cont_entry_t *) for
1624 		 * that.
1625 		 */
1626 
1627 		memset(cont_pkt64, 0, sizeof(*cont_pkt64));
1628 
1629 		cont_pkt64->entry_count = 1;
1630 		cont_pkt64->sys_define = 0;
1631 
1632 		if (enable_64bit_addressing) {
1633 			cont_pkt64->entry_type = CONTINUE_A64_TYPE;
1634 			dword_ptr =
1635 			    (uint32_t *)&cont_pkt64->dseg_0_address;
1636 		} else {
1637 			cont_pkt64->entry_type = CONTINUE_TYPE;
1638 			dword_ptr =
1639 			    (uint32_t *)&((cont_entry_t *)
1640 				cont_pkt64)->dseg_0_address;
1641 		}
1642 
1643 		/* Load continuation entry data segments */
1644 		for (cnt = 0;
1645 		    cnt < prm->tgt->datasegs_per_cont && prm->seg_cnt;
1646 		    cnt++, prm->seg_cnt--) {
1647 			*dword_ptr++ =
1648 			    cpu_to_le32(pci_dma_lo32
1649 				(sg_dma_address(prm->sg)));
1650 			if (enable_64bit_addressing) {
1651 				*dword_ptr++ =
1652 				    cpu_to_le32(pci_dma_hi32
1653 					(sg_dma_address
1654 					(prm->sg)));
1655 			}
1656 			*dword_ptr++ = cpu_to_le32(sg_dma_len(prm->sg));
1657 
1658 			prm->sg = sg_next(prm->sg);
1659 		}
1660 	}
1661 }
1662 
1663 /*
1664  * ha->hardware_lock supposed to be held on entry. We have already made sure
1665  * that there is sufficient amount of request entries to not drop it.
1666  */
1667 static void qlt_load_data_segments(struct qla_tgt_prm *prm,
1668 	struct scsi_qla_host *vha)
1669 {
1670 	int cnt;
1671 	uint32_t *dword_ptr;
1672 	int enable_64bit_addressing = prm->tgt->tgt_enable_64bit_addr;
1673 	struct ctio7_to_24xx *pkt24 = (struct ctio7_to_24xx *)prm->pkt;
1674 
1675 	pkt24->u.status0.transfer_length = cpu_to_le32(prm->cmd->bufflen);
1676 
1677 	/* Setup packet address segment pointer */
1678 	dword_ptr = pkt24->u.status0.dseg_0_address;
1679 
1680 	/* Set total data segment count */
1681 	if (prm->seg_cnt)
1682 		pkt24->dseg_count = cpu_to_le16(prm->seg_cnt);
1683 
1684 	if (prm->seg_cnt == 0) {
1685 		/* No data transfer */
1686 		*dword_ptr++ = 0;
1687 		*dword_ptr = 0;
1688 		return;
1689 	}
1690 
1691 	/* If scatter gather */
1692 
1693 	/* Load command entry data segments */
1694 	for (cnt = 0;
1695 	    (cnt < prm->tgt->datasegs_per_cmd) && prm->seg_cnt;
1696 	    cnt++, prm->seg_cnt--) {
1697 		*dword_ptr++ =
1698 		    cpu_to_le32(pci_dma_lo32(sg_dma_address(prm->sg)));
1699 		if (enable_64bit_addressing) {
1700 			*dword_ptr++ =
1701 			    cpu_to_le32(pci_dma_hi32(
1702 				sg_dma_address(prm->sg)));
1703 		}
1704 		*dword_ptr++ = cpu_to_le32(sg_dma_len(prm->sg));
1705 
1706 		prm->sg = sg_next(prm->sg);
1707 	}
1708 
1709 	qlt_load_cont_data_segments(prm, vha);
1710 }
1711 
1712 static inline int qlt_has_data(struct qla_tgt_cmd *cmd)
1713 {
1714 	return cmd->bufflen > 0;
1715 }
1716 
1717 /*
1718  * Called without ha->hardware_lock held
1719  */
1720 static int qlt_pre_xmit_response(struct qla_tgt_cmd *cmd,
1721 	struct qla_tgt_prm *prm, int xmit_type, uint8_t scsi_status,
1722 	uint32_t *full_req_cnt)
1723 {
1724 	struct qla_tgt *tgt = cmd->tgt;
1725 	struct scsi_qla_host *vha = tgt->vha;
1726 	struct qla_hw_data *ha = vha->hw;
1727 	struct se_cmd *se_cmd = &cmd->se_cmd;
1728 
1729 	if (unlikely(cmd->aborted)) {
1730 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf014,
1731 		       "qla_target(%d): terminating exchange for aborted cmd=%p (se_cmd=%p, tag=%lld)",
1732 		       vha->vp_idx, cmd, se_cmd, se_cmd->tag);
1733 
1734 		cmd->state = QLA_TGT_STATE_ABORTED;
1735 		cmd->cmd_flags |= BIT_6;
1736 
1737 		qlt_send_term_exchange(vha, cmd, &cmd->atio, 0);
1738 
1739 		/* !! At this point cmd could be already freed !! */
1740 		return QLA_TGT_PRE_XMIT_RESP_CMD_ABORTED;
1741 	}
1742 
1743 	prm->cmd = cmd;
1744 	prm->tgt = tgt;
1745 	prm->rq_result = scsi_status;
1746 	prm->sense_buffer = &cmd->sense_buffer[0];
1747 	prm->sense_buffer_len = TRANSPORT_SENSE_BUFFER;
1748 	prm->sg = NULL;
1749 	prm->seg_cnt = -1;
1750 	prm->req_cnt = 1;
1751 	prm->add_status_pkt = 0;
1752 
1753 	/* Send marker if required */
1754 	if (qlt_issue_marker(vha, 0) != QLA_SUCCESS)
1755 		return -EFAULT;
1756 
1757 	if ((xmit_type & QLA_TGT_XMIT_DATA) && qlt_has_data(cmd)) {
1758 		if  (qlt_pci_map_calc_cnt(prm) != 0)
1759 			return -EAGAIN;
1760 	}
1761 
1762 	*full_req_cnt = prm->req_cnt;
1763 
1764 	if (se_cmd->se_cmd_flags & SCF_UNDERFLOW_BIT) {
1765 		prm->residual = se_cmd->residual_count;
1766 		ql_dbg(ql_dbg_io + ql_dbg_verbose, vha, 0x305c,
1767 		    "Residual underflow: %d (tag %lld, op %x, bufflen %d, rq_result %x)\n",
1768 		       prm->residual, se_cmd->tag,
1769 		       se_cmd->t_task_cdb ? se_cmd->t_task_cdb[0] : 0,
1770 		       cmd->bufflen, prm->rq_result);
1771 		prm->rq_result |= SS_RESIDUAL_UNDER;
1772 	} else if (se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT) {
1773 		prm->residual = se_cmd->residual_count;
1774 		ql_dbg(ql_dbg_io, vha, 0x305d,
1775 		    "Residual overflow: %d (tag %lld, op %x, bufflen %d, rq_result %x)\n",
1776 		       prm->residual, se_cmd->tag, se_cmd->t_task_cdb ?
1777 		       se_cmd->t_task_cdb[0] : 0, cmd->bufflen, prm->rq_result);
1778 		prm->rq_result |= SS_RESIDUAL_OVER;
1779 	}
1780 
1781 	if (xmit_type & QLA_TGT_XMIT_STATUS) {
1782 		/*
1783 		 * If QLA_TGT_XMIT_DATA is not set, add_status_pkt will be
1784 		 * ignored in *xmit_response() below
1785 		 */
1786 		if (qlt_has_data(cmd)) {
1787 			if (QLA_TGT_SENSE_VALID(prm->sense_buffer) ||
1788 			    (IS_FWI2_CAPABLE(ha) &&
1789 			    (prm->rq_result != 0))) {
1790 				prm->add_status_pkt = 1;
1791 				(*full_req_cnt)++;
1792 			}
1793 		}
1794 	}
1795 
1796 	return 0;
1797 }
1798 
1799 static inline int qlt_need_explicit_conf(struct qla_hw_data *ha,
1800 	struct qla_tgt_cmd *cmd, int sending_sense)
1801 {
1802 	if (ha->tgt.enable_class_2)
1803 		return 0;
1804 
1805 	if (sending_sense)
1806 		return cmd->conf_compl_supported;
1807 	else
1808 		return ha->tgt.enable_explicit_conf &&
1809 		    cmd->conf_compl_supported;
1810 }
1811 
1812 #ifdef CONFIG_QLA_TGT_DEBUG_SRR
1813 /*
1814  *  Original taken from the XFS code
1815  */
1816 static unsigned long qlt_srr_random(void)
1817 {
1818 	static int Inited;
1819 	static unsigned long RandomValue;
1820 	static DEFINE_SPINLOCK(lock);
1821 	/* cycles pseudo-randomly through all values between 1 and 2^31 - 2 */
1822 	register long rv;
1823 	register long lo;
1824 	register long hi;
1825 	unsigned long flags;
1826 
1827 	spin_lock_irqsave(&lock, flags);
1828 	if (!Inited) {
1829 		RandomValue = jiffies;
1830 		Inited = 1;
1831 	}
1832 	rv = RandomValue;
1833 	hi = rv / 127773;
1834 	lo = rv % 127773;
1835 	rv = 16807 * lo - 2836 * hi;
1836 	if (rv <= 0)
1837 		rv += 2147483647;
1838 	RandomValue = rv;
1839 	spin_unlock_irqrestore(&lock, flags);
1840 	return rv;
1841 }
1842 
1843 static void qlt_check_srr_debug(struct qla_tgt_cmd *cmd, int *xmit_type)
1844 {
1845 #if 0 /* This is not a real status packets lost, so it won't lead to SRR */
1846 	if ((*xmit_type & QLA_TGT_XMIT_STATUS) && (qlt_srr_random() % 200)
1847 	    == 50) {
1848 		*xmit_type &= ~QLA_TGT_XMIT_STATUS;
1849 		ql_dbg(ql_dbg_tgt_mgt, cmd->vha, 0xf015,
1850 		    "Dropping cmd %p (tag %d) status", cmd, se_cmd->tag);
1851 	}
1852 #endif
1853 	/*
1854 	 * It's currently not possible to simulate SRRs for FCP_WRITE without
1855 	 * a physical link layer failure, so don't even try here..
1856 	 */
1857 	if (cmd->dma_data_direction != DMA_FROM_DEVICE)
1858 		return;
1859 
1860 	if (qlt_has_data(cmd) && (cmd->sg_cnt > 1) &&
1861 	    ((qlt_srr_random() % 100) == 20)) {
1862 		int i, leave = 0;
1863 		unsigned int tot_len = 0;
1864 
1865 		while (leave == 0)
1866 			leave = qlt_srr_random() % cmd->sg_cnt;
1867 
1868 		for (i = 0; i < leave; i++)
1869 			tot_len += cmd->sg[i].length;
1870 
1871 		ql_dbg(ql_dbg_tgt_mgt, cmd->vha, 0xf016,
1872 		    "Cutting cmd %p (tag %d) buffer"
1873 		    " tail to len %d, sg_cnt %d (cmd->bufflen %d,"
1874 		    " cmd->sg_cnt %d)", cmd, se_cmd->tag, tot_len, leave,
1875 		    cmd->bufflen, cmd->sg_cnt);
1876 
1877 		cmd->bufflen = tot_len;
1878 		cmd->sg_cnt = leave;
1879 	}
1880 
1881 	if (qlt_has_data(cmd) && ((qlt_srr_random() % 100) == 70)) {
1882 		unsigned int offset = qlt_srr_random() % cmd->bufflen;
1883 
1884 		ql_dbg(ql_dbg_tgt_mgt, cmd->vha, 0xf017,
1885 		    "Cutting cmd %p (tag %d) buffer head "
1886 		    "to offset %d (cmd->bufflen %d)", cmd, se_cmd->tag, offset,
1887 		    cmd->bufflen);
1888 		if (offset == 0)
1889 			*xmit_type &= ~QLA_TGT_XMIT_DATA;
1890 		else if (qlt_set_data_offset(cmd, offset)) {
1891 			ql_dbg(ql_dbg_tgt_mgt, cmd->vha, 0xf018,
1892 			    "qlt_set_data_offset() failed (tag %d)", se_cmd->tag);
1893 		}
1894 	}
1895 }
1896 #else
1897 static inline void qlt_check_srr_debug(struct qla_tgt_cmd *cmd, int *xmit_type)
1898 {}
1899 #endif
1900 
1901 static void qlt_24xx_init_ctio_to_isp(struct ctio7_to_24xx *ctio,
1902 	struct qla_tgt_prm *prm)
1903 {
1904 	prm->sense_buffer_len = min_t(uint32_t, prm->sense_buffer_len,
1905 	    (uint32_t)sizeof(ctio->u.status1.sense_data));
1906 	ctio->u.status0.flags |=
1907 	    __constant_cpu_to_le16(CTIO7_FLAGS_SEND_STATUS);
1908 	if (qlt_need_explicit_conf(prm->tgt->ha, prm->cmd, 0)) {
1909 		ctio->u.status0.flags |= __constant_cpu_to_le16(
1910 		    CTIO7_FLAGS_EXPLICIT_CONFORM |
1911 		    CTIO7_FLAGS_CONFORM_REQ);
1912 	}
1913 	ctio->u.status0.residual = cpu_to_le32(prm->residual);
1914 	ctio->u.status0.scsi_status = cpu_to_le16(prm->rq_result);
1915 	if (QLA_TGT_SENSE_VALID(prm->sense_buffer)) {
1916 		int i;
1917 
1918 		if (qlt_need_explicit_conf(prm->tgt->ha, prm->cmd, 1)) {
1919 			if (prm->cmd->se_cmd.scsi_status != 0) {
1920 				ql_dbg(ql_dbg_tgt, prm->cmd->vha, 0xe017,
1921 				    "Skipping EXPLICIT_CONFORM and "
1922 				    "CTIO7_FLAGS_CONFORM_REQ for FCP READ w/ "
1923 				    "non GOOD status\n");
1924 				goto skip_explict_conf;
1925 			}
1926 			ctio->u.status1.flags |= __constant_cpu_to_le16(
1927 			    CTIO7_FLAGS_EXPLICIT_CONFORM |
1928 			    CTIO7_FLAGS_CONFORM_REQ);
1929 		}
1930 skip_explict_conf:
1931 		ctio->u.status1.flags &=
1932 		    ~__constant_cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_0);
1933 		ctio->u.status1.flags |=
1934 		    __constant_cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1);
1935 		ctio->u.status1.scsi_status |=
1936 		    __constant_cpu_to_le16(SS_SENSE_LEN_VALID);
1937 		ctio->u.status1.sense_length =
1938 		    cpu_to_le16(prm->sense_buffer_len);
1939 		for (i = 0; i < prm->sense_buffer_len/4; i++)
1940 			((uint32_t *)ctio->u.status1.sense_data)[i] =
1941 				cpu_to_be32(((uint32_t *)prm->sense_buffer)[i]);
1942 #if 0
1943 		if (unlikely((prm->sense_buffer_len % 4) != 0)) {
1944 			static int q;
1945 			if (q < 10) {
1946 				ql_dbg(ql_dbg_tgt, vha, 0xe04f,
1947 				    "qla_target(%d): %d bytes of sense "
1948 				    "lost", prm->tgt->ha->vp_idx,
1949 				    prm->sense_buffer_len % 4);
1950 				q++;
1951 			}
1952 		}
1953 #endif
1954 	} else {
1955 		ctio->u.status1.flags &=
1956 		    ~__constant_cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_0);
1957 		ctio->u.status1.flags |=
1958 		    __constant_cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1);
1959 		ctio->u.status1.sense_length = 0;
1960 		memset(ctio->u.status1.sense_data, 0,
1961 		    sizeof(ctio->u.status1.sense_data));
1962 	}
1963 
1964 	/* Sense with len > 24, is it possible ??? */
1965 }
1966 
1967 
1968 
1969 /* diff  */
1970 static inline int
1971 qlt_hba_err_chk_enabled(struct se_cmd *se_cmd)
1972 {
1973 	/*
1974 	 * Uncomment when corresponding SCSI changes are done.
1975 	 *
1976 	 if (!sp->cmd->prot_chk)
1977 	 return 0;
1978 	 *
1979 	 */
1980 	switch (se_cmd->prot_op) {
1981 	case TARGET_PROT_DOUT_INSERT:
1982 	case TARGET_PROT_DIN_STRIP:
1983 		if (ql2xenablehba_err_chk >= 1)
1984 			return 1;
1985 		break;
1986 	case TARGET_PROT_DOUT_PASS:
1987 	case TARGET_PROT_DIN_PASS:
1988 		if (ql2xenablehba_err_chk >= 2)
1989 			return 1;
1990 		break;
1991 	case TARGET_PROT_DIN_INSERT:
1992 	case TARGET_PROT_DOUT_STRIP:
1993 		return 1;
1994 	default:
1995 		break;
1996 	}
1997 	return 0;
1998 }
1999 
2000 /*
2001  * qla24xx_set_t10dif_tags_from_cmd - Extract Ref and App tags from SCSI command
2002  *
2003  */
2004 static inline void
2005 qlt_set_t10dif_tags(struct se_cmd *se_cmd, struct crc_context *ctx)
2006 {
2007 	uint32_t lba = 0xffffffff & se_cmd->t_task_lba;
2008 
2009 	/* wait til Mode Sense/Select cmd, modepage Ah, subpage 2
2010 	 * have been immplemented by TCM, before AppTag is avail.
2011 	 * Look for modesense_handlers[]
2012 	 */
2013 	ctx->app_tag = 0;
2014 	ctx->app_tag_mask[0] = 0x0;
2015 	ctx->app_tag_mask[1] = 0x0;
2016 
2017 	switch (se_cmd->prot_type) {
2018 	case TARGET_DIF_TYPE0_PROT:
2019 		/*
2020 		 * No check for ql2xenablehba_err_chk, as it would be an
2021 		 * I/O error if hba tag generation is not done.
2022 		 */
2023 		ctx->ref_tag = cpu_to_le32(lba);
2024 
2025 		if (!qlt_hba_err_chk_enabled(se_cmd))
2026 			break;
2027 
2028 		/* enable ALL bytes of the ref tag */
2029 		ctx->ref_tag_mask[0] = 0xff;
2030 		ctx->ref_tag_mask[1] = 0xff;
2031 		ctx->ref_tag_mask[2] = 0xff;
2032 		ctx->ref_tag_mask[3] = 0xff;
2033 		break;
2034 	/*
2035 	 * For TYpe 1 protection: 16 bit GUARD tag, 32 bit REF tag, and
2036 	 * 16 bit app tag.
2037 	 */
2038 	case TARGET_DIF_TYPE1_PROT:
2039 		ctx->ref_tag = cpu_to_le32(lba);
2040 
2041 		if (!qlt_hba_err_chk_enabled(se_cmd))
2042 			break;
2043 
2044 		/* enable ALL bytes of the ref tag */
2045 		ctx->ref_tag_mask[0] = 0xff;
2046 		ctx->ref_tag_mask[1] = 0xff;
2047 		ctx->ref_tag_mask[2] = 0xff;
2048 		ctx->ref_tag_mask[3] = 0xff;
2049 		break;
2050 	/*
2051 	 * For TYPE 2 protection: 16 bit GUARD + 32 bit REF tag has to
2052 	 * match LBA in CDB + N
2053 	 */
2054 	case TARGET_DIF_TYPE2_PROT:
2055 		ctx->ref_tag = cpu_to_le32(lba);
2056 
2057 		if (!qlt_hba_err_chk_enabled(se_cmd))
2058 			break;
2059 
2060 		/* enable ALL bytes of the ref tag */
2061 		ctx->ref_tag_mask[0] = 0xff;
2062 		ctx->ref_tag_mask[1] = 0xff;
2063 		ctx->ref_tag_mask[2] = 0xff;
2064 		ctx->ref_tag_mask[3] = 0xff;
2065 		break;
2066 
2067 	/* For Type 3 protection: 16 bit GUARD only */
2068 	case TARGET_DIF_TYPE3_PROT:
2069 		ctx->ref_tag_mask[0] = ctx->ref_tag_mask[1] =
2070 			ctx->ref_tag_mask[2] = ctx->ref_tag_mask[3] = 0x00;
2071 		break;
2072 	}
2073 }
2074 
2075 
2076 static inline int
2077 qlt_build_ctio_crc2_pkt(struct qla_tgt_prm *prm, scsi_qla_host_t *vha)
2078 {
2079 	uint32_t		*cur_dsd;
2080 	int			sgc;
2081 	uint32_t		transfer_length = 0;
2082 	uint32_t		data_bytes;
2083 	uint32_t		dif_bytes;
2084 	uint8_t			bundling = 1;
2085 	uint8_t			*clr_ptr;
2086 	struct crc_context	*crc_ctx_pkt = NULL;
2087 	struct qla_hw_data	*ha;
2088 	struct ctio_crc2_to_fw	*pkt;
2089 	dma_addr_t		crc_ctx_dma;
2090 	uint16_t		fw_prot_opts = 0;
2091 	struct qla_tgt_cmd	*cmd = prm->cmd;
2092 	struct se_cmd		*se_cmd = &cmd->se_cmd;
2093 	uint32_t h;
2094 	struct atio_from_isp *atio = &prm->cmd->atio;
2095 	uint16_t t16;
2096 
2097 	sgc = 0;
2098 	ha = vha->hw;
2099 
2100 	pkt = (struct ctio_crc2_to_fw *)vha->req->ring_ptr;
2101 	prm->pkt = pkt;
2102 	memset(pkt, 0, sizeof(*pkt));
2103 
2104 	ql_dbg(ql_dbg_tgt, vha, 0xe071,
2105 		"qla_target(%d):%s: se_cmd[%p] CRC2 prot_op[0x%x] cmd prot sg:cnt[%p:%x] lba[%llu]\n",
2106 		vha->vp_idx, __func__, se_cmd, se_cmd->prot_op,
2107 		prm->prot_sg, prm->prot_seg_cnt, se_cmd->t_task_lba);
2108 
2109 	if ((se_cmd->prot_op == TARGET_PROT_DIN_INSERT) ||
2110 	    (se_cmd->prot_op == TARGET_PROT_DOUT_STRIP))
2111 		bundling = 0;
2112 
2113 	/* Compute dif len and adjust data len to incude protection */
2114 	data_bytes = cmd->bufflen;
2115 	dif_bytes  = (data_bytes / cmd->blk_sz) * 8;
2116 
2117 	switch (se_cmd->prot_op) {
2118 	case TARGET_PROT_DIN_INSERT:
2119 	case TARGET_PROT_DOUT_STRIP:
2120 		transfer_length = data_bytes;
2121 		data_bytes += dif_bytes;
2122 		break;
2123 
2124 	case TARGET_PROT_DIN_STRIP:
2125 	case TARGET_PROT_DOUT_INSERT:
2126 	case TARGET_PROT_DIN_PASS:
2127 	case TARGET_PROT_DOUT_PASS:
2128 		transfer_length = data_bytes + dif_bytes;
2129 		break;
2130 
2131 	default:
2132 		BUG();
2133 		break;
2134 	}
2135 
2136 	if (!qlt_hba_err_chk_enabled(se_cmd))
2137 		fw_prot_opts |= 0x10; /* Disable Guard tag checking */
2138 	/* HBA error checking enabled */
2139 	else if (IS_PI_UNINIT_CAPABLE(ha)) {
2140 		if ((se_cmd->prot_type == TARGET_DIF_TYPE1_PROT) ||
2141 		    (se_cmd->prot_type == TARGET_DIF_TYPE2_PROT))
2142 			fw_prot_opts |= PO_DIS_VALD_APP_ESC;
2143 		else if (se_cmd->prot_type == TARGET_DIF_TYPE3_PROT)
2144 			fw_prot_opts |= PO_DIS_VALD_APP_REF_ESC;
2145 	}
2146 
2147 	switch (se_cmd->prot_op) {
2148 	case TARGET_PROT_DIN_INSERT:
2149 	case TARGET_PROT_DOUT_INSERT:
2150 		fw_prot_opts |= PO_MODE_DIF_INSERT;
2151 		break;
2152 	case TARGET_PROT_DIN_STRIP:
2153 	case TARGET_PROT_DOUT_STRIP:
2154 		fw_prot_opts |= PO_MODE_DIF_REMOVE;
2155 		break;
2156 	case TARGET_PROT_DIN_PASS:
2157 	case TARGET_PROT_DOUT_PASS:
2158 		fw_prot_opts |= PO_MODE_DIF_PASS;
2159 		/* FUTURE: does tcm require T10CRC<->IPCKSUM conversion? */
2160 		break;
2161 	default:/* Normal Request */
2162 		fw_prot_opts |= PO_MODE_DIF_PASS;
2163 		break;
2164 	}
2165 
2166 
2167 	/* ---- PKT ---- */
2168 	/* Update entry type to indicate Command Type CRC_2 IOCB */
2169 	pkt->entry_type  = CTIO_CRC2;
2170 	pkt->entry_count = 1;
2171 	pkt->vp_index = vha->vp_idx;
2172 
2173 	h = qlt_make_handle(vha);
2174 	if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
2175 		/*
2176 		 * CTIO type 7 from the firmware doesn't provide a way to
2177 		 * know the initiator's LOOP ID, hence we can't find
2178 		 * the session and, so, the command.
2179 		 */
2180 		return -EAGAIN;
2181 	} else
2182 		ha->tgt.cmds[h-1] = prm->cmd;
2183 
2184 
2185 	pkt->handle  = h | CTIO_COMPLETION_HANDLE_MARK;
2186 	pkt->nport_handle = prm->cmd->loop_id;
2187 	pkt->timeout = __constant_cpu_to_le16(QLA_TGT_TIMEOUT);
2188 	pkt->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
2189 	pkt->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
2190 	pkt->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
2191 	pkt->exchange_addr   = atio->u.isp24.exchange_addr;
2192 
2193 	/* silence compile warning */
2194 	t16 = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2195 	pkt->ox_id  = cpu_to_le16(t16);
2196 
2197 	t16 = (atio->u.isp24.attr << 9);
2198 	pkt->flags |= cpu_to_le16(t16);
2199 	pkt->relative_offset = cpu_to_le32(prm->cmd->offset);
2200 
2201 	/* Set transfer direction */
2202 	if (cmd->dma_data_direction == DMA_TO_DEVICE)
2203 		pkt->flags = __constant_cpu_to_le16(CTIO7_FLAGS_DATA_IN);
2204 	else if (cmd->dma_data_direction == DMA_FROM_DEVICE)
2205 		pkt->flags = __constant_cpu_to_le16(CTIO7_FLAGS_DATA_OUT);
2206 
2207 
2208 	pkt->dseg_count = prm->tot_dsds;
2209 	/* Fibre channel byte count */
2210 	pkt->transfer_length = cpu_to_le32(transfer_length);
2211 
2212 
2213 	/* ----- CRC context -------- */
2214 
2215 	/* Allocate CRC context from global pool */
2216 	crc_ctx_pkt = cmd->ctx =
2217 	    dma_pool_alloc(ha->dl_dma_pool, GFP_ATOMIC, &crc_ctx_dma);
2218 
2219 	if (!crc_ctx_pkt)
2220 		goto crc_queuing_error;
2221 
2222 	/* Zero out CTX area. */
2223 	clr_ptr = (uint8_t *)crc_ctx_pkt;
2224 	memset(clr_ptr, 0, sizeof(*crc_ctx_pkt));
2225 
2226 	crc_ctx_pkt->crc_ctx_dma = crc_ctx_dma;
2227 	INIT_LIST_HEAD(&crc_ctx_pkt->dsd_list);
2228 
2229 	/* Set handle */
2230 	crc_ctx_pkt->handle = pkt->handle;
2231 
2232 	qlt_set_t10dif_tags(se_cmd, crc_ctx_pkt);
2233 
2234 	pkt->crc_context_address[0] = cpu_to_le32(LSD(crc_ctx_dma));
2235 	pkt->crc_context_address[1] = cpu_to_le32(MSD(crc_ctx_dma));
2236 	pkt->crc_context_len = CRC_CONTEXT_LEN_FW;
2237 
2238 
2239 	if (!bundling) {
2240 		cur_dsd = (uint32_t *) &crc_ctx_pkt->u.nobundling.data_address;
2241 	} else {
2242 		/*
2243 		 * Configure Bundling if we need to fetch interlaving
2244 		 * protection PCI accesses
2245 		 */
2246 		fw_prot_opts |= PO_ENABLE_DIF_BUNDLING;
2247 		crc_ctx_pkt->u.bundling.dif_byte_count = cpu_to_le32(dif_bytes);
2248 		crc_ctx_pkt->u.bundling.dseg_count =
2249 			cpu_to_le16(prm->tot_dsds - prm->prot_seg_cnt);
2250 		cur_dsd = (uint32_t *) &crc_ctx_pkt->u.bundling.data_address;
2251 	}
2252 
2253 	/* Finish the common fields of CRC pkt */
2254 	crc_ctx_pkt->blk_size   = cpu_to_le16(cmd->blk_sz);
2255 	crc_ctx_pkt->prot_opts  = cpu_to_le16(fw_prot_opts);
2256 	crc_ctx_pkt->byte_count = cpu_to_le32(data_bytes);
2257 	crc_ctx_pkt->guard_seed = __constant_cpu_to_le16(0);
2258 
2259 
2260 	/* Walks data segments */
2261 	pkt->flags |= __constant_cpu_to_le16(CTIO7_FLAGS_DSD_PTR);
2262 
2263 	if (!bundling && prm->prot_seg_cnt) {
2264 		if (qla24xx_walk_and_build_sglist_no_difb(ha, NULL, cur_dsd,
2265 			prm->tot_dsds, cmd))
2266 			goto crc_queuing_error;
2267 	} else if (qla24xx_walk_and_build_sglist(ha, NULL, cur_dsd,
2268 		(prm->tot_dsds - prm->prot_seg_cnt), cmd))
2269 		goto crc_queuing_error;
2270 
2271 	if (bundling && prm->prot_seg_cnt) {
2272 		/* Walks dif segments */
2273 		pkt->add_flags |= CTIO_CRC2_AF_DIF_DSD_ENA;
2274 
2275 		cur_dsd = (uint32_t *) &crc_ctx_pkt->u.bundling.dif_address;
2276 		if (qla24xx_walk_and_build_prot_sglist(ha, NULL, cur_dsd,
2277 			prm->prot_seg_cnt, cmd))
2278 			goto crc_queuing_error;
2279 	}
2280 	return QLA_SUCCESS;
2281 
2282 crc_queuing_error:
2283 	/* Cleanup will be performed by the caller */
2284 
2285 	return QLA_FUNCTION_FAILED;
2286 }
2287 
2288 
2289 /*
2290  * Callback to setup response of xmit_type of QLA_TGT_XMIT_DATA and *
2291  * QLA_TGT_XMIT_STATUS for >= 24xx silicon
2292  */
2293 int qlt_xmit_response(struct qla_tgt_cmd *cmd, int xmit_type,
2294 	uint8_t scsi_status)
2295 {
2296 	struct scsi_qla_host *vha = cmd->vha;
2297 	struct qla_hw_data *ha = vha->hw;
2298 	struct ctio7_to_24xx *pkt;
2299 	struct qla_tgt_prm prm;
2300 	uint32_t full_req_cnt = 0;
2301 	unsigned long flags = 0;
2302 	int res;
2303 
2304 	memset(&prm, 0, sizeof(prm));
2305 	qlt_check_srr_debug(cmd, &xmit_type);
2306 
2307 	ql_dbg(ql_dbg_tgt, cmd->vha, 0xe018,
2308 	    "is_send_status=%d, cmd->bufflen=%d, cmd->sg_cnt=%d, cmd->dma_data_direction=%d se_cmd[%p]\n",
2309 	    (xmit_type & QLA_TGT_XMIT_STATUS) ?
2310 	    1 : 0, cmd->bufflen, cmd->sg_cnt, cmd->dma_data_direction,
2311 	    &cmd->se_cmd);
2312 
2313 	res = qlt_pre_xmit_response(cmd, &prm, xmit_type, scsi_status,
2314 	    &full_req_cnt);
2315 	if (unlikely(res != 0)) {
2316 		if (res == QLA_TGT_PRE_XMIT_RESP_CMD_ABORTED)
2317 			return 0;
2318 
2319 		return res;
2320 	}
2321 
2322 	spin_lock_irqsave(&ha->hardware_lock, flags);
2323 
2324 	if (qla2x00_reset_active(vha) || cmd->reset_count != ha->chip_reset) {
2325 		/*
2326 		 * Either a chip reset is active or this request was from
2327 		 * previous life, just abort the processing.
2328 		 */
2329 		cmd->state = QLA_TGT_STATE_PROCESSED;
2330 		qlt_abort_cmd_on_host_reset(cmd->vha, cmd);
2331 		ql_dbg(ql_dbg_async, vha, 0xe101,
2332 			"RESET-RSP active/old-count/new-count = %d/%d/%d.\n",
2333 			qla2x00_reset_active(vha), cmd->reset_count,
2334 			ha->chip_reset);
2335 		spin_unlock_irqrestore(&ha->hardware_lock, flags);
2336 		return 0;
2337 	}
2338 
2339 	/* Does F/W have an IOCBs for this request */
2340 	res = qlt_check_reserve_free_req(vha, full_req_cnt);
2341 	if (unlikely(res))
2342 		goto out_unmap_unlock;
2343 
2344 	if (cmd->se_cmd.prot_op && (xmit_type & QLA_TGT_XMIT_DATA))
2345 		res = qlt_build_ctio_crc2_pkt(&prm, vha);
2346 	else
2347 		res = qlt_24xx_build_ctio_pkt(&prm, vha);
2348 	if (unlikely(res != 0))
2349 		goto out_unmap_unlock;
2350 
2351 
2352 	pkt = (struct ctio7_to_24xx *)prm.pkt;
2353 
2354 	if (qlt_has_data(cmd) && (xmit_type & QLA_TGT_XMIT_DATA)) {
2355 		pkt->u.status0.flags |=
2356 		    __constant_cpu_to_le16(CTIO7_FLAGS_DATA_IN |
2357 			CTIO7_FLAGS_STATUS_MODE_0);
2358 
2359 		if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL)
2360 			qlt_load_data_segments(&prm, vha);
2361 
2362 		if (prm.add_status_pkt == 0) {
2363 			if (xmit_type & QLA_TGT_XMIT_STATUS) {
2364 				pkt->u.status0.scsi_status =
2365 				    cpu_to_le16(prm.rq_result);
2366 				pkt->u.status0.residual =
2367 				    cpu_to_le32(prm.residual);
2368 				pkt->u.status0.flags |= __constant_cpu_to_le16(
2369 				    CTIO7_FLAGS_SEND_STATUS);
2370 				if (qlt_need_explicit_conf(ha, cmd, 0)) {
2371 					pkt->u.status0.flags |=
2372 					    __constant_cpu_to_le16(
2373 						CTIO7_FLAGS_EXPLICIT_CONFORM |
2374 						CTIO7_FLAGS_CONFORM_REQ);
2375 				}
2376 			}
2377 
2378 		} else {
2379 			/*
2380 			 * We have already made sure that there is sufficient
2381 			 * amount of request entries to not drop HW lock in
2382 			 * req_pkt().
2383 			 */
2384 			struct ctio7_to_24xx *ctio =
2385 				(struct ctio7_to_24xx *)qlt_get_req_pkt(vha);
2386 
2387 			ql_dbg(ql_dbg_io, vha, 0x305e,
2388 			    "Building additional status packet 0x%p.\n",
2389 			    ctio);
2390 
2391 			/*
2392 			 * T10Dif: ctio_crc2_to_fw overlay ontop of
2393 			 * ctio7_to_24xx
2394 			 */
2395 			memcpy(ctio, pkt, sizeof(*ctio));
2396 			/* reset back to CTIO7 */
2397 			ctio->entry_count = 1;
2398 			ctio->entry_type = CTIO_TYPE7;
2399 			ctio->dseg_count = 0;
2400 			ctio->u.status1.flags &= ~__constant_cpu_to_le16(
2401 			    CTIO7_FLAGS_DATA_IN);
2402 
2403 			/* Real finish is ctio_m1's finish */
2404 			pkt->handle |= CTIO_INTERMEDIATE_HANDLE_MARK;
2405 			pkt->u.status0.flags |= __constant_cpu_to_le16(
2406 			    CTIO7_FLAGS_DONT_RET_CTIO);
2407 
2408 			/* qlt_24xx_init_ctio_to_isp will correct
2409 			 * all neccessary fields that's part of CTIO7.
2410 			 * There should be no residual of CTIO-CRC2 data.
2411 			 */
2412 			qlt_24xx_init_ctio_to_isp((struct ctio7_to_24xx *)ctio,
2413 			    &prm);
2414 			pr_debug("Status CTIO7: %p\n", ctio);
2415 		}
2416 	} else
2417 		qlt_24xx_init_ctio_to_isp(pkt, &prm);
2418 
2419 
2420 	cmd->state = QLA_TGT_STATE_PROCESSED; /* Mid-level is done processing */
2421 	cmd->cmd_sent_to_fw = 1;
2422 
2423 	/* Memory Barrier */
2424 	wmb();
2425 	qla2x00_start_iocbs(vha, vha->req);
2426 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
2427 
2428 	return 0;
2429 
2430 out_unmap_unlock:
2431 	qlt_unmap_sg(vha, cmd);
2432 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
2433 
2434 	return res;
2435 }
2436 EXPORT_SYMBOL(qlt_xmit_response);
2437 
2438 int qlt_rdy_to_xfer(struct qla_tgt_cmd *cmd)
2439 {
2440 	struct ctio7_to_24xx *pkt;
2441 	struct scsi_qla_host *vha = cmd->vha;
2442 	struct qla_hw_data *ha = vha->hw;
2443 	struct qla_tgt *tgt = cmd->tgt;
2444 	struct qla_tgt_prm prm;
2445 	unsigned long flags;
2446 	int res = 0;
2447 
2448 	memset(&prm, 0, sizeof(prm));
2449 	prm.cmd = cmd;
2450 	prm.tgt = tgt;
2451 	prm.sg = NULL;
2452 	prm.req_cnt = 1;
2453 
2454 	/* Send marker if required */
2455 	if (qlt_issue_marker(vha, 0) != QLA_SUCCESS)
2456 		return -EIO;
2457 
2458 	/* Calculate number of entries and segments required */
2459 	if (qlt_pci_map_calc_cnt(&prm) != 0)
2460 		return -EAGAIN;
2461 
2462 	spin_lock_irqsave(&ha->hardware_lock, flags);
2463 
2464 	if (qla2x00_reset_active(vha) || cmd->reset_count != ha->chip_reset) {
2465 		/*
2466 		 * Either a chip reset is active or this request was from
2467 		 * previous life, just abort the processing.
2468 		 */
2469 		cmd->state = QLA_TGT_STATE_NEED_DATA;
2470 		qlt_abort_cmd_on_host_reset(cmd->vha, cmd);
2471 		ql_dbg(ql_dbg_async, vha, 0xe102,
2472 			"RESET-XFR active/old-count/new-count = %d/%d/%d.\n",
2473 			qla2x00_reset_active(vha), cmd->reset_count,
2474 			ha->chip_reset);
2475 		spin_unlock_irqrestore(&ha->hardware_lock, flags);
2476 		return 0;
2477 	}
2478 
2479 	/* Does F/W have an IOCBs for this request */
2480 	res = qlt_check_reserve_free_req(vha, prm.req_cnt);
2481 	if (res != 0)
2482 		goto out_unlock_free_unmap;
2483 	if (cmd->se_cmd.prot_op)
2484 		res = qlt_build_ctio_crc2_pkt(&prm, vha);
2485 	else
2486 		res = qlt_24xx_build_ctio_pkt(&prm, vha);
2487 
2488 	if (unlikely(res != 0))
2489 		goto out_unlock_free_unmap;
2490 	pkt = (struct ctio7_to_24xx *)prm.pkt;
2491 	pkt->u.status0.flags |= __constant_cpu_to_le16(CTIO7_FLAGS_DATA_OUT |
2492 	    CTIO7_FLAGS_STATUS_MODE_0);
2493 
2494 	if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL)
2495 		qlt_load_data_segments(&prm, vha);
2496 
2497 	cmd->state = QLA_TGT_STATE_NEED_DATA;
2498 	cmd->cmd_sent_to_fw = 1;
2499 
2500 	/* Memory Barrier */
2501 	wmb();
2502 	qla2x00_start_iocbs(vha, vha->req);
2503 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
2504 
2505 	return res;
2506 
2507 out_unlock_free_unmap:
2508 	qlt_unmap_sg(vha, cmd);
2509 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
2510 
2511 	return res;
2512 }
2513 EXPORT_SYMBOL(qlt_rdy_to_xfer);
2514 
2515 
2516 /*
2517  * Checks the guard or meta-data for the type of error
2518  * detected by the HBA.
2519  */
2520 static inline int
2521 qlt_handle_dif_error(struct scsi_qla_host *vha, struct qla_tgt_cmd *cmd,
2522 		struct ctio_crc_from_fw *sts)
2523 {
2524 	uint8_t		*ap = &sts->actual_dif[0];
2525 	uint8_t		*ep = &sts->expected_dif[0];
2526 	uint32_t	e_ref_tag, a_ref_tag;
2527 	uint16_t	e_app_tag, a_app_tag;
2528 	uint16_t	e_guard, a_guard;
2529 	uint64_t	lba = cmd->se_cmd.t_task_lba;
2530 
2531 	a_guard   = be16_to_cpu(*(uint16_t *)(ap + 0));
2532 	a_app_tag = be16_to_cpu(*(uint16_t *)(ap + 2));
2533 	a_ref_tag = be32_to_cpu(*(uint32_t *)(ap + 4));
2534 
2535 	e_guard   = be16_to_cpu(*(uint16_t *)(ep + 0));
2536 	e_app_tag = be16_to_cpu(*(uint16_t *)(ep + 2));
2537 	e_ref_tag = be32_to_cpu(*(uint32_t *)(ep + 4));
2538 
2539 	ql_dbg(ql_dbg_tgt, vha, 0xe075,
2540 	    "iocb(s) %p Returned STATUS.\n", sts);
2541 
2542 	ql_dbg(ql_dbg_tgt, vha, 0xf075,
2543 	    "dif check TGT cdb 0x%x lba 0x%llx: [Actual|Expected] Ref Tag[0x%x|0x%x], App Tag [0x%x|0x%x], Guard [0x%x|0x%x]\n",
2544 	    cmd->atio.u.isp24.fcp_cmnd.cdb[0], lba,
2545 	    a_ref_tag, e_ref_tag, a_app_tag, e_app_tag, a_guard, e_guard);
2546 
2547 	/*
2548 	 * Ignore sector if:
2549 	 * For type     3: ref & app tag is all 'f's
2550 	 * For type 0,1,2: app tag is all 'f's
2551 	 */
2552 	if ((a_app_tag == 0xffff) &&
2553 	    ((cmd->se_cmd.prot_type != TARGET_DIF_TYPE3_PROT) ||
2554 	     (a_ref_tag == 0xffffffff))) {
2555 		uint32_t blocks_done;
2556 
2557 		/* 2TB boundary case covered automatically with this */
2558 		blocks_done = e_ref_tag - (uint32_t)lba + 1;
2559 		cmd->se_cmd.bad_sector = e_ref_tag;
2560 		cmd->se_cmd.pi_err = 0;
2561 		ql_dbg(ql_dbg_tgt, vha, 0xf074,
2562 			"need to return scsi good\n");
2563 
2564 		/* Update protection tag */
2565 		if (cmd->prot_sg_cnt) {
2566 			uint32_t i, j = 0, k = 0, num_ent;
2567 			struct scatterlist *sg, *sgl;
2568 
2569 
2570 			sgl = cmd->prot_sg;
2571 
2572 			/* Patch the corresponding protection tags */
2573 			for_each_sg(sgl, sg, cmd->prot_sg_cnt, i) {
2574 				num_ent = sg_dma_len(sg) / 8;
2575 				if (k + num_ent < blocks_done) {
2576 					k += num_ent;
2577 					continue;
2578 				}
2579 				j = blocks_done - k - 1;
2580 				k = blocks_done;
2581 				break;
2582 			}
2583 
2584 			if (k != blocks_done) {
2585 				ql_log(ql_log_warn, vha, 0xf076,
2586 				    "unexpected tag values tag:lba=%u:%llu)\n",
2587 				    e_ref_tag, (unsigned long long)lba);
2588 				goto out;
2589 			}
2590 
2591 #if 0
2592 			struct sd_dif_tuple *spt;
2593 			/* TODO:
2594 			 * This section came from initiator. Is it valid here?
2595 			 * should ulp be override with actual val???
2596 			 */
2597 			spt = page_address(sg_page(sg)) + sg->offset;
2598 			spt += j;
2599 
2600 			spt->app_tag = 0xffff;
2601 			if (cmd->se_cmd.prot_type == SCSI_PROT_DIF_TYPE3)
2602 				spt->ref_tag = 0xffffffff;
2603 #endif
2604 		}
2605 
2606 		return 0;
2607 	}
2608 
2609 	/* check guard */
2610 	if (e_guard != a_guard) {
2611 		cmd->se_cmd.pi_err = TCM_LOGICAL_BLOCK_GUARD_CHECK_FAILED;
2612 		cmd->se_cmd.bad_sector = cmd->se_cmd.t_task_lba;
2613 
2614 		ql_log(ql_log_warn, vha, 0xe076,
2615 		    "Guard ERR: cdb 0x%x lba 0x%llx: [Actual|Expected] Ref Tag[0x%x|0x%x], App Tag [0x%x|0x%x], Guard [0x%x|0x%x] cmd=%p\n",
2616 		    cmd->atio.u.isp24.fcp_cmnd.cdb[0], lba,
2617 		    a_ref_tag, e_ref_tag, a_app_tag, e_app_tag,
2618 		    a_guard, e_guard, cmd);
2619 		goto out;
2620 	}
2621 
2622 	/* check ref tag */
2623 	if (e_ref_tag != a_ref_tag) {
2624 		cmd->se_cmd.pi_err = TCM_LOGICAL_BLOCK_REF_TAG_CHECK_FAILED;
2625 		cmd->se_cmd.bad_sector = e_ref_tag;
2626 
2627 		ql_log(ql_log_warn, vha, 0xe077,
2628 			"Ref Tag ERR: cdb 0x%x lba 0x%llx: [Actual|Expected] Ref Tag[0x%x|0x%x], App Tag [0x%x|0x%x], Guard [0x%x|0x%x] cmd=%p\n",
2629 			cmd->atio.u.isp24.fcp_cmnd.cdb[0], lba,
2630 			a_ref_tag, e_ref_tag, a_app_tag, e_app_tag,
2631 			a_guard, e_guard, cmd);
2632 		goto out;
2633 	}
2634 
2635 	/* check appl tag */
2636 	if (e_app_tag != a_app_tag) {
2637 		cmd->se_cmd.pi_err = TCM_LOGICAL_BLOCK_APP_TAG_CHECK_FAILED;
2638 		cmd->se_cmd.bad_sector = cmd->se_cmd.t_task_lba;
2639 
2640 		ql_log(ql_log_warn, vha, 0xe078,
2641 			"App Tag ERR: cdb 0x%x lba 0x%llx: [Actual|Expected] Ref Tag[0x%x|0x%x], App Tag [0x%x|0x%x], Guard [0x%x|0x%x] cmd=%p\n",
2642 			cmd->atio.u.isp24.fcp_cmnd.cdb[0], lba,
2643 			a_ref_tag, e_ref_tag, a_app_tag, e_app_tag,
2644 			a_guard, e_guard, cmd);
2645 		goto out;
2646 	}
2647 out:
2648 	return 1;
2649 }
2650 
2651 
2652 /* If hardware_lock held on entry, might drop it, then reaquire */
2653 /* This function sends the appropriate CTIO to ISP 2xxx or 24xx */
2654 static int __qlt_send_term_exchange(struct scsi_qla_host *vha,
2655 	struct qla_tgt_cmd *cmd,
2656 	struct atio_from_isp *atio)
2657 {
2658 	struct ctio7_to_24xx *ctio24;
2659 	struct qla_hw_data *ha = vha->hw;
2660 	request_t *pkt;
2661 	int ret = 0;
2662 	uint16_t temp;
2663 
2664 	ql_dbg(ql_dbg_tgt, vha, 0xe01c, "Sending TERM EXCH CTIO (ha=%p)\n", ha);
2665 
2666 	pkt = (request_t *)qla2x00_alloc_iocbs_ready(vha, NULL);
2667 	if (pkt == NULL) {
2668 		ql_dbg(ql_dbg_tgt, vha, 0xe050,
2669 		    "qla_target(%d): %s failed: unable to allocate "
2670 		    "request packet\n", vha->vp_idx, __func__);
2671 		return -ENOMEM;
2672 	}
2673 
2674 	if (cmd != NULL) {
2675 		if (cmd->state < QLA_TGT_STATE_PROCESSED) {
2676 			ql_dbg(ql_dbg_tgt, vha, 0xe051,
2677 			    "qla_target(%d): Terminating cmd %p with "
2678 			    "incorrect state %d\n", vha->vp_idx, cmd,
2679 			    cmd->state);
2680 		} else
2681 			ret = 1;
2682 	}
2683 
2684 	pkt->entry_count = 1;
2685 	pkt->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
2686 
2687 	ctio24 = (struct ctio7_to_24xx *)pkt;
2688 	ctio24->entry_type = CTIO_TYPE7;
2689 	ctio24->nport_handle = cmd ? cmd->loop_id : CTIO7_NHANDLE_UNRECOGNIZED;
2690 	ctio24->timeout = __constant_cpu_to_le16(QLA_TGT_TIMEOUT);
2691 	ctio24->vp_index = vha->vp_idx;
2692 	ctio24->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
2693 	ctio24->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
2694 	ctio24->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
2695 	ctio24->exchange_addr = atio->u.isp24.exchange_addr;
2696 	ctio24->u.status1.flags = (atio->u.isp24.attr << 9) |
2697 	    __constant_cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1 |
2698 		CTIO7_FLAGS_TERMINATE);
2699 	temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2700 	ctio24->u.status1.ox_id = cpu_to_le16(temp);
2701 
2702 	/* Most likely, it isn't needed */
2703 	ctio24->u.status1.residual = get_unaligned((uint32_t *)
2704 	    &atio->u.isp24.fcp_cmnd.add_cdb[
2705 	    atio->u.isp24.fcp_cmnd.add_cdb_len]);
2706 	if (ctio24->u.status1.residual != 0)
2707 		ctio24->u.status1.scsi_status |= SS_RESIDUAL_UNDER;
2708 
2709 	/* Memory Barrier */
2710 	wmb();
2711 	qla2x00_start_iocbs(vha, vha->req);
2712 	return ret;
2713 }
2714 
2715 static void qlt_send_term_exchange(struct scsi_qla_host *vha,
2716 	struct qla_tgt_cmd *cmd, struct atio_from_isp *atio, int ha_locked)
2717 {
2718 	unsigned long flags;
2719 	int rc;
2720 
2721 	if (qlt_issue_marker(vha, ha_locked) < 0)
2722 		return;
2723 
2724 	if (ha_locked) {
2725 		rc = __qlt_send_term_exchange(vha, cmd, atio);
2726 		if (rc == -ENOMEM)
2727 			qlt_alloc_qfull_cmd(vha, atio, 0, 0);
2728 		goto done;
2729 	}
2730 	spin_lock_irqsave(&vha->hw->hardware_lock, flags);
2731 	rc = __qlt_send_term_exchange(vha, cmd, atio);
2732 	if (rc == -ENOMEM)
2733 		qlt_alloc_qfull_cmd(vha, atio, 0, 0);
2734 	spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
2735 
2736 done:
2737 	if (cmd && ((cmd->state != QLA_TGT_STATE_ABORTED) ||
2738 	    !cmd->cmd_sent_to_fw)) {
2739 		if (!ha_locked && !in_interrupt())
2740 			msleep(250); /* just in case */
2741 
2742 		qlt_unmap_sg(vha, cmd);
2743 		vha->hw->tgt.tgt_ops->free_cmd(cmd);
2744 	}
2745 	return;
2746 }
2747 
2748 static void qlt_init_term_exchange(struct scsi_qla_host *vha)
2749 {
2750 	struct list_head free_list;
2751 	struct qla_tgt_cmd *cmd, *tcmd;
2752 
2753 	vha->hw->tgt.leak_exchg_thresh_hold =
2754 	    (vha->hw->fw_xcb_count/100) * LEAK_EXCHG_THRESH_HOLD_PERCENT;
2755 
2756 	cmd = tcmd = NULL;
2757 	if (!list_empty(&vha->hw->tgt.q_full_list)) {
2758 		INIT_LIST_HEAD(&free_list);
2759 		list_splice_init(&vha->hw->tgt.q_full_list, &free_list);
2760 
2761 		list_for_each_entry_safe(cmd, tcmd, &free_list, cmd_list) {
2762 			list_del(&cmd->cmd_list);
2763 			/* This cmd was never sent to TCM.  There is no need
2764 			 * to schedule free or call free_cmd
2765 			 */
2766 			qlt_free_cmd(cmd);
2767 			vha->hw->tgt.num_qfull_cmds_alloc--;
2768 		}
2769 	}
2770 	vha->hw->tgt.num_qfull_cmds_dropped = 0;
2771 }
2772 
2773 static void qlt_chk_exch_leak_thresh_hold(struct scsi_qla_host *vha)
2774 {
2775 	uint32_t total_leaked;
2776 
2777 	total_leaked = vha->hw->tgt.num_qfull_cmds_dropped;
2778 
2779 	if (vha->hw->tgt.leak_exchg_thresh_hold &&
2780 	    (total_leaked > vha->hw->tgt.leak_exchg_thresh_hold)) {
2781 
2782 		ql_dbg(ql_dbg_tgt, vha, 0xe079,
2783 		    "Chip reset due to exchange starvation: %d/%d.\n",
2784 		    total_leaked, vha->hw->fw_xcb_count);
2785 
2786 		if (IS_P3P_TYPE(vha->hw))
2787 			set_bit(FCOE_CTX_RESET_NEEDED, &vha->dpc_flags);
2788 		else
2789 			set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
2790 		qla2xxx_wake_dpc(vha);
2791 	}
2792 
2793 }
2794 
2795 void qlt_free_cmd(struct qla_tgt_cmd *cmd)
2796 {
2797 	struct qla_tgt_sess *sess = cmd->sess;
2798 
2799 	ql_dbg(ql_dbg_tgt, cmd->vha, 0xe074,
2800 	    "%s: se_cmd[%p] ox_id %04x\n",
2801 	    __func__, &cmd->se_cmd,
2802 	    be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
2803 
2804 	BUG_ON(cmd->cmd_in_wq);
2805 
2806 	if (!cmd->q_full)
2807 		qlt_decr_num_pend_cmds(cmd->vha);
2808 
2809 	BUG_ON(cmd->sg_mapped);
2810 	cmd->jiffies_at_free = get_jiffies_64();
2811 	if (unlikely(cmd->free_sg))
2812 		kfree(cmd->sg);
2813 
2814 	if (!sess || !sess->se_sess) {
2815 		WARN_ON(1);
2816 		return;
2817 	}
2818 	cmd->jiffies_at_free = get_jiffies_64();
2819 	percpu_ida_free(&sess->se_sess->sess_tag_pool, cmd->se_cmd.map_tag);
2820 }
2821 EXPORT_SYMBOL(qlt_free_cmd);
2822 
2823 /* ha->hardware_lock supposed to be held on entry */
2824 static int qlt_prepare_srr_ctio(struct scsi_qla_host *vha,
2825 	struct qla_tgt_cmd *cmd, void *ctio)
2826 {
2827 	struct qla_tgt_srr_ctio *sc;
2828 	struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
2829 	struct qla_tgt_srr_imm *imm;
2830 
2831 	tgt->ctio_srr_id++;
2832 	cmd->cmd_flags |= BIT_15;
2833 
2834 	ql_dbg(ql_dbg_tgt_mgt, vha, 0xf019,
2835 	    "qla_target(%d): CTIO with SRR status received\n", vha->vp_idx);
2836 
2837 	if (!ctio) {
2838 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf055,
2839 		    "qla_target(%d): SRR CTIO, but ctio is NULL\n",
2840 		    vha->vp_idx);
2841 		return -EINVAL;
2842 	}
2843 
2844 	sc = kzalloc(sizeof(*sc), GFP_ATOMIC);
2845 	if (sc != NULL) {
2846 		sc->cmd = cmd;
2847 		/* IRQ is already OFF */
2848 		spin_lock(&tgt->srr_lock);
2849 		sc->srr_id = tgt->ctio_srr_id;
2850 		list_add_tail(&sc->srr_list_entry,
2851 		    &tgt->srr_ctio_list);
2852 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01a,
2853 		    "CTIO SRR %p added (id %d)\n", sc, sc->srr_id);
2854 		if (tgt->imm_srr_id == tgt->ctio_srr_id) {
2855 			int found = 0;
2856 			list_for_each_entry(imm, &tgt->srr_imm_list,
2857 			    srr_list_entry) {
2858 				if (imm->srr_id == sc->srr_id) {
2859 					found = 1;
2860 					break;
2861 				}
2862 			}
2863 			if (found) {
2864 				ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01b,
2865 				    "Scheduling srr work\n");
2866 				schedule_work(&tgt->srr_work);
2867 			} else {
2868 				ql_dbg(ql_dbg_tgt_mgt, vha, 0xf056,
2869 				    "qla_target(%d): imm_srr_id "
2870 				    "== ctio_srr_id (%d), but there is no "
2871 				    "corresponding SRR IMM, deleting CTIO "
2872 				    "SRR %p\n", vha->vp_idx,
2873 				    tgt->ctio_srr_id, sc);
2874 				list_del(&sc->srr_list_entry);
2875 				spin_unlock(&tgt->srr_lock);
2876 
2877 				kfree(sc);
2878 				return -EINVAL;
2879 			}
2880 		}
2881 		spin_unlock(&tgt->srr_lock);
2882 	} else {
2883 		struct qla_tgt_srr_imm *ti;
2884 
2885 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf057,
2886 		    "qla_target(%d): Unable to allocate SRR CTIO entry\n",
2887 		    vha->vp_idx);
2888 		spin_lock(&tgt->srr_lock);
2889 		list_for_each_entry_safe(imm, ti, &tgt->srr_imm_list,
2890 		    srr_list_entry) {
2891 			if (imm->srr_id == tgt->ctio_srr_id) {
2892 				ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01c,
2893 				    "IMM SRR %p deleted (id %d)\n",
2894 				    imm, imm->srr_id);
2895 				list_del(&imm->srr_list_entry);
2896 				qlt_reject_free_srr_imm(vha, imm, 1);
2897 			}
2898 		}
2899 		spin_unlock(&tgt->srr_lock);
2900 
2901 		return -ENOMEM;
2902 	}
2903 
2904 	return 0;
2905 }
2906 
2907 /*
2908  * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
2909  */
2910 static int qlt_term_ctio_exchange(struct scsi_qla_host *vha, void *ctio,
2911 	struct qla_tgt_cmd *cmd, uint32_t status)
2912 {
2913 	int term = 0;
2914 
2915 	if (ctio != NULL) {
2916 		struct ctio7_from_24xx *c = (struct ctio7_from_24xx *)ctio;
2917 		term = !(c->flags &
2918 		    __constant_cpu_to_le16(OF_TERM_EXCH));
2919 	} else
2920 		term = 1;
2921 
2922 	if (term)
2923 		qlt_send_term_exchange(vha, cmd, &cmd->atio, 1);
2924 
2925 	return term;
2926 }
2927 
2928 /* ha->hardware_lock supposed to be held on entry */
2929 static inline struct qla_tgt_cmd *qlt_get_cmd(struct scsi_qla_host *vha,
2930 	uint32_t handle)
2931 {
2932 	struct qla_hw_data *ha = vha->hw;
2933 
2934 	handle--;
2935 	if (ha->tgt.cmds[handle] != NULL) {
2936 		struct qla_tgt_cmd *cmd = ha->tgt.cmds[handle];
2937 		ha->tgt.cmds[handle] = NULL;
2938 		return cmd;
2939 	} else
2940 		return NULL;
2941 }
2942 
2943 /* ha->hardware_lock supposed to be held on entry */
2944 static struct qla_tgt_cmd *qlt_ctio_to_cmd(struct scsi_qla_host *vha,
2945 	uint32_t handle, void *ctio)
2946 {
2947 	struct qla_tgt_cmd *cmd = NULL;
2948 
2949 	/* Clear out internal marks */
2950 	handle &= ~(CTIO_COMPLETION_HANDLE_MARK |
2951 	    CTIO_INTERMEDIATE_HANDLE_MARK);
2952 
2953 	if (handle != QLA_TGT_NULL_HANDLE) {
2954 		if (unlikely(handle == QLA_TGT_SKIP_HANDLE))
2955 			return NULL;
2956 
2957 		/* handle-1 is actually used */
2958 		if (unlikely(handle > DEFAULT_OUTSTANDING_COMMANDS)) {
2959 			ql_dbg(ql_dbg_tgt, vha, 0xe052,
2960 			    "qla_target(%d): Wrong handle %x received\n",
2961 			    vha->vp_idx, handle);
2962 			return NULL;
2963 		}
2964 		cmd = qlt_get_cmd(vha, handle);
2965 		if (unlikely(cmd == NULL)) {
2966 			ql_dbg(ql_dbg_tgt, vha, 0xe053,
2967 			    "qla_target(%d): Suspicious: unable to "
2968 			    "find the command with handle %x\n", vha->vp_idx,
2969 			    handle);
2970 			return NULL;
2971 		}
2972 	} else if (ctio != NULL) {
2973 		/* We can't get loop ID from CTIO7 */
2974 		ql_dbg(ql_dbg_tgt, vha, 0xe054,
2975 		    "qla_target(%d): Wrong CTIO received: QLA24xx doesn't "
2976 		    "support NULL handles\n", vha->vp_idx);
2977 		return NULL;
2978 	}
2979 
2980 	return cmd;
2981 }
2982 
2983 /* hardware_lock should be held by caller. */
2984 static void
2985 qlt_abort_cmd_on_host_reset(struct scsi_qla_host *vha, struct qla_tgt_cmd *cmd)
2986 {
2987 	struct qla_hw_data *ha = vha->hw;
2988 	uint32_t handle;
2989 
2990 	if (cmd->sg_mapped)
2991 		qlt_unmap_sg(vha, cmd);
2992 
2993 	handle = qlt_make_handle(vha);
2994 
2995 	/* TODO: fix debug message type and ids. */
2996 	if (cmd->state == QLA_TGT_STATE_PROCESSED) {
2997 		ql_dbg(ql_dbg_io, vha, 0xff00,
2998 		    "HOST-ABORT: handle=%d, state=PROCESSED.\n", handle);
2999 	} else if (cmd->state == QLA_TGT_STATE_NEED_DATA) {
3000 		cmd->write_data_transferred = 0;
3001 		cmd->state = QLA_TGT_STATE_DATA_IN;
3002 
3003 		ql_dbg(ql_dbg_io, vha, 0xff01,
3004 		    "HOST-ABORT: handle=%d, state=DATA_IN.\n", handle);
3005 
3006 		ha->tgt.tgt_ops->handle_data(cmd);
3007 		return;
3008 	} else if (cmd->state == QLA_TGT_STATE_ABORTED) {
3009 		ql_dbg(ql_dbg_io, vha, 0xff02,
3010 		    "HOST-ABORT: handle=%d, state=ABORTED.\n", handle);
3011 	} else {
3012 		ql_dbg(ql_dbg_io, vha, 0xff03,
3013 		    "HOST-ABORT: handle=%d, state=BAD(%d).\n", handle,
3014 		    cmd->state);
3015 		dump_stack();
3016 	}
3017 
3018 	cmd->cmd_flags |= BIT_12;
3019 	ha->tgt.tgt_ops->free_cmd(cmd);
3020 }
3021 
3022 void
3023 qlt_host_reset_handler(struct qla_hw_data *ha)
3024 {
3025 	struct qla_tgt_cmd *cmd;
3026 	unsigned long flags;
3027 	scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
3028 	scsi_qla_host_t *vha = NULL;
3029 	struct qla_tgt *tgt = base_vha->vha_tgt.qla_tgt;
3030 	uint32_t i;
3031 
3032 	if (!base_vha->hw->tgt.tgt_ops)
3033 		return;
3034 
3035 	if (!tgt || qla_ini_mode_enabled(base_vha)) {
3036 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf003,
3037 			"Target mode disabled\n");
3038 		return;
3039 	}
3040 
3041 	ql_dbg(ql_dbg_tgt_mgt, vha, 0xff10,
3042 	    "HOST-ABORT-HNDLR: base_vha->dpc_flags=%lx.\n",
3043 	    base_vha->dpc_flags);
3044 
3045 	spin_lock_irqsave(&ha->hardware_lock, flags);
3046 	for (i = 1; i < DEFAULT_OUTSTANDING_COMMANDS + 1; i++) {
3047 		cmd = qlt_get_cmd(base_vha, i);
3048 		if (!cmd)
3049 			continue;
3050 		/* ha->tgt.cmds entry is cleared by qlt_get_cmd. */
3051 		vha = cmd->vha;
3052 		qlt_abort_cmd_on_host_reset(vha, cmd);
3053 	}
3054 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
3055 }
3056 
3057 
3058 /*
3059  * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
3060  */
3061 static void qlt_do_ctio_completion(struct scsi_qla_host *vha, uint32_t handle,
3062 	uint32_t status, void *ctio)
3063 {
3064 	struct qla_hw_data *ha = vha->hw;
3065 	struct se_cmd *se_cmd;
3066 	const struct target_core_fabric_ops *tfo;
3067 	struct qla_tgt_cmd *cmd;
3068 
3069 	if (handle & CTIO_INTERMEDIATE_HANDLE_MARK) {
3070 		/* That could happen only in case of an error/reset/abort */
3071 		if (status != CTIO_SUCCESS) {
3072 			ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01d,
3073 			    "Intermediate CTIO received"
3074 			    " (status %x)\n", status);
3075 		}
3076 		return;
3077 	}
3078 
3079 	cmd = qlt_ctio_to_cmd(vha, handle, ctio);
3080 	if (cmd == NULL)
3081 		return;
3082 
3083 	se_cmd = &cmd->se_cmd;
3084 	tfo = se_cmd->se_tfo;
3085 	cmd->cmd_sent_to_fw = 0;
3086 
3087 	qlt_unmap_sg(vha, cmd);
3088 
3089 	if (unlikely(status != CTIO_SUCCESS)) {
3090 		switch (status & 0xFFFF) {
3091 		case CTIO_LIP_RESET:
3092 		case CTIO_TARGET_RESET:
3093 		case CTIO_ABORTED:
3094 			/* driver request abort via Terminate exchange */
3095 		case CTIO_TIMEOUT:
3096 		case CTIO_INVALID_RX_ID:
3097 			/* They are OK */
3098 			ql_dbg(ql_dbg_tgt_mgt, vha, 0xf058,
3099 			    "qla_target(%d): CTIO with "
3100 			    "status %#x received, state %x, se_cmd %p, "
3101 			    "(LIP_RESET=e, ABORTED=2, TARGET_RESET=17, "
3102 			    "TIMEOUT=b, INVALID_RX_ID=8)\n", vha->vp_idx,
3103 			    status, cmd->state, se_cmd);
3104 			break;
3105 
3106 		case CTIO_PORT_LOGGED_OUT:
3107 		case CTIO_PORT_UNAVAILABLE:
3108 			ql_dbg(ql_dbg_tgt_mgt, vha, 0xf059,
3109 			    "qla_target(%d): CTIO with PORT LOGGED "
3110 			    "OUT (29) or PORT UNAVAILABLE (28) status %x "
3111 			    "received (state %x, se_cmd %p)\n", vha->vp_idx,
3112 			    status, cmd->state, se_cmd);
3113 			break;
3114 
3115 		case CTIO_SRR_RECEIVED:
3116 			ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05a,
3117 			    "qla_target(%d): CTIO with SRR_RECEIVED"
3118 			    " status %x received (state %x, se_cmd %p)\n",
3119 			    vha->vp_idx, status, cmd->state, se_cmd);
3120 			if (qlt_prepare_srr_ctio(vha, cmd, ctio) != 0)
3121 				break;
3122 			else
3123 				return;
3124 
3125 		case CTIO_DIF_ERROR: {
3126 			struct ctio_crc_from_fw *crc =
3127 				(struct ctio_crc_from_fw *)ctio;
3128 			ql_dbg(ql_dbg_tgt_mgt, vha, 0xf073,
3129 			    "qla_target(%d): CTIO with DIF_ERROR status %x received (state %x, se_cmd %p) actual_dif[0x%llx] expect_dif[0x%llx]\n",
3130 			    vha->vp_idx, status, cmd->state, se_cmd,
3131 			    *((u64 *)&crc->actual_dif[0]),
3132 			    *((u64 *)&crc->expected_dif[0]));
3133 
3134 			if (qlt_handle_dif_error(vha, cmd, ctio)) {
3135 				if (cmd->state == QLA_TGT_STATE_NEED_DATA) {
3136 					/* scsi Write/xfer rdy complete */
3137 					goto skip_term;
3138 				} else {
3139 					/* scsi read/xmit respond complete
3140 					 * call handle dif to send scsi status
3141 					 * rather than terminate exchange.
3142 					 */
3143 					cmd->state = QLA_TGT_STATE_PROCESSED;
3144 					ha->tgt.tgt_ops->handle_dif_err(cmd);
3145 					return;
3146 				}
3147 			} else {
3148 				/* Need to generate a SCSI good completion.
3149 				 * because FW did not send scsi status.
3150 				 */
3151 				status = 0;
3152 				goto skip_term;
3153 			}
3154 			break;
3155 		}
3156 		default:
3157 			ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05b,
3158 			    "qla_target(%d): CTIO with error status 0x%x received (state %x, se_cmd %p\n",
3159 			    vha->vp_idx, status, cmd->state, se_cmd);
3160 			break;
3161 		}
3162 
3163 
3164 		/* "cmd->state == QLA_TGT_STATE_ABORTED" means
3165 		 * cmd is already aborted/terminated, we don't
3166 		 * need to terminate again.  The exchange is already
3167 		 * cleaned up/freed at FW level.  Just cleanup at driver
3168 		 * level.
3169 		 */
3170 		if ((cmd->state != QLA_TGT_STATE_NEED_DATA) &&
3171 		    (cmd->state != QLA_TGT_STATE_ABORTED)) {
3172 			cmd->cmd_flags |= BIT_13;
3173 			if (qlt_term_ctio_exchange(vha, ctio, cmd, status))
3174 				return;
3175 		}
3176 	}
3177 skip_term:
3178 
3179 	if (cmd->state == QLA_TGT_STATE_PROCESSED) {
3180 		;
3181 	} else if (cmd->state == QLA_TGT_STATE_NEED_DATA) {
3182 		int rx_status = 0;
3183 
3184 		cmd->state = QLA_TGT_STATE_DATA_IN;
3185 
3186 		if (unlikely(status != CTIO_SUCCESS))
3187 			rx_status = -EIO;
3188 		else
3189 			cmd->write_data_transferred = 1;
3190 
3191 		ha->tgt.tgt_ops->handle_data(cmd);
3192 		return;
3193 	} else if (cmd->state == QLA_TGT_STATE_ABORTED) {
3194 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01e,
3195 		  "Aborted command %p (tag %lld) finished\n", cmd, se_cmd->tag);
3196 	} else {
3197 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05c,
3198 		    "qla_target(%d): A command in state (%d) should "
3199 		    "not return a CTIO complete\n", vha->vp_idx, cmd->state);
3200 	}
3201 
3202 	if (unlikely(status != CTIO_SUCCESS) &&
3203 		(cmd->state != QLA_TGT_STATE_ABORTED)) {
3204 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01f, "Finishing failed CTIO\n");
3205 		dump_stack();
3206 	}
3207 
3208 
3209 	ha->tgt.tgt_ops->free_cmd(cmd);
3210 }
3211 
3212 static inline int qlt_get_fcp_task_attr(struct scsi_qla_host *vha,
3213 	uint8_t task_codes)
3214 {
3215 	int fcp_task_attr;
3216 
3217 	switch (task_codes) {
3218 	case ATIO_SIMPLE_QUEUE:
3219 		fcp_task_attr = TCM_SIMPLE_TAG;
3220 		break;
3221 	case ATIO_HEAD_OF_QUEUE:
3222 		fcp_task_attr = TCM_HEAD_TAG;
3223 		break;
3224 	case ATIO_ORDERED_QUEUE:
3225 		fcp_task_attr = TCM_ORDERED_TAG;
3226 		break;
3227 	case ATIO_ACA_QUEUE:
3228 		fcp_task_attr = TCM_ACA_TAG;
3229 		break;
3230 	case ATIO_UNTAGGED:
3231 		fcp_task_attr = TCM_SIMPLE_TAG;
3232 		break;
3233 	default:
3234 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05d,
3235 		    "qla_target: unknown task code %x, use ORDERED instead\n",
3236 		    task_codes);
3237 		fcp_task_attr = TCM_ORDERED_TAG;
3238 		break;
3239 	}
3240 
3241 	return fcp_task_attr;
3242 }
3243 
3244 static struct qla_tgt_sess *qlt_make_local_sess(struct scsi_qla_host *,
3245 					uint8_t *);
3246 /*
3247  * Process context for I/O path into tcm_qla2xxx code
3248  */
3249 static void __qlt_do_work(struct qla_tgt_cmd *cmd)
3250 {
3251 	scsi_qla_host_t *vha = cmd->vha;
3252 	struct qla_hw_data *ha = vha->hw;
3253 	struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
3254 	struct qla_tgt_sess *sess = cmd->sess;
3255 	struct atio_from_isp *atio = &cmd->atio;
3256 	unsigned char *cdb;
3257 	unsigned long flags;
3258 	uint32_t data_length;
3259 	int ret, fcp_task_attr, data_dir, bidi = 0;
3260 
3261 	cmd->cmd_in_wq = 0;
3262 	cmd->cmd_flags |= BIT_1;
3263 	if (tgt->tgt_stop)
3264 		goto out_term;
3265 
3266 	cdb = &atio->u.isp24.fcp_cmnd.cdb[0];
3267 	cmd->se_cmd.tag = atio->u.isp24.exchange_addr;
3268 	cmd->unpacked_lun = scsilun_to_int(
3269 	    (struct scsi_lun *)&atio->u.isp24.fcp_cmnd.lun);
3270 
3271 	if (atio->u.isp24.fcp_cmnd.rddata &&
3272 	    atio->u.isp24.fcp_cmnd.wrdata) {
3273 		bidi = 1;
3274 		data_dir = DMA_TO_DEVICE;
3275 	} else if (atio->u.isp24.fcp_cmnd.rddata)
3276 		data_dir = DMA_FROM_DEVICE;
3277 	else if (atio->u.isp24.fcp_cmnd.wrdata)
3278 		data_dir = DMA_TO_DEVICE;
3279 	else
3280 		data_dir = DMA_NONE;
3281 
3282 	fcp_task_attr = qlt_get_fcp_task_attr(vha,
3283 	    atio->u.isp24.fcp_cmnd.task_attr);
3284 	data_length = be32_to_cpu(get_unaligned((uint32_t *)
3285 	    &atio->u.isp24.fcp_cmnd.add_cdb[
3286 	    atio->u.isp24.fcp_cmnd.add_cdb_len]));
3287 
3288 	ret = ha->tgt.tgt_ops->handle_cmd(vha, cmd, cdb, data_length,
3289 				          fcp_task_attr, data_dir, bidi);
3290 	if (ret != 0)
3291 		goto out_term;
3292 	/*
3293 	 * Drop extra session reference from qla_tgt_handle_cmd_for_atio*(
3294 	 */
3295 	spin_lock_irqsave(&ha->hardware_lock, flags);
3296 	ha->tgt.tgt_ops->put_sess(sess);
3297 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
3298 	return;
3299 
3300 out_term:
3301 	ql_dbg(ql_dbg_io, vha, 0x3060, "Terminating work cmd %p", cmd);
3302 	/*
3303 	 * cmd has not sent to target yet, so pass NULL as the second
3304 	 * argument to qlt_send_term_exchange() and free the memory here.
3305 	 */
3306 	cmd->cmd_flags |= BIT_2;
3307 	spin_lock_irqsave(&ha->hardware_lock, flags);
3308 	qlt_send_term_exchange(vha, NULL, &cmd->atio, 1);
3309 
3310 	qlt_decr_num_pend_cmds(vha);
3311 	percpu_ida_free(&sess->se_sess->sess_tag_pool, cmd->se_cmd.map_tag);
3312 	ha->tgt.tgt_ops->put_sess(sess);
3313 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
3314 }
3315 
3316 static void qlt_do_work(struct work_struct *work)
3317 {
3318 	struct qla_tgt_cmd *cmd = container_of(work, struct qla_tgt_cmd, work);
3319 
3320 	__qlt_do_work(cmd);
3321 }
3322 
3323 static struct qla_tgt_cmd *qlt_get_tag(scsi_qla_host_t *vha,
3324 				       struct qla_tgt_sess *sess,
3325 				       struct atio_from_isp *atio)
3326 {
3327 	struct se_session *se_sess = sess->se_sess;
3328 	struct qla_tgt_cmd *cmd;
3329 	int tag;
3330 
3331 	tag = percpu_ida_alloc(&se_sess->sess_tag_pool, TASK_RUNNING);
3332 	if (tag < 0)
3333 		return NULL;
3334 
3335 	cmd = &((struct qla_tgt_cmd *)se_sess->sess_cmd_map)[tag];
3336 	memset(cmd, 0, sizeof(struct qla_tgt_cmd));
3337 
3338 	memcpy(&cmd->atio, atio, sizeof(*atio));
3339 	cmd->state = QLA_TGT_STATE_NEW;
3340 	cmd->tgt = vha->vha_tgt.qla_tgt;
3341 	qlt_incr_num_pend_cmds(vha);
3342 	cmd->vha = vha;
3343 	cmd->se_cmd.map_tag = tag;
3344 	cmd->sess = sess;
3345 	cmd->loop_id = sess->loop_id;
3346 	cmd->conf_compl_supported = sess->conf_compl_supported;
3347 
3348 	return cmd;
3349 }
3350 
3351 static void qlt_send_busy(struct scsi_qla_host *, struct atio_from_isp *,
3352 			  uint16_t);
3353 
3354 static void qlt_create_sess_from_atio(struct work_struct *work)
3355 {
3356 	struct qla_tgt_sess_op *op = container_of(work,
3357 					struct qla_tgt_sess_op, work);
3358 	scsi_qla_host_t *vha = op->vha;
3359 	struct qla_hw_data *ha = vha->hw;
3360 	struct qla_tgt_sess *sess;
3361 	struct qla_tgt_cmd *cmd;
3362 	unsigned long flags;
3363 	uint8_t *s_id = op->atio.u.isp24.fcp_hdr.s_id;
3364 
3365 	ql_dbg(ql_dbg_tgt_mgt, vha, 0xf022,
3366 		"qla_target(%d): Unable to find wwn login"
3367 		" (s_id %x:%x:%x), trying to create it manually\n",
3368 		vha->vp_idx, s_id[0], s_id[1], s_id[2]);
3369 
3370 	if (op->atio.u.raw.entry_count > 1) {
3371 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf023,
3372 		        "Dropping multy entry atio %p\n", &op->atio);
3373 		goto out_term;
3374 	}
3375 
3376 	mutex_lock(&vha->vha_tgt.tgt_mutex);
3377 	sess = qlt_make_local_sess(vha, s_id);
3378 	/* sess has an extra creation ref. */
3379 	mutex_unlock(&vha->vha_tgt.tgt_mutex);
3380 
3381 	if (!sess)
3382 		goto out_term;
3383 	/*
3384 	 * Now obtain a pre-allocated session tag using the original op->atio
3385 	 * packet header, and dispatch into __qlt_do_work() using the existing
3386 	 * process context.
3387 	 */
3388 	cmd = qlt_get_tag(vha, sess, &op->atio);
3389 	if (!cmd) {
3390 		spin_lock_irqsave(&ha->hardware_lock, flags);
3391 		qlt_send_busy(vha, &op->atio, SAM_STAT_BUSY);
3392 		ha->tgt.tgt_ops->put_sess(sess);
3393 		spin_unlock_irqrestore(&ha->hardware_lock, flags);
3394 		kfree(op);
3395 		return;
3396 	}
3397 	/*
3398 	 * __qlt_do_work() will call ha->tgt.tgt_ops->put_sess() to release
3399 	 * the extra reference taken above by qlt_make_local_sess()
3400 	 */
3401 	__qlt_do_work(cmd);
3402 	kfree(op);
3403 	return;
3404 
3405 out_term:
3406 	spin_lock_irqsave(&ha->hardware_lock, flags);
3407 	qlt_send_term_exchange(vha, NULL, &op->atio, 1);
3408 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
3409 	kfree(op);
3410 
3411 }
3412 
3413 /* ha->hardware_lock supposed to be held on entry */
3414 static int qlt_handle_cmd_for_atio(struct scsi_qla_host *vha,
3415 	struct atio_from_isp *atio)
3416 {
3417 	struct qla_hw_data *ha = vha->hw;
3418 	struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
3419 	struct qla_tgt_sess *sess;
3420 	struct qla_tgt_cmd *cmd;
3421 
3422 	if (unlikely(tgt->tgt_stop)) {
3423 		ql_dbg(ql_dbg_io, vha, 0x3061,
3424 		    "New command while device %p is shutting down\n", tgt);
3425 		return -EFAULT;
3426 	}
3427 
3428 	sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, atio->u.isp24.fcp_hdr.s_id);
3429 	if (unlikely(!sess)) {
3430 		struct qla_tgt_sess_op *op = kzalloc(sizeof(struct qla_tgt_sess_op),
3431 						     GFP_ATOMIC);
3432 		if (!op)
3433 			return -ENOMEM;
3434 
3435 		memcpy(&op->atio, atio, sizeof(*atio));
3436 		op->vha = vha;
3437 		INIT_WORK(&op->work, qlt_create_sess_from_atio);
3438 		queue_work(qla_tgt_wq, &op->work);
3439 		return 0;
3440 	}
3441 	/*
3442 	 * Do kref_get() before returning + dropping qla_hw_data->hardware_lock.
3443 	 */
3444 	kref_get(&sess->se_sess->sess_kref);
3445 
3446 	cmd = qlt_get_tag(vha, sess, atio);
3447 	if (!cmd) {
3448 		ql_dbg(ql_dbg_io, vha, 0x3062,
3449 		    "qla_target(%d): Allocation of cmd failed\n", vha->vp_idx);
3450 		ha->tgt.tgt_ops->put_sess(sess);
3451 		return -ENOMEM;
3452 	}
3453 
3454 	cmd->cmd_flags = 0;
3455 	cmd->jiffies_at_alloc = get_jiffies_64();
3456 
3457 	cmd->reset_count = vha->hw->chip_reset;
3458 
3459 	cmd->cmd_in_wq = 1;
3460 	cmd->cmd_flags |= BIT_0;
3461 	INIT_WORK(&cmd->work, qlt_do_work);
3462 	queue_work(qla_tgt_wq, &cmd->work);
3463 	return 0;
3464 
3465 }
3466 
3467 /* ha->hardware_lock supposed to be held on entry */
3468 static int qlt_issue_task_mgmt(struct qla_tgt_sess *sess, uint32_t lun,
3469 	int fn, void *iocb, int flags)
3470 {
3471 	struct scsi_qla_host *vha = sess->vha;
3472 	struct qla_hw_data *ha = vha->hw;
3473 	struct qla_tgt_mgmt_cmd *mcmd;
3474 	int res;
3475 	uint8_t tmr_func;
3476 
3477 	mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
3478 	if (!mcmd) {
3479 		ql_dbg(ql_dbg_tgt_tmr, vha, 0x10009,
3480 		    "qla_target(%d): Allocation of management "
3481 		    "command failed, some commands and their data could "
3482 		    "leak\n", vha->vp_idx);
3483 		return -ENOMEM;
3484 	}
3485 	memset(mcmd, 0, sizeof(*mcmd));
3486 	mcmd->sess = sess;
3487 
3488 	if (iocb) {
3489 		memcpy(&mcmd->orig_iocb.imm_ntfy, iocb,
3490 		    sizeof(mcmd->orig_iocb.imm_ntfy));
3491 	}
3492 	mcmd->tmr_func = fn;
3493 	mcmd->flags = flags;
3494 	mcmd->reset_count = vha->hw->chip_reset;
3495 
3496 	switch (fn) {
3497 	case QLA_TGT_CLEAR_ACA:
3498 		ql_dbg(ql_dbg_tgt_tmr, vha, 0x10000,
3499 		    "qla_target(%d): CLEAR_ACA received\n", sess->vha->vp_idx);
3500 		tmr_func = TMR_CLEAR_ACA;
3501 		break;
3502 
3503 	case QLA_TGT_TARGET_RESET:
3504 		ql_dbg(ql_dbg_tgt_tmr, vha, 0x10001,
3505 		    "qla_target(%d): TARGET_RESET received\n",
3506 		    sess->vha->vp_idx);
3507 		tmr_func = TMR_TARGET_WARM_RESET;
3508 		break;
3509 
3510 	case QLA_TGT_LUN_RESET:
3511 		ql_dbg(ql_dbg_tgt_tmr, vha, 0x10002,
3512 		    "qla_target(%d): LUN_RESET received\n", sess->vha->vp_idx);
3513 		tmr_func = TMR_LUN_RESET;
3514 		break;
3515 
3516 	case QLA_TGT_CLEAR_TS:
3517 		ql_dbg(ql_dbg_tgt_tmr, vha, 0x10003,
3518 		    "qla_target(%d): CLEAR_TS received\n", sess->vha->vp_idx);
3519 		tmr_func = TMR_CLEAR_TASK_SET;
3520 		break;
3521 
3522 	case QLA_TGT_ABORT_TS:
3523 		ql_dbg(ql_dbg_tgt_tmr, vha, 0x10004,
3524 		    "qla_target(%d): ABORT_TS received\n", sess->vha->vp_idx);
3525 		tmr_func = TMR_ABORT_TASK_SET;
3526 		break;
3527 #if 0
3528 	case QLA_TGT_ABORT_ALL:
3529 		ql_dbg(ql_dbg_tgt_tmr, vha, 0x10005,
3530 		    "qla_target(%d): Doing ABORT_ALL_TASKS\n",
3531 		    sess->vha->vp_idx);
3532 		tmr_func = 0;
3533 		break;
3534 
3535 	case QLA_TGT_ABORT_ALL_SESS:
3536 		ql_dbg(ql_dbg_tgt_tmr, vha, 0x10006,
3537 		    "qla_target(%d): Doing ABORT_ALL_TASKS_SESS\n",
3538 		    sess->vha->vp_idx);
3539 		tmr_func = 0;
3540 		break;
3541 
3542 	case QLA_TGT_NEXUS_LOSS_SESS:
3543 		ql_dbg(ql_dbg_tgt_tmr, vha, 0x10007,
3544 		    "qla_target(%d): Doing NEXUS_LOSS_SESS\n",
3545 		    sess->vha->vp_idx);
3546 		tmr_func = 0;
3547 		break;
3548 
3549 	case QLA_TGT_NEXUS_LOSS:
3550 		ql_dbg(ql_dbg_tgt_tmr, vha, 0x10008,
3551 		    "qla_target(%d): Doing NEXUS_LOSS\n", sess->vha->vp_idx);
3552 		tmr_func = 0;
3553 		break;
3554 #endif
3555 	default:
3556 		ql_dbg(ql_dbg_tgt_tmr, vha, 0x1000a,
3557 		    "qla_target(%d): Unknown task mgmt fn 0x%x\n",
3558 		    sess->vha->vp_idx, fn);
3559 		mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
3560 		return -ENOSYS;
3561 	}
3562 
3563 	res = ha->tgt.tgt_ops->handle_tmr(mcmd, lun, tmr_func, 0);
3564 	if (res != 0) {
3565 		ql_dbg(ql_dbg_tgt_tmr, vha, 0x1000b,
3566 		    "qla_target(%d): tgt.tgt_ops->handle_tmr() failed: %d\n",
3567 		    sess->vha->vp_idx, res);
3568 		mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
3569 		return -EFAULT;
3570 	}
3571 
3572 	return 0;
3573 }
3574 
3575 /* ha->hardware_lock supposed to be held on entry */
3576 static int qlt_handle_task_mgmt(struct scsi_qla_host *vha, void *iocb)
3577 {
3578 	struct atio_from_isp *a = (struct atio_from_isp *)iocb;
3579 	struct qla_hw_data *ha = vha->hw;
3580 	struct qla_tgt *tgt;
3581 	struct qla_tgt_sess *sess;
3582 	uint32_t lun, unpacked_lun;
3583 	int lun_size, fn;
3584 
3585 	tgt = vha->vha_tgt.qla_tgt;
3586 
3587 	lun = a->u.isp24.fcp_cmnd.lun;
3588 	lun_size = sizeof(a->u.isp24.fcp_cmnd.lun);
3589 	fn = a->u.isp24.fcp_cmnd.task_mgmt_flags;
3590 	sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
3591 	    a->u.isp24.fcp_hdr.s_id);
3592 	unpacked_lun = scsilun_to_int((struct scsi_lun *)&lun);
3593 
3594 	if (!sess) {
3595 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf024,
3596 		    "qla_target(%d): task mgmt fn 0x%x for "
3597 		    "non-existant session\n", vha->vp_idx, fn);
3598 		return qlt_sched_sess_work(tgt, QLA_TGT_SESS_WORK_TM, iocb,
3599 		    sizeof(struct atio_from_isp));
3600 	}
3601 
3602 	return qlt_issue_task_mgmt(sess, unpacked_lun, fn, iocb, 0);
3603 }
3604 
3605 /* ha->hardware_lock supposed to be held on entry */
3606 static int __qlt_abort_task(struct scsi_qla_host *vha,
3607 	struct imm_ntfy_from_isp *iocb, struct qla_tgt_sess *sess)
3608 {
3609 	struct atio_from_isp *a = (struct atio_from_isp *)iocb;
3610 	struct qla_hw_data *ha = vha->hw;
3611 	struct qla_tgt_mgmt_cmd *mcmd;
3612 	uint32_t lun, unpacked_lun;
3613 	int rc;
3614 
3615 	mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
3616 	if (mcmd == NULL) {
3617 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05f,
3618 		    "qla_target(%d): %s: Allocation of ABORT cmd failed\n",
3619 		    vha->vp_idx, __func__);
3620 		return -ENOMEM;
3621 	}
3622 	memset(mcmd, 0, sizeof(*mcmd));
3623 
3624 	mcmd->sess = sess;
3625 	memcpy(&mcmd->orig_iocb.imm_ntfy, iocb,
3626 	    sizeof(mcmd->orig_iocb.imm_ntfy));
3627 
3628 	lun = a->u.isp24.fcp_cmnd.lun;
3629 	unpacked_lun = scsilun_to_int((struct scsi_lun *)&lun);
3630 	mcmd->reset_count = vha->hw->chip_reset;
3631 
3632 	rc = ha->tgt.tgt_ops->handle_tmr(mcmd, unpacked_lun, TMR_ABORT_TASK,
3633 	    le16_to_cpu(iocb->u.isp2x.seq_id));
3634 	if (rc != 0) {
3635 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf060,
3636 		    "qla_target(%d): tgt_ops->handle_tmr() failed: %d\n",
3637 		    vha->vp_idx, rc);
3638 		mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
3639 		return -EFAULT;
3640 	}
3641 
3642 	return 0;
3643 }
3644 
3645 /* ha->hardware_lock supposed to be held on entry */
3646 static int qlt_abort_task(struct scsi_qla_host *vha,
3647 	struct imm_ntfy_from_isp *iocb)
3648 {
3649 	struct qla_hw_data *ha = vha->hw;
3650 	struct qla_tgt_sess *sess;
3651 	int loop_id;
3652 
3653 	loop_id = GET_TARGET_ID(ha, (struct atio_from_isp *)iocb);
3654 
3655 	sess = ha->tgt.tgt_ops->find_sess_by_loop_id(vha, loop_id);
3656 	if (sess == NULL) {
3657 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf025,
3658 		    "qla_target(%d): task abort for unexisting "
3659 		    "session\n", vha->vp_idx);
3660 		return qlt_sched_sess_work(vha->vha_tgt.qla_tgt,
3661 		    QLA_TGT_SESS_WORK_ABORT, iocb, sizeof(*iocb));
3662 	}
3663 
3664 	return __qlt_abort_task(vha, iocb, sess);
3665 }
3666 
3667 /*
3668  * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
3669  */
3670 static int qlt_24xx_handle_els(struct scsi_qla_host *vha,
3671 	struct imm_ntfy_from_isp *iocb)
3672 {
3673 	int res = 0;
3674 
3675 	ql_dbg(ql_dbg_tgt_mgt, vha, 0xf026,
3676 	    "qla_target(%d): Port ID: 0x%3phC ELS opcode: 0x%02x\n",
3677 	    vha->vp_idx, iocb->u.isp24.port_id, iocb->u.isp24.status_subcode);
3678 
3679 	switch (iocb->u.isp24.status_subcode) {
3680 	case ELS_PLOGI:
3681 	case ELS_FLOGI:
3682 	case ELS_PRLI:
3683 	case ELS_LOGO:
3684 	case ELS_PRLO:
3685 		res = qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS);
3686 		break;
3687 	case ELS_PDISC:
3688 	case ELS_ADISC:
3689 	{
3690 		struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
3691 		if (tgt->link_reinit_iocb_pending) {
3692 			qlt_send_notify_ack(vha, &tgt->link_reinit_iocb,
3693 			    0, 0, 0, 0, 0, 0);
3694 			tgt->link_reinit_iocb_pending = 0;
3695 		}
3696 		res = 1; /* send notify ack */
3697 		break;
3698 	}
3699 
3700 	default:
3701 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf061,
3702 		    "qla_target(%d): Unsupported ELS command %x "
3703 		    "received\n", vha->vp_idx, iocb->u.isp24.status_subcode);
3704 		res = qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS);
3705 		break;
3706 	}
3707 
3708 	return res;
3709 }
3710 
3711 static int qlt_set_data_offset(struct qla_tgt_cmd *cmd, uint32_t offset)
3712 {
3713 #if 1
3714 	/*
3715 	 * FIXME: Reject non zero SRR relative offset until we can test
3716 	 * this code properly.
3717 	 */
3718 	pr_debug("Rejecting non zero SRR rel_offs: %u\n", offset);
3719 	return -1;
3720 #else
3721 	struct scatterlist *sg, *sgp, *sg_srr, *sg_srr_start = NULL;
3722 	size_t first_offset = 0, rem_offset = offset, tmp = 0;
3723 	int i, sg_srr_cnt, bufflen = 0;
3724 
3725 	ql_dbg(ql_dbg_tgt, cmd->vha, 0xe023,
3726 	    "Entering qla_tgt_set_data_offset: cmd: %p, cmd->sg: %p, "
3727 	    "cmd->sg_cnt: %u, direction: %d\n",
3728 	    cmd, cmd->sg, cmd->sg_cnt, cmd->dma_data_direction);
3729 
3730 	if (!cmd->sg || !cmd->sg_cnt) {
3731 		ql_dbg(ql_dbg_tgt, cmd->vha, 0xe055,
3732 		    "Missing cmd->sg or zero cmd->sg_cnt in"
3733 		    " qla_tgt_set_data_offset\n");
3734 		return -EINVAL;
3735 	}
3736 	/*
3737 	 * Walk the current cmd->sg list until we locate the new sg_srr_start
3738 	 */
3739 	for_each_sg(cmd->sg, sg, cmd->sg_cnt, i) {
3740 		ql_dbg(ql_dbg_tgt, cmd->vha, 0xe024,
3741 		    "sg[%d]: %p page: %p, length: %d, offset: %d\n",
3742 		    i, sg, sg_page(sg), sg->length, sg->offset);
3743 
3744 		if ((sg->length + tmp) > offset) {
3745 			first_offset = rem_offset;
3746 			sg_srr_start = sg;
3747 			ql_dbg(ql_dbg_tgt, cmd->vha, 0xe025,
3748 			    "Found matching sg[%d], using %p as sg_srr_start, "
3749 			    "and using first_offset: %zu\n", i, sg,
3750 			    first_offset);
3751 			break;
3752 		}
3753 		tmp += sg->length;
3754 		rem_offset -= sg->length;
3755 	}
3756 
3757 	if (!sg_srr_start) {
3758 		ql_dbg(ql_dbg_tgt, cmd->vha, 0xe056,
3759 		    "Unable to locate sg_srr_start for offset: %u\n", offset);
3760 		return -EINVAL;
3761 	}
3762 	sg_srr_cnt = (cmd->sg_cnt - i);
3763 
3764 	sg_srr = kzalloc(sizeof(struct scatterlist) * sg_srr_cnt, GFP_KERNEL);
3765 	if (!sg_srr) {
3766 		ql_dbg(ql_dbg_tgt, cmd->vha, 0xe057,
3767 		    "Unable to allocate sgp\n");
3768 		return -ENOMEM;
3769 	}
3770 	sg_init_table(sg_srr, sg_srr_cnt);
3771 	sgp = &sg_srr[0];
3772 	/*
3773 	 * Walk the remaining list for sg_srr_start, mapping to the newly
3774 	 * allocated sg_srr taking first_offset into account.
3775 	 */
3776 	for_each_sg(sg_srr_start, sg, sg_srr_cnt, i) {
3777 		if (first_offset) {
3778 			sg_set_page(sgp, sg_page(sg),
3779 			    (sg->length - first_offset), first_offset);
3780 			first_offset = 0;
3781 		} else {
3782 			sg_set_page(sgp, sg_page(sg), sg->length, 0);
3783 		}
3784 		bufflen += sgp->length;
3785 
3786 		sgp = sg_next(sgp);
3787 		if (!sgp)
3788 			break;
3789 	}
3790 
3791 	cmd->sg = sg_srr;
3792 	cmd->sg_cnt = sg_srr_cnt;
3793 	cmd->bufflen = bufflen;
3794 	cmd->offset += offset;
3795 	cmd->free_sg = 1;
3796 
3797 	ql_dbg(ql_dbg_tgt, cmd->vha, 0xe026, "New cmd->sg: %p\n", cmd->sg);
3798 	ql_dbg(ql_dbg_tgt, cmd->vha, 0xe027, "New cmd->sg_cnt: %u\n",
3799 	    cmd->sg_cnt);
3800 	ql_dbg(ql_dbg_tgt, cmd->vha, 0xe028, "New cmd->bufflen: %u\n",
3801 	    cmd->bufflen);
3802 	ql_dbg(ql_dbg_tgt, cmd->vha, 0xe029, "New cmd->offset: %u\n",
3803 	    cmd->offset);
3804 
3805 	if (cmd->sg_cnt < 0)
3806 		BUG();
3807 
3808 	if (cmd->bufflen < 0)
3809 		BUG();
3810 
3811 	return 0;
3812 #endif
3813 }
3814 
3815 static inline int qlt_srr_adjust_data(struct qla_tgt_cmd *cmd,
3816 	uint32_t srr_rel_offs, int *xmit_type)
3817 {
3818 	int res = 0, rel_offs;
3819 
3820 	rel_offs = srr_rel_offs - cmd->offset;
3821 	ql_dbg(ql_dbg_tgt_mgt, cmd->vha, 0xf027, "srr_rel_offs=%d, rel_offs=%d",
3822 	    srr_rel_offs, rel_offs);
3823 
3824 	*xmit_type = QLA_TGT_XMIT_ALL;
3825 
3826 	if (rel_offs < 0) {
3827 		ql_dbg(ql_dbg_tgt_mgt, cmd->vha, 0xf062,
3828 		    "qla_target(%d): SRR rel_offs (%d) < 0",
3829 		    cmd->vha->vp_idx, rel_offs);
3830 		res = -1;
3831 	} else if (rel_offs == cmd->bufflen)
3832 		*xmit_type = QLA_TGT_XMIT_STATUS;
3833 	else if (rel_offs > 0)
3834 		res = qlt_set_data_offset(cmd, rel_offs);
3835 
3836 	return res;
3837 }
3838 
3839 /* No locks, thread context */
3840 static void qlt_handle_srr(struct scsi_qla_host *vha,
3841 	struct qla_tgt_srr_ctio *sctio, struct qla_tgt_srr_imm *imm)
3842 {
3843 	struct imm_ntfy_from_isp *ntfy =
3844 	    (struct imm_ntfy_from_isp *)&imm->imm_ntfy;
3845 	struct qla_hw_data *ha = vha->hw;
3846 	struct qla_tgt_cmd *cmd = sctio->cmd;
3847 	struct se_cmd *se_cmd = &cmd->se_cmd;
3848 	unsigned long flags;
3849 	int xmit_type = 0, resp = 0;
3850 	uint32_t offset;
3851 	uint16_t srr_ui;
3852 
3853 	offset = le32_to_cpu(ntfy->u.isp24.srr_rel_offs);
3854 	srr_ui = ntfy->u.isp24.srr_ui;
3855 
3856 	ql_dbg(ql_dbg_tgt_mgt, vha, 0xf028, "SRR cmd %p, srr_ui %x\n",
3857 	    cmd, srr_ui);
3858 
3859 	switch (srr_ui) {
3860 	case SRR_IU_STATUS:
3861 		spin_lock_irqsave(&ha->hardware_lock, flags);
3862 		qlt_send_notify_ack(vha, ntfy,
3863 		    0, 0, 0, NOTIFY_ACK_SRR_FLAGS_ACCEPT, 0, 0);
3864 		spin_unlock_irqrestore(&ha->hardware_lock, flags);
3865 		xmit_type = QLA_TGT_XMIT_STATUS;
3866 		resp = 1;
3867 		break;
3868 	case SRR_IU_DATA_IN:
3869 		if (!cmd->sg || !cmd->sg_cnt) {
3870 			ql_dbg(ql_dbg_tgt_mgt, vha, 0xf063,
3871 			    "Unable to process SRR_IU_DATA_IN due to"
3872 			    " missing cmd->sg, state: %d\n", cmd->state);
3873 			dump_stack();
3874 			goto out_reject;
3875 		}
3876 		if (se_cmd->scsi_status != 0) {
3877 			ql_dbg(ql_dbg_tgt, vha, 0xe02a,
3878 			    "Rejecting SRR_IU_DATA_IN with non GOOD "
3879 			    "scsi_status\n");
3880 			goto out_reject;
3881 		}
3882 		cmd->bufflen = se_cmd->data_length;
3883 
3884 		if (qlt_has_data(cmd)) {
3885 			if (qlt_srr_adjust_data(cmd, offset, &xmit_type) != 0)
3886 				goto out_reject;
3887 			spin_lock_irqsave(&ha->hardware_lock, flags);
3888 			qlt_send_notify_ack(vha, ntfy,
3889 			    0, 0, 0, NOTIFY_ACK_SRR_FLAGS_ACCEPT, 0, 0);
3890 			spin_unlock_irqrestore(&ha->hardware_lock, flags);
3891 			resp = 1;
3892 		} else {
3893 			ql_dbg(ql_dbg_tgt_mgt, vha, 0xf064,
3894 			       "qla_target(%d): SRR for in data for cmd without them (tag %lld, SCSI status %d), reject",
3895 			       vha->vp_idx, se_cmd->tag,
3896 			    cmd->se_cmd.scsi_status);
3897 			goto out_reject;
3898 		}
3899 		break;
3900 	case SRR_IU_DATA_OUT:
3901 		if (!cmd->sg || !cmd->sg_cnt) {
3902 			ql_dbg(ql_dbg_tgt_mgt, vha, 0xf065,
3903 			    "Unable to process SRR_IU_DATA_OUT due to"
3904 			    " missing cmd->sg\n");
3905 			dump_stack();
3906 			goto out_reject;
3907 		}
3908 		if (se_cmd->scsi_status != 0) {
3909 			ql_dbg(ql_dbg_tgt, vha, 0xe02b,
3910 			    "Rejecting SRR_IU_DATA_OUT"
3911 			    " with non GOOD scsi_status\n");
3912 			goto out_reject;
3913 		}
3914 		cmd->bufflen = se_cmd->data_length;
3915 
3916 		if (qlt_has_data(cmd)) {
3917 			if (qlt_srr_adjust_data(cmd, offset, &xmit_type) != 0)
3918 				goto out_reject;
3919 			spin_lock_irqsave(&ha->hardware_lock, flags);
3920 			qlt_send_notify_ack(vha, ntfy,
3921 			    0, 0, 0, NOTIFY_ACK_SRR_FLAGS_ACCEPT, 0, 0);
3922 			spin_unlock_irqrestore(&ha->hardware_lock, flags);
3923 			if (xmit_type & QLA_TGT_XMIT_DATA) {
3924 				cmd->cmd_flags |= BIT_8;
3925 				qlt_rdy_to_xfer(cmd);
3926 			}
3927 		} else {
3928 			ql_dbg(ql_dbg_tgt_mgt, vha, 0xf066,
3929 			    "qla_target(%d): SRR for out data for cmd without them (tag %lld, SCSI status %d), reject",
3930 			       vha->vp_idx, se_cmd->tag, cmd->se_cmd.scsi_status);
3931 			goto out_reject;
3932 		}
3933 		break;
3934 	default:
3935 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf067,
3936 		    "qla_target(%d): Unknown srr_ui value %x",
3937 		    vha->vp_idx, srr_ui);
3938 		goto out_reject;
3939 	}
3940 
3941 	/* Transmit response in case of status and data-in cases */
3942 	if (resp) {
3943 		cmd->cmd_flags |= BIT_7;
3944 		qlt_xmit_response(cmd, xmit_type, se_cmd->scsi_status);
3945 	}
3946 
3947 	return;
3948 
3949 out_reject:
3950 	spin_lock_irqsave(&ha->hardware_lock, flags);
3951 	qlt_send_notify_ack(vha, ntfy, 0, 0, 0,
3952 	    NOTIFY_ACK_SRR_FLAGS_REJECT,
3953 	    NOTIFY_ACK_SRR_REJECT_REASON_UNABLE_TO_PERFORM,
3954 	    NOTIFY_ACK_SRR_FLAGS_REJECT_EXPL_NO_EXPL);
3955 	if (cmd->state == QLA_TGT_STATE_NEED_DATA) {
3956 		cmd->state = QLA_TGT_STATE_DATA_IN;
3957 		dump_stack();
3958 	} else {
3959 		cmd->cmd_flags |= BIT_9;
3960 		qlt_send_term_exchange(vha, cmd, &cmd->atio, 1);
3961 	}
3962 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
3963 }
3964 
3965 static void qlt_reject_free_srr_imm(struct scsi_qla_host *vha,
3966 	struct qla_tgt_srr_imm *imm, int ha_locked)
3967 {
3968 	struct qla_hw_data *ha = vha->hw;
3969 	unsigned long flags = 0;
3970 
3971 	if (!ha_locked)
3972 		spin_lock_irqsave(&ha->hardware_lock, flags);
3973 
3974 	qlt_send_notify_ack(vha, (void *)&imm->imm_ntfy, 0, 0, 0,
3975 	    NOTIFY_ACK_SRR_FLAGS_REJECT,
3976 	    NOTIFY_ACK_SRR_REJECT_REASON_UNABLE_TO_PERFORM,
3977 	    NOTIFY_ACK_SRR_FLAGS_REJECT_EXPL_NO_EXPL);
3978 
3979 	if (!ha_locked)
3980 		spin_unlock_irqrestore(&ha->hardware_lock, flags);
3981 
3982 	kfree(imm);
3983 }
3984 
3985 static void qlt_handle_srr_work(struct work_struct *work)
3986 {
3987 	struct qla_tgt *tgt = container_of(work, struct qla_tgt, srr_work);
3988 	struct scsi_qla_host *vha = tgt->vha;
3989 	struct qla_tgt_srr_ctio *sctio;
3990 	unsigned long flags;
3991 
3992 	ql_dbg(ql_dbg_tgt_mgt, vha, 0xf029, "Entering SRR work (tgt %p)\n",
3993 	    tgt);
3994 
3995 restart:
3996 	spin_lock_irqsave(&tgt->srr_lock, flags);
3997 	list_for_each_entry(sctio, &tgt->srr_ctio_list, srr_list_entry) {
3998 		struct qla_tgt_srr_imm *imm, *i, *ti;
3999 		struct qla_tgt_cmd *cmd;
4000 		struct se_cmd *se_cmd;
4001 
4002 		imm = NULL;
4003 		list_for_each_entry_safe(i, ti, &tgt->srr_imm_list,
4004 						srr_list_entry) {
4005 			if (i->srr_id == sctio->srr_id) {
4006 				list_del(&i->srr_list_entry);
4007 				if (imm) {
4008 					ql_dbg(ql_dbg_tgt_mgt, vha, 0xf068,
4009 					  "qla_target(%d): There must be "
4010 					  "only one IMM SRR per CTIO SRR "
4011 					  "(IMM SRR %p, id %d, CTIO %p\n",
4012 					  vha->vp_idx, i, i->srr_id, sctio);
4013 					qlt_reject_free_srr_imm(tgt->vha, i, 0);
4014 				} else
4015 					imm = i;
4016 			}
4017 		}
4018 
4019 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf02a,
4020 		    "IMM SRR %p, CTIO SRR %p (id %d)\n", imm, sctio,
4021 		    sctio->srr_id);
4022 
4023 		if (imm == NULL) {
4024 			ql_dbg(ql_dbg_tgt_mgt, vha, 0xf02b,
4025 			    "Not found matching IMM for SRR CTIO (id %d)\n",
4026 			    sctio->srr_id);
4027 			continue;
4028 		} else
4029 			list_del(&sctio->srr_list_entry);
4030 
4031 		spin_unlock_irqrestore(&tgt->srr_lock, flags);
4032 
4033 		cmd = sctio->cmd;
4034 		/*
4035 		 * Reset qla_tgt_cmd SRR values and SGL pointer+count to follow
4036 		 * tcm_qla2xxx_write_pending() and tcm_qla2xxx_queue_data_in()
4037 		 * logic..
4038 		 */
4039 		cmd->offset = 0;
4040 		if (cmd->free_sg) {
4041 			kfree(cmd->sg);
4042 			cmd->sg = NULL;
4043 			cmd->free_sg = 0;
4044 		}
4045 		se_cmd = &cmd->se_cmd;
4046 
4047 		cmd->sg_cnt = se_cmd->t_data_nents;
4048 		cmd->sg = se_cmd->t_data_sg;
4049 
4050 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf02c,
4051 		       "SRR cmd %p (se_cmd %p, tag %lld, op %x), sg_cnt=%d, offset=%d",
4052 		       cmd, &cmd->se_cmd, se_cmd->tag, se_cmd->t_task_cdb ?
4053 		       se_cmd->t_task_cdb[0] : 0, cmd->sg_cnt, cmd->offset);
4054 
4055 		qlt_handle_srr(vha, sctio, imm);
4056 
4057 		kfree(imm);
4058 		kfree(sctio);
4059 		goto restart;
4060 	}
4061 	spin_unlock_irqrestore(&tgt->srr_lock, flags);
4062 }
4063 
4064 /* ha->hardware_lock supposed to be held on entry */
4065 static void qlt_prepare_srr_imm(struct scsi_qla_host *vha,
4066 	struct imm_ntfy_from_isp *iocb)
4067 {
4068 	struct qla_tgt_srr_imm *imm;
4069 	struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4070 	struct qla_tgt_srr_ctio *sctio;
4071 
4072 	tgt->imm_srr_id++;
4073 
4074 	ql_log(ql_log_warn, vha, 0xf02d, "qla_target(%d): SRR received\n",
4075 	    vha->vp_idx);
4076 
4077 	imm = kzalloc(sizeof(*imm), GFP_ATOMIC);
4078 	if (imm != NULL) {
4079 		memcpy(&imm->imm_ntfy, iocb, sizeof(imm->imm_ntfy));
4080 
4081 		/* IRQ is already OFF */
4082 		spin_lock(&tgt->srr_lock);
4083 		imm->srr_id = tgt->imm_srr_id;
4084 		list_add_tail(&imm->srr_list_entry,
4085 		    &tgt->srr_imm_list);
4086 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf02e,
4087 		    "IMM NTFY SRR %p added (id %d, ui %x)\n",
4088 		    imm, imm->srr_id, iocb->u.isp24.srr_ui);
4089 		if (tgt->imm_srr_id == tgt->ctio_srr_id) {
4090 			int found = 0;
4091 			list_for_each_entry(sctio, &tgt->srr_ctio_list,
4092 			    srr_list_entry) {
4093 				if (sctio->srr_id == imm->srr_id) {
4094 					found = 1;
4095 					break;
4096 				}
4097 			}
4098 			if (found) {
4099 				ql_dbg(ql_dbg_tgt_mgt, vha, 0xf02f, "%s",
4100 				    "Scheduling srr work\n");
4101 				schedule_work(&tgt->srr_work);
4102 			} else {
4103 				ql_dbg(ql_dbg_tgt_mgt, vha, 0xf030,
4104 				    "qla_target(%d): imm_srr_id "
4105 				    "== ctio_srr_id (%d), but there is no "
4106 				    "corresponding SRR CTIO, deleting IMM "
4107 				    "SRR %p\n", vha->vp_idx, tgt->ctio_srr_id,
4108 				    imm);
4109 				list_del(&imm->srr_list_entry);
4110 
4111 				kfree(imm);
4112 
4113 				spin_unlock(&tgt->srr_lock);
4114 				goto out_reject;
4115 			}
4116 		}
4117 		spin_unlock(&tgt->srr_lock);
4118 	} else {
4119 		struct qla_tgt_srr_ctio *ts;
4120 
4121 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf069,
4122 		    "qla_target(%d): Unable to allocate SRR IMM "
4123 		    "entry, SRR request will be rejected\n", vha->vp_idx);
4124 
4125 		/* IRQ is already OFF */
4126 		spin_lock(&tgt->srr_lock);
4127 		list_for_each_entry_safe(sctio, ts, &tgt->srr_ctio_list,
4128 		    srr_list_entry) {
4129 			if (sctio->srr_id == tgt->imm_srr_id) {
4130 				ql_dbg(ql_dbg_tgt_mgt, vha, 0xf031,
4131 				    "CTIO SRR %p deleted (id %d)\n",
4132 				    sctio, sctio->srr_id);
4133 				list_del(&sctio->srr_list_entry);
4134 				qlt_send_term_exchange(vha, sctio->cmd,
4135 				    &sctio->cmd->atio, 1);
4136 				kfree(sctio);
4137 			}
4138 		}
4139 		spin_unlock(&tgt->srr_lock);
4140 		goto out_reject;
4141 	}
4142 
4143 	return;
4144 
4145 out_reject:
4146 	qlt_send_notify_ack(vha, iocb, 0, 0, 0,
4147 	    NOTIFY_ACK_SRR_FLAGS_REJECT,
4148 	    NOTIFY_ACK_SRR_REJECT_REASON_UNABLE_TO_PERFORM,
4149 	    NOTIFY_ACK_SRR_FLAGS_REJECT_EXPL_NO_EXPL);
4150 }
4151 
4152 /*
4153  * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
4154  */
4155 static void qlt_handle_imm_notify(struct scsi_qla_host *vha,
4156 	struct imm_ntfy_from_isp *iocb)
4157 {
4158 	struct qla_hw_data *ha = vha->hw;
4159 	uint32_t add_flags = 0;
4160 	int send_notify_ack = 1;
4161 	uint16_t status;
4162 
4163 	status = le16_to_cpu(iocb->u.isp2x.status);
4164 	switch (status) {
4165 	case IMM_NTFY_LIP_RESET:
4166 	{
4167 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf032,
4168 		    "qla_target(%d): LIP reset (loop %#x), subcode %x\n",
4169 		    vha->vp_idx, le16_to_cpu(iocb->u.isp24.nport_handle),
4170 		    iocb->u.isp24.status_subcode);
4171 
4172 		if (qlt_reset(vha, iocb, QLA_TGT_ABORT_ALL) == 0)
4173 			send_notify_ack = 0;
4174 		break;
4175 	}
4176 
4177 	case IMM_NTFY_LIP_LINK_REINIT:
4178 	{
4179 		struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4180 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf033,
4181 		    "qla_target(%d): LINK REINIT (loop %#x, "
4182 		    "subcode %x)\n", vha->vp_idx,
4183 		    le16_to_cpu(iocb->u.isp24.nport_handle),
4184 		    iocb->u.isp24.status_subcode);
4185 		if (tgt->link_reinit_iocb_pending) {
4186 			qlt_send_notify_ack(vha, &tgt->link_reinit_iocb,
4187 			    0, 0, 0, 0, 0, 0);
4188 		}
4189 		memcpy(&tgt->link_reinit_iocb, iocb, sizeof(*iocb));
4190 		tgt->link_reinit_iocb_pending = 1;
4191 		/*
4192 		 * QLogic requires to wait after LINK REINIT for possible
4193 		 * PDISC or ADISC ELS commands
4194 		 */
4195 		send_notify_ack = 0;
4196 		break;
4197 	}
4198 
4199 	case IMM_NTFY_PORT_LOGOUT:
4200 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf034,
4201 		    "qla_target(%d): Port logout (loop "
4202 		    "%#x, subcode %x)\n", vha->vp_idx,
4203 		    le16_to_cpu(iocb->u.isp24.nport_handle),
4204 		    iocb->u.isp24.status_subcode);
4205 
4206 		if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS) == 0)
4207 			send_notify_ack = 0;
4208 		/* The sessions will be cleared in the callback, if needed */
4209 		break;
4210 
4211 	case IMM_NTFY_GLBL_TPRLO:
4212 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf035,
4213 		    "qla_target(%d): Global TPRLO (%x)\n", vha->vp_idx, status);
4214 		if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS) == 0)
4215 			send_notify_ack = 0;
4216 		/* The sessions will be cleared in the callback, if needed */
4217 		break;
4218 
4219 	case IMM_NTFY_PORT_CONFIG:
4220 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf036,
4221 		    "qla_target(%d): Port config changed (%x)\n", vha->vp_idx,
4222 		    status);
4223 		if (qlt_reset(vha, iocb, QLA_TGT_ABORT_ALL) == 0)
4224 			send_notify_ack = 0;
4225 		/* The sessions will be cleared in the callback, if needed */
4226 		break;
4227 
4228 	case IMM_NTFY_GLBL_LOGO:
4229 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06a,
4230 		    "qla_target(%d): Link failure detected\n",
4231 		    vha->vp_idx);
4232 		/* I_T nexus loss */
4233 		if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS) == 0)
4234 			send_notify_ack = 0;
4235 		break;
4236 
4237 	case IMM_NTFY_IOCB_OVERFLOW:
4238 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06b,
4239 		    "qla_target(%d): Cannot provide requested "
4240 		    "capability (IOCB overflowed the immediate notify "
4241 		    "resource count)\n", vha->vp_idx);
4242 		break;
4243 
4244 	case IMM_NTFY_ABORT_TASK:
4245 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf037,
4246 		    "qla_target(%d): Abort Task (S %08x I %#x -> "
4247 		    "L %#x)\n", vha->vp_idx,
4248 		    le16_to_cpu(iocb->u.isp2x.seq_id),
4249 		    GET_TARGET_ID(ha, (struct atio_from_isp *)iocb),
4250 		    le16_to_cpu(iocb->u.isp2x.lun));
4251 		if (qlt_abort_task(vha, iocb) == 0)
4252 			send_notify_ack = 0;
4253 		break;
4254 
4255 	case IMM_NTFY_RESOURCE:
4256 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06c,
4257 		    "qla_target(%d): Out of resources, host %ld\n",
4258 		    vha->vp_idx, vha->host_no);
4259 		break;
4260 
4261 	case IMM_NTFY_MSG_RX:
4262 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf038,
4263 		    "qla_target(%d): Immediate notify task %x\n",
4264 		    vha->vp_idx, iocb->u.isp2x.task_flags);
4265 		if (qlt_handle_task_mgmt(vha, iocb) == 0)
4266 			send_notify_ack = 0;
4267 		break;
4268 
4269 	case IMM_NTFY_ELS:
4270 		if (qlt_24xx_handle_els(vha, iocb) == 0)
4271 			send_notify_ack = 0;
4272 		break;
4273 
4274 	case IMM_NTFY_SRR:
4275 		qlt_prepare_srr_imm(vha, iocb);
4276 		send_notify_ack = 0;
4277 		break;
4278 
4279 	default:
4280 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06d,
4281 		    "qla_target(%d): Received unknown immediate "
4282 		    "notify status %x\n", vha->vp_idx, status);
4283 		break;
4284 	}
4285 
4286 	if (send_notify_ack)
4287 		qlt_send_notify_ack(vha, iocb, add_flags, 0, 0, 0, 0, 0);
4288 }
4289 
4290 /*
4291  * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
4292  * This function sends busy to ISP 2xxx or 24xx.
4293  */
4294 static int __qlt_send_busy(struct scsi_qla_host *vha,
4295 	struct atio_from_isp *atio, uint16_t status)
4296 {
4297 	struct ctio7_to_24xx *ctio24;
4298 	struct qla_hw_data *ha = vha->hw;
4299 	request_t *pkt;
4300 	struct qla_tgt_sess *sess = NULL;
4301 
4302 	sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
4303 	    atio->u.isp24.fcp_hdr.s_id);
4304 	if (!sess) {
4305 		qlt_send_term_exchange(vha, NULL, atio, 1);
4306 		return 0;
4307 	}
4308 	/* Sending marker isn't necessary, since we called from ISR */
4309 
4310 	pkt = (request_t *)qla2x00_alloc_iocbs(vha, NULL);
4311 	if (!pkt) {
4312 		ql_dbg(ql_dbg_io, vha, 0x3063,
4313 		    "qla_target(%d): %s failed: unable to allocate "
4314 		    "request packet", vha->vp_idx, __func__);
4315 		return -ENOMEM;
4316 	}
4317 
4318 	pkt->entry_count = 1;
4319 	pkt->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
4320 
4321 	ctio24 = (struct ctio7_to_24xx *)pkt;
4322 	ctio24->entry_type = CTIO_TYPE7;
4323 	ctio24->nport_handle = sess->loop_id;
4324 	ctio24->timeout = __constant_cpu_to_le16(QLA_TGT_TIMEOUT);
4325 	ctio24->vp_index = vha->vp_idx;
4326 	ctio24->initiator_id[0] = atio->u.isp24.fcp_hdr.s_id[2];
4327 	ctio24->initiator_id[1] = atio->u.isp24.fcp_hdr.s_id[1];
4328 	ctio24->initiator_id[2] = atio->u.isp24.fcp_hdr.s_id[0];
4329 	ctio24->exchange_addr = atio->u.isp24.exchange_addr;
4330 	ctio24->u.status1.flags = (atio->u.isp24.attr << 9) |
4331 	    __constant_cpu_to_le16(
4332 		CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS |
4333 		CTIO7_FLAGS_DONT_RET_CTIO);
4334 	/*
4335 	 * CTIO from fw w/o se_cmd doesn't provide enough info to retry it,
4336 	 * if the explicit conformation is used.
4337 	 */
4338 	ctio24->u.status1.ox_id = swab16(atio->u.isp24.fcp_hdr.ox_id);
4339 	ctio24->u.status1.scsi_status = cpu_to_le16(status);
4340 	/* Memory Barrier */
4341 	wmb();
4342 	qla2x00_start_iocbs(vha, vha->req);
4343 	return 0;
4344 }
4345 
4346 /*
4347  * This routine is used to allocate a command for either a QFull condition
4348  * (ie reply SAM_STAT_BUSY) or to terminate an exchange that did not go
4349  * out previously.
4350  */
4351 static void
4352 qlt_alloc_qfull_cmd(struct scsi_qla_host *vha,
4353 	struct atio_from_isp *atio, uint16_t status, int qfull)
4354 {
4355 	struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4356 	struct qla_hw_data *ha = vha->hw;
4357 	struct qla_tgt_sess *sess;
4358 	struct se_session *se_sess;
4359 	struct qla_tgt_cmd *cmd;
4360 	int tag;
4361 
4362 	if (unlikely(tgt->tgt_stop)) {
4363 		ql_dbg(ql_dbg_io, vha, 0x300a,
4364 			"New command while device %p is shutting down\n", tgt);
4365 		return;
4366 	}
4367 
4368 	if ((vha->hw->tgt.num_qfull_cmds_alloc + 1) > MAX_QFULL_CMDS_ALLOC) {
4369 		vha->hw->tgt.num_qfull_cmds_dropped++;
4370 		if (vha->hw->tgt.num_qfull_cmds_dropped >
4371 			vha->hw->qla_stats.stat_max_qfull_cmds_dropped)
4372 			vha->hw->qla_stats.stat_max_qfull_cmds_dropped =
4373 				vha->hw->tgt.num_qfull_cmds_dropped;
4374 
4375 		ql_dbg(ql_dbg_io, vha, 0x3068,
4376 			"qla_target(%d): %s: QFull CMD dropped[%d]\n",
4377 			vha->vp_idx, __func__,
4378 			vha->hw->tgt.num_qfull_cmds_dropped);
4379 
4380 		qlt_chk_exch_leak_thresh_hold(vha);
4381 		return;
4382 	}
4383 
4384 	sess = ha->tgt.tgt_ops->find_sess_by_s_id
4385 		(vha, atio->u.isp24.fcp_hdr.s_id);
4386 	if (!sess)
4387 		return;
4388 
4389 	se_sess = sess->se_sess;
4390 
4391 	tag = percpu_ida_alloc(&se_sess->sess_tag_pool, TASK_RUNNING);
4392 	if (tag < 0)
4393 		return;
4394 
4395 	cmd = &((struct qla_tgt_cmd *)se_sess->sess_cmd_map)[tag];
4396 	if (!cmd) {
4397 		ql_dbg(ql_dbg_io, vha, 0x3009,
4398 			"qla_target(%d): %s: Allocation of cmd failed\n",
4399 			vha->vp_idx, __func__);
4400 
4401 		vha->hw->tgt.num_qfull_cmds_dropped++;
4402 		if (vha->hw->tgt.num_qfull_cmds_dropped >
4403 			vha->hw->qla_stats.stat_max_qfull_cmds_dropped)
4404 			vha->hw->qla_stats.stat_max_qfull_cmds_dropped =
4405 				vha->hw->tgt.num_qfull_cmds_dropped;
4406 
4407 		qlt_chk_exch_leak_thresh_hold(vha);
4408 		return;
4409 	}
4410 
4411 	memset(cmd, 0, sizeof(struct qla_tgt_cmd));
4412 
4413 	qlt_incr_num_pend_cmds(vha);
4414 	INIT_LIST_HEAD(&cmd->cmd_list);
4415 	memcpy(&cmd->atio, atio, sizeof(*atio));
4416 
4417 	cmd->tgt = vha->vha_tgt.qla_tgt;
4418 	cmd->vha = vha;
4419 	cmd->reset_count = vha->hw->chip_reset;
4420 	cmd->q_full = 1;
4421 
4422 	if (qfull) {
4423 		cmd->q_full = 1;
4424 		/* NOTE: borrowing the state field to carry the status */
4425 		cmd->state = status;
4426 	} else
4427 		cmd->term_exchg = 1;
4428 
4429 	list_add_tail(&cmd->cmd_list, &vha->hw->tgt.q_full_list);
4430 
4431 	vha->hw->tgt.num_qfull_cmds_alloc++;
4432 	if (vha->hw->tgt.num_qfull_cmds_alloc >
4433 		vha->hw->qla_stats.stat_max_qfull_cmds_alloc)
4434 		vha->hw->qla_stats.stat_max_qfull_cmds_alloc =
4435 			vha->hw->tgt.num_qfull_cmds_alloc;
4436 }
4437 
4438 int
4439 qlt_free_qfull_cmds(struct scsi_qla_host *vha)
4440 {
4441 	struct qla_hw_data *ha = vha->hw;
4442 	unsigned long flags;
4443 	struct qla_tgt_cmd *cmd, *tcmd;
4444 	struct list_head free_list;
4445 	int rc = 0;
4446 
4447 	if (list_empty(&ha->tgt.q_full_list))
4448 		return 0;
4449 
4450 	INIT_LIST_HEAD(&free_list);
4451 
4452 	spin_lock_irqsave(&vha->hw->hardware_lock, flags);
4453 
4454 	if (list_empty(&ha->tgt.q_full_list)) {
4455 		spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
4456 		return 0;
4457 	}
4458 
4459 	list_for_each_entry_safe(cmd, tcmd, &ha->tgt.q_full_list, cmd_list) {
4460 		if (cmd->q_full)
4461 			/* cmd->state is a borrowed field to hold status */
4462 			rc = __qlt_send_busy(vha, &cmd->atio, cmd->state);
4463 		else if (cmd->term_exchg)
4464 			rc = __qlt_send_term_exchange(vha, NULL, &cmd->atio);
4465 
4466 		if (rc == -ENOMEM)
4467 			break;
4468 
4469 		if (cmd->q_full)
4470 			ql_dbg(ql_dbg_io, vha, 0x3006,
4471 			    "%s: busy sent for ox_id[%04x]\n", __func__,
4472 			    be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
4473 		else if (cmd->term_exchg)
4474 			ql_dbg(ql_dbg_io, vha, 0x3007,
4475 			    "%s: Term exchg sent for ox_id[%04x]\n", __func__,
4476 			    be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
4477 		else
4478 			ql_dbg(ql_dbg_io, vha, 0x3008,
4479 			    "%s: Unexpected cmd in QFull list %p\n", __func__,
4480 			    cmd);
4481 
4482 		list_del(&cmd->cmd_list);
4483 		list_add_tail(&cmd->cmd_list, &free_list);
4484 
4485 		/* piggy back on hardware_lock for protection */
4486 		vha->hw->tgt.num_qfull_cmds_alloc--;
4487 	}
4488 	spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
4489 
4490 	cmd = NULL;
4491 
4492 	list_for_each_entry_safe(cmd, tcmd, &free_list, cmd_list) {
4493 		list_del(&cmd->cmd_list);
4494 		/* This cmd was never sent to TCM.  There is no need
4495 		 * to schedule free or call free_cmd
4496 		 */
4497 		qlt_free_cmd(cmd);
4498 	}
4499 	return rc;
4500 }
4501 
4502 static void
4503 qlt_send_busy(struct scsi_qla_host *vha,
4504 	struct atio_from_isp *atio, uint16_t status)
4505 {
4506 	int rc = 0;
4507 
4508 	rc = __qlt_send_busy(vha, atio, status);
4509 	if (rc == -ENOMEM)
4510 		qlt_alloc_qfull_cmd(vha, atio, status, 1);
4511 }
4512 
4513 static int
4514 qlt_chk_qfull_thresh_hold(struct scsi_qla_host *vha,
4515 	struct atio_from_isp *atio)
4516 {
4517 	struct qla_hw_data *ha = vha->hw;
4518 	uint16_t status;
4519 
4520 	if (ha->tgt.num_pend_cmds < Q_FULL_THRESH_HOLD(ha))
4521 		return 0;
4522 
4523 	status = temp_sam_status;
4524 	qlt_send_busy(vha, atio, status);
4525 	return 1;
4526 }
4527 
4528 /* ha->hardware_lock supposed to be held on entry */
4529 /* called via callback from qla2xxx */
4530 static void qlt_24xx_atio_pkt(struct scsi_qla_host *vha,
4531 	struct atio_from_isp *atio)
4532 {
4533 	struct qla_hw_data *ha = vha->hw;
4534 	struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4535 	int rc;
4536 
4537 	if (unlikely(tgt == NULL)) {
4538 		ql_dbg(ql_dbg_io, vha, 0x3064,
4539 		    "ATIO pkt, but no tgt (ha %p)", ha);
4540 		return;
4541 	}
4542 	/*
4543 	 * In tgt_stop mode we also should allow all requests to pass.
4544 	 * Otherwise, some commands can stuck.
4545 	 */
4546 
4547 	tgt->irq_cmd_count++;
4548 
4549 	switch (atio->u.raw.entry_type) {
4550 	case ATIO_TYPE7:
4551 		if (unlikely(atio->u.isp24.exchange_addr ==
4552 		    ATIO_EXCHANGE_ADDRESS_UNKNOWN)) {
4553 			ql_dbg(ql_dbg_io, vha, 0x3065,
4554 			    "qla_target(%d): ATIO_TYPE7 "
4555 			    "received with UNKNOWN exchange address, "
4556 			    "sending QUEUE_FULL\n", vha->vp_idx);
4557 			qlt_send_busy(vha, atio, SAM_STAT_TASK_SET_FULL);
4558 			break;
4559 		}
4560 
4561 
4562 
4563 		if (likely(atio->u.isp24.fcp_cmnd.task_mgmt_flags == 0)) {
4564 			rc = qlt_chk_qfull_thresh_hold(vha, atio);
4565 			if (rc != 0) {
4566 				tgt->irq_cmd_count--;
4567 				return;
4568 			}
4569 			rc = qlt_handle_cmd_for_atio(vha, atio);
4570 		} else {
4571 			rc = qlt_handle_task_mgmt(vha, atio);
4572 		}
4573 		if (unlikely(rc != 0)) {
4574 			if (rc == -ESRCH) {
4575 #if 1 /* With TERM EXCHANGE some FC cards refuse to boot */
4576 				qlt_send_busy(vha, atio, SAM_STAT_BUSY);
4577 #else
4578 				qlt_send_term_exchange(vha, NULL, atio, 1);
4579 #endif
4580 			} else {
4581 				if (tgt->tgt_stop) {
4582 					ql_dbg(ql_dbg_tgt, vha, 0xe059,
4583 					    "qla_target: Unable to send "
4584 					    "command to target for req, "
4585 					    "ignoring.\n");
4586 				} else {
4587 					ql_dbg(ql_dbg_tgt, vha, 0xe05a,
4588 					    "qla_target(%d): Unable to send "
4589 					    "command to target, sending BUSY "
4590 					    "status.\n", vha->vp_idx);
4591 					qlt_send_busy(vha, atio, SAM_STAT_BUSY);
4592 				}
4593 			}
4594 		}
4595 		break;
4596 
4597 	case IMMED_NOTIFY_TYPE:
4598 	{
4599 		if (unlikely(atio->u.isp2x.entry_status != 0)) {
4600 			ql_dbg(ql_dbg_tgt, vha, 0xe05b,
4601 			    "qla_target(%d): Received ATIO packet %x "
4602 			    "with error status %x\n", vha->vp_idx,
4603 			    atio->u.raw.entry_type,
4604 			    atio->u.isp2x.entry_status);
4605 			break;
4606 		}
4607 		ql_dbg(ql_dbg_tgt, vha, 0xe02e, "%s", "IMMED_NOTIFY ATIO");
4608 		qlt_handle_imm_notify(vha, (struct imm_ntfy_from_isp *)atio);
4609 		break;
4610 	}
4611 
4612 	default:
4613 		ql_dbg(ql_dbg_tgt, vha, 0xe05c,
4614 		    "qla_target(%d): Received unknown ATIO atio "
4615 		    "type %x\n", vha->vp_idx, atio->u.raw.entry_type);
4616 		break;
4617 	}
4618 
4619 	tgt->irq_cmd_count--;
4620 }
4621 
4622 /* ha->hardware_lock supposed to be held on entry */
4623 /* called via callback from qla2xxx */
4624 static void qlt_response_pkt(struct scsi_qla_host *vha, response_t *pkt)
4625 {
4626 	struct qla_hw_data *ha = vha->hw;
4627 	struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4628 
4629 	if (unlikely(tgt == NULL)) {
4630 		ql_dbg(ql_dbg_tgt, vha, 0xe05d,
4631 		    "qla_target(%d): Response pkt %x received, but no "
4632 		    "tgt (ha %p)\n", vha->vp_idx, pkt->entry_type, ha);
4633 		return;
4634 	}
4635 
4636 	/*
4637 	 * In tgt_stop mode we also should allow all requests to pass.
4638 	 * Otherwise, some commands can stuck.
4639 	 */
4640 
4641 	tgt->irq_cmd_count++;
4642 
4643 	switch (pkt->entry_type) {
4644 	case CTIO_CRC2:
4645 	case CTIO_TYPE7:
4646 	{
4647 		struct ctio7_from_24xx *entry = (struct ctio7_from_24xx *)pkt;
4648 		qlt_do_ctio_completion(vha, entry->handle,
4649 		    le16_to_cpu(entry->status)|(pkt->entry_status << 16),
4650 		    entry);
4651 		break;
4652 	}
4653 
4654 	case ACCEPT_TGT_IO_TYPE:
4655 	{
4656 		struct atio_from_isp *atio = (struct atio_from_isp *)pkt;
4657 		int rc;
4658 		if (atio->u.isp2x.status !=
4659 		    __constant_cpu_to_le16(ATIO_CDB_VALID)) {
4660 			ql_dbg(ql_dbg_tgt, vha, 0xe05e,
4661 			    "qla_target(%d): ATIO with error "
4662 			    "status %x received\n", vha->vp_idx,
4663 			    le16_to_cpu(atio->u.isp2x.status));
4664 			break;
4665 		}
4666 
4667 		rc = qlt_chk_qfull_thresh_hold(vha, atio);
4668 		if (rc != 0) {
4669 			tgt->irq_cmd_count--;
4670 			return;
4671 		}
4672 
4673 		rc = qlt_handle_cmd_for_atio(vha, atio);
4674 		if (unlikely(rc != 0)) {
4675 			if (rc == -ESRCH) {
4676 #if 1 /* With TERM EXCHANGE some FC cards refuse to boot */
4677 				qlt_send_busy(vha, atio, 0);
4678 #else
4679 				qlt_send_term_exchange(vha, NULL, atio, 1);
4680 #endif
4681 			} else {
4682 				if (tgt->tgt_stop) {
4683 					ql_dbg(ql_dbg_tgt, vha, 0xe05f,
4684 					    "qla_target: Unable to send "
4685 					    "command to target, sending TERM "
4686 					    "EXCHANGE for rsp\n");
4687 					qlt_send_term_exchange(vha, NULL,
4688 					    atio, 1);
4689 				} else {
4690 					ql_dbg(ql_dbg_tgt, vha, 0xe060,
4691 					    "qla_target(%d): Unable to send "
4692 					    "command to target, sending BUSY "
4693 					    "status\n", vha->vp_idx);
4694 					qlt_send_busy(vha, atio, 0);
4695 				}
4696 			}
4697 		}
4698 	}
4699 	break;
4700 
4701 	case CONTINUE_TGT_IO_TYPE:
4702 	{
4703 		struct ctio_to_2xxx *entry = (struct ctio_to_2xxx *)pkt;
4704 		qlt_do_ctio_completion(vha, entry->handle,
4705 		    le16_to_cpu(entry->status)|(pkt->entry_status << 16),
4706 		    entry);
4707 		break;
4708 	}
4709 
4710 	case CTIO_A64_TYPE:
4711 	{
4712 		struct ctio_to_2xxx *entry = (struct ctio_to_2xxx *)pkt;
4713 		qlt_do_ctio_completion(vha, entry->handle,
4714 		    le16_to_cpu(entry->status)|(pkt->entry_status << 16),
4715 		    entry);
4716 		break;
4717 	}
4718 
4719 	case IMMED_NOTIFY_TYPE:
4720 		ql_dbg(ql_dbg_tgt, vha, 0xe035, "%s", "IMMED_NOTIFY\n");
4721 		qlt_handle_imm_notify(vha, (struct imm_ntfy_from_isp *)pkt);
4722 		break;
4723 
4724 	case NOTIFY_ACK_TYPE:
4725 		if (tgt->notify_ack_expected > 0) {
4726 			struct nack_to_isp *entry = (struct nack_to_isp *)pkt;
4727 			ql_dbg(ql_dbg_tgt, vha, 0xe036,
4728 			    "NOTIFY_ACK seq %08x status %x\n",
4729 			    le16_to_cpu(entry->u.isp2x.seq_id),
4730 			    le16_to_cpu(entry->u.isp2x.status));
4731 			tgt->notify_ack_expected--;
4732 			if (entry->u.isp2x.status !=
4733 			    __constant_cpu_to_le16(NOTIFY_ACK_SUCCESS)) {
4734 				ql_dbg(ql_dbg_tgt, vha, 0xe061,
4735 				    "qla_target(%d): NOTIFY_ACK "
4736 				    "failed %x\n", vha->vp_idx,
4737 				    le16_to_cpu(entry->u.isp2x.status));
4738 			}
4739 		} else {
4740 			ql_dbg(ql_dbg_tgt, vha, 0xe062,
4741 			    "qla_target(%d): Unexpected NOTIFY_ACK received\n",
4742 			    vha->vp_idx);
4743 		}
4744 		break;
4745 
4746 	case ABTS_RECV_24XX:
4747 		ql_dbg(ql_dbg_tgt, vha, 0xe037,
4748 		    "ABTS_RECV_24XX: instance %d\n", vha->vp_idx);
4749 		qlt_24xx_handle_abts(vha, (struct abts_recv_from_24xx *)pkt);
4750 		break;
4751 
4752 	case ABTS_RESP_24XX:
4753 		if (tgt->abts_resp_expected > 0) {
4754 			struct abts_resp_from_24xx_fw *entry =
4755 				(struct abts_resp_from_24xx_fw *)pkt;
4756 			ql_dbg(ql_dbg_tgt, vha, 0xe038,
4757 			    "ABTS_RESP_24XX: compl_status %x\n",
4758 			    entry->compl_status);
4759 			tgt->abts_resp_expected--;
4760 			if (le16_to_cpu(entry->compl_status) !=
4761 			    ABTS_RESP_COMPL_SUCCESS) {
4762 				if ((entry->error_subcode1 == 0x1E) &&
4763 				    (entry->error_subcode2 == 0)) {
4764 					/*
4765 					 * We've got a race here: aborted
4766 					 * exchange not terminated, i.e.
4767 					 * response for the aborted command was
4768 					 * sent between the abort request was
4769 					 * received and processed.
4770 					 * Unfortunately, the firmware has a
4771 					 * silly requirement that all aborted
4772 					 * exchanges must be explicitely
4773 					 * terminated, otherwise it refuses to
4774 					 * send responses for the abort
4775 					 * requests. So, we have to
4776 					 * (re)terminate the exchange and retry
4777 					 * the abort response.
4778 					 */
4779 					qlt_24xx_retry_term_exchange(vha,
4780 					    entry);
4781 				} else
4782 					ql_dbg(ql_dbg_tgt, vha, 0xe063,
4783 					    "qla_target(%d): ABTS_RESP_24XX "
4784 					    "failed %x (subcode %x:%x)",
4785 					    vha->vp_idx, entry->compl_status,
4786 					    entry->error_subcode1,
4787 					    entry->error_subcode2);
4788 			}
4789 		} else {
4790 			ql_dbg(ql_dbg_tgt, vha, 0xe064,
4791 			    "qla_target(%d): Unexpected ABTS_RESP_24XX "
4792 			    "received\n", vha->vp_idx);
4793 		}
4794 		break;
4795 
4796 	default:
4797 		ql_dbg(ql_dbg_tgt, vha, 0xe065,
4798 		    "qla_target(%d): Received unknown response pkt "
4799 		    "type %x\n", vha->vp_idx, pkt->entry_type);
4800 		break;
4801 	}
4802 
4803 	tgt->irq_cmd_count--;
4804 }
4805 
4806 /*
4807  * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
4808  */
4809 void qlt_async_event(uint16_t code, struct scsi_qla_host *vha,
4810 	uint16_t *mailbox)
4811 {
4812 	struct qla_hw_data *ha = vha->hw;
4813 	struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4814 	int login_code;
4815 
4816 	if (!ha->tgt.tgt_ops)
4817 		return;
4818 
4819 	if (unlikely(tgt == NULL)) {
4820 		ql_dbg(ql_dbg_tgt, vha, 0xe03a,
4821 		    "ASYNC EVENT %#x, but no tgt (ha %p)\n", code, ha);
4822 		return;
4823 	}
4824 
4825 	if (((code == MBA_POINT_TO_POINT) || (code == MBA_CHG_IN_CONNECTION)) &&
4826 	    IS_QLA2100(ha))
4827 		return;
4828 	/*
4829 	 * In tgt_stop mode we also should allow all requests to pass.
4830 	 * Otherwise, some commands can stuck.
4831 	 */
4832 
4833 	tgt->irq_cmd_count++;
4834 
4835 	switch (code) {
4836 	case MBA_RESET:			/* Reset */
4837 	case MBA_SYSTEM_ERR:		/* System Error */
4838 	case MBA_REQ_TRANSFER_ERR:	/* Request Transfer Error */
4839 	case MBA_RSP_TRANSFER_ERR:	/* Response Transfer Error */
4840 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03a,
4841 		    "qla_target(%d): System error async event %#x "
4842 		    "occurred", vha->vp_idx, code);
4843 		break;
4844 	case MBA_WAKEUP_THRES:		/* Request Queue Wake-up. */
4845 		set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
4846 		break;
4847 
4848 	case MBA_LOOP_UP:
4849 	{
4850 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03b,
4851 		    "qla_target(%d): Async LOOP_UP occurred "
4852 		    "(m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)", vha->vp_idx,
4853 		    le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
4854 		    le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
4855 		if (tgt->link_reinit_iocb_pending) {
4856 			qlt_send_notify_ack(vha, (void *)&tgt->link_reinit_iocb,
4857 			    0, 0, 0, 0, 0, 0);
4858 			tgt->link_reinit_iocb_pending = 0;
4859 		}
4860 		break;
4861 	}
4862 
4863 	case MBA_LIP_OCCURRED:
4864 	case MBA_LOOP_DOWN:
4865 	case MBA_LIP_RESET:
4866 	case MBA_RSCN_UPDATE:
4867 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03c,
4868 		    "qla_target(%d): Async event %#x occurred "
4869 		    "(m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)", vha->vp_idx, code,
4870 		    le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
4871 		    le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
4872 		break;
4873 
4874 	case MBA_PORT_UPDATE:
4875 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03d,
4876 		    "qla_target(%d): Port update async event %#x "
4877 		    "occurred: updating the ports database (m[0]=%x, m[1]=%x, "
4878 		    "m[2]=%x, m[3]=%x)", vha->vp_idx, code,
4879 		    le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
4880 		    le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
4881 
4882 		login_code = le16_to_cpu(mailbox[2]);
4883 		if (login_code == 0x4)
4884 			ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03e,
4885 			    "Async MB 2: Got PLOGI Complete\n");
4886 		else if (login_code == 0x7)
4887 			ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03f,
4888 			    "Async MB 2: Port Logged Out\n");
4889 		break;
4890 
4891 	default:
4892 		break;
4893 	}
4894 
4895 	tgt->irq_cmd_count--;
4896 }
4897 
4898 static fc_port_t *qlt_get_port_database(struct scsi_qla_host *vha,
4899 	uint16_t loop_id)
4900 {
4901 	fc_port_t *fcport;
4902 	int rc;
4903 
4904 	fcport = kzalloc(sizeof(*fcport), GFP_KERNEL);
4905 	if (!fcport) {
4906 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06f,
4907 		    "qla_target(%d): Allocation of tmp FC port failed",
4908 		    vha->vp_idx);
4909 		return NULL;
4910 	}
4911 
4912 	fcport->loop_id = loop_id;
4913 
4914 	rc = qla2x00_get_port_database(vha, fcport, 0);
4915 	if (rc != QLA_SUCCESS) {
4916 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf070,
4917 		    "qla_target(%d): Failed to retrieve fcport "
4918 		    "information -- get_port_database() returned %x "
4919 		    "(loop_id=0x%04x)", vha->vp_idx, rc, loop_id);
4920 		kfree(fcport);
4921 		return NULL;
4922 	}
4923 
4924 	return fcport;
4925 }
4926 
4927 /* Must be called under tgt_mutex */
4928 static struct qla_tgt_sess *qlt_make_local_sess(struct scsi_qla_host *vha,
4929 	uint8_t *s_id)
4930 {
4931 	struct qla_tgt_sess *sess = NULL;
4932 	fc_port_t *fcport = NULL;
4933 	int rc, global_resets;
4934 	uint16_t loop_id = 0;
4935 
4936 retry:
4937 	global_resets =
4938 	    atomic_read(&vha->vha_tgt.qla_tgt->tgt_global_resets_count);
4939 
4940 	rc = qla24xx_get_loop_id(vha, s_id, &loop_id);
4941 	if (rc != 0) {
4942 		if ((s_id[0] == 0xFF) &&
4943 		    (s_id[1] == 0xFC)) {
4944 			/*
4945 			 * This is Domain Controller, so it should be
4946 			 * OK to drop SCSI commands from it.
4947 			 */
4948 			ql_dbg(ql_dbg_tgt_mgt, vha, 0xf042,
4949 			    "Unable to find initiator with S_ID %x:%x:%x",
4950 			    s_id[0], s_id[1], s_id[2]);
4951 		} else
4952 			ql_dbg(ql_dbg_tgt_mgt, vha, 0xf071,
4953 			    "qla_target(%d): Unable to find "
4954 			    "initiator with S_ID %x:%x:%x",
4955 			    vha->vp_idx, s_id[0], s_id[1],
4956 			    s_id[2]);
4957 		return NULL;
4958 	}
4959 
4960 	fcport = qlt_get_port_database(vha, loop_id);
4961 	if (!fcport)
4962 		return NULL;
4963 
4964 	if (global_resets !=
4965 	    atomic_read(&vha->vha_tgt.qla_tgt->tgt_global_resets_count)) {
4966 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf043,
4967 		    "qla_target(%d): global reset during session discovery "
4968 		    "(counter was %d, new %d), retrying", vha->vp_idx,
4969 		    global_resets,
4970 		    atomic_read(&vha->vha_tgt.
4971 			qla_tgt->tgt_global_resets_count));
4972 		goto retry;
4973 	}
4974 
4975 	sess = qlt_create_sess(vha, fcport, true);
4976 
4977 	kfree(fcport);
4978 	return sess;
4979 }
4980 
4981 static void qlt_abort_work(struct qla_tgt *tgt,
4982 	struct qla_tgt_sess_work_param *prm)
4983 {
4984 	struct scsi_qla_host *vha = tgt->vha;
4985 	struct qla_hw_data *ha = vha->hw;
4986 	struct qla_tgt_sess *sess = NULL;
4987 	unsigned long flags;
4988 	uint32_t be_s_id;
4989 	uint8_t s_id[3];
4990 	int rc;
4991 
4992 	spin_lock_irqsave(&ha->hardware_lock, flags);
4993 
4994 	if (tgt->tgt_stop)
4995 		goto out_term;
4996 
4997 	s_id[0] = prm->abts.fcp_hdr_le.s_id[2];
4998 	s_id[1] = prm->abts.fcp_hdr_le.s_id[1];
4999 	s_id[2] = prm->abts.fcp_hdr_le.s_id[0];
5000 
5001 	sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
5002 	    (unsigned char *)&be_s_id);
5003 	if (!sess) {
5004 		spin_unlock_irqrestore(&ha->hardware_lock, flags);
5005 
5006 		mutex_lock(&vha->vha_tgt.tgt_mutex);
5007 		sess = qlt_make_local_sess(vha, s_id);
5008 		/* sess has got an extra creation ref */
5009 		mutex_unlock(&vha->vha_tgt.tgt_mutex);
5010 
5011 		spin_lock_irqsave(&ha->hardware_lock, flags);
5012 		if (!sess)
5013 			goto out_term;
5014 	} else {
5015 		kref_get(&sess->se_sess->sess_kref);
5016 	}
5017 
5018 	if (tgt->tgt_stop)
5019 		goto out_term;
5020 
5021 	rc = __qlt_24xx_handle_abts(vha, &prm->abts, sess);
5022 	if (rc != 0)
5023 		goto out_term;
5024 
5025 	ha->tgt.tgt_ops->put_sess(sess);
5026 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
5027 	return;
5028 
5029 out_term:
5030 	qlt_24xx_send_abts_resp(vha, &prm->abts, FCP_TMF_REJECTED, false);
5031 	if (sess)
5032 		ha->tgt.tgt_ops->put_sess(sess);
5033 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
5034 }
5035 
5036 static void qlt_tmr_work(struct qla_tgt *tgt,
5037 	struct qla_tgt_sess_work_param *prm)
5038 {
5039 	struct atio_from_isp *a = &prm->tm_iocb2;
5040 	struct scsi_qla_host *vha = tgt->vha;
5041 	struct qla_hw_data *ha = vha->hw;
5042 	struct qla_tgt_sess *sess = NULL;
5043 	unsigned long flags;
5044 	uint8_t *s_id = NULL; /* to hide compiler warnings */
5045 	int rc;
5046 	uint32_t lun, unpacked_lun;
5047 	int lun_size, fn;
5048 	void *iocb;
5049 
5050 	spin_lock_irqsave(&ha->hardware_lock, flags);
5051 
5052 	if (tgt->tgt_stop)
5053 		goto out_term;
5054 
5055 	s_id = prm->tm_iocb2.u.isp24.fcp_hdr.s_id;
5056 	sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id);
5057 	if (!sess) {
5058 		spin_unlock_irqrestore(&ha->hardware_lock, flags);
5059 
5060 		mutex_lock(&vha->vha_tgt.tgt_mutex);
5061 		sess = qlt_make_local_sess(vha, s_id);
5062 		/* sess has got an extra creation ref */
5063 		mutex_unlock(&vha->vha_tgt.tgt_mutex);
5064 
5065 		spin_lock_irqsave(&ha->hardware_lock, flags);
5066 		if (!sess)
5067 			goto out_term;
5068 	} else {
5069 		kref_get(&sess->se_sess->sess_kref);
5070 	}
5071 
5072 	iocb = a;
5073 	lun = a->u.isp24.fcp_cmnd.lun;
5074 	lun_size = sizeof(lun);
5075 	fn = a->u.isp24.fcp_cmnd.task_mgmt_flags;
5076 	unpacked_lun = scsilun_to_int((struct scsi_lun *)&lun);
5077 
5078 	rc = qlt_issue_task_mgmt(sess, unpacked_lun, fn, iocb, 0);
5079 	if (rc != 0)
5080 		goto out_term;
5081 
5082 	ha->tgt.tgt_ops->put_sess(sess);
5083 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
5084 	return;
5085 
5086 out_term:
5087 	qlt_send_term_exchange(vha, NULL, &prm->tm_iocb2, 1);
5088 	if (sess)
5089 		ha->tgt.tgt_ops->put_sess(sess);
5090 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
5091 }
5092 
5093 static void qlt_sess_work_fn(struct work_struct *work)
5094 {
5095 	struct qla_tgt *tgt = container_of(work, struct qla_tgt, sess_work);
5096 	struct scsi_qla_host *vha = tgt->vha;
5097 	unsigned long flags;
5098 
5099 	ql_dbg(ql_dbg_tgt_mgt, vha, 0xf000, "Sess work (tgt %p)", tgt);
5100 
5101 	spin_lock_irqsave(&tgt->sess_work_lock, flags);
5102 	while (!list_empty(&tgt->sess_works_list)) {
5103 		struct qla_tgt_sess_work_param *prm = list_entry(
5104 		    tgt->sess_works_list.next, typeof(*prm),
5105 		    sess_works_list_entry);
5106 
5107 		/*
5108 		 * This work can be scheduled on several CPUs at time, so we
5109 		 * must delete the entry to eliminate double processing
5110 		 */
5111 		list_del(&prm->sess_works_list_entry);
5112 
5113 		spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
5114 
5115 		switch (prm->type) {
5116 		case QLA_TGT_SESS_WORK_ABORT:
5117 			qlt_abort_work(tgt, prm);
5118 			break;
5119 		case QLA_TGT_SESS_WORK_TM:
5120 			qlt_tmr_work(tgt, prm);
5121 			break;
5122 		default:
5123 			BUG_ON(1);
5124 			break;
5125 		}
5126 
5127 		spin_lock_irqsave(&tgt->sess_work_lock, flags);
5128 
5129 		kfree(prm);
5130 	}
5131 	spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
5132 }
5133 
5134 /* Must be called under tgt_host_action_mutex */
5135 int qlt_add_target(struct qla_hw_data *ha, struct scsi_qla_host *base_vha)
5136 {
5137 	struct qla_tgt *tgt;
5138 
5139 	if (!QLA_TGT_MODE_ENABLED())
5140 		return 0;
5141 
5142 	if (!IS_TGT_MODE_CAPABLE(ha)) {
5143 		ql_log(ql_log_warn, base_vha, 0xe070,
5144 		    "This adapter does not support target mode.\n");
5145 		return 0;
5146 	}
5147 
5148 	ql_dbg(ql_dbg_tgt, base_vha, 0xe03b,
5149 	    "Registering target for host %ld(%p).\n", base_vha->host_no, ha);
5150 
5151 	BUG_ON(base_vha->vha_tgt.qla_tgt != NULL);
5152 
5153 	tgt = kzalloc(sizeof(struct qla_tgt), GFP_KERNEL);
5154 	if (!tgt) {
5155 		ql_dbg(ql_dbg_tgt, base_vha, 0xe066,
5156 		    "Unable to allocate struct qla_tgt\n");
5157 		return -ENOMEM;
5158 	}
5159 
5160 	if (!(base_vha->host->hostt->supported_mode & MODE_TARGET))
5161 		base_vha->host->hostt->supported_mode |= MODE_TARGET;
5162 
5163 	tgt->ha = ha;
5164 	tgt->vha = base_vha;
5165 	init_waitqueue_head(&tgt->waitQ);
5166 	INIT_LIST_HEAD(&tgt->sess_list);
5167 	INIT_LIST_HEAD(&tgt->del_sess_list);
5168 	INIT_DELAYED_WORK(&tgt->sess_del_work,
5169 		(void (*)(struct work_struct *))qlt_del_sess_work_fn);
5170 	spin_lock_init(&tgt->sess_work_lock);
5171 	INIT_WORK(&tgt->sess_work, qlt_sess_work_fn);
5172 	INIT_LIST_HEAD(&tgt->sess_works_list);
5173 	spin_lock_init(&tgt->srr_lock);
5174 	INIT_LIST_HEAD(&tgt->srr_ctio_list);
5175 	INIT_LIST_HEAD(&tgt->srr_imm_list);
5176 	INIT_WORK(&tgt->srr_work, qlt_handle_srr_work);
5177 	atomic_set(&tgt->tgt_global_resets_count, 0);
5178 
5179 	base_vha->vha_tgt.qla_tgt = tgt;
5180 
5181 	ql_dbg(ql_dbg_tgt, base_vha, 0xe067,
5182 		"qla_target(%d): using 64 Bit PCI addressing",
5183 		base_vha->vp_idx);
5184 	tgt->tgt_enable_64bit_addr = 1;
5185 	/* 3 is reserved */
5186 	tgt->sg_tablesize = QLA_TGT_MAX_SG_24XX(base_vha->req->length - 3);
5187 	tgt->datasegs_per_cmd = QLA_TGT_DATASEGS_PER_CMD_24XX;
5188 	tgt->datasegs_per_cont = QLA_TGT_DATASEGS_PER_CONT_24XX;
5189 
5190 	if (base_vha->fc_vport)
5191 		return 0;
5192 
5193 	mutex_lock(&qla_tgt_mutex);
5194 	list_add_tail(&tgt->tgt_list_entry, &qla_tgt_glist);
5195 	mutex_unlock(&qla_tgt_mutex);
5196 
5197 	return 0;
5198 }
5199 
5200 /* Must be called under tgt_host_action_mutex */
5201 int qlt_remove_target(struct qla_hw_data *ha, struct scsi_qla_host *vha)
5202 {
5203 	if (!vha->vha_tgt.qla_tgt)
5204 		return 0;
5205 
5206 	if (vha->fc_vport) {
5207 		qlt_release(vha->vha_tgt.qla_tgt);
5208 		return 0;
5209 	}
5210 
5211 	/* free left over qfull cmds */
5212 	qlt_init_term_exchange(vha);
5213 
5214 	mutex_lock(&qla_tgt_mutex);
5215 	list_del(&vha->vha_tgt.qla_tgt->tgt_list_entry);
5216 	mutex_unlock(&qla_tgt_mutex);
5217 
5218 	ql_dbg(ql_dbg_tgt, vha, 0xe03c, "Unregistering target for host %ld(%p)",
5219 	    vha->host_no, ha);
5220 	qlt_release(vha->vha_tgt.qla_tgt);
5221 
5222 	return 0;
5223 }
5224 
5225 static void qlt_lport_dump(struct scsi_qla_host *vha, u64 wwpn,
5226 	unsigned char *b)
5227 {
5228 	int i;
5229 
5230 	pr_debug("qla2xxx HW vha->node_name: ");
5231 	for (i = 0; i < WWN_SIZE; i++)
5232 		pr_debug("%02x ", vha->node_name[i]);
5233 	pr_debug("\n");
5234 	pr_debug("qla2xxx HW vha->port_name: ");
5235 	for (i = 0; i < WWN_SIZE; i++)
5236 		pr_debug("%02x ", vha->port_name[i]);
5237 	pr_debug("\n");
5238 
5239 	pr_debug("qla2xxx passed configfs WWPN: ");
5240 	put_unaligned_be64(wwpn, b);
5241 	for (i = 0; i < WWN_SIZE; i++)
5242 		pr_debug("%02x ", b[i]);
5243 	pr_debug("\n");
5244 }
5245 
5246 /**
5247  * qla_tgt_lport_register - register lport with external module
5248  *
5249  * @qla_tgt_ops: Pointer for tcm_qla2xxx qla_tgt_ops
5250  * @wwpn: Passwd FC target WWPN
5251  * @callback:  lport initialization callback for tcm_qla2xxx code
5252  * @target_lport_ptr: pointer for tcm_qla2xxx specific lport data
5253  */
5254 int qlt_lport_register(void *target_lport_ptr, u64 phys_wwpn,
5255 		       u64 npiv_wwpn, u64 npiv_wwnn,
5256 		       int (*callback)(struct scsi_qla_host *, void *, u64, u64))
5257 {
5258 	struct qla_tgt *tgt;
5259 	struct scsi_qla_host *vha;
5260 	struct qla_hw_data *ha;
5261 	struct Scsi_Host *host;
5262 	unsigned long flags;
5263 	int rc;
5264 	u8 b[WWN_SIZE];
5265 
5266 	mutex_lock(&qla_tgt_mutex);
5267 	list_for_each_entry(tgt, &qla_tgt_glist, tgt_list_entry) {
5268 		vha = tgt->vha;
5269 		ha = vha->hw;
5270 
5271 		host = vha->host;
5272 		if (!host)
5273 			continue;
5274 
5275 		if (!(host->hostt->supported_mode & MODE_TARGET))
5276 			continue;
5277 
5278 		spin_lock_irqsave(&ha->hardware_lock, flags);
5279 		if ((!npiv_wwpn || !npiv_wwnn) && host->active_mode & MODE_TARGET) {
5280 			pr_debug("MODE_TARGET already active on qla2xxx(%d)\n",
5281 			    host->host_no);
5282 			spin_unlock_irqrestore(&ha->hardware_lock, flags);
5283 			continue;
5284 		}
5285 		if (tgt->tgt_stop) {
5286 			pr_debug("MODE_TARGET in shutdown on qla2xxx(%d)\n",
5287 				 host->host_no);
5288 			spin_unlock_irqrestore(&ha->hardware_lock, flags);
5289 			continue;
5290 		}
5291 		spin_unlock_irqrestore(&ha->hardware_lock, flags);
5292 
5293 		if (!scsi_host_get(host)) {
5294 			ql_dbg(ql_dbg_tgt, vha, 0xe068,
5295 			    "Unable to scsi_host_get() for"
5296 			    " qla2xxx scsi_host\n");
5297 			continue;
5298 		}
5299 		qlt_lport_dump(vha, phys_wwpn, b);
5300 
5301 		if (memcmp(vha->port_name, b, WWN_SIZE)) {
5302 			scsi_host_put(host);
5303 			continue;
5304 		}
5305 		rc = (*callback)(vha, target_lport_ptr, npiv_wwpn, npiv_wwnn);
5306 		if (rc != 0)
5307 			scsi_host_put(host);
5308 
5309 		mutex_unlock(&qla_tgt_mutex);
5310 		return rc;
5311 	}
5312 	mutex_unlock(&qla_tgt_mutex);
5313 
5314 	return -ENODEV;
5315 }
5316 EXPORT_SYMBOL(qlt_lport_register);
5317 
5318 /**
5319  * qla_tgt_lport_deregister - Degister lport
5320  *
5321  * @vha:  Registered scsi_qla_host pointer
5322  */
5323 void qlt_lport_deregister(struct scsi_qla_host *vha)
5324 {
5325 	struct qla_hw_data *ha = vha->hw;
5326 	struct Scsi_Host *sh = vha->host;
5327 	/*
5328 	 * Clear the target_lport_ptr qla_target_template pointer in qla_hw_data
5329 	 */
5330 	vha->vha_tgt.target_lport_ptr = NULL;
5331 	ha->tgt.tgt_ops = NULL;
5332 	/*
5333 	 * Release the Scsi_Host reference for the underlying qla2xxx host
5334 	 */
5335 	scsi_host_put(sh);
5336 }
5337 EXPORT_SYMBOL(qlt_lport_deregister);
5338 
5339 /* Must be called under HW lock */
5340 static void qlt_set_mode(struct scsi_qla_host *vha)
5341 {
5342 	struct qla_hw_data *ha = vha->hw;
5343 
5344 	switch (ql2x_ini_mode) {
5345 	case QLA2XXX_INI_MODE_DISABLED:
5346 	case QLA2XXX_INI_MODE_EXCLUSIVE:
5347 		vha->host->active_mode = MODE_TARGET;
5348 		break;
5349 	case QLA2XXX_INI_MODE_ENABLED:
5350 		vha->host->active_mode |= MODE_TARGET;
5351 		break;
5352 	default:
5353 		break;
5354 	}
5355 
5356 	if (ha->tgt.ini_mode_force_reverse)
5357 		qla_reverse_ini_mode(vha);
5358 }
5359 
5360 /* Must be called under HW lock */
5361 static void qlt_clear_mode(struct scsi_qla_host *vha)
5362 {
5363 	struct qla_hw_data *ha = vha->hw;
5364 
5365 	switch (ql2x_ini_mode) {
5366 	case QLA2XXX_INI_MODE_DISABLED:
5367 		vha->host->active_mode = MODE_UNKNOWN;
5368 		break;
5369 	case QLA2XXX_INI_MODE_EXCLUSIVE:
5370 		vha->host->active_mode = MODE_INITIATOR;
5371 		break;
5372 	case QLA2XXX_INI_MODE_ENABLED:
5373 		vha->host->active_mode &= ~MODE_TARGET;
5374 		break;
5375 	default:
5376 		break;
5377 	}
5378 
5379 	if (ha->tgt.ini_mode_force_reverse)
5380 		qla_reverse_ini_mode(vha);
5381 }
5382 
5383 /*
5384  * qla_tgt_enable_vha - NO LOCK HELD
5385  *
5386  * host_reset, bring up w/ Target Mode Enabled
5387  */
5388 void
5389 qlt_enable_vha(struct scsi_qla_host *vha)
5390 {
5391 	struct qla_hw_data *ha = vha->hw;
5392 	struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5393 	unsigned long flags;
5394 	scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
5395 
5396 	if (!tgt) {
5397 		ql_dbg(ql_dbg_tgt, vha, 0xe069,
5398 		    "Unable to locate qla_tgt pointer from"
5399 		    " struct qla_hw_data\n");
5400 		dump_stack();
5401 		return;
5402 	}
5403 
5404 	spin_lock_irqsave(&ha->hardware_lock, flags);
5405 	tgt->tgt_stopped = 0;
5406 	qlt_set_mode(vha);
5407 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
5408 
5409 	if (vha->vp_idx) {
5410 		qla24xx_disable_vp(vha);
5411 		qla24xx_enable_vp(vha);
5412 	} else {
5413 		set_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags);
5414 		qla2xxx_wake_dpc(base_vha);
5415 		qla2x00_wait_for_hba_online(base_vha);
5416 	}
5417 }
5418 EXPORT_SYMBOL(qlt_enable_vha);
5419 
5420 /*
5421  * qla_tgt_disable_vha - NO LOCK HELD
5422  *
5423  * Disable Target Mode and reset the adapter
5424  */
5425 static void qlt_disable_vha(struct scsi_qla_host *vha)
5426 {
5427 	struct qla_hw_data *ha = vha->hw;
5428 	struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5429 	unsigned long flags;
5430 
5431 	if (!tgt) {
5432 		ql_dbg(ql_dbg_tgt, vha, 0xe06a,
5433 		    "Unable to locate qla_tgt pointer from"
5434 		    " struct qla_hw_data\n");
5435 		dump_stack();
5436 		return;
5437 	}
5438 
5439 	spin_lock_irqsave(&ha->hardware_lock, flags);
5440 	qlt_clear_mode(vha);
5441 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
5442 
5443 	set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
5444 	qla2xxx_wake_dpc(vha);
5445 	qla2x00_wait_for_hba_online(vha);
5446 }
5447 
5448 /*
5449  * Called from qla_init.c:qla24xx_vport_create() contex to setup
5450  * the target mode specific struct scsi_qla_host and struct qla_hw_data
5451  * members.
5452  */
5453 void
5454 qlt_vport_create(struct scsi_qla_host *vha, struct qla_hw_data *ha)
5455 {
5456 	if (!qla_tgt_mode_enabled(vha))
5457 		return;
5458 
5459 	vha->vha_tgt.qla_tgt = NULL;
5460 
5461 	mutex_init(&vha->vha_tgt.tgt_mutex);
5462 	mutex_init(&vha->vha_tgt.tgt_host_action_mutex);
5463 
5464 	qlt_clear_mode(vha);
5465 
5466 	/*
5467 	 * NOTE: Currently the value is kept the same for <24xx and
5468 	 * >=24xx ISPs. If it is necessary to change it,
5469 	 * the check should be added for specific ISPs,
5470 	 * assigning the value appropriately.
5471 	 */
5472 	ha->tgt.atio_q_length = ATIO_ENTRY_CNT_24XX;
5473 
5474 	qlt_add_target(ha, vha);
5475 }
5476 
5477 void
5478 qlt_rff_id(struct scsi_qla_host *vha, struct ct_sns_req *ct_req)
5479 {
5480 	/*
5481 	 * FC-4 Feature bit 0 indicates target functionality to the name server.
5482 	 */
5483 	if (qla_tgt_mode_enabled(vha)) {
5484 		if (qla_ini_mode_enabled(vha))
5485 			ct_req->req.rff_id.fc4_feature = BIT_0 | BIT_1;
5486 		else
5487 			ct_req->req.rff_id.fc4_feature = BIT_0;
5488 	} else if (qla_ini_mode_enabled(vha)) {
5489 		ct_req->req.rff_id.fc4_feature = BIT_1;
5490 	}
5491 }
5492 
5493 /*
5494  * qlt_init_atio_q_entries() - Initializes ATIO queue entries.
5495  * @ha: HA context
5496  *
5497  * Beginning of ATIO ring has initialization control block already built
5498  * by nvram config routine.
5499  *
5500  * Returns 0 on success.
5501  */
5502 void
5503 qlt_init_atio_q_entries(struct scsi_qla_host *vha)
5504 {
5505 	struct qla_hw_data *ha = vha->hw;
5506 	uint16_t cnt;
5507 	struct atio_from_isp *pkt = (struct atio_from_isp *)ha->tgt.atio_ring;
5508 
5509 	if (!qla_tgt_mode_enabled(vha))
5510 		return;
5511 
5512 	for (cnt = 0; cnt < ha->tgt.atio_q_length; cnt++) {
5513 		pkt->u.raw.signature = ATIO_PROCESSED;
5514 		pkt++;
5515 	}
5516 
5517 }
5518 
5519 /*
5520  * qlt_24xx_process_atio_queue() - Process ATIO queue entries.
5521  * @ha: SCSI driver HA context
5522  */
5523 void
5524 qlt_24xx_process_atio_queue(struct scsi_qla_host *vha)
5525 {
5526 	struct qla_hw_data *ha = vha->hw;
5527 	struct atio_from_isp *pkt;
5528 	int cnt, i;
5529 
5530 	if (!vha->flags.online)
5531 		return;
5532 
5533 	while (ha->tgt.atio_ring_ptr->signature != ATIO_PROCESSED) {
5534 		pkt = (struct atio_from_isp *)ha->tgt.atio_ring_ptr;
5535 		cnt = pkt->u.raw.entry_count;
5536 
5537 		qlt_24xx_atio_pkt_all_vps(vha, (struct atio_from_isp *)pkt);
5538 
5539 		for (i = 0; i < cnt; i++) {
5540 			ha->tgt.atio_ring_index++;
5541 			if (ha->tgt.atio_ring_index == ha->tgt.atio_q_length) {
5542 				ha->tgt.atio_ring_index = 0;
5543 				ha->tgt.atio_ring_ptr = ha->tgt.atio_ring;
5544 			} else
5545 				ha->tgt.atio_ring_ptr++;
5546 
5547 			pkt->u.raw.signature = ATIO_PROCESSED;
5548 			pkt = (struct atio_from_isp *)ha->tgt.atio_ring_ptr;
5549 		}
5550 		wmb();
5551 	}
5552 
5553 	/* Adjust ring index */
5554 	WRT_REG_DWORD(ISP_ATIO_Q_OUT(vha), ha->tgt.atio_ring_index);
5555 }
5556 
5557 void
5558 qlt_24xx_config_rings(struct scsi_qla_host *vha)
5559 {
5560 	struct qla_hw_data *ha = vha->hw;
5561 	if (!QLA_TGT_MODE_ENABLED())
5562 		return;
5563 
5564 	WRT_REG_DWORD(ISP_ATIO_Q_IN(vha), 0);
5565 	WRT_REG_DWORD(ISP_ATIO_Q_OUT(vha), 0);
5566 	RD_REG_DWORD(ISP_ATIO_Q_OUT(vha));
5567 
5568 	if (IS_ATIO_MSIX_CAPABLE(ha)) {
5569 		struct qla_msix_entry *msix = &ha->msix_entries[2];
5570 		struct init_cb_24xx *icb = (struct init_cb_24xx *)ha->init_cb;
5571 
5572 		icb->msix_atio = cpu_to_le16(msix->entry);
5573 		ql_dbg(ql_dbg_init, vha, 0xf072,
5574 		    "Registering ICB vector 0x%x for atio que.\n",
5575 		    msix->entry);
5576 	}
5577 }
5578 
5579 void
5580 qlt_24xx_config_nvram_stage1(struct scsi_qla_host *vha, struct nvram_24xx *nv)
5581 {
5582 	struct qla_hw_data *ha = vha->hw;
5583 
5584 	if (qla_tgt_mode_enabled(vha)) {
5585 		if (!ha->tgt.saved_set) {
5586 			/* We save only once */
5587 			ha->tgt.saved_exchange_count = nv->exchange_count;
5588 			ha->tgt.saved_firmware_options_1 =
5589 			    nv->firmware_options_1;
5590 			ha->tgt.saved_firmware_options_2 =
5591 			    nv->firmware_options_2;
5592 			ha->tgt.saved_firmware_options_3 =
5593 			    nv->firmware_options_3;
5594 			ha->tgt.saved_set = 1;
5595 		}
5596 
5597 		nv->exchange_count = __constant_cpu_to_le16(0xFFFF);
5598 
5599 		/* Enable target mode */
5600 		nv->firmware_options_1 |= __constant_cpu_to_le32(BIT_4);
5601 
5602 		/* Disable ini mode, if requested */
5603 		if (!qla_ini_mode_enabled(vha))
5604 			nv->firmware_options_1 |= __constant_cpu_to_le32(BIT_5);
5605 
5606 		/* Disable Full Login after LIP */
5607 		nv->firmware_options_1 &= __constant_cpu_to_le32(~BIT_13);
5608 		/* Enable initial LIP */
5609 		nv->firmware_options_1 &= __constant_cpu_to_le32(~BIT_9);
5610 		if (ql2xtgt_tape_enable)
5611 			/* Enable FC Tape support */
5612 			nv->firmware_options_2 |= cpu_to_le32(BIT_12);
5613 		else
5614 			/* Disable FC Tape support */
5615 			nv->firmware_options_2 &= cpu_to_le32(~BIT_12);
5616 
5617 		/* Disable Full Login after LIP */
5618 		nv->host_p &= __constant_cpu_to_le32(~BIT_10);
5619 		/* Enable target PRLI control */
5620 		nv->firmware_options_2 |= __constant_cpu_to_le32(BIT_14);
5621 	} else {
5622 		if (ha->tgt.saved_set) {
5623 			nv->exchange_count = ha->tgt.saved_exchange_count;
5624 			nv->firmware_options_1 =
5625 			    ha->tgt.saved_firmware_options_1;
5626 			nv->firmware_options_2 =
5627 			    ha->tgt.saved_firmware_options_2;
5628 			nv->firmware_options_3 =
5629 			    ha->tgt.saved_firmware_options_3;
5630 		}
5631 		return;
5632 	}
5633 
5634 	/* out-of-order frames reassembly */
5635 	nv->firmware_options_3 |= BIT_6|BIT_9;
5636 
5637 	if (ha->tgt.enable_class_2) {
5638 		if (vha->flags.init_done)
5639 			fc_host_supported_classes(vha->host) =
5640 				FC_COS_CLASS2 | FC_COS_CLASS3;
5641 
5642 		nv->firmware_options_2 |= __constant_cpu_to_le32(BIT_8);
5643 	} else {
5644 		if (vha->flags.init_done)
5645 			fc_host_supported_classes(vha->host) = FC_COS_CLASS3;
5646 
5647 		nv->firmware_options_2 &= ~__constant_cpu_to_le32(BIT_8);
5648 	}
5649 }
5650 
5651 void
5652 qlt_24xx_config_nvram_stage2(struct scsi_qla_host *vha,
5653 	struct init_cb_24xx *icb)
5654 {
5655 	struct qla_hw_data *ha = vha->hw;
5656 
5657 	if (ha->tgt.node_name_set) {
5658 		memcpy(icb->node_name, ha->tgt.tgt_node_name, WWN_SIZE);
5659 		icb->firmware_options_1 |= __constant_cpu_to_le32(BIT_14);
5660 	}
5661 }
5662 
5663 void
5664 qlt_81xx_config_nvram_stage1(struct scsi_qla_host *vha, struct nvram_81xx *nv)
5665 {
5666 	struct qla_hw_data *ha = vha->hw;
5667 
5668 	if (!QLA_TGT_MODE_ENABLED())
5669 		return;
5670 
5671 	if (qla_tgt_mode_enabled(vha)) {
5672 		if (!ha->tgt.saved_set) {
5673 			/* We save only once */
5674 			ha->tgt.saved_exchange_count = nv->exchange_count;
5675 			ha->tgt.saved_firmware_options_1 =
5676 			    nv->firmware_options_1;
5677 			ha->tgt.saved_firmware_options_2 =
5678 			    nv->firmware_options_2;
5679 			ha->tgt.saved_firmware_options_3 =
5680 			    nv->firmware_options_3;
5681 			ha->tgt.saved_set = 1;
5682 		}
5683 
5684 		nv->exchange_count = __constant_cpu_to_le16(0xFFFF);
5685 
5686 		/* Enable target mode */
5687 		nv->firmware_options_1 |= __constant_cpu_to_le32(BIT_4);
5688 
5689 		/* Disable ini mode, if requested */
5690 		if (!qla_ini_mode_enabled(vha))
5691 			nv->firmware_options_1 |=
5692 			    __constant_cpu_to_le32(BIT_5);
5693 
5694 		/* Disable Full Login after LIP */
5695 		nv->firmware_options_1 &= __constant_cpu_to_le32(~BIT_13);
5696 		/* Enable initial LIP */
5697 		nv->firmware_options_1 &= __constant_cpu_to_le32(~BIT_9);
5698 		if (ql2xtgt_tape_enable)
5699 			/* Enable FC tape support */
5700 			nv->firmware_options_2 |= cpu_to_le32(BIT_12);
5701 		else
5702 			/* Disable FC tape support */
5703 			nv->firmware_options_2 &= cpu_to_le32(~BIT_12);
5704 
5705 		/* Disable Full Login after LIP */
5706 		nv->host_p &= __constant_cpu_to_le32(~BIT_10);
5707 		/* Enable target PRLI control */
5708 		nv->firmware_options_2 |= __constant_cpu_to_le32(BIT_14);
5709 	} else {
5710 		if (ha->tgt.saved_set) {
5711 			nv->exchange_count = ha->tgt.saved_exchange_count;
5712 			nv->firmware_options_1 =
5713 			    ha->tgt.saved_firmware_options_1;
5714 			nv->firmware_options_2 =
5715 			    ha->tgt.saved_firmware_options_2;
5716 			nv->firmware_options_3 =
5717 			    ha->tgt.saved_firmware_options_3;
5718 		}
5719 		return;
5720 	}
5721 
5722 	/* out-of-order frames reassembly */
5723 	nv->firmware_options_3 |= BIT_6|BIT_9;
5724 
5725 	if (ha->tgt.enable_class_2) {
5726 		if (vha->flags.init_done)
5727 			fc_host_supported_classes(vha->host) =
5728 				FC_COS_CLASS2 | FC_COS_CLASS3;
5729 
5730 		nv->firmware_options_2 |= __constant_cpu_to_le32(BIT_8);
5731 	} else {
5732 		if (vha->flags.init_done)
5733 			fc_host_supported_classes(vha->host) = FC_COS_CLASS3;
5734 
5735 		nv->firmware_options_2 &= ~__constant_cpu_to_le32(BIT_8);
5736 	}
5737 }
5738 
5739 void
5740 qlt_81xx_config_nvram_stage2(struct scsi_qla_host *vha,
5741 	struct init_cb_81xx *icb)
5742 {
5743 	struct qla_hw_data *ha = vha->hw;
5744 
5745 	if (!QLA_TGT_MODE_ENABLED())
5746 		return;
5747 
5748 	if (ha->tgt.node_name_set) {
5749 		memcpy(icb->node_name, ha->tgt.tgt_node_name, WWN_SIZE);
5750 		icb->firmware_options_1 |= __constant_cpu_to_le32(BIT_14);
5751 	}
5752 }
5753 
5754 void
5755 qlt_83xx_iospace_config(struct qla_hw_data *ha)
5756 {
5757 	if (!QLA_TGT_MODE_ENABLED())
5758 		return;
5759 
5760 	ha->msix_count += 1; /* For ATIO Q */
5761 }
5762 
5763 int
5764 qlt_24xx_process_response_error(struct scsi_qla_host *vha,
5765 	struct sts_entry_24xx *pkt)
5766 {
5767 	switch (pkt->entry_type) {
5768 	case ABTS_RECV_24XX:
5769 	case ABTS_RESP_24XX:
5770 	case CTIO_TYPE7:
5771 	case NOTIFY_ACK_TYPE:
5772 	case CTIO_CRC2:
5773 		return 1;
5774 	default:
5775 		return 0;
5776 	}
5777 }
5778 
5779 void
5780 qlt_modify_vp_config(struct scsi_qla_host *vha,
5781 	struct vp_config_entry_24xx *vpmod)
5782 {
5783 	if (qla_tgt_mode_enabled(vha))
5784 		vpmod->options_idx1 &= ~BIT_5;
5785 	/* Disable ini mode, if requested */
5786 	if (!qla_ini_mode_enabled(vha))
5787 		vpmod->options_idx1 &= ~BIT_4;
5788 }
5789 
5790 void
5791 qlt_probe_one_stage1(struct scsi_qla_host *base_vha, struct qla_hw_data *ha)
5792 {
5793 	if (!QLA_TGT_MODE_ENABLED())
5794 		return;
5795 
5796 	if  (ha->mqenable || IS_QLA83XX(ha)) {
5797 		ISP_ATIO_Q_IN(base_vha) = &ha->mqiobase->isp25mq.atio_q_in;
5798 		ISP_ATIO_Q_OUT(base_vha) = &ha->mqiobase->isp25mq.atio_q_out;
5799 	} else {
5800 		ISP_ATIO_Q_IN(base_vha) = &ha->iobase->isp24.atio_q_in;
5801 		ISP_ATIO_Q_OUT(base_vha) = &ha->iobase->isp24.atio_q_out;
5802 	}
5803 
5804 	mutex_init(&base_vha->vha_tgt.tgt_mutex);
5805 	mutex_init(&base_vha->vha_tgt.tgt_host_action_mutex);
5806 	qlt_clear_mode(base_vha);
5807 }
5808 
5809 irqreturn_t
5810 qla83xx_msix_atio_q(int irq, void *dev_id)
5811 {
5812 	struct rsp_que *rsp;
5813 	scsi_qla_host_t	*vha;
5814 	struct qla_hw_data *ha;
5815 	unsigned long flags;
5816 
5817 	rsp = (struct rsp_que *) dev_id;
5818 	ha = rsp->hw;
5819 	vha = pci_get_drvdata(ha->pdev);
5820 
5821 	spin_lock_irqsave(&ha->hardware_lock, flags);
5822 
5823 	qlt_24xx_process_atio_queue(vha);
5824 	qla24xx_process_response_queue(vha, rsp);
5825 
5826 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
5827 
5828 	return IRQ_HANDLED;
5829 }
5830 
5831 int
5832 qlt_mem_alloc(struct qla_hw_data *ha)
5833 {
5834 	if (!QLA_TGT_MODE_ENABLED())
5835 		return 0;
5836 
5837 	ha->tgt.tgt_vp_map = kzalloc(sizeof(struct qla_tgt_vp_map) *
5838 	    MAX_MULTI_ID_FABRIC, GFP_KERNEL);
5839 	if (!ha->tgt.tgt_vp_map)
5840 		return -ENOMEM;
5841 
5842 	ha->tgt.atio_ring = dma_alloc_coherent(&ha->pdev->dev,
5843 	    (ha->tgt.atio_q_length + 1) * sizeof(struct atio_from_isp),
5844 	    &ha->tgt.atio_dma, GFP_KERNEL);
5845 	if (!ha->tgt.atio_ring) {
5846 		kfree(ha->tgt.tgt_vp_map);
5847 		return -ENOMEM;
5848 	}
5849 	return 0;
5850 }
5851 
5852 void
5853 qlt_mem_free(struct qla_hw_data *ha)
5854 {
5855 	if (!QLA_TGT_MODE_ENABLED())
5856 		return;
5857 
5858 	if (ha->tgt.atio_ring) {
5859 		dma_free_coherent(&ha->pdev->dev, (ha->tgt.atio_q_length + 1) *
5860 		    sizeof(struct atio_from_isp), ha->tgt.atio_ring,
5861 		    ha->tgt.atio_dma);
5862 	}
5863 	kfree(ha->tgt.tgt_vp_map);
5864 }
5865 
5866 /* vport_slock to be held by the caller */
5867 void
5868 qlt_update_vp_map(struct scsi_qla_host *vha, int cmd)
5869 {
5870 	if (!QLA_TGT_MODE_ENABLED())
5871 		return;
5872 
5873 	switch (cmd) {
5874 	case SET_VP_IDX:
5875 		vha->hw->tgt.tgt_vp_map[vha->vp_idx].vha = vha;
5876 		break;
5877 	case SET_AL_PA:
5878 		vha->hw->tgt.tgt_vp_map[vha->d_id.b.al_pa].idx = vha->vp_idx;
5879 		break;
5880 	case RESET_VP_IDX:
5881 		vha->hw->tgt.tgt_vp_map[vha->vp_idx].vha = NULL;
5882 		break;
5883 	case RESET_AL_PA:
5884 		vha->hw->tgt.tgt_vp_map[vha->d_id.b.al_pa].idx = 0;
5885 		break;
5886 	}
5887 }
5888 
5889 static int __init qlt_parse_ini_mode(void)
5890 {
5891 	if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_EXCLUSIVE) == 0)
5892 		ql2x_ini_mode = QLA2XXX_INI_MODE_EXCLUSIVE;
5893 	else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_DISABLED) == 0)
5894 		ql2x_ini_mode = QLA2XXX_INI_MODE_DISABLED;
5895 	else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_ENABLED) == 0)
5896 		ql2x_ini_mode = QLA2XXX_INI_MODE_ENABLED;
5897 	else
5898 		return false;
5899 
5900 	return true;
5901 }
5902 
5903 int __init qlt_init(void)
5904 {
5905 	int ret;
5906 
5907 	if (!qlt_parse_ini_mode()) {
5908 		ql_log(ql_log_fatal, NULL, 0xe06b,
5909 		    "qlt_parse_ini_mode() failed\n");
5910 		return -EINVAL;
5911 	}
5912 
5913 	if (!QLA_TGT_MODE_ENABLED())
5914 		return 0;
5915 
5916 	qla_tgt_mgmt_cmd_cachep = kmem_cache_create("qla_tgt_mgmt_cmd_cachep",
5917 	    sizeof(struct qla_tgt_mgmt_cmd), __alignof__(struct
5918 	    qla_tgt_mgmt_cmd), 0, NULL);
5919 	if (!qla_tgt_mgmt_cmd_cachep) {
5920 		ql_log(ql_log_fatal, NULL, 0xe06d,
5921 		    "kmem_cache_create for qla_tgt_mgmt_cmd_cachep failed\n");
5922 		return -ENOMEM;
5923 	}
5924 
5925 	qla_tgt_mgmt_cmd_mempool = mempool_create(25, mempool_alloc_slab,
5926 	    mempool_free_slab, qla_tgt_mgmt_cmd_cachep);
5927 	if (!qla_tgt_mgmt_cmd_mempool) {
5928 		ql_log(ql_log_fatal, NULL, 0xe06e,
5929 		    "mempool_create for qla_tgt_mgmt_cmd_mempool failed\n");
5930 		ret = -ENOMEM;
5931 		goto out_mgmt_cmd_cachep;
5932 	}
5933 
5934 	qla_tgt_wq = alloc_workqueue("qla_tgt_wq", 0, 0);
5935 	if (!qla_tgt_wq) {
5936 		ql_log(ql_log_fatal, NULL, 0xe06f,
5937 		    "alloc_workqueue for qla_tgt_wq failed\n");
5938 		ret = -ENOMEM;
5939 		goto out_cmd_mempool;
5940 	}
5941 	/*
5942 	 * Return 1 to signal that initiator-mode is being disabled
5943 	 */
5944 	return (ql2x_ini_mode == QLA2XXX_INI_MODE_DISABLED) ? 1 : 0;
5945 
5946 out_cmd_mempool:
5947 	mempool_destroy(qla_tgt_mgmt_cmd_mempool);
5948 out_mgmt_cmd_cachep:
5949 	kmem_cache_destroy(qla_tgt_mgmt_cmd_cachep);
5950 	return ret;
5951 }
5952 
5953 void qlt_exit(void)
5954 {
5955 	if (!QLA_TGT_MODE_ENABLED())
5956 		return;
5957 
5958 	destroy_workqueue(qla_tgt_wq);
5959 	mempool_destroy(qla_tgt_mgmt_cmd_mempool);
5960 	kmem_cache_destroy(qla_tgt_mgmt_cmd_cachep);
5961 }
5962