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