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