1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  qla_target.c SCSI LLD infrastructure for QLogic 22xx/23xx/24xx/25xx
4  *
5  *  based on qla2x00t.c code:
6  *
7  *  Copyright (C) 2004 - 2010 Vladislav Bolkhovitin <vst@vlnb.net>
8  *  Copyright (C) 2004 - 2005 Leonid Stoljar
9  *  Copyright (C) 2006 Nathaniel Clark <nate@misrule.us>
10  *  Copyright (C) 2006 - 2010 ID7 Ltd.
11  *
12  *  Forward port and refactoring to modern qla2xxx and target/configfs
13  *
14  *  Copyright (C) 2010-2013 Nicholas A. Bellinger <nab@kernel.org>
15  */
16 
17 #include <linux/module.h>
18 #include <linux/init.h>
19 #include <linux/types.h>
20 #include <linux/blkdev.h>
21 #include <linux/interrupt.h>
22 #include <linux/pci.h>
23 #include <linux/delay.h>
24 #include <linux/list.h>
25 #include <linux/workqueue.h>
26 #include <asm/unaligned.h>
27 #include <scsi/scsi.h>
28 #include <scsi/scsi_host.h>
29 #include <scsi/scsi_tcq.h>
30 
31 #include "qla_def.h"
32 #include "qla_target.h"
33 
34 static int ql2xtgt_tape_enable;
35 module_param(ql2xtgt_tape_enable, int, S_IRUGO|S_IWUSR);
36 MODULE_PARM_DESC(ql2xtgt_tape_enable,
37 		"Enables Sequence level error recovery (aka FC Tape). Default is 0 - no SLER. 1 - Enable SLER.");
38 
39 static char *qlini_mode = QLA2XXX_INI_MODE_STR_ENABLED;
40 module_param(qlini_mode, charp, S_IRUGO);
41 MODULE_PARM_DESC(qlini_mode,
42 	"Determines when initiator mode will be enabled. Possible values: "
43 	"\"exclusive\" - initiator mode will be enabled on load, "
44 	"disabled on enabling target mode and then on disabling target mode "
45 	"enabled back; "
46 	"\"disabled\" - initiator mode will never be enabled; "
47 	"\"dual\" - Initiator Modes will be enabled. Target Mode can be activated "
48 	"when ready "
49 	"\"enabled\" (default) - initiator mode will always stay enabled.");
50 
51 static int ql_dm_tgt_ex_pct = 0;
52 module_param(ql_dm_tgt_ex_pct, int, S_IRUGO|S_IWUSR);
53 MODULE_PARM_DESC(ql_dm_tgt_ex_pct,
54 	"For Dual Mode (qlini_mode=dual), this parameter determines "
55 	"the percentage of exchanges/cmds FW will allocate resources "
56 	"for Target mode.");
57 
58 int ql2xuctrlirq = 1;
59 module_param(ql2xuctrlirq, int, 0644);
60 MODULE_PARM_DESC(ql2xuctrlirq,
61     "User to control IRQ placement via smp_affinity."
62     "Valid with qlini_mode=disabled."
63     "1(default): enable");
64 
65 int ql2x_ini_mode = QLA2XXX_INI_MODE_EXCLUSIVE;
66 
67 static int qla_sam_status = SAM_STAT_BUSY;
68 static int tc_sam_status = SAM_STAT_TASK_SET_FULL; /* target core */
69 
70 /*
71  * From scsi/fc/fc_fcp.h
72  */
73 enum fcp_resp_rsp_codes {
74 	FCP_TMF_CMPL = 0,
75 	FCP_DATA_LEN_INVALID = 1,
76 	FCP_CMND_FIELDS_INVALID = 2,
77 	FCP_DATA_PARAM_MISMATCH = 3,
78 	FCP_TMF_REJECTED = 4,
79 	FCP_TMF_FAILED = 5,
80 	FCP_TMF_INVALID_LUN = 9,
81 };
82 
83 /*
84  * fc_pri_ta from scsi/fc/fc_fcp.h
85  */
86 #define FCP_PTA_SIMPLE      0   /* simple task attribute */
87 #define FCP_PTA_HEADQ       1   /* head of queue task attribute */
88 #define FCP_PTA_ORDERED     2   /* ordered task attribute */
89 #define FCP_PTA_ACA         4   /* auto. contingent allegiance */
90 #define FCP_PTA_MASK        7   /* mask for task attribute field */
91 #define FCP_PRI_SHIFT       3   /* priority field starts in bit 3 */
92 #define FCP_PRI_RESVD_MASK  0x80        /* reserved bits in priority field */
93 
94 /*
95  * This driver calls qla2x00_alloc_iocbs() and qla2x00_issue_marker(), which
96  * must be called under HW lock and could unlock/lock it inside.
97  * It isn't an issue, since in the current implementation on the time when
98  * those functions are called:
99  *
100  *   - Either context is IRQ and only IRQ handler can modify HW data,
101  *     including rings related fields,
102  *
103  *   - Or access to target mode variables from struct qla_tgt doesn't
104  *     cross those functions boundaries, except tgt_stop, which
105  *     additionally protected by irq_cmd_count.
106  */
107 /* Predefs for callbacks handed to qla2xxx LLD */
108 static void qlt_24xx_atio_pkt(struct scsi_qla_host *ha,
109 	struct atio_from_isp *pkt, uint8_t);
110 static void qlt_response_pkt(struct scsi_qla_host *ha, struct rsp_que *rsp,
111 	response_t *pkt);
112 static int qlt_issue_task_mgmt(struct fc_port *sess, u64 lun,
113 	int fn, void *iocb, int flags);
114 static void qlt_send_term_exchange(struct qla_qpair *, struct qla_tgt_cmd
115 	*cmd, struct atio_from_isp *atio, int ha_locked, int ul_abort);
116 static void qlt_alloc_qfull_cmd(struct scsi_qla_host *vha,
117 	struct atio_from_isp *atio, uint16_t status, int qfull);
118 static void qlt_disable_vha(struct scsi_qla_host *vha);
119 static void qlt_clear_tgt_db(struct qla_tgt *tgt);
120 static void qlt_send_notify_ack(struct qla_qpair *qpair,
121 	struct imm_ntfy_from_isp *ntfy,
122 	uint32_t add_flags, uint16_t resp_code, int resp_code_valid,
123 	uint16_t srr_flags, uint16_t srr_reject_code, uint8_t srr_explan);
124 static void qlt_send_term_imm_notif(struct scsi_qla_host *vha,
125 	struct imm_ntfy_from_isp *imm, int ha_locked);
126 static struct fc_port *qlt_create_sess(struct scsi_qla_host *vha,
127 	fc_port_t *fcport, bool local);
128 void qlt_unreg_sess(struct fc_port *sess);
129 static void qlt_24xx_handle_abts(struct scsi_qla_host *,
130 	struct abts_recv_from_24xx *);
131 static void qlt_send_busy(struct qla_qpair *, struct atio_from_isp *,
132     uint16_t);
133 static int qlt_check_reserve_free_req(struct qla_qpair *qpair, uint32_t);
134 static inline uint32_t qlt_make_handle(struct qla_qpair *);
135 
136 /*
137  * Global Variables
138  */
139 static struct kmem_cache *qla_tgt_mgmt_cmd_cachep;
140 struct kmem_cache *qla_tgt_plogi_cachep;
141 static mempool_t *qla_tgt_mgmt_cmd_mempool;
142 static struct workqueue_struct *qla_tgt_wq;
143 static DEFINE_MUTEX(qla_tgt_mutex);
144 static LIST_HEAD(qla_tgt_glist);
145 
146 static const char *prot_op_str(u32 prot_op)
147 {
148 	switch (prot_op) {
149 	case TARGET_PROT_NORMAL:	return "NORMAL";
150 	case TARGET_PROT_DIN_INSERT:	return "DIN_INSERT";
151 	case TARGET_PROT_DOUT_INSERT:	return "DOUT_INSERT";
152 	case TARGET_PROT_DIN_STRIP:	return "DIN_STRIP";
153 	case TARGET_PROT_DOUT_STRIP:	return "DOUT_STRIP";
154 	case TARGET_PROT_DIN_PASS:	return "DIN_PASS";
155 	case TARGET_PROT_DOUT_PASS:	return "DOUT_PASS";
156 	default:			return "UNKNOWN";
157 	}
158 }
159 
160 /* This API intentionally takes dest as a parameter, rather than returning
161  * int value to avoid caller forgetting to issue wmb() after the store */
162 void qlt_do_generation_tick(struct scsi_qla_host *vha, int *dest)
163 {
164 	scsi_qla_host_t *base_vha = pci_get_drvdata(vha->hw->pdev);
165 	*dest = atomic_inc_return(&base_vha->generation_tick);
166 	/* memory barrier */
167 	wmb();
168 }
169 
170 /* Might release hw lock, then reaquire!! */
171 static inline int qlt_issue_marker(struct scsi_qla_host *vha, int vha_locked)
172 {
173 	/* Send marker if required */
174 	if (unlikely(vha->marker_needed != 0)) {
175 		int rc = qla2x00_issue_marker(vha, vha_locked);
176 
177 		if (rc != QLA_SUCCESS) {
178 			ql_dbg(ql_dbg_tgt, vha, 0xe03d,
179 			    "qla_target(%d): issue_marker() failed\n",
180 			    vha->vp_idx);
181 		}
182 		return rc;
183 	}
184 	return QLA_SUCCESS;
185 }
186 
187 struct scsi_qla_host *qla_find_host_by_d_id(struct scsi_qla_host *vha,
188 					    be_id_t d_id)
189 {
190 	struct scsi_qla_host *host;
191 	uint32_t key;
192 
193 	if (vha->d_id.b.area == d_id.area &&
194 	    vha->d_id.b.domain == d_id.domain &&
195 	    vha->d_id.b.al_pa == d_id.al_pa)
196 		return vha;
197 
198 	key = be_to_port_id(d_id).b24;
199 
200 	host = btree_lookup32(&vha->hw->host_map, key);
201 	if (!host)
202 		ql_dbg(ql_dbg_tgt_mgt + ql_dbg_verbose, vha, 0xf005,
203 		    "Unable to find host %06x\n", key);
204 
205 	return host;
206 }
207 
208 static inline
209 struct scsi_qla_host *qlt_find_host_by_vp_idx(struct scsi_qla_host *vha,
210 	uint16_t vp_idx)
211 {
212 	struct qla_hw_data *ha = vha->hw;
213 
214 	if (vha->vp_idx == vp_idx)
215 		return vha;
216 
217 	BUG_ON(ha->tgt.tgt_vp_map == NULL);
218 	if (likely(test_bit(vp_idx, ha->vp_idx_map)))
219 		return ha->tgt.tgt_vp_map[vp_idx].vha;
220 
221 	return NULL;
222 }
223 
224 static inline void qlt_incr_num_pend_cmds(struct scsi_qla_host *vha)
225 {
226 	unsigned long flags;
227 
228 	spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
229 
230 	vha->hw->tgt.num_pend_cmds++;
231 	if (vha->hw->tgt.num_pend_cmds > vha->qla_stats.stat_max_pend_cmds)
232 		vha->qla_stats.stat_max_pend_cmds =
233 			vha->hw->tgt.num_pend_cmds;
234 	spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
235 }
236 static inline void qlt_decr_num_pend_cmds(struct scsi_qla_host *vha)
237 {
238 	unsigned long flags;
239 
240 	spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
241 	vha->hw->tgt.num_pend_cmds--;
242 	spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
243 }
244 
245 
246 static void qlt_queue_unknown_atio(scsi_qla_host_t *vha,
247 	struct atio_from_isp *atio, uint8_t ha_locked)
248 {
249 	struct qla_tgt_sess_op *u;
250 	struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
251 	unsigned long flags;
252 
253 	if (tgt->tgt_stop) {
254 		ql_dbg(ql_dbg_async, vha, 0x502c,
255 		    "qla_target(%d): dropping unknown ATIO_TYPE7, because tgt is being stopped",
256 		    vha->vp_idx);
257 		goto out_term;
258 	}
259 
260 	u = kzalloc(sizeof(*u), GFP_ATOMIC);
261 	if (u == NULL)
262 		goto out_term;
263 
264 	u->vha = vha;
265 	memcpy(&u->atio, atio, sizeof(*atio));
266 	INIT_LIST_HEAD(&u->cmd_list);
267 
268 	spin_lock_irqsave(&vha->cmd_list_lock, flags);
269 	list_add_tail(&u->cmd_list, &vha->unknown_atio_list);
270 	spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
271 
272 	schedule_delayed_work(&vha->unknown_atio_work, 1);
273 
274 out:
275 	return;
276 
277 out_term:
278 	qlt_send_term_exchange(vha->hw->base_qpair, NULL, atio, ha_locked, 0);
279 	goto out;
280 }
281 
282 static void qlt_try_to_dequeue_unknown_atios(struct scsi_qla_host *vha,
283 	uint8_t ha_locked)
284 {
285 	struct qla_tgt_sess_op *u, *t;
286 	scsi_qla_host_t *host;
287 	struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
288 	unsigned long flags;
289 	uint8_t queued = 0;
290 
291 	list_for_each_entry_safe(u, t, &vha->unknown_atio_list, cmd_list) {
292 		if (u->aborted) {
293 			ql_dbg(ql_dbg_async, vha, 0x502e,
294 			    "Freeing unknown %s %p, because of Abort\n",
295 			    "ATIO_TYPE7", u);
296 			qlt_send_term_exchange(vha->hw->base_qpair, NULL,
297 			    &u->atio, ha_locked, 0);
298 			goto abort;
299 		}
300 
301 		host = qla_find_host_by_d_id(vha, u->atio.u.isp24.fcp_hdr.d_id);
302 		if (host != NULL) {
303 			ql_dbg(ql_dbg_async + ql_dbg_verbose, vha, 0x502f,
304 			    "Requeuing unknown ATIO_TYPE7 %p\n", u);
305 			qlt_24xx_atio_pkt(host, &u->atio, ha_locked);
306 		} else if (tgt->tgt_stop) {
307 			ql_dbg(ql_dbg_async + ql_dbg_verbose, vha, 0x503a,
308 			    "Freeing unknown %s %p, because tgt is being stopped\n",
309 			    "ATIO_TYPE7", u);
310 			qlt_send_term_exchange(vha->hw->base_qpair, NULL,
311 			    &u->atio, ha_locked, 0);
312 		} else {
313 			ql_dbg(ql_dbg_async + ql_dbg_verbose, vha, 0x503d,
314 			    "Reschedule u %p, vha %p, host %p\n", u, vha, host);
315 			if (!queued) {
316 				queued = 1;
317 				schedule_delayed_work(&vha->unknown_atio_work,
318 				    1);
319 			}
320 			continue;
321 		}
322 
323 abort:
324 		spin_lock_irqsave(&vha->cmd_list_lock, flags);
325 		list_del(&u->cmd_list);
326 		spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
327 		kfree(u);
328 	}
329 }
330 
331 void qlt_unknown_atio_work_fn(struct work_struct *work)
332 {
333 	struct scsi_qla_host *vha = container_of(to_delayed_work(work),
334 	    struct scsi_qla_host, unknown_atio_work);
335 
336 	qlt_try_to_dequeue_unknown_atios(vha, 0);
337 }
338 
339 static bool qlt_24xx_atio_pkt_all_vps(struct scsi_qla_host *vha,
340 	struct atio_from_isp *atio, uint8_t ha_locked)
341 {
342 	ql_dbg(ql_dbg_tgt, vha, 0xe072,
343 		"%s: qla_target(%d): type %x ox_id %04x\n",
344 		__func__, vha->vp_idx, atio->u.raw.entry_type,
345 		be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id));
346 
347 	switch (atio->u.raw.entry_type) {
348 	case ATIO_TYPE7:
349 	{
350 		struct scsi_qla_host *host = qla_find_host_by_d_id(vha,
351 		    atio->u.isp24.fcp_hdr.d_id);
352 		if (unlikely(NULL == host)) {
353 			ql_dbg(ql_dbg_tgt, vha, 0xe03e,
354 			    "qla_target(%d): Received ATIO_TYPE7 "
355 			    "with unknown d_id %x:%x:%x\n", vha->vp_idx,
356 			    atio->u.isp24.fcp_hdr.d_id.domain,
357 			    atio->u.isp24.fcp_hdr.d_id.area,
358 			    atio->u.isp24.fcp_hdr.d_id.al_pa);
359 
360 
361 			qlt_queue_unknown_atio(vha, atio, ha_locked);
362 			break;
363 		}
364 		if (unlikely(!list_empty(&vha->unknown_atio_list)))
365 			qlt_try_to_dequeue_unknown_atios(vha, ha_locked);
366 
367 		qlt_24xx_atio_pkt(host, atio, ha_locked);
368 		break;
369 	}
370 
371 	case IMMED_NOTIFY_TYPE:
372 	{
373 		struct scsi_qla_host *host = vha;
374 		struct imm_ntfy_from_isp *entry =
375 		    (struct imm_ntfy_from_isp *)atio;
376 
377 		qlt_issue_marker(vha, ha_locked);
378 
379 		if ((entry->u.isp24.vp_index != 0xFF) &&
380 		    (entry->u.isp24.nport_handle != cpu_to_le16(0xFFFF))) {
381 			host = qlt_find_host_by_vp_idx(vha,
382 			    entry->u.isp24.vp_index);
383 			if (unlikely(!host)) {
384 				ql_dbg(ql_dbg_tgt, vha, 0xe03f,
385 				    "qla_target(%d): Received "
386 				    "ATIO (IMMED_NOTIFY_TYPE) "
387 				    "with unknown vp_index %d\n",
388 				    vha->vp_idx, entry->u.isp24.vp_index);
389 				break;
390 			}
391 		}
392 		qlt_24xx_atio_pkt(host, atio, ha_locked);
393 		break;
394 	}
395 
396 	case VP_RPT_ID_IOCB_TYPE:
397 		qla24xx_report_id_acquisition(vha,
398 			(struct vp_rpt_id_entry_24xx *)atio);
399 		break;
400 
401 	case ABTS_RECV_24XX:
402 	{
403 		struct abts_recv_from_24xx *entry =
404 			(struct abts_recv_from_24xx *)atio;
405 		struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
406 			entry->vp_index);
407 		unsigned long flags;
408 
409 		if (unlikely(!host)) {
410 			ql_dbg(ql_dbg_tgt, vha, 0xe00a,
411 			    "qla_target(%d): Response pkt (ABTS_RECV_24XX) "
412 			    "received, with unknown vp_index %d\n",
413 			    vha->vp_idx, entry->vp_index);
414 			break;
415 		}
416 		if (!ha_locked)
417 			spin_lock_irqsave(&host->hw->hardware_lock, flags);
418 		qlt_24xx_handle_abts(host, (struct abts_recv_from_24xx *)atio);
419 		if (!ha_locked)
420 			spin_unlock_irqrestore(&host->hw->hardware_lock, flags);
421 		break;
422 	}
423 
424 	/* case PUREX_IOCB_TYPE: ql2xmvasynctoatio */
425 
426 	default:
427 		ql_dbg(ql_dbg_tgt, vha, 0xe040,
428 		    "qla_target(%d): Received unknown ATIO atio "
429 		    "type %x\n", vha->vp_idx, atio->u.raw.entry_type);
430 		break;
431 	}
432 
433 	return false;
434 }
435 
436 void qlt_response_pkt_all_vps(struct scsi_qla_host *vha,
437 	struct rsp_que *rsp, response_t *pkt)
438 {
439 	switch (pkt->entry_type) {
440 	case CTIO_CRC2:
441 		ql_dbg(ql_dbg_tgt, vha, 0xe073,
442 			"qla_target(%d):%s: CRC2 Response pkt\n",
443 			vha->vp_idx, __func__);
444 		fallthrough;
445 	case CTIO_TYPE7:
446 	{
447 		struct ctio7_from_24xx *entry = (struct ctio7_from_24xx *)pkt;
448 		struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
449 		    entry->vp_index);
450 		if (unlikely(!host)) {
451 			ql_dbg(ql_dbg_tgt, vha, 0xe041,
452 			    "qla_target(%d): Response pkt (CTIO_TYPE7) "
453 			    "received, with unknown vp_index %d\n",
454 			    vha->vp_idx, entry->vp_index);
455 			break;
456 		}
457 		qlt_response_pkt(host, rsp, pkt);
458 		break;
459 	}
460 
461 	case IMMED_NOTIFY_TYPE:
462 	{
463 		struct scsi_qla_host *host;
464 		struct imm_ntfy_from_isp *entry =
465 		    (struct imm_ntfy_from_isp *)pkt;
466 
467 		host = qlt_find_host_by_vp_idx(vha, entry->u.isp24.vp_index);
468 		if (unlikely(!host)) {
469 			ql_dbg(ql_dbg_tgt, vha, 0xe042,
470 			    "qla_target(%d): Response pkt (IMMED_NOTIFY_TYPE) "
471 			    "received, with unknown vp_index %d\n",
472 			    vha->vp_idx, entry->u.isp24.vp_index);
473 			break;
474 		}
475 		qlt_response_pkt(host, rsp, pkt);
476 		break;
477 	}
478 
479 	case NOTIFY_ACK_TYPE:
480 	{
481 		struct scsi_qla_host *host = vha;
482 		struct nack_to_isp *entry = (struct nack_to_isp *)pkt;
483 
484 		if (0xFF != entry->u.isp24.vp_index) {
485 			host = qlt_find_host_by_vp_idx(vha,
486 			    entry->u.isp24.vp_index);
487 			if (unlikely(!host)) {
488 				ql_dbg(ql_dbg_tgt, vha, 0xe043,
489 				    "qla_target(%d): Response "
490 				    "pkt (NOTIFY_ACK_TYPE) "
491 				    "received, with unknown "
492 				    "vp_index %d\n", vha->vp_idx,
493 				    entry->u.isp24.vp_index);
494 				break;
495 			}
496 		}
497 		qlt_response_pkt(host, rsp, pkt);
498 		break;
499 	}
500 
501 	case ABTS_RECV_24XX:
502 	{
503 		struct abts_recv_from_24xx *entry =
504 		    (struct abts_recv_from_24xx *)pkt;
505 		struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
506 		    entry->vp_index);
507 		if (unlikely(!host)) {
508 			ql_dbg(ql_dbg_tgt, vha, 0xe044,
509 			    "qla_target(%d): Response pkt "
510 			    "(ABTS_RECV_24XX) received, with unknown "
511 			    "vp_index %d\n", vha->vp_idx, entry->vp_index);
512 			break;
513 		}
514 		qlt_response_pkt(host, rsp, pkt);
515 		break;
516 	}
517 
518 	case ABTS_RESP_24XX:
519 	{
520 		struct abts_resp_to_24xx *entry =
521 		    (struct abts_resp_to_24xx *)pkt;
522 		struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
523 		    entry->vp_index);
524 		if (unlikely(!host)) {
525 			ql_dbg(ql_dbg_tgt, vha, 0xe045,
526 			    "qla_target(%d): Response pkt "
527 			    "(ABTS_RECV_24XX) received, with unknown "
528 			    "vp_index %d\n", vha->vp_idx, entry->vp_index);
529 			break;
530 		}
531 		qlt_response_pkt(host, rsp, pkt);
532 		break;
533 	}
534 	default:
535 		qlt_response_pkt(vha, rsp, pkt);
536 		break;
537 	}
538 
539 }
540 
541 /*
542  * All qlt_plogi_ack_t operations are protected by hardware_lock
543  */
544 static int qla24xx_post_nack_work(struct scsi_qla_host *vha, fc_port_t *fcport,
545 	struct imm_ntfy_from_isp *ntfy, int type)
546 {
547 	struct qla_work_evt *e;
548 
549 	e = qla2x00_alloc_work(vha, QLA_EVT_NACK);
550 	if (!e)
551 		return QLA_FUNCTION_FAILED;
552 
553 	e->u.nack.fcport = fcport;
554 	e->u.nack.type = type;
555 	memcpy(e->u.nack.iocb, ntfy, sizeof(struct imm_ntfy_from_isp));
556 	return qla2x00_post_work(vha, e);
557 }
558 
559 static void qla2x00_async_nack_sp_done(srb_t *sp, int res)
560 {
561 	struct scsi_qla_host *vha = sp->vha;
562 	unsigned long flags;
563 
564 	ql_dbg(ql_dbg_disc, vha, 0x20f2,
565 	    "Async done-%s res %x %8phC  type %d\n",
566 	    sp->name, res, sp->fcport->port_name, sp->type);
567 
568 	spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
569 	sp->fcport->flags &= ~FCF_ASYNC_SENT;
570 	sp->fcport->chip_reset = vha->hw->base_qpair->chip_reset;
571 
572 	switch (sp->type) {
573 	case SRB_NACK_PLOGI:
574 		sp->fcport->login_gen++;
575 		sp->fcport->fw_login_state = DSC_LS_PLOGI_COMP;
576 		sp->fcport->logout_on_delete = 1;
577 		sp->fcport->plogi_nack_done_deadline = jiffies + HZ;
578 		sp->fcport->send_els_logo = 0;
579 
580 		if (sp->fcport->flags & FCF_FCSP_DEVICE) {
581 			ql_dbg(ql_dbg_edif, vha, 0x20ef,
582 			    "%s %8phC edif: PLOGI- AUTH WAIT\n", __func__,
583 			    sp->fcport->port_name);
584 			qla2x00_set_fcport_disc_state(sp->fcport,
585 			    DSC_LOGIN_AUTH_PEND);
586 			qla2x00_post_aen_work(vha, FCH_EVT_PORT_ONLINE,
587 			    sp->fcport->d_id.b24);
588 			qla_edb_eventcreate(vha, VND_CMD_AUTH_STATE_NEEDED, sp->fcport->d_id.b24,
589 			    0, sp->fcport);
590 		}
591 		break;
592 
593 	case SRB_NACK_PRLI:
594 		sp->fcport->fw_login_state = DSC_LS_PRLI_COMP;
595 		sp->fcport->deleted = 0;
596 		sp->fcport->send_els_logo = 0;
597 
598 		if (!sp->fcport->login_succ &&
599 		    !IS_SW_RESV_ADDR(sp->fcport->d_id)) {
600 			sp->fcport->login_succ = 1;
601 
602 			vha->fcport_count++;
603 			spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
604 			qla24xx_sched_upd_fcport(sp->fcport);
605 			spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
606 		} else {
607 			sp->fcport->login_retry = 0;
608 			qla2x00_set_fcport_disc_state(sp->fcport,
609 			    DSC_LOGIN_COMPLETE);
610 			sp->fcport->deleted = 0;
611 			sp->fcport->logout_on_delete = 1;
612 		}
613 		break;
614 
615 	case SRB_NACK_LOGO:
616 		sp->fcport->login_gen++;
617 		sp->fcport->fw_login_state = DSC_LS_PORT_UNAVAIL;
618 		qlt_logo_completion_handler(sp->fcport, MBS_COMMAND_COMPLETE);
619 		break;
620 	}
621 	spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
622 
623 	kref_put(&sp->cmd_kref, qla2x00_sp_release);
624 }
625 
626 int qla24xx_async_notify_ack(scsi_qla_host_t *vha, fc_port_t *fcport,
627 	struct imm_ntfy_from_isp *ntfy, int type)
628 {
629 	int rval = QLA_FUNCTION_FAILED;
630 	srb_t *sp;
631 	char *c = NULL;
632 
633 	fcport->flags |= FCF_ASYNC_SENT;
634 	switch (type) {
635 	case SRB_NACK_PLOGI:
636 		fcport->fw_login_state = DSC_LS_PLOGI_PEND;
637 		c = "PLOGI";
638 		if (vha->hw->flags.edif_enabled &&
639 		    (le16_to_cpu(ntfy->u.isp24.flags) & NOTIFY24XX_FLAGS_FCSP))
640 			fcport->flags |= FCF_FCSP_DEVICE;
641 		break;
642 	case SRB_NACK_PRLI:
643 		fcport->fw_login_state = DSC_LS_PRLI_PEND;
644 		fcport->deleted = 0;
645 		c = "PRLI";
646 		break;
647 	case SRB_NACK_LOGO:
648 		fcport->fw_login_state = DSC_LS_LOGO_PEND;
649 		c = "LOGO";
650 		break;
651 	}
652 
653 	sp = qla2x00_get_sp(vha, fcport, GFP_ATOMIC);
654 	if (!sp)
655 		goto done;
656 
657 	sp->type = type;
658 	sp->name = "nack";
659 	qla2x00_init_async_sp(sp, qla2x00_get_async_timeout(vha) + 2,
660 			      qla2x00_async_nack_sp_done);
661 
662 	sp->u.iocb_cmd.u.nack.ntfy = ntfy;
663 
664 	ql_dbg(ql_dbg_disc, vha, 0x20f4,
665 	    "Async-%s %8phC hndl %x %s\n",
666 	    sp->name, fcport->port_name, sp->handle, c);
667 
668 	rval = qla2x00_start_sp(sp);
669 	if (rval != QLA_SUCCESS)
670 		goto done_free_sp;
671 
672 	return rval;
673 
674 done_free_sp:
675 	kref_put(&sp->cmd_kref, qla2x00_sp_release);
676 done:
677 	fcport->flags &= ~FCF_ASYNC_SENT;
678 	return rval;
679 }
680 
681 void qla24xx_do_nack_work(struct scsi_qla_host *vha, struct qla_work_evt *e)
682 {
683 	fc_port_t *t;
684 
685 	switch (e->u.nack.type) {
686 	case SRB_NACK_PRLI:
687 		t = e->u.nack.fcport;
688 		flush_work(&t->del_work);
689 		flush_work(&t->free_work);
690 		mutex_lock(&vha->vha_tgt.tgt_mutex);
691 		t = qlt_create_sess(vha, e->u.nack.fcport, 0);
692 		mutex_unlock(&vha->vha_tgt.tgt_mutex);
693 		if (t) {
694 			ql_log(ql_log_info, vha, 0xd034,
695 			    "%s create sess success %p", __func__, t);
696 			/* create sess has an extra kref */
697 			vha->hw->tgt.tgt_ops->put_sess(e->u.nack.fcport);
698 		}
699 		break;
700 	}
701 	qla24xx_async_notify_ack(vha, e->u.nack.fcport,
702 	    (struct imm_ntfy_from_isp *)e->u.nack.iocb, e->u.nack.type);
703 }
704 
705 void qla24xx_delete_sess_fn(struct work_struct *work)
706 {
707 	fc_port_t *fcport = container_of(work, struct fc_port, del_work);
708 	struct qla_hw_data *ha = NULL;
709 
710 	if (!fcport || !fcport->vha || !fcport->vha->hw)
711 		return;
712 
713 	ha = fcport->vha->hw;
714 
715 	if (fcport->se_sess) {
716 		ha->tgt.tgt_ops->shutdown_sess(fcport);
717 		ha->tgt.tgt_ops->put_sess(fcport);
718 	} else {
719 		qlt_unreg_sess(fcport);
720 	}
721 }
722 
723 /*
724  * Called from qla2x00_reg_remote_port()
725  */
726 void qlt_fc_port_added(struct scsi_qla_host *vha, fc_port_t *fcport)
727 {
728 	struct qla_hw_data *ha = vha->hw;
729 	struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
730 	struct fc_port *sess = fcport;
731 	unsigned long flags;
732 
733 	if (!vha->hw->tgt.tgt_ops)
734 		return;
735 
736 	spin_lock_irqsave(&ha->tgt.sess_lock, flags);
737 	if (tgt->tgt_stop) {
738 		spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
739 		return;
740 	}
741 
742 	if (fcport->disc_state == DSC_DELETE_PEND) {
743 		spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
744 		return;
745 	}
746 
747 	if (!sess->se_sess) {
748 		spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
749 
750 		mutex_lock(&vha->vha_tgt.tgt_mutex);
751 		sess = qlt_create_sess(vha, fcport, false);
752 		mutex_unlock(&vha->vha_tgt.tgt_mutex);
753 
754 		spin_lock_irqsave(&ha->tgt.sess_lock, flags);
755 	} else {
756 		if (fcport->fw_login_state == DSC_LS_PRLI_COMP) {
757 			spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
758 			return;
759 		}
760 
761 		if (!kref_get_unless_zero(&sess->sess_kref)) {
762 			ql_dbg(ql_dbg_disc, vha, 0x2107,
763 			    "%s: kref_get fail sess %8phC \n",
764 			    __func__, sess->port_name);
765 			spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
766 			return;
767 		}
768 
769 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04c,
770 		    "qla_target(%u): %ssession for port %8phC "
771 		    "(loop ID %d) reappeared\n", vha->vp_idx,
772 		    sess->local ? "local " : "", sess->port_name, sess->loop_id);
773 
774 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf007,
775 		    "Reappeared sess %p\n", sess);
776 
777 		ha->tgt.tgt_ops->update_sess(sess, fcport->d_id,
778 		    fcport->loop_id,
779 		    (fcport->flags & FCF_CONF_COMP_SUPPORTED));
780 	}
781 
782 	if (sess && sess->local) {
783 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04d,
784 		    "qla_target(%u): local session for "
785 		    "port %8phC (loop ID %d) became global\n", vha->vp_idx,
786 		    fcport->port_name, sess->loop_id);
787 		sess->local = 0;
788 	}
789 	spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
790 
791 	ha->tgt.tgt_ops->put_sess(sess);
792 }
793 
794 /*
795  * This is a zero-base ref-counting solution, since hardware_lock
796  * guarantees that ref_count is not modified concurrently.
797  * Upon successful return content of iocb is undefined
798  */
799 static struct qlt_plogi_ack_t *
800 qlt_plogi_ack_find_add(struct scsi_qla_host *vha, port_id_t *id,
801 		       struct imm_ntfy_from_isp *iocb)
802 {
803 	struct qlt_plogi_ack_t *pla;
804 
805 	lockdep_assert_held(&vha->hw->hardware_lock);
806 
807 	list_for_each_entry(pla, &vha->plogi_ack_list, list) {
808 		if (pla->id.b24 == id->b24) {
809 			ql_dbg(ql_dbg_disc + ql_dbg_verbose, vha, 0x210d,
810 			    "%s %d %8phC Term INOT due to new INOT",
811 			    __func__, __LINE__,
812 			    pla->iocb.u.isp24.port_name);
813 			qlt_send_term_imm_notif(vha, &pla->iocb, 1);
814 			memcpy(&pla->iocb, iocb, sizeof(pla->iocb));
815 			return pla;
816 		}
817 	}
818 
819 	pla = kmem_cache_zalloc(qla_tgt_plogi_cachep, GFP_ATOMIC);
820 	if (!pla) {
821 		ql_dbg(ql_dbg_async, vha, 0x5088,
822 		       "qla_target(%d): Allocation of plogi_ack failed\n",
823 		       vha->vp_idx);
824 		return NULL;
825 	}
826 
827 	memcpy(&pla->iocb, iocb, sizeof(pla->iocb));
828 	pla->id = *id;
829 	list_add_tail(&pla->list, &vha->plogi_ack_list);
830 
831 	return pla;
832 }
833 
834 void qlt_plogi_ack_unref(struct scsi_qla_host *vha,
835     struct qlt_plogi_ack_t *pla)
836 {
837 	struct imm_ntfy_from_isp *iocb = &pla->iocb;
838 	port_id_t port_id;
839 	uint16_t loop_id;
840 	fc_port_t *fcport = pla->fcport;
841 
842 	BUG_ON(!pla->ref_count);
843 	pla->ref_count--;
844 
845 	if (pla->ref_count)
846 		return;
847 
848 	ql_dbg(ql_dbg_disc, vha, 0x5089,
849 	    "Sending PLOGI ACK to wwn %8phC s_id %02x:%02x:%02x loop_id %#04x"
850 	    " exch %#x ox_id %#x\n", iocb->u.isp24.port_name,
851 	    iocb->u.isp24.port_id[2], iocb->u.isp24.port_id[1],
852 	    iocb->u.isp24.port_id[0],
853 	    le16_to_cpu(iocb->u.isp24.nport_handle),
854 	    iocb->u.isp24.exchange_address, iocb->ox_id);
855 
856 	port_id.b.domain = iocb->u.isp24.port_id[2];
857 	port_id.b.area   = iocb->u.isp24.port_id[1];
858 	port_id.b.al_pa  = iocb->u.isp24.port_id[0];
859 	port_id.b.rsvd_1 = 0;
860 
861 	loop_id = le16_to_cpu(iocb->u.isp24.nport_handle);
862 
863 	fcport->loop_id = loop_id;
864 	fcport->d_id = port_id;
865 	if (iocb->u.isp24.status_subcode == ELS_PLOGI)
866 		qla24xx_post_nack_work(vha, fcport, iocb, SRB_NACK_PLOGI);
867 	else
868 		qla24xx_post_nack_work(vha, fcport, iocb, SRB_NACK_PRLI);
869 
870 	list_for_each_entry(fcport, &vha->vp_fcports, list) {
871 		if (fcport->plogi_link[QLT_PLOGI_LINK_SAME_WWN] == pla)
872 			fcport->plogi_link[QLT_PLOGI_LINK_SAME_WWN] = NULL;
873 		if (fcport->plogi_link[QLT_PLOGI_LINK_CONFLICT] == pla)
874 			fcport->plogi_link[QLT_PLOGI_LINK_CONFLICT] = NULL;
875 	}
876 
877 	list_del(&pla->list);
878 	kmem_cache_free(qla_tgt_plogi_cachep, pla);
879 }
880 
881 void
882 qlt_plogi_ack_link(struct scsi_qla_host *vha, struct qlt_plogi_ack_t *pla,
883     struct fc_port *sess, enum qlt_plogi_link_t link)
884 {
885 	struct imm_ntfy_from_isp *iocb = &pla->iocb;
886 	/* Inc ref_count first because link might already be pointing at pla */
887 	pla->ref_count++;
888 
889 	ql_dbg(ql_dbg_tgt_mgt, vha, 0xf097,
890 		"Linking sess %p [%d] wwn %8phC with PLOGI ACK to wwn %8phC"
891 		" s_id %02x:%02x:%02x, ref=%d pla %p link %d\n",
892 		sess, link, sess->port_name,
893 		iocb->u.isp24.port_name, iocb->u.isp24.port_id[2],
894 		iocb->u.isp24.port_id[1], iocb->u.isp24.port_id[0],
895 		pla->ref_count, pla, link);
896 
897 	if (link == QLT_PLOGI_LINK_CONFLICT) {
898 		switch (sess->disc_state) {
899 		case DSC_DELETED:
900 		case DSC_DELETE_PEND:
901 			pla->ref_count--;
902 			return;
903 		default:
904 			break;
905 		}
906 	}
907 
908 	if (sess->plogi_link[link])
909 		qlt_plogi_ack_unref(vha, sess->plogi_link[link]);
910 
911 	if (link == QLT_PLOGI_LINK_SAME_WWN)
912 		pla->fcport = sess;
913 
914 	sess->plogi_link[link] = pla;
915 }
916 
917 typedef struct {
918 	/* These fields must be initialized by the caller */
919 	port_id_t id;
920 	/*
921 	 * number of cmds dropped while we were waiting for
922 	 * initiator to ack LOGO initialize to 1 if LOGO is
923 	 * triggered by a command, otherwise, to 0
924 	 */
925 	int cmd_count;
926 
927 	/* These fields are used by callee */
928 	struct list_head list;
929 } qlt_port_logo_t;
930 
931 static void
932 qlt_send_first_logo(struct scsi_qla_host *vha, qlt_port_logo_t *logo)
933 {
934 	qlt_port_logo_t *tmp;
935 	int res;
936 
937 	if (test_bit(PFLG_DRIVER_REMOVING, &vha->pci_flags)) {
938 		res = 0;
939 		goto out;
940 	}
941 
942 	mutex_lock(&vha->vha_tgt.tgt_mutex);
943 
944 	list_for_each_entry(tmp, &vha->logo_list, list) {
945 		if (tmp->id.b24 == logo->id.b24) {
946 			tmp->cmd_count += logo->cmd_count;
947 			mutex_unlock(&vha->vha_tgt.tgt_mutex);
948 			return;
949 		}
950 	}
951 
952 	list_add_tail(&logo->list, &vha->logo_list);
953 
954 	mutex_unlock(&vha->vha_tgt.tgt_mutex);
955 
956 	res = qla24xx_els_dcmd_iocb(vha, ELS_DCMD_LOGO, logo->id);
957 
958 	mutex_lock(&vha->vha_tgt.tgt_mutex);
959 	list_del(&logo->list);
960 	mutex_unlock(&vha->vha_tgt.tgt_mutex);
961 
962 out:
963 	ql_dbg(ql_dbg_tgt_mgt, vha, 0xf098,
964 	    "Finished LOGO to %02x:%02x:%02x, dropped %d cmds, res = %#x\n",
965 	    logo->id.b.domain, logo->id.b.area, logo->id.b.al_pa,
966 	    logo->cmd_count, res);
967 }
968 
969 void qlt_free_session_done(struct work_struct *work)
970 {
971 	struct fc_port *sess = container_of(work, struct fc_port,
972 	    free_work);
973 	struct qla_tgt *tgt = sess->tgt;
974 	struct scsi_qla_host *vha = sess->vha;
975 	struct qla_hw_data *ha = vha->hw;
976 	unsigned long flags;
977 	bool logout_started = false;
978 	scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
979 	struct qlt_plogi_ack_t *own =
980 		sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN];
981 
982 	ql_dbg(ql_dbg_disc, vha, 0xf084,
983 		"%s: se_sess %p / sess %p from port %8phC loop_id %#04x"
984 		" s_id %02x:%02x:%02x logout %d keep %d els_logo %d\n",
985 		__func__, sess->se_sess, sess, sess->port_name, sess->loop_id,
986 		sess->d_id.b.domain, sess->d_id.b.area, sess->d_id.b.al_pa,
987 		sess->logout_on_delete, sess->keep_nport_handle,
988 		sess->send_els_logo);
989 
990 	if (!IS_SW_RESV_ADDR(sess->d_id)) {
991 		if (ha->flags.edif_enabled &&
992 		    (!own || own->iocb.u.isp24.status_subcode == ELS_PLOGI)) {
993 			sess->edif.authok = 0;
994 			if (!ha->flags.host_shutting_down) {
995 				ql_dbg(ql_dbg_edif, vha, 0x911e,
996 					"%s wwpn %8phC calling qla2x00_release_all_sadb\n",
997 					__func__, sess->port_name);
998 				qla2x00_release_all_sadb(vha, sess);
999 			} else {
1000 				ql_dbg(ql_dbg_edif, vha, 0x911e,
1001 					"%s bypassing release_all_sadb\n",
1002 					__func__);
1003 			}
1004 			qla_edif_clear_appdata(vha, sess);
1005 			qla_edif_sess_down(vha, sess);
1006 		}
1007 		qla2x00_mark_device_lost(vha, sess, 0);
1008 
1009 		if (sess->send_els_logo) {
1010 			qlt_port_logo_t logo;
1011 
1012 			logo.id = sess->d_id;
1013 			logo.cmd_count = 0;
1014 			INIT_LIST_HEAD(&logo.list);
1015 			if (!own)
1016 				qlt_send_first_logo(vha, &logo);
1017 			sess->send_els_logo = 0;
1018 		}
1019 
1020 		if (sess->logout_on_delete && sess->loop_id != FC_NO_LOOP_ID) {
1021 			int rc;
1022 
1023 			if (!own ||
1024 			     (own->iocb.u.isp24.status_subcode == ELS_PLOGI)) {
1025 				sess->logout_completed = 0;
1026 				rc = qla2x00_post_async_logout_work(vha, sess,
1027 				    NULL);
1028 				if (rc != QLA_SUCCESS)
1029 					ql_log(ql_log_warn, vha, 0xf085,
1030 					    "Schedule logo failed sess %p rc %d\n",
1031 					    sess, rc);
1032 				else
1033 					logout_started = true;
1034 			} else if (own && (own->iocb.u.isp24.status_subcode ==
1035 				ELS_PRLI) && ha->flags.rida_fmt2) {
1036 				rc = qla2x00_post_async_prlo_work(vha, sess,
1037 				    NULL);
1038 				if (rc != QLA_SUCCESS)
1039 					ql_log(ql_log_warn, vha, 0xf085,
1040 					    "Schedule PRLO failed sess %p rc %d\n",
1041 					    sess, rc);
1042 				else
1043 					logout_started = true;
1044 			}
1045 		} /* if sess->logout_on_delete */
1046 
1047 		if (sess->nvme_flag & NVME_FLAG_REGISTERED &&
1048 		    !(sess->nvme_flag & NVME_FLAG_DELETING)) {
1049 			sess->nvme_flag |= NVME_FLAG_DELETING;
1050 			qla_nvme_unregister_remote_port(sess);
1051 		}
1052 	}
1053 
1054 	/*
1055 	 * Release the target session for FC Nexus from fabric module code.
1056 	 */
1057 	if (sess->se_sess != NULL)
1058 		ha->tgt.tgt_ops->free_session(sess);
1059 
1060 	if (logout_started) {
1061 		bool traced = false;
1062 		u16 cnt = 0;
1063 
1064 		while (!READ_ONCE(sess->logout_completed)) {
1065 			if (!traced) {
1066 				ql_dbg(ql_dbg_disc, vha, 0xf086,
1067 					"%s: waiting for sess %p logout\n",
1068 					__func__, sess);
1069 				traced = true;
1070 			}
1071 			msleep(100);
1072 			cnt++;
1073 			/*
1074 			 * Driver timeout is set to 22 Sec, update count value to loop
1075 			 * long enough for log-out to complete before advancing. Otherwise,
1076 			 * straddling logout can interfere with re-login attempt.
1077 			 */
1078 			if (cnt > 230)
1079 				break;
1080 		}
1081 
1082 		ql_dbg(ql_dbg_disc, vha, 0xf087,
1083 		    "%s: sess %p logout completed\n", __func__, sess);
1084 	}
1085 
1086 	if (sess->logo_ack_needed) {
1087 		sess->logo_ack_needed = 0;
1088 		qla24xx_async_notify_ack(vha, sess,
1089 			(struct imm_ntfy_from_isp *)sess->iocb, SRB_NACK_LOGO);
1090 	}
1091 
1092 	spin_lock_irqsave(&vha->work_lock, flags);
1093 	sess->flags &= ~FCF_ASYNC_SENT;
1094 	spin_unlock_irqrestore(&vha->work_lock, flags);
1095 
1096 	spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1097 	if (sess->se_sess) {
1098 		sess->se_sess = NULL;
1099 		if (tgt && !IS_SW_RESV_ADDR(sess->d_id))
1100 			tgt->sess_count--;
1101 	}
1102 
1103 	qla2x00_set_fcport_disc_state(sess, DSC_DELETED);
1104 	sess->fw_login_state = DSC_LS_PORT_UNAVAIL;
1105 	sess->deleted = QLA_SESS_DELETED;
1106 
1107 	if (sess->login_succ && !IS_SW_RESV_ADDR(sess->d_id)) {
1108 		vha->fcport_count--;
1109 		sess->login_succ = 0;
1110 	}
1111 
1112 	qla2x00_clear_loop_id(sess);
1113 
1114 	if (sess->conflict) {
1115 		sess->conflict->login_pause = 0;
1116 		sess->conflict = NULL;
1117 		if (!test_bit(UNLOADING, &vha->dpc_flags))
1118 			set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1119 	}
1120 
1121 	{
1122 		struct qlt_plogi_ack_t *con =
1123 		    sess->plogi_link[QLT_PLOGI_LINK_CONFLICT];
1124 		struct imm_ntfy_from_isp *iocb;
1125 
1126 		own = sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN];
1127 
1128 		if (con) {
1129 			iocb = &con->iocb;
1130 			ql_dbg(ql_dbg_tgt_mgt, vha, 0xf099,
1131 				 "se_sess %p / sess %p port %8phC is gone,"
1132 				 " %s (ref=%d), releasing PLOGI for %8phC (ref=%d)\n",
1133 				 sess->se_sess, sess, sess->port_name,
1134 				 own ? "releasing own PLOGI" : "no own PLOGI pending",
1135 				 own ? own->ref_count : -1,
1136 				 iocb->u.isp24.port_name, con->ref_count);
1137 			qlt_plogi_ack_unref(vha, con);
1138 			sess->plogi_link[QLT_PLOGI_LINK_CONFLICT] = NULL;
1139 		} else {
1140 			ql_dbg(ql_dbg_tgt_mgt, vha, 0xf09a,
1141 			    "se_sess %p / sess %p port %8phC is gone, %s (ref=%d)\n",
1142 			    sess->se_sess, sess, sess->port_name,
1143 			    own ? "releasing own PLOGI" :
1144 			    "no own PLOGI pending",
1145 			    own ? own->ref_count : -1);
1146 		}
1147 
1148 		if (own) {
1149 			sess->fw_login_state = DSC_LS_PLOGI_PEND;
1150 			qlt_plogi_ack_unref(vha, own);
1151 			sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN] = NULL;
1152 		}
1153 	}
1154 
1155 	sess->explicit_logout = 0;
1156 	spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1157 	sess->free_pending = 0;
1158 
1159 	qla2x00_dfs_remove_rport(vha, sess);
1160 
1161 	ql_dbg(ql_dbg_disc, vha, 0xf001,
1162 	    "Unregistration of sess %p %8phC finished fcp_cnt %d\n",
1163 		sess, sess->port_name, vha->fcport_count);
1164 
1165 	if (tgt && (tgt->sess_count == 0))
1166 		wake_up_all(&tgt->waitQ);
1167 
1168 	if (!test_bit(PFLG_DRIVER_REMOVING, &base_vha->pci_flags) &&
1169 	    !(vha->vp_idx && test_bit(VPORT_DELETE, &vha->dpc_flags)) &&
1170 	    (!tgt || !tgt->tgt_stop) && !LOOP_TRANSITION(vha)) {
1171 		switch (vha->host->active_mode) {
1172 		case MODE_INITIATOR:
1173 		case MODE_DUAL:
1174 			set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1175 			qla2xxx_wake_dpc(vha);
1176 			break;
1177 		case MODE_TARGET:
1178 		default:
1179 			/* no-op */
1180 			break;
1181 		}
1182 	}
1183 
1184 	if (vha->fcport_count == 0)
1185 		wake_up_all(&vha->fcport_waitQ);
1186 }
1187 
1188 /* ha->tgt.sess_lock supposed to be held on entry */
1189 void qlt_unreg_sess(struct fc_port *sess)
1190 {
1191 	struct scsi_qla_host *vha = sess->vha;
1192 	unsigned long flags;
1193 
1194 	ql_dbg(ql_dbg_disc, sess->vha, 0x210a,
1195 	    "%s sess %p for deletion %8phC\n",
1196 	    __func__, sess, sess->port_name);
1197 
1198 	spin_lock_irqsave(&sess->vha->work_lock, flags);
1199 	if (sess->free_pending) {
1200 		spin_unlock_irqrestore(&sess->vha->work_lock, flags);
1201 		return;
1202 	}
1203 	sess->free_pending = 1;
1204 	/*
1205 	 * Use FCF_ASYNC_SENT flag to block other cmds used in sess
1206 	 * management from being sent.
1207 	 */
1208 	sess->flags |= FCF_ASYNC_SENT;
1209 	spin_unlock_irqrestore(&sess->vha->work_lock, flags);
1210 
1211 	if (sess->se_sess)
1212 		vha->hw->tgt.tgt_ops->clear_nacl_from_fcport_map(sess);
1213 
1214 	sess->deleted = QLA_SESS_DELETION_IN_PROGRESS;
1215 	qla2x00_set_fcport_disc_state(sess, DSC_DELETE_PEND);
1216 	sess->last_rscn_gen = sess->rscn_gen;
1217 	sess->last_login_gen = sess->login_gen;
1218 
1219 	queue_work(sess->vha->hw->wq, &sess->free_work);
1220 }
1221 EXPORT_SYMBOL(qlt_unreg_sess);
1222 
1223 static int qlt_reset(struct scsi_qla_host *vha, void *iocb, int mcmd)
1224 {
1225 	struct qla_hw_data *ha = vha->hw;
1226 	struct fc_port *sess = NULL;
1227 	uint16_t loop_id;
1228 	int res = 0;
1229 	struct imm_ntfy_from_isp *n = (struct imm_ntfy_from_isp *)iocb;
1230 	unsigned long flags;
1231 
1232 	loop_id = le16_to_cpu(n->u.isp24.nport_handle);
1233 	if (loop_id == 0xFFFF) {
1234 		/* Global event */
1235 		atomic_inc(&vha->vha_tgt.qla_tgt->tgt_global_resets_count);
1236 		spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1237 		qlt_clear_tgt_db(vha->vha_tgt.qla_tgt);
1238 		spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1239 	} else {
1240 		spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1241 		sess = ha->tgt.tgt_ops->find_sess_by_loop_id(vha, loop_id);
1242 		spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1243 	}
1244 
1245 	ql_dbg(ql_dbg_tgt, vha, 0xe000,
1246 	    "Using sess for qla_tgt_reset: %p\n", sess);
1247 	if (!sess) {
1248 		res = -ESRCH;
1249 		return res;
1250 	}
1251 
1252 	ql_dbg(ql_dbg_tgt, vha, 0xe047,
1253 	    "scsi(%ld): resetting (session %p from port %8phC mcmd %x, "
1254 	    "loop_id %d)\n", vha->host_no, sess, sess->port_name,
1255 	    mcmd, loop_id);
1256 
1257 	return qlt_issue_task_mgmt(sess, 0, mcmd, iocb, QLA24XX_MGMT_SEND_NACK);
1258 }
1259 
1260 static void qla24xx_chk_fcp_state(struct fc_port *sess)
1261 {
1262 	if (sess->chip_reset != sess->vha->hw->base_qpair->chip_reset) {
1263 		sess->logout_on_delete = 0;
1264 		sess->logo_ack_needed = 0;
1265 		sess->fw_login_state = DSC_LS_PORT_UNAVAIL;
1266 	}
1267 }
1268 
1269 void qlt_schedule_sess_for_deletion(struct fc_port *sess)
1270 {
1271 	struct qla_tgt *tgt = sess->tgt;
1272 	unsigned long flags;
1273 	u16 sec;
1274 
1275 	switch (sess->disc_state) {
1276 	case DSC_DELETE_PEND:
1277 		return;
1278 	case DSC_DELETED:
1279 		if (!sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN] &&
1280 			!sess->plogi_link[QLT_PLOGI_LINK_CONFLICT]) {
1281 			if (tgt && tgt->tgt_stop && tgt->sess_count == 0)
1282 				wake_up_all(&tgt->waitQ);
1283 
1284 			if (sess->vha->fcport_count == 0)
1285 				wake_up_all(&sess->vha->fcport_waitQ);
1286 			return;
1287 		}
1288 		break;
1289 	case DSC_UPD_FCPORT:
1290 		/*
1291 		 * This port is not done reporting to upper layer.
1292 		 * let it finish
1293 		 */
1294 		sess->next_disc_state = DSC_DELETE_PEND;
1295 		sec = jiffies_to_msecs(jiffies -
1296 		    sess->jiffies_at_registration)/1000;
1297 		if (sess->sec_since_registration < sec && sec && !(sec % 5)) {
1298 			sess->sec_since_registration = sec;
1299 			ql_dbg(ql_dbg_disc, sess->vha, 0xffff,
1300 			    "%s %8phC : Slow Rport registration(%d Sec)\n",
1301 			    __func__, sess->port_name, sec);
1302 		}
1303 		return;
1304 	default:
1305 		break;
1306 	}
1307 
1308 	spin_lock_irqsave(&sess->vha->work_lock, flags);
1309 	if (sess->deleted == QLA_SESS_DELETION_IN_PROGRESS) {
1310 		spin_unlock_irqrestore(&sess->vha->work_lock, flags);
1311 		return;
1312 	}
1313 	sess->deleted = QLA_SESS_DELETION_IN_PROGRESS;
1314 	spin_unlock_irqrestore(&sess->vha->work_lock, flags);
1315 
1316 	sess->prli_pend_timer = 0;
1317 	qla2x00_set_fcport_disc_state(sess, DSC_DELETE_PEND);
1318 
1319 	qla24xx_chk_fcp_state(sess);
1320 
1321 	ql_dbg(ql_log_warn, sess->vha, 0xe001,
1322 	    "Scheduling sess %p for deletion %8phC fc4_type %x\n",
1323 	    sess, sess->port_name, sess->fc4_type);
1324 
1325 	WARN_ON(!queue_work(sess->vha->hw->wq, &sess->del_work));
1326 }
1327 
1328 static void qlt_clear_tgt_db(struct qla_tgt *tgt)
1329 {
1330 	struct fc_port *sess;
1331 	scsi_qla_host_t *vha = tgt->vha;
1332 
1333 	list_for_each_entry(sess, &vha->vp_fcports, list) {
1334 		if (sess->se_sess)
1335 			qlt_schedule_sess_for_deletion(sess);
1336 	}
1337 
1338 	/* At this point tgt could be already dead */
1339 }
1340 
1341 static int qla24xx_get_loop_id(struct scsi_qla_host *vha, be_id_t s_id,
1342 	uint16_t *loop_id)
1343 {
1344 	struct qla_hw_data *ha = vha->hw;
1345 	dma_addr_t gid_list_dma;
1346 	struct gid_list_info *gid_list, *gid;
1347 	int res, rc, i;
1348 	uint16_t entries;
1349 
1350 	gid_list = dma_alloc_coherent(&ha->pdev->dev, qla2x00_gid_list_size(ha),
1351 	    &gid_list_dma, GFP_KERNEL);
1352 	if (!gid_list) {
1353 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf044,
1354 		    "qla_target(%d): DMA Alloc failed of %u\n",
1355 		    vha->vp_idx, qla2x00_gid_list_size(ha));
1356 		return -ENOMEM;
1357 	}
1358 
1359 	/* Get list of logged in devices */
1360 	rc = qla24xx_gidlist_wait(vha, gid_list, gid_list_dma, &entries);
1361 	if (rc != QLA_SUCCESS) {
1362 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf045,
1363 		    "qla_target(%d): get_id_list() failed: %x\n",
1364 		    vha->vp_idx, rc);
1365 		res = -EBUSY;
1366 		goto out_free_id_list;
1367 	}
1368 
1369 	gid = gid_list;
1370 	res = -ENOENT;
1371 	for (i = 0; i < entries; i++) {
1372 		if (gid->al_pa == s_id.al_pa &&
1373 		    gid->area == s_id.area &&
1374 		    gid->domain == s_id.domain) {
1375 			*loop_id = le16_to_cpu(gid->loop_id);
1376 			res = 0;
1377 			break;
1378 		}
1379 		gid = (void *)gid + ha->gid_list_info_size;
1380 	}
1381 
1382 out_free_id_list:
1383 	dma_free_coherent(&ha->pdev->dev, qla2x00_gid_list_size(ha),
1384 	    gid_list, gid_list_dma);
1385 	return res;
1386 }
1387 
1388 /*
1389  * Adds an extra ref to allow to drop hw lock after adding sess to the list.
1390  * Caller must put it.
1391  */
1392 static struct fc_port *qlt_create_sess(
1393 	struct scsi_qla_host *vha,
1394 	fc_port_t *fcport,
1395 	bool local)
1396 {
1397 	struct qla_hw_data *ha = vha->hw;
1398 	struct fc_port *sess = fcport;
1399 	unsigned long flags;
1400 
1401 	if (vha->vha_tgt.qla_tgt->tgt_stop)
1402 		return NULL;
1403 
1404 	if (fcport->se_sess) {
1405 		if (!kref_get_unless_zero(&sess->sess_kref)) {
1406 			ql_dbg(ql_dbg_disc, vha, 0x20f6,
1407 			    "%s: kref_get_unless_zero failed for %8phC\n",
1408 			    __func__, sess->port_name);
1409 			return NULL;
1410 		}
1411 		return fcport;
1412 	}
1413 	sess->tgt = vha->vha_tgt.qla_tgt;
1414 	sess->local = local;
1415 
1416 	/*
1417 	 * Under normal circumstances we want to logout from firmware when
1418 	 * session eventually ends and release corresponding nport handle.
1419 	 * In the exception cases (e.g. when new PLOGI is waiting) corresponding
1420 	 * code will adjust these flags as necessary.
1421 	 */
1422 	sess->logout_on_delete = 1;
1423 	sess->keep_nport_handle = 0;
1424 	sess->logout_completed = 0;
1425 
1426 	if (ha->tgt.tgt_ops->check_initiator_node_acl(vha,
1427 	    &fcport->port_name[0], sess) < 0) {
1428 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf015,
1429 		    "(%d) %8phC check_initiator_node_acl failed\n",
1430 		    vha->vp_idx, fcport->port_name);
1431 		return NULL;
1432 	} else {
1433 		kref_init(&fcport->sess_kref);
1434 		/*
1435 		 * Take an extra reference to ->sess_kref here to handle
1436 		 * fc_port access across ->tgt.sess_lock reaquire.
1437 		 */
1438 		if (!kref_get_unless_zero(&sess->sess_kref)) {
1439 			ql_dbg(ql_dbg_disc, vha, 0x20f7,
1440 			    "%s: kref_get_unless_zero failed for %8phC\n",
1441 			    __func__, sess->port_name);
1442 			return NULL;
1443 		}
1444 
1445 		spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1446 		if (!IS_SW_RESV_ADDR(sess->d_id))
1447 			vha->vha_tgt.qla_tgt->sess_count++;
1448 
1449 		qlt_do_generation_tick(vha, &sess->generation);
1450 		spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1451 	}
1452 
1453 	ql_dbg(ql_dbg_tgt_mgt, vha, 0xf006,
1454 	    "Adding sess %p se_sess %p  to tgt %p sess_count %d\n",
1455 	    sess, sess->se_sess, vha->vha_tgt.qla_tgt,
1456 	    vha->vha_tgt.qla_tgt->sess_count);
1457 
1458 	ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04b,
1459 	    "qla_target(%d): %ssession for wwn %8phC (loop_id %d, "
1460 	    "s_id %x:%x:%x, confirmed completion %ssupported) added\n",
1461 	    vha->vp_idx, local ?  "local " : "", fcport->port_name,
1462 	    fcport->loop_id, sess->d_id.b.domain, sess->d_id.b.area,
1463 	    sess->d_id.b.al_pa, sess->conf_compl_supported ?  "" : "not ");
1464 
1465 	return sess;
1466 }
1467 
1468 /*
1469  * max_gen - specifies maximum session generation
1470  * at which this deletion requestion is still valid
1471  */
1472 void
1473 qlt_fc_port_deleted(struct scsi_qla_host *vha, fc_port_t *fcport, int max_gen)
1474 {
1475 	struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
1476 	struct fc_port *sess = fcport;
1477 	unsigned long flags;
1478 
1479 	if (!vha->hw->tgt.tgt_ops)
1480 		return;
1481 
1482 	if (!tgt)
1483 		return;
1484 
1485 	spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
1486 	if (tgt->tgt_stop) {
1487 		spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1488 		return;
1489 	}
1490 	if (!sess->se_sess) {
1491 		spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1492 		return;
1493 	}
1494 
1495 	if (max_gen - sess->generation < 0) {
1496 		spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1497 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf092,
1498 		    "Ignoring stale deletion request for se_sess %p / sess %p"
1499 		    " for port %8phC, req_gen %d, sess_gen %d\n",
1500 		    sess->se_sess, sess, sess->port_name, max_gen,
1501 		    sess->generation);
1502 		return;
1503 	}
1504 
1505 	ql_dbg(ql_dbg_tgt_mgt, vha, 0xf008, "qla_tgt_fc_port_deleted %p", sess);
1506 
1507 	sess->local = 1;
1508 	spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1509 	qlt_schedule_sess_for_deletion(sess);
1510 }
1511 
1512 static inline int test_tgt_sess_count(struct qla_tgt *tgt)
1513 {
1514 	struct qla_hw_data *ha = tgt->ha;
1515 	unsigned long flags;
1516 	int res;
1517 	/*
1518 	 * We need to protect against race, when tgt is freed before or
1519 	 * inside wake_up()
1520 	 */
1521 	spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1522 	ql_dbg(ql_dbg_tgt, tgt->vha, 0xe002,
1523 	    "tgt %p, sess_count=%d\n",
1524 	    tgt, tgt->sess_count);
1525 	res = (tgt->sess_count == 0);
1526 	spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1527 
1528 	return res;
1529 }
1530 
1531 /* Called by tcm_qla2xxx configfs code */
1532 int qlt_stop_phase1(struct qla_tgt *tgt)
1533 {
1534 	struct scsi_qla_host *vha = tgt->vha;
1535 	struct qla_hw_data *ha = tgt->ha;
1536 	unsigned long flags;
1537 
1538 	mutex_lock(&ha->optrom_mutex);
1539 	mutex_lock(&qla_tgt_mutex);
1540 
1541 	if (tgt->tgt_stop || tgt->tgt_stopped) {
1542 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04e,
1543 		    "Already in tgt->tgt_stop or tgt_stopped state\n");
1544 		mutex_unlock(&qla_tgt_mutex);
1545 		mutex_unlock(&ha->optrom_mutex);
1546 		return -EPERM;
1547 	}
1548 
1549 	ql_dbg(ql_dbg_tgt_mgt, vha, 0xe003, "Stopping target for host %ld(%p)\n",
1550 	    vha->host_no, vha);
1551 	/*
1552 	 * Mutex needed to sync with qla_tgt_fc_port_[added,deleted].
1553 	 * Lock is needed, because we still can get an incoming packet.
1554 	 */
1555 	mutex_lock(&vha->vha_tgt.tgt_mutex);
1556 	tgt->tgt_stop = 1;
1557 	qlt_clear_tgt_db(tgt);
1558 	mutex_unlock(&vha->vha_tgt.tgt_mutex);
1559 	mutex_unlock(&qla_tgt_mutex);
1560 
1561 	ql_dbg(ql_dbg_tgt_mgt, vha, 0xf009,
1562 	    "Waiting for sess works (tgt %p)", tgt);
1563 	spin_lock_irqsave(&tgt->sess_work_lock, flags);
1564 	while (!list_empty(&tgt->sess_works_list)) {
1565 		spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1566 		flush_scheduled_work();
1567 		spin_lock_irqsave(&tgt->sess_work_lock, flags);
1568 	}
1569 	spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1570 
1571 	ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00a,
1572 	    "Waiting for tgt %p: sess_count=%d\n", tgt, tgt->sess_count);
1573 
1574 	wait_event_timeout(tgt->waitQ, test_tgt_sess_count(tgt), 10*HZ);
1575 
1576 	/* Big hammer */
1577 	if (!ha->flags.host_shutting_down &&
1578 	    (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)))
1579 		qlt_disable_vha(vha);
1580 
1581 	/* Wait for sessions to clear out (just in case) */
1582 	wait_event_timeout(tgt->waitQ, test_tgt_sess_count(tgt), 10*HZ);
1583 	mutex_unlock(&ha->optrom_mutex);
1584 
1585 	return 0;
1586 }
1587 EXPORT_SYMBOL(qlt_stop_phase1);
1588 
1589 /* Called by tcm_qla2xxx configfs code */
1590 void qlt_stop_phase2(struct qla_tgt *tgt)
1591 {
1592 	scsi_qla_host_t *vha = tgt->vha;
1593 
1594 	if (tgt->tgt_stopped) {
1595 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04f,
1596 		    "Already in tgt->tgt_stopped state\n");
1597 		dump_stack();
1598 		return;
1599 	}
1600 	if (!tgt->tgt_stop) {
1601 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00b,
1602 		    "%s: phase1 stop is not completed\n", __func__);
1603 		dump_stack();
1604 		return;
1605 	}
1606 
1607 	mutex_lock(&tgt->ha->optrom_mutex);
1608 	mutex_lock(&vha->vha_tgt.tgt_mutex);
1609 	tgt->tgt_stop = 0;
1610 	tgt->tgt_stopped = 1;
1611 	mutex_unlock(&vha->vha_tgt.tgt_mutex);
1612 	mutex_unlock(&tgt->ha->optrom_mutex);
1613 
1614 	ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00c, "Stop of tgt %p finished\n",
1615 	    tgt);
1616 
1617 	switch (vha->qlini_mode) {
1618 	case QLA2XXX_INI_MODE_EXCLUSIVE:
1619 		vha->flags.online = 1;
1620 		set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
1621 		break;
1622 	default:
1623 		break;
1624 	}
1625 }
1626 EXPORT_SYMBOL(qlt_stop_phase2);
1627 
1628 /* Called from qlt_remove_target() -> qla2x00_remove_one() */
1629 static void qlt_release(struct qla_tgt *tgt)
1630 {
1631 	scsi_qla_host_t *vha = tgt->vha;
1632 	void *node;
1633 	u64 key = 0;
1634 	u16 i;
1635 	struct qla_qpair_hint *h;
1636 	struct qla_hw_data *ha = vha->hw;
1637 
1638 	if (!tgt->tgt_stop && !tgt->tgt_stopped)
1639 		qlt_stop_phase1(tgt);
1640 
1641 	if (!tgt->tgt_stopped)
1642 		qlt_stop_phase2(tgt);
1643 
1644 	for (i = 0; i < vha->hw->max_qpairs + 1; i++) {
1645 		unsigned long flags;
1646 
1647 		h = &tgt->qphints[i];
1648 		if (h->qpair) {
1649 			spin_lock_irqsave(h->qpair->qp_lock_ptr, flags);
1650 			list_del(&h->hint_elem);
1651 			spin_unlock_irqrestore(h->qpair->qp_lock_ptr, flags);
1652 			h->qpair = NULL;
1653 		}
1654 	}
1655 	kfree(tgt->qphints);
1656 	mutex_lock(&qla_tgt_mutex);
1657 	list_del(&vha->vha_tgt.qla_tgt->tgt_list_entry);
1658 	mutex_unlock(&qla_tgt_mutex);
1659 
1660 	btree_for_each_safe64(&tgt->lun_qpair_map, key, node)
1661 		btree_remove64(&tgt->lun_qpair_map, key);
1662 
1663 	btree_destroy64(&tgt->lun_qpair_map);
1664 
1665 	if (vha->vp_idx)
1666 		if (ha->tgt.tgt_ops &&
1667 		    ha->tgt.tgt_ops->remove_target &&
1668 		    vha->vha_tgt.target_lport_ptr)
1669 			ha->tgt.tgt_ops->remove_target(vha);
1670 
1671 	vha->vha_tgt.qla_tgt = NULL;
1672 
1673 	ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00d,
1674 	    "Release of tgt %p finished\n", tgt);
1675 
1676 	kfree(tgt);
1677 }
1678 
1679 /* ha->hardware_lock supposed to be held on entry */
1680 static int qlt_sched_sess_work(struct qla_tgt *tgt, int type,
1681 	const void *param, unsigned int param_size)
1682 {
1683 	struct qla_tgt_sess_work_param *prm;
1684 	unsigned long flags;
1685 
1686 	prm = kzalloc(sizeof(*prm), GFP_ATOMIC);
1687 	if (!prm) {
1688 		ql_dbg(ql_dbg_tgt_mgt, tgt->vha, 0xf050,
1689 		    "qla_target(%d): Unable to create session "
1690 		    "work, command will be refused", 0);
1691 		return -ENOMEM;
1692 	}
1693 
1694 	ql_dbg(ql_dbg_tgt_mgt, tgt->vha, 0xf00e,
1695 	    "Scheduling work (type %d, prm %p)"
1696 	    " to find session for param %p (size %d, tgt %p)\n",
1697 	    type, prm, param, param_size, tgt);
1698 
1699 	prm->type = type;
1700 	memcpy(&prm->tm_iocb, param, param_size);
1701 
1702 	spin_lock_irqsave(&tgt->sess_work_lock, flags);
1703 	list_add_tail(&prm->sess_works_list_entry, &tgt->sess_works_list);
1704 	spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1705 
1706 	schedule_work(&tgt->sess_work);
1707 
1708 	return 0;
1709 }
1710 
1711 /*
1712  * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1713  */
1714 static void qlt_send_notify_ack(struct qla_qpair *qpair,
1715 	struct imm_ntfy_from_isp *ntfy,
1716 	uint32_t add_flags, uint16_t resp_code, int resp_code_valid,
1717 	uint16_t srr_flags, uint16_t srr_reject_code, uint8_t srr_explan)
1718 {
1719 	struct scsi_qla_host *vha = qpair->vha;
1720 	struct qla_hw_data *ha = vha->hw;
1721 	request_t *pkt;
1722 	struct nack_to_isp *nack;
1723 
1724 	if (!ha->flags.fw_started)
1725 		return;
1726 
1727 	ql_dbg(ql_dbg_tgt, vha, 0xe004, "Sending NOTIFY_ACK (ha=%p)\n", ha);
1728 
1729 	pkt = (request_t *)__qla2x00_alloc_iocbs(qpair, NULL);
1730 	if (!pkt) {
1731 		ql_dbg(ql_dbg_tgt, vha, 0xe049,
1732 		    "qla_target(%d): %s failed: unable to allocate "
1733 		    "request packet\n", vha->vp_idx, __func__);
1734 		return;
1735 	}
1736 
1737 	if (vha->vha_tgt.qla_tgt != NULL)
1738 		vha->vha_tgt.qla_tgt->notify_ack_expected++;
1739 
1740 	pkt->entry_type = NOTIFY_ACK_TYPE;
1741 	pkt->entry_count = 1;
1742 
1743 	nack = (struct nack_to_isp *)pkt;
1744 	nack->ox_id = ntfy->ox_id;
1745 
1746 	nack->u.isp24.handle = QLA_TGT_SKIP_HANDLE;
1747 	nack->u.isp24.nport_handle = ntfy->u.isp24.nport_handle;
1748 	if (le16_to_cpu(ntfy->u.isp24.status) == IMM_NTFY_ELS) {
1749 		nack->u.isp24.flags = ntfy->u.isp24.flags &
1750 			cpu_to_le16(NOTIFY24XX_FLAGS_PUREX_IOCB);
1751 	}
1752 	nack->u.isp24.srr_rx_id = ntfy->u.isp24.srr_rx_id;
1753 	nack->u.isp24.status = ntfy->u.isp24.status;
1754 	nack->u.isp24.status_subcode = ntfy->u.isp24.status_subcode;
1755 	nack->u.isp24.fw_handle = ntfy->u.isp24.fw_handle;
1756 	nack->u.isp24.exchange_address = ntfy->u.isp24.exchange_address;
1757 	nack->u.isp24.srr_rel_offs = ntfy->u.isp24.srr_rel_offs;
1758 	nack->u.isp24.srr_ui = ntfy->u.isp24.srr_ui;
1759 	nack->u.isp24.srr_flags = cpu_to_le16(srr_flags);
1760 	nack->u.isp24.srr_reject_code = srr_reject_code;
1761 	nack->u.isp24.srr_reject_code_expl = srr_explan;
1762 	nack->u.isp24.vp_index = ntfy->u.isp24.vp_index;
1763 
1764 	/* TODO qualify this with EDIF enable */
1765 	if (ntfy->u.isp24.status_subcode == ELS_PLOGI &&
1766 	    (le16_to_cpu(ntfy->u.isp24.flags) & NOTIFY24XX_FLAGS_FCSP)) {
1767 		nack->u.isp24.flags |= cpu_to_le16(NOTIFY_ACK_FLAGS_FCSP);
1768 	}
1769 
1770 	ql_dbg(ql_dbg_tgt, vha, 0xe005,
1771 	    "qla_target(%d): Sending 24xx Notify Ack %d\n",
1772 	    vha->vp_idx, nack->u.isp24.status);
1773 
1774 	/* Memory Barrier */
1775 	wmb();
1776 	qla2x00_start_iocbs(vha, qpair->req);
1777 }
1778 
1779 static int qlt_build_abts_resp_iocb(struct qla_tgt_mgmt_cmd *mcmd)
1780 {
1781 	struct scsi_qla_host *vha = mcmd->vha;
1782 	struct qla_hw_data *ha = vha->hw;
1783 	struct abts_resp_to_24xx *resp;
1784 	__le32 f_ctl;
1785 	uint32_t h;
1786 	uint8_t *p;
1787 	int rc;
1788 	struct abts_recv_from_24xx *abts = &mcmd->orig_iocb.abts;
1789 	struct qla_qpair *qpair = mcmd->qpair;
1790 
1791 	ql_dbg(ql_dbg_tgt, vha, 0xe006,
1792 	    "Sending task mgmt ABTS response (ha=%p, status=%x)\n",
1793 	    ha, mcmd->fc_tm_rsp);
1794 
1795 	rc = qlt_check_reserve_free_req(qpair, 1);
1796 	if (rc) {
1797 		ql_dbg(ql_dbg_tgt, vha, 0xe04a,
1798 		    "qla_target(%d): %s failed: unable to allocate request packet\n",
1799 		    vha->vp_idx, __func__);
1800 		return -EAGAIN;
1801 	}
1802 
1803 	resp = (struct abts_resp_to_24xx *)qpair->req->ring_ptr;
1804 	memset(resp, 0, sizeof(*resp));
1805 
1806 	h = qlt_make_handle(qpair);
1807 	if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
1808 		/*
1809 		 * CTIO type 7 from the firmware doesn't provide a way to
1810 		 * know the initiator's LOOP ID, hence we can't find
1811 		 * the session and, so, the command.
1812 		 */
1813 		return -EAGAIN;
1814 	} else {
1815 		qpair->req->outstanding_cmds[h] = (srb_t *)mcmd;
1816 	}
1817 
1818 	resp->handle = make_handle(qpair->req->id, h);
1819 	resp->entry_type = ABTS_RESP_24XX;
1820 	resp->entry_count = 1;
1821 	resp->nport_handle = abts->nport_handle;
1822 	resp->vp_index = vha->vp_idx;
1823 	resp->sof_type = abts->sof_type;
1824 	resp->exchange_address = abts->exchange_address;
1825 	resp->fcp_hdr_le = abts->fcp_hdr_le;
1826 	f_ctl = cpu_to_le32(F_CTL_EXCH_CONTEXT_RESP |
1827 	    F_CTL_LAST_SEQ | F_CTL_END_SEQ |
1828 	    F_CTL_SEQ_INITIATIVE);
1829 	p = (uint8_t *)&f_ctl;
1830 	resp->fcp_hdr_le.f_ctl[0] = *p++;
1831 	resp->fcp_hdr_le.f_ctl[1] = *p++;
1832 	resp->fcp_hdr_le.f_ctl[2] = *p;
1833 
1834 	resp->fcp_hdr_le.d_id = abts->fcp_hdr_le.s_id;
1835 	resp->fcp_hdr_le.s_id = abts->fcp_hdr_le.d_id;
1836 
1837 	resp->exchange_addr_to_abort = abts->exchange_addr_to_abort;
1838 	if (mcmd->fc_tm_rsp == FCP_TMF_CMPL) {
1839 		resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_ACC;
1840 		resp->payload.ba_acct.seq_id_valid = SEQ_ID_INVALID;
1841 		resp->payload.ba_acct.low_seq_cnt = 0x0000;
1842 		resp->payload.ba_acct.high_seq_cnt = cpu_to_le16(0xFFFF);
1843 		resp->payload.ba_acct.ox_id = abts->fcp_hdr_le.ox_id;
1844 		resp->payload.ba_acct.rx_id = abts->fcp_hdr_le.rx_id;
1845 	} else {
1846 		resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_RJT;
1847 		resp->payload.ba_rjt.reason_code =
1848 			BA_RJT_REASON_CODE_UNABLE_TO_PERFORM;
1849 		/* Other bytes are zero */
1850 	}
1851 
1852 	vha->vha_tgt.qla_tgt->abts_resp_expected++;
1853 
1854 	/* Memory Barrier */
1855 	wmb();
1856 	if (qpair->reqq_start_iocbs)
1857 		qpair->reqq_start_iocbs(qpair);
1858 	else
1859 		qla2x00_start_iocbs(vha, qpair->req);
1860 
1861 	return rc;
1862 }
1863 
1864 /*
1865  * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1866  */
1867 static void qlt_24xx_send_abts_resp(struct qla_qpair *qpair,
1868 	struct abts_recv_from_24xx *abts, uint32_t status,
1869 	bool ids_reversed)
1870 {
1871 	struct scsi_qla_host *vha = qpair->vha;
1872 	struct qla_hw_data *ha = vha->hw;
1873 	struct abts_resp_to_24xx *resp;
1874 	__le32 f_ctl;
1875 	uint8_t *p;
1876 
1877 	ql_dbg(ql_dbg_tgt, vha, 0xe006,
1878 	    "Sending task mgmt ABTS response (ha=%p, atio=%p, status=%x\n",
1879 	    ha, abts, status);
1880 
1881 	resp = (struct abts_resp_to_24xx *)qla2x00_alloc_iocbs_ready(qpair,
1882 	    NULL);
1883 	if (!resp) {
1884 		ql_dbg(ql_dbg_tgt, vha, 0xe04a,
1885 		    "qla_target(%d): %s failed: unable to allocate "
1886 		    "request packet", vha->vp_idx, __func__);
1887 		return;
1888 	}
1889 
1890 	resp->entry_type = ABTS_RESP_24XX;
1891 	resp->handle = QLA_TGT_SKIP_HANDLE;
1892 	resp->entry_count = 1;
1893 	resp->nport_handle = abts->nport_handle;
1894 	resp->vp_index = vha->vp_idx;
1895 	resp->sof_type = abts->sof_type;
1896 	resp->exchange_address = abts->exchange_address;
1897 	resp->fcp_hdr_le = abts->fcp_hdr_le;
1898 	f_ctl = cpu_to_le32(F_CTL_EXCH_CONTEXT_RESP |
1899 	    F_CTL_LAST_SEQ | F_CTL_END_SEQ |
1900 	    F_CTL_SEQ_INITIATIVE);
1901 	p = (uint8_t *)&f_ctl;
1902 	resp->fcp_hdr_le.f_ctl[0] = *p++;
1903 	resp->fcp_hdr_le.f_ctl[1] = *p++;
1904 	resp->fcp_hdr_le.f_ctl[2] = *p;
1905 	if (ids_reversed) {
1906 		resp->fcp_hdr_le.d_id = abts->fcp_hdr_le.d_id;
1907 		resp->fcp_hdr_le.s_id = abts->fcp_hdr_le.s_id;
1908 	} else {
1909 		resp->fcp_hdr_le.d_id = abts->fcp_hdr_le.s_id;
1910 		resp->fcp_hdr_le.s_id = abts->fcp_hdr_le.d_id;
1911 	}
1912 	resp->exchange_addr_to_abort = abts->exchange_addr_to_abort;
1913 	if (status == FCP_TMF_CMPL) {
1914 		resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_ACC;
1915 		resp->payload.ba_acct.seq_id_valid = SEQ_ID_INVALID;
1916 		resp->payload.ba_acct.low_seq_cnt = 0x0000;
1917 		resp->payload.ba_acct.high_seq_cnt = cpu_to_le16(0xFFFF);
1918 		resp->payload.ba_acct.ox_id = abts->fcp_hdr_le.ox_id;
1919 		resp->payload.ba_acct.rx_id = abts->fcp_hdr_le.rx_id;
1920 	} else {
1921 		resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_RJT;
1922 		resp->payload.ba_rjt.reason_code =
1923 			BA_RJT_REASON_CODE_UNABLE_TO_PERFORM;
1924 		/* Other bytes are zero */
1925 	}
1926 
1927 	vha->vha_tgt.qla_tgt->abts_resp_expected++;
1928 
1929 	/* Memory Barrier */
1930 	wmb();
1931 	if (qpair->reqq_start_iocbs)
1932 		qpair->reqq_start_iocbs(qpair);
1933 	else
1934 		qla2x00_start_iocbs(vha, qpair->req);
1935 }
1936 
1937 /*
1938  * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1939  */
1940 static void qlt_24xx_retry_term_exchange(struct scsi_qla_host *vha,
1941     struct qla_qpair *qpair, response_t *pkt, struct qla_tgt_mgmt_cmd *mcmd)
1942 {
1943 	struct ctio7_to_24xx *ctio;
1944 	u16 tmp;
1945 	struct abts_recv_from_24xx *entry;
1946 
1947 	ctio = (struct ctio7_to_24xx *)qla2x00_alloc_iocbs_ready(qpair, NULL);
1948 	if (ctio == NULL) {
1949 		ql_dbg(ql_dbg_tgt, vha, 0xe04b,
1950 		    "qla_target(%d): %s failed: unable to allocate "
1951 		    "request packet\n", vha->vp_idx, __func__);
1952 		return;
1953 	}
1954 
1955 	if (mcmd)
1956 		/* abts from remote port */
1957 		entry = &mcmd->orig_iocb.abts;
1958 	else
1959 		/* abts from this driver.  */
1960 		entry = (struct abts_recv_from_24xx *)pkt;
1961 
1962 	/*
1963 	 * We've got on entrance firmware's response on by us generated
1964 	 * ABTS response. So, in it ID fields are reversed.
1965 	 */
1966 
1967 	ctio->entry_type = CTIO_TYPE7;
1968 	ctio->entry_count = 1;
1969 	ctio->nport_handle = entry->nport_handle;
1970 	ctio->handle = QLA_TGT_SKIP_HANDLE |	CTIO_COMPLETION_HANDLE_MARK;
1971 	ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
1972 	ctio->vp_index = vha->vp_idx;
1973 	ctio->exchange_addr = entry->exchange_addr_to_abort;
1974 	tmp = (CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_TERMINATE);
1975 
1976 	if (mcmd) {
1977 		ctio->initiator_id = entry->fcp_hdr_le.s_id;
1978 
1979 		if (mcmd->flags & QLA24XX_MGMT_ABORT_IO_ATTR_VALID)
1980 			tmp |= (mcmd->abort_io_attr << 9);
1981 		else if (qpair->retry_term_cnt & 1)
1982 			tmp |= (0x4 << 9);
1983 	} else {
1984 		ctio->initiator_id = entry->fcp_hdr_le.d_id;
1985 
1986 		if (qpair->retry_term_cnt & 1)
1987 			tmp |= (0x4 << 9);
1988 	}
1989 	ctio->u.status1.flags = cpu_to_le16(tmp);
1990 	ctio->u.status1.ox_id = entry->fcp_hdr_le.ox_id;
1991 
1992 	ql_dbg(ql_dbg_tgt, vha, 0xe007,
1993 	    "Sending retry TERM EXCH CTIO7 flags %04xh oxid %04xh attr valid %x\n",
1994 	    le16_to_cpu(ctio->u.status1.flags),
1995 	    le16_to_cpu(ctio->u.status1.ox_id),
1996 	    (mcmd && mcmd->flags & QLA24XX_MGMT_ABORT_IO_ATTR_VALID) ? 1 : 0);
1997 
1998 	/* Memory Barrier */
1999 	wmb();
2000 	if (qpair->reqq_start_iocbs)
2001 		qpair->reqq_start_iocbs(qpair);
2002 	else
2003 		qla2x00_start_iocbs(vha, qpair->req);
2004 
2005 	if (mcmd)
2006 		qlt_build_abts_resp_iocb(mcmd);
2007 	else
2008 		qlt_24xx_send_abts_resp(qpair,
2009 		    (struct abts_recv_from_24xx *)entry, FCP_TMF_CMPL, true);
2010 
2011 }
2012 
2013 /* drop cmds for the given lun
2014  * XXX only looks for cmds on the port through which lun reset was recieved
2015  * XXX does not go through the list of other port (which may have cmds
2016  *     for the same lun)
2017  */
2018 static void abort_cmds_for_lun(struct scsi_qla_host *vha, u64 lun, be_id_t s_id)
2019 {
2020 	struct qla_tgt_sess_op *op;
2021 	struct qla_tgt_cmd *cmd;
2022 	uint32_t key;
2023 	unsigned long flags;
2024 
2025 	key = sid_to_key(s_id);
2026 	spin_lock_irqsave(&vha->cmd_list_lock, flags);
2027 	list_for_each_entry(op, &vha->unknown_atio_list, cmd_list) {
2028 		uint32_t op_key;
2029 		u64 op_lun;
2030 
2031 		op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
2032 		op_lun = scsilun_to_int(
2033 			(struct scsi_lun *)&op->atio.u.isp24.fcp_cmnd.lun);
2034 		if (op_key == key && op_lun == lun)
2035 			op->aborted = true;
2036 	}
2037 
2038 	list_for_each_entry(cmd, &vha->qla_cmd_list, cmd_list) {
2039 		uint32_t cmd_key;
2040 		u64 cmd_lun;
2041 
2042 		cmd_key = sid_to_key(cmd->atio.u.isp24.fcp_hdr.s_id);
2043 		cmd_lun = scsilun_to_int(
2044 			(struct scsi_lun *)&cmd->atio.u.isp24.fcp_cmnd.lun);
2045 		if (cmd_key == key && cmd_lun == lun)
2046 			cmd->aborted = 1;
2047 	}
2048 	spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
2049 }
2050 
2051 static struct qla_qpair_hint *qlt_find_qphint(struct scsi_qla_host *vha,
2052     uint64_t unpacked_lun)
2053 {
2054 	struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
2055 	struct qla_qpair_hint *h = NULL;
2056 
2057 	if (vha->flags.qpairs_available) {
2058 		h = btree_lookup64(&tgt->lun_qpair_map, unpacked_lun);
2059 		if (!h)
2060 			h = &tgt->qphints[0];
2061 	} else {
2062 		h = &tgt->qphints[0];
2063 	}
2064 
2065 	return h;
2066 }
2067 
2068 static void qlt_do_tmr_work(struct work_struct *work)
2069 {
2070 	struct qla_tgt_mgmt_cmd *mcmd =
2071 		container_of(work, struct qla_tgt_mgmt_cmd, work);
2072 	struct qla_hw_data *ha = mcmd->vha->hw;
2073 	int rc;
2074 	uint32_t tag;
2075 	unsigned long flags;
2076 
2077 	switch (mcmd->tmr_func) {
2078 	case QLA_TGT_ABTS:
2079 		tag = le32_to_cpu(mcmd->orig_iocb.abts.exchange_addr_to_abort);
2080 		break;
2081 	default:
2082 		tag = 0;
2083 		break;
2084 	}
2085 
2086 	rc = ha->tgt.tgt_ops->handle_tmr(mcmd, mcmd->unpacked_lun,
2087 	    mcmd->tmr_func, tag);
2088 
2089 	if (rc != 0) {
2090 		spin_lock_irqsave(mcmd->qpair->qp_lock_ptr, flags);
2091 		switch (mcmd->tmr_func) {
2092 		case QLA_TGT_ABTS:
2093 			mcmd->fc_tm_rsp = FCP_TMF_REJECTED;
2094 			qlt_build_abts_resp_iocb(mcmd);
2095 			break;
2096 		case QLA_TGT_LUN_RESET:
2097 		case QLA_TGT_CLEAR_TS:
2098 		case QLA_TGT_ABORT_TS:
2099 		case QLA_TGT_CLEAR_ACA:
2100 		case QLA_TGT_TARGET_RESET:
2101 			qlt_send_busy(mcmd->qpair, &mcmd->orig_iocb.atio,
2102 			    qla_sam_status);
2103 			break;
2104 
2105 		case QLA_TGT_ABORT_ALL:
2106 		case QLA_TGT_NEXUS_LOSS_SESS:
2107 		case QLA_TGT_NEXUS_LOSS:
2108 			qlt_send_notify_ack(mcmd->qpair,
2109 			    &mcmd->orig_iocb.imm_ntfy, 0, 0, 0, 0, 0, 0);
2110 			break;
2111 		}
2112 		spin_unlock_irqrestore(mcmd->qpair->qp_lock_ptr, flags);
2113 
2114 		ql_dbg(ql_dbg_tgt_mgt, mcmd->vha, 0xf052,
2115 		    "qla_target(%d):  tgt_ops->handle_tmr() failed: %d\n",
2116 		    mcmd->vha->vp_idx, rc);
2117 		mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
2118 	}
2119 }
2120 
2121 /* ha->hardware_lock supposed to be held on entry */
2122 static int __qlt_24xx_handle_abts(struct scsi_qla_host *vha,
2123 	struct abts_recv_from_24xx *abts, struct fc_port *sess)
2124 {
2125 	struct qla_hw_data *ha = vha->hw;
2126 	struct qla_tgt_mgmt_cmd *mcmd;
2127 	struct qla_qpair_hint *h = &vha->vha_tgt.qla_tgt->qphints[0];
2128 	struct qla_tgt_cmd *abort_cmd;
2129 
2130 	ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00f,
2131 	    "qla_target(%d): task abort (tag=%d)\n",
2132 	    vha->vp_idx, abts->exchange_addr_to_abort);
2133 
2134 	mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
2135 	if (mcmd == NULL) {
2136 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf051,
2137 		    "qla_target(%d): %s: Allocation of ABORT cmd failed",
2138 		    vha->vp_idx, __func__);
2139 		return -ENOMEM;
2140 	}
2141 	memset(mcmd, 0, sizeof(*mcmd));
2142 	mcmd->cmd_type = TYPE_TGT_TMCMD;
2143 	mcmd->sess = sess;
2144 	memcpy(&mcmd->orig_iocb.abts, abts, sizeof(mcmd->orig_iocb.abts));
2145 	mcmd->reset_count = ha->base_qpair->chip_reset;
2146 	mcmd->tmr_func = QLA_TGT_ABTS;
2147 	mcmd->qpair = h->qpair;
2148 	mcmd->vha = vha;
2149 
2150 	/*
2151 	 * LUN is looked up by target-core internally based on the passed
2152 	 * abts->exchange_addr_to_abort tag.
2153 	 */
2154 	mcmd->se_cmd.cpuid = h->cpuid;
2155 
2156 	abort_cmd = ha->tgt.tgt_ops->find_cmd_by_tag(sess,
2157 				le32_to_cpu(abts->exchange_addr_to_abort));
2158 	if (!abort_cmd)
2159 		return -EIO;
2160 	mcmd->unpacked_lun = abort_cmd->se_cmd.orig_fe_lun;
2161 
2162 	if (abort_cmd->qpair) {
2163 		mcmd->qpair = abort_cmd->qpair;
2164 		mcmd->se_cmd.cpuid = abort_cmd->se_cmd.cpuid;
2165 		mcmd->abort_io_attr = abort_cmd->atio.u.isp24.attr;
2166 		mcmd->flags = QLA24XX_MGMT_ABORT_IO_ATTR_VALID;
2167 	}
2168 
2169 	INIT_WORK(&mcmd->work, qlt_do_tmr_work);
2170 	queue_work_on(mcmd->se_cmd.cpuid, qla_tgt_wq, &mcmd->work);
2171 
2172 	return 0;
2173 }
2174 
2175 /*
2176  * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
2177  */
2178 static void qlt_24xx_handle_abts(struct scsi_qla_host *vha,
2179 	struct abts_recv_from_24xx *abts)
2180 {
2181 	struct qla_hw_data *ha = vha->hw;
2182 	struct fc_port *sess;
2183 	uint32_t tag = le32_to_cpu(abts->exchange_addr_to_abort);
2184 	be_id_t s_id;
2185 	int rc;
2186 	unsigned long flags;
2187 
2188 	if (le32_to_cpu(abts->fcp_hdr_le.parameter) & ABTS_PARAM_ABORT_SEQ) {
2189 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf053,
2190 		    "qla_target(%d): ABTS: Abort Sequence not "
2191 		    "supported\n", vha->vp_idx);
2192 		qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2193 		    false);
2194 		return;
2195 	}
2196 
2197 	if (tag == ATIO_EXCHANGE_ADDRESS_UNKNOWN) {
2198 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf010,
2199 		    "qla_target(%d): ABTS: Unknown Exchange "
2200 		    "Address received\n", vha->vp_idx);
2201 		qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2202 		    false);
2203 		return;
2204 	}
2205 
2206 	ql_dbg(ql_dbg_tgt_mgt, vha, 0xf011,
2207 	    "qla_target(%d): task abort (s_id=%x:%x:%x, "
2208 	    "tag=%d, param=%x)\n", vha->vp_idx, abts->fcp_hdr_le.s_id.domain,
2209 	    abts->fcp_hdr_le.s_id.area, abts->fcp_hdr_le.s_id.al_pa, tag,
2210 	    le32_to_cpu(abts->fcp_hdr_le.parameter));
2211 
2212 	s_id = le_id_to_be(abts->fcp_hdr_le.s_id);
2213 
2214 	spin_lock_irqsave(&ha->tgt.sess_lock, flags);
2215 	sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id);
2216 	if (!sess) {
2217 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf012,
2218 		    "qla_target(%d): task abort for non-existent session\n",
2219 		    vha->vp_idx);
2220 		spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
2221 
2222 		qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2223 			    false);
2224 		return;
2225 	}
2226 	spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
2227 
2228 
2229 	if (sess->deleted) {
2230 		qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2231 		    false);
2232 		return;
2233 	}
2234 
2235 	rc = __qlt_24xx_handle_abts(vha, abts, sess);
2236 	if (rc != 0) {
2237 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf054,
2238 		    "qla_target(%d): __qlt_24xx_handle_abts() failed: %d\n",
2239 		    vha->vp_idx, rc);
2240 		qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2241 		    false);
2242 		return;
2243 	}
2244 }
2245 
2246 /*
2247  * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
2248  */
2249 static void qlt_24xx_send_task_mgmt_ctio(struct qla_qpair *qpair,
2250 	struct qla_tgt_mgmt_cmd *mcmd, uint32_t resp_code)
2251 {
2252 	struct scsi_qla_host *ha = mcmd->vha;
2253 	struct atio_from_isp *atio = &mcmd->orig_iocb.atio;
2254 	struct ctio7_to_24xx *ctio;
2255 	uint16_t temp;
2256 
2257 	ql_dbg(ql_dbg_tgt, ha, 0xe008,
2258 	    "Sending task mgmt CTIO7 (ha=%p, atio=%p, resp_code=%x\n",
2259 	    ha, atio, resp_code);
2260 
2261 
2262 	ctio = (struct ctio7_to_24xx *)__qla2x00_alloc_iocbs(qpair, NULL);
2263 	if (ctio == NULL) {
2264 		ql_dbg(ql_dbg_tgt, ha, 0xe04c,
2265 		    "qla_target(%d): %s failed: unable to allocate "
2266 		    "request packet\n", ha->vp_idx, __func__);
2267 		return;
2268 	}
2269 
2270 	ctio->entry_type = CTIO_TYPE7;
2271 	ctio->entry_count = 1;
2272 	ctio->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
2273 	ctio->nport_handle = cpu_to_le16(mcmd->sess->loop_id);
2274 	ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
2275 	ctio->vp_index = ha->vp_idx;
2276 	ctio->initiator_id = be_id_to_le(atio->u.isp24.fcp_hdr.s_id);
2277 	ctio->exchange_addr = atio->u.isp24.exchange_addr;
2278 	temp = (atio->u.isp24.attr << 9)|
2279 		CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS;
2280 	ctio->u.status1.flags = cpu_to_le16(temp);
2281 	temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2282 	ctio->u.status1.ox_id = cpu_to_le16(temp);
2283 	ctio->u.status1.scsi_status =
2284 	    cpu_to_le16(SS_RESPONSE_INFO_LEN_VALID);
2285 	ctio->u.status1.response_len = cpu_to_le16(8);
2286 	ctio->u.status1.sense_data[0] = resp_code;
2287 
2288 	/* Memory Barrier */
2289 	wmb();
2290 	if (qpair->reqq_start_iocbs)
2291 		qpair->reqq_start_iocbs(qpair);
2292 	else
2293 		qla2x00_start_iocbs(ha, qpair->req);
2294 }
2295 
2296 void qlt_free_mcmd(struct qla_tgt_mgmt_cmd *mcmd)
2297 {
2298 	mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
2299 }
2300 EXPORT_SYMBOL(qlt_free_mcmd);
2301 
2302 /*
2303  * ha->hardware_lock supposed to be held on entry. Might drop it, then
2304  * reacquire
2305  */
2306 void qlt_send_resp_ctio(struct qla_qpair *qpair, struct qla_tgt_cmd *cmd,
2307     uint8_t scsi_status, uint8_t sense_key, uint8_t asc, uint8_t ascq)
2308 {
2309 	struct atio_from_isp *atio = &cmd->atio;
2310 	struct ctio7_to_24xx *ctio;
2311 	uint16_t temp;
2312 	struct scsi_qla_host *vha = cmd->vha;
2313 
2314 	ql_dbg(ql_dbg_tgt_dif, vha, 0x3066,
2315 	    "Sending response CTIO7 (vha=%p, atio=%p, scsi_status=%02x, "
2316 	    "sense_key=%02x, asc=%02x, ascq=%02x",
2317 	    vha, atio, scsi_status, sense_key, asc, ascq);
2318 
2319 	ctio = (struct ctio7_to_24xx *)qla2x00_alloc_iocbs(vha, NULL);
2320 	if (!ctio) {
2321 		ql_dbg(ql_dbg_async, vha, 0x3067,
2322 		    "qla2x00t(%ld): %s failed: unable to allocate request packet",
2323 		    vha->host_no, __func__);
2324 		goto out;
2325 	}
2326 
2327 	ctio->entry_type = CTIO_TYPE7;
2328 	ctio->entry_count = 1;
2329 	ctio->handle = QLA_TGT_SKIP_HANDLE;
2330 	ctio->nport_handle = cpu_to_le16(cmd->sess->loop_id);
2331 	ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
2332 	ctio->vp_index = vha->vp_idx;
2333 	ctio->initiator_id = be_id_to_le(atio->u.isp24.fcp_hdr.s_id);
2334 	ctio->exchange_addr = atio->u.isp24.exchange_addr;
2335 	temp = (atio->u.isp24.attr << 9) |
2336 	    CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS;
2337 	ctio->u.status1.flags = cpu_to_le16(temp);
2338 	temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2339 	ctio->u.status1.ox_id = cpu_to_le16(temp);
2340 	ctio->u.status1.scsi_status =
2341 	    cpu_to_le16(SS_RESPONSE_INFO_LEN_VALID | scsi_status);
2342 	ctio->u.status1.response_len = cpu_to_le16(18);
2343 	ctio->u.status1.residual = cpu_to_le32(get_datalen_for_atio(atio));
2344 
2345 	if (ctio->u.status1.residual != 0)
2346 		ctio->u.status1.scsi_status |=
2347 		    cpu_to_le16(SS_RESIDUAL_UNDER);
2348 
2349 	/* Fixed format sense data. */
2350 	ctio->u.status1.sense_data[0] = 0x70;
2351 	ctio->u.status1.sense_data[2] = sense_key;
2352 	/* Additional sense length */
2353 	ctio->u.status1.sense_data[7] = 0xa;
2354 	/* ASC and ASCQ */
2355 	ctio->u.status1.sense_data[12] = asc;
2356 	ctio->u.status1.sense_data[13] = ascq;
2357 
2358 	/* Memory Barrier */
2359 	wmb();
2360 
2361 	if (qpair->reqq_start_iocbs)
2362 		qpair->reqq_start_iocbs(qpair);
2363 	else
2364 		qla2x00_start_iocbs(vha, qpair->req);
2365 
2366 out:
2367 	return;
2368 }
2369 
2370 /* callback from target fabric module code */
2371 void qlt_xmit_tm_rsp(struct qla_tgt_mgmt_cmd *mcmd)
2372 {
2373 	struct scsi_qla_host *vha = mcmd->sess->vha;
2374 	struct qla_hw_data *ha = vha->hw;
2375 	unsigned long flags;
2376 	struct qla_qpair *qpair = mcmd->qpair;
2377 	bool free_mcmd = true;
2378 
2379 	ql_dbg(ql_dbg_tgt_mgt, vha, 0xf013,
2380 	    "TM response mcmd (%p) status %#x state %#x",
2381 	    mcmd, mcmd->fc_tm_rsp, mcmd->flags);
2382 
2383 	spin_lock_irqsave(qpair->qp_lock_ptr, flags);
2384 
2385 	if (!vha->flags.online || mcmd->reset_count != qpair->chip_reset) {
2386 		/*
2387 		 * Either the port is not online or this request was from
2388 		 * previous life, just abort the processing.
2389 		 */
2390 		ql_dbg(ql_dbg_async, vha, 0xe100,
2391 			"RESET-TMR online/active/old-count/new-count = %d/%d/%d/%d.\n",
2392 			vha->flags.online, qla2x00_reset_active(vha),
2393 			mcmd->reset_count, qpair->chip_reset);
2394 		ha->tgt.tgt_ops->free_mcmd(mcmd);
2395 		spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
2396 		return;
2397 	}
2398 
2399 	if (mcmd->flags == QLA24XX_MGMT_SEND_NACK) {
2400 		switch (mcmd->orig_iocb.imm_ntfy.u.isp24.status_subcode) {
2401 		case ELS_LOGO:
2402 		case ELS_PRLO:
2403 		case ELS_TPRLO:
2404 			ql_dbg(ql_dbg_disc, vha, 0x2106,
2405 			    "TM response logo %8phC status %#x state %#x",
2406 			    mcmd->sess->port_name, mcmd->fc_tm_rsp,
2407 			    mcmd->flags);
2408 			qlt_schedule_sess_for_deletion(mcmd->sess);
2409 			break;
2410 		default:
2411 			qlt_send_notify_ack(vha->hw->base_qpair,
2412 			    &mcmd->orig_iocb.imm_ntfy, 0, 0, 0, 0, 0, 0);
2413 			break;
2414 		}
2415 	} else {
2416 		if (mcmd->orig_iocb.atio.u.raw.entry_type == ABTS_RECV_24XX) {
2417 			qlt_build_abts_resp_iocb(mcmd);
2418 			free_mcmd = false;
2419 		} else
2420 			qlt_24xx_send_task_mgmt_ctio(qpair, mcmd,
2421 			    mcmd->fc_tm_rsp);
2422 	}
2423 	/*
2424 	 * Make the callback for ->free_mcmd() to queue_work() and invoke
2425 	 * target_put_sess_cmd() to drop cmd_kref to 1.  The final
2426 	 * target_put_sess_cmd() call will be made from TFO->check_stop_free()
2427 	 * -> tcm_qla2xxx_check_stop_free() to release the TMR associated se_cmd
2428 	 * descriptor after TFO->queue_tm_rsp() -> tcm_qla2xxx_queue_tm_rsp() ->
2429 	 * qlt_xmit_tm_rsp() returns here..
2430 	 */
2431 	if (free_mcmd)
2432 		ha->tgt.tgt_ops->free_mcmd(mcmd);
2433 
2434 	spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
2435 }
2436 EXPORT_SYMBOL(qlt_xmit_tm_rsp);
2437 
2438 /* No locks */
2439 static int qlt_pci_map_calc_cnt(struct qla_tgt_prm *prm)
2440 {
2441 	struct qla_tgt_cmd *cmd = prm->cmd;
2442 
2443 	BUG_ON(cmd->sg_cnt == 0);
2444 
2445 	prm->sg = (struct scatterlist *)cmd->sg;
2446 	prm->seg_cnt = dma_map_sg(&cmd->qpair->pdev->dev, cmd->sg,
2447 	    cmd->sg_cnt, cmd->dma_data_direction);
2448 	if (unlikely(prm->seg_cnt == 0))
2449 		goto out_err;
2450 
2451 	prm->cmd->sg_mapped = 1;
2452 
2453 	if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL) {
2454 		/*
2455 		 * If greater than four sg entries then we need to allocate
2456 		 * the continuation entries
2457 		 */
2458 		if (prm->seg_cnt > QLA_TGT_DATASEGS_PER_CMD_24XX)
2459 			prm->req_cnt += DIV_ROUND_UP(prm->seg_cnt -
2460 			QLA_TGT_DATASEGS_PER_CMD_24XX,
2461 			QLA_TGT_DATASEGS_PER_CONT_24XX);
2462 	} else {
2463 		/* DIF */
2464 		if ((cmd->se_cmd.prot_op == TARGET_PROT_DIN_INSERT) ||
2465 		    (cmd->se_cmd.prot_op == TARGET_PROT_DOUT_STRIP)) {
2466 			prm->seg_cnt = DIV_ROUND_UP(cmd->bufflen, cmd->blk_sz);
2467 			prm->tot_dsds = prm->seg_cnt;
2468 		} else
2469 			prm->tot_dsds = prm->seg_cnt;
2470 
2471 		if (cmd->prot_sg_cnt) {
2472 			prm->prot_sg      = cmd->prot_sg;
2473 			prm->prot_seg_cnt = dma_map_sg(&cmd->qpair->pdev->dev,
2474 				cmd->prot_sg, cmd->prot_sg_cnt,
2475 				cmd->dma_data_direction);
2476 			if (unlikely(prm->prot_seg_cnt == 0))
2477 				goto out_err;
2478 
2479 			if ((cmd->se_cmd.prot_op == TARGET_PROT_DIN_INSERT) ||
2480 			    (cmd->se_cmd.prot_op == TARGET_PROT_DOUT_STRIP)) {
2481 				/* Dif Bundling not support here */
2482 				prm->prot_seg_cnt = DIV_ROUND_UP(cmd->bufflen,
2483 								cmd->blk_sz);
2484 				prm->tot_dsds += prm->prot_seg_cnt;
2485 			} else
2486 				prm->tot_dsds += prm->prot_seg_cnt;
2487 		}
2488 	}
2489 
2490 	return 0;
2491 
2492 out_err:
2493 	ql_dbg_qp(ql_dbg_tgt, prm->cmd->qpair, 0xe04d,
2494 	    "qla_target(%d): PCI mapping failed: sg_cnt=%d",
2495 	    0, prm->cmd->sg_cnt);
2496 	return -1;
2497 }
2498 
2499 static void qlt_unmap_sg(struct scsi_qla_host *vha, struct qla_tgt_cmd *cmd)
2500 {
2501 	struct qla_hw_data *ha;
2502 	struct qla_qpair *qpair;
2503 
2504 	if (!cmd->sg_mapped)
2505 		return;
2506 
2507 	qpair = cmd->qpair;
2508 
2509 	dma_unmap_sg(&qpair->pdev->dev, cmd->sg, cmd->sg_cnt,
2510 	    cmd->dma_data_direction);
2511 	cmd->sg_mapped = 0;
2512 
2513 	if (cmd->prot_sg_cnt)
2514 		dma_unmap_sg(&qpair->pdev->dev, cmd->prot_sg, cmd->prot_sg_cnt,
2515 			cmd->dma_data_direction);
2516 
2517 	if (!cmd->ctx)
2518 		return;
2519 	ha = vha->hw;
2520 	if (cmd->ctx_dsd_alloced)
2521 		qla2x00_clean_dsd_pool(ha, cmd->ctx);
2522 
2523 	dma_pool_free(ha->dl_dma_pool, cmd->ctx, cmd->ctx->crc_ctx_dma);
2524 }
2525 
2526 static int qlt_check_reserve_free_req(struct qla_qpair *qpair,
2527 	uint32_t req_cnt)
2528 {
2529 	uint32_t cnt;
2530 	struct req_que *req = qpair->req;
2531 
2532 	if (req->cnt < (req_cnt + 2)) {
2533 		cnt = (uint16_t)(qpair->use_shadow_reg ? *req->out_ptr :
2534 		    rd_reg_dword_relaxed(req->req_q_out));
2535 
2536 		if  (req->ring_index < cnt)
2537 			req->cnt = cnt - req->ring_index;
2538 		else
2539 			req->cnt = req->length - (req->ring_index - cnt);
2540 
2541 		if (unlikely(req->cnt < (req_cnt + 2)))
2542 			return -EAGAIN;
2543 	}
2544 
2545 	req->cnt -= req_cnt;
2546 
2547 	return 0;
2548 }
2549 
2550 /*
2551  * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
2552  */
2553 static inline void *qlt_get_req_pkt(struct req_que *req)
2554 {
2555 	/* Adjust ring index. */
2556 	req->ring_index++;
2557 	if (req->ring_index == req->length) {
2558 		req->ring_index = 0;
2559 		req->ring_ptr = req->ring;
2560 	} else {
2561 		req->ring_ptr++;
2562 	}
2563 	return (cont_entry_t *)req->ring_ptr;
2564 }
2565 
2566 /* ha->hardware_lock supposed to be held on entry */
2567 static inline uint32_t qlt_make_handle(struct qla_qpair *qpair)
2568 {
2569 	uint32_t h;
2570 	int index;
2571 	uint8_t found = 0;
2572 	struct req_que *req = qpair->req;
2573 
2574 	h = req->current_outstanding_cmd;
2575 
2576 	for (index = 1; index < req->num_outstanding_cmds; index++) {
2577 		h++;
2578 		if (h == req->num_outstanding_cmds)
2579 			h = 1;
2580 
2581 		if (h == QLA_TGT_SKIP_HANDLE)
2582 			continue;
2583 
2584 		if (!req->outstanding_cmds[h]) {
2585 			found = 1;
2586 			break;
2587 		}
2588 	}
2589 
2590 	if (found) {
2591 		req->current_outstanding_cmd = h;
2592 	} else {
2593 		ql_dbg(ql_dbg_io, qpair->vha, 0x305b,
2594 		    "qla_target(%d): Ran out of empty cmd slots\n",
2595 		    qpair->vha->vp_idx);
2596 		h = QLA_TGT_NULL_HANDLE;
2597 	}
2598 
2599 	return h;
2600 }
2601 
2602 /* ha->hardware_lock supposed to be held on entry */
2603 static int qlt_24xx_build_ctio_pkt(struct qla_qpair *qpair,
2604 	struct qla_tgt_prm *prm)
2605 {
2606 	uint32_t h;
2607 	struct ctio7_to_24xx *pkt;
2608 	struct atio_from_isp *atio = &prm->cmd->atio;
2609 	uint16_t temp;
2610 	struct qla_tgt_cmd      *cmd = prm->cmd;
2611 
2612 	pkt = (struct ctio7_to_24xx *)qpair->req->ring_ptr;
2613 	prm->pkt = pkt;
2614 	memset(pkt, 0, sizeof(*pkt));
2615 
2616 	pkt->entry_type = CTIO_TYPE7;
2617 	pkt->entry_count = (uint8_t)prm->req_cnt;
2618 	pkt->vp_index = prm->cmd->vp_idx;
2619 
2620 	h = qlt_make_handle(qpair);
2621 	if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
2622 		/*
2623 		 * CTIO type 7 from the firmware doesn't provide a way to
2624 		 * know the initiator's LOOP ID, hence we can't find
2625 		 * the session and, so, the command.
2626 		 */
2627 		return -EAGAIN;
2628 	} else
2629 		qpair->req->outstanding_cmds[h] = (srb_t *)prm->cmd;
2630 
2631 	pkt->handle = make_handle(qpair->req->id, h);
2632 	pkt->handle |= CTIO_COMPLETION_HANDLE_MARK;
2633 	pkt->nport_handle = cpu_to_le16(prm->cmd->loop_id);
2634 	pkt->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
2635 	pkt->initiator_id = be_id_to_le(atio->u.isp24.fcp_hdr.s_id);
2636 	pkt->exchange_addr = atio->u.isp24.exchange_addr;
2637 	temp = atio->u.isp24.attr << 9;
2638 	pkt->u.status0.flags |= cpu_to_le16(temp);
2639 	temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2640 	pkt->u.status0.ox_id = cpu_to_le16(temp);
2641 	pkt->u.status0.relative_offset = cpu_to_le32(prm->cmd->offset);
2642 
2643 	if (cmd->edif) {
2644 		if (cmd->dma_data_direction == DMA_TO_DEVICE)
2645 			prm->cmd->sess->edif.rx_bytes += cmd->bufflen;
2646 		if (cmd->dma_data_direction == DMA_FROM_DEVICE)
2647 			prm->cmd->sess->edif.tx_bytes += cmd->bufflen;
2648 
2649 		pkt->u.status0.edif_flags |= EF_EN_EDIF;
2650 	}
2651 
2652 	return 0;
2653 }
2654 
2655 /*
2656  * ha->hardware_lock supposed to be held on entry. We have already made sure
2657  * that there is sufficient amount of request entries to not drop it.
2658  */
2659 static void qlt_load_cont_data_segments(struct qla_tgt_prm *prm)
2660 {
2661 	int cnt;
2662 	struct dsd64 *cur_dsd;
2663 
2664 	/* Build continuation packets */
2665 	while (prm->seg_cnt > 0) {
2666 		cont_a64_entry_t *cont_pkt64 =
2667 			(cont_a64_entry_t *)qlt_get_req_pkt(
2668 			   prm->cmd->qpair->req);
2669 
2670 		/*
2671 		 * Make sure that from cont_pkt64 none of
2672 		 * 64-bit specific fields used for 32-bit
2673 		 * addressing. Cast to (cont_entry_t *) for
2674 		 * that.
2675 		 */
2676 
2677 		memset(cont_pkt64, 0, sizeof(*cont_pkt64));
2678 
2679 		cont_pkt64->entry_count = 1;
2680 		cont_pkt64->sys_define = 0;
2681 
2682 		cont_pkt64->entry_type = CONTINUE_A64_TYPE;
2683 		cur_dsd = cont_pkt64->dsd;
2684 
2685 		/* Load continuation entry data segments */
2686 		for (cnt = 0;
2687 		    cnt < QLA_TGT_DATASEGS_PER_CONT_24XX && prm->seg_cnt;
2688 		    cnt++, prm->seg_cnt--) {
2689 			append_dsd64(&cur_dsd, prm->sg);
2690 			prm->sg = sg_next(prm->sg);
2691 		}
2692 	}
2693 }
2694 
2695 /*
2696  * ha->hardware_lock supposed to be held on entry. We have already made sure
2697  * that there is sufficient amount of request entries to not drop it.
2698  */
2699 static void qlt_load_data_segments(struct qla_tgt_prm *prm)
2700 {
2701 	int cnt;
2702 	struct dsd64 *cur_dsd;
2703 	struct ctio7_to_24xx *pkt24 = (struct ctio7_to_24xx *)prm->pkt;
2704 
2705 	pkt24->u.status0.transfer_length = cpu_to_le32(prm->cmd->bufflen);
2706 
2707 	/* Setup packet address segment pointer */
2708 	cur_dsd = &pkt24->u.status0.dsd;
2709 
2710 	/* Set total data segment count */
2711 	if (prm->seg_cnt)
2712 		pkt24->dseg_count = cpu_to_le16(prm->seg_cnt);
2713 
2714 	if (prm->seg_cnt == 0) {
2715 		/* No data transfer */
2716 		cur_dsd->address = 0;
2717 		cur_dsd->length = 0;
2718 		return;
2719 	}
2720 
2721 	/* If scatter gather */
2722 
2723 	/* Load command entry data segments */
2724 	for (cnt = 0;
2725 	    (cnt < QLA_TGT_DATASEGS_PER_CMD_24XX) && prm->seg_cnt;
2726 	    cnt++, prm->seg_cnt--) {
2727 		append_dsd64(&cur_dsd, prm->sg);
2728 		prm->sg = sg_next(prm->sg);
2729 	}
2730 
2731 	qlt_load_cont_data_segments(prm);
2732 }
2733 
2734 static inline int qlt_has_data(struct qla_tgt_cmd *cmd)
2735 {
2736 	return cmd->bufflen > 0;
2737 }
2738 
2739 static void qlt_print_dif_err(struct qla_tgt_prm *prm)
2740 {
2741 	struct qla_tgt_cmd *cmd;
2742 	struct scsi_qla_host *vha;
2743 
2744 	/* asc 0x10=dif error */
2745 	if (prm->sense_buffer && (prm->sense_buffer[12] == 0x10)) {
2746 		cmd = prm->cmd;
2747 		vha = cmd->vha;
2748 		/* ASCQ */
2749 		switch (prm->sense_buffer[13]) {
2750 		case 1:
2751 			ql_dbg(ql_dbg_tgt_dif, vha, 0xe00b,
2752 			    "BE detected Guard TAG ERR: lba[0x%llx|%lld] len[0x%x] "
2753 			    "se_cmd=%p tag[%x]",
2754 			    cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2755 			    cmd->atio.u.isp24.exchange_addr);
2756 			break;
2757 		case 2:
2758 			ql_dbg(ql_dbg_tgt_dif, vha, 0xe00c,
2759 			    "BE detected APP TAG ERR: lba[0x%llx|%lld] len[0x%x] "
2760 			    "se_cmd=%p tag[%x]",
2761 			    cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2762 			    cmd->atio.u.isp24.exchange_addr);
2763 			break;
2764 		case 3:
2765 			ql_dbg(ql_dbg_tgt_dif, vha, 0xe00f,
2766 			    "BE detected REF TAG ERR: lba[0x%llx|%lld] len[0x%x] "
2767 			    "se_cmd=%p tag[%x]",
2768 			    cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2769 			    cmd->atio.u.isp24.exchange_addr);
2770 			break;
2771 		default:
2772 			ql_dbg(ql_dbg_tgt_dif, vha, 0xe010,
2773 			    "BE detected Dif ERR: lba[%llx|%lld] len[%x] "
2774 			    "se_cmd=%p tag[%x]",
2775 			    cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2776 			    cmd->atio.u.isp24.exchange_addr);
2777 			break;
2778 		}
2779 		ql_dump_buffer(ql_dbg_tgt_dif, vha, 0xe011, cmd->cdb, 16);
2780 	}
2781 }
2782 
2783 /*
2784  * Called without ha->hardware_lock held
2785  */
2786 static int qlt_pre_xmit_response(struct qla_tgt_cmd *cmd,
2787 	struct qla_tgt_prm *prm, int xmit_type, uint8_t scsi_status,
2788 	uint32_t *full_req_cnt)
2789 {
2790 	struct se_cmd *se_cmd = &cmd->se_cmd;
2791 	struct qla_qpair *qpair = cmd->qpair;
2792 
2793 	prm->cmd = cmd;
2794 	prm->tgt = cmd->tgt;
2795 	prm->pkt = NULL;
2796 	prm->rq_result = scsi_status;
2797 	prm->sense_buffer = &cmd->sense_buffer[0];
2798 	prm->sense_buffer_len = TRANSPORT_SENSE_BUFFER;
2799 	prm->sg = NULL;
2800 	prm->seg_cnt = -1;
2801 	prm->req_cnt = 1;
2802 	prm->residual = 0;
2803 	prm->add_status_pkt = 0;
2804 	prm->prot_sg = NULL;
2805 	prm->prot_seg_cnt = 0;
2806 	prm->tot_dsds = 0;
2807 
2808 	if ((xmit_type & QLA_TGT_XMIT_DATA) && qlt_has_data(cmd)) {
2809 		if  (qlt_pci_map_calc_cnt(prm) != 0)
2810 			return -EAGAIN;
2811 	}
2812 
2813 	*full_req_cnt = prm->req_cnt;
2814 
2815 	if (se_cmd->se_cmd_flags & SCF_UNDERFLOW_BIT) {
2816 		prm->residual = se_cmd->residual_count;
2817 		ql_dbg_qp(ql_dbg_io + ql_dbg_verbose, qpair, 0x305c,
2818 		    "Residual underflow: %d (tag %lld, op %x, bufflen %d, rq_result %x)\n",
2819 		       prm->residual, se_cmd->tag,
2820 		       se_cmd->t_task_cdb ? se_cmd->t_task_cdb[0] : 0,
2821 		       cmd->bufflen, prm->rq_result);
2822 		prm->rq_result |= SS_RESIDUAL_UNDER;
2823 	} else if (se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT) {
2824 		prm->residual = se_cmd->residual_count;
2825 		ql_dbg_qp(ql_dbg_io, qpair, 0x305d,
2826 		    "Residual overflow: %d (tag %lld, op %x, bufflen %d, rq_result %x)\n",
2827 		       prm->residual, se_cmd->tag, se_cmd->t_task_cdb ?
2828 		       se_cmd->t_task_cdb[0] : 0, cmd->bufflen, prm->rq_result);
2829 		prm->rq_result |= SS_RESIDUAL_OVER;
2830 	}
2831 
2832 	if (xmit_type & QLA_TGT_XMIT_STATUS) {
2833 		/*
2834 		 * If QLA_TGT_XMIT_DATA is not set, add_status_pkt will be
2835 		 * ignored in *xmit_response() below
2836 		 */
2837 		if (qlt_has_data(cmd)) {
2838 			if (QLA_TGT_SENSE_VALID(prm->sense_buffer) ||
2839 			    (IS_FWI2_CAPABLE(cmd->vha->hw) &&
2840 			    (prm->rq_result != 0))) {
2841 				prm->add_status_pkt = 1;
2842 				(*full_req_cnt)++;
2843 			}
2844 		}
2845 	}
2846 
2847 	return 0;
2848 }
2849 
2850 static inline int qlt_need_explicit_conf(struct qla_tgt_cmd *cmd,
2851     int sending_sense)
2852 {
2853 	if (cmd->qpair->enable_class_2)
2854 		return 0;
2855 
2856 	if (sending_sense)
2857 		return cmd->conf_compl_supported;
2858 	else
2859 		return cmd->qpair->enable_explicit_conf &&
2860                     cmd->conf_compl_supported;
2861 }
2862 
2863 static void qlt_24xx_init_ctio_to_isp(struct ctio7_to_24xx *ctio,
2864 	struct qla_tgt_prm *prm)
2865 {
2866 	prm->sense_buffer_len = min_t(uint32_t, prm->sense_buffer_len,
2867 	    (uint32_t)sizeof(ctio->u.status1.sense_data));
2868 	ctio->u.status0.flags |= cpu_to_le16(CTIO7_FLAGS_SEND_STATUS);
2869 	if (qlt_need_explicit_conf(prm->cmd, 0)) {
2870 		ctio->u.status0.flags |= cpu_to_le16(
2871 		    CTIO7_FLAGS_EXPLICIT_CONFORM |
2872 		    CTIO7_FLAGS_CONFORM_REQ);
2873 	}
2874 	ctio->u.status0.residual = cpu_to_le32(prm->residual);
2875 	ctio->u.status0.scsi_status = cpu_to_le16(prm->rq_result);
2876 	if (QLA_TGT_SENSE_VALID(prm->sense_buffer)) {
2877 		int i;
2878 
2879 		if (qlt_need_explicit_conf(prm->cmd, 1)) {
2880 			if ((prm->rq_result & SS_SCSI_STATUS_BYTE) != 0) {
2881 				ql_dbg_qp(ql_dbg_tgt, prm->cmd->qpair, 0xe017,
2882 				    "Skipping EXPLICIT_CONFORM and "
2883 				    "CTIO7_FLAGS_CONFORM_REQ for FCP READ w/ "
2884 				    "non GOOD status\n");
2885 				goto skip_explict_conf;
2886 			}
2887 			ctio->u.status1.flags |= cpu_to_le16(
2888 			    CTIO7_FLAGS_EXPLICIT_CONFORM |
2889 			    CTIO7_FLAGS_CONFORM_REQ);
2890 		}
2891 skip_explict_conf:
2892 		ctio->u.status1.flags &=
2893 		    ~cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_0);
2894 		ctio->u.status1.flags |=
2895 		    cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1);
2896 		ctio->u.status1.scsi_status |=
2897 		    cpu_to_le16(SS_SENSE_LEN_VALID);
2898 		ctio->u.status1.sense_length =
2899 		    cpu_to_le16(prm->sense_buffer_len);
2900 		for (i = 0; i < prm->sense_buffer_len/4; i++) {
2901 			uint32_t v;
2902 
2903 			v = get_unaligned_be32(
2904 					&((uint32_t *)prm->sense_buffer)[i]);
2905 			put_unaligned_le32(v,
2906 				&((uint32_t *)ctio->u.status1.sense_data)[i]);
2907 		}
2908 		qlt_print_dif_err(prm);
2909 
2910 	} else {
2911 		ctio->u.status1.flags &=
2912 		    ~cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_0);
2913 		ctio->u.status1.flags |=
2914 		    cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1);
2915 		ctio->u.status1.sense_length = 0;
2916 		memset(ctio->u.status1.sense_data, 0,
2917 		    sizeof(ctio->u.status1.sense_data));
2918 	}
2919 
2920 	/* Sense with len > 24, is it possible ??? */
2921 }
2922 
2923 static inline int
2924 qlt_hba_err_chk_enabled(struct se_cmd *se_cmd)
2925 {
2926 	switch (se_cmd->prot_op) {
2927 	case TARGET_PROT_DOUT_INSERT:
2928 	case TARGET_PROT_DIN_STRIP:
2929 		if (ql2xenablehba_err_chk >= 1)
2930 			return 1;
2931 		break;
2932 	case TARGET_PROT_DOUT_PASS:
2933 	case TARGET_PROT_DIN_PASS:
2934 		if (ql2xenablehba_err_chk >= 2)
2935 			return 1;
2936 		break;
2937 	case TARGET_PROT_DIN_INSERT:
2938 	case TARGET_PROT_DOUT_STRIP:
2939 		return 1;
2940 	default:
2941 		break;
2942 	}
2943 	return 0;
2944 }
2945 
2946 static inline int
2947 qla_tgt_ref_mask_check(struct se_cmd *se_cmd)
2948 {
2949 	switch (se_cmd->prot_op) {
2950 	case TARGET_PROT_DIN_INSERT:
2951 	case TARGET_PROT_DOUT_INSERT:
2952 	case TARGET_PROT_DIN_STRIP:
2953 	case TARGET_PROT_DOUT_STRIP:
2954 	case TARGET_PROT_DIN_PASS:
2955 	case TARGET_PROT_DOUT_PASS:
2956 	    return 1;
2957 	default:
2958 	    return 0;
2959 	}
2960 	return 0;
2961 }
2962 
2963 /*
2964  * qla_tgt_set_dif_tags - Extract Ref and App tags from SCSI command
2965  */
2966 static void
2967 qla_tgt_set_dif_tags(struct qla_tgt_cmd *cmd, struct crc_context *ctx,
2968     uint16_t *pfw_prot_opts)
2969 {
2970 	struct se_cmd *se_cmd = &cmd->se_cmd;
2971 	uint32_t lba = 0xffffffff & se_cmd->t_task_lba;
2972 	scsi_qla_host_t *vha = cmd->tgt->vha;
2973 	struct qla_hw_data *ha = vha->hw;
2974 	uint32_t t32 = 0;
2975 
2976 	/*
2977 	 * wait till Mode Sense/Select cmd, modepage Ah, subpage 2
2978 	 * have been immplemented by TCM, before AppTag is avail.
2979 	 * Look for modesense_handlers[]
2980 	 */
2981 	ctx->app_tag = 0;
2982 	ctx->app_tag_mask[0] = 0x0;
2983 	ctx->app_tag_mask[1] = 0x0;
2984 
2985 	if (IS_PI_UNINIT_CAPABLE(ha)) {
2986 		if ((se_cmd->prot_type == TARGET_DIF_TYPE1_PROT) ||
2987 		    (se_cmd->prot_type == TARGET_DIF_TYPE2_PROT))
2988 			*pfw_prot_opts |= PO_DIS_VALD_APP_ESC;
2989 		else if (se_cmd->prot_type == TARGET_DIF_TYPE3_PROT)
2990 			*pfw_prot_opts |= PO_DIS_VALD_APP_REF_ESC;
2991 	}
2992 
2993 	t32 = ha->tgt.tgt_ops->get_dif_tags(cmd, pfw_prot_opts);
2994 
2995 	switch (se_cmd->prot_type) {
2996 	case TARGET_DIF_TYPE0_PROT:
2997 		/*
2998 		 * No check for ql2xenablehba_err_chk, as it
2999 		 * would be an I/O error if hba tag generation
3000 		 * is not done.
3001 		 */
3002 		ctx->ref_tag = cpu_to_le32(lba);
3003 		/* enable ALL bytes of the ref tag */
3004 		ctx->ref_tag_mask[0] = 0xff;
3005 		ctx->ref_tag_mask[1] = 0xff;
3006 		ctx->ref_tag_mask[2] = 0xff;
3007 		ctx->ref_tag_mask[3] = 0xff;
3008 		break;
3009 	case TARGET_DIF_TYPE1_PROT:
3010 	    /*
3011 	     * For TYPE 1 protection: 16 bit GUARD tag, 32 bit
3012 	     * REF tag, and 16 bit app tag.
3013 	     */
3014 	    ctx->ref_tag = cpu_to_le32(lba);
3015 	    if (!qla_tgt_ref_mask_check(se_cmd) ||
3016 		!(ha->tgt.tgt_ops->chk_dif_tags(t32))) {
3017 		    *pfw_prot_opts |= PO_DIS_REF_TAG_VALD;
3018 		    break;
3019 	    }
3020 	    /* enable ALL bytes of the ref tag */
3021 	    ctx->ref_tag_mask[0] = 0xff;
3022 	    ctx->ref_tag_mask[1] = 0xff;
3023 	    ctx->ref_tag_mask[2] = 0xff;
3024 	    ctx->ref_tag_mask[3] = 0xff;
3025 	    break;
3026 	case TARGET_DIF_TYPE2_PROT:
3027 	    /*
3028 	     * For TYPE 2 protection: 16 bit GUARD + 32 bit REF
3029 	     * tag has to match LBA in CDB + N
3030 	     */
3031 	    ctx->ref_tag = cpu_to_le32(lba);
3032 	    if (!qla_tgt_ref_mask_check(se_cmd) ||
3033 		!(ha->tgt.tgt_ops->chk_dif_tags(t32))) {
3034 		    *pfw_prot_opts |= PO_DIS_REF_TAG_VALD;
3035 		    break;
3036 	    }
3037 	    /* enable ALL bytes of the ref tag */
3038 	    ctx->ref_tag_mask[0] = 0xff;
3039 	    ctx->ref_tag_mask[1] = 0xff;
3040 	    ctx->ref_tag_mask[2] = 0xff;
3041 	    ctx->ref_tag_mask[3] = 0xff;
3042 	    break;
3043 	case TARGET_DIF_TYPE3_PROT:
3044 	    /* For TYPE 3 protection: 16 bit GUARD only */
3045 	    *pfw_prot_opts |= PO_DIS_REF_TAG_VALD;
3046 	    ctx->ref_tag_mask[0] = ctx->ref_tag_mask[1] =
3047 		ctx->ref_tag_mask[2] = ctx->ref_tag_mask[3] = 0x00;
3048 	    break;
3049 	}
3050 }
3051 
3052 static inline int
3053 qlt_build_ctio_crc2_pkt(struct qla_qpair *qpair, struct qla_tgt_prm *prm)
3054 {
3055 	struct dsd64		*cur_dsd;
3056 	uint32_t		transfer_length = 0;
3057 	uint32_t		data_bytes;
3058 	uint32_t		dif_bytes;
3059 	uint8_t			bundling = 1;
3060 	struct crc_context	*crc_ctx_pkt = NULL;
3061 	struct qla_hw_data	*ha;
3062 	struct ctio_crc2_to_fw	*pkt;
3063 	dma_addr_t		crc_ctx_dma;
3064 	uint16_t		fw_prot_opts = 0;
3065 	struct qla_tgt_cmd	*cmd = prm->cmd;
3066 	struct se_cmd		*se_cmd = &cmd->se_cmd;
3067 	uint32_t h;
3068 	struct atio_from_isp *atio = &prm->cmd->atio;
3069 	struct qla_tc_param	tc;
3070 	uint16_t t16;
3071 	scsi_qla_host_t *vha = cmd->vha;
3072 
3073 	ha = vha->hw;
3074 
3075 	pkt = (struct ctio_crc2_to_fw *)qpair->req->ring_ptr;
3076 	prm->pkt = pkt;
3077 	memset(pkt, 0, sizeof(*pkt));
3078 
3079 	ql_dbg_qp(ql_dbg_tgt, cmd->qpair, 0xe071,
3080 		"qla_target(%d):%s: se_cmd[%p] CRC2 prot_op[0x%x] cmd prot sg:cnt[%p:%x] lba[%llu]\n",
3081 		cmd->vp_idx, __func__, se_cmd, se_cmd->prot_op,
3082 		prm->prot_sg, prm->prot_seg_cnt, se_cmd->t_task_lba);
3083 
3084 	if ((se_cmd->prot_op == TARGET_PROT_DIN_INSERT) ||
3085 	    (se_cmd->prot_op == TARGET_PROT_DOUT_STRIP))
3086 		bundling = 0;
3087 
3088 	/* Compute dif len and adjust data len to incude protection */
3089 	data_bytes = cmd->bufflen;
3090 	dif_bytes  = (data_bytes / cmd->blk_sz) * 8;
3091 
3092 	switch (se_cmd->prot_op) {
3093 	case TARGET_PROT_DIN_INSERT:
3094 	case TARGET_PROT_DOUT_STRIP:
3095 		transfer_length = data_bytes;
3096 		if (cmd->prot_sg_cnt)
3097 			data_bytes += dif_bytes;
3098 		break;
3099 	case TARGET_PROT_DIN_STRIP:
3100 	case TARGET_PROT_DOUT_INSERT:
3101 	case TARGET_PROT_DIN_PASS:
3102 	case TARGET_PROT_DOUT_PASS:
3103 		transfer_length = data_bytes + dif_bytes;
3104 		break;
3105 	default:
3106 		BUG();
3107 		break;
3108 	}
3109 
3110 	if (!qlt_hba_err_chk_enabled(se_cmd))
3111 		fw_prot_opts |= 0x10; /* Disable Guard tag checking */
3112 	/* HBA error checking enabled */
3113 	else if (IS_PI_UNINIT_CAPABLE(ha)) {
3114 		if ((se_cmd->prot_type == TARGET_DIF_TYPE1_PROT) ||
3115 		    (se_cmd->prot_type == TARGET_DIF_TYPE2_PROT))
3116 			fw_prot_opts |= PO_DIS_VALD_APP_ESC;
3117 		else if (se_cmd->prot_type == TARGET_DIF_TYPE3_PROT)
3118 			fw_prot_opts |= PO_DIS_VALD_APP_REF_ESC;
3119 	}
3120 
3121 	switch (se_cmd->prot_op) {
3122 	case TARGET_PROT_DIN_INSERT:
3123 	case TARGET_PROT_DOUT_INSERT:
3124 		fw_prot_opts |= PO_MODE_DIF_INSERT;
3125 		break;
3126 	case TARGET_PROT_DIN_STRIP:
3127 	case TARGET_PROT_DOUT_STRIP:
3128 		fw_prot_opts |= PO_MODE_DIF_REMOVE;
3129 		break;
3130 	case TARGET_PROT_DIN_PASS:
3131 	case TARGET_PROT_DOUT_PASS:
3132 		fw_prot_opts |= PO_MODE_DIF_PASS;
3133 		/* FUTURE: does tcm require T10CRC<->IPCKSUM conversion? */
3134 		break;
3135 	default:/* Normal Request */
3136 		fw_prot_opts |= PO_MODE_DIF_PASS;
3137 		break;
3138 	}
3139 
3140 	/* ---- PKT ---- */
3141 	/* Update entry type to indicate Command Type CRC_2 IOCB */
3142 	pkt->entry_type  = CTIO_CRC2;
3143 	pkt->entry_count = 1;
3144 	pkt->vp_index = cmd->vp_idx;
3145 
3146 	h = qlt_make_handle(qpair);
3147 	if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
3148 		/*
3149 		 * CTIO type 7 from the firmware doesn't provide a way to
3150 		 * know the initiator's LOOP ID, hence we can't find
3151 		 * the session and, so, the command.
3152 		 */
3153 		return -EAGAIN;
3154 	} else
3155 		qpair->req->outstanding_cmds[h] = (srb_t *)prm->cmd;
3156 
3157 	pkt->handle  = make_handle(qpair->req->id, h);
3158 	pkt->handle |= CTIO_COMPLETION_HANDLE_MARK;
3159 	pkt->nport_handle = cpu_to_le16(prm->cmd->loop_id);
3160 	pkt->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
3161 	pkt->initiator_id = be_id_to_le(atio->u.isp24.fcp_hdr.s_id);
3162 	pkt->exchange_addr   = atio->u.isp24.exchange_addr;
3163 
3164 	/* silence compile warning */
3165 	t16 = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
3166 	pkt->ox_id  = cpu_to_le16(t16);
3167 
3168 	t16 = (atio->u.isp24.attr << 9);
3169 	pkt->flags |= cpu_to_le16(t16);
3170 	pkt->relative_offset = cpu_to_le32(prm->cmd->offset);
3171 
3172 	/* Set transfer direction */
3173 	if (cmd->dma_data_direction == DMA_TO_DEVICE)
3174 		pkt->flags = cpu_to_le16(CTIO7_FLAGS_DATA_IN);
3175 	else if (cmd->dma_data_direction == DMA_FROM_DEVICE)
3176 		pkt->flags = cpu_to_le16(CTIO7_FLAGS_DATA_OUT);
3177 
3178 	pkt->dseg_count = cpu_to_le16(prm->tot_dsds);
3179 	/* Fibre channel byte count */
3180 	pkt->transfer_length = cpu_to_le32(transfer_length);
3181 
3182 	/* ----- CRC context -------- */
3183 
3184 	/* Allocate CRC context from global pool */
3185 	crc_ctx_pkt = cmd->ctx =
3186 	    dma_pool_zalloc(ha->dl_dma_pool, GFP_ATOMIC, &crc_ctx_dma);
3187 
3188 	if (!crc_ctx_pkt)
3189 		goto crc_queuing_error;
3190 
3191 	crc_ctx_pkt->crc_ctx_dma = crc_ctx_dma;
3192 	INIT_LIST_HEAD(&crc_ctx_pkt->dsd_list);
3193 
3194 	/* Set handle */
3195 	crc_ctx_pkt->handle = pkt->handle;
3196 
3197 	qla_tgt_set_dif_tags(cmd, crc_ctx_pkt, &fw_prot_opts);
3198 
3199 	put_unaligned_le64(crc_ctx_dma, &pkt->crc_context_address);
3200 	pkt->crc_context_len = cpu_to_le16(CRC_CONTEXT_LEN_FW);
3201 
3202 	if (!bundling) {
3203 		cur_dsd = &crc_ctx_pkt->u.nobundling.data_dsd[0];
3204 	} else {
3205 		/*
3206 		 * Configure Bundling if we need to fetch interlaving
3207 		 * protection PCI accesses
3208 		 */
3209 		fw_prot_opts |= PO_ENABLE_DIF_BUNDLING;
3210 		crc_ctx_pkt->u.bundling.dif_byte_count = cpu_to_le32(dif_bytes);
3211 		crc_ctx_pkt->u.bundling.dseg_count =
3212 			cpu_to_le16(prm->tot_dsds - prm->prot_seg_cnt);
3213 		cur_dsd = &crc_ctx_pkt->u.bundling.data_dsd[0];
3214 	}
3215 
3216 	/* Finish the common fields of CRC pkt */
3217 	crc_ctx_pkt->blk_size   = cpu_to_le16(cmd->blk_sz);
3218 	crc_ctx_pkt->prot_opts  = cpu_to_le16(fw_prot_opts);
3219 	crc_ctx_pkt->byte_count = cpu_to_le32(data_bytes);
3220 	crc_ctx_pkt->guard_seed = cpu_to_le16(0);
3221 
3222 	memset((uint8_t *)&tc, 0 , sizeof(tc));
3223 	tc.vha = vha;
3224 	tc.blk_sz = cmd->blk_sz;
3225 	tc.bufflen = cmd->bufflen;
3226 	tc.sg = cmd->sg;
3227 	tc.prot_sg = cmd->prot_sg;
3228 	tc.ctx = crc_ctx_pkt;
3229 	tc.ctx_dsd_alloced = &cmd->ctx_dsd_alloced;
3230 
3231 	/* Walks data segments */
3232 	pkt->flags |= cpu_to_le16(CTIO7_FLAGS_DSD_PTR);
3233 
3234 	if (!bundling && prm->prot_seg_cnt) {
3235 		if (qla24xx_walk_and_build_sglist_no_difb(ha, NULL, cur_dsd,
3236 			prm->tot_dsds, &tc))
3237 			goto crc_queuing_error;
3238 	} else if (qla24xx_walk_and_build_sglist(ha, NULL, cur_dsd,
3239 		(prm->tot_dsds - prm->prot_seg_cnt), &tc))
3240 		goto crc_queuing_error;
3241 
3242 	if (bundling && prm->prot_seg_cnt) {
3243 		/* Walks dif segments */
3244 		pkt->add_flags |= CTIO_CRC2_AF_DIF_DSD_ENA;
3245 
3246 		cur_dsd = &crc_ctx_pkt->u.bundling.dif_dsd;
3247 		if (qla24xx_walk_and_build_prot_sglist(ha, NULL, cur_dsd,
3248 			prm->prot_seg_cnt, cmd))
3249 			goto crc_queuing_error;
3250 	}
3251 	return QLA_SUCCESS;
3252 
3253 crc_queuing_error:
3254 	/* Cleanup will be performed by the caller */
3255 	qpair->req->outstanding_cmds[h] = NULL;
3256 
3257 	return QLA_FUNCTION_FAILED;
3258 }
3259 
3260 /*
3261  * Callback to setup response of xmit_type of QLA_TGT_XMIT_DATA and *
3262  * QLA_TGT_XMIT_STATUS for >= 24xx silicon
3263  */
3264 int qlt_xmit_response(struct qla_tgt_cmd *cmd, int xmit_type,
3265 	uint8_t scsi_status)
3266 {
3267 	struct scsi_qla_host *vha = cmd->vha;
3268 	struct qla_qpair *qpair = cmd->qpair;
3269 	struct ctio7_to_24xx *pkt;
3270 	struct qla_tgt_prm prm;
3271 	uint32_t full_req_cnt = 0;
3272 	unsigned long flags = 0;
3273 	int res;
3274 
3275 	if (!qpair->fw_started || (cmd->reset_count != qpair->chip_reset) ||
3276 	    (cmd->sess && cmd->sess->deleted)) {
3277 		cmd->state = QLA_TGT_STATE_PROCESSED;
3278 		return 0;
3279 	}
3280 
3281 	ql_dbg_qp(ql_dbg_tgt, qpair, 0xe018,
3282 	    "is_send_status=%d, cmd->bufflen=%d, cmd->sg_cnt=%d, cmd->dma_data_direction=%d se_cmd[%p] qp %d\n",
3283 	    (xmit_type & QLA_TGT_XMIT_STATUS) ?
3284 	    1 : 0, cmd->bufflen, cmd->sg_cnt, cmd->dma_data_direction,
3285 	    &cmd->se_cmd, qpair->id);
3286 
3287 	res = qlt_pre_xmit_response(cmd, &prm, xmit_type, scsi_status,
3288 	    &full_req_cnt);
3289 	if (unlikely(res != 0)) {
3290 		return res;
3291 	}
3292 
3293 	spin_lock_irqsave(qpair->qp_lock_ptr, flags);
3294 
3295 	if (xmit_type == QLA_TGT_XMIT_STATUS)
3296 		qpair->tgt_counters.core_qla_snd_status++;
3297 	else
3298 		qpair->tgt_counters.core_qla_que_buf++;
3299 
3300 	if (!qpair->fw_started || cmd->reset_count != qpair->chip_reset) {
3301 		/*
3302 		 * Either the port is not online or this request was from
3303 		 * previous life, just abort the processing.
3304 		 */
3305 		cmd->state = QLA_TGT_STATE_PROCESSED;
3306 		ql_dbg_qp(ql_dbg_async, qpair, 0xe101,
3307 			"RESET-RSP online/active/old-count/new-count = %d/%d/%d/%d.\n",
3308 			vha->flags.online, qla2x00_reset_active(vha),
3309 			cmd->reset_count, qpair->chip_reset);
3310 		res = 0;
3311 		goto out_unmap_unlock;
3312 	}
3313 
3314 	/* Does F/W have an IOCBs for this request */
3315 	res = qlt_check_reserve_free_req(qpair, full_req_cnt);
3316 	if (unlikely(res))
3317 		goto out_unmap_unlock;
3318 
3319 	if (cmd->se_cmd.prot_op && (xmit_type & QLA_TGT_XMIT_DATA))
3320 		res = qlt_build_ctio_crc2_pkt(qpair, &prm);
3321 	else
3322 		res = qlt_24xx_build_ctio_pkt(qpair, &prm);
3323 	if (unlikely(res != 0)) {
3324 		qpair->req->cnt += full_req_cnt;
3325 		goto out_unmap_unlock;
3326 	}
3327 
3328 	pkt = (struct ctio7_to_24xx *)prm.pkt;
3329 
3330 	if (qlt_has_data(cmd) && (xmit_type & QLA_TGT_XMIT_DATA)) {
3331 		pkt->u.status0.flags |=
3332 		    cpu_to_le16(CTIO7_FLAGS_DATA_IN |
3333 			CTIO7_FLAGS_STATUS_MODE_0);
3334 
3335 		if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL)
3336 			qlt_load_data_segments(&prm);
3337 
3338 		if (prm.add_status_pkt == 0) {
3339 			if (xmit_type & QLA_TGT_XMIT_STATUS) {
3340 				pkt->u.status0.scsi_status =
3341 				    cpu_to_le16(prm.rq_result);
3342 				if (!cmd->edif)
3343 					pkt->u.status0.residual =
3344 						cpu_to_le32(prm.residual);
3345 
3346 				pkt->u.status0.flags |= cpu_to_le16(
3347 				    CTIO7_FLAGS_SEND_STATUS);
3348 				if (qlt_need_explicit_conf(cmd, 0)) {
3349 					pkt->u.status0.flags |=
3350 					    cpu_to_le16(
3351 						CTIO7_FLAGS_EXPLICIT_CONFORM |
3352 						CTIO7_FLAGS_CONFORM_REQ);
3353 				}
3354 			}
3355 
3356 		} else {
3357 			/*
3358 			 * We have already made sure that there is sufficient
3359 			 * amount of request entries to not drop HW lock in
3360 			 * req_pkt().
3361 			 */
3362 			struct ctio7_to_24xx *ctio =
3363 				(struct ctio7_to_24xx *)qlt_get_req_pkt(
3364 				    qpair->req);
3365 
3366 			ql_dbg_qp(ql_dbg_tgt, qpair, 0x305e,
3367 			    "Building additional status packet 0x%p.\n",
3368 			    ctio);
3369 
3370 			/*
3371 			 * T10Dif: ctio_crc2_to_fw overlay ontop of
3372 			 * ctio7_to_24xx
3373 			 */
3374 			memcpy(ctio, pkt, sizeof(*ctio));
3375 			/* reset back to CTIO7 */
3376 			ctio->entry_count = 1;
3377 			ctio->entry_type = CTIO_TYPE7;
3378 			ctio->dseg_count = 0;
3379 			ctio->u.status1.flags &= ~cpu_to_le16(
3380 			    CTIO7_FLAGS_DATA_IN);
3381 
3382 			/* Real finish is ctio_m1's finish */
3383 			pkt->handle |= CTIO_INTERMEDIATE_HANDLE_MARK;
3384 			pkt->u.status0.flags |= cpu_to_le16(
3385 			    CTIO7_FLAGS_DONT_RET_CTIO);
3386 
3387 			/* qlt_24xx_init_ctio_to_isp will correct
3388 			 * all neccessary fields that's part of CTIO7.
3389 			 * There should be no residual of CTIO-CRC2 data.
3390 			 */
3391 			qlt_24xx_init_ctio_to_isp((struct ctio7_to_24xx *)ctio,
3392 			    &prm);
3393 		}
3394 	} else
3395 		qlt_24xx_init_ctio_to_isp(pkt, &prm);
3396 
3397 
3398 	cmd->state = QLA_TGT_STATE_PROCESSED; /* Mid-level is done processing */
3399 	cmd->cmd_sent_to_fw = 1;
3400 	cmd->ctio_flags = le16_to_cpu(pkt->u.status0.flags);
3401 
3402 	/* Memory Barrier */
3403 	wmb();
3404 	if (qpair->reqq_start_iocbs)
3405 		qpair->reqq_start_iocbs(qpair);
3406 	else
3407 		qla2x00_start_iocbs(vha, qpair->req);
3408 	spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3409 
3410 	return 0;
3411 
3412 out_unmap_unlock:
3413 	qlt_unmap_sg(vha, cmd);
3414 	spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3415 
3416 	return res;
3417 }
3418 EXPORT_SYMBOL(qlt_xmit_response);
3419 
3420 int qlt_rdy_to_xfer(struct qla_tgt_cmd *cmd)
3421 {
3422 	struct ctio7_to_24xx *pkt;
3423 	struct scsi_qla_host *vha = cmd->vha;
3424 	struct qla_tgt *tgt = cmd->tgt;
3425 	struct qla_tgt_prm prm;
3426 	unsigned long flags = 0;
3427 	int res = 0;
3428 	struct qla_qpair *qpair = cmd->qpair;
3429 
3430 	memset(&prm, 0, sizeof(prm));
3431 	prm.cmd = cmd;
3432 	prm.tgt = tgt;
3433 	prm.sg = NULL;
3434 	prm.req_cnt = 1;
3435 
3436 	if (!qpair->fw_started || (cmd->reset_count != qpair->chip_reset) ||
3437 	    (cmd->sess && cmd->sess->deleted)) {
3438 		/*
3439 		 * Either the port is not online or this request was from
3440 		 * previous life, just abort the processing.
3441 		 */
3442 		cmd->aborted = 1;
3443 		cmd->write_data_transferred = 0;
3444 		cmd->state = QLA_TGT_STATE_DATA_IN;
3445 		vha->hw->tgt.tgt_ops->handle_data(cmd);
3446 		ql_dbg_qp(ql_dbg_async, qpair, 0xe102,
3447 			"RESET-XFR online/active/old-count/new-count = %d/%d/%d/%d.\n",
3448 			vha->flags.online, qla2x00_reset_active(vha),
3449 			cmd->reset_count, qpair->chip_reset);
3450 		return 0;
3451 	}
3452 
3453 	/* Calculate number of entries and segments required */
3454 	if (qlt_pci_map_calc_cnt(&prm) != 0)
3455 		return -EAGAIN;
3456 
3457 	spin_lock_irqsave(qpair->qp_lock_ptr, flags);
3458 	/* Does F/W have an IOCBs for this request */
3459 	res = qlt_check_reserve_free_req(qpair, prm.req_cnt);
3460 	if (res != 0)
3461 		goto out_unlock_free_unmap;
3462 	if (cmd->se_cmd.prot_op)
3463 		res = qlt_build_ctio_crc2_pkt(qpair, &prm);
3464 	else
3465 		res = qlt_24xx_build_ctio_pkt(qpair, &prm);
3466 
3467 	if (unlikely(res != 0)) {
3468 		qpair->req->cnt += prm.req_cnt;
3469 		goto out_unlock_free_unmap;
3470 	}
3471 
3472 	pkt = (struct ctio7_to_24xx *)prm.pkt;
3473 	pkt->u.status0.flags |= cpu_to_le16(CTIO7_FLAGS_DATA_OUT |
3474 	    CTIO7_FLAGS_STATUS_MODE_0);
3475 
3476 	if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL)
3477 		qlt_load_data_segments(&prm);
3478 
3479 	cmd->state = QLA_TGT_STATE_NEED_DATA;
3480 	cmd->cmd_sent_to_fw = 1;
3481 	cmd->ctio_flags = le16_to_cpu(pkt->u.status0.flags);
3482 
3483 	/* Memory Barrier */
3484 	wmb();
3485 	if (qpair->reqq_start_iocbs)
3486 		qpair->reqq_start_iocbs(qpair);
3487 	else
3488 		qla2x00_start_iocbs(vha, qpair->req);
3489 	spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3490 
3491 	return res;
3492 
3493 out_unlock_free_unmap:
3494 	qlt_unmap_sg(vha, cmd);
3495 	spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3496 
3497 	return res;
3498 }
3499 EXPORT_SYMBOL(qlt_rdy_to_xfer);
3500 
3501 
3502 /*
3503  * it is assumed either hardware_lock or qpair lock is held.
3504  */
3505 static void
3506 qlt_handle_dif_error(struct qla_qpair *qpair, struct qla_tgt_cmd *cmd,
3507 	struct ctio_crc_from_fw *sts)
3508 {
3509 	uint8_t		*ap = &sts->actual_dif[0];
3510 	uint8_t		*ep = &sts->expected_dif[0];
3511 	uint64_t	lba = cmd->se_cmd.t_task_lba;
3512 	uint8_t scsi_status, sense_key, asc, ascq;
3513 	unsigned long flags;
3514 	struct scsi_qla_host *vha = cmd->vha;
3515 
3516 	cmd->trc_flags |= TRC_DIF_ERR;
3517 
3518 	cmd->a_guard   = get_unaligned_be16(ap + 0);
3519 	cmd->a_app_tag = get_unaligned_be16(ap + 2);
3520 	cmd->a_ref_tag = get_unaligned_be32(ap + 4);
3521 
3522 	cmd->e_guard   = get_unaligned_be16(ep + 0);
3523 	cmd->e_app_tag = get_unaligned_be16(ep + 2);
3524 	cmd->e_ref_tag = get_unaligned_be32(ep + 4);
3525 
3526 	ql_dbg(ql_dbg_tgt_dif, vha, 0xf075,
3527 	    "%s: aborted %d state %d\n", __func__, cmd->aborted, cmd->state);
3528 
3529 	scsi_status = sense_key = asc = ascq = 0;
3530 
3531 	/* check appl tag */
3532 	if (cmd->e_app_tag != cmd->a_app_tag) {
3533 		ql_dbg(ql_dbg_tgt_dif, vha, 0xe00d,
3534 		    "App Tag ERR: cdb[%x] lba[%llx %llx] blks[%x] [Actual|Expected] Ref[%x|%x], App[%x|%x], Guard [%x|%x] cmd=%p ox_id[%04x]",
3535 		    cmd->cdb[0], lba, (lba+cmd->num_blks), cmd->num_blks,
3536 		    cmd->a_ref_tag, cmd->e_ref_tag, cmd->a_app_tag,
3537 		    cmd->e_app_tag, cmd->a_guard, cmd->e_guard, cmd,
3538 		    cmd->atio.u.isp24.fcp_hdr.ox_id);
3539 
3540 		cmd->dif_err_code = DIF_ERR_APP;
3541 		scsi_status = SAM_STAT_CHECK_CONDITION;
3542 		sense_key = ABORTED_COMMAND;
3543 		asc = 0x10;
3544 		ascq = 0x2;
3545 	}
3546 
3547 	/* check ref tag */
3548 	if (cmd->e_ref_tag != cmd->a_ref_tag) {
3549 		ql_dbg(ql_dbg_tgt_dif, vha, 0xe00e,
3550 		    "Ref Tag ERR: cdb[%x] lba[%llx %llx] blks[%x] [Actual|Expected] Ref[%x|%x], App[%x|%x], Guard[%x|%x] cmd=%p ox_id[%04x] ",
3551 		    cmd->cdb[0], lba, (lba+cmd->num_blks), cmd->num_blks,
3552 		    cmd->a_ref_tag, cmd->e_ref_tag, cmd->a_app_tag,
3553 		    cmd->e_app_tag, cmd->a_guard, cmd->e_guard, cmd,
3554 		    cmd->atio.u.isp24.fcp_hdr.ox_id);
3555 
3556 		cmd->dif_err_code = DIF_ERR_REF;
3557 		scsi_status = SAM_STAT_CHECK_CONDITION;
3558 		sense_key = ABORTED_COMMAND;
3559 		asc = 0x10;
3560 		ascq = 0x3;
3561 		goto out;
3562 	}
3563 
3564 	/* check guard */
3565 	if (cmd->e_guard != cmd->a_guard) {
3566 		ql_dbg(ql_dbg_tgt_dif, vha, 0xe012,
3567 		    "Guard ERR: cdb[%x] lba[%llx %llx] blks[%x] [Actual|Expected] Ref[%x|%x], App[%x|%x], Guard [%x|%x] cmd=%p ox_id[%04x]",
3568 		    cmd->cdb[0], lba, (lba+cmd->num_blks), cmd->num_blks,
3569 		    cmd->a_ref_tag, cmd->e_ref_tag, cmd->a_app_tag,
3570 		    cmd->e_app_tag, cmd->a_guard, cmd->e_guard, cmd,
3571 		    cmd->atio.u.isp24.fcp_hdr.ox_id);
3572 
3573 		cmd->dif_err_code = DIF_ERR_GRD;
3574 		scsi_status = SAM_STAT_CHECK_CONDITION;
3575 		sense_key = ABORTED_COMMAND;
3576 		asc = 0x10;
3577 		ascq = 0x1;
3578 	}
3579 out:
3580 	switch (cmd->state) {
3581 	case QLA_TGT_STATE_NEED_DATA:
3582 		/* handle_data will load DIF error code  */
3583 		cmd->state = QLA_TGT_STATE_DATA_IN;
3584 		vha->hw->tgt.tgt_ops->handle_data(cmd);
3585 		break;
3586 	default:
3587 		spin_lock_irqsave(&cmd->cmd_lock, flags);
3588 		if (cmd->aborted) {
3589 			spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3590 			vha->hw->tgt.tgt_ops->free_cmd(cmd);
3591 			break;
3592 		}
3593 		spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3594 
3595 		qlt_send_resp_ctio(qpair, cmd, scsi_status, sense_key, asc,
3596 		    ascq);
3597 		/* assume scsi status gets out on the wire.
3598 		 * Will not wait for completion.
3599 		 */
3600 		vha->hw->tgt.tgt_ops->free_cmd(cmd);
3601 		break;
3602 	}
3603 }
3604 
3605 /* If hardware_lock held on entry, might drop it, then reaquire */
3606 /* This function sends the appropriate CTIO to ISP 2xxx or 24xx */
3607 static int __qlt_send_term_imm_notif(struct scsi_qla_host *vha,
3608 	struct imm_ntfy_from_isp *ntfy)
3609 {
3610 	struct nack_to_isp *nack;
3611 	struct qla_hw_data *ha = vha->hw;
3612 	request_t *pkt;
3613 	int ret = 0;
3614 
3615 	ql_dbg(ql_dbg_tgt_tmr, vha, 0xe01c,
3616 	    "Sending TERM ELS CTIO (ha=%p)\n", ha);
3617 
3618 	pkt = (request_t *)qla2x00_alloc_iocbs(vha, NULL);
3619 	if (pkt == NULL) {
3620 		ql_dbg(ql_dbg_tgt, vha, 0xe080,
3621 		    "qla_target(%d): %s failed: unable to allocate "
3622 		    "request packet\n", vha->vp_idx, __func__);
3623 		return -ENOMEM;
3624 	}
3625 
3626 	pkt->entry_type = NOTIFY_ACK_TYPE;
3627 	pkt->entry_count = 1;
3628 	pkt->handle = QLA_TGT_SKIP_HANDLE;
3629 
3630 	nack = (struct nack_to_isp *)pkt;
3631 	nack->ox_id = ntfy->ox_id;
3632 
3633 	nack->u.isp24.nport_handle = ntfy->u.isp24.nport_handle;
3634 	if (le16_to_cpu(ntfy->u.isp24.status) == IMM_NTFY_ELS) {
3635 		nack->u.isp24.flags = ntfy->u.isp24.flags &
3636 			cpu_to_le16(NOTIFY24XX_FLAGS_PUREX_IOCB);
3637 	}
3638 
3639 	/* terminate */
3640 	nack->u.isp24.flags |=
3641 		__constant_cpu_to_le16(NOTIFY_ACK_FLAGS_TERMINATE);
3642 
3643 	nack->u.isp24.srr_rx_id = ntfy->u.isp24.srr_rx_id;
3644 	nack->u.isp24.status = ntfy->u.isp24.status;
3645 	nack->u.isp24.status_subcode = ntfy->u.isp24.status_subcode;
3646 	nack->u.isp24.fw_handle = ntfy->u.isp24.fw_handle;
3647 	nack->u.isp24.exchange_address = ntfy->u.isp24.exchange_address;
3648 	nack->u.isp24.srr_rel_offs = ntfy->u.isp24.srr_rel_offs;
3649 	nack->u.isp24.srr_ui = ntfy->u.isp24.srr_ui;
3650 	nack->u.isp24.vp_index = ntfy->u.isp24.vp_index;
3651 
3652 	qla2x00_start_iocbs(vha, vha->req);
3653 	return ret;
3654 }
3655 
3656 static void qlt_send_term_imm_notif(struct scsi_qla_host *vha,
3657 	struct imm_ntfy_from_isp *imm, int ha_locked)
3658 {
3659 	int rc;
3660 
3661 	WARN_ON_ONCE(!ha_locked);
3662 	rc = __qlt_send_term_imm_notif(vha, imm);
3663 	pr_debug("rc = %d\n", rc);
3664 }
3665 
3666 /*
3667  * If hardware_lock held on entry, might drop it, then reaquire
3668  * This function sends the appropriate CTIO to ISP 2xxx or 24xx
3669  */
3670 static int __qlt_send_term_exchange(struct qla_qpair *qpair,
3671 	struct qla_tgt_cmd *cmd,
3672 	struct atio_from_isp *atio)
3673 {
3674 	struct scsi_qla_host *vha = qpair->vha;
3675 	struct ctio7_to_24xx *ctio24;
3676 	struct qla_hw_data *ha = vha->hw;
3677 	request_t *pkt;
3678 	int ret = 0;
3679 	uint16_t temp;
3680 
3681 	ql_dbg(ql_dbg_tgt, vha, 0xe009, "Sending TERM EXCH CTIO (ha=%p)\n", ha);
3682 
3683 	if (cmd)
3684 		vha = cmd->vha;
3685 
3686 	pkt = (request_t *)qla2x00_alloc_iocbs_ready(qpair, NULL);
3687 	if (pkt == NULL) {
3688 		ql_dbg(ql_dbg_tgt, vha, 0xe050,
3689 		    "qla_target(%d): %s failed: unable to allocate "
3690 		    "request packet\n", vha->vp_idx, __func__);
3691 		return -ENOMEM;
3692 	}
3693 
3694 	if (cmd != NULL) {
3695 		if (cmd->state < QLA_TGT_STATE_PROCESSED) {
3696 			ql_dbg(ql_dbg_tgt, vha, 0xe051,
3697 			    "qla_target(%d): Terminating cmd %p with "
3698 			    "incorrect state %d\n", vha->vp_idx, cmd,
3699 			    cmd->state);
3700 		} else
3701 			ret = 1;
3702 	}
3703 
3704 	qpair->tgt_counters.num_term_xchg_sent++;
3705 	pkt->entry_count = 1;
3706 	pkt->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
3707 
3708 	ctio24 = (struct ctio7_to_24xx *)pkt;
3709 	ctio24->entry_type = CTIO_TYPE7;
3710 	ctio24->nport_handle = cpu_to_le16(CTIO7_NHANDLE_UNRECOGNIZED);
3711 	ctio24->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
3712 	ctio24->vp_index = vha->vp_idx;
3713 	ctio24->initiator_id = be_id_to_le(atio->u.isp24.fcp_hdr.s_id);
3714 	ctio24->exchange_addr = atio->u.isp24.exchange_addr;
3715 	temp = (atio->u.isp24.attr << 9) | CTIO7_FLAGS_STATUS_MODE_1 |
3716 		CTIO7_FLAGS_TERMINATE;
3717 	ctio24->u.status1.flags = cpu_to_le16(temp);
3718 	temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
3719 	ctio24->u.status1.ox_id = cpu_to_le16(temp);
3720 
3721 	/* Memory Barrier */
3722 	wmb();
3723 	if (qpair->reqq_start_iocbs)
3724 		qpair->reqq_start_iocbs(qpair);
3725 	else
3726 		qla2x00_start_iocbs(vha, qpair->req);
3727 	return ret;
3728 }
3729 
3730 static void qlt_send_term_exchange(struct qla_qpair *qpair,
3731 	struct qla_tgt_cmd *cmd, struct atio_from_isp *atio, int ha_locked,
3732 	int ul_abort)
3733 {
3734 	struct scsi_qla_host *vha;
3735 	unsigned long flags = 0;
3736 	int rc;
3737 
3738 	/* why use different vha? NPIV */
3739 	if (cmd)
3740 		vha = cmd->vha;
3741 	else
3742 		vha = qpair->vha;
3743 
3744 	if (ha_locked) {
3745 		rc = __qlt_send_term_exchange(qpair, cmd, atio);
3746 		if (rc == -ENOMEM)
3747 			qlt_alloc_qfull_cmd(vha, atio, 0, 0);
3748 		goto done;
3749 	}
3750 	spin_lock_irqsave(qpair->qp_lock_ptr, flags);
3751 	rc = __qlt_send_term_exchange(qpair, cmd, atio);
3752 	if (rc == -ENOMEM)
3753 		qlt_alloc_qfull_cmd(vha, atio, 0, 0);
3754 
3755 done:
3756 	if (cmd && !ul_abort && !cmd->aborted) {
3757 		if (cmd->sg_mapped)
3758 			qlt_unmap_sg(vha, cmd);
3759 		vha->hw->tgt.tgt_ops->free_cmd(cmd);
3760 	}
3761 
3762 	if (!ha_locked)
3763 		spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3764 
3765 	return;
3766 }
3767 
3768 static void qlt_init_term_exchange(struct scsi_qla_host *vha)
3769 {
3770 	struct list_head free_list;
3771 	struct qla_tgt_cmd *cmd, *tcmd;
3772 
3773 	vha->hw->tgt.leak_exchg_thresh_hold =
3774 	    (vha->hw->cur_fw_xcb_count/100) * LEAK_EXCHG_THRESH_HOLD_PERCENT;
3775 
3776 	cmd = tcmd = NULL;
3777 	if (!list_empty(&vha->hw->tgt.q_full_list)) {
3778 		INIT_LIST_HEAD(&free_list);
3779 		list_splice_init(&vha->hw->tgt.q_full_list, &free_list);
3780 
3781 		list_for_each_entry_safe(cmd, tcmd, &free_list, cmd_list) {
3782 			list_del(&cmd->cmd_list);
3783 			/* This cmd was never sent to TCM.  There is no need
3784 			 * to schedule free or call free_cmd
3785 			 */
3786 			qlt_free_cmd(cmd);
3787 			vha->hw->tgt.num_qfull_cmds_alloc--;
3788 		}
3789 	}
3790 	vha->hw->tgt.num_qfull_cmds_dropped = 0;
3791 }
3792 
3793 static void qlt_chk_exch_leak_thresh_hold(struct scsi_qla_host *vha)
3794 {
3795 	uint32_t total_leaked;
3796 
3797 	total_leaked = vha->hw->tgt.num_qfull_cmds_dropped;
3798 
3799 	if (vha->hw->tgt.leak_exchg_thresh_hold &&
3800 	    (total_leaked > vha->hw->tgt.leak_exchg_thresh_hold)) {
3801 
3802 		ql_dbg(ql_dbg_tgt, vha, 0xe079,
3803 		    "Chip reset due to exchange starvation: %d/%d.\n",
3804 		    total_leaked, vha->hw->cur_fw_xcb_count);
3805 
3806 		if (IS_P3P_TYPE(vha->hw))
3807 			set_bit(FCOE_CTX_RESET_NEEDED, &vha->dpc_flags);
3808 		else
3809 			set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
3810 		qla2xxx_wake_dpc(vha);
3811 	}
3812 
3813 }
3814 
3815 int qlt_abort_cmd(struct qla_tgt_cmd *cmd)
3816 {
3817 	struct qla_tgt *tgt = cmd->tgt;
3818 	struct scsi_qla_host *vha = tgt->vha;
3819 	struct se_cmd *se_cmd = &cmd->se_cmd;
3820 	unsigned long flags;
3821 
3822 	ql_dbg(ql_dbg_tgt_mgt, vha, 0xf014,
3823 	    "qla_target(%d): terminating exchange for aborted cmd=%p "
3824 	    "(se_cmd=%p, tag=%llu)", vha->vp_idx, cmd, &cmd->se_cmd,
3825 	    se_cmd->tag);
3826 
3827 	spin_lock_irqsave(&cmd->cmd_lock, flags);
3828 	if (cmd->aborted) {
3829 		if (cmd->sg_mapped)
3830 			qlt_unmap_sg(vha, cmd);
3831 
3832 		spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3833 		/*
3834 		 * It's normal to see 2 calls in this path:
3835 		 *  1) XFER Rdy completion + CMD_T_ABORT
3836 		 *  2) TCM TMR - drain_state_list
3837 		 */
3838 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf016,
3839 		    "multiple abort. %p transport_state %x, t_state %x, "
3840 		    "se_cmd_flags %x\n", cmd, cmd->se_cmd.transport_state,
3841 		    cmd->se_cmd.t_state, cmd->se_cmd.se_cmd_flags);
3842 		return -EIO;
3843 	}
3844 	cmd->aborted = 1;
3845 	cmd->trc_flags |= TRC_ABORT;
3846 	spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3847 
3848 	qlt_send_term_exchange(cmd->qpair, cmd, &cmd->atio, 0, 1);
3849 	return 0;
3850 }
3851 EXPORT_SYMBOL(qlt_abort_cmd);
3852 
3853 void qlt_free_cmd(struct qla_tgt_cmd *cmd)
3854 {
3855 	struct fc_port *sess = cmd->sess;
3856 
3857 	ql_dbg(ql_dbg_tgt, cmd->vha, 0xe074,
3858 	    "%s: se_cmd[%p] ox_id %04x\n",
3859 	    __func__, &cmd->se_cmd,
3860 	    be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
3861 
3862 	BUG_ON(cmd->cmd_in_wq);
3863 
3864 	if (!cmd->q_full)
3865 		qlt_decr_num_pend_cmds(cmd->vha);
3866 
3867 	BUG_ON(cmd->sg_mapped);
3868 	cmd->jiffies_at_free = get_jiffies_64();
3869 	if (unlikely(cmd->free_sg))
3870 		kfree(cmd->sg);
3871 
3872 	if (!sess || !sess->se_sess) {
3873 		WARN_ON(1);
3874 		return;
3875 	}
3876 	cmd->jiffies_at_free = get_jiffies_64();
3877 	cmd->vha->hw->tgt.tgt_ops->rel_cmd(cmd);
3878 }
3879 EXPORT_SYMBOL(qlt_free_cmd);
3880 
3881 /*
3882  * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
3883  */
3884 static int qlt_term_ctio_exchange(struct qla_qpair *qpair, void *ctio,
3885 	struct qla_tgt_cmd *cmd, uint32_t status)
3886 {
3887 	int term = 0;
3888 	struct scsi_qla_host *vha = qpair->vha;
3889 
3890 	if (cmd->se_cmd.prot_op)
3891 		ql_dbg(ql_dbg_tgt_dif, vha, 0xe013,
3892 		    "Term DIF cmd: lba[0x%llx|%lld] len[0x%x] "
3893 		    "se_cmd=%p tag[%x] op %#x/%s",
3894 		     cmd->lba, cmd->lba,
3895 		     cmd->num_blks, &cmd->se_cmd,
3896 		     cmd->atio.u.isp24.exchange_addr,
3897 		     cmd->se_cmd.prot_op,
3898 		     prot_op_str(cmd->se_cmd.prot_op));
3899 
3900 	if (ctio != NULL) {
3901 		struct ctio7_from_24xx *c = (struct ctio7_from_24xx *)ctio;
3902 
3903 		term = !(c->flags &
3904 		    cpu_to_le16(OF_TERM_EXCH));
3905 	} else
3906 		term = 1;
3907 
3908 	if (term)
3909 		qlt_send_term_exchange(qpair, cmd, &cmd->atio, 1, 0);
3910 
3911 	return term;
3912 }
3913 
3914 
3915 /* ha->hardware_lock supposed to be held on entry */
3916 static void *qlt_ctio_to_cmd(struct scsi_qla_host *vha,
3917 	struct rsp_que *rsp, uint32_t handle, void *ctio)
3918 {
3919 	void *cmd = NULL;
3920 	struct req_que *req;
3921 	int qid = GET_QID(handle);
3922 	uint32_t h = handle & ~QLA_TGT_HANDLE_MASK;
3923 
3924 	if (unlikely(h == QLA_TGT_SKIP_HANDLE))
3925 		return NULL;
3926 
3927 	if (qid == rsp->req->id) {
3928 		req = rsp->req;
3929 	} else if (vha->hw->req_q_map[qid]) {
3930 		ql_dbg(ql_dbg_tgt_mgt, vha, 0x1000a,
3931 		    "qla_target(%d): CTIO completion with different QID %d handle %x\n",
3932 		    vha->vp_idx, rsp->id, handle);
3933 		req = vha->hw->req_q_map[qid];
3934 	} else {
3935 		return NULL;
3936 	}
3937 
3938 	h &= QLA_CMD_HANDLE_MASK;
3939 
3940 	if (h != QLA_TGT_NULL_HANDLE) {
3941 		if (unlikely(h >= req->num_outstanding_cmds)) {
3942 			ql_dbg(ql_dbg_tgt, vha, 0xe052,
3943 			    "qla_target(%d): Wrong handle %x received\n",
3944 			    vha->vp_idx, handle);
3945 			return NULL;
3946 		}
3947 
3948 		cmd = req->outstanding_cmds[h];
3949 		if (unlikely(cmd == NULL)) {
3950 			ql_dbg(ql_dbg_async, vha, 0xe053,
3951 			    "qla_target(%d): Suspicious: unable to find the command with handle %x req->id %d rsp->id %d\n",
3952 				vha->vp_idx, handle, req->id, rsp->id);
3953 			return NULL;
3954 		}
3955 		req->outstanding_cmds[h] = NULL;
3956 	} else if (ctio != NULL) {
3957 		/* We can't get loop ID from CTIO7 */
3958 		ql_dbg(ql_dbg_tgt, vha, 0xe054,
3959 		    "qla_target(%d): Wrong CTIO received: QLA24xx doesn't "
3960 		    "support NULL handles\n", vha->vp_idx);
3961 		return NULL;
3962 	}
3963 
3964 	return cmd;
3965 }
3966 
3967 /*
3968  * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
3969  */
3970 static void qlt_do_ctio_completion(struct scsi_qla_host *vha,
3971     struct rsp_que *rsp, uint32_t handle, uint32_t status, void *ctio)
3972 {
3973 	struct qla_hw_data *ha = vha->hw;
3974 	struct se_cmd *se_cmd;
3975 	struct qla_tgt_cmd *cmd;
3976 	struct qla_qpair *qpair = rsp->qpair;
3977 
3978 	if (handle & CTIO_INTERMEDIATE_HANDLE_MARK) {
3979 		/* That could happen only in case of an error/reset/abort */
3980 		if (status != CTIO_SUCCESS) {
3981 			ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01d,
3982 			    "Intermediate CTIO received"
3983 			    " (status %x)\n", status);
3984 		}
3985 		return;
3986 	}
3987 
3988 	cmd = qlt_ctio_to_cmd(vha, rsp, handle, ctio);
3989 	if (cmd == NULL)
3990 		return;
3991 
3992 	if ((le16_to_cpu(((struct ctio7_from_24xx *)ctio)->flags) & CTIO7_FLAGS_DATA_OUT) &&
3993 	    cmd->sess) {
3994 		qlt_chk_edif_rx_sa_delete_pending(vha, cmd->sess,
3995 		    (struct ctio7_from_24xx *)ctio);
3996 	}
3997 
3998 	se_cmd = &cmd->se_cmd;
3999 	cmd->cmd_sent_to_fw = 0;
4000 
4001 	qlt_unmap_sg(vha, cmd);
4002 
4003 	if (unlikely(status != CTIO_SUCCESS)) {
4004 		switch (status & 0xFFFF) {
4005 		case CTIO_INVALID_RX_ID:
4006 			if (printk_ratelimit())
4007 				dev_info(&vha->hw->pdev->dev,
4008 				    "qla_target(%d): CTIO with INVALID_RX_ID ATIO attr %x CTIO Flags %x|%x\n",
4009 				    vha->vp_idx, cmd->atio.u.isp24.attr,
4010 				    ((cmd->ctio_flags >> 9) & 0xf),
4011 				    cmd->ctio_flags);
4012 
4013 			break;
4014 		case CTIO_LIP_RESET:
4015 		case CTIO_TARGET_RESET:
4016 		case CTIO_ABORTED:
4017 			/* driver request abort via Terminate exchange */
4018 		case CTIO_TIMEOUT:
4019 			/* They are OK */
4020 			ql_dbg(ql_dbg_tgt_mgt, vha, 0xf058,
4021 			    "qla_target(%d): CTIO with "
4022 			    "status %#x received, state %x, se_cmd %p, "
4023 			    "(LIP_RESET=e, ABORTED=2, TARGET_RESET=17, "
4024 			    "TIMEOUT=b, INVALID_RX_ID=8)\n", vha->vp_idx,
4025 			    status, cmd->state, se_cmd);
4026 			break;
4027 
4028 		case CTIO_PORT_LOGGED_OUT:
4029 		case CTIO_PORT_UNAVAILABLE:
4030 		{
4031 			int logged_out =
4032 				(status & 0xFFFF) == CTIO_PORT_LOGGED_OUT;
4033 
4034 			ql_dbg(ql_dbg_tgt_mgt, vha, 0xf059,
4035 			    "qla_target(%d): CTIO with %s status %x "
4036 			    "received (state %x, se_cmd %p)\n", vha->vp_idx,
4037 			    logged_out ? "PORT LOGGED OUT" : "PORT UNAVAILABLE",
4038 			    status, cmd->state, se_cmd);
4039 
4040 			if (logged_out && cmd->sess) {
4041 				/*
4042 				 * Session is already logged out, but we need
4043 				 * to notify initiator, who's not aware of this
4044 				 */
4045 				cmd->sess->send_els_logo = 1;
4046 				ql_dbg(ql_dbg_disc, vha, 0x20f8,
4047 				    "%s %d %8phC post del sess\n",
4048 				    __func__, __LINE__, cmd->sess->port_name);
4049 
4050 				qlt_schedule_sess_for_deletion(cmd->sess);
4051 			}
4052 			break;
4053 		}
4054 		case CTIO_DIF_ERROR: {
4055 			struct ctio_crc_from_fw *crc =
4056 				(struct ctio_crc_from_fw *)ctio;
4057 			ql_dbg(ql_dbg_tgt_mgt, vha, 0xf073,
4058 			    "qla_target(%d): CTIO with DIF_ERROR status %x "
4059 			    "received (state %x, ulp_cmd %p) actual_dif[0x%llx] "
4060 			    "expect_dif[0x%llx]\n",
4061 			    vha->vp_idx, status, cmd->state, se_cmd,
4062 			    *((u64 *)&crc->actual_dif[0]),
4063 			    *((u64 *)&crc->expected_dif[0]));
4064 
4065 			qlt_handle_dif_error(qpair, cmd, ctio);
4066 			return;
4067 		}
4068 
4069 		case CTIO_FAST_AUTH_ERR:
4070 		case CTIO_FAST_INCOMP_PAD_LEN:
4071 		case CTIO_FAST_INVALID_REQ:
4072 		case CTIO_FAST_SPI_ERR:
4073 			ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05b,
4074 			    "qla_target(%d): CTIO with EDIF error status 0x%x received (state %x, se_cmd %p\n",
4075 			    vha->vp_idx, status, cmd->state, se_cmd);
4076 			break;
4077 
4078 		default:
4079 			ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05b,
4080 			    "qla_target(%d): CTIO with error status 0x%x received (state %x, se_cmd %p\n",
4081 			    vha->vp_idx, status, cmd->state, se_cmd);
4082 			break;
4083 		}
4084 
4085 
4086 		/* "cmd->aborted" means
4087 		 * cmd is already aborted/terminated, we don't
4088 		 * need to terminate again.  The exchange is already
4089 		 * cleaned up/freed at FW level.  Just cleanup at driver
4090 		 * level.
4091 		 */
4092 		if ((cmd->state != QLA_TGT_STATE_NEED_DATA) &&
4093 		    (!cmd->aborted)) {
4094 			cmd->trc_flags |= TRC_CTIO_ERR;
4095 			if (qlt_term_ctio_exchange(qpair, ctio, cmd, status))
4096 				return;
4097 		}
4098 	}
4099 
4100 	if (cmd->state == QLA_TGT_STATE_PROCESSED) {
4101 		cmd->trc_flags |= TRC_CTIO_DONE;
4102 	} else if (cmd->state == QLA_TGT_STATE_NEED_DATA) {
4103 		cmd->state = QLA_TGT_STATE_DATA_IN;
4104 
4105 		if (status == CTIO_SUCCESS)
4106 			cmd->write_data_transferred = 1;
4107 
4108 		ha->tgt.tgt_ops->handle_data(cmd);
4109 		return;
4110 	} else if (cmd->aborted) {
4111 		cmd->trc_flags |= TRC_CTIO_ABORTED;
4112 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01e,
4113 		  "Aborted command %p (tag %lld) finished\n", cmd, se_cmd->tag);
4114 	} else {
4115 		cmd->trc_flags |= TRC_CTIO_STRANGE;
4116 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05c,
4117 		    "qla_target(%d): A command in state (%d) should "
4118 		    "not return a CTIO complete\n", vha->vp_idx, cmd->state);
4119 	}
4120 
4121 	if (unlikely(status != CTIO_SUCCESS) &&
4122 		!cmd->aborted) {
4123 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01f, "Finishing failed CTIO\n");
4124 		dump_stack();
4125 	}
4126 
4127 	ha->tgt.tgt_ops->free_cmd(cmd);
4128 }
4129 
4130 static inline int qlt_get_fcp_task_attr(struct scsi_qla_host *vha,
4131 	uint8_t task_codes)
4132 {
4133 	int fcp_task_attr;
4134 
4135 	switch (task_codes) {
4136 	case ATIO_SIMPLE_QUEUE:
4137 		fcp_task_attr = TCM_SIMPLE_TAG;
4138 		break;
4139 	case ATIO_HEAD_OF_QUEUE:
4140 		fcp_task_attr = TCM_HEAD_TAG;
4141 		break;
4142 	case ATIO_ORDERED_QUEUE:
4143 		fcp_task_attr = TCM_ORDERED_TAG;
4144 		break;
4145 	case ATIO_ACA_QUEUE:
4146 		fcp_task_attr = TCM_ACA_TAG;
4147 		break;
4148 	case ATIO_UNTAGGED:
4149 		fcp_task_attr = TCM_SIMPLE_TAG;
4150 		break;
4151 	default:
4152 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05d,
4153 		    "qla_target: unknown task code %x, use ORDERED instead\n",
4154 		    task_codes);
4155 		fcp_task_attr = TCM_ORDERED_TAG;
4156 		break;
4157 	}
4158 
4159 	return fcp_task_attr;
4160 }
4161 
4162 /*
4163  * Process context for I/O path into tcm_qla2xxx code
4164  */
4165 static void __qlt_do_work(struct qla_tgt_cmd *cmd)
4166 {
4167 	scsi_qla_host_t *vha = cmd->vha;
4168 	struct qla_hw_data *ha = vha->hw;
4169 	struct fc_port *sess = cmd->sess;
4170 	struct atio_from_isp *atio = &cmd->atio;
4171 	unsigned char *cdb;
4172 	unsigned long flags;
4173 	uint32_t data_length;
4174 	int ret, fcp_task_attr, data_dir, bidi = 0;
4175 	struct qla_qpair *qpair = cmd->qpair;
4176 
4177 	cmd->cmd_in_wq = 0;
4178 	cmd->trc_flags |= TRC_DO_WORK;
4179 
4180 	if (cmd->aborted) {
4181 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf082,
4182 		    "cmd with tag %u is aborted\n",
4183 		    cmd->atio.u.isp24.exchange_addr);
4184 		goto out_term;
4185 	}
4186 
4187 	spin_lock_init(&cmd->cmd_lock);
4188 	cdb = &atio->u.isp24.fcp_cmnd.cdb[0];
4189 	cmd->se_cmd.tag = le32_to_cpu(atio->u.isp24.exchange_addr);
4190 
4191 	if (atio->u.isp24.fcp_cmnd.rddata &&
4192 	    atio->u.isp24.fcp_cmnd.wrdata) {
4193 		bidi = 1;
4194 		data_dir = DMA_TO_DEVICE;
4195 	} else if (atio->u.isp24.fcp_cmnd.rddata)
4196 		data_dir = DMA_FROM_DEVICE;
4197 	else if (atio->u.isp24.fcp_cmnd.wrdata)
4198 		data_dir = DMA_TO_DEVICE;
4199 	else
4200 		data_dir = DMA_NONE;
4201 
4202 	fcp_task_attr = qlt_get_fcp_task_attr(vha,
4203 	    atio->u.isp24.fcp_cmnd.task_attr);
4204 	data_length = get_datalen_for_atio(atio);
4205 
4206 	ret = ha->tgt.tgt_ops->handle_cmd(vha, cmd, cdb, data_length,
4207 				          fcp_task_attr, data_dir, bidi);
4208 	if (ret != 0)
4209 		goto out_term;
4210 	/*
4211 	 * Drop extra session reference from qlt_handle_cmd_for_atio().
4212 	 */
4213 	ha->tgt.tgt_ops->put_sess(sess);
4214 	return;
4215 
4216 out_term:
4217 	ql_dbg(ql_dbg_io, vha, 0x3060, "Terminating work cmd %p", cmd);
4218 	/*
4219 	 * cmd has not sent to target yet, so pass NULL as the second
4220 	 * argument to qlt_send_term_exchange() and free the memory here.
4221 	 */
4222 	cmd->trc_flags |= TRC_DO_WORK_ERR;
4223 	spin_lock_irqsave(qpair->qp_lock_ptr, flags);
4224 	qlt_send_term_exchange(qpair, NULL, &cmd->atio, 1, 0);
4225 
4226 	qlt_decr_num_pend_cmds(vha);
4227 	cmd->vha->hw->tgt.tgt_ops->rel_cmd(cmd);
4228 	spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
4229 
4230 	ha->tgt.tgt_ops->put_sess(sess);
4231 }
4232 
4233 static void qlt_do_work(struct work_struct *work)
4234 {
4235 	struct qla_tgt_cmd *cmd = container_of(work, struct qla_tgt_cmd, work);
4236 	scsi_qla_host_t *vha = cmd->vha;
4237 	unsigned long flags;
4238 
4239 	spin_lock_irqsave(&vha->cmd_list_lock, flags);
4240 	list_del(&cmd->cmd_list);
4241 	spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
4242 
4243 	__qlt_do_work(cmd);
4244 }
4245 
4246 void qlt_clr_qp_table(struct scsi_qla_host *vha)
4247 {
4248 	unsigned long flags;
4249 	struct qla_hw_data *ha = vha->hw;
4250 	struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4251 	void *node;
4252 	u64 key = 0;
4253 
4254 	ql_log(ql_log_info, vha, 0x706c,
4255 	    "User update Number of Active Qpairs %d\n",
4256 	    ha->tgt.num_act_qpairs);
4257 
4258 	spin_lock_irqsave(&ha->tgt.atio_lock, flags);
4259 
4260 	btree_for_each_safe64(&tgt->lun_qpair_map, key, node)
4261 		btree_remove64(&tgt->lun_qpair_map, key);
4262 
4263 	ha->base_qpair->lun_cnt = 0;
4264 	for (key = 0; key < ha->max_qpairs; key++)
4265 		if (ha->queue_pair_map[key])
4266 			ha->queue_pair_map[key]->lun_cnt = 0;
4267 
4268 	spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
4269 }
4270 
4271 static void qlt_assign_qpair(struct scsi_qla_host *vha,
4272 	struct qla_tgt_cmd *cmd)
4273 {
4274 	struct qla_qpair *qpair, *qp;
4275 	struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4276 	struct qla_qpair_hint *h;
4277 
4278 	if (vha->flags.qpairs_available) {
4279 		h = btree_lookup64(&tgt->lun_qpair_map, cmd->unpacked_lun);
4280 		if (unlikely(!h)) {
4281 			/* spread lun to qpair ratio evently */
4282 			int lcnt = 0, rc;
4283 			struct scsi_qla_host *base_vha =
4284 				pci_get_drvdata(vha->hw->pdev);
4285 
4286 			qpair = vha->hw->base_qpair;
4287 			if (qpair->lun_cnt == 0) {
4288 				qpair->lun_cnt++;
4289 				h = qla_qpair_to_hint(tgt, qpair);
4290 				BUG_ON(!h);
4291 				rc = btree_insert64(&tgt->lun_qpair_map,
4292 					cmd->unpacked_lun, h, GFP_ATOMIC);
4293 				if (rc) {
4294 					qpair->lun_cnt--;
4295 					ql_log(ql_log_info, vha, 0xd037,
4296 					    "Unable to insert lun %llx into lun_qpair_map\n",
4297 					    cmd->unpacked_lun);
4298 				}
4299 				goto out;
4300 			} else {
4301 				lcnt = qpair->lun_cnt;
4302 			}
4303 
4304 			h = NULL;
4305 			list_for_each_entry(qp, &base_vha->qp_list,
4306 			    qp_list_elem) {
4307 				if (qp->lun_cnt == 0) {
4308 					qp->lun_cnt++;
4309 					h = qla_qpair_to_hint(tgt, qp);
4310 					BUG_ON(!h);
4311 					rc = btree_insert64(&tgt->lun_qpair_map,
4312 					    cmd->unpacked_lun, h, GFP_ATOMIC);
4313 					if (rc) {
4314 						qp->lun_cnt--;
4315 						ql_log(ql_log_info, vha, 0xd038,
4316 							"Unable to insert lun %llx into lun_qpair_map\n",
4317 							cmd->unpacked_lun);
4318 					}
4319 					qpair = qp;
4320 					goto out;
4321 				} else {
4322 					if (qp->lun_cnt < lcnt) {
4323 						lcnt = qp->lun_cnt;
4324 						qpair = qp;
4325 						continue;
4326 					}
4327 				}
4328 			}
4329 			BUG_ON(!qpair);
4330 			qpair->lun_cnt++;
4331 			h = qla_qpair_to_hint(tgt, qpair);
4332 			BUG_ON(!h);
4333 			rc = btree_insert64(&tgt->lun_qpair_map,
4334 				cmd->unpacked_lun, h, GFP_ATOMIC);
4335 			if (rc) {
4336 				qpair->lun_cnt--;
4337 				ql_log(ql_log_info, vha, 0xd039,
4338 				   "Unable to insert lun %llx into lun_qpair_map\n",
4339 				   cmd->unpacked_lun);
4340 			}
4341 		}
4342 	} else {
4343 		h = &tgt->qphints[0];
4344 	}
4345 out:
4346 	cmd->qpair = h->qpair;
4347 	cmd->se_cmd.cpuid = h->cpuid;
4348 }
4349 
4350 static struct qla_tgt_cmd *qlt_get_tag(scsi_qla_host_t *vha,
4351 				       struct fc_port *sess,
4352 				       struct atio_from_isp *atio)
4353 {
4354 	struct qla_tgt_cmd *cmd;
4355 
4356 	cmd = vha->hw->tgt.tgt_ops->get_cmd(sess);
4357 	if (!cmd)
4358 		return NULL;
4359 
4360 	cmd->cmd_type = TYPE_TGT_CMD;
4361 	memcpy(&cmd->atio, atio, sizeof(*atio));
4362 	INIT_LIST_HEAD(&cmd->sess_cmd_list);
4363 	cmd->state = QLA_TGT_STATE_NEW;
4364 	cmd->tgt = vha->vha_tgt.qla_tgt;
4365 	qlt_incr_num_pend_cmds(vha);
4366 	cmd->vha = vha;
4367 	cmd->sess = sess;
4368 	cmd->loop_id = sess->loop_id;
4369 	cmd->conf_compl_supported = sess->conf_compl_supported;
4370 
4371 	cmd->trc_flags = 0;
4372 	cmd->jiffies_at_alloc = get_jiffies_64();
4373 
4374 	cmd->unpacked_lun = scsilun_to_int(
4375 	    (struct scsi_lun *)&atio->u.isp24.fcp_cmnd.lun);
4376 	qlt_assign_qpair(vha, cmd);
4377 	cmd->reset_count = vha->hw->base_qpair->chip_reset;
4378 	cmd->vp_idx = vha->vp_idx;
4379 	cmd->edif = sess->edif.enable;
4380 
4381 	return cmd;
4382 }
4383 
4384 /* ha->hardware_lock supposed to be held on entry */
4385 static int qlt_handle_cmd_for_atio(struct scsi_qla_host *vha,
4386 	struct atio_from_isp *atio)
4387 {
4388 	struct qla_hw_data *ha = vha->hw;
4389 	struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4390 	struct fc_port *sess;
4391 	struct qla_tgt_cmd *cmd;
4392 	unsigned long flags;
4393 	port_id_t id;
4394 
4395 	if (unlikely(tgt->tgt_stop)) {
4396 		ql_dbg(ql_dbg_io, vha, 0x3061,
4397 		    "New command while device %p is shutting down\n", tgt);
4398 		return -ENODEV;
4399 	}
4400 
4401 	id = be_to_port_id(atio->u.isp24.fcp_hdr.s_id);
4402 	if (IS_SW_RESV_ADDR(id))
4403 		return -EBUSY;
4404 
4405 	sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, atio->u.isp24.fcp_hdr.s_id);
4406 	if (unlikely(!sess))
4407 		return -EFAULT;
4408 
4409 	/* Another WWN used to have our s_id. Our PLOGI scheduled its
4410 	 * session deletion, but it's still in sess_del_work wq */
4411 	if (sess->deleted) {
4412 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf002,
4413 		    "New command while old session %p is being deleted\n",
4414 		    sess);
4415 		return -EFAULT;
4416 	}
4417 
4418 	/*
4419 	 * Do kref_get() before returning + dropping qla_hw_data->hardware_lock.
4420 	 */
4421 	if (!kref_get_unless_zero(&sess->sess_kref)) {
4422 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf004,
4423 		    "%s: kref_get fail, %8phC oxid %x \n",
4424 		    __func__, sess->port_name,
4425 		     be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id));
4426 		return -EFAULT;
4427 	}
4428 
4429 	cmd = qlt_get_tag(vha, sess, atio);
4430 	if (!cmd) {
4431 		ql_dbg(ql_dbg_io, vha, 0x3062,
4432 		    "qla_target(%d): Allocation of cmd failed\n", vha->vp_idx);
4433 		ha->tgt.tgt_ops->put_sess(sess);
4434 		return -EBUSY;
4435 	}
4436 
4437 	cmd->cmd_in_wq = 1;
4438 	cmd->trc_flags |= TRC_NEW_CMD;
4439 
4440 	spin_lock_irqsave(&vha->cmd_list_lock, flags);
4441 	list_add_tail(&cmd->cmd_list, &vha->qla_cmd_list);
4442 	spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
4443 
4444 	INIT_WORK(&cmd->work, qlt_do_work);
4445 	if (vha->flags.qpairs_available) {
4446 		queue_work_on(cmd->se_cmd.cpuid, qla_tgt_wq, &cmd->work);
4447 	} else if (ha->msix_count) {
4448 		if (cmd->atio.u.isp24.fcp_cmnd.rddata)
4449 			queue_work_on(smp_processor_id(), qla_tgt_wq,
4450 			    &cmd->work);
4451 		else
4452 			queue_work_on(cmd->se_cmd.cpuid, qla_tgt_wq,
4453 			    &cmd->work);
4454 	} else {
4455 		queue_work(qla_tgt_wq, &cmd->work);
4456 	}
4457 
4458 	return 0;
4459 }
4460 
4461 /* ha->hardware_lock supposed to be held on entry */
4462 static int qlt_issue_task_mgmt(struct fc_port *sess, u64 lun,
4463 	int fn, void *iocb, int flags)
4464 {
4465 	struct scsi_qla_host *vha = sess->vha;
4466 	struct qla_hw_data *ha = vha->hw;
4467 	struct qla_tgt_mgmt_cmd *mcmd;
4468 	struct atio_from_isp *a = (struct atio_from_isp *)iocb;
4469 	struct qla_qpair_hint *h = &vha->vha_tgt.qla_tgt->qphints[0];
4470 
4471 	mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
4472 	if (!mcmd) {
4473 		ql_dbg(ql_dbg_tgt_tmr, vha, 0x10009,
4474 		    "qla_target(%d): Allocation of management "
4475 		    "command failed, some commands and their data could "
4476 		    "leak\n", vha->vp_idx);
4477 		return -ENOMEM;
4478 	}
4479 	memset(mcmd, 0, sizeof(*mcmd));
4480 	mcmd->sess = sess;
4481 
4482 	if (iocb) {
4483 		memcpy(&mcmd->orig_iocb.imm_ntfy, iocb,
4484 		    sizeof(mcmd->orig_iocb.imm_ntfy));
4485 	}
4486 	mcmd->tmr_func = fn;
4487 	mcmd->flags = flags;
4488 	mcmd->reset_count = ha->base_qpair->chip_reset;
4489 	mcmd->qpair = h->qpair;
4490 	mcmd->vha = vha;
4491 	mcmd->se_cmd.cpuid = h->cpuid;
4492 	mcmd->unpacked_lun = lun;
4493 
4494 	switch (fn) {
4495 	case QLA_TGT_LUN_RESET:
4496 	case QLA_TGT_CLEAR_TS:
4497 	case QLA_TGT_ABORT_TS:
4498 		abort_cmds_for_lun(vha, lun, a->u.isp24.fcp_hdr.s_id);
4499 		fallthrough;
4500 	case QLA_TGT_CLEAR_ACA:
4501 		h = qlt_find_qphint(vha, mcmd->unpacked_lun);
4502 		mcmd->qpair = h->qpair;
4503 		mcmd->se_cmd.cpuid = h->cpuid;
4504 		break;
4505 
4506 	case QLA_TGT_TARGET_RESET:
4507 	case QLA_TGT_NEXUS_LOSS_SESS:
4508 	case QLA_TGT_NEXUS_LOSS:
4509 	case QLA_TGT_ABORT_ALL:
4510 	default:
4511 		/* no-op */
4512 		break;
4513 	}
4514 
4515 	INIT_WORK(&mcmd->work, qlt_do_tmr_work);
4516 	queue_work_on(mcmd->se_cmd.cpuid, qla_tgt_wq,
4517 	    &mcmd->work);
4518 
4519 	return 0;
4520 }
4521 
4522 /* ha->hardware_lock supposed to be held on entry */
4523 static int qlt_handle_task_mgmt(struct scsi_qla_host *vha, void *iocb)
4524 {
4525 	struct atio_from_isp *a = (struct atio_from_isp *)iocb;
4526 	struct qla_hw_data *ha = vha->hw;
4527 	struct fc_port *sess;
4528 	u64 unpacked_lun;
4529 	int fn;
4530 	unsigned long flags;
4531 
4532 	fn = a->u.isp24.fcp_cmnd.task_mgmt_flags;
4533 
4534 	spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4535 	sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
4536 	    a->u.isp24.fcp_hdr.s_id);
4537 	spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4538 
4539 	unpacked_lun =
4540 	    scsilun_to_int((struct scsi_lun *)&a->u.isp24.fcp_cmnd.lun);
4541 
4542 	if (sess == NULL || sess->deleted)
4543 		return -EFAULT;
4544 
4545 	return qlt_issue_task_mgmt(sess, unpacked_lun, fn, iocb, 0);
4546 }
4547 
4548 /* ha->hardware_lock supposed to be held on entry */
4549 static int __qlt_abort_task(struct scsi_qla_host *vha,
4550 	struct imm_ntfy_from_isp *iocb, struct fc_port *sess)
4551 {
4552 	struct atio_from_isp *a = (struct atio_from_isp *)iocb;
4553 	struct qla_hw_data *ha = vha->hw;
4554 	struct qla_tgt_mgmt_cmd *mcmd;
4555 	u64 unpacked_lun;
4556 	int rc;
4557 
4558 	mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
4559 	if (mcmd == NULL) {
4560 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05f,
4561 		    "qla_target(%d): %s: Allocation of ABORT cmd failed\n",
4562 		    vha->vp_idx, __func__);
4563 		return -ENOMEM;
4564 	}
4565 	memset(mcmd, 0, sizeof(*mcmd));
4566 
4567 	mcmd->sess = sess;
4568 	memcpy(&mcmd->orig_iocb.imm_ntfy, iocb,
4569 	    sizeof(mcmd->orig_iocb.imm_ntfy));
4570 
4571 	unpacked_lun =
4572 	    scsilun_to_int((struct scsi_lun *)&a->u.isp24.fcp_cmnd.lun);
4573 	mcmd->reset_count = ha->base_qpair->chip_reset;
4574 	mcmd->tmr_func = QLA_TGT_2G_ABORT_TASK;
4575 	mcmd->qpair = ha->base_qpair;
4576 
4577 	rc = ha->tgt.tgt_ops->handle_tmr(mcmd, unpacked_lun, mcmd->tmr_func,
4578 	    le16_to_cpu(iocb->u.isp2x.seq_id));
4579 	if (rc != 0) {
4580 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf060,
4581 		    "qla_target(%d): tgt_ops->handle_tmr() failed: %d\n",
4582 		    vha->vp_idx, rc);
4583 		mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
4584 		return -EFAULT;
4585 	}
4586 
4587 	return 0;
4588 }
4589 
4590 /* ha->hardware_lock supposed to be held on entry */
4591 static int qlt_abort_task(struct scsi_qla_host *vha,
4592 	struct imm_ntfy_from_isp *iocb)
4593 {
4594 	struct qla_hw_data *ha = vha->hw;
4595 	struct fc_port *sess;
4596 	int loop_id;
4597 	unsigned long flags;
4598 
4599 	loop_id = GET_TARGET_ID(ha, (struct atio_from_isp *)iocb);
4600 
4601 	spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4602 	sess = ha->tgt.tgt_ops->find_sess_by_loop_id(vha, loop_id);
4603 	spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4604 
4605 	if (sess == NULL) {
4606 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf025,
4607 		    "qla_target(%d): task abort for unexisting "
4608 		    "session\n", vha->vp_idx);
4609 		return qlt_sched_sess_work(vha->vha_tgt.qla_tgt,
4610 		    QLA_TGT_SESS_WORK_ABORT, iocb, sizeof(*iocb));
4611 	}
4612 
4613 	return __qlt_abort_task(vha, iocb, sess);
4614 }
4615 
4616 void qlt_logo_completion_handler(fc_port_t *fcport, int rc)
4617 {
4618 	if (rc != MBS_COMMAND_COMPLETE) {
4619 		ql_dbg(ql_dbg_tgt_mgt, fcport->vha, 0xf093,
4620 			"%s: se_sess %p / sess %p from"
4621 			" port %8phC loop_id %#04x s_id %02x:%02x:%02x"
4622 			" LOGO failed: %#x\n",
4623 			__func__,
4624 			fcport->se_sess,
4625 			fcport,
4626 			fcport->port_name, fcport->loop_id,
4627 			fcport->d_id.b.domain, fcport->d_id.b.area,
4628 			fcport->d_id.b.al_pa, rc);
4629 	}
4630 
4631 	fcport->logout_completed = 1;
4632 }
4633 
4634 /*
4635 * ha->hardware_lock supposed to be held on entry (to protect tgt->sess_list)
4636 *
4637 * Schedules sessions with matching port_id/loop_id but different wwn for
4638 * deletion. Returns existing session with matching wwn if present.
4639 * Null otherwise.
4640 */
4641 struct fc_port *
4642 qlt_find_sess_invalidate_other(scsi_qla_host_t *vha, uint64_t wwn,
4643     port_id_t port_id, uint16_t loop_id, struct fc_port **conflict_sess)
4644 {
4645 	struct fc_port *sess = NULL, *other_sess;
4646 	uint64_t other_wwn;
4647 
4648 	*conflict_sess = NULL;
4649 
4650 	list_for_each_entry(other_sess, &vha->vp_fcports, list) {
4651 
4652 		other_wwn = wwn_to_u64(other_sess->port_name);
4653 
4654 		if (wwn == other_wwn) {
4655 			WARN_ON(sess);
4656 			sess = other_sess;
4657 			continue;
4658 		}
4659 
4660 		/* find other sess with nport_id collision */
4661 		if (port_id.b24 == other_sess->d_id.b24) {
4662 			if (loop_id != other_sess->loop_id) {
4663 				ql_dbg(ql_dbg_disc, vha, 0x1000c,
4664 				    "Invalidating sess %p loop_id %d wwn %llx.\n",
4665 				    other_sess, other_sess->loop_id, other_wwn);
4666 
4667 				/*
4668 				 * logout_on_delete is set by default, but another
4669 				 * session that has the same s_id/loop_id combo
4670 				 * might have cleared it when requested this session
4671 				 * deletion, so don't touch it
4672 				 */
4673 				qlt_schedule_sess_for_deletion(other_sess);
4674 			} else {
4675 				/*
4676 				 * Another wwn used to have our s_id/loop_id
4677 				 * kill the session, but don't free the loop_id
4678 				 */
4679 				ql_dbg(ql_dbg_disc, vha, 0xf01b,
4680 				    "Invalidating sess %p loop_id %d wwn %llx.\n",
4681 				    other_sess, other_sess->loop_id, other_wwn);
4682 
4683 				other_sess->keep_nport_handle = 1;
4684 				if (other_sess->disc_state != DSC_DELETED)
4685 					*conflict_sess = other_sess;
4686 				qlt_schedule_sess_for_deletion(other_sess);
4687 			}
4688 			continue;
4689 		}
4690 
4691 		/* find other sess with nport handle collision */
4692 		if ((loop_id == other_sess->loop_id) &&
4693 			(loop_id != FC_NO_LOOP_ID)) {
4694 			ql_dbg(ql_dbg_disc, vha, 0x1000d,
4695 			       "Invalidating sess %p loop_id %d wwn %llx.\n",
4696 			       other_sess, other_sess->loop_id, other_wwn);
4697 
4698 			/* Same loop_id but different s_id
4699 			 * Ok to kill and logout */
4700 			qlt_schedule_sess_for_deletion(other_sess);
4701 		}
4702 	}
4703 
4704 	return sess;
4705 }
4706 
4707 /* Abort any commands for this s_id waiting on qla_tgt_wq workqueue */
4708 static int abort_cmds_for_s_id(struct scsi_qla_host *vha, port_id_t *s_id)
4709 {
4710 	struct qla_tgt_sess_op *op;
4711 	struct qla_tgt_cmd *cmd;
4712 	uint32_t key;
4713 	int count = 0;
4714 	unsigned long flags;
4715 
4716 	key = (((u32)s_id->b.domain << 16) |
4717 	       ((u32)s_id->b.area   <<  8) |
4718 	       ((u32)s_id->b.al_pa));
4719 
4720 	spin_lock_irqsave(&vha->cmd_list_lock, flags);
4721 	list_for_each_entry(op, &vha->unknown_atio_list, cmd_list) {
4722 		uint32_t op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
4723 
4724 		if (op_key == key) {
4725 			op->aborted = true;
4726 			count++;
4727 		}
4728 	}
4729 
4730 	list_for_each_entry(cmd, &vha->qla_cmd_list, cmd_list) {
4731 		uint32_t cmd_key = sid_to_key(cmd->atio.u.isp24.fcp_hdr.s_id);
4732 
4733 		if (cmd_key == key) {
4734 			cmd->aborted = 1;
4735 			count++;
4736 		}
4737 	}
4738 	spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
4739 
4740 	return count;
4741 }
4742 
4743 static int qlt_handle_login(struct scsi_qla_host *vha,
4744     struct imm_ntfy_from_isp *iocb)
4745 {
4746 	struct fc_port *sess = NULL, *conflict_sess = NULL;
4747 	uint64_t wwn;
4748 	port_id_t port_id;
4749 	uint16_t loop_id, wd3_lo;
4750 	int res = 0;
4751 	struct qlt_plogi_ack_t *pla;
4752 	unsigned long flags;
4753 
4754 	lockdep_assert_held(&vha->hw->hardware_lock);
4755 
4756 	wwn = wwn_to_u64(iocb->u.isp24.port_name);
4757 
4758 	port_id.b.domain = iocb->u.isp24.port_id[2];
4759 	port_id.b.area   = iocb->u.isp24.port_id[1];
4760 	port_id.b.al_pa  = iocb->u.isp24.port_id[0];
4761 	port_id.b.rsvd_1 = 0;
4762 
4763 	loop_id = le16_to_cpu(iocb->u.isp24.nport_handle);
4764 
4765 	/* Mark all stale commands sitting in qla_tgt_wq for deletion */
4766 	abort_cmds_for_s_id(vha, &port_id);
4767 
4768 	if (wwn) {
4769 		spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
4770 		sess = qlt_find_sess_invalidate_other(vha, wwn,
4771 		    port_id, loop_id, &conflict_sess);
4772 		spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
4773 	} else {
4774 		ql_dbg(ql_dbg_disc, vha, 0xffff,
4775 		    "%s %d Term INOT due to WWN=0 lid=%d, NportID %06X ",
4776 		    __func__, __LINE__, loop_id, port_id.b24);
4777 		qlt_send_term_imm_notif(vha, iocb, 1);
4778 		goto out;
4779 	}
4780 
4781 	if (IS_SW_RESV_ADDR(port_id)) {
4782 		res = 1;
4783 		goto out;
4784 	}
4785 
4786 	if (vha->hw->flags.edif_enabled &&
4787 	    !(vha->e_dbell.db_flags & EDB_ACTIVE) &&
4788 	    iocb->u.isp24.status_subcode == ELS_PLOGI &&
4789 	    !(le16_to_cpu(iocb->u.isp24.flags) & NOTIFY24XX_FLAGS_FCSP)) {
4790 		ql_dbg(ql_dbg_disc, vha, 0xffff,
4791 			"%s %d Term INOT due to app not available lid=%d, NportID %06X ",
4792 			__func__, __LINE__, loop_id, port_id.b24);
4793 		qlt_send_term_imm_notif(vha, iocb, 1);
4794 		goto out;
4795 	}
4796 
4797 	if (vha->hw->flags.edif_enabled) {
4798 		if (DBELL_INACTIVE(vha)) {
4799 			ql_dbg(ql_dbg_disc, vha, 0xffff,
4800 			       "%s %d Term INOT due to app not started lid=%d, NportID %06X ",
4801 			       __func__, __LINE__, loop_id, port_id.b24);
4802 			qlt_send_term_imm_notif(vha, iocb, 1);
4803 			goto out;
4804 		} else if (iocb->u.isp24.status_subcode == ELS_PLOGI &&
4805 			   !(le16_to_cpu(iocb->u.isp24.flags) & NOTIFY24XX_FLAGS_FCSP)) {
4806 			ql_dbg(ql_dbg_disc, vha, 0xffff,
4807 			       "%s %d Term INOT due to unsecure lid=%d, NportID %06X ",
4808 			       __func__, __LINE__, loop_id, port_id.b24);
4809 			qlt_send_term_imm_notif(vha, iocb, 1);
4810 			goto out;
4811 		}
4812 	}
4813 
4814 	pla = qlt_plogi_ack_find_add(vha, &port_id, iocb);
4815 	if (!pla) {
4816 		ql_dbg(ql_dbg_disc + ql_dbg_verbose, vha, 0xffff,
4817 		    "%s %d %8phC Term INOT due to mem alloc fail",
4818 		    __func__, __LINE__,
4819 		    iocb->u.isp24.port_name);
4820 		qlt_send_term_imm_notif(vha, iocb, 1);
4821 		goto out;
4822 	}
4823 
4824 	if (conflict_sess) {
4825 		conflict_sess->login_gen++;
4826 		qlt_plogi_ack_link(vha, pla, conflict_sess,
4827 		    QLT_PLOGI_LINK_CONFLICT);
4828 	}
4829 
4830 	if (!sess) {
4831 		pla->ref_count++;
4832 		ql_dbg(ql_dbg_disc, vha, 0xffff,
4833 		    "%s %d %8phC post new sess\n",
4834 		    __func__, __LINE__, iocb->u.isp24.port_name);
4835 		if (iocb->u.isp24.status_subcode == ELS_PLOGI)
4836 			qla24xx_post_newsess_work(vha, &port_id,
4837 			    iocb->u.isp24.port_name,
4838 			    iocb->u.isp24.u.plogi.node_name,
4839 			    pla, 0);
4840 		else
4841 			qla24xx_post_newsess_work(vha, &port_id,
4842 			    iocb->u.isp24.port_name, NULL,
4843 			    pla, 0);
4844 
4845 		goto out;
4846 	}
4847 
4848 	if (sess->disc_state == DSC_UPD_FCPORT) {
4849 		u16 sec;
4850 
4851 		/*
4852 		 * Remote port registration is still going on from
4853 		 * previous login. Allow it to finish before we
4854 		 * accept the new login.
4855 		 */
4856 		sess->next_disc_state = DSC_DELETE_PEND;
4857 		sec = jiffies_to_msecs(jiffies -
4858 		    sess->jiffies_at_registration) / 1000;
4859 		if (sess->sec_since_registration < sec && sec &&
4860 		    !(sec % 5)) {
4861 			sess->sec_since_registration = sec;
4862 			ql_dbg(ql_dbg_disc, vha, 0xffff,
4863 			    "%s %8phC - Slow Rport registration (%d Sec)\n",
4864 			    __func__, sess->port_name, sec);
4865 		}
4866 
4867 		if (!conflict_sess) {
4868 			list_del(&pla->list);
4869 			kmem_cache_free(qla_tgt_plogi_cachep, pla);
4870 		}
4871 
4872 		qlt_send_term_imm_notif(vha, iocb, 1);
4873 		goto out;
4874 	}
4875 
4876 	qlt_plogi_ack_link(vha, pla, sess, QLT_PLOGI_LINK_SAME_WWN);
4877 	sess->d_id = port_id;
4878 	sess->login_gen++;
4879 	sess->loop_id = loop_id;
4880 
4881 	if (iocb->u.isp24.status_subcode == ELS_PLOGI) {
4882 		/* remote port has assigned Port ID */
4883 		if (N2N_TOPO(vha->hw) && fcport_is_bigger(sess))
4884 			vha->d_id = sess->d_id;
4885 
4886 		ql_dbg(ql_dbg_disc, vha, 0xffff,
4887 		    "%s %8phC - send port online\n",
4888 		    __func__, sess->port_name);
4889 
4890 		qla2x00_post_aen_work(vha, FCH_EVT_PORT_ONLINE,
4891 		    sess->d_id.b24);
4892 	}
4893 
4894 	if (iocb->u.isp24.status_subcode == ELS_PRLI) {
4895 		sess->fw_login_state = DSC_LS_PRLI_PEND;
4896 		sess->local = 0;
4897 		sess->loop_id = loop_id;
4898 		sess->d_id = port_id;
4899 		sess->fw_login_state = DSC_LS_PRLI_PEND;
4900 		wd3_lo = le16_to_cpu(iocb->u.isp24.u.prli.wd3_lo);
4901 
4902 		if (wd3_lo & BIT_7)
4903 			sess->conf_compl_supported = 1;
4904 
4905 		if ((wd3_lo & BIT_4) == 0)
4906 			sess->port_type = FCT_INITIATOR;
4907 		else
4908 			sess->port_type = FCT_TARGET;
4909 
4910 	} else
4911 		sess->fw_login_state = DSC_LS_PLOGI_PEND;
4912 
4913 
4914 	ql_dbg(ql_dbg_disc, vha, 0x20f9,
4915 	    "%s %d %8phC  DS %d\n",
4916 	    __func__, __LINE__, sess->port_name, sess->disc_state);
4917 
4918 	switch (sess->disc_state) {
4919 	case DSC_DELETED:
4920 	case DSC_LOGIN_PEND:
4921 		qlt_plogi_ack_unref(vha, pla);
4922 		break;
4923 
4924 	default:
4925 		/*
4926 		 * Under normal circumstances we want to release nport handle
4927 		 * during LOGO process to avoid nport handle leaks inside FW.
4928 		 * The exception is when LOGO is done while another PLOGI with
4929 		 * the same nport handle is waiting as might be the case here.
4930 		 * Note: there is always a possibily of a race where session
4931 		 * deletion has already started for other reasons (e.g. ACL
4932 		 * removal) and now PLOGI arrives:
4933 		 * 1. if PLOGI arrived in FW after nport handle has been freed,
4934 		 *    FW must have assigned this PLOGI a new/same handle and we
4935 		 *    can proceed ACK'ing it as usual when session deletion
4936 		 *    completes.
4937 		 * 2. if PLOGI arrived in FW before LOGO with LCF_FREE_NPORT
4938 		 *    bit reached it, the handle has now been released. We'll
4939 		 *    get an error when we ACK this PLOGI. Nothing will be sent
4940 		 *    back to initiator. Initiator should eventually retry
4941 		 *    PLOGI and situation will correct itself.
4942 		 */
4943 		sess->keep_nport_handle = ((sess->loop_id == loop_id) &&
4944 		    (sess->d_id.b24 == port_id.b24));
4945 
4946 		ql_dbg(ql_dbg_disc, vha, 0x20f9,
4947 		    "%s %d %8phC post del sess\n",
4948 		    __func__, __LINE__, sess->port_name);
4949 
4950 
4951 		qlt_schedule_sess_for_deletion(sess);
4952 		break;
4953 	}
4954 out:
4955 	return res;
4956 }
4957 
4958 /*
4959  * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
4960  */
4961 static int qlt_24xx_handle_els(struct scsi_qla_host *vha,
4962 	struct imm_ntfy_from_isp *iocb)
4963 {
4964 	struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4965 	struct qla_hw_data *ha = vha->hw;
4966 	struct fc_port *sess = NULL, *conflict_sess = NULL;
4967 	uint64_t wwn;
4968 	port_id_t port_id;
4969 	uint16_t loop_id;
4970 	uint16_t wd3_lo;
4971 	int res = 0;
4972 	unsigned long flags;
4973 
4974 	lockdep_assert_held(&ha->hardware_lock);
4975 
4976 	wwn = wwn_to_u64(iocb->u.isp24.port_name);
4977 
4978 	port_id.b.domain = iocb->u.isp24.port_id[2];
4979 	port_id.b.area   = iocb->u.isp24.port_id[1];
4980 	port_id.b.al_pa  = iocb->u.isp24.port_id[0];
4981 	port_id.b.rsvd_1 = 0;
4982 
4983 	loop_id = le16_to_cpu(iocb->u.isp24.nport_handle);
4984 
4985 	ql_dbg(ql_dbg_disc, vha, 0xf026,
4986 	    "qla_target(%d): Port ID: %02x:%02x:%02x ELS opcode: 0x%02x lid %d %8phC\n",
4987 	    vha->vp_idx, iocb->u.isp24.port_id[2],
4988 		iocb->u.isp24.port_id[1], iocb->u.isp24.port_id[0],
4989 		   iocb->u.isp24.status_subcode, loop_id,
4990 		iocb->u.isp24.port_name);
4991 
4992 	/* res = 1 means ack at the end of thread
4993 	 * res = 0 means ack async/later.
4994 	 */
4995 	switch (iocb->u.isp24.status_subcode) {
4996 	case ELS_PLOGI:
4997 		res = qlt_handle_login(vha, iocb);
4998 		break;
4999 
5000 	case ELS_PRLI:
5001 		if (N2N_TOPO(ha)) {
5002 			sess = qla2x00_find_fcport_by_wwpn(vha,
5003 			    iocb->u.isp24.port_name, 1);
5004 
5005 			if (vha->hw->flags.edif_enabled && sess &&
5006 			    (!(sess->flags & FCF_FCSP_DEVICE) ||
5007 			     !sess->edif.authok)) {
5008 				ql_dbg(ql_dbg_disc, vha, 0xffff,
5009 				       "%s %d %8phC Term PRLI due to unauthorize PRLI\n",
5010 				       __func__, __LINE__, iocb->u.isp24.port_name);
5011 				qlt_send_term_imm_notif(vha, iocb, 1);
5012 				break;
5013 			}
5014 
5015 			if (sess && sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN]) {
5016 				ql_dbg(ql_dbg_disc, vha, 0xffff,
5017 				    "%s %d %8phC Term PRLI due to PLOGI ACK not completed\n",
5018 				    __func__, __LINE__,
5019 				    iocb->u.isp24.port_name);
5020 				qlt_send_term_imm_notif(vha, iocb, 1);
5021 				break;
5022 			}
5023 
5024 			res = qlt_handle_login(vha, iocb);
5025 			break;
5026 		}
5027 
5028 		if (IS_SW_RESV_ADDR(port_id)) {
5029 			res = 1;
5030 			break;
5031 		}
5032 
5033 		wd3_lo = le16_to_cpu(iocb->u.isp24.u.prli.wd3_lo);
5034 
5035 		if (wwn) {
5036 			spin_lock_irqsave(&tgt->ha->tgt.sess_lock, flags);
5037 			sess = qlt_find_sess_invalidate_other(vha, wwn, port_id,
5038 				loop_id, &conflict_sess);
5039 			spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock, flags);
5040 		}
5041 
5042 		if (conflict_sess) {
5043 			switch (conflict_sess->disc_state) {
5044 			case DSC_DELETED:
5045 			case DSC_DELETE_PEND:
5046 				break;
5047 			default:
5048 				ql_dbg(ql_dbg_tgt_mgt, vha, 0xf09b,
5049 				    "PRLI with conflicting sess %p port %8phC\n",
5050 				    conflict_sess, conflict_sess->port_name);
5051 				conflict_sess->fw_login_state =
5052 				    DSC_LS_PORT_UNAVAIL;
5053 				qlt_send_term_imm_notif(vha, iocb, 1);
5054 				res = 0;
5055 				break;
5056 			}
5057 		}
5058 
5059 		if (sess != NULL) {
5060 			bool delete = false;
5061 			int sec;
5062 
5063 			if (vha->hw->flags.edif_enabled && sess &&
5064 			    (!(sess->flags & FCF_FCSP_DEVICE) ||
5065 			     !sess->edif.authok)) {
5066 				ql_dbg(ql_dbg_disc, vha, 0xffff,
5067 				       "%s %d %8phC Term PRLI due to unauthorize prli\n",
5068 				       __func__, __LINE__, iocb->u.isp24.port_name);
5069 				qlt_send_term_imm_notif(vha, iocb, 1);
5070 				break;
5071 			}
5072 
5073 			spin_lock_irqsave(&tgt->ha->tgt.sess_lock, flags);
5074 			switch (sess->fw_login_state) {
5075 			case DSC_LS_PLOGI_PEND:
5076 			case DSC_LS_PLOGI_COMP:
5077 			case DSC_LS_PRLI_COMP:
5078 				break;
5079 			default:
5080 				delete = true;
5081 				break;
5082 			}
5083 
5084 			switch (sess->disc_state) {
5085 			case DSC_UPD_FCPORT:
5086 				spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock,
5087 				    flags);
5088 
5089 				sec = jiffies_to_msecs(jiffies -
5090 				    sess->jiffies_at_registration)/1000;
5091 				if (sess->sec_since_registration < sec && sec &&
5092 				    !(sec % 5)) {
5093 					sess->sec_since_registration = sec;
5094 					ql_dbg(ql_dbg_disc, sess->vha, 0xffff,
5095 					    "%s %8phC : Slow Rport registration(%d Sec)\n",
5096 					    __func__, sess->port_name, sec);
5097 				}
5098 				qlt_send_term_imm_notif(vha, iocb, 1);
5099 				return 0;
5100 
5101 			case DSC_LOGIN_PEND:
5102 			case DSC_GPDB:
5103 			case DSC_LOGIN_COMPLETE:
5104 			case DSC_ADISC:
5105 				delete = false;
5106 				break;
5107 			default:
5108 				break;
5109 			}
5110 
5111 			if (delete) {
5112 				spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock,
5113 				    flags);
5114 				/*
5115 				 * Impatient initiator sent PRLI before last
5116 				 * PLOGI could finish. Will force him to re-try,
5117 				 * while last one finishes.
5118 				 */
5119 				ql_log(ql_log_warn, sess->vha, 0xf095,
5120 				    "sess %p PRLI received, before plogi ack.\n",
5121 				    sess);
5122 				qlt_send_term_imm_notif(vha, iocb, 1);
5123 				res = 0;
5124 				break;
5125 			}
5126 
5127 			/*
5128 			 * This shouldn't happen under normal circumstances,
5129 			 * since we have deleted the old session during PLOGI
5130 			 */
5131 			ql_dbg(ql_dbg_tgt_mgt, vha, 0xf096,
5132 			    "PRLI (loop_id %#04x) for existing sess %p (loop_id %#04x)\n",
5133 			    sess->loop_id, sess, iocb->u.isp24.nport_handle);
5134 
5135 			sess->local = 0;
5136 			sess->loop_id = loop_id;
5137 			sess->d_id = port_id;
5138 			sess->fw_login_state = DSC_LS_PRLI_PEND;
5139 
5140 			if (wd3_lo & BIT_7)
5141 				sess->conf_compl_supported = 1;
5142 
5143 			if ((wd3_lo & BIT_4) == 0)
5144 				sess->port_type = FCT_INITIATOR;
5145 			else
5146 				sess->port_type = FCT_TARGET;
5147 
5148 			spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock, flags);
5149 		}
5150 		res = 1; /* send notify ack */
5151 
5152 		/* Make session global (not used in fabric mode) */
5153 		if (ha->current_topology != ISP_CFG_F) {
5154 			if (sess) {
5155 				ql_dbg(ql_dbg_disc, vha, 0x20fa,
5156 				    "%s %d %8phC post nack\n",
5157 				    __func__, __LINE__, sess->port_name);
5158 				qla24xx_post_nack_work(vha, sess, iocb,
5159 					SRB_NACK_PRLI);
5160 				res = 0;
5161 			} else {
5162 				set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
5163 				set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
5164 				qla2xxx_wake_dpc(vha);
5165 			}
5166 		} else {
5167 			if (sess) {
5168 				ql_dbg(ql_dbg_disc, vha, 0x20fb,
5169 				    "%s %d %8phC post nack\n",
5170 				    __func__, __LINE__, sess->port_name);
5171 				qla24xx_post_nack_work(vha, sess, iocb,
5172 					SRB_NACK_PRLI);
5173 				res = 0;
5174 			}
5175 		}
5176 		break;
5177 
5178 	case ELS_TPRLO:
5179 		if (le16_to_cpu(iocb->u.isp24.flags) &
5180 			NOTIFY24XX_FLAGS_GLOBAL_TPRLO) {
5181 			loop_id = 0xFFFF;
5182 			qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS);
5183 			res = 1;
5184 			break;
5185 		}
5186 		fallthrough;
5187 	case ELS_LOGO:
5188 	case ELS_PRLO:
5189 		spin_lock_irqsave(&ha->tgt.sess_lock, flags);
5190 		sess = qla2x00_find_fcport_by_loopid(vha, loop_id);
5191 		spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
5192 
5193 		if (sess) {
5194 			sess->login_gen++;
5195 			sess->fw_login_state = DSC_LS_LOGO_PEND;
5196 			sess->logo_ack_needed = 1;
5197 			memcpy(sess->iocb, iocb, IOCB_SIZE);
5198 		}
5199 
5200 		res = qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS);
5201 
5202 		ql_dbg(ql_dbg_disc, vha, 0x20fc,
5203 		    "%s: logo %llx res %d sess %p ",
5204 		    __func__, wwn, res, sess);
5205 		if (res == 0) {
5206 			/*
5207 			 * cmd went upper layer, look for qlt_xmit_tm_rsp()
5208 			 * for LOGO_ACK & sess delete
5209 			 */
5210 			BUG_ON(!sess);
5211 			res = 0;
5212 		} else {
5213 			/* cmd did not go to upper layer. */
5214 			if (sess) {
5215 				qlt_schedule_sess_for_deletion(sess);
5216 				res = 0;
5217 			}
5218 			/* else logo will be ack */
5219 		}
5220 		break;
5221 	case ELS_PDISC:
5222 	case ELS_ADISC:
5223 	{
5224 		struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5225 
5226 		if (tgt->link_reinit_iocb_pending) {
5227 			qlt_send_notify_ack(ha->base_qpair,
5228 			    &tgt->link_reinit_iocb, 0, 0, 0, 0, 0, 0);
5229 			tgt->link_reinit_iocb_pending = 0;
5230 		}
5231 
5232 		sess = qla2x00_find_fcport_by_wwpn(vha,
5233 		    iocb->u.isp24.port_name, 1);
5234 		if (sess) {
5235 			ql_dbg(ql_dbg_disc, vha, 0x20fd,
5236 				"sess %p lid %d|%d DS %d LS %d\n",
5237 				sess, sess->loop_id, loop_id,
5238 				sess->disc_state, sess->fw_login_state);
5239 		}
5240 
5241 		res = 1; /* send notify ack */
5242 		break;
5243 	}
5244 
5245 	case ELS_FLOGI:	/* should never happen */
5246 	default:
5247 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf061,
5248 		    "qla_target(%d): Unsupported ELS command %x "
5249 		    "received\n", vha->vp_idx, iocb->u.isp24.status_subcode);
5250 		res = qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS);
5251 		break;
5252 	}
5253 
5254 	ql_dbg(ql_dbg_disc, vha, 0xf026,
5255 	    "qla_target(%d): Exit ELS opcode: 0x%02x res %d\n",
5256 	    vha->vp_idx, iocb->u.isp24.status_subcode, res);
5257 
5258 	return res;
5259 }
5260 
5261 /*
5262  * ha->hardware_lock supposed to be held on entry.
5263  * Might drop it, then reacquire.
5264  */
5265 static void qlt_handle_imm_notify(struct scsi_qla_host *vha,
5266 	struct imm_ntfy_from_isp *iocb)
5267 {
5268 	struct qla_hw_data *ha = vha->hw;
5269 	uint32_t add_flags = 0;
5270 	int send_notify_ack = 1;
5271 	uint16_t status;
5272 
5273 	lockdep_assert_held(&ha->hardware_lock);
5274 
5275 	status = le16_to_cpu(iocb->u.isp2x.status);
5276 	switch (status) {
5277 	case IMM_NTFY_LIP_RESET:
5278 	{
5279 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf032,
5280 		    "qla_target(%d): LIP reset (loop %#x), subcode %x\n",
5281 		    vha->vp_idx, le16_to_cpu(iocb->u.isp24.nport_handle),
5282 		    iocb->u.isp24.status_subcode);
5283 
5284 		if (qlt_reset(vha, iocb, QLA_TGT_ABORT_ALL) == 0)
5285 			send_notify_ack = 0;
5286 		break;
5287 	}
5288 
5289 	case IMM_NTFY_LIP_LINK_REINIT:
5290 	{
5291 		struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5292 
5293 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf033,
5294 		    "qla_target(%d): LINK REINIT (loop %#x, "
5295 		    "subcode %x)\n", vha->vp_idx,
5296 		    le16_to_cpu(iocb->u.isp24.nport_handle),
5297 		    iocb->u.isp24.status_subcode);
5298 		if (tgt->link_reinit_iocb_pending) {
5299 			qlt_send_notify_ack(ha->base_qpair,
5300 			    &tgt->link_reinit_iocb, 0, 0, 0, 0, 0, 0);
5301 		}
5302 		memcpy(&tgt->link_reinit_iocb, iocb, sizeof(*iocb));
5303 		tgt->link_reinit_iocb_pending = 1;
5304 		/*
5305 		 * QLogic requires to wait after LINK REINIT for possible
5306 		 * PDISC or ADISC ELS commands
5307 		 */
5308 		send_notify_ack = 0;
5309 		break;
5310 	}
5311 
5312 	case IMM_NTFY_PORT_LOGOUT:
5313 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf034,
5314 		    "qla_target(%d): Port logout (loop "
5315 		    "%#x, subcode %x)\n", vha->vp_idx,
5316 		    le16_to_cpu(iocb->u.isp24.nport_handle),
5317 		    iocb->u.isp24.status_subcode);
5318 
5319 		if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS) == 0)
5320 			send_notify_ack = 0;
5321 		/* The sessions will be cleared in the callback, if needed */
5322 		break;
5323 
5324 	case IMM_NTFY_GLBL_TPRLO:
5325 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf035,
5326 		    "qla_target(%d): Global TPRLO (%x)\n", vha->vp_idx, status);
5327 		if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS) == 0)
5328 			send_notify_ack = 0;
5329 		/* The sessions will be cleared in the callback, if needed */
5330 		break;
5331 
5332 	case IMM_NTFY_PORT_CONFIG:
5333 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf036,
5334 		    "qla_target(%d): Port config changed (%x)\n", vha->vp_idx,
5335 		    status);
5336 		if (qlt_reset(vha, iocb, QLA_TGT_ABORT_ALL) == 0)
5337 			send_notify_ack = 0;
5338 		/* The sessions will be cleared in the callback, if needed */
5339 		break;
5340 
5341 	case IMM_NTFY_GLBL_LOGO:
5342 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06a,
5343 		    "qla_target(%d): Link failure detected\n",
5344 		    vha->vp_idx);
5345 		/* I_T nexus loss */
5346 		if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS) == 0)
5347 			send_notify_ack = 0;
5348 		break;
5349 
5350 	case IMM_NTFY_IOCB_OVERFLOW:
5351 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06b,
5352 		    "qla_target(%d): Cannot provide requested "
5353 		    "capability (IOCB overflowed the immediate notify "
5354 		    "resource count)\n", vha->vp_idx);
5355 		break;
5356 
5357 	case IMM_NTFY_ABORT_TASK:
5358 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf037,
5359 		    "qla_target(%d): Abort Task (S %08x I %#x -> "
5360 		    "L %#x)\n", vha->vp_idx,
5361 		    le16_to_cpu(iocb->u.isp2x.seq_id),
5362 		    GET_TARGET_ID(ha, (struct atio_from_isp *)iocb),
5363 		    le16_to_cpu(iocb->u.isp2x.lun));
5364 		if (qlt_abort_task(vha, iocb) == 0)
5365 			send_notify_ack = 0;
5366 		break;
5367 
5368 	case IMM_NTFY_RESOURCE:
5369 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06c,
5370 		    "qla_target(%d): Out of resources, host %ld\n",
5371 		    vha->vp_idx, vha->host_no);
5372 		break;
5373 
5374 	case IMM_NTFY_MSG_RX:
5375 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf038,
5376 		    "qla_target(%d): Immediate notify task %x\n",
5377 		    vha->vp_idx, iocb->u.isp2x.task_flags);
5378 		break;
5379 
5380 	case IMM_NTFY_ELS:
5381 		if (qlt_24xx_handle_els(vha, iocb) == 0)
5382 			send_notify_ack = 0;
5383 		break;
5384 	default:
5385 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06d,
5386 		    "qla_target(%d): Received unknown immediate "
5387 		    "notify status %x\n", vha->vp_idx, status);
5388 		break;
5389 	}
5390 
5391 	if (send_notify_ack)
5392 		qlt_send_notify_ack(ha->base_qpair, iocb, add_flags, 0, 0, 0,
5393 		    0, 0);
5394 }
5395 
5396 /*
5397  * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
5398  * This function sends busy to ISP 2xxx or 24xx.
5399  */
5400 static int __qlt_send_busy(struct qla_qpair *qpair,
5401 	struct atio_from_isp *atio, uint16_t status)
5402 {
5403 	struct scsi_qla_host *vha = qpair->vha;
5404 	struct ctio7_to_24xx *ctio24;
5405 	struct qla_hw_data *ha = vha->hw;
5406 	request_t *pkt;
5407 	struct fc_port *sess = NULL;
5408 	unsigned long flags;
5409 	u16 temp;
5410 	port_id_t id;
5411 
5412 	id = be_to_port_id(atio->u.isp24.fcp_hdr.s_id);
5413 
5414 	spin_lock_irqsave(&ha->tgt.sess_lock, flags);
5415 	sess = qla2x00_find_fcport_by_nportid(vha, &id, 1);
5416 	spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
5417 	if (!sess) {
5418 		qlt_send_term_exchange(qpair, NULL, atio, 1, 0);
5419 		return 0;
5420 	}
5421 	/* Sending marker isn't necessary, since we called from ISR */
5422 
5423 	pkt = (request_t *)__qla2x00_alloc_iocbs(qpair, NULL);
5424 	if (!pkt) {
5425 		ql_dbg(ql_dbg_io, vha, 0x3063,
5426 		    "qla_target(%d): %s failed: unable to allocate "
5427 		    "request packet", vha->vp_idx, __func__);
5428 		return -ENOMEM;
5429 	}
5430 
5431 	qpair->tgt_counters.num_q_full_sent++;
5432 	pkt->entry_count = 1;
5433 	pkt->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
5434 
5435 	ctio24 = (struct ctio7_to_24xx *)pkt;
5436 	ctio24->entry_type = CTIO_TYPE7;
5437 	ctio24->nport_handle = cpu_to_le16(sess->loop_id);
5438 	ctio24->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
5439 	ctio24->vp_index = vha->vp_idx;
5440 	ctio24->initiator_id = be_id_to_le(atio->u.isp24.fcp_hdr.s_id);
5441 	ctio24->exchange_addr = atio->u.isp24.exchange_addr;
5442 	temp = (atio->u.isp24.attr << 9) |
5443 		CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS |
5444 		CTIO7_FLAGS_DONT_RET_CTIO;
5445 	ctio24->u.status1.flags = cpu_to_le16(temp);
5446 	/*
5447 	 * CTIO from fw w/o se_cmd doesn't provide enough info to retry it,
5448 	 * if the explicit conformation is used.
5449 	 */
5450 	ctio24->u.status1.ox_id =
5451 		cpu_to_le16(be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id));
5452 	ctio24->u.status1.scsi_status = cpu_to_le16(status);
5453 
5454 	ctio24->u.status1.residual = cpu_to_le32(get_datalen_for_atio(atio));
5455 
5456 	if (ctio24->u.status1.residual != 0)
5457 		ctio24->u.status1.scsi_status |= cpu_to_le16(SS_RESIDUAL_UNDER);
5458 
5459 	/* Memory Barrier */
5460 	wmb();
5461 	if (qpair->reqq_start_iocbs)
5462 		qpair->reqq_start_iocbs(qpair);
5463 	else
5464 		qla2x00_start_iocbs(vha, qpair->req);
5465 	return 0;
5466 }
5467 
5468 /*
5469  * This routine is used to allocate a command for either a QFull condition
5470  * (ie reply SAM_STAT_BUSY) or to terminate an exchange that did not go
5471  * out previously.
5472  */
5473 static void
5474 qlt_alloc_qfull_cmd(struct scsi_qla_host *vha,
5475 	struct atio_from_isp *atio, uint16_t status, int qfull)
5476 {
5477 	struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5478 	struct qla_hw_data *ha = vha->hw;
5479 	struct fc_port *sess;
5480 	struct qla_tgt_cmd *cmd;
5481 	unsigned long flags;
5482 
5483 	if (unlikely(tgt->tgt_stop)) {
5484 		ql_dbg(ql_dbg_io, vha, 0x300a,
5485 			"New command while device %p is shutting down\n", tgt);
5486 		return;
5487 	}
5488 
5489 	if ((vha->hw->tgt.num_qfull_cmds_alloc + 1) > MAX_QFULL_CMDS_ALLOC) {
5490 		vha->hw->tgt.num_qfull_cmds_dropped++;
5491 		if (vha->hw->tgt.num_qfull_cmds_dropped >
5492 			vha->qla_stats.stat_max_qfull_cmds_dropped)
5493 			vha->qla_stats.stat_max_qfull_cmds_dropped =
5494 				vha->hw->tgt.num_qfull_cmds_dropped;
5495 
5496 		ql_dbg(ql_dbg_io, vha, 0x3068,
5497 			"qla_target(%d): %s: QFull CMD dropped[%d]\n",
5498 			vha->vp_idx, __func__,
5499 			vha->hw->tgt.num_qfull_cmds_dropped);
5500 
5501 		qlt_chk_exch_leak_thresh_hold(vha);
5502 		return;
5503 	}
5504 
5505 	sess = ha->tgt.tgt_ops->find_sess_by_s_id
5506 		(vha, atio->u.isp24.fcp_hdr.s_id);
5507 	if (!sess)
5508 		return;
5509 
5510 	cmd = ha->tgt.tgt_ops->get_cmd(sess);
5511 	if (!cmd) {
5512 		ql_dbg(ql_dbg_io, vha, 0x3009,
5513 			"qla_target(%d): %s: Allocation of cmd failed\n",
5514 			vha->vp_idx, __func__);
5515 
5516 		vha->hw->tgt.num_qfull_cmds_dropped++;
5517 		if (vha->hw->tgt.num_qfull_cmds_dropped >
5518 			vha->qla_stats.stat_max_qfull_cmds_dropped)
5519 			vha->qla_stats.stat_max_qfull_cmds_dropped =
5520 				vha->hw->tgt.num_qfull_cmds_dropped;
5521 
5522 		qlt_chk_exch_leak_thresh_hold(vha);
5523 		return;
5524 	}
5525 
5526 	qlt_incr_num_pend_cmds(vha);
5527 	INIT_LIST_HEAD(&cmd->cmd_list);
5528 	memcpy(&cmd->atio, atio, sizeof(*atio));
5529 
5530 	cmd->tgt = vha->vha_tgt.qla_tgt;
5531 	cmd->vha = vha;
5532 	cmd->reset_count = ha->base_qpair->chip_reset;
5533 	cmd->q_full = 1;
5534 	cmd->qpair = ha->base_qpair;
5535 
5536 	if (qfull) {
5537 		cmd->q_full = 1;
5538 		/* NOTE: borrowing the state field to carry the status */
5539 		cmd->state = status;
5540 	} else
5541 		cmd->term_exchg = 1;
5542 
5543 	spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
5544 	list_add_tail(&cmd->cmd_list, &vha->hw->tgt.q_full_list);
5545 
5546 	vha->hw->tgt.num_qfull_cmds_alloc++;
5547 	if (vha->hw->tgt.num_qfull_cmds_alloc >
5548 		vha->qla_stats.stat_max_qfull_cmds_alloc)
5549 		vha->qla_stats.stat_max_qfull_cmds_alloc =
5550 			vha->hw->tgt.num_qfull_cmds_alloc;
5551 	spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
5552 }
5553 
5554 int
5555 qlt_free_qfull_cmds(struct qla_qpair *qpair)
5556 {
5557 	struct scsi_qla_host *vha = qpair->vha;
5558 	struct qla_hw_data *ha = vha->hw;
5559 	unsigned long flags;
5560 	struct qla_tgt_cmd *cmd, *tcmd;
5561 	struct list_head free_list, q_full_list;
5562 	int rc = 0;
5563 
5564 	if (list_empty(&ha->tgt.q_full_list))
5565 		return 0;
5566 
5567 	INIT_LIST_HEAD(&free_list);
5568 	INIT_LIST_HEAD(&q_full_list);
5569 
5570 	spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
5571 	if (list_empty(&ha->tgt.q_full_list)) {
5572 		spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
5573 		return 0;
5574 	}
5575 
5576 	list_splice_init(&vha->hw->tgt.q_full_list, &q_full_list);
5577 	spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
5578 
5579 	spin_lock_irqsave(qpair->qp_lock_ptr, flags);
5580 	list_for_each_entry_safe(cmd, tcmd, &q_full_list, cmd_list) {
5581 		if (cmd->q_full)
5582 			/* cmd->state is a borrowed field to hold status */
5583 			rc = __qlt_send_busy(qpair, &cmd->atio, cmd->state);
5584 		else if (cmd->term_exchg)
5585 			rc = __qlt_send_term_exchange(qpair, NULL, &cmd->atio);
5586 
5587 		if (rc == -ENOMEM)
5588 			break;
5589 
5590 		if (cmd->q_full)
5591 			ql_dbg(ql_dbg_io, vha, 0x3006,
5592 			    "%s: busy sent for ox_id[%04x]\n", __func__,
5593 			    be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
5594 		else if (cmd->term_exchg)
5595 			ql_dbg(ql_dbg_io, vha, 0x3007,
5596 			    "%s: Term exchg sent for ox_id[%04x]\n", __func__,
5597 			    be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
5598 		else
5599 			ql_dbg(ql_dbg_io, vha, 0x3008,
5600 			    "%s: Unexpected cmd in QFull list %p\n", __func__,
5601 			    cmd);
5602 
5603 		list_move_tail(&cmd->cmd_list, &free_list);
5604 
5605 		/* piggy back on hardware_lock for protection */
5606 		vha->hw->tgt.num_qfull_cmds_alloc--;
5607 	}
5608 	spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
5609 
5610 	cmd = NULL;
5611 
5612 	list_for_each_entry_safe(cmd, tcmd, &free_list, cmd_list) {
5613 		list_del(&cmd->cmd_list);
5614 		/* This cmd was never sent to TCM.  There is no need
5615 		 * to schedule free or call free_cmd
5616 		 */
5617 		qlt_free_cmd(cmd);
5618 	}
5619 
5620 	if (!list_empty(&q_full_list)) {
5621 		spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
5622 		list_splice(&q_full_list, &vha->hw->tgt.q_full_list);
5623 		spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
5624 	}
5625 
5626 	return rc;
5627 }
5628 
5629 static void
5630 qlt_send_busy(struct qla_qpair *qpair, struct atio_from_isp *atio,
5631     uint16_t status)
5632 {
5633 	int rc = 0;
5634 	struct scsi_qla_host *vha = qpair->vha;
5635 
5636 	rc = __qlt_send_busy(qpair, atio, status);
5637 	if (rc == -ENOMEM)
5638 		qlt_alloc_qfull_cmd(vha, atio, status, 1);
5639 }
5640 
5641 static int
5642 qlt_chk_qfull_thresh_hold(struct scsi_qla_host *vha, struct qla_qpair *qpair,
5643 	struct atio_from_isp *atio, uint8_t ha_locked)
5644 {
5645 	struct qla_hw_data *ha = vha->hw;
5646 	unsigned long flags;
5647 
5648 	if (ha->tgt.num_pend_cmds < Q_FULL_THRESH_HOLD(ha))
5649 		return 0;
5650 
5651 	if (!ha_locked)
5652 		spin_lock_irqsave(&ha->hardware_lock, flags);
5653 	qlt_send_busy(qpair, atio, qla_sam_status);
5654 	if (!ha_locked)
5655 		spin_unlock_irqrestore(&ha->hardware_lock, flags);
5656 
5657 	return 1;
5658 }
5659 
5660 /* ha->hardware_lock supposed to be held on entry */
5661 /* called via callback from qla2xxx */
5662 static void qlt_24xx_atio_pkt(struct scsi_qla_host *vha,
5663 	struct atio_from_isp *atio, uint8_t ha_locked)
5664 {
5665 	struct qla_hw_data *ha = vha->hw;
5666 	struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5667 	int rc;
5668 	unsigned long flags = 0;
5669 
5670 	if (unlikely(tgt == NULL)) {
5671 		ql_dbg(ql_dbg_tgt, vha, 0x3064,
5672 		    "ATIO pkt, but no tgt (ha %p)", ha);
5673 		return;
5674 	}
5675 	/*
5676 	 * In tgt_stop mode we also should allow all requests to pass.
5677 	 * Otherwise, some commands can stuck.
5678 	 */
5679 
5680 	tgt->atio_irq_cmd_count++;
5681 
5682 	switch (atio->u.raw.entry_type) {
5683 	case ATIO_TYPE7:
5684 		if (unlikely(atio->u.isp24.exchange_addr ==
5685 			     cpu_to_le32(ATIO_EXCHANGE_ADDRESS_UNKNOWN))) {
5686 			ql_dbg(ql_dbg_io, vha, 0x3065,
5687 			    "qla_target(%d): ATIO_TYPE7 "
5688 			    "received with UNKNOWN exchange address, "
5689 			    "sending QUEUE_FULL\n", vha->vp_idx);
5690 			if (!ha_locked)
5691 				spin_lock_irqsave(&ha->hardware_lock, flags);
5692 			qlt_send_busy(ha->base_qpair, atio, qla_sam_status);
5693 			if (!ha_locked)
5694 				spin_unlock_irqrestore(&ha->hardware_lock,
5695 				    flags);
5696 			break;
5697 		}
5698 
5699 		if (likely(atio->u.isp24.fcp_cmnd.task_mgmt_flags == 0)) {
5700 			rc = qlt_chk_qfull_thresh_hold(vha, ha->base_qpair,
5701 			    atio, ha_locked);
5702 			if (rc != 0) {
5703 				tgt->atio_irq_cmd_count--;
5704 				return;
5705 			}
5706 			rc = qlt_handle_cmd_for_atio(vha, atio);
5707 		} else {
5708 			rc = qlt_handle_task_mgmt(vha, atio);
5709 		}
5710 		if (unlikely(rc != 0)) {
5711 			if (!ha_locked)
5712 				spin_lock_irqsave(&ha->hardware_lock, flags);
5713 			switch (rc) {
5714 			case -ENODEV:
5715 				ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5716 				    "qla_target: Unable to send command to target\n");
5717 				break;
5718 			case -EBADF:
5719 				ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5720 				    "qla_target: Unable to send command to target, sending TERM EXCHANGE for rsp\n");
5721 				qlt_send_term_exchange(ha->base_qpair, NULL,
5722 				    atio, 1, 0);
5723 				break;
5724 			case -EBUSY:
5725 				ql_dbg(ql_dbg_tgt, vha, 0xe060,
5726 				    "qla_target(%d): Unable to send command to target, sending BUSY status\n",
5727 				    vha->vp_idx);
5728 				qlt_send_busy(ha->base_qpair, atio,
5729 				    tc_sam_status);
5730 				break;
5731 			default:
5732 				ql_dbg(ql_dbg_tgt, vha, 0xe060,
5733 				    "qla_target(%d): Unable to send command to target, sending BUSY status\n",
5734 				    vha->vp_idx);
5735 				qlt_send_busy(ha->base_qpair, atio,
5736 				    qla_sam_status);
5737 				break;
5738 			}
5739 			if (!ha_locked)
5740 				spin_unlock_irqrestore(&ha->hardware_lock,
5741 				    flags);
5742 		}
5743 		break;
5744 
5745 	case IMMED_NOTIFY_TYPE:
5746 	{
5747 		if (unlikely(atio->u.isp2x.entry_status != 0)) {
5748 			ql_dbg(ql_dbg_tgt, vha, 0xe05b,
5749 			    "qla_target(%d): Received ATIO packet %x "
5750 			    "with error status %x\n", vha->vp_idx,
5751 			    atio->u.raw.entry_type,
5752 			    atio->u.isp2x.entry_status);
5753 			break;
5754 		}
5755 		ql_dbg(ql_dbg_tgt, vha, 0xe02e, "%s", "IMMED_NOTIFY ATIO");
5756 
5757 		if (!ha_locked)
5758 			spin_lock_irqsave(&ha->hardware_lock, flags);
5759 		qlt_handle_imm_notify(vha, (struct imm_ntfy_from_isp *)atio);
5760 		if (!ha_locked)
5761 			spin_unlock_irqrestore(&ha->hardware_lock, flags);
5762 		break;
5763 	}
5764 
5765 	default:
5766 		ql_dbg(ql_dbg_tgt, vha, 0xe05c,
5767 		    "qla_target(%d): Received unknown ATIO atio "
5768 		    "type %x\n", vha->vp_idx, atio->u.raw.entry_type);
5769 		break;
5770 	}
5771 
5772 	tgt->atio_irq_cmd_count--;
5773 }
5774 
5775 /*
5776  * qpair lock is assume to be held
5777  * rc = 0 : send terminate & abts respond
5778  * rc != 0: do not send term & abts respond
5779  */
5780 static int qlt_chk_unresolv_exchg(struct scsi_qla_host *vha,
5781     struct qla_qpair *qpair, struct abts_resp_from_24xx_fw *entry)
5782 {
5783 	struct qla_hw_data *ha = vha->hw;
5784 	int rc = 0;
5785 
5786 	/*
5787 	 * Detect unresolved exchange. If the same ABTS is unable
5788 	 * to terminate an existing command and the same ABTS loops
5789 	 * between FW & Driver, then force FW dump. Under 1 jiff,
5790 	 * we should see multiple loops.
5791 	 */
5792 	if (qpair->retry_term_exchg_addr == entry->exchange_addr_to_abort &&
5793 	    qpair->retry_term_jiff == jiffies) {
5794 		/* found existing exchange */
5795 		qpair->retry_term_cnt++;
5796 		if (qpair->retry_term_cnt >= 5) {
5797 			rc = -EIO;
5798 			qpair->retry_term_cnt = 0;
5799 			ql_log(ql_log_warn, vha, 0xffff,
5800 			    "Unable to send ABTS Respond. Dumping firmware.\n");
5801 			ql_dump_buffer(ql_dbg_tgt_mgt + ql_dbg_buffer,
5802 			    vha, 0xffff, (uint8_t *)entry, sizeof(*entry));
5803 
5804 			if (qpair == ha->base_qpair)
5805 				ha->isp_ops->fw_dump(vha);
5806 			else
5807 				qla2xxx_dump_fw(vha);
5808 
5809 			set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
5810 			qla2xxx_wake_dpc(vha);
5811 		}
5812 	} else if (qpair->retry_term_jiff != jiffies) {
5813 		qpair->retry_term_exchg_addr = entry->exchange_addr_to_abort;
5814 		qpair->retry_term_cnt = 0;
5815 		qpair->retry_term_jiff = jiffies;
5816 	}
5817 
5818 	return rc;
5819 }
5820 
5821 
5822 static void qlt_handle_abts_completion(struct scsi_qla_host *vha,
5823 	struct rsp_que *rsp, response_t *pkt)
5824 {
5825 	struct abts_resp_from_24xx_fw *entry =
5826 		(struct abts_resp_from_24xx_fw *)pkt;
5827 	u32 h = pkt->handle & ~QLA_TGT_HANDLE_MASK;
5828 	struct qla_tgt_mgmt_cmd *mcmd;
5829 	struct qla_hw_data *ha = vha->hw;
5830 
5831 	mcmd = qlt_ctio_to_cmd(vha, rsp, pkt->handle, pkt);
5832 	if (mcmd == NULL && h != QLA_TGT_SKIP_HANDLE) {
5833 		ql_dbg(ql_dbg_async, vha, 0xe064,
5834 		    "qla_target(%d): ABTS Comp without mcmd\n",
5835 		    vha->vp_idx);
5836 		return;
5837 	}
5838 
5839 	if (mcmd)
5840 		vha  = mcmd->vha;
5841 	vha->vha_tgt.qla_tgt->abts_resp_expected--;
5842 
5843 	ql_dbg(ql_dbg_tgt, vha, 0xe038,
5844 	    "ABTS_RESP_24XX: compl_status %x\n",
5845 	    entry->compl_status);
5846 
5847 	if (le16_to_cpu(entry->compl_status) != ABTS_RESP_COMPL_SUCCESS) {
5848 		if (le32_to_cpu(entry->error_subcode1) == 0x1E &&
5849 		    le32_to_cpu(entry->error_subcode2) == 0) {
5850 			if (qlt_chk_unresolv_exchg(vha, rsp->qpair, entry)) {
5851 				ha->tgt.tgt_ops->free_mcmd(mcmd);
5852 				return;
5853 			}
5854 			qlt_24xx_retry_term_exchange(vha, rsp->qpair,
5855 			    pkt, mcmd);
5856 		} else {
5857 			ql_dbg(ql_dbg_tgt, vha, 0xe063,
5858 			    "qla_target(%d): ABTS_RESP_24XX failed %x (subcode %x:%x)",
5859 			    vha->vp_idx, entry->compl_status,
5860 			    entry->error_subcode1,
5861 			    entry->error_subcode2);
5862 			ha->tgt.tgt_ops->free_mcmd(mcmd);
5863 		}
5864 	} else if (mcmd) {
5865 		ha->tgt.tgt_ops->free_mcmd(mcmd);
5866 	}
5867 }
5868 
5869 /* ha->hardware_lock supposed to be held on entry */
5870 /* called via callback from qla2xxx */
5871 static void qlt_response_pkt(struct scsi_qla_host *vha,
5872 	struct rsp_que *rsp, response_t *pkt)
5873 {
5874 	struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5875 
5876 	if (unlikely(tgt == NULL)) {
5877 		ql_dbg(ql_dbg_tgt, vha, 0xe05d,
5878 		    "qla_target(%d): Response pkt %x received, but no tgt (ha %p)\n",
5879 		    vha->vp_idx, pkt->entry_type, vha->hw);
5880 		return;
5881 	}
5882 
5883 	/*
5884 	 * In tgt_stop mode we also should allow all requests to pass.
5885 	 * Otherwise, some commands can stuck.
5886 	 */
5887 
5888 	switch (pkt->entry_type) {
5889 	case CTIO_CRC2:
5890 	case CTIO_TYPE7:
5891 	{
5892 		struct ctio7_from_24xx *entry = (struct ctio7_from_24xx *)pkt;
5893 
5894 		qlt_do_ctio_completion(vha, rsp, entry->handle,
5895 		    le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5896 		    entry);
5897 		break;
5898 	}
5899 
5900 	case ACCEPT_TGT_IO_TYPE:
5901 	{
5902 		struct atio_from_isp *atio = (struct atio_from_isp *)pkt;
5903 		int rc;
5904 
5905 		if (atio->u.isp2x.status !=
5906 		    cpu_to_le16(ATIO_CDB_VALID)) {
5907 			ql_dbg(ql_dbg_tgt, vha, 0xe05e,
5908 			    "qla_target(%d): ATIO with error "
5909 			    "status %x received\n", vha->vp_idx,
5910 			    le16_to_cpu(atio->u.isp2x.status));
5911 			break;
5912 		}
5913 
5914 		rc = qlt_chk_qfull_thresh_hold(vha, rsp->qpair, atio, 1);
5915 		if (rc != 0)
5916 			return;
5917 
5918 		rc = qlt_handle_cmd_for_atio(vha, atio);
5919 		if (unlikely(rc != 0)) {
5920 			switch (rc) {
5921 			case -ENODEV:
5922 				ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5923 				    "qla_target: Unable to send command to target\n");
5924 				break;
5925 			case -EBADF:
5926 				ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5927 				    "qla_target: Unable to send command to target, sending TERM EXCHANGE for rsp\n");
5928 				qlt_send_term_exchange(rsp->qpair, NULL,
5929 				    atio, 1, 0);
5930 				break;
5931 			case -EBUSY:
5932 				ql_dbg(ql_dbg_tgt, vha, 0xe060,
5933 				    "qla_target(%d): Unable to send command to target, sending BUSY status\n",
5934 				    vha->vp_idx);
5935 				qlt_send_busy(rsp->qpair, atio,
5936 				    tc_sam_status);
5937 				break;
5938 			default:
5939 				ql_dbg(ql_dbg_tgt, vha, 0xe060,
5940 				    "qla_target(%d): Unable to send command to target, sending BUSY status\n",
5941 				    vha->vp_idx);
5942 				qlt_send_busy(rsp->qpair, atio,
5943 				    qla_sam_status);
5944 				break;
5945 			}
5946 		}
5947 	}
5948 	break;
5949 
5950 	case CONTINUE_TGT_IO_TYPE:
5951 	{
5952 		struct ctio_to_2xxx *entry = (struct ctio_to_2xxx *)pkt;
5953 
5954 		qlt_do_ctio_completion(vha, rsp, entry->handle,
5955 		    le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5956 		    entry);
5957 		break;
5958 	}
5959 
5960 	case CTIO_A64_TYPE:
5961 	{
5962 		struct ctio_to_2xxx *entry = (struct ctio_to_2xxx *)pkt;
5963 
5964 		qlt_do_ctio_completion(vha, rsp, entry->handle,
5965 		    le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5966 		    entry);
5967 		break;
5968 	}
5969 
5970 	case IMMED_NOTIFY_TYPE:
5971 		ql_dbg(ql_dbg_tgt, vha, 0xe035, "%s", "IMMED_NOTIFY\n");
5972 		qlt_handle_imm_notify(vha, (struct imm_ntfy_from_isp *)pkt);
5973 		break;
5974 
5975 	case NOTIFY_ACK_TYPE:
5976 		if (tgt->notify_ack_expected > 0) {
5977 			struct nack_to_isp *entry = (struct nack_to_isp *)pkt;
5978 
5979 			ql_dbg(ql_dbg_tgt, vha, 0xe036,
5980 			    "NOTIFY_ACK seq %08x status %x\n",
5981 			    le16_to_cpu(entry->u.isp2x.seq_id),
5982 			    le16_to_cpu(entry->u.isp2x.status));
5983 			tgt->notify_ack_expected--;
5984 			if (entry->u.isp2x.status !=
5985 			    cpu_to_le16(NOTIFY_ACK_SUCCESS)) {
5986 				ql_dbg(ql_dbg_tgt, vha, 0xe061,
5987 				    "qla_target(%d): NOTIFY_ACK "
5988 				    "failed %x\n", vha->vp_idx,
5989 				    le16_to_cpu(entry->u.isp2x.status));
5990 			}
5991 		} else {
5992 			ql_dbg(ql_dbg_tgt, vha, 0xe062,
5993 			    "qla_target(%d): Unexpected NOTIFY_ACK received\n",
5994 			    vha->vp_idx);
5995 		}
5996 		break;
5997 
5998 	case ABTS_RECV_24XX:
5999 		ql_dbg(ql_dbg_tgt, vha, 0xe037,
6000 		    "ABTS_RECV_24XX: instance %d\n", vha->vp_idx);
6001 		qlt_24xx_handle_abts(vha, (struct abts_recv_from_24xx *)pkt);
6002 		break;
6003 
6004 	case ABTS_RESP_24XX:
6005 		if (tgt->abts_resp_expected > 0) {
6006 			qlt_handle_abts_completion(vha, rsp, pkt);
6007 		} else {
6008 			ql_dbg(ql_dbg_tgt, vha, 0xe064,
6009 			    "qla_target(%d): Unexpected ABTS_RESP_24XX "
6010 			    "received\n", vha->vp_idx);
6011 		}
6012 		break;
6013 
6014 	default:
6015 		ql_dbg(ql_dbg_tgt, vha, 0xe065,
6016 		    "qla_target(%d): Received unknown response pkt "
6017 		    "type %x\n", vha->vp_idx, pkt->entry_type);
6018 		break;
6019 	}
6020 
6021 }
6022 
6023 /*
6024  * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
6025  */
6026 void qlt_async_event(uint16_t code, struct scsi_qla_host *vha,
6027 	uint16_t *mailbox)
6028 {
6029 	struct qla_hw_data *ha = vha->hw;
6030 	struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
6031 	int login_code;
6032 
6033 	if (!tgt || tgt->tgt_stop || tgt->tgt_stopped)
6034 		return;
6035 
6036 	if (((code == MBA_POINT_TO_POINT) || (code == MBA_CHG_IN_CONNECTION)) &&
6037 	    IS_QLA2100(ha))
6038 		return;
6039 	/*
6040 	 * In tgt_stop mode we also should allow all requests to pass.
6041 	 * Otherwise, some commands can stuck.
6042 	 */
6043 
6044 
6045 	switch (code) {
6046 	case MBA_RESET:			/* Reset */
6047 	case MBA_SYSTEM_ERR:		/* System Error */
6048 	case MBA_REQ_TRANSFER_ERR:	/* Request Transfer Error */
6049 	case MBA_RSP_TRANSFER_ERR:	/* Response Transfer Error */
6050 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03a,
6051 		    "qla_target(%d): System error async event %#x "
6052 		    "occurred", vha->vp_idx, code);
6053 		break;
6054 	case MBA_WAKEUP_THRES:		/* Request Queue Wake-up. */
6055 		set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
6056 		break;
6057 
6058 	case MBA_LOOP_UP:
6059 	{
6060 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03b,
6061 		    "qla_target(%d): Async LOOP_UP occurred "
6062 		    "(m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)", vha->vp_idx,
6063 		    mailbox[0], mailbox[1], mailbox[2], mailbox[3]);
6064 		if (tgt->link_reinit_iocb_pending) {
6065 			qlt_send_notify_ack(ha->base_qpair,
6066 			    &tgt->link_reinit_iocb,
6067 			    0, 0, 0, 0, 0, 0);
6068 			tgt->link_reinit_iocb_pending = 0;
6069 		}
6070 		break;
6071 	}
6072 
6073 	case MBA_LIP_OCCURRED:
6074 	case MBA_LOOP_DOWN:
6075 	case MBA_LIP_RESET:
6076 	case MBA_RSCN_UPDATE:
6077 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03c,
6078 		    "qla_target(%d): Async event %#x occurred "
6079 		    "(m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)", vha->vp_idx, code,
6080 		    mailbox[0], mailbox[1], mailbox[2], mailbox[3]);
6081 		break;
6082 
6083 	case MBA_REJECTED_FCP_CMD:
6084 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf017,
6085 		    "qla_target(%d): Async event LS_REJECT occurred (m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)",
6086 		    vha->vp_idx,
6087 		    mailbox[0], mailbox[1], mailbox[2], mailbox[3]);
6088 
6089 		if (mailbox[3] == 1) {
6090 			/* exchange starvation. */
6091 			vha->hw->exch_starvation++;
6092 			if (vha->hw->exch_starvation > 5) {
6093 				ql_log(ql_log_warn, vha, 0xd03a,
6094 				    "Exchange starvation-. Resetting RISC\n");
6095 
6096 				vha->hw->exch_starvation = 0;
6097 				if (IS_P3P_TYPE(vha->hw))
6098 					set_bit(FCOE_CTX_RESET_NEEDED,
6099 					    &vha->dpc_flags);
6100 				else
6101 					set_bit(ISP_ABORT_NEEDED,
6102 					    &vha->dpc_flags);
6103 				qla2xxx_wake_dpc(vha);
6104 			}
6105 		}
6106 		break;
6107 
6108 	case MBA_PORT_UPDATE:
6109 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03d,
6110 		    "qla_target(%d): Port update async event %#x "
6111 		    "occurred: updating the ports database (m[0]=%x, m[1]=%x, "
6112 		    "m[2]=%x, m[3]=%x)", vha->vp_idx, code,
6113 		    mailbox[0], mailbox[1], mailbox[2], mailbox[3]);
6114 
6115 		login_code = mailbox[2];
6116 		if (login_code == 0x4) {
6117 			ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03e,
6118 			    "Async MB 2: Got PLOGI Complete\n");
6119 			vha->hw->exch_starvation = 0;
6120 		} else if (login_code == 0x7)
6121 			ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03f,
6122 			    "Async MB 2: Port Logged Out\n");
6123 		break;
6124 	default:
6125 		break;
6126 	}
6127 
6128 }
6129 
6130 static fc_port_t *qlt_get_port_database(struct scsi_qla_host *vha,
6131 	uint16_t loop_id)
6132 {
6133 	fc_port_t *fcport, *tfcp, *del;
6134 	int rc;
6135 	unsigned long flags;
6136 	u8 newfcport = 0;
6137 
6138 	fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
6139 	if (!fcport) {
6140 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06f,
6141 		    "qla_target(%d): Allocation of tmp FC port failed",
6142 		    vha->vp_idx);
6143 		return NULL;
6144 	}
6145 
6146 	fcport->loop_id = loop_id;
6147 
6148 	rc = qla24xx_gpdb_wait(vha, fcport, 0);
6149 	if (rc != QLA_SUCCESS) {
6150 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf070,
6151 		    "qla_target(%d): Failed to retrieve fcport "
6152 		    "information -- get_port_database() returned %x "
6153 		    "(loop_id=0x%04x)", vha->vp_idx, rc, loop_id);
6154 		kfree(fcport);
6155 		return NULL;
6156 	}
6157 
6158 	del = NULL;
6159 	spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
6160 	tfcp = qla2x00_find_fcport_by_wwpn(vha, fcport->port_name, 1);
6161 
6162 	if (tfcp) {
6163 		tfcp->d_id = fcport->d_id;
6164 		tfcp->port_type = fcport->port_type;
6165 		tfcp->supported_classes = fcport->supported_classes;
6166 		tfcp->flags |= fcport->flags;
6167 		tfcp->scan_state = QLA_FCPORT_FOUND;
6168 
6169 		del = fcport;
6170 		fcport = tfcp;
6171 	} else {
6172 		if (vha->hw->current_topology == ISP_CFG_F)
6173 			fcport->flags |= FCF_FABRIC_DEVICE;
6174 
6175 		list_add_tail(&fcport->list, &vha->vp_fcports);
6176 		if (!IS_SW_RESV_ADDR(fcport->d_id))
6177 		   vha->fcport_count++;
6178 		fcport->login_gen++;
6179 		qla2x00_set_fcport_disc_state(fcport, DSC_LOGIN_COMPLETE);
6180 		fcport->login_succ = 1;
6181 		newfcport = 1;
6182 	}
6183 
6184 	fcport->deleted = 0;
6185 	spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
6186 
6187 	switch (vha->host->active_mode) {
6188 	case MODE_INITIATOR:
6189 	case MODE_DUAL:
6190 		if (newfcport) {
6191 			if (!IS_IIDMA_CAPABLE(vha->hw) || !vha->hw->flags.gpsc_supported) {
6192 				qla24xx_sched_upd_fcport(fcport);
6193 			} else {
6194 				ql_dbg(ql_dbg_disc, vha, 0x20ff,
6195 				   "%s %d %8phC post gpsc fcp_cnt %d\n",
6196 				   __func__, __LINE__, fcport->port_name, vha->fcport_count);
6197 				qla24xx_post_gpsc_work(vha, fcport);
6198 			}
6199 		}
6200 		break;
6201 
6202 	case MODE_TARGET:
6203 	default:
6204 		break;
6205 	}
6206 	if (del)
6207 		qla2x00_free_fcport(del);
6208 
6209 	return fcport;
6210 }
6211 
6212 /* Must be called under tgt_mutex */
6213 static struct fc_port *qlt_make_local_sess(struct scsi_qla_host *vha,
6214 					   be_id_t s_id)
6215 {
6216 	struct fc_port *sess = NULL;
6217 	fc_port_t *fcport = NULL;
6218 	int rc, global_resets;
6219 	uint16_t loop_id = 0;
6220 
6221 	if (s_id.domain == 0xFF && s_id.area == 0xFC) {
6222 		/*
6223 		 * This is Domain Controller, so it should be
6224 		 * OK to drop SCSI commands from it.
6225 		 */
6226 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf042,
6227 		    "Unable to find initiator with S_ID %x:%x:%x",
6228 		    s_id.domain, s_id.area, s_id.al_pa);
6229 		return NULL;
6230 	}
6231 
6232 	mutex_lock(&vha->vha_tgt.tgt_mutex);
6233 
6234 retry:
6235 	global_resets =
6236 	    atomic_read(&vha->vha_tgt.qla_tgt->tgt_global_resets_count);
6237 
6238 	rc = qla24xx_get_loop_id(vha, s_id, &loop_id);
6239 	if (rc != 0) {
6240 		mutex_unlock(&vha->vha_tgt.tgt_mutex);
6241 
6242 		ql_log(ql_log_info, vha, 0xf071,
6243 		    "qla_target(%d): Unable to find "
6244 		    "initiator with S_ID %x:%x:%x",
6245 		    vha->vp_idx, s_id.domain, s_id.area, s_id.al_pa);
6246 
6247 		if (rc == -ENOENT) {
6248 			qlt_port_logo_t logo;
6249 
6250 			logo.id = be_to_port_id(s_id);
6251 			logo.cmd_count = 1;
6252 			qlt_send_first_logo(vha, &logo);
6253 		}
6254 
6255 		return NULL;
6256 	}
6257 
6258 	fcport = qlt_get_port_database(vha, loop_id);
6259 	if (!fcport) {
6260 		mutex_unlock(&vha->vha_tgt.tgt_mutex);
6261 		return NULL;
6262 	}
6263 
6264 	if (global_resets !=
6265 	    atomic_read(&vha->vha_tgt.qla_tgt->tgt_global_resets_count)) {
6266 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf043,
6267 		    "qla_target(%d): global reset during session discovery "
6268 		    "(counter was %d, new %d), retrying", vha->vp_idx,
6269 		    global_resets,
6270 		    atomic_read(&vha->vha_tgt.
6271 			qla_tgt->tgt_global_resets_count));
6272 		goto retry;
6273 	}
6274 
6275 	sess = qlt_create_sess(vha, fcport, true);
6276 
6277 	mutex_unlock(&vha->vha_tgt.tgt_mutex);
6278 
6279 	return sess;
6280 }
6281 
6282 static void qlt_abort_work(struct qla_tgt *tgt,
6283 	struct qla_tgt_sess_work_param *prm)
6284 {
6285 	struct scsi_qla_host *vha = tgt->vha;
6286 	struct qla_hw_data *ha = vha->hw;
6287 	struct fc_port *sess = NULL;
6288 	unsigned long flags = 0, flags2 = 0;
6289 	be_id_t s_id;
6290 	int rc;
6291 
6292 	spin_lock_irqsave(&ha->tgt.sess_lock, flags2);
6293 
6294 	if (tgt->tgt_stop)
6295 		goto out_term2;
6296 
6297 	s_id = le_id_to_be(prm->abts.fcp_hdr_le.s_id);
6298 
6299 	sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id);
6300 	if (!sess) {
6301 		spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
6302 
6303 		sess = qlt_make_local_sess(vha, s_id);
6304 		/* sess has got an extra creation ref */
6305 
6306 		spin_lock_irqsave(&ha->tgt.sess_lock, flags2);
6307 		if (!sess)
6308 			goto out_term2;
6309 	} else {
6310 		if (sess->deleted) {
6311 			sess = NULL;
6312 			goto out_term2;
6313 		}
6314 
6315 		if (!kref_get_unless_zero(&sess->sess_kref)) {
6316 			ql_dbg(ql_dbg_tgt_tmr, vha, 0xf01c,
6317 			    "%s: kref_get fail %8phC \n",
6318 			     __func__, sess->port_name);
6319 			sess = NULL;
6320 			goto out_term2;
6321 		}
6322 	}
6323 
6324 	rc = __qlt_24xx_handle_abts(vha, &prm->abts, sess);
6325 	spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
6326 
6327 	ha->tgt.tgt_ops->put_sess(sess);
6328 
6329 	if (rc != 0)
6330 		goto out_term;
6331 	return;
6332 
6333 out_term2:
6334 	spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
6335 
6336 out_term:
6337 	spin_lock_irqsave(&ha->hardware_lock, flags);
6338 	qlt_24xx_send_abts_resp(ha->base_qpair, &prm->abts,
6339 	    FCP_TMF_REJECTED, false);
6340 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
6341 }
6342 
6343 static void qlt_tmr_work(struct qla_tgt *tgt,
6344 	struct qla_tgt_sess_work_param *prm)
6345 {
6346 	struct atio_from_isp *a = &prm->tm_iocb2;
6347 	struct scsi_qla_host *vha = tgt->vha;
6348 	struct qla_hw_data *ha = vha->hw;
6349 	struct fc_port *sess;
6350 	unsigned long flags;
6351 	be_id_t s_id;
6352 	int rc;
6353 	u64 unpacked_lun;
6354 	int fn;
6355 	void *iocb;
6356 
6357 	spin_lock_irqsave(&ha->tgt.sess_lock, flags);
6358 
6359 	if (tgt->tgt_stop)
6360 		goto out_term2;
6361 
6362 	s_id = prm->tm_iocb2.u.isp24.fcp_hdr.s_id;
6363 	sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id);
6364 	if (!sess) {
6365 		spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
6366 
6367 		sess = qlt_make_local_sess(vha, s_id);
6368 		/* sess has got an extra creation ref */
6369 
6370 		spin_lock_irqsave(&ha->tgt.sess_lock, flags);
6371 		if (!sess)
6372 			goto out_term2;
6373 	} else {
6374 		if (sess->deleted) {
6375 			goto out_term2;
6376 		}
6377 
6378 		if (!kref_get_unless_zero(&sess->sess_kref)) {
6379 			ql_dbg(ql_dbg_tgt_tmr, vha, 0xf020,
6380 			    "%s: kref_get fail %8phC\n",
6381 			     __func__, sess->port_name);
6382 			goto out_term2;
6383 		}
6384 	}
6385 
6386 	iocb = a;
6387 	fn = a->u.isp24.fcp_cmnd.task_mgmt_flags;
6388 	unpacked_lun =
6389 	    scsilun_to_int((struct scsi_lun *)&a->u.isp24.fcp_cmnd.lun);
6390 
6391 	rc = qlt_issue_task_mgmt(sess, unpacked_lun, fn, iocb, 0);
6392 	spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
6393 
6394 	ha->tgt.tgt_ops->put_sess(sess);
6395 
6396 	if (rc != 0)
6397 		goto out_term;
6398 	return;
6399 
6400 out_term2:
6401 	spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
6402 out_term:
6403 	qlt_send_term_exchange(ha->base_qpair, NULL, &prm->tm_iocb2, 1, 0);
6404 }
6405 
6406 static void qlt_sess_work_fn(struct work_struct *work)
6407 {
6408 	struct qla_tgt *tgt = container_of(work, struct qla_tgt, sess_work);
6409 	struct scsi_qla_host *vha = tgt->vha;
6410 	unsigned long flags;
6411 
6412 	ql_dbg(ql_dbg_tgt_mgt, vha, 0xf000, "Sess work (tgt %p)", tgt);
6413 
6414 	spin_lock_irqsave(&tgt->sess_work_lock, flags);
6415 	while (!list_empty(&tgt->sess_works_list)) {
6416 		struct qla_tgt_sess_work_param *prm = list_entry(
6417 		    tgt->sess_works_list.next, typeof(*prm),
6418 		    sess_works_list_entry);
6419 
6420 		/*
6421 		 * This work can be scheduled on several CPUs at time, so we
6422 		 * must delete the entry to eliminate double processing
6423 		 */
6424 		list_del(&prm->sess_works_list_entry);
6425 
6426 		spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
6427 
6428 		switch (prm->type) {
6429 		case QLA_TGT_SESS_WORK_ABORT:
6430 			qlt_abort_work(tgt, prm);
6431 			break;
6432 		case QLA_TGT_SESS_WORK_TM:
6433 			qlt_tmr_work(tgt, prm);
6434 			break;
6435 		default:
6436 			BUG_ON(1);
6437 			break;
6438 		}
6439 
6440 		spin_lock_irqsave(&tgt->sess_work_lock, flags);
6441 
6442 		kfree(prm);
6443 	}
6444 	spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
6445 }
6446 
6447 /* Must be called under tgt_host_action_mutex */
6448 int qlt_add_target(struct qla_hw_data *ha, struct scsi_qla_host *base_vha)
6449 {
6450 	struct qla_tgt *tgt;
6451 	int rc, i;
6452 	struct qla_qpair_hint *h;
6453 
6454 	if (!QLA_TGT_MODE_ENABLED())
6455 		return 0;
6456 
6457 	if (!IS_TGT_MODE_CAPABLE(ha)) {
6458 		ql_log(ql_log_warn, base_vha, 0xe070,
6459 		    "This adapter does not support target mode.\n");
6460 		return 0;
6461 	}
6462 
6463 	ql_dbg(ql_dbg_tgt, base_vha, 0xe03b,
6464 	    "Registering target for host %ld(%p).\n", base_vha->host_no, ha);
6465 
6466 	BUG_ON(base_vha->vha_tgt.qla_tgt != NULL);
6467 
6468 	tgt = kzalloc(sizeof(struct qla_tgt), GFP_KERNEL);
6469 	if (!tgt) {
6470 		ql_dbg(ql_dbg_tgt, base_vha, 0xe066,
6471 		    "Unable to allocate struct qla_tgt\n");
6472 		return -ENOMEM;
6473 	}
6474 
6475 	tgt->qphints = kcalloc(ha->max_qpairs + 1,
6476 			       sizeof(struct qla_qpair_hint),
6477 			       GFP_KERNEL);
6478 	if (!tgt->qphints) {
6479 		kfree(tgt);
6480 		ql_log(ql_log_warn, base_vha, 0x0197,
6481 		    "Unable to allocate qpair hints.\n");
6482 		return -ENOMEM;
6483 	}
6484 
6485 	if (!(base_vha->host->hostt->supported_mode & MODE_TARGET))
6486 		base_vha->host->hostt->supported_mode |= MODE_TARGET;
6487 
6488 	rc = btree_init64(&tgt->lun_qpair_map);
6489 	if (rc) {
6490 		kfree(tgt->qphints);
6491 		kfree(tgt);
6492 		ql_log(ql_log_info, base_vha, 0x0198,
6493 			"Unable to initialize lun_qpair_map btree\n");
6494 		return -EIO;
6495 	}
6496 	h = &tgt->qphints[0];
6497 	h->qpair = ha->base_qpair;
6498 	INIT_LIST_HEAD(&h->hint_elem);
6499 	h->cpuid = ha->base_qpair->cpuid;
6500 	list_add_tail(&h->hint_elem, &ha->base_qpair->hints_list);
6501 
6502 	for (i = 0; i < ha->max_qpairs; i++) {
6503 		unsigned long flags;
6504 
6505 		struct qla_qpair *qpair = ha->queue_pair_map[i];
6506 
6507 		h = &tgt->qphints[i + 1];
6508 		INIT_LIST_HEAD(&h->hint_elem);
6509 		if (qpair) {
6510 			h->qpair = qpair;
6511 			spin_lock_irqsave(qpair->qp_lock_ptr, flags);
6512 			list_add_tail(&h->hint_elem, &qpair->hints_list);
6513 			spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
6514 			h->cpuid = qpair->cpuid;
6515 		}
6516 	}
6517 
6518 	tgt->ha = ha;
6519 	tgt->vha = base_vha;
6520 	init_waitqueue_head(&tgt->waitQ);
6521 	INIT_LIST_HEAD(&tgt->del_sess_list);
6522 	spin_lock_init(&tgt->sess_work_lock);
6523 	INIT_WORK(&tgt->sess_work, qlt_sess_work_fn);
6524 	INIT_LIST_HEAD(&tgt->sess_works_list);
6525 	atomic_set(&tgt->tgt_global_resets_count, 0);
6526 
6527 	base_vha->vha_tgt.qla_tgt = tgt;
6528 
6529 	ql_dbg(ql_dbg_tgt, base_vha, 0xe067,
6530 		"qla_target(%d): using 64 Bit PCI addressing",
6531 		base_vha->vp_idx);
6532 	/* 3 is reserved */
6533 	tgt->sg_tablesize = QLA_TGT_MAX_SG_24XX(base_vha->req->length - 3);
6534 
6535 	mutex_lock(&qla_tgt_mutex);
6536 	list_add_tail(&tgt->tgt_list_entry, &qla_tgt_glist);
6537 	mutex_unlock(&qla_tgt_mutex);
6538 
6539 	if (ha->tgt.tgt_ops && ha->tgt.tgt_ops->add_target)
6540 		ha->tgt.tgt_ops->add_target(base_vha);
6541 
6542 	return 0;
6543 }
6544 
6545 /* Must be called under tgt_host_action_mutex */
6546 int qlt_remove_target(struct qla_hw_data *ha, struct scsi_qla_host *vha)
6547 {
6548 	if (!vha->vha_tgt.qla_tgt)
6549 		return 0;
6550 
6551 	if (vha->fc_vport) {
6552 		qlt_release(vha->vha_tgt.qla_tgt);
6553 		return 0;
6554 	}
6555 
6556 	/* free left over qfull cmds */
6557 	qlt_init_term_exchange(vha);
6558 
6559 	ql_dbg(ql_dbg_tgt, vha, 0xe03c, "Unregistering target for host %ld(%p)",
6560 	    vha->host_no, ha);
6561 	qlt_release(vha->vha_tgt.qla_tgt);
6562 
6563 	return 0;
6564 }
6565 
6566 void qla_remove_hostmap(struct qla_hw_data *ha)
6567 {
6568 	struct scsi_qla_host *node;
6569 	u32 key = 0;
6570 
6571 	btree_for_each_safe32(&ha->host_map, key, node)
6572 		btree_remove32(&ha->host_map, key);
6573 
6574 	btree_destroy32(&ha->host_map);
6575 }
6576 
6577 static void qlt_lport_dump(struct scsi_qla_host *vha, u64 wwpn,
6578 	unsigned char *b)
6579 {
6580 	pr_debug("qla2xxx HW vha->node_name: %8phC\n", vha->node_name);
6581 	pr_debug("qla2xxx HW vha->port_name: %8phC\n", vha->port_name);
6582 	put_unaligned_be64(wwpn, b);
6583 	pr_debug("qla2xxx passed configfs WWPN: %8phC\n", b);
6584 }
6585 
6586 /**
6587  * qlt_lport_register - register lport with external module
6588  *
6589  * @target_lport_ptr: pointer for tcm_qla2xxx specific lport data
6590  * @phys_wwpn: physical port WWPN
6591  * @npiv_wwpn: NPIV WWPN
6592  * @npiv_wwnn: NPIV WWNN
6593  * @callback:  lport initialization callback for tcm_qla2xxx code
6594  */
6595 int qlt_lport_register(void *target_lport_ptr, u64 phys_wwpn,
6596 		       u64 npiv_wwpn, u64 npiv_wwnn,
6597 		       int (*callback)(struct scsi_qla_host *, void *, u64, u64))
6598 {
6599 	struct qla_tgt *tgt;
6600 	struct scsi_qla_host *vha;
6601 	struct qla_hw_data *ha;
6602 	struct Scsi_Host *host;
6603 	unsigned long flags;
6604 	int rc;
6605 	u8 b[WWN_SIZE];
6606 
6607 	mutex_lock(&qla_tgt_mutex);
6608 	list_for_each_entry(tgt, &qla_tgt_glist, tgt_list_entry) {
6609 		vha = tgt->vha;
6610 		ha = vha->hw;
6611 
6612 		host = vha->host;
6613 		if (!host)
6614 			continue;
6615 
6616 		if (!(host->hostt->supported_mode & MODE_TARGET))
6617 			continue;
6618 
6619 		if (vha->qlini_mode == QLA2XXX_INI_MODE_ENABLED)
6620 			continue;
6621 
6622 		spin_lock_irqsave(&ha->hardware_lock, flags);
6623 		if ((!npiv_wwpn || !npiv_wwnn) && host->active_mode & MODE_TARGET) {
6624 			pr_debug("MODE_TARGET already active on qla2xxx(%d)\n",
6625 			    host->host_no);
6626 			spin_unlock_irqrestore(&ha->hardware_lock, flags);
6627 			continue;
6628 		}
6629 		if (tgt->tgt_stop) {
6630 			pr_debug("MODE_TARGET in shutdown on qla2xxx(%d)\n",
6631 				 host->host_no);
6632 			spin_unlock_irqrestore(&ha->hardware_lock, flags);
6633 			continue;
6634 		}
6635 		spin_unlock_irqrestore(&ha->hardware_lock, flags);
6636 
6637 		if (!scsi_host_get(host)) {
6638 			ql_dbg(ql_dbg_tgt, vha, 0xe068,
6639 			    "Unable to scsi_host_get() for"
6640 			    " qla2xxx scsi_host\n");
6641 			continue;
6642 		}
6643 		qlt_lport_dump(vha, phys_wwpn, b);
6644 
6645 		if (memcmp(vha->port_name, b, WWN_SIZE)) {
6646 			scsi_host_put(host);
6647 			continue;
6648 		}
6649 		rc = (*callback)(vha, target_lport_ptr, npiv_wwpn, npiv_wwnn);
6650 		if (rc != 0)
6651 			scsi_host_put(host);
6652 
6653 		mutex_unlock(&qla_tgt_mutex);
6654 		return rc;
6655 	}
6656 	mutex_unlock(&qla_tgt_mutex);
6657 
6658 	return -ENODEV;
6659 }
6660 EXPORT_SYMBOL(qlt_lport_register);
6661 
6662 /**
6663  * qlt_lport_deregister - Degister lport
6664  *
6665  * @vha:  Registered scsi_qla_host pointer
6666  */
6667 void qlt_lport_deregister(struct scsi_qla_host *vha)
6668 {
6669 	struct qla_hw_data *ha = vha->hw;
6670 	struct Scsi_Host *sh = vha->host;
6671 	/*
6672 	 * Clear the target_lport_ptr qla_target_template pointer in qla_hw_data
6673 	 */
6674 	vha->vha_tgt.target_lport_ptr = NULL;
6675 	ha->tgt.tgt_ops = NULL;
6676 	/*
6677 	 * Release the Scsi_Host reference for the underlying qla2xxx host
6678 	 */
6679 	scsi_host_put(sh);
6680 }
6681 EXPORT_SYMBOL(qlt_lport_deregister);
6682 
6683 /* Must be called under HW lock */
6684 void qlt_set_mode(struct scsi_qla_host *vha)
6685 {
6686 	switch (vha->qlini_mode) {
6687 	case QLA2XXX_INI_MODE_DISABLED:
6688 	case QLA2XXX_INI_MODE_EXCLUSIVE:
6689 		vha->host->active_mode = MODE_TARGET;
6690 		break;
6691 	case QLA2XXX_INI_MODE_ENABLED:
6692 		vha->host->active_mode = MODE_INITIATOR;
6693 		break;
6694 	case QLA2XXX_INI_MODE_DUAL:
6695 		vha->host->active_mode = MODE_DUAL;
6696 		break;
6697 	default:
6698 		break;
6699 	}
6700 }
6701 
6702 /* Must be called under HW lock */
6703 static void qlt_clear_mode(struct scsi_qla_host *vha)
6704 {
6705 	switch (vha->qlini_mode) {
6706 	case QLA2XXX_INI_MODE_DISABLED:
6707 		vha->host->active_mode = MODE_UNKNOWN;
6708 		break;
6709 	case QLA2XXX_INI_MODE_EXCLUSIVE:
6710 		vha->host->active_mode = MODE_INITIATOR;
6711 		break;
6712 	case QLA2XXX_INI_MODE_ENABLED:
6713 	case QLA2XXX_INI_MODE_DUAL:
6714 		vha->host->active_mode = MODE_INITIATOR;
6715 		break;
6716 	default:
6717 		break;
6718 	}
6719 }
6720 
6721 /*
6722  * qla_tgt_enable_vha - NO LOCK HELD
6723  *
6724  * host_reset, bring up w/ Target Mode Enabled
6725  */
6726 void
6727 qlt_enable_vha(struct scsi_qla_host *vha)
6728 {
6729 	struct qla_hw_data *ha = vha->hw;
6730 	struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
6731 	unsigned long flags;
6732 	scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
6733 
6734 	if (!tgt) {
6735 		ql_dbg(ql_dbg_tgt, vha, 0xe069,
6736 		    "Unable to locate qla_tgt pointer from"
6737 		    " struct qla_hw_data\n");
6738 		dump_stack();
6739 		return;
6740 	}
6741 	if (vha->qlini_mode == QLA2XXX_INI_MODE_ENABLED)
6742 		return;
6743 
6744 	if (ha->tgt.num_act_qpairs > ha->max_qpairs)
6745 		ha->tgt.num_act_qpairs = ha->max_qpairs;
6746 	spin_lock_irqsave(&ha->hardware_lock, flags);
6747 	tgt->tgt_stopped = 0;
6748 	qlt_set_mode(vha);
6749 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
6750 
6751 	mutex_lock(&ha->optrom_mutex);
6752 	ql_dbg(ql_dbg_tgt_mgt, vha, 0xf021,
6753 	    "%s.\n", __func__);
6754 	if (vha->vp_idx) {
6755 		qla24xx_disable_vp(vha);
6756 		qla24xx_enable_vp(vha);
6757 	} else {
6758 		set_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags);
6759 		qla2xxx_wake_dpc(base_vha);
6760 		WARN_ON_ONCE(qla2x00_wait_for_hba_online(base_vha) !=
6761 			     QLA_SUCCESS);
6762 	}
6763 	mutex_unlock(&ha->optrom_mutex);
6764 }
6765 EXPORT_SYMBOL(qlt_enable_vha);
6766 
6767 /*
6768  * qla_tgt_disable_vha - NO LOCK HELD
6769  *
6770  * Disable Target Mode and reset the adapter
6771  */
6772 static void qlt_disable_vha(struct scsi_qla_host *vha)
6773 {
6774 	struct qla_hw_data *ha = vha->hw;
6775 	struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
6776 	unsigned long flags;
6777 
6778 	if (!tgt) {
6779 		ql_dbg(ql_dbg_tgt, vha, 0xe06a,
6780 		    "Unable to locate qla_tgt pointer from"
6781 		    " struct qla_hw_data\n");
6782 		dump_stack();
6783 		return;
6784 	}
6785 
6786 	spin_lock_irqsave(&ha->hardware_lock, flags);
6787 	qlt_clear_mode(vha);
6788 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
6789 
6790 	set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
6791 	qla2xxx_wake_dpc(vha);
6792 
6793 	/*
6794 	 * We are expecting the offline state.
6795 	 * QLA_FUNCTION_FAILED means that adapter is offline.
6796 	 */
6797 	if (qla2x00_wait_for_hba_online(vha) != QLA_SUCCESS)
6798 		ql_dbg(ql_dbg_tgt, vha, 0xe081,
6799 		       "adapter is offline\n");
6800 }
6801 
6802 /*
6803  * Called from qla_init.c:qla24xx_vport_create() contex to setup
6804  * the target mode specific struct scsi_qla_host and struct qla_hw_data
6805  * members.
6806  */
6807 void
6808 qlt_vport_create(struct scsi_qla_host *vha, struct qla_hw_data *ha)
6809 {
6810 	vha->vha_tgt.qla_tgt = NULL;
6811 
6812 	mutex_init(&vha->vha_tgt.tgt_mutex);
6813 	mutex_init(&vha->vha_tgt.tgt_host_action_mutex);
6814 
6815 	qlt_clear_mode(vha);
6816 
6817 	/*
6818 	 * NOTE: Currently the value is kept the same for <24xx and
6819 	 * >=24xx ISPs. If it is necessary to change it,
6820 	 * the check should be added for specific ISPs,
6821 	 * assigning the value appropriately.
6822 	 */
6823 	ha->tgt.atio_q_length = ATIO_ENTRY_CNT_24XX;
6824 
6825 	qlt_add_target(ha, vha);
6826 }
6827 
6828 u8
6829 qlt_rff_id(struct scsi_qla_host *vha)
6830 {
6831 	u8 fc4_feature = 0;
6832 	/*
6833 	 * FC-4 Feature bit 0 indicates target functionality to the name server.
6834 	 */
6835 	if (qla_tgt_mode_enabled(vha)) {
6836 		fc4_feature = BIT_0;
6837 	} else if (qla_ini_mode_enabled(vha)) {
6838 		fc4_feature = BIT_1;
6839 	} else if (qla_dual_mode_enabled(vha))
6840 		fc4_feature = BIT_0 | BIT_1;
6841 
6842 	return fc4_feature;
6843 }
6844 
6845 /*
6846  * qlt_init_atio_q_entries() - Initializes ATIO queue entries.
6847  * @ha: HA context
6848  *
6849  * Beginning of ATIO ring has initialization control block already built
6850  * by nvram config routine.
6851  *
6852  * Returns 0 on success.
6853  */
6854 void
6855 qlt_init_atio_q_entries(struct scsi_qla_host *vha)
6856 {
6857 	struct qla_hw_data *ha = vha->hw;
6858 	uint16_t cnt;
6859 	struct atio_from_isp *pkt = (struct atio_from_isp *)ha->tgt.atio_ring;
6860 
6861 	if (qla_ini_mode_enabled(vha))
6862 		return;
6863 
6864 	for (cnt = 0; cnt < ha->tgt.atio_q_length; cnt++) {
6865 		pkt->u.raw.signature = cpu_to_le32(ATIO_PROCESSED);
6866 		pkt++;
6867 	}
6868 
6869 }
6870 
6871 /*
6872  * qlt_24xx_process_atio_queue() - Process ATIO queue entries.
6873  * @ha: SCSI driver HA context
6874  */
6875 void
6876 qlt_24xx_process_atio_queue(struct scsi_qla_host *vha, uint8_t ha_locked)
6877 {
6878 	struct qla_hw_data *ha = vha->hw;
6879 	struct atio_from_isp *pkt;
6880 	int cnt, i;
6881 
6882 	if (!ha->flags.fw_started)
6883 		return;
6884 
6885 	while ((ha->tgt.atio_ring_ptr->signature != ATIO_PROCESSED) ||
6886 	    fcpcmd_is_corrupted(ha->tgt.atio_ring_ptr)) {
6887 		pkt = (struct atio_from_isp *)ha->tgt.atio_ring_ptr;
6888 		cnt = pkt->u.raw.entry_count;
6889 
6890 		if (unlikely(fcpcmd_is_corrupted(ha->tgt.atio_ring_ptr))) {
6891 			/*
6892 			 * This packet is corrupted. The header + payload
6893 			 * can not be trusted. There is no point in passing
6894 			 * it further up.
6895 			 */
6896 			ql_log(ql_log_warn, vha, 0xd03c,
6897 			    "corrupted fcp frame SID[%3phN] OXID[%04x] EXCG[%x] %64phN\n",
6898 			    &pkt->u.isp24.fcp_hdr.s_id,
6899 			    be16_to_cpu(pkt->u.isp24.fcp_hdr.ox_id),
6900 			    pkt->u.isp24.exchange_addr, pkt);
6901 
6902 			adjust_corrupted_atio(pkt);
6903 			qlt_send_term_exchange(ha->base_qpair, NULL, pkt,
6904 			    ha_locked, 0);
6905 		} else {
6906 			qlt_24xx_atio_pkt_all_vps(vha,
6907 			    (struct atio_from_isp *)pkt, ha_locked);
6908 		}
6909 
6910 		for (i = 0; i < cnt; i++) {
6911 			ha->tgt.atio_ring_index++;
6912 			if (ha->tgt.atio_ring_index == ha->tgt.atio_q_length) {
6913 				ha->tgt.atio_ring_index = 0;
6914 				ha->tgt.atio_ring_ptr = ha->tgt.atio_ring;
6915 			} else
6916 				ha->tgt.atio_ring_ptr++;
6917 
6918 			pkt->u.raw.signature = cpu_to_le32(ATIO_PROCESSED);
6919 			pkt = (struct atio_from_isp *)ha->tgt.atio_ring_ptr;
6920 		}
6921 		wmb();
6922 	}
6923 
6924 	/* Adjust ring index */
6925 	wrt_reg_dword(ISP_ATIO_Q_OUT(vha), ha->tgt.atio_ring_index);
6926 }
6927 
6928 void
6929 qlt_24xx_config_rings(struct scsi_qla_host *vha)
6930 {
6931 	struct qla_hw_data *ha = vha->hw;
6932 	struct qla_msix_entry *msix = &ha->msix_entries[2];
6933 	struct init_cb_24xx *icb = (struct init_cb_24xx *)ha->init_cb;
6934 
6935 	if (!QLA_TGT_MODE_ENABLED())
6936 		return;
6937 
6938 	wrt_reg_dword(ISP_ATIO_Q_IN(vha), 0);
6939 	wrt_reg_dword(ISP_ATIO_Q_OUT(vha), 0);
6940 	rd_reg_dword(ISP_ATIO_Q_OUT(vha));
6941 
6942 	if (ha->flags.msix_enabled) {
6943 		if (IS_QLA83XX(ha) || IS_QLA27XX(ha) || IS_QLA28XX(ha)) {
6944 			if (IS_QLA2071(ha)) {
6945 				/* 4 ports Baker: Enable Interrupt Handshake */
6946 				icb->msix_atio = 0;
6947 				icb->firmware_options_2 |= cpu_to_le32(BIT_26);
6948 			} else {
6949 				icb->msix_atio = cpu_to_le16(msix->entry);
6950 				icb->firmware_options_2 &= cpu_to_le32(~BIT_26);
6951 			}
6952 			ql_dbg(ql_dbg_init, vha, 0xf072,
6953 			    "Registering ICB vector 0x%x for atio que.\n",
6954 			    msix->entry);
6955 		}
6956 	} else {
6957 		/* INTx|MSI */
6958 		if (IS_QLA83XX(ha) || IS_QLA27XX(ha) || IS_QLA28XX(ha)) {
6959 			icb->msix_atio = 0;
6960 			icb->firmware_options_2 |= cpu_to_le32(BIT_26);
6961 			ql_dbg(ql_dbg_init, vha, 0xf072,
6962 			    "%s: Use INTx for ATIOQ.\n", __func__);
6963 		}
6964 	}
6965 }
6966 
6967 void
6968 qlt_24xx_config_nvram_stage1(struct scsi_qla_host *vha, struct nvram_24xx *nv)
6969 {
6970 	struct qla_hw_data *ha = vha->hw;
6971 	u32 tmp;
6972 
6973 	if (!QLA_TGT_MODE_ENABLED())
6974 		return;
6975 
6976 	if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)) {
6977 		if (!ha->tgt.saved_set) {
6978 			/* We save only once */
6979 			ha->tgt.saved_exchange_count = nv->exchange_count;
6980 			ha->tgt.saved_firmware_options_1 =
6981 			    nv->firmware_options_1;
6982 			ha->tgt.saved_firmware_options_2 =
6983 			    nv->firmware_options_2;
6984 			ha->tgt.saved_firmware_options_3 =
6985 			    nv->firmware_options_3;
6986 			ha->tgt.saved_set = 1;
6987 		}
6988 
6989 		if (qla_tgt_mode_enabled(vha))
6990 			nv->exchange_count = cpu_to_le16(0xFFFF);
6991 		else			/* dual */
6992 			nv->exchange_count = cpu_to_le16(vha->ql2xexchoffld);
6993 
6994 		/* Enable target mode */
6995 		nv->firmware_options_1 |= cpu_to_le32(BIT_4);
6996 
6997 		/* Disable ini mode, if requested */
6998 		if (qla_tgt_mode_enabled(vha))
6999 			nv->firmware_options_1 |= cpu_to_le32(BIT_5);
7000 
7001 		/* Disable Full Login after LIP */
7002 		nv->firmware_options_1 &= cpu_to_le32(~BIT_13);
7003 		/* Enable initial LIP */
7004 		nv->firmware_options_1 &= cpu_to_le32(~BIT_9);
7005 		if (ql2xtgt_tape_enable)
7006 			/* Enable FC Tape support */
7007 			nv->firmware_options_2 |= cpu_to_le32(BIT_12);
7008 		else
7009 			/* Disable FC Tape support */
7010 			nv->firmware_options_2 &= cpu_to_le32(~BIT_12);
7011 
7012 		/* Disable Full Login after LIP */
7013 		nv->host_p &= cpu_to_le32(~BIT_10);
7014 
7015 		/*
7016 		 * clear BIT 15 explicitly as we have seen at least
7017 		 * a couple of instances where this was set and this
7018 		 * was causing the firmware to not be initialized.
7019 		 */
7020 		nv->firmware_options_1 &= cpu_to_le32(~BIT_15);
7021 		/* Enable target PRLI control */
7022 		nv->firmware_options_2 |= cpu_to_le32(BIT_14);
7023 
7024 		if (IS_QLA25XX(ha)) {
7025 			/* Change Loop-prefer to Pt-Pt */
7026 			tmp = ~(BIT_4|BIT_5|BIT_6);
7027 			nv->firmware_options_2 &= cpu_to_le32(tmp);
7028 			tmp = P2P << 4;
7029 			nv->firmware_options_2 |= cpu_to_le32(tmp);
7030 		}
7031 	} else {
7032 		if (ha->tgt.saved_set) {
7033 			nv->exchange_count = ha->tgt.saved_exchange_count;
7034 			nv->firmware_options_1 =
7035 			    ha->tgt.saved_firmware_options_1;
7036 			nv->firmware_options_2 =
7037 			    ha->tgt.saved_firmware_options_2;
7038 			nv->firmware_options_3 =
7039 			    ha->tgt.saved_firmware_options_3;
7040 		}
7041 		return;
7042 	}
7043 
7044 	if (ha->base_qpair->enable_class_2) {
7045 		if (vha->flags.init_done)
7046 			fc_host_supported_classes(vha->host) =
7047 				FC_COS_CLASS2 | FC_COS_CLASS3;
7048 
7049 		nv->firmware_options_2 |= cpu_to_le32(BIT_8);
7050 	} else {
7051 		if (vha->flags.init_done)
7052 			fc_host_supported_classes(vha->host) = FC_COS_CLASS3;
7053 
7054 		nv->firmware_options_2 &= ~cpu_to_le32(BIT_8);
7055 	}
7056 }
7057 
7058 void
7059 qlt_24xx_config_nvram_stage2(struct scsi_qla_host *vha,
7060 	struct init_cb_24xx *icb)
7061 {
7062 	struct qla_hw_data *ha = vha->hw;
7063 
7064 	if (!QLA_TGT_MODE_ENABLED())
7065 		return;
7066 
7067 	if (ha->tgt.node_name_set) {
7068 		memcpy(icb->node_name, ha->tgt.tgt_node_name, WWN_SIZE);
7069 		icb->firmware_options_1 |= cpu_to_le32(BIT_14);
7070 	}
7071 }
7072 
7073 void
7074 qlt_81xx_config_nvram_stage1(struct scsi_qla_host *vha, struct nvram_81xx *nv)
7075 {
7076 	struct qla_hw_data *ha = vha->hw;
7077 	u32 tmp;
7078 
7079 	if (!QLA_TGT_MODE_ENABLED())
7080 		return;
7081 
7082 	if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)) {
7083 		if (!ha->tgt.saved_set) {
7084 			/* We save only once */
7085 			ha->tgt.saved_exchange_count = nv->exchange_count;
7086 			ha->tgt.saved_firmware_options_1 =
7087 			    nv->firmware_options_1;
7088 			ha->tgt.saved_firmware_options_2 =
7089 			    nv->firmware_options_2;
7090 			ha->tgt.saved_firmware_options_3 =
7091 			    nv->firmware_options_3;
7092 			ha->tgt.saved_set = 1;
7093 		}
7094 
7095 		if (qla_tgt_mode_enabled(vha))
7096 			nv->exchange_count = cpu_to_le16(0xFFFF);
7097 		else			/* dual */
7098 			nv->exchange_count = cpu_to_le16(vha->ql2xexchoffld);
7099 
7100 		/* Enable target mode */
7101 		nv->firmware_options_1 |= cpu_to_le32(BIT_4);
7102 
7103 		/* Disable ini mode, if requested */
7104 		if (qla_tgt_mode_enabled(vha))
7105 			nv->firmware_options_1 |= cpu_to_le32(BIT_5);
7106 		/* Disable Full Login after LIP */
7107 		nv->firmware_options_1 &= cpu_to_le32(~BIT_13);
7108 		/* Enable initial LIP */
7109 		nv->firmware_options_1 &= cpu_to_le32(~BIT_9);
7110 		/*
7111 		 * clear BIT 15 explicitly as we have seen at
7112 		 * least a couple of instances where this was set
7113 		 * and this was causing the firmware to not be
7114 		 * initialized.
7115 		 */
7116 		nv->firmware_options_1 &= cpu_to_le32(~BIT_15);
7117 		if (ql2xtgt_tape_enable)
7118 			/* Enable FC tape support */
7119 			nv->firmware_options_2 |= cpu_to_le32(BIT_12);
7120 		else
7121 			/* Disable FC tape support */
7122 			nv->firmware_options_2 &= cpu_to_le32(~BIT_12);
7123 
7124 		/* Disable Full Login after LIP */
7125 		nv->host_p &= cpu_to_le32(~BIT_10);
7126 		/* Enable target PRLI control */
7127 		nv->firmware_options_2 |= cpu_to_le32(BIT_14);
7128 
7129 		/* Change Loop-prefer to Pt-Pt */
7130 		tmp = ~(BIT_4|BIT_5|BIT_6);
7131 		nv->firmware_options_2 &= cpu_to_le32(tmp);
7132 		tmp = P2P << 4;
7133 		nv->firmware_options_2 |= cpu_to_le32(tmp);
7134 	} else {
7135 		if (ha->tgt.saved_set) {
7136 			nv->exchange_count = ha->tgt.saved_exchange_count;
7137 			nv->firmware_options_1 =
7138 			    ha->tgt.saved_firmware_options_1;
7139 			nv->firmware_options_2 =
7140 			    ha->tgt.saved_firmware_options_2;
7141 			nv->firmware_options_3 =
7142 			    ha->tgt.saved_firmware_options_3;
7143 		}
7144 		return;
7145 	}
7146 
7147 	if (ha->base_qpair->enable_class_2) {
7148 		if (vha->flags.init_done)
7149 			fc_host_supported_classes(vha->host) =
7150 				FC_COS_CLASS2 | FC_COS_CLASS3;
7151 
7152 		nv->firmware_options_2 |= cpu_to_le32(BIT_8);
7153 	} else {
7154 		if (vha->flags.init_done)
7155 			fc_host_supported_classes(vha->host) = FC_COS_CLASS3;
7156 
7157 		nv->firmware_options_2 &= ~cpu_to_le32(BIT_8);
7158 	}
7159 }
7160 
7161 void
7162 qlt_81xx_config_nvram_stage2(struct scsi_qla_host *vha,
7163 	struct init_cb_81xx *icb)
7164 {
7165 	struct qla_hw_data *ha = vha->hw;
7166 
7167 	if (!QLA_TGT_MODE_ENABLED())
7168 		return;
7169 
7170 	if (ha->tgt.node_name_set) {
7171 		memcpy(icb->node_name, ha->tgt.tgt_node_name, WWN_SIZE);
7172 		icb->firmware_options_1 |= cpu_to_le32(BIT_14);
7173 	}
7174 }
7175 
7176 void
7177 qlt_83xx_iospace_config(struct qla_hw_data *ha)
7178 {
7179 	if (!QLA_TGT_MODE_ENABLED())
7180 		return;
7181 
7182 	ha->msix_count += 1; /* For ATIO Q */
7183 }
7184 
7185 
7186 void
7187 qlt_modify_vp_config(struct scsi_qla_host *vha,
7188 	struct vp_config_entry_24xx *vpmod)
7189 {
7190 	/* enable target mode.  Bit5 = 1 => disable */
7191 	if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha))
7192 		vpmod->options_idx1 &= ~BIT_5;
7193 
7194 	/* Disable ini mode, if requested.  bit4 = 1 => disable */
7195 	if (qla_tgt_mode_enabled(vha))
7196 		vpmod->options_idx1 &= ~BIT_4;
7197 }
7198 
7199 void
7200 qlt_probe_one_stage1(struct scsi_qla_host *base_vha, struct qla_hw_data *ha)
7201 {
7202 	mutex_init(&base_vha->vha_tgt.tgt_mutex);
7203 	if (!QLA_TGT_MODE_ENABLED())
7204 		return;
7205 
7206 	if  (ha->mqenable || IS_QLA83XX(ha) || IS_QLA27XX(ha) || IS_QLA28XX(ha)) {
7207 		ISP_ATIO_Q_IN(base_vha) = &ha->mqiobase->isp25mq.atio_q_in;
7208 		ISP_ATIO_Q_OUT(base_vha) = &ha->mqiobase->isp25mq.atio_q_out;
7209 	} else {
7210 		ISP_ATIO_Q_IN(base_vha) = &ha->iobase->isp24.atio_q_in;
7211 		ISP_ATIO_Q_OUT(base_vha) = &ha->iobase->isp24.atio_q_out;
7212 	}
7213 
7214 	mutex_init(&base_vha->vha_tgt.tgt_host_action_mutex);
7215 
7216 	INIT_LIST_HEAD(&base_vha->unknown_atio_list);
7217 	INIT_DELAYED_WORK(&base_vha->unknown_atio_work,
7218 	    qlt_unknown_atio_work_fn);
7219 
7220 	qlt_clear_mode(base_vha);
7221 
7222 	qlt_update_vp_map(base_vha, SET_VP_IDX);
7223 }
7224 
7225 irqreturn_t
7226 qla83xx_msix_atio_q(int irq, void *dev_id)
7227 {
7228 	struct rsp_que *rsp;
7229 	scsi_qla_host_t	*vha;
7230 	struct qla_hw_data *ha;
7231 	unsigned long flags;
7232 
7233 	rsp = (struct rsp_que *) dev_id;
7234 	ha = rsp->hw;
7235 	vha = pci_get_drvdata(ha->pdev);
7236 
7237 	spin_lock_irqsave(&ha->tgt.atio_lock, flags);
7238 
7239 	qlt_24xx_process_atio_queue(vha, 0);
7240 
7241 	spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
7242 
7243 	return IRQ_HANDLED;
7244 }
7245 
7246 static void
7247 qlt_handle_abts_recv_work(struct work_struct *work)
7248 {
7249 	struct qla_tgt_sess_op *op = container_of(work,
7250 		struct qla_tgt_sess_op, work);
7251 	scsi_qla_host_t *vha = op->vha;
7252 	struct qla_hw_data *ha = vha->hw;
7253 	unsigned long flags;
7254 
7255 	if (qla2x00_reset_active(vha) ||
7256 	    (op->chip_reset != ha->base_qpair->chip_reset))
7257 		return;
7258 
7259 	spin_lock_irqsave(&ha->tgt.atio_lock, flags);
7260 	qlt_24xx_process_atio_queue(vha, 0);
7261 	spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
7262 
7263 	spin_lock_irqsave(&ha->hardware_lock, flags);
7264 	qlt_response_pkt_all_vps(vha, op->rsp, (response_t *)&op->atio);
7265 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
7266 
7267 	kfree(op);
7268 }
7269 
7270 void
7271 qlt_handle_abts_recv(struct scsi_qla_host *vha, struct rsp_que *rsp,
7272     response_t *pkt)
7273 {
7274 	struct qla_tgt_sess_op *op;
7275 
7276 	op = kzalloc(sizeof(*op), GFP_ATOMIC);
7277 
7278 	if (!op) {
7279 		/* do not reach for ATIO queue here.  This is best effort err
7280 		 * recovery at this point.
7281 		 */
7282 		qlt_response_pkt_all_vps(vha, rsp, pkt);
7283 		return;
7284 	}
7285 
7286 	memcpy(&op->atio, pkt, sizeof(*pkt));
7287 	op->vha = vha;
7288 	op->chip_reset = vha->hw->base_qpair->chip_reset;
7289 	op->rsp = rsp;
7290 	INIT_WORK(&op->work, qlt_handle_abts_recv_work);
7291 	queue_work(qla_tgt_wq, &op->work);
7292 	return;
7293 }
7294 
7295 int
7296 qlt_mem_alloc(struct qla_hw_data *ha)
7297 {
7298 	if (!QLA_TGT_MODE_ENABLED())
7299 		return 0;
7300 
7301 	ha->tgt.tgt_vp_map = kcalloc(MAX_MULTI_ID_FABRIC,
7302 				     sizeof(struct qla_tgt_vp_map),
7303 				     GFP_KERNEL);
7304 	if (!ha->tgt.tgt_vp_map)
7305 		return -ENOMEM;
7306 
7307 	ha->tgt.atio_ring = dma_alloc_coherent(&ha->pdev->dev,
7308 	    (ha->tgt.atio_q_length + 1) * sizeof(struct atio_from_isp),
7309 	    &ha->tgt.atio_dma, GFP_KERNEL);
7310 	if (!ha->tgt.atio_ring) {
7311 		kfree(ha->tgt.tgt_vp_map);
7312 		return -ENOMEM;
7313 	}
7314 	return 0;
7315 }
7316 
7317 void
7318 qlt_mem_free(struct qla_hw_data *ha)
7319 {
7320 	if (!QLA_TGT_MODE_ENABLED())
7321 		return;
7322 
7323 	if (ha->tgt.atio_ring) {
7324 		dma_free_coherent(&ha->pdev->dev, (ha->tgt.atio_q_length + 1) *
7325 		    sizeof(struct atio_from_isp), ha->tgt.atio_ring,
7326 		    ha->tgt.atio_dma);
7327 	}
7328 	ha->tgt.atio_ring = NULL;
7329 	ha->tgt.atio_dma = 0;
7330 	kfree(ha->tgt.tgt_vp_map);
7331 	ha->tgt.tgt_vp_map = NULL;
7332 }
7333 
7334 /* vport_slock to be held by the caller */
7335 void
7336 qlt_update_vp_map(struct scsi_qla_host *vha, int cmd)
7337 {
7338 	void *slot;
7339 	u32 key;
7340 	int rc;
7341 
7342 	key = vha->d_id.b24;
7343 
7344 	switch (cmd) {
7345 	case SET_VP_IDX:
7346 		if (!QLA_TGT_MODE_ENABLED())
7347 			return;
7348 		vha->hw->tgt.tgt_vp_map[vha->vp_idx].vha = vha;
7349 		break;
7350 	case SET_AL_PA:
7351 		slot = btree_lookup32(&vha->hw->host_map, key);
7352 		if (!slot) {
7353 			ql_dbg(ql_dbg_tgt_mgt, vha, 0xf018,
7354 			    "Save vha in host_map %p %06x\n", vha, key);
7355 			rc = btree_insert32(&vha->hw->host_map,
7356 				key, vha, GFP_ATOMIC);
7357 			if (rc)
7358 				ql_log(ql_log_info, vha, 0xd03e,
7359 				    "Unable to insert s_id into host_map: %06x\n",
7360 				    key);
7361 			return;
7362 		}
7363 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf019,
7364 		    "replace existing vha in host_map %p %06x\n", vha, key);
7365 		btree_update32(&vha->hw->host_map, key, vha);
7366 		break;
7367 	case RESET_VP_IDX:
7368 		if (!QLA_TGT_MODE_ENABLED())
7369 			return;
7370 		vha->hw->tgt.tgt_vp_map[vha->vp_idx].vha = NULL;
7371 		break;
7372 	case RESET_AL_PA:
7373 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01a,
7374 		   "clear vha in host_map %p %06x\n", vha, key);
7375 		slot = btree_lookup32(&vha->hw->host_map, key);
7376 		if (slot)
7377 			btree_remove32(&vha->hw->host_map, key);
7378 		vha->d_id.b24 = 0;
7379 		break;
7380 	}
7381 }
7382 
7383 void qlt_update_host_map(struct scsi_qla_host *vha, port_id_t id)
7384 {
7385 
7386 	if (!vha->d_id.b24) {
7387 		vha->d_id = id;
7388 		qlt_update_vp_map(vha, SET_AL_PA);
7389 	} else if (vha->d_id.b24 != id.b24) {
7390 		qlt_update_vp_map(vha, RESET_AL_PA);
7391 		vha->d_id = id;
7392 		qlt_update_vp_map(vha, SET_AL_PA);
7393 	}
7394 }
7395 
7396 static int __init qlt_parse_ini_mode(void)
7397 {
7398 	if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_EXCLUSIVE) == 0)
7399 		ql2x_ini_mode = QLA2XXX_INI_MODE_EXCLUSIVE;
7400 	else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_DISABLED) == 0)
7401 		ql2x_ini_mode = QLA2XXX_INI_MODE_DISABLED;
7402 	else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_ENABLED) == 0)
7403 		ql2x_ini_mode = QLA2XXX_INI_MODE_ENABLED;
7404 	else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_DUAL) == 0)
7405 		ql2x_ini_mode = QLA2XXX_INI_MODE_DUAL;
7406 	else
7407 		return false;
7408 
7409 	return true;
7410 }
7411 
7412 int __init qlt_init(void)
7413 {
7414 	int ret;
7415 
7416 	BUILD_BUG_ON(sizeof(struct ctio7_to_24xx) != 64);
7417 	BUILD_BUG_ON(sizeof(struct ctio_to_2xxx) != 64);
7418 
7419 	if (!qlt_parse_ini_mode()) {
7420 		ql_log(ql_log_fatal, NULL, 0xe06b,
7421 		    "qlt_parse_ini_mode() failed\n");
7422 		return -EINVAL;
7423 	}
7424 
7425 	if (!QLA_TGT_MODE_ENABLED())
7426 		return 0;
7427 
7428 	qla_tgt_mgmt_cmd_cachep = kmem_cache_create("qla_tgt_mgmt_cmd_cachep",
7429 	    sizeof(struct qla_tgt_mgmt_cmd), __alignof__(struct
7430 	    qla_tgt_mgmt_cmd), 0, NULL);
7431 	if (!qla_tgt_mgmt_cmd_cachep) {
7432 		ql_log(ql_log_fatal, NULL, 0xd04b,
7433 		    "kmem_cache_create for qla_tgt_mgmt_cmd_cachep failed\n");
7434 		return -ENOMEM;
7435 	}
7436 
7437 	qla_tgt_plogi_cachep = kmem_cache_create("qla_tgt_plogi_cachep",
7438 	    sizeof(struct qlt_plogi_ack_t), __alignof__(struct qlt_plogi_ack_t),
7439 	    0, NULL);
7440 
7441 	if (!qla_tgt_plogi_cachep) {
7442 		ql_log(ql_log_fatal, NULL, 0xe06d,
7443 		    "kmem_cache_create for qla_tgt_plogi_cachep failed\n");
7444 		ret = -ENOMEM;
7445 		goto out_mgmt_cmd_cachep;
7446 	}
7447 
7448 	qla_tgt_mgmt_cmd_mempool = mempool_create(25, mempool_alloc_slab,
7449 	    mempool_free_slab, qla_tgt_mgmt_cmd_cachep);
7450 	if (!qla_tgt_mgmt_cmd_mempool) {
7451 		ql_log(ql_log_fatal, NULL, 0xe06e,
7452 		    "mempool_create for qla_tgt_mgmt_cmd_mempool failed\n");
7453 		ret = -ENOMEM;
7454 		goto out_plogi_cachep;
7455 	}
7456 
7457 	qla_tgt_wq = alloc_workqueue("qla_tgt_wq", 0, 0);
7458 	if (!qla_tgt_wq) {
7459 		ql_log(ql_log_fatal, NULL, 0xe06f,
7460 		    "alloc_workqueue for qla_tgt_wq failed\n");
7461 		ret = -ENOMEM;
7462 		goto out_cmd_mempool;
7463 	}
7464 	/*
7465 	 * Return 1 to signal that initiator-mode is being disabled
7466 	 */
7467 	return (ql2x_ini_mode == QLA2XXX_INI_MODE_DISABLED) ? 1 : 0;
7468 
7469 out_cmd_mempool:
7470 	mempool_destroy(qla_tgt_mgmt_cmd_mempool);
7471 out_plogi_cachep:
7472 	kmem_cache_destroy(qla_tgt_plogi_cachep);
7473 out_mgmt_cmd_cachep:
7474 	kmem_cache_destroy(qla_tgt_mgmt_cmd_cachep);
7475 	return ret;
7476 }
7477 
7478 void qlt_exit(void)
7479 {
7480 	if (!QLA_TGT_MODE_ENABLED())
7481 		return;
7482 
7483 	destroy_workqueue(qla_tgt_wq);
7484 	mempool_destroy(qla_tgt_mgmt_cmd_mempool);
7485 	kmem_cache_destroy(qla_tgt_plogi_cachep);
7486 	kmem_cache_destroy(qla_tgt_mgmt_cmd_cachep);
7487 }
7488