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 		if (cmd->sg_mapped)
3823 			qlt_unmap_sg(vha, cmd);
3824 
3825 		spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3826 		/*
3827 		 * It's normal to see 2 calls in this path:
3828 		 *  1) XFER Rdy completion + CMD_T_ABORT
3829 		 *  2) TCM TMR - drain_state_list
3830 		 */
3831 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf016,
3832 		    "multiple abort. %p transport_state %x, t_state %x, "
3833 		    "se_cmd_flags %x\n", cmd, cmd->se_cmd.transport_state,
3834 		    cmd->se_cmd.t_state, cmd->se_cmd.se_cmd_flags);
3835 		return -EIO;
3836 	}
3837 	cmd->aborted = 1;
3838 	cmd->trc_flags |= TRC_ABORT;
3839 	spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3840 
3841 	qlt_send_term_exchange(cmd->qpair, cmd, &cmd->atio, 0, 1);
3842 	return 0;
3843 }
3844 EXPORT_SYMBOL(qlt_abort_cmd);
3845 
3846 void qlt_free_cmd(struct qla_tgt_cmd *cmd)
3847 {
3848 	struct fc_port *sess = cmd->sess;
3849 
3850 	ql_dbg(ql_dbg_tgt, cmd->vha, 0xe074,
3851 	    "%s: se_cmd[%p] ox_id %04x\n",
3852 	    __func__, &cmd->se_cmd,
3853 	    be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
3854 
3855 	BUG_ON(cmd->cmd_in_wq);
3856 
3857 	if (!cmd->q_full)
3858 		qlt_decr_num_pend_cmds(cmd->vha);
3859 
3860 	BUG_ON(cmd->sg_mapped);
3861 	cmd->jiffies_at_free = get_jiffies_64();
3862 
3863 	if (!sess || !sess->se_sess) {
3864 		WARN_ON(1);
3865 		return;
3866 	}
3867 	cmd->jiffies_at_free = get_jiffies_64();
3868 	cmd->vha->hw->tgt.tgt_ops->rel_cmd(cmd);
3869 }
3870 EXPORT_SYMBOL(qlt_free_cmd);
3871 
3872 /*
3873  * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
3874  */
3875 static int qlt_term_ctio_exchange(struct qla_qpair *qpair, void *ctio,
3876 	struct qla_tgt_cmd *cmd, uint32_t status)
3877 {
3878 	int term = 0;
3879 	struct scsi_qla_host *vha = qpair->vha;
3880 
3881 	if (cmd->se_cmd.prot_op)
3882 		ql_dbg(ql_dbg_tgt_dif, vha, 0xe013,
3883 		    "Term DIF cmd: lba[0x%llx|%lld] len[0x%x] "
3884 		    "se_cmd=%p tag[%x] op %#x/%s",
3885 		     cmd->lba, cmd->lba,
3886 		     cmd->num_blks, &cmd->se_cmd,
3887 		     cmd->atio.u.isp24.exchange_addr,
3888 		     cmd->se_cmd.prot_op,
3889 		     prot_op_str(cmd->se_cmd.prot_op));
3890 
3891 	if (ctio != NULL) {
3892 		struct ctio7_from_24xx *c = (struct ctio7_from_24xx *)ctio;
3893 
3894 		term = !(c->flags &
3895 		    cpu_to_le16(OF_TERM_EXCH));
3896 	} else
3897 		term = 1;
3898 
3899 	if (term)
3900 		qlt_send_term_exchange(qpair, cmd, &cmd->atio, 1, 0);
3901 
3902 	return term;
3903 }
3904 
3905 
3906 /* ha->hardware_lock supposed to be held on entry */
3907 static void *qlt_ctio_to_cmd(struct scsi_qla_host *vha,
3908 	struct rsp_que *rsp, uint32_t handle, void *ctio)
3909 {
3910 	void *cmd = NULL;
3911 	struct req_que *req;
3912 	int qid = GET_QID(handle);
3913 	uint32_t h = handle & ~QLA_TGT_HANDLE_MASK;
3914 
3915 	if (unlikely(h == QLA_TGT_SKIP_HANDLE))
3916 		return NULL;
3917 
3918 	if (qid == rsp->req->id) {
3919 		req = rsp->req;
3920 	} else if (vha->hw->req_q_map[qid]) {
3921 		ql_dbg(ql_dbg_tgt_mgt, vha, 0x1000a,
3922 		    "qla_target(%d): CTIO completion with different QID %d handle %x\n",
3923 		    vha->vp_idx, rsp->id, handle);
3924 		req = vha->hw->req_q_map[qid];
3925 	} else {
3926 		return NULL;
3927 	}
3928 
3929 	h &= QLA_CMD_HANDLE_MASK;
3930 
3931 	if (h != QLA_TGT_NULL_HANDLE) {
3932 		if (unlikely(h >= req->num_outstanding_cmds)) {
3933 			ql_dbg(ql_dbg_tgt, vha, 0xe052,
3934 			    "qla_target(%d): Wrong handle %x received\n",
3935 			    vha->vp_idx, handle);
3936 			return NULL;
3937 		}
3938 
3939 		cmd = req->outstanding_cmds[h];
3940 		if (unlikely(cmd == NULL)) {
3941 			ql_dbg(ql_dbg_async, vha, 0xe053,
3942 			    "qla_target(%d): Suspicious: unable to find the command with handle %x req->id %d rsp->id %d\n",
3943 				vha->vp_idx, handle, req->id, rsp->id);
3944 			return NULL;
3945 		}
3946 		req->outstanding_cmds[h] = NULL;
3947 	} else if (ctio != NULL) {
3948 		/* We can't get loop ID from CTIO7 */
3949 		ql_dbg(ql_dbg_tgt, vha, 0xe054,
3950 		    "qla_target(%d): Wrong CTIO received: QLA24xx doesn't "
3951 		    "support NULL handles\n", vha->vp_idx);
3952 		return NULL;
3953 	}
3954 
3955 	return cmd;
3956 }
3957 
3958 /*
3959  * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
3960  */
3961 static void qlt_do_ctio_completion(struct scsi_qla_host *vha,
3962     struct rsp_que *rsp, uint32_t handle, uint32_t status, void *ctio)
3963 {
3964 	struct qla_hw_data *ha = vha->hw;
3965 	struct se_cmd *se_cmd;
3966 	struct qla_tgt_cmd *cmd;
3967 	struct qla_qpair *qpair = rsp->qpair;
3968 
3969 	if (handle & CTIO_INTERMEDIATE_HANDLE_MARK) {
3970 		/* That could happen only in case of an error/reset/abort */
3971 		if (status != CTIO_SUCCESS) {
3972 			ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01d,
3973 			    "Intermediate CTIO received"
3974 			    " (status %x)\n", status);
3975 		}
3976 		return;
3977 	}
3978 
3979 	cmd = qlt_ctio_to_cmd(vha, rsp, handle, ctio);
3980 	if (cmd == NULL)
3981 		return;
3982 
3983 	if ((le16_to_cpu(((struct ctio7_from_24xx *)ctio)->flags) & CTIO7_FLAGS_DATA_OUT) &&
3984 	    cmd->sess) {
3985 		qlt_chk_edif_rx_sa_delete_pending(vha, cmd->sess,
3986 		    (struct ctio7_from_24xx *)ctio);
3987 	}
3988 
3989 	se_cmd = &cmd->se_cmd;
3990 	cmd->cmd_sent_to_fw = 0;
3991 
3992 	qlt_unmap_sg(vha, cmd);
3993 
3994 	if (unlikely(status != CTIO_SUCCESS)) {
3995 		switch (status & 0xFFFF) {
3996 		case CTIO_INVALID_RX_ID:
3997 			if (printk_ratelimit())
3998 				dev_info(&vha->hw->pdev->dev,
3999 				    "qla_target(%d): CTIO with INVALID_RX_ID ATIO attr %x CTIO Flags %x|%x\n",
4000 				    vha->vp_idx, cmd->atio.u.isp24.attr,
4001 				    ((cmd->ctio_flags >> 9) & 0xf),
4002 				    cmd->ctio_flags);
4003 
4004 			break;
4005 		case CTIO_LIP_RESET:
4006 		case CTIO_TARGET_RESET:
4007 		case CTIO_ABORTED:
4008 			/* driver request abort via Terminate exchange */
4009 		case CTIO_TIMEOUT:
4010 			/* They are OK */
4011 			ql_dbg(ql_dbg_tgt_mgt, vha, 0xf058,
4012 			    "qla_target(%d): CTIO with "
4013 			    "status %#x received, state %x, se_cmd %p, "
4014 			    "(LIP_RESET=e, ABORTED=2, TARGET_RESET=17, "
4015 			    "TIMEOUT=b, INVALID_RX_ID=8)\n", vha->vp_idx,
4016 			    status, cmd->state, se_cmd);
4017 			break;
4018 
4019 		case CTIO_PORT_LOGGED_OUT:
4020 		case CTIO_PORT_UNAVAILABLE:
4021 		{
4022 			int logged_out =
4023 				(status & 0xFFFF) == CTIO_PORT_LOGGED_OUT;
4024 
4025 			ql_dbg(ql_dbg_tgt_mgt, vha, 0xf059,
4026 			    "qla_target(%d): CTIO with %s status %x "
4027 			    "received (state %x, se_cmd %p)\n", vha->vp_idx,
4028 			    logged_out ? "PORT LOGGED OUT" : "PORT UNAVAILABLE",
4029 			    status, cmd->state, se_cmd);
4030 
4031 			if (logged_out && cmd->sess) {
4032 				/*
4033 				 * Session is already logged out, but we need
4034 				 * to notify initiator, who's not aware of this
4035 				 */
4036 				cmd->sess->send_els_logo = 1;
4037 				ql_dbg(ql_dbg_disc, vha, 0x20f8,
4038 				    "%s %d %8phC post del sess\n",
4039 				    __func__, __LINE__, cmd->sess->port_name);
4040 
4041 				qlt_schedule_sess_for_deletion(cmd->sess);
4042 			}
4043 			break;
4044 		}
4045 		case CTIO_DIF_ERROR: {
4046 			struct ctio_crc_from_fw *crc =
4047 				(struct ctio_crc_from_fw *)ctio;
4048 			ql_dbg(ql_dbg_tgt_mgt, vha, 0xf073,
4049 			    "qla_target(%d): CTIO with DIF_ERROR status %x "
4050 			    "received (state %x, ulp_cmd %p) actual_dif[0x%llx] "
4051 			    "expect_dif[0x%llx]\n",
4052 			    vha->vp_idx, status, cmd->state, se_cmd,
4053 			    *((u64 *)&crc->actual_dif[0]),
4054 			    *((u64 *)&crc->expected_dif[0]));
4055 
4056 			qlt_handle_dif_error(qpair, cmd, ctio);
4057 			return;
4058 		}
4059 
4060 		case CTIO_FAST_AUTH_ERR:
4061 		case CTIO_FAST_INCOMP_PAD_LEN:
4062 		case CTIO_FAST_INVALID_REQ:
4063 		case CTIO_FAST_SPI_ERR:
4064 			ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05b,
4065 			    "qla_target(%d): CTIO with EDIF error status 0x%x received (state %x, se_cmd %p\n",
4066 			    vha->vp_idx, status, cmd->state, se_cmd);
4067 			break;
4068 
4069 		default:
4070 			ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05b,
4071 			    "qla_target(%d): CTIO with error status 0x%x received (state %x, se_cmd %p\n",
4072 			    vha->vp_idx, status, cmd->state, se_cmd);
4073 			break;
4074 		}
4075 
4076 
4077 		/* "cmd->aborted" means
4078 		 * cmd is already aborted/terminated, we don't
4079 		 * need to terminate again.  The exchange is already
4080 		 * cleaned up/freed at FW level.  Just cleanup at driver
4081 		 * level.
4082 		 */
4083 		if ((cmd->state != QLA_TGT_STATE_NEED_DATA) &&
4084 		    (!cmd->aborted)) {
4085 			cmd->trc_flags |= TRC_CTIO_ERR;
4086 			if (qlt_term_ctio_exchange(qpair, ctio, cmd, status))
4087 				return;
4088 		}
4089 	}
4090 
4091 	if (cmd->state == QLA_TGT_STATE_PROCESSED) {
4092 		cmd->trc_flags |= TRC_CTIO_DONE;
4093 	} else if (cmd->state == QLA_TGT_STATE_NEED_DATA) {
4094 		cmd->state = QLA_TGT_STATE_DATA_IN;
4095 
4096 		if (status == CTIO_SUCCESS)
4097 			cmd->write_data_transferred = 1;
4098 
4099 		ha->tgt.tgt_ops->handle_data(cmd);
4100 		return;
4101 	} else if (cmd->aborted) {
4102 		cmd->trc_flags |= TRC_CTIO_ABORTED;
4103 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01e,
4104 		  "Aborted command %p (tag %lld) finished\n", cmd, se_cmd->tag);
4105 	} else {
4106 		cmd->trc_flags |= TRC_CTIO_STRANGE;
4107 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05c,
4108 		    "qla_target(%d): A command in state (%d) should "
4109 		    "not return a CTIO complete\n", vha->vp_idx, cmd->state);
4110 	}
4111 
4112 	if (unlikely(status != CTIO_SUCCESS) &&
4113 		!cmd->aborted) {
4114 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01f, "Finishing failed CTIO\n");
4115 		dump_stack();
4116 	}
4117 
4118 	ha->tgt.tgt_ops->free_cmd(cmd);
4119 }
4120 
4121 static inline int qlt_get_fcp_task_attr(struct scsi_qla_host *vha,
4122 	uint8_t task_codes)
4123 {
4124 	int fcp_task_attr;
4125 
4126 	switch (task_codes) {
4127 	case ATIO_SIMPLE_QUEUE:
4128 		fcp_task_attr = TCM_SIMPLE_TAG;
4129 		break;
4130 	case ATIO_HEAD_OF_QUEUE:
4131 		fcp_task_attr = TCM_HEAD_TAG;
4132 		break;
4133 	case ATIO_ORDERED_QUEUE:
4134 		fcp_task_attr = TCM_ORDERED_TAG;
4135 		break;
4136 	case ATIO_ACA_QUEUE:
4137 		fcp_task_attr = TCM_ACA_TAG;
4138 		break;
4139 	case ATIO_UNTAGGED:
4140 		fcp_task_attr = TCM_SIMPLE_TAG;
4141 		break;
4142 	default:
4143 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05d,
4144 		    "qla_target: unknown task code %x, use ORDERED instead\n",
4145 		    task_codes);
4146 		fcp_task_attr = TCM_ORDERED_TAG;
4147 		break;
4148 	}
4149 
4150 	return fcp_task_attr;
4151 }
4152 
4153 /*
4154  * Process context for I/O path into tcm_qla2xxx code
4155  */
4156 static void __qlt_do_work(struct qla_tgt_cmd *cmd)
4157 {
4158 	scsi_qla_host_t *vha = cmd->vha;
4159 	struct qla_hw_data *ha = vha->hw;
4160 	struct fc_port *sess = cmd->sess;
4161 	struct atio_from_isp *atio = &cmd->atio;
4162 	unsigned char *cdb;
4163 	unsigned long flags;
4164 	uint32_t data_length;
4165 	int ret, fcp_task_attr, data_dir, bidi = 0;
4166 	struct qla_qpair *qpair = cmd->qpair;
4167 
4168 	cmd->cmd_in_wq = 0;
4169 	cmd->trc_flags |= TRC_DO_WORK;
4170 
4171 	if (cmd->aborted) {
4172 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf082,
4173 		    "cmd with tag %u is aborted\n",
4174 		    cmd->atio.u.isp24.exchange_addr);
4175 		goto out_term;
4176 	}
4177 
4178 	spin_lock_init(&cmd->cmd_lock);
4179 	cdb = &atio->u.isp24.fcp_cmnd.cdb[0];
4180 	cmd->se_cmd.tag = le32_to_cpu(atio->u.isp24.exchange_addr);
4181 
4182 	if (atio->u.isp24.fcp_cmnd.rddata &&
4183 	    atio->u.isp24.fcp_cmnd.wrdata) {
4184 		bidi = 1;
4185 		data_dir = DMA_TO_DEVICE;
4186 	} else if (atio->u.isp24.fcp_cmnd.rddata)
4187 		data_dir = DMA_FROM_DEVICE;
4188 	else if (atio->u.isp24.fcp_cmnd.wrdata)
4189 		data_dir = DMA_TO_DEVICE;
4190 	else
4191 		data_dir = DMA_NONE;
4192 
4193 	fcp_task_attr = qlt_get_fcp_task_attr(vha,
4194 	    atio->u.isp24.fcp_cmnd.task_attr);
4195 	data_length = get_datalen_for_atio(atio);
4196 
4197 	ret = ha->tgt.tgt_ops->handle_cmd(vha, cmd, cdb, data_length,
4198 				          fcp_task_attr, data_dir, bidi);
4199 	if (ret != 0)
4200 		goto out_term;
4201 	/*
4202 	 * Drop extra session reference from qlt_handle_cmd_for_atio().
4203 	 */
4204 	ha->tgt.tgt_ops->put_sess(sess);
4205 	return;
4206 
4207 out_term:
4208 	ql_dbg(ql_dbg_io, vha, 0x3060, "Terminating work cmd %p", cmd);
4209 	/*
4210 	 * cmd has not sent to target yet, so pass NULL as the second
4211 	 * argument to qlt_send_term_exchange() and free the memory here.
4212 	 */
4213 	cmd->trc_flags |= TRC_DO_WORK_ERR;
4214 	spin_lock_irqsave(qpair->qp_lock_ptr, flags);
4215 	qlt_send_term_exchange(qpair, NULL, &cmd->atio, 1, 0);
4216 
4217 	qlt_decr_num_pend_cmds(vha);
4218 	cmd->vha->hw->tgt.tgt_ops->rel_cmd(cmd);
4219 	spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
4220 
4221 	ha->tgt.tgt_ops->put_sess(sess);
4222 }
4223 
4224 static void qlt_do_work(struct work_struct *work)
4225 {
4226 	struct qla_tgt_cmd *cmd = container_of(work, struct qla_tgt_cmd, work);
4227 	scsi_qla_host_t *vha = cmd->vha;
4228 	unsigned long flags;
4229 
4230 	spin_lock_irqsave(&vha->cmd_list_lock, flags);
4231 	list_del(&cmd->cmd_list);
4232 	spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
4233 
4234 	__qlt_do_work(cmd);
4235 }
4236 
4237 void qlt_clr_qp_table(struct scsi_qla_host *vha)
4238 {
4239 	unsigned long flags;
4240 	struct qla_hw_data *ha = vha->hw;
4241 	struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4242 	void *node;
4243 	u64 key = 0;
4244 
4245 	ql_log(ql_log_info, vha, 0x706c,
4246 	    "User update Number of Active Qpairs %d\n",
4247 	    ha->tgt.num_act_qpairs);
4248 
4249 	spin_lock_irqsave(&ha->tgt.atio_lock, flags);
4250 
4251 	btree_for_each_safe64(&tgt->lun_qpair_map, key, node)
4252 		btree_remove64(&tgt->lun_qpair_map, key);
4253 
4254 	ha->base_qpair->lun_cnt = 0;
4255 	for (key = 0; key < ha->max_qpairs; key++)
4256 		if (ha->queue_pair_map[key])
4257 			ha->queue_pair_map[key]->lun_cnt = 0;
4258 
4259 	spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
4260 }
4261 
4262 static void qlt_assign_qpair(struct scsi_qla_host *vha,
4263 	struct qla_tgt_cmd *cmd)
4264 {
4265 	struct qla_qpair *qpair, *qp;
4266 	struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4267 	struct qla_qpair_hint *h;
4268 
4269 	if (vha->flags.qpairs_available) {
4270 		h = btree_lookup64(&tgt->lun_qpair_map, cmd->unpacked_lun);
4271 		if (unlikely(!h)) {
4272 			/* spread lun to qpair ratio evently */
4273 			int lcnt = 0, rc;
4274 			struct scsi_qla_host *base_vha =
4275 				pci_get_drvdata(vha->hw->pdev);
4276 
4277 			qpair = vha->hw->base_qpair;
4278 			if (qpair->lun_cnt == 0) {
4279 				qpair->lun_cnt++;
4280 				h = qla_qpair_to_hint(tgt, qpair);
4281 				BUG_ON(!h);
4282 				rc = btree_insert64(&tgt->lun_qpair_map,
4283 					cmd->unpacked_lun, h, GFP_ATOMIC);
4284 				if (rc) {
4285 					qpair->lun_cnt--;
4286 					ql_log(ql_log_info, vha, 0xd037,
4287 					    "Unable to insert lun %llx into lun_qpair_map\n",
4288 					    cmd->unpacked_lun);
4289 				}
4290 				goto out;
4291 			} else {
4292 				lcnt = qpair->lun_cnt;
4293 			}
4294 
4295 			h = NULL;
4296 			list_for_each_entry(qp, &base_vha->qp_list,
4297 			    qp_list_elem) {
4298 				if (qp->lun_cnt == 0) {
4299 					qp->lun_cnt++;
4300 					h = qla_qpair_to_hint(tgt, qp);
4301 					BUG_ON(!h);
4302 					rc = btree_insert64(&tgt->lun_qpair_map,
4303 					    cmd->unpacked_lun, h, GFP_ATOMIC);
4304 					if (rc) {
4305 						qp->lun_cnt--;
4306 						ql_log(ql_log_info, vha, 0xd038,
4307 							"Unable to insert lun %llx into lun_qpair_map\n",
4308 							cmd->unpacked_lun);
4309 					}
4310 					qpair = qp;
4311 					goto out;
4312 				} else {
4313 					if (qp->lun_cnt < lcnt) {
4314 						lcnt = qp->lun_cnt;
4315 						qpair = qp;
4316 						continue;
4317 					}
4318 				}
4319 			}
4320 			BUG_ON(!qpair);
4321 			qpair->lun_cnt++;
4322 			h = qla_qpair_to_hint(tgt, qpair);
4323 			BUG_ON(!h);
4324 			rc = btree_insert64(&tgt->lun_qpair_map,
4325 				cmd->unpacked_lun, h, GFP_ATOMIC);
4326 			if (rc) {
4327 				qpair->lun_cnt--;
4328 				ql_log(ql_log_info, vha, 0xd039,
4329 				   "Unable to insert lun %llx into lun_qpair_map\n",
4330 				   cmd->unpacked_lun);
4331 			}
4332 		}
4333 	} else {
4334 		h = &tgt->qphints[0];
4335 	}
4336 out:
4337 	cmd->qpair = h->qpair;
4338 	cmd->se_cmd.cpuid = h->cpuid;
4339 }
4340 
4341 static struct qla_tgt_cmd *qlt_get_tag(scsi_qla_host_t *vha,
4342 				       struct fc_port *sess,
4343 				       struct atio_from_isp *atio)
4344 {
4345 	struct qla_tgt_cmd *cmd;
4346 
4347 	cmd = vha->hw->tgt.tgt_ops->get_cmd(sess);
4348 	if (!cmd)
4349 		return NULL;
4350 
4351 	cmd->cmd_type = TYPE_TGT_CMD;
4352 	memcpy(&cmd->atio, atio, sizeof(*atio));
4353 	INIT_LIST_HEAD(&cmd->sess_cmd_list);
4354 	cmd->state = QLA_TGT_STATE_NEW;
4355 	cmd->tgt = vha->vha_tgt.qla_tgt;
4356 	qlt_incr_num_pend_cmds(vha);
4357 	cmd->vha = vha;
4358 	cmd->sess = sess;
4359 	cmd->loop_id = sess->loop_id;
4360 	cmd->conf_compl_supported = sess->conf_compl_supported;
4361 
4362 	cmd->trc_flags = 0;
4363 	cmd->jiffies_at_alloc = get_jiffies_64();
4364 
4365 	cmd->unpacked_lun = scsilun_to_int(
4366 	    (struct scsi_lun *)&atio->u.isp24.fcp_cmnd.lun);
4367 	qlt_assign_qpair(vha, cmd);
4368 	cmd->reset_count = vha->hw->base_qpair->chip_reset;
4369 	cmd->vp_idx = vha->vp_idx;
4370 	cmd->edif = sess->edif.enable;
4371 
4372 	return cmd;
4373 }
4374 
4375 /* ha->hardware_lock supposed to be held on entry */
4376 static int qlt_handle_cmd_for_atio(struct scsi_qla_host *vha,
4377 	struct atio_from_isp *atio)
4378 {
4379 	struct qla_hw_data *ha = vha->hw;
4380 	struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4381 	struct fc_port *sess;
4382 	struct qla_tgt_cmd *cmd;
4383 	unsigned long flags;
4384 	port_id_t id;
4385 
4386 	if (unlikely(tgt->tgt_stop)) {
4387 		ql_dbg(ql_dbg_io, vha, 0x3061,
4388 		    "New command while device %p is shutting down\n", tgt);
4389 		return -ENODEV;
4390 	}
4391 
4392 	id = be_to_port_id(atio->u.isp24.fcp_hdr.s_id);
4393 	if (IS_SW_RESV_ADDR(id))
4394 		return -EBUSY;
4395 
4396 	sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, atio->u.isp24.fcp_hdr.s_id);
4397 	if (unlikely(!sess))
4398 		return -EFAULT;
4399 
4400 	/* Another WWN used to have our s_id. Our PLOGI scheduled its
4401 	 * session deletion, but it's still in sess_del_work wq */
4402 	if (sess->deleted) {
4403 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf002,
4404 		    "New command while old session %p is being deleted\n",
4405 		    sess);
4406 		return -EFAULT;
4407 	}
4408 
4409 	/*
4410 	 * Do kref_get() before returning + dropping qla_hw_data->hardware_lock.
4411 	 */
4412 	if (!kref_get_unless_zero(&sess->sess_kref)) {
4413 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf004,
4414 		    "%s: kref_get fail, %8phC oxid %x \n",
4415 		    __func__, sess->port_name,
4416 		     be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id));
4417 		return -EFAULT;
4418 	}
4419 
4420 	cmd = qlt_get_tag(vha, sess, atio);
4421 	if (!cmd) {
4422 		ql_dbg(ql_dbg_io, vha, 0x3062,
4423 		    "qla_target(%d): Allocation of cmd failed\n", vha->vp_idx);
4424 		ha->tgt.tgt_ops->put_sess(sess);
4425 		return -EBUSY;
4426 	}
4427 
4428 	cmd->cmd_in_wq = 1;
4429 	cmd->trc_flags |= TRC_NEW_CMD;
4430 
4431 	spin_lock_irqsave(&vha->cmd_list_lock, flags);
4432 	list_add_tail(&cmd->cmd_list, &vha->qla_cmd_list);
4433 	spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
4434 
4435 	INIT_WORK(&cmd->work, qlt_do_work);
4436 	if (vha->flags.qpairs_available) {
4437 		queue_work_on(cmd->se_cmd.cpuid, qla_tgt_wq, &cmd->work);
4438 	} else if (ha->msix_count) {
4439 		if (cmd->atio.u.isp24.fcp_cmnd.rddata)
4440 			queue_work_on(smp_processor_id(), qla_tgt_wq,
4441 			    &cmd->work);
4442 		else
4443 			queue_work_on(cmd->se_cmd.cpuid, qla_tgt_wq,
4444 			    &cmd->work);
4445 	} else {
4446 		queue_work(qla_tgt_wq, &cmd->work);
4447 	}
4448 
4449 	return 0;
4450 }
4451 
4452 /* ha->hardware_lock supposed to be held on entry */
4453 static int qlt_issue_task_mgmt(struct fc_port *sess, u64 lun,
4454 	int fn, void *iocb, int flags)
4455 {
4456 	struct scsi_qla_host *vha = sess->vha;
4457 	struct qla_hw_data *ha = vha->hw;
4458 	struct qla_tgt_mgmt_cmd *mcmd;
4459 	struct atio_from_isp *a = (struct atio_from_isp *)iocb;
4460 	struct qla_qpair_hint *h = &vha->vha_tgt.qla_tgt->qphints[0];
4461 
4462 	mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
4463 	if (!mcmd) {
4464 		ql_dbg(ql_dbg_tgt_tmr, vha, 0x10009,
4465 		    "qla_target(%d): Allocation of management "
4466 		    "command failed, some commands and their data could "
4467 		    "leak\n", vha->vp_idx);
4468 		return -ENOMEM;
4469 	}
4470 	memset(mcmd, 0, sizeof(*mcmd));
4471 	mcmd->sess = sess;
4472 
4473 	if (iocb) {
4474 		memcpy(&mcmd->orig_iocb.imm_ntfy, iocb,
4475 		    sizeof(mcmd->orig_iocb.imm_ntfy));
4476 	}
4477 	mcmd->tmr_func = fn;
4478 	mcmd->flags = flags;
4479 	mcmd->reset_count = ha->base_qpair->chip_reset;
4480 	mcmd->qpair = h->qpair;
4481 	mcmd->vha = vha;
4482 	mcmd->se_cmd.cpuid = h->cpuid;
4483 	mcmd->unpacked_lun = lun;
4484 
4485 	switch (fn) {
4486 	case QLA_TGT_LUN_RESET:
4487 	case QLA_TGT_CLEAR_TS:
4488 	case QLA_TGT_ABORT_TS:
4489 		abort_cmds_for_lun(vha, lun, a->u.isp24.fcp_hdr.s_id);
4490 		fallthrough;
4491 	case QLA_TGT_CLEAR_ACA:
4492 		h = qlt_find_qphint(vha, mcmd->unpacked_lun);
4493 		mcmd->qpair = h->qpair;
4494 		mcmd->se_cmd.cpuid = h->cpuid;
4495 		break;
4496 
4497 	case QLA_TGT_TARGET_RESET:
4498 	case QLA_TGT_NEXUS_LOSS_SESS:
4499 	case QLA_TGT_NEXUS_LOSS:
4500 	case QLA_TGT_ABORT_ALL:
4501 	default:
4502 		/* no-op */
4503 		break;
4504 	}
4505 
4506 	INIT_WORK(&mcmd->work, qlt_do_tmr_work);
4507 	queue_work_on(mcmd->se_cmd.cpuid, qla_tgt_wq,
4508 	    &mcmd->work);
4509 
4510 	return 0;
4511 }
4512 
4513 /* ha->hardware_lock supposed to be held on entry */
4514 static int qlt_handle_task_mgmt(struct scsi_qla_host *vha, void *iocb)
4515 {
4516 	struct atio_from_isp *a = (struct atio_from_isp *)iocb;
4517 	struct qla_hw_data *ha = vha->hw;
4518 	struct fc_port *sess;
4519 	u64 unpacked_lun;
4520 	int fn;
4521 	unsigned long flags;
4522 
4523 	fn = a->u.isp24.fcp_cmnd.task_mgmt_flags;
4524 
4525 	spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4526 	sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
4527 	    a->u.isp24.fcp_hdr.s_id);
4528 	spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4529 
4530 	unpacked_lun =
4531 	    scsilun_to_int((struct scsi_lun *)&a->u.isp24.fcp_cmnd.lun);
4532 
4533 	if (sess == NULL || sess->deleted)
4534 		return -EFAULT;
4535 
4536 	return qlt_issue_task_mgmt(sess, unpacked_lun, fn, iocb, 0);
4537 }
4538 
4539 /* ha->hardware_lock supposed to be held on entry */
4540 static int __qlt_abort_task(struct scsi_qla_host *vha,
4541 	struct imm_ntfy_from_isp *iocb, struct fc_port *sess)
4542 {
4543 	struct atio_from_isp *a = (struct atio_from_isp *)iocb;
4544 	struct qla_hw_data *ha = vha->hw;
4545 	struct qla_tgt_mgmt_cmd *mcmd;
4546 	u64 unpacked_lun;
4547 	int rc;
4548 
4549 	mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
4550 	if (mcmd == NULL) {
4551 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05f,
4552 		    "qla_target(%d): %s: Allocation of ABORT cmd failed\n",
4553 		    vha->vp_idx, __func__);
4554 		return -ENOMEM;
4555 	}
4556 	memset(mcmd, 0, sizeof(*mcmd));
4557 
4558 	mcmd->sess = sess;
4559 	memcpy(&mcmd->orig_iocb.imm_ntfy, iocb,
4560 	    sizeof(mcmd->orig_iocb.imm_ntfy));
4561 
4562 	unpacked_lun =
4563 	    scsilun_to_int((struct scsi_lun *)&a->u.isp24.fcp_cmnd.lun);
4564 	mcmd->reset_count = ha->base_qpair->chip_reset;
4565 	mcmd->tmr_func = QLA_TGT_2G_ABORT_TASK;
4566 	mcmd->qpair = ha->base_qpair;
4567 
4568 	rc = ha->tgt.tgt_ops->handle_tmr(mcmd, unpacked_lun, mcmd->tmr_func,
4569 	    le16_to_cpu(iocb->u.isp2x.seq_id));
4570 	if (rc != 0) {
4571 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf060,
4572 		    "qla_target(%d): tgt_ops->handle_tmr() failed: %d\n",
4573 		    vha->vp_idx, rc);
4574 		mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
4575 		return -EFAULT;
4576 	}
4577 
4578 	return 0;
4579 }
4580 
4581 /* ha->hardware_lock supposed to be held on entry */
4582 static int qlt_abort_task(struct scsi_qla_host *vha,
4583 	struct imm_ntfy_from_isp *iocb)
4584 {
4585 	struct qla_hw_data *ha = vha->hw;
4586 	struct fc_port *sess;
4587 	int loop_id;
4588 	unsigned long flags;
4589 
4590 	loop_id = GET_TARGET_ID(ha, (struct atio_from_isp *)iocb);
4591 
4592 	spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4593 	sess = ha->tgt.tgt_ops->find_sess_by_loop_id(vha, loop_id);
4594 	spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
4595 
4596 	if (sess == NULL) {
4597 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf025,
4598 		    "qla_target(%d): task abort for unexisting "
4599 		    "session\n", vha->vp_idx);
4600 		return qlt_sched_sess_work(vha->vha_tgt.qla_tgt,
4601 		    QLA_TGT_SESS_WORK_ABORT, iocb, sizeof(*iocb));
4602 	}
4603 
4604 	return __qlt_abort_task(vha, iocb, sess);
4605 }
4606 
4607 void qlt_logo_completion_handler(fc_port_t *fcport, int rc)
4608 {
4609 	if (rc != MBS_COMMAND_COMPLETE) {
4610 		ql_dbg(ql_dbg_tgt_mgt, fcport->vha, 0xf093,
4611 			"%s: se_sess %p / sess %p from"
4612 			" port %8phC loop_id %#04x s_id %02x:%02x:%02x"
4613 			" LOGO failed: %#x\n",
4614 			__func__,
4615 			fcport->se_sess,
4616 			fcport,
4617 			fcport->port_name, fcport->loop_id,
4618 			fcport->d_id.b.domain, fcport->d_id.b.area,
4619 			fcport->d_id.b.al_pa, rc);
4620 	}
4621 
4622 	fcport->logout_completed = 1;
4623 }
4624 
4625 /*
4626 * ha->hardware_lock supposed to be held on entry (to protect tgt->sess_list)
4627 *
4628 * Schedules sessions with matching port_id/loop_id but different wwn for
4629 * deletion. Returns existing session with matching wwn if present.
4630 * Null otherwise.
4631 */
4632 struct fc_port *
4633 qlt_find_sess_invalidate_other(scsi_qla_host_t *vha, uint64_t wwn,
4634     port_id_t port_id, uint16_t loop_id, struct fc_port **conflict_sess)
4635 {
4636 	struct fc_port *sess = NULL, *other_sess;
4637 	uint64_t other_wwn;
4638 
4639 	*conflict_sess = NULL;
4640 
4641 	list_for_each_entry(other_sess, &vha->vp_fcports, list) {
4642 
4643 		other_wwn = wwn_to_u64(other_sess->port_name);
4644 
4645 		if (wwn == other_wwn) {
4646 			WARN_ON(sess);
4647 			sess = other_sess;
4648 			continue;
4649 		}
4650 
4651 		/* find other sess with nport_id collision */
4652 		if (port_id.b24 == other_sess->d_id.b24) {
4653 			if (loop_id != other_sess->loop_id) {
4654 				ql_dbg(ql_dbg_disc, vha, 0x1000c,
4655 				    "Invalidating sess %p loop_id %d wwn %llx.\n",
4656 				    other_sess, other_sess->loop_id, other_wwn);
4657 
4658 				/*
4659 				 * logout_on_delete is set by default, but another
4660 				 * session that has the same s_id/loop_id combo
4661 				 * might have cleared it when requested this session
4662 				 * deletion, so don't touch it
4663 				 */
4664 				qlt_schedule_sess_for_deletion(other_sess);
4665 			} else {
4666 				/*
4667 				 * Another wwn used to have our s_id/loop_id
4668 				 * kill the session, but don't free the loop_id
4669 				 */
4670 				ql_dbg(ql_dbg_disc, vha, 0xf01b,
4671 				    "Invalidating sess %p loop_id %d wwn %llx.\n",
4672 				    other_sess, other_sess->loop_id, other_wwn);
4673 
4674 				other_sess->keep_nport_handle = 1;
4675 				if (other_sess->disc_state != DSC_DELETED)
4676 					*conflict_sess = other_sess;
4677 				qlt_schedule_sess_for_deletion(other_sess);
4678 			}
4679 			continue;
4680 		}
4681 
4682 		/* find other sess with nport handle collision */
4683 		if ((loop_id == other_sess->loop_id) &&
4684 			(loop_id != FC_NO_LOOP_ID)) {
4685 			ql_dbg(ql_dbg_disc, vha, 0x1000d,
4686 			       "Invalidating sess %p loop_id %d wwn %llx.\n",
4687 			       other_sess, other_sess->loop_id, other_wwn);
4688 
4689 			/* Same loop_id but different s_id
4690 			 * Ok to kill and logout */
4691 			qlt_schedule_sess_for_deletion(other_sess);
4692 		}
4693 	}
4694 
4695 	return sess;
4696 }
4697 
4698 /* Abort any commands for this s_id waiting on qla_tgt_wq workqueue */
4699 static int abort_cmds_for_s_id(struct scsi_qla_host *vha, port_id_t *s_id)
4700 {
4701 	struct qla_tgt_sess_op *op;
4702 	struct qla_tgt_cmd *cmd;
4703 	uint32_t key;
4704 	int count = 0;
4705 	unsigned long flags;
4706 
4707 	key = (((u32)s_id->b.domain << 16) |
4708 	       ((u32)s_id->b.area   <<  8) |
4709 	       ((u32)s_id->b.al_pa));
4710 
4711 	spin_lock_irqsave(&vha->cmd_list_lock, flags);
4712 	list_for_each_entry(op, &vha->unknown_atio_list, cmd_list) {
4713 		uint32_t op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
4714 
4715 		if (op_key == key) {
4716 			op->aborted = true;
4717 			count++;
4718 		}
4719 	}
4720 
4721 	list_for_each_entry(cmd, &vha->qla_cmd_list, cmd_list) {
4722 		uint32_t cmd_key = sid_to_key(cmd->atio.u.isp24.fcp_hdr.s_id);
4723 
4724 		if (cmd_key == key) {
4725 			cmd->aborted = 1;
4726 			count++;
4727 		}
4728 	}
4729 	spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
4730 
4731 	return count;
4732 }
4733 
4734 static int qlt_handle_login(struct scsi_qla_host *vha,
4735     struct imm_ntfy_from_isp *iocb)
4736 {
4737 	struct fc_port *sess = NULL, *conflict_sess = NULL;
4738 	uint64_t wwn;
4739 	port_id_t port_id;
4740 	uint16_t loop_id, wd3_lo;
4741 	int res = 0;
4742 	struct qlt_plogi_ack_t *pla;
4743 	unsigned long flags;
4744 
4745 	lockdep_assert_held(&vha->hw->hardware_lock);
4746 
4747 	wwn = wwn_to_u64(iocb->u.isp24.port_name);
4748 
4749 	port_id.b.domain = iocb->u.isp24.port_id[2];
4750 	port_id.b.area   = iocb->u.isp24.port_id[1];
4751 	port_id.b.al_pa  = iocb->u.isp24.port_id[0];
4752 	port_id.b.rsvd_1 = 0;
4753 
4754 	loop_id = le16_to_cpu(iocb->u.isp24.nport_handle);
4755 
4756 	/* Mark all stale commands sitting in qla_tgt_wq for deletion */
4757 	abort_cmds_for_s_id(vha, &port_id);
4758 
4759 	if (wwn) {
4760 		spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
4761 		sess = qlt_find_sess_invalidate_other(vha, wwn,
4762 		    port_id, loop_id, &conflict_sess);
4763 		spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
4764 	} else {
4765 		ql_dbg(ql_dbg_disc, vha, 0xffff,
4766 		    "%s %d Term INOT due to WWN=0 lid=%d, NportID %06X ",
4767 		    __func__, __LINE__, loop_id, port_id.b24);
4768 		qlt_send_term_imm_notif(vha, iocb, 1);
4769 		goto out;
4770 	}
4771 
4772 	if (IS_SW_RESV_ADDR(port_id)) {
4773 		res = 1;
4774 		goto out;
4775 	}
4776 
4777 	if (vha->hw->flags.edif_enabled &&
4778 	    !(vha->e_dbell.db_flags & EDB_ACTIVE) &&
4779 	    iocb->u.isp24.status_subcode == ELS_PLOGI &&
4780 	    !(le16_to_cpu(iocb->u.isp24.flags) & NOTIFY24XX_FLAGS_FCSP)) {
4781 		ql_dbg(ql_dbg_disc, vha, 0xffff,
4782 			"%s %d Term INOT due to app not available lid=%d, NportID %06X ",
4783 			__func__, __LINE__, loop_id, port_id.b24);
4784 		qlt_send_term_imm_notif(vha, iocb, 1);
4785 		goto out;
4786 	}
4787 
4788 	if (vha->hw->flags.edif_enabled) {
4789 		if (DBELL_INACTIVE(vha)) {
4790 			ql_dbg(ql_dbg_disc, vha, 0xffff,
4791 			       "%s %d Term INOT due to app not started lid=%d, NportID %06X ",
4792 			       __func__, __LINE__, loop_id, port_id.b24);
4793 			qlt_send_term_imm_notif(vha, iocb, 1);
4794 			goto out;
4795 		} else if (iocb->u.isp24.status_subcode == ELS_PLOGI &&
4796 			   !(le16_to_cpu(iocb->u.isp24.flags) & NOTIFY24XX_FLAGS_FCSP)) {
4797 			ql_dbg(ql_dbg_disc, vha, 0xffff,
4798 			       "%s %d Term INOT due to unsecure lid=%d, NportID %06X ",
4799 			       __func__, __LINE__, loop_id, port_id.b24);
4800 			qlt_send_term_imm_notif(vha, iocb, 1);
4801 			goto out;
4802 		}
4803 	}
4804 
4805 	pla = qlt_plogi_ack_find_add(vha, &port_id, iocb);
4806 	if (!pla) {
4807 		ql_dbg(ql_dbg_disc + ql_dbg_verbose, vha, 0xffff,
4808 		    "%s %d %8phC Term INOT due to mem alloc fail",
4809 		    __func__, __LINE__,
4810 		    iocb->u.isp24.port_name);
4811 		qlt_send_term_imm_notif(vha, iocb, 1);
4812 		goto out;
4813 	}
4814 
4815 	if (conflict_sess) {
4816 		conflict_sess->login_gen++;
4817 		qlt_plogi_ack_link(vha, pla, conflict_sess,
4818 		    QLT_PLOGI_LINK_CONFLICT);
4819 	}
4820 
4821 	if (!sess) {
4822 		pla->ref_count++;
4823 		ql_dbg(ql_dbg_disc, vha, 0xffff,
4824 		    "%s %d %8phC post new sess\n",
4825 		    __func__, __LINE__, iocb->u.isp24.port_name);
4826 		if (iocb->u.isp24.status_subcode == ELS_PLOGI)
4827 			qla24xx_post_newsess_work(vha, &port_id,
4828 			    iocb->u.isp24.port_name,
4829 			    iocb->u.isp24.u.plogi.node_name,
4830 			    pla, 0);
4831 		else
4832 			qla24xx_post_newsess_work(vha, &port_id,
4833 			    iocb->u.isp24.port_name, NULL,
4834 			    pla, 0);
4835 
4836 		goto out;
4837 	}
4838 
4839 	if (sess->disc_state == DSC_UPD_FCPORT) {
4840 		u16 sec;
4841 
4842 		/*
4843 		 * Remote port registration is still going on from
4844 		 * previous login. Allow it to finish before we
4845 		 * accept the new login.
4846 		 */
4847 		sess->next_disc_state = DSC_DELETE_PEND;
4848 		sec = jiffies_to_msecs(jiffies -
4849 		    sess->jiffies_at_registration) / 1000;
4850 		if (sess->sec_since_registration < sec && sec &&
4851 		    !(sec % 5)) {
4852 			sess->sec_since_registration = sec;
4853 			ql_dbg(ql_dbg_disc, vha, 0xffff,
4854 			    "%s %8phC - Slow Rport registration (%d Sec)\n",
4855 			    __func__, sess->port_name, sec);
4856 		}
4857 
4858 		if (!conflict_sess) {
4859 			list_del(&pla->list);
4860 			kmem_cache_free(qla_tgt_plogi_cachep, pla);
4861 		}
4862 
4863 		qlt_send_term_imm_notif(vha, iocb, 1);
4864 		goto out;
4865 	}
4866 
4867 	qlt_plogi_ack_link(vha, pla, sess, QLT_PLOGI_LINK_SAME_WWN);
4868 	sess->d_id = port_id;
4869 	sess->login_gen++;
4870 	sess->loop_id = loop_id;
4871 
4872 	if (iocb->u.isp24.status_subcode == ELS_PLOGI) {
4873 		/* remote port has assigned Port ID */
4874 		if (N2N_TOPO(vha->hw) && fcport_is_bigger(sess))
4875 			vha->d_id = sess->d_id;
4876 
4877 		ql_dbg(ql_dbg_disc, vha, 0xffff,
4878 		    "%s %8phC - send port online\n",
4879 		    __func__, sess->port_name);
4880 
4881 		qla2x00_post_aen_work(vha, FCH_EVT_PORT_ONLINE,
4882 		    sess->d_id.b24);
4883 	}
4884 
4885 	if (iocb->u.isp24.status_subcode == ELS_PRLI) {
4886 		sess->fw_login_state = DSC_LS_PRLI_PEND;
4887 		sess->local = 0;
4888 		sess->loop_id = loop_id;
4889 		sess->d_id = port_id;
4890 		sess->fw_login_state = DSC_LS_PRLI_PEND;
4891 		wd3_lo = le16_to_cpu(iocb->u.isp24.u.prli.wd3_lo);
4892 
4893 		if (wd3_lo & BIT_7)
4894 			sess->conf_compl_supported = 1;
4895 
4896 		if ((wd3_lo & BIT_4) == 0)
4897 			sess->port_type = FCT_INITIATOR;
4898 		else
4899 			sess->port_type = FCT_TARGET;
4900 
4901 	} else
4902 		sess->fw_login_state = DSC_LS_PLOGI_PEND;
4903 
4904 
4905 	ql_dbg(ql_dbg_disc, vha, 0x20f9,
4906 	    "%s %d %8phC  DS %d\n",
4907 	    __func__, __LINE__, sess->port_name, sess->disc_state);
4908 
4909 	switch (sess->disc_state) {
4910 	case DSC_DELETED:
4911 	case DSC_LOGIN_PEND:
4912 		qlt_plogi_ack_unref(vha, pla);
4913 		break;
4914 
4915 	default:
4916 		/*
4917 		 * Under normal circumstances we want to release nport handle
4918 		 * during LOGO process to avoid nport handle leaks inside FW.
4919 		 * The exception is when LOGO is done while another PLOGI with
4920 		 * the same nport handle is waiting as might be the case here.
4921 		 * Note: there is always a possibily of a race where session
4922 		 * deletion has already started for other reasons (e.g. ACL
4923 		 * removal) and now PLOGI arrives:
4924 		 * 1. if PLOGI arrived in FW after nport handle has been freed,
4925 		 *    FW must have assigned this PLOGI a new/same handle and we
4926 		 *    can proceed ACK'ing it as usual when session deletion
4927 		 *    completes.
4928 		 * 2. if PLOGI arrived in FW before LOGO with LCF_FREE_NPORT
4929 		 *    bit reached it, the handle has now been released. We'll
4930 		 *    get an error when we ACK this PLOGI. Nothing will be sent
4931 		 *    back to initiator. Initiator should eventually retry
4932 		 *    PLOGI and situation will correct itself.
4933 		 */
4934 		sess->keep_nport_handle = ((sess->loop_id == loop_id) &&
4935 		    (sess->d_id.b24 == port_id.b24));
4936 
4937 		ql_dbg(ql_dbg_disc, vha, 0x20f9,
4938 		    "%s %d %8phC post del sess\n",
4939 		    __func__, __LINE__, sess->port_name);
4940 
4941 
4942 		qlt_schedule_sess_for_deletion(sess);
4943 		break;
4944 	}
4945 out:
4946 	return res;
4947 }
4948 
4949 /*
4950  * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
4951  */
4952 static int qlt_24xx_handle_els(struct scsi_qla_host *vha,
4953 	struct imm_ntfy_from_isp *iocb)
4954 {
4955 	struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4956 	struct qla_hw_data *ha = vha->hw;
4957 	struct fc_port *sess = NULL, *conflict_sess = NULL;
4958 	uint64_t wwn;
4959 	port_id_t port_id;
4960 	uint16_t loop_id;
4961 	uint16_t wd3_lo;
4962 	int res = 0;
4963 	unsigned long flags;
4964 
4965 	lockdep_assert_held(&ha->hardware_lock);
4966 
4967 	wwn = wwn_to_u64(iocb->u.isp24.port_name);
4968 
4969 	port_id.b.domain = iocb->u.isp24.port_id[2];
4970 	port_id.b.area   = iocb->u.isp24.port_id[1];
4971 	port_id.b.al_pa  = iocb->u.isp24.port_id[0];
4972 	port_id.b.rsvd_1 = 0;
4973 
4974 	loop_id = le16_to_cpu(iocb->u.isp24.nport_handle);
4975 
4976 	ql_dbg(ql_dbg_disc, vha, 0xf026,
4977 	    "qla_target(%d): Port ID: %02x:%02x:%02x ELS opcode: 0x%02x lid %d %8phC\n",
4978 	    vha->vp_idx, iocb->u.isp24.port_id[2],
4979 		iocb->u.isp24.port_id[1], iocb->u.isp24.port_id[0],
4980 		   iocb->u.isp24.status_subcode, loop_id,
4981 		iocb->u.isp24.port_name);
4982 
4983 	/* res = 1 means ack at the end of thread
4984 	 * res = 0 means ack async/later.
4985 	 */
4986 	switch (iocb->u.isp24.status_subcode) {
4987 	case ELS_PLOGI:
4988 		res = qlt_handle_login(vha, iocb);
4989 		break;
4990 
4991 	case ELS_PRLI:
4992 		if (N2N_TOPO(ha)) {
4993 			sess = qla2x00_find_fcport_by_wwpn(vha,
4994 			    iocb->u.isp24.port_name, 1);
4995 
4996 			if (vha->hw->flags.edif_enabled && sess &&
4997 			    (!(sess->flags & FCF_FCSP_DEVICE) ||
4998 			     !sess->edif.authok)) {
4999 				ql_dbg(ql_dbg_disc, vha, 0xffff,
5000 				       "%s %d %8phC Term PRLI due to unauthorize PRLI\n",
5001 				       __func__, __LINE__, iocb->u.isp24.port_name);
5002 				qlt_send_term_imm_notif(vha, iocb, 1);
5003 				break;
5004 			}
5005 
5006 			if (sess && sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN]) {
5007 				ql_dbg(ql_dbg_disc, vha, 0xffff,
5008 				    "%s %d %8phC Term PRLI due to PLOGI ACK not completed\n",
5009 				    __func__, __LINE__,
5010 				    iocb->u.isp24.port_name);
5011 				qlt_send_term_imm_notif(vha, iocb, 1);
5012 				break;
5013 			}
5014 
5015 			res = qlt_handle_login(vha, iocb);
5016 			break;
5017 		}
5018 
5019 		if (IS_SW_RESV_ADDR(port_id)) {
5020 			res = 1;
5021 			break;
5022 		}
5023 
5024 		wd3_lo = le16_to_cpu(iocb->u.isp24.u.prli.wd3_lo);
5025 
5026 		if (wwn) {
5027 			spin_lock_irqsave(&tgt->ha->tgt.sess_lock, flags);
5028 			sess = qlt_find_sess_invalidate_other(vha, wwn, port_id,
5029 				loop_id, &conflict_sess);
5030 			spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock, flags);
5031 		}
5032 
5033 		if (conflict_sess) {
5034 			switch (conflict_sess->disc_state) {
5035 			case DSC_DELETED:
5036 			case DSC_DELETE_PEND:
5037 				break;
5038 			default:
5039 				ql_dbg(ql_dbg_tgt_mgt, vha, 0xf09b,
5040 				    "PRLI with conflicting sess %p port %8phC\n",
5041 				    conflict_sess, conflict_sess->port_name);
5042 				conflict_sess->fw_login_state =
5043 				    DSC_LS_PORT_UNAVAIL;
5044 				qlt_send_term_imm_notif(vha, iocb, 1);
5045 				res = 0;
5046 				break;
5047 			}
5048 		}
5049 
5050 		if (sess != NULL) {
5051 			bool delete = false;
5052 			int sec;
5053 
5054 			if (vha->hw->flags.edif_enabled && sess &&
5055 			    (!(sess->flags & FCF_FCSP_DEVICE) ||
5056 			     !sess->edif.authok)) {
5057 				ql_dbg(ql_dbg_disc, vha, 0xffff,
5058 				       "%s %d %8phC Term PRLI due to unauthorize prli\n",
5059 				       __func__, __LINE__, iocb->u.isp24.port_name);
5060 				qlt_send_term_imm_notif(vha, iocb, 1);
5061 				break;
5062 			}
5063 
5064 			spin_lock_irqsave(&tgt->ha->tgt.sess_lock, flags);
5065 			switch (sess->fw_login_state) {
5066 			case DSC_LS_PLOGI_PEND:
5067 			case DSC_LS_PLOGI_COMP:
5068 			case DSC_LS_PRLI_COMP:
5069 				break;
5070 			default:
5071 				delete = true;
5072 				break;
5073 			}
5074 
5075 			switch (sess->disc_state) {
5076 			case DSC_UPD_FCPORT:
5077 				spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock,
5078 				    flags);
5079 
5080 				sec = jiffies_to_msecs(jiffies -
5081 				    sess->jiffies_at_registration)/1000;
5082 				if (sess->sec_since_registration < sec && sec &&
5083 				    !(sec % 5)) {
5084 					sess->sec_since_registration = sec;
5085 					ql_dbg(ql_dbg_disc, sess->vha, 0xffff,
5086 					    "%s %8phC : Slow Rport registration(%d Sec)\n",
5087 					    __func__, sess->port_name, sec);
5088 				}
5089 				qlt_send_term_imm_notif(vha, iocb, 1);
5090 				return 0;
5091 
5092 			case DSC_LOGIN_PEND:
5093 			case DSC_GPDB:
5094 			case DSC_LOGIN_COMPLETE:
5095 			case DSC_ADISC:
5096 				delete = false;
5097 				break;
5098 			default:
5099 				break;
5100 			}
5101 
5102 			if (delete) {
5103 				spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock,
5104 				    flags);
5105 				/*
5106 				 * Impatient initiator sent PRLI before last
5107 				 * PLOGI could finish. Will force him to re-try,
5108 				 * while last one finishes.
5109 				 */
5110 				ql_log(ql_log_warn, sess->vha, 0xf095,
5111 				    "sess %p PRLI received, before plogi ack.\n",
5112 				    sess);
5113 				qlt_send_term_imm_notif(vha, iocb, 1);
5114 				res = 0;
5115 				break;
5116 			}
5117 
5118 			/*
5119 			 * This shouldn't happen under normal circumstances,
5120 			 * since we have deleted the old session during PLOGI
5121 			 */
5122 			ql_dbg(ql_dbg_tgt_mgt, vha, 0xf096,
5123 			    "PRLI (loop_id %#04x) for existing sess %p (loop_id %#04x)\n",
5124 			    sess->loop_id, sess, iocb->u.isp24.nport_handle);
5125 
5126 			sess->local = 0;
5127 			sess->loop_id = loop_id;
5128 			sess->d_id = port_id;
5129 			sess->fw_login_state = DSC_LS_PRLI_PEND;
5130 
5131 			if (wd3_lo & BIT_7)
5132 				sess->conf_compl_supported = 1;
5133 
5134 			if ((wd3_lo & BIT_4) == 0)
5135 				sess->port_type = FCT_INITIATOR;
5136 			else
5137 				sess->port_type = FCT_TARGET;
5138 
5139 			spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock, flags);
5140 		}
5141 		res = 1; /* send notify ack */
5142 
5143 		/* Make session global (not used in fabric mode) */
5144 		if (ha->current_topology != ISP_CFG_F) {
5145 			if (sess) {
5146 				ql_dbg(ql_dbg_disc, vha, 0x20fa,
5147 				    "%s %d %8phC post nack\n",
5148 				    __func__, __LINE__, sess->port_name);
5149 				qla24xx_post_nack_work(vha, sess, iocb,
5150 					SRB_NACK_PRLI);
5151 				res = 0;
5152 			} else {
5153 				set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
5154 				set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
5155 				qla2xxx_wake_dpc(vha);
5156 			}
5157 		} else {
5158 			if (sess) {
5159 				ql_dbg(ql_dbg_disc, vha, 0x20fb,
5160 				    "%s %d %8phC post nack\n",
5161 				    __func__, __LINE__, sess->port_name);
5162 				qla24xx_post_nack_work(vha, sess, iocb,
5163 					SRB_NACK_PRLI);
5164 				res = 0;
5165 			}
5166 		}
5167 		break;
5168 
5169 	case ELS_TPRLO:
5170 		if (le16_to_cpu(iocb->u.isp24.flags) &
5171 			NOTIFY24XX_FLAGS_GLOBAL_TPRLO) {
5172 			loop_id = 0xFFFF;
5173 			qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS);
5174 			res = 1;
5175 			break;
5176 		}
5177 		fallthrough;
5178 	case ELS_LOGO:
5179 	case ELS_PRLO:
5180 		spin_lock_irqsave(&ha->tgt.sess_lock, flags);
5181 		sess = qla2x00_find_fcport_by_loopid(vha, loop_id);
5182 		spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
5183 
5184 		if (sess) {
5185 			sess->login_gen++;
5186 			sess->fw_login_state = DSC_LS_LOGO_PEND;
5187 			sess->logo_ack_needed = 1;
5188 			memcpy(sess->iocb, iocb, IOCB_SIZE);
5189 		}
5190 
5191 		res = qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS);
5192 
5193 		ql_dbg(ql_dbg_disc, vha, 0x20fc,
5194 		    "%s: logo %llx res %d sess %p ",
5195 		    __func__, wwn, res, sess);
5196 		if (res == 0) {
5197 			/*
5198 			 * cmd went upper layer, look for qlt_xmit_tm_rsp()
5199 			 * for LOGO_ACK & sess delete
5200 			 */
5201 			BUG_ON(!sess);
5202 			res = 0;
5203 		} else {
5204 			/* cmd did not go to upper layer. */
5205 			if (sess) {
5206 				qlt_schedule_sess_for_deletion(sess);
5207 				res = 0;
5208 			}
5209 			/* else logo will be ack */
5210 		}
5211 		break;
5212 	case ELS_PDISC:
5213 	case ELS_ADISC:
5214 	{
5215 		struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5216 
5217 		if (tgt->link_reinit_iocb_pending) {
5218 			qlt_send_notify_ack(ha->base_qpair,
5219 			    &tgt->link_reinit_iocb, 0, 0, 0, 0, 0, 0);
5220 			tgt->link_reinit_iocb_pending = 0;
5221 		}
5222 
5223 		sess = qla2x00_find_fcport_by_wwpn(vha,
5224 		    iocb->u.isp24.port_name, 1);
5225 		if (sess) {
5226 			ql_dbg(ql_dbg_disc, vha, 0x20fd,
5227 				"sess %p lid %d|%d DS %d LS %d\n",
5228 				sess, sess->loop_id, loop_id,
5229 				sess->disc_state, sess->fw_login_state);
5230 		}
5231 
5232 		res = 1; /* send notify ack */
5233 		break;
5234 	}
5235 
5236 	case ELS_FLOGI:	/* should never happen */
5237 	default:
5238 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf061,
5239 		    "qla_target(%d): Unsupported ELS command %x "
5240 		    "received\n", vha->vp_idx, iocb->u.isp24.status_subcode);
5241 		res = qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS);
5242 		break;
5243 	}
5244 
5245 	ql_dbg(ql_dbg_disc, vha, 0xf026,
5246 	    "qla_target(%d): Exit ELS opcode: 0x%02x res %d\n",
5247 	    vha->vp_idx, iocb->u.isp24.status_subcode, res);
5248 
5249 	return res;
5250 }
5251 
5252 /*
5253  * ha->hardware_lock supposed to be held on entry.
5254  * Might drop it, then reacquire.
5255  */
5256 static void qlt_handle_imm_notify(struct scsi_qla_host *vha,
5257 	struct imm_ntfy_from_isp *iocb)
5258 {
5259 	struct qla_hw_data *ha = vha->hw;
5260 	uint32_t add_flags = 0;
5261 	int send_notify_ack = 1;
5262 	uint16_t status;
5263 
5264 	lockdep_assert_held(&ha->hardware_lock);
5265 
5266 	status = le16_to_cpu(iocb->u.isp2x.status);
5267 	switch (status) {
5268 	case IMM_NTFY_LIP_RESET:
5269 	{
5270 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf032,
5271 		    "qla_target(%d): LIP reset (loop %#x), subcode %x\n",
5272 		    vha->vp_idx, le16_to_cpu(iocb->u.isp24.nport_handle),
5273 		    iocb->u.isp24.status_subcode);
5274 
5275 		if (qlt_reset(vha, iocb, QLA_TGT_ABORT_ALL) == 0)
5276 			send_notify_ack = 0;
5277 		break;
5278 	}
5279 
5280 	case IMM_NTFY_LIP_LINK_REINIT:
5281 	{
5282 		struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5283 
5284 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf033,
5285 		    "qla_target(%d): LINK REINIT (loop %#x, "
5286 		    "subcode %x)\n", vha->vp_idx,
5287 		    le16_to_cpu(iocb->u.isp24.nport_handle),
5288 		    iocb->u.isp24.status_subcode);
5289 		if (tgt->link_reinit_iocb_pending) {
5290 			qlt_send_notify_ack(ha->base_qpair,
5291 			    &tgt->link_reinit_iocb, 0, 0, 0, 0, 0, 0);
5292 		}
5293 		memcpy(&tgt->link_reinit_iocb, iocb, sizeof(*iocb));
5294 		tgt->link_reinit_iocb_pending = 1;
5295 		/*
5296 		 * QLogic requires to wait after LINK REINIT for possible
5297 		 * PDISC or ADISC ELS commands
5298 		 */
5299 		send_notify_ack = 0;
5300 		break;
5301 	}
5302 
5303 	case IMM_NTFY_PORT_LOGOUT:
5304 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf034,
5305 		    "qla_target(%d): Port logout (loop "
5306 		    "%#x, subcode %x)\n", vha->vp_idx,
5307 		    le16_to_cpu(iocb->u.isp24.nport_handle),
5308 		    iocb->u.isp24.status_subcode);
5309 
5310 		if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS) == 0)
5311 			send_notify_ack = 0;
5312 		/* The sessions will be cleared in the callback, if needed */
5313 		break;
5314 
5315 	case IMM_NTFY_GLBL_TPRLO:
5316 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf035,
5317 		    "qla_target(%d): Global TPRLO (%x)\n", vha->vp_idx, status);
5318 		if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS) == 0)
5319 			send_notify_ack = 0;
5320 		/* The sessions will be cleared in the callback, if needed */
5321 		break;
5322 
5323 	case IMM_NTFY_PORT_CONFIG:
5324 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf036,
5325 		    "qla_target(%d): Port config changed (%x)\n", vha->vp_idx,
5326 		    status);
5327 		if (qlt_reset(vha, iocb, QLA_TGT_ABORT_ALL) == 0)
5328 			send_notify_ack = 0;
5329 		/* The sessions will be cleared in the callback, if needed */
5330 		break;
5331 
5332 	case IMM_NTFY_GLBL_LOGO:
5333 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06a,
5334 		    "qla_target(%d): Link failure detected\n",
5335 		    vha->vp_idx);
5336 		/* I_T nexus loss */
5337 		if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS) == 0)
5338 			send_notify_ack = 0;
5339 		break;
5340 
5341 	case IMM_NTFY_IOCB_OVERFLOW:
5342 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06b,
5343 		    "qla_target(%d): Cannot provide requested "
5344 		    "capability (IOCB overflowed the immediate notify "
5345 		    "resource count)\n", vha->vp_idx);
5346 		break;
5347 
5348 	case IMM_NTFY_ABORT_TASK:
5349 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf037,
5350 		    "qla_target(%d): Abort Task (S %08x I %#x -> "
5351 		    "L %#x)\n", vha->vp_idx,
5352 		    le16_to_cpu(iocb->u.isp2x.seq_id),
5353 		    GET_TARGET_ID(ha, (struct atio_from_isp *)iocb),
5354 		    le16_to_cpu(iocb->u.isp2x.lun));
5355 		if (qlt_abort_task(vha, iocb) == 0)
5356 			send_notify_ack = 0;
5357 		break;
5358 
5359 	case IMM_NTFY_RESOURCE:
5360 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06c,
5361 		    "qla_target(%d): Out of resources, host %ld\n",
5362 		    vha->vp_idx, vha->host_no);
5363 		break;
5364 
5365 	case IMM_NTFY_MSG_RX:
5366 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf038,
5367 		    "qla_target(%d): Immediate notify task %x\n",
5368 		    vha->vp_idx, iocb->u.isp2x.task_flags);
5369 		break;
5370 
5371 	case IMM_NTFY_ELS:
5372 		if (qlt_24xx_handle_els(vha, iocb) == 0)
5373 			send_notify_ack = 0;
5374 		break;
5375 	default:
5376 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06d,
5377 		    "qla_target(%d): Received unknown immediate "
5378 		    "notify status %x\n", vha->vp_idx, status);
5379 		break;
5380 	}
5381 
5382 	if (send_notify_ack)
5383 		qlt_send_notify_ack(ha->base_qpair, iocb, add_flags, 0, 0, 0,
5384 		    0, 0);
5385 }
5386 
5387 /*
5388  * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
5389  * This function sends busy to ISP 2xxx or 24xx.
5390  */
5391 static int __qlt_send_busy(struct qla_qpair *qpair,
5392 	struct atio_from_isp *atio, uint16_t status)
5393 {
5394 	struct scsi_qla_host *vha = qpair->vha;
5395 	struct ctio7_to_24xx *ctio24;
5396 	struct qla_hw_data *ha = vha->hw;
5397 	request_t *pkt;
5398 	struct fc_port *sess = NULL;
5399 	unsigned long flags;
5400 	u16 temp;
5401 	port_id_t id;
5402 
5403 	id = be_to_port_id(atio->u.isp24.fcp_hdr.s_id);
5404 
5405 	spin_lock_irqsave(&ha->tgt.sess_lock, flags);
5406 	sess = qla2x00_find_fcport_by_nportid(vha, &id, 1);
5407 	spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
5408 	if (!sess) {
5409 		qlt_send_term_exchange(qpair, NULL, atio, 1, 0);
5410 		return 0;
5411 	}
5412 	/* Sending marker isn't necessary, since we called from ISR */
5413 
5414 	pkt = (request_t *)__qla2x00_alloc_iocbs(qpair, NULL);
5415 	if (!pkt) {
5416 		ql_dbg(ql_dbg_io, vha, 0x3063,
5417 		    "qla_target(%d): %s failed: unable to allocate "
5418 		    "request packet", vha->vp_idx, __func__);
5419 		return -ENOMEM;
5420 	}
5421 
5422 	qpair->tgt_counters.num_q_full_sent++;
5423 	pkt->entry_count = 1;
5424 	pkt->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
5425 
5426 	ctio24 = (struct ctio7_to_24xx *)pkt;
5427 	ctio24->entry_type = CTIO_TYPE7;
5428 	ctio24->nport_handle = cpu_to_le16(sess->loop_id);
5429 	ctio24->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
5430 	ctio24->vp_index = vha->vp_idx;
5431 	ctio24->initiator_id = be_id_to_le(atio->u.isp24.fcp_hdr.s_id);
5432 	ctio24->exchange_addr = atio->u.isp24.exchange_addr;
5433 	temp = (atio->u.isp24.attr << 9) |
5434 		CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS |
5435 		CTIO7_FLAGS_DONT_RET_CTIO;
5436 	ctio24->u.status1.flags = cpu_to_le16(temp);
5437 	/*
5438 	 * CTIO from fw w/o se_cmd doesn't provide enough info to retry it,
5439 	 * if the explicit conformation is used.
5440 	 */
5441 	ctio24->u.status1.ox_id =
5442 		cpu_to_le16(be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id));
5443 	ctio24->u.status1.scsi_status = cpu_to_le16(status);
5444 
5445 	ctio24->u.status1.residual = cpu_to_le32(get_datalen_for_atio(atio));
5446 
5447 	if (ctio24->u.status1.residual != 0)
5448 		ctio24->u.status1.scsi_status |= cpu_to_le16(SS_RESIDUAL_UNDER);
5449 
5450 	/* Memory Barrier */
5451 	wmb();
5452 	if (qpair->reqq_start_iocbs)
5453 		qpair->reqq_start_iocbs(qpair);
5454 	else
5455 		qla2x00_start_iocbs(vha, qpair->req);
5456 	return 0;
5457 }
5458 
5459 /*
5460  * This routine is used to allocate a command for either a QFull condition
5461  * (ie reply SAM_STAT_BUSY) or to terminate an exchange that did not go
5462  * out previously.
5463  */
5464 static void
5465 qlt_alloc_qfull_cmd(struct scsi_qla_host *vha,
5466 	struct atio_from_isp *atio, uint16_t status, int qfull)
5467 {
5468 	struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5469 	struct qla_hw_data *ha = vha->hw;
5470 	struct fc_port *sess;
5471 	struct qla_tgt_cmd *cmd;
5472 	unsigned long flags;
5473 
5474 	if (unlikely(tgt->tgt_stop)) {
5475 		ql_dbg(ql_dbg_io, vha, 0x300a,
5476 			"New command while device %p is shutting down\n", tgt);
5477 		return;
5478 	}
5479 
5480 	if ((vha->hw->tgt.num_qfull_cmds_alloc + 1) > MAX_QFULL_CMDS_ALLOC) {
5481 		vha->hw->tgt.num_qfull_cmds_dropped++;
5482 		if (vha->hw->tgt.num_qfull_cmds_dropped >
5483 			vha->qla_stats.stat_max_qfull_cmds_dropped)
5484 			vha->qla_stats.stat_max_qfull_cmds_dropped =
5485 				vha->hw->tgt.num_qfull_cmds_dropped;
5486 
5487 		ql_dbg(ql_dbg_io, vha, 0x3068,
5488 			"qla_target(%d): %s: QFull CMD dropped[%d]\n",
5489 			vha->vp_idx, __func__,
5490 			vha->hw->tgt.num_qfull_cmds_dropped);
5491 
5492 		qlt_chk_exch_leak_thresh_hold(vha);
5493 		return;
5494 	}
5495 
5496 	sess = ha->tgt.tgt_ops->find_sess_by_s_id
5497 		(vha, atio->u.isp24.fcp_hdr.s_id);
5498 	if (!sess)
5499 		return;
5500 
5501 	cmd = ha->tgt.tgt_ops->get_cmd(sess);
5502 	if (!cmd) {
5503 		ql_dbg(ql_dbg_io, vha, 0x3009,
5504 			"qla_target(%d): %s: Allocation of cmd failed\n",
5505 			vha->vp_idx, __func__);
5506 
5507 		vha->hw->tgt.num_qfull_cmds_dropped++;
5508 		if (vha->hw->tgt.num_qfull_cmds_dropped >
5509 			vha->qla_stats.stat_max_qfull_cmds_dropped)
5510 			vha->qla_stats.stat_max_qfull_cmds_dropped =
5511 				vha->hw->tgt.num_qfull_cmds_dropped;
5512 
5513 		qlt_chk_exch_leak_thresh_hold(vha);
5514 		return;
5515 	}
5516 
5517 	qlt_incr_num_pend_cmds(vha);
5518 	INIT_LIST_HEAD(&cmd->cmd_list);
5519 	memcpy(&cmd->atio, atio, sizeof(*atio));
5520 
5521 	cmd->tgt = vha->vha_tgt.qla_tgt;
5522 	cmd->vha = vha;
5523 	cmd->reset_count = ha->base_qpair->chip_reset;
5524 	cmd->q_full = 1;
5525 	cmd->qpair = ha->base_qpair;
5526 
5527 	if (qfull) {
5528 		cmd->q_full = 1;
5529 		/* NOTE: borrowing the state field to carry the status */
5530 		cmd->state = status;
5531 	} else
5532 		cmd->term_exchg = 1;
5533 
5534 	spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
5535 	list_add_tail(&cmd->cmd_list, &vha->hw->tgt.q_full_list);
5536 
5537 	vha->hw->tgt.num_qfull_cmds_alloc++;
5538 	if (vha->hw->tgt.num_qfull_cmds_alloc >
5539 		vha->qla_stats.stat_max_qfull_cmds_alloc)
5540 		vha->qla_stats.stat_max_qfull_cmds_alloc =
5541 			vha->hw->tgt.num_qfull_cmds_alloc;
5542 	spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
5543 }
5544 
5545 int
5546 qlt_free_qfull_cmds(struct qla_qpair *qpair)
5547 {
5548 	struct scsi_qla_host *vha = qpair->vha;
5549 	struct qla_hw_data *ha = vha->hw;
5550 	unsigned long flags;
5551 	struct qla_tgt_cmd *cmd, *tcmd;
5552 	struct list_head free_list, q_full_list;
5553 	int rc = 0;
5554 
5555 	if (list_empty(&ha->tgt.q_full_list))
5556 		return 0;
5557 
5558 	INIT_LIST_HEAD(&free_list);
5559 	INIT_LIST_HEAD(&q_full_list);
5560 
5561 	spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
5562 	if (list_empty(&ha->tgt.q_full_list)) {
5563 		spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
5564 		return 0;
5565 	}
5566 
5567 	list_splice_init(&vha->hw->tgt.q_full_list, &q_full_list);
5568 	spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
5569 
5570 	spin_lock_irqsave(qpair->qp_lock_ptr, flags);
5571 	list_for_each_entry_safe(cmd, tcmd, &q_full_list, cmd_list) {
5572 		if (cmd->q_full)
5573 			/* cmd->state is a borrowed field to hold status */
5574 			rc = __qlt_send_busy(qpair, &cmd->atio, cmd->state);
5575 		else if (cmd->term_exchg)
5576 			rc = __qlt_send_term_exchange(qpair, NULL, &cmd->atio);
5577 
5578 		if (rc == -ENOMEM)
5579 			break;
5580 
5581 		if (cmd->q_full)
5582 			ql_dbg(ql_dbg_io, vha, 0x3006,
5583 			    "%s: busy sent for ox_id[%04x]\n", __func__,
5584 			    be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
5585 		else if (cmd->term_exchg)
5586 			ql_dbg(ql_dbg_io, vha, 0x3007,
5587 			    "%s: Term exchg sent for ox_id[%04x]\n", __func__,
5588 			    be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
5589 		else
5590 			ql_dbg(ql_dbg_io, vha, 0x3008,
5591 			    "%s: Unexpected cmd in QFull list %p\n", __func__,
5592 			    cmd);
5593 
5594 		list_move_tail(&cmd->cmd_list, &free_list);
5595 
5596 		/* piggy back on hardware_lock for protection */
5597 		vha->hw->tgt.num_qfull_cmds_alloc--;
5598 	}
5599 	spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
5600 
5601 	cmd = NULL;
5602 
5603 	list_for_each_entry_safe(cmd, tcmd, &free_list, cmd_list) {
5604 		list_del(&cmd->cmd_list);
5605 		/* This cmd was never sent to TCM.  There is no need
5606 		 * to schedule free or call free_cmd
5607 		 */
5608 		qlt_free_cmd(cmd);
5609 	}
5610 
5611 	if (!list_empty(&q_full_list)) {
5612 		spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
5613 		list_splice(&q_full_list, &vha->hw->tgt.q_full_list);
5614 		spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
5615 	}
5616 
5617 	return rc;
5618 }
5619 
5620 static void
5621 qlt_send_busy(struct qla_qpair *qpair, struct atio_from_isp *atio,
5622     uint16_t status)
5623 {
5624 	int rc = 0;
5625 	struct scsi_qla_host *vha = qpair->vha;
5626 
5627 	rc = __qlt_send_busy(qpair, atio, status);
5628 	if (rc == -ENOMEM)
5629 		qlt_alloc_qfull_cmd(vha, atio, status, 1);
5630 }
5631 
5632 static int
5633 qlt_chk_qfull_thresh_hold(struct scsi_qla_host *vha, struct qla_qpair *qpair,
5634 	struct atio_from_isp *atio, uint8_t ha_locked)
5635 {
5636 	struct qla_hw_data *ha = vha->hw;
5637 	unsigned long flags;
5638 
5639 	if (ha->tgt.num_pend_cmds < Q_FULL_THRESH_HOLD(ha))
5640 		return 0;
5641 
5642 	if (!ha_locked)
5643 		spin_lock_irqsave(&ha->hardware_lock, flags);
5644 	qlt_send_busy(qpair, atio, qla_sam_status);
5645 	if (!ha_locked)
5646 		spin_unlock_irqrestore(&ha->hardware_lock, flags);
5647 
5648 	return 1;
5649 }
5650 
5651 /* ha->hardware_lock supposed to be held on entry */
5652 /* called via callback from qla2xxx */
5653 static void qlt_24xx_atio_pkt(struct scsi_qla_host *vha,
5654 	struct atio_from_isp *atio, uint8_t ha_locked)
5655 {
5656 	struct qla_hw_data *ha = vha->hw;
5657 	struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5658 	int rc;
5659 	unsigned long flags = 0;
5660 
5661 	if (unlikely(tgt == NULL)) {
5662 		ql_dbg(ql_dbg_tgt, vha, 0x3064,
5663 		    "ATIO pkt, but no tgt (ha %p)", ha);
5664 		return;
5665 	}
5666 	/*
5667 	 * In tgt_stop mode we also should allow all requests to pass.
5668 	 * Otherwise, some commands can stuck.
5669 	 */
5670 
5671 	tgt->atio_irq_cmd_count++;
5672 
5673 	switch (atio->u.raw.entry_type) {
5674 	case ATIO_TYPE7:
5675 		if (unlikely(atio->u.isp24.exchange_addr ==
5676 			     cpu_to_le32(ATIO_EXCHANGE_ADDRESS_UNKNOWN))) {
5677 			ql_dbg(ql_dbg_io, vha, 0x3065,
5678 			    "qla_target(%d): ATIO_TYPE7 "
5679 			    "received with UNKNOWN exchange address, "
5680 			    "sending QUEUE_FULL\n", vha->vp_idx);
5681 			if (!ha_locked)
5682 				spin_lock_irqsave(&ha->hardware_lock, flags);
5683 			qlt_send_busy(ha->base_qpair, atio, qla_sam_status);
5684 			if (!ha_locked)
5685 				spin_unlock_irqrestore(&ha->hardware_lock,
5686 				    flags);
5687 			break;
5688 		}
5689 
5690 		if (likely(atio->u.isp24.fcp_cmnd.task_mgmt_flags == 0)) {
5691 			rc = qlt_chk_qfull_thresh_hold(vha, ha->base_qpair,
5692 			    atio, ha_locked);
5693 			if (rc != 0) {
5694 				tgt->atio_irq_cmd_count--;
5695 				return;
5696 			}
5697 			rc = qlt_handle_cmd_for_atio(vha, atio);
5698 		} else {
5699 			rc = qlt_handle_task_mgmt(vha, atio);
5700 		}
5701 		if (unlikely(rc != 0)) {
5702 			if (!ha_locked)
5703 				spin_lock_irqsave(&ha->hardware_lock, flags);
5704 			switch (rc) {
5705 			case -ENODEV:
5706 				ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5707 				    "qla_target: Unable to send command to target\n");
5708 				break;
5709 			case -EBADF:
5710 				ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5711 				    "qla_target: Unable to send command to target, sending TERM EXCHANGE for rsp\n");
5712 				qlt_send_term_exchange(ha->base_qpair, NULL,
5713 				    atio, 1, 0);
5714 				break;
5715 			case -EBUSY:
5716 				ql_dbg(ql_dbg_tgt, vha, 0xe060,
5717 				    "qla_target(%d): Unable to send command to target, sending BUSY status\n",
5718 				    vha->vp_idx);
5719 				qlt_send_busy(ha->base_qpair, atio,
5720 				    tc_sam_status);
5721 				break;
5722 			default:
5723 				ql_dbg(ql_dbg_tgt, vha, 0xe060,
5724 				    "qla_target(%d): Unable to send command to target, sending BUSY status\n",
5725 				    vha->vp_idx);
5726 				qlt_send_busy(ha->base_qpair, atio,
5727 				    qla_sam_status);
5728 				break;
5729 			}
5730 			if (!ha_locked)
5731 				spin_unlock_irqrestore(&ha->hardware_lock,
5732 				    flags);
5733 		}
5734 		break;
5735 
5736 	case IMMED_NOTIFY_TYPE:
5737 	{
5738 		if (unlikely(atio->u.isp2x.entry_status != 0)) {
5739 			ql_dbg(ql_dbg_tgt, vha, 0xe05b,
5740 			    "qla_target(%d): Received ATIO packet %x "
5741 			    "with error status %x\n", vha->vp_idx,
5742 			    atio->u.raw.entry_type,
5743 			    atio->u.isp2x.entry_status);
5744 			break;
5745 		}
5746 		ql_dbg(ql_dbg_tgt, vha, 0xe02e, "%s", "IMMED_NOTIFY ATIO");
5747 
5748 		if (!ha_locked)
5749 			spin_lock_irqsave(&ha->hardware_lock, flags);
5750 		qlt_handle_imm_notify(vha, (struct imm_ntfy_from_isp *)atio);
5751 		if (!ha_locked)
5752 			spin_unlock_irqrestore(&ha->hardware_lock, flags);
5753 		break;
5754 	}
5755 
5756 	default:
5757 		ql_dbg(ql_dbg_tgt, vha, 0xe05c,
5758 		    "qla_target(%d): Received unknown ATIO atio "
5759 		    "type %x\n", vha->vp_idx, atio->u.raw.entry_type);
5760 		break;
5761 	}
5762 
5763 	tgt->atio_irq_cmd_count--;
5764 }
5765 
5766 /*
5767  * qpair lock is assume to be held
5768  * rc = 0 : send terminate & abts respond
5769  * rc != 0: do not send term & abts respond
5770  */
5771 static int qlt_chk_unresolv_exchg(struct scsi_qla_host *vha,
5772     struct qla_qpair *qpair, struct abts_resp_from_24xx_fw *entry)
5773 {
5774 	struct qla_hw_data *ha = vha->hw;
5775 	int rc = 0;
5776 
5777 	/*
5778 	 * Detect unresolved exchange. If the same ABTS is unable
5779 	 * to terminate an existing command and the same ABTS loops
5780 	 * between FW & Driver, then force FW dump. Under 1 jiff,
5781 	 * we should see multiple loops.
5782 	 */
5783 	if (qpair->retry_term_exchg_addr == entry->exchange_addr_to_abort &&
5784 	    qpair->retry_term_jiff == jiffies) {
5785 		/* found existing exchange */
5786 		qpair->retry_term_cnt++;
5787 		if (qpair->retry_term_cnt >= 5) {
5788 			rc = -EIO;
5789 			qpair->retry_term_cnt = 0;
5790 			ql_log(ql_log_warn, vha, 0xffff,
5791 			    "Unable to send ABTS Respond. Dumping firmware.\n");
5792 			ql_dump_buffer(ql_dbg_tgt_mgt + ql_dbg_buffer,
5793 			    vha, 0xffff, (uint8_t *)entry, sizeof(*entry));
5794 
5795 			if (qpair == ha->base_qpair)
5796 				ha->isp_ops->fw_dump(vha);
5797 			else
5798 				qla2xxx_dump_fw(vha);
5799 
5800 			set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
5801 			qla2xxx_wake_dpc(vha);
5802 		}
5803 	} else if (qpair->retry_term_jiff != jiffies) {
5804 		qpair->retry_term_exchg_addr = entry->exchange_addr_to_abort;
5805 		qpair->retry_term_cnt = 0;
5806 		qpair->retry_term_jiff = jiffies;
5807 	}
5808 
5809 	return rc;
5810 }
5811 
5812 
5813 static void qlt_handle_abts_completion(struct scsi_qla_host *vha,
5814 	struct rsp_que *rsp, response_t *pkt)
5815 {
5816 	struct abts_resp_from_24xx_fw *entry =
5817 		(struct abts_resp_from_24xx_fw *)pkt;
5818 	u32 h = pkt->handle & ~QLA_TGT_HANDLE_MASK;
5819 	struct qla_tgt_mgmt_cmd *mcmd;
5820 	struct qla_hw_data *ha = vha->hw;
5821 
5822 	mcmd = qlt_ctio_to_cmd(vha, rsp, pkt->handle, pkt);
5823 	if (mcmd == NULL && h != QLA_TGT_SKIP_HANDLE) {
5824 		ql_dbg(ql_dbg_async, vha, 0xe064,
5825 		    "qla_target(%d): ABTS Comp without mcmd\n",
5826 		    vha->vp_idx);
5827 		return;
5828 	}
5829 
5830 	if (mcmd)
5831 		vha  = mcmd->vha;
5832 	vha->vha_tgt.qla_tgt->abts_resp_expected--;
5833 
5834 	ql_dbg(ql_dbg_tgt, vha, 0xe038,
5835 	    "ABTS_RESP_24XX: compl_status %x\n",
5836 	    entry->compl_status);
5837 
5838 	if (le16_to_cpu(entry->compl_status) != ABTS_RESP_COMPL_SUCCESS) {
5839 		if (le32_to_cpu(entry->error_subcode1) == 0x1E &&
5840 		    le32_to_cpu(entry->error_subcode2) == 0) {
5841 			if (qlt_chk_unresolv_exchg(vha, rsp->qpair, entry)) {
5842 				ha->tgt.tgt_ops->free_mcmd(mcmd);
5843 				return;
5844 			}
5845 			qlt_24xx_retry_term_exchange(vha, rsp->qpair,
5846 			    pkt, mcmd);
5847 		} else {
5848 			ql_dbg(ql_dbg_tgt, vha, 0xe063,
5849 			    "qla_target(%d): ABTS_RESP_24XX failed %x (subcode %x:%x)",
5850 			    vha->vp_idx, entry->compl_status,
5851 			    entry->error_subcode1,
5852 			    entry->error_subcode2);
5853 			ha->tgt.tgt_ops->free_mcmd(mcmd);
5854 		}
5855 	} else if (mcmd) {
5856 		ha->tgt.tgt_ops->free_mcmd(mcmd);
5857 	}
5858 }
5859 
5860 /* ha->hardware_lock supposed to be held on entry */
5861 /* called via callback from qla2xxx */
5862 static void qlt_response_pkt(struct scsi_qla_host *vha,
5863 	struct rsp_que *rsp, response_t *pkt)
5864 {
5865 	struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5866 
5867 	if (unlikely(tgt == NULL)) {
5868 		ql_dbg(ql_dbg_tgt, vha, 0xe05d,
5869 		    "qla_target(%d): Response pkt %x received, but no tgt (ha %p)\n",
5870 		    vha->vp_idx, pkt->entry_type, vha->hw);
5871 		return;
5872 	}
5873 
5874 	/*
5875 	 * In tgt_stop mode we also should allow all requests to pass.
5876 	 * Otherwise, some commands can stuck.
5877 	 */
5878 
5879 	switch (pkt->entry_type) {
5880 	case CTIO_CRC2:
5881 	case CTIO_TYPE7:
5882 	{
5883 		struct ctio7_from_24xx *entry = (struct ctio7_from_24xx *)pkt;
5884 
5885 		qlt_do_ctio_completion(vha, rsp, entry->handle,
5886 		    le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5887 		    entry);
5888 		break;
5889 	}
5890 
5891 	case ACCEPT_TGT_IO_TYPE:
5892 	{
5893 		struct atio_from_isp *atio = (struct atio_from_isp *)pkt;
5894 		int rc;
5895 
5896 		if (atio->u.isp2x.status !=
5897 		    cpu_to_le16(ATIO_CDB_VALID)) {
5898 			ql_dbg(ql_dbg_tgt, vha, 0xe05e,
5899 			    "qla_target(%d): ATIO with error "
5900 			    "status %x received\n", vha->vp_idx,
5901 			    le16_to_cpu(atio->u.isp2x.status));
5902 			break;
5903 		}
5904 
5905 		rc = qlt_chk_qfull_thresh_hold(vha, rsp->qpair, atio, 1);
5906 		if (rc != 0)
5907 			return;
5908 
5909 		rc = qlt_handle_cmd_for_atio(vha, atio);
5910 		if (unlikely(rc != 0)) {
5911 			switch (rc) {
5912 			case -ENODEV:
5913 				ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5914 				    "qla_target: Unable to send command to target\n");
5915 				break;
5916 			case -EBADF:
5917 				ql_dbg(ql_dbg_tgt, vha, 0xe05f,
5918 				    "qla_target: Unable to send command to target, sending TERM EXCHANGE for rsp\n");
5919 				qlt_send_term_exchange(rsp->qpair, NULL,
5920 				    atio, 1, 0);
5921 				break;
5922 			case -EBUSY:
5923 				ql_dbg(ql_dbg_tgt, vha, 0xe060,
5924 				    "qla_target(%d): Unable to send command to target, sending BUSY status\n",
5925 				    vha->vp_idx);
5926 				qlt_send_busy(rsp->qpair, atio,
5927 				    tc_sam_status);
5928 				break;
5929 			default:
5930 				ql_dbg(ql_dbg_tgt, vha, 0xe060,
5931 				    "qla_target(%d): Unable to send command to target, sending BUSY status\n",
5932 				    vha->vp_idx);
5933 				qlt_send_busy(rsp->qpair, atio,
5934 				    qla_sam_status);
5935 				break;
5936 			}
5937 		}
5938 	}
5939 	break;
5940 
5941 	case CONTINUE_TGT_IO_TYPE:
5942 	{
5943 		struct ctio_to_2xxx *entry = (struct ctio_to_2xxx *)pkt;
5944 
5945 		qlt_do_ctio_completion(vha, rsp, entry->handle,
5946 		    le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5947 		    entry);
5948 		break;
5949 	}
5950 
5951 	case CTIO_A64_TYPE:
5952 	{
5953 		struct ctio_to_2xxx *entry = (struct ctio_to_2xxx *)pkt;
5954 
5955 		qlt_do_ctio_completion(vha, rsp, entry->handle,
5956 		    le16_to_cpu(entry->status)|(pkt->entry_status << 16),
5957 		    entry);
5958 		break;
5959 	}
5960 
5961 	case IMMED_NOTIFY_TYPE:
5962 		ql_dbg(ql_dbg_tgt, vha, 0xe035, "%s", "IMMED_NOTIFY\n");
5963 		qlt_handle_imm_notify(vha, (struct imm_ntfy_from_isp *)pkt);
5964 		break;
5965 
5966 	case NOTIFY_ACK_TYPE:
5967 		if (tgt->notify_ack_expected > 0) {
5968 			struct nack_to_isp *entry = (struct nack_to_isp *)pkt;
5969 
5970 			ql_dbg(ql_dbg_tgt, vha, 0xe036,
5971 			    "NOTIFY_ACK seq %08x status %x\n",
5972 			    le16_to_cpu(entry->u.isp2x.seq_id),
5973 			    le16_to_cpu(entry->u.isp2x.status));
5974 			tgt->notify_ack_expected--;
5975 			if (entry->u.isp2x.status !=
5976 			    cpu_to_le16(NOTIFY_ACK_SUCCESS)) {
5977 				ql_dbg(ql_dbg_tgt, vha, 0xe061,
5978 				    "qla_target(%d): NOTIFY_ACK "
5979 				    "failed %x\n", vha->vp_idx,
5980 				    le16_to_cpu(entry->u.isp2x.status));
5981 			}
5982 		} else {
5983 			ql_dbg(ql_dbg_tgt, vha, 0xe062,
5984 			    "qla_target(%d): Unexpected NOTIFY_ACK received\n",
5985 			    vha->vp_idx);
5986 		}
5987 		break;
5988 
5989 	case ABTS_RECV_24XX:
5990 		ql_dbg(ql_dbg_tgt, vha, 0xe037,
5991 		    "ABTS_RECV_24XX: instance %d\n", vha->vp_idx);
5992 		qlt_24xx_handle_abts(vha, (struct abts_recv_from_24xx *)pkt);
5993 		break;
5994 
5995 	case ABTS_RESP_24XX:
5996 		if (tgt->abts_resp_expected > 0) {
5997 			qlt_handle_abts_completion(vha, rsp, pkt);
5998 		} else {
5999 			ql_dbg(ql_dbg_tgt, vha, 0xe064,
6000 			    "qla_target(%d): Unexpected ABTS_RESP_24XX "
6001 			    "received\n", vha->vp_idx);
6002 		}
6003 		break;
6004 
6005 	default:
6006 		ql_dbg(ql_dbg_tgt, vha, 0xe065,
6007 		    "qla_target(%d): Received unknown response pkt "
6008 		    "type %x\n", vha->vp_idx, pkt->entry_type);
6009 		break;
6010 	}
6011 
6012 }
6013 
6014 /*
6015  * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
6016  */
6017 void qlt_async_event(uint16_t code, struct scsi_qla_host *vha,
6018 	uint16_t *mailbox)
6019 {
6020 	struct qla_hw_data *ha = vha->hw;
6021 	struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
6022 	int login_code;
6023 
6024 	if (!tgt || tgt->tgt_stop || tgt->tgt_stopped)
6025 		return;
6026 
6027 	if (((code == MBA_POINT_TO_POINT) || (code == MBA_CHG_IN_CONNECTION)) &&
6028 	    IS_QLA2100(ha))
6029 		return;
6030 	/*
6031 	 * In tgt_stop mode we also should allow all requests to pass.
6032 	 * Otherwise, some commands can stuck.
6033 	 */
6034 
6035 
6036 	switch (code) {
6037 	case MBA_RESET:			/* Reset */
6038 	case MBA_SYSTEM_ERR:		/* System Error */
6039 	case MBA_REQ_TRANSFER_ERR:	/* Request Transfer Error */
6040 	case MBA_RSP_TRANSFER_ERR:	/* Response Transfer Error */
6041 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03a,
6042 		    "qla_target(%d): System error async event %#x "
6043 		    "occurred", vha->vp_idx, code);
6044 		break;
6045 	case MBA_WAKEUP_THRES:		/* Request Queue Wake-up. */
6046 		set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
6047 		break;
6048 
6049 	case MBA_LOOP_UP:
6050 	{
6051 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03b,
6052 		    "qla_target(%d): Async LOOP_UP occurred "
6053 		    "(m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)", vha->vp_idx,
6054 		    mailbox[0], mailbox[1], mailbox[2], mailbox[3]);
6055 		if (tgt->link_reinit_iocb_pending) {
6056 			qlt_send_notify_ack(ha->base_qpair,
6057 			    &tgt->link_reinit_iocb,
6058 			    0, 0, 0, 0, 0, 0);
6059 			tgt->link_reinit_iocb_pending = 0;
6060 		}
6061 		break;
6062 	}
6063 
6064 	case MBA_LIP_OCCURRED:
6065 	case MBA_LOOP_DOWN:
6066 	case MBA_LIP_RESET:
6067 	case MBA_RSCN_UPDATE:
6068 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03c,
6069 		    "qla_target(%d): Async event %#x occurred "
6070 		    "(m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)", vha->vp_idx, code,
6071 		    mailbox[0], mailbox[1], mailbox[2], mailbox[3]);
6072 		break;
6073 
6074 	case MBA_REJECTED_FCP_CMD:
6075 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf017,
6076 		    "qla_target(%d): Async event LS_REJECT occurred (m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)",
6077 		    vha->vp_idx,
6078 		    mailbox[0], mailbox[1], mailbox[2], mailbox[3]);
6079 
6080 		if (mailbox[3] == 1) {
6081 			/* exchange starvation. */
6082 			vha->hw->exch_starvation++;
6083 			if (vha->hw->exch_starvation > 5) {
6084 				ql_log(ql_log_warn, vha, 0xd03a,
6085 				    "Exchange starvation-. Resetting RISC\n");
6086 
6087 				vha->hw->exch_starvation = 0;
6088 				if (IS_P3P_TYPE(vha->hw))
6089 					set_bit(FCOE_CTX_RESET_NEEDED,
6090 					    &vha->dpc_flags);
6091 				else
6092 					set_bit(ISP_ABORT_NEEDED,
6093 					    &vha->dpc_flags);
6094 				qla2xxx_wake_dpc(vha);
6095 			}
6096 		}
6097 		break;
6098 
6099 	case MBA_PORT_UPDATE:
6100 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03d,
6101 		    "qla_target(%d): Port update async event %#x "
6102 		    "occurred: updating the ports database (m[0]=%x, m[1]=%x, "
6103 		    "m[2]=%x, m[3]=%x)", vha->vp_idx, code,
6104 		    mailbox[0], mailbox[1], mailbox[2], mailbox[3]);
6105 
6106 		login_code = mailbox[2];
6107 		if (login_code == 0x4) {
6108 			ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03e,
6109 			    "Async MB 2: Got PLOGI Complete\n");
6110 			vha->hw->exch_starvation = 0;
6111 		} else if (login_code == 0x7)
6112 			ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03f,
6113 			    "Async MB 2: Port Logged Out\n");
6114 		break;
6115 	default:
6116 		break;
6117 	}
6118 
6119 }
6120 
6121 static fc_port_t *qlt_get_port_database(struct scsi_qla_host *vha,
6122 	uint16_t loop_id)
6123 {
6124 	fc_port_t *fcport, *tfcp, *del;
6125 	int rc;
6126 	unsigned long flags;
6127 	u8 newfcport = 0;
6128 
6129 	fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
6130 	if (!fcport) {
6131 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06f,
6132 		    "qla_target(%d): Allocation of tmp FC port failed",
6133 		    vha->vp_idx);
6134 		return NULL;
6135 	}
6136 
6137 	fcport->loop_id = loop_id;
6138 
6139 	rc = qla24xx_gpdb_wait(vha, fcport, 0);
6140 	if (rc != QLA_SUCCESS) {
6141 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf070,
6142 		    "qla_target(%d): Failed to retrieve fcport "
6143 		    "information -- get_port_database() returned %x "
6144 		    "(loop_id=0x%04x)", vha->vp_idx, rc, loop_id);
6145 		kfree(fcport);
6146 		return NULL;
6147 	}
6148 
6149 	del = NULL;
6150 	spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
6151 	tfcp = qla2x00_find_fcport_by_wwpn(vha, fcport->port_name, 1);
6152 
6153 	if (tfcp) {
6154 		tfcp->d_id = fcport->d_id;
6155 		tfcp->port_type = fcport->port_type;
6156 		tfcp->supported_classes = fcport->supported_classes;
6157 		tfcp->flags |= fcport->flags;
6158 		tfcp->scan_state = QLA_FCPORT_FOUND;
6159 
6160 		del = fcport;
6161 		fcport = tfcp;
6162 	} else {
6163 		if (vha->hw->current_topology == ISP_CFG_F)
6164 			fcport->flags |= FCF_FABRIC_DEVICE;
6165 
6166 		list_add_tail(&fcport->list, &vha->vp_fcports);
6167 		if (!IS_SW_RESV_ADDR(fcport->d_id))
6168 		   vha->fcport_count++;
6169 		fcport->login_gen++;
6170 		qla2x00_set_fcport_disc_state(fcport, DSC_LOGIN_COMPLETE);
6171 		fcport->login_succ = 1;
6172 		newfcport = 1;
6173 	}
6174 
6175 	fcport->deleted = 0;
6176 	spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
6177 
6178 	switch (vha->host->active_mode) {
6179 	case MODE_INITIATOR:
6180 	case MODE_DUAL:
6181 		if (newfcport) {
6182 			if (!IS_IIDMA_CAPABLE(vha->hw) || !vha->hw->flags.gpsc_supported) {
6183 				qla24xx_sched_upd_fcport(fcport);
6184 			} else {
6185 				ql_dbg(ql_dbg_disc, vha, 0x20ff,
6186 				   "%s %d %8phC post gpsc fcp_cnt %d\n",
6187 				   __func__, __LINE__, fcport->port_name, vha->fcport_count);
6188 				qla24xx_post_gpsc_work(vha, fcport);
6189 			}
6190 		}
6191 		break;
6192 
6193 	case MODE_TARGET:
6194 	default:
6195 		break;
6196 	}
6197 	if (del)
6198 		qla2x00_free_fcport(del);
6199 
6200 	return fcport;
6201 }
6202 
6203 /* Must be called under tgt_mutex */
6204 static struct fc_port *qlt_make_local_sess(struct scsi_qla_host *vha,
6205 					   be_id_t s_id)
6206 {
6207 	struct fc_port *sess = NULL;
6208 	fc_port_t *fcport = NULL;
6209 	int rc, global_resets;
6210 	uint16_t loop_id = 0;
6211 
6212 	if (s_id.domain == 0xFF && s_id.area == 0xFC) {
6213 		/*
6214 		 * This is Domain Controller, so it should be
6215 		 * OK to drop SCSI commands from it.
6216 		 */
6217 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf042,
6218 		    "Unable to find initiator with S_ID %x:%x:%x",
6219 		    s_id.domain, s_id.area, s_id.al_pa);
6220 		return NULL;
6221 	}
6222 
6223 	mutex_lock(&vha->vha_tgt.tgt_mutex);
6224 
6225 retry:
6226 	global_resets =
6227 	    atomic_read(&vha->vha_tgt.qla_tgt->tgt_global_resets_count);
6228 
6229 	rc = qla24xx_get_loop_id(vha, s_id, &loop_id);
6230 	if (rc != 0) {
6231 		mutex_unlock(&vha->vha_tgt.tgt_mutex);
6232 
6233 		ql_log(ql_log_info, vha, 0xf071,
6234 		    "qla_target(%d): Unable to find "
6235 		    "initiator with S_ID %x:%x:%x",
6236 		    vha->vp_idx, s_id.domain, s_id.area, s_id.al_pa);
6237 
6238 		if (rc == -ENOENT) {
6239 			qlt_port_logo_t logo;
6240 
6241 			logo.id = be_to_port_id(s_id);
6242 			logo.cmd_count = 1;
6243 			qlt_send_first_logo(vha, &logo);
6244 		}
6245 
6246 		return NULL;
6247 	}
6248 
6249 	fcport = qlt_get_port_database(vha, loop_id);
6250 	if (!fcport) {
6251 		mutex_unlock(&vha->vha_tgt.tgt_mutex);
6252 		return NULL;
6253 	}
6254 
6255 	if (global_resets !=
6256 	    atomic_read(&vha->vha_tgt.qla_tgt->tgt_global_resets_count)) {
6257 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf043,
6258 		    "qla_target(%d): global reset during session discovery "
6259 		    "(counter was %d, new %d), retrying", vha->vp_idx,
6260 		    global_resets,
6261 		    atomic_read(&vha->vha_tgt.
6262 			qla_tgt->tgt_global_resets_count));
6263 		goto retry;
6264 	}
6265 
6266 	sess = qlt_create_sess(vha, fcport, true);
6267 
6268 	mutex_unlock(&vha->vha_tgt.tgt_mutex);
6269 
6270 	return sess;
6271 }
6272 
6273 static void qlt_abort_work(struct qla_tgt *tgt,
6274 	struct qla_tgt_sess_work_param *prm)
6275 {
6276 	struct scsi_qla_host *vha = tgt->vha;
6277 	struct qla_hw_data *ha = vha->hw;
6278 	struct fc_port *sess = NULL;
6279 	unsigned long flags = 0, flags2 = 0;
6280 	be_id_t s_id;
6281 	int rc;
6282 
6283 	spin_lock_irqsave(&ha->tgt.sess_lock, flags2);
6284 
6285 	if (tgt->tgt_stop)
6286 		goto out_term2;
6287 
6288 	s_id = le_id_to_be(prm->abts.fcp_hdr_le.s_id);
6289 
6290 	sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id);
6291 	if (!sess) {
6292 		spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
6293 
6294 		sess = qlt_make_local_sess(vha, s_id);
6295 		/* sess has got an extra creation ref */
6296 
6297 		spin_lock_irqsave(&ha->tgt.sess_lock, flags2);
6298 		if (!sess)
6299 			goto out_term2;
6300 	} else {
6301 		if (sess->deleted) {
6302 			sess = NULL;
6303 			goto out_term2;
6304 		}
6305 
6306 		if (!kref_get_unless_zero(&sess->sess_kref)) {
6307 			ql_dbg(ql_dbg_tgt_tmr, vha, 0xf01c,
6308 			    "%s: kref_get fail %8phC \n",
6309 			     __func__, sess->port_name);
6310 			sess = NULL;
6311 			goto out_term2;
6312 		}
6313 	}
6314 
6315 	rc = __qlt_24xx_handle_abts(vha, &prm->abts, sess);
6316 	spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
6317 
6318 	ha->tgt.tgt_ops->put_sess(sess);
6319 
6320 	if (rc != 0)
6321 		goto out_term;
6322 	return;
6323 
6324 out_term2:
6325 	spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
6326 
6327 out_term:
6328 	spin_lock_irqsave(&ha->hardware_lock, flags);
6329 	qlt_24xx_send_abts_resp(ha->base_qpair, &prm->abts,
6330 	    FCP_TMF_REJECTED, false);
6331 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
6332 }
6333 
6334 static void qlt_tmr_work(struct qla_tgt *tgt,
6335 	struct qla_tgt_sess_work_param *prm)
6336 {
6337 	struct atio_from_isp *a = &prm->tm_iocb2;
6338 	struct scsi_qla_host *vha = tgt->vha;
6339 	struct qla_hw_data *ha = vha->hw;
6340 	struct fc_port *sess;
6341 	unsigned long flags;
6342 	be_id_t s_id;
6343 	int rc;
6344 	u64 unpacked_lun;
6345 	int fn;
6346 	void *iocb;
6347 
6348 	spin_lock_irqsave(&ha->tgt.sess_lock, flags);
6349 
6350 	if (tgt->tgt_stop)
6351 		goto out_term2;
6352 
6353 	s_id = prm->tm_iocb2.u.isp24.fcp_hdr.s_id;
6354 	sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id);
6355 	if (!sess) {
6356 		spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
6357 
6358 		sess = qlt_make_local_sess(vha, s_id);
6359 		/* sess has got an extra creation ref */
6360 
6361 		spin_lock_irqsave(&ha->tgt.sess_lock, flags);
6362 		if (!sess)
6363 			goto out_term2;
6364 	} else {
6365 		if (sess->deleted) {
6366 			goto out_term2;
6367 		}
6368 
6369 		if (!kref_get_unless_zero(&sess->sess_kref)) {
6370 			ql_dbg(ql_dbg_tgt_tmr, vha, 0xf020,
6371 			    "%s: kref_get fail %8phC\n",
6372 			     __func__, sess->port_name);
6373 			goto out_term2;
6374 		}
6375 	}
6376 
6377 	iocb = a;
6378 	fn = a->u.isp24.fcp_cmnd.task_mgmt_flags;
6379 	unpacked_lun =
6380 	    scsilun_to_int((struct scsi_lun *)&a->u.isp24.fcp_cmnd.lun);
6381 
6382 	rc = qlt_issue_task_mgmt(sess, unpacked_lun, fn, iocb, 0);
6383 	spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
6384 
6385 	ha->tgt.tgt_ops->put_sess(sess);
6386 
6387 	if (rc != 0)
6388 		goto out_term;
6389 	return;
6390 
6391 out_term2:
6392 	spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
6393 out_term:
6394 	qlt_send_term_exchange(ha->base_qpair, NULL, &prm->tm_iocb2, 1, 0);
6395 }
6396 
6397 static void qlt_sess_work_fn(struct work_struct *work)
6398 {
6399 	struct qla_tgt *tgt = container_of(work, struct qla_tgt, sess_work);
6400 	struct scsi_qla_host *vha = tgt->vha;
6401 	unsigned long flags;
6402 
6403 	ql_dbg(ql_dbg_tgt_mgt, vha, 0xf000, "Sess work (tgt %p)", tgt);
6404 
6405 	spin_lock_irqsave(&tgt->sess_work_lock, flags);
6406 	while (!list_empty(&tgt->sess_works_list)) {
6407 		struct qla_tgt_sess_work_param *prm = list_entry(
6408 		    tgt->sess_works_list.next, typeof(*prm),
6409 		    sess_works_list_entry);
6410 
6411 		/*
6412 		 * This work can be scheduled on several CPUs at time, so we
6413 		 * must delete the entry to eliminate double processing
6414 		 */
6415 		list_del(&prm->sess_works_list_entry);
6416 
6417 		spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
6418 
6419 		switch (prm->type) {
6420 		case QLA_TGT_SESS_WORK_ABORT:
6421 			qlt_abort_work(tgt, prm);
6422 			break;
6423 		case QLA_TGT_SESS_WORK_TM:
6424 			qlt_tmr_work(tgt, prm);
6425 			break;
6426 		default:
6427 			BUG_ON(1);
6428 			break;
6429 		}
6430 
6431 		spin_lock_irqsave(&tgt->sess_work_lock, flags);
6432 
6433 		kfree(prm);
6434 	}
6435 	spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
6436 }
6437 
6438 /* Must be called under tgt_host_action_mutex */
6439 int qlt_add_target(struct qla_hw_data *ha, struct scsi_qla_host *base_vha)
6440 {
6441 	struct qla_tgt *tgt;
6442 	int rc, i;
6443 	struct qla_qpair_hint *h;
6444 
6445 	if (!QLA_TGT_MODE_ENABLED())
6446 		return 0;
6447 
6448 	if (!IS_TGT_MODE_CAPABLE(ha)) {
6449 		ql_log(ql_log_warn, base_vha, 0xe070,
6450 		    "This adapter does not support target mode.\n");
6451 		return 0;
6452 	}
6453 
6454 	ql_dbg(ql_dbg_tgt, base_vha, 0xe03b,
6455 	    "Registering target for host %ld(%p).\n", base_vha->host_no, ha);
6456 
6457 	BUG_ON(base_vha->vha_tgt.qla_tgt != NULL);
6458 
6459 	tgt = kzalloc(sizeof(struct qla_tgt), GFP_KERNEL);
6460 	if (!tgt) {
6461 		ql_dbg(ql_dbg_tgt, base_vha, 0xe066,
6462 		    "Unable to allocate struct qla_tgt\n");
6463 		return -ENOMEM;
6464 	}
6465 
6466 	tgt->qphints = kcalloc(ha->max_qpairs + 1,
6467 			       sizeof(struct qla_qpair_hint),
6468 			       GFP_KERNEL);
6469 	if (!tgt->qphints) {
6470 		kfree(tgt);
6471 		ql_log(ql_log_warn, base_vha, 0x0197,
6472 		    "Unable to allocate qpair hints.\n");
6473 		return -ENOMEM;
6474 	}
6475 
6476 	if (!(base_vha->host->hostt->supported_mode & MODE_TARGET))
6477 		base_vha->host->hostt->supported_mode |= MODE_TARGET;
6478 
6479 	rc = btree_init64(&tgt->lun_qpair_map);
6480 	if (rc) {
6481 		kfree(tgt->qphints);
6482 		kfree(tgt);
6483 		ql_log(ql_log_info, base_vha, 0x0198,
6484 			"Unable to initialize lun_qpair_map btree\n");
6485 		return -EIO;
6486 	}
6487 	h = &tgt->qphints[0];
6488 	h->qpair = ha->base_qpair;
6489 	INIT_LIST_HEAD(&h->hint_elem);
6490 	h->cpuid = ha->base_qpair->cpuid;
6491 	list_add_tail(&h->hint_elem, &ha->base_qpair->hints_list);
6492 
6493 	for (i = 0; i < ha->max_qpairs; i++) {
6494 		unsigned long flags;
6495 
6496 		struct qla_qpair *qpair = ha->queue_pair_map[i];
6497 
6498 		h = &tgt->qphints[i + 1];
6499 		INIT_LIST_HEAD(&h->hint_elem);
6500 		if (qpair) {
6501 			h->qpair = qpair;
6502 			spin_lock_irqsave(qpair->qp_lock_ptr, flags);
6503 			list_add_tail(&h->hint_elem, &qpair->hints_list);
6504 			spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
6505 			h->cpuid = qpair->cpuid;
6506 		}
6507 	}
6508 
6509 	tgt->ha = ha;
6510 	tgt->vha = base_vha;
6511 	init_waitqueue_head(&tgt->waitQ);
6512 	INIT_LIST_HEAD(&tgt->del_sess_list);
6513 	spin_lock_init(&tgt->sess_work_lock);
6514 	INIT_WORK(&tgt->sess_work, qlt_sess_work_fn);
6515 	INIT_LIST_HEAD(&tgt->sess_works_list);
6516 	atomic_set(&tgt->tgt_global_resets_count, 0);
6517 
6518 	base_vha->vha_tgt.qla_tgt = tgt;
6519 
6520 	ql_dbg(ql_dbg_tgt, base_vha, 0xe067,
6521 		"qla_target(%d): using 64 Bit PCI addressing",
6522 		base_vha->vp_idx);
6523 	/* 3 is reserved */
6524 	tgt->sg_tablesize = QLA_TGT_MAX_SG_24XX(base_vha->req->length - 3);
6525 
6526 	mutex_lock(&qla_tgt_mutex);
6527 	list_add_tail(&tgt->tgt_list_entry, &qla_tgt_glist);
6528 	mutex_unlock(&qla_tgt_mutex);
6529 
6530 	if (ha->tgt.tgt_ops && ha->tgt.tgt_ops->add_target)
6531 		ha->tgt.tgt_ops->add_target(base_vha);
6532 
6533 	return 0;
6534 }
6535 
6536 /* Must be called under tgt_host_action_mutex */
6537 int qlt_remove_target(struct qla_hw_data *ha, struct scsi_qla_host *vha)
6538 {
6539 	if (!vha->vha_tgt.qla_tgt)
6540 		return 0;
6541 
6542 	if (vha->fc_vport) {
6543 		qlt_release(vha->vha_tgt.qla_tgt);
6544 		return 0;
6545 	}
6546 
6547 	/* free left over qfull cmds */
6548 	qlt_init_term_exchange(vha);
6549 
6550 	ql_dbg(ql_dbg_tgt, vha, 0xe03c, "Unregistering target for host %ld(%p)",
6551 	    vha->host_no, ha);
6552 	qlt_release(vha->vha_tgt.qla_tgt);
6553 
6554 	return 0;
6555 }
6556 
6557 void qla_remove_hostmap(struct qla_hw_data *ha)
6558 {
6559 	struct scsi_qla_host *node;
6560 	u32 key = 0;
6561 
6562 	btree_for_each_safe32(&ha->host_map, key, node)
6563 		btree_remove32(&ha->host_map, key);
6564 
6565 	btree_destroy32(&ha->host_map);
6566 }
6567 
6568 static void qlt_lport_dump(struct scsi_qla_host *vha, u64 wwpn,
6569 	unsigned char *b)
6570 {
6571 	pr_debug("qla2xxx HW vha->node_name: %8phC\n", vha->node_name);
6572 	pr_debug("qla2xxx HW vha->port_name: %8phC\n", vha->port_name);
6573 	put_unaligned_be64(wwpn, b);
6574 	pr_debug("qla2xxx passed configfs WWPN: %8phC\n", b);
6575 }
6576 
6577 /**
6578  * qlt_lport_register - register lport with external module
6579  *
6580  * @target_lport_ptr: pointer for tcm_qla2xxx specific lport data
6581  * @phys_wwpn: physical port WWPN
6582  * @npiv_wwpn: NPIV WWPN
6583  * @npiv_wwnn: NPIV WWNN
6584  * @callback:  lport initialization callback for tcm_qla2xxx code
6585  */
6586 int qlt_lport_register(void *target_lport_ptr, u64 phys_wwpn,
6587 		       u64 npiv_wwpn, u64 npiv_wwnn,
6588 		       int (*callback)(struct scsi_qla_host *, void *, u64, u64))
6589 {
6590 	struct qla_tgt *tgt;
6591 	struct scsi_qla_host *vha;
6592 	struct qla_hw_data *ha;
6593 	struct Scsi_Host *host;
6594 	unsigned long flags;
6595 	int rc;
6596 	u8 b[WWN_SIZE];
6597 
6598 	mutex_lock(&qla_tgt_mutex);
6599 	list_for_each_entry(tgt, &qla_tgt_glist, tgt_list_entry) {
6600 		vha = tgt->vha;
6601 		ha = vha->hw;
6602 
6603 		host = vha->host;
6604 		if (!host)
6605 			continue;
6606 
6607 		if (!(host->hostt->supported_mode & MODE_TARGET))
6608 			continue;
6609 
6610 		if (vha->qlini_mode == QLA2XXX_INI_MODE_ENABLED)
6611 			continue;
6612 
6613 		spin_lock_irqsave(&ha->hardware_lock, flags);
6614 		if ((!npiv_wwpn || !npiv_wwnn) && host->active_mode & MODE_TARGET) {
6615 			pr_debug("MODE_TARGET already active on qla2xxx(%d)\n",
6616 			    host->host_no);
6617 			spin_unlock_irqrestore(&ha->hardware_lock, flags);
6618 			continue;
6619 		}
6620 		if (tgt->tgt_stop) {
6621 			pr_debug("MODE_TARGET in shutdown on qla2xxx(%d)\n",
6622 				 host->host_no);
6623 			spin_unlock_irqrestore(&ha->hardware_lock, flags);
6624 			continue;
6625 		}
6626 		spin_unlock_irqrestore(&ha->hardware_lock, flags);
6627 
6628 		if (!scsi_host_get(host)) {
6629 			ql_dbg(ql_dbg_tgt, vha, 0xe068,
6630 			    "Unable to scsi_host_get() for"
6631 			    " qla2xxx scsi_host\n");
6632 			continue;
6633 		}
6634 		qlt_lport_dump(vha, phys_wwpn, b);
6635 
6636 		if (memcmp(vha->port_name, b, WWN_SIZE)) {
6637 			scsi_host_put(host);
6638 			continue;
6639 		}
6640 		rc = (*callback)(vha, target_lport_ptr, npiv_wwpn, npiv_wwnn);
6641 		if (rc != 0)
6642 			scsi_host_put(host);
6643 
6644 		mutex_unlock(&qla_tgt_mutex);
6645 		return rc;
6646 	}
6647 	mutex_unlock(&qla_tgt_mutex);
6648 
6649 	return -ENODEV;
6650 }
6651 EXPORT_SYMBOL(qlt_lport_register);
6652 
6653 /**
6654  * qlt_lport_deregister - Degister lport
6655  *
6656  * @vha:  Registered scsi_qla_host pointer
6657  */
6658 void qlt_lport_deregister(struct scsi_qla_host *vha)
6659 {
6660 	struct qla_hw_data *ha = vha->hw;
6661 	struct Scsi_Host *sh = vha->host;
6662 	/*
6663 	 * Clear the target_lport_ptr qla_target_template pointer in qla_hw_data
6664 	 */
6665 	vha->vha_tgt.target_lport_ptr = NULL;
6666 	ha->tgt.tgt_ops = NULL;
6667 	/*
6668 	 * Release the Scsi_Host reference for the underlying qla2xxx host
6669 	 */
6670 	scsi_host_put(sh);
6671 }
6672 EXPORT_SYMBOL(qlt_lport_deregister);
6673 
6674 /* Must be called under HW lock */
6675 void qlt_set_mode(struct scsi_qla_host *vha)
6676 {
6677 	switch (vha->qlini_mode) {
6678 	case QLA2XXX_INI_MODE_DISABLED:
6679 	case QLA2XXX_INI_MODE_EXCLUSIVE:
6680 		vha->host->active_mode = MODE_TARGET;
6681 		break;
6682 	case QLA2XXX_INI_MODE_ENABLED:
6683 		vha->host->active_mode = MODE_INITIATOR;
6684 		break;
6685 	case QLA2XXX_INI_MODE_DUAL:
6686 		vha->host->active_mode = MODE_DUAL;
6687 		break;
6688 	default:
6689 		break;
6690 	}
6691 }
6692 
6693 /* Must be called under HW lock */
6694 static void qlt_clear_mode(struct scsi_qla_host *vha)
6695 {
6696 	switch (vha->qlini_mode) {
6697 	case QLA2XXX_INI_MODE_DISABLED:
6698 		vha->host->active_mode = MODE_UNKNOWN;
6699 		break;
6700 	case QLA2XXX_INI_MODE_EXCLUSIVE:
6701 		vha->host->active_mode = MODE_INITIATOR;
6702 		break;
6703 	case QLA2XXX_INI_MODE_ENABLED:
6704 	case QLA2XXX_INI_MODE_DUAL:
6705 		vha->host->active_mode = MODE_INITIATOR;
6706 		break;
6707 	default:
6708 		break;
6709 	}
6710 }
6711 
6712 /*
6713  * qla_tgt_enable_vha - NO LOCK HELD
6714  *
6715  * host_reset, bring up w/ Target Mode Enabled
6716  */
6717 void
6718 qlt_enable_vha(struct scsi_qla_host *vha)
6719 {
6720 	struct qla_hw_data *ha = vha->hw;
6721 	struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
6722 	unsigned long flags;
6723 	scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
6724 
6725 	if (!tgt) {
6726 		ql_dbg(ql_dbg_tgt, vha, 0xe069,
6727 		    "Unable to locate qla_tgt pointer from"
6728 		    " struct qla_hw_data\n");
6729 		dump_stack();
6730 		return;
6731 	}
6732 	if (vha->qlini_mode == QLA2XXX_INI_MODE_ENABLED)
6733 		return;
6734 
6735 	if (ha->tgt.num_act_qpairs > ha->max_qpairs)
6736 		ha->tgt.num_act_qpairs = ha->max_qpairs;
6737 	spin_lock_irqsave(&ha->hardware_lock, flags);
6738 	tgt->tgt_stopped = 0;
6739 	qlt_set_mode(vha);
6740 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
6741 
6742 	mutex_lock(&ha->optrom_mutex);
6743 	ql_dbg(ql_dbg_tgt_mgt, vha, 0xf021,
6744 	    "%s.\n", __func__);
6745 	if (vha->vp_idx) {
6746 		qla24xx_disable_vp(vha);
6747 		qla24xx_enable_vp(vha);
6748 	} else {
6749 		set_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags);
6750 		qla2xxx_wake_dpc(base_vha);
6751 		WARN_ON_ONCE(qla2x00_wait_for_hba_online(base_vha) !=
6752 			     QLA_SUCCESS);
6753 	}
6754 	mutex_unlock(&ha->optrom_mutex);
6755 }
6756 EXPORT_SYMBOL(qlt_enable_vha);
6757 
6758 /*
6759  * qla_tgt_disable_vha - NO LOCK HELD
6760  *
6761  * Disable Target Mode and reset the adapter
6762  */
6763 static void qlt_disable_vha(struct scsi_qla_host *vha)
6764 {
6765 	struct qla_hw_data *ha = vha->hw;
6766 	struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
6767 	unsigned long flags;
6768 
6769 	if (!tgt) {
6770 		ql_dbg(ql_dbg_tgt, vha, 0xe06a,
6771 		    "Unable to locate qla_tgt pointer from"
6772 		    " struct qla_hw_data\n");
6773 		dump_stack();
6774 		return;
6775 	}
6776 
6777 	spin_lock_irqsave(&ha->hardware_lock, flags);
6778 	qlt_clear_mode(vha);
6779 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
6780 
6781 	set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
6782 	qla2xxx_wake_dpc(vha);
6783 
6784 	/*
6785 	 * We are expecting the offline state.
6786 	 * QLA_FUNCTION_FAILED means that adapter is offline.
6787 	 */
6788 	if (qla2x00_wait_for_hba_online(vha) != QLA_SUCCESS)
6789 		ql_dbg(ql_dbg_tgt, vha, 0xe081,
6790 		       "adapter is offline\n");
6791 }
6792 
6793 /*
6794  * Called from qla_init.c:qla24xx_vport_create() contex to setup
6795  * the target mode specific struct scsi_qla_host and struct qla_hw_data
6796  * members.
6797  */
6798 void
6799 qlt_vport_create(struct scsi_qla_host *vha, struct qla_hw_data *ha)
6800 {
6801 	vha->vha_tgt.qla_tgt = NULL;
6802 
6803 	mutex_init(&vha->vha_tgt.tgt_mutex);
6804 	mutex_init(&vha->vha_tgt.tgt_host_action_mutex);
6805 
6806 	qlt_clear_mode(vha);
6807 
6808 	/*
6809 	 * NOTE: Currently the value is kept the same for <24xx and
6810 	 * >=24xx ISPs. If it is necessary to change it,
6811 	 * the check should be added for specific ISPs,
6812 	 * assigning the value appropriately.
6813 	 */
6814 	ha->tgt.atio_q_length = ATIO_ENTRY_CNT_24XX;
6815 
6816 	qlt_add_target(ha, vha);
6817 }
6818 
6819 u8
6820 qlt_rff_id(struct scsi_qla_host *vha)
6821 {
6822 	u8 fc4_feature = 0;
6823 	/*
6824 	 * FC-4 Feature bit 0 indicates target functionality to the name server.
6825 	 */
6826 	if (qla_tgt_mode_enabled(vha)) {
6827 		fc4_feature = BIT_0;
6828 	} else if (qla_ini_mode_enabled(vha)) {
6829 		fc4_feature = BIT_1;
6830 	} else if (qla_dual_mode_enabled(vha))
6831 		fc4_feature = BIT_0 | BIT_1;
6832 
6833 	return fc4_feature;
6834 }
6835 
6836 /*
6837  * qlt_init_atio_q_entries() - Initializes ATIO queue entries.
6838  * @ha: HA context
6839  *
6840  * Beginning of ATIO ring has initialization control block already built
6841  * by nvram config routine.
6842  *
6843  * Returns 0 on success.
6844  */
6845 void
6846 qlt_init_atio_q_entries(struct scsi_qla_host *vha)
6847 {
6848 	struct qla_hw_data *ha = vha->hw;
6849 	uint16_t cnt;
6850 	struct atio_from_isp *pkt = (struct atio_from_isp *)ha->tgt.atio_ring;
6851 
6852 	if (qla_ini_mode_enabled(vha))
6853 		return;
6854 
6855 	for (cnt = 0; cnt < ha->tgt.atio_q_length; cnt++) {
6856 		pkt->u.raw.signature = cpu_to_le32(ATIO_PROCESSED);
6857 		pkt++;
6858 	}
6859 
6860 }
6861 
6862 /*
6863  * qlt_24xx_process_atio_queue() - Process ATIO queue entries.
6864  * @ha: SCSI driver HA context
6865  */
6866 void
6867 qlt_24xx_process_atio_queue(struct scsi_qla_host *vha, uint8_t ha_locked)
6868 {
6869 	struct qla_hw_data *ha = vha->hw;
6870 	struct atio_from_isp *pkt;
6871 	int cnt, i;
6872 
6873 	if (!ha->flags.fw_started)
6874 		return;
6875 
6876 	while ((ha->tgt.atio_ring_ptr->signature != ATIO_PROCESSED) ||
6877 	    fcpcmd_is_corrupted(ha->tgt.atio_ring_ptr)) {
6878 		pkt = (struct atio_from_isp *)ha->tgt.atio_ring_ptr;
6879 		cnt = pkt->u.raw.entry_count;
6880 
6881 		if (unlikely(fcpcmd_is_corrupted(ha->tgt.atio_ring_ptr))) {
6882 			/*
6883 			 * This packet is corrupted. The header + payload
6884 			 * can not be trusted. There is no point in passing
6885 			 * it further up.
6886 			 */
6887 			ql_log(ql_log_warn, vha, 0xd03c,
6888 			    "corrupted fcp frame SID[%3phN] OXID[%04x] EXCG[%x] %64phN\n",
6889 			    &pkt->u.isp24.fcp_hdr.s_id,
6890 			    be16_to_cpu(pkt->u.isp24.fcp_hdr.ox_id),
6891 			    pkt->u.isp24.exchange_addr, pkt);
6892 
6893 			adjust_corrupted_atio(pkt);
6894 			qlt_send_term_exchange(ha->base_qpair, NULL, pkt,
6895 			    ha_locked, 0);
6896 		} else {
6897 			qlt_24xx_atio_pkt_all_vps(vha,
6898 			    (struct atio_from_isp *)pkt, ha_locked);
6899 		}
6900 
6901 		for (i = 0; i < cnt; i++) {
6902 			ha->tgt.atio_ring_index++;
6903 			if (ha->tgt.atio_ring_index == ha->tgt.atio_q_length) {
6904 				ha->tgt.atio_ring_index = 0;
6905 				ha->tgt.atio_ring_ptr = ha->tgt.atio_ring;
6906 			} else
6907 				ha->tgt.atio_ring_ptr++;
6908 
6909 			pkt->u.raw.signature = cpu_to_le32(ATIO_PROCESSED);
6910 			pkt = (struct atio_from_isp *)ha->tgt.atio_ring_ptr;
6911 		}
6912 		wmb();
6913 	}
6914 
6915 	/* Adjust ring index */
6916 	wrt_reg_dword(ISP_ATIO_Q_OUT(vha), ha->tgt.atio_ring_index);
6917 }
6918 
6919 void
6920 qlt_24xx_config_rings(struct scsi_qla_host *vha)
6921 {
6922 	struct qla_hw_data *ha = vha->hw;
6923 	struct qla_msix_entry *msix = &ha->msix_entries[2];
6924 	struct init_cb_24xx *icb = (struct init_cb_24xx *)ha->init_cb;
6925 
6926 	if (!QLA_TGT_MODE_ENABLED())
6927 		return;
6928 
6929 	wrt_reg_dword(ISP_ATIO_Q_IN(vha), 0);
6930 	wrt_reg_dword(ISP_ATIO_Q_OUT(vha), 0);
6931 	rd_reg_dword(ISP_ATIO_Q_OUT(vha));
6932 
6933 	if (ha->flags.msix_enabled) {
6934 		if (IS_QLA83XX(ha) || IS_QLA27XX(ha) || IS_QLA28XX(ha)) {
6935 			if (IS_QLA2071(ha)) {
6936 				/* 4 ports Baker: Enable Interrupt Handshake */
6937 				icb->msix_atio = 0;
6938 				icb->firmware_options_2 |= cpu_to_le32(BIT_26);
6939 			} else {
6940 				icb->msix_atio = cpu_to_le16(msix->entry);
6941 				icb->firmware_options_2 &= cpu_to_le32(~BIT_26);
6942 			}
6943 			ql_dbg(ql_dbg_init, vha, 0xf072,
6944 			    "Registering ICB vector 0x%x for atio que.\n",
6945 			    msix->entry);
6946 		}
6947 	} else {
6948 		/* INTx|MSI */
6949 		if (IS_QLA83XX(ha) || IS_QLA27XX(ha) || IS_QLA28XX(ha)) {
6950 			icb->msix_atio = 0;
6951 			icb->firmware_options_2 |= cpu_to_le32(BIT_26);
6952 			ql_dbg(ql_dbg_init, vha, 0xf072,
6953 			    "%s: Use INTx for ATIOQ.\n", __func__);
6954 		}
6955 	}
6956 }
6957 
6958 void
6959 qlt_24xx_config_nvram_stage1(struct scsi_qla_host *vha, struct nvram_24xx *nv)
6960 {
6961 	struct qla_hw_data *ha = vha->hw;
6962 	u32 tmp;
6963 
6964 	if (!QLA_TGT_MODE_ENABLED())
6965 		return;
6966 
6967 	if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)) {
6968 		if (!ha->tgt.saved_set) {
6969 			/* We save only once */
6970 			ha->tgt.saved_exchange_count = nv->exchange_count;
6971 			ha->tgt.saved_firmware_options_1 =
6972 			    nv->firmware_options_1;
6973 			ha->tgt.saved_firmware_options_2 =
6974 			    nv->firmware_options_2;
6975 			ha->tgt.saved_firmware_options_3 =
6976 			    nv->firmware_options_3;
6977 			ha->tgt.saved_set = 1;
6978 		}
6979 
6980 		if (qla_tgt_mode_enabled(vha))
6981 			nv->exchange_count = cpu_to_le16(0xFFFF);
6982 		else			/* dual */
6983 			nv->exchange_count = cpu_to_le16(vha->ql2xexchoffld);
6984 
6985 		/* Enable target mode */
6986 		nv->firmware_options_1 |= cpu_to_le32(BIT_4);
6987 
6988 		/* Disable ini mode, if requested */
6989 		if (qla_tgt_mode_enabled(vha))
6990 			nv->firmware_options_1 |= cpu_to_le32(BIT_5);
6991 
6992 		/* Disable Full Login after LIP */
6993 		nv->firmware_options_1 &= cpu_to_le32(~BIT_13);
6994 		/* Enable initial LIP */
6995 		nv->firmware_options_1 &= cpu_to_le32(~BIT_9);
6996 		if (ql2xtgt_tape_enable)
6997 			/* Enable FC Tape support */
6998 			nv->firmware_options_2 |= cpu_to_le32(BIT_12);
6999 		else
7000 			/* Disable FC Tape support */
7001 			nv->firmware_options_2 &= cpu_to_le32(~BIT_12);
7002 
7003 		/* Disable Full Login after LIP */
7004 		nv->host_p &= cpu_to_le32(~BIT_10);
7005 
7006 		/*
7007 		 * clear BIT 15 explicitly as we have seen at least
7008 		 * a couple of instances where this was set and this
7009 		 * was causing the firmware to not be initialized.
7010 		 */
7011 		nv->firmware_options_1 &= cpu_to_le32(~BIT_15);
7012 		/* Enable target PRLI control */
7013 		nv->firmware_options_2 |= cpu_to_le32(BIT_14);
7014 
7015 		if (IS_QLA25XX(ha)) {
7016 			/* Change Loop-prefer to Pt-Pt */
7017 			tmp = ~(BIT_4|BIT_5|BIT_6);
7018 			nv->firmware_options_2 &= cpu_to_le32(tmp);
7019 			tmp = P2P << 4;
7020 			nv->firmware_options_2 |= cpu_to_le32(tmp);
7021 		}
7022 	} else {
7023 		if (ha->tgt.saved_set) {
7024 			nv->exchange_count = ha->tgt.saved_exchange_count;
7025 			nv->firmware_options_1 =
7026 			    ha->tgt.saved_firmware_options_1;
7027 			nv->firmware_options_2 =
7028 			    ha->tgt.saved_firmware_options_2;
7029 			nv->firmware_options_3 =
7030 			    ha->tgt.saved_firmware_options_3;
7031 		}
7032 		return;
7033 	}
7034 
7035 	if (ha->base_qpair->enable_class_2) {
7036 		if (vha->flags.init_done)
7037 			fc_host_supported_classes(vha->host) =
7038 				FC_COS_CLASS2 | FC_COS_CLASS3;
7039 
7040 		nv->firmware_options_2 |= cpu_to_le32(BIT_8);
7041 	} else {
7042 		if (vha->flags.init_done)
7043 			fc_host_supported_classes(vha->host) = FC_COS_CLASS3;
7044 
7045 		nv->firmware_options_2 &= ~cpu_to_le32(BIT_8);
7046 	}
7047 }
7048 
7049 void
7050 qlt_24xx_config_nvram_stage2(struct scsi_qla_host *vha,
7051 	struct init_cb_24xx *icb)
7052 {
7053 	struct qla_hw_data *ha = vha->hw;
7054 
7055 	if (!QLA_TGT_MODE_ENABLED())
7056 		return;
7057 
7058 	if (ha->tgt.node_name_set) {
7059 		memcpy(icb->node_name, ha->tgt.tgt_node_name, WWN_SIZE);
7060 		icb->firmware_options_1 |= cpu_to_le32(BIT_14);
7061 	}
7062 }
7063 
7064 void
7065 qlt_81xx_config_nvram_stage1(struct scsi_qla_host *vha, struct nvram_81xx *nv)
7066 {
7067 	struct qla_hw_data *ha = vha->hw;
7068 	u32 tmp;
7069 
7070 	if (!QLA_TGT_MODE_ENABLED())
7071 		return;
7072 
7073 	if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)) {
7074 		if (!ha->tgt.saved_set) {
7075 			/* We save only once */
7076 			ha->tgt.saved_exchange_count = nv->exchange_count;
7077 			ha->tgt.saved_firmware_options_1 =
7078 			    nv->firmware_options_1;
7079 			ha->tgt.saved_firmware_options_2 =
7080 			    nv->firmware_options_2;
7081 			ha->tgt.saved_firmware_options_3 =
7082 			    nv->firmware_options_3;
7083 			ha->tgt.saved_set = 1;
7084 		}
7085 
7086 		if (qla_tgt_mode_enabled(vha))
7087 			nv->exchange_count = cpu_to_le16(0xFFFF);
7088 		else			/* dual */
7089 			nv->exchange_count = cpu_to_le16(vha->ql2xexchoffld);
7090 
7091 		/* Enable target mode */
7092 		nv->firmware_options_1 |= cpu_to_le32(BIT_4);
7093 
7094 		/* Disable ini mode, if requested */
7095 		if (qla_tgt_mode_enabled(vha))
7096 			nv->firmware_options_1 |= cpu_to_le32(BIT_5);
7097 		/* Disable Full Login after LIP */
7098 		nv->firmware_options_1 &= cpu_to_le32(~BIT_13);
7099 		/* Enable initial LIP */
7100 		nv->firmware_options_1 &= cpu_to_le32(~BIT_9);
7101 		/*
7102 		 * clear BIT 15 explicitly as we have seen at
7103 		 * least a couple of instances where this was set
7104 		 * and this was causing the firmware to not be
7105 		 * initialized.
7106 		 */
7107 		nv->firmware_options_1 &= cpu_to_le32(~BIT_15);
7108 		if (ql2xtgt_tape_enable)
7109 			/* Enable FC tape support */
7110 			nv->firmware_options_2 |= cpu_to_le32(BIT_12);
7111 		else
7112 			/* Disable FC tape support */
7113 			nv->firmware_options_2 &= cpu_to_le32(~BIT_12);
7114 
7115 		/* Disable Full Login after LIP */
7116 		nv->host_p &= cpu_to_le32(~BIT_10);
7117 		/* Enable target PRLI control */
7118 		nv->firmware_options_2 |= cpu_to_le32(BIT_14);
7119 
7120 		/* Change Loop-prefer to Pt-Pt */
7121 		tmp = ~(BIT_4|BIT_5|BIT_6);
7122 		nv->firmware_options_2 &= cpu_to_le32(tmp);
7123 		tmp = P2P << 4;
7124 		nv->firmware_options_2 |= cpu_to_le32(tmp);
7125 	} else {
7126 		if (ha->tgt.saved_set) {
7127 			nv->exchange_count = ha->tgt.saved_exchange_count;
7128 			nv->firmware_options_1 =
7129 			    ha->tgt.saved_firmware_options_1;
7130 			nv->firmware_options_2 =
7131 			    ha->tgt.saved_firmware_options_2;
7132 			nv->firmware_options_3 =
7133 			    ha->tgt.saved_firmware_options_3;
7134 		}
7135 		return;
7136 	}
7137 
7138 	if (ha->base_qpair->enable_class_2) {
7139 		if (vha->flags.init_done)
7140 			fc_host_supported_classes(vha->host) =
7141 				FC_COS_CLASS2 | FC_COS_CLASS3;
7142 
7143 		nv->firmware_options_2 |= cpu_to_le32(BIT_8);
7144 	} else {
7145 		if (vha->flags.init_done)
7146 			fc_host_supported_classes(vha->host) = FC_COS_CLASS3;
7147 
7148 		nv->firmware_options_2 &= ~cpu_to_le32(BIT_8);
7149 	}
7150 }
7151 
7152 void
7153 qlt_81xx_config_nvram_stage2(struct scsi_qla_host *vha,
7154 	struct init_cb_81xx *icb)
7155 {
7156 	struct qla_hw_data *ha = vha->hw;
7157 
7158 	if (!QLA_TGT_MODE_ENABLED())
7159 		return;
7160 
7161 	if (ha->tgt.node_name_set) {
7162 		memcpy(icb->node_name, ha->tgt.tgt_node_name, WWN_SIZE);
7163 		icb->firmware_options_1 |= cpu_to_le32(BIT_14);
7164 	}
7165 }
7166 
7167 void
7168 qlt_83xx_iospace_config(struct qla_hw_data *ha)
7169 {
7170 	if (!QLA_TGT_MODE_ENABLED())
7171 		return;
7172 
7173 	ha->msix_count += 1; /* For ATIO Q */
7174 }
7175 
7176 
7177 void
7178 qlt_modify_vp_config(struct scsi_qla_host *vha,
7179 	struct vp_config_entry_24xx *vpmod)
7180 {
7181 	/* enable target mode.  Bit5 = 1 => disable */
7182 	if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha))
7183 		vpmod->options_idx1 &= ~BIT_5;
7184 
7185 	/* Disable ini mode, if requested.  bit4 = 1 => disable */
7186 	if (qla_tgt_mode_enabled(vha))
7187 		vpmod->options_idx1 &= ~BIT_4;
7188 }
7189 
7190 void
7191 qlt_probe_one_stage1(struct scsi_qla_host *base_vha, struct qla_hw_data *ha)
7192 {
7193 	mutex_init(&base_vha->vha_tgt.tgt_mutex);
7194 	if (!QLA_TGT_MODE_ENABLED())
7195 		return;
7196 
7197 	if  (ha->mqenable || IS_QLA83XX(ha) || IS_QLA27XX(ha) || IS_QLA28XX(ha)) {
7198 		ISP_ATIO_Q_IN(base_vha) = &ha->mqiobase->isp25mq.atio_q_in;
7199 		ISP_ATIO_Q_OUT(base_vha) = &ha->mqiobase->isp25mq.atio_q_out;
7200 	} else {
7201 		ISP_ATIO_Q_IN(base_vha) = &ha->iobase->isp24.atio_q_in;
7202 		ISP_ATIO_Q_OUT(base_vha) = &ha->iobase->isp24.atio_q_out;
7203 	}
7204 
7205 	mutex_init(&base_vha->vha_tgt.tgt_host_action_mutex);
7206 
7207 	INIT_LIST_HEAD(&base_vha->unknown_atio_list);
7208 	INIT_DELAYED_WORK(&base_vha->unknown_atio_work,
7209 	    qlt_unknown_atio_work_fn);
7210 
7211 	qlt_clear_mode(base_vha);
7212 
7213 	qlt_update_vp_map(base_vha, SET_VP_IDX);
7214 }
7215 
7216 irqreturn_t
7217 qla83xx_msix_atio_q(int irq, void *dev_id)
7218 {
7219 	struct rsp_que *rsp;
7220 	scsi_qla_host_t	*vha;
7221 	struct qla_hw_data *ha;
7222 	unsigned long flags;
7223 
7224 	rsp = (struct rsp_que *) dev_id;
7225 	ha = rsp->hw;
7226 	vha = pci_get_drvdata(ha->pdev);
7227 
7228 	spin_lock_irqsave(&ha->tgt.atio_lock, flags);
7229 
7230 	qlt_24xx_process_atio_queue(vha, 0);
7231 
7232 	spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
7233 
7234 	return IRQ_HANDLED;
7235 }
7236 
7237 static void
7238 qlt_handle_abts_recv_work(struct work_struct *work)
7239 {
7240 	struct qla_tgt_sess_op *op = container_of(work,
7241 		struct qla_tgt_sess_op, work);
7242 	scsi_qla_host_t *vha = op->vha;
7243 	struct qla_hw_data *ha = vha->hw;
7244 	unsigned long flags;
7245 
7246 	if (qla2x00_reset_active(vha) ||
7247 	    (op->chip_reset != ha->base_qpair->chip_reset))
7248 		return;
7249 
7250 	spin_lock_irqsave(&ha->tgt.atio_lock, flags);
7251 	qlt_24xx_process_atio_queue(vha, 0);
7252 	spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
7253 
7254 	spin_lock_irqsave(&ha->hardware_lock, flags);
7255 	qlt_response_pkt_all_vps(vha, op->rsp, (response_t *)&op->atio);
7256 	spin_unlock_irqrestore(&ha->hardware_lock, flags);
7257 
7258 	kfree(op);
7259 }
7260 
7261 void
7262 qlt_handle_abts_recv(struct scsi_qla_host *vha, struct rsp_que *rsp,
7263     response_t *pkt)
7264 {
7265 	struct qla_tgt_sess_op *op;
7266 
7267 	op = kzalloc(sizeof(*op), GFP_ATOMIC);
7268 
7269 	if (!op) {
7270 		/* do not reach for ATIO queue here.  This is best effort err
7271 		 * recovery at this point.
7272 		 */
7273 		qlt_response_pkt_all_vps(vha, rsp, pkt);
7274 		return;
7275 	}
7276 
7277 	memcpy(&op->atio, pkt, sizeof(*pkt));
7278 	op->vha = vha;
7279 	op->chip_reset = vha->hw->base_qpair->chip_reset;
7280 	op->rsp = rsp;
7281 	INIT_WORK(&op->work, qlt_handle_abts_recv_work);
7282 	queue_work(qla_tgt_wq, &op->work);
7283 	return;
7284 }
7285 
7286 int
7287 qlt_mem_alloc(struct qla_hw_data *ha)
7288 {
7289 	if (!QLA_TGT_MODE_ENABLED())
7290 		return 0;
7291 
7292 	ha->tgt.tgt_vp_map = kcalloc(MAX_MULTI_ID_FABRIC,
7293 				     sizeof(struct qla_tgt_vp_map),
7294 				     GFP_KERNEL);
7295 	if (!ha->tgt.tgt_vp_map)
7296 		return -ENOMEM;
7297 
7298 	ha->tgt.atio_ring = dma_alloc_coherent(&ha->pdev->dev,
7299 	    (ha->tgt.atio_q_length + 1) * sizeof(struct atio_from_isp),
7300 	    &ha->tgt.atio_dma, GFP_KERNEL);
7301 	if (!ha->tgt.atio_ring) {
7302 		kfree(ha->tgt.tgt_vp_map);
7303 		return -ENOMEM;
7304 	}
7305 	return 0;
7306 }
7307 
7308 void
7309 qlt_mem_free(struct qla_hw_data *ha)
7310 {
7311 	if (!QLA_TGT_MODE_ENABLED())
7312 		return;
7313 
7314 	if (ha->tgt.atio_ring) {
7315 		dma_free_coherent(&ha->pdev->dev, (ha->tgt.atio_q_length + 1) *
7316 		    sizeof(struct atio_from_isp), ha->tgt.atio_ring,
7317 		    ha->tgt.atio_dma);
7318 	}
7319 	ha->tgt.atio_ring = NULL;
7320 	ha->tgt.atio_dma = 0;
7321 	kfree(ha->tgt.tgt_vp_map);
7322 	ha->tgt.tgt_vp_map = NULL;
7323 }
7324 
7325 /* vport_slock to be held by the caller */
7326 void
7327 qlt_update_vp_map(struct scsi_qla_host *vha, int cmd)
7328 {
7329 	void *slot;
7330 	u32 key;
7331 	int rc;
7332 
7333 	key = vha->d_id.b24;
7334 
7335 	switch (cmd) {
7336 	case SET_VP_IDX:
7337 		if (!QLA_TGT_MODE_ENABLED())
7338 			return;
7339 		vha->hw->tgt.tgt_vp_map[vha->vp_idx].vha = vha;
7340 		break;
7341 	case SET_AL_PA:
7342 		slot = btree_lookup32(&vha->hw->host_map, key);
7343 		if (!slot) {
7344 			ql_dbg(ql_dbg_tgt_mgt, vha, 0xf018,
7345 			    "Save vha in host_map %p %06x\n", vha, key);
7346 			rc = btree_insert32(&vha->hw->host_map,
7347 				key, vha, GFP_ATOMIC);
7348 			if (rc)
7349 				ql_log(ql_log_info, vha, 0xd03e,
7350 				    "Unable to insert s_id into host_map: %06x\n",
7351 				    key);
7352 			return;
7353 		}
7354 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf019,
7355 		    "replace existing vha in host_map %p %06x\n", vha, key);
7356 		btree_update32(&vha->hw->host_map, key, vha);
7357 		break;
7358 	case RESET_VP_IDX:
7359 		if (!QLA_TGT_MODE_ENABLED())
7360 			return;
7361 		vha->hw->tgt.tgt_vp_map[vha->vp_idx].vha = NULL;
7362 		break;
7363 	case RESET_AL_PA:
7364 		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01a,
7365 		   "clear vha in host_map %p %06x\n", vha, key);
7366 		slot = btree_lookup32(&vha->hw->host_map, key);
7367 		if (slot)
7368 			btree_remove32(&vha->hw->host_map, key);
7369 		vha->d_id.b24 = 0;
7370 		break;
7371 	}
7372 }
7373 
7374 void qlt_update_host_map(struct scsi_qla_host *vha, port_id_t id)
7375 {
7376 
7377 	if (!vha->d_id.b24) {
7378 		vha->d_id = id;
7379 		qlt_update_vp_map(vha, SET_AL_PA);
7380 	} else if (vha->d_id.b24 != id.b24) {
7381 		qlt_update_vp_map(vha, RESET_AL_PA);
7382 		vha->d_id = id;
7383 		qlt_update_vp_map(vha, SET_AL_PA);
7384 	}
7385 }
7386 
7387 static int __init qlt_parse_ini_mode(void)
7388 {
7389 	if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_EXCLUSIVE) == 0)
7390 		ql2x_ini_mode = QLA2XXX_INI_MODE_EXCLUSIVE;
7391 	else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_DISABLED) == 0)
7392 		ql2x_ini_mode = QLA2XXX_INI_MODE_DISABLED;
7393 	else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_ENABLED) == 0)
7394 		ql2x_ini_mode = QLA2XXX_INI_MODE_ENABLED;
7395 	else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_DUAL) == 0)
7396 		ql2x_ini_mode = QLA2XXX_INI_MODE_DUAL;
7397 	else
7398 		return false;
7399 
7400 	return true;
7401 }
7402 
7403 int __init qlt_init(void)
7404 {
7405 	int ret;
7406 
7407 	BUILD_BUG_ON(sizeof(struct ctio7_to_24xx) != 64);
7408 	BUILD_BUG_ON(sizeof(struct ctio_to_2xxx) != 64);
7409 
7410 	if (!qlt_parse_ini_mode()) {
7411 		ql_log(ql_log_fatal, NULL, 0xe06b,
7412 		    "qlt_parse_ini_mode() failed\n");
7413 		return -EINVAL;
7414 	}
7415 
7416 	if (!QLA_TGT_MODE_ENABLED())
7417 		return 0;
7418 
7419 	qla_tgt_mgmt_cmd_cachep = kmem_cache_create("qla_tgt_mgmt_cmd_cachep",
7420 	    sizeof(struct qla_tgt_mgmt_cmd), __alignof__(struct
7421 	    qla_tgt_mgmt_cmd), 0, NULL);
7422 	if (!qla_tgt_mgmt_cmd_cachep) {
7423 		ql_log(ql_log_fatal, NULL, 0xd04b,
7424 		    "kmem_cache_create for qla_tgt_mgmt_cmd_cachep failed\n");
7425 		return -ENOMEM;
7426 	}
7427 
7428 	qla_tgt_plogi_cachep = kmem_cache_create("qla_tgt_plogi_cachep",
7429 	    sizeof(struct qlt_plogi_ack_t), __alignof__(struct qlt_plogi_ack_t),
7430 	    0, NULL);
7431 
7432 	if (!qla_tgt_plogi_cachep) {
7433 		ql_log(ql_log_fatal, NULL, 0xe06d,
7434 		    "kmem_cache_create for qla_tgt_plogi_cachep failed\n");
7435 		ret = -ENOMEM;
7436 		goto out_mgmt_cmd_cachep;
7437 	}
7438 
7439 	qla_tgt_mgmt_cmd_mempool = mempool_create(25, mempool_alloc_slab,
7440 	    mempool_free_slab, qla_tgt_mgmt_cmd_cachep);
7441 	if (!qla_tgt_mgmt_cmd_mempool) {
7442 		ql_log(ql_log_fatal, NULL, 0xe06e,
7443 		    "mempool_create for qla_tgt_mgmt_cmd_mempool failed\n");
7444 		ret = -ENOMEM;
7445 		goto out_plogi_cachep;
7446 	}
7447 
7448 	qla_tgt_wq = alloc_workqueue("qla_tgt_wq", 0, 0);
7449 	if (!qla_tgt_wq) {
7450 		ql_log(ql_log_fatal, NULL, 0xe06f,
7451 		    "alloc_workqueue for qla_tgt_wq failed\n");
7452 		ret = -ENOMEM;
7453 		goto out_cmd_mempool;
7454 	}
7455 	/*
7456 	 * Return 1 to signal that initiator-mode is being disabled
7457 	 */
7458 	return (ql2x_ini_mode == QLA2XXX_INI_MODE_DISABLED) ? 1 : 0;
7459 
7460 out_cmd_mempool:
7461 	mempool_destroy(qla_tgt_mgmt_cmd_mempool);
7462 out_plogi_cachep:
7463 	kmem_cache_destroy(qla_tgt_plogi_cachep);
7464 out_mgmt_cmd_cachep:
7465 	kmem_cache_destroy(qla_tgt_mgmt_cmd_cachep);
7466 	return ret;
7467 }
7468 
7469 void qlt_exit(void)
7470 {
7471 	if (!QLA_TGT_MODE_ENABLED())
7472 		return;
7473 
7474 	destroy_workqueue(qla_tgt_wq);
7475 	mempool_destroy(qla_tgt_mgmt_cmd_mempool);
7476 	kmem_cache_destroy(qla_tgt_plogi_cachep);
7477 	kmem_cache_destroy(qla_tgt_mgmt_cmd_cachep);
7478 }
7479