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