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