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