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