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