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