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