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